compiler: Do not dereference a named pointer type when looking up a method.
[official-gcc.git] / gcc / dwarf2out.c
blob3d50ac984f307cce0247a127bbe57485d2f353bc
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 "hashtab.h"
68 #include "hash-set.h"
69 #include "vec.h"
70 #include "machmode.h"
71 #include "hard-reg-set.h"
72 #include "input.h"
73 #include "function.h"
74 #include "emit-rtl.h"
75 #include "hash-table.h"
76 #include "version.h"
77 #include "flags.h"
78 #include "regs.h"
79 #include "rtlhash.h"
80 #include "insn-config.h"
81 #include "reload.h"
82 #include "output.h"
83 #include "expr.h"
84 #include "except.h"
85 #include "dwarf2.h"
86 #include "dwarf2out.h"
87 #include "dwarf2asm.h"
88 #include "toplev.h"
89 #include "md5.h"
90 #include "tm_p.h"
91 #include "diagnostic.h"
92 #include "tree-pretty-print.h"
93 #include "debug.h"
94 #include "target.h"
95 #include "common/common-target.h"
96 #include "langhooks.h"
97 #include "hash-map.h"
98 #include "is-a.h"
99 #include "plugin-api.h"
100 #include "ipa-ref.h"
101 #include "cgraph.h"
102 #include "ira.h"
103 #include "lra.h"
104 #include "dumpfile.h"
105 #include "opts.h"
106 #include "tree-dfa.h"
107 #include "gdb/gdb-index.h"
108 #include "rtl-iter.h"
110 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
111 static rtx_insn *last_var_location_insn;
112 static rtx_insn *cached_next_real_insn;
113 static void dwarf2out_decl (tree);
115 #ifdef VMS_DEBUGGING_INFO
116 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
118 /* Define this macro to be a nonzero value if the directory specifications
119 which are output in the debug info should end with a separator. */
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
121 /* Define this macro to evaluate to a nonzero value if GCC should refrain
122 from generating indirect strings in DWARF2 debug information, for instance
123 if your target is stuck with an old version of GDB that is unable to
124 process them properly or uses VMS Debug. */
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
126 #else
127 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
128 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
129 #endif
131 /* ??? Poison these here until it can be done generically. They've been
132 totally replaced in this file; make sure it stays that way. */
133 #undef DWARF2_UNWIND_INFO
134 #undef DWARF2_FRAME_INFO
135 #if (GCC_VERSION >= 3000)
136 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
137 #endif
139 /* The size of the target's pointer type. */
140 #ifndef PTR_SIZE
141 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
142 #endif
144 /* Array of RTXes referenced by the debugging information, which therefore
145 must be kept around forever. */
146 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
148 /* A pointer to the base of a list of incomplete types which might be
149 completed at some later time. incomplete_types_list needs to be a
150 vec<tree, va_gc> *because we want to tell the garbage collector about
151 it. */
152 static GTY(()) vec<tree, va_gc> *incomplete_types;
154 /* A pointer to the base of a table of references to declaration
155 scopes. This table is a display which tracks the nesting
156 of declaration scopes at the current scope and containing
157 scopes. This table is used to find the proper place to
158 define type declaration DIE's. */
159 static GTY(()) vec<tree, va_gc> *decl_scope_table;
161 /* Pointers to various DWARF2 sections. */
162 static GTY(()) section *debug_info_section;
163 static GTY(()) section *debug_skeleton_info_section;
164 static GTY(()) section *debug_abbrev_section;
165 static GTY(()) section *debug_skeleton_abbrev_section;
166 static GTY(()) section *debug_aranges_section;
167 static GTY(()) section *debug_addr_section;
168 static GTY(()) section *debug_macinfo_section;
169 static GTY(()) section *debug_line_section;
170 static GTY(()) section *debug_skeleton_line_section;
171 static GTY(()) section *debug_loc_section;
172 static GTY(()) section *debug_pubnames_section;
173 static GTY(()) section *debug_pubtypes_section;
174 static GTY(()) section *debug_str_section;
175 static GTY(()) section *debug_str_dwo_section;
176 static GTY(()) section *debug_str_offsets_section;
177 static GTY(()) section *debug_ranges_section;
178 static GTY(()) section *debug_frame_section;
180 /* Maximum size (in bytes) of an artificially generated label. */
181 #define MAX_ARTIFICIAL_LABEL_BYTES 30
183 /* According to the (draft) DWARF 3 specification, the initial length
184 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
185 bytes are 0xffffffff, followed by the length stored in the next 8
186 bytes.
188 However, the SGI/MIPS ABI uses an initial length which is equal to
189 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
191 #ifndef DWARF_INITIAL_LENGTH_SIZE
192 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
193 #endif
195 /* Round SIZE up to the nearest BOUNDARY. */
196 #define DWARF_ROUND(SIZE,BOUNDARY) \
197 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
199 /* CIE identifier. */
200 #if HOST_BITS_PER_WIDE_INT >= 64
201 #define DWARF_CIE_ID \
202 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
203 #else
204 #define DWARF_CIE_ID DW_CIE_ID
205 #endif
208 /* A vector for a table that contains frame description
209 information for each routine. */
210 #define NOT_INDEXED (-1U)
211 #define NO_INDEX_ASSIGNED (-2U)
213 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
215 struct GTY((for_user)) indirect_string_node {
216 const char *str;
217 unsigned int refcount;
218 enum dwarf_form form;
219 char *label;
220 unsigned int index;
223 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
225 typedef const char *compare_type;
227 static hashval_t hash (indirect_string_node *);
228 static bool equal (indirect_string_node *, const char *);
231 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
233 /* With split_debug_info, both the comp_dir and dwo_name go in the
234 main object file, rather than the dwo, similar to the force_direct
235 parameter elsewhere but with additional complications:
237 1) The string is needed in both the main object file and the dwo.
238 That is, the comp_dir and dwo_name will appear in both places.
240 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
241 DW_FORM_GNU_str_index.
243 3) GCC chooses the form to use late, depending on the size and
244 reference count.
246 Rather than forcing the all debug string handling functions and
247 callers to deal with these complications, simply use a separate,
248 special-cased string table for any attribute that should go in the
249 main object file. This limits the complexity to just the places
250 that need it. */
252 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
254 static GTY(()) int dw2_string_counter;
256 /* True if the compilation unit places functions in more than one section. */
257 static GTY(()) bool have_multiple_function_sections = false;
259 /* Whether the default text and cold text sections have been used at all. */
261 static GTY(()) bool text_section_used = false;
262 static GTY(()) bool cold_text_section_used = false;
264 /* The default cold text section. */
265 static GTY(()) section *cold_text_section;
267 /* The DIE for C++14 'auto' in a function return type. */
268 static GTY(()) dw_die_ref auto_die;
270 /* The DIE for C++14 'decltype(auto)' in a function return type. */
271 static GTY(()) dw_die_ref decltype_auto_die;
273 /* Forward declarations for functions defined in this file. */
275 static char *stripattributes (const char *);
276 static void output_call_frame_info (int);
277 static void dwarf2out_note_section_used (void);
279 /* Personality decl of current unit. Used only when assembler does not support
280 personality CFI. */
281 static GTY(()) rtx current_unit_personality;
283 /* Data and reference forms for relocatable data. */
284 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
285 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
287 #ifndef DEBUG_FRAME_SECTION
288 #define DEBUG_FRAME_SECTION ".debug_frame"
289 #endif
291 #ifndef FUNC_BEGIN_LABEL
292 #define FUNC_BEGIN_LABEL "LFB"
293 #endif
295 #ifndef FUNC_END_LABEL
296 #define FUNC_END_LABEL "LFE"
297 #endif
299 #ifndef PROLOGUE_END_LABEL
300 #define PROLOGUE_END_LABEL "LPE"
301 #endif
303 #ifndef EPILOGUE_BEGIN_LABEL
304 #define EPILOGUE_BEGIN_LABEL "LEB"
305 #endif
307 #ifndef FRAME_BEGIN_LABEL
308 #define FRAME_BEGIN_LABEL "Lframe"
309 #endif
310 #define CIE_AFTER_SIZE_LABEL "LSCIE"
311 #define CIE_END_LABEL "LECIE"
312 #define FDE_LABEL "LSFDE"
313 #define FDE_AFTER_SIZE_LABEL "LASFDE"
314 #define FDE_END_LABEL "LEFDE"
315 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
316 #define LINE_NUMBER_END_LABEL "LELT"
317 #define LN_PROLOG_AS_LABEL "LASLTP"
318 #define LN_PROLOG_END_LABEL "LELTP"
319 #define DIE_LABEL_PREFIX "DW"
321 /* Match the base name of a file to the base name of a compilation unit. */
323 static int
324 matches_main_base (const char *path)
326 /* Cache the last query. */
327 static const char *last_path = NULL;
328 static int last_match = 0;
329 if (path != last_path)
331 const char *base;
332 int length = base_of_path (path, &base);
333 last_path = path;
334 last_match = (length == main_input_baselength
335 && memcmp (base, main_input_basename, length) == 0);
337 return last_match;
340 #ifdef DEBUG_DEBUG_STRUCT
342 static int
343 dump_struct_debug (tree type, enum debug_info_usage usage,
344 enum debug_struct_file criterion, int generic,
345 int matches, int result)
347 /* Find the type name. */
348 tree type_decl = TYPE_STUB_DECL (type);
349 tree t = type_decl;
350 const char *name = 0;
351 if (TREE_CODE (t) == TYPE_DECL)
352 t = DECL_NAME (t);
353 if (t)
354 name = IDENTIFIER_POINTER (t);
356 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
357 criterion,
358 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
359 matches ? "bas" : "hdr",
360 generic ? "gen" : "ord",
361 usage == DINFO_USAGE_DFN ? ";" :
362 usage == DINFO_USAGE_DIR_USE ? "." : "*",
363 result,
364 (void*) type_decl, name);
365 return result;
367 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
368 dump_struct_debug (type, usage, criterion, generic, matches, result)
370 #else
372 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
373 (result)
375 #endif
377 /* Get the number of HOST_WIDE_INTs needed to represent the precision
378 of the number. */
380 static unsigned int
381 get_full_len (const wide_int &op)
383 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
384 / HOST_BITS_PER_WIDE_INT);
387 static bool
388 should_emit_struct_debug (tree type, enum debug_info_usage usage)
390 enum debug_struct_file criterion;
391 tree type_decl;
392 bool generic = lang_hooks.types.generic_p (type);
394 if (generic)
395 criterion = debug_struct_generic[usage];
396 else
397 criterion = debug_struct_ordinary[usage];
399 if (criterion == DINFO_STRUCT_FILE_NONE)
400 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
401 if (criterion == DINFO_STRUCT_FILE_ANY)
402 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
404 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
406 if (type_decl != NULL)
408 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
409 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
411 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
412 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
415 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
418 /* Return a pointer to a copy of the section string name S with all
419 attributes stripped off, and an asterisk prepended (for assemble_name). */
421 static inline char *
422 stripattributes (const char *s)
424 char *stripped = XNEWVEC (char, strlen (s) + 2);
425 char *p = stripped;
427 *p++ = '*';
429 while (*s && *s != ',')
430 *p++ = *s++;
432 *p = '\0';
433 return stripped;
436 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
437 switch to the data section instead, and write out a synthetic start label
438 for collect2 the first time around. */
440 static void
441 switch_to_eh_frame_section (bool back)
443 tree label;
445 #ifdef EH_FRAME_SECTION_NAME
446 if (eh_frame_section == 0)
448 int flags;
450 if (EH_TABLES_CAN_BE_READ_ONLY)
452 int fde_encoding;
453 int per_encoding;
454 int lsda_encoding;
456 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
457 /*global=*/0);
458 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
459 /*global=*/1);
460 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
461 /*global=*/0);
462 flags = ((! flag_pic
463 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
464 && (fde_encoding & 0x70) != DW_EH_PE_aligned
465 && (per_encoding & 0x70) != DW_EH_PE_absptr
466 && (per_encoding & 0x70) != DW_EH_PE_aligned
467 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
468 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
469 ? 0 : SECTION_WRITE);
471 else
472 flags = SECTION_WRITE;
473 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
475 #endif /* EH_FRAME_SECTION_NAME */
477 if (eh_frame_section)
478 switch_to_section (eh_frame_section);
479 else
481 /* We have no special eh_frame section. Put the information in
482 the data section and emit special labels to guide collect2. */
483 switch_to_section (data_section);
485 if (!back)
487 label = get_file_function_name ("F");
488 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
489 targetm.asm_out.globalize_label (asm_out_file,
490 IDENTIFIER_POINTER (label));
491 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
496 /* Switch [BACK] to the eh or debug frame table section, depending on
497 FOR_EH. */
499 static void
500 switch_to_frame_table_section (int for_eh, bool back)
502 if (for_eh)
503 switch_to_eh_frame_section (back);
504 else
506 if (!debug_frame_section)
507 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
508 SECTION_DEBUG, NULL);
509 switch_to_section (debug_frame_section);
513 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
515 enum dw_cfi_oprnd_type
516 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
518 switch (cfi)
520 case DW_CFA_nop:
521 case DW_CFA_GNU_window_save:
522 case DW_CFA_remember_state:
523 case DW_CFA_restore_state:
524 return dw_cfi_oprnd_unused;
526 case DW_CFA_set_loc:
527 case DW_CFA_advance_loc1:
528 case DW_CFA_advance_loc2:
529 case DW_CFA_advance_loc4:
530 case DW_CFA_MIPS_advance_loc8:
531 return dw_cfi_oprnd_addr;
533 case DW_CFA_offset:
534 case DW_CFA_offset_extended:
535 case DW_CFA_def_cfa:
536 case DW_CFA_offset_extended_sf:
537 case DW_CFA_def_cfa_sf:
538 case DW_CFA_restore:
539 case DW_CFA_restore_extended:
540 case DW_CFA_undefined:
541 case DW_CFA_same_value:
542 case DW_CFA_def_cfa_register:
543 case DW_CFA_register:
544 case DW_CFA_expression:
545 return dw_cfi_oprnd_reg_num;
547 case DW_CFA_def_cfa_offset:
548 case DW_CFA_GNU_args_size:
549 case DW_CFA_def_cfa_offset_sf:
550 return dw_cfi_oprnd_offset;
552 case DW_CFA_def_cfa_expression:
553 return dw_cfi_oprnd_loc;
555 default:
556 gcc_unreachable ();
560 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
562 enum dw_cfi_oprnd_type
563 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
565 switch (cfi)
567 case DW_CFA_def_cfa:
568 case DW_CFA_def_cfa_sf:
569 case DW_CFA_offset:
570 case DW_CFA_offset_extended_sf:
571 case DW_CFA_offset_extended:
572 return dw_cfi_oprnd_offset;
574 case DW_CFA_register:
575 return dw_cfi_oprnd_reg_num;
577 case DW_CFA_expression:
578 return dw_cfi_oprnd_loc;
580 default:
581 return dw_cfi_oprnd_unused;
585 /* Output one FDE. */
587 static void
588 output_fde (dw_fde_ref fde, bool for_eh, bool second,
589 char *section_start_label, int fde_encoding, char *augmentation,
590 bool any_lsda_needed, int lsda_encoding)
592 const char *begin, *end;
593 static unsigned int j;
594 char l1[20], l2[20];
596 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
597 /* empty */ 0);
598 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
599 for_eh + j);
600 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
601 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
602 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
603 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
604 " indicating 64-bit DWARF extension");
605 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
606 "FDE Length");
607 ASM_OUTPUT_LABEL (asm_out_file, l1);
609 if (for_eh)
610 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
611 else
612 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
613 debug_frame_section, "FDE CIE offset");
615 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
616 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
618 if (for_eh)
620 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
621 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
622 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
623 "FDE initial location");
624 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
625 end, begin, "FDE address range");
627 else
629 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
630 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
633 if (augmentation[0])
635 if (any_lsda_needed)
637 int size = size_of_encoded_value (lsda_encoding);
639 if (lsda_encoding == DW_EH_PE_aligned)
641 int offset = ( 4 /* Length */
642 + 4 /* CIE offset */
643 + 2 * size_of_encoded_value (fde_encoding)
644 + 1 /* Augmentation size */ );
645 int pad = -offset & (PTR_SIZE - 1);
647 size += pad;
648 gcc_assert (size_of_uleb128 (size) == 1);
651 dw2_asm_output_data_uleb128 (size, "Augmentation size");
653 if (fde->uses_eh_lsda)
655 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
656 fde->funcdef_number);
657 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
658 gen_rtx_SYMBOL_REF (Pmode, l1),
659 false,
660 "Language Specific Data Area");
662 else
664 if (lsda_encoding == DW_EH_PE_aligned)
665 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
666 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
667 "Language Specific Data Area (none)");
670 else
671 dw2_asm_output_data_uleb128 (0, "Augmentation size");
674 /* Loop through the Call Frame Instructions associated with this FDE. */
675 fde->dw_fde_current_label = begin;
677 size_t from, until, i;
679 from = 0;
680 until = vec_safe_length (fde->dw_fde_cfi);
682 if (fde->dw_fde_second_begin == NULL)
684 else if (!second)
685 until = fde->dw_fde_switch_cfi_index;
686 else
687 from = fde->dw_fde_switch_cfi_index;
689 for (i = from; i < until; i++)
690 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
693 /* If we are to emit a ref/link from function bodies to their frame tables,
694 do it now. This is typically performed to make sure that tables
695 associated with functions are dragged with them and not discarded in
696 garbage collecting links. We need to do this on a per function basis to
697 cope with -ffunction-sections. */
699 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
700 /* Switch to the function section, emit the ref to the tables, and
701 switch *back* into the table section. */
702 switch_to_section (function_section (fde->decl));
703 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
704 switch_to_frame_table_section (for_eh, true);
705 #endif
707 /* Pad the FDE out to an address sized boundary. */
708 ASM_OUTPUT_ALIGN (asm_out_file,
709 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
710 ASM_OUTPUT_LABEL (asm_out_file, l2);
712 j += 2;
715 /* Return true if frame description entry FDE is needed for EH. */
717 static bool
718 fde_needed_for_eh_p (dw_fde_ref fde)
720 if (flag_asynchronous_unwind_tables)
721 return true;
723 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
724 return true;
726 if (fde->uses_eh_lsda)
727 return true;
729 /* If exceptions are enabled, we have collected nothrow info. */
730 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
731 return false;
733 return true;
736 /* Output the call frame information used to record information
737 that relates to calculating the frame pointer, and records the
738 location of saved registers. */
740 static void
741 output_call_frame_info (int for_eh)
743 unsigned int i;
744 dw_fde_ref fde;
745 dw_cfi_ref cfi;
746 char l1[20], l2[20], section_start_label[20];
747 bool any_lsda_needed = false;
748 char augmentation[6];
749 int augmentation_size;
750 int fde_encoding = DW_EH_PE_absptr;
751 int per_encoding = DW_EH_PE_absptr;
752 int lsda_encoding = DW_EH_PE_absptr;
753 int return_reg;
754 rtx personality = NULL;
755 int dw_cie_version;
757 /* Don't emit a CIE if there won't be any FDEs. */
758 if (!fde_vec)
759 return;
761 /* Nothing to do if the assembler's doing it all. */
762 if (dwarf2out_do_cfi_asm ())
763 return;
765 /* If we don't have any functions we'll want to unwind out of, don't emit
766 any EH unwind information. If we make FDEs linkonce, we may have to
767 emit an empty label for an FDE that wouldn't otherwise be emitted. We
768 want to avoid having an FDE kept around when the function it refers to
769 is discarded. Example where this matters: a primary function template
770 in C++ requires EH information, an explicit specialization doesn't. */
771 if (for_eh)
773 bool any_eh_needed = false;
775 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
777 if (fde->uses_eh_lsda)
778 any_eh_needed = any_lsda_needed = true;
779 else if (fde_needed_for_eh_p (fde))
780 any_eh_needed = true;
781 else if (TARGET_USES_WEAK_UNWIND_INFO)
782 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
785 if (!any_eh_needed)
786 return;
789 /* We're going to be generating comments, so turn on app. */
790 if (flag_debug_asm)
791 app_enable ();
793 /* Switch to the proper frame section, first time. */
794 switch_to_frame_table_section (for_eh, false);
796 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
797 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
799 /* Output the CIE. */
800 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
801 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
802 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
803 dw2_asm_output_data (4, 0xffffffff,
804 "Initial length escape value indicating 64-bit DWARF extension");
805 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
806 "Length of Common Information Entry");
807 ASM_OUTPUT_LABEL (asm_out_file, l1);
809 /* Now that the CIE pointer is PC-relative for EH,
810 use 0 to identify the CIE. */
811 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812 (for_eh ? 0 : DWARF_CIE_ID),
813 "CIE Identifier Tag");
815 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816 use CIE version 1, unless that would produce incorrect results
817 due to overflowing the return register column. */
818 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819 dw_cie_version = 1;
820 if (return_reg >= 256 || dwarf_version > 2)
821 dw_cie_version = 3;
822 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
824 augmentation[0] = 0;
825 augmentation_size = 0;
827 personality = current_unit_personality;
828 if (for_eh)
830 char *p;
832 /* Augmentation:
833 z Indicates that a uleb128 is present to size the
834 augmentation section.
835 L Indicates the encoding (and thus presence) of
836 an LSDA pointer in the FDE augmentation.
837 R Indicates a non-default pointer encoding for
838 FDE code pointers.
839 P Indicates the presence of an encoding + language
840 personality routine in the CIE augmentation. */
842 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
846 p = augmentation + 1;
847 if (personality)
849 *p++ = 'P';
850 augmentation_size += 1 + size_of_encoded_value (per_encoding);
851 assemble_external_libcall (personality);
853 if (any_lsda_needed)
855 *p++ = 'L';
856 augmentation_size += 1;
858 if (fde_encoding != DW_EH_PE_absptr)
860 *p++ = 'R';
861 augmentation_size += 1;
863 if (p > augmentation + 1)
865 augmentation[0] = 'z';
866 *p = '\0';
869 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
870 if (personality && per_encoding == DW_EH_PE_aligned)
872 int offset = ( 4 /* Length */
873 + 4 /* CIE Id */
874 + 1 /* CIE version */
875 + strlen (augmentation) + 1 /* Augmentation */
876 + size_of_uleb128 (1) /* Code alignment */
877 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878 + 1 /* RA column */
879 + 1 /* Augmentation size */
880 + 1 /* Personality encoding */ );
881 int pad = -offset & (PTR_SIZE - 1);
883 augmentation_size += pad;
885 /* Augmentations should be small, so there's scarce need to
886 iterate for a solution. Die if we exceed one uleb128 byte. */
887 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
891 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892 if (dw_cie_version >= 4)
894 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895 dw2_asm_output_data (1, 0, "CIE Segment Size");
897 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899 "CIE Data Alignment Factor");
901 if (dw_cie_version == 1)
902 dw2_asm_output_data (1, return_reg, "CIE RA Column");
903 else
904 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
906 if (augmentation[0])
908 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909 if (personality)
911 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912 eh_data_format_name (per_encoding));
913 dw2_asm_output_encoded_addr_rtx (per_encoding,
914 personality,
915 true, NULL);
918 if (any_lsda_needed)
919 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920 eh_data_format_name (lsda_encoding));
922 if (fde_encoding != DW_EH_PE_absptr)
923 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924 eh_data_format_name (fde_encoding));
927 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928 output_cfi (cfi, NULL, for_eh);
930 /* Pad the CIE out to an address sized boundary. */
931 ASM_OUTPUT_ALIGN (asm_out_file,
932 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933 ASM_OUTPUT_LABEL (asm_out_file, l2);
935 /* Loop through all of the FDE's. */
936 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
938 unsigned int k;
940 /* Don't emit EH unwind info for leaf functions that don't need it. */
941 if (for_eh && !fde_needed_for_eh_p (fde))
942 continue;
944 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946 augmentation, any_lsda_needed, lsda_encoding);
949 if (for_eh && targetm.terminate_dw2_eh_frame_info)
950 dw2_asm_output_data (4, 0, "End of Table");
952 /* Turn off app to make assembly quicker. */
953 if (flag_debug_asm)
954 app_disable ();
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
962 int enc;
963 rtx ref;
964 rtx personality = get_personality_function (current_function_decl);
966 fprintf (asm_out_file, "\t.cfi_startproc\n");
968 if (personality)
970 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971 ref = personality;
973 /* ??? The GAS support isn't entirely consistent. We have to
974 handle indirect support ourselves, but PC-relative is done
975 in the assembler. Further, the assembler can't handle any
976 of the weirder relocation types. */
977 if (enc & DW_EH_PE_indirect)
978 ref = dw2_force_const_mem (ref, true);
980 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981 output_addr_const (asm_out_file, ref);
982 fputc ('\n', asm_out_file);
985 if (crtl->uses_eh_lsda)
987 char lab[20];
989 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991 current_function_funcdef_no);
992 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
995 if (enc & DW_EH_PE_indirect)
996 ref = dw2_force_const_mem (ref, true);
998 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999 output_addr_const (asm_out_file, ref);
1000 fputc ('\n', asm_out_file);
1004 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1005 this allocation may be done before pass_final. */
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1010 dw_fde_ref fde;
1012 fde = ggc_cleared_alloc<dw_fde_node> ();
1013 fde->decl = current_function_decl;
1014 fde->funcdef_number = current_function_funcdef_no;
1015 fde->fde_index = vec_safe_length (fde_vec);
1016 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018 fde->nothrow = crtl->nothrow;
1019 fde->drap_reg = INVALID_REGNUM;
1020 fde->vdrap_reg = INVALID_REGNUM;
1022 /* Record the FDE associated with this function. */
1023 cfun->fde = fde;
1024 vec_safe_push (fde_vec, fde);
1026 return fde;
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030 the prologue. */
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034 const char *file ATTRIBUTE_UNUSED)
1036 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1037 char * dup_label;
1038 dw_fde_ref fde;
1039 section *fnsec;
1040 bool do_frame;
1042 current_function_func_begin_label = NULL;
1044 do_frame = dwarf2out_do_frame ();
1046 /* ??? current_function_func_begin_label is also used by except.c for
1047 call-site information. We must emit this label if it might be used. */
1048 if (!do_frame
1049 && (!flag_exceptions
1050 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1051 return;
1053 fnsec = function_section (current_function_decl);
1054 switch_to_section (fnsec);
1055 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1056 current_function_funcdef_no);
1057 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1058 current_function_funcdef_no);
1059 dup_label = xstrdup (label);
1060 current_function_func_begin_label = dup_label;
1062 /* We can elide the fde allocation if we're not emitting debug info. */
1063 if (!do_frame)
1064 return;
1066 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1067 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1068 would include pass_dwarf2_frame. If we've not created the FDE yet,
1069 do so now. */
1070 fde = cfun->fde;
1071 if (fde == NULL)
1072 fde = dwarf2out_alloc_current_fde ();
1074 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1075 fde->dw_fde_begin = dup_label;
1076 fde->dw_fde_current_label = dup_label;
1077 fde->in_std_section = (fnsec == text_section
1078 || (cold_text_section && fnsec == cold_text_section));
1080 /* We only want to output line number information for the genuine dwarf2
1081 prologue case, not the eh frame case. */
1082 #ifdef DWARF2_DEBUGGING_INFO
1083 if (file)
1084 dwarf2out_source_line (line, file, 0, true);
1085 #endif
1087 if (dwarf2out_do_cfi_asm ())
1088 dwarf2out_do_cfi_startproc (false);
1089 else
1091 rtx personality = get_personality_function (current_function_decl);
1092 if (!current_unit_personality)
1093 current_unit_personality = personality;
1095 /* We cannot keep a current personality per function as without CFI
1096 asm, at the point where we emit the CFI data, there is no current
1097 function anymore. */
1098 if (personality && current_unit_personality != personality)
1099 sorry ("multiple EH personalities are supported only with assemblers "
1100 "supporting .cfi_personality directive");
1104 /* Output a marker (i.e. a label) for the end of the generated code
1105 for a function prologue. This gets called *after* the prologue code has
1106 been generated. */
1108 void
1109 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1110 const char *file ATTRIBUTE_UNUSED)
1112 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1114 /* Output a label to mark the endpoint of the code generated for this
1115 function. */
1116 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1117 current_function_funcdef_no);
1118 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1119 current_function_funcdef_no);
1120 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1123 /* Output a marker (i.e. a label) for the beginning of the generated code
1124 for a function epilogue. This gets called *before* the prologue code has
1125 been generated. */
1127 void
1128 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1129 const char *file ATTRIBUTE_UNUSED)
1131 dw_fde_ref fde = cfun->fde;
1132 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1134 if (fde->dw_fde_vms_begin_epilogue)
1135 return;
1137 /* Output a label to mark the endpoint of the code generated for this
1138 function. */
1139 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1140 current_function_funcdef_no);
1141 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1142 current_function_funcdef_no);
1143 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1146 /* Output a marker (i.e. a label) for the absolute end of the generated code
1147 for a function definition. This gets called *after* the epilogue code has
1148 been generated. */
1150 void
1151 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1152 const char *file ATTRIBUTE_UNUSED)
1154 dw_fde_ref fde;
1155 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1157 last_var_location_insn = NULL;
1158 cached_next_real_insn = NULL;
1160 if (dwarf2out_do_cfi_asm ())
1161 fprintf (asm_out_file, "\t.cfi_endproc\n");
1163 /* Output a label to mark the endpoint of the code generated for this
1164 function. */
1165 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1166 current_function_funcdef_no);
1167 ASM_OUTPUT_LABEL (asm_out_file, label);
1168 fde = cfun->fde;
1169 gcc_assert (fde != NULL);
1170 if (fde->dw_fde_second_begin == NULL)
1171 fde->dw_fde_end = xstrdup (label);
1174 void
1175 dwarf2out_frame_finish (void)
1177 /* Output call frame information. */
1178 if (targetm.debug_unwind_info () == UI_DWARF2)
1179 output_call_frame_info (0);
1181 /* Output another copy for the unwinder. */
1182 if ((flag_unwind_tables || flag_exceptions)
1183 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1184 output_call_frame_info (1);
1187 /* Note that the current function section is being used for code. */
1189 static void
1190 dwarf2out_note_section_used (void)
1192 section *sec = current_function_section ();
1193 if (sec == text_section)
1194 text_section_used = true;
1195 else if (sec == cold_text_section)
1196 cold_text_section_used = true;
1199 static void var_location_switch_text_section (void);
1200 static void set_cur_line_info_table (section *);
1202 void
1203 dwarf2out_switch_text_section (void)
1205 section *sect;
1206 dw_fde_ref fde = cfun->fde;
1208 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1210 if (!in_cold_section_p)
1212 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1216 else
1218 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1219 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1220 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1222 have_multiple_function_sections = true;
1224 /* There is no need to mark used sections when not debugging. */
1225 if (cold_text_section != NULL)
1226 dwarf2out_note_section_used ();
1228 if (dwarf2out_do_cfi_asm ())
1229 fprintf (asm_out_file, "\t.cfi_endproc\n");
1231 /* Now do the real section switch. */
1232 sect = current_function_section ();
1233 switch_to_section (sect);
1235 fde->second_in_std_section
1236 = (sect == text_section
1237 || (cold_text_section && sect == cold_text_section));
1239 if (dwarf2out_do_cfi_asm ())
1240 dwarf2out_do_cfi_startproc (true);
1242 var_location_switch_text_section ();
1244 if (cold_text_section != NULL)
1245 set_cur_line_info_table (sect);
1248 /* And now, the subset of the debugging information support code necessary
1249 for emitting location expressions. */
1251 /* Data about a single source file. */
1252 struct GTY((for_user)) dwarf_file_data {
1253 const char * filename;
1254 int emitted_number;
1257 typedef struct GTY(()) deferred_locations_struct
1259 tree variable;
1260 dw_die_ref die;
1261 } deferred_locations;
1264 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1267 /* Describe an entry into the .debug_addr section. */
1269 enum ate_kind {
1270 ate_kind_rtx,
1271 ate_kind_rtx_dtprel,
1272 ate_kind_label
1275 typedef struct GTY((for_user)) addr_table_entry_struct {
1276 enum ate_kind kind;
1277 unsigned int refcount;
1278 unsigned int index;
1279 union addr_table_entry_struct_union
1281 rtx GTY ((tag ("0"))) rtl;
1282 char * GTY ((tag ("1"))) label;
1284 GTY ((desc ("%1.kind"))) addr;
1286 addr_table_entry;
1288 /* Location lists are ranges + location descriptions for that range,
1289 so you can track variables that are in different places over
1290 their entire life. */
1291 typedef struct GTY(()) dw_loc_list_struct {
1292 dw_loc_list_ref dw_loc_next;
1293 const char *begin; /* Label and addr_entry for start of range */
1294 addr_table_entry *begin_entry;
1295 const char *end; /* Label for end of range */
1296 char *ll_symbol; /* Label for beginning of location list.
1297 Only on head of list */
1298 const char *section; /* Section this loclist is relative to */
1299 dw_loc_descr_ref expr;
1300 hashval_t hash;
1301 /* True if all addresses in this and subsequent lists are known to be
1302 resolved. */
1303 bool resolved_addr;
1304 /* True if this list has been replaced by dw_loc_next. */
1305 bool replaced;
1306 bool emitted;
1307 /* True if the range should be emitted even if begin and end
1308 are the same. */
1309 bool force;
1310 } dw_loc_list_node;
1312 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1314 /* Convert a DWARF stack opcode into its string name. */
1316 static const char *
1317 dwarf_stack_op_name (unsigned int op)
1319 const char *name = get_DW_OP_name (op);
1321 if (name != NULL)
1322 return name;
1324 return "OP_<unknown>";
1327 /* Return a pointer to a newly allocated location description. Location
1328 descriptions are simple expression terms that can be strung
1329 together to form more complicated location (address) descriptions. */
1331 static inline dw_loc_descr_ref
1332 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1333 unsigned HOST_WIDE_INT oprnd2)
1335 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1337 descr->dw_loc_opc = op;
1338 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1339 descr->dw_loc_oprnd1.val_entry = NULL;
1340 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1341 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1342 descr->dw_loc_oprnd2.val_entry = NULL;
1343 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1345 return descr;
1348 /* Return a pointer to a newly allocated location description for
1349 REG and OFFSET. */
1351 static inline dw_loc_descr_ref
1352 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1354 if (reg <= 31)
1355 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1356 offset, 0);
1357 else
1358 return new_loc_descr (DW_OP_bregx, reg, offset);
1361 /* Add a location description term to a location description expression. */
1363 static inline void
1364 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1366 dw_loc_descr_ref *d;
1368 /* Find the end of the chain. */
1369 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1372 *d = descr;
1375 /* Compare two location operands for exact equality. */
1377 static bool
1378 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1380 if (a->val_class != b->val_class)
1381 return false;
1382 switch (a->val_class)
1384 case dw_val_class_none:
1385 return true;
1386 case dw_val_class_addr:
1387 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1389 case dw_val_class_offset:
1390 case dw_val_class_unsigned_const:
1391 case dw_val_class_const:
1392 case dw_val_class_range_list:
1393 case dw_val_class_lineptr:
1394 case dw_val_class_macptr:
1395 /* These are all HOST_WIDE_INT, signed or unsigned. */
1396 return a->v.val_unsigned == b->v.val_unsigned;
1398 case dw_val_class_loc:
1399 return a->v.val_loc == b->v.val_loc;
1400 case dw_val_class_loc_list:
1401 return a->v.val_loc_list == b->v.val_loc_list;
1402 case dw_val_class_die_ref:
1403 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1404 case dw_val_class_fde_ref:
1405 return a->v.val_fde_index == b->v.val_fde_index;
1406 case dw_val_class_lbl_id:
1407 case dw_val_class_high_pc:
1408 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1409 case dw_val_class_str:
1410 return a->v.val_str == b->v.val_str;
1411 case dw_val_class_flag:
1412 return a->v.val_flag == b->v.val_flag;
1413 case dw_val_class_file:
1414 return a->v.val_file == b->v.val_file;
1415 case dw_val_class_decl_ref:
1416 return a->v.val_decl_ref == b->v.val_decl_ref;
1418 case dw_val_class_const_double:
1419 return (a->v.val_double.high == b->v.val_double.high
1420 && a->v.val_double.low == b->v.val_double.low);
1422 case dw_val_class_wide_int:
1423 return *a->v.val_wide == *b->v.val_wide;
1425 case dw_val_class_vec:
1427 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1428 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1430 return (a_len == b_len
1431 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1434 case dw_val_class_data8:
1435 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1437 case dw_val_class_vms_delta:
1438 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1439 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1441 gcc_unreachable ();
1444 /* Compare two location atoms for exact equality. */
1446 static bool
1447 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1449 if (a->dw_loc_opc != b->dw_loc_opc)
1450 return false;
1452 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1453 address size, but since we always allocate cleared storage it
1454 should be zero for other types of locations. */
1455 if (a->dtprel != b->dtprel)
1456 return false;
1458 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1459 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1462 /* Compare two complete location expressions for exact equality. */
1464 bool
1465 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1467 while (1)
1469 if (a == b)
1470 return true;
1471 if (a == NULL || b == NULL)
1472 return false;
1473 if (!loc_descr_equal_p_1 (a, b))
1474 return false;
1476 a = a->dw_loc_next;
1477 b = b->dw_loc_next;
1482 /* Add a constant OFFSET to a location expression. */
1484 static void
1485 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1487 dw_loc_descr_ref loc;
1488 HOST_WIDE_INT *p;
1490 gcc_assert (*list_head != NULL);
1492 if (!offset)
1493 return;
1495 /* Find the end of the chain. */
1496 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1499 p = NULL;
1500 if (loc->dw_loc_opc == DW_OP_fbreg
1501 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1502 p = &loc->dw_loc_oprnd1.v.val_int;
1503 else if (loc->dw_loc_opc == DW_OP_bregx)
1504 p = &loc->dw_loc_oprnd2.v.val_int;
1506 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1507 offset. Don't optimize if an signed integer overflow would happen. */
1508 if (p != NULL
1509 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1510 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1511 *p += offset;
1513 else if (offset > 0)
1514 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1516 else
1518 loc->dw_loc_next = int_loc_descriptor (-offset);
1519 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1523 /* Add a constant OFFSET to a location list. */
1525 static void
1526 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1528 dw_loc_list_ref d;
1529 for (d = list_head; d != NULL; d = d->dw_loc_next)
1530 loc_descr_plus_const (&d->expr, offset);
1533 #define DWARF_REF_SIZE \
1534 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1536 static unsigned long int get_base_type_offset (dw_die_ref);
1538 /* Return the size of a location descriptor. */
1540 static unsigned long
1541 size_of_loc_descr (dw_loc_descr_ref loc)
1543 unsigned long size = 1;
1545 switch (loc->dw_loc_opc)
1547 case DW_OP_addr:
1548 size += DWARF2_ADDR_SIZE;
1549 break;
1550 case DW_OP_GNU_addr_index:
1551 case DW_OP_GNU_const_index:
1552 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1553 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1554 break;
1555 case DW_OP_const1u:
1556 case DW_OP_const1s:
1557 size += 1;
1558 break;
1559 case DW_OP_const2u:
1560 case DW_OP_const2s:
1561 size += 2;
1562 break;
1563 case DW_OP_const4u:
1564 case DW_OP_const4s:
1565 size += 4;
1566 break;
1567 case DW_OP_const8u:
1568 case DW_OP_const8s:
1569 size += 8;
1570 break;
1571 case DW_OP_constu:
1572 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1573 break;
1574 case DW_OP_consts:
1575 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1576 break;
1577 case DW_OP_pick:
1578 size += 1;
1579 break;
1580 case DW_OP_plus_uconst:
1581 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1582 break;
1583 case DW_OP_skip:
1584 case DW_OP_bra:
1585 size += 2;
1586 break;
1587 case DW_OP_breg0:
1588 case DW_OP_breg1:
1589 case DW_OP_breg2:
1590 case DW_OP_breg3:
1591 case DW_OP_breg4:
1592 case DW_OP_breg5:
1593 case DW_OP_breg6:
1594 case DW_OP_breg7:
1595 case DW_OP_breg8:
1596 case DW_OP_breg9:
1597 case DW_OP_breg10:
1598 case DW_OP_breg11:
1599 case DW_OP_breg12:
1600 case DW_OP_breg13:
1601 case DW_OP_breg14:
1602 case DW_OP_breg15:
1603 case DW_OP_breg16:
1604 case DW_OP_breg17:
1605 case DW_OP_breg18:
1606 case DW_OP_breg19:
1607 case DW_OP_breg20:
1608 case DW_OP_breg21:
1609 case DW_OP_breg22:
1610 case DW_OP_breg23:
1611 case DW_OP_breg24:
1612 case DW_OP_breg25:
1613 case DW_OP_breg26:
1614 case DW_OP_breg27:
1615 case DW_OP_breg28:
1616 case DW_OP_breg29:
1617 case DW_OP_breg30:
1618 case DW_OP_breg31:
1619 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1620 break;
1621 case DW_OP_regx:
1622 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1623 break;
1624 case DW_OP_fbreg:
1625 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1626 break;
1627 case DW_OP_bregx:
1628 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1629 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1630 break;
1631 case DW_OP_piece:
1632 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1633 break;
1634 case DW_OP_bit_piece:
1635 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1636 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1637 break;
1638 case DW_OP_deref_size:
1639 case DW_OP_xderef_size:
1640 size += 1;
1641 break;
1642 case DW_OP_call2:
1643 size += 2;
1644 break;
1645 case DW_OP_call4:
1646 size += 4;
1647 break;
1648 case DW_OP_call_ref:
1649 size += DWARF_REF_SIZE;
1650 break;
1651 case DW_OP_implicit_value:
1652 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1653 + loc->dw_loc_oprnd1.v.val_unsigned;
1654 break;
1655 case DW_OP_GNU_implicit_pointer:
1656 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1657 break;
1658 case DW_OP_GNU_entry_value:
1660 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1661 size += size_of_uleb128 (op_size) + op_size;
1662 break;
1664 case DW_OP_GNU_const_type:
1666 unsigned long o
1667 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1668 size += size_of_uleb128 (o) + 1;
1669 switch (loc->dw_loc_oprnd2.val_class)
1671 case dw_val_class_vec:
1672 size += loc->dw_loc_oprnd2.v.val_vec.length
1673 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1674 break;
1675 case dw_val_class_const:
1676 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1677 break;
1678 case dw_val_class_const_double:
1679 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1680 break;
1681 case dw_val_class_wide_int:
1682 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1683 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1684 break;
1685 default:
1686 gcc_unreachable ();
1688 break;
1690 case DW_OP_GNU_regval_type:
1692 unsigned long o
1693 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1694 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1695 + size_of_uleb128 (o);
1697 break;
1698 case DW_OP_GNU_deref_type:
1700 unsigned long o
1701 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1702 size += 1 + size_of_uleb128 (o);
1704 break;
1705 case DW_OP_GNU_convert:
1706 case DW_OP_GNU_reinterpret:
1707 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1708 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1709 else
1711 unsigned long o
1712 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1713 size += size_of_uleb128 (o);
1715 break;
1716 case DW_OP_GNU_parameter_ref:
1717 size += 4;
1718 break;
1719 default:
1720 break;
1723 return size;
1726 /* Return the size of a series of location descriptors. */
1728 unsigned long
1729 size_of_locs (dw_loc_descr_ref loc)
1731 dw_loc_descr_ref l;
1732 unsigned long size;
1734 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1735 field, to avoid writing to a PCH file. */
1736 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1738 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1739 break;
1740 size += size_of_loc_descr (l);
1742 if (! l)
1743 return size;
1745 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1747 l->dw_loc_addr = size;
1748 size += size_of_loc_descr (l);
1751 return size;
1754 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1755 static void get_ref_die_offset_label (char *, dw_die_ref);
1756 static unsigned long int get_ref_die_offset (dw_die_ref);
1758 /* Output location description stack opcode's operands (if any).
1759 The for_eh_or_skip parameter controls whether register numbers are
1760 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1761 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1762 info). This should be suppressed for the cases that have not been converted
1763 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1765 static void
1766 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1768 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1769 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1771 switch (loc->dw_loc_opc)
1773 #ifdef DWARF2_DEBUGGING_INFO
1774 case DW_OP_const2u:
1775 case DW_OP_const2s:
1776 dw2_asm_output_data (2, val1->v.val_int, NULL);
1777 break;
1778 case DW_OP_const4u:
1779 if (loc->dtprel)
1781 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1782 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1783 val1->v.val_addr);
1784 fputc ('\n', asm_out_file);
1785 break;
1787 /* FALLTHRU */
1788 case DW_OP_const4s:
1789 dw2_asm_output_data (4, val1->v.val_int, NULL);
1790 break;
1791 case DW_OP_const8u:
1792 if (loc->dtprel)
1794 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1795 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1796 val1->v.val_addr);
1797 fputc ('\n', asm_out_file);
1798 break;
1800 /* FALLTHRU */
1801 case DW_OP_const8s:
1802 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1803 dw2_asm_output_data (8, val1->v.val_int, NULL);
1804 break;
1805 case DW_OP_skip:
1806 case DW_OP_bra:
1808 int offset;
1810 gcc_assert (val1->val_class == dw_val_class_loc);
1811 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1813 dw2_asm_output_data (2, offset, NULL);
1815 break;
1816 case DW_OP_implicit_value:
1817 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1818 switch (val2->val_class)
1820 case dw_val_class_const:
1821 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1822 break;
1823 case dw_val_class_vec:
1825 unsigned int elt_size = val2->v.val_vec.elt_size;
1826 unsigned int len = val2->v.val_vec.length;
1827 unsigned int i;
1828 unsigned char *p;
1830 if (elt_size > sizeof (HOST_WIDE_INT))
1832 elt_size /= 2;
1833 len *= 2;
1835 for (i = 0, p = val2->v.val_vec.array;
1836 i < len;
1837 i++, p += elt_size)
1838 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1839 "fp or vector constant word %u", i);
1841 break;
1842 case dw_val_class_const_double:
1844 unsigned HOST_WIDE_INT first, second;
1846 if (WORDS_BIG_ENDIAN)
1848 first = val2->v.val_double.high;
1849 second = val2->v.val_double.low;
1851 else
1853 first = val2->v.val_double.low;
1854 second = val2->v.val_double.high;
1856 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1857 first, NULL);
1858 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1859 second, NULL);
1861 break;
1862 case dw_val_class_wide_int:
1864 int i;
1865 int len = get_full_len (*val2->v.val_wide);
1866 if (WORDS_BIG_ENDIAN)
1867 for (i = len - 1; i >= 0; --i)
1868 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1869 val2->v.val_wide->elt (i), NULL);
1870 else
1871 for (i = 0; i < len; ++i)
1872 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1873 val2->v.val_wide->elt (i), NULL);
1875 break;
1876 case dw_val_class_addr:
1877 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1878 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1879 break;
1880 default:
1881 gcc_unreachable ();
1883 break;
1884 #else
1885 case DW_OP_const2u:
1886 case DW_OP_const2s:
1887 case DW_OP_const4u:
1888 case DW_OP_const4s:
1889 case DW_OP_const8u:
1890 case DW_OP_const8s:
1891 case DW_OP_skip:
1892 case DW_OP_bra:
1893 case DW_OP_implicit_value:
1894 /* We currently don't make any attempt to make sure these are
1895 aligned properly like we do for the main unwind info, so
1896 don't support emitting things larger than a byte if we're
1897 only doing unwinding. */
1898 gcc_unreachable ();
1899 #endif
1900 case DW_OP_const1u:
1901 case DW_OP_const1s:
1902 dw2_asm_output_data (1, val1->v.val_int, NULL);
1903 break;
1904 case DW_OP_constu:
1905 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1906 break;
1907 case DW_OP_consts:
1908 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1909 break;
1910 case DW_OP_pick:
1911 dw2_asm_output_data (1, val1->v.val_int, NULL);
1912 break;
1913 case DW_OP_plus_uconst:
1914 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1915 break;
1916 case DW_OP_breg0:
1917 case DW_OP_breg1:
1918 case DW_OP_breg2:
1919 case DW_OP_breg3:
1920 case DW_OP_breg4:
1921 case DW_OP_breg5:
1922 case DW_OP_breg6:
1923 case DW_OP_breg7:
1924 case DW_OP_breg8:
1925 case DW_OP_breg9:
1926 case DW_OP_breg10:
1927 case DW_OP_breg11:
1928 case DW_OP_breg12:
1929 case DW_OP_breg13:
1930 case DW_OP_breg14:
1931 case DW_OP_breg15:
1932 case DW_OP_breg16:
1933 case DW_OP_breg17:
1934 case DW_OP_breg18:
1935 case DW_OP_breg19:
1936 case DW_OP_breg20:
1937 case DW_OP_breg21:
1938 case DW_OP_breg22:
1939 case DW_OP_breg23:
1940 case DW_OP_breg24:
1941 case DW_OP_breg25:
1942 case DW_OP_breg26:
1943 case DW_OP_breg27:
1944 case DW_OP_breg28:
1945 case DW_OP_breg29:
1946 case DW_OP_breg30:
1947 case DW_OP_breg31:
1948 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1949 break;
1950 case DW_OP_regx:
1952 unsigned r = val1->v.val_unsigned;
1953 if (for_eh_or_skip >= 0)
1954 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1955 gcc_assert (size_of_uleb128 (r)
1956 == size_of_uleb128 (val1->v.val_unsigned));
1957 dw2_asm_output_data_uleb128 (r, NULL);
1959 break;
1960 case DW_OP_fbreg:
1961 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1962 break;
1963 case DW_OP_bregx:
1965 unsigned r = val1->v.val_unsigned;
1966 if (for_eh_or_skip >= 0)
1967 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1968 gcc_assert (size_of_uleb128 (r)
1969 == size_of_uleb128 (val1->v.val_unsigned));
1970 dw2_asm_output_data_uleb128 (r, NULL);
1971 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1973 break;
1974 case DW_OP_piece:
1975 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1976 break;
1977 case DW_OP_bit_piece:
1978 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1979 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1980 break;
1981 case DW_OP_deref_size:
1982 case DW_OP_xderef_size:
1983 dw2_asm_output_data (1, val1->v.val_int, NULL);
1984 break;
1986 case DW_OP_addr:
1987 if (loc->dtprel)
1989 if (targetm.asm_out.output_dwarf_dtprel)
1991 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1992 DWARF2_ADDR_SIZE,
1993 val1->v.val_addr);
1994 fputc ('\n', asm_out_file);
1996 else
1997 gcc_unreachable ();
1999 else
2001 #ifdef DWARF2_DEBUGGING_INFO
2002 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2003 #else
2004 gcc_unreachable ();
2005 #endif
2007 break;
2009 case DW_OP_GNU_addr_index:
2010 case DW_OP_GNU_const_index:
2011 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2012 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2013 "(index into .debug_addr)");
2014 break;
2016 case DW_OP_GNU_implicit_pointer:
2018 char label[MAX_ARTIFICIAL_LABEL_BYTES
2019 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2020 gcc_assert (val1->val_class == dw_val_class_die_ref);
2021 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2022 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2023 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2025 break;
2027 case DW_OP_GNU_entry_value:
2028 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2029 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2030 break;
2032 case DW_OP_GNU_const_type:
2034 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2035 gcc_assert (o);
2036 dw2_asm_output_data_uleb128 (o, NULL);
2037 switch (val2->val_class)
2039 case dw_val_class_const:
2040 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2041 dw2_asm_output_data (1, l, NULL);
2042 dw2_asm_output_data (l, val2->v.val_int, NULL);
2043 break;
2044 case dw_val_class_vec:
2046 unsigned int elt_size = val2->v.val_vec.elt_size;
2047 unsigned int len = val2->v.val_vec.length;
2048 unsigned int i;
2049 unsigned char *p;
2051 l = len * elt_size;
2052 dw2_asm_output_data (1, l, NULL);
2053 if (elt_size > sizeof (HOST_WIDE_INT))
2055 elt_size /= 2;
2056 len *= 2;
2058 for (i = 0, p = val2->v.val_vec.array;
2059 i < len;
2060 i++, p += elt_size)
2061 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2062 "fp or vector constant word %u", i);
2064 break;
2065 case dw_val_class_const_double:
2067 unsigned HOST_WIDE_INT first, second;
2068 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2070 dw2_asm_output_data (1, 2 * l, NULL);
2071 if (WORDS_BIG_ENDIAN)
2073 first = val2->v.val_double.high;
2074 second = val2->v.val_double.low;
2076 else
2078 first = val2->v.val_double.low;
2079 second = val2->v.val_double.high;
2081 dw2_asm_output_data (l, first, NULL);
2082 dw2_asm_output_data (l, second, NULL);
2084 break;
2085 case dw_val_class_wide_int:
2087 int i;
2088 int len = get_full_len (*val2->v.val_wide);
2089 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2091 dw2_asm_output_data (1, len * l, NULL);
2092 if (WORDS_BIG_ENDIAN)
2093 for (i = len - 1; i >= 0; --i)
2094 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2095 else
2096 for (i = 0; i < len; ++i)
2097 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2099 break;
2100 default:
2101 gcc_unreachable ();
2104 break;
2105 case DW_OP_GNU_regval_type:
2107 unsigned r = val1->v.val_unsigned;
2108 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2109 gcc_assert (o);
2110 if (for_eh_or_skip >= 0)
2112 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2113 gcc_assert (size_of_uleb128 (r)
2114 == size_of_uleb128 (val1->v.val_unsigned));
2116 dw2_asm_output_data_uleb128 (r, NULL);
2117 dw2_asm_output_data_uleb128 (o, NULL);
2119 break;
2120 case DW_OP_GNU_deref_type:
2122 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2123 gcc_assert (o);
2124 dw2_asm_output_data (1, val1->v.val_int, NULL);
2125 dw2_asm_output_data_uleb128 (o, NULL);
2127 break;
2128 case DW_OP_GNU_convert:
2129 case DW_OP_GNU_reinterpret:
2130 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2131 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2132 else
2134 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2135 gcc_assert (o);
2136 dw2_asm_output_data_uleb128 (o, NULL);
2138 break;
2140 case DW_OP_GNU_parameter_ref:
2142 unsigned long o;
2143 gcc_assert (val1->val_class == dw_val_class_die_ref);
2144 o = get_ref_die_offset (val1->v.val_die_ref.die);
2145 dw2_asm_output_data (4, o, NULL);
2147 break;
2149 default:
2150 /* Other codes have no operands. */
2151 break;
2155 /* Output a sequence of location operations.
2156 The for_eh_or_skip parameter controls whether register numbers are
2157 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2158 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2159 info). This should be suppressed for the cases that have not been converted
2160 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2162 void
2163 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2165 for (; loc != NULL; loc = loc->dw_loc_next)
2167 enum dwarf_location_atom opc = loc->dw_loc_opc;
2168 /* Output the opcode. */
2169 if (for_eh_or_skip >= 0
2170 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2172 unsigned r = (opc - DW_OP_breg0);
2173 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2174 gcc_assert (r <= 31);
2175 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2177 else if (for_eh_or_skip >= 0
2178 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2180 unsigned r = (opc - DW_OP_reg0);
2181 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2182 gcc_assert (r <= 31);
2183 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2186 dw2_asm_output_data (1, opc,
2187 "%s", dwarf_stack_op_name (opc));
2189 /* Output the operand(s) (if any). */
2190 output_loc_operands (loc, for_eh_or_skip);
2194 /* Output location description stack opcode's operands (if any).
2195 The output is single bytes on a line, suitable for .cfi_escape. */
2197 static void
2198 output_loc_operands_raw (dw_loc_descr_ref loc)
2200 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2201 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2203 switch (loc->dw_loc_opc)
2205 case DW_OP_addr:
2206 case DW_OP_GNU_addr_index:
2207 case DW_OP_GNU_const_index:
2208 case DW_OP_implicit_value:
2209 /* We cannot output addresses in .cfi_escape, only bytes. */
2210 gcc_unreachable ();
2212 case DW_OP_const1u:
2213 case DW_OP_const1s:
2214 case DW_OP_pick:
2215 case DW_OP_deref_size:
2216 case DW_OP_xderef_size:
2217 fputc (',', asm_out_file);
2218 dw2_asm_output_data_raw (1, val1->v.val_int);
2219 break;
2221 case DW_OP_const2u:
2222 case DW_OP_const2s:
2223 fputc (',', asm_out_file);
2224 dw2_asm_output_data_raw (2, val1->v.val_int);
2225 break;
2227 case DW_OP_const4u:
2228 case DW_OP_const4s:
2229 fputc (',', asm_out_file);
2230 dw2_asm_output_data_raw (4, val1->v.val_int);
2231 break;
2233 case DW_OP_const8u:
2234 case DW_OP_const8s:
2235 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2236 fputc (',', asm_out_file);
2237 dw2_asm_output_data_raw (8, val1->v.val_int);
2238 break;
2240 case DW_OP_skip:
2241 case DW_OP_bra:
2243 int offset;
2245 gcc_assert (val1->val_class == dw_val_class_loc);
2246 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2248 fputc (',', asm_out_file);
2249 dw2_asm_output_data_raw (2, offset);
2251 break;
2253 case DW_OP_regx:
2255 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2256 gcc_assert (size_of_uleb128 (r)
2257 == size_of_uleb128 (val1->v.val_unsigned));
2258 fputc (',', asm_out_file);
2259 dw2_asm_output_data_uleb128_raw (r);
2261 break;
2263 case DW_OP_constu:
2264 case DW_OP_plus_uconst:
2265 case DW_OP_piece:
2266 fputc (',', asm_out_file);
2267 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2268 break;
2270 case DW_OP_bit_piece:
2271 fputc (',', asm_out_file);
2272 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2273 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2274 break;
2276 case DW_OP_consts:
2277 case DW_OP_breg0:
2278 case DW_OP_breg1:
2279 case DW_OP_breg2:
2280 case DW_OP_breg3:
2281 case DW_OP_breg4:
2282 case DW_OP_breg5:
2283 case DW_OP_breg6:
2284 case DW_OP_breg7:
2285 case DW_OP_breg8:
2286 case DW_OP_breg9:
2287 case DW_OP_breg10:
2288 case DW_OP_breg11:
2289 case DW_OP_breg12:
2290 case DW_OP_breg13:
2291 case DW_OP_breg14:
2292 case DW_OP_breg15:
2293 case DW_OP_breg16:
2294 case DW_OP_breg17:
2295 case DW_OP_breg18:
2296 case DW_OP_breg19:
2297 case DW_OP_breg20:
2298 case DW_OP_breg21:
2299 case DW_OP_breg22:
2300 case DW_OP_breg23:
2301 case DW_OP_breg24:
2302 case DW_OP_breg25:
2303 case DW_OP_breg26:
2304 case DW_OP_breg27:
2305 case DW_OP_breg28:
2306 case DW_OP_breg29:
2307 case DW_OP_breg30:
2308 case DW_OP_breg31:
2309 case DW_OP_fbreg:
2310 fputc (',', asm_out_file);
2311 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2312 break;
2314 case DW_OP_bregx:
2316 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2317 gcc_assert (size_of_uleb128 (r)
2318 == size_of_uleb128 (val1->v.val_unsigned));
2319 fputc (',', asm_out_file);
2320 dw2_asm_output_data_uleb128_raw (r);
2321 fputc (',', asm_out_file);
2322 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2324 break;
2326 case DW_OP_GNU_implicit_pointer:
2327 case DW_OP_GNU_entry_value:
2328 case DW_OP_GNU_const_type:
2329 case DW_OP_GNU_regval_type:
2330 case DW_OP_GNU_deref_type:
2331 case DW_OP_GNU_convert:
2332 case DW_OP_GNU_reinterpret:
2333 case DW_OP_GNU_parameter_ref:
2334 gcc_unreachable ();
2335 break;
2337 default:
2338 /* Other codes have no operands. */
2339 break;
2343 void
2344 output_loc_sequence_raw (dw_loc_descr_ref loc)
2346 while (1)
2348 enum dwarf_location_atom opc = loc->dw_loc_opc;
2349 /* Output the opcode. */
2350 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2352 unsigned r = (opc - DW_OP_breg0);
2353 r = DWARF2_FRAME_REG_OUT (r, 1);
2354 gcc_assert (r <= 31);
2355 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2357 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2359 unsigned r = (opc - DW_OP_reg0);
2360 r = DWARF2_FRAME_REG_OUT (r, 1);
2361 gcc_assert (r <= 31);
2362 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2364 /* Output the opcode. */
2365 fprintf (asm_out_file, "%#x", opc);
2366 output_loc_operands_raw (loc);
2368 if (!loc->dw_loc_next)
2369 break;
2370 loc = loc->dw_loc_next;
2372 fputc (',', asm_out_file);
2376 /* This function builds a dwarf location descriptor sequence from a
2377 dw_cfa_location, adding the given OFFSET to the result of the
2378 expression. */
2380 struct dw_loc_descr_node *
2381 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2383 struct dw_loc_descr_node *head, *tmp;
2385 offset += cfa->offset;
2387 if (cfa->indirect)
2389 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2390 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2391 head->dw_loc_oprnd1.val_entry = NULL;
2392 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2393 add_loc_descr (&head, tmp);
2394 if (offset != 0)
2396 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2397 add_loc_descr (&head, tmp);
2400 else
2401 head = new_reg_loc_descr (cfa->reg, offset);
2403 return head;
2406 /* This function builds a dwarf location descriptor sequence for
2407 the address at OFFSET from the CFA when stack is aligned to
2408 ALIGNMENT byte. */
2410 struct dw_loc_descr_node *
2411 build_cfa_aligned_loc (dw_cfa_location *cfa,
2412 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2414 struct dw_loc_descr_node *head;
2415 unsigned int dwarf_fp
2416 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2418 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2419 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2421 head = new_reg_loc_descr (dwarf_fp, 0);
2422 add_loc_descr (&head, int_loc_descriptor (alignment));
2423 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2424 loc_descr_plus_const (&head, offset);
2426 else
2427 head = new_reg_loc_descr (dwarf_fp, offset);
2428 return head;
2431 /* And now, the support for symbolic debugging information. */
2433 /* .debug_str support. */
2435 static void dwarf2out_init (const char *);
2436 static void dwarf2out_finish (const char *);
2437 static void dwarf2out_assembly_start (void);
2438 static void dwarf2out_define (unsigned int, const char *);
2439 static void dwarf2out_undef (unsigned int, const char *);
2440 static void dwarf2out_start_source_file (unsigned, const char *);
2441 static void dwarf2out_end_source_file (unsigned);
2442 static void dwarf2out_function_decl (tree);
2443 static void dwarf2out_begin_block (unsigned, unsigned);
2444 static void dwarf2out_end_block (unsigned, unsigned);
2445 static bool dwarf2out_ignore_block (const_tree);
2446 static void dwarf2out_global_decl (tree);
2447 static void dwarf2out_type_decl (tree, int);
2448 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2449 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2450 dw_die_ref);
2451 static void dwarf2out_abstract_function (tree);
2452 static void dwarf2out_var_location (rtx_insn *);
2453 static void dwarf2out_begin_function (tree);
2454 static void dwarf2out_end_function (unsigned int);
2455 static void dwarf2out_set_name (tree, tree);
2457 /* The debug hooks structure. */
2459 const struct gcc_debug_hooks dwarf2_debug_hooks =
2461 dwarf2out_init,
2462 dwarf2out_finish,
2463 dwarf2out_assembly_start,
2464 dwarf2out_define,
2465 dwarf2out_undef,
2466 dwarf2out_start_source_file,
2467 dwarf2out_end_source_file,
2468 dwarf2out_begin_block,
2469 dwarf2out_end_block,
2470 dwarf2out_ignore_block,
2471 dwarf2out_source_line,
2472 dwarf2out_begin_prologue,
2473 #if VMS_DEBUGGING_INFO
2474 dwarf2out_vms_end_prologue,
2475 dwarf2out_vms_begin_epilogue,
2476 #else
2477 debug_nothing_int_charstar,
2478 debug_nothing_int_charstar,
2479 #endif
2480 dwarf2out_end_epilogue,
2481 dwarf2out_begin_function,
2482 dwarf2out_end_function, /* end_function */
2483 dwarf2out_function_decl, /* function_decl */
2484 dwarf2out_global_decl,
2485 dwarf2out_type_decl, /* type_decl */
2486 dwarf2out_imported_module_or_decl,
2487 debug_nothing_tree, /* deferred_inline_function */
2488 /* The DWARF 2 backend tries to reduce debugging bloat by not
2489 emitting the abstract description of inline functions until
2490 something tries to reference them. */
2491 dwarf2out_abstract_function, /* outlining_inline_function */
2492 debug_nothing_rtx_code_label, /* label */
2493 debug_nothing_int, /* handle_pch */
2494 dwarf2out_var_location,
2495 dwarf2out_switch_text_section,
2496 dwarf2out_set_name,
2497 1, /* start_end_main_source_file */
2498 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2501 /* NOTE: In the comments in this file, many references are made to
2502 "Debugging Information Entries". This term is abbreviated as `DIE'
2503 throughout the remainder of this file. */
2505 /* An internal representation of the DWARF output is built, and then
2506 walked to generate the DWARF debugging info. The walk of the internal
2507 representation is done after the entire program has been compiled.
2508 The types below are used to describe the internal representation. */
2510 /* Whether to put type DIEs into their own section .debug_types instead
2511 of making them part of the .debug_info section. Only supported for
2512 Dwarf V4 or higher and the user didn't disable them through
2513 -fno-debug-types-section. It is more efficient to put them in a
2514 separate comdat sections since the linker will then be able to
2515 remove duplicates. But not all tools support .debug_types sections
2516 yet. */
2518 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2520 /* Various DIE's use offsets relative to the beginning of the
2521 .debug_info section to refer to each other. */
2523 typedef long int dw_offset;
2525 /* Define typedefs here to avoid circular dependencies. */
2527 typedef struct dw_attr_struct *dw_attr_ref;
2528 typedef struct dw_line_info_struct *dw_line_info_ref;
2529 typedef struct pubname_struct *pubname_ref;
2530 typedef struct dw_ranges_struct *dw_ranges_ref;
2531 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2532 typedef struct comdat_type_struct *comdat_type_node_ref;
2534 /* The entries in the line_info table more-or-less mirror the opcodes
2535 that are used in the real dwarf line table. Arrays of these entries
2536 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2537 supported. */
2539 enum dw_line_info_opcode {
2540 /* Emit DW_LNE_set_address; the operand is the label index. */
2541 LI_set_address,
2543 /* Emit a row to the matrix with the given line. This may be done
2544 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2545 special opcodes. */
2546 LI_set_line,
2548 /* Emit a DW_LNS_set_file. */
2549 LI_set_file,
2551 /* Emit a DW_LNS_set_column. */
2552 LI_set_column,
2554 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2555 LI_negate_stmt,
2557 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2558 LI_set_prologue_end,
2559 LI_set_epilogue_begin,
2561 /* Emit a DW_LNE_set_discriminator. */
2562 LI_set_discriminator
2565 typedef struct GTY(()) dw_line_info_struct {
2566 enum dw_line_info_opcode opcode;
2567 unsigned int val;
2568 } dw_line_info_entry;
2571 typedef struct GTY(()) dw_line_info_table_struct {
2572 /* The label that marks the end of this section. */
2573 const char *end_label;
2575 /* The values for the last row of the matrix, as collected in the table.
2576 These are used to minimize the changes to the next row. */
2577 unsigned int file_num;
2578 unsigned int line_num;
2579 unsigned int column_num;
2580 int discrim_num;
2581 bool is_stmt;
2582 bool in_use;
2584 vec<dw_line_info_entry, va_gc> *entries;
2585 } dw_line_info_table;
2587 typedef dw_line_info_table *dw_line_info_table_p;
2590 /* Each DIE attribute has a field specifying the attribute kind,
2591 a link to the next attribute in the chain, and an attribute value.
2592 Attributes are typically linked below the DIE they modify. */
2594 typedef struct GTY(()) dw_attr_struct {
2595 enum dwarf_attribute dw_attr;
2596 dw_val_node dw_attr_val;
2598 dw_attr_node;
2601 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2602 The children of each node form a circular list linked by
2603 die_sib. die_child points to the node *before* the "first" child node. */
2605 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2606 union die_symbol_or_type_node
2608 const char * GTY ((tag ("0"))) die_symbol;
2609 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2611 GTY ((desc ("%0.comdat_type_p"))) die_id;
2612 vec<dw_attr_node, va_gc> *die_attr;
2613 dw_die_ref die_parent;
2614 dw_die_ref die_child;
2615 dw_die_ref die_sib;
2616 dw_die_ref die_definition; /* ref from a specification to its definition */
2617 dw_offset die_offset;
2618 unsigned long die_abbrev;
2619 int die_mark;
2620 unsigned int decl_id;
2621 enum dwarf_tag die_tag;
2622 /* Die is used and must not be pruned as unused. */
2623 BOOL_BITFIELD die_perennial_p : 1;
2624 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2625 /* Lots of spare bits. */
2627 die_node;
2629 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2630 #define FOR_EACH_CHILD(die, c, expr) do { \
2631 c = die->die_child; \
2632 if (c) do { \
2633 c = c->die_sib; \
2634 expr; \
2635 } while (c != die->die_child); \
2636 } while (0)
2638 /* The pubname structure */
2640 typedef struct GTY(()) pubname_struct {
2641 dw_die_ref die;
2642 const char *name;
2644 pubname_entry;
2647 struct GTY(()) dw_ranges_struct {
2648 /* If this is positive, it's a block number, otherwise it's a
2649 bitwise-negated index into dw_ranges_by_label. */
2650 int num;
2653 /* A structure to hold a macinfo entry. */
2655 typedef struct GTY(()) macinfo_struct {
2656 unsigned char code;
2657 unsigned HOST_WIDE_INT lineno;
2658 const char *info;
2660 macinfo_entry;
2663 struct GTY(()) dw_ranges_by_label_struct {
2664 const char *begin;
2665 const char *end;
2668 /* The comdat type node structure. */
2669 typedef struct GTY(()) comdat_type_struct
2671 dw_die_ref root_die;
2672 dw_die_ref type_die;
2673 dw_die_ref skeleton_die;
2674 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2675 struct comdat_type_struct *next;
2677 comdat_type_node;
2679 /* The limbo die list structure. */
2680 typedef struct GTY(()) limbo_die_struct {
2681 dw_die_ref die;
2682 tree created_for;
2683 struct limbo_die_struct *next;
2685 limbo_die_node;
2687 typedef struct skeleton_chain_struct
2689 dw_die_ref old_die;
2690 dw_die_ref new_die;
2691 struct skeleton_chain_struct *parent;
2693 skeleton_chain_node;
2695 /* Define a macro which returns nonzero for a TYPE_DECL which was
2696 implicitly generated for a type.
2698 Note that, unlike the C front-end (which generates a NULL named
2699 TYPE_DECL node for each complete tagged type, each array type,
2700 and each function type node created) the C++ front-end generates
2701 a _named_ TYPE_DECL node for each tagged type node created.
2702 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2703 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2704 front-end, but for each type, tagged or not. */
2706 #define TYPE_DECL_IS_STUB(decl) \
2707 (DECL_NAME (decl) == NULL_TREE \
2708 || (DECL_ARTIFICIAL (decl) \
2709 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2710 /* This is necessary for stub decls that \
2711 appear in nested inline functions. */ \
2712 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2713 && (decl_ultimate_origin (decl) \
2714 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2716 /* Information concerning the compilation unit's programming
2717 language, and compiler version. */
2719 /* Fixed size portion of the DWARF compilation unit header. */
2720 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2721 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2723 /* Fixed size portion of the DWARF comdat type unit header. */
2724 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2725 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2726 + DWARF_OFFSET_SIZE)
2728 /* Fixed size portion of public names info. */
2729 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2731 /* Fixed size portion of the address range info. */
2732 #define DWARF_ARANGES_HEADER_SIZE \
2733 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2734 DWARF2_ADDR_SIZE * 2) \
2735 - DWARF_INITIAL_LENGTH_SIZE)
2737 /* Size of padding portion in the address range info. It must be
2738 aligned to twice the pointer size. */
2739 #define DWARF_ARANGES_PAD_SIZE \
2740 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2741 DWARF2_ADDR_SIZE * 2) \
2742 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2744 /* Use assembler line directives if available. */
2745 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2746 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2747 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2748 #else
2749 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2750 #endif
2751 #endif
2753 /* Minimum line offset in a special line info. opcode.
2754 This value was chosen to give a reasonable range of values. */
2755 #define DWARF_LINE_BASE -10
2757 /* First special line opcode - leave room for the standard opcodes. */
2758 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2760 /* Range of line offsets in a special line info. opcode. */
2761 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2763 /* Flag that indicates the initial value of the is_stmt_start flag.
2764 In the present implementation, we do not mark any lines as
2765 the beginning of a source statement, because that information
2766 is not made available by the GCC front-end. */
2767 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2769 /* Maximum number of operations per instruction bundle. */
2770 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2771 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2772 #endif
2774 /* This location is used by calc_die_sizes() to keep track
2775 the offset of each DIE within the .debug_info section. */
2776 static unsigned long next_die_offset;
2778 /* Record the root of the DIE's built for the current compilation unit. */
2779 static GTY(()) dw_die_ref single_comp_unit_die;
2781 /* A list of type DIEs that have been separated into comdat sections. */
2782 static GTY(()) comdat_type_node *comdat_type_list;
2784 /* A list of DIEs with a NULL parent waiting to be relocated. */
2785 static GTY(()) limbo_die_node *limbo_die_list;
2787 /* A list of DIEs for which we may have to generate
2788 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2789 static GTY(()) limbo_die_node *deferred_asm_name;
2791 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2793 typedef const char *compare_type;
2795 static hashval_t hash (dwarf_file_data *);
2796 static bool equal (dwarf_file_data *, const char *);
2799 /* Filenames referenced by this compilation unit. */
2800 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2802 struct decl_die_hasher : ggc_hasher<die_node *>
2804 typedef tree compare_type;
2806 static hashval_t hash (die_node *);
2807 static bool equal (die_node *, tree);
2809 /* A hash table of references to DIE's that describe declarations.
2810 The key is a DECL_UID() which is a unique number identifying each decl. */
2811 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2813 struct block_die_hasher : ggc_hasher<die_struct *>
2815 static hashval_t hash (die_struct *);
2816 static bool equal (die_struct *, die_struct *);
2819 /* A hash table of references to DIE's that describe COMMON blocks.
2820 The key is DECL_UID() ^ die_parent. */
2821 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2823 typedef struct GTY(()) die_arg_entry_struct {
2824 dw_die_ref die;
2825 tree arg;
2826 } die_arg_entry;
2829 /* Node of the variable location list. */
2830 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2831 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2832 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2833 in mode of the EXPR_LIST node and first EXPR_LIST operand
2834 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2835 location or NULL for padding. For larger bitsizes,
2836 mode is 0 and first operand is a CONCAT with bitsize
2837 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2838 NULL as second operand. */
2839 rtx GTY (()) loc;
2840 const char * GTY (()) label;
2841 struct var_loc_node * GTY (()) next;
2844 /* Variable location list. */
2845 struct GTY ((for_user)) var_loc_list_def {
2846 struct var_loc_node * GTY (()) first;
2848 /* Pointer to the last but one or last element of the
2849 chained list. If the list is empty, both first and
2850 last are NULL, if the list contains just one node
2851 or the last node certainly is not redundant, it points
2852 to the last node, otherwise points to the last but one.
2853 Do not mark it for GC because it is marked through the chain. */
2854 struct var_loc_node * GTY ((skip ("%h"))) last;
2856 /* Pointer to the last element before section switch,
2857 if NULL, either sections weren't switched or first
2858 is after section switch. */
2859 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2861 /* DECL_UID of the variable decl. */
2862 unsigned int decl_id;
2864 typedef struct var_loc_list_def var_loc_list;
2866 /* Call argument location list. */
2867 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2868 rtx GTY (()) call_arg_loc_note;
2869 const char * GTY (()) label;
2870 tree GTY (()) block;
2871 bool tail_call_p;
2872 rtx GTY (()) symbol_ref;
2873 struct call_arg_loc_node * GTY (()) next;
2877 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2879 typedef const_tree compare_type;
2881 static hashval_t hash (var_loc_list *);
2882 static bool equal (var_loc_list *, const_tree);
2885 /* Table of decl location linked lists. */
2886 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2888 /* Head and tail of call_arg_loc chain. */
2889 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2890 static struct call_arg_loc_node *call_arg_loc_last;
2892 /* Number of call sites in the current function. */
2893 static int call_site_count = -1;
2894 /* Number of tail call sites in the current function. */
2895 static int tail_call_site_count = -1;
2897 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2898 DIEs. */
2899 static vec<dw_die_ref> block_map;
2901 /* A cached location list. */
2902 struct GTY ((for_user)) cached_dw_loc_list_def {
2903 /* The DECL_UID of the decl that this entry describes. */
2904 unsigned int decl_id;
2906 /* The cached location list. */
2907 dw_loc_list_ref loc_list;
2909 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2911 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2914 typedef const_tree compare_type;
2916 static hashval_t hash (cached_dw_loc_list *);
2917 static bool equal (cached_dw_loc_list *, const_tree);
2920 /* Table of cached location lists. */
2921 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2923 /* A pointer to the base of a list of references to DIE's that
2924 are uniquely identified by their tag, presence/absence of
2925 children DIE's, and list of attribute/value pairs. */
2926 static GTY((length ("abbrev_die_table_allocated")))
2927 dw_die_ref *abbrev_die_table;
2929 /* Number of elements currently allocated for abbrev_die_table. */
2930 static GTY(()) unsigned abbrev_die_table_allocated;
2932 /* Number of elements in type_die_table currently in use. */
2933 static GTY(()) unsigned abbrev_die_table_in_use;
2935 /* Size (in elements) of increments by which we may expand the
2936 abbrev_die_table. */
2937 #define ABBREV_DIE_TABLE_INCREMENT 256
2939 /* A global counter for generating labels for line number data. */
2940 static unsigned int line_info_label_num;
2942 /* The current table to which we should emit line number information
2943 for the current function. This will be set up at the beginning of
2944 assembly for the function. */
2945 static dw_line_info_table *cur_line_info_table;
2947 /* The two default tables of line number info. */
2948 static GTY(()) dw_line_info_table *text_section_line_info;
2949 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2951 /* The set of all non-default tables of line number info. */
2952 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2954 /* A flag to tell pubnames/types export if there is an info section to
2955 refer to. */
2956 static bool info_section_emitted;
2958 /* A pointer to the base of a table that contains a list of publicly
2959 accessible names. */
2960 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2962 /* A pointer to the base of a table that contains a list of publicly
2963 accessible types. */
2964 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2966 /* A pointer to the base of a table that contains a list of macro
2967 defines/undefines (and file start/end markers). */
2968 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2970 /* True if .debug_macinfo or .debug_macros section is going to be
2971 emitted. */
2972 #define have_macinfo \
2973 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2974 && !macinfo_table->is_empty ())
2976 /* Array of dies for which we should generate .debug_ranges info. */
2977 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2979 /* Number of elements currently allocated for ranges_table. */
2980 static GTY(()) unsigned ranges_table_allocated;
2982 /* Number of elements in ranges_table currently in use. */
2983 static GTY(()) unsigned ranges_table_in_use;
2985 /* Array of pairs of labels referenced in ranges_table. */
2986 static GTY ((length ("ranges_by_label_allocated")))
2987 dw_ranges_by_label_ref ranges_by_label;
2989 /* Number of elements currently allocated for ranges_by_label. */
2990 static GTY(()) unsigned ranges_by_label_allocated;
2992 /* Number of elements in ranges_by_label currently in use. */
2993 static GTY(()) unsigned ranges_by_label_in_use;
2995 /* Size (in elements) of increments by which we may expand the
2996 ranges_table. */
2997 #define RANGES_TABLE_INCREMENT 64
2999 /* Whether we have location lists that need outputting */
3000 static GTY(()) bool have_location_lists;
3002 /* Unique label counter. */
3003 static GTY(()) unsigned int loclabel_num;
3005 /* Unique label counter for point-of-call tables. */
3006 static GTY(()) unsigned int poc_label_num;
3008 /* The last file entry emitted by maybe_emit_file(). */
3009 static GTY(()) struct dwarf_file_data * last_emitted_file;
3011 /* Number of internal labels generated by gen_internal_sym(). */
3012 static GTY(()) int label_num;
3014 /* Cached result of previous call to lookup_filename. */
3015 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3017 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3019 /* Instances of generic types for which we need to generate debug
3020 info that describe their generic parameters and arguments. That
3021 generation needs to happen once all types are properly laid out so
3022 we do it at the end of compilation. */
3023 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3025 /* Offset from the "steady-state frame pointer" to the frame base,
3026 within the current function. */
3027 static HOST_WIDE_INT frame_pointer_fb_offset;
3028 static bool frame_pointer_fb_offset_valid;
3030 static vec<dw_die_ref> base_types;
3032 /* Forward declarations for functions defined in this file. */
3034 static int is_pseudo_reg (const_rtx);
3035 static tree type_main_variant (tree);
3036 static int is_tagged_type (const_tree);
3037 static const char *dwarf_tag_name (unsigned);
3038 static const char *dwarf_attr_name (unsigned);
3039 static const char *dwarf_form_name (unsigned);
3040 static tree decl_ultimate_origin (const_tree);
3041 static tree decl_class_context (tree);
3042 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3043 static inline enum dw_val_class AT_class (dw_attr_ref);
3044 static inline unsigned int AT_index (dw_attr_ref);
3045 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3046 static inline unsigned AT_flag (dw_attr_ref);
3047 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3048 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3049 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3050 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3051 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3052 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3053 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3054 unsigned int, unsigned char *);
3055 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3056 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3057 static inline const char *AT_string (dw_attr_ref);
3058 static enum dwarf_form AT_string_form (dw_attr_ref);
3059 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3060 static void add_AT_specification (dw_die_ref, dw_die_ref);
3061 static inline dw_die_ref AT_ref (dw_attr_ref);
3062 static inline int AT_ref_external (dw_attr_ref);
3063 static inline void set_AT_ref_external (dw_attr_ref, int);
3064 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3065 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3066 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3067 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3068 dw_loc_list_ref);
3069 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3070 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3071 static void remove_addr_table_entry (addr_table_entry *);
3072 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3073 static inline rtx AT_addr (dw_attr_ref);
3074 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3075 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3076 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3077 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3078 unsigned HOST_WIDE_INT);
3079 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3080 unsigned long, bool);
3081 static inline const char *AT_lbl (dw_attr_ref);
3082 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3083 static const char *get_AT_low_pc (dw_die_ref);
3084 static const char *get_AT_hi_pc (dw_die_ref);
3085 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3086 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3087 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3088 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3089 static bool is_cxx (void);
3090 static bool is_fortran (void);
3091 static bool is_ada (void);
3092 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3093 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3094 static void add_child_die (dw_die_ref, dw_die_ref);
3095 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3096 static dw_die_ref lookup_type_die (tree);
3097 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3098 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3099 static void equate_type_number_to_die (tree, dw_die_ref);
3100 static dw_die_ref lookup_decl_die (tree);
3101 static var_loc_list *lookup_decl_loc (const_tree);
3102 static void equate_decl_number_to_die (tree, dw_die_ref);
3103 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3104 static void print_spaces (FILE *);
3105 static void print_die (dw_die_ref, FILE *);
3106 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3107 static dw_die_ref pop_compile_unit (dw_die_ref);
3108 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3109 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3110 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3111 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3112 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3113 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3114 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3115 struct md5_ctx *, int *);
3116 struct checksum_attributes;
3117 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3118 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3119 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3120 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3121 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3122 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3123 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3124 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3125 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3126 static void compute_section_prefix (dw_die_ref);
3127 static int is_type_die (dw_die_ref);
3128 static int is_comdat_die (dw_die_ref);
3129 static int is_symbol_die (dw_die_ref);
3130 static inline bool is_template_instantiation (dw_die_ref);
3131 static void assign_symbol_names (dw_die_ref);
3132 static void break_out_includes (dw_die_ref);
3133 static int is_declaration_die (dw_die_ref);
3134 static int should_move_die_to_comdat (dw_die_ref);
3135 static dw_die_ref clone_as_declaration (dw_die_ref);
3136 static dw_die_ref clone_die (dw_die_ref);
3137 static dw_die_ref clone_tree (dw_die_ref);
3138 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3139 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3140 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3141 static dw_die_ref generate_skeleton (dw_die_ref);
3142 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3143 dw_die_ref,
3144 dw_die_ref);
3145 static void break_out_comdat_types (dw_die_ref);
3146 static void copy_decls_for_unworthy_types (dw_die_ref);
3148 static void add_sibling_attributes (dw_die_ref);
3149 static void output_location_lists (dw_die_ref);
3150 static int constant_size (unsigned HOST_WIDE_INT);
3151 static unsigned long size_of_die (dw_die_ref);
3152 static void calc_die_sizes (dw_die_ref);
3153 static void calc_base_type_die_sizes (void);
3154 static void mark_dies (dw_die_ref);
3155 static void unmark_dies (dw_die_ref);
3156 static void unmark_all_dies (dw_die_ref);
3157 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3158 static unsigned long size_of_aranges (void);
3159 static enum dwarf_form value_format (dw_attr_ref);
3160 static void output_value_format (dw_attr_ref);
3161 static void output_abbrev_section (void);
3162 static void output_die_abbrevs (unsigned long, dw_die_ref);
3163 static void output_die_symbol (dw_die_ref);
3164 static void output_die (dw_die_ref);
3165 static void output_compilation_unit_header (void);
3166 static void output_comp_unit (dw_die_ref, int);
3167 static void output_comdat_type_unit (comdat_type_node *);
3168 static const char *dwarf2_name (tree, int);
3169 static void add_pubname (tree, dw_die_ref);
3170 static void add_enumerator_pubname (const char *, dw_die_ref);
3171 static void add_pubname_string (const char *, dw_die_ref);
3172 static void add_pubtype (tree, dw_die_ref);
3173 static void output_pubnames (vec<pubname_entry, va_gc> *);
3174 static void output_aranges (unsigned long);
3175 static unsigned int add_ranges_num (int);
3176 static unsigned int add_ranges (const_tree);
3177 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3178 bool *, bool);
3179 static void output_ranges (void);
3180 static dw_line_info_table *new_line_info_table (void);
3181 static void output_line_info (bool);
3182 static void output_file_names (void);
3183 static dw_die_ref base_type_die (tree);
3184 static int is_base_type (tree);
3185 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3186 static int decl_quals (const_tree);
3187 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3188 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3189 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3190 static int type_is_enum (const_tree);
3191 static unsigned int dbx_reg_number (const_rtx);
3192 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3193 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3194 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3195 enum var_init_status);
3196 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3197 enum var_init_status);
3198 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3199 enum var_init_status);
3200 static int is_based_loc (const_rtx);
3201 static bool resolve_one_addr (rtx *);
3202 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3203 enum var_init_status);
3204 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3205 enum var_init_status);
3206 static dw_loc_list_ref loc_list_from_tree (tree, int);
3207 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3208 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3209 static tree field_type (const_tree);
3210 static unsigned int simple_type_align_in_bits (const_tree);
3211 static unsigned int simple_decl_align_in_bits (const_tree);
3212 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3213 static HOST_WIDE_INT field_byte_offset (const_tree);
3214 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3215 dw_loc_list_ref);
3216 static void add_data_member_location_attribute (dw_die_ref, tree);
3217 static bool add_const_value_attribute (dw_die_ref, rtx);
3218 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3219 static void insert_wide_int (const wide_int &, unsigned char *, int);
3220 static void insert_float (const_rtx, unsigned char *);
3221 static rtx rtl_for_decl_location (tree);
3222 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3223 enum dwarf_attribute);
3224 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3225 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3226 static void add_name_attribute (dw_die_ref, const char *);
3227 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3228 static void add_comp_dir_attribute (dw_die_ref);
3229 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3230 static void add_subscript_info (dw_die_ref, tree, bool);
3231 static void add_byte_size_attribute (dw_die_ref, tree);
3232 static void add_bit_offset_attribute (dw_die_ref, tree);
3233 static void add_bit_size_attribute (dw_die_ref, tree);
3234 static void add_prototyped_attribute (dw_die_ref, tree);
3235 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3236 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3237 static void add_src_coords_attributes (dw_die_ref, tree);
3238 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3239 static void push_decl_scope (tree);
3240 static void pop_decl_scope (void);
3241 static dw_die_ref scope_die_for (tree, dw_die_ref);
3242 static inline int local_scope_p (dw_die_ref);
3243 static inline int class_scope_p (dw_die_ref);
3244 static inline int class_or_namespace_scope_p (dw_die_ref);
3245 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3246 static void add_calling_convention_attribute (dw_die_ref, tree);
3247 static const char *type_tag (const_tree);
3248 static tree member_declared_type (const_tree);
3249 #if 0
3250 static const char *decl_start_label (tree);
3251 #endif
3252 static void gen_array_type_die (tree, dw_die_ref);
3253 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3254 #if 0
3255 static void gen_entry_point_die (tree, dw_die_ref);
3256 #endif
3257 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3258 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3259 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3260 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3261 static void gen_formal_types_die (tree, dw_die_ref);
3262 static void gen_subprogram_die (tree, dw_die_ref);
3263 static void gen_variable_die (tree, tree, dw_die_ref);
3264 static void gen_const_die (tree, dw_die_ref);
3265 static void gen_label_die (tree, dw_die_ref);
3266 static void gen_lexical_block_die (tree, dw_die_ref, int);
3267 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3268 static void gen_field_die (tree, dw_die_ref);
3269 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3270 static dw_die_ref gen_compile_unit_die (const char *);
3271 static void gen_inheritance_die (tree, tree, dw_die_ref);
3272 static void gen_member_die (tree, dw_die_ref);
3273 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3274 enum debug_info_usage);
3275 static void gen_subroutine_type_die (tree, dw_die_ref);
3276 static void gen_typedef_die (tree, dw_die_ref);
3277 static void gen_type_die (tree, dw_die_ref);
3278 static void gen_block_die (tree, dw_die_ref, int);
3279 static void decls_for_scope (tree, dw_die_ref, int);
3280 static inline int is_redundant_typedef (const_tree);
3281 static bool is_naming_typedef_decl (const_tree);
3282 static inline dw_die_ref get_context_die (tree);
3283 static void gen_namespace_die (tree, dw_die_ref);
3284 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3285 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3286 static dw_die_ref force_decl_die (tree);
3287 static dw_die_ref force_type_die (tree);
3288 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3289 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3290 static struct dwarf_file_data * lookup_filename (const char *);
3291 static void retry_incomplete_types (void);
3292 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3293 static void gen_generic_params_dies (tree);
3294 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3295 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3296 static void splice_child_die (dw_die_ref, dw_die_ref);
3297 static int file_info_cmp (const void *, const void *);
3298 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3299 const char *, const char *);
3300 static void output_loc_list (dw_loc_list_ref);
3301 static char *gen_internal_sym (const char *);
3302 static bool want_pubnames (void);
3304 static void prune_unmark_dies (dw_die_ref);
3305 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3306 static void prune_unused_types_mark (dw_die_ref, int);
3307 static void prune_unused_types_walk (dw_die_ref);
3308 static void prune_unused_types_walk_attribs (dw_die_ref);
3309 static void prune_unused_types_prune (dw_die_ref);
3310 static void prune_unused_types (void);
3311 static int maybe_emit_file (struct dwarf_file_data *fd);
3312 static inline const char *AT_vms_delta1 (dw_attr_ref);
3313 static inline const char *AT_vms_delta2 (dw_attr_ref);
3314 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3315 const char *, const char *);
3316 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3317 static void gen_remaining_tmpl_value_param_die_attribute (void);
3318 static bool generic_type_p (tree);
3319 static void schedule_generic_params_dies_gen (tree t);
3320 static void gen_scheduled_generic_parms_dies (void);
3322 static const char *comp_dir_string (void);
3324 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3326 /* enum for tracking thread-local variables whose address is really an offset
3327 relative to the TLS pointer, which will need link-time relocation, but will
3328 not need relocation by the DWARF consumer. */
3330 enum dtprel_bool
3332 dtprel_false = 0,
3333 dtprel_true = 1
3336 /* Return the operator to use for an address of a variable. For dtprel_true, we
3337 use DW_OP_const*. For regular variables, which need both link-time
3338 relocation and consumer-level relocation (e.g., to account for shared objects
3339 loaded at a random address), we use DW_OP_addr*. */
3341 static inline enum dwarf_location_atom
3342 dw_addr_op (enum dtprel_bool dtprel)
3344 if (dtprel == dtprel_true)
3345 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3346 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3347 else
3348 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3351 /* Return a pointer to a newly allocated address location description. If
3352 dwarf_split_debug_info is true, then record the address with the appropriate
3353 relocation. */
3354 static inline dw_loc_descr_ref
3355 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3357 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3359 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3360 ref->dw_loc_oprnd1.v.val_addr = addr;
3361 ref->dtprel = dtprel;
3362 if (dwarf_split_debug_info)
3363 ref->dw_loc_oprnd1.val_entry
3364 = add_addr_table_entry (addr,
3365 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3366 else
3367 ref->dw_loc_oprnd1.val_entry = NULL;
3369 return ref;
3372 /* Section names used to hold DWARF debugging information. */
3374 #ifndef DEBUG_INFO_SECTION
3375 #define DEBUG_INFO_SECTION ".debug_info"
3376 #endif
3377 #ifndef DEBUG_DWO_INFO_SECTION
3378 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3379 #endif
3380 #ifndef DEBUG_ABBREV_SECTION
3381 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3382 #endif
3383 #ifndef DEBUG_DWO_ABBREV_SECTION
3384 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3385 #endif
3386 #ifndef DEBUG_ARANGES_SECTION
3387 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3388 #endif
3389 #ifndef DEBUG_ADDR_SECTION
3390 #define DEBUG_ADDR_SECTION ".debug_addr"
3391 #endif
3392 #ifndef DEBUG_NORM_MACINFO_SECTION
3393 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3394 #endif
3395 #ifndef DEBUG_DWO_MACINFO_SECTION
3396 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3397 #endif
3398 #ifndef DEBUG_MACINFO_SECTION
3399 #define DEBUG_MACINFO_SECTION \
3400 (!dwarf_split_debug_info \
3401 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3402 #endif
3403 #ifndef DEBUG_NORM_MACRO_SECTION
3404 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3405 #endif
3406 #ifndef DEBUG_DWO_MACRO_SECTION
3407 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3408 #endif
3409 #ifndef DEBUG_MACRO_SECTION
3410 #define DEBUG_MACRO_SECTION \
3411 (!dwarf_split_debug_info \
3412 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3413 #endif
3414 #ifndef DEBUG_LINE_SECTION
3415 #define DEBUG_LINE_SECTION ".debug_line"
3416 #endif
3417 #ifndef DEBUG_DWO_LINE_SECTION
3418 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3419 #endif
3420 #ifndef DEBUG_LOC_SECTION
3421 #define DEBUG_LOC_SECTION ".debug_loc"
3422 #endif
3423 #ifndef DEBUG_DWO_LOC_SECTION
3424 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3425 #endif
3426 #ifndef DEBUG_PUBNAMES_SECTION
3427 #define DEBUG_PUBNAMES_SECTION \
3428 ((debug_generate_pub_sections == 2) \
3429 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3430 #endif
3431 #ifndef DEBUG_PUBTYPES_SECTION
3432 #define DEBUG_PUBTYPES_SECTION \
3433 ((debug_generate_pub_sections == 2) \
3434 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3435 #endif
3436 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3437 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3438 #ifndef DEBUG_STR_OFFSETS_SECTION
3439 #define DEBUG_STR_OFFSETS_SECTION \
3440 (!dwarf_split_debug_info \
3441 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3442 #endif
3443 #ifndef DEBUG_STR_DWO_SECTION
3444 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3445 #endif
3446 #ifndef DEBUG_STR_SECTION
3447 #define DEBUG_STR_SECTION ".debug_str"
3448 #endif
3449 #ifndef DEBUG_RANGES_SECTION
3450 #define DEBUG_RANGES_SECTION ".debug_ranges"
3451 #endif
3453 /* Standard ELF section names for compiled code and data. */
3454 #ifndef TEXT_SECTION_NAME
3455 #define TEXT_SECTION_NAME ".text"
3456 #endif
3458 /* Section flags for .debug_macinfo/.debug_macro section. */
3459 #define DEBUG_MACRO_SECTION_FLAGS \
3460 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3462 /* Section flags for .debug_str section. */
3463 #define DEBUG_STR_SECTION_FLAGS \
3464 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3465 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3466 : SECTION_DEBUG)
3468 /* Section flags for .debug_str.dwo section. */
3469 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3471 /* Labels we insert at beginning sections we can reference instead of
3472 the section names themselves. */
3474 #ifndef TEXT_SECTION_LABEL
3475 #define TEXT_SECTION_LABEL "Ltext"
3476 #endif
3477 #ifndef COLD_TEXT_SECTION_LABEL
3478 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3479 #endif
3480 #ifndef DEBUG_LINE_SECTION_LABEL
3481 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3482 #endif
3483 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3484 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3485 #endif
3486 #ifndef DEBUG_INFO_SECTION_LABEL
3487 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3488 #endif
3489 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3490 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3491 #endif
3492 #ifndef DEBUG_ABBREV_SECTION_LABEL
3493 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3494 #endif
3495 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3496 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3497 #endif
3498 #ifndef DEBUG_ADDR_SECTION_LABEL
3499 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3500 #endif
3501 #ifndef DEBUG_LOC_SECTION_LABEL
3502 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3503 #endif
3504 #ifndef DEBUG_RANGES_SECTION_LABEL
3505 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3506 #endif
3507 #ifndef DEBUG_MACINFO_SECTION_LABEL
3508 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3509 #endif
3510 #ifndef DEBUG_MACRO_SECTION_LABEL
3511 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3512 #endif
3513 #define SKELETON_COMP_DIE_ABBREV 1
3514 #define SKELETON_TYPE_DIE_ABBREV 2
3516 /* Definitions of defaults for formats and names of various special
3517 (artificial) labels which may be generated within this file (when the -g
3518 options is used and DWARF2_DEBUGGING_INFO is in effect.
3519 If necessary, these may be overridden from within the tm.h file, but
3520 typically, overriding these defaults is unnecessary. */
3522 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3523 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3524 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3525 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3526 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3527 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3528 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3529 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3530 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3531 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3532 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3533 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3534 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3535 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3537 #ifndef TEXT_END_LABEL
3538 #define TEXT_END_LABEL "Letext"
3539 #endif
3540 #ifndef COLD_END_LABEL
3541 #define COLD_END_LABEL "Letext_cold"
3542 #endif
3543 #ifndef BLOCK_BEGIN_LABEL
3544 #define BLOCK_BEGIN_LABEL "LBB"
3545 #endif
3546 #ifndef BLOCK_END_LABEL
3547 #define BLOCK_END_LABEL "LBE"
3548 #endif
3549 #ifndef LINE_CODE_LABEL
3550 #define LINE_CODE_LABEL "LM"
3551 #endif
3554 /* Return the root of the DIE's built for the current compilation unit. */
3555 static dw_die_ref
3556 comp_unit_die (void)
3558 if (!single_comp_unit_die)
3559 single_comp_unit_die = gen_compile_unit_die (NULL);
3560 return single_comp_unit_die;
3563 /* We allow a language front-end to designate a function that is to be
3564 called to "demangle" any name before it is put into a DIE. */
3566 static const char *(*demangle_name_func) (const char *);
3568 void
3569 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3571 demangle_name_func = func;
3574 /* Test if rtl node points to a pseudo register. */
3576 static inline int
3577 is_pseudo_reg (const_rtx rtl)
3579 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3580 || (GET_CODE (rtl) == SUBREG
3581 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3584 /* Return a reference to a type, with its const and volatile qualifiers
3585 removed. */
3587 static inline tree
3588 type_main_variant (tree type)
3590 type = TYPE_MAIN_VARIANT (type);
3592 /* ??? There really should be only one main variant among any group of
3593 variants of a given type (and all of the MAIN_VARIANT values for all
3594 members of the group should point to that one type) but sometimes the C
3595 front-end messes this up for array types, so we work around that bug
3596 here. */
3597 if (TREE_CODE (type) == ARRAY_TYPE)
3598 while (type != TYPE_MAIN_VARIANT (type))
3599 type = TYPE_MAIN_VARIANT (type);
3601 return type;
3604 /* Return nonzero if the given type node represents a tagged type. */
3606 static inline int
3607 is_tagged_type (const_tree type)
3609 enum tree_code code = TREE_CODE (type);
3611 return (code == RECORD_TYPE || code == UNION_TYPE
3612 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3615 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3617 static void
3618 get_ref_die_offset_label (char *label, dw_die_ref ref)
3620 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3623 /* Return die_offset of a DIE reference to a base type. */
3625 static unsigned long int
3626 get_base_type_offset (dw_die_ref ref)
3628 if (ref->die_offset)
3629 return ref->die_offset;
3630 if (comp_unit_die ()->die_abbrev)
3632 calc_base_type_die_sizes ();
3633 gcc_assert (ref->die_offset);
3635 return ref->die_offset;
3638 /* Return die_offset of a DIE reference other than base type. */
3640 static unsigned long int
3641 get_ref_die_offset (dw_die_ref ref)
3643 gcc_assert (ref->die_offset);
3644 return ref->die_offset;
3647 /* Convert a DIE tag into its string name. */
3649 static const char *
3650 dwarf_tag_name (unsigned int tag)
3652 const char *name = get_DW_TAG_name (tag);
3654 if (name != NULL)
3655 return name;
3657 return "DW_TAG_<unknown>";
3660 /* Convert a DWARF attribute code into its string name. */
3662 static const char *
3663 dwarf_attr_name (unsigned int attr)
3665 const char *name;
3667 switch (attr)
3669 #if VMS_DEBUGGING_INFO
3670 case DW_AT_HP_prologue:
3671 return "DW_AT_HP_prologue";
3672 #else
3673 case DW_AT_MIPS_loop_unroll_factor:
3674 return "DW_AT_MIPS_loop_unroll_factor";
3675 #endif
3677 #if VMS_DEBUGGING_INFO
3678 case DW_AT_HP_epilogue:
3679 return "DW_AT_HP_epilogue";
3680 #else
3681 case DW_AT_MIPS_stride:
3682 return "DW_AT_MIPS_stride";
3683 #endif
3686 name = get_DW_AT_name (attr);
3688 if (name != NULL)
3689 return name;
3691 return "DW_AT_<unknown>";
3694 /* Convert a DWARF value form code into its string name. */
3696 static const char *
3697 dwarf_form_name (unsigned int form)
3699 const char *name = get_DW_FORM_name (form);
3701 if (name != NULL)
3702 return name;
3704 return "DW_FORM_<unknown>";
3707 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3708 instance of an inlined instance of a decl which is local to an inline
3709 function, so we have to trace all of the way back through the origin chain
3710 to find out what sort of node actually served as the original seed for the
3711 given block. */
3713 static tree
3714 decl_ultimate_origin (const_tree decl)
3716 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3717 return NULL_TREE;
3719 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3720 we're trying to output the abstract instance of this function. */
3721 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3722 return NULL_TREE;
3724 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3725 most distant ancestor, this should never happen. */
3726 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3728 return DECL_ABSTRACT_ORIGIN (decl);
3731 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3732 of a virtual function may refer to a base class, so we check the 'this'
3733 parameter. */
3735 static tree
3736 decl_class_context (tree decl)
3738 tree context = NULL_TREE;
3740 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3741 context = DECL_CONTEXT (decl);
3742 else
3743 context = TYPE_MAIN_VARIANT
3744 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3746 if (context && !TYPE_P (context))
3747 context = NULL_TREE;
3749 return context;
3752 /* Add an attribute/value pair to a DIE. */
3754 static inline void
3755 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3757 /* Maybe this should be an assert? */
3758 if (die == NULL)
3759 return;
3761 vec_safe_reserve (die->die_attr, 1);
3762 vec_safe_push (die->die_attr, *attr);
3765 static inline enum dw_val_class
3766 AT_class (dw_attr_ref a)
3768 return a->dw_attr_val.val_class;
3771 /* Return the index for any attribute that will be referenced with a
3772 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3773 are stored in dw_attr_val.v.val_str for reference counting
3774 pruning. */
3776 static inline unsigned int
3777 AT_index (dw_attr_ref a)
3779 if (AT_class (a) == dw_val_class_str)
3780 return a->dw_attr_val.v.val_str->index;
3781 else if (a->dw_attr_val.val_entry != NULL)
3782 return a->dw_attr_val.val_entry->index;
3783 return NOT_INDEXED;
3786 /* Add a flag value attribute to a DIE. */
3788 static inline void
3789 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3791 dw_attr_node attr;
3793 attr.dw_attr = attr_kind;
3794 attr.dw_attr_val.val_class = dw_val_class_flag;
3795 attr.dw_attr_val.val_entry = NULL;
3796 attr.dw_attr_val.v.val_flag = flag;
3797 add_dwarf_attr (die, &attr);
3800 static inline unsigned
3801 AT_flag (dw_attr_ref a)
3803 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3804 return a->dw_attr_val.v.val_flag;
3807 /* Add a signed integer attribute value to a DIE. */
3809 static inline void
3810 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3812 dw_attr_node attr;
3814 attr.dw_attr = attr_kind;
3815 attr.dw_attr_val.val_class = dw_val_class_const;
3816 attr.dw_attr_val.val_entry = NULL;
3817 attr.dw_attr_val.v.val_int = int_val;
3818 add_dwarf_attr (die, &attr);
3821 static inline HOST_WIDE_INT
3822 AT_int (dw_attr_ref a)
3824 gcc_assert (a && AT_class (a) == dw_val_class_const);
3825 return a->dw_attr_val.v.val_int;
3828 /* Add an unsigned integer attribute value to a DIE. */
3830 static inline void
3831 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3832 unsigned HOST_WIDE_INT unsigned_val)
3834 dw_attr_node attr;
3836 attr.dw_attr = attr_kind;
3837 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3838 attr.dw_attr_val.val_entry = NULL;
3839 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3840 add_dwarf_attr (die, &attr);
3843 static inline unsigned HOST_WIDE_INT
3844 AT_unsigned (dw_attr_ref a)
3846 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3847 return a->dw_attr_val.v.val_unsigned;
3850 /* Add an unsigned wide integer attribute value to a DIE. */
3852 static inline void
3853 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3854 const wide_int& w)
3856 dw_attr_node attr;
3858 attr.dw_attr = attr_kind;
3859 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3860 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3861 *attr.dw_attr_val.v.val_wide = w;
3862 add_dwarf_attr (die, &attr);
3865 /* Add an unsigned double integer attribute value to a DIE. */
3867 static inline void
3868 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3869 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3871 dw_attr_node attr;
3873 attr.dw_attr = attr_kind;
3874 attr.dw_attr_val.val_class = dw_val_class_const_double;
3875 attr.dw_attr_val.val_entry = NULL;
3876 attr.dw_attr_val.v.val_double.high = high;
3877 attr.dw_attr_val.v.val_double.low = low;
3878 add_dwarf_attr (die, &attr);
3881 /* Add a floating point attribute value to a DIE and return it. */
3883 static inline void
3884 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3885 unsigned int length, unsigned int elt_size, unsigned char *array)
3887 dw_attr_node attr;
3889 attr.dw_attr = attr_kind;
3890 attr.dw_attr_val.val_class = dw_val_class_vec;
3891 attr.dw_attr_val.val_entry = NULL;
3892 attr.dw_attr_val.v.val_vec.length = length;
3893 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3894 attr.dw_attr_val.v.val_vec.array = array;
3895 add_dwarf_attr (die, &attr);
3898 /* Add an 8-byte data attribute value to a DIE. */
3900 static inline void
3901 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3902 unsigned char data8[8])
3904 dw_attr_node attr;
3906 attr.dw_attr = attr_kind;
3907 attr.dw_attr_val.val_class = dw_val_class_data8;
3908 attr.dw_attr_val.val_entry = NULL;
3909 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3910 add_dwarf_attr (die, &attr);
3913 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3914 dwarf_split_debug_info, address attributes in dies destined for the
3915 final executable have force_direct set to avoid using indexed
3916 references. */
3918 static inline void
3919 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3920 bool force_direct)
3922 dw_attr_node attr;
3923 char * lbl_id;
3925 lbl_id = xstrdup (lbl_low);
3926 attr.dw_attr = DW_AT_low_pc;
3927 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3928 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3929 if (dwarf_split_debug_info && !force_direct)
3930 attr.dw_attr_val.val_entry
3931 = add_addr_table_entry (lbl_id, ate_kind_label);
3932 else
3933 attr.dw_attr_val.val_entry = NULL;
3934 add_dwarf_attr (die, &attr);
3936 attr.dw_attr = DW_AT_high_pc;
3937 if (dwarf_version < 4)
3938 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3939 else
3940 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3941 lbl_id = xstrdup (lbl_high);
3942 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3943 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3944 && dwarf_split_debug_info && !force_direct)
3945 attr.dw_attr_val.val_entry
3946 = add_addr_table_entry (lbl_id, ate_kind_label);
3947 else
3948 attr.dw_attr_val.val_entry = NULL;
3949 add_dwarf_attr (die, &attr);
3952 /* Hash and equality functions for debug_str_hash. */
3954 hashval_t
3955 indirect_string_hasher::hash (indirect_string_node *x)
3957 return htab_hash_string (x->str);
3960 bool
3961 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3963 return strcmp (x1->str, x2) == 0;
3966 /* Add STR to the given string hash table. */
3968 static struct indirect_string_node *
3969 find_AT_string_in_table (const char *str,
3970 hash_table<indirect_string_hasher> *table)
3972 struct indirect_string_node *node;
3974 indirect_string_node **slot
3975 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
3976 if (*slot == NULL)
3978 node = ggc_cleared_alloc<indirect_string_node> ();
3979 node->str = ggc_strdup (str);
3980 *slot = node;
3982 else
3983 node = *slot;
3985 node->refcount++;
3986 return node;
3989 /* Add STR to the indirect string hash table. */
3991 static struct indirect_string_node *
3992 find_AT_string (const char *str)
3994 if (! debug_str_hash)
3995 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
3997 return find_AT_string_in_table (str, debug_str_hash);
4000 /* Add a string attribute value to a DIE. */
4002 static inline void
4003 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4005 dw_attr_node attr;
4006 struct indirect_string_node *node;
4008 node = find_AT_string (str);
4010 attr.dw_attr = attr_kind;
4011 attr.dw_attr_val.val_class = dw_val_class_str;
4012 attr.dw_attr_val.val_entry = NULL;
4013 attr.dw_attr_val.v.val_str = node;
4014 add_dwarf_attr (die, &attr);
4017 static inline const char *
4018 AT_string (dw_attr_ref a)
4020 gcc_assert (a && AT_class (a) == dw_val_class_str);
4021 return a->dw_attr_val.v.val_str->str;
4024 /* Call this function directly to bypass AT_string_form's logic to put
4025 the string inline in the die. */
4027 static void
4028 set_indirect_string (struct indirect_string_node *node)
4030 char label[32];
4031 /* Already indirect is a no op. */
4032 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4034 gcc_assert (node->label);
4035 return;
4037 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4038 ++dw2_string_counter;
4039 node->label = xstrdup (label);
4041 if (!dwarf_split_debug_info)
4043 node->form = DW_FORM_strp;
4044 node->index = NOT_INDEXED;
4046 else
4048 node->form = DW_FORM_GNU_str_index;
4049 node->index = NO_INDEX_ASSIGNED;
4053 /* Find out whether a string should be output inline in DIE
4054 or out-of-line in .debug_str section. */
4056 static enum dwarf_form
4057 find_string_form (struct indirect_string_node *node)
4059 unsigned int len;
4061 if (node->form)
4062 return node->form;
4064 len = strlen (node->str) + 1;
4066 /* If the string is shorter or equal to the size of the reference, it is
4067 always better to put it inline. */
4068 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4069 return node->form = DW_FORM_string;
4071 /* If we cannot expect the linker to merge strings in .debug_str
4072 section, only put it into .debug_str if it is worth even in this
4073 single module. */
4074 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4075 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4076 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4077 return node->form = DW_FORM_string;
4079 set_indirect_string (node);
4081 return node->form;
4084 /* Find out whether the string referenced from the attribute should be
4085 output inline in DIE or out-of-line in .debug_str section. */
4087 static enum dwarf_form
4088 AT_string_form (dw_attr_ref a)
4090 gcc_assert (a && AT_class (a) == dw_val_class_str);
4091 return find_string_form (a->dw_attr_val.v.val_str);
4094 /* Add a DIE reference attribute value to a DIE. */
4096 static inline void
4097 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4099 dw_attr_node attr;
4101 #ifdef ENABLE_CHECKING
4102 gcc_assert (targ_die != NULL);
4103 #else
4104 /* With LTO we can end up trying to reference something we didn't create
4105 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4106 if (targ_die == NULL)
4107 return;
4108 #endif
4110 attr.dw_attr = attr_kind;
4111 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4112 attr.dw_attr_val.val_entry = NULL;
4113 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4114 attr.dw_attr_val.v.val_die_ref.external = 0;
4115 add_dwarf_attr (die, &attr);
4118 /* Change DIE reference REF to point to NEW_DIE instead. */
4120 static inline void
4121 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4123 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4124 ref->dw_attr_val.v.val_die_ref.die = new_die;
4125 ref->dw_attr_val.v.val_die_ref.external = 0;
4128 /* Add an AT_specification attribute to a DIE, and also make the back
4129 pointer from the specification to the definition. */
4131 static inline void
4132 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4134 add_AT_die_ref (die, DW_AT_specification, targ_die);
4135 gcc_assert (!targ_die->die_definition);
4136 targ_die->die_definition = die;
4139 static inline dw_die_ref
4140 AT_ref (dw_attr_ref a)
4142 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4143 return a->dw_attr_val.v.val_die_ref.die;
4146 static inline int
4147 AT_ref_external (dw_attr_ref a)
4149 if (a && AT_class (a) == dw_val_class_die_ref)
4150 return a->dw_attr_val.v.val_die_ref.external;
4152 return 0;
4155 static inline void
4156 set_AT_ref_external (dw_attr_ref a, int i)
4158 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4159 a->dw_attr_val.v.val_die_ref.external = i;
4162 /* Add an FDE reference attribute value to a DIE. */
4164 static inline void
4165 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4167 dw_attr_node attr;
4169 attr.dw_attr = attr_kind;
4170 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4171 attr.dw_attr_val.val_entry = NULL;
4172 attr.dw_attr_val.v.val_fde_index = targ_fde;
4173 add_dwarf_attr (die, &attr);
4176 /* Add a location description attribute value to a DIE. */
4178 static inline void
4179 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4181 dw_attr_node attr;
4183 attr.dw_attr = attr_kind;
4184 attr.dw_attr_val.val_class = dw_val_class_loc;
4185 attr.dw_attr_val.val_entry = NULL;
4186 attr.dw_attr_val.v.val_loc = loc;
4187 add_dwarf_attr (die, &attr);
4190 static inline dw_loc_descr_ref
4191 AT_loc (dw_attr_ref a)
4193 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4194 return a->dw_attr_val.v.val_loc;
4197 static inline void
4198 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4200 dw_attr_node attr;
4202 attr.dw_attr = attr_kind;
4203 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4204 attr.dw_attr_val.val_entry = NULL;
4205 attr.dw_attr_val.v.val_loc_list = loc_list;
4206 add_dwarf_attr (die, &attr);
4207 have_location_lists = true;
4210 static inline dw_loc_list_ref
4211 AT_loc_list (dw_attr_ref a)
4213 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4214 return a->dw_attr_val.v.val_loc_list;
4217 static inline dw_loc_list_ref *
4218 AT_loc_list_ptr (dw_attr_ref a)
4220 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4221 return &a->dw_attr_val.v.val_loc_list;
4224 struct addr_hasher : ggc_hasher<addr_table_entry *>
4226 static hashval_t hash (addr_table_entry *);
4227 static bool equal (addr_table_entry *, addr_table_entry *);
4230 /* Table of entries into the .debug_addr section. */
4232 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4234 /* Hash an address_table_entry. */
4236 hashval_t
4237 addr_hasher::hash (addr_table_entry *a)
4239 inchash::hash hstate;
4240 switch (a->kind)
4242 case ate_kind_rtx:
4243 hstate.add_int (0);
4244 break;
4245 case ate_kind_rtx_dtprel:
4246 hstate.add_int (1);
4247 break;
4248 case ate_kind_label:
4249 return htab_hash_string (a->addr.label);
4250 default:
4251 gcc_unreachable ();
4253 inchash::add_rtx (a->addr.rtl, hstate);
4254 return hstate.end ();
4257 /* Determine equality for two address_table_entries. */
4259 bool
4260 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4262 if (a1->kind != a2->kind)
4263 return 0;
4264 switch (a1->kind)
4266 case ate_kind_rtx:
4267 case ate_kind_rtx_dtprel:
4268 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4269 case ate_kind_label:
4270 return strcmp (a1->addr.label, a2->addr.label) == 0;
4271 default:
4272 gcc_unreachable ();
4276 /* Initialize an addr_table_entry. */
4278 void
4279 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4281 e->kind = kind;
4282 switch (kind)
4284 case ate_kind_rtx:
4285 case ate_kind_rtx_dtprel:
4286 e->addr.rtl = (rtx) addr;
4287 break;
4288 case ate_kind_label:
4289 e->addr.label = (char *) addr;
4290 break;
4292 e->refcount = 0;
4293 e->index = NO_INDEX_ASSIGNED;
4296 /* Add attr to the address table entry to the table. Defer setting an
4297 index until output time. */
4299 static addr_table_entry *
4300 add_addr_table_entry (void *addr, enum ate_kind kind)
4302 addr_table_entry *node;
4303 addr_table_entry finder;
4305 gcc_assert (dwarf_split_debug_info);
4306 if (! addr_index_table)
4307 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4308 init_addr_table_entry (&finder, kind, addr);
4309 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4311 if (*slot == HTAB_EMPTY_ENTRY)
4313 node = ggc_cleared_alloc<addr_table_entry> ();
4314 init_addr_table_entry (node, kind, addr);
4315 *slot = node;
4317 else
4318 node = *slot;
4320 node->refcount++;
4321 return node;
4324 /* Remove an entry from the addr table by decrementing its refcount.
4325 Strictly, decrementing the refcount would be enough, but the
4326 assertion that the entry is actually in the table has found
4327 bugs. */
4329 static void
4330 remove_addr_table_entry (addr_table_entry *entry)
4332 gcc_assert (dwarf_split_debug_info && addr_index_table);
4333 /* After an index is assigned, the table is frozen. */
4334 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4335 entry->refcount--;
4338 /* Given a location list, remove all addresses it refers to from the
4339 address_table. */
4341 static void
4342 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4344 for (; descr; descr = descr->dw_loc_next)
4345 if (descr->dw_loc_oprnd1.val_entry != NULL)
4347 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4348 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4352 /* A helper function for dwarf2out_finish called through
4353 htab_traverse. Assign an addr_table_entry its index. All entries
4354 must be collected into the table when this function is called,
4355 because the indexing code relies on htab_traverse to traverse nodes
4356 in the same order for each run. */
4359 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4361 addr_table_entry *node = *h;
4363 /* Don't index unreferenced nodes. */
4364 if (node->refcount == 0)
4365 return 1;
4367 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4368 node->index = *index;
4369 *index += 1;
4371 return 1;
4374 /* Add an address constant attribute value to a DIE. When using
4375 dwarf_split_debug_info, address attributes in dies destined for the
4376 final executable should be direct references--setting the parameter
4377 force_direct ensures this behavior. */
4379 static inline void
4380 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4381 bool force_direct)
4383 dw_attr_node attr;
4385 attr.dw_attr = attr_kind;
4386 attr.dw_attr_val.val_class = dw_val_class_addr;
4387 attr.dw_attr_val.v.val_addr = addr;
4388 if (dwarf_split_debug_info && !force_direct)
4389 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4390 else
4391 attr.dw_attr_val.val_entry = NULL;
4392 add_dwarf_attr (die, &attr);
4395 /* Get the RTX from to an address DIE attribute. */
4397 static inline rtx
4398 AT_addr (dw_attr_ref a)
4400 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4401 return a->dw_attr_val.v.val_addr;
4404 /* Add a file attribute value to a DIE. */
4406 static inline void
4407 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4408 struct dwarf_file_data *fd)
4410 dw_attr_node attr;
4412 attr.dw_attr = attr_kind;
4413 attr.dw_attr_val.val_class = dw_val_class_file;
4414 attr.dw_attr_val.val_entry = NULL;
4415 attr.dw_attr_val.v.val_file = fd;
4416 add_dwarf_attr (die, &attr);
4419 /* Get the dwarf_file_data from a file DIE attribute. */
4421 static inline struct dwarf_file_data *
4422 AT_file (dw_attr_ref a)
4424 gcc_assert (a && AT_class (a) == dw_val_class_file);
4425 return a->dw_attr_val.v.val_file;
4428 /* Add a vms delta attribute value to a DIE. */
4430 static inline void
4431 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4432 const char *lbl1, const char *lbl2)
4434 dw_attr_node attr;
4436 attr.dw_attr = attr_kind;
4437 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4438 attr.dw_attr_val.val_entry = NULL;
4439 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4440 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4441 add_dwarf_attr (die, &attr);
4444 /* Add a label identifier attribute value to a DIE. */
4446 static inline void
4447 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4448 const char *lbl_id)
4450 dw_attr_node attr;
4452 attr.dw_attr = attr_kind;
4453 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4454 attr.dw_attr_val.val_entry = NULL;
4455 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4456 if (dwarf_split_debug_info)
4457 attr.dw_attr_val.val_entry
4458 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4459 ate_kind_label);
4460 add_dwarf_attr (die, &attr);
4463 /* Add a section offset attribute value to a DIE, an offset into the
4464 debug_line section. */
4466 static inline void
4467 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4468 const char *label)
4470 dw_attr_node attr;
4472 attr.dw_attr = attr_kind;
4473 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4474 attr.dw_attr_val.val_entry = NULL;
4475 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4476 add_dwarf_attr (die, &attr);
4479 /* Add a section offset attribute value to a DIE, an offset into the
4480 debug_macinfo section. */
4482 static inline void
4483 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4484 const char *label)
4486 dw_attr_node attr;
4488 attr.dw_attr = attr_kind;
4489 attr.dw_attr_val.val_class = dw_val_class_macptr;
4490 attr.dw_attr_val.val_entry = NULL;
4491 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4492 add_dwarf_attr (die, &attr);
4495 /* Add an offset attribute value to a DIE. */
4497 static inline void
4498 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4499 unsigned HOST_WIDE_INT offset)
4501 dw_attr_node attr;
4503 attr.dw_attr = attr_kind;
4504 attr.dw_attr_val.val_class = dw_val_class_offset;
4505 attr.dw_attr_val.val_entry = NULL;
4506 attr.dw_attr_val.v.val_offset = offset;
4507 add_dwarf_attr (die, &attr);
4510 /* Add a range_list attribute value to a DIE. When using
4511 dwarf_split_debug_info, address attributes in dies destined for the
4512 final executable should be direct references--setting the parameter
4513 force_direct ensures this behavior. */
4515 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4516 #define RELOCATED_OFFSET (NULL)
4518 static void
4519 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4520 long unsigned int offset, bool force_direct)
4522 dw_attr_node attr;
4524 attr.dw_attr = attr_kind;
4525 attr.dw_attr_val.val_class = dw_val_class_range_list;
4526 /* For the range_list attribute, use val_entry to store whether the
4527 offset should follow split-debug-info or normal semantics. This
4528 value is read in output_range_list_offset. */
4529 if (dwarf_split_debug_info && !force_direct)
4530 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4531 else
4532 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4533 attr.dw_attr_val.v.val_offset = offset;
4534 add_dwarf_attr (die, &attr);
4537 /* Return the start label of a delta attribute. */
4539 static inline const char *
4540 AT_vms_delta1 (dw_attr_ref a)
4542 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4543 return a->dw_attr_val.v.val_vms_delta.lbl1;
4546 /* Return the end label of a delta attribute. */
4548 static inline const char *
4549 AT_vms_delta2 (dw_attr_ref a)
4551 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4552 return a->dw_attr_val.v.val_vms_delta.lbl2;
4555 static inline const char *
4556 AT_lbl (dw_attr_ref a)
4558 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4559 || AT_class (a) == dw_val_class_lineptr
4560 || AT_class (a) == dw_val_class_macptr
4561 || AT_class (a) == dw_val_class_high_pc));
4562 return a->dw_attr_val.v.val_lbl_id;
4565 /* Get the attribute of type attr_kind. */
4567 static dw_attr_ref
4568 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4570 dw_attr_ref a;
4571 unsigned ix;
4572 dw_die_ref spec = NULL;
4574 if (! die)
4575 return NULL;
4577 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4578 if (a->dw_attr == attr_kind)
4579 return a;
4580 else if (a->dw_attr == DW_AT_specification
4581 || a->dw_attr == DW_AT_abstract_origin)
4582 spec = AT_ref (a);
4584 if (spec)
4585 return get_AT (spec, attr_kind);
4587 return NULL;
4590 /* Returns the parent of the declaration of DIE. */
4592 static dw_die_ref
4593 get_die_parent (dw_die_ref die)
4595 dw_die_ref t;
4597 if (!die)
4598 return NULL;
4600 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4601 || (t = get_AT_ref (die, DW_AT_specification)))
4602 die = t;
4604 return die->die_parent;
4607 /* Return the "low pc" attribute value, typically associated with a subprogram
4608 DIE. Return null if the "low pc" attribute is either not present, or if it
4609 cannot be represented as an assembler label identifier. */
4611 static inline const char *
4612 get_AT_low_pc (dw_die_ref die)
4614 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4616 return a ? AT_lbl (a) : NULL;
4619 /* Return the "high pc" attribute value, typically associated with a subprogram
4620 DIE. Return null if the "high pc" attribute is either not present, or if it
4621 cannot be represented as an assembler label identifier. */
4623 static inline const char *
4624 get_AT_hi_pc (dw_die_ref die)
4626 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4628 return a ? AT_lbl (a) : NULL;
4631 /* Return the value of the string attribute designated by ATTR_KIND, or
4632 NULL if it is not present. */
4634 static inline const char *
4635 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4637 dw_attr_ref a = get_AT (die, attr_kind);
4639 return a ? AT_string (a) : NULL;
4642 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4643 if it is not present. */
4645 static inline int
4646 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4648 dw_attr_ref a = get_AT (die, attr_kind);
4650 return a ? AT_flag (a) : 0;
4653 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4654 if it is not present. */
4656 static inline unsigned
4657 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4659 dw_attr_ref a = get_AT (die, attr_kind);
4661 return a ? AT_unsigned (a) : 0;
4664 static inline dw_die_ref
4665 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4667 dw_attr_ref a = get_AT (die, attr_kind);
4669 return a ? AT_ref (a) : NULL;
4672 static inline struct dwarf_file_data *
4673 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4675 dw_attr_ref a = get_AT (die, attr_kind);
4677 return a ? AT_file (a) : NULL;
4680 /* Return TRUE if the language is C++. */
4682 static inline bool
4683 is_cxx (void)
4685 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4687 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4690 /* Return TRUE if the language is Java. */
4692 static inline bool
4693 is_java (void)
4695 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4697 return lang == DW_LANG_Java;
4700 /* Return TRUE if the language is Fortran. */
4702 static inline bool
4703 is_fortran (void)
4705 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4707 return (lang == DW_LANG_Fortran77
4708 || lang == DW_LANG_Fortran90
4709 || lang == DW_LANG_Fortran95);
4712 /* Return TRUE if the language is Ada. */
4714 static inline bool
4715 is_ada (void)
4717 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4719 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4722 /* Remove the specified attribute if present. */
4724 static void
4725 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4727 dw_attr_ref a;
4728 unsigned ix;
4730 if (! die)
4731 return;
4733 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4734 if (a->dw_attr == attr_kind)
4736 if (AT_class (a) == dw_val_class_str)
4737 if (a->dw_attr_val.v.val_str->refcount)
4738 a->dw_attr_val.v.val_str->refcount--;
4740 /* vec::ordered_remove should help reduce the number of abbrevs
4741 that are needed. */
4742 die->die_attr->ordered_remove (ix);
4743 return;
4747 /* Remove CHILD from its parent. PREV must have the property that
4748 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4750 static void
4751 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4753 gcc_assert (child->die_parent == prev->die_parent);
4754 gcc_assert (prev->die_sib == child);
4755 if (prev == child)
4757 gcc_assert (child->die_parent->die_child == child);
4758 prev = NULL;
4760 else
4761 prev->die_sib = child->die_sib;
4762 if (child->die_parent->die_child == child)
4763 child->die_parent->die_child = prev;
4766 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4767 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4769 static void
4770 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4772 dw_die_ref parent = old_child->die_parent;
4774 gcc_assert (parent == prev->die_parent);
4775 gcc_assert (prev->die_sib == old_child);
4777 new_child->die_parent = parent;
4778 if (prev == old_child)
4780 gcc_assert (parent->die_child == old_child);
4781 new_child->die_sib = new_child;
4783 else
4785 prev->die_sib = new_child;
4786 new_child->die_sib = old_child->die_sib;
4788 if (old_child->die_parent->die_child == old_child)
4789 old_child->die_parent->die_child = new_child;
4792 /* Move all children from OLD_PARENT to NEW_PARENT. */
4794 static void
4795 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4797 dw_die_ref c;
4798 new_parent->die_child = old_parent->die_child;
4799 old_parent->die_child = NULL;
4800 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4803 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4804 matches TAG. */
4806 static void
4807 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4809 dw_die_ref c;
4811 c = die->die_child;
4812 if (c) do {
4813 dw_die_ref prev = c;
4814 c = c->die_sib;
4815 while (c->die_tag == tag)
4817 remove_child_with_prev (c, prev);
4818 /* Might have removed every child. */
4819 if (c == c->die_sib)
4820 return;
4821 c = c->die_sib;
4823 } while (c != die->die_child);
4826 /* Add a CHILD_DIE as the last child of DIE. */
4828 static void
4829 add_child_die (dw_die_ref die, dw_die_ref child_die)
4831 /* FIXME this should probably be an assert. */
4832 if (! die || ! child_die)
4833 return;
4834 gcc_assert (die != child_die);
4836 child_die->die_parent = die;
4837 if (die->die_child)
4839 child_die->die_sib = die->die_child->die_sib;
4840 die->die_child->die_sib = child_die;
4842 else
4843 child_die->die_sib = child_die;
4844 die->die_child = child_die;
4847 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4848 is the specification, to the end of PARENT's list of children.
4849 This is done by removing and re-adding it. */
4851 static void
4852 splice_child_die (dw_die_ref parent, dw_die_ref child)
4854 dw_die_ref p;
4856 /* We want the declaration DIE from inside the class, not the
4857 specification DIE at toplevel. */
4858 if (child->die_parent != parent)
4860 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4862 if (tmp)
4863 child = tmp;
4866 gcc_assert (child->die_parent == parent
4867 || (child->die_parent
4868 == get_AT_ref (parent, DW_AT_specification)));
4870 for (p = child->die_parent->die_child; ; p = p->die_sib)
4871 if (p->die_sib == child)
4873 remove_child_with_prev (child, p);
4874 break;
4877 add_child_die (parent, child);
4880 /* Return a pointer to a newly created DIE node. */
4882 static inline dw_die_ref
4883 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4885 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4887 die->die_tag = tag_value;
4889 if (parent_die != NULL)
4890 add_child_die (parent_die, die);
4891 else
4893 limbo_die_node *limbo_node;
4895 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4896 limbo_node->die = die;
4897 limbo_node->created_for = t;
4898 limbo_node->next = limbo_die_list;
4899 limbo_die_list = limbo_node;
4902 return die;
4905 /* Return the DIE associated with the given type specifier. */
4907 static inline dw_die_ref
4908 lookup_type_die (tree type)
4910 return TYPE_SYMTAB_DIE (type);
4913 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4914 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4915 anonymous type instead the one of the naming typedef. */
4917 static inline dw_die_ref
4918 strip_naming_typedef (tree type, dw_die_ref type_die)
4920 if (type
4921 && TREE_CODE (type) == RECORD_TYPE
4922 && type_die
4923 && type_die->die_tag == DW_TAG_typedef
4924 && is_naming_typedef_decl (TYPE_NAME (type)))
4925 type_die = get_AT_ref (type_die, DW_AT_type);
4926 return type_die;
4929 /* Like lookup_type_die, but if type is an anonymous type named by a
4930 typedef[1], return the DIE of the anonymous type instead the one of
4931 the naming typedef. This is because in gen_typedef_die, we did
4932 equate the anonymous struct named by the typedef with the DIE of
4933 the naming typedef. So by default, lookup_type_die on an anonymous
4934 struct yields the DIE of the naming typedef.
4936 [1]: Read the comment of is_naming_typedef_decl to learn about what
4937 a naming typedef is. */
4939 static inline dw_die_ref
4940 lookup_type_die_strip_naming_typedef (tree type)
4942 dw_die_ref die = lookup_type_die (type);
4943 return strip_naming_typedef (type, die);
4946 /* Equate a DIE to a given type specifier. */
4948 static inline void
4949 equate_type_number_to_die (tree type, dw_die_ref type_die)
4951 TYPE_SYMTAB_DIE (type) = type_die;
4954 /* Returns a hash value for X (which really is a die_struct). */
4956 inline hashval_t
4957 decl_die_hasher::hash (die_node *x)
4959 return (hashval_t) x->decl_id;
4962 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4964 inline bool
4965 decl_die_hasher::equal (die_node *x, tree y)
4967 return (x->decl_id == DECL_UID (y));
4970 /* Return the DIE associated with a given declaration. */
4972 static inline dw_die_ref
4973 lookup_decl_die (tree decl)
4975 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
4978 /* Returns a hash value for X (which really is a var_loc_list). */
4980 inline hashval_t
4981 decl_loc_hasher::hash (var_loc_list *x)
4983 return (hashval_t) x->decl_id;
4986 /* Return nonzero if decl_id of var_loc_list X is the same as
4987 UID of decl *Y. */
4989 inline bool
4990 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
4992 return (x->decl_id == DECL_UID (y));
4995 /* Return the var_loc list associated with a given declaration. */
4997 static inline var_loc_list *
4998 lookup_decl_loc (const_tree decl)
5000 if (!decl_loc_table)
5001 return NULL;
5002 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5005 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5007 inline hashval_t
5008 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5010 return (hashval_t) x->decl_id;
5013 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5014 UID of decl *Y. */
5016 inline bool
5017 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5019 return (x->decl_id == DECL_UID (y));
5022 /* Equate a DIE to a particular declaration. */
5024 static void
5025 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5027 unsigned int decl_id = DECL_UID (decl);
5029 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5030 decl_die->decl_id = decl_id;
5033 /* Return how many bits covers PIECE EXPR_LIST. */
5035 static int
5036 decl_piece_bitsize (rtx piece)
5038 int ret = (int) GET_MODE (piece);
5039 if (ret)
5040 return ret;
5041 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5042 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5043 return INTVAL (XEXP (XEXP (piece, 0), 0));
5046 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5048 static rtx *
5049 decl_piece_varloc_ptr (rtx piece)
5051 if ((int) GET_MODE (piece))
5052 return &XEXP (piece, 0);
5053 else
5054 return &XEXP (XEXP (piece, 0), 1);
5057 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5058 Next is the chain of following piece nodes. */
5060 static rtx_expr_list *
5061 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5063 if (bitsize <= (int) MAX_MACHINE_MODE)
5064 return alloc_EXPR_LIST (bitsize, loc_note, next);
5065 else
5066 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5067 GEN_INT (bitsize),
5068 loc_note), next);
5071 /* Return rtx that should be stored into loc field for
5072 LOC_NOTE and BITPOS/BITSIZE. */
5074 static rtx
5075 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5076 HOST_WIDE_INT bitsize)
5078 if (bitsize != -1)
5080 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5081 if (bitpos != 0)
5082 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5084 return loc_note;
5087 /* This function either modifies location piece list *DEST in
5088 place (if SRC and INNER is NULL), or copies location piece list
5089 *SRC to *DEST while modifying it. Location BITPOS is modified
5090 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5091 not copied and if needed some padding around it is added.
5092 When modifying in place, DEST should point to EXPR_LIST where
5093 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5094 to the start of the whole list and INNER points to the EXPR_LIST
5095 where earlier pieces cover PIECE_BITPOS bits. */
5097 static void
5098 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5099 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5100 HOST_WIDE_INT bitsize, rtx loc_note)
5102 int diff;
5103 bool copy = inner != NULL;
5105 if (copy)
5107 /* First copy all nodes preceding the current bitpos. */
5108 while (src != inner)
5110 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5111 decl_piece_bitsize (*src), NULL_RTX);
5112 dest = &XEXP (*dest, 1);
5113 src = &XEXP (*src, 1);
5116 /* Add padding if needed. */
5117 if (bitpos != piece_bitpos)
5119 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5120 copy ? NULL_RTX : *dest);
5121 dest = &XEXP (*dest, 1);
5123 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5125 gcc_assert (!copy);
5126 /* A piece with correct bitpos and bitsize already exist,
5127 just update the location for it and return. */
5128 *decl_piece_varloc_ptr (*dest) = loc_note;
5129 return;
5131 /* Add the piece that changed. */
5132 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5133 dest = &XEXP (*dest, 1);
5134 /* Skip over pieces that overlap it. */
5135 diff = bitpos - piece_bitpos + bitsize;
5136 if (!copy)
5137 src = dest;
5138 while (diff > 0 && *src)
5140 rtx piece = *src;
5141 diff -= decl_piece_bitsize (piece);
5142 if (copy)
5143 src = &XEXP (piece, 1);
5144 else
5146 *src = XEXP (piece, 1);
5147 free_EXPR_LIST_node (piece);
5150 /* Add padding if needed. */
5151 if (diff < 0 && *src)
5153 if (!copy)
5154 dest = src;
5155 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5156 dest = &XEXP (*dest, 1);
5158 if (!copy)
5159 return;
5160 /* Finally copy all nodes following it. */
5161 while (*src)
5163 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5164 decl_piece_bitsize (*src), NULL_RTX);
5165 dest = &XEXP (*dest, 1);
5166 src = &XEXP (*src, 1);
5170 /* Add a variable location node to the linked list for DECL. */
5172 static struct var_loc_node *
5173 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5175 unsigned int decl_id;
5176 var_loc_list *temp;
5177 struct var_loc_node *loc = NULL;
5178 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5180 if (TREE_CODE (decl) == VAR_DECL
5181 && DECL_HAS_DEBUG_EXPR_P (decl))
5183 tree realdecl = DECL_DEBUG_EXPR (decl);
5184 if (handled_component_p (realdecl)
5185 || (TREE_CODE (realdecl) == MEM_REF
5186 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5188 HOST_WIDE_INT maxsize;
5189 tree innerdecl;
5190 innerdecl
5191 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5192 if (!DECL_P (innerdecl)
5193 || DECL_IGNORED_P (innerdecl)
5194 || TREE_STATIC (innerdecl)
5195 || bitsize <= 0
5196 || bitpos + bitsize > 256
5197 || bitsize != maxsize)
5198 return NULL;
5199 decl = innerdecl;
5203 decl_id = DECL_UID (decl);
5204 var_loc_list **slot
5205 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5206 if (*slot == NULL)
5208 temp = ggc_cleared_alloc<var_loc_list> ();
5209 temp->decl_id = decl_id;
5210 *slot = temp;
5212 else
5213 temp = *slot;
5215 /* For PARM_DECLs try to keep around the original incoming value,
5216 even if that means we'll emit a zero-range .debug_loc entry. */
5217 if (temp->last
5218 && temp->first == temp->last
5219 && TREE_CODE (decl) == PARM_DECL
5220 && NOTE_P (temp->first->loc)
5221 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5222 && DECL_INCOMING_RTL (decl)
5223 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5224 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5225 == GET_CODE (DECL_INCOMING_RTL (decl))
5226 && prev_real_insn (temp->first->loc) == NULL_RTX
5227 && (bitsize != -1
5228 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5229 NOTE_VAR_LOCATION_LOC (loc_note))
5230 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5231 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5233 loc = ggc_cleared_alloc<var_loc_node> ();
5234 temp->first->next = loc;
5235 temp->last = loc;
5236 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5238 else if (temp->last)
5240 struct var_loc_node *last = temp->last, *unused = NULL;
5241 rtx *piece_loc = NULL, last_loc_note;
5242 int piece_bitpos = 0;
5243 if (last->next)
5245 last = last->next;
5246 gcc_assert (last->next == NULL);
5248 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5250 piece_loc = &last->loc;
5253 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5254 if (piece_bitpos + cur_bitsize > bitpos)
5255 break;
5256 piece_bitpos += cur_bitsize;
5257 piece_loc = &XEXP (*piece_loc, 1);
5259 while (*piece_loc);
5261 /* TEMP->LAST here is either pointer to the last but one or
5262 last element in the chained list, LAST is pointer to the
5263 last element. */
5264 if (label && strcmp (last->label, label) == 0)
5266 /* For SRA optimized variables if there weren't any real
5267 insns since last note, just modify the last node. */
5268 if (piece_loc != NULL)
5270 adjust_piece_list (piece_loc, NULL, NULL,
5271 bitpos, piece_bitpos, bitsize, loc_note);
5272 return NULL;
5274 /* If the last note doesn't cover any instructions, remove it. */
5275 if (temp->last != last)
5277 temp->last->next = NULL;
5278 unused = last;
5279 last = temp->last;
5280 gcc_assert (strcmp (last->label, label) != 0);
5282 else
5284 gcc_assert (temp->first == temp->last
5285 || (temp->first->next == temp->last
5286 && TREE_CODE (decl) == PARM_DECL));
5287 memset (temp->last, '\0', sizeof (*temp->last));
5288 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5289 return temp->last;
5292 if (bitsize == -1 && NOTE_P (last->loc))
5293 last_loc_note = last->loc;
5294 else if (piece_loc != NULL
5295 && *piece_loc != NULL_RTX
5296 && piece_bitpos == bitpos
5297 && decl_piece_bitsize (*piece_loc) == bitsize)
5298 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5299 else
5300 last_loc_note = NULL_RTX;
5301 /* If the current location is the same as the end of the list,
5302 and either both or neither of the locations is uninitialized,
5303 we have nothing to do. */
5304 if (last_loc_note == NULL_RTX
5305 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5306 NOTE_VAR_LOCATION_LOC (loc_note)))
5307 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5308 != NOTE_VAR_LOCATION_STATUS (loc_note))
5309 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5310 == VAR_INIT_STATUS_UNINITIALIZED)
5311 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5312 == VAR_INIT_STATUS_UNINITIALIZED))))
5314 /* Add LOC to the end of list and update LAST. If the last
5315 element of the list has been removed above, reuse its
5316 memory for the new node, otherwise allocate a new one. */
5317 if (unused)
5319 loc = unused;
5320 memset (loc, '\0', sizeof (*loc));
5322 else
5323 loc = ggc_cleared_alloc<var_loc_node> ();
5324 if (bitsize == -1 || piece_loc == NULL)
5325 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5326 else
5327 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5328 bitpos, piece_bitpos, bitsize, loc_note);
5329 last->next = loc;
5330 /* Ensure TEMP->LAST will point either to the new last but one
5331 element of the chain, or to the last element in it. */
5332 if (last != temp->last)
5333 temp->last = last;
5335 else if (unused)
5336 ggc_free (unused);
5338 else
5340 loc = ggc_cleared_alloc<var_loc_node> ();
5341 temp->first = loc;
5342 temp->last = loc;
5343 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5345 return loc;
5348 /* Keep track of the number of spaces used to indent the
5349 output of the debugging routines that print the structure of
5350 the DIE internal representation. */
5351 static int print_indent;
5353 /* Indent the line the number of spaces given by print_indent. */
5355 static inline void
5356 print_spaces (FILE *outfile)
5358 fprintf (outfile, "%*s", print_indent, "");
5361 /* Print a type signature in hex. */
5363 static inline void
5364 print_signature (FILE *outfile, char *sig)
5366 int i;
5368 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5369 fprintf (outfile, "%02x", sig[i] & 0xff);
5372 /* Print the information associated with a given DIE, and its children.
5373 This routine is a debugging aid only. */
5375 static void
5376 print_die (dw_die_ref die, FILE *outfile)
5378 dw_attr_ref a;
5379 dw_die_ref c;
5380 unsigned ix;
5382 print_spaces (outfile);
5383 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5384 die->die_offset, dwarf_tag_name (die->die_tag),
5385 (void*) die);
5386 print_spaces (outfile);
5387 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5388 fprintf (outfile, " offset: %ld", die->die_offset);
5389 fprintf (outfile, " mark: %d\n", die->die_mark);
5391 if (die->comdat_type_p)
5393 print_spaces (outfile);
5394 fprintf (outfile, " signature: ");
5395 print_signature (outfile, die->die_id.die_type_node->signature);
5396 fprintf (outfile, "\n");
5399 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5401 print_spaces (outfile);
5402 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5404 switch (AT_class (a))
5406 case dw_val_class_addr:
5407 fprintf (outfile, "address");
5408 break;
5409 case dw_val_class_offset:
5410 fprintf (outfile, "offset");
5411 break;
5412 case dw_val_class_loc:
5413 fprintf (outfile, "location descriptor");
5414 break;
5415 case dw_val_class_loc_list:
5416 fprintf (outfile, "location list -> label:%s",
5417 AT_loc_list (a)->ll_symbol);
5418 break;
5419 case dw_val_class_range_list:
5420 fprintf (outfile, "range list");
5421 break;
5422 case dw_val_class_const:
5423 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5424 break;
5425 case dw_val_class_unsigned_const:
5426 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5427 break;
5428 case dw_val_class_const_double:
5429 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5430 HOST_WIDE_INT_PRINT_UNSIGNED")",
5431 a->dw_attr_val.v.val_double.high,
5432 a->dw_attr_val.v.val_double.low);
5433 break;
5434 case dw_val_class_wide_int:
5436 int i = a->dw_attr_val.v.val_wide->get_len ();
5437 fprintf (outfile, "constant (");
5438 gcc_assert (i > 0);
5439 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5440 fprintf (outfile, "0x");
5441 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5442 a->dw_attr_val.v.val_wide->elt (--i));
5443 while (--i >= 0)
5444 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5445 a->dw_attr_val.v.val_wide->elt (i));
5446 fprintf (outfile, ")");
5447 break;
5449 case dw_val_class_vec:
5450 fprintf (outfile, "floating-point or vector constant");
5451 break;
5452 case dw_val_class_flag:
5453 fprintf (outfile, "%u", AT_flag (a));
5454 break;
5455 case dw_val_class_die_ref:
5456 if (AT_ref (a) != NULL)
5458 if (AT_ref (a)->comdat_type_p)
5460 fprintf (outfile, "die -> signature: ");
5461 print_signature (outfile,
5462 AT_ref (a)->die_id.die_type_node->signature);
5464 else if (AT_ref (a)->die_id.die_symbol)
5465 fprintf (outfile, "die -> label: %s",
5466 AT_ref (a)->die_id.die_symbol);
5467 else
5468 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5469 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5471 else
5472 fprintf (outfile, "die -> <null>");
5473 break;
5474 case dw_val_class_vms_delta:
5475 fprintf (outfile, "delta: @slotcount(%s-%s)",
5476 AT_vms_delta2 (a), AT_vms_delta1 (a));
5477 break;
5478 case dw_val_class_lbl_id:
5479 case dw_val_class_lineptr:
5480 case dw_val_class_macptr:
5481 case dw_val_class_high_pc:
5482 fprintf (outfile, "label: %s", AT_lbl (a));
5483 break;
5484 case dw_val_class_str:
5485 if (AT_string (a) != NULL)
5486 fprintf (outfile, "\"%s\"", AT_string (a));
5487 else
5488 fprintf (outfile, "<null>");
5489 break;
5490 case dw_val_class_file:
5491 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5492 AT_file (a)->emitted_number);
5493 break;
5494 case dw_val_class_data8:
5496 int i;
5498 for (i = 0; i < 8; i++)
5499 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5500 break;
5502 default:
5503 break;
5506 fprintf (outfile, "\n");
5509 if (die->die_child != NULL)
5511 print_indent += 4;
5512 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5513 print_indent -= 4;
5515 if (print_indent == 0)
5516 fprintf (outfile, "\n");
5519 /* Print the information collected for a given DIE. */
5521 DEBUG_FUNCTION void
5522 debug_dwarf_die (dw_die_ref die)
5524 print_die (die, stderr);
5527 DEBUG_FUNCTION void
5528 debug (die_struct &ref)
5530 print_die (&ref, stderr);
5533 DEBUG_FUNCTION void
5534 debug (die_struct *ptr)
5536 if (ptr)
5537 debug (*ptr);
5538 else
5539 fprintf (stderr, "<nil>\n");
5543 /* Print all DWARF information collected for the compilation unit.
5544 This routine is a debugging aid only. */
5546 DEBUG_FUNCTION void
5547 debug_dwarf (void)
5549 print_indent = 0;
5550 print_die (comp_unit_die (), stderr);
5553 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5554 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5555 DIE that marks the start of the DIEs for this include file. */
5557 static dw_die_ref
5558 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5560 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5561 dw_die_ref new_unit = gen_compile_unit_die (filename);
5563 new_unit->die_sib = old_unit;
5564 return new_unit;
5567 /* Close an include-file CU and reopen the enclosing one. */
5569 static dw_die_ref
5570 pop_compile_unit (dw_die_ref old_unit)
5572 dw_die_ref new_unit = old_unit->die_sib;
5574 old_unit->die_sib = NULL;
5575 return new_unit;
5578 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5579 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5580 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5582 /* Calculate the checksum of a location expression. */
5584 static inline void
5585 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5587 int tem;
5588 inchash::hash hstate;
5589 hashval_t hash;
5591 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5592 CHECKSUM (tem);
5593 hash_loc_operands (loc, hstate);
5594 hash = hstate.end();
5595 CHECKSUM (hash);
5598 /* Calculate the checksum of an attribute. */
5600 static void
5601 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5603 dw_loc_descr_ref loc;
5604 rtx r;
5606 CHECKSUM (at->dw_attr);
5608 /* We don't care that this was compiled with a different compiler
5609 snapshot; if the output is the same, that's what matters. */
5610 if (at->dw_attr == DW_AT_producer)
5611 return;
5613 switch (AT_class (at))
5615 case dw_val_class_const:
5616 CHECKSUM (at->dw_attr_val.v.val_int);
5617 break;
5618 case dw_val_class_unsigned_const:
5619 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5620 break;
5621 case dw_val_class_const_double:
5622 CHECKSUM (at->dw_attr_val.v.val_double);
5623 break;
5624 case dw_val_class_wide_int:
5625 CHECKSUM (*at->dw_attr_val.v.val_wide);
5626 break;
5627 case dw_val_class_vec:
5628 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5629 (at->dw_attr_val.v.val_vec.length
5630 * at->dw_attr_val.v.val_vec.elt_size));
5631 break;
5632 case dw_val_class_flag:
5633 CHECKSUM (at->dw_attr_val.v.val_flag);
5634 break;
5635 case dw_val_class_str:
5636 CHECKSUM_STRING (AT_string (at));
5637 break;
5639 case dw_val_class_addr:
5640 r = AT_addr (at);
5641 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5642 CHECKSUM_STRING (XSTR (r, 0));
5643 break;
5645 case dw_val_class_offset:
5646 CHECKSUM (at->dw_attr_val.v.val_offset);
5647 break;
5649 case dw_val_class_loc:
5650 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5651 loc_checksum (loc, ctx);
5652 break;
5654 case dw_val_class_die_ref:
5655 die_checksum (AT_ref (at), ctx, mark);
5656 break;
5658 case dw_val_class_fde_ref:
5659 case dw_val_class_vms_delta:
5660 case dw_val_class_lbl_id:
5661 case dw_val_class_lineptr:
5662 case dw_val_class_macptr:
5663 case dw_val_class_high_pc:
5664 break;
5666 case dw_val_class_file:
5667 CHECKSUM_STRING (AT_file (at)->filename);
5668 break;
5670 case dw_val_class_data8:
5671 CHECKSUM (at->dw_attr_val.v.val_data8);
5672 break;
5674 default:
5675 break;
5679 /* Calculate the checksum of a DIE. */
5681 static void
5682 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5684 dw_die_ref c;
5685 dw_attr_ref a;
5686 unsigned ix;
5688 /* To avoid infinite recursion. */
5689 if (die->die_mark)
5691 CHECKSUM (die->die_mark);
5692 return;
5694 die->die_mark = ++(*mark);
5696 CHECKSUM (die->die_tag);
5698 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5699 attr_checksum (a, ctx, mark);
5701 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5704 #undef CHECKSUM
5705 #undef CHECKSUM_BLOCK
5706 #undef CHECKSUM_STRING
5708 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5709 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5710 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5711 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5712 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5713 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5714 #define CHECKSUM_ATTR(FOO) \
5715 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5717 /* Calculate the checksum of a number in signed LEB128 format. */
5719 static void
5720 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5722 unsigned char byte;
5723 bool more;
5725 while (1)
5727 byte = (value & 0x7f);
5728 value >>= 7;
5729 more = !((value == 0 && (byte & 0x40) == 0)
5730 || (value == -1 && (byte & 0x40) != 0));
5731 if (more)
5732 byte |= 0x80;
5733 CHECKSUM (byte);
5734 if (!more)
5735 break;
5739 /* Calculate the checksum of a number in unsigned LEB128 format. */
5741 static void
5742 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5744 while (1)
5746 unsigned char byte = (value & 0x7f);
5747 value >>= 7;
5748 if (value != 0)
5749 /* More bytes to follow. */
5750 byte |= 0x80;
5751 CHECKSUM (byte);
5752 if (value == 0)
5753 break;
5757 /* Checksum the context of the DIE. This adds the names of any
5758 surrounding namespaces or structures to the checksum. */
5760 static void
5761 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5763 const char *name;
5764 dw_die_ref spec;
5765 int tag = die->die_tag;
5767 if (tag != DW_TAG_namespace
5768 && tag != DW_TAG_structure_type
5769 && tag != DW_TAG_class_type)
5770 return;
5772 name = get_AT_string (die, DW_AT_name);
5774 spec = get_AT_ref (die, DW_AT_specification);
5775 if (spec != NULL)
5776 die = spec;
5778 if (die->die_parent != NULL)
5779 checksum_die_context (die->die_parent, ctx);
5781 CHECKSUM_ULEB128 ('C');
5782 CHECKSUM_ULEB128 (tag);
5783 if (name != NULL)
5784 CHECKSUM_STRING (name);
5787 /* Calculate the checksum of a location expression. */
5789 static inline void
5790 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5792 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5793 were emitted as a DW_FORM_sdata instead of a location expression. */
5794 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5796 CHECKSUM_ULEB128 (DW_FORM_sdata);
5797 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5798 return;
5801 /* Otherwise, just checksum the raw location expression. */
5802 while (loc != NULL)
5804 inchash::hash hstate;
5805 hashval_t hash;
5807 CHECKSUM_ULEB128 (loc->dtprel);
5808 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5809 hash_loc_operands (loc, hstate);
5810 hash = hstate.end ();
5811 CHECKSUM (hash);
5812 loc = loc->dw_loc_next;
5816 /* Calculate the checksum of an attribute. */
5818 static void
5819 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5820 struct md5_ctx *ctx, int *mark)
5822 dw_loc_descr_ref loc;
5823 rtx r;
5825 if (AT_class (at) == dw_val_class_die_ref)
5827 dw_die_ref target_die = AT_ref (at);
5829 /* For pointer and reference types, we checksum only the (qualified)
5830 name of the target type (if there is a name). For friend entries,
5831 we checksum only the (qualified) name of the target type or function.
5832 This allows the checksum to remain the same whether the target type
5833 is complete or not. */
5834 if ((at->dw_attr == DW_AT_type
5835 && (tag == DW_TAG_pointer_type
5836 || tag == DW_TAG_reference_type
5837 || tag == DW_TAG_rvalue_reference_type
5838 || tag == DW_TAG_ptr_to_member_type))
5839 || (at->dw_attr == DW_AT_friend
5840 && tag == DW_TAG_friend))
5842 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5844 if (name_attr != NULL)
5846 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5848 if (decl == NULL)
5849 decl = target_die;
5850 CHECKSUM_ULEB128 ('N');
5851 CHECKSUM_ULEB128 (at->dw_attr);
5852 if (decl->die_parent != NULL)
5853 checksum_die_context (decl->die_parent, ctx);
5854 CHECKSUM_ULEB128 ('E');
5855 CHECKSUM_STRING (AT_string (name_attr));
5856 return;
5860 /* For all other references to another DIE, we check to see if the
5861 target DIE has already been visited. If it has, we emit a
5862 backward reference; if not, we descend recursively. */
5863 if (target_die->die_mark > 0)
5865 CHECKSUM_ULEB128 ('R');
5866 CHECKSUM_ULEB128 (at->dw_attr);
5867 CHECKSUM_ULEB128 (target_die->die_mark);
5869 else
5871 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5873 if (decl == NULL)
5874 decl = target_die;
5875 target_die->die_mark = ++(*mark);
5876 CHECKSUM_ULEB128 ('T');
5877 CHECKSUM_ULEB128 (at->dw_attr);
5878 if (decl->die_parent != NULL)
5879 checksum_die_context (decl->die_parent, ctx);
5880 die_checksum_ordered (target_die, ctx, mark);
5882 return;
5885 CHECKSUM_ULEB128 ('A');
5886 CHECKSUM_ULEB128 (at->dw_attr);
5888 switch (AT_class (at))
5890 case dw_val_class_const:
5891 CHECKSUM_ULEB128 (DW_FORM_sdata);
5892 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5893 break;
5895 case dw_val_class_unsigned_const:
5896 CHECKSUM_ULEB128 (DW_FORM_sdata);
5897 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5898 break;
5900 case dw_val_class_const_double:
5901 CHECKSUM_ULEB128 (DW_FORM_block);
5902 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5903 CHECKSUM (at->dw_attr_val.v.val_double);
5904 break;
5906 case dw_val_class_wide_int:
5907 CHECKSUM_ULEB128 (DW_FORM_block);
5908 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5909 CHECKSUM (*at->dw_attr_val.v.val_wide);
5910 break;
5912 case dw_val_class_vec:
5913 CHECKSUM_ULEB128 (DW_FORM_block);
5914 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5915 * at->dw_attr_val.v.val_vec.elt_size);
5916 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5917 (at->dw_attr_val.v.val_vec.length
5918 * at->dw_attr_val.v.val_vec.elt_size));
5919 break;
5921 case dw_val_class_flag:
5922 CHECKSUM_ULEB128 (DW_FORM_flag);
5923 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5924 break;
5926 case dw_val_class_str:
5927 CHECKSUM_ULEB128 (DW_FORM_string);
5928 CHECKSUM_STRING (AT_string (at));
5929 break;
5931 case dw_val_class_addr:
5932 r = AT_addr (at);
5933 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5934 CHECKSUM_ULEB128 (DW_FORM_string);
5935 CHECKSUM_STRING (XSTR (r, 0));
5936 break;
5938 case dw_val_class_offset:
5939 CHECKSUM_ULEB128 (DW_FORM_sdata);
5940 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5941 break;
5943 case dw_val_class_loc:
5944 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5945 loc_checksum_ordered (loc, ctx);
5946 break;
5948 case dw_val_class_fde_ref:
5949 case dw_val_class_lbl_id:
5950 case dw_val_class_lineptr:
5951 case dw_val_class_macptr:
5952 case dw_val_class_high_pc:
5953 break;
5955 case dw_val_class_file:
5956 CHECKSUM_ULEB128 (DW_FORM_string);
5957 CHECKSUM_STRING (AT_file (at)->filename);
5958 break;
5960 case dw_val_class_data8:
5961 CHECKSUM (at->dw_attr_val.v.val_data8);
5962 break;
5964 default:
5965 break;
5969 struct checksum_attributes
5971 dw_attr_ref at_name;
5972 dw_attr_ref at_type;
5973 dw_attr_ref at_friend;
5974 dw_attr_ref at_accessibility;
5975 dw_attr_ref at_address_class;
5976 dw_attr_ref at_allocated;
5977 dw_attr_ref at_artificial;
5978 dw_attr_ref at_associated;
5979 dw_attr_ref at_binary_scale;
5980 dw_attr_ref at_bit_offset;
5981 dw_attr_ref at_bit_size;
5982 dw_attr_ref at_bit_stride;
5983 dw_attr_ref at_byte_size;
5984 dw_attr_ref at_byte_stride;
5985 dw_attr_ref at_const_value;
5986 dw_attr_ref at_containing_type;
5987 dw_attr_ref at_count;
5988 dw_attr_ref at_data_location;
5989 dw_attr_ref at_data_member_location;
5990 dw_attr_ref at_decimal_scale;
5991 dw_attr_ref at_decimal_sign;
5992 dw_attr_ref at_default_value;
5993 dw_attr_ref at_digit_count;
5994 dw_attr_ref at_discr;
5995 dw_attr_ref at_discr_list;
5996 dw_attr_ref at_discr_value;
5997 dw_attr_ref at_encoding;
5998 dw_attr_ref at_endianity;
5999 dw_attr_ref at_explicit;
6000 dw_attr_ref at_is_optional;
6001 dw_attr_ref at_location;
6002 dw_attr_ref at_lower_bound;
6003 dw_attr_ref at_mutable;
6004 dw_attr_ref at_ordering;
6005 dw_attr_ref at_picture_string;
6006 dw_attr_ref at_prototyped;
6007 dw_attr_ref at_small;
6008 dw_attr_ref at_segment;
6009 dw_attr_ref at_string_length;
6010 dw_attr_ref at_threads_scaled;
6011 dw_attr_ref at_upper_bound;
6012 dw_attr_ref at_use_location;
6013 dw_attr_ref at_use_UTF8;
6014 dw_attr_ref at_variable_parameter;
6015 dw_attr_ref at_virtuality;
6016 dw_attr_ref at_visibility;
6017 dw_attr_ref at_vtable_elem_location;
6020 /* Collect the attributes that we will want to use for the checksum. */
6022 static void
6023 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6025 dw_attr_ref a;
6026 unsigned ix;
6028 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6030 switch (a->dw_attr)
6032 case DW_AT_name:
6033 attrs->at_name = a;
6034 break;
6035 case DW_AT_type:
6036 attrs->at_type = a;
6037 break;
6038 case DW_AT_friend:
6039 attrs->at_friend = a;
6040 break;
6041 case DW_AT_accessibility:
6042 attrs->at_accessibility = a;
6043 break;
6044 case DW_AT_address_class:
6045 attrs->at_address_class = a;
6046 break;
6047 case DW_AT_allocated:
6048 attrs->at_allocated = a;
6049 break;
6050 case DW_AT_artificial:
6051 attrs->at_artificial = a;
6052 break;
6053 case DW_AT_associated:
6054 attrs->at_associated = a;
6055 break;
6056 case DW_AT_binary_scale:
6057 attrs->at_binary_scale = a;
6058 break;
6059 case DW_AT_bit_offset:
6060 attrs->at_bit_offset = a;
6061 break;
6062 case DW_AT_bit_size:
6063 attrs->at_bit_size = a;
6064 break;
6065 case DW_AT_bit_stride:
6066 attrs->at_bit_stride = a;
6067 break;
6068 case DW_AT_byte_size:
6069 attrs->at_byte_size = a;
6070 break;
6071 case DW_AT_byte_stride:
6072 attrs->at_byte_stride = a;
6073 break;
6074 case DW_AT_const_value:
6075 attrs->at_const_value = a;
6076 break;
6077 case DW_AT_containing_type:
6078 attrs->at_containing_type = a;
6079 break;
6080 case DW_AT_count:
6081 attrs->at_count = a;
6082 break;
6083 case DW_AT_data_location:
6084 attrs->at_data_location = a;
6085 break;
6086 case DW_AT_data_member_location:
6087 attrs->at_data_member_location = a;
6088 break;
6089 case DW_AT_decimal_scale:
6090 attrs->at_decimal_scale = a;
6091 break;
6092 case DW_AT_decimal_sign:
6093 attrs->at_decimal_sign = a;
6094 break;
6095 case DW_AT_default_value:
6096 attrs->at_default_value = a;
6097 break;
6098 case DW_AT_digit_count:
6099 attrs->at_digit_count = a;
6100 break;
6101 case DW_AT_discr:
6102 attrs->at_discr = a;
6103 break;
6104 case DW_AT_discr_list:
6105 attrs->at_discr_list = a;
6106 break;
6107 case DW_AT_discr_value:
6108 attrs->at_discr_value = a;
6109 break;
6110 case DW_AT_encoding:
6111 attrs->at_encoding = a;
6112 break;
6113 case DW_AT_endianity:
6114 attrs->at_endianity = a;
6115 break;
6116 case DW_AT_explicit:
6117 attrs->at_explicit = a;
6118 break;
6119 case DW_AT_is_optional:
6120 attrs->at_is_optional = a;
6121 break;
6122 case DW_AT_location:
6123 attrs->at_location = a;
6124 break;
6125 case DW_AT_lower_bound:
6126 attrs->at_lower_bound = a;
6127 break;
6128 case DW_AT_mutable:
6129 attrs->at_mutable = a;
6130 break;
6131 case DW_AT_ordering:
6132 attrs->at_ordering = a;
6133 break;
6134 case DW_AT_picture_string:
6135 attrs->at_picture_string = a;
6136 break;
6137 case DW_AT_prototyped:
6138 attrs->at_prototyped = a;
6139 break;
6140 case DW_AT_small:
6141 attrs->at_small = a;
6142 break;
6143 case DW_AT_segment:
6144 attrs->at_segment = a;
6145 break;
6146 case DW_AT_string_length:
6147 attrs->at_string_length = a;
6148 break;
6149 case DW_AT_threads_scaled:
6150 attrs->at_threads_scaled = a;
6151 break;
6152 case DW_AT_upper_bound:
6153 attrs->at_upper_bound = a;
6154 break;
6155 case DW_AT_use_location:
6156 attrs->at_use_location = a;
6157 break;
6158 case DW_AT_use_UTF8:
6159 attrs->at_use_UTF8 = a;
6160 break;
6161 case DW_AT_variable_parameter:
6162 attrs->at_variable_parameter = a;
6163 break;
6164 case DW_AT_virtuality:
6165 attrs->at_virtuality = a;
6166 break;
6167 case DW_AT_visibility:
6168 attrs->at_visibility = a;
6169 break;
6170 case DW_AT_vtable_elem_location:
6171 attrs->at_vtable_elem_location = a;
6172 break;
6173 default:
6174 break;
6179 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6181 static void
6182 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6184 dw_die_ref c;
6185 dw_die_ref decl;
6186 struct checksum_attributes attrs;
6188 CHECKSUM_ULEB128 ('D');
6189 CHECKSUM_ULEB128 (die->die_tag);
6191 memset (&attrs, 0, sizeof (attrs));
6193 decl = get_AT_ref (die, DW_AT_specification);
6194 if (decl != NULL)
6195 collect_checksum_attributes (&attrs, decl);
6196 collect_checksum_attributes (&attrs, die);
6198 CHECKSUM_ATTR (attrs.at_name);
6199 CHECKSUM_ATTR (attrs.at_accessibility);
6200 CHECKSUM_ATTR (attrs.at_address_class);
6201 CHECKSUM_ATTR (attrs.at_allocated);
6202 CHECKSUM_ATTR (attrs.at_artificial);
6203 CHECKSUM_ATTR (attrs.at_associated);
6204 CHECKSUM_ATTR (attrs.at_binary_scale);
6205 CHECKSUM_ATTR (attrs.at_bit_offset);
6206 CHECKSUM_ATTR (attrs.at_bit_size);
6207 CHECKSUM_ATTR (attrs.at_bit_stride);
6208 CHECKSUM_ATTR (attrs.at_byte_size);
6209 CHECKSUM_ATTR (attrs.at_byte_stride);
6210 CHECKSUM_ATTR (attrs.at_const_value);
6211 CHECKSUM_ATTR (attrs.at_containing_type);
6212 CHECKSUM_ATTR (attrs.at_count);
6213 CHECKSUM_ATTR (attrs.at_data_location);
6214 CHECKSUM_ATTR (attrs.at_data_member_location);
6215 CHECKSUM_ATTR (attrs.at_decimal_scale);
6216 CHECKSUM_ATTR (attrs.at_decimal_sign);
6217 CHECKSUM_ATTR (attrs.at_default_value);
6218 CHECKSUM_ATTR (attrs.at_digit_count);
6219 CHECKSUM_ATTR (attrs.at_discr);
6220 CHECKSUM_ATTR (attrs.at_discr_list);
6221 CHECKSUM_ATTR (attrs.at_discr_value);
6222 CHECKSUM_ATTR (attrs.at_encoding);
6223 CHECKSUM_ATTR (attrs.at_endianity);
6224 CHECKSUM_ATTR (attrs.at_explicit);
6225 CHECKSUM_ATTR (attrs.at_is_optional);
6226 CHECKSUM_ATTR (attrs.at_location);
6227 CHECKSUM_ATTR (attrs.at_lower_bound);
6228 CHECKSUM_ATTR (attrs.at_mutable);
6229 CHECKSUM_ATTR (attrs.at_ordering);
6230 CHECKSUM_ATTR (attrs.at_picture_string);
6231 CHECKSUM_ATTR (attrs.at_prototyped);
6232 CHECKSUM_ATTR (attrs.at_small);
6233 CHECKSUM_ATTR (attrs.at_segment);
6234 CHECKSUM_ATTR (attrs.at_string_length);
6235 CHECKSUM_ATTR (attrs.at_threads_scaled);
6236 CHECKSUM_ATTR (attrs.at_upper_bound);
6237 CHECKSUM_ATTR (attrs.at_use_location);
6238 CHECKSUM_ATTR (attrs.at_use_UTF8);
6239 CHECKSUM_ATTR (attrs.at_variable_parameter);
6240 CHECKSUM_ATTR (attrs.at_virtuality);
6241 CHECKSUM_ATTR (attrs.at_visibility);
6242 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6243 CHECKSUM_ATTR (attrs.at_type);
6244 CHECKSUM_ATTR (attrs.at_friend);
6246 /* Checksum the child DIEs. */
6247 c = die->die_child;
6248 if (c) do {
6249 dw_attr_ref name_attr;
6251 c = c->die_sib;
6252 name_attr = get_AT (c, DW_AT_name);
6253 if (is_template_instantiation (c))
6255 /* Ignore instantiations of member type and function templates. */
6257 else if (name_attr != NULL
6258 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6260 /* Use a shallow checksum for named nested types and member
6261 functions. */
6262 CHECKSUM_ULEB128 ('S');
6263 CHECKSUM_ULEB128 (c->die_tag);
6264 CHECKSUM_STRING (AT_string (name_attr));
6266 else
6268 /* Use a deep checksum for other children. */
6269 /* Mark this DIE so it gets processed when unmarking. */
6270 if (c->die_mark == 0)
6271 c->die_mark = -1;
6272 die_checksum_ordered (c, ctx, mark);
6274 } while (c != die->die_child);
6276 CHECKSUM_ULEB128 (0);
6279 /* Add a type name and tag to a hash. */
6280 static void
6281 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6283 CHECKSUM_ULEB128 (tag);
6284 CHECKSUM_STRING (name);
6287 #undef CHECKSUM
6288 #undef CHECKSUM_STRING
6289 #undef CHECKSUM_ATTR
6290 #undef CHECKSUM_LEB128
6291 #undef CHECKSUM_ULEB128
6293 /* Generate the type signature for DIE. This is computed by generating an
6294 MD5 checksum over the DIE's tag, its relevant attributes, and its
6295 children. Attributes that are references to other DIEs are processed
6296 by recursion, using the MARK field to prevent infinite recursion.
6297 If the DIE is nested inside a namespace or another type, we also
6298 need to include that context in the signature. The lower 64 bits
6299 of the resulting MD5 checksum comprise the signature. */
6301 static void
6302 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6304 int mark;
6305 const char *name;
6306 unsigned char checksum[16];
6307 struct md5_ctx ctx;
6308 dw_die_ref decl;
6309 dw_die_ref parent;
6311 name = get_AT_string (die, DW_AT_name);
6312 decl = get_AT_ref (die, DW_AT_specification);
6313 parent = get_die_parent (die);
6315 /* First, compute a signature for just the type name (and its surrounding
6316 context, if any. This is stored in the type unit DIE for link-time
6317 ODR (one-definition rule) checking. */
6319 if (is_cxx () && name != NULL)
6321 md5_init_ctx (&ctx);
6323 /* Checksum the names of surrounding namespaces and structures. */
6324 if (parent != NULL)
6325 checksum_die_context (parent, &ctx);
6327 /* Checksum the current DIE. */
6328 die_odr_checksum (die->die_tag, name, &ctx);
6329 md5_finish_ctx (&ctx, checksum);
6331 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6334 /* Next, compute the complete type signature. */
6336 md5_init_ctx (&ctx);
6337 mark = 1;
6338 die->die_mark = mark;
6340 /* Checksum the names of surrounding namespaces and structures. */
6341 if (parent != NULL)
6342 checksum_die_context (parent, &ctx);
6344 /* Checksum the DIE and its children. */
6345 die_checksum_ordered (die, &ctx, &mark);
6346 unmark_all_dies (die);
6347 md5_finish_ctx (&ctx, checksum);
6349 /* Store the signature in the type node and link the type DIE and the
6350 type node together. */
6351 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6352 DWARF_TYPE_SIGNATURE_SIZE);
6353 die->comdat_type_p = true;
6354 die->die_id.die_type_node = type_node;
6355 type_node->type_die = die;
6357 /* If the DIE is a specification, link its declaration to the type node
6358 as well. */
6359 if (decl != NULL)
6361 decl->comdat_type_p = true;
6362 decl->die_id.die_type_node = type_node;
6366 /* Do the location expressions look same? */
6367 static inline int
6368 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6370 return loc1->dw_loc_opc == loc2->dw_loc_opc
6371 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6372 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6375 /* Do the values look the same? */
6376 static int
6377 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6379 dw_loc_descr_ref loc1, loc2;
6380 rtx r1, r2;
6382 if (v1->val_class != v2->val_class)
6383 return 0;
6385 switch (v1->val_class)
6387 case dw_val_class_const:
6388 return v1->v.val_int == v2->v.val_int;
6389 case dw_val_class_unsigned_const:
6390 return v1->v.val_unsigned == v2->v.val_unsigned;
6391 case dw_val_class_const_double:
6392 return v1->v.val_double.high == v2->v.val_double.high
6393 && v1->v.val_double.low == v2->v.val_double.low;
6394 case dw_val_class_wide_int:
6395 return *v1->v.val_wide == *v2->v.val_wide;
6396 case dw_val_class_vec:
6397 if (v1->v.val_vec.length != v2->v.val_vec.length
6398 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6399 return 0;
6400 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6401 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6402 return 0;
6403 return 1;
6404 case dw_val_class_flag:
6405 return v1->v.val_flag == v2->v.val_flag;
6406 case dw_val_class_str:
6407 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6409 case dw_val_class_addr:
6410 r1 = v1->v.val_addr;
6411 r2 = v2->v.val_addr;
6412 if (GET_CODE (r1) != GET_CODE (r2))
6413 return 0;
6414 return !rtx_equal_p (r1, r2);
6416 case dw_val_class_offset:
6417 return v1->v.val_offset == v2->v.val_offset;
6419 case dw_val_class_loc:
6420 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6421 loc1 && loc2;
6422 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6423 if (!same_loc_p (loc1, loc2, mark))
6424 return 0;
6425 return !loc1 && !loc2;
6427 case dw_val_class_die_ref:
6428 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6430 case dw_val_class_fde_ref:
6431 case dw_val_class_vms_delta:
6432 case dw_val_class_lbl_id:
6433 case dw_val_class_lineptr:
6434 case dw_val_class_macptr:
6435 case dw_val_class_high_pc:
6436 return 1;
6438 case dw_val_class_file:
6439 return v1->v.val_file == v2->v.val_file;
6441 case dw_val_class_data8:
6442 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6444 default:
6445 return 1;
6449 /* Do the attributes look the same? */
6451 static int
6452 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6454 if (at1->dw_attr != at2->dw_attr)
6455 return 0;
6457 /* We don't care that this was compiled with a different compiler
6458 snapshot; if the output is the same, that's what matters. */
6459 if (at1->dw_attr == DW_AT_producer)
6460 return 1;
6462 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6465 /* Do the dies look the same? */
6467 static int
6468 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6470 dw_die_ref c1, c2;
6471 dw_attr_ref a1;
6472 unsigned ix;
6474 /* To avoid infinite recursion. */
6475 if (die1->die_mark)
6476 return die1->die_mark == die2->die_mark;
6477 die1->die_mark = die2->die_mark = ++(*mark);
6479 if (die1->die_tag != die2->die_tag)
6480 return 0;
6482 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6483 return 0;
6485 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6486 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6487 return 0;
6489 c1 = die1->die_child;
6490 c2 = die2->die_child;
6491 if (! c1)
6493 if (c2)
6494 return 0;
6496 else
6497 for (;;)
6499 if (!same_die_p (c1, c2, mark))
6500 return 0;
6501 c1 = c1->die_sib;
6502 c2 = c2->die_sib;
6503 if (c1 == die1->die_child)
6505 if (c2 == die2->die_child)
6506 break;
6507 else
6508 return 0;
6512 return 1;
6515 /* Do the dies look the same? Wrapper around same_die_p. */
6517 static int
6518 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6520 int mark = 0;
6521 int ret = same_die_p (die1, die2, &mark);
6523 unmark_all_dies (die1);
6524 unmark_all_dies (die2);
6526 return ret;
6529 /* The prefix to attach to symbols on DIEs in the current comdat debug
6530 info section. */
6531 static const char *comdat_symbol_id;
6533 /* The index of the current symbol within the current comdat CU. */
6534 static unsigned int comdat_symbol_number;
6536 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6537 children, and set comdat_symbol_id accordingly. */
6539 static void
6540 compute_section_prefix (dw_die_ref unit_die)
6542 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6543 const char *base = die_name ? lbasename (die_name) : "anonymous";
6544 char *name = XALLOCAVEC (char, strlen (base) + 64);
6545 char *p;
6546 int i, mark;
6547 unsigned char checksum[16];
6548 struct md5_ctx ctx;
6550 /* Compute the checksum of the DIE, then append part of it as hex digits to
6551 the name filename of the unit. */
6553 md5_init_ctx (&ctx);
6554 mark = 0;
6555 die_checksum (unit_die, &ctx, &mark);
6556 unmark_all_dies (unit_die);
6557 md5_finish_ctx (&ctx, checksum);
6559 sprintf (name, "%s.", base);
6560 clean_symbol_name (name);
6562 p = name + strlen (name);
6563 for (i = 0; i < 4; i++)
6565 sprintf (p, "%.2x", checksum[i]);
6566 p += 2;
6569 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6570 comdat_symbol_number = 0;
6573 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6575 static int
6576 is_type_die (dw_die_ref die)
6578 switch (die->die_tag)
6580 case DW_TAG_array_type:
6581 case DW_TAG_class_type:
6582 case DW_TAG_interface_type:
6583 case DW_TAG_enumeration_type:
6584 case DW_TAG_pointer_type:
6585 case DW_TAG_reference_type:
6586 case DW_TAG_rvalue_reference_type:
6587 case DW_TAG_string_type:
6588 case DW_TAG_structure_type:
6589 case DW_TAG_subroutine_type:
6590 case DW_TAG_union_type:
6591 case DW_TAG_ptr_to_member_type:
6592 case DW_TAG_set_type:
6593 case DW_TAG_subrange_type:
6594 case DW_TAG_base_type:
6595 case DW_TAG_const_type:
6596 case DW_TAG_file_type:
6597 case DW_TAG_packed_type:
6598 case DW_TAG_volatile_type:
6599 case DW_TAG_typedef:
6600 return 1;
6601 default:
6602 return 0;
6606 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6607 Basically, we want to choose the bits that are likely to be shared between
6608 compilations (types) and leave out the bits that are specific to individual
6609 compilations (functions). */
6611 static int
6612 is_comdat_die (dw_die_ref c)
6614 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6615 we do for stabs. The advantage is a greater likelihood of sharing between
6616 objects that don't include headers in the same order (and therefore would
6617 put the base types in a different comdat). jason 8/28/00 */
6619 if (c->die_tag == DW_TAG_base_type)
6620 return 0;
6622 if (c->die_tag == DW_TAG_pointer_type
6623 || c->die_tag == DW_TAG_reference_type
6624 || c->die_tag == DW_TAG_rvalue_reference_type
6625 || c->die_tag == DW_TAG_const_type
6626 || c->die_tag == DW_TAG_volatile_type)
6628 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6630 return t ? is_comdat_die (t) : 0;
6633 return is_type_die (c);
6636 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6637 compilation unit. */
6639 static int
6640 is_symbol_die (dw_die_ref c)
6642 return (is_type_die (c)
6643 || is_declaration_die (c)
6644 || c->die_tag == DW_TAG_namespace
6645 || c->die_tag == DW_TAG_module);
6648 /* Returns true iff C is a compile-unit DIE. */
6650 static inline bool
6651 is_cu_die (dw_die_ref c)
6653 return c && c->die_tag == DW_TAG_compile_unit;
6656 /* Returns true iff C is a unit DIE of some sort. */
6658 static inline bool
6659 is_unit_die (dw_die_ref c)
6661 return c && (c->die_tag == DW_TAG_compile_unit
6662 || c->die_tag == DW_TAG_partial_unit
6663 || c->die_tag == DW_TAG_type_unit);
6666 /* Returns true iff C is a namespace DIE. */
6668 static inline bool
6669 is_namespace_die (dw_die_ref c)
6671 return c && c->die_tag == DW_TAG_namespace;
6674 /* Returns true iff C is a class or structure DIE. */
6676 static inline bool
6677 is_class_die (dw_die_ref c)
6679 return c && (c->die_tag == DW_TAG_class_type
6680 || c->die_tag == DW_TAG_structure_type);
6683 /* Return non-zero if this DIE is a template parameter. */
6685 static inline bool
6686 is_template_parameter (dw_die_ref die)
6688 switch (die->die_tag)
6690 case DW_TAG_template_type_param:
6691 case DW_TAG_template_value_param:
6692 case DW_TAG_GNU_template_template_param:
6693 case DW_TAG_GNU_template_parameter_pack:
6694 return true;
6695 default:
6696 return false;
6700 /* Return non-zero if this DIE represents a template instantiation. */
6702 static inline bool
6703 is_template_instantiation (dw_die_ref die)
6705 dw_die_ref c;
6707 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6708 return false;
6709 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6710 return false;
6713 static char *
6714 gen_internal_sym (const char *prefix)
6716 char buf[256];
6718 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6719 return xstrdup (buf);
6722 /* Assign symbols to all worthy DIEs under DIE. */
6724 static void
6725 assign_symbol_names (dw_die_ref die)
6727 dw_die_ref c;
6729 if (is_symbol_die (die) && !die->comdat_type_p)
6731 if (comdat_symbol_id)
6733 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6735 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6736 comdat_symbol_id, comdat_symbol_number++);
6737 die->die_id.die_symbol = xstrdup (p);
6739 else
6740 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6743 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6746 struct cu_hash_table_entry
6748 dw_die_ref cu;
6749 unsigned min_comdat_num, max_comdat_num;
6750 struct cu_hash_table_entry *next;
6753 /* Helpers to manipulate hash table of CUs. */
6755 struct cu_hash_table_entry_hasher
6757 typedef cu_hash_table_entry value_type;
6758 typedef die_struct compare_type;
6759 static inline hashval_t hash (const value_type *);
6760 static inline bool equal (const value_type *, const compare_type *);
6761 static inline void remove (value_type *);
6764 inline hashval_t
6765 cu_hash_table_entry_hasher::hash (const value_type *entry)
6767 return htab_hash_string (entry->cu->die_id.die_symbol);
6770 inline bool
6771 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6772 const compare_type *entry2)
6774 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6777 inline void
6778 cu_hash_table_entry_hasher::remove (value_type *entry)
6780 struct cu_hash_table_entry *next;
6782 while (entry)
6784 next = entry->next;
6785 free (entry);
6786 entry = next;
6790 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6792 /* Check whether we have already seen this CU and set up SYM_NUM
6793 accordingly. */
6794 static int
6795 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6797 struct cu_hash_table_entry dummy;
6798 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6800 dummy.max_comdat_num = 0;
6802 slot = htable->find_slot_with_hash (cu,
6803 htab_hash_string (cu->die_id.die_symbol),
6804 INSERT);
6805 entry = *slot;
6807 for (; entry; last = entry, entry = entry->next)
6809 if (same_die_p_wrap (cu, entry->cu))
6810 break;
6813 if (entry)
6815 *sym_num = entry->min_comdat_num;
6816 return 1;
6819 entry = XCNEW (struct cu_hash_table_entry);
6820 entry->cu = cu;
6821 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6822 entry->next = *slot;
6823 *slot = entry;
6825 return 0;
6828 /* Record SYM_NUM to record of CU in HTABLE. */
6829 static void
6830 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6831 unsigned int sym_num)
6833 struct cu_hash_table_entry **slot, *entry;
6835 slot = htable->find_slot_with_hash (cu,
6836 htab_hash_string (cu->die_id.die_symbol),
6837 NO_INSERT);
6838 entry = *slot;
6840 entry->max_comdat_num = sym_num;
6843 /* Traverse the DIE (which is always comp_unit_die), and set up
6844 additional compilation units for each of the include files we see
6845 bracketed by BINCL/EINCL. */
6847 static void
6848 break_out_includes (dw_die_ref die)
6850 dw_die_ref c;
6851 dw_die_ref unit = NULL;
6852 limbo_die_node *node, **pnode;
6854 c = die->die_child;
6855 if (c) do {
6856 dw_die_ref prev = c;
6857 c = c->die_sib;
6858 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6859 || (unit && is_comdat_die (c)))
6861 dw_die_ref next = c->die_sib;
6863 /* This DIE is for a secondary CU; remove it from the main one. */
6864 remove_child_with_prev (c, prev);
6866 if (c->die_tag == DW_TAG_GNU_BINCL)
6867 unit = push_new_compile_unit (unit, c);
6868 else if (c->die_tag == DW_TAG_GNU_EINCL)
6869 unit = pop_compile_unit (unit);
6870 else
6871 add_child_die (unit, c);
6872 c = next;
6873 if (c == die->die_child)
6874 break;
6876 } while (c != die->die_child);
6878 #if 0
6879 /* We can only use this in debugging, since the frontend doesn't check
6880 to make sure that we leave every include file we enter. */
6881 gcc_assert (!unit);
6882 #endif
6884 assign_symbol_names (die);
6885 cu_hash_type cu_hash_table (10);
6886 for (node = limbo_die_list, pnode = &limbo_die_list;
6887 node;
6888 node = node->next)
6890 int is_dupl;
6892 compute_section_prefix (node->die);
6893 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6894 &comdat_symbol_number);
6895 assign_symbol_names (node->die);
6896 if (is_dupl)
6897 *pnode = node->next;
6898 else
6900 pnode = &node->next;
6901 record_comdat_symbol_number (node->die, &cu_hash_table,
6902 comdat_symbol_number);
6907 /* Return non-zero if this DIE is a declaration. */
6909 static int
6910 is_declaration_die (dw_die_ref die)
6912 dw_attr_ref a;
6913 unsigned ix;
6915 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6916 if (a->dw_attr == DW_AT_declaration)
6917 return 1;
6919 return 0;
6922 /* Return non-zero if this DIE is nested inside a subprogram. */
6924 static int
6925 is_nested_in_subprogram (dw_die_ref die)
6927 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6929 if (decl == NULL)
6930 decl = die;
6931 return local_scope_p (decl);
6934 /* Return non-zero if this DIE contains a defining declaration of a
6935 subprogram. */
6937 static int
6938 contains_subprogram_definition (dw_die_ref die)
6940 dw_die_ref c;
6942 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6943 return 1;
6944 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6945 return 0;
6948 /* Return non-zero if this is a type DIE that should be moved to a
6949 COMDAT .debug_types section. */
6951 static int
6952 should_move_die_to_comdat (dw_die_ref die)
6954 switch (die->die_tag)
6956 case DW_TAG_class_type:
6957 case DW_TAG_structure_type:
6958 case DW_TAG_enumeration_type:
6959 case DW_TAG_union_type:
6960 /* Don't move declarations, inlined instances, types nested in a
6961 subprogram, or types that contain subprogram definitions. */
6962 if (is_declaration_die (die)
6963 || get_AT (die, DW_AT_abstract_origin)
6964 || is_nested_in_subprogram (die)
6965 || contains_subprogram_definition (die))
6966 return 0;
6967 return 1;
6968 case DW_TAG_array_type:
6969 case DW_TAG_interface_type:
6970 case DW_TAG_pointer_type:
6971 case DW_TAG_reference_type:
6972 case DW_TAG_rvalue_reference_type:
6973 case DW_TAG_string_type:
6974 case DW_TAG_subroutine_type:
6975 case DW_TAG_ptr_to_member_type:
6976 case DW_TAG_set_type:
6977 case DW_TAG_subrange_type:
6978 case DW_TAG_base_type:
6979 case DW_TAG_const_type:
6980 case DW_TAG_file_type:
6981 case DW_TAG_packed_type:
6982 case DW_TAG_volatile_type:
6983 case DW_TAG_typedef:
6984 default:
6985 return 0;
6989 /* Make a clone of DIE. */
6991 static dw_die_ref
6992 clone_die (dw_die_ref die)
6994 dw_die_ref clone;
6995 dw_attr_ref a;
6996 unsigned ix;
6998 clone = ggc_cleared_alloc<die_node> ();
6999 clone->die_tag = die->die_tag;
7001 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7002 add_dwarf_attr (clone, a);
7004 return clone;
7007 /* Make a clone of the tree rooted at DIE. */
7009 static dw_die_ref
7010 clone_tree (dw_die_ref die)
7012 dw_die_ref c;
7013 dw_die_ref clone = clone_die (die);
7015 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7017 return clone;
7020 /* Make a clone of DIE as a declaration. */
7022 static dw_die_ref
7023 clone_as_declaration (dw_die_ref die)
7025 dw_die_ref clone;
7026 dw_die_ref decl;
7027 dw_attr_ref a;
7028 unsigned ix;
7030 /* If the DIE is already a declaration, just clone it. */
7031 if (is_declaration_die (die))
7032 return clone_die (die);
7034 /* If the DIE is a specification, just clone its declaration DIE. */
7035 decl = get_AT_ref (die, DW_AT_specification);
7036 if (decl != NULL)
7038 clone = clone_die (decl);
7039 if (die->comdat_type_p)
7040 add_AT_die_ref (clone, DW_AT_signature, die);
7041 return clone;
7044 clone = ggc_cleared_alloc<die_node> ();
7045 clone->die_tag = die->die_tag;
7047 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7049 /* We don't want to copy over all attributes.
7050 For example we don't want DW_AT_byte_size because otherwise we will no
7051 longer have a declaration and GDB will treat it as a definition. */
7053 switch (a->dw_attr)
7055 case DW_AT_abstract_origin:
7056 case DW_AT_artificial:
7057 case DW_AT_containing_type:
7058 case DW_AT_external:
7059 case DW_AT_name:
7060 case DW_AT_type:
7061 case DW_AT_virtuality:
7062 case DW_AT_linkage_name:
7063 case DW_AT_MIPS_linkage_name:
7064 add_dwarf_attr (clone, a);
7065 break;
7066 case DW_AT_byte_size:
7067 default:
7068 break;
7072 if (die->comdat_type_p)
7073 add_AT_die_ref (clone, DW_AT_signature, die);
7075 add_AT_flag (clone, DW_AT_declaration, 1);
7076 return clone;
7080 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7082 struct decl_table_entry
7084 dw_die_ref orig;
7085 dw_die_ref copy;
7088 /* Helpers to manipulate hash table of copied declarations. */
7090 /* Hashtable helpers. */
7092 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7094 typedef decl_table_entry value_type;
7095 typedef die_struct compare_type;
7096 static inline hashval_t hash (const value_type *);
7097 static inline bool equal (const value_type *, const compare_type *);
7100 inline hashval_t
7101 decl_table_entry_hasher::hash (const value_type *entry)
7103 return htab_hash_pointer (entry->orig);
7106 inline bool
7107 decl_table_entry_hasher::equal (const value_type *entry1,
7108 const compare_type *entry2)
7110 return entry1->orig == entry2;
7113 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7115 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7116 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7117 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7118 to check if the ancestor has already been copied into UNIT. */
7120 static dw_die_ref
7121 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7122 decl_hash_type *decl_table)
7124 dw_die_ref parent = die->die_parent;
7125 dw_die_ref new_parent = unit;
7126 dw_die_ref copy;
7127 decl_table_entry **slot = NULL;
7128 struct decl_table_entry *entry = NULL;
7130 if (decl_table)
7132 /* Check if the entry has already been copied to UNIT. */
7133 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7134 INSERT);
7135 if (*slot != HTAB_EMPTY_ENTRY)
7137 entry = *slot;
7138 return entry->copy;
7141 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7142 entry = XCNEW (struct decl_table_entry);
7143 entry->orig = die;
7144 entry->copy = NULL;
7145 *slot = entry;
7148 if (parent != NULL)
7150 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7151 if (spec != NULL)
7152 parent = spec;
7153 if (!is_unit_die (parent))
7154 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7157 copy = clone_as_declaration (die);
7158 add_child_die (new_parent, copy);
7160 if (decl_table)
7162 /* Record the pointer to the copy. */
7163 entry->copy = copy;
7166 return copy;
7168 /* Copy the declaration context to the new type unit DIE. This includes
7169 any surrounding namespace or type declarations. If the DIE has an
7170 AT_specification attribute, it also includes attributes and children
7171 attached to the specification, and returns a pointer to the original
7172 parent of the declaration DIE. Returns NULL otherwise. */
7174 static dw_die_ref
7175 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7177 dw_die_ref decl;
7178 dw_die_ref new_decl;
7179 dw_die_ref orig_parent = NULL;
7181 decl = get_AT_ref (die, DW_AT_specification);
7182 if (decl == NULL)
7183 decl = die;
7184 else
7186 unsigned ix;
7187 dw_die_ref c;
7188 dw_attr_ref a;
7190 /* The original DIE will be changed to a declaration, and must
7191 be moved to be a child of the original declaration DIE. */
7192 orig_parent = decl->die_parent;
7194 /* Copy the type node pointer from the new DIE to the original
7195 declaration DIE so we can forward references later. */
7196 decl->comdat_type_p = true;
7197 decl->die_id.die_type_node = die->die_id.die_type_node;
7199 remove_AT (die, DW_AT_specification);
7201 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7203 if (a->dw_attr != DW_AT_name
7204 && a->dw_attr != DW_AT_declaration
7205 && a->dw_attr != DW_AT_external)
7206 add_dwarf_attr (die, a);
7209 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7212 if (decl->die_parent != NULL
7213 && !is_unit_die (decl->die_parent))
7215 new_decl = copy_ancestor_tree (unit, decl, NULL);
7216 if (new_decl != NULL)
7218 remove_AT (new_decl, DW_AT_signature);
7219 add_AT_specification (die, new_decl);
7223 return orig_parent;
7226 /* Generate the skeleton ancestor tree for the given NODE, then clone
7227 the DIE and add the clone into the tree. */
7229 static void
7230 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7232 if (node->new_die != NULL)
7233 return;
7235 node->new_die = clone_as_declaration (node->old_die);
7237 if (node->parent != NULL)
7239 generate_skeleton_ancestor_tree (node->parent);
7240 add_child_die (node->parent->new_die, node->new_die);
7244 /* Generate a skeleton tree of DIEs containing any declarations that are
7245 found in the original tree. We traverse the tree looking for declaration
7246 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7248 static void
7249 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7251 skeleton_chain_node node;
7252 dw_die_ref c;
7253 dw_die_ref first;
7254 dw_die_ref prev = NULL;
7255 dw_die_ref next = NULL;
7257 node.parent = parent;
7259 first = c = parent->old_die->die_child;
7260 if (c)
7261 next = c->die_sib;
7262 if (c) do {
7263 if (prev == NULL || prev->die_sib == c)
7264 prev = c;
7265 c = next;
7266 next = (c == first ? NULL : c->die_sib);
7267 node.old_die = c;
7268 node.new_die = NULL;
7269 if (is_declaration_die (c))
7271 if (is_template_instantiation (c))
7273 /* Instantiated templates do not need to be cloned into the
7274 type unit. Just move the DIE and its children back to
7275 the skeleton tree (in the main CU). */
7276 remove_child_with_prev (c, prev);
7277 add_child_die (parent->new_die, c);
7278 c = prev;
7280 else
7282 /* Clone the existing DIE, move the original to the skeleton
7283 tree (which is in the main CU), and put the clone, with
7284 all the original's children, where the original came from
7285 (which is about to be moved to the type unit). */
7286 dw_die_ref clone = clone_die (c);
7287 move_all_children (c, clone);
7289 /* If the original has a DW_AT_object_pointer attribute,
7290 it would now point to a child DIE just moved to the
7291 cloned tree, so we need to remove that attribute from
7292 the original. */
7293 remove_AT (c, DW_AT_object_pointer);
7295 replace_child (c, clone, prev);
7296 generate_skeleton_ancestor_tree (parent);
7297 add_child_die (parent->new_die, c);
7298 node.new_die = c;
7299 c = clone;
7302 generate_skeleton_bottom_up (&node);
7303 } while (next != NULL);
7306 /* Wrapper function for generate_skeleton_bottom_up. */
7308 static dw_die_ref
7309 generate_skeleton (dw_die_ref die)
7311 skeleton_chain_node node;
7313 node.old_die = die;
7314 node.new_die = NULL;
7315 node.parent = NULL;
7317 /* If this type definition is nested inside another type,
7318 and is not an instantiation of a template, always leave
7319 at least a declaration in its place. */
7320 if (die->die_parent != NULL
7321 && is_type_die (die->die_parent)
7322 && !is_template_instantiation (die))
7323 node.new_die = clone_as_declaration (die);
7325 generate_skeleton_bottom_up (&node);
7326 return node.new_die;
7329 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7330 declaration. The original DIE is moved to a new compile unit so that
7331 existing references to it follow it to the new location. If any of the
7332 original DIE's descendants is a declaration, we need to replace the
7333 original DIE with a skeleton tree and move the declarations back into the
7334 skeleton tree. */
7336 static dw_die_ref
7337 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7338 dw_die_ref prev)
7340 dw_die_ref skeleton, orig_parent;
7342 /* Copy the declaration context to the type unit DIE. If the returned
7343 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7344 that DIE. */
7345 orig_parent = copy_declaration_context (unit, child);
7347 skeleton = generate_skeleton (child);
7348 if (skeleton == NULL)
7349 remove_child_with_prev (child, prev);
7350 else
7352 skeleton->comdat_type_p = true;
7353 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7355 /* If the original DIE was a specification, we need to put
7356 the skeleton under the parent DIE of the declaration.
7357 This leaves the original declaration in the tree, but
7358 it will be pruned later since there are no longer any
7359 references to it. */
7360 if (orig_parent != NULL)
7362 remove_child_with_prev (child, prev);
7363 add_child_die (orig_parent, skeleton);
7365 else
7366 replace_child (child, skeleton, prev);
7369 return skeleton;
7372 /* Traverse the DIE and set up additional .debug_types sections for each
7373 type worthy of being placed in a COMDAT section. */
7375 static void
7376 break_out_comdat_types (dw_die_ref die)
7378 dw_die_ref c;
7379 dw_die_ref first;
7380 dw_die_ref prev = NULL;
7381 dw_die_ref next = NULL;
7382 dw_die_ref unit = NULL;
7384 first = c = die->die_child;
7385 if (c)
7386 next = c->die_sib;
7387 if (c) do {
7388 if (prev == NULL || prev->die_sib == c)
7389 prev = c;
7390 c = next;
7391 next = (c == first ? NULL : c->die_sib);
7392 if (should_move_die_to_comdat (c))
7394 dw_die_ref replacement;
7395 comdat_type_node_ref type_node;
7397 /* Break out nested types into their own type units. */
7398 break_out_comdat_types (c);
7400 /* Create a new type unit DIE as the root for the new tree, and
7401 add it to the list of comdat types. */
7402 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7403 add_AT_unsigned (unit, DW_AT_language,
7404 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7405 type_node = ggc_cleared_alloc<comdat_type_node> ();
7406 type_node->root_die = unit;
7407 type_node->next = comdat_type_list;
7408 comdat_type_list = type_node;
7410 /* Generate the type signature. */
7411 generate_type_signature (c, type_node);
7413 /* Copy the declaration context, attributes, and children of the
7414 declaration into the new type unit DIE, then remove this DIE
7415 from the main CU (or replace it with a skeleton if necessary). */
7416 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7417 type_node->skeleton_die = replacement;
7419 /* Add the DIE to the new compunit. */
7420 add_child_die (unit, c);
7422 if (replacement != NULL)
7423 c = replacement;
7425 else if (c->die_tag == DW_TAG_namespace
7426 || c->die_tag == DW_TAG_class_type
7427 || c->die_tag == DW_TAG_structure_type
7428 || c->die_tag == DW_TAG_union_type)
7430 /* Look for nested types that can be broken out. */
7431 break_out_comdat_types (c);
7433 } while (next != NULL);
7436 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7437 Enter all the cloned children into the hash table decl_table. */
7439 static dw_die_ref
7440 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7442 dw_die_ref c;
7443 dw_die_ref clone;
7444 struct decl_table_entry *entry;
7445 decl_table_entry **slot;
7447 if (die->die_tag == DW_TAG_subprogram)
7448 clone = clone_as_declaration (die);
7449 else
7450 clone = clone_die (die);
7452 slot = decl_table->find_slot_with_hash (die,
7453 htab_hash_pointer (die), INSERT);
7455 /* Assert that DIE isn't in the hash table yet. If it would be there
7456 before, the ancestors would be necessarily there as well, therefore
7457 clone_tree_partial wouldn't be called. */
7458 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7460 entry = XCNEW (struct decl_table_entry);
7461 entry->orig = die;
7462 entry->copy = clone;
7463 *slot = entry;
7465 if (die->die_tag != DW_TAG_subprogram)
7466 FOR_EACH_CHILD (die, c,
7467 add_child_die (clone, clone_tree_partial (c, decl_table)));
7469 return clone;
7472 /* Walk the DIE and its children, looking for references to incomplete
7473 or trivial types that are unmarked (i.e., that are not in the current
7474 type_unit). */
7476 static void
7477 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7479 dw_die_ref c;
7480 dw_attr_ref a;
7481 unsigned ix;
7483 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7485 if (AT_class (a) == dw_val_class_die_ref)
7487 dw_die_ref targ = AT_ref (a);
7488 decl_table_entry **slot;
7489 struct decl_table_entry *entry;
7491 if (targ->die_mark != 0 || targ->comdat_type_p)
7492 continue;
7494 slot = decl_table->find_slot_with_hash (targ,
7495 htab_hash_pointer (targ),
7496 INSERT);
7498 if (*slot != HTAB_EMPTY_ENTRY)
7500 /* TARG has already been copied, so we just need to
7501 modify the reference to point to the copy. */
7502 entry = *slot;
7503 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7505 else
7507 dw_die_ref parent = unit;
7508 dw_die_ref copy = clone_die (targ);
7510 /* Record in DECL_TABLE that TARG has been copied.
7511 Need to do this now, before the recursive call,
7512 because DECL_TABLE may be expanded and SLOT
7513 would no longer be a valid pointer. */
7514 entry = XCNEW (struct decl_table_entry);
7515 entry->orig = targ;
7516 entry->copy = copy;
7517 *slot = entry;
7519 /* If TARG is not a declaration DIE, we need to copy its
7520 children. */
7521 if (!is_declaration_die (targ))
7523 FOR_EACH_CHILD (
7524 targ, c,
7525 add_child_die (copy,
7526 clone_tree_partial (c, decl_table)));
7529 /* Make sure the cloned tree is marked as part of the
7530 type unit. */
7531 mark_dies (copy);
7533 /* If TARG has surrounding context, copy its ancestor tree
7534 into the new type unit. */
7535 if (targ->die_parent != NULL
7536 && !is_unit_die (targ->die_parent))
7537 parent = copy_ancestor_tree (unit, targ->die_parent,
7538 decl_table);
7540 add_child_die (parent, copy);
7541 a->dw_attr_val.v.val_die_ref.die = copy;
7543 /* Make sure the newly-copied DIE is walked. If it was
7544 installed in a previously-added context, it won't
7545 get visited otherwise. */
7546 if (parent != unit)
7548 /* Find the highest point of the newly-added tree,
7549 mark each node along the way, and walk from there. */
7550 parent->die_mark = 1;
7551 while (parent->die_parent
7552 && parent->die_parent->die_mark == 0)
7554 parent = parent->die_parent;
7555 parent->die_mark = 1;
7557 copy_decls_walk (unit, parent, decl_table);
7563 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7566 /* Copy declarations for "unworthy" types into the new comdat section.
7567 Incomplete types, modified types, and certain other types aren't broken
7568 out into comdat sections of their own, so they don't have a signature,
7569 and we need to copy the declaration into the same section so that we
7570 don't have an external reference. */
7572 static void
7573 copy_decls_for_unworthy_types (dw_die_ref unit)
7575 mark_dies (unit);
7576 decl_hash_type decl_table (10);
7577 copy_decls_walk (unit, unit, &decl_table);
7578 unmark_dies (unit);
7581 /* Traverse the DIE and add a sibling attribute if it may have the
7582 effect of speeding up access to siblings. To save some space,
7583 avoid generating sibling attributes for DIE's without children. */
7585 static void
7586 add_sibling_attributes (dw_die_ref die)
7588 dw_die_ref c;
7590 if (! die->die_child)
7591 return;
7593 if (die->die_parent && die != die->die_parent->die_child)
7594 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7596 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7599 /* Output all location lists for the DIE and its children. */
7601 static void
7602 output_location_lists (dw_die_ref die)
7604 dw_die_ref c;
7605 dw_attr_ref a;
7606 unsigned ix;
7608 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7609 if (AT_class (a) == dw_val_class_loc_list)
7610 output_loc_list (AT_loc_list (a));
7612 FOR_EACH_CHILD (die, c, output_location_lists (c));
7615 /* We want to limit the number of external references, because they are
7616 larger than local references: a relocation takes multiple words, and
7617 even a sig8 reference is always eight bytes, whereas a local reference
7618 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7619 So if we encounter multiple external references to the same type DIE, we
7620 make a local typedef stub for it and redirect all references there.
7622 This is the element of the hash table for keeping track of these
7623 references. */
7625 struct external_ref
7627 dw_die_ref type;
7628 dw_die_ref stub;
7629 unsigned n_refs;
7632 /* Hashtable helpers. */
7634 struct external_ref_hasher : typed_free_remove <external_ref>
7636 typedef external_ref value_type;
7637 typedef external_ref compare_type;
7638 static inline hashval_t hash (const value_type *);
7639 static inline bool equal (const value_type *, const compare_type *);
7642 inline hashval_t
7643 external_ref_hasher::hash (const value_type *r)
7645 dw_die_ref die = r->type;
7646 hashval_t h = 0;
7648 /* We can't use the address of the DIE for hashing, because
7649 that will make the order of the stub DIEs non-deterministic. */
7650 if (! die->comdat_type_p)
7651 /* We have a symbol; use it to compute a hash. */
7652 h = htab_hash_string (die->die_id.die_symbol);
7653 else
7655 /* We have a type signature; use a subset of the bits as the hash.
7656 The 8-byte signature is at least as large as hashval_t. */
7657 comdat_type_node_ref type_node = die->die_id.die_type_node;
7658 memcpy (&h, type_node->signature, sizeof (h));
7660 return h;
7663 inline bool
7664 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7666 return r1->type == r2->type;
7669 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7671 /* Return a pointer to the external_ref for references to DIE. */
7673 static struct external_ref *
7674 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7676 struct external_ref ref, *ref_p;
7677 external_ref **slot;
7679 ref.type = die;
7680 slot = map->find_slot (&ref, INSERT);
7681 if (*slot != HTAB_EMPTY_ENTRY)
7682 return *slot;
7684 ref_p = XCNEW (struct external_ref);
7685 ref_p->type = die;
7686 *slot = ref_p;
7687 return ref_p;
7690 /* Subroutine of optimize_external_refs, below.
7692 If we see a type skeleton, record it as our stub. If we see external
7693 references, remember how many we've seen. */
7695 static void
7696 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7698 dw_die_ref c;
7699 dw_attr_ref a;
7700 unsigned ix;
7701 struct external_ref *ref_p;
7703 if (is_type_die (die)
7704 && (c = get_AT_ref (die, DW_AT_signature)))
7706 /* This is a local skeleton; use it for local references. */
7707 ref_p = lookup_external_ref (map, c);
7708 ref_p->stub = die;
7711 /* Scan the DIE references, and remember any that refer to DIEs from
7712 other CUs (i.e. those which are not marked). */
7713 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7714 if (AT_class (a) == dw_val_class_die_ref
7715 && (c = AT_ref (a))->die_mark == 0
7716 && is_type_die (c))
7718 ref_p = lookup_external_ref (map, c);
7719 ref_p->n_refs++;
7722 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7725 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7726 points to an external_ref, DATA is the CU we're processing. If we don't
7727 already have a local stub, and we have multiple refs, build a stub. */
7730 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7732 struct external_ref *ref_p = *slot;
7734 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7736 /* We have multiple references to this type, so build a small stub.
7737 Both of these forms are a bit dodgy from the perspective of the
7738 DWARF standard, since technically they should have names. */
7739 dw_die_ref cu = data;
7740 dw_die_ref type = ref_p->type;
7741 dw_die_ref stub = NULL;
7743 if (type->comdat_type_p)
7745 /* If we refer to this type via sig8, use AT_signature. */
7746 stub = new_die (type->die_tag, cu, NULL_TREE);
7747 add_AT_die_ref (stub, DW_AT_signature, type);
7749 else
7751 /* Otherwise, use a typedef with no name. */
7752 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7753 add_AT_die_ref (stub, DW_AT_type, type);
7756 stub->die_mark++;
7757 ref_p->stub = stub;
7759 return 1;
7762 /* DIE is a unit; look through all the DIE references to see if there are
7763 any external references to types, and if so, create local stubs for
7764 them which will be applied in build_abbrev_table. This is useful because
7765 references to local DIEs are smaller. */
7767 static external_ref_hash_type *
7768 optimize_external_refs (dw_die_ref die)
7770 external_ref_hash_type *map = new external_ref_hash_type (10);
7771 optimize_external_refs_1 (die, map);
7772 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7773 return map;
7776 /* The format of each DIE (and its attribute value pairs) is encoded in an
7777 abbreviation table. This routine builds the abbreviation table and assigns
7778 a unique abbreviation id for each abbreviation entry. The children of each
7779 die are visited recursively. */
7781 static void
7782 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7784 unsigned long abbrev_id;
7785 unsigned int n_alloc;
7786 dw_die_ref c;
7787 dw_attr_ref a;
7788 unsigned ix;
7790 /* Scan the DIE references, and replace any that refer to
7791 DIEs from other CUs (i.e. those which are not marked) with
7792 the local stubs we built in optimize_external_refs. */
7793 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7794 if (AT_class (a) == dw_val_class_die_ref
7795 && (c = AT_ref (a))->die_mark == 0)
7797 struct external_ref *ref_p;
7798 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7800 ref_p = lookup_external_ref (extern_map, c);
7801 if (ref_p->stub && ref_p->stub != die)
7802 change_AT_die_ref (a, ref_p->stub);
7803 else
7804 /* We aren't changing this reference, so mark it external. */
7805 set_AT_ref_external (a, 1);
7808 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7810 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7811 dw_attr_ref die_a, abbrev_a;
7812 unsigned ix;
7813 bool ok = true;
7815 if (abbrev->die_tag != die->die_tag)
7816 continue;
7817 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7818 continue;
7820 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7821 continue;
7823 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7825 abbrev_a = &(*abbrev->die_attr)[ix];
7826 if ((abbrev_a->dw_attr != die_a->dw_attr)
7827 || (value_format (abbrev_a) != value_format (die_a)))
7829 ok = false;
7830 break;
7833 if (ok)
7834 break;
7837 if (abbrev_id >= abbrev_die_table_in_use)
7839 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7841 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7842 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7843 n_alloc);
7845 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7846 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7847 abbrev_die_table_allocated = n_alloc;
7850 ++abbrev_die_table_in_use;
7851 abbrev_die_table[abbrev_id] = die;
7854 die->die_abbrev = abbrev_id;
7855 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7858 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7860 static int
7861 constant_size (unsigned HOST_WIDE_INT value)
7863 int log;
7865 if (value == 0)
7866 log = 0;
7867 else
7868 log = floor_log2 (value);
7870 log = log / 8;
7871 log = 1 << (floor_log2 (log) + 1);
7873 return log;
7876 /* Return the size of a DIE as it is represented in the
7877 .debug_info section. */
7879 static unsigned long
7880 size_of_die (dw_die_ref die)
7882 unsigned long size = 0;
7883 dw_attr_ref a;
7884 unsigned ix;
7885 enum dwarf_form form;
7887 size += size_of_uleb128 (die->die_abbrev);
7888 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7890 switch (AT_class (a))
7892 case dw_val_class_addr:
7893 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7895 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7896 size += size_of_uleb128 (AT_index (a));
7898 else
7899 size += DWARF2_ADDR_SIZE;
7900 break;
7901 case dw_val_class_offset:
7902 size += DWARF_OFFSET_SIZE;
7903 break;
7904 case dw_val_class_loc:
7906 unsigned long lsize = size_of_locs (AT_loc (a));
7908 /* Block length. */
7909 if (dwarf_version >= 4)
7910 size += size_of_uleb128 (lsize);
7911 else
7912 size += constant_size (lsize);
7913 size += lsize;
7915 break;
7916 case dw_val_class_loc_list:
7917 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7919 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7920 size += size_of_uleb128 (AT_index (a));
7922 else
7923 size += DWARF_OFFSET_SIZE;
7924 break;
7925 case dw_val_class_range_list:
7926 size += DWARF_OFFSET_SIZE;
7927 break;
7928 case dw_val_class_const:
7929 size += size_of_sleb128 (AT_int (a));
7930 break;
7931 case dw_val_class_unsigned_const:
7933 int csize = constant_size (AT_unsigned (a));
7934 if (dwarf_version == 3
7935 && a->dw_attr == DW_AT_data_member_location
7936 && csize >= 4)
7937 size += size_of_uleb128 (AT_unsigned (a));
7938 else
7939 size += csize;
7941 break;
7942 case dw_val_class_const_double:
7943 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7944 if (HOST_BITS_PER_WIDE_INT >= 64)
7945 size++; /* block */
7946 break;
7947 case dw_val_class_wide_int:
7948 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7949 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7950 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7951 > 64)
7952 size++; /* block */
7953 break;
7954 case dw_val_class_vec:
7955 size += constant_size (a->dw_attr_val.v.val_vec.length
7956 * a->dw_attr_val.v.val_vec.elt_size)
7957 + a->dw_attr_val.v.val_vec.length
7958 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7959 break;
7960 case dw_val_class_flag:
7961 if (dwarf_version >= 4)
7962 /* Currently all add_AT_flag calls pass in 1 as last argument,
7963 so DW_FORM_flag_present can be used. If that ever changes,
7964 we'll need to use DW_FORM_flag and have some optimization
7965 in build_abbrev_table that will change those to
7966 DW_FORM_flag_present if it is set to 1 in all DIEs using
7967 the same abbrev entry. */
7968 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7969 else
7970 size += 1;
7971 break;
7972 case dw_val_class_die_ref:
7973 if (AT_ref_external (a))
7975 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7976 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7977 is sized by target address length, whereas in DWARF3
7978 it's always sized as an offset. */
7979 if (use_debug_types)
7980 size += DWARF_TYPE_SIGNATURE_SIZE;
7981 else if (dwarf_version == 2)
7982 size += DWARF2_ADDR_SIZE;
7983 else
7984 size += DWARF_OFFSET_SIZE;
7986 else
7987 size += DWARF_OFFSET_SIZE;
7988 break;
7989 case dw_val_class_fde_ref:
7990 size += DWARF_OFFSET_SIZE;
7991 break;
7992 case dw_val_class_lbl_id:
7993 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7995 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7996 size += size_of_uleb128 (AT_index (a));
7998 else
7999 size += DWARF2_ADDR_SIZE;
8000 break;
8001 case dw_val_class_lineptr:
8002 case dw_val_class_macptr:
8003 size += DWARF_OFFSET_SIZE;
8004 break;
8005 case dw_val_class_str:
8006 form = AT_string_form (a);
8007 if (form == DW_FORM_strp)
8008 size += DWARF_OFFSET_SIZE;
8009 else if (form == DW_FORM_GNU_str_index)
8010 size += size_of_uleb128 (AT_index (a));
8011 else
8012 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8013 break;
8014 case dw_val_class_file:
8015 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8016 break;
8017 case dw_val_class_data8:
8018 size += 8;
8019 break;
8020 case dw_val_class_vms_delta:
8021 size += DWARF_OFFSET_SIZE;
8022 break;
8023 case dw_val_class_high_pc:
8024 size += DWARF2_ADDR_SIZE;
8025 break;
8026 default:
8027 gcc_unreachable ();
8031 return size;
8034 /* Size the debugging information associated with a given DIE. Visits the
8035 DIE's children recursively. Updates the global variable next_die_offset, on
8036 each time through. Uses the current value of next_die_offset to update the
8037 die_offset field in each DIE. */
8039 static void
8040 calc_die_sizes (dw_die_ref die)
8042 dw_die_ref c;
8044 gcc_assert (die->die_offset == 0
8045 || (unsigned long int) die->die_offset == next_die_offset);
8046 die->die_offset = next_die_offset;
8047 next_die_offset += size_of_die (die);
8049 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8051 if (die->die_child != NULL)
8052 /* Count the null byte used to terminate sibling lists. */
8053 next_die_offset += 1;
8056 /* Size just the base type children at the start of the CU.
8057 This is needed because build_abbrev needs to size locs
8058 and sizing of type based stack ops needs to know die_offset
8059 values for the base types. */
8061 static void
8062 calc_base_type_die_sizes (void)
8064 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8065 unsigned int i;
8066 dw_die_ref base_type;
8067 #if ENABLE_ASSERT_CHECKING
8068 dw_die_ref prev = comp_unit_die ()->die_child;
8069 #endif
8071 die_offset += size_of_die (comp_unit_die ());
8072 for (i = 0; base_types.iterate (i, &base_type); i++)
8074 #if ENABLE_ASSERT_CHECKING
8075 gcc_assert (base_type->die_offset == 0
8076 && prev->die_sib == base_type
8077 && base_type->die_child == NULL
8078 && base_type->die_abbrev);
8079 prev = base_type;
8080 #endif
8081 base_type->die_offset = die_offset;
8082 die_offset += size_of_die (base_type);
8086 /* Set the marks for a die and its children. We do this so
8087 that we know whether or not a reference needs to use FORM_ref_addr; only
8088 DIEs in the same CU will be marked. We used to clear out the offset
8089 and use that as the flag, but ran into ordering problems. */
8091 static void
8092 mark_dies (dw_die_ref die)
8094 dw_die_ref c;
8096 gcc_assert (!die->die_mark);
8098 die->die_mark = 1;
8099 FOR_EACH_CHILD (die, c, mark_dies (c));
8102 /* Clear the marks for a die and its children. */
8104 static void
8105 unmark_dies (dw_die_ref die)
8107 dw_die_ref c;
8109 if (! use_debug_types)
8110 gcc_assert (die->die_mark);
8112 die->die_mark = 0;
8113 FOR_EACH_CHILD (die, c, unmark_dies (c));
8116 /* Clear the marks for a die, its children and referred dies. */
8118 static void
8119 unmark_all_dies (dw_die_ref die)
8121 dw_die_ref c;
8122 dw_attr_ref a;
8123 unsigned ix;
8125 if (!die->die_mark)
8126 return;
8127 die->die_mark = 0;
8129 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8131 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8132 if (AT_class (a) == dw_val_class_die_ref)
8133 unmark_all_dies (AT_ref (a));
8136 /* Calculate if the entry should appear in the final output file. It may be
8137 from a pruned a type. */
8139 static bool
8140 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8142 /* By limiting gnu pubnames to definitions only, gold can generate a
8143 gdb index without entries for declarations, which don't include
8144 enough information to be useful. */
8145 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8146 return false;
8148 if (table == pubname_table)
8150 /* Enumerator names are part of the pubname table, but the
8151 parent DW_TAG_enumeration_type die may have been pruned.
8152 Don't output them if that is the case. */
8153 if (p->die->die_tag == DW_TAG_enumerator &&
8154 (p->die->die_parent == NULL
8155 || !p->die->die_parent->die_perennial_p))
8156 return false;
8158 /* Everything else in the pubname table is included. */
8159 return true;
8162 /* The pubtypes table shouldn't include types that have been
8163 pruned. */
8164 return (p->die->die_offset != 0
8165 || !flag_eliminate_unused_debug_types);
8168 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8169 generated for the compilation unit. */
8171 static unsigned long
8172 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8174 unsigned long size;
8175 unsigned i;
8176 pubname_ref p;
8177 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8179 size = DWARF_PUBNAMES_HEADER_SIZE;
8180 FOR_EACH_VEC_ELT (*names, i, p)
8181 if (include_pubname_in_output (names, p))
8182 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8184 size += DWARF_OFFSET_SIZE;
8185 return size;
8188 /* Return the size of the information in the .debug_aranges section. */
8190 static unsigned long
8191 size_of_aranges (void)
8193 unsigned long size;
8195 size = DWARF_ARANGES_HEADER_SIZE;
8197 /* Count the address/length pair for this compilation unit. */
8198 if (text_section_used)
8199 size += 2 * DWARF2_ADDR_SIZE;
8200 if (cold_text_section_used)
8201 size += 2 * DWARF2_ADDR_SIZE;
8202 if (have_multiple_function_sections)
8204 unsigned fde_idx;
8205 dw_fde_ref fde;
8207 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8209 if (DECL_IGNORED_P (fde->decl))
8210 continue;
8211 if (!fde->in_std_section)
8212 size += 2 * DWARF2_ADDR_SIZE;
8213 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8214 size += 2 * DWARF2_ADDR_SIZE;
8218 /* Count the two zero words used to terminated the address range table. */
8219 size += 2 * DWARF2_ADDR_SIZE;
8220 return size;
8223 /* Select the encoding of an attribute value. */
8225 static enum dwarf_form
8226 value_format (dw_attr_ref a)
8228 switch (AT_class (a))
8230 case dw_val_class_addr:
8231 /* Only very few attributes allow DW_FORM_addr. */
8232 switch (a->dw_attr)
8234 case DW_AT_low_pc:
8235 case DW_AT_high_pc:
8236 case DW_AT_entry_pc:
8237 case DW_AT_trampoline:
8238 return (AT_index (a) == NOT_INDEXED
8239 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8240 default:
8241 break;
8243 switch (DWARF2_ADDR_SIZE)
8245 case 1:
8246 return DW_FORM_data1;
8247 case 2:
8248 return DW_FORM_data2;
8249 case 4:
8250 return DW_FORM_data4;
8251 case 8:
8252 return DW_FORM_data8;
8253 default:
8254 gcc_unreachable ();
8256 case dw_val_class_range_list:
8257 case dw_val_class_loc_list:
8258 if (dwarf_version >= 4)
8259 return DW_FORM_sec_offset;
8260 /* FALLTHRU */
8261 case dw_val_class_vms_delta:
8262 case dw_val_class_offset:
8263 switch (DWARF_OFFSET_SIZE)
8265 case 4:
8266 return DW_FORM_data4;
8267 case 8:
8268 return DW_FORM_data8;
8269 default:
8270 gcc_unreachable ();
8272 case dw_val_class_loc:
8273 if (dwarf_version >= 4)
8274 return DW_FORM_exprloc;
8275 switch (constant_size (size_of_locs (AT_loc (a))))
8277 case 1:
8278 return DW_FORM_block1;
8279 case 2:
8280 return DW_FORM_block2;
8281 case 4:
8282 return DW_FORM_block4;
8283 default:
8284 gcc_unreachable ();
8286 case dw_val_class_const:
8287 return DW_FORM_sdata;
8288 case dw_val_class_unsigned_const:
8289 switch (constant_size (AT_unsigned (a)))
8291 case 1:
8292 return DW_FORM_data1;
8293 case 2:
8294 return DW_FORM_data2;
8295 case 4:
8296 /* In DWARF3 DW_AT_data_member_location with
8297 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8298 constant, so we need to use DW_FORM_udata if we need
8299 a large constant. */
8300 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8301 return DW_FORM_udata;
8302 return DW_FORM_data4;
8303 case 8:
8304 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8305 return DW_FORM_udata;
8306 return DW_FORM_data8;
8307 default:
8308 gcc_unreachable ();
8310 case dw_val_class_const_double:
8311 switch (HOST_BITS_PER_WIDE_INT)
8313 case 8:
8314 return DW_FORM_data2;
8315 case 16:
8316 return DW_FORM_data4;
8317 case 32:
8318 return DW_FORM_data8;
8319 case 64:
8320 default:
8321 return DW_FORM_block1;
8323 case dw_val_class_wide_int:
8324 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8326 case 8:
8327 return DW_FORM_data1;
8328 case 16:
8329 return DW_FORM_data2;
8330 case 32:
8331 return DW_FORM_data4;
8332 case 64:
8333 return DW_FORM_data8;
8334 default:
8335 return DW_FORM_block1;
8337 case dw_val_class_vec:
8338 switch (constant_size (a->dw_attr_val.v.val_vec.length
8339 * a->dw_attr_val.v.val_vec.elt_size))
8341 case 1:
8342 return DW_FORM_block1;
8343 case 2:
8344 return DW_FORM_block2;
8345 case 4:
8346 return DW_FORM_block4;
8347 default:
8348 gcc_unreachable ();
8350 case dw_val_class_flag:
8351 if (dwarf_version >= 4)
8353 /* Currently all add_AT_flag calls pass in 1 as last argument,
8354 so DW_FORM_flag_present can be used. If that ever changes,
8355 we'll need to use DW_FORM_flag and have some optimization
8356 in build_abbrev_table that will change those to
8357 DW_FORM_flag_present if it is set to 1 in all DIEs using
8358 the same abbrev entry. */
8359 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8360 return DW_FORM_flag_present;
8362 return DW_FORM_flag;
8363 case dw_val_class_die_ref:
8364 if (AT_ref_external (a))
8365 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8366 else
8367 return DW_FORM_ref;
8368 case dw_val_class_fde_ref:
8369 return DW_FORM_data;
8370 case dw_val_class_lbl_id:
8371 return (AT_index (a) == NOT_INDEXED
8372 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8373 case dw_val_class_lineptr:
8374 case dw_val_class_macptr:
8375 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8376 case dw_val_class_str:
8377 return AT_string_form (a);
8378 case dw_val_class_file:
8379 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8381 case 1:
8382 return DW_FORM_data1;
8383 case 2:
8384 return DW_FORM_data2;
8385 case 4:
8386 return DW_FORM_data4;
8387 default:
8388 gcc_unreachable ();
8391 case dw_val_class_data8:
8392 return DW_FORM_data8;
8394 case dw_val_class_high_pc:
8395 switch (DWARF2_ADDR_SIZE)
8397 case 1:
8398 return DW_FORM_data1;
8399 case 2:
8400 return DW_FORM_data2;
8401 case 4:
8402 return DW_FORM_data4;
8403 case 8:
8404 return DW_FORM_data8;
8405 default:
8406 gcc_unreachable ();
8409 default:
8410 gcc_unreachable ();
8414 /* Output the encoding of an attribute value. */
8416 static void
8417 output_value_format (dw_attr_ref a)
8419 enum dwarf_form form = value_format (a);
8421 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8424 /* Given a die and id, produce the appropriate abbreviations. */
8426 static void
8427 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8429 unsigned ix;
8430 dw_attr_ref a_attr;
8432 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8433 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8434 dwarf_tag_name (abbrev->die_tag));
8436 if (abbrev->die_child != NULL)
8437 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8438 else
8439 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8441 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8443 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8444 dwarf_attr_name (a_attr->dw_attr));
8445 output_value_format (a_attr);
8448 dw2_asm_output_data (1, 0, NULL);
8449 dw2_asm_output_data (1, 0, NULL);
8453 /* Output the .debug_abbrev section which defines the DIE abbreviation
8454 table. */
8456 static void
8457 output_abbrev_section (void)
8459 unsigned long abbrev_id;
8461 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8462 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8464 /* Terminate the table. */
8465 dw2_asm_output_data (1, 0, NULL);
8468 /* Output a symbol we can use to refer to this DIE from another CU. */
8470 static inline void
8471 output_die_symbol (dw_die_ref die)
8473 const char *sym = die->die_id.die_symbol;
8475 gcc_assert (!die->comdat_type_p);
8477 if (sym == 0)
8478 return;
8480 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8481 /* We make these global, not weak; if the target doesn't support
8482 .linkonce, it doesn't support combining the sections, so debugging
8483 will break. */
8484 targetm.asm_out.globalize_label (asm_out_file, sym);
8486 ASM_OUTPUT_LABEL (asm_out_file, sym);
8489 /* Return a new location list, given the begin and end range, and the
8490 expression. */
8492 static inline dw_loc_list_ref
8493 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8494 const char *section)
8496 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8498 retlist->begin = begin;
8499 retlist->begin_entry = NULL;
8500 retlist->end = end;
8501 retlist->expr = expr;
8502 retlist->section = section;
8504 return retlist;
8507 /* Generate a new internal symbol for this location list node, if it
8508 hasn't got one yet. */
8510 static inline void
8511 gen_llsym (dw_loc_list_ref list)
8513 gcc_assert (!list->ll_symbol);
8514 list->ll_symbol = gen_internal_sym ("LLST");
8517 /* Output the location list given to us. */
8519 static void
8520 output_loc_list (dw_loc_list_ref list_head)
8522 dw_loc_list_ref curr = list_head;
8524 if (list_head->emitted)
8525 return;
8526 list_head->emitted = true;
8528 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8530 /* Walk the location list, and output each range + expression. */
8531 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8533 unsigned long size;
8534 /* Don't output an entry that starts and ends at the same address. */
8535 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8536 continue;
8537 size = size_of_locs (curr->expr);
8538 /* If the expression is too large, drop it on the floor. We could
8539 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8540 in the expression, but >= 64KB expressions for a single value
8541 in a single range are unlikely very useful. */
8542 if (size > 0xffff)
8543 continue;
8544 if (dwarf_split_debug_info)
8546 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8547 "Location list start/length entry (%s)",
8548 list_head->ll_symbol);
8549 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8550 "Location list range start index (%s)",
8551 curr->begin);
8552 /* The length field is 4 bytes. If we ever need to support
8553 an 8-byte length, we can add a new DW_LLE code or fall back
8554 to DW_LLE_GNU_start_end_entry. */
8555 dw2_asm_output_delta (4, curr->end, curr->begin,
8556 "Location list range length (%s)",
8557 list_head->ll_symbol);
8559 else if (!have_multiple_function_sections)
8561 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8562 "Location list begin address (%s)",
8563 list_head->ll_symbol);
8564 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8565 "Location list end address (%s)",
8566 list_head->ll_symbol);
8568 else
8570 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8571 "Location list begin address (%s)",
8572 list_head->ll_symbol);
8573 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8574 "Location list end address (%s)",
8575 list_head->ll_symbol);
8578 /* Output the block length for this list of location operations. */
8579 gcc_assert (size <= 0xffff);
8580 dw2_asm_output_data (2, size, "%s", "Location expression size");
8582 output_loc_sequence (curr->expr, -1);
8585 if (dwarf_split_debug_info)
8586 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8587 "Location list terminator (%s)",
8588 list_head->ll_symbol);
8589 else
8591 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8592 "Location list terminator begin (%s)",
8593 list_head->ll_symbol);
8594 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8595 "Location list terminator end (%s)",
8596 list_head->ll_symbol);
8600 /* Output a range_list offset into the debug_range section. Emit a
8601 relocated reference if val_entry is NULL, otherwise, emit an
8602 indirect reference. */
8604 static void
8605 output_range_list_offset (dw_attr_ref a)
8607 const char *name = dwarf_attr_name (a->dw_attr);
8609 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8611 char *p = strchr (ranges_section_label, '\0');
8612 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8613 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8614 debug_ranges_section, "%s", name);
8615 *p = '\0';
8617 else
8618 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8619 "%s (offset from %s)", name, ranges_section_label);
8622 /* Output the offset into the debug_loc section. */
8624 static void
8625 output_loc_list_offset (dw_attr_ref a)
8627 char *sym = AT_loc_list (a)->ll_symbol;
8629 gcc_assert (sym);
8630 if (dwarf_split_debug_info)
8631 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8632 "%s", dwarf_attr_name (a->dw_attr));
8633 else
8634 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8635 "%s", dwarf_attr_name (a->dw_attr));
8638 /* Output an attribute's index or value appropriately. */
8640 static void
8641 output_attr_index_or_value (dw_attr_ref a)
8643 const char *name = dwarf_attr_name (a->dw_attr);
8645 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8647 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8648 return;
8650 switch (AT_class (a))
8652 case dw_val_class_addr:
8653 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8654 break;
8655 case dw_val_class_high_pc:
8656 case dw_val_class_lbl_id:
8657 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8658 break;
8659 case dw_val_class_loc_list:
8660 output_loc_list_offset (a);
8661 break;
8662 default:
8663 gcc_unreachable ();
8667 /* Output a type signature. */
8669 static inline void
8670 output_signature (const char *sig, const char *name)
8672 int i;
8674 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8675 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8678 /* Output the DIE and its attributes. Called recursively to generate
8679 the definitions of each child DIE. */
8681 static void
8682 output_die (dw_die_ref die)
8684 dw_attr_ref a;
8685 dw_die_ref c;
8686 unsigned long size;
8687 unsigned ix;
8689 /* If someone in another CU might refer to us, set up a symbol for
8690 them to point to. */
8691 if (! die->comdat_type_p && die->die_id.die_symbol)
8692 output_die_symbol (die);
8694 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8695 (unsigned long)die->die_offset,
8696 dwarf_tag_name (die->die_tag));
8698 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8700 const char *name = dwarf_attr_name (a->dw_attr);
8702 switch (AT_class (a))
8704 case dw_val_class_addr:
8705 output_attr_index_or_value (a);
8706 break;
8708 case dw_val_class_offset:
8709 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8710 "%s", name);
8711 break;
8713 case dw_val_class_range_list:
8714 output_range_list_offset (a);
8715 break;
8717 case dw_val_class_loc:
8718 size = size_of_locs (AT_loc (a));
8720 /* Output the block length for this list of location operations. */
8721 if (dwarf_version >= 4)
8722 dw2_asm_output_data_uleb128 (size, "%s", name);
8723 else
8724 dw2_asm_output_data (constant_size (size), size, "%s", name);
8726 output_loc_sequence (AT_loc (a), -1);
8727 break;
8729 case dw_val_class_const:
8730 /* ??? It would be slightly more efficient to use a scheme like is
8731 used for unsigned constants below, but gdb 4.x does not sign
8732 extend. Gdb 5.x does sign extend. */
8733 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8734 break;
8736 case dw_val_class_unsigned_const:
8738 int csize = constant_size (AT_unsigned (a));
8739 if (dwarf_version == 3
8740 && a->dw_attr == DW_AT_data_member_location
8741 && csize >= 4)
8742 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8743 else
8744 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8746 break;
8748 case dw_val_class_const_double:
8750 unsigned HOST_WIDE_INT first, second;
8752 if (HOST_BITS_PER_WIDE_INT >= 64)
8753 dw2_asm_output_data (1,
8754 HOST_BITS_PER_DOUBLE_INT
8755 / HOST_BITS_PER_CHAR,
8756 NULL);
8758 if (WORDS_BIG_ENDIAN)
8760 first = a->dw_attr_val.v.val_double.high;
8761 second = a->dw_attr_val.v.val_double.low;
8763 else
8765 first = a->dw_attr_val.v.val_double.low;
8766 second = a->dw_attr_val.v.val_double.high;
8769 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8770 first, "%s", name);
8771 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8772 second, NULL);
8774 break;
8776 case dw_val_class_wide_int:
8778 int i;
8779 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8780 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8781 if (len * HOST_BITS_PER_WIDE_INT > 64)
8782 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8783 NULL);
8785 if (WORDS_BIG_ENDIAN)
8786 for (i = len - 1; i >= 0; --i)
8788 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8789 name);
8790 name = NULL;
8792 else
8793 for (i = 0; i < len; ++i)
8795 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8796 name);
8797 name = NULL;
8800 break;
8802 case dw_val_class_vec:
8804 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8805 unsigned int len = a->dw_attr_val.v.val_vec.length;
8806 unsigned int i;
8807 unsigned char *p;
8809 dw2_asm_output_data (constant_size (len * elt_size),
8810 len * elt_size, "%s", name);
8811 if (elt_size > sizeof (HOST_WIDE_INT))
8813 elt_size /= 2;
8814 len *= 2;
8816 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8817 i < len;
8818 i++, p += elt_size)
8819 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8820 "fp or vector constant word %u", i);
8821 break;
8824 case dw_val_class_flag:
8825 if (dwarf_version >= 4)
8827 /* Currently all add_AT_flag calls pass in 1 as last argument,
8828 so DW_FORM_flag_present can be used. If that ever changes,
8829 we'll need to use DW_FORM_flag and have some optimization
8830 in build_abbrev_table that will change those to
8831 DW_FORM_flag_present if it is set to 1 in all DIEs using
8832 the same abbrev entry. */
8833 gcc_assert (AT_flag (a) == 1);
8834 if (flag_debug_asm)
8835 fprintf (asm_out_file, "\t\t\t%s %s\n",
8836 ASM_COMMENT_START, name);
8837 break;
8839 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8840 break;
8842 case dw_val_class_loc_list:
8843 output_attr_index_or_value (a);
8844 break;
8846 case dw_val_class_die_ref:
8847 if (AT_ref_external (a))
8849 if (AT_ref (a)->comdat_type_p)
8851 comdat_type_node_ref type_node =
8852 AT_ref (a)->die_id.die_type_node;
8854 gcc_assert (type_node);
8855 output_signature (type_node->signature, name);
8857 else
8859 const char *sym = AT_ref (a)->die_id.die_symbol;
8860 int size;
8862 gcc_assert (sym);
8863 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8864 length, whereas in DWARF3 it's always sized as an
8865 offset. */
8866 if (dwarf_version == 2)
8867 size = DWARF2_ADDR_SIZE;
8868 else
8869 size = DWARF_OFFSET_SIZE;
8870 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8871 name);
8874 else
8876 gcc_assert (AT_ref (a)->die_offset);
8877 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8878 "%s", name);
8880 break;
8882 case dw_val_class_fde_ref:
8884 char l1[20];
8886 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8887 a->dw_attr_val.v.val_fde_index * 2);
8888 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8889 "%s", name);
8891 break;
8893 case dw_val_class_vms_delta:
8894 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8895 AT_vms_delta2 (a), AT_vms_delta1 (a),
8896 "%s", name);
8897 break;
8899 case dw_val_class_lbl_id:
8900 output_attr_index_or_value (a);
8901 break;
8903 case dw_val_class_lineptr:
8904 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8905 debug_line_section, "%s", name);
8906 break;
8908 case dw_val_class_macptr:
8909 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8910 debug_macinfo_section, "%s", name);
8911 break;
8913 case dw_val_class_str:
8914 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8915 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8916 a->dw_attr_val.v.val_str->label,
8917 debug_str_section,
8918 "%s: \"%s\"", name, AT_string (a));
8919 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8920 dw2_asm_output_data_uleb128 (AT_index (a),
8921 "%s: \"%s\"", name, AT_string (a));
8922 else
8923 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8924 break;
8926 case dw_val_class_file:
8928 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8930 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8931 a->dw_attr_val.v.val_file->filename);
8932 break;
8935 case dw_val_class_data8:
8937 int i;
8939 for (i = 0; i < 8; i++)
8940 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8941 i == 0 ? "%s" : NULL, name);
8942 break;
8945 case dw_val_class_high_pc:
8946 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8947 get_AT_low_pc (die), "DW_AT_high_pc");
8948 break;
8950 default:
8951 gcc_unreachable ();
8955 FOR_EACH_CHILD (die, c, output_die (c));
8957 /* Add null byte to terminate sibling list. */
8958 if (die->die_child != NULL)
8959 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8960 (unsigned long) die->die_offset);
8963 /* Output the compilation unit that appears at the beginning of the
8964 .debug_info section, and precedes the DIE descriptions. */
8966 static void
8967 output_compilation_unit_header (void)
8969 int ver = dwarf_version;
8971 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8972 dw2_asm_output_data (4, 0xffffffff,
8973 "Initial length escape value indicating 64-bit DWARF extension");
8974 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8975 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8976 "Length of Compilation Unit Info");
8977 dw2_asm_output_data (2, ver, "DWARF version number");
8978 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8979 debug_abbrev_section,
8980 "Offset Into Abbrev. Section");
8981 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8984 /* Output the compilation unit DIE and its children. */
8986 static void
8987 output_comp_unit (dw_die_ref die, int output_if_empty)
8989 const char *secname, *oldsym;
8990 char *tmp;
8992 /* Unless we are outputting main CU, we may throw away empty ones. */
8993 if (!output_if_empty && die->die_child == NULL)
8994 return;
8996 /* Even if there are no children of this DIE, we must output the information
8997 about the compilation unit. Otherwise, on an empty translation unit, we
8998 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8999 will then complain when examining the file. First mark all the DIEs in
9000 this CU so we know which get local refs. */
9001 mark_dies (die);
9003 external_ref_hash_type *extern_map = optimize_external_refs (die);
9005 build_abbrev_table (die, extern_map);
9007 delete extern_map;
9009 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9010 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9011 calc_die_sizes (die);
9013 oldsym = die->die_id.die_symbol;
9014 if (oldsym)
9016 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9018 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9019 secname = tmp;
9020 die->die_id.die_symbol = NULL;
9021 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9023 else
9025 switch_to_section (debug_info_section);
9026 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9027 info_section_emitted = true;
9030 /* Output debugging information. */
9031 output_compilation_unit_header ();
9032 output_die (die);
9034 /* Leave the marks on the main CU, so we can check them in
9035 output_pubnames. */
9036 if (oldsym)
9038 unmark_dies (die);
9039 die->die_id.die_symbol = oldsym;
9043 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9044 and .debug_pubtypes. This is configured per-target, but can be
9045 overridden by the -gpubnames or -gno-pubnames options. */
9047 static inline bool
9048 want_pubnames (void)
9050 if (debug_info_level <= DINFO_LEVEL_TERSE)
9051 return false;
9052 if (debug_generate_pub_sections != -1)
9053 return debug_generate_pub_sections;
9054 return targetm.want_debug_pub_sections;
9057 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9059 static void
9060 add_AT_pubnames (dw_die_ref die)
9062 if (want_pubnames ())
9063 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9066 /* Add a string attribute value to a skeleton DIE. */
9068 static inline void
9069 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9070 const char *str)
9072 dw_attr_node attr;
9073 struct indirect_string_node *node;
9075 if (! skeleton_debug_str_hash)
9076 skeleton_debug_str_hash
9077 = hash_table<indirect_string_hasher>::create_ggc (10);
9079 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9080 find_string_form (node);
9081 if (node->form == DW_FORM_GNU_str_index)
9082 node->form = DW_FORM_strp;
9084 attr.dw_attr = attr_kind;
9085 attr.dw_attr_val.val_class = dw_val_class_str;
9086 attr.dw_attr_val.val_entry = NULL;
9087 attr.dw_attr_val.v.val_str = node;
9088 add_dwarf_attr (die, &attr);
9091 /* Helper function to generate top-level dies for skeleton debug_info and
9092 debug_types. */
9094 static void
9095 add_top_level_skeleton_die_attrs (dw_die_ref die)
9097 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9098 const char *comp_dir = comp_dir_string ();
9100 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9101 if (comp_dir != NULL)
9102 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9103 add_AT_pubnames (die);
9104 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9107 /* Output skeleton debug sections that point to the dwo file. */
9109 static void
9110 output_skeleton_debug_sections (dw_die_ref comp_unit)
9112 /* These attributes will be found in the full debug_info section. */
9113 remove_AT (comp_unit, DW_AT_producer);
9114 remove_AT (comp_unit, DW_AT_language);
9116 switch_to_section (debug_skeleton_info_section);
9117 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9119 /* Produce the skeleton compilation-unit header. This one differs enough from
9120 a normal CU header that it's better not to call output_compilation_unit
9121 header. */
9122 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9123 dw2_asm_output_data (4, 0xffffffff,
9124 "Initial length escape value indicating 64-bit DWARF extension");
9126 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9127 DWARF_COMPILE_UNIT_HEADER_SIZE
9128 - DWARF_INITIAL_LENGTH_SIZE
9129 + size_of_die (comp_unit),
9130 "Length of Compilation Unit Info");
9131 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9132 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9133 debug_abbrev_section,
9134 "Offset Into Abbrev. Section");
9135 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9137 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9138 output_die (comp_unit);
9140 /* Build the skeleton debug_abbrev section. */
9141 switch_to_section (debug_skeleton_abbrev_section);
9142 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9144 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9146 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9149 /* Output a comdat type unit DIE and its children. */
9151 static void
9152 output_comdat_type_unit (comdat_type_node *node)
9154 const char *secname;
9155 char *tmp;
9156 int i;
9157 #if defined (OBJECT_FORMAT_ELF)
9158 tree comdat_key;
9159 #endif
9161 /* First mark all the DIEs in this CU so we know which get local refs. */
9162 mark_dies (node->root_die);
9164 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9166 build_abbrev_table (node->root_die, extern_map);
9168 delete extern_map;
9169 extern_map = NULL;
9171 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9172 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9173 calc_die_sizes (node->root_die);
9175 #if defined (OBJECT_FORMAT_ELF)
9176 if (!dwarf_split_debug_info)
9177 secname = ".debug_types";
9178 else
9179 secname = ".debug_types.dwo";
9181 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9182 sprintf (tmp, "wt.");
9183 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9184 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9185 comdat_key = get_identifier (tmp);
9186 targetm.asm_out.named_section (secname,
9187 SECTION_DEBUG | SECTION_LINKONCE,
9188 comdat_key);
9189 #else
9190 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9191 sprintf (tmp, ".gnu.linkonce.wt.");
9192 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9193 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9194 secname = tmp;
9195 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9196 #endif
9198 /* Output debugging information. */
9199 output_compilation_unit_header ();
9200 output_signature (node->signature, "Type Signature");
9201 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9202 "Offset to Type DIE");
9203 output_die (node->root_die);
9205 unmark_dies (node->root_die);
9208 /* Return the DWARF2/3 pubname associated with a decl. */
9210 static const char *
9211 dwarf2_name (tree decl, int scope)
9213 if (DECL_NAMELESS (decl))
9214 return NULL;
9215 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9218 /* Add a new entry to .debug_pubnames if appropriate. */
9220 static void
9221 add_pubname_string (const char *str, dw_die_ref die)
9223 pubname_entry e;
9225 e.die = die;
9226 e.name = xstrdup (str);
9227 vec_safe_push (pubname_table, e);
9230 static void
9231 add_pubname (tree decl, dw_die_ref die)
9233 if (!want_pubnames ())
9234 return;
9236 /* Don't add items to the table when we expect that the consumer will have
9237 just read the enclosing die. For example, if the consumer is looking at a
9238 class_member, it will either be inside the class already, or will have just
9239 looked up the class to find the member. Either way, searching the class is
9240 faster than searching the index. */
9241 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9242 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9244 const char *name = dwarf2_name (decl, 1);
9246 if (name)
9247 add_pubname_string (name, die);
9251 /* Add an enumerator to the pubnames section. */
9253 static void
9254 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9256 pubname_entry e;
9258 gcc_assert (scope_name);
9259 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9260 e.die = die;
9261 vec_safe_push (pubname_table, e);
9264 /* Add a new entry to .debug_pubtypes if appropriate. */
9266 static void
9267 add_pubtype (tree decl, dw_die_ref die)
9269 pubname_entry e;
9271 if (!want_pubnames ())
9272 return;
9274 if ((TREE_PUBLIC (decl)
9275 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9276 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9278 tree scope = NULL;
9279 const char *scope_name = "";
9280 const char *sep = is_cxx () ? "::" : ".";
9281 const char *name;
9283 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9284 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9286 scope_name = lang_hooks.dwarf_name (scope, 1);
9287 if (scope_name != NULL && scope_name[0] != '\0')
9288 scope_name = concat (scope_name, sep, NULL);
9289 else
9290 scope_name = "";
9293 if (TYPE_P (decl))
9294 name = type_tag (decl);
9295 else
9296 name = lang_hooks.dwarf_name (decl, 1);
9298 /* If we don't have a name for the type, there's no point in adding
9299 it to the table. */
9300 if (name != NULL && name[0] != '\0')
9302 e.die = die;
9303 e.name = concat (scope_name, name, NULL);
9304 vec_safe_push (pubtype_table, e);
9307 /* Although it might be more consistent to add the pubinfo for the
9308 enumerators as their dies are created, they should only be added if the
9309 enum type meets the criteria above. So rather than re-check the parent
9310 enum type whenever an enumerator die is created, just output them all
9311 here. This isn't protected by the name conditional because anonymous
9312 enums don't have names. */
9313 if (die->die_tag == DW_TAG_enumeration_type)
9315 dw_die_ref c;
9317 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9322 /* Output a single entry in the pubnames table. */
9324 static void
9325 output_pubname (dw_offset die_offset, pubname_entry *entry)
9327 dw_die_ref die = entry->die;
9328 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9330 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9332 if (debug_generate_pub_sections == 2)
9334 /* This logic follows gdb's method for determining the value of the flag
9335 byte. */
9336 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9337 switch (die->die_tag)
9339 case DW_TAG_typedef:
9340 case DW_TAG_base_type:
9341 case DW_TAG_subrange_type:
9342 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9343 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9344 break;
9345 case DW_TAG_enumerator:
9346 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9347 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9348 if (!is_cxx () && !is_java ())
9349 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9350 break;
9351 case DW_TAG_subprogram:
9352 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9353 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9354 if (!is_ada ())
9355 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9356 break;
9357 case DW_TAG_constant:
9358 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9359 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9360 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9361 break;
9362 case DW_TAG_variable:
9363 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9364 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9365 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9366 break;
9367 case DW_TAG_namespace:
9368 case DW_TAG_imported_declaration:
9369 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9370 break;
9371 case DW_TAG_class_type:
9372 case DW_TAG_interface_type:
9373 case DW_TAG_structure_type:
9374 case DW_TAG_union_type:
9375 case DW_TAG_enumeration_type:
9376 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9377 if (!is_cxx () && !is_java ())
9378 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9379 break;
9380 default:
9381 /* An unusual tag. Leave the flag-byte empty. */
9382 break;
9384 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9385 "GDB-index flags");
9388 dw2_asm_output_nstring (entry->name, -1, "external name");
9392 /* Output the public names table used to speed up access to externally
9393 visible names; or the public types table used to find type definitions. */
9395 static void
9396 output_pubnames (vec<pubname_entry, va_gc> *names)
9398 unsigned i;
9399 unsigned long pubnames_length = size_of_pubnames (names);
9400 pubname_ref pub;
9402 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9403 dw2_asm_output_data (4, 0xffffffff,
9404 "Initial length escape value indicating 64-bit DWARF extension");
9405 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9407 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9408 dw2_asm_output_data (2, 2, "DWARF Version");
9410 if (dwarf_split_debug_info)
9411 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9412 debug_skeleton_info_section,
9413 "Offset of Compilation Unit Info");
9414 else
9415 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9416 debug_info_section,
9417 "Offset of Compilation Unit Info");
9418 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9419 "Compilation Unit Length");
9421 FOR_EACH_VEC_ELT (*names, i, pub)
9423 if (include_pubname_in_output (names, pub))
9425 dw_offset die_offset = pub->die->die_offset;
9427 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9428 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9429 gcc_assert (pub->die->die_mark);
9431 /* If we're putting types in their own .debug_types sections,
9432 the .debug_pubtypes table will still point to the compile
9433 unit (not the type unit), so we want to use the offset of
9434 the skeleton DIE (if there is one). */
9435 if (pub->die->comdat_type_p && names == pubtype_table)
9437 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9439 if (type_node != NULL)
9440 die_offset = (type_node->skeleton_die != NULL
9441 ? type_node->skeleton_die->die_offset
9442 : comp_unit_die ()->die_offset);
9445 output_pubname (die_offset, pub);
9449 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9452 /* Output public names and types tables if necessary. */
9454 static void
9455 output_pubtables (void)
9457 if (!want_pubnames () || !info_section_emitted)
9458 return;
9460 switch_to_section (debug_pubnames_section);
9461 output_pubnames (pubname_table);
9462 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9463 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9464 simply won't look for the section. */
9465 switch_to_section (debug_pubtypes_section);
9466 output_pubnames (pubtype_table);
9470 /* Output the information that goes into the .debug_aranges table.
9471 Namely, define the beginning and ending address range of the
9472 text section generated for this compilation unit. */
9474 static void
9475 output_aranges (unsigned long aranges_length)
9477 unsigned i;
9479 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9480 dw2_asm_output_data (4, 0xffffffff,
9481 "Initial length escape value indicating 64-bit DWARF extension");
9482 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9483 "Length of Address Ranges Info");
9484 /* Version number for aranges is still 2, even in DWARF3. */
9485 dw2_asm_output_data (2, 2, "DWARF Version");
9486 if (dwarf_split_debug_info)
9487 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9488 debug_skeleton_info_section,
9489 "Offset of Compilation Unit Info");
9490 else
9491 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9492 debug_info_section,
9493 "Offset of Compilation Unit Info");
9494 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9495 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9497 /* We need to align to twice the pointer size here. */
9498 if (DWARF_ARANGES_PAD_SIZE)
9500 /* Pad using a 2 byte words so that padding is correct for any
9501 pointer size. */
9502 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9503 2 * DWARF2_ADDR_SIZE);
9504 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9505 dw2_asm_output_data (2, 0, NULL);
9508 /* It is necessary not to output these entries if the sections were
9509 not used; if the sections were not used, the length will be 0 and
9510 the address may end up as 0 if the section is discarded by ld
9511 --gc-sections, leaving an invalid (0, 0) entry that can be
9512 confused with the terminator. */
9513 if (text_section_used)
9515 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9516 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9517 text_section_label, "Length");
9519 if (cold_text_section_used)
9521 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9522 "Address");
9523 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9524 cold_text_section_label, "Length");
9527 if (have_multiple_function_sections)
9529 unsigned fde_idx;
9530 dw_fde_ref fde;
9532 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9534 if (DECL_IGNORED_P (fde->decl))
9535 continue;
9536 if (!fde->in_std_section)
9538 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9539 "Address");
9540 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9541 fde->dw_fde_begin, "Length");
9543 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9545 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9546 "Address");
9547 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9548 fde->dw_fde_second_begin, "Length");
9553 /* Output the terminator words. */
9554 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9555 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9558 /* Add a new entry to .debug_ranges. Return the offset at which it
9559 was placed. */
9561 static unsigned int
9562 add_ranges_num (int num)
9564 unsigned int in_use = ranges_table_in_use;
9566 if (in_use == ranges_table_allocated)
9568 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9569 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9570 ranges_table_allocated);
9571 memset (ranges_table + ranges_table_in_use, 0,
9572 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9575 ranges_table[in_use].num = num;
9576 ranges_table_in_use = in_use + 1;
9578 return in_use * 2 * DWARF2_ADDR_SIZE;
9581 /* Add a new entry to .debug_ranges corresponding to a block, or a
9582 range terminator if BLOCK is NULL. */
9584 static unsigned int
9585 add_ranges (const_tree block)
9587 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9590 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9591 When using dwarf_split_debug_info, address attributes in dies destined
9592 for the final executable should be direct references--setting the
9593 parameter force_direct ensures this behavior. */
9595 static void
9596 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9597 bool *added, bool force_direct)
9599 unsigned int in_use = ranges_by_label_in_use;
9600 unsigned int offset;
9602 if (in_use == ranges_by_label_allocated)
9604 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9605 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9606 ranges_by_label,
9607 ranges_by_label_allocated);
9608 memset (ranges_by_label + ranges_by_label_in_use, 0,
9609 RANGES_TABLE_INCREMENT
9610 * sizeof (struct dw_ranges_by_label_struct));
9613 ranges_by_label[in_use].begin = begin;
9614 ranges_by_label[in_use].end = end;
9615 ranges_by_label_in_use = in_use + 1;
9617 offset = add_ranges_num (-(int)in_use - 1);
9618 if (!*added)
9620 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9621 *added = true;
9625 static void
9626 output_ranges (void)
9628 unsigned i;
9629 static const char *const start_fmt = "Offset %#x";
9630 const char *fmt = start_fmt;
9632 for (i = 0; i < ranges_table_in_use; i++)
9634 int block_num = ranges_table[i].num;
9636 if (block_num > 0)
9638 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9639 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9641 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9642 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9644 /* If all code is in the text section, then the compilation
9645 unit base address defaults to DW_AT_low_pc, which is the
9646 base of the text section. */
9647 if (!have_multiple_function_sections)
9649 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9650 text_section_label,
9651 fmt, i * 2 * DWARF2_ADDR_SIZE);
9652 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9653 text_section_label, NULL);
9656 /* Otherwise, the compilation unit base address is zero,
9657 which allows us to use absolute addresses, and not worry
9658 about whether the target supports cross-section
9659 arithmetic. */
9660 else
9662 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9663 fmt, i * 2 * DWARF2_ADDR_SIZE);
9664 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9667 fmt = NULL;
9670 /* Negative block_num stands for an index into ranges_by_label. */
9671 else if (block_num < 0)
9673 int lab_idx = - block_num - 1;
9675 if (!have_multiple_function_sections)
9677 gcc_unreachable ();
9678 #if 0
9679 /* If we ever use add_ranges_by_labels () for a single
9680 function section, all we have to do is to take out
9681 the #if 0 above. */
9682 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9683 ranges_by_label[lab_idx].begin,
9684 text_section_label,
9685 fmt, i * 2 * DWARF2_ADDR_SIZE);
9686 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9687 ranges_by_label[lab_idx].end,
9688 text_section_label, NULL);
9689 #endif
9691 else
9693 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9694 ranges_by_label[lab_idx].begin,
9695 fmt, i * 2 * DWARF2_ADDR_SIZE);
9696 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9697 ranges_by_label[lab_idx].end,
9698 NULL);
9701 else
9703 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9704 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9705 fmt = start_fmt;
9710 /* Data structure containing information about input files. */
9711 struct file_info
9713 const char *path; /* Complete file name. */
9714 const char *fname; /* File name part. */
9715 int length; /* Length of entire string. */
9716 struct dwarf_file_data * file_idx; /* Index in input file table. */
9717 int dir_idx; /* Index in directory table. */
9720 /* Data structure containing information about directories with source
9721 files. */
9722 struct dir_info
9724 const char *path; /* Path including directory name. */
9725 int length; /* Path length. */
9726 int prefix; /* Index of directory entry which is a prefix. */
9727 int count; /* Number of files in this directory. */
9728 int dir_idx; /* Index of directory used as base. */
9731 /* Callback function for file_info comparison. We sort by looking at
9732 the directories in the path. */
9734 static int
9735 file_info_cmp (const void *p1, const void *p2)
9737 const struct file_info *const s1 = (const struct file_info *) p1;
9738 const struct file_info *const s2 = (const struct file_info *) p2;
9739 const unsigned char *cp1;
9740 const unsigned char *cp2;
9742 /* Take care of file names without directories. We need to make sure that
9743 we return consistent values to qsort since some will get confused if
9744 we return the same value when identical operands are passed in opposite
9745 orders. So if neither has a directory, return 0 and otherwise return
9746 1 or -1 depending on which one has the directory. */
9747 if ((s1->path == s1->fname || s2->path == s2->fname))
9748 return (s2->path == s2->fname) - (s1->path == s1->fname);
9750 cp1 = (const unsigned char *) s1->path;
9751 cp2 = (const unsigned char *) s2->path;
9753 while (1)
9755 ++cp1;
9756 ++cp2;
9757 /* Reached the end of the first path? If so, handle like above. */
9758 if ((cp1 == (const unsigned char *) s1->fname)
9759 || (cp2 == (const unsigned char *) s2->fname))
9760 return ((cp2 == (const unsigned char *) s2->fname)
9761 - (cp1 == (const unsigned char *) s1->fname));
9763 /* Character of current path component the same? */
9764 else if (*cp1 != *cp2)
9765 return *cp1 - *cp2;
9769 struct file_name_acquire_data
9771 struct file_info *files;
9772 int used_files;
9773 int max_files;
9776 /* Traversal function for the hash table. */
9779 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9781 struct dwarf_file_data *d = *slot;
9782 struct file_info *fi;
9783 const char *f;
9785 gcc_assert (fnad->max_files >= d->emitted_number);
9787 if (! d->emitted_number)
9788 return 1;
9790 gcc_assert (fnad->max_files != fnad->used_files);
9792 fi = fnad->files + fnad->used_files++;
9794 /* Skip all leading "./". */
9795 f = d->filename;
9796 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9797 f += 2;
9799 /* Create a new array entry. */
9800 fi->path = f;
9801 fi->length = strlen (f);
9802 fi->file_idx = d;
9804 /* Search for the file name part. */
9805 f = strrchr (f, DIR_SEPARATOR);
9806 #if defined (DIR_SEPARATOR_2)
9808 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9810 if (g != NULL)
9812 if (f == NULL || f < g)
9813 f = g;
9816 #endif
9818 fi->fname = f == NULL ? fi->path : f + 1;
9819 return 1;
9822 /* Output the directory table and the file name table. We try to minimize
9823 the total amount of memory needed. A heuristic is used to avoid large
9824 slowdowns with many input files. */
9826 static void
9827 output_file_names (void)
9829 struct file_name_acquire_data fnad;
9830 int numfiles;
9831 struct file_info *files;
9832 struct dir_info *dirs;
9833 int *saved;
9834 int *savehere;
9835 int *backmap;
9836 int ndirs;
9837 int idx_offset;
9838 int i;
9840 if (!last_emitted_file)
9842 dw2_asm_output_data (1, 0, "End directory table");
9843 dw2_asm_output_data (1, 0, "End file name table");
9844 return;
9847 numfiles = last_emitted_file->emitted_number;
9849 /* Allocate the various arrays we need. */
9850 files = XALLOCAVEC (struct file_info, numfiles);
9851 dirs = XALLOCAVEC (struct dir_info, numfiles);
9853 fnad.files = files;
9854 fnad.used_files = 0;
9855 fnad.max_files = numfiles;
9856 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9857 gcc_assert (fnad.used_files == fnad.max_files);
9859 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9861 /* Find all the different directories used. */
9862 dirs[0].path = files[0].path;
9863 dirs[0].length = files[0].fname - files[0].path;
9864 dirs[0].prefix = -1;
9865 dirs[0].count = 1;
9866 dirs[0].dir_idx = 0;
9867 files[0].dir_idx = 0;
9868 ndirs = 1;
9870 for (i = 1; i < numfiles; i++)
9871 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9872 && memcmp (dirs[ndirs - 1].path, files[i].path,
9873 dirs[ndirs - 1].length) == 0)
9875 /* Same directory as last entry. */
9876 files[i].dir_idx = ndirs - 1;
9877 ++dirs[ndirs - 1].count;
9879 else
9881 int j;
9883 /* This is a new directory. */
9884 dirs[ndirs].path = files[i].path;
9885 dirs[ndirs].length = files[i].fname - files[i].path;
9886 dirs[ndirs].count = 1;
9887 dirs[ndirs].dir_idx = ndirs;
9888 files[i].dir_idx = ndirs;
9890 /* Search for a prefix. */
9891 dirs[ndirs].prefix = -1;
9892 for (j = 0; j < ndirs; j++)
9893 if (dirs[j].length < dirs[ndirs].length
9894 && dirs[j].length > 1
9895 && (dirs[ndirs].prefix == -1
9896 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9897 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9898 dirs[ndirs].prefix = j;
9900 ++ndirs;
9903 /* Now to the actual work. We have to find a subset of the directories which
9904 allow expressing the file name using references to the directory table
9905 with the least amount of characters. We do not do an exhaustive search
9906 where we would have to check out every combination of every single
9907 possible prefix. Instead we use a heuristic which provides nearly optimal
9908 results in most cases and never is much off. */
9909 saved = XALLOCAVEC (int, ndirs);
9910 savehere = XALLOCAVEC (int, ndirs);
9912 memset (saved, '\0', ndirs * sizeof (saved[0]));
9913 for (i = 0; i < ndirs; i++)
9915 int j;
9916 int total;
9918 /* We can always save some space for the current directory. But this
9919 does not mean it will be enough to justify adding the directory. */
9920 savehere[i] = dirs[i].length;
9921 total = (savehere[i] - saved[i]) * dirs[i].count;
9923 for (j = i + 1; j < ndirs; j++)
9925 savehere[j] = 0;
9926 if (saved[j] < dirs[i].length)
9928 /* Determine whether the dirs[i] path is a prefix of the
9929 dirs[j] path. */
9930 int k;
9932 k = dirs[j].prefix;
9933 while (k != -1 && k != (int) i)
9934 k = dirs[k].prefix;
9936 if (k == (int) i)
9938 /* Yes it is. We can possibly save some memory by
9939 writing the filenames in dirs[j] relative to
9940 dirs[i]. */
9941 savehere[j] = dirs[i].length;
9942 total += (savehere[j] - saved[j]) * dirs[j].count;
9947 /* Check whether we can save enough to justify adding the dirs[i]
9948 directory. */
9949 if (total > dirs[i].length + 1)
9951 /* It's worthwhile adding. */
9952 for (j = i; j < ndirs; j++)
9953 if (savehere[j] > 0)
9955 /* Remember how much we saved for this directory so far. */
9956 saved[j] = savehere[j];
9958 /* Remember the prefix directory. */
9959 dirs[j].dir_idx = i;
9964 /* Emit the directory name table. */
9965 idx_offset = dirs[0].length > 0 ? 1 : 0;
9966 for (i = 1 - idx_offset; i < ndirs; i++)
9967 dw2_asm_output_nstring (dirs[i].path,
9968 dirs[i].length
9969 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9970 "Directory Entry: %#x", i + idx_offset);
9972 dw2_asm_output_data (1, 0, "End directory table");
9974 /* We have to emit them in the order of emitted_number since that's
9975 used in the debug info generation. To do this efficiently we
9976 generate a back-mapping of the indices first. */
9977 backmap = XALLOCAVEC (int, numfiles);
9978 for (i = 0; i < numfiles; i++)
9979 backmap[files[i].file_idx->emitted_number - 1] = i;
9981 /* Now write all the file names. */
9982 for (i = 0; i < numfiles; i++)
9984 int file_idx = backmap[i];
9985 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9987 #ifdef VMS_DEBUGGING_INFO
9988 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9990 /* Setting these fields can lead to debugger miscomparisons,
9991 but VMS Debug requires them to be set correctly. */
9993 int ver;
9994 long long cdt;
9995 long siz;
9996 int maxfilelen = strlen (files[file_idx].path)
9997 + dirs[dir_idx].length
9998 + MAX_VMS_VERSION_LEN + 1;
9999 char *filebuf = XALLOCAVEC (char, maxfilelen);
10001 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10002 snprintf (filebuf, maxfilelen, "%s;%d",
10003 files[file_idx].path + dirs[dir_idx].length, ver);
10005 dw2_asm_output_nstring
10006 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10008 /* Include directory index. */
10009 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10011 /* Modification time. */
10012 dw2_asm_output_data_uleb128
10013 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10014 ? cdt : 0,
10015 NULL);
10017 /* File length in bytes. */
10018 dw2_asm_output_data_uleb128
10019 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10020 ? siz : 0,
10021 NULL);
10022 #else
10023 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10024 "File Entry: %#x", (unsigned) i + 1);
10026 /* Include directory index. */
10027 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10029 /* Modification time. */
10030 dw2_asm_output_data_uleb128 (0, NULL);
10032 /* File length in bytes. */
10033 dw2_asm_output_data_uleb128 (0, NULL);
10034 #endif /* VMS_DEBUGGING_INFO */
10037 dw2_asm_output_data (1, 0, "End file name table");
10041 /* Output one line number table into the .debug_line section. */
10043 static void
10044 output_one_line_info_table (dw_line_info_table *table)
10046 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10047 unsigned int current_line = 1;
10048 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10049 dw_line_info_entry *ent;
10050 size_t i;
10052 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10054 switch (ent->opcode)
10056 case LI_set_address:
10057 /* ??? Unfortunately, we have little choice here currently, and
10058 must always use the most general form. GCC does not know the
10059 address delta itself, so we can't use DW_LNS_advance_pc. Many
10060 ports do have length attributes which will give an upper bound
10061 on the address range. We could perhaps use length attributes
10062 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10063 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10065 /* This can handle any delta. This takes
10066 4+DWARF2_ADDR_SIZE bytes. */
10067 dw2_asm_output_data (1, 0, "set address %s", line_label);
10068 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10069 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10070 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10071 break;
10073 case LI_set_line:
10074 if (ent->val == current_line)
10076 /* We still need to start a new row, so output a copy insn. */
10077 dw2_asm_output_data (1, DW_LNS_copy,
10078 "copy line %u", current_line);
10080 else
10082 int line_offset = ent->val - current_line;
10083 int line_delta = line_offset - DWARF_LINE_BASE;
10085 current_line = ent->val;
10086 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10088 /* This can handle deltas from -10 to 234, using the current
10089 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10090 This takes 1 byte. */
10091 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10092 "line %u", current_line);
10094 else
10096 /* This can handle any delta. This takes at least 4 bytes,
10097 depending on the value being encoded. */
10098 dw2_asm_output_data (1, DW_LNS_advance_line,
10099 "advance to line %u", current_line);
10100 dw2_asm_output_data_sleb128 (line_offset, NULL);
10101 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10104 break;
10106 case LI_set_file:
10107 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10108 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10109 break;
10111 case LI_set_column:
10112 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10113 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10114 break;
10116 case LI_negate_stmt:
10117 current_is_stmt = !current_is_stmt;
10118 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10119 "is_stmt %d", current_is_stmt);
10120 break;
10122 case LI_set_prologue_end:
10123 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10124 "set prologue end");
10125 break;
10127 case LI_set_epilogue_begin:
10128 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10129 "set epilogue begin");
10130 break;
10132 case LI_set_discriminator:
10133 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10134 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10135 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10136 dw2_asm_output_data_uleb128 (ent->val, NULL);
10137 break;
10141 /* Emit debug info for the address of the end of the table. */
10142 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10143 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10144 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10145 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10147 dw2_asm_output_data (1, 0, "end sequence");
10148 dw2_asm_output_data_uleb128 (1, NULL);
10149 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10152 /* Output the source line number correspondence information. This
10153 information goes into the .debug_line section. */
10155 static void
10156 output_line_info (bool prologue_only)
10158 char l1[20], l2[20], p1[20], p2[20];
10159 int ver = dwarf_version;
10160 bool saw_one = false;
10161 int opc;
10163 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10164 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10165 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10166 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10168 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10169 dw2_asm_output_data (4, 0xffffffff,
10170 "Initial length escape value indicating 64-bit DWARF extension");
10171 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10172 "Length of Source Line Info");
10173 ASM_OUTPUT_LABEL (asm_out_file, l1);
10175 dw2_asm_output_data (2, ver, "DWARF Version");
10176 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10177 ASM_OUTPUT_LABEL (asm_out_file, p1);
10179 /* Define the architecture-dependent minimum instruction length (in bytes).
10180 In this implementation of DWARF, this field is used for information
10181 purposes only. Since GCC generates assembly language, we have no
10182 a priori knowledge of how many instruction bytes are generated for each
10183 source line, and therefore can use only the DW_LNE_set_address and
10184 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10185 this as '1', which is "correct enough" for all architectures,
10186 and don't let the target override. */
10187 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10189 if (ver >= 4)
10190 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10191 "Maximum Operations Per Instruction");
10192 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10193 "Default is_stmt_start flag");
10194 dw2_asm_output_data (1, DWARF_LINE_BASE,
10195 "Line Base Value (Special Opcodes)");
10196 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10197 "Line Range Value (Special Opcodes)");
10198 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10199 "Special Opcode Base");
10201 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10203 int n_op_args;
10204 switch (opc)
10206 case DW_LNS_advance_pc:
10207 case DW_LNS_advance_line:
10208 case DW_LNS_set_file:
10209 case DW_LNS_set_column:
10210 case DW_LNS_fixed_advance_pc:
10211 case DW_LNS_set_isa:
10212 n_op_args = 1;
10213 break;
10214 default:
10215 n_op_args = 0;
10216 break;
10219 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10220 opc, n_op_args);
10223 /* Write out the information about the files we use. */
10224 output_file_names ();
10225 ASM_OUTPUT_LABEL (asm_out_file, p2);
10226 if (prologue_only)
10228 /* Output the marker for the end of the line number info. */
10229 ASM_OUTPUT_LABEL (asm_out_file, l2);
10230 return;
10233 if (separate_line_info)
10235 dw_line_info_table *table;
10236 size_t i;
10238 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10239 if (table->in_use)
10241 output_one_line_info_table (table);
10242 saw_one = true;
10245 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10247 output_one_line_info_table (cold_text_section_line_info);
10248 saw_one = true;
10251 /* ??? Some Darwin linkers crash on a .debug_line section with no
10252 sequences. Further, merely a DW_LNE_end_sequence entry is not
10253 sufficient -- the address column must also be initialized.
10254 Make sure to output at least one set_address/end_sequence pair,
10255 choosing .text since that section is always present. */
10256 if (text_section_line_info->in_use || !saw_one)
10257 output_one_line_info_table (text_section_line_info);
10259 /* Output the marker for the end of the line number info. */
10260 ASM_OUTPUT_LABEL (asm_out_file, l2);
10263 /* Given a pointer to a tree node for some base type, return a pointer to
10264 a DIE that describes the given type.
10266 This routine must only be called for GCC type nodes that correspond to
10267 Dwarf base (fundamental) types. */
10269 static dw_die_ref
10270 base_type_die (tree type)
10272 dw_die_ref base_type_result;
10273 enum dwarf_type encoding;
10275 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10276 return 0;
10278 /* If this is a subtype that should not be emitted as a subrange type,
10279 use the base type. See subrange_type_for_debug_p. */
10280 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10281 type = TREE_TYPE (type);
10283 switch (TREE_CODE (type))
10285 case INTEGER_TYPE:
10286 if ((dwarf_version >= 4 || !dwarf_strict)
10287 && TYPE_NAME (type)
10288 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10289 && DECL_IS_BUILTIN (TYPE_NAME (type))
10290 && DECL_NAME (TYPE_NAME (type)))
10292 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10293 if (strcmp (name, "char16_t") == 0
10294 || strcmp (name, "char32_t") == 0)
10296 encoding = DW_ATE_UTF;
10297 break;
10300 if (TYPE_STRING_FLAG (type))
10302 if (TYPE_UNSIGNED (type))
10303 encoding = DW_ATE_unsigned_char;
10304 else
10305 encoding = DW_ATE_signed_char;
10307 else if (TYPE_UNSIGNED (type))
10308 encoding = DW_ATE_unsigned;
10309 else
10310 encoding = DW_ATE_signed;
10311 break;
10313 case REAL_TYPE:
10314 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10316 if (dwarf_version >= 3 || !dwarf_strict)
10317 encoding = DW_ATE_decimal_float;
10318 else
10319 encoding = DW_ATE_lo_user;
10321 else
10322 encoding = DW_ATE_float;
10323 break;
10325 case FIXED_POINT_TYPE:
10326 if (!(dwarf_version >= 3 || !dwarf_strict))
10327 encoding = DW_ATE_lo_user;
10328 else if (TYPE_UNSIGNED (type))
10329 encoding = DW_ATE_unsigned_fixed;
10330 else
10331 encoding = DW_ATE_signed_fixed;
10332 break;
10334 /* Dwarf2 doesn't know anything about complex ints, so use
10335 a user defined type for it. */
10336 case COMPLEX_TYPE:
10337 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10338 encoding = DW_ATE_complex_float;
10339 else
10340 encoding = DW_ATE_lo_user;
10341 break;
10343 case BOOLEAN_TYPE:
10344 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10345 encoding = DW_ATE_boolean;
10346 break;
10348 default:
10349 /* No other TREE_CODEs are Dwarf fundamental types. */
10350 gcc_unreachable ();
10353 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10355 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10356 int_size_in_bytes (type));
10357 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10358 add_pubtype (type, base_type_result);
10360 return base_type_result;
10363 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10364 named 'auto' in its type: return true for it, false otherwise. */
10366 static inline bool
10367 is_cxx_auto (tree type)
10369 if (is_cxx ())
10371 tree name = TYPE_IDENTIFIER (type);
10372 if (name == get_identifier ("auto")
10373 || name == get_identifier ("decltype(auto)"))
10374 return true;
10376 return false;
10379 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10380 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10382 static inline int
10383 is_base_type (tree type)
10385 switch (TREE_CODE (type))
10387 case ERROR_MARK:
10388 case VOID_TYPE:
10389 case INTEGER_TYPE:
10390 case REAL_TYPE:
10391 case FIXED_POINT_TYPE:
10392 case COMPLEX_TYPE:
10393 case BOOLEAN_TYPE:
10394 case POINTER_BOUNDS_TYPE:
10395 return 1;
10397 case ARRAY_TYPE:
10398 case RECORD_TYPE:
10399 case UNION_TYPE:
10400 case QUAL_UNION_TYPE:
10401 case ENUMERAL_TYPE:
10402 case FUNCTION_TYPE:
10403 case METHOD_TYPE:
10404 case POINTER_TYPE:
10405 case REFERENCE_TYPE:
10406 case NULLPTR_TYPE:
10407 case OFFSET_TYPE:
10408 case LANG_TYPE:
10409 case VECTOR_TYPE:
10410 return 0;
10412 default:
10413 if (is_cxx_auto (type))
10414 return 0;
10415 gcc_unreachable ();
10418 return 0;
10421 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10422 node, return the size in bits for the type if it is a constant, or else
10423 return the alignment for the type if the type's size is not constant, or
10424 else return BITS_PER_WORD if the type actually turns out to be an
10425 ERROR_MARK node. */
10427 static inline unsigned HOST_WIDE_INT
10428 simple_type_size_in_bits (const_tree type)
10430 if (TREE_CODE (type) == ERROR_MARK)
10431 return BITS_PER_WORD;
10432 else if (TYPE_SIZE (type) == NULL_TREE)
10433 return 0;
10434 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10435 return tree_to_uhwi (TYPE_SIZE (type));
10436 else
10437 return TYPE_ALIGN (type);
10440 /* Similarly, but return an offset_int instead of UHWI. */
10442 static inline offset_int
10443 offset_int_type_size_in_bits (const_tree type)
10445 if (TREE_CODE (type) == ERROR_MARK)
10446 return BITS_PER_WORD;
10447 else if (TYPE_SIZE (type) == NULL_TREE)
10448 return 0;
10449 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10450 return wi::to_offset (TYPE_SIZE (type));
10451 else
10452 return TYPE_ALIGN (type);
10455 /* Given a pointer to a tree node for a subrange type, return a pointer
10456 to a DIE that describes the given type. */
10458 static dw_die_ref
10459 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10461 dw_die_ref subrange_die;
10462 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10464 if (context_die == NULL)
10465 context_die = comp_unit_die ();
10467 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10469 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10471 /* The size of the subrange type and its base type do not match,
10472 so we need to generate a size attribute for the subrange type. */
10473 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10476 if (low)
10477 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10478 if (high)
10479 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10481 return subrange_die;
10484 /* Returns the (const and/or volatile) cv_qualifiers associated with
10485 the decl node. This will normally be augmented with the
10486 cv_qualifiers of the underlying type in add_type_attribute. */
10488 static int
10489 decl_quals (const_tree decl)
10491 return ((TREE_READONLY (decl)
10492 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10493 | (TREE_THIS_VOLATILE (decl)
10494 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10497 /* Determine the TYPE whose qualifiers match the largest strict subset
10498 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10499 qualifiers outside QUAL_MASK. */
10501 static int
10502 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10504 tree t;
10505 int best_rank = 0, best_qual = 0, max_rank;
10507 type_quals &= qual_mask;
10508 max_rank = popcount_hwi (type_quals) - 1;
10510 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10511 t = TYPE_NEXT_VARIANT (t))
10513 int q = TYPE_QUALS (t) & qual_mask;
10515 if ((q & type_quals) == q && q != type_quals
10516 && check_base_type (t, type))
10518 int rank = popcount_hwi (q);
10520 if (rank > best_rank)
10522 best_rank = rank;
10523 best_qual = q;
10528 return best_qual;
10531 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10532 entry that chains various modifiers in front of the given type. */
10534 static dw_die_ref
10535 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10537 enum tree_code code = TREE_CODE (type);
10538 dw_die_ref mod_type_die;
10539 dw_die_ref sub_die = NULL;
10540 tree item_type = NULL;
10541 tree qualified_type;
10542 tree name, low, high;
10543 dw_die_ref mod_scope;
10544 /* Only these cv-qualifiers are currently handled. */
10545 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10546 | TYPE_QUAL_RESTRICT);
10548 if (code == ERROR_MARK)
10549 return NULL;
10551 cv_quals &= cv_qual_mask;
10553 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10554 tag modifier (and not an attribute) old consumers won't be able
10555 to handle it. */
10556 if (dwarf_version < 3)
10557 cv_quals &= ~TYPE_QUAL_RESTRICT;
10559 /* See if we already have the appropriately qualified variant of
10560 this type. */
10561 qualified_type = get_qualified_type (type, cv_quals);
10563 if (qualified_type == sizetype
10564 && TYPE_NAME (qualified_type)
10565 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10567 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10569 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10570 && TYPE_PRECISION (t)
10571 == TYPE_PRECISION (qualified_type)
10572 && TYPE_UNSIGNED (t)
10573 == TYPE_UNSIGNED (qualified_type));
10574 qualified_type = t;
10577 /* If we do, then we can just use its DIE, if it exists. */
10578 if (qualified_type)
10580 mod_type_die = lookup_type_die (qualified_type);
10581 if (mod_type_die)
10582 return mod_type_die;
10585 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10587 /* Handle C typedef types. */
10588 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10589 && !DECL_ARTIFICIAL (name))
10591 tree dtype = TREE_TYPE (name);
10593 if (qualified_type == dtype)
10595 /* For a named type, use the typedef. */
10596 gen_type_die (qualified_type, context_die);
10597 return lookup_type_die (qualified_type);
10599 else
10601 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10602 dquals &= cv_qual_mask;
10603 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10604 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10605 /* cv-unqualified version of named type. Just use
10606 the unnamed type to which it refers. */
10607 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10608 cv_quals, context_die);
10609 /* Else cv-qualified version of named type; fall through. */
10613 mod_scope = scope_die_for (type, context_die);
10615 if (cv_quals)
10617 struct qual_info { int q; enum dwarf_tag t; };
10618 static const struct qual_info qual_info[] =
10620 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10621 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10622 { TYPE_QUAL_CONST, DW_TAG_const_type },
10624 int sub_quals;
10625 unsigned i;
10627 /* Determine a lesser qualified type that most closely matches
10628 this one. Then generate DW_TAG_* entries for the remaining
10629 qualifiers. */
10630 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10631 cv_qual_mask);
10632 mod_type_die = modified_type_die (type, sub_quals, context_die);
10634 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10635 if (qual_info[i].q & cv_quals & ~sub_quals)
10637 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10638 if (mod_type_die)
10639 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10640 mod_type_die = d;
10643 else if (code == POINTER_TYPE)
10645 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10646 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10647 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10648 item_type = TREE_TYPE (type);
10649 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10650 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10651 TYPE_ADDR_SPACE (item_type));
10653 else if (code == REFERENCE_TYPE)
10655 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10656 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10657 type);
10658 else
10659 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10660 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10661 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10662 item_type = TREE_TYPE (type);
10663 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10664 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10665 TYPE_ADDR_SPACE (item_type));
10667 else if (code == INTEGER_TYPE
10668 && TREE_TYPE (type) != NULL_TREE
10669 && subrange_type_for_debug_p (type, &low, &high))
10671 mod_type_die = subrange_type_die (type, low, high, context_die);
10672 item_type = TREE_TYPE (type);
10674 else if (is_base_type (type))
10675 mod_type_die = base_type_die (type);
10676 else
10678 gen_type_die (type, context_die);
10680 /* We have to get the type_main_variant here (and pass that to the
10681 `lookup_type_die' routine) because the ..._TYPE node we have
10682 might simply be a *copy* of some original type node (where the
10683 copy was created to help us keep track of typedef names) and
10684 that copy might have a different TYPE_UID from the original
10685 ..._TYPE node. */
10686 if (TREE_CODE (type) != VECTOR_TYPE)
10687 return lookup_type_die (type_main_variant (type));
10688 else
10689 /* Vectors have the debugging information in the type,
10690 not the main variant. */
10691 return lookup_type_die (type);
10694 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10695 don't output a DW_TAG_typedef, since there isn't one in the
10696 user's program; just attach a DW_AT_name to the type.
10697 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10698 if the base type already has the same name. */
10699 if (name
10700 && ((TREE_CODE (name) != TYPE_DECL
10701 && (qualified_type == TYPE_MAIN_VARIANT (type)
10702 || (cv_quals == TYPE_UNQUALIFIED)))
10703 || (TREE_CODE (name) == TYPE_DECL
10704 && TREE_TYPE (name) == qualified_type
10705 && DECL_NAME (name))))
10707 if (TREE_CODE (name) == TYPE_DECL)
10708 /* Could just call add_name_and_src_coords_attributes here,
10709 but since this is a builtin type it doesn't have any
10710 useful source coordinates anyway. */
10711 name = DECL_NAME (name);
10712 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10714 /* This probably indicates a bug. */
10715 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10717 name = TYPE_IDENTIFIER (type);
10718 add_name_attribute (mod_type_die,
10719 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10722 if (qualified_type)
10723 equate_type_number_to_die (qualified_type, mod_type_die);
10725 if (item_type)
10726 /* We must do this after the equate_type_number_to_die call, in case
10727 this is a recursive type. This ensures that the modified_type_die
10728 recursion will terminate even if the type is recursive. Recursive
10729 types are possible in Ada. */
10730 sub_die = modified_type_die (item_type,
10731 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10732 context_die);
10734 if (sub_die != NULL)
10735 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10737 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10738 if (TYPE_ARTIFICIAL (type))
10739 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10741 return mod_type_die;
10744 /* Generate DIEs for the generic parameters of T.
10745 T must be either a generic type or a generic function.
10746 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10748 static void
10749 gen_generic_params_dies (tree t)
10751 tree parms, args;
10752 int parms_num, i;
10753 dw_die_ref die = NULL;
10754 int non_default;
10756 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10757 return;
10759 if (TYPE_P (t))
10760 die = lookup_type_die (t);
10761 else if (DECL_P (t))
10762 die = lookup_decl_die (t);
10764 gcc_assert (die);
10766 parms = lang_hooks.get_innermost_generic_parms (t);
10767 if (!parms)
10768 /* T has no generic parameter. It means T is neither a generic type
10769 or function. End of story. */
10770 return;
10772 parms_num = TREE_VEC_LENGTH (parms);
10773 args = lang_hooks.get_innermost_generic_args (t);
10774 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10775 non_default = int_cst_value (TREE_CHAIN (args));
10776 else
10777 non_default = TREE_VEC_LENGTH (args);
10778 for (i = 0; i < parms_num; i++)
10780 tree parm, arg, arg_pack_elems;
10781 dw_die_ref parm_die;
10783 parm = TREE_VEC_ELT (parms, i);
10784 arg = TREE_VEC_ELT (args, i);
10785 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10786 gcc_assert (parm && TREE_VALUE (parm) && arg);
10788 if (parm && TREE_VALUE (parm) && arg)
10790 /* If PARM represents a template parameter pack,
10791 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10792 by DW_TAG_template_*_parameter DIEs for the argument
10793 pack elements of ARG. Note that ARG would then be
10794 an argument pack. */
10795 if (arg_pack_elems)
10796 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10797 arg_pack_elems,
10798 die);
10799 else
10800 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10801 true /* emit name */, die);
10802 if (i >= non_default)
10803 add_AT_flag (parm_die, DW_AT_default_value, 1);
10808 /* Create and return a DIE for PARM which should be
10809 the representation of a generic type parameter.
10810 For instance, in the C++ front end, PARM would be a template parameter.
10811 ARG is the argument to PARM.
10812 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10813 name of the PARM.
10814 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10815 as a child node. */
10817 static dw_die_ref
10818 generic_parameter_die (tree parm, tree arg,
10819 bool emit_name_p,
10820 dw_die_ref parent_die)
10822 dw_die_ref tmpl_die = NULL;
10823 const char *name = NULL;
10825 if (!parm || !DECL_NAME (parm) || !arg)
10826 return NULL;
10828 /* We support non-type generic parameters and arguments,
10829 type generic parameters and arguments, as well as
10830 generic generic parameters (a.k.a. template template parameters in C++)
10831 and arguments. */
10832 if (TREE_CODE (parm) == PARM_DECL)
10833 /* PARM is a nontype generic parameter */
10834 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10835 else if (TREE_CODE (parm) == TYPE_DECL)
10836 /* PARM is a type generic parameter. */
10837 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10838 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10839 /* PARM is a generic generic parameter.
10840 Its DIE is a GNU extension. It shall have a
10841 DW_AT_name attribute to represent the name of the template template
10842 parameter, and a DW_AT_GNU_template_name attribute to represent the
10843 name of the template template argument. */
10844 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10845 parent_die, parm);
10846 else
10847 gcc_unreachable ();
10849 if (tmpl_die)
10851 tree tmpl_type;
10853 /* If PARM is a generic parameter pack, it means we are
10854 emitting debug info for a template argument pack element.
10855 In other terms, ARG is a template argument pack element.
10856 In that case, we don't emit any DW_AT_name attribute for
10857 the die. */
10858 if (emit_name_p)
10860 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10861 gcc_assert (name);
10862 add_AT_string (tmpl_die, DW_AT_name, name);
10865 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10867 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10868 TMPL_DIE should have a child DW_AT_type attribute that is set
10869 to the type of the argument to PARM, which is ARG.
10870 If PARM is a type generic parameter, TMPL_DIE should have a
10871 child DW_AT_type that is set to ARG. */
10872 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10873 add_type_attribute (tmpl_die, tmpl_type,
10874 (TREE_THIS_VOLATILE (tmpl_type)
10875 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10876 parent_die);
10878 else
10880 /* So TMPL_DIE is a DIE representing a
10881 a generic generic template parameter, a.k.a template template
10882 parameter in C++ and arg is a template. */
10884 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10885 to the name of the argument. */
10886 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10887 if (name)
10888 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10891 if (TREE_CODE (parm) == PARM_DECL)
10892 /* So PARM is a non-type generic parameter.
10893 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10894 attribute of TMPL_DIE which value represents the value
10895 of ARG.
10896 We must be careful here:
10897 The value of ARG might reference some function decls.
10898 We might currently be emitting debug info for a generic
10899 type and types are emitted before function decls, we don't
10900 know if the function decls referenced by ARG will actually be
10901 emitted after cgraph computations.
10902 So must defer the generation of the DW_AT_const_value to
10903 after cgraph is ready. */
10904 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10907 return tmpl_die;
10910 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10911 PARM_PACK must be a template parameter pack. The returned DIE
10912 will be child DIE of PARENT_DIE. */
10914 static dw_die_ref
10915 template_parameter_pack_die (tree parm_pack,
10916 tree parm_pack_args,
10917 dw_die_ref parent_die)
10919 dw_die_ref die;
10920 int j;
10922 gcc_assert (parent_die && parm_pack);
10924 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10925 add_name_and_src_coords_attributes (die, parm_pack);
10926 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10927 generic_parameter_die (parm_pack,
10928 TREE_VEC_ELT (parm_pack_args, j),
10929 false /* Don't emit DW_AT_name */,
10930 die);
10931 return die;
10934 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10935 an enumerated type. */
10937 static inline int
10938 type_is_enum (const_tree type)
10940 return TREE_CODE (type) == ENUMERAL_TYPE;
10943 /* Return the DBX register number described by a given RTL node. */
10945 static unsigned int
10946 dbx_reg_number (const_rtx rtl)
10948 unsigned regno = REGNO (rtl);
10950 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10952 #ifdef LEAF_REG_REMAP
10953 if (crtl->uses_only_leaf_regs)
10955 int leaf_reg = LEAF_REG_REMAP (regno);
10956 if (leaf_reg != -1)
10957 regno = (unsigned) leaf_reg;
10959 #endif
10961 regno = DBX_REGISTER_NUMBER (regno);
10962 gcc_assert (regno != INVALID_REGNUM);
10963 return regno;
10966 /* Optionally add a DW_OP_piece term to a location description expression.
10967 DW_OP_piece is only added if the location description expression already
10968 doesn't end with DW_OP_piece. */
10970 static void
10971 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10973 dw_loc_descr_ref loc;
10975 if (*list_head != NULL)
10977 /* Find the end of the chain. */
10978 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10981 if (loc->dw_loc_opc != DW_OP_piece)
10982 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10986 /* Return a location descriptor that designates a machine register or
10987 zero if there is none. */
10989 static dw_loc_descr_ref
10990 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10992 rtx regs;
10994 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10995 return 0;
10997 /* We only use "frame base" when we're sure we're talking about the
10998 post-prologue local stack frame. We do this by *not* running
10999 register elimination until this point, and recognizing the special
11000 argument pointer and soft frame pointer rtx's.
11001 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11002 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11003 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11005 dw_loc_descr_ref result = NULL;
11007 if (dwarf_version >= 4 || !dwarf_strict)
11009 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11010 initialized);
11011 if (result)
11012 add_loc_descr (&result,
11013 new_loc_descr (DW_OP_stack_value, 0, 0));
11015 return result;
11018 regs = targetm.dwarf_register_span (rtl);
11020 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11021 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11022 else
11024 unsigned int dbx_regnum = dbx_reg_number (rtl);
11025 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11026 return 0;
11027 return one_reg_loc_descriptor (dbx_regnum, initialized);
11031 /* Return a location descriptor that designates a machine register for
11032 a given hard register number. */
11034 static dw_loc_descr_ref
11035 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11037 dw_loc_descr_ref reg_loc_descr;
11039 if (regno <= 31)
11040 reg_loc_descr
11041 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11042 else
11043 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11045 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11046 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11048 return reg_loc_descr;
11051 /* Given an RTL of a register, return a location descriptor that
11052 designates a value that spans more than one register. */
11054 static dw_loc_descr_ref
11055 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11056 enum var_init_status initialized)
11058 int size, i;
11059 dw_loc_descr_ref loc_result = NULL;
11061 /* Simple, contiguous registers. */
11062 if (regs == NULL_RTX)
11064 unsigned reg = REGNO (rtl);
11065 int nregs;
11067 #ifdef LEAF_REG_REMAP
11068 if (crtl->uses_only_leaf_regs)
11070 int leaf_reg = LEAF_REG_REMAP (reg);
11071 if (leaf_reg != -1)
11072 reg = (unsigned) leaf_reg;
11074 #endif
11076 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11077 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11079 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11081 loc_result = NULL;
11082 while (nregs--)
11084 dw_loc_descr_ref t;
11086 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11087 VAR_INIT_STATUS_INITIALIZED);
11088 add_loc_descr (&loc_result, t);
11089 add_loc_descr_op_piece (&loc_result, size);
11090 ++reg;
11092 return loc_result;
11095 /* Now onto stupid register sets in non contiguous locations. */
11097 gcc_assert (GET_CODE (regs) == PARALLEL);
11099 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11100 loc_result = NULL;
11102 for (i = 0; i < XVECLEN (regs, 0); ++i)
11104 dw_loc_descr_ref t;
11106 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11107 VAR_INIT_STATUS_INITIALIZED);
11108 add_loc_descr (&loc_result, t);
11109 add_loc_descr_op_piece (&loc_result, size);
11112 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11113 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11114 return loc_result;
11117 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11119 /* Return a location descriptor that designates a constant i,
11120 as a compound operation from constant (i >> shift), constant shift
11121 and DW_OP_shl. */
11123 static dw_loc_descr_ref
11124 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11126 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11127 add_loc_descr (&ret, int_loc_descriptor (shift));
11128 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11129 return ret;
11132 /* Return a location descriptor that designates a constant. */
11134 static dw_loc_descr_ref
11135 int_loc_descriptor (HOST_WIDE_INT i)
11137 enum dwarf_location_atom op;
11139 /* Pick the smallest representation of a constant, rather than just
11140 defaulting to the LEB encoding. */
11141 if (i >= 0)
11143 int clz = clz_hwi (i);
11144 int ctz = ctz_hwi (i);
11145 if (i <= 31)
11146 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11147 else if (i <= 0xff)
11148 op = DW_OP_const1u;
11149 else if (i <= 0xffff)
11150 op = DW_OP_const2u;
11151 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11152 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11153 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11154 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11155 while DW_OP_const4u is 5 bytes. */
11156 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11157 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11158 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11159 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11160 while DW_OP_const4u is 5 bytes. */
11161 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11162 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11163 op = DW_OP_const4u;
11164 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11165 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11166 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11167 while DW_OP_constu of constant >= 0x100000000 takes at least
11168 6 bytes. */
11169 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11170 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11171 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11172 >= HOST_BITS_PER_WIDE_INT)
11173 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11174 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11175 while DW_OP_constu takes in this case at least 6 bytes. */
11176 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11177 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11178 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11179 && size_of_uleb128 (i) > 6)
11180 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11181 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11182 else
11183 op = DW_OP_constu;
11185 else
11187 if (i >= -0x80)
11188 op = DW_OP_const1s;
11189 else if (i >= -0x8000)
11190 op = DW_OP_const2s;
11191 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11193 if (size_of_int_loc_descriptor (i) < 5)
11195 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11196 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11197 return ret;
11199 op = DW_OP_const4s;
11201 else
11203 if (size_of_int_loc_descriptor (i)
11204 < (unsigned long) 1 + size_of_sleb128 (i))
11206 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11207 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11208 return ret;
11210 op = DW_OP_consts;
11214 return new_loc_descr (op, i, 0);
11217 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11218 without actually allocating it. */
11220 static unsigned long
11221 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11223 return size_of_int_loc_descriptor (i >> shift)
11224 + size_of_int_loc_descriptor (shift)
11225 + 1;
11228 /* Return size_of_locs (int_loc_descriptor (i)) without
11229 actually allocating it. */
11231 static unsigned long
11232 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11234 unsigned long s;
11236 if (i >= 0)
11238 int clz, ctz;
11239 if (i <= 31)
11240 return 1;
11241 else if (i <= 0xff)
11242 return 2;
11243 else if (i <= 0xffff)
11244 return 3;
11245 clz = clz_hwi (i);
11246 ctz = ctz_hwi (i);
11247 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11248 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11249 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11250 - clz - 5);
11251 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11252 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11253 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11254 - clz - 8);
11255 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11256 return 5;
11257 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11258 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11259 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11260 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11261 - clz - 8);
11262 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11263 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11264 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11265 - clz - 16);
11266 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11267 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11268 && s > 6)
11269 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11270 - clz - 32);
11271 else
11272 return 1 + s;
11274 else
11276 if (i >= -0x80)
11277 return 2;
11278 else if (i >= -0x8000)
11279 return 3;
11280 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11282 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11284 s = size_of_int_loc_descriptor (-i) + 1;
11285 if (s < 5)
11286 return s;
11288 return 5;
11290 else
11292 unsigned long r = 1 + size_of_sleb128 (i);
11293 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11295 s = size_of_int_loc_descriptor (-i) + 1;
11296 if (s < r)
11297 return s;
11299 return r;
11304 /* Return loc description representing "address" of integer value.
11305 This can appear only as toplevel expression. */
11307 static dw_loc_descr_ref
11308 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11310 int litsize;
11311 dw_loc_descr_ref loc_result = NULL;
11313 if (!(dwarf_version >= 4 || !dwarf_strict))
11314 return NULL;
11316 litsize = size_of_int_loc_descriptor (i);
11317 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11318 is more compact. For DW_OP_stack_value we need:
11319 litsize + 1 (DW_OP_stack_value)
11320 and for DW_OP_implicit_value:
11321 1 (DW_OP_implicit_value) + 1 (length) + size. */
11322 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11324 loc_result = int_loc_descriptor (i);
11325 add_loc_descr (&loc_result,
11326 new_loc_descr (DW_OP_stack_value, 0, 0));
11327 return loc_result;
11330 loc_result = new_loc_descr (DW_OP_implicit_value,
11331 size, 0);
11332 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11333 loc_result->dw_loc_oprnd2.v.val_int = i;
11334 return loc_result;
11337 /* Return a location descriptor that designates a base+offset location. */
11339 static dw_loc_descr_ref
11340 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11341 enum var_init_status initialized)
11343 unsigned int regno;
11344 dw_loc_descr_ref result;
11345 dw_fde_ref fde = cfun->fde;
11347 /* We only use "frame base" when we're sure we're talking about the
11348 post-prologue local stack frame. We do this by *not* running
11349 register elimination until this point, and recognizing the special
11350 argument pointer and soft frame pointer rtx's. */
11351 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11353 rtx elim = (ira_use_lra_p
11354 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11355 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11357 if (elim != reg)
11359 if (GET_CODE (elim) == PLUS)
11361 offset += INTVAL (XEXP (elim, 1));
11362 elim = XEXP (elim, 0);
11364 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11365 && (elim == hard_frame_pointer_rtx
11366 || elim == stack_pointer_rtx))
11367 || elim == (frame_pointer_needed
11368 ? hard_frame_pointer_rtx
11369 : stack_pointer_rtx));
11371 /* If drap register is used to align stack, use frame
11372 pointer + offset to access stack variables. If stack
11373 is aligned without drap, use stack pointer + offset to
11374 access stack variables. */
11375 if (crtl->stack_realign_tried
11376 && reg == frame_pointer_rtx)
11378 int base_reg
11379 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11380 ? HARD_FRAME_POINTER_REGNUM
11381 : REGNO (elim));
11382 return new_reg_loc_descr (base_reg, offset);
11385 gcc_assert (frame_pointer_fb_offset_valid);
11386 offset += frame_pointer_fb_offset;
11387 return new_loc_descr (DW_OP_fbreg, offset, 0);
11391 regno = REGNO (reg);
11392 #ifdef LEAF_REG_REMAP
11393 if (crtl->uses_only_leaf_regs)
11395 int leaf_reg = LEAF_REG_REMAP (regno);
11396 if (leaf_reg != -1)
11397 regno = (unsigned) leaf_reg;
11399 #endif
11400 regno = DWARF_FRAME_REGNUM (regno);
11402 if (!optimize && fde
11403 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11405 /* Use cfa+offset to represent the location of arguments passed
11406 on the stack when drap is used to align stack.
11407 Only do this when not optimizing, for optimized code var-tracking
11408 is supposed to track where the arguments live and the register
11409 used as vdrap or drap in some spot might be used for something
11410 else in other part of the routine. */
11411 return new_loc_descr (DW_OP_fbreg, offset, 0);
11414 if (regno <= 31)
11415 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11416 offset, 0);
11417 else
11418 result = new_loc_descr (DW_OP_bregx, regno, offset);
11420 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11421 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11423 return result;
11426 /* Return true if this RTL expression describes a base+offset calculation. */
11428 static inline int
11429 is_based_loc (const_rtx rtl)
11431 return (GET_CODE (rtl) == PLUS
11432 && ((REG_P (XEXP (rtl, 0))
11433 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11434 && CONST_INT_P (XEXP (rtl, 1)))));
11437 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11438 failed. */
11440 static dw_loc_descr_ref
11441 tls_mem_loc_descriptor (rtx mem)
11443 tree base;
11444 dw_loc_descr_ref loc_result;
11446 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11447 return NULL;
11449 base = get_base_address (MEM_EXPR (mem));
11450 if (base == NULL
11451 || TREE_CODE (base) != VAR_DECL
11452 || !DECL_THREAD_LOCAL_P (base))
11453 return NULL;
11455 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11456 if (loc_result == NULL)
11457 return NULL;
11459 if (MEM_OFFSET (mem))
11460 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11462 return loc_result;
11465 /* Output debug info about reason why we failed to expand expression as dwarf
11466 expression. */
11468 static void
11469 expansion_failed (tree expr, rtx rtl, char const *reason)
11471 if (dump_file && (dump_flags & TDF_DETAILS))
11473 fprintf (dump_file, "Failed to expand as dwarf: ");
11474 if (expr)
11475 print_generic_expr (dump_file, expr, dump_flags);
11476 if (rtl)
11478 fprintf (dump_file, "\n");
11479 print_rtl (dump_file, rtl);
11481 fprintf (dump_file, "\nReason: %s\n", reason);
11485 /* Helper function for const_ok_for_output. */
11487 static bool
11488 const_ok_for_output_1 (rtx rtl)
11490 if (GET_CODE (rtl) == UNSPEC)
11492 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11493 we can't express it in the debug info. */
11494 #ifdef ENABLE_CHECKING
11495 /* Don't complain about TLS UNSPECs, those are just too hard to
11496 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11497 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11498 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11499 if (XVECLEN (rtl, 0) == 0
11500 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11501 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11502 inform (current_function_decl
11503 ? DECL_SOURCE_LOCATION (current_function_decl)
11504 : UNKNOWN_LOCATION,
11505 #if NUM_UNSPEC_VALUES > 0
11506 "non-delegitimized UNSPEC %s (%d) found in variable location",
11507 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11508 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11509 XINT (rtl, 1));
11510 #else
11511 "non-delegitimized UNSPEC %d found in variable location",
11512 XINT (rtl, 1));
11513 #endif
11514 #endif
11515 expansion_failed (NULL_TREE, rtl,
11516 "UNSPEC hasn't been delegitimized.\n");
11517 return false;
11520 if (targetm.const_not_ok_for_debug_p (rtl))
11522 expansion_failed (NULL_TREE, rtl,
11523 "Expression rejected for debug by the backend.\n");
11524 return false;
11527 /* FIXME: Refer to PR60655. It is possible for simplification
11528 of rtl expressions in var tracking to produce such expressions.
11529 We should really identify / validate expressions
11530 enclosed in CONST that can be handled by assemblers on various
11531 targets and only handle legitimate cases here. */
11532 if (GET_CODE (rtl) != SYMBOL_REF)
11534 if (GET_CODE (rtl) == NOT)
11535 return false;
11536 return true;
11539 if (CONSTANT_POOL_ADDRESS_P (rtl))
11541 bool marked;
11542 get_pool_constant_mark (rtl, &marked);
11543 /* If all references to this pool constant were optimized away,
11544 it was not output and thus we can't represent it. */
11545 if (!marked)
11547 expansion_failed (NULL_TREE, rtl,
11548 "Constant was removed from constant pool.\n");
11549 return false;
11553 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11554 return false;
11556 /* Avoid references to external symbols in debug info, on several targets
11557 the linker might even refuse to link when linking a shared library,
11558 and in many other cases the relocations for .debug_info/.debug_loc are
11559 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11560 to be defined within the same shared library or executable are fine. */
11561 if (SYMBOL_REF_EXTERNAL_P (rtl))
11563 tree decl = SYMBOL_REF_DECL (rtl);
11565 if (decl == NULL || !targetm.binds_local_p (decl))
11567 expansion_failed (NULL_TREE, rtl,
11568 "Symbol not defined in current TU.\n");
11569 return false;
11573 return true;
11576 /* Return true if constant RTL can be emitted in DW_OP_addr or
11577 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11578 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11580 static bool
11581 const_ok_for_output (rtx rtl)
11583 if (GET_CODE (rtl) == SYMBOL_REF)
11584 return const_ok_for_output_1 (rtl);
11586 if (GET_CODE (rtl) == CONST)
11588 subrtx_var_iterator::array_type array;
11589 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11590 if (!const_ok_for_output_1 (*iter))
11591 return false;
11592 return true;
11595 return true;
11598 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11599 if possible, NULL otherwise. */
11601 static dw_die_ref
11602 base_type_for_mode (machine_mode mode, bool unsignedp)
11604 dw_die_ref type_die;
11605 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11607 if (type == NULL)
11608 return NULL;
11609 switch (TREE_CODE (type))
11611 case INTEGER_TYPE:
11612 case REAL_TYPE:
11613 break;
11614 default:
11615 return NULL;
11617 type_die = lookup_type_die (type);
11618 if (!type_die)
11619 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11620 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11621 return NULL;
11622 return type_die;
11625 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11626 type matching MODE, or, if MODE is narrower than or as wide as
11627 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11628 possible. */
11630 static dw_loc_descr_ref
11631 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11633 machine_mode outer_mode = mode;
11634 dw_die_ref type_die;
11635 dw_loc_descr_ref cvt;
11637 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11639 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11640 return op;
11642 type_die = base_type_for_mode (outer_mode, 1);
11643 if (type_die == NULL)
11644 return NULL;
11645 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11646 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11647 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11648 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11649 add_loc_descr (&op, cvt);
11650 return op;
11653 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11655 static dw_loc_descr_ref
11656 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11657 dw_loc_descr_ref op1)
11659 dw_loc_descr_ref ret = op0;
11660 add_loc_descr (&ret, op1);
11661 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11662 if (STORE_FLAG_VALUE != 1)
11664 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11665 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11667 return ret;
11670 /* Return location descriptor for signed comparison OP RTL. */
11672 static dw_loc_descr_ref
11673 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11674 machine_mode mem_mode)
11676 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11677 dw_loc_descr_ref op0, op1;
11678 int shift;
11680 if (op_mode == VOIDmode)
11681 op_mode = GET_MODE (XEXP (rtl, 1));
11682 if (op_mode == VOIDmode)
11683 return NULL;
11685 if (dwarf_strict
11686 && (GET_MODE_CLASS (op_mode) != MODE_INT
11687 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11688 return NULL;
11690 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11691 VAR_INIT_STATUS_INITIALIZED);
11692 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11693 VAR_INIT_STATUS_INITIALIZED);
11695 if (op0 == NULL || op1 == NULL)
11696 return NULL;
11698 if (GET_MODE_CLASS (op_mode) != MODE_INT
11699 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11700 return compare_loc_descriptor (op, op0, op1);
11702 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11704 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11705 dw_loc_descr_ref cvt;
11707 if (type_die == NULL)
11708 return NULL;
11709 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11710 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11711 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11712 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11713 add_loc_descr (&op0, cvt);
11714 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11715 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11716 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11717 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11718 add_loc_descr (&op1, cvt);
11719 return compare_loc_descriptor (op, op0, op1);
11722 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11723 /* For eq/ne, if the operands are known to be zero-extended,
11724 there is no need to do the fancy shifting up. */
11725 if (op == DW_OP_eq || op == DW_OP_ne)
11727 dw_loc_descr_ref last0, last1;
11728 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11730 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11732 /* deref_size zero extends, and for constants we can check
11733 whether they are zero extended or not. */
11734 if (((last0->dw_loc_opc == DW_OP_deref_size
11735 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11736 || (CONST_INT_P (XEXP (rtl, 0))
11737 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11738 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11739 && ((last1->dw_loc_opc == DW_OP_deref_size
11740 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11741 || (CONST_INT_P (XEXP (rtl, 1))
11742 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11743 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11744 return compare_loc_descriptor (op, op0, op1);
11746 /* EQ/NE comparison against constant in narrower type than
11747 DWARF2_ADDR_SIZE can be performed either as
11748 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11749 DW_OP_{eq,ne}
11751 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11752 DW_OP_{eq,ne}. Pick whatever is shorter. */
11753 if (CONST_INT_P (XEXP (rtl, 1))
11754 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11755 && (size_of_int_loc_descriptor (shift) + 1
11756 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11757 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11758 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11759 & GET_MODE_MASK (op_mode))))
11761 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11762 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11763 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11764 & GET_MODE_MASK (op_mode));
11765 return compare_loc_descriptor (op, op0, op1);
11768 add_loc_descr (&op0, int_loc_descriptor (shift));
11769 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11770 if (CONST_INT_P (XEXP (rtl, 1)))
11771 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11772 else
11774 add_loc_descr (&op1, int_loc_descriptor (shift));
11775 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11777 return compare_loc_descriptor (op, op0, op1);
11780 /* Return location descriptor for unsigned comparison OP RTL. */
11782 static dw_loc_descr_ref
11783 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11784 machine_mode mem_mode)
11786 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11787 dw_loc_descr_ref op0, op1;
11789 if (op_mode == VOIDmode)
11790 op_mode = GET_MODE (XEXP (rtl, 1));
11791 if (op_mode == VOIDmode)
11792 return NULL;
11793 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11794 return NULL;
11796 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11797 return NULL;
11799 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11800 VAR_INIT_STATUS_INITIALIZED);
11801 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11802 VAR_INIT_STATUS_INITIALIZED);
11804 if (op0 == NULL || op1 == NULL)
11805 return NULL;
11807 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11809 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11810 dw_loc_descr_ref last0, last1;
11811 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11813 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11815 if (CONST_INT_P (XEXP (rtl, 0)))
11816 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11817 /* deref_size zero extends, so no need to mask it again. */
11818 else if (last0->dw_loc_opc != DW_OP_deref_size
11819 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11821 add_loc_descr (&op0, int_loc_descriptor (mask));
11822 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11824 if (CONST_INT_P (XEXP (rtl, 1)))
11825 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11826 /* deref_size zero extends, so no need to mask it again. */
11827 else if (last1->dw_loc_opc != DW_OP_deref_size
11828 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11830 add_loc_descr (&op1, int_loc_descriptor (mask));
11831 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11834 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11836 HOST_WIDE_INT bias = 1;
11837 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11838 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11839 if (CONST_INT_P (XEXP (rtl, 1)))
11840 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11841 + INTVAL (XEXP (rtl, 1)));
11842 else
11843 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11844 bias, 0));
11846 return compare_loc_descriptor (op, op0, op1);
11849 /* Return location descriptor for {U,S}{MIN,MAX}. */
11851 static dw_loc_descr_ref
11852 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11853 machine_mode mem_mode)
11855 enum dwarf_location_atom op;
11856 dw_loc_descr_ref op0, op1, ret;
11857 dw_loc_descr_ref bra_node, drop_node;
11859 if (dwarf_strict
11860 && (GET_MODE_CLASS (mode) != MODE_INT
11861 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11862 return NULL;
11864 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11865 VAR_INIT_STATUS_INITIALIZED);
11866 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11867 VAR_INIT_STATUS_INITIALIZED);
11869 if (op0 == NULL || op1 == NULL)
11870 return NULL;
11872 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11873 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11874 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11875 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11877 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11879 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11880 add_loc_descr (&op0, int_loc_descriptor (mask));
11881 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11882 add_loc_descr (&op1, int_loc_descriptor (mask));
11883 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11885 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11887 HOST_WIDE_INT bias = 1;
11888 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11889 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11890 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11893 else if (GET_MODE_CLASS (mode) == MODE_INT
11894 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11896 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11897 add_loc_descr (&op0, int_loc_descriptor (shift));
11898 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11899 add_loc_descr (&op1, int_loc_descriptor (shift));
11900 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11902 else if (GET_MODE_CLASS (mode) == MODE_INT
11903 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11905 dw_die_ref type_die = base_type_for_mode (mode, 0);
11906 dw_loc_descr_ref cvt;
11907 if (type_die == NULL)
11908 return NULL;
11909 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11910 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11911 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11912 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11913 add_loc_descr (&op0, cvt);
11914 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11915 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11916 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11917 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11918 add_loc_descr (&op1, cvt);
11921 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11922 op = DW_OP_lt;
11923 else
11924 op = DW_OP_gt;
11925 ret = op0;
11926 add_loc_descr (&ret, op1);
11927 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11928 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11929 add_loc_descr (&ret, bra_node);
11930 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11931 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11932 add_loc_descr (&ret, drop_node);
11933 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11934 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11935 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11936 && GET_MODE_CLASS (mode) == MODE_INT
11937 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11938 ret = convert_descriptor_to_mode (mode, ret);
11939 return ret;
11942 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11943 but after converting arguments to type_die, afterwards
11944 convert back to unsigned. */
11946 static dw_loc_descr_ref
11947 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11948 machine_mode mode, machine_mode mem_mode)
11950 dw_loc_descr_ref cvt, op0, op1;
11952 if (type_die == NULL)
11953 return NULL;
11954 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11955 VAR_INIT_STATUS_INITIALIZED);
11956 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11957 VAR_INIT_STATUS_INITIALIZED);
11958 if (op0 == NULL || op1 == NULL)
11959 return NULL;
11960 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11961 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11962 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11963 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11964 add_loc_descr (&op0, cvt);
11965 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11966 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11967 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11968 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11969 add_loc_descr (&op1, cvt);
11970 add_loc_descr (&op0, op1);
11971 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11972 return convert_descriptor_to_mode (mode, op0);
11975 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11976 const0 is DW_OP_lit0 or corresponding typed constant,
11977 const1 is DW_OP_lit1 or corresponding typed constant
11978 and constMSB is constant with just the MSB bit set
11979 for the mode):
11980 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11981 L1: const0 DW_OP_swap
11982 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11983 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11984 L3: DW_OP_drop
11985 L4: DW_OP_nop
11987 CTZ is similar:
11988 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11989 L1: const0 DW_OP_swap
11990 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11991 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11992 L3: DW_OP_drop
11993 L4: DW_OP_nop
11995 FFS is similar:
11996 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11997 L1: const1 DW_OP_swap
11998 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11999 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12000 L3: DW_OP_drop
12001 L4: DW_OP_nop */
12003 static dw_loc_descr_ref
12004 clz_loc_descriptor (rtx rtl, machine_mode mode,
12005 machine_mode mem_mode)
12007 dw_loc_descr_ref op0, ret, tmp;
12008 HOST_WIDE_INT valv;
12009 dw_loc_descr_ref l1jump, l1label;
12010 dw_loc_descr_ref l2jump, l2label;
12011 dw_loc_descr_ref l3jump, l3label;
12012 dw_loc_descr_ref l4jump, l4label;
12013 rtx msb;
12015 if (GET_MODE_CLASS (mode) != MODE_INT
12016 || GET_MODE (XEXP (rtl, 0)) != mode)
12017 return NULL;
12019 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12020 VAR_INIT_STATUS_INITIALIZED);
12021 if (op0 == NULL)
12022 return NULL;
12023 ret = op0;
12024 if (GET_CODE (rtl) == CLZ)
12026 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12027 valv = GET_MODE_BITSIZE (mode);
12029 else if (GET_CODE (rtl) == FFS)
12030 valv = 0;
12031 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12032 valv = GET_MODE_BITSIZE (mode);
12033 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12034 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12035 add_loc_descr (&ret, l1jump);
12036 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12037 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12038 VAR_INIT_STATUS_INITIALIZED);
12039 if (tmp == NULL)
12040 return NULL;
12041 add_loc_descr (&ret, tmp);
12042 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12043 add_loc_descr (&ret, l4jump);
12044 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12045 ? const1_rtx : const0_rtx,
12046 mode, mem_mode,
12047 VAR_INIT_STATUS_INITIALIZED);
12048 if (l1label == NULL)
12049 return NULL;
12050 add_loc_descr (&ret, l1label);
12051 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12052 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12053 add_loc_descr (&ret, l2label);
12054 if (GET_CODE (rtl) != CLZ)
12055 msb = const1_rtx;
12056 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12057 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12058 << (GET_MODE_BITSIZE (mode) - 1));
12059 else
12060 msb = immed_wide_int_const
12061 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12062 GET_MODE_PRECISION (mode)), mode);
12063 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12064 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12065 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12066 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12067 else
12068 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12069 VAR_INIT_STATUS_INITIALIZED);
12070 if (tmp == NULL)
12071 return NULL;
12072 add_loc_descr (&ret, tmp);
12073 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12074 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12075 add_loc_descr (&ret, l3jump);
12076 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12077 VAR_INIT_STATUS_INITIALIZED);
12078 if (tmp == NULL)
12079 return NULL;
12080 add_loc_descr (&ret, tmp);
12081 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12082 ? DW_OP_shl : DW_OP_shr, 0, 0));
12083 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12084 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12085 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12086 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12087 add_loc_descr (&ret, l2jump);
12088 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12089 add_loc_descr (&ret, l3label);
12090 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12091 add_loc_descr (&ret, l4label);
12092 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12093 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12094 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12095 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12096 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12097 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12098 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12099 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12100 return ret;
12103 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12104 const1 is DW_OP_lit1 or corresponding typed constant):
12105 const0 DW_OP_swap
12106 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12107 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12108 L2: DW_OP_drop
12110 PARITY is similar:
12111 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12112 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12113 L2: DW_OP_drop */
12115 static dw_loc_descr_ref
12116 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12117 machine_mode mem_mode)
12119 dw_loc_descr_ref op0, ret, tmp;
12120 dw_loc_descr_ref l1jump, l1label;
12121 dw_loc_descr_ref l2jump, l2label;
12123 if (GET_MODE_CLASS (mode) != MODE_INT
12124 || GET_MODE (XEXP (rtl, 0)) != mode)
12125 return NULL;
12127 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12128 VAR_INIT_STATUS_INITIALIZED);
12129 if (op0 == NULL)
12130 return NULL;
12131 ret = op0;
12132 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12133 VAR_INIT_STATUS_INITIALIZED);
12134 if (tmp == NULL)
12135 return NULL;
12136 add_loc_descr (&ret, tmp);
12137 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12138 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12139 add_loc_descr (&ret, l1label);
12140 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12141 add_loc_descr (&ret, l2jump);
12142 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12143 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12144 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12145 VAR_INIT_STATUS_INITIALIZED);
12146 if (tmp == NULL)
12147 return NULL;
12148 add_loc_descr (&ret, tmp);
12149 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12150 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12151 ? DW_OP_plus : DW_OP_xor, 0, 0));
12152 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12153 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12154 VAR_INIT_STATUS_INITIALIZED);
12155 add_loc_descr (&ret, tmp);
12156 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12157 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12158 add_loc_descr (&ret, l1jump);
12159 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12160 add_loc_descr (&ret, l2label);
12161 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12162 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12163 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12164 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12165 return ret;
12168 /* BSWAP (constS is initial shift count, either 56 or 24):
12169 constS const0
12170 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12171 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12172 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12173 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12174 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12176 static dw_loc_descr_ref
12177 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12178 machine_mode mem_mode)
12180 dw_loc_descr_ref op0, ret, tmp;
12181 dw_loc_descr_ref l1jump, l1label;
12182 dw_loc_descr_ref l2jump, l2label;
12184 if (GET_MODE_CLASS (mode) != MODE_INT
12185 || BITS_PER_UNIT != 8
12186 || (GET_MODE_BITSIZE (mode) != 32
12187 && GET_MODE_BITSIZE (mode) != 64))
12188 return NULL;
12190 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12191 VAR_INIT_STATUS_INITIALIZED);
12192 if (op0 == NULL)
12193 return NULL;
12195 ret = op0;
12196 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12197 mode, mem_mode,
12198 VAR_INIT_STATUS_INITIALIZED);
12199 if (tmp == NULL)
12200 return NULL;
12201 add_loc_descr (&ret, tmp);
12202 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12203 VAR_INIT_STATUS_INITIALIZED);
12204 if (tmp == NULL)
12205 return NULL;
12206 add_loc_descr (&ret, tmp);
12207 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12208 add_loc_descr (&ret, l1label);
12209 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12210 mode, mem_mode,
12211 VAR_INIT_STATUS_INITIALIZED);
12212 add_loc_descr (&ret, tmp);
12213 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12214 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12215 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12216 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12217 VAR_INIT_STATUS_INITIALIZED);
12218 if (tmp == NULL)
12219 return NULL;
12220 add_loc_descr (&ret, tmp);
12221 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12222 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12223 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12224 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12225 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12226 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12227 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12228 VAR_INIT_STATUS_INITIALIZED);
12229 add_loc_descr (&ret, tmp);
12230 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12231 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12232 add_loc_descr (&ret, l2jump);
12233 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12234 VAR_INIT_STATUS_INITIALIZED);
12235 add_loc_descr (&ret, tmp);
12236 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12237 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12238 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12239 add_loc_descr (&ret, l1jump);
12240 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12241 add_loc_descr (&ret, l2label);
12242 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12243 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12244 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12245 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12246 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12247 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12248 return ret;
12251 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12252 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12253 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12254 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12256 ROTATERT is similar:
12257 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12258 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12259 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12261 static dw_loc_descr_ref
12262 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12263 machine_mode mem_mode)
12265 rtx rtlop1 = XEXP (rtl, 1);
12266 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12267 int i;
12269 if (GET_MODE_CLASS (mode) != MODE_INT)
12270 return NULL;
12272 if (GET_MODE (rtlop1) != VOIDmode
12273 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12274 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12275 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12276 VAR_INIT_STATUS_INITIALIZED);
12277 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12278 VAR_INIT_STATUS_INITIALIZED);
12279 if (op0 == NULL || op1 == NULL)
12280 return NULL;
12281 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12282 for (i = 0; i < 2; i++)
12284 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12285 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12286 mode, mem_mode,
12287 VAR_INIT_STATUS_INITIALIZED);
12288 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12289 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12290 ? DW_OP_const4u
12291 : HOST_BITS_PER_WIDE_INT == 64
12292 ? DW_OP_const8u : DW_OP_constu,
12293 GET_MODE_MASK (mode), 0);
12294 else
12295 mask[i] = NULL;
12296 if (mask[i] == NULL)
12297 return NULL;
12298 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12300 ret = op0;
12301 add_loc_descr (&ret, op1);
12302 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12303 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12304 if (GET_CODE (rtl) == ROTATERT)
12306 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12307 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12308 GET_MODE_BITSIZE (mode), 0));
12310 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12311 if (mask[0] != NULL)
12312 add_loc_descr (&ret, mask[0]);
12313 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12314 if (mask[1] != NULL)
12316 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12317 add_loc_descr (&ret, mask[1]);
12318 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12320 if (GET_CODE (rtl) == ROTATE)
12322 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12323 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12324 GET_MODE_BITSIZE (mode), 0));
12326 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12327 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12328 return ret;
12331 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12332 for DEBUG_PARAMETER_REF RTL. */
12334 static dw_loc_descr_ref
12335 parameter_ref_descriptor (rtx rtl)
12337 dw_loc_descr_ref ret;
12338 dw_die_ref ref;
12340 if (dwarf_strict)
12341 return NULL;
12342 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12343 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12344 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12345 if (ref)
12347 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12348 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12349 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12351 else
12353 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12354 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12356 return ret;
12359 /* The following routine converts the RTL for a variable or parameter
12360 (resident in memory) into an equivalent Dwarf representation of a
12361 mechanism for getting the address of that same variable onto the top of a
12362 hypothetical "address evaluation" stack.
12364 When creating memory location descriptors, we are effectively transforming
12365 the RTL for a memory-resident object into its Dwarf postfix expression
12366 equivalent. This routine recursively descends an RTL tree, turning
12367 it into Dwarf postfix code as it goes.
12369 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12371 MEM_MODE is the mode of the memory reference, needed to handle some
12372 autoincrement addressing modes.
12374 Return 0 if we can't represent the location. */
12376 dw_loc_descr_ref
12377 mem_loc_descriptor (rtx rtl, machine_mode mode,
12378 machine_mode mem_mode,
12379 enum var_init_status initialized)
12381 dw_loc_descr_ref mem_loc_result = NULL;
12382 enum dwarf_location_atom op;
12383 dw_loc_descr_ref op0, op1;
12384 rtx inner = NULL_RTX;
12386 if (mode == VOIDmode)
12387 mode = GET_MODE (rtl);
12389 /* Note that for a dynamically sized array, the location we will generate a
12390 description of here will be the lowest numbered location which is
12391 actually within the array. That's *not* necessarily the same as the
12392 zeroth element of the array. */
12394 rtl = targetm.delegitimize_address (rtl);
12396 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12397 return NULL;
12399 switch (GET_CODE (rtl))
12401 case POST_INC:
12402 case POST_DEC:
12403 case POST_MODIFY:
12404 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12406 case SUBREG:
12407 /* The case of a subreg may arise when we have a local (register)
12408 variable or a formal (register) parameter which doesn't quite fill
12409 up an entire register. For now, just assume that it is
12410 legitimate to make the Dwarf info refer to the whole register which
12411 contains the given subreg. */
12412 if (!subreg_lowpart_p (rtl))
12413 break;
12414 inner = SUBREG_REG (rtl);
12415 case TRUNCATE:
12416 if (inner == NULL_RTX)
12417 inner = XEXP (rtl, 0);
12418 if (GET_MODE_CLASS (mode) == MODE_INT
12419 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12420 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12421 #ifdef POINTERS_EXTEND_UNSIGNED
12422 || (mode == Pmode && mem_mode != VOIDmode)
12423 #endif
12425 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12427 mem_loc_result = mem_loc_descriptor (inner,
12428 GET_MODE (inner),
12429 mem_mode, initialized);
12430 break;
12432 if (dwarf_strict)
12433 break;
12434 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12435 break;
12436 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12437 && (GET_MODE_CLASS (mode) != MODE_INT
12438 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12439 break;
12440 else
12442 dw_die_ref type_die;
12443 dw_loc_descr_ref cvt;
12445 mem_loc_result = mem_loc_descriptor (inner,
12446 GET_MODE (inner),
12447 mem_mode, initialized);
12448 if (mem_loc_result == NULL)
12449 break;
12450 type_die = base_type_for_mode (mode,
12451 GET_MODE_CLASS (mode) == MODE_INT);
12452 if (type_die == NULL)
12454 mem_loc_result = NULL;
12455 break;
12457 if (GET_MODE_SIZE (mode)
12458 != GET_MODE_SIZE (GET_MODE (inner)))
12459 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12460 else
12461 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12462 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12463 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12464 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12465 add_loc_descr (&mem_loc_result, cvt);
12467 break;
12469 case REG:
12470 if (GET_MODE_CLASS (mode) != MODE_INT
12471 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12472 && rtl != arg_pointer_rtx
12473 && rtl != frame_pointer_rtx
12474 #ifdef POINTERS_EXTEND_UNSIGNED
12475 && (mode != Pmode || mem_mode == VOIDmode)
12476 #endif
12479 dw_die_ref type_die;
12480 unsigned int dbx_regnum;
12482 if (dwarf_strict)
12483 break;
12484 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12485 break;
12486 type_die = base_type_for_mode (mode,
12487 GET_MODE_CLASS (mode) == MODE_INT);
12488 if (type_die == NULL)
12489 break;
12491 dbx_regnum = dbx_reg_number (rtl);
12492 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12493 break;
12494 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12495 dbx_regnum, 0);
12496 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12497 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12498 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12499 break;
12501 /* Whenever a register number forms a part of the description of the
12502 method for calculating the (dynamic) address of a memory resident
12503 object, DWARF rules require the register number be referred to as
12504 a "base register". This distinction is not based in any way upon
12505 what category of register the hardware believes the given register
12506 belongs to. This is strictly DWARF terminology we're dealing with
12507 here. Note that in cases where the location of a memory-resident
12508 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12509 OP_CONST (0)) the actual DWARF location descriptor that we generate
12510 may just be OP_BASEREG (basereg). This may look deceptively like
12511 the object in question was allocated to a register (rather than in
12512 memory) so DWARF consumers need to be aware of the subtle
12513 distinction between OP_REG and OP_BASEREG. */
12514 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12515 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12516 else if (stack_realign_drap
12517 && crtl->drap_reg
12518 && crtl->args.internal_arg_pointer == rtl
12519 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12521 /* If RTL is internal_arg_pointer, which has been optimized
12522 out, use DRAP instead. */
12523 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12524 VAR_INIT_STATUS_INITIALIZED);
12526 break;
12528 case SIGN_EXTEND:
12529 case ZERO_EXTEND:
12530 if (GET_MODE_CLASS (mode) != MODE_INT)
12531 break;
12532 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12533 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12534 if (op0 == 0)
12535 break;
12536 else if (GET_CODE (rtl) == ZERO_EXTEND
12537 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12538 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12539 < HOST_BITS_PER_WIDE_INT
12540 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12541 to expand zero extend as two shifts instead of
12542 masking. */
12543 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12545 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12546 mem_loc_result = op0;
12547 add_loc_descr (&mem_loc_result,
12548 int_loc_descriptor (GET_MODE_MASK (imode)));
12549 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12551 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12553 int shift = DWARF2_ADDR_SIZE
12554 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12555 shift *= BITS_PER_UNIT;
12556 if (GET_CODE (rtl) == SIGN_EXTEND)
12557 op = DW_OP_shra;
12558 else
12559 op = DW_OP_shr;
12560 mem_loc_result = op0;
12561 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12562 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12563 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12564 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12566 else if (!dwarf_strict)
12568 dw_die_ref type_die1, type_die2;
12569 dw_loc_descr_ref cvt;
12571 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12572 GET_CODE (rtl) == ZERO_EXTEND);
12573 if (type_die1 == NULL)
12574 break;
12575 type_die2 = base_type_for_mode (mode, 1);
12576 if (type_die2 == NULL)
12577 break;
12578 mem_loc_result = op0;
12579 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12580 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12581 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12582 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12583 add_loc_descr (&mem_loc_result, cvt);
12584 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12585 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12586 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12587 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12588 add_loc_descr (&mem_loc_result, cvt);
12590 break;
12592 case MEM:
12594 rtx new_rtl = avoid_constant_pool_reference (rtl);
12595 if (new_rtl != rtl)
12597 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12598 initialized);
12599 if (mem_loc_result != NULL)
12600 return mem_loc_result;
12603 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12604 get_address_mode (rtl), mode,
12605 VAR_INIT_STATUS_INITIALIZED);
12606 if (mem_loc_result == NULL)
12607 mem_loc_result = tls_mem_loc_descriptor (rtl);
12608 if (mem_loc_result != NULL)
12610 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12611 || GET_MODE_CLASS (mode) != MODE_INT)
12613 dw_die_ref type_die;
12614 dw_loc_descr_ref deref;
12616 if (dwarf_strict)
12617 return NULL;
12618 type_die
12619 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12620 if (type_die == NULL)
12621 return NULL;
12622 deref = new_loc_descr (DW_OP_GNU_deref_type,
12623 GET_MODE_SIZE (mode), 0);
12624 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12625 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12626 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12627 add_loc_descr (&mem_loc_result, deref);
12629 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12630 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12631 else
12632 add_loc_descr (&mem_loc_result,
12633 new_loc_descr (DW_OP_deref_size,
12634 GET_MODE_SIZE (mode), 0));
12636 break;
12638 case LO_SUM:
12639 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12641 case LABEL_REF:
12642 /* Some ports can transform a symbol ref into a label ref, because
12643 the symbol ref is too far away and has to be dumped into a constant
12644 pool. */
12645 case CONST:
12646 case SYMBOL_REF:
12647 if ((GET_MODE_CLASS (mode) != MODE_INT
12648 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12649 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12650 #ifdef POINTERS_EXTEND_UNSIGNED
12651 && (mode != Pmode || mem_mode == VOIDmode)
12652 #endif
12654 break;
12655 if (GET_CODE (rtl) == SYMBOL_REF
12656 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12658 dw_loc_descr_ref temp;
12660 /* If this is not defined, we have no way to emit the data. */
12661 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12662 break;
12664 temp = new_addr_loc_descr (rtl, dtprel_true);
12666 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12667 add_loc_descr (&mem_loc_result, temp);
12669 break;
12672 if (!const_ok_for_output (rtl))
12673 break;
12675 symref:
12676 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12677 vec_safe_push (used_rtx_array, rtl);
12678 break;
12680 case CONCAT:
12681 case CONCATN:
12682 case VAR_LOCATION:
12683 case DEBUG_IMPLICIT_PTR:
12684 expansion_failed (NULL_TREE, rtl,
12685 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12686 return 0;
12688 case ENTRY_VALUE:
12689 if (dwarf_strict)
12690 return NULL;
12691 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12693 if (GET_MODE_CLASS (mode) != MODE_INT
12694 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12695 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12696 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12697 else
12699 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12700 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12701 return NULL;
12702 op0 = one_reg_loc_descriptor (dbx_regnum,
12703 VAR_INIT_STATUS_INITIALIZED);
12706 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12707 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12709 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12710 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12711 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12712 return NULL;
12714 else
12715 gcc_unreachable ();
12716 if (op0 == NULL)
12717 return NULL;
12718 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12719 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12720 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12721 break;
12723 case DEBUG_PARAMETER_REF:
12724 mem_loc_result = parameter_ref_descriptor (rtl);
12725 break;
12727 case PRE_MODIFY:
12728 /* Extract the PLUS expression nested inside and fall into
12729 PLUS code below. */
12730 rtl = XEXP (rtl, 1);
12731 goto plus;
12733 case PRE_INC:
12734 case PRE_DEC:
12735 /* Turn these into a PLUS expression and fall into the PLUS code
12736 below. */
12737 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12738 gen_int_mode (GET_CODE (rtl) == PRE_INC
12739 ? GET_MODE_UNIT_SIZE (mem_mode)
12740 : -GET_MODE_UNIT_SIZE (mem_mode),
12741 mode));
12743 /* ... fall through ... */
12745 case PLUS:
12746 plus:
12747 if (is_based_loc (rtl)
12748 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12749 || XEXP (rtl, 0) == arg_pointer_rtx
12750 || XEXP (rtl, 0) == frame_pointer_rtx)
12751 && GET_MODE_CLASS (mode) == MODE_INT)
12752 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12753 INTVAL (XEXP (rtl, 1)),
12754 VAR_INIT_STATUS_INITIALIZED);
12755 else
12757 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12758 VAR_INIT_STATUS_INITIALIZED);
12759 if (mem_loc_result == 0)
12760 break;
12762 if (CONST_INT_P (XEXP (rtl, 1))
12763 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12764 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12765 else
12767 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12768 VAR_INIT_STATUS_INITIALIZED);
12769 if (op1 == 0)
12770 return NULL;
12771 add_loc_descr (&mem_loc_result, op1);
12772 add_loc_descr (&mem_loc_result,
12773 new_loc_descr (DW_OP_plus, 0, 0));
12776 break;
12778 /* If a pseudo-reg is optimized away, it is possible for it to
12779 be replaced with a MEM containing a multiply or shift. */
12780 case MINUS:
12781 op = DW_OP_minus;
12782 goto do_binop;
12784 case MULT:
12785 op = DW_OP_mul;
12786 goto do_binop;
12788 case DIV:
12789 if (!dwarf_strict
12790 && GET_MODE_CLASS (mode) == MODE_INT
12791 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12793 mem_loc_result = typed_binop (DW_OP_div, rtl,
12794 base_type_for_mode (mode, 0),
12795 mode, mem_mode);
12796 break;
12798 op = DW_OP_div;
12799 goto do_binop;
12801 case UMOD:
12802 op = DW_OP_mod;
12803 goto do_binop;
12805 case ASHIFT:
12806 op = DW_OP_shl;
12807 goto do_shift;
12809 case ASHIFTRT:
12810 op = DW_OP_shra;
12811 goto do_shift;
12813 case LSHIFTRT:
12814 op = DW_OP_shr;
12815 goto do_shift;
12817 do_shift:
12818 if (GET_MODE_CLASS (mode) != MODE_INT)
12819 break;
12820 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12821 VAR_INIT_STATUS_INITIALIZED);
12823 rtx rtlop1 = XEXP (rtl, 1);
12824 if (GET_MODE (rtlop1) != VOIDmode
12825 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12826 < GET_MODE_BITSIZE (mode))
12827 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12828 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12829 VAR_INIT_STATUS_INITIALIZED);
12832 if (op0 == 0 || op1 == 0)
12833 break;
12835 mem_loc_result = op0;
12836 add_loc_descr (&mem_loc_result, op1);
12837 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12838 break;
12840 case AND:
12841 op = DW_OP_and;
12842 goto do_binop;
12844 case IOR:
12845 op = DW_OP_or;
12846 goto do_binop;
12848 case XOR:
12849 op = DW_OP_xor;
12850 goto do_binop;
12852 do_binop:
12853 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12854 VAR_INIT_STATUS_INITIALIZED);
12855 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12856 VAR_INIT_STATUS_INITIALIZED);
12858 if (op0 == 0 || op1 == 0)
12859 break;
12861 mem_loc_result = op0;
12862 add_loc_descr (&mem_loc_result, op1);
12863 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12864 break;
12866 case MOD:
12867 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12869 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12870 base_type_for_mode (mode, 0),
12871 mode, mem_mode);
12872 break;
12875 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12876 VAR_INIT_STATUS_INITIALIZED);
12877 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12878 VAR_INIT_STATUS_INITIALIZED);
12880 if (op0 == 0 || op1 == 0)
12881 break;
12883 mem_loc_result = op0;
12884 add_loc_descr (&mem_loc_result, op1);
12885 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12886 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12887 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12888 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12889 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12890 break;
12892 case UDIV:
12893 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12895 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12897 op = DW_OP_div;
12898 goto do_binop;
12900 mem_loc_result = typed_binop (DW_OP_div, rtl,
12901 base_type_for_mode (mode, 1),
12902 mode, mem_mode);
12904 break;
12906 case NOT:
12907 op = DW_OP_not;
12908 goto do_unop;
12910 case ABS:
12911 op = DW_OP_abs;
12912 goto do_unop;
12914 case NEG:
12915 op = DW_OP_neg;
12916 goto do_unop;
12918 do_unop:
12919 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12920 VAR_INIT_STATUS_INITIALIZED);
12922 if (op0 == 0)
12923 break;
12925 mem_loc_result = op0;
12926 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12927 break;
12929 case CONST_INT:
12930 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12931 #ifdef POINTERS_EXTEND_UNSIGNED
12932 || (mode == Pmode
12933 && mem_mode != VOIDmode
12934 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12935 #endif
12938 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12939 break;
12941 if (!dwarf_strict
12942 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12943 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12945 dw_die_ref type_die = base_type_for_mode (mode, 1);
12946 machine_mode amode;
12947 if (type_die == NULL)
12948 return NULL;
12949 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12950 MODE_INT, 0);
12951 if (INTVAL (rtl) >= 0
12952 && amode != BLKmode
12953 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12954 /* const DW_OP_GNU_convert <XXX> vs.
12955 DW_OP_GNU_const_type <XXX, 1, const>. */
12956 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12957 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12959 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12960 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12961 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12962 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12963 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12964 add_loc_descr (&mem_loc_result, op0);
12965 return mem_loc_result;
12967 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12968 INTVAL (rtl));
12969 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12970 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12971 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12972 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12973 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12974 else
12976 mem_loc_result->dw_loc_oprnd2.val_class
12977 = dw_val_class_const_double;
12978 mem_loc_result->dw_loc_oprnd2.v.val_double
12979 = double_int::from_shwi (INTVAL (rtl));
12982 break;
12984 case CONST_DOUBLE:
12985 if (!dwarf_strict)
12987 dw_die_ref type_die;
12989 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12990 CONST_DOUBLE rtx could represent either a large integer
12991 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
12992 the value is always a floating point constant.
12994 When it is an integer, a CONST_DOUBLE is used whenever
12995 the constant requires 2 HWIs to be adequately represented.
12996 We output CONST_DOUBLEs as blocks. */
12997 if (mode == VOIDmode
12998 || (GET_MODE (rtl) == VOIDmode
12999 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13000 break;
13001 type_die = base_type_for_mode (mode,
13002 GET_MODE_CLASS (mode) == MODE_INT);
13003 if (type_die == NULL)
13004 return NULL;
13005 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13006 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13007 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13008 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13009 #if TARGET_SUPPORTS_WIDE_INT == 0
13010 if (!SCALAR_FLOAT_MODE_P (mode))
13012 mem_loc_result->dw_loc_oprnd2.val_class
13013 = dw_val_class_const_double;
13014 mem_loc_result->dw_loc_oprnd2.v.val_double
13015 = rtx_to_double_int (rtl);
13017 else
13018 #endif
13020 unsigned int length = GET_MODE_SIZE (mode);
13021 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13023 insert_float (rtl, array);
13024 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13025 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13026 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13027 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13030 break;
13032 case CONST_WIDE_INT:
13033 if (!dwarf_strict)
13035 dw_die_ref type_die;
13037 type_die = base_type_for_mode (mode,
13038 GET_MODE_CLASS (mode) == MODE_INT);
13039 if (type_die == NULL)
13040 return NULL;
13041 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13042 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13043 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13044 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13045 mem_loc_result->dw_loc_oprnd2.val_class
13046 = dw_val_class_wide_int;
13047 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13048 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13050 break;
13052 case EQ:
13053 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13054 break;
13056 case GE:
13057 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13058 break;
13060 case GT:
13061 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13062 break;
13064 case LE:
13065 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13066 break;
13068 case LT:
13069 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13070 break;
13072 case NE:
13073 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13074 break;
13076 case GEU:
13077 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13078 break;
13080 case GTU:
13081 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13082 break;
13084 case LEU:
13085 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13086 break;
13088 case LTU:
13089 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13090 break;
13092 case UMIN:
13093 case UMAX:
13094 if (GET_MODE_CLASS (mode) != MODE_INT)
13095 break;
13096 /* FALLTHRU */
13097 case SMIN:
13098 case SMAX:
13099 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13100 break;
13102 case ZERO_EXTRACT:
13103 case SIGN_EXTRACT:
13104 if (CONST_INT_P (XEXP (rtl, 1))
13105 && CONST_INT_P (XEXP (rtl, 2))
13106 && ((unsigned) INTVAL (XEXP (rtl, 1))
13107 + (unsigned) INTVAL (XEXP (rtl, 2))
13108 <= GET_MODE_BITSIZE (mode))
13109 && GET_MODE_CLASS (mode) == MODE_INT
13110 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13111 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13113 int shift, size;
13114 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13115 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13116 if (op0 == 0)
13117 break;
13118 if (GET_CODE (rtl) == SIGN_EXTRACT)
13119 op = DW_OP_shra;
13120 else
13121 op = DW_OP_shr;
13122 mem_loc_result = op0;
13123 size = INTVAL (XEXP (rtl, 1));
13124 shift = INTVAL (XEXP (rtl, 2));
13125 if (BITS_BIG_ENDIAN)
13126 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13127 - shift - size;
13128 if (shift + size != (int) DWARF2_ADDR_SIZE)
13130 add_loc_descr (&mem_loc_result,
13131 int_loc_descriptor (DWARF2_ADDR_SIZE
13132 - shift - size));
13133 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13135 if (size != (int) DWARF2_ADDR_SIZE)
13137 add_loc_descr (&mem_loc_result,
13138 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13139 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13142 break;
13144 case IF_THEN_ELSE:
13146 dw_loc_descr_ref op2, bra_node, drop_node;
13147 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13148 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13149 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13150 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13151 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13152 VAR_INIT_STATUS_INITIALIZED);
13153 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13154 VAR_INIT_STATUS_INITIALIZED);
13155 if (op0 == NULL || op1 == NULL || op2 == NULL)
13156 break;
13158 mem_loc_result = op1;
13159 add_loc_descr (&mem_loc_result, op2);
13160 add_loc_descr (&mem_loc_result, op0);
13161 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13162 add_loc_descr (&mem_loc_result, bra_node);
13163 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13164 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13165 add_loc_descr (&mem_loc_result, drop_node);
13166 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13167 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13169 break;
13171 case FLOAT_EXTEND:
13172 case FLOAT_TRUNCATE:
13173 case FLOAT:
13174 case UNSIGNED_FLOAT:
13175 case FIX:
13176 case UNSIGNED_FIX:
13177 if (!dwarf_strict)
13179 dw_die_ref type_die;
13180 dw_loc_descr_ref cvt;
13182 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13183 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13184 if (op0 == NULL)
13185 break;
13186 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13187 && (GET_CODE (rtl) == FLOAT
13188 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13189 <= DWARF2_ADDR_SIZE))
13191 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13192 GET_CODE (rtl) == UNSIGNED_FLOAT);
13193 if (type_die == NULL)
13194 break;
13195 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13196 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13197 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13198 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13199 add_loc_descr (&op0, cvt);
13201 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13202 if (type_die == NULL)
13203 break;
13204 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13205 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13206 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13207 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13208 add_loc_descr (&op0, cvt);
13209 if (GET_MODE_CLASS (mode) == MODE_INT
13210 && (GET_CODE (rtl) == FIX
13211 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13213 op0 = convert_descriptor_to_mode (mode, op0);
13214 if (op0 == NULL)
13215 break;
13217 mem_loc_result = op0;
13219 break;
13221 case CLZ:
13222 case CTZ:
13223 case FFS:
13224 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13225 break;
13227 case POPCOUNT:
13228 case PARITY:
13229 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13230 break;
13232 case BSWAP:
13233 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13234 break;
13236 case ROTATE:
13237 case ROTATERT:
13238 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13239 break;
13241 case COMPARE:
13242 /* In theory, we could implement the above. */
13243 /* DWARF cannot represent the unsigned compare operations
13244 natively. */
13245 case SS_MULT:
13246 case US_MULT:
13247 case SS_DIV:
13248 case US_DIV:
13249 case SS_PLUS:
13250 case US_PLUS:
13251 case SS_MINUS:
13252 case US_MINUS:
13253 case SS_NEG:
13254 case US_NEG:
13255 case SS_ABS:
13256 case SS_ASHIFT:
13257 case US_ASHIFT:
13258 case SS_TRUNCATE:
13259 case US_TRUNCATE:
13260 case UNORDERED:
13261 case ORDERED:
13262 case UNEQ:
13263 case UNGE:
13264 case UNGT:
13265 case UNLE:
13266 case UNLT:
13267 case LTGT:
13268 case FRACT_CONVERT:
13269 case UNSIGNED_FRACT_CONVERT:
13270 case SAT_FRACT:
13271 case UNSIGNED_SAT_FRACT:
13272 case SQRT:
13273 case ASM_OPERANDS:
13274 case VEC_MERGE:
13275 case VEC_SELECT:
13276 case VEC_CONCAT:
13277 case VEC_DUPLICATE:
13278 case UNSPEC:
13279 case HIGH:
13280 case FMA:
13281 case STRICT_LOW_PART:
13282 case CONST_VECTOR:
13283 case CONST_FIXED:
13284 case CLRSB:
13285 case CLOBBER:
13286 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13287 can't express it in the debug info. This can happen e.g. with some
13288 TLS UNSPECs. */
13289 break;
13291 case CONST_STRING:
13292 resolve_one_addr (&rtl);
13293 goto symref;
13295 default:
13296 #ifdef ENABLE_CHECKING
13297 print_rtl (stderr, rtl);
13298 gcc_unreachable ();
13299 #else
13300 break;
13301 #endif
13304 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13305 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13307 return mem_loc_result;
13310 /* Return a descriptor that describes the concatenation of two locations.
13311 This is typically a complex variable. */
13313 static dw_loc_descr_ref
13314 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13316 dw_loc_descr_ref cc_loc_result = NULL;
13317 dw_loc_descr_ref x0_ref
13318 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13319 dw_loc_descr_ref x1_ref
13320 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13322 if (x0_ref == 0 || x1_ref == 0)
13323 return 0;
13325 cc_loc_result = x0_ref;
13326 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13328 add_loc_descr (&cc_loc_result, x1_ref);
13329 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13331 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13332 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13334 return cc_loc_result;
13337 /* Return a descriptor that describes the concatenation of N
13338 locations. */
13340 static dw_loc_descr_ref
13341 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13343 unsigned int i;
13344 dw_loc_descr_ref cc_loc_result = NULL;
13345 unsigned int n = XVECLEN (concatn, 0);
13347 for (i = 0; i < n; ++i)
13349 dw_loc_descr_ref ref;
13350 rtx x = XVECEXP (concatn, 0, i);
13352 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13353 if (ref == NULL)
13354 return NULL;
13356 add_loc_descr (&cc_loc_result, ref);
13357 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13360 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13361 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13363 return cc_loc_result;
13366 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13367 for DEBUG_IMPLICIT_PTR RTL. */
13369 static dw_loc_descr_ref
13370 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13372 dw_loc_descr_ref ret;
13373 dw_die_ref ref;
13375 if (dwarf_strict)
13376 return NULL;
13377 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13378 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13379 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13380 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13381 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13382 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13383 if (ref)
13385 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13386 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13387 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13389 else
13391 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13392 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13394 return ret;
13397 /* Output a proper Dwarf location descriptor for a variable or parameter
13398 which is either allocated in a register or in a memory location. For a
13399 register, we just generate an OP_REG and the register number. For a
13400 memory location we provide a Dwarf postfix expression describing how to
13401 generate the (dynamic) address of the object onto the address stack.
13403 MODE is mode of the decl if this loc_descriptor is going to be used in
13404 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13405 allowed, VOIDmode otherwise.
13407 If we don't know how to describe it, return 0. */
13409 static dw_loc_descr_ref
13410 loc_descriptor (rtx rtl, machine_mode mode,
13411 enum var_init_status initialized)
13413 dw_loc_descr_ref loc_result = NULL;
13415 switch (GET_CODE (rtl))
13417 case SUBREG:
13418 /* The case of a subreg may arise when we have a local (register)
13419 variable or a formal (register) parameter which doesn't quite fill
13420 up an entire register. For now, just assume that it is
13421 legitimate to make the Dwarf info refer to the whole register which
13422 contains the given subreg. */
13423 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13424 loc_result = loc_descriptor (SUBREG_REG (rtl),
13425 GET_MODE (SUBREG_REG (rtl)), initialized);
13426 else
13427 goto do_default;
13428 break;
13430 case REG:
13431 loc_result = reg_loc_descriptor (rtl, initialized);
13432 break;
13434 case MEM:
13435 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13436 GET_MODE (rtl), initialized);
13437 if (loc_result == NULL)
13438 loc_result = tls_mem_loc_descriptor (rtl);
13439 if (loc_result == NULL)
13441 rtx new_rtl = avoid_constant_pool_reference (rtl);
13442 if (new_rtl != rtl)
13443 loc_result = loc_descriptor (new_rtl, mode, initialized);
13445 break;
13447 case CONCAT:
13448 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13449 initialized);
13450 break;
13452 case CONCATN:
13453 loc_result = concatn_loc_descriptor (rtl, initialized);
13454 break;
13456 case VAR_LOCATION:
13457 /* Single part. */
13458 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13460 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13461 if (GET_CODE (loc) == EXPR_LIST)
13462 loc = XEXP (loc, 0);
13463 loc_result = loc_descriptor (loc, mode, initialized);
13464 break;
13467 rtl = XEXP (rtl, 1);
13468 /* FALLTHRU */
13470 case PARALLEL:
13472 rtvec par_elems = XVEC (rtl, 0);
13473 int num_elem = GET_NUM_ELEM (par_elems);
13474 machine_mode mode;
13475 int i;
13477 /* Create the first one, so we have something to add to. */
13478 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13479 VOIDmode, initialized);
13480 if (loc_result == NULL)
13481 return NULL;
13482 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13483 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13484 for (i = 1; i < num_elem; i++)
13486 dw_loc_descr_ref temp;
13488 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13489 VOIDmode, initialized);
13490 if (temp == NULL)
13491 return NULL;
13492 add_loc_descr (&loc_result, temp);
13493 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13494 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13497 break;
13499 case CONST_INT:
13500 if (mode != VOIDmode && mode != BLKmode)
13501 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13502 INTVAL (rtl));
13503 break;
13505 case CONST_DOUBLE:
13506 if (mode == VOIDmode)
13507 mode = GET_MODE (rtl);
13509 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13511 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13513 /* Note that a CONST_DOUBLE rtx could represent either an integer
13514 or a floating-point constant. A CONST_DOUBLE is used whenever
13515 the constant requires more than one word in order to be
13516 adequately represented. We output CONST_DOUBLEs as blocks. */
13517 loc_result = new_loc_descr (DW_OP_implicit_value,
13518 GET_MODE_SIZE (mode), 0);
13519 #if TARGET_SUPPORTS_WIDE_INT == 0
13520 if (!SCALAR_FLOAT_MODE_P (mode))
13522 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13523 loc_result->dw_loc_oprnd2.v.val_double
13524 = rtx_to_double_int (rtl);
13526 else
13527 #endif
13529 unsigned int length = GET_MODE_SIZE (mode);
13530 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13532 insert_float (rtl, array);
13533 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13534 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13535 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13536 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13539 break;
13541 case CONST_WIDE_INT:
13542 if (mode == VOIDmode)
13543 mode = GET_MODE (rtl);
13545 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13547 loc_result = new_loc_descr (DW_OP_implicit_value,
13548 GET_MODE_SIZE (mode), 0);
13549 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13550 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13551 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13553 break;
13555 case CONST_VECTOR:
13556 if (mode == VOIDmode)
13557 mode = GET_MODE (rtl);
13559 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13561 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13562 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13563 unsigned char *array
13564 = ggc_vec_alloc<unsigned char> (length * elt_size);
13565 unsigned int i;
13566 unsigned char *p;
13567 machine_mode imode = GET_MODE_INNER (mode);
13569 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13570 switch (GET_MODE_CLASS (mode))
13572 case MODE_VECTOR_INT:
13573 for (i = 0, p = array; i < length; i++, p += elt_size)
13575 rtx elt = CONST_VECTOR_ELT (rtl, i);
13576 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13578 break;
13580 case MODE_VECTOR_FLOAT:
13581 for (i = 0, p = array; i < length; i++, p += elt_size)
13583 rtx elt = CONST_VECTOR_ELT (rtl, i);
13584 insert_float (elt, p);
13586 break;
13588 default:
13589 gcc_unreachable ();
13592 loc_result = new_loc_descr (DW_OP_implicit_value,
13593 length * elt_size, 0);
13594 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13595 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13596 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13597 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13599 break;
13601 case CONST:
13602 if (mode == VOIDmode
13603 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13604 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13605 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13607 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13608 break;
13610 /* FALLTHROUGH */
13611 case SYMBOL_REF:
13612 if (!const_ok_for_output (rtl))
13613 break;
13614 case LABEL_REF:
13615 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13616 && (dwarf_version >= 4 || !dwarf_strict))
13618 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13619 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13620 vec_safe_push (used_rtx_array, rtl);
13622 break;
13624 case DEBUG_IMPLICIT_PTR:
13625 loc_result = implicit_ptr_descriptor (rtl, 0);
13626 break;
13628 case PLUS:
13629 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13630 && CONST_INT_P (XEXP (rtl, 1)))
13632 loc_result
13633 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13634 break;
13636 /* FALLTHRU */
13637 do_default:
13638 default:
13639 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13640 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13641 && dwarf_version >= 4)
13642 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13644 /* Value expression. */
13645 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13646 if (loc_result)
13647 add_loc_descr (&loc_result,
13648 new_loc_descr (DW_OP_stack_value, 0, 0));
13650 break;
13653 return loc_result;
13656 /* We need to figure out what section we should use as the base for the
13657 address ranges where a given location is valid.
13658 1. If this particular DECL has a section associated with it, use that.
13659 2. If this function has a section associated with it, use that.
13660 3. Otherwise, use the text section.
13661 XXX: If you split a variable across multiple sections, we won't notice. */
13663 static const char *
13664 secname_for_decl (const_tree decl)
13666 const char *secname;
13668 if (VAR_OR_FUNCTION_DECL_P (decl)
13669 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13670 && DECL_SECTION_NAME (decl))
13671 secname = DECL_SECTION_NAME (decl);
13672 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13673 secname = DECL_SECTION_NAME (current_function_decl);
13674 else if (cfun && in_cold_section_p)
13675 secname = crtl->subsections.cold_section_label;
13676 else
13677 secname = text_section_label;
13679 return secname;
13682 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13684 static bool
13685 decl_by_reference_p (tree decl)
13687 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13688 || TREE_CODE (decl) == VAR_DECL)
13689 && DECL_BY_REFERENCE (decl));
13692 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13693 for VARLOC. */
13695 static dw_loc_descr_ref
13696 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13697 enum var_init_status initialized)
13699 int have_address = 0;
13700 dw_loc_descr_ref descr;
13701 machine_mode mode;
13703 if (want_address != 2)
13705 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13706 /* Single part. */
13707 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13709 varloc = PAT_VAR_LOCATION_LOC (varloc);
13710 if (GET_CODE (varloc) == EXPR_LIST)
13711 varloc = XEXP (varloc, 0);
13712 mode = GET_MODE (varloc);
13713 if (MEM_P (varloc))
13715 rtx addr = XEXP (varloc, 0);
13716 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13717 mode, initialized);
13718 if (descr)
13719 have_address = 1;
13720 else
13722 rtx x = avoid_constant_pool_reference (varloc);
13723 if (x != varloc)
13724 descr = mem_loc_descriptor (x, mode, VOIDmode,
13725 initialized);
13728 else
13729 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13731 else
13732 return 0;
13734 else
13736 if (GET_CODE (varloc) == VAR_LOCATION)
13737 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13738 else
13739 mode = DECL_MODE (loc);
13740 descr = loc_descriptor (varloc, mode, initialized);
13741 have_address = 1;
13744 if (!descr)
13745 return 0;
13747 if (want_address == 2 && !have_address
13748 && (dwarf_version >= 4 || !dwarf_strict))
13750 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13752 expansion_failed (loc, NULL_RTX,
13753 "DWARF address size mismatch");
13754 return 0;
13756 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13757 have_address = 1;
13759 /* Show if we can't fill the request for an address. */
13760 if (want_address && !have_address)
13762 expansion_failed (loc, NULL_RTX,
13763 "Want address and only have value");
13764 return 0;
13767 /* If we've got an address and don't want one, dereference. */
13768 if (!want_address && have_address)
13770 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13771 enum dwarf_location_atom op;
13773 if (size > DWARF2_ADDR_SIZE || size == -1)
13775 expansion_failed (loc, NULL_RTX,
13776 "DWARF address size mismatch");
13777 return 0;
13779 else if (size == DWARF2_ADDR_SIZE)
13780 op = DW_OP_deref;
13781 else
13782 op = DW_OP_deref_size;
13784 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13787 return descr;
13790 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13791 if it is not possible. */
13793 static dw_loc_descr_ref
13794 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13796 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13797 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13798 else if (dwarf_version >= 3 || !dwarf_strict)
13799 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13800 else
13801 return NULL;
13804 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13805 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13807 static dw_loc_descr_ref
13808 dw_sra_loc_expr (tree decl, rtx loc)
13810 rtx p;
13811 unsigned int padsize = 0;
13812 dw_loc_descr_ref descr, *descr_tail;
13813 unsigned HOST_WIDE_INT decl_size;
13814 rtx varloc;
13815 enum var_init_status initialized;
13817 if (DECL_SIZE (decl) == NULL
13818 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13819 return NULL;
13821 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13822 descr = NULL;
13823 descr_tail = &descr;
13825 for (p = loc; p; p = XEXP (p, 1))
13827 unsigned int bitsize = decl_piece_bitsize (p);
13828 rtx loc_note = *decl_piece_varloc_ptr (p);
13829 dw_loc_descr_ref cur_descr;
13830 dw_loc_descr_ref *tail, last = NULL;
13831 unsigned int opsize = 0;
13833 if (loc_note == NULL_RTX
13834 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13836 padsize += bitsize;
13837 continue;
13839 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13840 varloc = NOTE_VAR_LOCATION (loc_note);
13841 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13842 if (cur_descr == NULL)
13844 padsize += bitsize;
13845 continue;
13848 /* Check that cur_descr either doesn't use
13849 DW_OP_*piece operations, or their sum is equal
13850 to bitsize. Otherwise we can't embed it. */
13851 for (tail = &cur_descr; *tail != NULL;
13852 tail = &(*tail)->dw_loc_next)
13853 if ((*tail)->dw_loc_opc == DW_OP_piece)
13855 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13856 * BITS_PER_UNIT;
13857 last = *tail;
13859 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13861 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13862 last = *tail;
13865 if (last != NULL && opsize != bitsize)
13867 padsize += bitsize;
13868 /* Discard the current piece of the descriptor and release any
13869 addr_table entries it uses. */
13870 remove_loc_list_addr_table_entries (cur_descr);
13871 continue;
13874 /* If there is a hole, add DW_OP_*piece after empty DWARF
13875 expression, which means that those bits are optimized out. */
13876 if (padsize)
13878 if (padsize > decl_size)
13880 remove_loc_list_addr_table_entries (cur_descr);
13881 goto discard_descr;
13883 decl_size -= padsize;
13884 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13885 if (*descr_tail == NULL)
13887 remove_loc_list_addr_table_entries (cur_descr);
13888 goto discard_descr;
13890 descr_tail = &(*descr_tail)->dw_loc_next;
13891 padsize = 0;
13893 *descr_tail = cur_descr;
13894 descr_tail = tail;
13895 if (bitsize > decl_size)
13896 goto discard_descr;
13897 decl_size -= bitsize;
13898 if (last == NULL)
13900 HOST_WIDE_INT offset = 0;
13901 if (GET_CODE (varloc) == VAR_LOCATION
13902 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13904 varloc = PAT_VAR_LOCATION_LOC (varloc);
13905 if (GET_CODE (varloc) == EXPR_LIST)
13906 varloc = XEXP (varloc, 0);
13910 if (GET_CODE (varloc) == CONST
13911 || GET_CODE (varloc) == SIGN_EXTEND
13912 || GET_CODE (varloc) == ZERO_EXTEND)
13913 varloc = XEXP (varloc, 0);
13914 else if (GET_CODE (varloc) == SUBREG)
13915 varloc = SUBREG_REG (varloc);
13916 else
13917 break;
13919 while (1);
13920 /* DW_OP_bit_size offset should be zero for register
13921 or implicit location descriptions and empty location
13922 descriptions, but for memory addresses needs big endian
13923 adjustment. */
13924 if (MEM_P (varloc))
13926 unsigned HOST_WIDE_INT memsize
13927 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13928 if (memsize != bitsize)
13930 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13931 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13932 goto discard_descr;
13933 if (memsize < bitsize)
13934 goto discard_descr;
13935 if (BITS_BIG_ENDIAN)
13936 offset = memsize - bitsize;
13940 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13941 if (*descr_tail == NULL)
13942 goto discard_descr;
13943 descr_tail = &(*descr_tail)->dw_loc_next;
13947 /* If there were any non-empty expressions, add padding till the end of
13948 the decl. */
13949 if (descr != NULL && decl_size != 0)
13951 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13952 if (*descr_tail == NULL)
13953 goto discard_descr;
13955 return descr;
13957 discard_descr:
13958 /* Discard the descriptor and release any addr_table entries it uses. */
13959 remove_loc_list_addr_table_entries (descr);
13960 return NULL;
13963 /* Return the dwarf representation of the location list LOC_LIST of
13964 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13965 function. */
13967 static dw_loc_list_ref
13968 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13970 const char *endname, *secname;
13971 rtx varloc;
13972 enum var_init_status initialized;
13973 struct var_loc_node *node;
13974 dw_loc_descr_ref descr;
13975 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13976 dw_loc_list_ref list = NULL;
13977 dw_loc_list_ref *listp = &list;
13979 /* Now that we know what section we are using for a base,
13980 actually construct the list of locations.
13981 The first location information is what is passed to the
13982 function that creates the location list, and the remaining
13983 locations just get added on to that list.
13984 Note that we only know the start address for a location
13985 (IE location changes), so to build the range, we use
13986 the range [current location start, next location start].
13987 This means we have to special case the last node, and generate
13988 a range of [last location start, end of function label]. */
13990 secname = secname_for_decl (decl);
13992 for (node = loc_list->first; node; node = node->next)
13993 if (GET_CODE (node->loc) == EXPR_LIST
13994 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13996 if (GET_CODE (node->loc) == EXPR_LIST)
13998 /* This requires DW_OP_{,bit_}piece, which is not usable
13999 inside DWARF expressions. */
14000 if (want_address != 2)
14001 continue;
14002 descr = dw_sra_loc_expr (decl, node->loc);
14003 if (descr == NULL)
14004 continue;
14006 else
14008 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14009 varloc = NOTE_VAR_LOCATION (node->loc);
14010 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14012 if (descr)
14014 bool range_across_switch = false;
14015 /* If section switch happens in between node->label
14016 and node->next->label (or end of function) and
14017 we can't emit it as a single entry list,
14018 emit two ranges, first one ending at the end
14019 of first partition and second one starting at the
14020 beginning of second partition. */
14021 if (node == loc_list->last_before_switch
14022 && (node != loc_list->first || loc_list->first->next)
14023 && current_function_decl)
14025 endname = cfun->fde->dw_fde_end;
14026 range_across_switch = true;
14028 /* The variable has a location between NODE->LABEL and
14029 NODE->NEXT->LABEL. */
14030 else if (node->next)
14031 endname = node->next->label;
14032 /* If the variable has a location at the last label
14033 it keeps its location until the end of function. */
14034 else if (!current_function_decl)
14035 endname = text_end_label;
14036 else
14038 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14039 current_function_funcdef_no);
14040 endname = ggc_strdup (label_id);
14043 *listp = new_loc_list (descr, node->label, endname, secname);
14044 if (TREE_CODE (decl) == PARM_DECL
14045 && node == loc_list->first
14046 && NOTE_P (node->loc)
14047 && strcmp (node->label, endname) == 0)
14048 (*listp)->force = true;
14049 listp = &(*listp)->dw_loc_next;
14051 if (range_across_switch)
14053 if (GET_CODE (node->loc) == EXPR_LIST)
14054 descr = dw_sra_loc_expr (decl, node->loc);
14055 else
14057 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14058 varloc = NOTE_VAR_LOCATION (node->loc);
14059 descr = dw_loc_list_1 (decl, varloc, want_address,
14060 initialized);
14062 gcc_assert (descr);
14063 /* The variable has a location between NODE->LABEL and
14064 NODE->NEXT->LABEL. */
14065 if (node->next)
14066 endname = node->next->label;
14067 else
14068 endname = cfun->fde->dw_fde_second_end;
14069 *listp = new_loc_list (descr,
14070 cfun->fde->dw_fde_second_begin,
14071 endname, secname);
14072 listp = &(*listp)->dw_loc_next;
14077 /* Try to avoid the overhead of a location list emitting a location
14078 expression instead, but only if we didn't have more than one
14079 location entry in the first place. If some entries were not
14080 representable, we don't want to pretend a single entry that was
14081 applies to the entire scope in which the variable is
14082 available. */
14083 if (list && loc_list->first->next)
14084 gen_llsym (list);
14086 return list;
14089 /* Return if the loc_list has only single element and thus can be represented
14090 as location description. */
14092 static bool
14093 single_element_loc_list_p (dw_loc_list_ref list)
14095 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14096 return !list->ll_symbol;
14099 /* To each location in list LIST add loc descr REF. */
14101 static void
14102 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14104 dw_loc_descr_ref copy;
14105 add_loc_descr (&list->expr, ref);
14106 list = list->dw_loc_next;
14107 while (list)
14109 copy = ggc_alloc<dw_loc_descr_node> ();
14110 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14111 add_loc_descr (&list->expr, copy);
14112 while (copy->dw_loc_next)
14114 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14115 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14116 copy->dw_loc_next = new_copy;
14117 copy = new_copy;
14119 list = list->dw_loc_next;
14123 /* Given two lists RET and LIST
14124 produce location list that is result of adding expression in LIST
14125 to expression in RET on each position in program.
14126 Might be destructive on both RET and LIST.
14128 TODO: We handle only simple cases of RET or LIST having at most one
14129 element. General case would inolve sorting the lists in program order
14130 and merging them that will need some additional work.
14131 Adding that will improve quality of debug info especially for SRA-ed
14132 structures. */
14134 static void
14135 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14137 if (!list)
14138 return;
14139 if (!*ret)
14141 *ret = list;
14142 return;
14144 if (!list->dw_loc_next)
14146 add_loc_descr_to_each (*ret, list->expr);
14147 return;
14149 if (!(*ret)->dw_loc_next)
14151 add_loc_descr_to_each (list, (*ret)->expr);
14152 *ret = list;
14153 return;
14155 expansion_failed (NULL_TREE, NULL_RTX,
14156 "Don't know how to merge two non-trivial"
14157 " location lists.\n");
14158 *ret = NULL;
14159 return;
14162 /* LOC is constant expression. Try a luck, look it up in constant
14163 pool and return its loc_descr of its address. */
14165 static dw_loc_descr_ref
14166 cst_pool_loc_descr (tree loc)
14168 /* Get an RTL for this, if something has been emitted. */
14169 rtx rtl = lookup_constant_def (loc);
14171 if (!rtl || !MEM_P (rtl))
14173 gcc_assert (!rtl);
14174 return 0;
14176 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14178 /* TODO: We might get more coverage if we was actually delaying expansion
14179 of all expressions till end of compilation when constant pools are fully
14180 populated. */
14181 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14183 expansion_failed (loc, NULL_RTX,
14184 "CST value in contant pool but not marked.");
14185 return 0;
14187 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14188 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14191 /* Return dw_loc_list representing address of addr_expr LOC
14192 by looking for inner INDIRECT_REF expression and turning
14193 it into simple arithmetics. */
14195 static dw_loc_list_ref
14196 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14198 tree obj, offset;
14199 HOST_WIDE_INT bitsize, bitpos, bytepos;
14200 machine_mode mode;
14201 int unsignedp, volatilep = 0;
14202 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14204 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14205 &bitsize, &bitpos, &offset, &mode,
14206 &unsignedp, &volatilep, false);
14207 STRIP_NOPS (obj);
14208 if (bitpos % BITS_PER_UNIT)
14210 expansion_failed (loc, NULL_RTX, "bitfield access");
14211 return 0;
14213 if (!INDIRECT_REF_P (obj))
14215 expansion_failed (obj,
14216 NULL_RTX, "no indirect ref in inner refrence");
14217 return 0;
14219 if (!offset && !bitpos)
14220 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14221 else if (toplev
14222 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14223 && (dwarf_version >= 4 || !dwarf_strict))
14225 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14226 if (!list_ret)
14227 return 0;
14228 if (offset)
14230 /* Variable offset. */
14231 list_ret1 = loc_list_from_tree (offset, 0);
14232 if (list_ret1 == 0)
14233 return 0;
14234 add_loc_list (&list_ret, list_ret1);
14235 if (!list_ret)
14236 return 0;
14237 add_loc_descr_to_each (list_ret,
14238 new_loc_descr (DW_OP_plus, 0, 0));
14240 bytepos = bitpos / BITS_PER_UNIT;
14241 if (bytepos > 0)
14242 add_loc_descr_to_each (list_ret,
14243 new_loc_descr (DW_OP_plus_uconst,
14244 bytepos, 0));
14245 else if (bytepos < 0)
14246 loc_list_plus_const (list_ret, bytepos);
14247 add_loc_descr_to_each (list_ret,
14248 new_loc_descr (DW_OP_stack_value, 0, 0));
14250 return list_ret;
14254 /* Generate Dwarf location list representing LOC.
14255 If WANT_ADDRESS is false, expression computing LOC will be computed
14256 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14257 if WANT_ADDRESS is 2, expression computing address useable in location
14258 will be returned (i.e. DW_OP_reg can be used
14259 to refer to register values). */
14261 static dw_loc_list_ref
14262 loc_list_from_tree (tree loc, int want_address)
14264 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14265 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14266 int have_address = 0;
14267 enum dwarf_location_atom op;
14269 /* ??? Most of the time we do not take proper care for sign/zero
14270 extending the values properly. Hopefully this won't be a real
14271 problem... */
14273 switch (TREE_CODE (loc))
14275 case ERROR_MARK:
14276 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14277 return 0;
14279 case PLACEHOLDER_EXPR:
14280 /* This case involves extracting fields from an object to determine the
14281 position of other fields. We don't try to encode this here. The
14282 only user of this is Ada, which encodes the needed information using
14283 the names of types. */
14284 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14285 return 0;
14287 case CALL_EXPR:
14288 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14289 /* There are no opcodes for these operations. */
14290 return 0;
14292 case PREINCREMENT_EXPR:
14293 case PREDECREMENT_EXPR:
14294 case POSTINCREMENT_EXPR:
14295 case POSTDECREMENT_EXPR:
14296 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14297 /* There are no opcodes for these operations. */
14298 return 0;
14300 case ADDR_EXPR:
14301 /* If we already want an address, see if there is INDIRECT_REF inside
14302 e.g. for &this->field. */
14303 if (want_address)
14305 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14306 (loc, want_address == 2);
14307 if (list_ret)
14308 have_address = 1;
14309 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14310 && (ret = cst_pool_loc_descr (loc)))
14311 have_address = 1;
14313 /* Otherwise, process the argument and look for the address. */
14314 if (!list_ret && !ret)
14315 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14316 else
14318 if (want_address)
14319 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14320 return NULL;
14322 break;
14324 case VAR_DECL:
14325 if (DECL_THREAD_LOCAL_P (loc))
14327 rtx rtl;
14328 enum dwarf_location_atom tls_op;
14329 enum dtprel_bool dtprel = dtprel_false;
14331 if (targetm.have_tls)
14333 /* If this is not defined, we have no way to emit the
14334 data. */
14335 if (!targetm.asm_out.output_dwarf_dtprel)
14336 return 0;
14338 /* The way DW_OP_GNU_push_tls_address is specified, we
14339 can only look up addresses of objects in the current
14340 module. We used DW_OP_addr as first op, but that's
14341 wrong, because DW_OP_addr is relocated by the debug
14342 info consumer, while DW_OP_GNU_push_tls_address
14343 operand shouldn't be. */
14344 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14345 return 0;
14346 dtprel = dtprel_true;
14347 tls_op = DW_OP_GNU_push_tls_address;
14349 else
14351 if (!targetm.emutls.debug_form_tls_address
14352 || !(dwarf_version >= 3 || !dwarf_strict))
14353 return 0;
14354 /* We stuffed the control variable into the DECL_VALUE_EXPR
14355 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14356 no longer appear in gimple code. We used the control
14357 variable in specific so that we could pick it up here. */
14358 loc = DECL_VALUE_EXPR (loc);
14359 tls_op = DW_OP_form_tls_address;
14362 rtl = rtl_for_decl_location (loc);
14363 if (rtl == NULL_RTX)
14364 return 0;
14366 if (!MEM_P (rtl))
14367 return 0;
14368 rtl = XEXP (rtl, 0);
14369 if (! CONSTANT_P (rtl))
14370 return 0;
14372 ret = new_addr_loc_descr (rtl, dtprel);
14373 ret1 = new_loc_descr (tls_op, 0, 0);
14374 add_loc_descr (&ret, ret1);
14376 have_address = 1;
14377 break;
14379 /* FALLTHRU */
14381 case PARM_DECL:
14382 case RESULT_DECL:
14383 if (DECL_HAS_VALUE_EXPR_P (loc))
14384 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14385 want_address);
14386 /* FALLTHRU */
14388 case FUNCTION_DECL:
14390 rtx rtl;
14391 var_loc_list *loc_list = lookup_decl_loc (loc);
14393 if (loc_list && loc_list->first)
14395 list_ret = dw_loc_list (loc_list, loc, want_address);
14396 have_address = want_address != 0;
14397 break;
14399 rtl = rtl_for_decl_location (loc);
14400 if (rtl == NULL_RTX)
14402 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14403 return 0;
14405 else if (CONST_INT_P (rtl))
14407 HOST_WIDE_INT val = INTVAL (rtl);
14408 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14409 val &= GET_MODE_MASK (DECL_MODE (loc));
14410 ret = int_loc_descriptor (val);
14412 else if (GET_CODE (rtl) == CONST_STRING)
14414 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14415 return 0;
14417 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14418 ret = new_addr_loc_descr (rtl, dtprel_false);
14419 else
14421 machine_mode mode, mem_mode;
14423 /* Certain constructs can only be represented at top-level. */
14424 if (want_address == 2)
14426 ret = loc_descriptor (rtl, VOIDmode,
14427 VAR_INIT_STATUS_INITIALIZED);
14428 have_address = 1;
14430 else
14432 mode = GET_MODE (rtl);
14433 mem_mode = VOIDmode;
14434 if (MEM_P (rtl))
14436 mem_mode = mode;
14437 mode = get_address_mode (rtl);
14438 rtl = XEXP (rtl, 0);
14439 have_address = 1;
14441 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14442 VAR_INIT_STATUS_INITIALIZED);
14444 if (!ret)
14445 expansion_failed (loc, rtl,
14446 "failed to produce loc descriptor for rtl");
14449 break;
14451 case MEM_REF:
14452 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14454 have_address = 1;
14455 goto do_plus;
14457 /* Fallthru. */
14458 case INDIRECT_REF:
14459 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14460 have_address = 1;
14461 break;
14463 case TARGET_MEM_REF:
14464 case SSA_NAME:
14465 return NULL;
14467 case COMPOUND_EXPR:
14468 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14470 CASE_CONVERT:
14471 case VIEW_CONVERT_EXPR:
14472 case SAVE_EXPR:
14473 case MODIFY_EXPR:
14474 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14476 case COMPONENT_REF:
14477 case BIT_FIELD_REF:
14478 case ARRAY_REF:
14479 case ARRAY_RANGE_REF:
14480 case REALPART_EXPR:
14481 case IMAGPART_EXPR:
14483 tree obj, offset;
14484 HOST_WIDE_INT bitsize, bitpos, bytepos;
14485 machine_mode mode;
14486 int unsignedp, volatilep = 0;
14488 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14489 &unsignedp, &volatilep, false);
14491 gcc_assert (obj != loc);
14493 list_ret = loc_list_from_tree (obj,
14494 want_address == 2
14495 && !bitpos && !offset ? 2 : 1);
14496 /* TODO: We can extract value of the small expression via shifting even
14497 for nonzero bitpos. */
14498 if (list_ret == 0)
14499 return 0;
14500 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14502 expansion_failed (loc, NULL_RTX,
14503 "bitfield access");
14504 return 0;
14507 if (offset != NULL_TREE)
14509 /* Variable offset. */
14510 list_ret1 = loc_list_from_tree (offset, 0);
14511 if (list_ret1 == 0)
14512 return 0;
14513 add_loc_list (&list_ret, list_ret1);
14514 if (!list_ret)
14515 return 0;
14516 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14519 bytepos = bitpos / BITS_PER_UNIT;
14520 if (bytepos > 0)
14521 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14522 else if (bytepos < 0)
14523 loc_list_plus_const (list_ret, bytepos);
14525 have_address = 1;
14526 break;
14529 case INTEGER_CST:
14530 if ((want_address || !tree_fits_shwi_p (loc))
14531 && (ret = cst_pool_loc_descr (loc)))
14532 have_address = 1;
14533 else if (want_address == 2
14534 && tree_fits_shwi_p (loc)
14535 && (ret = address_of_int_loc_descriptor
14536 (int_size_in_bytes (TREE_TYPE (loc)),
14537 tree_to_shwi (loc))))
14538 have_address = 1;
14539 else if (tree_fits_shwi_p (loc))
14540 ret = int_loc_descriptor (tree_to_shwi (loc));
14541 else
14543 expansion_failed (loc, NULL_RTX,
14544 "Integer operand is not host integer");
14545 return 0;
14547 break;
14549 case CONSTRUCTOR:
14550 case REAL_CST:
14551 case STRING_CST:
14552 case COMPLEX_CST:
14553 if ((ret = cst_pool_loc_descr (loc)))
14554 have_address = 1;
14555 else
14556 /* We can construct small constants here using int_loc_descriptor. */
14557 expansion_failed (loc, NULL_RTX,
14558 "constructor or constant not in constant pool");
14559 break;
14561 case TRUTH_AND_EXPR:
14562 case TRUTH_ANDIF_EXPR:
14563 case BIT_AND_EXPR:
14564 op = DW_OP_and;
14565 goto do_binop;
14567 case TRUTH_XOR_EXPR:
14568 case BIT_XOR_EXPR:
14569 op = DW_OP_xor;
14570 goto do_binop;
14572 case TRUTH_OR_EXPR:
14573 case TRUTH_ORIF_EXPR:
14574 case BIT_IOR_EXPR:
14575 op = DW_OP_or;
14576 goto do_binop;
14578 case FLOOR_DIV_EXPR:
14579 case CEIL_DIV_EXPR:
14580 case ROUND_DIV_EXPR:
14581 case TRUNC_DIV_EXPR:
14582 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14583 return 0;
14584 op = DW_OP_div;
14585 goto do_binop;
14587 case MINUS_EXPR:
14588 op = DW_OP_minus;
14589 goto do_binop;
14591 case FLOOR_MOD_EXPR:
14592 case CEIL_MOD_EXPR:
14593 case ROUND_MOD_EXPR:
14594 case TRUNC_MOD_EXPR:
14595 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14597 op = DW_OP_mod;
14598 goto do_binop;
14600 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14601 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14602 if (list_ret == 0 || list_ret1 == 0)
14603 return 0;
14605 add_loc_list (&list_ret, list_ret1);
14606 if (list_ret == 0)
14607 return 0;
14608 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14609 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14610 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14611 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14612 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14613 break;
14615 case MULT_EXPR:
14616 op = DW_OP_mul;
14617 goto do_binop;
14619 case LSHIFT_EXPR:
14620 op = DW_OP_shl;
14621 goto do_binop;
14623 case RSHIFT_EXPR:
14624 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14625 goto do_binop;
14627 case POINTER_PLUS_EXPR:
14628 case PLUS_EXPR:
14629 do_plus:
14630 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14632 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14633 if (list_ret == 0)
14634 return 0;
14636 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14637 break;
14640 op = DW_OP_plus;
14641 goto do_binop;
14643 case LE_EXPR:
14644 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14645 return 0;
14647 op = DW_OP_le;
14648 goto do_binop;
14650 case GE_EXPR:
14651 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14652 return 0;
14654 op = DW_OP_ge;
14655 goto do_binop;
14657 case LT_EXPR:
14658 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14659 return 0;
14661 op = DW_OP_lt;
14662 goto do_binop;
14664 case GT_EXPR:
14665 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14666 return 0;
14668 op = DW_OP_gt;
14669 goto do_binop;
14671 case EQ_EXPR:
14672 op = DW_OP_eq;
14673 goto do_binop;
14675 case NE_EXPR:
14676 op = DW_OP_ne;
14677 goto do_binop;
14679 do_binop:
14680 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14681 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14682 if (list_ret == 0 || list_ret1 == 0)
14683 return 0;
14685 add_loc_list (&list_ret, list_ret1);
14686 if (list_ret == 0)
14687 return 0;
14688 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14689 break;
14691 case TRUTH_NOT_EXPR:
14692 case BIT_NOT_EXPR:
14693 op = DW_OP_not;
14694 goto do_unop;
14696 case ABS_EXPR:
14697 op = DW_OP_abs;
14698 goto do_unop;
14700 case NEGATE_EXPR:
14701 op = DW_OP_neg;
14702 goto do_unop;
14704 do_unop:
14705 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14706 if (list_ret == 0)
14707 return 0;
14709 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14710 break;
14712 case MIN_EXPR:
14713 case MAX_EXPR:
14715 const enum tree_code code =
14716 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14718 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14719 build2 (code, integer_type_node,
14720 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14721 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14724 /* ... fall through ... */
14726 case COND_EXPR:
14728 dw_loc_descr_ref lhs
14729 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14730 dw_loc_list_ref rhs
14731 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14732 dw_loc_descr_ref bra_node, jump_node, tmp;
14734 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14735 if (list_ret == 0 || lhs == 0 || rhs == 0)
14736 return 0;
14738 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14739 add_loc_descr_to_each (list_ret, bra_node);
14741 add_loc_list (&list_ret, rhs);
14742 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14743 add_loc_descr_to_each (list_ret, jump_node);
14745 add_loc_descr_to_each (list_ret, lhs);
14746 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14747 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14749 /* ??? Need a node to point the skip at. Use a nop. */
14750 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14751 add_loc_descr_to_each (list_ret, tmp);
14752 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14753 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14755 break;
14757 case FIX_TRUNC_EXPR:
14758 return 0;
14760 default:
14761 /* Leave front-end specific codes as simply unknown. This comes
14762 up, for instance, with the C STMT_EXPR. */
14763 if ((unsigned int) TREE_CODE (loc)
14764 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14766 expansion_failed (loc, NULL_RTX,
14767 "language specific tree node");
14768 return 0;
14771 #ifdef ENABLE_CHECKING
14772 /* Otherwise this is a generic code; we should just lists all of
14773 these explicitly. We forgot one. */
14774 gcc_unreachable ();
14775 #else
14776 /* In a release build, we want to degrade gracefully: better to
14777 generate incomplete debugging information than to crash. */
14778 return NULL;
14779 #endif
14782 if (!ret && !list_ret)
14783 return 0;
14785 if (want_address == 2 && !have_address
14786 && (dwarf_version >= 4 || !dwarf_strict))
14788 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14790 expansion_failed (loc, NULL_RTX,
14791 "DWARF address size mismatch");
14792 return 0;
14794 if (ret)
14795 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14796 else
14797 add_loc_descr_to_each (list_ret,
14798 new_loc_descr (DW_OP_stack_value, 0, 0));
14799 have_address = 1;
14801 /* Show if we can't fill the request for an address. */
14802 if (want_address && !have_address)
14804 expansion_failed (loc, NULL_RTX,
14805 "Want address and only have value");
14806 return 0;
14809 gcc_assert (!ret || !list_ret);
14811 /* If we've got an address and don't want one, dereference. */
14812 if (!want_address && have_address)
14814 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14816 if (size > DWARF2_ADDR_SIZE || size == -1)
14818 expansion_failed (loc, NULL_RTX,
14819 "DWARF address size mismatch");
14820 return 0;
14822 else if (size == DWARF2_ADDR_SIZE)
14823 op = DW_OP_deref;
14824 else
14825 op = DW_OP_deref_size;
14827 if (ret)
14828 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14829 else
14830 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14832 if (ret)
14833 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14835 return list_ret;
14838 /* Same as above but return only single location expression. */
14839 static dw_loc_descr_ref
14840 loc_descriptor_from_tree (tree loc, int want_address)
14842 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14843 if (!ret)
14844 return NULL;
14845 if (ret->dw_loc_next)
14847 expansion_failed (loc, NULL_RTX,
14848 "Location list where only loc descriptor needed");
14849 return NULL;
14851 return ret->expr;
14854 /* Given a value, round it up to the lowest multiple of `boundary'
14855 which is not less than the value itself. */
14857 static inline HOST_WIDE_INT
14858 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14860 return (((value + boundary - 1) / boundary) * boundary);
14863 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14864 pointer to the declared type for the relevant field variable, or return
14865 `integer_type_node' if the given node turns out to be an
14866 ERROR_MARK node. */
14868 static inline tree
14869 field_type (const_tree decl)
14871 tree type;
14873 if (TREE_CODE (decl) == ERROR_MARK)
14874 return integer_type_node;
14876 type = DECL_BIT_FIELD_TYPE (decl);
14877 if (type == NULL_TREE)
14878 type = TREE_TYPE (decl);
14880 return type;
14883 /* Given a pointer to a tree node, return the alignment in bits for
14884 it, or else return BITS_PER_WORD if the node actually turns out to
14885 be an ERROR_MARK node. */
14887 static inline unsigned
14888 simple_type_align_in_bits (const_tree type)
14890 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14893 static inline unsigned
14894 simple_decl_align_in_bits (const_tree decl)
14896 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14899 /* Return the result of rounding T up to ALIGN. */
14901 static inline offset_int
14902 round_up_to_align (const offset_int &t, unsigned int align)
14904 return wi::udiv_trunc (t + align - 1, align) * align;
14907 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14908 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14909 or return 0 if we are unable to determine what that offset is, either
14910 because the argument turns out to be a pointer to an ERROR_MARK node, or
14911 because the offset is actually variable. (We can't handle the latter case
14912 just yet). */
14914 static HOST_WIDE_INT
14915 field_byte_offset (const_tree decl)
14917 offset_int object_offset_in_bits;
14918 offset_int object_offset_in_bytes;
14919 offset_int bitpos_int;
14921 if (TREE_CODE (decl) == ERROR_MARK)
14922 return 0;
14924 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14926 /* We cannot yet cope with fields whose positions are variable, so
14927 for now, when we see such things, we simply return 0. Someday, we may
14928 be able to handle such cases, but it will be damn difficult. */
14929 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14930 return 0;
14932 bitpos_int = wi::to_offset (bit_position (decl));
14934 #ifdef PCC_BITFIELD_TYPE_MATTERS
14935 if (PCC_BITFIELD_TYPE_MATTERS)
14937 tree type;
14938 tree field_size_tree;
14939 offset_int deepest_bitpos;
14940 offset_int field_size_in_bits;
14941 unsigned int type_align_in_bits;
14942 unsigned int decl_align_in_bits;
14943 offset_int type_size_in_bits;
14945 type = field_type (decl);
14946 type_size_in_bits = offset_int_type_size_in_bits (type);
14947 type_align_in_bits = simple_type_align_in_bits (type);
14949 field_size_tree = DECL_SIZE (decl);
14951 /* The size could be unspecified if there was an error, or for
14952 a flexible array member. */
14953 if (!field_size_tree)
14954 field_size_tree = bitsize_zero_node;
14956 /* If the size of the field is not constant, use the type size. */
14957 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14958 field_size_in_bits = wi::to_offset (field_size_tree);
14959 else
14960 field_size_in_bits = type_size_in_bits;
14962 decl_align_in_bits = simple_decl_align_in_bits (decl);
14964 /* The GCC front-end doesn't make any attempt to keep track of the
14965 starting bit offset (relative to the start of the containing
14966 structure type) of the hypothetical "containing object" for a
14967 bit-field. Thus, when computing the byte offset value for the
14968 start of the "containing object" of a bit-field, we must deduce
14969 this information on our own. This can be rather tricky to do in
14970 some cases. For example, handling the following structure type
14971 definition when compiling for an i386/i486 target (which only
14972 aligns long long's to 32-bit boundaries) can be very tricky:
14974 struct S { int field1; long long field2:31; };
14976 Fortunately, there is a simple rule-of-thumb which can be used
14977 in such cases. When compiling for an i386/i486, GCC will
14978 allocate 8 bytes for the structure shown above. It decides to
14979 do this based upon one simple rule for bit-field allocation.
14980 GCC allocates each "containing object" for each bit-field at
14981 the first (i.e. lowest addressed) legitimate alignment boundary
14982 (based upon the required minimum alignment for the declared
14983 type of the field) which it can possibly use, subject to the
14984 condition that there is still enough available space remaining
14985 in the containing object (when allocated at the selected point)
14986 to fully accommodate all of the bits of the bit-field itself.
14988 This simple rule makes it obvious why GCC allocates 8 bytes for
14989 each object of the structure type shown above. When looking
14990 for a place to allocate the "containing object" for `field2',
14991 the compiler simply tries to allocate a 64-bit "containing
14992 object" at each successive 32-bit boundary (starting at zero)
14993 until it finds a place to allocate that 64- bit field such that
14994 at least 31 contiguous (and previously unallocated) bits remain
14995 within that selected 64 bit field. (As it turns out, for the
14996 example above, the compiler finds it is OK to allocate the
14997 "containing object" 64-bit field at bit-offset zero within the
14998 structure type.)
15000 Here we attempt to work backwards from the limited set of facts
15001 we're given, and we try to deduce from those facts, where GCC
15002 must have believed that the containing object started (within
15003 the structure type). The value we deduce is then used (by the
15004 callers of this routine) to generate DW_AT_location and
15005 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15006 the case of DW_AT_location, regular fields as well). */
15008 /* Figure out the bit-distance from the start of the structure to
15009 the "deepest" bit of the bit-field. */
15010 deepest_bitpos = bitpos_int + field_size_in_bits;
15012 /* This is the tricky part. Use some fancy footwork to deduce
15013 where the lowest addressed bit of the containing object must
15014 be. */
15015 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15017 /* Round up to type_align by default. This works best for
15018 bitfields. */
15019 object_offset_in_bits
15020 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15022 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15024 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15026 /* Round up to decl_align instead. */
15027 object_offset_in_bits
15028 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15031 else
15032 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15033 object_offset_in_bits = bitpos_int;
15035 object_offset_in_bytes
15036 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15037 return object_offset_in_bytes.to_shwi ();
15040 /* The following routines define various Dwarf attributes and any data
15041 associated with them. */
15043 /* Add a location description attribute value to a DIE.
15045 This emits location attributes suitable for whole variables and
15046 whole parameters. Note that the location attributes for struct fields are
15047 generated by the routine `data_member_location_attribute' below. */
15049 static inline void
15050 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15051 dw_loc_list_ref descr)
15053 if (descr == 0)
15054 return;
15055 if (single_element_loc_list_p (descr))
15056 add_AT_loc (die, attr_kind, descr->expr);
15057 else
15058 add_AT_loc_list (die, attr_kind, descr);
15061 /* Add DW_AT_accessibility attribute to DIE if needed. */
15063 static void
15064 add_accessibility_attribute (dw_die_ref die, tree decl)
15066 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15067 children, otherwise the default is DW_ACCESS_public. In DWARF2
15068 the default has always been DW_ACCESS_public. */
15069 if (TREE_PROTECTED (decl))
15070 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15071 else if (TREE_PRIVATE (decl))
15073 if (dwarf_version == 2
15074 || die->die_parent == NULL
15075 || die->die_parent->die_tag != DW_TAG_class_type)
15076 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15078 else if (dwarf_version > 2
15079 && die->die_parent
15080 && die->die_parent->die_tag == DW_TAG_class_type)
15081 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15084 /* Attach the specialized form of location attribute used for data members of
15085 struct and union types. In the special case of a FIELD_DECL node which
15086 represents a bit-field, the "offset" part of this special location
15087 descriptor must indicate the distance in bytes from the lowest-addressed
15088 byte of the containing struct or union type to the lowest-addressed byte of
15089 the "containing object" for the bit-field. (See the `field_byte_offset'
15090 function above).
15092 For any given bit-field, the "containing object" is a hypothetical object
15093 (of some integral or enum type) within which the given bit-field lives. The
15094 type of this hypothetical "containing object" is always the same as the
15095 declared type of the individual bit-field itself (for GCC anyway... the
15096 DWARF spec doesn't actually mandate this). Note that it is the size (in
15097 bytes) of the hypothetical "containing object" which will be given in the
15098 DW_AT_byte_size attribute for this bit-field. (See the
15099 `byte_size_attribute' function below.) It is also used when calculating the
15100 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15101 function below.) */
15103 static void
15104 add_data_member_location_attribute (dw_die_ref die, tree decl)
15106 HOST_WIDE_INT offset;
15107 dw_loc_descr_ref loc_descr = 0;
15109 if (TREE_CODE (decl) == TREE_BINFO)
15111 /* We're working on the TAG_inheritance for a base class. */
15112 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15114 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15115 aren't at a fixed offset from all (sub)objects of the same
15116 type. We need to extract the appropriate offset from our
15117 vtable. The following dwarf expression means
15119 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15121 This is specific to the V3 ABI, of course. */
15123 dw_loc_descr_ref tmp;
15125 /* Make a copy of the object address. */
15126 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15127 add_loc_descr (&loc_descr, tmp);
15129 /* Extract the vtable address. */
15130 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15131 add_loc_descr (&loc_descr, tmp);
15133 /* Calculate the address of the offset. */
15134 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15135 gcc_assert (offset < 0);
15137 tmp = int_loc_descriptor (-offset);
15138 add_loc_descr (&loc_descr, tmp);
15139 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15140 add_loc_descr (&loc_descr, tmp);
15142 /* Extract the offset. */
15143 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15144 add_loc_descr (&loc_descr, tmp);
15146 /* Add it to the object address. */
15147 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15148 add_loc_descr (&loc_descr, tmp);
15150 else
15151 offset = tree_to_shwi (BINFO_OFFSET (decl));
15153 else
15154 offset = field_byte_offset (decl);
15156 if (! loc_descr)
15158 if (dwarf_version > 2)
15160 /* Don't need to output a location expression, just the constant. */
15161 if (offset < 0)
15162 add_AT_int (die, DW_AT_data_member_location, offset);
15163 else
15164 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15165 return;
15167 else
15169 enum dwarf_location_atom op;
15171 /* The DWARF2 standard says that we should assume that the structure
15172 address is already on the stack, so we can specify a structure
15173 field address by using DW_OP_plus_uconst. */
15174 op = DW_OP_plus_uconst;
15175 loc_descr = new_loc_descr (op, offset, 0);
15179 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15182 /* Writes integer values to dw_vec_const array. */
15184 static void
15185 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15187 while (size != 0)
15189 *dest++ = val & 0xff;
15190 val >>= 8;
15191 --size;
15195 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15197 static HOST_WIDE_INT
15198 extract_int (const unsigned char *src, unsigned int size)
15200 HOST_WIDE_INT val = 0;
15202 src += size;
15203 while (size != 0)
15205 val <<= 8;
15206 val |= *--src & 0xff;
15207 --size;
15209 return val;
15212 /* Writes wide_int values to dw_vec_const array. */
15214 static void
15215 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15217 int i;
15219 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15221 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15222 return;
15225 /* We'd have to extend this code to support odd sizes. */
15226 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15228 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15230 if (WORDS_BIG_ENDIAN)
15231 for (i = n - 1; i >= 0; i--)
15233 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15234 dest += sizeof (HOST_WIDE_INT);
15236 else
15237 for (i = 0; i < n; i++)
15239 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15240 dest += sizeof (HOST_WIDE_INT);
15244 /* Writes floating point values to dw_vec_const array. */
15246 static void
15247 insert_float (const_rtx rtl, unsigned char *array)
15249 REAL_VALUE_TYPE rv;
15250 long val[4];
15251 int i;
15253 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15254 real_to_target (val, &rv, GET_MODE (rtl));
15256 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15257 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15259 insert_int (val[i], 4, array);
15260 array += 4;
15264 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15265 does not have a "location" either in memory or in a register. These
15266 things can arise in GNU C when a constant is passed as an actual parameter
15267 to an inlined function. They can also arise in C++ where declared
15268 constants do not necessarily get memory "homes". */
15270 static bool
15271 add_const_value_attribute (dw_die_ref die, rtx rtl)
15273 switch (GET_CODE (rtl))
15275 case CONST_INT:
15277 HOST_WIDE_INT val = INTVAL (rtl);
15279 if (val < 0)
15280 add_AT_int (die, DW_AT_const_value, val);
15281 else
15282 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15284 return true;
15286 case CONST_WIDE_INT:
15287 add_AT_wide (die, DW_AT_const_value,
15288 std::make_pair (rtl, GET_MODE (rtl)));
15289 return true;
15291 case CONST_DOUBLE:
15292 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15293 floating-point constant. A CONST_DOUBLE is used whenever the
15294 constant requires more than one word in order to be adequately
15295 represented. */
15297 machine_mode mode = GET_MODE (rtl);
15299 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15300 add_AT_double (die, DW_AT_const_value,
15301 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15302 else
15304 unsigned int length = GET_MODE_SIZE (mode);
15305 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15307 insert_float (rtl, array);
15308 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15311 return true;
15313 case CONST_VECTOR:
15315 machine_mode mode = GET_MODE (rtl);
15316 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15317 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15318 unsigned char *array
15319 = ggc_vec_alloc<unsigned char> (length * elt_size);
15320 unsigned int i;
15321 unsigned char *p;
15322 machine_mode imode = GET_MODE_INNER (mode);
15324 switch (GET_MODE_CLASS (mode))
15326 case MODE_VECTOR_INT:
15327 for (i = 0, p = array; i < length; i++, p += elt_size)
15329 rtx elt = CONST_VECTOR_ELT (rtl, i);
15330 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15332 break;
15334 case MODE_VECTOR_FLOAT:
15335 for (i = 0, p = array; i < length; i++, p += elt_size)
15337 rtx elt = CONST_VECTOR_ELT (rtl, i);
15338 insert_float (elt, p);
15340 break;
15342 default:
15343 gcc_unreachable ();
15346 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15348 return true;
15350 case CONST_STRING:
15351 if (dwarf_version >= 4 || !dwarf_strict)
15353 dw_loc_descr_ref loc_result;
15354 resolve_one_addr (&rtl);
15355 rtl_addr:
15356 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15357 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15358 add_AT_loc (die, DW_AT_location, loc_result);
15359 vec_safe_push (used_rtx_array, rtl);
15360 return true;
15362 return false;
15364 case CONST:
15365 if (CONSTANT_P (XEXP (rtl, 0)))
15366 return add_const_value_attribute (die, XEXP (rtl, 0));
15367 /* FALLTHROUGH */
15368 case SYMBOL_REF:
15369 if (!const_ok_for_output (rtl))
15370 return false;
15371 case LABEL_REF:
15372 if (dwarf_version >= 4 || !dwarf_strict)
15373 goto rtl_addr;
15374 return false;
15376 case PLUS:
15377 /* In cases where an inlined instance of an inline function is passed
15378 the address of an `auto' variable (which is local to the caller) we
15379 can get a situation where the DECL_RTL of the artificial local
15380 variable (for the inlining) which acts as a stand-in for the
15381 corresponding formal parameter (of the inline function) will look
15382 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15383 exactly a compile-time constant expression, but it isn't the address
15384 of the (artificial) local variable either. Rather, it represents the
15385 *value* which the artificial local variable always has during its
15386 lifetime. We currently have no way to represent such quasi-constant
15387 values in Dwarf, so for now we just punt and generate nothing. */
15388 return false;
15390 case HIGH:
15391 case CONST_FIXED:
15392 return false;
15394 case MEM:
15395 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15396 && MEM_READONLY_P (rtl)
15397 && GET_MODE (rtl) == BLKmode)
15399 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15400 return true;
15402 return false;
15404 default:
15405 /* No other kinds of rtx should be possible here. */
15406 gcc_unreachable ();
15408 return false;
15411 /* Determine whether the evaluation of EXPR references any variables
15412 or functions which aren't otherwise used (and therefore may not be
15413 output). */
15414 static tree
15415 reference_to_unused (tree * tp, int * walk_subtrees,
15416 void * data ATTRIBUTE_UNUSED)
15418 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15419 *walk_subtrees = 0;
15421 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15422 && ! TREE_ASM_WRITTEN (*tp))
15423 return *tp;
15424 /* ??? The C++ FE emits debug information for using decls, so
15425 putting gcc_unreachable here falls over. See PR31899. For now
15426 be conservative. */
15427 else if (!symtab->global_info_ready
15428 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15429 return *tp;
15430 else if (TREE_CODE (*tp) == VAR_DECL)
15432 varpool_node *node = varpool_node::get (*tp);
15433 if (!node || !node->definition)
15434 return *tp;
15436 else if (TREE_CODE (*tp) == FUNCTION_DECL
15437 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15439 /* The call graph machinery must have finished analyzing,
15440 optimizing and gimplifying the CU by now.
15441 So if *TP has no call graph node associated
15442 to it, it means *TP will not be emitted. */
15443 if (!cgraph_node::get (*tp))
15444 return *tp;
15446 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15447 return *tp;
15449 return NULL_TREE;
15452 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15453 for use in a later add_const_value_attribute call. */
15455 static rtx
15456 rtl_for_decl_init (tree init, tree type)
15458 rtx rtl = NULL_RTX;
15460 STRIP_NOPS (init);
15462 /* If a variable is initialized with a string constant without embedded
15463 zeros, build CONST_STRING. */
15464 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15466 tree enttype = TREE_TYPE (type);
15467 tree domain = TYPE_DOMAIN (type);
15468 machine_mode mode = TYPE_MODE (enttype);
15470 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15471 && domain
15472 && integer_zerop (TYPE_MIN_VALUE (domain))
15473 && compare_tree_int (TYPE_MAX_VALUE (domain),
15474 TREE_STRING_LENGTH (init) - 1) == 0
15475 && ((size_t) TREE_STRING_LENGTH (init)
15476 == strlen (TREE_STRING_POINTER (init)) + 1))
15478 rtl = gen_rtx_CONST_STRING (VOIDmode,
15479 ggc_strdup (TREE_STRING_POINTER (init)));
15480 rtl = gen_rtx_MEM (BLKmode, rtl);
15481 MEM_READONLY_P (rtl) = 1;
15484 /* Other aggregates, and complex values, could be represented using
15485 CONCAT: FIXME! */
15486 else if (AGGREGATE_TYPE_P (type)
15487 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15488 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15489 || TREE_CODE (type) == COMPLEX_TYPE)
15491 /* Vectors only work if their mode is supported by the target.
15492 FIXME: generic vectors ought to work too. */
15493 else if (TREE_CODE (type) == VECTOR_TYPE
15494 && !VECTOR_MODE_P (TYPE_MODE (type)))
15496 /* If the initializer is something that we know will expand into an
15497 immediate RTL constant, expand it now. We must be careful not to
15498 reference variables which won't be output. */
15499 else if (initializer_constant_valid_p (init, type)
15500 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15502 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15503 possible. */
15504 if (TREE_CODE (type) == VECTOR_TYPE)
15505 switch (TREE_CODE (init))
15507 case VECTOR_CST:
15508 break;
15509 case CONSTRUCTOR:
15510 if (TREE_CONSTANT (init))
15512 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15513 bool constant_p = true;
15514 tree value;
15515 unsigned HOST_WIDE_INT ix;
15517 /* Even when ctor is constant, it might contain non-*_CST
15518 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15519 belong into VECTOR_CST nodes. */
15520 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15521 if (!CONSTANT_CLASS_P (value))
15523 constant_p = false;
15524 break;
15527 if (constant_p)
15529 init = build_vector_from_ctor (type, elts);
15530 break;
15533 /* FALLTHRU */
15535 default:
15536 return NULL;
15539 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15541 /* If expand_expr returns a MEM, it wasn't immediate. */
15542 gcc_assert (!rtl || !MEM_P (rtl));
15545 return rtl;
15548 /* Generate RTL for the variable DECL to represent its location. */
15550 static rtx
15551 rtl_for_decl_location (tree decl)
15553 rtx rtl;
15555 /* Here we have to decide where we are going to say the parameter "lives"
15556 (as far as the debugger is concerned). We only have a couple of
15557 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15559 DECL_RTL normally indicates where the parameter lives during most of the
15560 activation of the function. If optimization is enabled however, this
15561 could be either NULL or else a pseudo-reg. Both of those cases indicate
15562 that the parameter doesn't really live anywhere (as far as the code
15563 generation parts of GCC are concerned) during most of the function's
15564 activation. That will happen (for example) if the parameter is never
15565 referenced within the function.
15567 We could just generate a location descriptor here for all non-NULL
15568 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15569 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15570 where DECL_RTL is NULL or is a pseudo-reg.
15572 Note however that we can only get away with using DECL_INCOMING_RTL as
15573 a backup substitute for DECL_RTL in certain limited cases. In cases
15574 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15575 we can be sure that the parameter was passed using the same type as it is
15576 declared to have within the function, and that its DECL_INCOMING_RTL
15577 points us to a place where a value of that type is passed.
15579 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15580 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15581 because in these cases DECL_INCOMING_RTL points us to a value of some
15582 type which is *different* from the type of the parameter itself. Thus,
15583 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15584 such cases, the debugger would end up (for example) trying to fetch a
15585 `float' from a place which actually contains the first part of a
15586 `double'. That would lead to really incorrect and confusing
15587 output at debug-time.
15589 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15590 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15591 are a couple of exceptions however. On little-endian machines we can
15592 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15593 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15594 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15595 when (on a little-endian machine) a non-prototyped function has a
15596 parameter declared to be of type `short' or `char'. In such cases,
15597 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15598 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15599 passed `int' value. If the debugger then uses that address to fetch
15600 a `short' or a `char' (on a little-endian machine) the result will be
15601 the correct data, so we allow for such exceptional cases below.
15603 Note that our goal here is to describe the place where the given formal
15604 parameter lives during most of the function's activation (i.e. between the
15605 end of the prologue and the start of the epilogue). We'll do that as best
15606 as we can. Note however that if the given formal parameter is modified
15607 sometime during the execution of the function, then a stack backtrace (at
15608 debug-time) will show the function as having been called with the *new*
15609 value rather than the value which was originally passed in. This happens
15610 rarely enough that it is not a major problem, but it *is* a problem, and
15611 I'd like to fix it.
15613 A future version of dwarf2out.c may generate two additional attributes for
15614 any given DW_TAG_formal_parameter DIE which will describe the "passed
15615 type" and the "passed location" for the given formal parameter in addition
15616 to the attributes we now generate to indicate the "declared type" and the
15617 "active location" for each parameter. This additional set of attributes
15618 could be used by debuggers for stack backtraces. Separately, note that
15619 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15620 This happens (for example) for inlined-instances of inline function formal
15621 parameters which are never referenced. This really shouldn't be
15622 happening. All PARM_DECL nodes should get valid non-NULL
15623 DECL_INCOMING_RTL values. FIXME. */
15625 /* Use DECL_RTL as the "location" unless we find something better. */
15626 rtl = DECL_RTL_IF_SET (decl);
15628 /* When generating abstract instances, ignore everything except
15629 constants, symbols living in memory, and symbols living in
15630 fixed registers. */
15631 if (! reload_completed)
15633 if (rtl
15634 && (CONSTANT_P (rtl)
15635 || (MEM_P (rtl)
15636 && CONSTANT_P (XEXP (rtl, 0)))
15637 || (REG_P (rtl)
15638 && TREE_CODE (decl) == VAR_DECL
15639 && TREE_STATIC (decl))))
15641 rtl = targetm.delegitimize_address (rtl);
15642 return rtl;
15644 rtl = NULL_RTX;
15646 else if (TREE_CODE (decl) == PARM_DECL)
15648 if (rtl == NULL_RTX
15649 || is_pseudo_reg (rtl)
15650 || (MEM_P (rtl)
15651 && is_pseudo_reg (XEXP (rtl, 0))
15652 && DECL_INCOMING_RTL (decl)
15653 && MEM_P (DECL_INCOMING_RTL (decl))
15654 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15656 tree declared_type = TREE_TYPE (decl);
15657 tree passed_type = DECL_ARG_TYPE (decl);
15658 machine_mode dmode = TYPE_MODE (declared_type);
15659 machine_mode pmode = TYPE_MODE (passed_type);
15661 /* This decl represents a formal parameter which was optimized out.
15662 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15663 all cases where (rtl == NULL_RTX) just below. */
15664 if (dmode == pmode)
15665 rtl = DECL_INCOMING_RTL (decl);
15666 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15667 && SCALAR_INT_MODE_P (dmode)
15668 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15669 && DECL_INCOMING_RTL (decl))
15671 rtx inc = DECL_INCOMING_RTL (decl);
15672 if (REG_P (inc))
15673 rtl = inc;
15674 else if (MEM_P (inc))
15676 if (BYTES_BIG_ENDIAN)
15677 rtl = adjust_address_nv (inc, dmode,
15678 GET_MODE_SIZE (pmode)
15679 - GET_MODE_SIZE (dmode));
15680 else
15681 rtl = inc;
15686 /* If the parm was passed in registers, but lives on the stack, then
15687 make a big endian correction if the mode of the type of the
15688 parameter is not the same as the mode of the rtl. */
15689 /* ??? This is the same series of checks that are made in dbxout.c before
15690 we reach the big endian correction code there. It isn't clear if all
15691 of these checks are necessary here, but keeping them all is the safe
15692 thing to do. */
15693 else if (MEM_P (rtl)
15694 && XEXP (rtl, 0) != const0_rtx
15695 && ! CONSTANT_P (XEXP (rtl, 0))
15696 /* Not passed in memory. */
15697 && !MEM_P (DECL_INCOMING_RTL (decl))
15698 /* Not passed by invisible reference. */
15699 && (!REG_P (XEXP (rtl, 0))
15700 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15701 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15702 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15703 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15704 #endif
15706 /* Big endian correction check. */
15707 && BYTES_BIG_ENDIAN
15708 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15709 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15710 < UNITS_PER_WORD))
15712 machine_mode addr_mode = get_address_mode (rtl);
15713 int offset = (UNITS_PER_WORD
15714 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15716 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15717 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15720 else if (TREE_CODE (decl) == VAR_DECL
15721 && rtl
15722 && MEM_P (rtl)
15723 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15724 && BYTES_BIG_ENDIAN)
15726 machine_mode addr_mode = get_address_mode (rtl);
15727 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15728 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15730 /* If a variable is declared "register" yet is smaller than
15731 a register, then if we store the variable to memory, it
15732 looks like we're storing a register-sized value, when in
15733 fact we are not. We need to adjust the offset of the
15734 storage location to reflect the actual value's bytes,
15735 else gdb will not be able to display it. */
15736 if (rsize > dsize)
15737 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15738 plus_constant (addr_mode, XEXP (rtl, 0),
15739 rsize - dsize));
15742 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15743 and will have been substituted directly into all expressions that use it.
15744 C does not have such a concept, but C++ and other languages do. */
15745 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15746 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15748 if (rtl)
15749 rtl = targetm.delegitimize_address (rtl);
15751 /* If we don't look past the constant pool, we risk emitting a
15752 reference to a constant pool entry that isn't referenced from
15753 code, and thus is not emitted. */
15754 if (rtl)
15755 rtl = avoid_constant_pool_reference (rtl);
15757 /* Try harder to get a rtl. If this symbol ends up not being emitted
15758 in the current CU, resolve_addr will remove the expression referencing
15759 it. */
15760 if (rtl == NULL_RTX
15761 && TREE_CODE (decl) == VAR_DECL
15762 && !DECL_EXTERNAL (decl)
15763 && TREE_STATIC (decl)
15764 && DECL_NAME (decl)
15765 && !DECL_HARD_REGISTER (decl)
15766 && DECL_MODE (decl) != VOIDmode)
15768 rtl = make_decl_rtl_for_debug (decl);
15769 if (!MEM_P (rtl)
15770 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15771 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15772 rtl = NULL_RTX;
15775 return rtl;
15778 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15779 returned. If so, the decl for the COMMON block is returned, and the
15780 value is the offset into the common block for the symbol. */
15782 static tree
15783 fortran_common (tree decl, HOST_WIDE_INT *value)
15785 tree val_expr, cvar;
15786 machine_mode mode;
15787 HOST_WIDE_INT bitsize, bitpos;
15788 tree offset;
15789 int unsignedp, volatilep = 0;
15791 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15792 it does not have a value (the offset into the common area), or if it
15793 is thread local (as opposed to global) then it isn't common, and shouldn't
15794 be handled as such. */
15795 if (TREE_CODE (decl) != VAR_DECL
15796 || !TREE_STATIC (decl)
15797 || !DECL_HAS_VALUE_EXPR_P (decl)
15798 || !is_fortran ())
15799 return NULL_TREE;
15801 val_expr = DECL_VALUE_EXPR (decl);
15802 if (TREE_CODE (val_expr) != COMPONENT_REF)
15803 return NULL_TREE;
15805 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15806 &mode, &unsignedp, &volatilep, true);
15808 if (cvar == NULL_TREE
15809 || TREE_CODE (cvar) != VAR_DECL
15810 || DECL_ARTIFICIAL (cvar)
15811 || !TREE_PUBLIC (cvar))
15812 return NULL_TREE;
15814 *value = 0;
15815 if (offset != NULL)
15817 if (!tree_fits_shwi_p (offset))
15818 return NULL_TREE;
15819 *value = tree_to_shwi (offset);
15821 if (bitpos != 0)
15822 *value += bitpos / BITS_PER_UNIT;
15824 return cvar;
15827 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15828 data attribute for a variable or a parameter. We generate the
15829 DW_AT_const_value attribute only in those cases where the given variable
15830 or parameter does not have a true "location" either in memory or in a
15831 register. This can happen (for example) when a constant is passed as an
15832 actual argument in a call to an inline function. (It's possible that
15833 these things can crop up in other ways also.) Note that one type of
15834 constant value which can be passed into an inlined function is a constant
15835 pointer. This can happen for example if an actual argument in an inlined
15836 function call evaluates to a compile-time constant address.
15838 CACHE_P is true if it is worth caching the location list for DECL,
15839 so that future calls can reuse it rather than regenerate it from scratch.
15840 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15841 since we will need to refer to them each time the function is inlined. */
15843 static bool
15844 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15845 enum dwarf_attribute attr)
15847 rtx rtl;
15848 dw_loc_list_ref list;
15849 var_loc_list *loc_list;
15850 cached_dw_loc_list *cache;
15852 if (TREE_CODE (decl) == ERROR_MARK)
15853 return false;
15855 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15856 || TREE_CODE (decl) == RESULT_DECL);
15858 /* Try to get some constant RTL for this decl, and use that as the value of
15859 the location. */
15861 rtl = rtl_for_decl_location (decl);
15862 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15863 && add_const_value_attribute (die, rtl))
15864 return true;
15866 /* See if we have single element location list that is equivalent to
15867 a constant value. That way we are better to use add_const_value_attribute
15868 rather than expanding constant value equivalent. */
15869 loc_list = lookup_decl_loc (decl);
15870 if (loc_list
15871 && loc_list->first
15872 && loc_list->first->next == NULL
15873 && NOTE_P (loc_list->first->loc)
15874 && NOTE_VAR_LOCATION (loc_list->first->loc)
15875 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15877 struct var_loc_node *node;
15879 node = loc_list->first;
15880 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15881 if (GET_CODE (rtl) == EXPR_LIST)
15882 rtl = XEXP (rtl, 0);
15883 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15884 && add_const_value_attribute (die, rtl))
15885 return true;
15887 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15888 list several times. See if we've already cached the contents. */
15889 list = NULL;
15890 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15891 cache_p = false;
15892 if (cache_p)
15894 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
15895 if (cache)
15896 list = cache->loc_list;
15898 if (list == NULL)
15900 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15901 /* It is usually worth caching this result if the decl is from
15902 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15903 if (cache_p && list && list->dw_loc_next)
15905 cached_dw_loc_list **slot
15906 = cached_dw_loc_list_table->find_slot_with_hash (decl,
15907 DECL_UID (decl),
15908 INSERT);
15909 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15910 cache->decl_id = DECL_UID (decl);
15911 cache->loc_list = list;
15912 *slot = cache;
15915 if (list)
15917 add_AT_location_description (die, attr, list);
15918 return true;
15920 /* None of that worked, so it must not really have a location;
15921 try adding a constant value attribute from the DECL_INITIAL. */
15922 return tree_add_const_value_attribute_for_decl (die, decl);
15925 /* Add VARIABLE and DIE into deferred locations list. */
15927 static void
15928 defer_location (tree variable, dw_die_ref die)
15930 deferred_locations entry;
15931 entry.variable = variable;
15932 entry.die = die;
15933 vec_safe_push (deferred_locations_list, entry);
15936 /* Helper function for tree_add_const_value_attribute. Natively encode
15937 initializer INIT into an array. Return true if successful. */
15939 static bool
15940 native_encode_initializer (tree init, unsigned char *array, int size)
15942 tree type;
15944 if (init == NULL_TREE)
15945 return false;
15947 STRIP_NOPS (init);
15948 switch (TREE_CODE (init))
15950 case STRING_CST:
15951 type = TREE_TYPE (init);
15952 if (TREE_CODE (type) == ARRAY_TYPE)
15954 tree enttype = TREE_TYPE (type);
15955 machine_mode mode = TYPE_MODE (enttype);
15957 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15958 return false;
15959 if (int_size_in_bytes (type) != size)
15960 return false;
15961 if (size > TREE_STRING_LENGTH (init))
15963 memcpy (array, TREE_STRING_POINTER (init),
15964 TREE_STRING_LENGTH (init));
15965 memset (array + TREE_STRING_LENGTH (init),
15966 '\0', size - TREE_STRING_LENGTH (init));
15968 else
15969 memcpy (array, TREE_STRING_POINTER (init), size);
15970 return true;
15972 return false;
15973 case CONSTRUCTOR:
15974 type = TREE_TYPE (init);
15975 if (int_size_in_bytes (type) != size)
15976 return false;
15977 if (TREE_CODE (type) == ARRAY_TYPE)
15979 HOST_WIDE_INT min_index;
15980 unsigned HOST_WIDE_INT cnt;
15981 int curpos = 0, fieldsize;
15982 constructor_elt *ce;
15984 if (TYPE_DOMAIN (type) == NULL_TREE
15985 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15986 return false;
15988 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15989 if (fieldsize <= 0)
15990 return false;
15992 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15993 memset (array, '\0', size);
15994 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15996 tree val = ce->value;
15997 tree index = ce->index;
15998 int pos = curpos;
15999 if (index && TREE_CODE (index) == RANGE_EXPR)
16000 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16001 * fieldsize;
16002 else if (index)
16003 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16005 if (val)
16007 STRIP_NOPS (val);
16008 if (!native_encode_initializer (val, array + pos, fieldsize))
16009 return false;
16011 curpos = pos + fieldsize;
16012 if (index && TREE_CODE (index) == RANGE_EXPR)
16014 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16015 - tree_to_shwi (TREE_OPERAND (index, 0));
16016 while (count-- > 0)
16018 if (val)
16019 memcpy (array + curpos, array + pos, fieldsize);
16020 curpos += fieldsize;
16023 gcc_assert (curpos <= size);
16025 return true;
16027 else if (TREE_CODE (type) == RECORD_TYPE
16028 || TREE_CODE (type) == UNION_TYPE)
16030 tree field = NULL_TREE;
16031 unsigned HOST_WIDE_INT cnt;
16032 constructor_elt *ce;
16034 if (int_size_in_bytes (type) != size)
16035 return false;
16037 if (TREE_CODE (type) == RECORD_TYPE)
16038 field = TYPE_FIELDS (type);
16040 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16042 tree val = ce->value;
16043 int pos, fieldsize;
16045 if (ce->index != 0)
16046 field = ce->index;
16048 if (val)
16049 STRIP_NOPS (val);
16051 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16052 return false;
16054 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16055 && TYPE_DOMAIN (TREE_TYPE (field))
16056 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16057 return false;
16058 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16059 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16060 return false;
16061 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16062 pos = int_byte_position (field);
16063 gcc_assert (pos + fieldsize <= size);
16064 if (val
16065 && !native_encode_initializer (val, array + pos, fieldsize))
16066 return false;
16068 return true;
16070 return false;
16071 case VIEW_CONVERT_EXPR:
16072 case NON_LVALUE_EXPR:
16073 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16074 default:
16075 return native_encode_expr (init, array, size) == size;
16079 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16080 attribute is the const value T. */
16082 static bool
16083 tree_add_const_value_attribute (dw_die_ref die, tree t)
16085 tree init;
16086 tree type = TREE_TYPE (t);
16087 rtx rtl;
16089 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16090 return false;
16092 init = t;
16093 gcc_assert (!DECL_P (init));
16095 rtl = rtl_for_decl_init (init, type);
16096 if (rtl)
16097 return add_const_value_attribute (die, rtl);
16098 /* If the host and target are sane, try harder. */
16099 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16100 && initializer_constant_valid_p (init, type))
16102 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16103 if (size > 0 && (int) size == size)
16105 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16107 if (native_encode_initializer (init, array, size))
16109 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16110 return true;
16112 ggc_free (array);
16115 return false;
16118 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16119 attribute is the const value of T, where T is an integral constant
16120 variable with static storage duration
16121 (so it can't be a PARM_DECL or a RESULT_DECL). */
16123 static bool
16124 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16127 if (!decl
16128 || (TREE_CODE (decl) != VAR_DECL
16129 && TREE_CODE (decl) != CONST_DECL)
16130 || (TREE_CODE (decl) == VAR_DECL
16131 && !TREE_STATIC (decl)))
16132 return false;
16134 if (TREE_READONLY (decl)
16135 && ! TREE_THIS_VOLATILE (decl)
16136 && DECL_INITIAL (decl))
16137 /* OK */;
16138 else
16139 return false;
16141 /* Don't add DW_AT_const_value if abstract origin already has one. */
16142 if (get_AT (var_die, DW_AT_const_value))
16143 return false;
16145 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16148 /* Convert the CFI instructions for the current function into a
16149 location list. This is used for DW_AT_frame_base when we targeting
16150 a dwarf2 consumer that does not support the dwarf3
16151 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16152 expressions. */
16154 static dw_loc_list_ref
16155 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16157 int ix;
16158 dw_fde_ref fde;
16159 dw_loc_list_ref list, *list_tail;
16160 dw_cfi_ref cfi;
16161 dw_cfa_location last_cfa, next_cfa;
16162 const char *start_label, *last_label, *section;
16163 dw_cfa_location remember;
16165 fde = cfun->fde;
16166 gcc_assert (fde != NULL);
16168 section = secname_for_decl (current_function_decl);
16169 list_tail = &list;
16170 list = NULL;
16172 memset (&next_cfa, 0, sizeof (next_cfa));
16173 next_cfa.reg = INVALID_REGNUM;
16174 remember = next_cfa;
16176 start_label = fde->dw_fde_begin;
16178 /* ??? Bald assumption that the CIE opcode list does not contain
16179 advance opcodes. */
16180 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16181 lookup_cfa_1 (cfi, &next_cfa, &remember);
16183 last_cfa = next_cfa;
16184 last_label = start_label;
16186 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16188 /* If the first partition contained no CFI adjustments, the
16189 CIE opcodes apply to the whole first partition. */
16190 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16191 fde->dw_fde_begin, fde->dw_fde_end, section);
16192 list_tail =&(*list_tail)->dw_loc_next;
16193 start_label = last_label = fde->dw_fde_second_begin;
16196 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16198 switch (cfi->dw_cfi_opc)
16200 case DW_CFA_set_loc:
16201 case DW_CFA_advance_loc1:
16202 case DW_CFA_advance_loc2:
16203 case DW_CFA_advance_loc4:
16204 if (!cfa_equal_p (&last_cfa, &next_cfa))
16206 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16207 start_label, last_label, section);
16209 list_tail = &(*list_tail)->dw_loc_next;
16210 last_cfa = next_cfa;
16211 start_label = last_label;
16213 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16214 break;
16216 case DW_CFA_advance_loc:
16217 /* The encoding is complex enough that we should never emit this. */
16218 gcc_unreachable ();
16220 default:
16221 lookup_cfa_1 (cfi, &next_cfa, &remember);
16222 break;
16224 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16226 if (!cfa_equal_p (&last_cfa, &next_cfa))
16228 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16229 start_label, last_label, section);
16231 list_tail = &(*list_tail)->dw_loc_next;
16232 last_cfa = next_cfa;
16233 start_label = last_label;
16235 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16236 start_label, fde->dw_fde_end, section);
16237 list_tail = &(*list_tail)->dw_loc_next;
16238 start_label = last_label = fde->dw_fde_second_begin;
16242 if (!cfa_equal_p (&last_cfa, &next_cfa))
16244 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16245 start_label, last_label, section);
16246 list_tail = &(*list_tail)->dw_loc_next;
16247 start_label = last_label;
16250 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16251 start_label,
16252 fde->dw_fde_second_begin
16253 ? fde->dw_fde_second_end : fde->dw_fde_end,
16254 section);
16256 if (list && list->dw_loc_next)
16257 gen_llsym (list);
16259 return list;
16262 /* Compute a displacement from the "steady-state frame pointer" to the
16263 frame base (often the same as the CFA), and store it in
16264 frame_pointer_fb_offset. OFFSET is added to the displacement
16265 before the latter is negated. */
16267 static void
16268 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16270 rtx reg, elim;
16272 #ifdef FRAME_POINTER_CFA_OFFSET
16273 reg = frame_pointer_rtx;
16274 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16275 #else
16276 reg = arg_pointer_rtx;
16277 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16278 #endif
16280 elim = (ira_use_lra_p
16281 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16282 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16283 if (GET_CODE (elim) == PLUS)
16285 offset += INTVAL (XEXP (elim, 1));
16286 elim = XEXP (elim, 0);
16289 frame_pointer_fb_offset = -offset;
16291 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16292 in which to eliminate. This is because it's stack pointer isn't
16293 directly accessible as a register within the ISA. To work around
16294 this, assume that while we cannot provide a proper value for
16295 frame_pointer_fb_offset, we won't need one either. */
16296 frame_pointer_fb_offset_valid
16297 = ((SUPPORTS_STACK_ALIGNMENT
16298 && (elim == hard_frame_pointer_rtx
16299 || elim == stack_pointer_rtx))
16300 || elim == (frame_pointer_needed
16301 ? hard_frame_pointer_rtx
16302 : stack_pointer_rtx));
16305 /* Generate a DW_AT_name attribute given some string value to be included as
16306 the value of the attribute. */
16308 static void
16309 add_name_attribute (dw_die_ref die, const char *name_string)
16311 if (name_string != NULL && *name_string != 0)
16313 if (demangle_name_func)
16314 name_string = (*demangle_name_func) (name_string);
16316 add_AT_string (die, DW_AT_name, name_string);
16320 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16321 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16322 of TYPE accordingly.
16324 ??? This is a temporary measure until after we're able to generate
16325 regular DWARF for the complex Ada type system. */
16327 static void
16328 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16329 dw_die_ref context_die)
16331 tree dtype;
16332 dw_die_ref dtype_die;
16334 if (!lang_hooks.types.descriptive_type)
16335 return;
16337 dtype = lang_hooks.types.descriptive_type (type);
16338 if (!dtype)
16339 return;
16341 dtype_die = lookup_type_die (dtype);
16342 if (!dtype_die)
16344 gen_type_die (dtype, context_die);
16345 dtype_die = lookup_type_die (dtype);
16346 gcc_assert (dtype_die);
16349 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16352 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16354 static const char *
16355 comp_dir_string (void)
16357 const char *wd;
16358 char *wd1;
16359 static const char *cached_wd = NULL;
16361 if (cached_wd != NULL)
16362 return cached_wd;
16364 wd = get_src_pwd ();
16365 if (wd == NULL)
16366 return NULL;
16368 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16370 int wdlen;
16372 wdlen = strlen (wd);
16373 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16374 strcpy (wd1, wd);
16375 wd1 [wdlen] = DIR_SEPARATOR;
16376 wd1 [wdlen + 1] = 0;
16377 wd = wd1;
16380 cached_wd = remap_debug_filename (wd);
16381 return cached_wd;
16384 /* Generate a DW_AT_comp_dir attribute for DIE. */
16386 static void
16387 add_comp_dir_attribute (dw_die_ref die)
16389 const char * wd = comp_dir_string ();
16390 if (wd != NULL)
16391 add_AT_string (die, DW_AT_comp_dir, wd);
16394 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16395 default. */
16397 static int
16398 lower_bound_default (void)
16400 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16402 case DW_LANG_C:
16403 case DW_LANG_C89:
16404 case DW_LANG_C99:
16405 case DW_LANG_C_plus_plus:
16406 case DW_LANG_ObjC:
16407 case DW_LANG_ObjC_plus_plus:
16408 case DW_LANG_Java:
16409 return 0;
16410 case DW_LANG_Fortran77:
16411 case DW_LANG_Fortran90:
16412 case DW_LANG_Fortran95:
16413 return 1;
16414 case DW_LANG_UPC:
16415 case DW_LANG_D:
16416 case DW_LANG_Python:
16417 return dwarf_version >= 4 ? 0 : -1;
16418 case DW_LANG_Ada95:
16419 case DW_LANG_Ada83:
16420 case DW_LANG_Cobol74:
16421 case DW_LANG_Cobol85:
16422 case DW_LANG_Pascal83:
16423 case DW_LANG_Modula2:
16424 case DW_LANG_PLI:
16425 return dwarf_version >= 4 ? 1 : -1;
16426 default:
16427 return -1;
16431 /* Given a tree node describing an array bound (either lower or upper) output
16432 a representation for that bound. */
16434 static void
16435 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16437 switch (TREE_CODE (bound))
16439 case ERROR_MARK:
16440 return;
16442 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16443 case INTEGER_CST:
16445 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16446 int dflt;
16448 /* Use the default if possible. */
16449 if (bound_attr == DW_AT_lower_bound
16450 && tree_fits_shwi_p (bound)
16451 && (dflt = lower_bound_default ()) != -1
16452 && tree_to_shwi (bound) == dflt)
16455 /* If HOST_WIDE_INT is big enough then represent the bound as
16456 a constant value. We need to choose a form based on
16457 whether the type is signed or unsigned. We cannot just
16458 call add_AT_unsigned if the value itself is positive
16459 (add_AT_unsigned might add the unsigned value encoded as
16460 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16461 bounds type and then sign extend any unsigned values found
16462 for signed types. This is needed only for
16463 DW_AT_{lower,upper}_bound, since for most other attributes,
16464 consumers will treat DW_FORM_data[1248] as unsigned values,
16465 regardless of the underlying type. */
16466 else if (prec <= HOST_BITS_PER_WIDE_INT
16467 || tree_fits_uhwi_p (bound))
16469 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16470 add_AT_unsigned (subrange_die, bound_attr,
16471 TREE_INT_CST_LOW (bound));
16472 else
16473 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16475 else
16476 /* Otherwise represent the bound as an unsigned value with
16477 the precision of its type. The precision and signedness
16478 of the type will be necessary to re-interpret it
16479 unambiguously. */
16480 add_AT_wide (subrange_die, bound_attr, bound);
16482 break;
16484 CASE_CONVERT:
16485 case VIEW_CONVERT_EXPR:
16486 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16487 break;
16489 case SAVE_EXPR:
16490 break;
16492 case VAR_DECL:
16493 case PARM_DECL:
16494 case RESULT_DECL:
16496 dw_die_ref decl_die = lookup_decl_die (bound);
16498 /* ??? Can this happen, or should the variable have been bound
16499 first? Probably it can, since I imagine that we try to create
16500 the types of parameters in the order in which they exist in
16501 the list, and won't have created a forward reference to a
16502 later parameter. */
16503 if (decl_die != NULL)
16505 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16506 break;
16509 /* FALLTHRU */
16511 default:
16513 /* Otherwise try to create a stack operation procedure to
16514 evaluate the value of the array bound. */
16516 dw_die_ref ctx, decl_die;
16517 dw_loc_list_ref list;
16519 list = loc_list_from_tree (bound, 2);
16520 if (list == NULL || single_element_loc_list_p (list))
16522 /* If DW_AT_*bound is not a reference nor constant, it is
16523 a DWARF expression rather than location description.
16524 For that loc_list_from_tree (bound, 0) is needed.
16525 If that fails to give a single element list,
16526 fall back to outputting this as a reference anyway. */
16527 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16528 if (list2 && single_element_loc_list_p (list2))
16530 add_AT_loc (subrange_die, bound_attr, list2->expr);
16531 break;
16534 if (list == NULL)
16535 break;
16537 if (current_function_decl == 0)
16538 ctx = comp_unit_die ();
16539 else
16540 ctx = lookup_decl_die (current_function_decl);
16542 decl_die = new_die (DW_TAG_variable, ctx, bound);
16543 add_AT_flag (decl_die, DW_AT_artificial, 1);
16544 add_type_attribute (decl_die, TREE_TYPE (bound), TYPE_QUAL_CONST, ctx);
16545 add_AT_location_description (decl_die, DW_AT_location, list);
16546 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16547 break;
16552 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16553 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16554 Note that the block of subscript information for an array type also
16555 includes information about the element type of the given array type. */
16557 static void
16558 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16560 unsigned dimension_number;
16561 tree lower, upper;
16562 dw_die_ref subrange_die;
16564 for (dimension_number = 0;
16565 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16566 type = TREE_TYPE (type), dimension_number++)
16568 tree domain = TYPE_DOMAIN (type);
16570 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16571 break;
16573 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16574 and (in GNU C only) variable bounds. Handle all three forms
16575 here. */
16576 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16577 if (domain)
16579 /* We have an array type with specified bounds. */
16580 lower = TYPE_MIN_VALUE (domain);
16581 upper = TYPE_MAX_VALUE (domain);
16583 /* Define the index type. */
16584 if (TREE_TYPE (domain))
16586 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16587 TREE_TYPE field. We can't emit debug info for this
16588 because it is an unnamed integral type. */
16589 if (TREE_CODE (domain) == INTEGER_TYPE
16590 && TYPE_NAME (domain) == NULL_TREE
16591 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16592 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16594 else
16595 add_type_attribute (subrange_die, TREE_TYPE (domain),
16596 TYPE_UNQUALIFIED, type_die);
16599 /* ??? If upper is NULL, the array has unspecified length,
16600 but it does have a lower bound. This happens with Fortran
16601 dimension arr(N:*)
16602 Since the debugger is definitely going to need to know N
16603 to produce useful results, go ahead and output the lower
16604 bound solo, and hope the debugger can cope. */
16606 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16607 if (upper)
16608 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16611 /* Otherwise we have an array type with an unspecified length. The
16612 DWARF-2 spec does not say how to handle this; let's just leave out the
16613 bounds. */
16617 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16619 static void
16620 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16622 dw_die_ref decl_die;
16623 HOST_WIDE_INT size;
16625 switch (TREE_CODE (tree_node))
16627 case ERROR_MARK:
16628 size = 0;
16629 break;
16630 case ENUMERAL_TYPE:
16631 case RECORD_TYPE:
16632 case UNION_TYPE:
16633 case QUAL_UNION_TYPE:
16634 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16635 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16637 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16638 return;
16640 size = int_size_in_bytes (tree_node);
16641 break;
16642 case FIELD_DECL:
16643 /* For a data member of a struct or union, the DW_AT_byte_size is
16644 generally given as the number of bytes normally allocated for an
16645 object of the *declared* type of the member itself. This is true
16646 even for bit-fields. */
16647 size = int_size_in_bytes (field_type (tree_node));
16648 break;
16649 default:
16650 gcc_unreachable ();
16653 /* Note that `size' might be -1 when we get to this point. If it is, that
16654 indicates that the byte size of the entity in question is variable. We
16655 have no good way of expressing this fact in Dwarf at the present time,
16656 when location description was not used by the caller code instead. */
16657 if (size >= 0)
16658 add_AT_unsigned (die, DW_AT_byte_size, size);
16661 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16662 which specifies the distance in bits from the highest order bit of the
16663 "containing object" for the bit-field to the highest order bit of the
16664 bit-field itself.
16666 For any given bit-field, the "containing object" is a hypothetical object
16667 (of some integral or enum type) within which the given bit-field lives. The
16668 type of this hypothetical "containing object" is always the same as the
16669 declared type of the individual bit-field itself. The determination of the
16670 exact location of the "containing object" for a bit-field is rather
16671 complicated. It's handled by the `field_byte_offset' function (above).
16673 Note that it is the size (in bytes) of the hypothetical "containing object"
16674 which will be given in the DW_AT_byte_size attribute for this bit-field.
16675 (See `byte_size_attribute' above). */
16677 static inline void
16678 add_bit_offset_attribute (dw_die_ref die, tree decl)
16680 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16681 tree type = DECL_BIT_FIELD_TYPE (decl);
16682 HOST_WIDE_INT bitpos_int;
16683 HOST_WIDE_INT highest_order_object_bit_offset;
16684 HOST_WIDE_INT highest_order_field_bit_offset;
16685 HOST_WIDE_INT bit_offset;
16687 /* Must be a field and a bit field. */
16688 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16690 /* We can't yet handle bit-fields whose offsets are variable, so if we
16691 encounter such things, just return without generating any attribute
16692 whatsoever. Likewise for variable or too large size. */
16693 if (! tree_fits_shwi_p (bit_position (decl))
16694 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16695 return;
16697 bitpos_int = int_bit_position (decl);
16699 /* Note that the bit offset is always the distance (in bits) from the
16700 highest-order bit of the "containing object" to the highest-order bit of
16701 the bit-field itself. Since the "high-order end" of any object or field
16702 is different on big-endian and little-endian machines, the computation
16703 below must take account of these differences. */
16704 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16705 highest_order_field_bit_offset = bitpos_int;
16707 if (! BYTES_BIG_ENDIAN)
16709 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16710 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16713 bit_offset
16714 = (! BYTES_BIG_ENDIAN
16715 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16716 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16718 if (bit_offset < 0)
16719 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16720 else
16721 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16724 /* For a FIELD_DECL node which represents a bit field, output an attribute
16725 which specifies the length in bits of the given field. */
16727 static inline void
16728 add_bit_size_attribute (dw_die_ref die, tree decl)
16730 /* Must be a field and a bit field. */
16731 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16732 && DECL_BIT_FIELD_TYPE (decl));
16734 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16735 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16738 /* If the compiled language is ANSI C, then add a 'prototyped'
16739 attribute, if arg types are given for the parameters of a function. */
16741 static inline void
16742 add_prototyped_attribute (dw_die_ref die, tree func_type)
16744 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16746 case DW_LANG_C:
16747 case DW_LANG_C89:
16748 case DW_LANG_C99:
16749 case DW_LANG_ObjC:
16750 if (prototype_p (func_type))
16751 add_AT_flag (die, DW_AT_prototyped, 1);
16752 break;
16753 default:
16754 break;
16758 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16759 by looking in either the type declaration or object declaration
16760 equate table. */
16762 static inline dw_die_ref
16763 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16765 dw_die_ref origin_die = NULL;
16767 if (TREE_CODE (origin) != FUNCTION_DECL)
16769 /* We may have gotten separated from the block for the inlined
16770 function, if we're in an exception handler or some such; make
16771 sure that the abstract function has been written out.
16773 Doing this for nested functions is wrong, however; functions are
16774 distinct units, and our context might not even be inline. */
16775 tree fn = origin;
16777 if (TYPE_P (fn))
16778 fn = TYPE_STUB_DECL (fn);
16780 fn = decl_function_context (fn);
16781 if (fn)
16782 dwarf2out_abstract_function (fn);
16785 if (DECL_P (origin))
16786 origin_die = lookup_decl_die (origin);
16787 else if (TYPE_P (origin))
16788 origin_die = lookup_type_die (origin);
16790 /* XXX: Functions that are never lowered don't always have correct block
16791 trees (in the case of java, they simply have no block tree, in some other
16792 languages). For these functions, there is nothing we can really do to
16793 output correct debug info for inlined functions in all cases. Rather
16794 than die, we'll just produce deficient debug info now, in that we will
16795 have variables without a proper abstract origin. In the future, when all
16796 functions are lowered, we should re-add a gcc_assert (origin_die)
16797 here. */
16799 if (origin_die)
16800 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16801 return origin_die;
16804 /* We do not currently support the pure_virtual attribute. */
16806 static inline void
16807 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16809 if (DECL_VINDEX (func_decl))
16811 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16813 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16814 add_AT_loc (die, DW_AT_vtable_elem_location,
16815 new_loc_descr (DW_OP_constu,
16816 tree_to_shwi (DECL_VINDEX (func_decl)),
16817 0));
16819 /* GNU extension: Record what type this method came from originally. */
16820 if (debug_info_level > DINFO_LEVEL_TERSE
16821 && DECL_CONTEXT (func_decl))
16822 add_AT_die_ref (die, DW_AT_containing_type,
16823 lookup_type_die (DECL_CONTEXT (func_decl)));
16827 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16828 given decl. This used to be a vendor extension until after DWARF 4
16829 standardized it. */
16831 static void
16832 add_linkage_attr (dw_die_ref die, tree decl)
16834 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16836 /* Mimic what assemble_name_raw does with a leading '*'. */
16837 if (name[0] == '*')
16838 name = &name[1];
16840 if (dwarf_version >= 4)
16841 add_AT_string (die, DW_AT_linkage_name, name);
16842 else
16843 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16846 /* Add source coordinate attributes for the given decl. */
16848 static void
16849 add_src_coords_attributes (dw_die_ref die, tree decl)
16851 expanded_location s;
16853 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16854 return;
16855 s = expand_location (DECL_SOURCE_LOCATION (decl));
16856 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16857 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16860 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16862 static void
16863 add_linkage_name (dw_die_ref die, tree decl)
16865 if (debug_info_level > DINFO_LEVEL_NONE
16866 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16867 && TREE_PUBLIC (decl)
16868 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16869 && die->die_tag != DW_TAG_member)
16871 /* Defer until we have an assembler name set. */
16872 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16874 limbo_die_node *asm_name;
16876 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16877 asm_name->die = die;
16878 asm_name->created_for = decl;
16879 asm_name->next = deferred_asm_name;
16880 deferred_asm_name = asm_name;
16882 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16883 add_linkage_attr (die, decl);
16887 /* Add a DW_AT_name attribute and source coordinate attribute for the
16888 given decl, but only if it actually has a name. */
16890 static void
16891 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16893 tree decl_name;
16895 decl_name = DECL_NAME (decl);
16896 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16898 const char *name = dwarf2_name (decl, 0);
16899 if (name)
16900 add_name_attribute (die, name);
16901 if (! DECL_ARTIFICIAL (decl))
16902 add_src_coords_attributes (die, decl);
16904 add_linkage_name (die, decl);
16907 #ifdef VMS_DEBUGGING_INFO
16908 /* Get the function's name, as described by its RTL. This may be different
16909 from the DECL_NAME name used in the source file. */
16910 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16912 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16913 XEXP (DECL_RTL (decl), 0), false);
16914 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16916 #endif /* VMS_DEBUGGING_INFO */
16919 #ifdef VMS_DEBUGGING_INFO
16920 /* Output the debug main pointer die for VMS */
16922 void
16923 dwarf2out_vms_debug_main_pointer (void)
16925 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16926 dw_die_ref die;
16928 /* Allocate the VMS debug main subprogram die. */
16929 die = ggc_cleared_alloc<die_node> ();
16930 die->die_tag = DW_TAG_subprogram;
16931 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16932 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16933 current_function_funcdef_no);
16934 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16936 /* Make it the first child of comp_unit_die (). */
16937 die->die_parent = comp_unit_die ();
16938 if (comp_unit_die ()->die_child)
16940 die->die_sib = comp_unit_die ()->die_child->die_sib;
16941 comp_unit_die ()->die_child->die_sib = die;
16943 else
16945 die->die_sib = die;
16946 comp_unit_die ()->die_child = die;
16949 #endif /* VMS_DEBUGGING_INFO */
16951 /* Push a new declaration scope. */
16953 static void
16954 push_decl_scope (tree scope)
16956 vec_safe_push (decl_scope_table, scope);
16959 /* Pop a declaration scope. */
16961 static inline void
16962 pop_decl_scope (void)
16964 decl_scope_table->pop ();
16967 /* walk_tree helper function for uses_local_type, below. */
16969 static tree
16970 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16972 if (!TYPE_P (*tp))
16973 *walk_subtrees = 0;
16974 else
16976 tree name = TYPE_NAME (*tp);
16977 if (name && DECL_P (name) && decl_function_context (name))
16978 return *tp;
16980 return NULL_TREE;
16983 /* If TYPE involves a function-local type (including a local typedef to a
16984 non-local type), returns that type; otherwise returns NULL_TREE. */
16986 static tree
16987 uses_local_type (tree type)
16989 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16990 return used;
16993 /* Return the DIE for the scope that immediately contains this type.
16994 Non-named types that do not involve a function-local type get global
16995 scope. Named types nested in namespaces or other types get their
16996 containing scope. All other types (i.e. function-local named types) get
16997 the current active scope. */
16999 static dw_die_ref
17000 scope_die_for (tree t, dw_die_ref context_die)
17002 dw_die_ref scope_die = NULL;
17003 tree containing_scope;
17005 /* Non-types always go in the current scope. */
17006 gcc_assert (TYPE_P (t));
17008 /* Use the scope of the typedef, rather than the scope of the type
17009 it refers to. */
17010 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17011 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17012 else
17013 containing_scope = TYPE_CONTEXT (t);
17015 /* Use the containing namespace if there is one. */
17016 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17018 if (context_die == lookup_decl_die (containing_scope))
17019 /* OK */;
17020 else if (debug_info_level > DINFO_LEVEL_TERSE)
17021 context_die = get_context_die (containing_scope);
17022 else
17023 containing_scope = NULL_TREE;
17026 /* Ignore function type "scopes" from the C frontend. They mean that
17027 a tagged type is local to a parmlist of a function declarator, but
17028 that isn't useful to DWARF. */
17029 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17030 containing_scope = NULL_TREE;
17032 if (SCOPE_FILE_SCOPE_P (containing_scope))
17034 /* If T uses a local type keep it local as well, to avoid references
17035 to function-local DIEs from outside the function. */
17036 if (current_function_decl && uses_local_type (t))
17037 scope_die = context_die;
17038 else
17039 scope_die = comp_unit_die ();
17041 else if (TYPE_P (containing_scope))
17043 /* For types, we can just look up the appropriate DIE. */
17044 if (debug_info_level > DINFO_LEVEL_TERSE)
17045 scope_die = get_context_die (containing_scope);
17046 else
17048 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17049 if (scope_die == NULL)
17050 scope_die = comp_unit_die ();
17053 else
17054 scope_die = context_die;
17056 return scope_die;
17059 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17061 static inline int
17062 local_scope_p (dw_die_ref context_die)
17064 for (; context_die; context_die = context_die->die_parent)
17065 if (context_die->die_tag == DW_TAG_inlined_subroutine
17066 || context_die->die_tag == DW_TAG_subprogram)
17067 return 1;
17069 return 0;
17072 /* Returns nonzero if CONTEXT_DIE is a class. */
17074 static inline int
17075 class_scope_p (dw_die_ref context_die)
17077 return (context_die
17078 && (context_die->die_tag == DW_TAG_structure_type
17079 || context_die->die_tag == DW_TAG_class_type
17080 || context_die->die_tag == DW_TAG_interface_type
17081 || context_die->die_tag == DW_TAG_union_type));
17084 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17085 whether or not to treat a DIE in this context as a declaration. */
17087 static inline int
17088 class_or_namespace_scope_p (dw_die_ref context_die)
17090 return (class_scope_p (context_die)
17091 || (context_die && context_die->die_tag == DW_TAG_namespace));
17094 /* Many forms of DIEs require a "type description" attribute. This
17095 routine locates the proper "type descriptor" die for the type given
17096 by 'type' plus any additional qualifiers given by 'cv_quals', and
17097 adds a DW_AT_type attribute below the given die. */
17099 static void
17100 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17101 dw_die_ref context_die)
17103 enum tree_code code = TREE_CODE (type);
17104 dw_die_ref type_die = NULL;
17106 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17107 or fixed-point type, use the inner type. This is because we have no
17108 support for unnamed types in base_type_die. This can happen if this is
17109 an Ada subrange type. Correct solution is emit a subrange type die. */
17110 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17111 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17112 type = TREE_TYPE (type), code = TREE_CODE (type);
17114 if (code == ERROR_MARK
17115 /* Handle a special case. For functions whose return type is void, we
17116 generate *no* type attribute. (Note that no object may have type
17117 `void', so this only applies to function return types). */
17118 || code == VOID_TYPE)
17119 return;
17121 type_die = modified_type_die (type,
17122 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17123 context_die);
17125 if (type_die != NULL)
17126 add_AT_die_ref (object_die, DW_AT_type, type_die);
17129 /* Given an object die, add the calling convention attribute for the
17130 function call type. */
17131 static void
17132 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17134 enum dwarf_calling_convention value = DW_CC_normal;
17136 value = ((enum dwarf_calling_convention)
17137 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17139 if (is_fortran ()
17140 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17142 /* DWARF 2 doesn't provide a way to identify a program's source-level
17143 entry point. DW_AT_calling_convention attributes are only meant
17144 to describe functions' calling conventions. However, lacking a
17145 better way to signal the Fortran main program, we used this for
17146 a long time, following existing custom. Now, DWARF 4 has
17147 DW_AT_main_subprogram, which we add below, but some tools still
17148 rely on the old way, which we thus keep. */
17149 value = DW_CC_program;
17151 if (dwarf_version >= 4 || !dwarf_strict)
17152 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17155 /* Only add the attribute if the backend requests it, and
17156 is not DW_CC_normal. */
17157 if (value && (value != DW_CC_normal))
17158 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17161 /* Given a tree pointer to a struct, class, union, or enum type node, return
17162 a pointer to the (string) tag name for the given type, or zero if the type
17163 was declared without a tag. */
17165 static const char *
17166 type_tag (const_tree type)
17168 const char *name = 0;
17170 if (TYPE_NAME (type) != 0)
17172 tree t = 0;
17174 /* Find the IDENTIFIER_NODE for the type name. */
17175 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17176 && !TYPE_NAMELESS (type))
17177 t = TYPE_NAME (type);
17179 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17180 a TYPE_DECL node, regardless of whether or not a `typedef' was
17181 involved. */
17182 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17183 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17185 /* We want to be extra verbose. Don't call dwarf_name if
17186 DECL_NAME isn't set. The default hook for decl_printable_name
17187 doesn't like that, and in this context it's correct to return
17188 0, instead of "<anonymous>" or the like. */
17189 if (DECL_NAME (TYPE_NAME (type))
17190 && !DECL_NAMELESS (TYPE_NAME (type)))
17191 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17194 /* Now get the name as a string, or invent one. */
17195 if (!name && t != 0)
17196 name = IDENTIFIER_POINTER (t);
17199 return (name == 0 || *name == '\0') ? 0 : name;
17202 /* Return the type associated with a data member, make a special check
17203 for bit field types. */
17205 static inline tree
17206 member_declared_type (const_tree member)
17208 return (DECL_BIT_FIELD_TYPE (member)
17209 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17212 /* Get the decl's label, as described by its RTL. This may be different
17213 from the DECL_NAME name used in the source file. */
17215 #if 0
17216 static const char *
17217 decl_start_label (tree decl)
17219 rtx x;
17220 const char *fnname;
17222 x = DECL_RTL (decl);
17223 gcc_assert (MEM_P (x));
17225 x = XEXP (x, 0);
17226 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17228 fnname = XSTR (x, 0);
17229 return fnname;
17231 #endif
17233 /* These routines generate the internal representation of the DIE's for
17234 the compilation unit. Debugging information is collected by walking
17235 the declaration trees passed in from dwarf2out_decl(). */
17237 static void
17238 gen_array_type_die (tree type, dw_die_ref context_die)
17240 dw_die_ref scope_die = scope_die_for (type, context_die);
17241 dw_die_ref array_die;
17243 /* GNU compilers represent multidimensional array types as sequences of one
17244 dimensional array types whose element types are themselves array types.
17245 We sometimes squish that down to a single array_type DIE with multiple
17246 subscripts in the Dwarf debugging info. The draft Dwarf specification
17247 say that we are allowed to do this kind of compression in C, because
17248 there is no difference between an array of arrays and a multidimensional
17249 array. We don't do this for Ada to remain as close as possible to the
17250 actual representation, which is especially important against the language
17251 flexibilty wrt arrays of variable size. */
17253 bool collapse_nested_arrays = !is_ada ();
17254 tree element_type;
17256 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17257 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17258 if (TYPE_STRING_FLAG (type)
17259 && TREE_CODE (type) == ARRAY_TYPE
17260 && is_fortran ()
17261 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17263 HOST_WIDE_INT size;
17265 array_die = new_die (DW_TAG_string_type, scope_die, type);
17266 add_name_attribute (array_die, type_tag (type));
17267 equate_type_number_to_die (type, array_die);
17268 size = int_size_in_bytes (type);
17269 if (size >= 0)
17270 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17271 else if (TYPE_DOMAIN (type) != NULL_TREE
17272 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17273 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17275 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17276 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17278 size = int_size_in_bytes (TREE_TYPE (szdecl));
17279 if (loc && size > 0)
17281 add_AT_location_description (array_die, DW_AT_string_length, loc);
17282 if (size != DWARF2_ADDR_SIZE)
17283 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17286 return;
17289 array_die = new_die (DW_TAG_array_type, scope_die, type);
17290 add_name_attribute (array_die, type_tag (type));
17291 equate_type_number_to_die (type, array_die);
17293 if (TREE_CODE (type) == VECTOR_TYPE)
17294 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17296 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17297 if (is_fortran ()
17298 && TREE_CODE (type) == ARRAY_TYPE
17299 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17300 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17301 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17303 #if 0
17304 /* We default the array ordering. SDB will probably do
17305 the right things even if DW_AT_ordering is not present. It's not even
17306 an issue until we start to get into multidimensional arrays anyway. If
17307 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17308 then we'll have to put the DW_AT_ordering attribute back in. (But if
17309 and when we find out that we need to put these in, we will only do so
17310 for multidimensional arrays. */
17311 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17312 #endif
17314 if (TREE_CODE (type) == VECTOR_TYPE)
17316 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17317 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17318 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17319 add_bound_info (subrange_die, DW_AT_upper_bound,
17320 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17322 else
17323 add_subscript_info (array_die, type, collapse_nested_arrays);
17325 /* Add representation of the type of the elements of this array type and
17326 emit the corresponding DIE if we haven't done it already. */
17327 element_type = TREE_TYPE (type);
17328 if (collapse_nested_arrays)
17329 while (TREE_CODE (element_type) == ARRAY_TYPE)
17331 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17332 break;
17333 element_type = TREE_TYPE (element_type);
17336 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17338 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17339 if (TYPE_ARTIFICIAL (type))
17340 add_AT_flag (array_die, DW_AT_artificial, 1);
17342 if (get_AT (array_die, DW_AT_name))
17343 add_pubtype (type, array_die);
17346 static dw_loc_descr_ref
17347 descr_info_loc (tree val, tree base_decl)
17349 HOST_WIDE_INT size;
17350 dw_loc_descr_ref loc, loc2;
17351 enum dwarf_location_atom op;
17353 if (val == base_decl)
17354 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17356 switch (TREE_CODE (val))
17358 CASE_CONVERT:
17359 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17360 case VAR_DECL:
17361 return loc_descriptor_from_tree (val, 0);
17362 case INTEGER_CST:
17363 if (tree_fits_shwi_p (val))
17364 return int_loc_descriptor (tree_to_shwi (val));
17365 break;
17366 case INDIRECT_REF:
17367 size = int_size_in_bytes (TREE_TYPE (val));
17368 if (size < 0)
17369 break;
17370 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17371 if (!loc)
17372 break;
17373 if (size == DWARF2_ADDR_SIZE)
17374 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17375 else
17376 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17377 return loc;
17378 case POINTER_PLUS_EXPR:
17379 case PLUS_EXPR:
17380 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17381 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17383 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17384 if (!loc)
17385 break;
17386 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17388 else
17390 op = DW_OP_plus;
17391 do_binop:
17392 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17393 if (!loc)
17394 break;
17395 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17396 if (!loc2)
17397 break;
17398 add_loc_descr (&loc, loc2);
17399 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17401 return loc;
17402 case MINUS_EXPR:
17403 op = DW_OP_minus;
17404 goto do_binop;
17405 case MULT_EXPR:
17406 op = DW_OP_mul;
17407 goto do_binop;
17408 case EQ_EXPR:
17409 op = DW_OP_eq;
17410 goto do_binop;
17411 case NE_EXPR:
17412 op = DW_OP_ne;
17413 goto do_binop;
17414 default:
17415 break;
17417 return NULL;
17420 static void
17421 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17422 tree val, tree base_decl)
17424 dw_loc_descr_ref loc;
17426 if (tree_fits_shwi_p (val))
17428 add_AT_unsigned (die, attr, tree_to_shwi (val));
17429 return;
17432 loc = descr_info_loc (val, base_decl);
17433 if (!loc)
17434 return;
17436 add_AT_loc (die, attr, loc);
17439 /* This routine generates DIE for array with hidden descriptor, details
17440 are filled into *info by a langhook. */
17442 static void
17443 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17444 dw_die_ref context_die)
17446 dw_die_ref scope_die = scope_die_for (type, context_die);
17447 dw_die_ref array_die;
17448 int dim;
17450 array_die = new_die (DW_TAG_array_type, scope_die, type);
17451 add_name_attribute (array_die, type_tag (type));
17452 equate_type_number_to_die (type, array_die);
17454 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17455 if (is_fortran ()
17456 && info->ndimensions >= 2)
17457 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17459 if (info->data_location)
17460 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17461 info->base_decl);
17462 if (info->associated)
17463 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17464 info->base_decl);
17465 if (info->allocated)
17466 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17467 info->base_decl);
17469 for (dim = 0; dim < info->ndimensions; dim++)
17471 dw_die_ref subrange_die
17472 = new_die (DW_TAG_subrange_type, array_die, NULL);
17474 if (info->dimen[dim].lower_bound)
17476 /* If it is the default value, omit it. */
17477 int dflt;
17479 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17480 && (dflt = lower_bound_default ()) != -1
17481 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17483 else
17484 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17485 info->dimen[dim].lower_bound,
17486 info->base_decl);
17488 if (info->dimen[dim].upper_bound)
17489 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17490 info->dimen[dim].upper_bound,
17491 info->base_decl);
17492 if (info->dimen[dim].stride)
17493 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17494 info->dimen[dim].stride,
17495 info->base_decl);
17498 gen_type_die (info->element_type, context_die);
17499 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17500 context_die);
17502 if (get_AT (array_die, DW_AT_name))
17503 add_pubtype (type, array_die);
17506 #if 0
17507 static void
17508 gen_entry_point_die (tree decl, dw_die_ref context_die)
17510 tree origin = decl_ultimate_origin (decl);
17511 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17513 if (origin != NULL)
17514 add_abstract_origin_attribute (decl_die, origin);
17515 else
17517 add_name_and_src_coords_attributes (decl_die, decl);
17518 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17519 TYPE_UNQUALIFIED, context_die);
17522 if (DECL_ABSTRACT_P (decl))
17523 equate_decl_number_to_die (decl, decl_die);
17524 else
17525 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17527 #endif
17529 /* Walk through the list of incomplete types again, trying once more to
17530 emit full debugging info for them. */
17532 static void
17533 retry_incomplete_types (void)
17535 int i;
17537 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17538 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17539 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17542 /* Determine what tag to use for a record type. */
17544 static enum dwarf_tag
17545 record_type_tag (tree type)
17547 if (! lang_hooks.types.classify_record)
17548 return DW_TAG_structure_type;
17550 switch (lang_hooks.types.classify_record (type))
17552 case RECORD_IS_STRUCT:
17553 return DW_TAG_structure_type;
17555 case RECORD_IS_CLASS:
17556 return DW_TAG_class_type;
17558 case RECORD_IS_INTERFACE:
17559 if (dwarf_version >= 3 || !dwarf_strict)
17560 return DW_TAG_interface_type;
17561 return DW_TAG_structure_type;
17563 default:
17564 gcc_unreachable ();
17568 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17569 include all of the information about the enumeration values also. Each
17570 enumerated type name/value is listed as a child of the enumerated type
17571 DIE. */
17573 static dw_die_ref
17574 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17576 dw_die_ref type_die = lookup_type_die (type);
17578 if (type_die == NULL)
17580 type_die = new_die (DW_TAG_enumeration_type,
17581 scope_die_for (type, context_die), type);
17582 equate_type_number_to_die (type, type_die);
17583 add_name_attribute (type_die, type_tag (type));
17584 if (dwarf_version >= 4 || !dwarf_strict)
17586 if (ENUM_IS_SCOPED (type))
17587 add_AT_flag (type_die, DW_AT_enum_class, 1);
17588 if (ENUM_IS_OPAQUE (type))
17589 add_AT_flag (type_die, DW_AT_declaration, 1);
17592 else if (! TYPE_SIZE (type))
17593 return type_die;
17594 else
17595 remove_AT (type_die, DW_AT_declaration);
17597 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17598 given enum type is incomplete, do not generate the DW_AT_byte_size
17599 attribute or the DW_AT_element_list attribute. */
17600 if (TYPE_SIZE (type))
17602 tree link;
17604 TREE_ASM_WRITTEN (type) = 1;
17605 add_byte_size_attribute (type_die, type);
17606 if (dwarf_version >= 3 || !dwarf_strict)
17608 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17609 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17610 context_die);
17612 if (TYPE_STUB_DECL (type) != NULL_TREE)
17614 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17615 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17618 /* If the first reference to this type was as the return type of an
17619 inline function, then it may not have a parent. Fix this now. */
17620 if (type_die->die_parent == NULL)
17621 add_child_die (scope_die_for (type, context_die), type_die);
17623 for (link = TYPE_VALUES (type);
17624 link != NULL; link = TREE_CHAIN (link))
17626 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17627 tree value = TREE_VALUE (link);
17629 add_name_attribute (enum_die,
17630 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17632 if (TREE_CODE (value) == CONST_DECL)
17633 value = DECL_INITIAL (value);
17635 if (simple_type_size_in_bits (TREE_TYPE (value))
17636 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17638 /* For constant forms created by add_AT_unsigned DWARF
17639 consumers (GDB, elfutils, etc.) always zero extend
17640 the value. Only when the actual value is negative
17641 do we need to use add_AT_int to generate a constant
17642 form that can represent negative values. */
17643 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17644 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17645 add_AT_unsigned (enum_die, DW_AT_const_value,
17646 (unsigned HOST_WIDE_INT) val);
17647 else
17648 add_AT_int (enum_die, DW_AT_const_value, val);
17650 else
17651 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17652 that here. TODO: This should be re-worked to use correct
17653 signed/unsigned double tags for all cases. */
17654 add_AT_wide (enum_die, DW_AT_const_value, value);
17657 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17658 if (TYPE_ARTIFICIAL (type))
17659 add_AT_flag (type_die, DW_AT_artificial, 1);
17661 else
17662 add_AT_flag (type_die, DW_AT_declaration, 1);
17664 add_pubtype (type, type_die);
17666 return type_die;
17669 /* Generate a DIE to represent either a real live formal parameter decl or to
17670 represent just the type of some formal parameter position in some function
17671 type.
17673 Note that this routine is a bit unusual because its argument may be a
17674 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17675 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17676 node. If it's the former then this function is being called to output a
17677 DIE to represent a formal parameter object (or some inlining thereof). If
17678 it's the latter, then this function is only being called to output a
17679 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17680 argument type of some subprogram type.
17681 If EMIT_NAME_P is true, name and source coordinate attributes
17682 are emitted. */
17684 static dw_die_ref
17685 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17686 dw_die_ref context_die)
17688 tree node_or_origin = node ? node : origin;
17689 tree ultimate_origin;
17690 dw_die_ref parm_die
17691 = new_die (DW_TAG_formal_parameter, context_die, node);
17693 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17695 case tcc_declaration:
17696 ultimate_origin = decl_ultimate_origin (node_or_origin);
17697 if (node || ultimate_origin)
17698 origin = ultimate_origin;
17699 if (origin != NULL)
17700 add_abstract_origin_attribute (parm_die, origin);
17701 else if (emit_name_p)
17702 add_name_and_src_coords_attributes (parm_die, node);
17703 if (origin == NULL
17704 || (! DECL_ABSTRACT_P (node_or_origin)
17705 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17706 decl_function_context
17707 (node_or_origin))))
17709 tree type = TREE_TYPE (node_or_origin);
17710 if (decl_by_reference_p (node_or_origin))
17711 add_type_attribute (parm_die, TREE_TYPE (type),
17712 TYPE_UNQUALIFIED, context_die);
17713 else
17714 add_type_attribute (parm_die, type,
17715 decl_quals (node_or_origin),
17716 context_die);
17718 if (origin == NULL && DECL_ARTIFICIAL (node))
17719 add_AT_flag (parm_die, DW_AT_artificial, 1);
17721 if (node && node != origin)
17722 equate_decl_number_to_die (node, parm_die);
17723 if (! DECL_ABSTRACT_P (node_or_origin))
17724 add_location_or_const_value_attribute (parm_die, node_or_origin,
17725 node == NULL, DW_AT_location);
17727 break;
17729 case tcc_type:
17730 /* We were called with some kind of a ..._TYPE node. */
17731 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17732 context_die);
17733 break;
17735 default:
17736 gcc_unreachable ();
17739 return parm_die;
17742 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17743 children DW_TAG_formal_parameter DIEs representing the arguments of the
17744 parameter pack.
17746 PARM_PACK must be a function parameter pack.
17747 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17748 must point to the subsequent arguments of the function PACK_ARG belongs to.
17749 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17750 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17751 following the last one for which a DIE was generated. */
17753 static dw_die_ref
17754 gen_formal_parameter_pack_die (tree parm_pack,
17755 tree pack_arg,
17756 dw_die_ref subr_die,
17757 tree *next_arg)
17759 tree arg;
17760 dw_die_ref parm_pack_die;
17762 gcc_assert (parm_pack
17763 && lang_hooks.function_parameter_pack_p (parm_pack)
17764 && subr_die);
17766 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17767 add_src_coords_attributes (parm_pack_die, parm_pack);
17769 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17771 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17772 parm_pack))
17773 break;
17774 gen_formal_parameter_die (arg, NULL,
17775 false /* Don't emit name attribute. */,
17776 parm_pack_die);
17778 if (next_arg)
17779 *next_arg = arg;
17780 return parm_pack_die;
17783 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17784 at the end of an (ANSI prototyped) formal parameters list. */
17786 static void
17787 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17789 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17792 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17793 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17794 parameters as specified in some function type specification (except for
17795 those which appear as part of a function *definition*). */
17797 static void
17798 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17800 tree link;
17801 tree formal_type = NULL;
17802 tree first_parm_type;
17803 tree arg;
17805 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17807 arg = DECL_ARGUMENTS (function_or_method_type);
17808 function_or_method_type = TREE_TYPE (function_or_method_type);
17810 else
17811 arg = NULL_TREE;
17813 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17815 /* Make our first pass over the list of formal parameter types and output a
17816 DW_TAG_formal_parameter DIE for each one. */
17817 for (link = first_parm_type; link; )
17819 dw_die_ref parm_die;
17821 formal_type = TREE_VALUE (link);
17822 if (formal_type == void_type_node)
17823 break;
17825 /* Output a (nameless) DIE to represent the formal parameter itself. */
17826 if (!POINTER_BOUNDS_TYPE_P (formal_type))
17828 parm_die = gen_formal_parameter_die (formal_type, NULL,
17829 true /* Emit name attribute. */,
17830 context_die);
17831 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17832 && link == first_parm_type)
17834 add_AT_flag (parm_die, DW_AT_artificial, 1);
17835 if (dwarf_version >= 3 || !dwarf_strict)
17836 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17838 else if (arg && DECL_ARTIFICIAL (arg))
17839 add_AT_flag (parm_die, DW_AT_artificial, 1);
17842 link = TREE_CHAIN (link);
17843 if (arg)
17844 arg = DECL_CHAIN (arg);
17847 /* If this function type has an ellipsis, add a
17848 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17849 if (formal_type != void_type_node)
17850 gen_unspecified_parameters_die (function_or_method_type, context_die);
17852 /* Make our second (and final) pass over the list of formal parameter types
17853 and output DIEs to represent those types (as necessary). */
17854 for (link = TYPE_ARG_TYPES (function_or_method_type);
17855 link && TREE_VALUE (link);
17856 link = TREE_CHAIN (link))
17857 gen_type_die (TREE_VALUE (link), context_die);
17860 /* We want to generate the DIE for TYPE so that we can generate the
17861 die for MEMBER, which has been defined; we will need to refer back
17862 to the member declaration nested within TYPE. If we're trying to
17863 generate minimal debug info for TYPE, processing TYPE won't do the
17864 trick; we need to attach the member declaration by hand. */
17866 static void
17867 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17869 gen_type_die (type, context_die);
17871 /* If we're trying to avoid duplicate debug info, we may not have
17872 emitted the member decl for this function. Emit it now. */
17873 if (TYPE_STUB_DECL (type)
17874 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17875 && ! lookup_decl_die (member))
17877 dw_die_ref type_die;
17878 gcc_assert (!decl_ultimate_origin (member));
17880 push_decl_scope (type);
17881 type_die = lookup_type_die_strip_naming_typedef (type);
17882 if (TREE_CODE (member) == FUNCTION_DECL)
17883 gen_subprogram_die (member, type_die);
17884 else if (TREE_CODE (member) == FIELD_DECL)
17886 /* Ignore the nameless fields that are used to skip bits but handle
17887 C++ anonymous unions and structs. */
17888 if (DECL_NAME (member) != NULL_TREE
17889 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17890 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17892 gen_type_die (member_declared_type (member), type_die);
17893 gen_field_die (member, type_die);
17896 else
17897 gen_variable_die (member, NULL_TREE, type_die);
17899 pop_decl_scope ();
17903 /* Forward declare these functions, because they are mutually recursive
17904 with their set_block_* pairing functions. */
17905 static void set_decl_origin_self (tree);
17906 static void set_decl_abstract_flags (tree, int);
17908 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17909 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17910 that it points to the node itself, thus indicating that the node is its
17911 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17912 the given node is NULL, recursively descend the decl/block tree which
17913 it is the root of, and for each other ..._DECL or BLOCK node contained
17914 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17915 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17916 values to point to themselves. */
17918 static void
17919 set_block_origin_self (tree stmt)
17921 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17923 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17926 tree local_decl;
17928 for (local_decl = BLOCK_VARS (stmt);
17929 local_decl != NULL_TREE;
17930 local_decl = DECL_CHAIN (local_decl))
17931 if (! DECL_EXTERNAL (local_decl))
17932 set_decl_origin_self (local_decl); /* Potential recursion. */
17936 tree subblock;
17938 for (subblock = BLOCK_SUBBLOCKS (stmt);
17939 subblock != NULL_TREE;
17940 subblock = BLOCK_CHAIN (subblock))
17941 set_block_origin_self (subblock); /* Recurse. */
17946 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17947 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17948 node to so that it points to the node itself, thus indicating that the
17949 node represents its own (abstract) origin. Additionally, if the
17950 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17951 the decl/block tree of which the given node is the root of, and for
17952 each other ..._DECL or BLOCK node contained therein whose
17953 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17954 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17955 point to themselves. */
17957 static void
17958 set_decl_origin_self (tree decl)
17960 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17962 DECL_ABSTRACT_ORIGIN (decl) = decl;
17963 if (TREE_CODE (decl) == FUNCTION_DECL)
17965 tree arg;
17967 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17968 DECL_ABSTRACT_ORIGIN (arg) = arg;
17969 if (DECL_INITIAL (decl) != NULL_TREE
17970 && DECL_INITIAL (decl) != error_mark_node)
17971 set_block_origin_self (DECL_INITIAL (decl));
17976 /* Given a pointer to some BLOCK node, and a boolean value to set the
17977 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17978 the given block, and for all local decls and all local sub-blocks
17979 (recursively) which are contained therein. */
17981 static void
17982 set_block_abstract_flags (tree stmt, int setting)
17984 tree local_decl;
17985 tree subblock;
17986 unsigned int i;
17988 BLOCK_ABSTRACT (stmt) = setting;
17990 for (local_decl = BLOCK_VARS (stmt);
17991 local_decl != NULL_TREE;
17992 local_decl = DECL_CHAIN (local_decl))
17993 if (! DECL_EXTERNAL (local_decl))
17994 set_decl_abstract_flags (local_decl, setting);
17996 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17998 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17999 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18000 || TREE_CODE (local_decl) == PARM_DECL)
18001 set_decl_abstract_flags (local_decl, setting);
18004 for (subblock = BLOCK_SUBBLOCKS (stmt);
18005 subblock != NULL_TREE;
18006 subblock = BLOCK_CHAIN (subblock))
18007 set_block_abstract_flags (subblock, setting);
18010 /* Given a pointer to some ..._DECL node, and a boolean value to set the
18011 "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
18012 given decl, and (in the case where the decl is a FUNCTION_DECL) also
18013 set the abstract flags for all of the parameters, local vars, local
18014 blocks and sub-blocks (recursively) to the same setting. */
18016 static void
18017 set_decl_abstract_flags (tree decl, int setting)
18019 DECL_ABSTRACT_P (decl) = setting;
18020 if (TREE_CODE (decl) == FUNCTION_DECL)
18022 tree arg;
18024 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18025 DECL_ABSTRACT_P (arg) = setting;
18026 if (DECL_INITIAL (decl) != NULL_TREE
18027 && DECL_INITIAL (decl) != error_mark_node)
18028 set_block_abstract_flags (DECL_INITIAL (decl), setting);
18032 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18033 may later generate inlined and/or out-of-line instances of. */
18035 static void
18036 dwarf2out_abstract_function (tree decl)
18038 dw_die_ref old_die;
18039 tree save_fn;
18040 tree context;
18041 int was_abstract;
18042 hash_table<decl_loc_hasher> *old_decl_loc_table;
18043 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18044 int old_call_site_count, old_tail_call_site_count;
18045 struct call_arg_loc_node *old_call_arg_locations;
18047 /* Make sure we have the actual abstract inline, not a clone. */
18048 decl = DECL_ORIGIN (decl);
18050 old_die = lookup_decl_die (decl);
18051 if (old_die && get_AT (old_die, DW_AT_inline))
18052 /* We've already generated the abstract instance. */
18053 return;
18055 /* We can be called while recursively when seeing block defining inlined subroutine
18056 DIE. Be sure to not clobber the outer location table nor use it or we would
18057 get locations in abstract instantces. */
18058 old_decl_loc_table = decl_loc_table;
18059 decl_loc_table = NULL;
18060 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18061 cached_dw_loc_list_table = NULL;
18062 old_call_arg_locations = call_arg_locations;
18063 call_arg_locations = NULL;
18064 old_call_site_count = call_site_count;
18065 call_site_count = -1;
18066 old_tail_call_site_count = tail_call_site_count;
18067 tail_call_site_count = -1;
18069 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18070 we don't get confused by DECL_ABSTRACT_P. */
18071 if (debug_info_level > DINFO_LEVEL_TERSE)
18073 context = decl_class_context (decl);
18074 if (context)
18075 gen_type_die_for_member
18076 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18079 /* Pretend we've just finished compiling this function. */
18080 save_fn = current_function_decl;
18081 current_function_decl = decl;
18083 was_abstract = DECL_ABSTRACT_P (decl);
18084 set_decl_abstract_flags (decl, 1);
18085 dwarf2out_decl (decl);
18086 if (! was_abstract)
18087 set_decl_abstract_flags (decl, 0);
18089 current_function_decl = save_fn;
18090 decl_loc_table = old_decl_loc_table;
18091 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18092 call_arg_locations = old_call_arg_locations;
18093 call_site_count = old_call_site_count;
18094 tail_call_site_count = old_tail_call_site_count;
18097 /* Helper function of premark_used_types() which gets called through
18098 htab_traverse.
18100 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18101 marked as unused by prune_unused_types. */
18103 bool
18104 premark_used_types_helper (tree const &type, void *)
18106 dw_die_ref die;
18108 die = lookup_type_die (type);
18109 if (die != NULL)
18110 die->die_perennial_p = 1;
18111 return true;
18114 /* Helper function of premark_types_used_by_global_vars which gets called
18115 through htab_traverse.
18117 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18118 marked as unused by prune_unused_types. The DIE of the type is marked
18119 only if the global variable using the type will actually be emitted. */
18122 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18123 void *)
18125 struct types_used_by_vars_entry *entry;
18126 dw_die_ref die;
18128 entry = (struct types_used_by_vars_entry *) *slot;
18129 gcc_assert (entry->type != NULL
18130 && entry->var_decl != NULL);
18131 die = lookup_type_die (entry->type);
18132 if (die)
18134 /* Ask cgraph if the global variable really is to be emitted.
18135 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18136 varpool_node *node = varpool_node::get (entry->var_decl);
18137 if (node && node->definition)
18139 die->die_perennial_p = 1;
18140 /* Keep the parent DIEs as well. */
18141 while ((die = die->die_parent) && die->die_perennial_p == 0)
18142 die->die_perennial_p = 1;
18145 return 1;
18148 /* Mark all members of used_types_hash as perennial. */
18150 static void
18151 premark_used_types (struct function *fun)
18153 if (fun && fun->used_types_hash)
18154 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18157 /* Mark all members of types_used_by_vars_entry as perennial. */
18159 static void
18160 premark_types_used_by_global_vars (void)
18162 if (types_used_by_vars_hash)
18163 types_used_by_vars_hash
18164 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18167 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18168 for CA_LOC call arg loc node. */
18170 static dw_die_ref
18171 gen_call_site_die (tree decl, dw_die_ref subr_die,
18172 struct call_arg_loc_node *ca_loc)
18174 dw_die_ref stmt_die = NULL, die;
18175 tree block = ca_loc->block;
18177 while (block
18178 && block != DECL_INITIAL (decl)
18179 && TREE_CODE (block) == BLOCK)
18181 if (block_map.length () > BLOCK_NUMBER (block))
18182 stmt_die = block_map[BLOCK_NUMBER (block)];
18183 if (stmt_die)
18184 break;
18185 block = BLOCK_SUPERCONTEXT (block);
18187 if (stmt_die == NULL)
18188 stmt_die = subr_die;
18189 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18190 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18191 if (ca_loc->tail_call_p)
18192 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18193 if (ca_loc->symbol_ref)
18195 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18196 if (tdie)
18197 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18198 else
18199 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18201 return die;
18204 /* Generate a DIE to represent a declared function (either file-scope or
18205 block-local). */
18207 static void
18208 gen_subprogram_die (tree decl, dw_die_ref context_die)
18210 tree origin = decl_ultimate_origin (decl);
18211 dw_die_ref subr_die;
18212 tree outer_scope;
18213 dw_die_ref old_die = lookup_decl_die (decl);
18214 int declaration = (current_function_decl != decl
18215 || class_or_namespace_scope_p (context_die));
18217 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18219 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18220 started to generate the abstract instance of an inline, decided to output
18221 its containing class, and proceeded to emit the declaration of the inline
18222 from the member list for the class. If so, DECLARATION takes priority;
18223 we'll get back to the abstract instance when done with the class. */
18225 /* The class-scope declaration DIE must be the primary DIE. */
18226 if (origin && declaration && class_or_namespace_scope_p (context_die))
18228 origin = NULL;
18229 gcc_assert (!old_die);
18232 /* Now that the C++ front end lazily declares artificial member fns, we
18233 might need to retrofit the declaration into its class. */
18234 if (!declaration && !origin && !old_die
18235 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18236 && !class_or_namespace_scope_p (context_die)
18237 && debug_info_level > DINFO_LEVEL_TERSE)
18238 old_die = force_decl_die (decl);
18240 if (origin != NULL)
18242 gcc_assert (!declaration || local_scope_p (context_die));
18244 /* Fixup die_parent for the abstract instance of a nested
18245 inline function. */
18246 if (old_die && old_die->die_parent == NULL)
18247 add_child_die (context_die, old_die);
18249 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18250 add_abstract_origin_attribute (subr_die, origin);
18251 /* This is where the actual code for a cloned function is.
18252 Let's emit linkage name attribute for it. This helps
18253 debuggers to e.g, set breakpoints into
18254 constructors/destructors when the user asks "break
18255 K::K". */
18256 add_linkage_name (subr_die, decl);
18258 else if (old_die)
18260 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18261 struct dwarf_file_data * file_index = lookup_filename (s.file);
18263 if (!get_AT_flag (old_die, DW_AT_declaration)
18264 /* We can have a normal definition following an inline one in the
18265 case of redefinition of GNU C extern inlines.
18266 It seems reasonable to use AT_specification in this case. */
18267 && !get_AT (old_die, DW_AT_inline))
18269 /* Detect and ignore this case, where we are trying to output
18270 something we have already output. */
18271 return;
18274 /* If the definition comes from the same place as the declaration,
18275 maybe use the old DIE. We always want the DIE for this function
18276 that has the *_pc attributes to be under comp_unit_die so the
18277 debugger can find it. We also need to do this for abstract
18278 instances of inlines, since the spec requires the out-of-line copy
18279 to have the same parent. For local class methods, this doesn't
18280 apply; we just use the old DIE. */
18281 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18282 && (DECL_ARTIFICIAL (decl)
18283 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18284 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18285 == (unsigned) s.line))))
18287 subr_die = old_die;
18289 /* Clear out the declaration attribute and the formal parameters.
18290 Do not remove all children, because it is possible that this
18291 declaration die was forced using force_decl_die(). In such
18292 cases die that forced declaration die (e.g. TAG_imported_module)
18293 is one of the children that we do not want to remove. */
18294 remove_AT (subr_die, DW_AT_declaration);
18295 remove_AT (subr_die, DW_AT_object_pointer);
18296 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18298 else
18300 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18301 add_AT_specification (subr_die, old_die);
18302 add_pubname (decl, subr_die);
18303 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18304 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18305 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18306 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18308 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18309 emit the real type on the definition die. */
18310 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18312 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18313 if (die == auto_die || die == decltype_auto_die)
18314 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18315 TYPE_UNQUALIFIED, context_die);
18319 else
18321 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18323 if (TREE_PUBLIC (decl))
18324 add_AT_flag (subr_die, DW_AT_external, 1);
18326 add_name_and_src_coords_attributes (subr_die, decl);
18327 add_pubname (decl, subr_die);
18328 if (debug_info_level > DINFO_LEVEL_TERSE)
18330 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18331 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18332 TYPE_UNQUALIFIED, context_die);
18335 add_pure_or_virtual_attribute (subr_die, decl);
18336 if (DECL_ARTIFICIAL (decl))
18337 add_AT_flag (subr_die, DW_AT_artificial, 1);
18339 add_accessibility_attribute (subr_die, decl);
18342 if (declaration)
18344 if (!old_die || !get_AT (old_die, DW_AT_inline))
18346 add_AT_flag (subr_die, DW_AT_declaration, 1);
18348 /* If this is an explicit function declaration then generate
18349 a DW_AT_explicit attribute. */
18350 if (lang_hooks.decls.function_decl_explicit_p (decl)
18351 && (dwarf_version >= 3 || !dwarf_strict))
18352 add_AT_flag (subr_die, DW_AT_explicit, 1);
18354 /* If this is a C++11 deleted special function member then generate
18355 a DW_AT_GNU_deleted attribute. */
18356 if (lang_hooks.decls.function_decl_deleted_p (decl)
18357 && (! dwarf_strict))
18358 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18360 /* The first time we see a member function, it is in the context of
18361 the class to which it belongs. We make sure of this by emitting
18362 the class first. The next time is the definition, which is
18363 handled above. The two may come from the same source text.
18365 Note that force_decl_die() forces function declaration die. It is
18366 later reused to represent definition. */
18367 equate_decl_number_to_die (decl, subr_die);
18370 else if (DECL_ABSTRACT_P (decl))
18372 if (DECL_DECLARED_INLINE_P (decl))
18374 if (cgraph_function_possibly_inlined_p (decl))
18375 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18376 else
18377 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18379 else
18381 if (cgraph_function_possibly_inlined_p (decl))
18382 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18383 else
18384 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18387 if (DECL_DECLARED_INLINE_P (decl)
18388 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18389 add_AT_flag (subr_die, DW_AT_artificial, 1);
18391 equate_decl_number_to_die (decl, subr_die);
18393 else if (!DECL_EXTERNAL (decl))
18395 HOST_WIDE_INT cfa_fb_offset;
18396 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18398 if (!old_die || !get_AT (old_die, DW_AT_inline))
18399 equate_decl_number_to_die (decl, subr_die);
18401 gcc_checking_assert (fun);
18402 if (!flag_reorder_blocks_and_partition)
18404 dw_fde_ref fde = fun->fde;
18405 if (fde->dw_fde_begin)
18407 /* We have already generated the labels. */
18408 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18409 fde->dw_fde_end, false);
18411 else
18413 /* Create start/end labels and add the range. */
18414 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18415 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18416 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18417 current_function_funcdef_no);
18418 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18419 current_function_funcdef_no);
18420 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18421 false);
18424 #if VMS_DEBUGGING_INFO
18425 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18426 Section 2.3 Prologue and Epilogue Attributes:
18427 When a breakpoint is set on entry to a function, it is generally
18428 desirable for execution to be suspended, not on the very first
18429 instruction of the function, but rather at a point after the
18430 function's frame has been set up, after any language defined local
18431 declaration processing has been completed, and before execution of
18432 the first statement of the function begins. Debuggers generally
18433 cannot properly determine where this point is. Similarly for a
18434 breakpoint set on exit from a function. The prologue and epilogue
18435 attributes allow a compiler to communicate the location(s) to use. */
18438 if (fde->dw_fde_vms_end_prologue)
18439 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18440 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18442 if (fde->dw_fde_vms_begin_epilogue)
18443 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18444 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18446 #endif
18449 else
18451 /* Generate pubnames entries for the split function code ranges. */
18452 dw_fde_ref fde = fun->fde;
18454 if (fde->dw_fde_second_begin)
18456 if (dwarf_version >= 3 || !dwarf_strict)
18458 /* We should use ranges for non-contiguous code section
18459 addresses. Use the actual code range for the initial
18460 section, since the HOT/COLD labels might precede an
18461 alignment offset. */
18462 bool range_list_added = false;
18463 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18464 fde->dw_fde_end, &range_list_added,
18465 false);
18466 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18467 fde->dw_fde_second_end,
18468 &range_list_added, false);
18469 if (range_list_added)
18470 add_ranges (NULL);
18472 else
18474 /* There is no real support in DW2 for this .. so we make
18475 a work-around. First, emit the pub name for the segment
18476 containing the function label. Then make and emit a
18477 simplified subprogram DIE for the second segment with the
18478 name pre-fixed by __hot/cold_sect_of_. We use the same
18479 linkage name for the second die so that gdb will find both
18480 sections when given "b foo". */
18481 const char *name = NULL;
18482 tree decl_name = DECL_NAME (decl);
18483 dw_die_ref seg_die;
18485 /* Do the 'primary' section. */
18486 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18487 fde->dw_fde_end, false);
18489 /* Build a minimal DIE for the secondary section. */
18490 seg_die = new_die (DW_TAG_subprogram,
18491 subr_die->die_parent, decl);
18493 if (TREE_PUBLIC (decl))
18494 add_AT_flag (seg_die, DW_AT_external, 1);
18496 if (decl_name != NULL
18497 && IDENTIFIER_POINTER (decl_name) != NULL)
18499 name = dwarf2_name (decl, 1);
18500 if (! DECL_ARTIFICIAL (decl))
18501 add_src_coords_attributes (seg_die, decl);
18503 add_linkage_name (seg_die, decl);
18505 gcc_assert (name != NULL);
18506 add_pure_or_virtual_attribute (seg_die, decl);
18507 if (DECL_ARTIFICIAL (decl))
18508 add_AT_flag (seg_die, DW_AT_artificial, 1);
18510 name = concat ("__second_sect_of_", name, NULL);
18511 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18512 fde->dw_fde_second_end, false);
18513 add_name_attribute (seg_die, name);
18514 if (want_pubnames ())
18515 add_pubname_string (name, seg_die);
18518 else
18519 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18520 false);
18523 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18525 /* We define the "frame base" as the function's CFA. This is more
18526 convenient for several reasons: (1) It's stable across the prologue
18527 and epilogue, which makes it better than just a frame pointer,
18528 (2) With dwarf3, there exists a one-byte encoding that allows us
18529 to reference the .debug_frame data by proxy, but failing that,
18530 (3) We can at least reuse the code inspection and interpretation
18531 code that determines the CFA position at various points in the
18532 function. */
18533 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18535 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18536 add_AT_loc (subr_die, DW_AT_frame_base, op);
18538 else
18540 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18541 if (list->dw_loc_next)
18542 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18543 else
18544 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18547 /* Compute a displacement from the "steady-state frame pointer" to
18548 the CFA. The former is what all stack slots and argument slots
18549 will reference in the rtl; the latter is what we've told the
18550 debugger about. We'll need to adjust all frame_base references
18551 by this displacement. */
18552 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18554 if (fun->static_chain_decl)
18555 add_AT_location_description (subr_die, DW_AT_static_link,
18556 loc_list_from_tree (fun->static_chain_decl, 2));
18559 /* Generate child dies for template paramaters. */
18560 if (debug_info_level > DINFO_LEVEL_TERSE)
18561 gen_generic_params_dies (decl);
18563 /* Now output descriptions of the arguments for this function. This gets
18564 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18565 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18566 `...' at the end of the formal parameter list. In order to find out if
18567 there was a trailing ellipsis or not, we must instead look at the type
18568 associated with the FUNCTION_DECL. This will be a node of type
18569 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18570 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18571 an ellipsis at the end. */
18573 /* In the case where we are describing a mere function declaration, all we
18574 need to do here (and all we *can* do here) is to describe the *types* of
18575 its formal parameters. */
18576 if (debug_info_level <= DINFO_LEVEL_TERSE)
18578 else if (declaration)
18579 gen_formal_types_die (decl, subr_die);
18580 else
18582 /* Generate DIEs to represent all known formal parameters. */
18583 tree parm = DECL_ARGUMENTS (decl);
18584 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18585 tree generic_decl_parm = generic_decl
18586 ? DECL_ARGUMENTS (generic_decl)
18587 : NULL;
18589 /* Now we want to walk the list of parameters of the function and
18590 emit their relevant DIEs.
18592 We consider the case of DECL being an instance of a generic function
18593 as well as it being a normal function.
18595 If DECL is an instance of a generic function we walk the
18596 parameters of the generic function declaration _and_ the parameters of
18597 DECL itself. This is useful because we want to emit specific DIEs for
18598 function parameter packs and those are declared as part of the
18599 generic function declaration. In that particular case,
18600 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18601 That DIE has children DIEs representing the set of arguments
18602 of the pack. Note that the set of pack arguments can be empty.
18603 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18604 children DIE.
18606 Otherwise, we just consider the parameters of DECL. */
18607 while (generic_decl_parm || parm)
18609 if (generic_decl_parm
18610 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18611 gen_formal_parameter_pack_die (generic_decl_parm,
18612 parm, subr_die,
18613 &parm);
18614 else if (parm && !POINTER_BOUNDS_P (parm))
18616 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18618 if (parm == DECL_ARGUMENTS (decl)
18619 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18620 && parm_die
18621 && (dwarf_version >= 3 || !dwarf_strict))
18622 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18624 parm = DECL_CHAIN (parm);
18626 else if (parm)
18627 parm = DECL_CHAIN (parm);
18629 if (generic_decl_parm)
18630 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18633 /* Decide whether we need an unspecified_parameters DIE at the end.
18634 There are 2 more cases to do this for: 1) the ansi ... declaration -
18635 this is detectable when the end of the arg list is not a
18636 void_type_node 2) an unprototyped function declaration (not a
18637 definition). This just means that we have no info about the
18638 parameters at all. */
18639 if (prototype_p (TREE_TYPE (decl)))
18641 /* This is the prototyped case, check for.... */
18642 if (stdarg_p (TREE_TYPE (decl)))
18643 gen_unspecified_parameters_die (decl, subr_die);
18645 else if (DECL_INITIAL (decl) == NULL_TREE)
18646 gen_unspecified_parameters_die (decl, subr_die);
18649 /* Output Dwarf info for all of the stuff within the body of the function
18650 (if it has one - it may be just a declaration). */
18651 outer_scope = DECL_INITIAL (decl);
18653 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18654 a function. This BLOCK actually represents the outermost binding contour
18655 for the function, i.e. the contour in which the function's formal
18656 parameters and labels get declared. Curiously, it appears that the front
18657 end doesn't actually put the PARM_DECL nodes for the current function onto
18658 the BLOCK_VARS list for this outer scope, but are strung off of the
18659 DECL_ARGUMENTS list for the function instead.
18661 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18662 the LABEL_DECL nodes for the function however, and we output DWARF info
18663 for those in decls_for_scope. Just within the `outer_scope' there will be
18664 a BLOCK node representing the function's outermost pair of curly braces,
18665 and any blocks used for the base and member initializers of a C++
18666 constructor function. */
18667 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18669 int call_site_note_count = 0;
18670 int tail_call_site_note_count = 0;
18672 /* Emit a DW_TAG_variable DIE for a named return value. */
18673 if (DECL_NAME (DECL_RESULT (decl)))
18674 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18676 decls_for_scope (outer_scope, subr_die, 0);
18678 if (call_arg_locations && !dwarf_strict)
18680 struct call_arg_loc_node *ca_loc;
18681 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18683 dw_die_ref die = NULL;
18684 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18685 rtx arg, next_arg;
18687 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18688 arg; arg = next_arg)
18690 dw_loc_descr_ref reg, val;
18691 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18692 dw_die_ref cdie, tdie = NULL;
18694 next_arg = XEXP (arg, 1);
18695 if (REG_P (XEXP (XEXP (arg, 0), 0))
18696 && next_arg
18697 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18698 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18699 && REGNO (XEXP (XEXP (arg, 0), 0))
18700 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18701 next_arg = XEXP (next_arg, 1);
18702 if (mode == VOIDmode)
18704 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18705 if (mode == VOIDmode)
18706 mode = GET_MODE (XEXP (arg, 0));
18708 if (mode == VOIDmode || mode == BLKmode)
18709 continue;
18710 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18712 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18713 tloc = XEXP (XEXP (arg, 0), 1);
18714 continue;
18716 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18717 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18719 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18720 tlocc = XEXP (XEXP (arg, 0), 1);
18721 continue;
18723 reg = NULL;
18724 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18725 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18726 VAR_INIT_STATUS_INITIALIZED);
18727 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18729 rtx mem = XEXP (XEXP (arg, 0), 0);
18730 reg = mem_loc_descriptor (XEXP (mem, 0),
18731 get_address_mode (mem),
18732 GET_MODE (mem),
18733 VAR_INIT_STATUS_INITIALIZED);
18735 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18736 == DEBUG_PARAMETER_REF)
18738 tree tdecl
18739 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18740 tdie = lookup_decl_die (tdecl);
18741 if (tdie == NULL)
18742 continue;
18744 else
18745 continue;
18746 if (reg == NULL
18747 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18748 != DEBUG_PARAMETER_REF)
18749 continue;
18750 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18751 VOIDmode,
18752 VAR_INIT_STATUS_INITIALIZED);
18753 if (val == NULL)
18754 continue;
18755 if (die == NULL)
18756 die = gen_call_site_die (decl, subr_die, ca_loc);
18757 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18758 NULL_TREE);
18759 if (reg != NULL)
18760 add_AT_loc (cdie, DW_AT_location, reg);
18761 else if (tdie != NULL)
18762 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18763 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18764 if (next_arg != XEXP (arg, 1))
18766 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18767 if (mode == VOIDmode)
18768 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18769 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18770 0), 1),
18771 mode, VOIDmode,
18772 VAR_INIT_STATUS_INITIALIZED);
18773 if (val != NULL)
18774 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18777 if (die == NULL
18778 && (ca_loc->symbol_ref || tloc))
18779 die = gen_call_site_die (decl, subr_die, ca_loc);
18780 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18782 dw_loc_descr_ref tval = NULL;
18784 if (tloc != NULL_RTX)
18785 tval = mem_loc_descriptor (tloc,
18786 GET_MODE (tloc) == VOIDmode
18787 ? Pmode : GET_MODE (tloc),
18788 VOIDmode,
18789 VAR_INIT_STATUS_INITIALIZED);
18790 if (tval)
18791 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18792 else if (tlocc != NULL_RTX)
18794 tval = mem_loc_descriptor (tlocc,
18795 GET_MODE (tlocc) == VOIDmode
18796 ? Pmode : GET_MODE (tlocc),
18797 VOIDmode,
18798 VAR_INIT_STATUS_INITIALIZED);
18799 if (tval)
18800 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18801 tval);
18804 if (die != NULL)
18806 call_site_note_count++;
18807 if (ca_loc->tail_call_p)
18808 tail_call_site_note_count++;
18812 call_arg_locations = NULL;
18813 call_arg_loc_last = NULL;
18814 if (tail_call_site_count >= 0
18815 && tail_call_site_count == tail_call_site_note_count
18816 && !dwarf_strict)
18818 if (call_site_count >= 0
18819 && call_site_count == call_site_note_count)
18820 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18821 else
18822 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18824 call_site_count = -1;
18825 tail_call_site_count = -1;
18828 if (subr_die != old_die)
18829 /* Add the calling convention attribute if requested. */
18830 add_calling_convention_attribute (subr_die, decl);
18833 /* Returns a hash value for X (which really is a die_struct). */
18835 hashval_t
18836 block_die_hasher::hash (die_struct *d)
18838 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18841 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18842 as decl_id and die_parent of die_struct Y. */
18844 bool
18845 block_die_hasher::equal (die_struct *x, die_struct *y)
18847 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
18850 /* Generate a DIE to represent a declared data object.
18851 Either DECL or ORIGIN must be non-null. */
18853 static void
18854 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18856 HOST_WIDE_INT off = 0;
18857 tree com_decl;
18858 tree decl_or_origin = decl ? decl : origin;
18859 tree ultimate_origin;
18860 dw_die_ref var_die;
18861 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18862 dw_die_ref origin_die;
18863 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18864 || class_or_namespace_scope_p (context_die));
18865 bool specialization_p = false;
18867 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18868 if (decl || ultimate_origin)
18869 origin = ultimate_origin;
18870 com_decl = fortran_common (decl_or_origin, &off);
18872 /* Symbol in common gets emitted as a child of the common block, in the form
18873 of a data member. */
18874 if (com_decl)
18876 dw_die_ref com_die;
18877 dw_loc_list_ref loc;
18878 die_node com_die_arg;
18880 var_die = lookup_decl_die (decl_or_origin);
18881 if (var_die)
18883 if (get_AT (var_die, DW_AT_location) == NULL)
18885 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18886 if (loc)
18888 if (off)
18890 /* Optimize the common case. */
18891 if (single_element_loc_list_p (loc)
18892 && loc->expr->dw_loc_opc == DW_OP_addr
18893 && loc->expr->dw_loc_next == NULL
18894 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18895 == SYMBOL_REF)
18897 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18898 loc->expr->dw_loc_oprnd1.v.val_addr
18899 = plus_constant (GET_MODE (x), x , off);
18901 else
18902 loc_list_plus_const (loc, off);
18904 add_AT_location_description (var_die, DW_AT_location, loc);
18905 remove_AT (var_die, DW_AT_declaration);
18908 return;
18911 if (common_block_die_table == NULL)
18912 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
18914 com_die_arg.decl_id = DECL_UID (com_decl);
18915 com_die_arg.die_parent = context_die;
18916 com_die = common_block_die_table->find (&com_die_arg);
18917 loc = loc_list_from_tree (com_decl, 2);
18918 if (com_die == NULL)
18920 const char *cnam
18921 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18922 die_node **slot;
18924 com_die = new_die (DW_TAG_common_block, context_die, decl);
18925 add_name_and_src_coords_attributes (com_die, com_decl);
18926 if (loc)
18928 add_AT_location_description (com_die, DW_AT_location, loc);
18929 /* Avoid sharing the same loc descriptor between
18930 DW_TAG_common_block and DW_TAG_variable. */
18931 loc = loc_list_from_tree (com_decl, 2);
18933 else if (DECL_EXTERNAL (decl))
18934 add_AT_flag (com_die, DW_AT_declaration, 1);
18935 if (want_pubnames ())
18936 add_pubname_string (cnam, com_die); /* ??? needed? */
18937 com_die->decl_id = DECL_UID (com_decl);
18938 slot = common_block_die_table->find_slot (com_die, INSERT);
18939 *slot = com_die;
18941 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18943 add_AT_location_description (com_die, DW_AT_location, loc);
18944 loc = loc_list_from_tree (com_decl, 2);
18945 remove_AT (com_die, DW_AT_declaration);
18947 var_die = new_die (DW_TAG_variable, com_die, decl);
18948 add_name_and_src_coords_attributes (var_die, decl);
18949 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
18950 context_die);
18951 add_AT_flag (var_die, DW_AT_external, 1);
18952 if (loc)
18954 if (off)
18956 /* Optimize the common case. */
18957 if (single_element_loc_list_p (loc)
18958 && loc->expr->dw_loc_opc == DW_OP_addr
18959 && loc->expr->dw_loc_next == NULL
18960 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18962 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18963 loc->expr->dw_loc_oprnd1.v.val_addr
18964 = plus_constant (GET_MODE (x), x, off);
18966 else
18967 loc_list_plus_const (loc, off);
18969 add_AT_location_description (var_die, DW_AT_location, loc);
18971 else if (DECL_EXTERNAL (decl))
18972 add_AT_flag (var_die, DW_AT_declaration, 1);
18973 equate_decl_number_to_die (decl, var_die);
18974 return;
18977 /* If the compiler emitted a definition for the DECL declaration
18978 and if we already emitted a DIE for it, don't emit a second
18979 DIE for it again. Allow re-declarations of DECLs that are
18980 inside functions, though. */
18981 if (old_die && declaration && !local_scope_p (context_die))
18982 return;
18984 /* For static data members, the declaration in the class is supposed
18985 to have DW_TAG_member tag; the specification should still be
18986 DW_TAG_variable referencing the DW_TAG_member DIE. */
18987 if (declaration && class_scope_p (context_die))
18988 var_die = new_die (DW_TAG_member, context_die, decl);
18989 else
18990 var_die = new_die (DW_TAG_variable, context_die, decl);
18992 origin_die = NULL;
18993 if (origin != NULL)
18994 origin_die = add_abstract_origin_attribute (var_die, origin);
18996 /* Loop unrolling can create multiple blocks that refer to the same
18997 static variable, so we must test for the DW_AT_declaration flag.
18999 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19000 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19001 sharing them.
19003 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19005 ??? The declare_in_namespace support causes us to get two DIEs for one
19006 variable, both of which are declarations. We want to avoid considering
19007 one to be a specification, so we must test that this DIE is not a
19008 declaration. */
19009 else if (old_die && TREE_STATIC (decl) && ! declaration
19010 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19012 /* This is a definition of a C++ class level static. */
19013 add_AT_specification (var_die, old_die);
19014 specialization_p = true;
19015 if (DECL_NAME (decl))
19017 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19018 struct dwarf_file_data * file_index = lookup_filename (s.file);
19020 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19021 add_AT_file (var_die, DW_AT_decl_file, file_index);
19023 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19024 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19026 if (old_die->die_tag == DW_TAG_member)
19027 add_linkage_name (var_die, decl);
19030 else
19031 add_name_and_src_coords_attributes (var_die, decl);
19033 if ((origin == NULL && !specialization_p)
19034 || (origin != NULL
19035 && !DECL_ABSTRACT_P (decl_or_origin)
19036 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19037 decl_function_context
19038 (decl_or_origin))))
19040 tree type = TREE_TYPE (decl_or_origin);
19042 if (decl_by_reference_p (decl_or_origin))
19043 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19044 context_die);
19045 else
19046 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19047 context_die);
19050 if (origin == NULL && !specialization_p)
19052 if (TREE_PUBLIC (decl))
19053 add_AT_flag (var_die, DW_AT_external, 1);
19055 if (DECL_ARTIFICIAL (decl))
19056 add_AT_flag (var_die, DW_AT_artificial, 1);
19058 add_accessibility_attribute (var_die, decl);
19061 if (declaration)
19062 add_AT_flag (var_die, DW_AT_declaration, 1);
19064 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19065 equate_decl_number_to_die (decl, var_die);
19067 if (! declaration
19068 && (! DECL_ABSTRACT_P (decl_or_origin)
19069 /* Local static vars are shared between all clones/inlines,
19070 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19071 already set. */
19072 || (TREE_CODE (decl_or_origin) == VAR_DECL
19073 && TREE_STATIC (decl_or_origin)
19074 && DECL_RTL_SET_P (decl_or_origin)))
19075 /* When abstract origin already has DW_AT_location attribute, no need
19076 to add it again. */
19077 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19079 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19080 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19081 defer_location (decl_or_origin, var_die);
19082 else
19083 add_location_or_const_value_attribute (var_die, decl_or_origin,
19084 decl == NULL, DW_AT_location);
19085 add_pubname (decl_or_origin, var_die);
19087 else
19088 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19091 /* Generate a DIE to represent a named constant. */
19093 static void
19094 gen_const_die (tree decl, dw_die_ref context_die)
19096 dw_die_ref const_die;
19097 tree type = TREE_TYPE (decl);
19099 const_die = new_die (DW_TAG_constant, context_die, decl);
19100 add_name_and_src_coords_attributes (const_die, decl);
19101 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19102 if (TREE_PUBLIC (decl))
19103 add_AT_flag (const_die, DW_AT_external, 1);
19104 if (DECL_ARTIFICIAL (decl))
19105 add_AT_flag (const_die, DW_AT_artificial, 1);
19106 tree_add_const_value_attribute_for_decl (const_die, decl);
19109 /* Generate a DIE to represent a label identifier. */
19111 static void
19112 gen_label_die (tree decl, dw_die_ref context_die)
19114 tree origin = decl_ultimate_origin (decl);
19115 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19116 rtx insn;
19117 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19119 if (origin != NULL)
19120 add_abstract_origin_attribute (lbl_die, origin);
19121 else
19122 add_name_and_src_coords_attributes (lbl_die, decl);
19124 if (DECL_ABSTRACT_P (decl))
19125 equate_decl_number_to_die (decl, lbl_die);
19126 else
19128 insn = DECL_RTL_IF_SET (decl);
19130 /* Deleted labels are programmer specified labels which have been
19131 eliminated because of various optimizations. We still emit them
19132 here so that it is possible to put breakpoints on them. */
19133 if (insn
19134 && (LABEL_P (insn)
19135 || ((NOTE_P (insn)
19136 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19138 /* When optimization is enabled (via -O) some parts of the compiler
19139 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19140 represent source-level labels which were explicitly declared by
19141 the user. This really shouldn't be happening though, so catch
19142 it if it ever does happen. */
19143 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19145 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19146 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19148 else if (insn
19149 && NOTE_P (insn)
19150 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19151 && CODE_LABEL_NUMBER (insn) != -1)
19153 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19154 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19159 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19160 attributes to the DIE for a block STMT, to describe where the inlined
19161 function was called from. This is similar to add_src_coords_attributes. */
19163 static inline void
19164 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19166 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19168 if (dwarf_version >= 3 || !dwarf_strict)
19170 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19171 add_AT_unsigned (die, DW_AT_call_line, s.line);
19176 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19177 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19179 static inline void
19180 add_high_low_attributes (tree stmt, dw_die_ref die)
19182 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19184 if (BLOCK_FRAGMENT_CHAIN (stmt)
19185 && (dwarf_version >= 3 || !dwarf_strict))
19187 tree chain, superblock = NULL_TREE;
19188 dw_die_ref pdie;
19189 dw_attr_ref attr = NULL;
19191 if (inlined_function_outer_scope_p (stmt))
19193 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19194 BLOCK_NUMBER (stmt));
19195 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19198 /* Optimize duplicate .debug_ranges lists or even tails of
19199 lists. If this BLOCK has same ranges as its supercontext,
19200 lookup DW_AT_ranges attribute in the supercontext (and
19201 recursively so), verify that the ranges_table contains the
19202 right values and use it instead of adding a new .debug_range. */
19203 for (chain = stmt, pdie = die;
19204 BLOCK_SAME_RANGE (chain);
19205 chain = BLOCK_SUPERCONTEXT (chain))
19207 dw_attr_ref new_attr;
19209 pdie = pdie->die_parent;
19210 if (pdie == NULL)
19211 break;
19212 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19213 break;
19214 new_attr = get_AT (pdie, DW_AT_ranges);
19215 if (new_attr == NULL
19216 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19217 break;
19218 attr = new_attr;
19219 superblock = BLOCK_SUPERCONTEXT (chain);
19221 if (attr != NULL
19222 && (ranges_table[attr->dw_attr_val.v.val_offset
19223 / 2 / DWARF2_ADDR_SIZE].num
19224 == BLOCK_NUMBER (superblock))
19225 && BLOCK_FRAGMENT_CHAIN (superblock))
19227 unsigned long off = attr->dw_attr_val.v.val_offset
19228 / 2 / DWARF2_ADDR_SIZE;
19229 unsigned long supercnt = 0, thiscnt = 0;
19230 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19231 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19233 ++supercnt;
19234 gcc_checking_assert (ranges_table[off + supercnt].num
19235 == BLOCK_NUMBER (chain));
19237 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19238 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19239 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19240 ++thiscnt;
19241 gcc_assert (supercnt >= thiscnt);
19242 add_AT_range_list (die, DW_AT_ranges,
19243 ((off + supercnt - thiscnt)
19244 * 2 * DWARF2_ADDR_SIZE),
19245 false);
19246 return;
19249 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19251 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19254 add_ranges (chain);
19255 chain = BLOCK_FRAGMENT_CHAIN (chain);
19257 while (chain);
19258 add_ranges (NULL);
19260 else
19262 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19263 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19264 BLOCK_NUMBER (stmt));
19265 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19266 BLOCK_NUMBER (stmt));
19267 add_AT_low_high_pc (die, label, label_high, false);
19271 /* Generate a DIE for a lexical block. */
19273 static void
19274 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19276 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19278 if (call_arg_locations)
19280 if (block_map.length () <= BLOCK_NUMBER (stmt))
19281 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19282 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19285 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19286 add_high_low_attributes (stmt, stmt_die);
19288 decls_for_scope (stmt, stmt_die, depth);
19291 /* Generate a DIE for an inlined subprogram. */
19293 static void
19294 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19296 tree decl;
19298 /* The instance of function that is effectively being inlined shall not
19299 be abstract. */
19300 gcc_assert (! BLOCK_ABSTRACT (stmt));
19302 decl = block_ultimate_origin (stmt);
19304 /* Emit info for the abstract instance first, if we haven't yet. We
19305 must emit this even if the block is abstract, otherwise when we
19306 emit the block below (or elsewhere), we may end up trying to emit
19307 a die whose origin die hasn't been emitted, and crashing. */
19308 dwarf2out_abstract_function (decl);
19310 if (! BLOCK_ABSTRACT (stmt))
19312 dw_die_ref subr_die
19313 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19315 if (call_arg_locations)
19317 if (block_map.length () <= BLOCK_NUMBER (stmt))
19318 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19319 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19321 add_abstract_origin_attribute (subr_die, decl);
19322 if (TREE_ASM_WRITTEN (stmt))
19323 add_high_low_attributes (stmt, subr_die);
19324 add_call_src_coords_attributes (stmt, subr_die);
19326 decls_for_scope (stmt, subr_die, depth);
19330 /* Generate a DIE for a field in a record, or structure. */
19332 static void
19333 gen_field_die (tree decl, dw_die_ref context_die)
19335 dw_die_ref decl_die;
19337 if (TREE_TYPE (decl) == error_mark_node)
19338 return;
19340 decl_die = new_die (DW_TAG_member, context_die, decl);
19341 add_name_and_src_coords_attributes (decl_die, decl);
19342 add_type_attribute (decl_die, member_declared_type (decl),
19343 decl_quals (decl), context_die);
19345 if (DECL_BIT_FIELD_TYPE (decl))
19347 add_byte_size_attribute (decl_die, decl);
19348 add_bit_size_attribute (decl_die, decl);
19349 add_bit_offset_attribute (decl_die, decl);
19352 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19353 add_data_member_location_attribute (decl_die, decl);
19355 if (DECL_ARTIFICIAL (decl))
19356 add_AT_flag (decl_die, DW_AT_artificial, 1);
19358 add_accessibility_attribute (decl_die, decl);
19360 /* Equate decl number to die, so that we can look up this decl later on. */
19361 equate_decl_number_to_die (decl, decl_die);
19364 #if 0
19365 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19366 Use modified_type_die instead.
19367 We keep this code here just in case these types of DIEs may be needed to
19368 represent certain things in other languages (e.g. Pascal) someday. */
19370 static void
19371 gen_pointer_type_die (tree type, dw_die_ref context_die)
19373 dw_die_ref ptr_die
19374 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19376 equate_type_number_to_die (type, ptr_die);
19377 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19378 context_die);
19379 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19382 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19383 Use modified_type_die instead.
19384 We keep this code here just in case these types of DIEs may be needed to
19385 represent certain things in other languages (e.g. Pascal) someday. */
19387 static void
19388 gen_reference_type_die (tree type, dw_die_ref context_die)
19390 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19392 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19393 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19394 else
19395 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19397 equate_type_number_to_die (type, ref_die);
19398 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19399 context_die);
19400 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19402 #endif
19404 /* Generate a DIE for a pointer to a member type. */
19406 static void
19407 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19409 dw_die_ref ptr_die
19410 = new_die (DW_TAG_ptr_to_member_type,
19411 scope_die_for (type, context_die), type);
19413 equate_type_number_to_die (type, ptr_die);
19414 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19415 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19416 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19417 context_die);
19420 typedef const char *dchar_p; /* For DEF_VEC_P. */
19422 static char *producer_string;
19424 /* Return a heap allocated producer string including command line options
19425 if -grecord-gcc-switches. */
19427 static char *
19428 gen_producer_string (void)
19430 size_t j;
19431 auto_vec<dchar_p> switches;
19432 const char *language_string = lang_hooks.name;
19433 char *producer, *tail;
19434 const char *p;
19435 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19436 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19438 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19439 switch (save_decoded_options[j].opt_index)
19441 case OPT_o:
19442 case OPT_d:
19443 case OPT_dumpbase:
19444 case OPT_dumpdir:
19445 case OPT_auxbase:
19446 case OPT_auxbase_strip:
19447 case OPT_quiet:
19448 case OPT_version:
19449 case OPT_v:
19450 case OPT_w:
19451 case OPT_L:
19452 case OPT_D:
19453 case OPT_I:
19454 case OPT_U:
19455 case OPT_SPECIAL_unknown:
19456 case OPT_SPECIAL_ignore:
19457 case OPT_SPECIAL_program_name:
19458 case OPT_SPECIAL_input_file:
19459 case OPT_grecord_gcc_switches:
19460 case OPT_gno_record_gcc_switches:
19461 case OPT__output_pch_:
19462 case OPT_fdiagnostics_show_location_:
19463 case OPT_fdiagnostics_show_option:
19464 case OPT_fdiagnostics_show_caret:
19465 case OPT_fdiagnostics_color_:
19466 case OPT_fverbose_asm:
19467 case OPT____:
19468 case OPT__sysroot_:
19469 case OPT_nostdinc:
19470 case OPT_nostdinc__:
19471 /* Ignore these. */
19472 continue;
19473 default:
19474 if (cl_options[save_decoded_options[j].opt_index].flags
19475 & CL_NO_DWARF_RECORD)
19476 continue;
19477 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19478 == '-');
19479 switch (save_decoded_options[j].canonical_option[0][1])
19481 case 'M':
19482 case 'i':
19483 case 'W':
19484 continue;
19485 case 'f':
19486 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19487 "dump", 4) == 0)
19488 continue;
19489 break;
19490 default:
19491 break;
19493 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19494 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19495 break;
19498 producer = XNEWVEC (char, plen + 1 + len + 1);
19499 tail = producer;
19500 sprintf (tail, "%s %s", language_string, version_string);
19501 tail += plen;
19503 FOR_EACH_VEC_ELT (switches, j, p)
19505 len = strlen (p);
19506 *tail = ' ';
19507 memcpy (tail + 1, p, len);
19508 tail += len + 1;
19511 *tail = '\0';
19512 return producer;
19515 /* Given a C and/or C++ language/version string return the "highest".
19516 C++ is assumed to be "higher" than C in this case. Used for merging
19517 LTO translation unit languages. */
19518 static const char *
19519 highest_c_language (const char *lang1, const char *lang2)
19521 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19522 return "GNU C++14";
19523 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19524 return "GNU C++11";
19525 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19526 return "GNU C++98";
19528 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19529 return "GNU C11";
19530 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19531 return "GNU C99";
19532 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19533 return "GNU C89";
19535 gcc_unreachable ();
19539 /* Generate the DIE for the compilation unit. */
19541 static dw_die_ref
19542 gen_compile_unit_die (const char *filename)
19544 dw_die_ref die;
19545 const char *language_string = lang_hooks.name;
19546 int language;
19548 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19550 if (filename)
19552 add_name_attribute (die, filename);
19553 /* Don't add cwd for <built-in>. */
19554 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19555 add_comp_dir_attribute (die);
19558 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19560 /* If our producer is LTO try to figure out a common language to use
19561 from the global list of translation units. */
19562 if (strcmp (language_string, "GNU GIMPLE") == 0)
19564 unsigned i;
19565 tree t;
19566 const char *common_lang = NULL;
19568 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19570 if (!TRANSLATION_UNIT_LANGUAGE (t))
19571 continue;
19572 if (!common_lang)
19573 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19574 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19576 else if (strncmp (common_lang, "GNU C", 5) == 0
19577 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19578 /* Mixing C and C++ is ok, use C++ in that case. */
19579 common_lang = highest_c_language (common_lang,
19580 TRANSLATION_UNIT_LANGUAGE (t));
19581 else
19583 /* Fall back to C. */
19584 common_lang = NULL;
19585 break;
19589 if (common_lang)
19590 language_string = common_lang;
19593 language = DW_LANG_C;
19594 if (strncmp (language_string, "GNU C", 5) == 0
19595 && (language_string[5] == 0 || ISDIGIT (language_string[5])))
19597 language = DW_LANG_C89;
19598 if (dwarf_version >= 3 || !dwarf_strict)
19599 if (strcmp (language_string, "GNU C99") == 0)
19600 language = DW_LANG_C99;
19602 else if (strncmp (language_string, "GNU C++", 7) == 0)
19603 language = DW_LANG_C_plus_plus;
19604 else if (strcmp (language_string, "GNU F77") == 0)
19605 language = DW_LANG_Fortran77;
19606 else if (strcmp (language_string, "GNU Pascal") == 0)
19607 language = DW_LANG_Pascal83;
19608 else if (dwarf_version >= 3 || !dwarf_strict)
19610 if (strcmp (language_string, "GNU Ada") == 0)
19611 language = DW_LANG_Ada95;
19612 else if (strcmp (language_string, "GNU Fortran") == 0)
19613 language = DW_LANG_Fortran95;
19614 else if (strcmp (language_string, "GNU Java") == 0)
19615 language = DW_LANG_Java;
19616 else if (strcmp (language_string, "GNU Objective-C") == 0)
19617 language = DW_LANG_ObjC;
19618 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19619 language = DW_LANG_ObjC_plus_plus;
19620 else if (dwarf_version >= 5 || !dwarf_strict)
19622 if (strcmp (language_string, "GNU Go") == 0)
19623 language = DW_LANG_Go;
19626 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19627 else if (strcmp (language_string, "GNU Fortran") == 0)
19628 language = DW_LANG_Fortran90;
19630 add_AT_unsigned (die, DW_AT_language, language);
19632 switch (language)
19634 case DW_LANG_Fortran77:
19635 case DW_LANG_Fortran90:
19636 case DW_LANG_Fortran95:
19637 /* Fortran has case insensitive identifiers and the front-end
19638 lowercases everything. */
19639 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19640 break;
19641 default:
19642 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19643 break;
19645 return die;
19648 /* Generate the DIE for a base class. */
19650 static void
19651 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19653 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19655 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19656 add_data_member_location_attribute (die, binfo);
19658 if (BINFO_VIRTUAL_P (binfo))
19659 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19661 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19662 children, otherwise the default is DW_ACCESS_public. In DWARF2
19663 the default has always been DW_ACCESS_private. */
19664 if (access == access_public_node)
19666 if (dwarf_version == 2
19667 || context_die->die_tag == DW_TAG_class_type)
19668 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19670 else if (access == access_protected_node)
19671 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19672 else if (dwarf_version > 2
19673 && context_die->die_tag != DW_TAG_class_type)
19674 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19677 /* Generate a DIE for a class member. */
19679 static void
19680 gen_member_die (tree type, dw_die_ref context_die)
19682 tree member;
19683 tree binfo = TYPE_BINFO (type);
19684 dw_die_ref child;
19686 /* If this is not an incomplete type, output descriptions of each of its
19687 members. Note that as we output the DIEs necessary to represent the
19688 members of this record or union type, we will also be trying to output
19689 DIEs to represent the *types* of those members. However the `type'
19690 function (above) will specifically avoid generating type DIEs for member
19691 types *within* the list of member DIEs for this (containing) type except
19692 for those types (of members) which are explicitly marked as also being
19693 members of this (containing) type themselves. The g++ front- end can
19694 force any given type to be treated as a member of some other (containing)
19695 type by setting the TYPE_CONTEXT of the given (member) type to point to
19696 the TREE node representing the appropriate (containing) type. */
19698 /* First output info about the base classes. */
19699 if (binfo)
19701 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19702 int i;
19703 tree base;
19705 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19706 gen_inheritance_die (base,
19707 (accesses ? (*accesses)[i] : access_public_node),
19708 context_die);
19711 /* Now output info about the data members and type members. */
19712 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19714 /* If we thought we were generating minimal debug info for TYPE
19715 and then changed our minds, some of the member declarations
19716 may have already been defined. Don't define them again, but
19717 do put them in the right order. */
19719 child = lookup_decl_die (member);
19720 if (child)
19721 splice_child_die (context_die, child);
19722 else
19723 gen_decl_die (member, NULL, context_die);
19726 /* Now output info about the function members (if any). */
19727 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19729 /* Don't include clones in the member list. */
19730 if (DECL_ABSTRACT_ORIGIN (member))
19731 continue;
19733 child = lookup_decl_die (member);
19734 if (child)
19735 splice_child_die (context_die, child);
19736 else
19737 gen_decl_die (member, NULL, context_die);
19741 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19742 is set, we pretend that the type was never defined, so we only get the
19743 member DIEs needed by later specification DIEs. */
19745 static void
19746 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19747 enum debug_info_usage usage)
19749 dw_die_ref type_die = lookup_type_die (type);
19750 dw_die_ref scope_die = 0;
19751 int nested = 0;
19752 int complete = (TYPE_SIZE (type)
19753 && (! TYPE_STUB_DECL (type)
19754 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19755 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19756 complete = complete && should_emit_struct_debug (type, usage);
19758 if (type_die && ! complete)
19759 return;
19761 if (TYPE_CONTEXT (type) != NULL_TREE
19762 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19763 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19764 nested = 1;
19766 scope_die = scope_die_for (type, context_die);
19768 /* Generate child dies for template paramaters. */
19769 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19770 schedule_generic_params_dies_gen (type);
19772 if (! type_die || (nested && is_cu_die (scope_die)))
19773 /* First occurrence of type or toplevel definition of nested class. */
19775 dw_die_ref old_die = type_die;
19777 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19778 ? record_type_tag (type) : DW_TAG_union_type,
19779 scope_die, type);
19780 equate_type_number_to_die (type, type_die);
19781 if (old_die)
19782 add_AT_specification (type_die, old_die);
19783 else
19784 add_name_attribute (type_die, type_tag (type));
19786 else
19787 remove_AT (type_die, DW_AT_declaration);
19789 /* If this type has been completed, then give it a byte_size attribute and
19790 then give a list of members. */
19791 if (complete && !ns_decl)
19793 /* Prevent infinite recursion in cases where the type of some member of
19794 this type is expressed in terms of this type itself. */
19795 TREE_ASM_WRITTEN (type) = 1;
19796 add_byte_size_attribute (type_die, type);
19797 if (TYPE_STUB_DECL (type) != NULL_TREE)
19799 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19800 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19803 /* If the first reference to this type was as the return type of an
19804 inline function, then it may not have a parent. Fix this now. */
19805 if (type_die->die_parent == NULL)
19806 add_child_die (scope_die, type_die);
19808 push_decl_scope (type);
19809 gen_member_die (type, type_die);
19810 pop_decl_scope ();
19812 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19813 if (TYPE_ARTIFICIAL (type))
19814 add_AT_flag (type_die, DW_AT_artificial, 1);
19816 /* GNU extension: Record what type our vtable lives in. */
19817 if (TYPE_VFIELD (type))
19819 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19821 gen_type_die (vtype, context_die);
19822 add_AT_die_ref (type_die, DW_AT_containing_type,
19823 lookup_type_die (vtype));
19826 else
19828 add_AT_flag (type_die, DW_AT_declaration, 1);
19830 /* We don't need to do this for function-local types. */
19831 if (TYPE_STUB_DECL (type)
19832 && ! decl_function_context (TYPE_STUB_DECL (type)))
19833 vec_safe_push (incomplete_types, type);
19836 if (get_AT (type_die, DW_AT_name))
19837 add_pubtype (type, type_die);
19840 /* Generate a DIE for a subroutine _type_. */
19842 static void
19843 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19845 tree return_type = TREE_TYPE (type);
19846 dw_die_ref subr_die
19847 = new_die (DW_TAG_subroutine_type,
19848 scope_die_for (type, context_die), type);
19850 equate_type_number_to_die (type, subr_die);
19851 add_prototyped_attribute (subr_die, type);
19852 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
19853 gen_formal_types_die (type, subr_die);
19855 if (get_AT (subr_die, DW_AT_name))
19856 add_pubtype (type, subr_die);
19859 /* Generate a DIE for a type definition. */
19861 static void
19862 gen_typedef_die (tree decl, dw_die_ref context_die)
19864 dw_die_ref type_die;
19865 tree origin;
19867 if (TREE_ASM_WRITTEN (decl))
19868 return;
19870 TREE_ASM_WRITTEN (decl) = 1;
19871 type_die = new_die (DW_TAG_typedef, context_die, decl);
19872 origin = decl_ultimate_origin (decl);
19873 if (origin != NULL)
19874 add_abstract_origin_attribute (type_die, origin);
19875 else
19877 tree type;
19879 add_name_and_src_coords_attributes (type_die, decl);
19880 if (DECL_ORIGINAL_TYPE (decl))
19882 type = DECL_ORIGINAL_TYPE (decl);
19884 gcc_assert (type != TREE_TYPE (decl));
19885 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19887 else
19889 type = TREE_TYPE (decl);
19891 if (is_naming_typedef_decl (TYPE_NAME (type)))
19893 /* Here, we are in the case of decl being a typedef naming
19894 an anonymous type, e.g:
19895 typedef struct {...} foo;
19896 In that case TREE_TYPE (decl) is not a typedef variant
19897 type and TYPE_NAME of the anonymous type is set to the
19898 TYPE_DECL of the typedef. This construct is emitted by
19899 the C++ FE.
19901 TYPE is the anonymous struct named by the typedef
19902 DECL. As we need the DW_AT_type attribute of the
19903 DW_TAG_typedef to point to the DIE of TYPE, let's
19904 generate that DIE right away. add_type_attribute
19905 called below will then pick (via lookup_type_die) that
19906 anonymous struct DIE. */
19907 if (!TREE_ASM_WRITTEN (type))
19908 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19910 /* This is a GNU Extension. We are adding a
19911 DW_AT_linkage_name attribute to the DIE of the
19912 anonymous struct TYPE. The value of that attribute
19913 is the name of the typedef decl naming the anonymous
19914 struct. This greatly eases the work of consumers of
19915 this debug info. */
19916 add_linkage_attr (lookup_type_die (type), decl);
19920 add_type_attribute (type_die, type, decl_quals (decl), context_die);
19922 if (is_naming_typedef_decl (decl))
19923 /* We want that all subsequent calls to lookup_type_die with
19924 TYPE in argument yield the DW_TAG_typedef we have just
19925 created. */
19926 equate_type_number_to_die (type, type_die);
19928 add_accessibility_attribute (type_die, decl);
19931 if (DECL_ABSTRACT_P (decl))
19932 equate_decl_number_to_die (decl, type_die);
19934 if (get_AT (type_die, DW_AT_name))
19935 add_pubtype (decl, type_die);
19938 /* Generate a DIE for a struct, class, enum or union type. */
19940 static void
19941 gen_tagged_type_die (tree type,
19942 dw_die_ref context_die,
19943 enum debug_info_usage usage)
19945 int need_pop;
19947 if (type == NULL_TREE
19948 || !is_tagged_type (type))
19949 return;
19951 /* If this is a nested type whose containing class hasn't been written
19952 out yet, writing it out will cover this one, too. This does not apply
19953 to instantiations of member class templates; they need to be added to
19954 the containing class as they are generated. FIXME: This hurts the
19955 idea of combining type decls from multiple TUs, since we can't predict
19956 what set of template instantiations we'll get. */
19957 if (TYPE_CONTEXT (type)
19958 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19959 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19961 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19963 if (TREE_ASM_WRITTEN (type))
19964 return;
19966 /* If that failed, attach ourselves to the stub. */
19967 push_decl_scope (TYPE_CONTEXT (type));
19968 context_die = lookup_type_die (TYPE_CONTEXT (type));
19969 need_pop = 1;
19971 else if (TYPE_CONTEXT (type) != NULL_TREE
19972 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19974 /* If this type is local to a function that hasn't been written
19975 out yet, use a NULL context for now; it will be fixed up in
19976 decls_for_scope. */
19977 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19978 /* A declaration DIE doesn't count; nested types need to go in the
19979 specification. */
19980 if (context_die && is_declaration_die (context_die))
19981 context_die = NULL;
19982 need_pop = 0;
19984 else
19986 context_die = declare_in_namespace (type, context_die);
19987 need_pop = 0;
19990 if (TREE_CODE (type) == ENUMERAL_TYPE)
19992 /* This might have been written out by the call to
19993 declare_in_namespace. */
19994 if (!TREE_ASM_WRITTEN (type))
19995 gen_enumeration_type_die (type, context_die);
19997 else
19998 gen_struct_or_union_type_die (type, context_die, usage);
20000 if (need_pop)
20001 pop_decl_scope ();
20003 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20004 it up if it is ever completed. gen_*_type_die will set it for us
20005 when appropriate. */
20008 /* Generate a type description DIE. */
20010 static void
20011 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20012 enum debug_info_usage usage)
20014 struct array_descr_info info;
20016 if (type == NULL_TREE || type == error_mark_node)
20017 return;
20019 if (TYPE_NAME (type) != NULL_TREE
20020 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20021 && is_redundant_typedef (TYPE_NAME (type))
20022 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20023 /* The DECL of this type is a typedef we don't want to emit debug
20024 info for but we want debug info for its underlying typedef.
20025 This can happen for e.g, the injected-class-name of a C++
20026 type. */
20027 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20029 /* If TYPE is a typedef type variant, let's generate debug info
20030 for the parent typedef which TYPE is a type of. */
20031 if (typedef_variant_p (type))
20033 if (TREE_ASM_WRITTEN (type))
20034 return;
20036 /* Prevent broken recursion; we can't hand off to the same type. */
20037 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20039 /* Give typedefs the right scope. */
20040 context_die = scope_die_for (type, context_die);
20042 TREE_ASM_WRITTEN (type) = 1;
20044 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20045 return;
20048 /* If type is an anonymous tagged type named by a typedef, let's
20049 generate debug info for the typedef. */
20050 if (is_naming_typedef_decl (TYPE_NAME (type)))
20052 /* Use the DIE of the containing namespace as the parent DIE of
20053 the type description DIE we want to generate. */
20054 if (DECL_CONTEXT (TYPE_NAME (type))
20055 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20056 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20058 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20059 return;
20062 /* If this is an array type with hidden descriptor, handle it first. */
20063 if (!TREE_ASM_WRITTEN (type)
20064 && lang_hooks.types.get_array_descr_info
20065 && lang_hooks.types.get_array_descr_info (type, &info)
20066 && (dwarf_version >= 3 || !dwarf_strict))
20068 gen_descr_array_type_die (type, &info, context_die);
20069 TREE_ASM_WRITTEN (type) = 1;
20070 return;
20073 /* We are going to output a DIE to represent the unqualified version
20074 of this type (i.e. without any const or volatile qualifiers) so
20075 get the main variant (i.e. the unqualified version) of this type
20076 now. (Vectors are special because the debugging info is in the
20077 cloned type itself). */
20078 if (TREE_CODE (type) != VECTOR_TYPE)
20079 type = type_main_variant (type);
20081 if (TREE_ASM_WRITTEN (type))
20082 return;
20084 switch (TREE_CODE (type))
20086 case ERROR_MARK:
20087 break;
20089 case POINTER_TYPE:
20090 case REFERENCE_TYPE:
20091 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20092 ensures that the gen_type_die recursion will terminate even if the
20093 type is recursive. Recursive types are possible in Ada. */
20094 /* ??? We could perhaps do this for all types before the switch
20095 statement. */
20096 TREE_ASM_WRITTEN (type) = 1;
20098 /* For these types, all that is required is that we output a DIE (or a
20099 set of DIEs) to represent the "basis" type. */
20100 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20101 DINFO_USAGE_IND_USE);
20102 break;
20104 case OFFSET_TYPE:
20105 /* This code is used for C++ pointer-to-data-member types.
20106 Output a description of the relevant class type. */
20107 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20108 DINFO_USAGE_IND_USE);
20110 /* Output a description of the type of the object pointed to. */
20111 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20112 DINFO_USAGE_IND_USE);
20114 /* Now output a DIE to represent this pointer-to-data-member type
20115 itself. */
20116 gen_ptr_to_mbr_type_die (type, context_die);
20117 break;
20119 case FUNCTION_TYPE:
20120 /* Force out return type (in case it wasn't forced out already). */
20121 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20122 DINFO_USAGE_DIR_USE);
20123 gen_subroutine_type_die (type, context_die);
20124 break;
20126 case METHOD_TYPE:
20127 /* Force out return type (in case it wasn't forced out already). */
20128 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20129 DINFO_USAGE_DIR_USE);
20130 gen_subroutine_type_die (type, context_die);
20131 break;
20133 case ARRAY_TYPE:
20134 gen_array_type_die (type, context_die);
20135 break;
20137 case VECTOR_TYPE:
20138 gen_array_type_die (type, context_die);
20139 break;
20141 case ENUMERAL_TYPE:
20142 case RECORD_TYPE:
20143 case UNION_TYPE:
20144 case QUAL_UNION_TYPE:
20145 gen_tagged_type_die (type, context_die, usage);
20146 return;
20148 case VOID_TYPE:
20149 case INTEGER_TYPE:
20150 case REAL_TYPE:
20151 case FIXED_POINT_TYPE:
20152 case COMPLEX_TYPE:
20153 case BOOLEAN_TYPE:
20154 case POINTER_BOUNDS_TYPE:
20155 /* No DIEs needed for fundamental types. */
20156 break;
20158 case NULLPTR_TYPE:
20159 case LANG_TYPE:
20160 /* Just use DW_TAG_unspecified_type. */
20162 dw_die_ref type_die = lookup_type_die (type);
20163 if (type_die == NULL)
20165 tree name = TYPE_IDENTIFIER (type);
20166 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20167 type);
20168 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20169 equate_type_number_to_die (type, type_die);
20172 break;
20174 default:
20175 if (is_cxx_auto (type))
20177 tree name = TYPE_IDENTIFIER (type);
20178 dw_die_ref *die = (name == get_identifier ("auto")
20179 ? &auto_die : &decltype_auto_die);
20180 if (!*die)
20182 *die = new_die (DW_TAG_unspecified_type,
20183 comp_unit_die (), NULL_TREE);
20184 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20186 equate_type_number_to_die (type, *die);
20187 break;
20189 gcc_unreachable ();
20192 TREE_ASM_WRITTEN (type) = 1;
20195 static void
20196 gen_type_die (tree type, dw_die_ref context_die)
20198 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20201 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20202 things which are local to the given block. */
20204 static void
20205 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20207 int must_output_die = 0;
20208 bool inlined_func;
20210 /* Ignore blocks that are NULL. */
20211 if (stmt == NULL_TREE)
20212 return;
20214 inlined_func = inlined_function_outer_scope_p (stmt);
20216 /* If the block is one fragment of a non-contiguous block, do not
20217 process the variables, since they will have been done by the
20218 origin block. Do process subblocks. */
20219 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20221 tree sub;
20223 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20224 gen_block_die (sub, context_die, depth + 1);
20226 return;
20229 /* Determine if we need to output any Dwarf DIEs at all to represent this
20230 block. */
20231 if (inlined_func)
20232 /* The outer scopes for inlinings *must* always be represented. We
20233 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20234 must_output_die = 1;
20235 else
20237 /* Determine if this block directly contains any "significant"
20238 local declarations which we will need to output DIEs for. */
20239 if (debug_info_level > DINFO_LEVEL_TERSE)
20240 /* We are not in terse mode so *any* local declaration counts
20241 as being a "significant" one. */
20242 must_output_die = ((BLOCK_VARS (stmt) != NULL
20243 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20244 && (TREE_USED (stmt)
20245 || TREE_ASM_WRITTEN (stmt)
20246 || BLOCK_ABSTRACT (stmt)));
20247 else if ((TREE_USED (stmt)
20248 || TREE_ASM_WRITTEN (stmt)
20249 || BLOCK_ABSTRACT (stmt))
20250 && !dwarf2out_ignore_block (stmt))
20251 must_output_die = 1;
20254 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20255 DIE for any block which contains no significant local declarations at
20256 all. Rather, in such cases we just call `decls_for_scope' so that any
20257 needed Dwarf info for any sub-blocks will get properly generated. Note
20258 that in terse mode, our definition of what constitutes a "significant"
20259 local declaration gets restricted to include only inlined function
20260 instances and local (nested) function definitions. */
20261 if (must_output_die)
20263 if (inlined_func)
20265 /* If STMT block is abstract, that means we have been called
20266 indirectly from dwarf2out_abstract_function.
20267 That function rightfully marks the descendent blocks (of
20268 the abstract function it is dealing with) as being abstract,
20269 precisely to prevent us from emitting any
20270 DW_TAG_inlined_subroutine DIE as a descendent
20271 of an abstract function instance. So in that case, we should
20272 not call gen_inlined_subroutine_die.
20274 Later though, when cgraph asks dwarf2out to emit info
20275 for the concrete instance of the function decl into which
20276 the concrete instance of STMT got inlined, the later will lead
20277 to the generation of a DW_TAG_inlined_subroutine DIE. */
20278 if (! BLOCK_ABSTRACT (stmt))
20279 gen_inlined_subroutine_die (stmt, context_die, depth);
20281 else
20282 gen_lexical_block_die (stmt, context_die, depth);
20284 else
20285 decls_for_scope (stmt, context_die, depth);
20288 /* Process variable DECL (or variable with origin ORIGIN) within
20289 block STMT and add it to CONTEXT_DIE. */
20290 static void
20291 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20293 dw_die_ref die;
20294 tree decl_or_origin = decl ? decl : origin;
20296 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20297 die = lookup_decl_die (decl_or_origin);
20298 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20299 && TYPE_DECL_IS_STUB (decl_or_origin))
20300 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20301 else
20302 die = NULL;
20304 if (die != NULL && die->die_parent == NULL)
20305 add_child_die (context_die, die);
20306 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20307 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20308 stmt, context_die);
20309 else
20310 gen_decl_die (decl, origin, context_die);
20313 /* Generate all of the decls declared within a given scope and (recursively)
20314 all of its sub-blocks. */
20316 static void
20317 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20319 tree decl;
20320 unsigned int i;
20321 tree subblocks;
20323 /* Ignore NULL blocks. */
20324 if (stmt == NULL_TREE)
20325 return;
20327 /* Output the DIEs to represent all of the data objects and typedefs
20328 declared directly within this block but not within any nested
20329 sub-blocks. Also, nested function and tag DIEs have been
20330 generated with a parent of NULL; fix that up now. We don't
20331 have to do this if we're at -g1. */
20332 if (debug_info_level > DINFO_LEVEL_TERSE)
20334 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20335 process_scope_var (stmt, decl, NULL_TREE, context_die);
20336 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20337 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20338 context_die);
20341 /* Even if we're at -g1, we need to process the subblocks in order to get
20342 inlined call information. */
20344 /* Output the DIEs to represent all sub-blocks (and the items declared
20345 therein) of this block. */
20346 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20347 subblocks != NULL;
20348 subblocks = BLOCK_CHAIN (subblocks))
20349 gen_block_die (subblocks, context_die, depth + 1);
20352 /* Is this a typedef we can avoid emitting? */
20354 static inline int
20355 is_redundant_typedef (const_tree decl)
20357 if (TYPE_DECL_IS_STUB (decl))
20358 return 1;
20360 if (DECL_ARTIFICIAL (decl)
20361 && DECL_CONTEXT (decl)
20362 && is_tagged_type (DECL_CONTEXT (decl))
20363 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20364 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20365 /* Also ignore the artificial member typedef for the class name. */
20366 return 1;
20368 return 0;
20371 /* Return TRUE if TYPE is a typedef that names a type for linkage
20372 purposes. This kind of typedefs is produced by the C++ FE for
20373 constructs like:
20375 typedef struct {...} foo;
20377 In that case, there is no typedef variant type produced for foo.
20378 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20379 struct type. */
20381 static bool
20382 is_naming_typedef_decl (const_tree decl)
20384 if (decl == NULL_TREE
20385 || TREE_CODE (decl) != TYPE_DECL
20386 || !is_tagged_type (TREE_TYPE (decl))
20387 || DECL_IS_BUILTIN (decl)
20388 || is_redundant_typedef (decl)
20389 /* It looks like Ada produces TYPE_DECLs that are very similar
20390 to C++ naming typedefs but that have different
20391 semantics. Let's be specific to c++ for now. */
20392 || !is_cxx ())
20393 return FALSE;
20395 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20396 && TYPE_NAME (TREE_TYPE (decl)) == decl
20397 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20398 != TYPE_NAME (TREE_TYPE (decl))));
20401 /* Returns the DIE for a context. */
20403 static inline dw_die_ref
20404 get_context_die (tree context)
20406 if (context)
20408 /* Find die that represents this context. */
20409 if (TYPE_P (context))
20411 context = TYPE_MAIN_VARIANT (context);
20412 return strip_naming_typedef (context, force_type_die (context));
20414 else
20415 return force_decl_die (context);
20417 return comp_unit_die ();
20420 /* Returns the DIE for decl. A DIE will always be returned. */
20422 static dw_die_ref
20423 force_decl_die (tree decl)
20425 dw_die_ref decl_die;
20426 unsigned saved_external_flag;
20427 tree save_fn = NULL_TREE;
20428 decl_die = lookup_decl_die (decl);
20429 if (!decl_die)
20431 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20433 decl_die = lookup_decl_die (decl);
20434 if (decl_die)
20435 return decl_die;
20437 switch (TREE_CODE (decl))
20439 case FUNCTION_DECL:
20440 /* Clear current_function_decl, so that gen_subprogram_die thinks
20441 that this is a declaration. At this point, we just want to force
20442 declaration die. */
20443 save_fn = current_function_decl;
20444 current_function_decl = NULL_TREE;
20445 gen_subprogram_die (decl, context_die);
20446 current_function_decl = save_fn;
20447 break;
20449 case VAR_DECL:
20450 /* Set external flag to force declaration die. Restore it after
20451 gen_decl_die() call. */
20452 saved_external_flag = DECL_EXTERNAL (decl);
20453 DECL_EXTERNAL (decl) = 1;
20454 gen_decl_die (decl, NULL, context_die);
20455 DECL_EXTERNAL (decl) = saved_external_flag;
20456 break;
20458 case NAMESPACE_DECL:
20459 if (dwarf_version >= 3 || !dwarf_strict)
20460 dwarf2out_decl (decl);
20461 else
20462 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20463 decl_die = comp_unit_die ();
20464 break;
20466 case TRANSLATION_UNIT_DECL:
20467 decl_die = comp_unit_die ();
20468 break;
20470 default:
20471 gcc_unreachable ();
20474 /* We should be able to find the DIE now. */
20475 if (!decl_die)
20476 decl_die = lookup_decl_die (decl);
20477 gcc_assert (decl_die);
20480 return decl_die;
20483 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20484 always returned. */
20486 static dw_die_ref
20487 force_type_die (tree type)
20489 dw_die_ref type_die;
20491 type_die = lookup_type_die (type);
20492 if (!type_die)
20494 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20496 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20497 context_die);
20498 gcc_assert (type_die);
20500 return type_die;
20503 /* Force out any required namespaces to be able to output DECL,
20504 and return the new context_die for it, if it's changed. */
20506 static dw_die_ref
20507 setup_namespace_context (tree thing, dw_die_ref context_die)
20509 tree context = (DECL_P (thing)
20510 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20511 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20512 /* Force out the namespace. */
20513 context_die = force_decl_die (context);
20515 return context_die;
20518 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20519 type) within its namespace, if appropriate.
20521 For compatibility with older debuggers, namespace DIEs only contain
20522 declarations; all definitions are emitted at CU scope. */
20524 static dw_die_ref
20525 declare_in_namespace (tree thing, dw_die_ref context_die)
20527 dw_die_ref ns_context;
20529 if (debug_info_level <= DINFO_LEVEL_TERSE)
20530 return context_die;
20532 /* External declarations in the local scope only need to be emitted
20533 once, not once in the namespace and once in the scope.
20535 This avoids declaring the `extern' below in the
20536 namespace DIE as well as in the innermost scope:
20538 namespace S
20540 int i=5;
20541 int foo()
20543 int i=8;
20544 extern int i;
20545 return i;
20549 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20550 return context_die;
20552 /* If this decl is from an inlined function, then don't try to emit it in its
20553 namespace, as we will get confused. It would have already been emitted
20554 when the abstract instance of the inline function was emitted anyways. */
20555 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20556 return context_die;
20558 ns_context = setup_namespace_context (thing, context_die);
20560 if (ns_context != context_die)
20562 if (is_fortran ())
20563 return ns_context;
20564 if (DECL_P (thing))
20565 gen_decl_die (thing, NULL, ns_context);
20566 else
20567 gen_type_die (thing, ns_context);
20569 return context_die;
20572 /* Generate a DIE for a namespace or namespace alias. */
20574 static void
20575 gen_namespace_die (tree decl, dw_die_ref context_die)
20577 dw_die_ref namespace_die;
20579 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20580 they are an alias of. */
20581 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20583 /* Output a real namespace or module. */
20584 context_die = setup_namespace_context (decl, comp_unit_die ());
20585 namespace_die = new_die (is_fortran ()
20586 ? DW_TAG_module : DW_TAG_namespace,
20587 context_die, decl);
20588 /* For Fortran modules defined in different CU don't add src coords. */
20589 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20591 const char *name = dwarf2_name (decl, 0);
20592 if (name)
20593 add_name_attribute (namespace_die, name);
20595 else
20596 add_name_and_src_coords_attributes (namespace_die, decl);
20597 if (DECL_EXTERNAL (decl))
20598 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20599 equate_decl_number_to_die (decl, namespace_die);
20601 else
20603 /* Output a namespace alias. */
20605 /* Force out the namespace we are an alias of, if necessary. */
20606 dw_die_ref origin_die
20607 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20609 if (DECL_FILE_SCOPE_P (decl)
20610 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20611 context_die = setup_namespace_context (decl, comp_unit_die ());
20612 /* Now create the namespace alias DIE. */
20613 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20614 add_name_and_src_coords_attributes (namespace_die, decl);
20615 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20616 equate_decl_number_to_die (decl, namespace_die);
20618 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20619 if (want_pubnames ())
20620 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20623 /* Generate Dwarf debug information for a decl described by DECL.
20624 The return value is currently only meaningful for PARM_DECLs,
20625 for all other decls it returns NULL. */
20627 static dw_die_ref
20628 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20630 tree decl_or_origin = decl ? decl : origin;
20631 tree class_origin = NULL, ultimate_origin;
20633 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20634 return NULL;
20636 /* Ignore pointer bounds decls. */
20637 if (DECL_P (decl_or_origin)
20638 && TREE_TYPE (decl_or_origin)
20639 && POINTER_BOUNDS_P (decl_or_origin))
20640 return NULL;
20642 switch (TREE_CODE (decl_or_origin))
20644 case ERROR_MARK:
20645 break;
20647 case CONST_DECL:
20648 if (!is_fortran () && !is_ada ())
20650 /* The individual enumerators of an enum type get output when we output
20651 the Dwarf representation of the relevant enum type itself. */
20652 break;
20655 /* Emit its type. */
20656 gen_type_die (TREE_TYPE (decl), context_die);
20658 /* And its containing namespace. */
20659 context_die = declare_in_namespace (decl, context_die);
20661 gen_const_die (decl, context_die);
20662 break;
20664 case FUNCTION_DECL:
20665 /* Don't output any DIEs to represent mere function declarations,
20666 unless they are class members or explicit block externs. */
20667 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20668 && DECL_FILE_SCOPE_P (decl_or_origin)
20669 && (current_function_decl == NULL_TREE
20670 || DECL_ARTIFICIAL (decl_or_origin)))
20671 break;
20673 #if 0
20674 /* FIXME */
20675 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20676 on local redeclarations of global functions. That seems broken. */
20677 if (current_function_decl != decl)
20678 /* This is only a declaration. */;
20679 #endif
20681 /* If we're emitting a clone, emit info for the abstract instance. */
20682 if (origin || DECL_ORIGIN (decl) != decl)
20683 dwarf2out_abstract_function (origin
20684 ? DECL_ORIGIN (origin)
20685 : DECL_ABSTRACT_ORIGIN (decl));
20687 /* If we're emitting an out-of-line copy of an inline function,
20688 emit info for the abstract instance and set up to refer to it. */
20689 else if (cgraph_function_possibly_inlined_p (decl)
20690 && ! DECL_ABSTRACT_P (decl)
20691 && ! class_or_namespace_scope_p (context_die)
20692 /* dwarf2out_abstract_function won't emit a die if this is just
20693 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20694 that case, because that works only if we have a die. */
20695 && DECL_INITIAL (decl) != NULL_TREE)
20697 dwarf2out_abstract_function (decl);
20698 set_decl_origin_self (decl);
20701 /* Otherwise we're emitting the primary DIE for this decl. */
20702 else if (debug_info_level > DINFO_LEVEL_TERSE)
20704 /* Before we describe the FUNCTION_DECL itself, make sure that we
20705 have its containing type. */
20706 if (!origin)
20707 origin = decl_class_context (decl);
20708 if (origin != NULL_TREE)
20709 gen_type_die (origin, context_die);
20711 /* And its return type. */
20712 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20714 /* And its virtual context. */
20715 if (DECL_VINDEX (decl) != NULL_TREE)
20716 gen_type_die (DECL_CONTEXT (decl), context_die);
20718 /* Make sure we have a member DIE for decl. */
20719 if (origin != NULL_TREE)
20720 gen_type_die_for_member (origin, decl, context_die);
20722 /* And its containing namespace. */
20723 context_die = declare_in_namespace (decl, context_die);
20726 /* Now output a DIE to represent the function itself. */
20727 if (decl)
20728 gen_subprogram_die (decl, context_die);
20729 break;
20731 case TYPE_DECL:
20732 /* If we are in terse mode, don't generate any DIEs to represent any
20733 actual typedefs. */
20734 if (debug_info_level <= DINFO_LEVEL_TERSE)
20735 break;
20737 /* In the special case of a TYPE_DECL node representing the declaration
20738 of some type tag, if the given TYPE_DECL is marked as having been
20739 instantiated from some other (original) TYPE_DECL node (e.g. one which
20740 was generated within the original definition of an inline function) we
20741 used to generate a special (abbreviated) DW_TAG_structure_type,
20742 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20743 should be actually referencing those DIEs, as variable DIEs with that
20744 type would be emitted already in the abstract origin, so it was always
20745 removed during unused type prunning. Don't add anything in this
20746 case. */
20747 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20748 break;
20750 if (is_redundant_typedef (decl))
20751 gen_type_die (TREE_TYPE (decl), context_die);
20752 else
20753 /* Output a DIE to represent the typedef itself. */
20754 gen_typedef_die (decl, context_die);
20755 break;
20757 case LABEL_DECL:
20758 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20759 gen_label_die (decl, context_die);
20760 break;
20762 case VAR_DECL:
20763 case RESULT_DECL:
20764 /* If we are in terse mode, don't generate any DIEs to represent any
20765 variable declarations or definitions. */
20766 if (debug_info_level <= DINFO_LEVEL_TERSE)
20767 break;
20769 /* Output any DIEs that are needed to specify the type of this data
20770 object. */
20771 if (decl_by_reference_p (decl_or_origin))
20772 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20773 else
20774 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20776 /* And its containing type. */
20777 class_origin = decl_class_context (decl_or_origin);
20778 if (class_origin != NULL_TREE)
20779 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20781 /* And its containing namespace. */
20782 context_die = declare_in_namespace (decl_or_origin, context_die);
20784 /* Now output the DIE to represent the data object itself. This gets
20785 complicated because of the possibility that the VAR_DECL really
20786 represents an inlined instance of a formal parameter for an inline
20787 function. */
20788 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20789 if (ultimate_origin != NULL_TREE
20790 && TREE_CODE (ultimate_origin) == PARM_DECL)
20791 gen_formal_parameter_die (decl, origin,
20792 true /* Emit name attribute. */,
20793 context_die);
20794 else
20795 gen_variable_die (decl, origin, context_die);
20796 break;
20798 case FIELD_DECL:
20799 /* Ignore the nameless fields that are used to skip bits but handle C++
20800 anonymous unions and structs. */
20801 if (DECL_NAME (decl) != NULL_TREE
20802 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20803 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20805 gen_type_die (member_declared_type (decl), context_die);
20806 gen_field_die (decl, context_die);
20808 break;
20810 case PARM_DECL:
20811 if (DECL_BY_REFERENCE (decl_or_origin))
20812 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20813 else
20814 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20815 return gen_formal_parameter_die (decl, origin,
20816 true /* Emit name attribute. */,
20817 context_die);
20819 case NAMESPACE_DECL:
20820 case IMPORTED_DECL:
20821 if (dwarf_version >= 3 || !dwarf_strict)
20822 gen_namespace_die (decl, context_die);
20823 break;
20825 case NAMELIST_DECL:
20826 gen_namelist_decl (DECL_NAME (decl), context_die,
20827 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20828 break;
20830 default:
20831 /* Probably some frontend-internal decl. Assume we don't care. */
20832 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20833 break;
20836 return NULL;
20839 /* Output debug information for global decl DECL. Called from toplev.c after
20840 compilation proper has finished. */
20842 static void
20843 dwarf2out_global_decl (tree decl)
20845 /* Output DWARF2 information for file-scope tentative data object
20846 declarations, file-scope (extern) function declarations (which
20847 had no corresponding body) and file-scope tagged type declarations
20848 and definitions which have not yet been forced out. */
20849 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20850 && !POINTER_BOUNDS_P (decl))
20851 dwarf2out_decl (decl);
20854 /* Output debug information for type decl DECL. Called from toplev.c
20855 and from language front ends (to record built-in types). */
20856 static void
20857 dwarf2out_type_decl (tree decl, int local)
20859 if (!local)
20860 dwarf2out_decl (decl);
20863 /* Output debug information for imported module or decl DECL.
20864 NAME is non-NULL name in the lexical block if the decl has been renamed.
20865 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20866 that DECL belongs to.
20867 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20868 static void
20869 dwarf2out_imported_module_or_decl_1 (tree decl,
20870 tree name,
20871 tree lexical_block,
20872 dw_die_ref lexical_block_die)
20874 expanded_location xloc;
20875 dw_die_ref imported_die = NULL;
20876 dw_die_ref at_import_die;
20878 if (TREE_CODE (decl) == IMPORTED_DECL)
20880 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20881 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20882 gcc_assert (decl);
20884 else
20885 xloc = expand_location (input_location);
20887 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20889 at_import_die = force_type_die (TREE_TYPE (decl));
20890 /* For namespace N { typedef void T; } using N::T; base_type_die
20891 returns NULL, but DW_TAG_imported_declaration requires
20892 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20893 if (!at_import_die)
20895 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20896 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20897 at_import_die = lookup_type_die (TREE_TYPE (decl));
20898 gcc_assert (at_import_die);
20901 else
20903 at_import_die = lookup_decl_die (decl);
20904 if (!at_import_die)
20906 /* If we're trying to avoid duplicate debug info, we may not have
20907 emitted the member decl for this field. Emit it now. */
20908 if (TREE_CODE (decl) == FIELD_DECL)
20910 tree type = DECL_CONTEXT (decl);
20912 if (TYPE_CONTEXT (type)
20913 && TYPE_P (TYPE_CONTEXT (type))
20914 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20915 DINFO_USAGE_DIR_USE))
20916 return;
20917 gen_type_die_for_member (type, decl,
20918 get_context_die (TYPE_CONTEXT (type)));
20920 if (TREE_CODE (decl) == NAMELIST_DECL)
20921 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20922 get_context_die (DECL_CONTEXT (decl)),
20923 NULL_TREE);
20924 else
20925 at_import_die = force_decl_die (decl);
20929 if (TREE_CODE (decl) == NAMESPACE_DECL)
20931 if (dwarf_version >= 3 || !dwarf_strict)
20932 imported_die = new_die (DW_TAG_imported_module,
20933 lexical_block_die,
20934 lexical_block);
20935 else
20936 return;
20938 else
20939 imported_die = new_die (DW_TAG_imported_declaration,
20940 lexical_block_die,
20941 lexical_block);
20943 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20944 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20945 if (name)
20946 add_AT_string (imported_die, DW_AT_name,
20947 IDENTIFIER_POINTER (name));
20948 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20951 /* Output debug information for imported module or decl DECL.
20952 NAME is non-NULL name in context if the decl has been renamed.
20953 CHILD is true if decl is one of the renamed decls as part of
20954 importing whole module. */
20956 static void
20957 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20958 bool child)
20960 /* dw_die_ref at_import_die; */
20961 dw_die_ref scope_die;
20963 if (debug_info_level <= DINFO_LEVEL_TERSE)
20964 return;
20966 gcc_assert (decl);
20968 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20969 We need decl DIE for reference and scope die. First, get DIE for the decl
20970 itself. */
20972 /* Get the scope die for decl context. Use comp_unit_die for global module
20973 or decl. If die is not found for non globals, force new die. */
20974 if (context
20975 && TYPE_P (context)
20976 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20977 return;
20979 if (!(dwarf_version >= 3 || !dwarf_strict))
20980 return;
20982 scope_die = get_context_die (context);
20984 if (child)
20986 gcc_assert (scope_die->die_child);
20987 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20988 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20989 scope_die = scope_die->die_child;
20992 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20993 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20997 /* Output debug information for namelists. */
20999 static dw_die_ref
21000 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21002 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21003 tree value;
21004 unsigned i;
21006 if (debug_info_level <= DINFO_LEVEL_TERSE)
21007 return NULL;
21009 gcc_assert (scope_die != NULL);
21010 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21011 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21013 /* If there are no item_decls, we have a nondefining namelist, e.g.
21014 with USE association; hence, set DW_AT_declaration. */
21015 if (item_decls == NULL_TREE)
21017 add_AT_flag (nml_die, DW_AT_declaration, 1);
21018 return nml_die;
21021 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21023 nml_item_ref_die = lookup_decl_die (value);
21024 if (!nml_item_ref_die)
21025 nml_item_ref_die = force_decl_die (value);
21027 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21028 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21030 return nml_die;
21034 /* Write the debugging output for DECL. */
21036 static void
21037 dwarf2out_decl (tree decl)
21039 dw_die_ref context_die = comp_unit_die ();
21041 switch (TREE_CODE (decl))
21043 case ERROR_MARK:
21044 return;
21046 case FUNCTION_DECL:
21047 /* What we would really like to do here is to filter out all mere
21048 file-scope declarations of file-scope functions which are never
21049 referenced later within this translation unit (and keep all of ones
21050 that *are* referenced later on) but we aren't clairvoyant, so we have
21051 no idea which functions will be referenced in the future (i.e. later
21052 on within the current translation unit). So here we just ignore all
21053 file-scope function declarations which are not also definitions. If
21054 and when the debugger needs to know something about these functions,
21055 it will have to hunt around and find the DWARF information associated
21056 with the definition of the function.
21058 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21059 nodes represent definitions and which ones represent mere
21060 declarations. We have to check DECL_INITIAL instead. That's because
21061 the C front-end supports some weird semantics for "extern inline"
21062 function definitions. These can get inlined within the current
21063 translation unit (and thus, we need to generate Dwarf info for their
21064 abstract instances so that the Dwarf info for the concrete inlined
21065 instances can have something to refer to) but the compiler never
21066 generates any out-of-lines instances of such things (despite the fact
21067 that they *are* definitions).
21069 The important point is that the C front-end marks these "extern
21070 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21071 them anyway. Note that the C++ front-end also plays some similar games
21072 for inline function definitions appearing within include files which
21073 also contain `#pragma interface' pragmas.
21075 If we are called from dwarf2out_abstract_function output a DIE
21076 anyway. We can end up here this way with early inlining and LTO
21077 where the inlined function is output in a different LTRANS unit
21078 or not at all. */
21079 if (DECL_INITIAL (decl) == NULL_TREE
21080 && ! DECL_ABSTRACT_P (decl))
21081 return;
21083 /* If we're a nested function, initially use a parent of NULL; if we're
21084 a plain function, this will be fixed up in decls_for_scope. If
21085 we're a method, it will be ignored, since we already have a DIE. */
21086 if (decl_function_context (decl)
21087 /* But if we're in terse mode, we don't care about scope. */
21088 && debug_info_level > DINFO_LEVEL_TERSE)
21089 context_die = NULL;
21090 break;
21092 case VAR_DECL:
21093 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21094 declaration and if the declaration was never even referenced from
21095 within this entire compilation unit. We suppress these DIEs in
21096 order to save space in the .debug section (by eliminating entries
21097 which are probably useless). Note that we must not suppress
21098 block-local extern declarations (whether used or not) because that
21099 would screw-up the debugger's name lookup mechanism and cause it to
21100 miss things which really ought to be in scope at a given point. */
21101 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21102 return;
21104 /* For local statics lookup proper context die. */
21105 if (TREE_STATIC (decl)
21106 && DECL_CONTEXT (decl)
21107 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21108 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21110 /* If we are in terse mode, don't generate any DIEs to represent any
21111 variable declarations or definitions. */
21112 if (debug_info_level <= DINFO_LEVEL_TERSE)
21113 return;
21114 break;
21116 case CONST_DECL:
21117 if (debug_info_level <= DINFO_LEVEL_TERSE)
21118 return;
21119 if (!is_fortran () && !is_ada ())
21120 return;
21121 if (TREE_STATIC (decl) && decl_function_context (decl))
21122 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21123 break;
21125 case NAMESPACE_DECL:
21126 case IMPORTED_DECL:
21127 if (debug_info_level <= DINFO_LEVEL_TERSE)
21128 return;
21129 if (lookup_decl_die (decl) != NULL)
21130 return;
21131 break;
21133 case TYPE_DECL:
21134 /* Don't emit stubs for types unless they are needed by other DIEs. */
21135 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21136 return;
21138 /* Don't bother trying to generate any DIEs to represent any of the
21139 normal built-in types for the language we are compiling. */
21140 if (DECL_IS_BUILTIN (decl))
21141 return;
21143 /* If we are in terse mode, don't generate any DIEs for types. */
21144 if (debug_info_level <= DINFO_LEVEL_TERSE)
21145 return;
21147 /* If we're a function-scope tag, initially use a parent of NULL;
21148 this will be fixed up in decls_for_scope. */
21149 if (decl_function_context (decl))
21150 context_die = NULL;
21152 break;
21154 case NAMELIST_DECL:
21155 break;
21157 default:
21158 return;
21161 gen_decl_die (decl, NULL, context_die);
21164 /* Write the debugging output for DECL. */
21166 static void
21167 dwarf2out_function_decl (tree decl)
21169 dwarf2out_decl (decl);
21170 call_arg_locations = NULL;
21171 call_arg_loc_last = NULL;
21172 call_site_count = -1;
21173 tail_call_site_count = -1;
21174 block_map.release ();
21175 decl_loc_table->empty ();
21176 cached_dw_loc_list_table->empty ();
21179 /* Output a marker (i.e. a label) for the beginning of the generated code for
21180 a lexical block. */
21182 static void
21183 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21184 unsigned int blocknum)
21186 switch_to_section (current_function_section ());
21187 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21190 /* Output a marker (i.e. a label) for the end of the generated code for a
21191 lexical block. */
21193 static void
21194 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21196 switch_to_section (current_function_section ());
21197 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21200 /* Returns nonzero if it is appropriate not to emit any debugging
21201 information for BLOCK, because it doesn't contain any instructions.
21203 Don't allow this for blocks with nested functions or local classes
21204 as we would end up with orphans, and in the presence of scheduling
21205 we may end up calling them anyway. */
21207 static bool
21208 dwarf2out_ignore_block (const_tree block)
21210 tree decl;
21211 unsigned int i;
21213 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21214 if (TREE_CODE (decl) == FUNCTION_DECL
21215 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21216 return 0;
21217 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21219 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21220 if (TREE_CODE (decl) == FUNCTION_DECL
21221 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21222 return 0;
21225 return 1;
21228 /* Hash table routines for file_hash. */
21230 bool
21231 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21233 return filename_cmp (p1->filename, p2) == 0;
21236 hashval_t
21237 dwarf_file_hasher::hash (dwarf_file_data *p)
21239 return htab_hash_string (p->filename);
21242 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21243 dwarf2out.c) and return its "index". The index of each (known) filename is
21244 just a unique number which is associated with only that one filename. We
21245 need such numbers for the sake of generating labels (in the .debug_sfnames
21246 section) and references to those files numbers (in the .debug_srcinfo
21247 and.debug_macinfo sections). If the filename given as an argument is not
21248 found in our current list, add it to the list and assign it the next
21249 available unique index number. In order to speed up searches, we remember
21250 the index of the filename was looked up last. This handles the majority of
21251 all searches. */
21253 static struct dwarf_file_data *
21254 lookup_filename (const char *file_name)
21256 struct dwarf_file_data * created;
21258 /* Check to see if the file name that was searched on the previous
21259 call matches this file name. If so, return the index. */
21260 if (file_table_last_lookup
21261 && (file_name == file_table_last_lookup->filename
21262 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21263 return file_table_last_lookup;
21265 /* Didn't match the previous lookup, search the table. */
21266 dwarf_file_data **slot
21267 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21268 INSERT);
21269 if (*slot)
21270 return *slot;
21272 created = ggc_alloc<dwarf_file_data> ();
21273 created->filename = file_name;
21274 created->emitted_number = 0;
21275 *slot = created;
21276 return created;
21279 /* If the assembler will construct the file table, then translate the compiler
21280 internal file table number into the assembler file table number, and emit
21281 a .file directive if we haven't already emitted one yet. The file table
21282 numbers are different because we prune debug info for unused variables and
21283 types, which may include filenames. */
21285 static int
21286 maybe_emit_file (struct dwarf_file_data * fd)
21288 if (! fd->emitted_number)
21290 if (last_emitted_file)
21291 fd->emitted_number = last_emitted_file->emitted_number + 1;
21292 else
21293 fd->emitted_number = 1;
21294 last_emitted_file = fd;
21296 if (DWARF2_ASM_LINE_DEBUG_INFO)
21298 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21299 output_quoted_string (asm_out_file,
21300 remap_debug_filename (fd->filename));
21301 fputc ('\n', asm_out_file);
21305 return fd->emitted_number;
21308 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21309 That generation should happen after function debug info has been
21310 generated. The value of the attribute is the constant value of ARG. */
21312 static void
21313 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21315 die_arg_entry entry;
21317 if (!die || !arg)
21318 return;
21320 if (!tmpl_value_parm_die_table)
21321 vec_alloc (tmpl_value_parm_die_table, 32);
21323 entry.die = die;
21324 entry.arg = arg;
21325 vec_safe_push (tmpl_value_parm_die_table, entry);
21328 /* Return TRUE if T is an instance of generic type, FALSE
21329 otherwise. */
21331 static bool
21332 generic_type_p (tree t)
21334 if (t == NULL_TREE || !TYPE_P (t))
21335 return false;
21336 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21339 /* Schedule the generation of the generic parameter dies for the
21340 instance of generic type T. The proper generation itself is later
21341 done by gen_scheduled_generic_parms_dies. */
21343 static void
21344 schedule_generic_params_dies_gen (tree t)
21346 if (!generic_type_p (t))
21347 return;
21349 if (!generic_type_instances)
21350 vec_alloc (generic_type_instances, 256);
21352 vec_safe_push (generic_type_instances, t);
21355 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21356 by append_entry_to_tmpl_value_parm_die_table. This function must
21357 be called after function DIEs have been generated. */
21359 static void
21360 gen_remaining_tmpl_value_param_die_attribute (void)
21362 if (tmpl_value_parm_die_table)
21364 unsigned i;
21365 die_arg_entry *e;
21367 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21368 tree_add_const_value_attribute (e->die, e->arg);
21372 /* Generate generic parameters DIEs for instances of generic types
21373 that have been previously scheduled by
21374 schedule_generic_params_dies_gen. This function must be called
21375 after all the types of the CU have been laid out. */
21377 static void
21378 gen_scheduled_generic_parms_dies (void)
21380 unsigned i;
21381 tree t;
21383 if (!generic_type_instances)
21384 return;
21386 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21387 if (COMPLETE_TYPE_P (t))
21388 gen_generic_params_dies (t);
21392 /* Replace DW_AT_name for the decl with name. */
21394 static void
21395 dwarf2out_set_name (tree decl, tree name)
21397 dw_die_ref die;
21398 dw_attr_ref attr;
21399 const char *dname;
21401 die = TYPE_SYMTAB_DIE (decl);
21402 if (!die)
21403 return;
21405 dname = dwarf2_name (name, 0);
21406 if (!dname)
21407 return;
21409 attr = get_AT (die, DW_AT_name);
21410 if (attr)
21412 struct indirect_string_node *node;
21414 node = find_AT_string (dname);
21415 /* replace the string. */
21416 attr->dw_attr_val.v.val_str = node;
21419 else
21420 add_name_attribute (die, dname);
21423 /* True if before or during processing of the first function being emitted. */
21424 static bool in_first_function_p = true;
21425 /* True if loc_note during dwarf2out_var_location call might still be
21426 before first real instruction at address equal to .Ltext0. */
21427 static bool maybe_at_text_label_p = true;
21428 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21429 static unsigned int first_loclabel_num_not_at_text_label;
21431 /* Called by the final INSN scan whenever we see a var location. We
21432 use it to drop labels in the right places, and throw the location in
21433 our lookup table. */
21435 static void
21436 dwarf2out_var_location (rtx_insn *loc_note)
21438 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21439 struct var_loc_node *newloc;
21440 rtx_insn *next_real, *next_note;
21441 static const char *last_label;
21442 static const char *last_postcall_label;
21443 static bool last_in_cold_section_p;
21444 static rtx_insn *expected_next_loc_note;
21445 tree decl;
21446 bool var_loc_p;
21448 if (!NOTE_P (loc_note))
21450 if (CALL_P (loc_note))
21452 call_site_count++;
21453 if (SIBLING_CALL_P (loc_note))
21454 tail_call_site_count++;
21456 return;
21459 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21460 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21461 return;
21463 /* Optimize processing a large consecutive sequence of location
21464 notes so we don't spend too much time in next_real_insn. If the
21465 next insn is another location note, remember the next_real_insn
21466 calculation for next time. */
21467 next_real = cached_next_real_insn;
21468 if (next_real)
21470 if (expected_next_loc_note != loc_note)
21471 next_real = NULL;
21474 next_note = NEXT_INSN (loc_note);
21475 if (! next_note
21476 || next_note->deleted ()
21477 || ! NOTE_P (next_note)
21478 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21479 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21480 next_note = NULL;
21482 if (! next_real)
21483 next_real = next_real_insn (loc_note);
21485 if (next_note)
21487 expected_next_loc_note = next_note;
21488 cached_next_real_insn = next_real;
21490 else
21491 cached_next_real_insn = NULL;
21493 /* If there are no instructions which would be affected by this note,
21494 don't do anything. */
21495 if (var_loc_p
21496 && next_real == NULL_RTX
21497 && !NOTE_DURING_CALL_P (loc_note))
21498 return;
21500 if (next_real == NULL_RTX)
21501 next_real = get_last_insn ();
21503 /* If there were any real insns between note we processed last time
21504 and this note (or if it is the first note), clear
21505 last_{,postcall_}label so that they are not reused this time. */
21506 if (last_var_location_insn == NULL_RTX
21507 || last_var_location_insn != next_real
21508 || last_in_cold_section_p != in_cold_section_p)
21510 last_label = NULL;
21511 last_postcall_label = NULL;
21514 if (var_loc_p)
21516 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21517 newloc = add_var_loc_to_decl (decl, loc_note,
21518 NOTE_DURING_CALL_P (loc_note)
21519 ? last_postcall_label : last_label);
21520 if (newloc == NULL)
21521 return;
21523 else
21525 decl = NULL_TREE;
21526 newloc = NULL;
21529 /* If there were no real insns between note we processed last time
21530 and this note, use the label we emitted last time. Otherwise
21531 create a new label and emit it. */
21532 if (last_label == NULL)
21534 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21535 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21536 loclabel_num++;
21537 last_label = ggc_strdup (loclabel);
21538 /* See if loclabel might be equal to .Ltext0. If yes,
21539 bump first_loclabel_num_not_at_text_label. */
21540 if (!have_multiple_function_sections
21541 && in_first_function_p
21542 && maybe_at_text_label_p)
21544 static rtx_insn *last_start;
21545 rtx_insn *insn;
21546 for (insn = loc_note; insn; insn = previous_insn (insn))
21547 if (insn == last_start)
21548 break;
21549 else if (!NONDEBUG_INSN_P (insn))
21550 continue;
21551 else
21553 rtx body = PATTERN (insn);
21554 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21555 continue;
21556 /* Inline asm could occupy zero bytes. */
21557 else if (GET_CODE (body) == ASM_INPUT
21558 || asm_noperands (body) >= 0)
21559 continue;
21560 #ifdef HAVE_attr_length
21561 else if (get_attr_min_length (insn) == 0)
21562 continue;
21563 #endif
21564 else
21566 /* Assume insn has non-zero length. */
21567 maybe_at_text_label_p = false;
21568 break;
21571 if (maybe_at_text_label_p)
21573 last_start = loc_note;
21574 first_loclabel_num_not_at_text_label = loclabel_num;
21579 if (!var_loc_p)
21581 struct call_arg_loc_node *ca_loc
21582 = ggc_cleared_alloc<call_arg_loc_node> ();
21583 rtx_insn *prev = prev_real_insn (loc_note);
21584 rtx x;
21585 ca_loc->call_arg_loc_note = loc_note;
21586 ca_loc->next = NULL;
21587 ca_loc->label = last_label;
21588 gcc_assert (prev
21589 && (CALL_P (prev)
21590 || (NONJUMP_INSN_P (prev)
21591 && GET_CODE (PATTERN (prev)) == SEQUENCE
21592 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21593 if (!CALL_P (prev))
21594 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21595 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21596 x = get_call_rtx_from (PATTERN (prev));
21597 if (x)
21599 x = XEXP (XEXP (x, 0), 0);
21600 if (GET_CODE (x) == SYMBOL_REF
21601 && SYMBOL_REF_DECL (x)
21602 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21603 ca_loc->symbol_ref = x;
21605 ca_loc->block = insn_scope (prev);
21606 if (call_arg_locations)
21607 call_arg_loc_last->next = ca_loc;
21608 else
21609 call_arg_locations = ca_loc;
21610 call_arg_loc_last = ca_loc;
21612 else if (!NOTE_DURING_CALL_P (loc_note))
21613 newloc->label = last_label;
21614 else
21616 if (!last_postcall_label)
21618 sprintf (loclabel, "%s-1", last_label);
21619 last_postcall_label = ggc_strdup (loclabel);
21621 newloc->label = last_postcall_label;
21624 last_var_location_insn = next_real;
21625 last_in_cold_section_p = in_cold_section_p;
21628 /* Note in one location list that text section has changed. */
21631 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21633 var_loc_list *list = *slot;
21634 if (list->first)
21635 list->last_before_switch
21636 = list->last->next ? list->last->next : list->last;
21637 return 1;
21640 /* Note in all location lists that text section has changed. */
21642 static void
21643 var_location_switch_text_section (void)
21645 if (decl_loc_table == NULL)
21646 return;
21648 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21651 /* Create a new line number table. */
21653 static dw_line_info_table *
21654 new_line_info_table (void)
21656 dw_line_info_table *table;
21658 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21659 table->file_num = 1;
21660 table->line_num = 1;
21661 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21663 return table;
21666 /* Lookup the "current" table into which we emit line info, so
21667 that we don't have to do it for every source line. */
21669 static void
21670 set_cur_line_info_table (section *sec)
21672 dw_line_info_table *table;
21674 if (sec == text_section)
21675 table = text_section_line_info;
21676 else if (sec == cold_text_section)
21678 table = cold_text_section_line_info;
21679 if (!table)
21681 cold_text_section_line_info = table = new_line_info_table ();
21682 table->end_label = cold_end_label;
21685 else
21687 const char *end_label;
21689 if (flag_reorder_blocks_and_partition)
21691 if (in_cold_section_p)
21692 end_label = crtl->subsections.cold_section_end_label;
21693 else
21694 end_label = crtl->subsections.hot_section_end_label;
21696 else
21698 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21699 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21700 current_function_funcdef_no);
21701 end_label = ggc_strdup (label);
21704 table = new_line_info_table ();
21705 table->end_label = end_label;
21707 vec_safe_push (separate_line_info, table);
21710 if (DWARF2_ASM_LINE_DEBUG_INFO)
21711 table->is_stmt = (cur_line_info_table
21712 ? cur_line_info_table->is_stmt
21713 : DWARF_LINE_DEFAULT_IS_STMT_START);
21714 cur_line_info_table = table;
21718 /* We need to reset the locations at the beginning of each
21719 function. We can't do this in the end_function hook, because the
21720 declarations that use the locations won't have been output when
21721 that hook is called. Also compute have_multiple_function_sections here. */
21723 static void
21724 dwarf2out_begin_function (tree fun)
21726 section *sec = function_section (fun);
21728 if (sec != text_section)
21729 have_multiple_function_sections = true;
21731 if (flag_reorder_blocks_and_partition && !cold_text_section)
21733 gcc_assert (current_function_decl == fun);
21734 cold_text_section = unlikely_text_section ();
21735 switch_to_section (cold_text_section);
21736 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21737 switch_to_section (sec);
21740 dwarf2out_note_section_used ();
21741 call_site_count = 0;
21742 tail_call_site_count = 0;
21744 set_cur_line_info_table (sec);
21747 /* Helper function of dwarf2out_end_function, called only after emitting
21748 the very first function into assembly. Check if some .debug_loc range
21749 might end with a .LVL* label that could be equal to .Ltext0.
21750 In that case we must force using absolute addresses in .debug_loc ranges,
21751 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21752 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21753 list terminator.
21754 Set have_multiple_function_sections to true in that case and
21755 terminate htab traversal. */
21758 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21760 var_loc_list *entry = *slot;
21761 struct var_loc_node *node;
21763 node = entry->first;
21764 if (node && node->next && node->next->label)
21766 unsigned int i;
21767 const char *label = node->next->label;
21768 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21770 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21772 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21773 if (strcmp (label, loclabel) == 0)
21775 have_multiple_function_sections = true;
21776 return 0;
21780 return 1;
21783 /* Hook called after emitting a function into assembly.
21784 This does something only for the very first function emitted. */
21786 static void
21787 dwarf2out_end_function (unsigned int)
21789 if (in_first_function_p
21790 && !have_multiple_function_sections
21791 && first_loclabel_num_not_at_text_label
21792 && decl_loc_table)
21793 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
21794 in_first_function_p = false;
21795 maybe_at_text_label_p = false;
21798 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21800 static void
21801 push_dw_line_info_entry (dw_line_info_table *table,
21802 enum dw_line_info_opcode opcode, unsigned int val)
21804 dw_line_info_entry e;
21805 e.opcode = opcode;
21806 e.val = val;
21807 vec_safe_push (table->entries, e);
21810 /* Output a label to mark the beginning of a source code line entry
21811 and record information relating to this source line, in
21812 'line_info_table' for later output of the .debug_line section. */
21813 /* ??? The discriminator parameter ought to be unsigned. */
21815 static void
21816 dwarf2out_source_line (unsigned int line, const char *filename,
21817 int discriminator, bool is_stmt)
21819 unsigned int file_num;
21820 dw_line_info_table *table;
21822 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21823 return;
21825 /* The discriminator column was added in dwarf4. Simplify the below
21826 by simply removing it if we're not supposed to output it. */
21827 if (dwarf_version < 4 && dwarf_strict)
21828 discriminator = 0;
21830 table = cur_line_info_table;
21831 file_num = maybe_emit_file (lookup_filename (filename));
21833 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21834 the debugger has used the second (possibly duplicate) line number
21835 at the beginning of the function to mark the end of the prologue.
21836 We could eliminate any other duplicates within the function. For
21837 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21838 that second line number entry. */
21839 /* Recall that this end-of-prologue indication is *not* the same thing
21840 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21841 to which the hook corresponds, follows the last insn that was
21842 emitted by gen_prologue. What we need is to precede the first insn
21843 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21844 insn that corresponds to something the user wrote. These may be
21845 very different locations once scheduling is enabled. */
21847 if (0 && file_num == table->file_num
21848 && line == table->line_num
21849 && discriminator == table->discrim_num
21850 && is_stmt == table->is_stmt)
21851 return;
21853 switch_to_section (current_function_section ());
21855 /* If requested, emit something human-readable. */
21856 if (flag_debug_asm)
21857 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21859 if (DWARF2_ASM_LINE_DEBUG_INFO)
21861 /* Emit the .loc directive understood by GNU as. */
21862 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21863 file_num, line, is_stmt, discriminator */
21864 fputs ("\t.loc ", asm_out_file);
21865 fprint_ul (asm_out_file, file_num);
21866 putc (' ', asm_out_file);
21867 fprint_ul (asm_out_file, line);
21868 putc (' ', asm_out_file);
21869 putc ('0', asm_out_file);
21871 if (is_stmt != table->is_stmt)
21873 fputs (" is_stmt ", asm_out_file);
21874 putc (is_stmt ? '1' : '0', asm_out_file);
21876 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21878 gcc_assert (discriminator > 0);
21879 fputs (" discriminator ", asm_out_file);
21880 fprint_ul (asm_out_file, (unsigned long) discriminator);
21882 putc ('\n', asm_out_file);
21884 else
21886 unsigned int label_num = ++line_info_label_num;
21888 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21890 push_dw_line_info_entry (table, LI_set_address, label_num);
21891 if (file_num != table->file_num)
21892 push_dw_line_info_entry (table, LI_set_file, file_num);
21893 if (discriminator != table->discrim_num)
21894 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21895 if (is_stmt != table->is_stmt)
21896 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21897 push_dw_line_info_entry (table, LI_set_line, line);
21900 table->file_num = file_num;
21901 table->line_num = line;
21902 table->discrim_num = discriminator;
21903 table->is_stmt = is_stmt;
21904 table->in_use = true;
21907 /* Record the beginning of a new source file. */
21909 static void
21910 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21912 if (flag_eliminate_dwarf2_dups)
21914 /* Record the beginning of the file for break_out_includes. */
21915 dw_die_ref bincl_die;
21917 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21918 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21921 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21923 macinfo_entry e;
21924 e.code = DW_MACINFO_start_file;
21925 e.lineno = lineno;
21926 e.info = ggc_strdup (filename);
21927 vec_safe_push (macinfo_table, e);
21931 /* Record the end of a source file. */
21933 static void
21934 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21936 if (flag_eliminate_dwarf2_dups)
21937 /* Record the end of the file for break_out_includes. */
21938 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21940 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21942 macinfo_entry e;
21943 e.code = DW_MACINFO_end_file;
21944 e.lineno = lineno;
21945 e.info = NULL;
21946 vec_safe_push (macinfo_table, e);
21950 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21951 the tail part of the directive line, i.e. the part which is past the
21952 initial whitespace, #, whitespace, directive-name, whitespace part. */
21954 static void
21955 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21956 const char *buffer ATTRIBUTE_UNUSED)
21958 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21960 macinfo_entry e;
21961 /* Insert a dummy first entry to be able to optimize the whole
21962 predefined macro block using DW_MACRO_GNU_transparent_include. */
21963 if (macinfo_table->is_empty () && lineno <= 1)
21965 e.code = 0;
21966 e.lineno = 0;
21967 e.info = NULL;
21968 vec_safe_push (macinfo_table, e);
21970 e.code = DW_MACINFO_define;
21971 e.lineno = lineno;
21972 e.info = ggc_strdup (buffer);
21973 vec_safe_push (macinfo_table, e);
21977 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21978 the tail part of the directive line, i.e. the part which is past the
21979 initial whitespace, #, whitespace, directive-name, whitespace part. */
21981 static void
21982 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21983 const char *buffer ATTRIBUTE_UNUSED)
21985 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21987 macinfo_entry e;
21988 /* Insert a dummy first entry to be able to optimize the whole
21989 predefined macro block using DW_MACRO_GNU_transparent_include. */
21990 if (macinfo_table->is_empty () && lineno <= 1)
21992 e.code = 0;
21993 e.lineno = 0;
21994 e.info = NULL;
21995 vec_safe_push (macinfo_table, e);
21997 e.code = DW_MACINFO_undef;
21998 e.lineno = lineno;
21999 e.info = ggc_strdup (buffer);
22000 vec_safe_push (macinfo_table, e);
22004 /* Helpers to manipulate hash table of CUs. */
22006 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22008 typedef macinfo_entry value_type;
22009 typedef macinfo_entry compare_type;
22010 static inline hashval_t hash (const value_type *);
22011 static inline bool equal (const value_type *, const compare_type *);
22014 inline hashval_t
22015 macinfo_entry_hasher::hash (const value_type *entry)
22017 return htab_hash_string (entry->info);
22020 inline bool
22021 macinfo_entry_hasher::equal (const value_type *entry1,
22022 const compare_type *entry2)
22024 return !strcmp (entry1->info, entry2->info);
22027 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22029 /* Output a single .debug_macinfo entry. */
22031 static void
22032 output_macinfo_op (macinfo_entry *ref)
22034 int file_num;
22035 size_t len;
22036 struct indirect_string_node *node;
22037 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22038 struct dwarf_file_data *fd;
22040 switch (ref->code)
22042 case DW_MACINFO_start_file:
22043 fd = lookup_filename (ref->info);
22044 file_num = maybe_emit_file (fd);
22045 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22046 dw2_asm_output_data_uleb128 (ref->lineno,
22047 "Included from line number %lu",
22048 (unsigned long) ref->lineno);
22049 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22050 break;
22051 case DW_MACINFO_end_file:
22052 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22053 break;
22054 case DW_MACINFO_define:
22055 case DW_MACINFO_undef:
22056 len = strlen (ref->info) + 1;
22057 if (!dwarf_strict
22058 && len > DWARF_OFFSET_SIZE
22059 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22060 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22062 ref->code = ref->code == DW_MACINFO_define
22063 ? DW_MACRO_GNU_define_indirect
22064 : DW_MACRO_GNU_undef_indirect;
22065 output_macinfo_op (ref);
22066 return;
22068 dw2_asm_output_data (1, ref->code,
22069 ref->code == DW_MACINFO_define
22070 ? "Define macro" : "Undefine macro");
22071 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22072 (unsigned long) ref->lineno);
22073 dw2_asm_output_nstring (ref->info, -1, "The macro");
22074 break;
22075 case DW_MACRO_GNU_define_indirect:
22076 case DW_MACRO_GNU_undef_indirect:
22077 node = find_AT_string (ref->info);
22078 gcc_assert (node
22079 && ((node->form == DW_FORM_strp)
22080 || (node->form == DW_FORM_GNU_str_index)));
22081 dw2_asm_output_data (1, ref->code,
22082 ref->code == DW_MACRO_GNU_define_indirect
22083 ? "Define macro indirect"
22084 : "Undefine macro indirect");
22085 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22086 (unsigned long) ref->lineno);
22087 if (node->form == DW_FORM_strp)
22088 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22089 debug_str_section, "The macro: \"%s\"",
22090 ref->info);
22091 else
22092 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22093 ref->info);
22094 break;
22095 case DW_MACRO_GNU_transparent_include:
22096 dw2_asm_output_data (1, ref->code, "Transparent include");
22097 ASM_GENERATE_INTERNAL_LABEL (label,
22098 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22099 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22100 break;
22101 default:
22102 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22103 ASM_COMMENT_START, (unsigned long) ref->code);
22104 break;
22108 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22109 other compilation unit .debug_macinfo sections. IDX is the first
22110 index of a define/undef, return the number of ops that should be
22111 emitted in a comdat .debug_macinfo section and emit
22112 a DW_MACRO_GNU_transparent_include entry referencing it.
22113 If the define/undef entry should be emitted normally, return 0. */
22115 static unsigned
22116 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22117 macinfo_hash_type **macinfo_htab)
22119 macinfo_entry *first, *second, *cur, *inc;
22120 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22121 unsigned char checksum[16];
22122 struct md5_ctx ctx;
22123 char *grp_name, *tail;
22124 const char *base;
22125 unsigned int i, count, encoded_filename_len, linebuf_len;
22126 macinfo_entry **slot;
22128 first = &(*macinfo_table)[idx];
22129 second = &(*macinfo_table)[idx + 1];
22131 /* Optimize only if there are at least two consecutive define/undef ops,
22132 and either all of them are before first DW_MACINFO_start_file
22133 with lineno {0,1} (i.e. predefined macro block), or all of them are
22134 in some included header file. */
22135 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22136 return 0;
22137 if (vec_safe_is_empty (files))
22139 if (first->lineno > 1 || second->lineno > 1)
22140 return 0;
22142 else if (first->lineno == 0)
22143 return 0;
22145 /* Find the last define/undef entry that can be grouped together
22146 with first and at the same time compute md5 checksum of their
22147 codes, linenumbers and strings. */
22148 md5_init_ctx (&ctx);
22149 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22150 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22151 break;
22152 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22153 break;
22154 else
22156 unsigned char code = cur->code;
22157 md5_process_bytes (&code, 1, &ctx);
22158 checksum_uleb128 (cur->lineno, &ctx);
22159 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22161 md5_finish_ctx (&ctx, checksum);
22162 count = i - idx;
22164 /* From the containing include filename (if any) pick up just
22165 usable characters from its basename. */
22166 if (vec_safe_is_empty (files))
22167 base = "";
22168 else
22169 base = lbasename (files->last ().info);
22170 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22171 if (ISIDNUM (base[i]) || base[i] == '.')
22172 encoded_filename_len++;
22173 /* Count . at the end. */
22174 if (encoded_filename_len)
22175 encoded_filename_len++;
22177 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22178 linebuf_len = strlen (linebuf);
22180 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22181 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22182 + 16 * 2 + 1);
22183 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22184 tail = grp_name + 4;
22185 if (encoded_filename_len)
22187 for (i = 0; base[i]; i++)
22188 if (ISIDNUM (base[i]) || base[i] == '.')
22189 *tail++ = base[i];
22190 *tail++ = '.';
22192 memcpy (tail, linebuf, linebuf_len);
22193 tail += linebuf_len;
22194 *tail++ = '.';
22195 for (i = 0; i < 16; i++)
22196 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22198 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22199 in the empty vector entry before the first define/undef. */
22200 inc = &(*macinfo_table)[idx - 1];
22201 inc->code = DW_MACRO_GNU_transparent_include;
22202 inc->lineno = 0;
22203 inc->info = ggc_strdup (grp_name);
22204 if (!*macinfo_htab)
22205 *macinfo_htab = new macinfo_hash_type (10);
22206 /* Avoid emitting duplicates. */
22207 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22208 if (*slot != NULL)
22210 inc->code = 0;
22211 inc->info = NULL;
22212 /* If such an entry has been used before, just emit
22213 a DW_MACRO_GNU_transparent_include op. */
22214 inc = *slot;
22215 output_macinfo_op (inc);
22216 /* And clear all macinfo_entry in the range to avoid emitting them
22217 in the second pass. */
22218 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22220 cur->code = 0;
22221 cur->info = NULL;
22224 else
22226 *slot = inc;
22227 inc->lineno = (*macinfo_htab)->elements ();
22228 output_macinfo_op (inc);
22230 return count;
22233 /* Save any strings needed by the macinfo table in the debug str
22234 table. All strings must be collected into the table by the time
22235 index_string is called. */
22237 static void
22238 save_macinfo_strings (void)
22240 unsigned len;
22241 unsigned i;
22242 macinfo_entry *ref;
22244 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22246 switch (ref->code)
22248 /* Match the logic in output_macinfo_op to decide on
22249 indirect strings. */
22250 case DW_MACINFO_define:
22251 case DW_MACINFO_undef:
22252 len = strlen (ref->info) + 1;
22253 if (!dwarf_strict
22254 && len > DWARF_OFFSET_SIZE
22255 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22256 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22257 set_indirect_string (find_AT_string (ref->info));
22258 break;
22259 case DW_MACRO_GNU_define_indirect:
22260 case DW_MACRO_GNU_undef_indirect:
22261 set_indirect_string (find_AT_string (ref->info));
22262 break;
22263 default:
22264 break;
22269 /* Output macinfo section(s). */
22271 static void
22272 output_macinfo (void)
22274 unsigned i;
22275 unsigned long length = vec_safe_length (macinfo_table);
22276 macinfo_entry *ref;
22277 vec<macinfo_entry, va_gc> *files = NULL;
22278 macinfo_hash_type *macinfo_htab = NULL;
22280 if (! length)
22281 return;
22283 /* output_macinfo* uses these interchangeably. */
22284 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22285 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22286 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22287 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22289 /* For .debug_macro emit the section header. */
22290 if (!dwarf_strict)
22292 dw2_asm_output_data (2, 4, "DWARF macro version number");
22293 if (DWARF_OFFSET_SIZE == 8)
22294 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22295 else
22296 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22297 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22298 (!dwarf_split_debug_info ? debug_line_section_label
22299 : debug_skeleton_line_section_label),
22300 debug_line_section, NULL);
22303 /* In the first loop, it emits the primary .debug_macinfo section
22304 and after each emitted op the macinfo_entry is cleared.
22305 If a longer range of define/undef ops can be optimized using
22306 DW_MACRO_GNU_transparent_include, the
22307 DW_MACRO_GNU_transparent_include op is emitted and kept in
22308 the vector before the first define/undef in the range and the
22309 whole range of define/undef ops is not emitted and kept. */
22310 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22312 switch (ref->code)
22314 case DW_MACINFO_start_file:
22315 vec_safe_push (files, *ref);
22316 break;
22317 case DW_MACINFO_end_file:
22318 if (!vec_safe_is_empty (files))
22319 files->pop ();
22320 break;
22321 case DW_MACINFO_define:
22322 case DW_MACINFO_undef:
22323 if (!dwarf_strict
22324 && HAVE_COMDAT_GROUP
22325 && vec_safe_length (files) != 1
22326 && i > 0
22327 && i + 1 < length
22328 && (*macinfo_table)[i - 1].code == 0)
22330 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22331 if (count)
22333 i += count - 1;
22334 continue;
22337 break;
22338 case 0:
22339 /* A dummy entry may be inserted at the beginning to be able
22340 to optimize the whole block of predefined macros. */
22341 if (i == 0)
22342 continue;
22343 default:
22344 break;
22346 output_macinfo_op (ref);
22347 ref->info = NULL;
22348 ref->code = 0;
22351 if (!macinfo_htab)
22352 return;
22354 delete macinfo_htab;
22355 macinfo_htab = NULL;
22357 /* If any DW_MACRO_GNU_transparent_include were used, on those
22358 DW_MACRO_GNU_transparent_include entries terminate the
22359 current chain and switch to a new comdat .debug_macinfo
22360 section and emit the define/undef entries within it. */
22361 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22362 switch (ref->code)
22364 case 0:
22365 continue;
22366 case DW_MACRO_GNU_transparent_include:
22368 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22369 tree comdat_key = get_identifier (ref->info);
22370 /* Terminate the previous .debug_macinfo section. */
22371 dw2_asm_output_data (1, 0, "End compilation unit");
22372 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22373 SECTION_DEBUG
22374 | SECTION_LINKONCE,
22375 comdat_key);
22376 ASM_GENERATE_INTERNAL_LABEL (label,
22377 DEBUG_MACRO_SECTION_LABEL,
22378 ref->lineno);
22379 ASM_OUTPUT_LABEL (asm_out_file, label);
22380 ref->code = 0;
22381 ref->info = NULL;
22382 dw2_asm_output_data (2, 4, "DWARF macro version number");
22383 if (DWARF_OFFSET_SIZE == 8)
22384 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22385 else
22386 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22388 break;
22389 case DW_MACINFO_define:
22390 case DW_MACINFO_undef:
22391 output_macinfo_op (ref);
22392 ref->code = 0;
22393 ref->info = NULL;
22394 break;
22395 default:
22396 gcc_unreachable ();
22400 /* Set up for Dwarf output at the start of compilation. */
22402 static void
22403 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22405 /* Allocate the file_table. */
22406 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22408 /* Allocate the decl_die_table. */
22409 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22411 /* Allocate the decl_loc_table. */
22412 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22414 /* Allocate the cached_dw_loc_list_table. */
22415 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22417 /* Allocate the initial hunk of the decl_scope_table. */
22418 vec_alloc (decl_scope_table, 256);
22420 /* Allocate the initial hunk of the abbrev_die_table. */
22421 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22422 (ABBREV_DIE_TABLE_INCREMENT);
22423 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22424 /* Zero-th entry is allocated, but unused. */
22425 abbrev_die_table_in_use = 1;
22427 /* Allocate the pubtypes and pubnames vectors. */
22428 vec_alloc (pubname_table, 32);
22429 vec_alloc (pubtype_table, 32);
22431 vec_alloc (incomplete_types, 64);
22433 vec_alloc (used_rtx_array, 32);
22435 if (!dwarf_split_debug_info)
22437 debug_info_section = get_section (DEBUG_INFO_SECTION,
22438 SECTION_DEBUG, NULL);
22439 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22440 SECTION_DEBUG, NULL);
22441 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22442 SECTION_DEBUG, NULL);
22444 else
22446 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22447 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22448 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22449 SECTION_DEBUG | SECTION_EXCLUDE,
22450 NULL);
22451 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22452 SECTION_DEBUG, NULL);
22453 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22454 SECTION_DEBUG, NULL);
22455 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22456 SECTION_DEBUG, NULL);
22457 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22458 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22460 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22461 the main .o, but the skeleton_line goes into the split off dwo. */
22462 debug_skeleton_line_section
22463 = get_section (DEBUG_DWO_LINE_SECTION,
22464 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22465 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22466 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22467 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22468 SECTION_DEBUG | SECTION_EXCLUDE,
22469 NULL);
22470 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22471 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22472 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22473 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22474 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22475 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22477 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22478 SECTION_DEBUG, NULL);
22479 debug_macinfo_section = get_section (dwarf_strict
22480 ? DEBUG_MACINFO_SECTION
22481 : DEBUG_MACRO_SECTION,
22482 DEBUG_MACRO_SECTION_FLAGS, NULL);
22483 debug_line_section = get_section (DEBUG_LINE_SECTION,
22484 SECTION_DEBUG, NULL);
22485 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22486 SECTION_DEBUG, NULL);
22487 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22488 SECTION_DEBUG, NULL);
22489 debug_str_section = get_section (DEBUG_STR_SECTION,
22490 DEBUG_STR_SECTION_FLAGS, NULL);
22491 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22492 SECTION_DEBUG, NULL);
22493 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22494 SECTION_DEBUG, NULL);
22496 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22497 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22498 DEBUG_ABBREV_SECTION_LABEL, 0);
22499 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22500 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22501 COLD_TEXT_SECTION_LABEL, 0);
22502 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22504 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22505 DEBUG_INFO_SECTION_LABEL, 0);
22506 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22507 DEBUG_LINE_SECTION_LABEL, 0);
22508 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22509 DEBUG_RANGES_SECTION_LABEL, 0);
22510 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22511 DEBUG_ADDR_SECTION_LABEL, 0);
22512 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22513 dwarf_strict
22514 ? DEBUG_MACINFO_SECTION_LABEL
22515 : DEBUG_MACRO_SECTION_LABEL, 0);
22516 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22518 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22519 vec_alloc (macinfo_table, 64);
22521 switch_to_section (text_section);
22522 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22524 /* Make sure the line number table for .text always exists. */
22525 text_section_line_info = new_line_info_table ();
22526 text_section_line_info->end_label = text_end_label;
22529 /* Called before compile () starts outputtting functions, variables
22530 and toplevel asms into assembly. */
22532 static void
22533 dwarf2out_assembly_start (void)
22535 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22536 && dwarf2out_do_cfi_asm ()
22537 && (!(flag_unwind_tables || flag_exceptions)
22538 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22539 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22542 /* A helper function for dwarf2out_finish called through
22543 htab_traverse. Assign a string its index. All strings must be
22544 collected into the table by the time index_string is called,
22545 because the indexing code relies on htab_traverse to traverse nodes
22546 in the same order for each run. */
22549 index_string (indirect_string_node **h, unsigned int *index)
22551 indirect_string_node *node = *h;
22553 find_string_form (node);
22554 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22556 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22557 node->index = *index;
22558 *index += 1;
22560 return 1;
22563 /* A helper function for output_indirect_strings called through
22564 htab_traverse. Output the offset to a string and update the
22565 current offset. */
22568 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22570 indirect_string_node *node = *h;
22572 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22574 /* Assert that this node has been assigned an index. */
22575 gcc_assert (node->index != NO_INDEX_ASSIGNED
22576 && node->index != NOT_INDEXED);
22577 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22578 "indexed string 0x%x: %s", node->index, node->str);
22579 *offset += strlen (node->str) + 1;
22581 return 1;
22584 /* A helper function for dwarf2out_finish called through
22585 htab_traverse. Output the indexed string. */
22588 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22590 struct indirect_string_node *node = *h;
22592 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22594 /* Assert that the strings are output in the same order as their
22595 indexes were assigned. */
22596 gcc_assert (*cur_idx == node->index);
22597 assemble_string (node->str, strlen (node->str) + 1);
22598 *cur_idx += 1;
22600 return 1;
22603 /* A helper function for dwarf2out_finish called through
22604 htab_traverse. Emit one queued .debug_str string. */
22607 output_indirect_string (indirect_string_node **h, void *)
22609 struct indirect_string_node *node = *h;
22611 node->form = find_string_form (node);
22612 if (node->form == DW_FORM_strp && node->refcount > 0)
22614 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22615 assemble_string (node->str, strlen (node->str) + 1);
22618 return 1;
22621 /* Output the indexed string table. */
22623 static void
22624 output_indirect_strings (void)
22626 switch_to_section (debug_str_section);
22627 if (!dwarf_split_debug_info)
22628 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22629 else
22631 unsigned int offset = 0;
22632 unsigned int cur_idx = 0;
22634 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22636 switch_to_section (debug_str_offsets_section);
22637 debug_str_hash->traverse_noresize
22638 <unsigned int *, output_index_string_offset> (&offset);
22639 switch_to_section (debug_str_dwo_section);
22640 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22641 (&cur_idx);
22645 /* Callback for htab_traverse to assign an index to an entry in the
22646 table, and to write that entry to the .debug_addr section. */
22649 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22651 addr_table_entry *entry = *slot;
22653 if (entry->refcount == 0)
22655 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22656 || entry->index == NOT_INDEXED);
22657 return 1;
22660 gcc_assert (entry->index == *cur_index);
22661 (*cur_index)++;
22663 switch (entry->kind)
22665 case ate_kind_rtx:
22666 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22667 "0x%x", entry->index);
22668 break;
22669 case ate_kind_rtx_dtprel:
22670 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22671 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22672 DWARF2_ADDR_SIZE,
22673 entry->addr.rtl);
22674 fputc ('\n', asm_out_file);
22675 break;
22676 case ate_kind_label:
22677 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22678 "0x%x", entry->index);
22679 break;
22680 default:
22681 gcc_unreachable ();
22683 return 1;
22686 /* Produce the .debug_addr section. */
22688 static void
22689 output_addr_table (void)
22691 unsigned int index = 0;
22692 if (addr_index_table == NULL || addr_index_table->size () == 0)
22693 return;
22695 switch_to_section (debug_addr_section);
22696 addr_index_table
22697 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22700 #if ENABLE_ASSERT_CHECKING
22701 /* Verify that all marks are clear. */
22703 static void
22704 verify_marks_clear (dw_die_ref die)
22706 dw_die_ref c;
22708 gcc_assert (! die->die_mark);
22709 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22711 #endif /* ENABLE_ASSERT_CHECKING */
22713 /* Clear the marks for a die and its children.
22714 Be cool if the mark isn't set. */
22716 static void
22717 prune_unmark_dies (dw_die_ref die)
22719 dw_die_ref c;
22721 if (die->die_mark)
22722 die->die_mark = 0;
22723 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22726 /* Given DIE that we're marking as used, find any other dies
22727 it references as attributes and mark them as used. */
22729 static void
22730 prune_unused_types_walk_attribs (dw_die_ref die)
22732 dw_attr_ref a;
22733 unsigned ix;
22735 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22737 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22739 /* A reference to another DIE.
22740 Make sure that it will get emitted.
22741 If it was broken out into a comdat group, don't follow it. */
22742 if (! AT_ref (a)->comdat_type_p
22743 || a->dw_attr == DW_AT_specification)
22744 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22746 /* Set the string's refcount to 0 so that prune_unused_types_mark
22747 accounts properly for it. */
22748 if (AT_class (a) == dw_val_class_str)
22749 a->dw_attr_val.v.val_str->refcount = 0;
22753 /* Mark the generic parameters and arguments children DIEs of DIE. */
22755 static void
22756 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22758 dw_die_ref c;
22760 if (die == NULL || die->die_child == NULL)
22761 return;
22762 c = die->die_child;
22765 if (is_template_parameter (c))
22766 prune_unused_types_mark (c, 1);
22767 c = c->die_sib;
22768 } while (c && c != die->die_child);
22771 /* Mark DIE as being used. If DOKIDS is true, then walk down
22772 to DIE's children. */
22774 static void
22775 prune_unused_types_mark (dw_die_ref die, int dokids)
22777 dw_die_ref c;
22779 if (die->die_mark == 0)
22781 /* We haven't done this node yet. Mark it as used. */
22782 die->die_mark = 1;
22783 /* If this is the DIE of a generic type instantiation,
22784 mark the children DIEs that describe its generic parms and
22785 args. */
22786 prune_unused_types_mark_generic_parms_dies (die);
22788 /* We also have to mark its parents as used.
22789 (But we don't want to mark our parent's kids due to this,
22790 unless it is a class.) */
22791 if (die->die_parent)
22792 prune_unused_types_mark (die->die_parent,
22793 class_scope_p (die->die_parent));
22795 /* Mark any referenced nodes. */
22796 prune_unused_types_walk_attribs (die);
22798 /* If this node is a specification,
22799 also mark the definition, if it exists. */
22800 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22801 prune_unused_types_mark (die->die_definition, 1);
22804 if (dokids && die->die_mark != 2)
22806 /* We need to walk the children, but haven't done so yet.
22807 Remember that we've walked the kids. */
22808 die->die_mark = 2;
22810 /* If this is an array type, we need to make sure our
22811 kids get marked, even if they're types. If we're
22812 breaking out types into comdat sections, do this
22813 for all type definitions. */
22814 if (die->die_tag == DW_TAG_array_type
22815 || (use_debug_types
22816 && is_type_die (die) && ! is_declaration_die (die)))
22817 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22818 else
22819 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22823 /* For local classes, look if any static member functions were emitted
22824 and if so, mark them. */
22826 static void
22827 prune_unused_types_walk_local_classes (dw_die_ref die)
22829 dw_die_ref c;
22831 if (die->die_mark == 2)
22832 return;
22834 switch (die->die_tag)
22836 case DW_TAG_structure_type:
22837 case DW_TAG_union_type:
22838 case DW_TAG_class_type:
22839 break;
22841 case DW_TAG_subprogram:
22842 if (!get_AT_flag (die, DW_AT_declaration)
22843 || die->die_definition != NULL)
22844 prune_unused_types_mark (die, 1);
22845 return;
22847 default:
22848 return;
22851 /* Mark children. */
22852 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22855 /* Walk the tree DIE and mark types that we actually use. */
22857 static void
22858 prune_unused_types_walk (dw_die_ref die)
22860 dw_die_ref c;
22862 /* Don't do anything if this node is already marked and
22863 children have been marked as well. */
22864 if (die->die_mark == 2)
22865 return;
22867 switch (die->die_tag)
22869 case DW_TAG_structure_type:
22870 case DW_TAG_union_type:
22871 case DW_TAG_class_type:
22872 if (die->die_perennial_p)
22873 break;
22875 for (c = die->die_parent; c; c = c->die_parent)
22876 if (c->die_tag == DW_TAG_subprogram)
22877 break;
22879 /* Finding used static member functions inside of classes
22880 is needed just for local classes, because for other classes
22881 static member function DIEs with DW_AT_specification
22882 are emitted outside of the DW_TAG_*_type. If we ever change
22883 it, we'd need to call this even for non-local classes. */
22884 if (c)
22885 prune_unused_types_walk_local_classes (die);
22887 /* It's a type node --- don't mark it. */
22888 return;
22890 case DW_TAG_const_type:
22891 case DW_TAG_packed_type:
22892 case DW_TAG_pointer_type:
22893 case DW_TAG_reference_type:
22894 case DW_TAG_rvalue_reference_type:
22895 case DW_TAG_volatile_type:
22896 case DW_TAG_typedef:
22897 case DW_TAG_array_type:
22898 case DW_TAG_interface_type:
22899 case DW_TAG_friend:
22900 case DW_TAG_variant_part:
22901 case DW_TAG_enumeration_type:
22902 case DW_TAG_subroutine_type:
22903 case DW_TAG_string_type:
22904 case DW_TAG_set_type:
22905 case DW_TAG_subrange_type:
22906 case DW_TAG_ptr_to_member_type:
22907 case DW_TAG_file_type:
22908 if (die->die_perennial_p)
22909 break;
22911 /* It's a type node --- don't mark it. */
22912 return;
22914 default:
22915 /* Mark everything else. */
22916 break;
22919 if (die->die_mark == 0)
22921 die->die_mark = 1;
22923 /* Now, mark any dies referenced from here. */
22924 prune_unused_types_walk_attribs (die);
22927 die->die_mark = 2;
22929 /* Mark children. */
22930 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22933 /* Increment the string counts on strings referred to from DIE's
22934 attributes. */
22936 static void
22937 prune_unused_types_update_strings (dw_die_ref die)
22939 dw_attr_ref a;
22940 unsigned ix;
22942 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22943 if (AT_class (a) == dw_val_class_str)
22945 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22946 s->refcount++;
22947 /* Avoid unnecessarily putting strings that are used less than
22948 twice in the hash table. */
22949 if (s->refcount
22950 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22952 indirect_string_node **slot
22953 = debug_str_hash->find_slot_with_hash (s->str,
22954 htab_hash_string (s->str),
22955 INSERT);
22956 gcc_assert (*slot == NULL);
22957 *slot = s;
22962 /* Remove from the tree DIE any dies that aren't marked. */
22964 static void
22965 prune_unused_types_prune (dw_die_ref die)
22967 dw_die_ref c;
22969 gcc_assert (die->die_mark);
22970 prune_unused_types_update_strings (die);
22972 if (! die->die_child)
22973 return;
22975 c = die->die_child;
22976 do {
22977 dw_die_ref prev = c;
22978 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22979 if (c == die->die_child)
22981 /* No marked children between 'prev' and the end of the list. */
22982 if (prev == c)
22983 /* No marked children at all. */
22984 die->die_child = NULL;
22985 else
22987 prev->die_sib = c->die_sib;
22988 die->die_child = prev;
22990 return;
22993 if (c != prev->die_sib)
22994 prev->die_sib = c;
22995 prune_unused_types_prune (c);
22996 } while (c != die->die_child);
22999 /* Remove dies representing declarations that we never use. */
23001 static void
23002 prune_unused_types (void)
23004 unsigned int i;
23005 limbo_die_node *node;
23006 comdat_type_node *ctnode;
23007 pubname_ref pub;
23008 dw_die_ref base_type;
23010 #if ENABLE_ASSERT_CHECKING
23011 /* All the marks should already be clear. */
23012 verify_marks_clear (comp_unit_die ());
23013 for (node = limbo_die_list; node; node = node->next)
23014 verify_marks_clear (node->die);
23015 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23016 verify_marks_clear (ctnode->root_die);
23017 #endif /* ENABLE_ASSERT_CHECKING */
23019 /* Mark types that are used in global variables. */
23020 premark_types_used_by_global_vars ();
23022 /* Set the mark on nodes that are actually used. */
23023 prune_unused_types_walk (comp_unit_die ());
23024 for (node = limbo_die_list; node; node = node->next)
23025 prune_unused_types_walk (node->die);
23026 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23028 prune_unused_types_walk (ctnode->root_die);
23029 prune_unused_types_mark (ctnode->type_die, 1);
23032 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23033 are unusual in that they are pubnames that are the children of pubtypes.
23034 They should only be marked via their parent DW_TAG_enumeration_type die,
23035 not as roots in themselves. */
23036 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23037 if (pub->die->die_tag != DW_TAG_enumerator)
23038 prune_unused_types_mark (pub->die, 1);
23039 for (i = 0; base_types.iterate (i, &base_type); i++)
23040 prune_unused_types_mark (base_type, 1);
23042 if (debug_str_hash)
23043 debug_str_hash->empty ();
23044 if (skeleton_debug_str_hash)
23045 skeleton_debug_str_hash->empty ();
23046 prune_unused_types_prune (comp_unit_die ());
23047 for (node = limbo_die_list; node; node = node->next)
23048 prune_unused_types_prune (node->die);
23049 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23050 prune_unused_types_prune (ctnode->root_die);
23052 /* Leave the marks clear. */
23053 prune_unmark_dies (comp_unit_die ());
23054 for (node = limbo_die_list; node; node = node->next)
23055 prune_unmark_dies (node->die);
23056 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23057 prune_unmark_dies (ctnode->root_die);
23060 /* Set the parameter to true if there are any relative pathnames in
23061 the file table. */
23063 file_table_relative_p (dwarf_file_data **slot, bool *p)
23065 struct dwarf_file_data *d = *slot;
23066 if (!IS_ABSOLUTE_PATH (d->filename))
23068 *p = true;
23069 return 0;
23071 return 1;
23074 /* Helpers to manipulate hash table of comdat type units. */
23076 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23078 typedef comdat_type_node value_type;
23079 typedef comdat_type_node compare_type;
23080 static inline hashval_t hash (const value_type *);
23081 static inline bool equal (const value_type *, const compare_type *);
23084 inline hashval_t
23085 comdat_type_hasher::hash (const value_type *type_node)
23087 hashval_t h;
23088 memcpy (&h, type_node->signature, sizeof (h));
23089 return h;
23092 inline bool
23093 comdat_type_hasher::equal (const value_type *type_node_1,
23094 const compare_type *type_node_2)
23096 return (! memcmp (type_node_1->signature, type_node_2->signature,
23097 DWARF_TYPE_SIGNATURE_SIZE));
23100 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23101 to the location it would have been added, should we know its
23102 DECL_ASSEMBLER_NAME when we added other attributes. This will
23103 probably improve compactness of debug info, removing equivalent
23104 abbrevs, and hide any differences caused by deferring the
23105 computation of the assembler name, triggered by e.g. PCH. */
23107 static inline void
23108 move_linkage_attr (dw_die_ref die)
23110 unsigned ix = vec_safe_length (die->die_attr);
23111 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23113 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23114 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23116 while (--ix > 0)
23118 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23120 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23121 break;
23124 if (ix != vec_safe_length (die->die_attr) - 1)
23126 die->die_attr->pop ();
23127 die->die_attr->quick_insert (ix, linkage);
23131 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23132 referenced from typed stack ops and count how often they are used. */
23134 static void
23135 mark_base_types (dw_loc_descr_ref loc)
23137 dw_die_ref base_type = NULL;
23139 for (; loc; loc = loc->dw_loc_next)
23141 switch (loc->dw_loc_opc)
23143 case DW_OP_GNU_regval_type:
23144 case DW_OP_GNU_deref_type:
23145 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23146 break;
23147 case DW_OP_GNU_convert:
23148 case DW_OP_GNU_reinterpret:
23149 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23150 continue;
23151 /* FALLTHRU */
23152 case DW_OP_GNU_const_type:
23153 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23154 break;
23155 case DW_OP_GNU_entry_value:
23156 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23157 continue;
23158 default:
23159 continue;
23161 gcc_assert (base_type->die_parent == comp_unit_die ());
23162 if (base_type->die_mark)
23163 base_type->die_mark++;
23164 else
23166 base_types.safe_push (base_type);
23167 base_type->die_mark = 1;
23172 /* Comparison function for sorting marked base types. */
23174 static int
23175 base_type_cmp (const void *x, const void *y)
23177 dw_die_ref dx = *(const dw_die_ref *) x;
23178 dw_die_ref dy = *(const dw_die_ref *) y;
23179 unsigned int byte_size1, byte_size2;
23180 unsigned int encoding1, encoding2;
23181 if (dx->die_mark > dy->die_mark)
23182 return -1;
23183 if (dx->die_mark < dy->die_mark)
23184 return 1;
23185 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23186 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23187 if (byte_size1 < byte_size2)
23188 return 1;
23189 if (byte_size1 > byte_size2)
23190 return -1;
23191 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23192 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23193 if (encoding1 < encoding2)
23194 return 1;
23195 if (encoding1 > encoding2)
23196 return -1;
23197 return 0;
23200 /* Move base types marked by mark_base_types as early as possible
23201 in the CU, sorted by decreasing usage count both to make the
23202 uleb128 references as small as possible and to make sure they
23203 will have die_offset already computed by calc_die_sizes when
23204 sizes of typed stack loc ops is computed. */
23206 static void
23207 move_marked_base_types (void)
23209 unsigned int i;
23210 dw_die_ref base_type, die, c;
23212 if (base_types.is_empty ())
23213 return;
23215 /* Sort by decreasing usage count, they will be added again in that
23216 order later on. */
23217 base_types.qsort (base_type_cmp);
23218 die = comp_unit_die ();
23219 c = die->die_child;
23222 dw_die_ref prev = c;
23223 c = c->die_sib;
23224 while (c->die_mark)
23226 remove_child_with_prev (c, prev);
23227 /* As base types got marked, there must be at least
23228 one node other than DW_TAG_base_type. */
23229 gcc_assert (c != c->die_sib);
23230 c = c->die_sib;
23233 while (c != die->die_child);
23234 gcc_assert (die->die_child);
23235 c = die->die_child;
23236 for (i = 0; base_types.iterate (i, &base_type); i++)
23238 base_type->die_mark = 0;
23239 base_type->die_sib = c->die_sib;
23240 c->die_sib = base_type;
23241 c = base_type;
23245 /* Helper function for resolve_addr, attempt to resolve
23246 one CONST_STRING, return true if successful. Similarly verify that
23247 SYMBOL_REFs refer to variables emitted in the current CU. */
23249 static bool
23250 resolve_one_addr (rtx *addr)
23252 rtx rtl = *addr;
23254 if (GET_CODE (rtl) == CONST_STRING)
23256 size_t len = strlen (XSTR (rtl, 0)) + 1;
23257 tree t = build_string (len, XSTR (rtl, 0));
23258 tree tlen = size_int (len - 1);
23259 TREE_TYPE (t)
23260 = build_array_type (char_type_node, build_index_type (tlen));
23261 rtl = lookup_constant_def (t);
23262 if (!rtl || !MEM_P (rtl))
23263 return false;
23264 rtl = XEXP (rtl, 0);
23265 if (GET_CODE (rtl) == SYMBOL_REF
23266 && SYMBOL_REF_DECL (rtl)
23267 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23268 return false;
23269 vec_safe_push (used_rtx_array, rtl);
23270 *addr = rtl;
23271 return true;
23274 if (GET_CODE (rtl) == SYMBOL_REF
23275 && SYMBOL_REF_DECL (rtl))
23277 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23279 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23280 return false;
23282 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23283 return false;
23286 if (GET_CODE (rtl) == CONST)
23288 subrtx_ptr_iterator::array_type array;
23289 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23290 if (!resolve_one_addr (*iter))
23291 return false;
23294 return true;
23297 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23298 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23299 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23301 static rtx
23302 string_cst_pool_decl (tree t)
23304 rtx rtl = output_constant_def (t, 1);
23305 unsigned char *array;
23306 dw_loc_descr_ref l;
23307 tree decl;
23308 size_t len;
23309 dw_die_ref ref;
23311 if (!rtl || !MEM_P (rtl))
23312 return NULL_RTX;
23313 rtl = XEXP (rtl, 0);
23314 if (GET_CODE (rtl) != SYMBOL_REF
23315 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23316 return NULL_RTX;
23318 decl = SYMBOL_REF_DECL (rtl);
23319 if (!lookup_decl_die (decl))
23321 len = TREE_STRING_LENGTH (t);
23322 vec_safe_push (used_rtx_array, rtl);
23323 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23324 array = ggc_vec_alloc<unsigned char> (len);
23325 memcpy (array, TREE_STRING_POINTER (t), len);
23326 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23327 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23328 l->dw_loc_oprnd2.v.val_vec.length = len;
23329 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23330 l->dw_loc_oprnd2.v.val_vec.array = array;
23331 add_AT_loc (ref, DW_AT_location, l);
23332 equate_decl_number_to_die (decl, ref);
23334 return rtl;
23337 /* Helper function of resolve_addr_in_expr. LOC is
23338 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23339 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23340 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23341 with DW_OP_GNU_implicit_pointer if possible
23342 and return true, if unsuccessful, return false. */
23344 static bool
23345 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23347 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23348 HOST_WIDE_INT offset = 0;
23349 dw_die_ref ref = NULL;
23350 tree decl;
23352 if (GET_CODE (rtl) == CONST
23353 && GET_CODE (XEXP (rtl, 0)) == PLUS
23354 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23356 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23357 rtl = XEXP (XEXP (rtl, 0), 0);
23359 if (GET_CODE (rtl) == CONST_STRING)
23361 size_t len = strlen (XSTR (rtl, 0)) + 1;
23362 tree t = build_string (len, XSTR (rtl, 0));
23363 tree tlen = size_int (len - 1);
23365 TREE_TYPE (t)
23366 = build_array_type (char_type_node, build_index_type (tlen));
23367 rtl = string_cst_pool_decl (t);
23368 if (!rtl)
23369 return false;
23371 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23373 decl = SYMBOL_REF_DECL (rtl);
23374 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23376 ref = lookup_decl_die (decl);
23377 if (ref && (get_AT (ref, DW_AT_location)
23378 || get_AT (ref, DW_AT_const_value)))
23380 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23381 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23382 loc->dw_loc_oprnd1.val_entry = NULL;
23383 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23384 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23385 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23386 loc->dw_loc_oprnd2.v.val_int = offset;
23387 return true;
23391 return false;
23394 /* Helper function for resolve_addr, handle one location
23395 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23396 the location list couldn't be resolved. */
23398 static bool
23399 resolve_addr_in_expr (dw_loc_descr_ref loc)
23401 dw_loc_descr_ref keep = NULL;
23402 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23403 switch (loc->dw_loc_opc)
23405 case DW_OP_addr:
23406 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23408 if ((prev == NULL
23409 || prev->dw_loc_opc == DW_OP_piece
23410 || prev->dw_loc_opc == DW_OP_bit_piece)
23411 && loc->dw_loc_next
23412 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23413 && !dwarf_strict
23414 && optimize_one_addr_into_implicit_ptr (loc))
23415 break;
23416 return false;
23418 break;
23419 case DW_OP_GNU_addr_index:
23420 case DW_OP_GNU_const_index:
23421 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23422 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23424 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23425 if (!resolve_one_addr (&rtl))
23426 return false;
23427 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23428 loc->dw_loc_oprnd1.val_entry =
23429 add_addr_table_entry (rtl, ate_kind_rtx);
23431 break;
23432 case DW_OP_const4u:
23433 case DW_OP_const8u:
23434 if (loc->dtprel
23435 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23436 return false;
23437 break;
23438 case DW_OP_plus_uconst:
23439 if (size_of_loc_descr (loc)
23440 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23442 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23444 dw_loc_descr_ref repl
23445 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23446 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23447 add_loc_descr (&repl, loc->dw_loc_next);
23448 *loc = *repl;
23450 break;
23451 case DW_OP_implicit_value:
23452 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23453 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23454 return false;
23455 break;
23456 case DW_OP_GNU_implicit_pointer:
23457 case DW_OP_GNU_parameter_ref:
23458 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23460 dw_die_ref ref
23461 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23462 if (ref == NULL)
23463 return false;
23464 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23465 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23466 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23468 break;
23469 case DW_OP_GNU_const_type:
23470 case DW_OP_GNU_regval_type:
23471 case DW_OP_GNU_deref_type:
23472 case DW_OP_GNU_convert:
23473 case DW_OP_GNU_reinterpret:
23474 while (loc->dw_loc_next
23475 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23477 dw_die_ref base1, base2;
23478 unsigned enc1, enc2, size1, size2;
23479 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23480 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23481 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23482 else if (loc->dw_loc_oprnd1.val_class
23483 == dw_val_class_unsigned_const)
23484 break;
23485 else
23486 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23487 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23488 == dw_val_class_unsigned_const)
23489 break;
23490 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23491 gcc_assert (base1->die_tag == DW_TAG_base_type
23492 && base2->die_tag == DW_TAG_base_type);
23493 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23494 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23495 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23496 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23497 if (size1 == size2
23498 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23499 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23500 && loc != keep)
23501 || enc1 == enc2))
23503 /* Optimize away next DW_OP_GNU_convert after
23504 adjusting LOC's base type die reference. */
23505 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23506 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23507 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23508 else
23509 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23510 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23511 continue;
23513 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23514 point typed stack entry. */
23515 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23516 keep = loc->dw_loc_next;
23517 break;
23519 break;
23520 default:
23521 break;
23523 return true;
23526 /* Helper function of resolve_addr. DIE had DW_AT_location of
23527 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23528 and DW_OP_addr couldn't be resolved. resolve_addr has already
23529 removed the DW_AT_location attribute. This function attempts to
23530 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23531 to it or DW_AT_const_value attribute, if possible. */
23533 static void
23534 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23536 if (TREE_CODE (decl) != VAR_DECL
23537 || lookup_decl_die (decl) != die
23538 || DECL_EXTERNAL (decl)
23539 || !TREE_STATIC (decl)
23540 || DECL_INITIAL (decl) == NULL_TREE
23541 || DECL_P (DECL_INITIAL (decl))
23542 || get_AT (die, DW_AT_const_value))
23543 return;
23545 tree init = DECL_INITIAL (decl);
23546 HOST_WIDE_INT offset = 0;
23547 /* For variables that have been optimized away and thus
23548 don't have a memory location, see if we can emit
23549 DW_AT_const_value instead. */
23550 if (tree_add_const_value_attribute (die, init))
23551 return;
23552 if (dwarf_strict)
23553 return;
23554 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23555 and ADDR_EXPR refers to a decl that has DW_AT_location or
23556 DW_AT_const_value (but isn't addressable, otherwise
23557 resolving the original DW_OP_addr wouldn't fail), see if
23558 we can add DW_OP_GNU_implicit_pointer. */
23559 STRIP_NOPS (init);
23560 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23561 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23563 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23564 init = TREE_OPERAND (init, 0);
23565 STRIP_NOPS (init);
23567 if (TREE_CODE (init) != ADDR_EXPR)
23568 return;
23569 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23570 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23571 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23572 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23573 && TREE_OPERAND (init, 0) != decl))
23575 dw_die_ref ref;
23576 dw_loc_descr_ref l;
23578 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23580 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23581 if (!rtl)
23582 return;
23583 decl = SYMBOL_REF_DECL (rtl);
23585 else
23586 decl = TREE_OPERAND (init, 0);
23587 ref = lookup_decl_die (decl);
23588 if (ref == NULL
23589 || (!get_AT (ref, DW_AT_location)
23590 && !get_AT (ref, DW_AT_const_value)))
23591 return;
23592 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23593 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23594 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23595 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23596 add_AT_loc (die, DW_AT_location, l);
23600 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23601 an address in .rodata section if the string literal is emitted there,
23602 or remove the containing location list or replace DW_AT_const_value
23603 with DW_AT_location and empty location expression, if it isn't found
23604 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23605 to something that has been emitted in the current CU. */
23607 static void
23608 resolve_addr (dw_die_ref die)
23610 dw_die_ref c;
23611 dw_attr_ref a;
23612 dw_loc_list_ref *curr, *start, loc;
23613 unsigned ix;
23615 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23616 switch (AT_class (a))
23618 case dw_val_class_loc_list:
23619 start = curr = AT_loc_list_ptr (a);
23620 loc = *curr;
23621 gcc_assert (loc);
23622 /* The same list can be referenced more than once. See if we have
23623 already recorded the result from a previous pass. */
23624 if (loc->replaced)
23625 *curr = loc->dw_loc_next;
23626 else if (!loc->resolved_addr)
23628 /* As things stand, we do not expect or allow one die to
23629 reference a suffix of another die's location list chain.
23630 References must be identical or completely separate.
23631 There is therefore no need to cache the result of this
23632 pass on any list other than the first; doing so
23633 would lead to unnecessary writes. */
23634 while (*curr)
23636 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23637 if (!resolve_addr_in_expr ((*curr)->expr))
23639 dw_loc_list_ref next = (*curr)->dw_loc_next;
23640 dw_loc_descr_ref l = (*curr)->expr;
23642 if (next && (*curr)->ll_symbol)
23644 gcc_assert (!next->ll_symbol);
23645 next->ll_symbol = (*curr)->ll_symbol;
23647 if (dwarf_split_debug_info)
23648 remove_loc_list_addr_table_entries (l);
23649 *curr = next;
23651 else
23653 mark_base_types ((*curr)->expr);
23654 curr = &(*curr)->dw_loc_next;
23657 if (loc == *start)
23658 loc->resolved_addr = 1;
23659 else
23661 loc->replaced = 1;
23662 loc->dw_loc_next = *start;
23665 if (!*start)
23667 remove_AT (die, a->dw_attr);
23668 ix--;
23670 break;
23671 case dw_val_class_loc:
23673 dw_loc_descr_ref l = AT_loc (a);
23674 /* For -gdwarf-2 don't attempt to optimize
23675 DW_AT_data_member_location containing
23676 DW_OP_plus_uconst - older consumers might
23677 rely on it being that op instead of a more complex,
23678 but shorter, location description. */
23679 if ((dwarf_version > 2
23680 || a->dw_attr != DW_AT_data_member_location
23681 || l == NULL
23682 || l->dw_loc_opc != DW_OP_plus_uconst
23683 || l->dw_loc_next != NULL)
23684 && !resolve_addr_in_expr (l))
23686 if (dwarf_split_debug_info)
23687 remove_loc_list_addr_table_entries (l);
23688 if (l != NULL
23689 && l->dw_loc_next == NULL
23690 && l->dw_loc_opc == DW_OP_addr
23691 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23692 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23693 && a->dw_attr == DW_AT_location)
23695 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23696 remove_AT (die, a->dw_attr);
23697 ix--;
23698 optimize_location_into_implicit_ptr (die, decl);
23699 break;
23701 remove_AT (die, a->dw_attr);
23702 ix--;
23704 else
23705 mark_base_types (l);
23707 break;
23708 case dw_val_class_addr:
23709 if (a->dw_attr == DW_AT_const_value
23710 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23712 if (AT_index (a) != NOT_INDEXED)
23713 remove_addr_table_entry (a->dw_attr_val.val_entry);
23714 remove_AT (die, a->dw_attr);
23715 ix--;
23717 if (die->die_tag == DW_TAG_GNU_call_site
23718 && a->dw_attr == DW_AT_abstract_origin)
23720 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23721 dw_die_ref tdie = lookup_decl_die (tdecl);
23722 if (tdie == NULL
23723 && DECL_EXTERNAL (tdecl)
23724 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23726 force_decl_die (tdecl);
23727 tdie = lookup_decl_die (tdecl);
23729 if (tdie)
23731 a->dw_attr_val.val_class = dw_val_class_die_ref;
23732 a->dw_attr_val.v.val_die_ref.die = tdie;
23733 a->dw_attr_val.v.val_die_ref.external = 0;
23735 else
23737 if (AT_index (a) != NOT_INDEXED)
23738 remove_addr_table_entry (a->dw_attr_val.val_entry);
23739 remove_AT (die, a->dw_attr);
23740 ix--;
23743 break;
23744 default:
23745 break;
23748 FOR_EACH_CHILD (die, c, resolve_addr (c));
23751 /* Helper routines for optimize_location_lists.
23752 This pass tries to share identical local lists in .debug_loc
23753 section. */
23755 /* Iteratively hash operands of LOC opcode into HSTATE. */
23757 static void
23758 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23760 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23761 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23763 switch (loc->dw_loc_opc)
23765 case DW_OP_const4u:
23766 case DW_OP_const8u:
23767 if (loc->dtprel)
23768 goto hash_addr;
23769 /* FALLTHRU */
23770 case DW_OP_const1u:
23771 case DW_OP_const1s:
23772 case DW_OP_const2u:
23773 case DW_OP_const2s:
23774 case DW_OP_const4s:
23775 case DW_OP_const8s:
23776 case DW_OP_constu:
23777 case DW_OP_consts:
23778 case DW_OP_pick:
23779 case DW_OP_plus_uconst:
23780 case DW_OP_breg0:
23781 case DW_OP_breg1:
23782 case DW_OP_breg2:
23783 case DW_OP_breg3:
23784 case DW_OP_breg4:
23785 case DW_OP_breg5:
23786 case DW_OP_breg6:
23787 case DW_OP_breg7:
23788 case DW_OP_breg8:
23789 case DW_OP_breg9:
23790 case DW_OP_breg10:
23791 case DW_OP_breg11:
23792 case DW_OP_breg12:
23793 case DW_OP_breg13:
23794 case DW_OP_breg14:
23795 case DW_OP_breg15:
23796 case DW_OP_breg16:
23797 case DW_OP_breg17:
23798 case DW_OP_breg18:
23799 case DW_OP_breg19:
23800 case DW_OP_breg20:
23801 case DW_OP_breg21:
23802 case DW_OP_breg22:
23803 case DW_OP_breg23:
23804 case DW_OP_breg24:
23805 case DW_OP_breg25:
23806 case DW_OP_breg26:
23807 case DW_OP_breg27:
23808 case DW_OP_breg28:
23809 case DW_OP_breg29:
23810 case DW_OP_breg30:
23811 case DW_OP_breg31:
23812 case DW_OP_regx:
23813 case DW_OP_fbreg:
23814 case DW_OP_piece:
23815 case DW_OP_deref_size:
23816 case DW_OP_xderef_size:
23817 hstate.add_object (val1->v.val_int);
23818 break;
23819 case DW_OP_skip:
23820 case DW_OP_bra:
23822 int offset;
23824 gcc_assert (val1->val_class == dw_val_class_loc);
23825 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23826 hstate.add_object (offset);
23828 break;
23829 case DW_OP_implicit_value:
23830 hstate.add_object (val1->v.val_unsigned);
23831 switch (val2->val_class)
23833 case dw_val_class_const:
23834 hstate.add_object (val2->v.val_int);
23835 break;
23836 case dw_val_class_vec:
23838 unsigned int elt_size = val2->v.val_vec.elt_size;
23839 unsigned int len = val2->v.val_vec.length;
23841 hstate.add_int (elt_size);
23842 hstate.add_int (len);
23843 hstate.add (val2->v.val_vec.array, len * elt_size);
23845 break;
23846 case dw_val_class_const_double:
23847 hstate.add_object (val2->v.val_double.low);
23848 hstate.add_object (val2->v.val_double.high);
23849 break;
23850 case dw_val_class_wide_int:
23851 hstate.add_object (*val2->v.val_wide);
23852 break;
23853 case dw_val_class_addr:
23854 inchash::add_rtx (val2->v.val_addr, hstate);
23855 break;
23856 default:
23857 gcc_unreachable ();
23859 break;
23860 case DW_OP_bregx:
23861 case DW_OP_bit_piece:
23862 hstate.add_object (val1->v.val_int);
23863 hstate.add_object (val2->v.val_int);
23864 break;
23865 case DW_OP_addr:
23866 hash_addr:
23867 if (loc->dtprel)
23869 unsigned char dtprel = 0xd1;
23870 hstate.add_object (dtprel);
23872 inchash::add_rtx (val1->v.val_addr, hstate);
23873 break;
23874 case DW_OP_GNU_addr_index:
23875 case DW_OP_GNU_const_index:
23877 if (loc->dtprel)
23879 unsigned char dtprel = 0xd1;
23880 hstate.add_object (dtprel);
23882 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
23884 break;
23885 case DW_OP_GNU_implicit_pointer:
23886 hstate.add_int (val2->v.val_int);
23887 break;
23888 case DW_OP_GNU_entry_value:
23889 hstate.add_object (val1->v.val_loc);
23890 break;
23891 case DW_OP_GNU_regval_type:
23892 case DW_OP_GNU_deref_type:
23894 unsigned int byte_size
23895 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23896 unsigned int encoding
23897 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23898 hstate.add_object (val1->v.val_int);
23899 hstate.add_object (byte_size);
23900 hstate.add_object (encoding);
23902 break;
23903 case DW_OP_GNU_convert:
23904 case DW_OP_GNU_reinterpret:
23905 if (val1->val_class == dw_val_class_unsigned_const)
23907 hstate.add_object (val1->v.val_unsigned);
23908 break;
23910 /* FALLTHRU */
23911 case DW_OP_GNU_const_type:
23913 unsigned int byte_size
23914 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23915 unsigned int encoding
23916 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23917 hstate.add_object (byte_size);
23918 hstate.add_object (encoding);
23919 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23920 break;
23921 hstate.add_object (val2->val_class);
23922 switch (val2->val_class)
23924 case dw_val_class_const:
23925 hstate.add_object (val2->v.val_int);
23926 break;
23927 case dw_val_class_vec:
23929 unsigned int elt_size = val2->v.val_vec.elt_size;
23930 unsigned int len = val2->v.val_vec.length;
23932 hstate.add_object (elt_size);
23933 hstate.add_object (len);
23934 hstate.add (val2->v.val_vec.array, len * elt_size);
23936 break;
23937 case dw_val_class_const_double:
23938 hstate.add_object (val2->v.val_double.low);
23939 hstate.add_object (val2->v.val_double.high);
23940 break;
23941 case dw_val_class_wide_int:
23942 hstate.add_object (*val2->v.val_wide);
23943 break;
23944 default:
23945 gcc_unreachable ();
23948 break;
23950 default:
23951 /* Other codes have no operands. */
23952 break;
23956 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
23958 static inline void
23959 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
23961 dw_loc_descr_ref l;
23962 bool sizes_computed = false;
23963 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23964 size_of_locs (loc);
23966 for (l = loc; l != NULL; l = l->dw_loc_next)
23968 enum dwarf_location_atom opc = l->dw_loc_opc;
23969 hstate.add_object (opc);
23970 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23972 size_of_locs (loc);
23973 sizes_computed = true;
23975 hash_loc_operands (l, hstate);
23979 /* Compute hash of the whole location list LIST_HEAD. */
23981 static inline void
23982 hash_loc_list (dw_loc_list_ref list_head)
23984 dw_loc_list_ref curr = list_head;
23985 inchash::hash hstate;
23987 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23989 hstate.add (curr->begin, strlen (curr->begin) + 1);
23990 hstate.add (curr->end, strlen (curr->end) + 1);
23991 if (curr->section)
23992 hstate.add (curr->section, strlen (curr->section) + 1);
23993 hash_locs (curr->expr, hstate);
23995 list_head->hash = hstate.end ();
23998 /* Return true if X and Y opcodes have the same operands. */
24000 static inline bool
24001 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24003 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24004 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24005 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24006 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24008 switch (x->dw_loc_opc)
24010 case DW_OP_const4u:
24011 case DW_OP_const8u:
24012 if (x->dtprel)
24013 goto hash_addr;
24014 /* FALLTHRU */
24015 case DW_OP_const1u:
24016 case DW_OP_const1s:
24017 case DW_OP_const2u:
24018 case DW_OP_const2s:
24019 case DW_OP_const4s:
24020 case DW_OP_const8s:
24021 case DW_OP_constu:
24022 case DW_OP_consts:
24023 case DW_OP_pick:
24024 case DW_OP_plus_uconst:
24025 case DW_OP_breg0:
24026 case DW_OP_breg1:
24027 case DW_OP_breg2:
24028 case DW_OP_breg3:
24029 case DW_OP_breg4:
24030 case DW_OP_breg5:
24031 case DW_OP_breg6:
24032 case DW_OP_breg7:
24033 case DW_OP_breg8:
24034 case DW_OP_breg9:
24035 case DW_OP_breg10:
24036 case DW_OP_breg11:
24037 case DW_OP_breg12:
24038 case DW_OP_breg13:
24039 case DW_OP_breg14:
24040 case DW_OP_breg15:
24041 case DW_OP_breg16:
24042 case DW_OP_breg17:
24043 case DW_OP_breg18:
24044 case DW_OP_breg19:
24045 case DW_OP_breg20:
24046 case DW_OP_breg21:
24047 case DW_OP_breg22:
24048 case DW_OP_breg23:
24049 case DW_OP_breg24:
24050 case DW_OP_breg25:
24051 case DW_OP_breg26:
24052 case DW_OP_breg27:
24053 case DW_OP_breg28:
24054 case DW_OP_breg29:
24055 case DW_OP_breg30:
24056 case DW_OP_breg31:
24057 case DW_OP_regx:
24058 case DW_OP_fbreg:
24059 case DW_OP_piece:
24060 case DW_OP_deref_size:
24061 case DW_OP_xderef_size:
24062 return valx1->v.val_int == valy1->v.val_int;
24063 case DW_OP_skip:
24064 case DW_OP_bra:
24065 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24066 can cause irrelevant differences in dw_loc_addr. */
24067 gcc_assert (valx1->val_class == dw_val_class_loc
24068 && valy1->val_class == dw_val_class_loc
24069 && (dwarf_split_debug_info
24070 || x->dw_loc_addr == y->dw_loc_addr));
24071 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24072 case DW_OP_implicit_value:
24073 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24074 || valx2->val_class != valy2->val_class)
24075 return false;
24076 switch (valx2->val_class)
24078 case dw_val_class_const:
24079 return valx2->v.val_int == valy2->v.val_int;
24080 case dw_val_class_vec:
24081 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24082 && valx2->v.val_vec.length == valy2->v.val_vec.length
24083 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24084 valx2->v.val_vec.elt_size
24085 * valx2->v.val_vec.length) == 0;
24086 case dw_val_class_const_double:
24087 return valx2->v.val_double.low == valy2->v.val_double.low
24088 && valx2->v.val_double.high == valy2->v.val_double.high;
24089 case dw_val_class_wide_int:
24090 return *valx2->v.val_wide == *valy2->v.val_wide;
24091 case dw_val_class_addr:
24092 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24093 default:
24094 gcc_unreachable ();
24096 case DW_OP_bregx:
24097 case DW_OP_bit_piece:
24098 return valx1->v.val_int == valy1->v.val_int
24099 && valx2->v.val_int == valy2->v.val_int;
24100 case DW_OP_addr:
24101 hash_addr:
24102 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24103 case DW_OP_GNU_addr_index:
24104 case DW_OP_GNU_const_index:
24106 rtx ax1 = valx1->val_entry->addr.rtl;
24107 rtx ay1 = valy1->val_entry->addr.rtl;
24108 return rtx_equal_p (ax1, ay1);
24110 case DW_OP_GNU_implicit_pointer:
24111 return valx1->val_class == dw_val_class_die_ref
24112 && valx1->val_class == valy1->val_class
24113 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24114 && valx2->v.val_int == valy2->v.val_int;
24115 case DW_OP_GNU_entry_value:
24116 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24117 case DW_OP_GNU_const_type:
24118 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24119 || valx2->val_class != valy2->val_class)
24120 return false;
24121 switch (valx2->val_class)
24123 case dw_val_class_const:
24124 return valx2->v.val_int == valy2->v.val_int;
24125 case dw_val_class_vec:
24126 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24127 && valx2->v.val_vec.length == valy2->v.val_vec.length
24128 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24129 valx2->v.val_vec.elt_size
24130 * valx2->v.val_vec.length) == 0;
24131 case dw_val_class_const_double:
24132 return valx2->v.val_double.low == valy2->v.val_double.low
24133 && valx2->v.val_double.high == valy2->v.val_double.high;
24134 case dw_val_class_wide_int:
24135 return *valx2->v.val_wide == *valy2->v.val_wide;
24136 default:
24137 gcc_unreachable ();
24139 case DW_OP_GNU_regval_type:
24140 case DW_OP_GNU_deref_type:
24141 return valx1->v.val_int == valy1->v.val_int
24142 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24143 case DW_OP_GNU_convert:
24144 case DW_OP_GNU_reinterpret:
24145 if (valx1->val_class != valy1->val_class)
24146 return false;
24147 if (valx1->val_class == dw_val_class_unsigned_const)
24148 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24149 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24150 case DW_OP_GNU_parameter_ref:
24151 return valx1->val_class == dw_val_class_die_ref
24152 && valx1->val_class == valy1->val_class
24153 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24154 default:
24155 /* Other codes have no operands. */
24156 return true;
24160 /* Return true if DWARF location expressions X and Y are the same. */
24162 static inline bool
24163 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24165 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24166 if (x->dw_loc_opc != y->dw_loc_opc
24167 || x->dtprel != y->dtprel
24168 || !compare_loc_operands (x, y))
24169 break;
24170 return x == NULL && y == NULL;
24173 /* Hashtable helpers. */
24175 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24177 typedef dw_loc_list_struct value_type;
24178 typedef dw_loc_list_struct compare_type;
24179 static inline hashval_t hash (const value_type *);
24180 static inline bool equal (const value_type *, const compare_type *);
24183 /* Return precomputed hash of location list X. */
24185 inline hashval_t
24186 loc_list_hasher::hash (const value_type *x)
24188 return x->hash;
24191 /* Return true if location lists A and B are the same. */
24193 inline bool
24194 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24196 if (a == b)
24197 return 1;
24198 if (a->hash != b->hash)
24199 return 0;
24200 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24201 if (strcmp (a->begin, b->begin) != 0
24202 || strcmp (a->end, b->end) != 0
24203 || (a->section == NULL) != (b->section == NULL)
24204 || (a->section && strcmp (a->section, b->section) != 0)
24205 || !compare_locs (a->expr, b->expr))
24206 break;
24207 return a == NULL && b == NULL;
24210 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24213 /* Recursively optimize location lists referenced from DIE
24214 children and share them whenever possible. */
24216 static void
24217 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24219 dw_die_ref c;
24220 dw_attr_ref a;
24221 unsigned ix;
24222 dw_loc_list_struct **slot;
24224 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24225 if (AT_class (a) == dw_val_class_loc_list)
24227 dw_loc_list_ref list = AT_loc_list (a);
24228 /* TODO: perform some optimizations here, before hashing
24229 it and storing into the hash table. */
24230 hash_loc_list (list);
24231 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24232 if (*slot == NULL)
24233 *slot = list;
24234 else
24235 a->dw_attr_val.v.val_loc_list = *slot;
24238 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24242 /* Recursively assign each location list a unique index into the debug_addr
24243 section. */
24245 static void
24246 index_location_lists (dw_die_ref die)
24248 dw_die_ref c;
24249 dw_attr_ref a;
24250 unsigned ix;
24252 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24253 if (AT_class (a) == dw_val_class_loc_list)
24255 dw_loc_list_ref list = AT_loc_list (a);
24256 dw_loc_list_ref curr;
24257 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24259 /* Don't index an entry that has already been indexed
24260 or won't be output. */
24261 if (curr->begin_entry != NULL
24262 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24263 continue;
24265 curr->begin_entry
24266 = add_addr_table_entry (xstrdup (curr->begin),
24267 ate_kind_label);
24271 FOR_EACH_CHILD (die, c, index_location_lists (c));
24274 /* Optimize location lists referenced from DIE
24275 children and share them whenever possible. */
24277 static void
24278 optimize_location_lists (dw_die_ref die)
24280 loc_list_hash_type htab (500);
24281 optimize_location_lists_1 (die, &htab);
24284 /* Output stuff that dwarf requires at the end of every file,
24285 and generate the DWARF-2 debugging info. */
24287 static void
24288 dwarf2out_finish (const char *filename)
24290 limbo_die_node *node, *next_node;
24291 comdat_type_node *ctnode;
24292 unsigned int i;
24293 dw_die_ref main_comp_unit_die;
24295 /* PCH might result in DW_AT_producer string being restored from the
24296 header compilation, so always fill it with empty string initially
24297 and overwrite only here. */
24298 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24299 producer_string = gen_producer_string ();
24300 producer->dw_attr_val.v.val_str->refcount--;
24301 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24303 gen_scheduled_generic_parms_dies ();
24304 gen_remaining_tmpl_value_param_die_attribute ();
24306 /* Add the name for the main input file now. We delayed this from
24307 dwarf2out_init to avoid complications with PCH. */
24308 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24309 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24310 add_comp_dir_attribute (comp_unit_die ());
24311 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24313 bool p = false;
24314 file_table->traverse<bool *, file_table_relative_p> (&p);
24315 if (p)
24316 add_comp_dir_attribute (comp_unit_die ());
24319 if (deferred_locations_list)
24320 for (i = 0; i < deferred_locations_list->length (); i++)
24322 add_location_or_const_value_attribute (
24323 (*deferred_locations_list)[i].die,
24324 (*deferred_locations_list)[i].variable,
24325 false,
24326 DW_AT_location);
24329 /* Traverse the limbo die list, and add parent/child links. The only
24330 dies without parents that should be here are concrete instances of
24331 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24332 For concrete instances, we can get the parent die from the abstract
24333 instance. */
24334 for (node = limbo_die_list; node; node = next_node)
24336 dw_die_ref die = node->die;
24337 next_node = node->next;
24339 if (die->die_parent == NULL)
24341 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24343 if (origin && origin->die_parent)
24344 add_child_die (origin->die_parent, die);
24345 else if (is_cu_die (die))
24347 else if (seen_error ())
24348 /* It's OK to be confused by errors in the input. */
24349 add_child_die (comp_unit_die (), die);
24350 else
24352 /* In certain situations, the lexical block containing a
24353 nested function can be optimized away, which results
24354 in the nested function die being orphaned. Likewise
24355 with the return type of that nested function. Force
24356 this to be a child of the containing function.
24358 It may happen that even the containing function got fully
24359 inlined and optimized out. In that case we are lost and
24360 assign the empty child. This should not be big issue as
24361 the function is likely unreachable too. */
24362 gcc_assert (node->created_for);
24364 if (DECL_P (node->created_for))
24365 origin = get_context_die (DECL_CONTEXT (node->created_for));
24366 else if (TYPE_P (node->created_for))
24367 origin = scope_die_for (node->created_for, comp_unit_die ());
24368 else
24369 origin = comp_unit_die ();
24371 add_child_die (origin, die);
24376 limbo_die_list = NULL;
24378 #if ENABLE_ASSERT_CHECKING
24380 dw_die_ref die = comp_unit_die (), c;
24381 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24383 #endif
24384 resolve_addr (comp_unit_die ());
24385 move_marked_base_types ();
24387 for (node = deferred_asm_name; node; node = node->next)
24389 tree decl = node->created_for;
24390 /* When generating LTO bytecode we can not generate new assembler
24391 names at this point and all important decls got theirs via
24392 free-lang-data. */
24393 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24394 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24396 add_linkage_attr (node->die, decl);
24397 move_linkage_attr (node->die);
24401 deferred_asm_name = NULL;
24403 /* Walk through the list of incomplete types again, trying once more to
24404 emit full debugging info for them. */
24405 retry_incomplete_types ();
24407 if (flag_eliminate_unused_debug_types)
24408 prune_unused_types ();
24410 /* Generate separate COMDAT sections for type DIEs. */
24411 if (use_debug_types)
24413 break_out_comdat_types (comp_unit_die ());
24415 /* Each new type_unit DIE was added to the limbo die list when created.
24416 Since these have all been added to comdat_type_list, clear the
24417 limbo die list. */
24418 limbo_die_list = NULL;
24420 /* For each new comdat type unit, copy declarations for incomplete
24421 types to make the new unit self-contained (i.e., no direct
24422 references to the main compile unit). */
24423 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24424 copy_decls_for_unworthy_types (ctnode->root_die);
24425 copy_decls_for_unworthy_types (comp_unit_die ());
24427 /* In the process of copying declarations from one unit to another,
24428 we may have left some declarations behind that are no longer
24429 referenced. Prune them. */
24430 prune_unused_types ();
24433 /* Generate separate CUs for each of the include files we've seen.
24434 They will go into limbo_die_list. */
24435 if (flag_eliminate_dwarf2_dups)
24436 break_out_includes (comp_unit_die ());
24438 /* Traverse the DIE's and add add sibling attributes to those DIE's
24439 that have children. */
24440 add_sibling_attributes (comp_unit_die ());
24441 for (node = limbo_die_list; node; node = node->next)
24442 add_sibling_attributes (node->die);
24443 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24444 add_sibling_attributes (ctnode->root_die);
24446 /* When splitting DWARF info, we put some attributes in the
24447 skeleton compile_unit DIE that remains in the .o, while
24448 most attributes go in the DWO compile_unit_die. */
24449 if (dwarf_split_debug_info)
24450 main_comp_unit_die = gen_compile_unit_die (NULL);
24451 else
24452 main_comp_unit_die = comp_unit_die ();
24454 /* Output a terminator label for the .text section. */
24455 switch_to_section (text_section);
24456 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24457 if (cold_text_section)
24459 switch_to_section (cold_text_section);
24460 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24463 /* We can only use the low/high_pc attributes if all of the code was
24464 in .text. */
24465 if (!have_multiple_function_sections
24466 || (dwarf_version < 3 && dwarf_strict))
24468 /* Don't add if the CU has no associated code. */
24469 if (text_section_used)
24470 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24471 text_end_label, true);
24473 else
24475 unsigned fde_idx;
24476 dw_fde_ref fde;
24477 bool range_list_added = false;
24479 if (text_section_used)
24480 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24481 text_end_label, &range_list_added, true);
24482 if (cold_text_section_used)
24483 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24484 cold_end_label, &range_list_added, true);
24486 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24488 if (DECL_IGNORED_P (fde->decl))
24489 continue;
24490 if (!fde->in_std_section)
24491 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24492 fde->dw_fde_end, &range_list_added,
24493 true);
24494 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24495 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24496 fde->dw_fde_second_end, &range_list_added,
24497 true);
24500 if (range_list_added)
24502 /* We need to give .debug_loc and .debug_ranges an appropriate
24503 "base address". Use zero so that these addresses become
24504 absolute. Historically, we've emitted the unexpected
24505 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24506 Emit both to give time for other tools to adapt. */
24507 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24508 if (! dwarf_strict && dwarf_version < 4)
24509 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24511 add_ranges (NULL);
24515 if (debug_info_level >= DINFO_LEVEL_TERSE)
24516 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24517 debug_line_section_label);
24519 if (have_macinfo)
24520 add_AT_macptr (comp_unit_die (),
24521 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24522 macinfo_section_label);
24524 if (dwarf_split_debug_info)
24526 /* optimize_location_lists calculates the size of the lists,
24527 so index them first, and assign indices to the entries.
24528 Although optimize_location_lists will remove entries from
24529 the table, it only does so for duplicates, and therefore
24530 only reduces ref_counts to 1. */
24531 index_location_lists (comp_unit_die ());
24533 if (addr_index_table != NULL)
24535 unsigned int index = 0;
24536 addr_index_table
24537 ->traverse_noresize<unsigned int *, index_addr_table_entry>
24538 (&index);
24542 if (have_location_lists)
24543 optimize_location_lists (comp_unit_die ());
24545 save_macinfo_strings ();
24547 if (dwarf_split_debug_info)
24549 unsigned int index = 0;
24551 /* Add attributes common to skeleton compile_units and
24552 type_units. Because these attributes include strings, it
24553 must be done before freezing the string table. Top-level
24554 skeleton die attrs are added when the skeleton type unit is
24555 created, so ensure it is created by this point. */
24556 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24557 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24560 /* Output all of the compilation units. We put the main one last so that
24561 the offsets are available to output_pubnames. */
24562 for (node = limbo_die_list; node; node = node->next)
24563 output_comp_unit (node->die, 0);
24565 hash_table<comdat_type_hasher> comdat_type_table (100);
24566 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24568 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24570 /* Don't output duplicate types. */
24571 if (*slot != HTAB_EMPTY_ENTRY)
24572 continue;
24574 /* Add a pointer to the line table for the main compilation unit
24575 so that the debugger can make sense of DW_AT_decl_file
24576 attributes. */
24577 if (debug_info_level >= DINFO_LEVEL_TERSE)
24578 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24579 (!dwarf_split_debug_info
24580 ? debug_line_section_label
24581 : debug_skeleton_line_section_label));
24583 output_comdat_type_unit (ctnode);
24584 *slot = ctnode;
24587 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24588 both the main_cu and all skeleton TUs. Making this call unconditional
24589 would end up either adding a second copy of the AT_pubnames attribute, or
24590 requiring a special case in add_top_level_skeleton_die_attrs. */
24591 if (!dwarf_split_debug_info)
24592 add_AT_pubnames (comp_unit_die ());
24594 if (dwarf_split_debug_info)
24596 int mark;
24597 unsigned char checksum[16];
24598 struct md5_ctx ctx;
24600 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24601 md5_init_ctx (&ctx);
24602 mark = 0;
24603 die_checksum (comp_unit_die (), &ctx, &mark);
24604 unmark_all_dies (comp_unit_die ());
24605 md5_finish_ctx (&ctx, checksum);
24607 /* Use the first 8 bytes of the checksum as the dwo_id,
24608 and add it to both comp-unit DIEs. */
24609 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24610 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24612 /* Add the base offset of the ranges table to the skeleton
24613 comp-unit DIE. */
24614 if (ranges_table_in_use)
24615 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24616 ranges_section_label);
24618 switch_to_section (debug_addr_section);
24619 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24620 output_addr_table ();
24623 /* Output the main compilation unit if non-empty or if .debug_macinfo
24624 or .debug_macro will be emitted. */
24625 output_comp_unit (comp_unit_die (), have_macinfo);
24627 if (dwarf_split_debug_info && info_section_emitted)
24628 output_skeleton_debug_sections (main_comp_unit_die);
24630 /* Output the abbreviation table. */
24631 if (abbrev_die_table_in_use != 1)
24633 switch_to_section (debug_abbrev_section);
24634 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24635 output_abbrev_section ();
24638 /* Output location list section if necessary. */
24639 if (have_location_lists)
24641 /* Output the location lists info. */
24642 switch_to_section (debug_loc_section);
24643 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24644 output_location_lists (comp_unit_die ());
24647 output_pubtables ();
24649 /* Output the address range information if a CU (.debug_info section)
24650 was emitted. We output an empty table even if we had no functions
24651 to put in it. This because the consumer has no way to tell the
24652 difference between an empty table that we omitted and failure to
24653 generate a table that would have contained data. */
24654 if (info_section_emitted)
24656 unsigned long aranges_length = size_of_aranges ();
24658 switch_to_section (debug_aranges_section);
24659 output_aranges (aranges_length);
24662 /* Output ranges section if necessary. */
24663 if (ranges_table_in_use)
24665 switch_to_section (debug_ranges_section);
24666 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24667 output_ranges ();
24670 /* Have to end the macro section. */
24671 if (have_macinfo)
24673 switch_to_section (debug_macinfo_section);
24674 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24675 output_macinfo ();
24676 dw2_asm_output_data (1, 0, "End compilation unit");
24679 /* Output the source line correspondence table. We must do this
24680 even if there is no line information. Otherwise, on an empty
24681 translation unit, we will generate a present, but empty,
24682 .debug_info section. IRIX 6.5 `nm' will then complain when
24683 examining the file. This is done late so that any filenames
24684 used by the debug_info section are marked as 'used'. */
24685 switch_to_section (debug_line_section);
24686 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24687 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24688 output_line_info (false);
24690 if (dwarf_split_debug_info && info_section_emitted)
24692 switch_to_section (debug_skeleton_line_section);
24693 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24694 output_line_info (true);
24697 /* If we emitted any indirect strings, output the string table too. */
24698 if (debug_str_hash || skeleton_debug_str_hash)
24699 output_indirect_strings ();
24702 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24703 within the same process. For use by toplev::finalize. */
24705 void
24706 dwarf2out_c_finalize (void)
24708 last_var_location_insn = NULL;
24709 cached_next_real_insn = NULL;
24710 used_rtx_array = NULL;
24711 incomplete_types = NULL;
24712 decl_scope_table = NULL;
24713 debug_info_section = NULL;
24714 debug_skeleton_info_section = NULL;
24715 debug_abbrev_section = NULL;
24716 debug_skeleton_abbrev_section = NULL;
24717 debug_aranges_section = NULL;
24718 debug_addr_section = NULL;
24719 debug_macinfo_section = NULL;
24720 debug_line_section = NULL;
24721 debug_skeleton_line_section = NULL;
24722 debug_loc_section = NULL;
24723 debug_pubnames_section = NULL;
24724 debug_pubtypes_section = NULL;
24725 debug_str_section = NULL;
24726 debug_str_dwo_section = NULL;
24727 debug_str_offsets_section = NULL;
24728 debug_ranges_section = NULL;
24729 debug_frame_section = NULL;
24730 fde_vec = NULL;
24731 debug_str_hash = NULL;
24732 skeleton_debug_str_hash = NULL;
24733 dw2_string_counter = 0;
24734 have_multiple_function_sections = false;
24735 text_section_used = false;
24736 cold_text_section_used = false;
24737 cold_text_section = NULL;
24738 current_unit_personality = NULL;
24740 deferred_locations_list = NULL;
24742 next_die_offset = 0;
24743 single_comp_unit_die = NULL;
24744 comdat_type_list = NULL;
24745 limbo_die_list = NULL;
24746 deferred_asm_name = NULL;
24747 file_table = NULL;
24748 decl_die_table = NULL;
24749 common_block_die_table = NULL;
24750 decl_loc_table = NULL;
24751 call_arg_locations = NULL;
24752 call_arg_loc_last = NULL;
24753 call_site_count = -1;
24754 tail_call_site_count = -1;
24755 //block_map = NULL;
24756 cached_dw_loc_list_table = NULL;
24757 abbrev_die_table = NULL;
24758 abbrev_die_table_allocated = 0;
24759 abbrev_die_table_in_use = 0;
24760 line_info_label_num = 0;
24761 cur_line_info_table = NULL;
24762 text_section_line_info = NULL;
24763 cold_text_section_line_info = NULL;
24764 separate_line_info = NULL;
24765 info_section_emitted = false;
24766 pubname_table = NULL;
24767 pubtype_table = NULL;
24768 macinfo_table = NULL;
24769 ranges_table = NULL;
24770 ranges_table_allocated = 0;
24771 ranges_table_in_use = 0;
24772 ranges_by_label = 0;
24773 ranges_by_label_allocated = 0;
24774 ranges_by_label_in_use = 0;
24775 have_location_lists = false;
24776 loclabel_num = 0;
24777 poc_label_num = 0;
24778 last_emitted_file = NULL;
24779 label_num = 0;
24780 file_table_last_lookup = NULL;
24781 tmpl_value_parm_die_table = NULL;
24782 generic_type_instances = NULL;
24783 frame_pointer_fb_offset = 0;
24784 frame_pointer_fb_offset_valid = false;
24785 base_types.release ();
24786 XDELETEVEC (producer_string);
24787 producer_string = NULL;
24790 #include "gt-dwarf2out.h"