gcc/ada/
[official-gcc.git] / gcc / dwarf2out.c
blob7a29aae0bf815b956e0f8ff5085fec6abc323405
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 return 1;
10396 case ARRAY_TYPE:
10397 case RECORD_TYPE:
10398 case UNION_TYPE:
10399 case QUAL_UNION_TYPE:
10400 case ENUMERAL_TYPE:
10401 case FUNCTION_TYPE:
10402 case METHOD_TYPE:
10403 case POINTER_TYPE:
10404 case REFERENCE_TYPE:
10405 case NULLPTR_TYPE:
10406 case OFFSET_TYPE:
10407 case LANG_TYPE:
10408 case VECTOR_TYPE:
10409 return 0;
10411 default:
10412 if (is_cxx_auto (type))
10413 return 0;
10414 gcc_unreachable ();
10417 return 0;
10420 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10421 node, return the size in bits for the type if it is a constant, or else
10422 return the alignment for the type if the type's size is not constant, or
10423 else return BITS_PER_WORD if the type actually turns out to be an
10424 ERROR_MARK node. */
10426 static inline unsigned HOST_WIDE_INT
10427 simple_type_size_in_bits (const_tree type)
10429 if (TREE_CODE (type) == ERROR_MARK)
10430 return BITS_PER_WORD;
10431 else if (TYPE_SIZE (type) == NULL_TREE)
10432 return 0;
10433 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10434 return tree_to_uhwi (TYPE_SIZE (type));
10435 else
10436 return TYPE_ALIGN (type);
10439 /* Similarly, but return an offset_int instead of UHWI. */
10441 static inline offset_int
10442 offset_int_type_size_in_bits (const_tree type)
10444 if (TREE_CODE (type) == ERROR_MARK)
10445 return BITS_PER_WORD;
10446 else if (TYPE_SIZE (type) == NULL_TREE)
10447 return 0;
10448 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10449 return wi::to_offset (TYPE_SIZE (type));
10450 else
10451 return TYPE_ALIGN (type);
10454 /* Given a pointer to a tree node for a subrange type, return a pointer
10455 to a DIE that describes the given type. */
10457 static dw_die_ref
10458 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10460 dw_die_ref subrange_die;
10461 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10463 if (context_die == NULL)
10464 context_die = comp_unit_die ();
10466 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10468 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10470 /* The size of the subrange type and its base type do not match,
10471 so we need to generate a size attribute for the subrange type. */
10472 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10475 if (low)
10476 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10477 if (high)
10478 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10480 return subrange_die;
10483 /* Returns the (const and/or volatile) cv_qualifiers associated with
10484 the decl node. This will normally be augmented with the
10485 cv_qualifiers of the underlying type in add_type_attribute. */
10487 static int
10488 decl_quals (const_tree decl)
10490 return ((TREE_READONLY (decl)
10491 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10492 | (TREE_THIS_VOLATILE (decl)
10493 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10496 /* Determine the TYPE whose qualifiers match the largest strict subset
10497 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10498 qualifiers outside QUAL_MASK. */
10500 static int
10501 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10503 tree t;
10504 int best_rank = 0, best_qual = 0, max_rank;
10506 type_quals &= qual_mask;
10507 max_rank = popcount_hwi (type_quals) - 1;
10509 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10510 t = TYPE_NEXT_VARIANT (t))
10512 int q = TYPE_QUALS (t) & qual_mask;
10514 if ((q & type_quals) == q && q != type_quals
10515 && check_base_type (t, type))
10517 int rank = popcount_hwi (q);
10519 if (rank > best_rank)
10521 best_rank = rank;
10522 best_qual = q;
10527 return best_qual;
10530 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10531 entry that chains various modifiers in front of the given type. */
10533 static dw_die_ref
10534 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10536 enum tree_code code = TREE_CODE (type);
10537 dw_die_ref mod_type_die;
10538 dw_die_ref sub_die = NULL;
10539 tree item_type = NULL;
10540 tree qualified_type;
10541 tree name, low, high;
10542 dw_die_ref mod_scope;
10543 /* Only these cv-qualifiers are currently handled. */
10544 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10545 | TYPE_QUAL_RESTRICT);
10547 if (code == ERROR_MARK)
10548 return NULL;
10550 cv_quals &= cv_qual_mask;
10552 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10553 tag modifier (and not an attribute) old consumers won't be able
10554 to handle it. */
10555 if (dwarf_version < 3)
10556 cv_quals &= ~TYPE_QUAL_RESTRICT;
10558 /* See if we already have the appropriately qualified variant of
10559 this type. */
10560 qualified_type = get_qualified_type (type, cv_quals);
10562 if (qualified_type == sizetype
10563 && TYPE_NAME (qualified_type)
10564 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10566 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10568 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10569 && TYPE_PRECISION (t)
10570 == TYPE_PRECISION (qualified_type)
10571 && TYPE_UNSIGNED (t)
10572 == TYPE_UNSIGNED (qualified_type));
10573 qualified_type = t;
10576 /* If we do, then we can just use its DIE, if it exists. */
10577 if (qualified_type)
10579 mod_type_die = lookup_type_die (qualified_type);
10580 if (mod_type_die)
10581 return mod_type_die;
10584 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10586 /* Handle C typedef types. */
10587 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10588 && !DECL_ARTIFICIAL (name))
10590 tree dtype = TREE_TYPE (name);
10592 if (qualified_type == dtype)
10594 /* For a named type, use the typedef. */
10595 gen_type_die (qualified_type, context_die);
10596 return lookup_type_die (qualified_type);
10598 else
10600 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10601 dquals &= cv_qual_mask;
10602 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10603 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10604 /* cv-unqualified version of named type. Just use
10605 the unnamed type to which it refers. */
10606 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10607 cv_quals, context_die);
10608 /* Else cv-qualified version of named type; fall through. */
10612 mod_scope = scope_die_for (type, context_die);
10614 if (cv_quals)
10616 struct qual_info { int q; enum dwarf_tag t; };
10617 static const struct qual_info qual_info[] =
10619 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10620 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10621 { TYPE_QUAL_CONST, DW_TAG_const_type },
10623 int sub_quals;
10624 unsigned i;
10626 /* Determine a lesser qualified type that most closely matches
10627 this one. Then generate DW_TAG_* entries for the remaining
10628 qualifiers. */
10629 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10630 cv_qual_mask);
10631 mod_type_die = modified_type_die (type, sub_quals, context_die);
10633 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10634 if (qual_info[i].q & cv_quals & ~sub_quals)
10636 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10637 if (mod_type_die)
10638 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10639 mod_type_die = d;
10642 else if (code == POINTER_TYPE)
10644 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10645 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10646 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10647 item_type = TREE_TYPE (type);
10648 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10649 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10650 TYPE_ADDR_SPACE (item_type));
10652 else if (code == REFERENCE_TYPE)
10654 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10655 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10656 type);
10657 else
10658 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10659 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10660 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10661 item_type = TREE_TYPE (type);
10662 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10663 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10664 TYPE_ADDR_SPACE (item_type));
10666 else if (code == INTEGER_TYPE
10667 && TREE_TYPE (type) != NULL_TREE
10668 && subrange_type_for_debug_p (type, &low, &high))
10670 mod_type_die = subrange_type_die (type, low, high, context_die);
10671 item_type = TREE_TYPE (type);
10673 else if (is_base_type (type))
10674 mod_type_die = base_type_die (type);
10675 else
10677 gen_type_die (type, context_die);
10679 /* We have to get the type_main_variant here (and pass that to the
10680 `lookup_type_die' routine) because the ..._TYPE node we have
10681 might simply be a *copy* of some original type node (where the
10682 copy was created to help us keep track of typedef names) and
10683 that copy might have a different TYPE_UID from the original
10684 ..._TYPE node. */
10685 if (TREE_CODE (type) != VECTOR_TYPE)
10686 return lookup_type_die (type_main_variant (type));
10687 else
10688 /* Vectors have the debugging information in the type,
10689 not the main variant. */
10690 return lookup_type_die (type);
10693 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10694 don't output a DW_TAG_typedef, since there isn't one in the
10695 user's program; just attach a DW_AT_name to the type.
10696 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10697 if the base type already has the same name. */
10698 if (name
10699 && ((TREE_CODE (name) != TYPE_DECL
10700 && (qualified_type == TYPE_MAIN_VARIANT (type)
10701 || (cv_quals == TYPE_UNQUALIFIED)))
10702 || (TREE_CODE (name) == TYPE_DECL
10703 && TREE_TYPE (name) == qualified_type
10704 && DECL_NAME (name))))
10706 if (TREE_CODE (name) == TYPE_DECL)
10707 /* Could just call add_name_and_src_coords_attributes here,
10708 but since this is a builtin type it doesn't have any
10709 useful source coordinates anyway. */
10710 name = DECL_NAME (name);
10711 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10713 /* This probably indicates a bug. */
10714 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10716 name = TYPE_IDENTIFIER (type);
10717 add_name_attribute (mod_type_die,
10718 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10721 if (qualified_type)
10722 equate_type_number_to_die (qualified_type, mod_type_die);
10724 if (item_type)
10725 /* We must do this after the equate_type_number_to_die call, in case
10726 this is a recursive type. This ensures that the modified_type_die
10727 recursion will terminate even if the type is recursive. Recursive
10728 types are possible in Ada. */
10729 sub_die = modified_type_die (item_type,
10730 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10731 context_die);
10733 if (sub_die != NULL)
10734 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10736 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10737 if (TYPE_ARTIFICIAL (type))
10738 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10740 return mod_type_die;
10743 /* Generate DIEs for the generic parameters of T.
10744 T must be either a generic type or a generic function.
10745 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10747 static void
10748 gen_generic_params_dies (tree t)
10750 tree parms, args;
10751 int parms_num, i;
10752 dw_die_ref die = NULL;
10753 int non_default;
10755 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10756 return;
10758 if (TYPE_P (t))
10759 die = lookup_type_die (t);
10760 else if (DECL_P (t))
10761 die = lookup_decl_die (t);
10763 gcc_assert (die);
10765 parms = lang_hooks.get_innermost_generic_parms (t);
10766 if (!parms)
10767 /* T has no generic parameter. It means T is neither a generic type
10768 or function. End of story. */
10769 return;
10771 parms_num = TREE_VEC_LENGTH (parms);
10772 args = lang_hooks.get_innermost_generic_args (t);
10773 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10774 non_default = int_cst_value (TREE_CHAIN (args));
10775 else
10776 non_default = TREE_VEC_LENGTH (args);
10777 for (i = 0; i < parms_num; i++)
10779 tree parm, arg, arg_pack_elems;
10780 dw_die_ref parm_die;
10782 parm = TREE_VEC_ELT (parms, i);
10783 arg = TREE_VEC_ELT (args, i);
10784 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10785 gcc_assert (parm && TREE_VALUE (parm) && arg);
10787 if (parm && TREE_VALUE (parm) && arg)
10789 /* If PARM represents a template parameter pack,
10790 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10791 by DW_TAG_template_*_parameter DIEs for the argument
10792 pack elements of ARG. Note that ARG would then be
10793 an argument pack. */
10794 if (arg_pack_elems)
10795 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10796 arg_pack_elems,
10797 die);
10798 else
10799 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10800 true /* emit name */, die);
10801 if (i >= non_default)
10802 add_AT_flag (parm_die, DW_AT_default_value, 1);
10807 /* Create and return a DIE for PARM which should be
10808 the representation of a generic type parameter.
10809 For instance, in the C++ front end, PARM would be a template parameter.
10810 ARG is the argument to PARM.
10811 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10812 name of the PARM.
10813 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10814 as a child node. */
10816 static dw_die_ref
10817 generic_parameter_die (tree parm, tree arg,
10818 bool emit_name_p,
10819 dw_die_ref parent_die)
10821 dw_die_ref tmpl_die = NULL;
10822 const char *name = NULL;
10824 if (!parm || !DECL_NAME (parm) || !arg)
10825 return NULL;
10827 /* We support non-type generic parameters and arguments,
10828 type generic parameters and arguments, as well as
10829 generic generic parameters (a.k.a. template template parameters in C++)
10830 and arguments. */
10831 if (TREE_CODE (parm) == PARM_DECL)
10832 /* PARM is a nontype generic parameter */
10833 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10834 else if (TREE_CODE (parm) == TYPE_DECL)
10835 /* PARM is a type generic parameter. */
10836 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10837 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10838 /* PARM is a generic generic parameter.
10839 Its DIE is a GNU extension. It shall have a
10840 DW_AT_name attribute to represent the name of the template template
10841 parameter, and a DW_AT_GNU_template_name attribute to represent the
10842 name of the template template argument. */
10843 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10844 parent_die, parm);
10845 else
10846 gcc_unreachable ();
10848 if (tmpl_die)
10850 tree tmpl_type;
10852 /* If PARM is a generic parameter pack, it means we are
10853 emitting debug info for a template argument pack element.
10854 In other terms, ARG is a template argument pack element.
10855 In that case, we don't emit any DW_AT_name attribute for
10856 the die. */
10857 if (emit_name_p)
10859 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10860 gcc_assert (name);
10861 add_AT_string (tmpl_die, DW_AT_name, name);
10864 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10866 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10867 TMPL_DIE should have a child DW_AT_type attribute that is set
10868 to the type of the argument to PARM, which is ARG.
10869 If PARM is a type generic parameter, TMPL_DIE should have a
10870 child DW_AT_type that is set to ARG. */
10871 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10872 add_type_attribute (tmpl_die, tmpl_type,
10873 (TREE_THIS_VOLATILE (tmpl_type)
10874 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10875 parent_die);
10877 else
10879 /* So TMPL_DIE is a DIE representing a
10880 a generic generic template parameter, a.k.a template template
10881 parameter in C++ and arg is a template. */
10883 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10884 to the name of the argument. */
10885 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10886 if (name)
10887 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10890 if (TREE_CODE (parm) == PARM_DECL)
10891 /* So PARM is a non-type generic parameter.
10892 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10893 attribute of TMPL_DIE which value represents the value
10894 of ARG.
10895 We must be careful here:
10896 The value of ARG might reference some function decls.
10897 We might currently be emitting debug info for a generic
10898 type and types are emitted before function decls, we don't
10899 know if the function decls referenced by ARG will actually be
10900 emitted after cgraph computations.
10901 So must defer the generation of the DW_AT_const_value to
10902 after cgraph is ready. */
10903 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10906 return tmpl_die;
10909 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10910 PARM_PACK must be a template parameter pack. The returned DIE
10911 will be child DIE of PARENT_DIE. */
10913 static dw_die_ref
10914 template_parameter_pack_die (tree parm_pack,
10915 tree parm_pack_args,
10916 dw_die_ref parent_die)
10918 dw_die_ref die;
10919 int j;
10921 gcc_assert (parent_die && parm_pack);
10923 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10924 add_name_and_src_coords_attributes (die, parm_pack);
10925 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10926 generic_parameter_die (parm_pack,
10927 TREE_VEC_ELT (parm_pack_args, j),
10928 false /* Don't emit DW_AT_name */,
10929 die);
10930 return die;
10933 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10934 an enumerated type. */
10936 static inline int
10937 type_is_enum (const_tree type)
10939 return TREE_CODE (type) == ENUMERAL_TYPE;
10942 /* Return the DBX register number described by a given RTL node. */
10944 static unsigned int
10945 dbx_reg_number (const_rtx rtl)
10947 unsigned regno = REGNO (rtl);
10949 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10951 #ifdef LEAF_REG_REMAP
10952 if (crtl->uses_only_leaf_regs)
10954 int leaf_reg = LEAF_REG_REMAP (regno);
10955 if (leaf_reg != -1)
10956 regno = (unsigned) leaf_reg;
10958 #endif
10960 regno = DBX_REGISTER_NUMBER (regno);
10961 gcc_assert (regno != INVALID_REGNUM);
10962 return regno;
10965 /* Optionally add a DW_OP_piece term to a location description expression.
10966 DW_OP_piece is only added if the location description expression already
10967 doesn't end with DW_OP_piece. */
10969 static void
10970 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10972 dw_loc_descr_ref loc;
10974 if (*list_head != NULL)
10976 /* Find the end of the chain. */
10977 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10980 if (loc->dw_loc_opc != DW_OP_piece)
10981 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10985 /* Return a location descriptor that designates a machine register or
10986 zero if there is none. */
10988 static dw_loc_descr_ref
10989 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10991 rtx regs;
10993 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10994 return 0;
10996 /* We only use "frame base" when we're sure we're talking about the
10997 post-prologue local stack frame. We do this by *not* running
10998 register elimination until this point, and recognizing the special
10999 argument pointer and soft frame pointer rtx's.
11000 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11001 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11002 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11004 dw_loc_descr_ref result = NULL;
11006 if (dwarf_version >= 4 || !dwarf_strict)
11008 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11009 initialized);
11010 if (result)
11011 add_loc_descr (&result,
11012 new_loc_descr (DW_OP_stack_value, 0, 0));
11014 return result;
11017 regs = targetm.dwarf_register_span (rtl);
11019 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11020 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11021 else
11023 unsigned int dbx_regnum = dbx_reg_number (rtl);
11024 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11025 return 0;
11026 return one_reg_loc_descriptor (dbx_regnum, initialized);
11030 /* Return a location descriptor that designates a machine register for
11031 a given hard register number. */
11033 static dw_loc_descr_ref
11034 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11036 dw_loc_descr_ref reg_loc_descr;
11038 if (regno <= 31)
11039 reg_loc_descr
11040 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11041 else
11042 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11044 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11045 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11047 return reg_loc_descr;
11050 /* Given an RTL of a register, return a location descriptor that
11051 designates a value that spans more than one register. */
11053 static dw_loc_descr_ref
11054 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11055 enum var_init_status initialized)
11057 int size, i;
11058 dw_loc_descr_ref loc_result = NULL;
11060 /* Simple, contiguous registers. */
11061 if (regs == NULL_RTX)
11063 unsigned reg = REGNO (rtl);
11064 int nregs;
11066 #ifdef LEAF_REG_REMAP
11067 if (crtl->uses_only_leaf_regs)
11069 int leaf_reg = LEAF_REG_REMAP (reg);
11070 if (leaf_reg != -1)
11071 reg = (unsigned) leaf_reg;
11073 #endif
11075 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11076 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11078 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11080 loc_result = NULL;
11081 while (nregs--)
11083 dw_loc_descr_ref t;
11085 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11086 VAR_INIT_STATUS_INITIALIZED);
11087 add_loc_descr (&loc_result, t);
11088 add_loc_descr_op_piece (&loc_result, size);
11089 ++reg;
11091 return loc_result;
11094 /* Now onto stupid register sets in non contiguous locations. */
11096 gcc_assert (GET_CODE (regs) == PARALLEL);
11098 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11099 loc_result = NULL;
11101 for (i = 0; i < XVECLEN (regs, 0); ++i)
11103 dw_loc_descr_ref t;
11105 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11106 VAR_INIT_STATUS_INITIALIZED);
11107 add_loc_descr (&loc_result, t);
11108 add_loc_descr_op_piece (&loc_result, size);
11111 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11112 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11113 return loc_result;
11116 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11118 /* Return a location descriptor that designates a constant i,
11119 as a compound operation from constant (i >> shift), constant shift
11120 and DW_OP_shl. */
11122 static dw_loc_descr_ref
11123 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11125 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11126 add_loc_descr (&ret, int_loc_descriptor (shift));
11127 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11128 return ret;
11131 /* Return a location descriptor that designates a constant. */
11133 static dw_loc_descr_ref
11134 int_loc_descriptor (HOST_WIDE_INT i)
11136 enum dwarf_location_atom op;
11138 /* Pick the smallest representation of a constant, rather than just
11139 defaulting to the LEB encoding. */
11140 if (i >= 0)
11142 int clz = clz_hwi (i);
11143 int ctz = ctz_hwi (i);
11144 if (i <= 31)
11145 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11146 else if (i <= 0xff)
11147 op = DW_OP_const1u;
11148 else if (i <= 0xffff)
11149 op = DW_OP_const2u;
11150 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11151 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11152 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11153 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11154 while DW_OP_const4u is 5 bytes. */
11155 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11156 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11157 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11158 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11159 while DW_OP_const4u is 5 bytes. */
11160 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11161 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11162 op = DW_OP_const4u;
11163 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11164 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11165 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11166 while DW_OP_constu of constant >= 0x100000000 takes at least
11167 6 bytes. */
11168 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11169 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11170 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11171 >= HOST_BITS_PER_WIDE_INT)
11172 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11173 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11174 while DW_OP_constu takes in this case at least 6 bytes. */
11175 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11176 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11177 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11178 && size_of_uleb128 (i) > 6)
11179 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11180 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11181 else
11182 op = DW_OP_constu;
11184 else
11186 if (i >= -0x80)
11187 op = DW_OP_const1s;
11188 else if (i >= -0x8000)
11189 op = DW_OP_const2s;
11190 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11192 if (size_of_int_loc_descriptor (i) < 5)
11194 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11195 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11196 return ret;
11198 op = DW_OP_const4s;
11200 else
11202 if (size_of_int_loc_descriptor (i)
11203 < (unsigned long) 1 + size_of_sleb128 (i))
11205 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11206 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11207 return ret;
11209 op = DW_OP_consts;
11213 return new_loc_descr (op, i, 0);
11216 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11217 without actually allocating it. */
11219 static unsigned long
11220 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11222 return size_of_int_loc_descriptor (i >> shift)
11223 + size_of_int_loc_descriptor (shift)
11224 + 1;
11227 /* Return size_of_locs (int_loc_descriptor (i)) without
11228 actually allocating it. */
11230 static unsigned long
11231 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11233 unsigned long s;
11235 if (i >= 0)
11237 int clz, ctz;
11238 if (i <= 31)
11239 return 1;
11240 else if (i <= 0xff)
11241 return 2;
11242 else if (i <= 0xffff)
11243 return 3;
11244 clz = clz_hwi (i);
11245 ctz = ctz_hwi (i);
11246 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11247 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11248 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11249 - clz - 5);
11250 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11251 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11252 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11253 - clz - 8);
11254 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11255 return 5;
11256 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11257 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11258 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11259 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11260 - clz - 8);
11261 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11262 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11263 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11264 - clz - 16);
11265 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11266 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11267 && s > 6)
11268 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11269 - clz - 32);
11270 else
11271 return 1 + s;
11273 else
11275 if (i >= -0x80)
11276 return 2;
11277 else if (i >= -0x8000)
11278 return 3;
11279 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11281 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11283 s = size_of_int_loc_descriptor (-i) + 1;
11284 if (s < 5)
11285 return s;
11287 return 5;
11289 else
11291 unsigned long r = 1 + size_of_sleb128 (i);
11292 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11294 s = size_of_int_loc_descriptor (-i) + 1;
11295 if (s < r)
11296 return s;
11298 return r;
11303 /* Return loc description representing "address" of integer value.
11304 This can appear only as toplevel expression. */
11306 static dw_loc_descr_ref
11307 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11309 int litsize;
11310 dw_loc_descr_ref loc_result = NULL;
11312 if (!(dwarf_version >= 4 || !dwarf_strict))
11313 return NULL;
11315 litsize = size_of_int_loc_descriptor (i);
11316 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11317 is more compact. For DW_OP_stack_value we need:
11318 litsize + 1 (DW_OP_stack_value)
11319 and for DW_OP_implicit_value:
11320 1 (DW_OP_implicit_value) + 1 (length) + size. */
11321 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11323 loc_result = int_loc_descriptor (i);
11324 add_loc_descr (&loc_result,
11325 new_loc_descr (DW_OP_stack_value, 0, 0));
11326 return loc_result;
11329 loc_result = new_loc_descr (DW_OP_implicit_value,
11330 size, 0);
11331 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11332 loc_result->dw_loc_oprnd2.v.val_int = i;
11333 return loc_result;
11336 /* Return a location descriptor that designates a base+offset location. */
11338 static dw_loc_descr_ref
11339 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11340 enum var_init_status initialized)
11342 unsigned int regno;
11343 dw_loc_descr_ref result;
11344 dw_fde_ref fde = cfun->fde;
11346 /* We only use "frame base" when we're sure we're talking about the
11347 post-prologue local stack frame. We do this by *not* running
11348 register elimination until this point, and recognizing the special
11349 argument pointer and soft frame pointer rtx's. */
11350 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11352 rtx elim = (ira_use_lra_p
11353 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11354 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11356 if (elim != reg)
11358 if (GET_CODE (elim) == PLUS)
11360 offset += INTVAL (XEXP (elim, 1));
11361 elim = XEXP (elim, 0);
11363 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11364 && (elim == hard_frame_pointer_rtx
11365 || elim == stack_pointer_rtx))
11366 || elim == (frame_pointer_needed
11367 ? hard_frame_pointer_rtx
11368 : stack_pointer_rtx));
11370 /* If drap register is used to align stack, use frame
11371 pointer + offset to access stack variables. If stack
11372 is aligned without drap, use stack pointer + offset to
11373 access stack variables. */
11374 if (crtl->stack_realign_tried
11375 && reg == frame_pointer_rtx)
11377 int base_reg
11378 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11379 ? HARD_FRAME_POINTER_REGNUM
11380 : REGNO (elim));
11381 return new_reg_loc_descr (base_reg, offset);
11384 gcc_assert (frame_pointer_fb_offset_valid);
11385 offset += frame_pointer_fb_offset;
11386 return new_loc_descr (DW_OP_fbreg, offset, 0);
11390 regno = REGNO (reg);
11391 #ifdef LEAF_REG_REMAP
11392 if (crtl->uses_only_leaf_regs)
11394 int leaf_reg = LEAF_REG_REMAP (regno);
11395 if (leaf_reg != -1)
11396 regno = (unsigned) leaf_reg;
11398 #endif
11399 regno = DWARF_FRAME_REGNUM (regno);
11401 if (!optimize && fde
11402 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11404 /* Use cfa+offset to represent the location of arguments passed
11405 on the stack when drap is used to align stack.
11406 Only do this when not optimizing, for optimized code var-tracking
11407 is supposed to track where the arguments live and the register
11408 used as vdrap or drap in some spot might be used for something
11409 else in other part of the routine. */
11410 return new_loc_descr (DW_OP_fbreg, offset, 0);
11413 if (regno <= 31)
11414 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11415 offset, 0);
11416 else
11417 result = new_loc_descr (DW_OP_bregx, regno, offset);
11419 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11420 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11422 return result;
11425 /* Return true if this RTL expression describes a base+offset calculation. */
11427 static inline int
11428 is_based_loc (const_rtx rtl)
11430 return (GET_CODE (rtl) == PLUS
11431 && ((REG_P (XEXP (rtl, 0))
11432 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11433 && CONST_INT_P (XEXP (rtl, 1)))));
11436 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11437 failed. */
11439 static dw_loc_descr_ref
11440 tls_mem_loc_descriptor (rtx mem)
11442 tree base;
11443 dw_loc_descr_ref loc_result;
11445 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11446 return NULL;
11448 base = get_base_address (MEM_EXPR (mem));
11449 if (base == NULL
11450 || TREE_CODE (base) != VAR_DECL
11451 || !DECL_THREAD_LOCAL_P (base))
11452 return NULL;
11454 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11455 if (loc_result == NULL)
11456 return NULL;
11458 if (MEM_OFFSET (mem))
11459 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11461 return loc_result;
11464 /* Output debug info about reason why we failed to expand expression as dwarf
11465 expression. */
11467 static void
11468 expansion_failed (tree expr, rtx rtl, char const *reason)
11470 if (dump_file && (dump_flags & TDF_DETAILS))
11472 fprintf (dump_file, "Failed to expand as dwarf: ");
11473 if (expr)
11474 print_generic_expr (dump_file, expr, dump_flags);
11475 if (rtl)
11477 fprintf (dump_file, "\n");
11478 print_rtl (dump_file, rtl);
11480 fprintf (dump_file, "\nReason: %s\n", reason);
11484 /* Helper function for const_ok_for_output. */
11486 static bool
11487 const_ok_for_output_1 (rtx rtl)
11489 if (GET_CODE (rtl) == UNSPEC)
11491 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11492 we can't express it in the debug info. */
11493 #ifdef ENABLE_CHECKING
11494 /* Don't complain about TLS UNSPECs, those are just too hard to
11495 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11496 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11497 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11498 if (XVECLEN (rtl, 0) == 0
11499 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11500 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11501 inform (current_function_decl
11502 ? DECL_SOURCE_LOCATION (current_function_decl)
11503 : UNKNOWN_LOCATION,
11504 #if NUM_UNSPEC_VALUES > 0
11505 "non-delegitimized UNSPEC %s (%d) found in variable location",
11506 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11507 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11508 XINT (rtl, 1));
11509 #else
11510 "non-delegitimized UNSPEC %d found in variable location",
11511 XINT (rtl, 1));
11512 #endif
11513 #endif
11514 expansion_failed (NULL_TREE, rtl,
11515 "UNSPEC hasn't been delegitimized.\n");
11516 return false;
11519 if (targetm.const_not_ok_for_debug_p (rtl))
11521 expansion_failed (NULL_TREE, rtl,
11522 "Expression rejected for debug by the backend.\n");
11523 return false;
11526 /* FIXME: Refer to PR60655. It is possible for simplification
11527 of rtl expressions in var tracking to produce such expressions.
11528 We should really identify / validate expressions
11529 enclosed in CONST that can be handled by assemblers on various
11530 targets and only handle legitimate cases here. */
11531 if (GET_CODE (rtl) != SYMBOL_REF)
11533 if (GET_CODE (rtl) == NOT)
11534 return false;
11535 return true;
11538 if (CONSTANT_POOL_ADDRESS_P (rtl))
11540 bool marked;
11541 get_pool_constant_mark (rtl, &marked);
11542 /* If all references to this pool constant were optimized away,
11543 it was not output and thus we can't represent it. */
11544 if (!marked)
11546 expansion_failed (NULL_TREE, rtl,
11547 "Constant was removed from constant pool.\n");
11548 return false;
11552 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11553 return false;
11555 /* Avoid references to external symbols in debug info, on several targets
11556 the linker might even refuse to link when linking a shared library,
11557 and in many other cases the relocations for .debug_info/.debug_loc are
11558 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11559 to be defined within the same shared library or executable are fine. */
11560 if (SYMBOL_REF_EXTERNAL_P (rtl))
11562 tree decl = SYMBOL_REF_DECL (rtl);
11564 if (decl == NULL || !targetm.binds_local_p (decl))
11566 expansion_failed (NULL_TREE, rtl,
11567 "Symbol not defined in current TU.\n");
11568 return false;
11572 return true;
11575 /* Return true if constant RTL can be emitted in DW_OP_addr or
11576 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11577 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11579 static bool
11580 const_ok_for_output (rtx rtl)
11582 if (GET_CODE (rtl) == SYMBOL_REF)
11583 return const_ok_for_output_1 (rtl);
11585 if (GET_CODE (rtl) == CONST)
11587 subrtx_var_iterator::array_type array;
11588 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11589 if (!const_ok_for_output_1 (*iter))
11590 return false;
11591 return true;
11594 return true;
11597 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11598 if possible, NULL otherwise. */
11600 static dw_die_ref
11601 base_type_for_mode (machine_mode mode, bool unsignedp)
11603 dw_die_ref type_die;
11604 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11606 if (type == NULL)
11607 return NULL;
11608 switch (TREE_CODE (type))
11610 case INTEGER_TYPE:
11611 case REAL_TYPE:
11612 break;
11613 default:
11614 return NULL;
11616 type_die = lookup_type_die (type);
11617 if (!type_die)
11618 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11619 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11620 return NULL;
11621 return type_die;
11624 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11625 type matching MODE, or, if MODE is narrower than or as wide as
11626 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11627 possible. */
11629 static dw_loc_descr_ref
11630 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11632 machine_mode outer_mode = mode;
11633 dw_die_ref type_die;
11634 dw_loc_descr_ref cvt;
11636 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11638 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11639 return op;
11641 type_die = base_type_for_mode (outer_mode, 1);
11642 if (type_die == NULL)
11643 return NULL;
11644 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11645 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11646 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11647 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11648 add_loc_descr (&op, cvt);
11649 return op;
11652 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11654 static dw_loc_descr_ref
11655 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11656 dw_loc_descr_ref op1)
11658 dw_loc_descr_ref ret = op0;
11659 add_loc_descr (&ret, op1);
11660 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11661 if (STORE_FLAG_VALUE != 1)
11663 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11664 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11666 return ret;
11669 /* Return location descriptor for signed comparison OP RTL. */
11671 static dw_loc_descr_ref
11672 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11673 machine_mode mem_mode)
11675 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11676 dw_loc_descr_ref op0, op1;
11677 int shift;
11679 if (op_mode == VOIDmode)
11680 op_mode = GET_MODE (XEXP (rtl, 1));
11681 if (op_mode == VOIDmode)
11682 return NULL;
11684 if (dwarf_strict
11685 && (GET_MODE_CLASS (op_mode) != MODE_INT
11686 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11687 return NULL;
11689 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11690 VAR_INIT_STATUS_INITIALIZED);
11691 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11692 VAR_INIT_STATUS_INITIALIZED);
11694 if (op0 == NULL || op1 == NULL)
11695 return NULL;
11697 if (GET_MODE_CLASS (op_mode) != MODE_INT
11698 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11699 return compare_loc_descriptor (op, op0, op1);
11701 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11703 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11704 dw_loc_descr_ref cvt;
11706 if (type_die == NULL)
11707 return NULL;
11708 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11709 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11710 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11711 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11712 add_loc_descr (&op0, cvt);
11713 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11714 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11715 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11716 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11717 add_loc_descr (&op1, cvt);
11718 return compare_loc_descriptor (op, op0, op1);
11721 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11722 /* For eq/ne, if the operands are known to be zero-extended,
11723 there is no need to do the fancy shifting up. */
11724 if (op == DW_OP_eq || op == DW_OP_ne)
11726 dw_loc_descr_ref last0, last1;
11727 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11729 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11731 /* deref_size zero extends, and for constants we can check
11732 whether they are zero extended or not. */
11733 if (((last0->dw_loc_opc == DW_OP_deref_size
11734 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11735 || (CONST_INT_P (XEXP (rtl, 0))
11736 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11737 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11738 && ((last1->dw_loc_opc == DW_OP_deref_size
11739 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11740 || (CONST_INT_P (XEXP (rtl, 1))
11741 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11742 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11743 return compare_loc_descriptor (op, op0, op1);
11745 /* EQ/NE comparison against constant in narrower type than
11746 DWARF2_ADDR_SIZE can be performed either as
11747 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11748 DW_OP_{eq,ne}
11750 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11751 DW_OP_{eq,ne}. Pick whatever is shorter. */
11752 if (CONST_INT_P (XEXP (rtl, 1))
11753 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11754 && (size_of_int_loc_descriptor (shift) + 1
11755 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11756 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11757 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11758 & GET_MODE_MASK (op_mode))))
11760 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11761 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11762 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11763 & GET_MODE_MASK (op_mode));
11764 return compare_loc_descriptor (op, op0, op1);
11767 add_loc_descr (&op0, int_loc_descriptor (shift));
11768 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11769 if (CONST_INT_P (XEXP (rtl, 1)))
11770 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11771 else
11773 add_loc_descr (&op1, int_loc_descriptor (shift));
11774 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11776 return compare_loc_descriptor (op, op0, op1);
11779 /* Return location descriptor for unsigned comparison OP RTL. */
11781 static dw_loc_descr_ref
11782 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11783 machine_mode mem_mode)
11785 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11786 dw_loc_descr_ref op0, op1;
11788 if (op_mode == VOIDmode)
11789 op_mode = GET_MODE (XEXP (rtl, 1));
11790 if (op_mode == VOIDmode)
11791 return NULL;
11792 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11793 return NULL;
11795 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11796 return NULL;
11798 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11799 VAR_INIT_STATUS_INITIALIZED);
11800 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11801 VAR_INIT_STATUS_INITIALIZED);
11803 if (op0 == NULL || op1 == NULL)
11804 return NULL;
11806 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11808 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11809 dw_loc_descr_ref last0, last1;
11810 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11812 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11814 if (CONST_INT_P (XEXP (rtl, 0)))
11815 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11816 /* deref_size zero extends, so no need to mask it again. */
11817 else if (last0->dw_loc_opc != DW_OP_deref_size
11818 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11820 add_loc_descr (&op0, int_loc_descriptor (mask));
11821 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11823 if (CONST_INT_P (XEXP (rtl, 1)))
11824 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11825 /* deref_size zero extends, so no need to mask it again. */
11826 else if (last1->dw_loc_opc != DW_OP_deref_size
11827 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11829 add_loc_descr (&op1, int_loc_descriptor (mask));
11830 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11833 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11835 HOST_WIDE_INT bias = 1;
11836 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11837 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11838 if (CONST_INT_P (XEXP (rtl, 1)))
11839 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11840 + INTVAL (XEXP (rtl, 1)));
11841 else
11842 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11843 bias, 0));
11845 return compare_loc_descriptor (op, op0, op1);
11848 /* Return location descriptor for {U,S}{MIN,MAX}. */
11850 static dw_loc_descr_ref
11851 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11852 machine_mode mem_mode)
11854 enum dwarf_location_atom op;
11855 dw_loc_descr_ref op0, op1, ret;
11856 dw_loc_descr_ref bra_node, drop_node;
11858 if (dwarf_strict
11859 && (GET_MODE_CLASS (mode) != MODE_INT
11860 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11861 return NULL;
11863 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11864 VAR_INIT_STATUS_INITIALIZED);
11865 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11866 VAR_INIT_STATUS_INITIALIZED);
11868 if (op0 == NULL || op1 == NULL)
11869 return NULL;
11871 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11872 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11873 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11874 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11876 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11878 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11879 add_loc_descr (&op0, int_loc_descriptor (mask));
11880 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11881 add_loc_descr (&op1, int_loc_descriptor (mask));
11882 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11884 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11886 HOST_WIDE_INT bias = 1;
11887 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11888 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11889 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11892 else if (GET_MODE_CLASS (mode) == MODE_INT
11893 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11895 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11896 add_loc_descr (&op0, int_loc_descriptor (shift));
11897 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11898 add_loc_descr (&op1, int_loc_descriptor (shift));
11899 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11901 else if (GET_MODE_CLASS (mode) == MODE_INT
11902 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11904 dw_die_ref type_die = base_type_for_mode (mode, 0);
11905 dw_loc_descr_ref cvt;
11906 if (type_die == NULL)
11907 return NULL;
11908 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11909 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11910 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11911 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11912 add_loc_descr (&op0, cvt);
11913 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11914 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11915 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11916 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11917 add_loc_descr (&op1, cvt);
11920 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11921 op = DW_OP_lt;
11922 else
11923 op = DW_OP_gt;
11924 ret = op0;
11925 add_loc_descr (&ret, op1);
11926 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11927 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11928 add_loc_descr (&ret, bra_node);
11929 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11930 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11931 add_loc_descr (&ret, drop_node);
11932 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11933 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11934 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11935 && GET_MODE_CLASS (mode) == MODE_INT
11936 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11937 ret = convert_descriptor_to_mode (mode, ret);
11938 return ret;
11941 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11942 but after converting arguments to type_die, afterwards
11943 convert back to unsigned. */
11945 static dw_loc_descr_ref
11946 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11947 machine_mode mode, machine_mode mem_mode)
11949 dw_loc_descr_ref cvt, op0, op1;
11951 if (type_die == NULL)
11952 return NULL;
11953 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11954 VAR_INIT_STATUS_INITIALIZED);
11955 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11956 VAR_INIT_STATUS_INITIALIZED);
11957 if (op0 == NULL || op1 == NULL)
11958 return NULL;
11959 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11960 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11961 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11962 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11963 add_loc_descr (&op0, cvt);
11964 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11965 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11966 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11967 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11968 add_loc_descr (&op1, cvt);
11969 add_loc_descr (&op0, op1);
11970 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11971 return convert_descriptor_to_mode (mode, op0);
11974 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11975 const0 is DW_OP_lit0 or corresponding typed constant,
11976 const1 is DW_OP_lit1 or corresponding typed constant
11977 and constMSB is constant with just the MSB bit set
11978 for the mode):
11979 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11980 L1: const0 DW_OP_swap
11981 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11982 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11983 L3: DW_OP_drop
11984 L4: DW_OP_nop
11986 CTZ is similar:
11987 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11988 L1: const0 DW_OP_swap
11989 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11990 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11991 L3: DW_OP_drop
11992 L4: DW_OP_nop
11994 FFS is similar:
11995 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11996 L1: const1 DW_OP_swap
11997 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11998 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11999 L3: DW_OP_drop
12000 L4: DW_OP_nop */
12002 static dw_loc_descr_ref
12003 clz_loc_descriptor (rtx rtl, machine_mode mode,
12004 machine_mode mem_mode)
12006 dw_loc_descr_ref op0, ret, tmp;
12007 HOST_WIDE_INT valv;
12008 dw_loc_descr_ref l1jump, l1label;
12009 dw_loc_descr_ref l2jump, l2label;
12010 dw_loc_descr_ref l3jump, l3label;
12011 dw_loc_descr_ref l4jump, l4label;
12012 rtx msb;
12014 if (GET_MODE_CLASS (mode) != MODE_INT
12015 || GET_MODE (XEXP (rtl, 0)) != mode)
12016 return NULL;
12018 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12019 VAR_INIT_STATUS_INITIALIZED);
12020 if (op0 == NULL)
12021 return NULL;
12022 ret = op0;
12023 if (GET_CODE (rtl) == CLZ)
12025 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12026 valv = GET_MODE_BITSIZE (mode);
12028 else if (GET_CODE (rtl) == FFS)
12029 valv = 0;
12030 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12031 valv = GET_MODE_BITSIZE (mode);
12032 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12033 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12034 add_loc_descr (&ret, l1jump);
12035 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12036 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12037 VAR_INIT_STATUS_INITIALIZED);
12038 if (tmp == NULL)
12039 return NULL;
12040 add_loc_descr (&ret, tmp);
12041 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12042 add_loc_descr (&ret, l4jump);
12043 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12044 ? const1_rtx : const0_rtx,
12045 mode, mem_mode,
12046 VAR_INIT_STATUS_INITIALIZED);
12047 if (l1label == NULL)
12048 return NULL;
12049 add_loc_descr (&ret, l1label);
12050 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12051 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12052 add_loc_descr (&ret, l2label);
12053 if (GET_CODE (rtl) != CLZ)
12054 msb = const1_rtx;
12055 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12056 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12057 << (GET_MODE_BITSIZE (mode) - 1));
12058 else
12059 msb = immed_wide_int_const
12060 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12061 GET_MODE_PRECISION (mode)), mode);
12062 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12063 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12064 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12065 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12066 else
12067 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12068 VAR_INIT_STATUS_INITIALIZED);
12069 if (tmp == NULL)
12070 return NULL;
12071 add_loc_descr (&ret, tmp);
12072 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12073 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12074 add_loc_descr (&ret, l3jump);
12075 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12076 VAR_INIT_STATUS_INITIALIZED);
12077 if (tmp == NULL)
12078 return NULL;
12079 add_loc_descr (&ret, tmp);
12080 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12081 ? DW_OP_shl : DW_OP_shr, 0, 0));
12082 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12083 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12084 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12085 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12086 add_loc_descr (&ret, l2jump);
12087 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12088 add_loc_descr (&ret, l3label);
12089 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12090 add_loc_descr (&ret, l4label);
12091 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12092 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12093 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12094 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12095 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12096 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12097 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12098 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12099 return ret;
12102 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12103 const1 is DW_OP_lit1 or corresponding typed constant):
12104 const0 DW_OP_swap
12105 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12106 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12107 L2: DW_OP_drop
12109 PARITY is similar:
12110 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12111 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12112 L2: DW_OP_drop */
12114 static dw_loc_descr_ref
12115 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12116 machine_mode mem_mode)
12118 dw_loc_descr_ref op0, ret, tmp;
12119 dw_loc_descr_ref l1jump, l1label;
12120 dw_loc_descr_ref l2jump, l2label;
12122 if (GET_MODE_CLASS (mode) != MODE_INT
12123 || GET_MODE (XEXP (rtl, 0)) != mode)
12124 return NULL;
12126 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12127 VAR_INIT_STATUS_INITIALIZED);
12128 if (op0 == NULL)
12129 return NULL;
12130 ret = op0;
12131 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12132 VAR_INIT_STATUS_INITIALIZED);
12133 if (tmp == NULL)
12134 return NULL;
12135 add_loc_descr (&ret, tmp);
12136 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12137 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12138 add_loc_descr (&ret, l1label);
12139 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12140 add_loc_descr (&ret, l2jump);
12141 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12142 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12143 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12144 VAR_INIT_STATUS_INITIALIZED);
12145 if (tmp == NULL)
12146 return NULL;
12147 add_loc_descr (&ret, tmp);
12148 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12149 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12150 ? DW_OP_plus : DW_OP_xor, 0, 0));
12151 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12152 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12153 VAR_INIT_STATUS_INITIALIZED);
12154 add_loc_descr (&ret, tmp);
12155 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12156 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12157 add_loc_descr (&ret, l1jump);
12158 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12159 add_loc_descr (&ret, l2label);
12160 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12161 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12162 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12163 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12164 return ret;
12167 /* BSWAP (constS is initial shift count, either 56 or 24):
12168 constS const0
12169 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12170 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12171 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12172 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12173 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12175 static dw_loc_descr_ref
12176 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12177 machine_mode mem_mode)
12179 dw_loc_descr_ref op0, ret, tmp;
12180 dw_loc_descr_ref l1jump, l1label;
12181 dw_loc_descr_ref l2jump, l2label;
12183 if (GET_MODE_CLASS (mode) != MODE_INT
12184 || BITS_PER_UNIT != 8
12185 || (GET_MODE_BITSIZE (mode) != 32
12186 && GET_MODE_BITSIZE (mode) != 64))
12187 return NULL;
12189 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12190 VAR_INIT_STATUS_INITIALIZED);
12191 if (op0 == NULL)
12192 return NULL;
12194 ret = op0;
12195 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12196 mode, mem_mode,
12197 VAR_INIT_STATUS_INITIALIZED);
12198 if (tmp == NULL)
12199 return NULL;
12200 add_loc_descr (&ret, tmp);
12201 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12202 VAR_INIT_STATUS_INITIALIZED);
12203 if (tmp == NULL)
12204 return NULL;
12205 add_loc_descr (&ret, tmp);
12206 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12207 add_loc_descr (&ret, l1label);
12208 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12209 mode, mem_mode,
12210 VAR_INIT_STATUS_INITIALIZED);
12211 add_loc_descr (&ret, tmp);
12212 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12213 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12214 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12215 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12216 VAR_INIT_STATUS_INITIALIZED);
12217 if (tmp == NULL)
12218 return NULL;
12219 add_loc_descr (&ret, tmp);
12220 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12221 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12222 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12223 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12224 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12225 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12226 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12227 VAR_INIT_STATUS_INITIALIZED);
12228 add_loc_descr (&ret, tmp);
12229 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12230 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12231 add_loc_descr (&ret, l2jump);
12232 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12233 VAR_INIT_STATUS_INITIALIZED);
12234 add_loc_descr (&ret, tmp);
12235 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12236 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12237 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12238 add_loc_descr (&ret, l1jump);
12239 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12240 add_loc_descr (&ret, l2label);
12241 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12242 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12243 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12244 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12245 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12246 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12247 return ret;
12250 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12251 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12252 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12253 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12255 ROTATERT is similar:
12256 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12257 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12258 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12260 static dw_loc_descr_ref
12261 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12262 machine_mode mem_mode)
12264 rtx rtlop1 = XEXP (rtl, 1);
12265 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12266 int i;
12268 if (GET_MODE_CLASS (mode) != MODE_INT)
12269 return NULL;
12271 if (GET_MODE (rtlop1) != VOIDmode
12272 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12273 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12274 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12275 VAR_INIT_STATUS_INITIALIZED);
12276 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12277 VAR_INIT_STATUS_INITIALIZED);
12278 if (op0 == NULL || op1 == NULL)
12279 return NULL;
12280 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12281 for (i = 0; i < 2; i++)
12283 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12284 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12285 mode, mem_mode,
12286 VAR_INIT_STATUS_INITIALIZED);
12287 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12288 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12289 ? DW_OP_const4u
12290 : HOST_BITS_PER_WIDE_INT == 64
12291 ? DW_OP_const8u : DW_OP_constu,
12292 GET_MODE_MASK (mode), 0);
12293 else
12294 mask[i] = NULL;
12295 if (mask[i] == NULL)
12296 return NULL;
12297 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12299 ret = op0;
12300 add_loc_descr (&ret, op1);
12301 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12302 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12303 if (GET_CODE (rtl) == ROTATERT)
12305 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12306 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12307 GET_MODE_BITSIZE (mode), 0));
12309 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12310 if (mask[0] != NULL)
12311 add_loc_descr (&ret, mask[0]);
12312 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12313 if (mask[1] != NULL)
12315 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12316 add_loc_descr (&ret, mask[1]);
12317 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12319 if (GET_CODE (rtl) == ROTATE)
12321 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12322 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12323 GET_MODE_BITSIZE (mode), 0));
12325 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12326 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12327 return ret;
12330 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12331 for DEBUG_PARAMETER_REF RTL. */
12333 static dw_loc_descr_ref
12334 parameter_ref_descriptor (rtx rtl)
12336 dw_loc_descr_ref ret;
12337 dw_die_ref ref;
12339 if (dwarf_strict)
12340 return NULL;
12341 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12342 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12343 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12344 if (ref)
12346 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12347 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12348 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12350 else
12352 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12353 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12355 return ret;
12358 /* The following routine converts the RTL for a variable or parameter
12359 (resident in memory) into an equivalent Dwarf representation of a
12360 mechanism for getting the address of that same variable onto the top of a
12361 hypothetical "address evaluation" stack.
12363 When creating memory location descriptors, we are effectively transforming
12364 the RTL for a memory-resident object into its Dwarf postfix expression
12365 equivalent. This routine recursively descends an RTL tree, turning
12366 it into Dwarf postfix code as it goes.
12368 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12370 MEM_MODE is the mode of the memory reference, needed to handle some
12371 autoincrement addressing modes.
12373 Return 0 if we can't represent the location. */
12375 dw_loc_descr_ref
12376 mem_loc_descriptor (rtx rtl, machine_mode mode,
12377 machine_mode mem_mode,
12378 enum var_init_status initialized)
12380 dw_loc_descr_ref mem_loc_result = NULL;
12381 enum dwarf_location_atom op;
12382 dw_loc_descr_ref op0, op1;
12383 rtx inner = NULL_RTX;
12385 if (mode == VOIDmode)
12386 mode = GET_MODE (rtl);
12388 /* Note that for a dynamically sized array, the location we will generate a
12389 description of here will be the lowest numbered location which is
12390 actually within the array. That's *not* necessarily the same as the
12391 zeroth element of the array. */
12393 rtl = targetm.delegitimize_address (rtl);
12395 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12396 return NULL;
12398 switch (GET_CODE (rtl))
12400 case POST_INC:
12401 case POST_DEC:
12402 case POST_MODIFY:
12403 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12405 case SUBREG:
12406 /* The case of a subreg may arise when we have a local (register)
12407 variable or a formal (register) parameter which doesn't quite fill
12408 up an entire register. For now, just assume that it is
12409 legitimate to make the Dwarf info refer to the whole register which
12410 contains the given subreg. */
12411 if (!subreg_lowpart_p (rtl))
12412 break;
12413 inner = SUBREG_REG (rtl);
12414 case TRUNCATE:
12415 if (inner == NULL_RTX)
12416 inner = XEXP (rtl, 0);
12417 if (GET_MODE_CLASS (mode) == MODE_INT
12418 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12419 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12420 #ifdef POINTERS_EXTEND_UNSIGNED
12421 || (mode == Pmode && mem_mode != VOIDmode)
12422 #endif
12424 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12426 mem_loc_result = mem_loc_descriptor (inner,
12427 GET_MODE (inner),
12428 mem_mode, initialized);
12429 break;
12431 if (dwarf_strict)
12432 break;
12433 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12434 break;
12435 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12436 && (GET_MODE_CLASS (mode) != MODE_INT
12437 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12438 break;
12439 else
12441 dw_die_ref type_die;
12442 dw_loc_descr_ref cvt;
12444 mem_loc_result = mem_loc_descriptor (inner,
12445 GET_MODE (inner),
12446 mem_mode, initialized);
12447 if (mem_loc_result == NULL)
12448 break;
12449 type_die = base_type_for_mode (mode,
12450 GET_MODE_CLASS (mode) == MODE_INT);
12451 if (type_die == NULL)
12453 mem_loc_result = NULL;
12454 break;
12456 if (GET_MODE_SIZE (mode)
12457 != GET_MODE_SIZE (GET_MODE (inner)))
12458 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12459 else
12460 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12461 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12462 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12463 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12464 add_loc_descr (&mem_loc_result, cvt);
12466 break;
12468 case REG:
12469 if (GET_MODE_CLASS (mode) != MODE_INT
12470 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12471 && rtl != arg_pointer_rtx
12472 && rtl != frame_pointer_rtx
12473 #ifdef POINTERS_EXTEND_UNSIGNED
12474 && (mode != Pmode || mem_mode == VOIDmode)
12475 #endif
12478 dw_die_ref type_die;
12479 unsigned int dbx_regnum;
12481 if (dwarf_strict)
12482 break;
12483 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12484 break;
12485 type_die = base_type_for_mode (mode,
12486 GET_MODE_CLASS (mode) == MODE_INT);
12487 if (type_die == NULL)
12488 break;
12490 dbx_regnum = dbx_reg_number (rtl);
12491 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12492 break;
12493 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12494 dbx_regnum, 0);
12495 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12496 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12497 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12498 break;
12500 /* Whenever a register number forms a part of the description of the
12501 method for calculating the (dynamic) address of a memory resident
12502 object, DWARF rules require the register number be referred to as
12503 a "base register". This distinction is not based in any way upon
12504 what category of register the hardware believes the given register
12505 belongs to. This is strictly DWARF terminology we're dealing with
12506 here. Note that in cases where the location of a memory-resident
12507 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12508 OP_CONST (0)) the actual DWARF location descriptor that we generate
12509 may just be OP_BASEREG (basereg). This may look deceptively like
12510 the object in question was allocated to a register (rather than in
12511 memory) so DWARF consumers need to be aware of the subtle
12512 distinction between OP_REG and OP_BASEREG. */
12513 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12514 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12515 else if (stack_realign_drap
12516 && crtl->drap_reg
12517 && crtl->args.internal_arg_pointer == rtl
12518 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12520 /* If RTL is internal_arg_pointer, which has been optimized
12521 out, use DRAP instead. */
12522 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12523 VAR_INIT_STATUS_INITIALIZED);
12525 break;
12527 case SIGN_EXTEND:
12528 case ZERO_EXTEND:
12529 if (GET_MODE_CLASS (mode) != MODE_INT)
12530 break;
12531 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12532 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12533 if (op0 == 0)
12534 break;
12535 else if (GET_CODE (rtl) == ZERO_EXTEND
12536 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12537 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12538 < HOST_BITS_PER_WIDE_INT
12539 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12540 to expand zero extend as two shifts instead of
12541 masking. */
12542 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12544 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12545 mem_loc_result = op0;
12546 add_loc_descr (&mem_loc_result,
12547 int_loc_descriptor (GET_MODE_MASK (imode)));
12548 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12550 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12552 int shift = DWARF2_ADDR_SIZE
12553 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12554 shift *= BITS_PER_UNIT;
12555 if (GET_CODE (rtl) == SIGN_EXTEND)
12556 op = DW_OP_shra;
12557 else
12558 op = DW_OP_shr;
12559 mem_loc_result = op0;
12560 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12561 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12562 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12563 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12565 else if (!dwarf_strict)
12567 dw_die_ref type_die1, type_die2;
12568 dw_loc_descr_ref cvt;
12570 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12571 GET_CODE (rtl) == ZERO_EXTEND);
12572 if (type_die1 == NULL)
12573 break;
12574 type_die2 = base_type_for_mode (mode, 1);
12575 if (type_die2 == NULL)
12576 break;
12577 mem_loc_result = op0;
12578 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12579 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12580 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12581 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12582 add_loc_descr (&mem_loc_result, cvt);
12583 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12584 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12585 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12586 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12587 add_loc_descr (&mem_loc_result, cvt);
12589 break;
12591 case MEM:
12593 rtx new_rtl = avoid_constant_pool_reference (rtl);
12594 if (new_rtl != rtl)
12596 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12597 initialized);
12598 if (mem_loc_result != NULL)
12599 return mem_loc_result;
12602 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12603 get_address_mode (rtl), mode,
12604 VAR_INIT_STATUS_INITIALIZED);
12605 if (mem_loc_result == NULL)
12606 mem_loc_result = tls_mem_loc_descriptor (rtl);
12607 if (mem_loc_result != NULL)
12609 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12610 || GET_MODE_CLASS (mode) != MODE_INT)
12612 dw_die_ref type_die;
12613 dw_loc_descr_ref deref;
12615 if (dwarf_strict)
12616 return NULL;
12617 type_die
12618 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12619 if (type_die == NULL)
12620 return NULL;
12621 deref = new_loc_descr (DW_OP_GNU_deref_type,
12622 GET_MODE_SIZE (mode), 0);
12623 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12624 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12625 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12626 add_loc_descr (&mem_loc_result, deref);
12628 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12629 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12630 else
12631 add_loc_descr (&mem_loc_result,
12632 new_loc_descr (DW_OP_deref_size,
12633 GET_MODE_SIZE (mode), 0));
12635 break;
12637 case LO_SUM:
12638 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12640 case LABEL_REF:
12641 /* Some ports can transform a symbol ref into a label ref, because
12642 the symbol ref is too far away and has to be dumped into a constant
12643 pool. */
12644 case CONST:
12645 case SYMBOL_REF:
12646 if ((GET_MODE_CLASS (mode) != MODE_INT
12647 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12648 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12649 #ifdef POINTERS_EXTEND_UNSIGNED
12650 && (mode != Pmode || mem_mode == VOIDmode)
12651 #endif
12653 break;
12654 if (GET_CODE (rtl) == SYMBOL_REF
12655 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12657 dw_loc_descr_ref temp;
12659 /* If this is not defined, we have no way to emit the data. */
12660 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12661 break;
12663 temp = new_addr_loc_descr (rtl, dtprel_true);
12665 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12666 add_loc_descr (&mem_loc_result, temp);
12668 break;
12671 if (!const_ok_for_output (rtl))
12672 break;
12674 symref:
12675 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12676 vec_safe_push (used_rtx_array, rtl);
12677 break;
12679 case CONCAT:
12680 case CONCATN:
12681 case VAR_LOCATION:
12682 case DEBUG_IMPLICIT_PTR:
12683 expansion_failed (NULL_TREE, rtl,
12684 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12685 return 0;
12687 case ENTRY_VALUE:
12688 if (dwarf_strict)
12689 return NULL;
12690 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12692 if (GET_MODE_CLASS (mode) != MODE_INT
12693 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12694 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12695 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12696 else
12698 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12699 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12700 return NULL;
12701 op0 = one_reg_loc_descriptor (dbx_regnum,
12702 VAR_INIT_STATUS_INITIALIZED);
12705 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12706 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12708 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12709 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12710 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12711 return NULL;
12713 else
12714 gcc_unreachable ();
12715 if (op0 == NULL)
12716 return NULL;
12717 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12718 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12719 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12720 break;
12722 case DEBUG_PARAMETER_REF:
12723 mem_loc_result = parameter_ref_descriptor (rtl);
12724 break;
12726 case PRE_MODIFY:
12727 /* Extract the PLUS expression nested inside and fall into
12728 PLUS code below. */
12729 rtl = XEXP (rtl, 1);
12730 goto plus;
12732 case PRE_INC:
12733 case PRE_DEC:
12734 /* Turn these into a PLUS expression and fall into the PLUS code
12735 below. */
12736 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12737 gen_int_mode (GET_CODE (rtl) == PRE_INC
12738 ? GET_MODE_UNIT_SIZE (mem_mode)
12739 : -GET_MODE_UNIT_SIZE (mem_mode),
12740 mode));
12742 /* ... fall through ... */
12744 case PLUS:
12745 plus:
12746 if (is_based_loc (rtl)
12747 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12748 || XEXP (rtl, 0) == arg_pointer_rtx
12749 || XEXP (rtl, 0) == frame_pointer_rtx)
12750 && GET_MODE_CLASS (mode) == MODE_INT)
12751 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12752 INTVAL (XEXP (rtl, 1)),
12753 VAR_INIT_STATUS_INITIALIZED);
12754 else
12756 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12757 VAR_INIT_STATUS_INITIALIZED);
12758 if (mem_loc_result == 0)
12759 break;
12761 if (CONST_INT_P (XEXP (rtl, 1))
12762 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12763 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12764 else
12766 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12767 VAR_INIT_STATUS_INITIALIZED);
12768 if (op1 == 0)
12769 return NULL;
12770 add_loc_descr (&mem_loc_result, op1);
12771 add_loc_descr (&mem_loc_result,
12772 new_loc_descr (DW_OP_plus, 0, 0));
12775 break;
12777 /* If a pseudo-reg is optimized away, it is possible for it to
12778 be replaced with a MEM containing a multiply or shift. */
12779 case MINUS:
12780 op = DW_OP_minus;
12781 goto do_binop;
12783 case MULT:
12784 op = DW_OP_mul;
12785 goto do_binop;
12787 case DIV:
12788 if (!dwarf_strict
12789 && GET_MODE_CLASS (mode) == MODE_INT
12790 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12792 mem_loc_result = typed_binop (DW_OP_div, rtl,
12793 base_type_for_mode (mode, 0),
12794 mode, mem_mode);
12795 break;
12797 op = DW_OP_div;
12798 goto do_binop;
12800 case UMOD:
12801 op = DW_OP_mod;
12802 goto do_binop;
12804 case ASHIFT:
12805 op = DW_OP_shl;
12806 goto do_shift;
12808 case ASHIFTRT:
12809 op = DW_OP_shra;
12810 goto do_shift;
12812 case LSHIFTRT:
12813 op = DW_OP_shr;
12814 goto do_shift;
12816 do_shift:
12817 if (GET_MODE_CLASS (mode) != MODE_INT)
12818 break;
12819 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12820 VAR_INIT_STATUS_INITIALIZED);
12822 rtx rtlop1 = XEXP (rtl, 1);
12823 if (GET_MODE (rtlop1) != VOIDmode
12824 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12825 < GET_MODE_BITSIZE (mode))
12826 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12827 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12828 VAR_INIT_STATUS_INITIALIZED);
12831 if (op0 == 0 || op1 == 0)
12832 break;
12834 mem_loc_result = op0;
12835 add_loc_descr (&mem_loc_result, op1);
12836 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12837 break;
12839 case AND:
12840 op = DW_OP_and;
12841 goto do_binop;
12843 case IOR:
12844 op = DW_OP_or;
12845 goto do_binop;
12847 case XOR:
12848 op = DW_OP_xor;
12849 goto do_binop;
12851 do_binop:
12852 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12853 VAR_INIT_STATUS_INITIALIZED);
12854 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12855 VAR_INIT_STATUS_INITIALIZED);
12857 if (op0 == 0 || op1 == 0)
12858 break;
12860 mem_loc_result = op0;
12861 add_loc_descr (&mem_loc_result, op1);
12862 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12863 break;
12865 case MOD:
12866 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12868 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12869 base_type_for_mode (mode, 0),
12870 mode, mem_mode);
12871 break;
12874 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12875 VAR_INIT_STATUS_INITIALIZED);
12876 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12877 VAR_INIT_STATUS_INITIALIZED);
12879 if (op0 == 0 || op1 == 0)
12880 break;
12882 mem_loc_result = op0;
12883 add_loc_descr (&mem_loc_result, op1);
12884 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
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_div, 0, 0));
12887 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12888 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12889 break;
12891 case UDIV:
12892 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12894 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12896 op = DW_OP_div;
12897 goto do_binop;
12899 mem_loc_result = typed_binop (DW_OP_div, rtl,
12900 base_type_for_mode (mode, 1),
12901 mode, mem_mode);
12903 break;
12905 case NOT:
12906 op = DW_OP_not;
12907 goto do_unop;
12909 case ABS:
12910 op = DW_OP_abs;
12911 goto do_unop;
12913 case NEG:
12914 op = DW_OP_neg;
12915 goto do_unop;
12917 do_unop:
12918 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12919 VAR_INIT_STATUS_INITIALIZED);
12921 if (op0 == 0)
12922 break;
12924 mem_loc_result = op0;
12925 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12926 break;
12928 case CONST_INT:
12929 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12930 #ifdef POINTERS_EXTEND_UNSIGNED
12931 || (mode == Pmode
12932 && mem_mode != VOIDmode
12933 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12934 #endif
12937 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12938 break;
12940 if (!dwarf_strict
12941 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12942 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12944 dw_die_ref type_die = base_type_for_mode (mode, 1);
12945 machine_mode amode;
12946 if (type_die == NULL)
12947 return NULL;
12948 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12949 MODE_INT, 0);
12950 if (INTVAL (rtl) >= 0
12951 && amode != BLKmode
12952 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12953 /* const DW_OP_GNU_convert <XXX> vs.
12954 DW_OP_GNU_const_type <XXX, 1, const>. */
12955 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12956 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12958 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12959 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12960 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12961 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12962 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12963 add_loc_descr (&mem_loc_result, op0);
12964 return mem_loc_result;
12966 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12967 INTVAL (rtl));
12968 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12969 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12970 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12971 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12972 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12973 else
12975 mem_loc_result->dw_loc_oprnd2.val_class
12976 = dw_val_class_const_double;
12977 mem_loc_result->dw_loc_oprnd2.v.val_double
12978 = double_int::from_shwi (INTVAL (rtl));
12981 break;
12983 case CONST_DOUBLE:
12984 if (!dwarf_strict)
12986 dw_die_ref type_die;
12988 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12989 CONST_DOUBLE rtx could represent either a large integer
12990 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
12991 the value is always a floating point constant.
12993 When it is an integer, a CONST_DOUBLE is used whenever
12994 the constant requires 2 HWIs to be adequately represented.
12995 We output CONST_DOUBLEs as blocks. */
12996 if (mode == VOIDmode
12997 || (GET_MODE (rtl) == VOIDmode
12998 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12999 break;
13000 type_die = base_type_for_mode (mode,
13001 GET_MODE_CLASS (mode) == MODE_INT);
13002 if (type_die == NULL)
13003 return NULL;
13004 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13005 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13006 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13007 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13008 #if TARGET_SUPPORTS_WIDE_INT == 0
13009 if (!SCALAR_FLOAT_MODE_P (mode))
13011 mem_loc_result->dw_loc_oprnd2.val_class
13012 = dw_val_class_const_double;
13013 mem_loc_result->dw_loc_oprnd2.v.val_double
13014 = rtx_to_double_int (rtl);
13016 else
13017 #endif
13019 unsigned int length = GET_MODE_SIZE (mode);
13020 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13022 insert_float (rtl, array);
13023 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13024 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13025 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13026 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13029 break;
13031 case CONST_WIDE_INT:
13032 if (!dwarf_strict)
13034 dw_die_ref type_die;
13036 type_die = base_type_for_mode (mode,
13037 GET_MODE_CLASS (mode) == MODE_INT);
13038 if (type_die == NULL)
13039 return NULL;
13040 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13041 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13042 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13043 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13044 mem_loc_result->dw_loc_oprnd2.val_class
13045 = dw_val_class_wide_int;
13046 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13047 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13049 break;
13051 case EQ:
13052 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13053 break;
13055 case GE:
13056 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13057 break;
13059 case GT:
13060 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13061 break;
13063 case LE:
13064 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13065 break;
13067 case LT:
13068 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13069 break;
13071 case NE:
13072 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13073 break;
13075 case GEU:
13076 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13077 break;
13079 case GTU:
13080 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13081 break;
13083 case LEU:
13084 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13085 break;
13087 case LTU:
13088 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13089 break;
13091 case UMIN:
13092 case UMAX:
13093 if (GET_MODE_CLASS (mode) != MODE_INT)
13094 break;
13095 /* FALLTHRU */
13096 case SMIN:
13097 case SMAX:
13098 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13099 break;
13101 case ZERO_EXTRACT:
13102 case SIGN_EXTRACT:
13103 if (CONST_INT_P (XEXP (rtl, 1))
13104 && CONST_INT_P (XEXP (rtl, 2))
13105 && ((unsigned) INTVAL (XEXP (rtl, 1))
13106 + (unsigned) INTVAL (XEXP (rtl, 2))
13107 <= GET_MODE_BITSIZE (mode))
13108 && GET_MODE_CLASS (mode) == MODE_INT
13109 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13110 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13112 int shift, size;
13113 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13114 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13115 if (op0 == 0)
13116 break;
13117 if (GET_CODE (rtl) == SIGN_EXTRACT)
13118 op = DW_OP_shra;
13119 else
13120 op = DW_OP_shr;
13121 mem_loc_result = op0;
13122 size = INTVAL (XEXP (rtl, 1));
13123 shift = INTVAL (XEXP (rtl, 2));
13124 if (BITS_BIG_ENDIAN)
13125 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13126 - shift - size;
13127 if (shift + size != (int) DWARF2_ADDR_SIZE)
13129 add_loc_descr (&mem_loc_result,
13130 int_loc_descriptor (DWARF2_ADDR_SIZE
13131 - shift - size));
13132 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13134 if (size != (int) DWARF2_ADDR_SIZE)
13136 add_loc_descr (&mem_loc_result,
13137 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13138 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13141 break;
13143 case IF_THEN_ELSE:
13145 dw_loc_descr_ref op2, bra_node, drop_node;
13146 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13147 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13148 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13149 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13150 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13151 VAR_INIT_STATUS_INITIALIZED);
13152 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13153 VAR_INIT_STATUS_INITIALIZED);
13154 if (op0 == NULL || op1 == NULL || op2 == NULL)
13155 break;
13157 mem_loc_result = op1;
13158 add_loc_descr (&mem_loc_result, op2);
13159 add_loc_descr (&mem_loc_result, op0);
13160 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13161 add_loc_descr (&mem_loc_result, bra_node);
13162 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13163 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13164 add_loc_descr (&mem_loc_result, drop_node);
13165 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13166 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13168 break;
13170 case FLOAT_EXTEND:
13171 case FLOAT_TRUNCATE:
13172 case FLOAT:
13173 case UNSIGNED_FLOAT:
13174 case FIX:
13175 case UNSIGNED_FIX:
13176 if (!dwarf_strict)
13178 dw_die_ref type_die;
13179 dw_loc_descr_ref cvt;
13181 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13182 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13183 if (op0 == NULL)
13184 break;
13185 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13186 && (GET_CODE (rtl) == FLOAT
13187 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13188 <= DWARF2_ADDR_SIZE))
13190 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13191 GET_CODE (rtl) == UNSIGNED_FLOAT);
13192 if (type_die == NULL)
13193 break;
13194 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13195 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13196 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13197 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13198 add_loc_descr (&op0, cvt);
13200 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13201 if (type_die == NULL)
13202 break;
13203 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13204 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13205 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13206 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13207 add_loc_descr (&op0, cvt);
13208 if (GET_MODE_CLASS (mode) == MODE_INT
13209 && (GET_CODE (rtl) == FIX
13210 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13212 op0 = convert_descriptor_to_mode (mode, op0);
13213 if (op0 == NULL)
13214 break;
13216 mem_loc_result = op0;
13218 break;
13220 case CLZ:
13221 case CTZ:
13222 case FFS:
13223 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13224 break;
13226 case POPCOUNT:
13227 case PARITY:
13228 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13229 break;
13231 case BSWAP:
13232 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13233 break;
13235 case ROTATE:
13236 case ROTATERT:
13237 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13238 break;
13240 case COMPARE:
13241 /* In theory, we could implement the above. */
13242 /* DWARF cannot represent the unsigned compare operations
13243 natively. */
13244 case SS_MULT:
13245 case US_MULT:
13246 case SS_DIV:
13247 case US_DIV:
13248 case SS_PLUS:
13249 case US_PLUS:
13250 case SS_MINUS:
13251 case US_MINUS:
13252 case SS_NEG:
13253 case US_NEG:
13254 case SS_ABS:
13255 case SS_ASHIFT:
13256 case US_ASHIFT:
13257 case SS_TRUNCATE:
13258 case US_TRUNCATE:
13259 case UNORDERED:
13260 case ORDERED:
13261 case UNEQ:
13262 case UNGE:
13263 case UNGT:
13264 case UNLE:
13265 case UNLT:
13266 case LTGT:
13267 case FRACT_CONVERT:
13268 case UNSIGNED_FRACT_CONVERT:
13269 case SAT_FRACT:
13270 case UNSIGNED_SAT_FRACT:
13271 case SQRT:
13272 case ASM_OPERANDS:
13273 case VEC_MERGE:
13274 case VEC_SELECT:
13275 case VEC_CONCAT:
13276 case VEC_DUPLICATE:
13277 case UNSPEC:
13278 case HIGH:
13279 case FMA:
13280 case STRICT_LOW_PART:
13281 case CONST_VECTOR:
13282 case CONST_FIXED:
13283 case CLRSB:
13284 case CLOBBER:
13285 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13286 can't express it in the debug info. This can happen e.g. with some
13287 TLS UNSPECs. */
13288 break;
13290 case CONST_STRING:
13291 resolve_one_addr (&rtl);
13292 goto symref;
13294 default:
13295 #ifdef ENABLE_CHECKING
13296 print_rtl (stderr, rtl);
13297 gcc_unreachable ();
13298 #else
13299 break;
13300 #endif
13303 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13304 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13306 return mem_loc_result;
13309 /* Return a descriptor that describes the concatenation of two locations.
13310 This is typically a complex variable. */
13312 static dw_loc_descr_ref
13313 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13315 dw_loc_descr_ref cc_loc_result = NULL;
13316 dw_loc_descr_ref x0_ref
13317 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13318 dw_loc_descr_ref x1_ref
13319 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13321 if (x0_ref == 0 || x1_ref == 0)
13322 return 0;
13324 cc_loc_result = x0_ref;
13325 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13327 add_loc_descr (&cc_loc_result, x1_ref);
13328 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13330 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13331 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13333 return cc_loc_result;
13336 /* Return a descriptor that describes the concatenation of N
13337 locations. */
13339 static dw_loc_descr_ref
13340 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13342 unsigned int i;
13343 dw_loc_descr_ref cc_loc_result = NULL;
13344 unsigned int n = XVECLEN (concatn, 0);
13346 for (i = 0; i < n; ++i)
13348 dw_loc_descr_ref ref;
13349 rtx x = XVECEXP (concatn, 0, i);
13351 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13352 if (ref == NULL)
13353 return NULL;
13355 add_loc_descr (&cc_loc_result, ref);
13356 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13359 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13360 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13362 return cc_loc_result;
13365 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13366 for DEBUG_IMPLICIT_PTR RTL. */
13368 static dw_loc_descr_ref
13369 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13371 dw_loc_descr_ref ret;
13372 dw_die_ref ref;
13374 if (dwarf_strict)
13375 return NULL;
13376 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13377 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13378 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13379 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13380 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13381 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13382 if (ref)
13384 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13385 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13386 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13388 else
13390 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13391 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13393 return ret;
13396 /* Output a proper Dwarf location descriptor for a variable or parameter
13397 which is either allocated in a register or in a memory location. For a
13398 register, we just generate an OP_REG and the register number. For a
13399 memory location we provide a Dwarf postfix expression describing how to
13400 generate the (dynamic) address of the object onto the address stack.
13402 MODE is mode of the decl if this loc_descriptor is going to be used in
13403 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13404 allowed, VOIDmode otherwise.
13406 If we don't know how to describe it, return 0. */
13408 static dw_loc_descr_ref
13409 loc_descriptor (rtx rtl, machine_mode mode,
13410 enum var_init_status initialized)
13412 dw_loc_descr_ref loc_result = NULL;
13414 switch (GET_CODE (rtl))
13416 case SUBREG:
13417 /* The case of a subreg may arise when we have a local (register)
13418 variable or a formal (register) parameter which doesn't quite fill
13419 up an entire register. For now, just assume that it is
13420 legitimate to make the Dwarf info refer to the whole register which
13421 contains the given subreg. */
13422 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13423 loc_result = loc_descriptor (SUBREG_REG (rtl),
13424 GET_MODE (SUBREG_REG (rtl)), initialized);
13425 else
13426 goto do_default;
13427 break;
13429 case REG:
13430 loc_result = reg_loc_descriptor (rtl, initialized);
13431 break;
13433 case MEM:
13434 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13435 GET_MODE (rtl), initialized);
13436 if (loc_result == NULL)
13437 loc_result = tls_mem_loc_descriptor (rtl);
13438 if (loc_result == NULL)
13440 rtx new_rtl = avoid_constant_pool_reference (rtl);
13441 if (new_rtl != rtl)
13442 loc_result = loc_descriptor (new_rtl, mode, initialized);
13444 break;
13446 case CONCAT:
13447 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13448 initialized);
13449 break;
13451 case CONCATN:
13452 loc_result = concatn_loc_descriptor (rtl, initialized);
13453 break;
13455 case VAR_LOCATION:
13456 /* Single part. */
13457 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13459 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13460 if (GET_CODE (loc) == EXPR_LIST)
13461 loc = XEXP (loc, 0);
13462 loc_result = loc_descriptor (loc, mode, initialized);
13463 break;
13466 rtl = XEXP (rtl, 1);
13467 /* FALLTHRU */
13469 case PARALLEL:
13471 rtvec par_elems = XVEC (rtl, 0);
13472 int num_elem = GET_NUM_ELEM (par_elems);
13473 machine_mode mode;
13474 int i;
13476 /* Create the first one, so we have something to add to. */
13477 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13478 VOIDmode, initialized);
13479 if (loc_result == NULL)
13480 return NULL;
13481 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13482 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13483 for (i = 1; i < num_elem; i++)
13485 dw_loc_descr_ref temp;
13487 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13488 VOIDmode, initialized);
13489 if (temp == NULL)
13490 return NULL;
13491 add_loc_descr (&loc_result, temp);
13492 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13493 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13496 break;
13498 case CONST_INT:
13499 if (mode != VOIDmode && mode != BLKmode)
13500 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13501 INTVAL (rtl));
13502 break;
13504 case CONST_DOUBLE:
13505 if (mode == VOIDmode)
13506 mode = GET_MODE (rtl);
13508 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13510 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13512 /* Note that a CONST_DOUBLE rtx could represent either an integer
13513 or a floating-point constant. A CONST_DOUBLE is used whenever
13514 the constant requires more than one word in order to be
13515 adequately represented. We output CONST_DOUBLEs as blocks. */
13516 loc_result = new_loc_descr (DW_OP_implicit_value,
13517 GET_MODE_SIZE (mode), 0);
13518 #if TARGET_SUPPORTS_WIDE_INT == 0
13519 if (!SCALAR_FLOAT_MODE_P (mode))
13521 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13522 loc_result->dw_loc_oprnd2.v.val_double
13523 = rtx_to_double_int (rtl);
13525 else
13526 #endif
13528 unsigned int length = GET_MODE_SIZE (mode);
13529 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13531 insert_float (rtl, array);
13532 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13533 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13534 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13535 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13538 break;
13540 case CONST_WIDE_INT:
13541 if (mode == VOIDmode)
13542 mode = GET_MODE (rtl);
13544 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13546 loc_result = new_loc_descr (DW_OP_implicit_value,
13547 GET_MODE_SIZE (mode), 0);
13548 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13549 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13550 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13552 break;
13554 case CONST_VECTOR:
13555 if (mode == VOIDmode)
13556 mode = GET_MODE (rtl);
13558 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13560 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13561 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13562 unsigned char *array
13563 = ggc_vec_alloc<unsigned char> (length * elt_size);
13564 unsigned int i;
13565 unsigned char *p;
13566 machine_mode imode = GET_MODE_INNER (mode);
13568 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13569 switch (GET_MODE_CLASS (mode))
13571 case MODE_VECTOR_INT:
13572 for (i = 0, p = array; i < length; i++, p += elt_size)
13574 rtx elt = CONST_VECTOR_ELT (rtl, i);
13575 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13577 break;
13579 case MODE_VECTOR_FLOAT:
13580 for (i = 0, p = array; i < length; i++, p += elt_size)
13582 rtx elt = CONST_VECTOR_ELT (rtl, i);
13583 insert_float (elt, p);
13585 break;
13587 default:
13588 gcc_unreachable ();
13591 loc_result = new_loc_descr (DW_OP_implicit_value,
13592 length * elt_size, 0);
13593 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13594 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13595 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13596 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13598 break;
13600 case CONST:
13601 if (mode == VOIDmode
13602 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13603 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13604 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13606 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13607 break;
13609 /* FALLTHROUGH */
13610 case SYMBOL_REF:
13611 if (!const_ok_for_output (rtl))
13612 break;
13613 case LABEL_REF:
13614 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13615 && (dwarf_version >= 4 || !dwarf_strict))
13617 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13618 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13619 vec_safe_push (used_rtx_array, rtl);
13621 break;
13623 case DEBUG_IMPLICIT_PTR:
13624 loc_result = implicit_ptr_descriptor (rtl, 0);
13625 break;
13627 case PLUS:
13628 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13629 && CONST_INT_P (XEXP (rtl, 1)))
13631 loc_result
13632 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13633 break;
13635 /* FALLTHRU */
13636 do_default:
13637 default:
13638 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13639 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13640 && dwarf_version >= 4)
13641 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13643 /* Value expression. */
13644 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13645 if (loc_result)
13646 add_loc_descr (&loc_result,
13647 new_loc_descr (DW_OP_stack_value, 0, 0));
13649 break;
13652 return loc_result;
13655 /* We need to figure out what section we should use as the base for the
13656 address ranges where a given location is valid.
13657 1. If this particular DECL has a section associated with it, use that.
13658 2. If this function has a section associated with it, use that.
13659 3. Otherwise, use the text section.
13660 XXX: If you split a variable across multiple sections, we won't notice. */
13662 static const char *
13663 secname_for_decl (const_tree decl)
13665 const char *secname;
13667 if (VAR_OR_FUNCTION_DECL_P (decl)
13668 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13669 && DECL_SECTION_NAME (decl))
13670 secname = DECL_SECTION_NAME (decl);
13671 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13672 secname = DECL_SECTION_NAME (current_function_decl);
13673 else if (cfun && in_cold_section_p)
13674 secname = crtl->subsections.cold_section_label;
13675 else
13676 secname = text_section_label;
13678 return secname;
13681 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13683 static bool
13684 decl_by_reference_p (tree decl)
13686 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13687 || TREE_CODE (decl) == VAR_DECL)
13688 && DECL_BY_REFERENCE (decl));
13691 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13692 for VARLOC. */
13694 static dw_loc_descr_ref
13695 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13696 enum var_init_status initialized)
13698 int have_address = 0;
13699 dw_loc_descr_ref descr;
13700 machine_mode mode;
13702 if (want_address != 2)
13704 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13705 /* Single part. */
13706 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13708 varloc = PAT_VAR_LOCATION_LOC (varloc);
13709 if (GET_CODE (varloc) == EXPR_LIST)
13710 varloc = XEXP (varloc, 0);
13711 mode = GET_MODE (varloc);
13712 if (MEM_P (varloc))
13714 rtx addr = XEXP (varloc, 0);
13715 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13716 mode, initialized);
13717 if (descr)
13718 have_address = 1;
13719 else
13721 rtx x = avoid_constant_pool_reference (varloc);
13722 if (x != varloc)
13723 descr = mem_loc_descriptor (x, mode, VOIDmode,
13724 initialized);
13727 else
13728 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13730 else
13731 return 0;
13733 else
13735 if (GET_CODE (varloc) == VAR_LOCATION)
13736 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13737 else
13738 mode = DECL_MODE (loc);
13739 descr = loc_descriptor (varloc, mode, initialized);
13740 have_address = 1;
13743 if (!descr)
13744 return 0;
13746 if (want_address == 2 && !have_address
13747 && (dwarf_version >= 4 || !dwarf_strict))
13749 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13751 expansion_failed (loc, NULL_RTX,
13752 "DWARF address size mismatch");
13753 return 0;
13755 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13756 have_address = 1;
13758 /* Show if we can't fill the request for an address. */
13759 if (want_address && !have_address)
13761 expansion_failed (loc, NULL_RTX,
13762 "Want address and only have value");
13763 return 0;
13766 /* If we've got an address and don't want one, dereference. */
13767 if (!want_address && have_address)
13769 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13770 enum dwarf_location_atom op;
13772 if (size > DWARF2_ADDR_SIZE || size == -1)
13774 expansion_failed (loc, NULL_RTX,
13775 "DWARF address size mismatch");
13776 return 0;
13778 else if (size == DWARF2_ADDR_SIZE)
13779 op = DW_OP_deref;
13780 else
13781 op = DW_OP_deref_size;
13783 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13786 return descr;
13789 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13790 if it is not possible. */
13792 static dw_loc_descr_ref
13793 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13795 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13796 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13797 else if (dwarf_version >= 3 || !dwarf_strict)
13798 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13799 else
13800 return NULL;
13803 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13804 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13806 static dw_loc_descr_ref
13807 dw_sra_loc_expr (tree decl, rtx loc)
13809 rtx p;
13810 unsigned int padsize = 0;
13811 dw_loc_descr_ref descr, *descr_tail;
13812 unsigned HOST_WIDE_INT decl_size;
13813 rtx varloc;
13814 enum var_init_status initialized;
13816 if (DECL_SIZE (decl) == NULL
13817 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13818 return NULL;
13820 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13821 descr = NULL;
13822 descr_tail = &descr;
13824 for (p = loc; p; p = XEXP (p, 1))
13826 unsigned int bitsize = decl_piece_bitsize (p);
13827 rtx loc_note = *decl_piece_varloc_ptr (p);
13828 dw_loc_descr_ref cur_descr;
13829 dw_loc_descr_ref *tail, last = NULL;
13830 unsigned int opsize = 0;
13832 if (loc_note == NULL_RTX
13833 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13835 padsize += bitsize;
13836 continue;
13838 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13839 varloc = NOTE_VAR_LOCATION (loc_note);
13840 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13841 if (cur_descr == NULL)
13843 padsize += bitsize;
13844 continue;
13847 /* Check that cur_descr either doesn't use
13848 DW_OP_*piece operations, or their sum is equal
13849 to bitsize. Otherwise we can't embed it. */
13850 for (tail = &cur_descr; *tail != NULL;
13851 tail = &(*tail)->dw_loc_next)
13852 if ((*tail)->dw_loc_opc == DW_OP_piece)
13854 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13855 * BITS_PER_UNIT;
13856 last = *tail;
13858 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13860 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13861 last = *tail;
13864 if (last != NULL && opsize != bitsize)
13866 padsize += bitsize;
13867 /* Discard the current piece of the descriptor and release any
13868 addr_table entries it uses. */
13869 remove_loc_list_addr_table_entries (cur_descr);
13870 continue;
13873 /* If there is a hole, add DW_OP_*piece after empty DWARF
13874 expression, which means that those bits are optimized out. */
13875 if (padsize)
13877 if (padsize > decl_size)
13879 remove_loc_list_addr_table_entries (cur_descr);
13880 goto discard_descr;
13882 decl_size -= padsize;
13883 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13884 if (*descr_tail == NULL)
13886 remove_loc_list_addr_table_entries (cur_descr);
13887 goto discard_descr;
13889 descr_tail = &(*descr_tail)->dw_loc_next;
13890 padsize = 0;
13892 *descr_tail = cur_descr;
13893 descr_tail = tail;
13894 if (bitsize > decl_size)
13895 goto discard_descr;
13896 decl_size -= bitsize;
13897 if (last == NULL)
13899 HOST_WIDE_INT offset = 0;
13900 if (GET_CODE (varloc) == VAR_LOCATION
13901 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13903 varloc = PAT_VAR_LOCATION_LOC (varloc);
13904 if (GET_CODE (varloc) == EXPR_LIST)
13905 varloc = XEXP (varloc, 0);
13909 if (GET_CODE (varloc) == CONST
13910 || GET_CODE (varloc) == SIGN_EXTEND
13911 || GET_CODE (varloc) == ZERO_EXTEND)
13912 varloc = XEXP (varloc, 0);
13913 else if (GET_CODE (varloc) == SUBREG)
13914 varloc = SUBREG_REG (varloc);
13915 else
13916 break;
13918 while (1);
13919 /* DW_OP_bit_size offset should be zero for register
13920 or implicit location descriptions and empty location
13921 descriptions, but for memory addresses needs big endian
13922 adjustment. */
13923 if (MEM_P (varloc))
13925 unsigned HOST_WIDE_INT memsize
13926 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13927 if (memsize != bitsize)
13929 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13930 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13931 goto discard_descr;
13932 if (memsize < bitsize)
13933 goto discard_descr;
13934 if (BITS_BIG_ENDIAN)
13935 offset = memsize - bitsize;
13939 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13940 if (*descr_tail == NULL)
13941 goto discard_descr;
13942 descr_tail = &(*descr_tail)->dw_loc_next;
13946 /* If there were any non-empty expressions, add padding till the end of
13947 the decl. */
13948 if (descr != NULL && decl_size != 0)
13950 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13951 if (*descr_tail == NULL)
13952 goto discard_descr;
13954 return descr;
13956 discard_descr:
13957 /* Discard the descriptor and release any addr_table entries it uses. */
13958 remove_loc_list_addr_table_entries (descr);
13959 return NULL;
13962 /* Return the dwarf representation of the location list LOC_LIST of
13963 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13964 function. */
13966 static dw_loc_list_ref
13967 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13969 const char *endname, *secname;
13970 rtx varloc;
13971 enum var_init_status initialized;
13972 struct var_loc_node *node;
13973 dw_loc_descr_ref descr;
13974 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13975 dw_loc_list_ref list = NULL;
13976 dw_loc_list_ref *listp = &list;
13978 /* Now that we know what section we are using for a base,
13979 actually construct the list of locations.
13980 The first location information is what is passed to the
13981 function that creates the location list, and the remaining
13982 locations just get added on to that list.
13983 Note that we only know the start address for a location
13984 (IE location changes), so to build the range, we use
13985 the range [current location start, next location start].
13986 This means we have to special case the last node, and generate
13987 a range of [last location start, end of function label]. */
13989 secname = secname_for_decl (decl);
13991 for (node = loc_list->first; node; node = node->next)
13992 if (GET_CODE (node->loc) == EXPR_LIST
13993 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13995 if (GET_CODE (node->loc) == EXPR_LIST)
13997 /* This requires DW_OP_{,bit_}piece, which is not usable
13998 inside DWARF expressions. */
13999 if (want_address != 2)
14000 continue;
14001 descr = dw_sra_loc_expr (decl, node->loc);
14002 if (descr == NULL)
14003 continue;
14005 else
14007 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14008 varloc = NOTE_VAR_LOCATION (node->loc);
14009 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14011 if (descr)
14013 bool range_across_switch = false;
14014 /* If section switch happens in between node->label
14015 and node->next->label (or end of function) and
14016 we can't emit it as a single entry list,
14017 emit two ranges, first one ending at the end
14018 of first partition and second one starting at the
14019 beginning of second partition. */
14020 if (node == loc_list->last_before_switch
14021 && (node != loc_list->first || loc_list->first->next)
14022 && current_function_decl)
14024 endname = cfun->fde->dw_fde_end;
14025 range_across_switch = true;
14027 /* The variable has a location between NODE->LABEL and
14028 NODE->NEXT->LABEL. */
14029 else if (node->next)
14030 endname = node->next->label;
14031 /* If the variable has a location at the last label
14032 it keeps its location until the end of function. */
14033 else if (!current_function_decl)
14034 endname = text_end_label;
14035 else
14037 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14038 current_function_funcdef_no);
14039 endname = ggc_strdup (label_id);
14042 *listp = new_loc_list (descr, node->label, endname, secname);
14043 if (TREE_CODE (decl) == PARM_DECL
14044 && node == loc_list->first
14045 && NOTE_P (node->loc)
14046 && strcmp (node->label, endname) == 0)
14047 (*listp)->force = true;
14048 listp = &(*listp)->dw_loc_next;
14050 if (range_across_switch)
14052 if (GET_CODE (node->loc) == EXPR_LIST)
14053 descr = dw_sra_loc_expr (decl, node->loc);
14054 else
14056 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14057 varloc = NOTE_VAR_LOCATION (node->loc);
14058 descr = dw_loc_list_1 (decl, varloc, want_address,
14059 initialized);
14061 gcc_assert (descr);
14062 /* The variable has a location between NODE->LABEL and
14063 NODE->NEXT->LABEL. */
14064 if (node->next)
14065 endname = node->next->label;
14066 else
14067 endname = cfun->fde->dw_fde_second_end;
14068 *listp = new_loc_list (descr,
14069 cfun->fde->dw_fde_second_begin,
14070 endname, secname);
14071 listp = &(*listp)->dw_loc_next;
14076 /* Try to avoid the overhead of a location list emitting a location
14077 expression instead, but only if we didn't have more than one
14078 location entry in the first place. If some entries were not
14079 representable, we don't want to pretend a single entry that was
14080 applies to the entire scope in which the variable is
14081 available. */
14082 if (list && loc_list->first->next)
14083 gen_llsym (list);
14085 return list;
14088 /* Return if the loc_list has only single element and thus can be represented
14089 as location description. */
14091 static bool
14092 single_element_loc_list_p (dw_loc_list_ref list)
14094 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14095 return !list->ll_symbol;
14098 /* To each location in list LIST add loc descr REF. */
14100 static void
14101 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14103 dw_loc_descr_ref copy;
14104 add_loc_descr (&list->expr, ref);
14105 list = list->dw_loc_next;
14106 while (list)
14108 copy = ggc_alloc<dw_loc_descr_node> ();
14109 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14110 add_loc_descr (&list->expr, copy);
14111 while (copy->dw_loc_next)
14113 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14114 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14115 copy->dw_loc_next = new_copy;
14116 copy = new_copy;
14118 list = list->dw_loc_next;
14122 /* Given two lists RET and LIST
14123 produce location list that is result of adding expression in LIST
14124 to expression in RET on each position in program.
14125 Might be destructive on both RET and LIST.
14127 TODO: We handle only simple cases of RET or LIST having at most one
14128 element. General case would inolve sorting the lists in program order
14129 and merging them that will need some additional work.
14130 Adding that will improve quality of debug info especially for SRA-ed
14131 structures. */
14133 static void
14134 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14136 if (!list)
14137 return;
14138 if (!*ret)
14140 *ret = list;
14141 return;
14143 if (!list->dw_loc_next)
14145 add_loc_descr_to_each (*ret, list->expr);
14146 return;
14148 if (!(*ret)->dw_loc_next)
14150 add_loc_descr_to_each (list, (*ret)->expr);
14151 *ret = list;
14152 return;
14154 expansion_failed (NULL_TREE, NULL_RTX,
14155 "Don't know how to merge two non-trivial"
14156 " location lists.\n");
14157 *ret = NULL;
14158 return;
14161 /* LOC is constant expression. Try a luck, look it up in constant
14162 pool and return its loc_descr of its address. */
14164 static dw_loc_descr_ref
14165 cst_pool_loc_descr (tree loc)
14167 /* Get an RTL for this, if something has been emitted. */
14168 rtx rtl = lookup_constant_def (loc);
14170 if (!rtl || !MEM_P (rtl))
14172 gcc_assert (!rtl);
14173 return 0;
14175 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14177 /* TODO: We might get more coverage if we was actually delaying expansion
14178 of all expressions till end of compilation when constant pools are fully
14179 populated. */
14180 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14182 expansion_failed (loc, NULL_RTX,
14183 "CST value in contant pool but not marked.");
14184 return 0;
14186 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14187 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14190 /* Return dw_loc_list representing address of addr_expr LOC
14191 by looking for inner INDIRECT_REF expression and turning
14192 it into simple arithmetics. */
14194 static dw_loc_list_ref
14195 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14197 tree obj, offset;
14198 HOST_WIDE_INT bitsize, bitpos, bytepos;
14199 machine_mode mode;
14200 int unsignedp, volatilep = 0;
14201 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14203 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14204 &bitsize, &bitpos, &offset, &mode,
14205 &unsignedp, &volatilep, false);
14206 STRIP_NOPS (obj);
14207 if (bitpos % BITS_PER_UNIT)
14209 expansion_failed (loc, NULL_RTX, "bitfield access");
14210 return 0;
14212 if (!INDIRECT_REF_P (obj))
14214 expansion_failed (obj,
14215 NULL_RTX, "no indirect ref in inner refrence");
14216 return 0;
14218 if (!offset && !bitpos)
14219 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14220 else if (toplev
14221 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14222 && (dwarf_version >= 4 || !dwarf_strict))
14224 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14225 if (!list_ret)
14226 return 0;
14227 if (offset)
14229 /* Variable offset. */
14230 list_ret1 = loc_list_from_tree (offset, 0);
14231 if (list_ret1 == 0)
14232 return 0;
14233 add_loc_list (&list_ret, list_ret1);
14234 if (!list_ret)
14235 return 0;
14236 add_loc_descr_to_each (list_ret,
14237 new_loc_descr (DW_OP_plus, 0, 0));
14239 bytepos = bitpos / BITS_PER_UNIT;
14240 if (bytepos > 0)
14241 add_loc_descr_to_each (list_ret,
14242 new_loc_descr (DW_OP_plus_uconst,
14243 bytepos, 0));
14244 else if (bytepos < 0)
14245 loc_list_plus_const (list_ret, bytepos);
14246 add_loc_descr_to_each (list_ret,
14247 new_loc_descr (DW_OP_stack_value, 0, 0));
14249 return list_ret;
14253 /* Generate Dwarf location list representing LOC.
14254 If WANT_ADDRESS is false, expression computing LOC will be computed
14255 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14256 if WANT_ADDRESS is 2, expression computing address useable in location
14257 will be returned (i.e. DW_OP_reg can be used
14258 to refer to register values). */
14260 static dw_loc_list_ref
14261 loc_list_from_tree (tree loc, int want_address)
14263 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14264 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14265 int have_address = 0;
14266 enum dwarf_location_atom op;
14268 /* ??? Most of the time we do not take proper care for sign/zero
14269 extending the values properly. Hopefully this won't be a real
14270 problem... */
14272 switch (TREE_CODE (loc))
14274 case ERROR_MARK:
14275 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14276 return 0;
14278 case PLACEHOLDER_EXPR:
14279 /* This case involves extracting fields from an object to determine the
14280 position of other fields. We don't try to encode this here. The
14281 only user of this is Ada, which encodes the needed information using
14282 the names of types. */
14283 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14284 return 0;
14286 case CALL_EXPR:
14287 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14288 /* There are no opcodes for these operations. */
14289 return 0;
14291 case PREINCREMENT_EXPR:
14292 case PREDECREMENT_EXPR:
14293 case POSTINCREMENT_EXPR:
14294 case POSTDECREMENT_EXPR:
14295 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14296 /* There are no opcodes for these operations. */
14297 return 0;
14299 case ADDR_EXPR:
14300 /* If we already want an address, see if there is INDIRECT_REF inside
14301 e.g. for &this->field. */
14302 if (want_address)
14304 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14305 (loc, want_address == 2);
14306 if (list_ret)
14307 have_address = 1;
14308 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14309 && (ret = cst_pool_loc_descr (loc)))
14310 have_address = 1;
14312 /* Otherwise, process the argument and look for the address. */
14313 if (!list_ret && !ret)
14314 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14315 else
14317 if (want_address)
14318 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14319 return NULL;
14321 break;
14323 case VAR_DECL:
14324 if (DECL_THREAD_LOCAL_P (loc))
14326 rtx rtl;
14327 enum dwarf_location_atom tls_op;
14328 enum dtprel_bool dtprel = dtprel_false;
14330 if (targetm.have_tls)
14332 /* If this is not defined, we have no way to emit the
14333 data. */
14334 if (!targetm.asm_out.output_dwarf_dtprel)
14335 return 0;
14337 /* The way DW_OP_GNU_push_tls_address is specified, we
14338 can only look up addresses of objects in the current
14339 module. We used DW_OP_addr as first op, but that's
14340 wrong, because DW_OP_addr is relocated by the debug
14341 info consumer, while DW_OP_GNU_push_tls_address
14342 operand shouldn't be. */
14343 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14344 return 0;
14345 dtprel = dtprel_true;
14346 tls_op = DW_OP_GNU_push_tls_address;
14348 else
14350 if (!targetm.emutls.debug_form_tls_address
14351 || !(dwarf_version >= 3 || !dwarf_strict))
14352 return 0;
14353 /* We stuffed the control variable into the DECL_VALUE_EXPR
14354 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14355 no longer appear in gimple code. We used the control
14356 variable in specific so that we could pick it up here. */
14357 loc = DECL_VALUE_EXPR (loc);
14358 tls_op = DW_OP_form_tls_address;
14361 rtl = rtl_for_decl_location (loc);
14362 if (rtl == NULL_RTX)
14363 return 0;
14365 if (!MEM_P (rtl))
14366 return 0;
14367 rtl = XEXP (rtl, 0);
14368 if (! CONSTANT_P (rtl))
14369 return 0;
14371 ret = new_addr_loc_descr (rtl, dtprel);
14372 ret1 = new_loc_descr (tls_op, 0, 0);
14373 add_loc_descr (&ret, ret1);
14375 have_address = 1;
14376 break;
14378 /* FALLTHRU */
14380 case PARM_DECL:
14381 case RESULT_DECL:
14382 if (DECL_HAS_VALUE_EXPR_P (loc))
14383 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14384 want_address);
14385 /* FALLTHRU */
14387 case FUNCTION_DECL:
14389 rtx rtl;
14390 var_loc_list *loc_list = lookup_decl_loc (loc);
14392 if (loc_list && loc_list->first)
14394 list_ret = dw_loc_list (loc_list, loc, want_address);
14395 have_address = want_address != 0;
14396 break;
14398 rtl = rtl_for_decl_location (loc);
14399 if (rtl == NULL_RTX)
14401 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14402 return 0;
14404 else if (CONST_INT_P (rtl))
14406 HOST_WIDE_INT val = INTVAL (rtl);
14407 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14408 val &= GET_MODE_MASK (DECL_MODE (loc));
14409 ret = int_loc_descriptor (val);
14411 else if (GET_CODE (rtl) == CONST_STRING)
14413 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14414 return 0;
14416 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14417 ret = new_addr_loc_descr (rtl, dtprel_false);
14418 else
14420 machine_mode mode, mem_mode;
14422 /* Certain constructs can only be represented at top-level. */
14423 if (want_address == 2)
14425 ret = loc_descriptor (rtl, VOIDmode,
14426 VAR_INIT_STATUS_INITIALIZED);
14427 have_address = 1;
14429 else
14431 mode = GET_MODE (rtl);
14432 mem_mode = VOIDmode;
14433 if (MEM_P (rtl))
14435 mem_mode = mode;
14436 mode = get_address_mode (rtl);
14437 rtl = XEXP (rtl, 0);
14438 have_address = 1;
14440 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14441 VAR_INIT_STATUS_INITIALIZED);
14443 if (!ret)
14444 expansion_failed (loc, rtl,
14445 "failed to produce loc descriptor for rtl");
14448 break;
14450 case MEM_REF:
14451 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14453 have_address = 1;
14454 goto do_plus;
14456 /* Fallthru. */
14457 case INDIRECT_REF:
14458 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14459 have_address = 1;
14460 break;
14462 case TARGET_MEM_REF:
14463 case SSA_NAME:
14464 return NULL;
14466 case COMPOUND_EXPR:
14467 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14469 CASE_CONVERT:
14470 case VIEW_CONVERT_EXPR:
14471 case SAVE_EXPR:
14472 case MODIFY_EXPR:
14473 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14475 case COMPONENT_REF:
14476 case BIT_FIELD_REF:
14477 case ARRAY_REF:
14478 case ARRAY_RANGE_REF:
14479 case REALPART_EXPR:
14480 case IMAGPART_EXPR:
14482 tree obj, offset;
14483 HOST_WIDE_INT bitsize, bitpos, bytepos;
14484 machine_mode mode;
14485 int unsignedp, volatilep = 0;
14487 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14488 &unsignedp, &volatilep, false);
14490 gcc_assert (obj != loc);
14492 list_ret = loc_list_from_tree (obj,
14493 want_address == 2
14494 && !bitpos && !offset ? 2 : 1);
14495 /* TODO: We can extract value of the small expression via shifting even
14496 for nonzero bitpos. */
14497 if (list_ret == 0)
14498 return 0;
14499 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14501 expansion_failed (loc, NULL_RTX,
14502 "bitfield access");
14503 return 0;
14506 if (offset != NULL_TREE)
14508 /* Variable offset. */
14509 list_ret1 = loc_list_from_tree (offset, 0);
14510 if (list_ret1 == 0)
14511 return 0;
14512 add_loc_list (&list_ret, list_ret1);
14513 if (!list_ret)
14514 return 0;
14515 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14518 bytepos = bitpos / BITS_PER_UNIT;
14519 if (bytepos > 0)
14520 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14521 else if (bytepos < 0)
14522 loc_list_plus_const (list_ret, bytepos);
14524 have_address = 1;
14525 break;
14528 case INTEGER_CST:
14529 if ((want_address || !tree_fits_shwi_p (loc))
14530 && (ret = cst_pool_loc_descr (loc)))
14531 have_address = 1;
14532 else if (want_address == 2
14533 && tree_fits_shwi_p (loc)
14534 && (ret = address_of_int_loc_descriptor
14535 (int_size_in_bytes (TREE_TYPE (loc)),
14536 tree_to_shwi (loc))))
14537 have_address = 1;
14538 else if (tree_fits_shwi_p (loc))
14539 ret = int_loc_descriptor (tree_to_shwi (loc));
14540 else
14542 expansion_failed (loc, NULL_RTX,
14543 "Integer operand is not host integer");
14544 return 0;
14546 break;
14548 case CONSTRUCTOR:
14549 case REAL_CST:
14550 case STRING_CST:
14551 case COMPLEX_CST:
14552 if ((ret = cst_pool_loc_descr (loc)))
14553 have_address = 1;
14554 else
14555 /* We can construct small constants here using int_loc_descriptor. */
14556 expansion_failed (loc, NULL_RTX,
14557 "constructor or constant not in constant pool");
14558 break;
14560 case TRUTH_AND_EXPR:
14561 case TRUTH_ANDIF_EXPR:
14562 case BIT_AND_EXPR:
14563 op = DW_OP_and;
14564 goto do_binop;
14566 case TRUTH_XOR_EXPR:
14567 case BIT_XOR_EXPR:
14568 op = DW_OP_xor;
14569 goto do_binop;
14571 case TRUTH_OR_EXPR:
14572 case TRUTH_ORIF_EXPR:
14573 case BIT_IOR_EXPR:
14574 op = DW_OP_or;
14575 goto do_binop;
14577 case FLOOR_DIV_EXPR:
14578 case CEIL_DIV_EXPR:
14579 case ROUND_DIV_EXPR:
14580 case TRUNC_DIV_EXPR:
14581 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14582 return 0;
14583 op = DW_OP_div;
14584 goto do_binop;
14586 case MINUS_EXPR:
14587 op = DW_OP_minus;
14588 goto do_binop;
14590 case FLOOR_MOD_EXPR:
14591 case CEIL_MOD_EXPR:
14592 case ROUND_MOD_EXPR:
14593 case TRUNC_MOD_EXPR:
14594 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14596 op = DW_OP_mod;
14597 goto do_binop;
14599 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14600 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14601 if (list_ret == 0 || list_ret1 == 0)
14602 return 0;
14604 add_loc_list (&list_ret, list_ret1);
14605 if (list_ret == 0)
14606 return 0;
14607 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 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_div, 0, 0));
14610 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14611 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14612 break;
14614 case MULT_EXPR:
14615 op = DW_OP_mul;
14616 goto do_binop;
14618 case LSHIFT_EXPR:
14619 op = DW_OP_shl;
14620 goto do_binop;
14622 case RSHIFT_EXPR:
14623 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14624 goto do_binop;
14626 case POINTER_PLUS_EXPR:
14627 case PLUS_EXPR:
14628 do_plus:
14629 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14631 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14632 if (list_ret == 0)
14633 return 0;
14635 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14636 break;
14639 op = DW_OP_plus;
14640 goto do_binop;
14642 case LE_EXPR:
14643 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14644 return 0;
14646 op = DW_OP_le;
14647 goto do_binop;
14649 case GE_EXPR:
14650 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14651 return 0;
14653 op = DW_OP_ge;
14654 goto do_binop;
14656 case LT_EXPR:
14657 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14658 return 0;
14660 op = DW_OP_lt;
14661 goto do_binop;
14663 case GT_EXPR:
14664 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14665 return 0;
14667 op = DW_OP_gt;
14668 goto do_binop;
14670 case EQ_EXPR:
14671 op = DW_OP_eq;
14672 goto do_binop;
14674 case NE_EXPR:
14675 op = DW_OP_ne;
14676 goto do_binop;
14678 do_binop:
14679 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14680 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14681 if (list_ret == 0 || list_ret1 == 0)
14682 return 0;
14684 add_loc_list (&list_ret, list_ret1);
14685 if (list_ret == 0)
14686 return 0;
14687 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14688 break;
14690 case TRUTH_NOT_EXPR:
14691 case BIT_NOT_EXPR:
14692 op = DW_OP_not;
14693 goto do_unop;
14695 case ABS_EXPR:
14696 op = DW_OP_abs;
14697 goto do_unop;
14699 case NEGATE_EXPR:
14700 op = DW_OP_neg;
14701 goto do_unop;
14703 do_unop:
14704 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14705 if (list_ret == 0)
14706 return 0;
14708 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14709 break;
14711 case MIN_EXPR:
14712 case MAX_EXPR:
14714 const enum tree_code code =
14715 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14717 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14718 build2 (code, integer_type_node,
14719 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14720 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14723 /* ... fall through ... */
14725 case COND_EXPR:
14727 dw_loc_descr_ref lhs
14728 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14729 dw_loc_list_ref rhs
14730 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14731 dw_loc_descr_ref bra_node, jump_node, tmp;
14733 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14734 if (list_ret == 0 || lhs == 0 || rhs == 0)
14735 return 0;
14737 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14738 add_loc_descr_to_each (list_ret, bra_node);
14740 add_loc_list (&list_ret, rhs);
14741 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14742 add_loc_descr_to_each (list_ret, jump_node);
14744 add_loc_descr_to_each (list_ret, lhs);
14745 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14746 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14748 /* ??? Need a node to point the skip at. Use a nop. */
14749 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14750 add_loc_descr_to_each (list_ret, tmp);
14751 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14752 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14754 break;
14756 case FIX_TRUNC_EXPR:
14757 return 0;
14759 default:
14760 /* Leave front-end specific codes as simply unknown. This comes
14761 up, for instance, with the C STMT_EXPR. */
14762 if ((unsigned int) TREE_CODE (loc)
14763 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14765 expansion_failed (loc, NULL_RTX,
14766 "language specific tree node");
14767 return 0;
14770 #ifdef ENABLE_CHECKING
14771 /* Otherwise this is a generic code; we should just lists all of
14772 these explicitly. We forgot one. */
14773 gcc_unreachable ();
14774 #else
14775 /* In a release build, we want to degrade gracefully: better to
14776 generate incomplete debugging information than to crash. */
14777 return NULL;
14778 #endif
14781 if (!ret && !list_ret)
14782 return 0;
14784 if (want_address == 2 && !have_address
14785 && (dwarf_version >= 4 || !dwarf_strict))
14787 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14789 expansion_failed (loc, NULL_RTX,
14790 "DWARF address size mismatch");
14791 return 0;
14793 if (ret)
14794 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14795 else
14796 add_loc_descr_to_each (list_ret,
14797 new_loc_descr (DW_OP_stack_value, 0, 0));
14798 have_address = 1;
14800 /* Show if we can't fill the request for an address. */
14801 if (want_address && !have_address)
14803 expansion_failed (loc, NULL_RTX,
14804 "Want address and only have value");
14805 return 0;
14808 gcc_assert (!ret || !list_ret);
14810 /* If we've got an address and don't want one, dereference. */
14811 if (!want_address && have_address)
14813 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14815 if (size > DWARF2_ADDR_SIZE || size == -1)
14817 expansion_failed (loc, NULL_RTX,
14818 "DWARF address size mismatch");
14819 return 0;
14821 else if (size == DWARF2_ADDR_SIZE)
14822 op = DW_OP_deref;
14823 else
14824 op = DW_OP_deref_size;
14826 if (ret)
14827 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14828 else
14829 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14831 if (ret)
14832 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14834 return list_ret;
14837 /* Same as above but return only single location expression. */
14838 static dw_loc_descr_ref
14839 loc_descriptor_from_tree (tree loc, int want_address)
14841 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14842 if (!ret)
14843 return NULL;
14844 if (ret->dw_loc_next)
14846 expansion_failed (loc, NULL_RTX,
14847 "Location list where only loc descriptor needed");
14848 return NULL;
14850 return ret->expr;
14853 /* Given a value, round it up to the lowest multiple of `boundary'
14854 which is not less than the value itself. */
14856 static inline HOST_WIDE_INT
14857 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14859 return (((value + boundary - 1) / boundary) * boundary);
14862 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14863 pointer to the declared type for the relevant field variable, or return
14864 `integer_type_node' if the given node turns out to be an
14865 ERROR_MARK node. */
14867 static inline tree
14868 field_type (const_tree decl)
14870 tree type;
14872 if (TREE_CODE (decl) == ERROR_MARK)
14873 return integer_type_node;
14875 type = DECL_BIT_FIELD_TYPE (decl);
14876 if (type == NULL_TREE)
14877 type = TREE_TYPE (decl);
14879 return type;
14882 /* Given a pointer to a tree node, return the alignment in bits for
14883 it, or else return BITS_PER_WORD if the node actually turns out to
14884 be an ERROR_MARK node. */
14886 static inline unsigned
14887 simple_type_align_in_bits (const_tree type)
14889 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14892 static inline unsigned
14893 simple_decl_align_in_bits (const_tree decl)
14895 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14898 /* Return the result of rounding T up to ALIGN. */
14900 static inline offset_int
14901 round_up_to_align (const offset_int &t, unsigned int align)
14903 return wi::udiv_trunc (t + align - 1, align) * align;
14906 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14907 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14908 or return 0 if we are unable to determine what that offset is, either
14909 because the argument turns out to be a pointer to an ERROR_MARK node, or
14910 because the offset is actually variable. (We can't handle the latter case
14911 just yet). */
14913 static HOST_WIDE_INT
14914 field_byte_offset (const_tree decl)
14916 offset_int object_offset_in_bits;
14917 offset_int object_offset_in_bytes;
14918 offset_int bitpos_int;
14920 if (TREE_CODE (decl) == ERROR_MARK)
14921 return 0;
14923 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14925 /* We cannot yet cope with fields whose positions are variable, so
14926 for now, when we see such things, we simply return 0. Someday, we may
14927 be able to handle such cases, but it will be damn difficult. */
14928 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14929 return 0;
14931 bitpos_int = wi::to_offset (bit_position (decl));
14933 #ifdef PCC_BITFIELD_TYPE_MATTERS
14934 if (PCC_BITFIELD_TYPE_MATTERS)
14936 tree type;
14937 tree field_size_tree;
14938 offset_int deepest_bitpos;
14939 offset_int field_size_in_bits;
14940 unsigned int type_align_in_bits;
14941 unsigned int decl_align_in_bits;
14942 offset_int type_size_in_bits;
14944 type = field_type (decl);
14945 type_size_in_bits = offset_int_type_size_in_bits (type);
14946 type_align_in_bits = simple_type_align_in_bits (type);
14948 field_size_tree = DECL_SIZE (decl);
14950 /* The size could be unspecified if there was an error, or for
14951 a flexible array member. */
14952 if (!field_size_tree)
14953 field_size_tree = bitsize_zero_node;
14955 /* If the size of the field is not constant, use the type size. */
14956 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14957 field_size_in_bits = wi::to_offset (field_size_tree);
14958 else
14959 field_size_in_bits = type_size_in_bits;
14961 decl_align_in_bits = simple_decl_align_in_bits (decl);
14963 /* The GCC front-end doesn't make any attempt to keep track of the
14964 starting bit offset (relative to the start of the containing
14965 structure type) of the hypothetical "containing object" for a
14966 bit-field. Thus, when computing the byte offset value for the
14967 start of the "containing object" of a bit-field, we must deduce
14968 this information on our own. This can be rather tricky to do in
14969 some cases. For example, handling the following structure type
14970 definition when compiling for an i386/i486 target (which only
14971 aligns long long's to 32-bit boundaries) can be very tricky:
14973 struct S { int field1; long long field2:31; };
14975 Fortunately, there is a simple rule-of-thumb which can be used
14976 in such cases. When compiling for an i386/i486, GCC will
14977 allocate 8 bytes for the structure shown above. It decides to
14978 do this based upon one simple rule for bit-field allocation.
14979 GCC allocates each "containing object" for each bit-field at
14980 the first (i.e. lowest addressed) legitimate alignment boundary
14981 (based upon the required minimum alignment for the declared
14982 type of the field) which it can possibly use, subject to the
14983 condition that there is still enough available space remaining
14984 in the containing object (when allocated at the selected point)
14985 to fully accommodate all of the bits of the bit-field itself.
14987 This simple rule makes it obvious why GCC allocates 8 bytes for
14988 each object of the structure type shown above. When looking
14989 for a place to allocate the "containing object" for `field2',
14990 the compiler simply tries to allocate a 64-bit "containing
14991 object" at each successive 32-bit boundary (starting at zero)
14992 until it finds a place to allocate that 64- bit field such that
14993 at least 31 contiguous (and previously unallocated) bits remain
14994 within that selected 64 bit field. (As it turns out, for the
14995 example above, the compiler finds it is OK to allocate the
14996 "containing object" 64-bit field at bit-offset zero within the
14997 structure type.)
14999 Here we attempt to work backwards from the limited set of facts
15000 we're given, and we try to deduce from those facts, where GCC
15001 must have believed that the containing object started (within
15002 the structure type). The value we deduce is then used (by the
15003 callers of this routine) to generate DW_AT_location and
15004 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15005 the case of DW_AT_location, regular fields as well). */
15007 /* Figure out the bit-distance from the start of the structure to
15008 the "deepest" bit of the bit-field. */
15009 deepest_bitpos = bitpos_int + field_size_in_bits;
15011 /* This is the tricky part. Use some fancy footwork to deduce
15012 where the lowest addressed bit of the containing object must
15013 be. */
15014 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15016 /* Round up to type_align by default. This works best for
15017 bitfields. */
15018 object_offset_in_bits
15019 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15021 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15023 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15025 /* Round up to decl_align instead. */
15026 object_offset_in_bits
15027 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15030 else
15031 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15032 object_offset_in_bits = bitpos_int;
15034 object_offset_in_bytes
15035 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15036 return object_offset_in_bytes.to_shwi ();
15039 /* The following routines define various Dwarf attributes and any data
15040 associated with them. */
15042 /* Add a location description attribute value to a DIE.
15044 This emits location attributes suitable for whole variables and
15045 whole parameters. Note that the location attributes for struct fields are
15046 generated by the routine `data_member_location_attribute' below. */
15048 static inline void
15049 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15050 dw_loc_list_ref descr)
15052 if (descr == 0)
15053 return;
15054 if (single_element_loc_list_p (descr))
15055 add_AT_loc (die, attr_kind, descr->expr);
15056 else
15057 add_AT_loc_list (die, attr_kind, descr);
15060 /* Add DW_AT_accessibility attribute to DIE if needed. */
15062 static void
15063 add_accessibility_attribute (dw_die_ref die, tree decl)
15065 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15066 children, otherwise the default is DW_ACCESS_public. In DWARF2
15067 the default has always been DW_ACCESS_public. */
15068 if (TREE_PROTECTED (decl))
15069 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15070 else if (TREE_PRIVATE (decl))
15072 if (dwarf_version == 2
15073 || die->die_parent == NULL
15074 || die->die_parent->die_tag != DW_TAG_class_type)
15075 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15077 else if (dwarf_version > 2
15078 && die->die_parent
15079 && die->die_parent->die_tag == DW_TAG_class_type)
15080 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15083 /* Attach the specialized form of location attribute used for data members of
15084 struct and union types. In the special case of a FIELD_DECL node which
15085 represents a bit-field, the "offset" part of this special location
15086 descriptor must indicate the distance in bytes from the lowest-addressed
15087 byte of the containing struct or union type to the lowest-addressed byte of
15088 the "containing object" for the bit-field. (See the `field_byte_offset'
15089 function above).
15091 For any given bit-field, the "containing object" is a hypothetical object
15092 (of some integral or enum type) within which the given bit-field lives. The
15093 type of this hypothetical "containing object" is always the same as the
15094 declared type of the individual bit-field itself (for GCC anyway... the
15095 DWARF spec doesn't actually mandate this). Note that it is the size (in
15096 bytes) of the hypothetical "containing object" which will be given in the
15097 DW_AT_byte_size attribute for this bit-field. (See the
15098 `byte_size_attribute' function below.) It is also used when calculating the
15099 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15100 function below.) */
15102 static void
15103 add_data_member_location_attribute (dw_die_ref die, tree decl)
15105 HOST_WIDE_INT offset;
15106 dw_loc_descr_ref loc_descr = 0;
15108 if (TREE_CODE (decl) == TREE_BINFO)
15110 /* We're working on the TAG_inheritance for a base class. */
15111 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15113 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15114 aren't at a fixed offset from all (sub)objects of the same
15115 type. We need to extract the appropriate offset from our
15116 vtable. The following dwarf expression means
15118 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15120 This is specific to the V3 ABI, of course. */
15122 dw_loc_descr_ref tmp;
15124 /* Make a copy of the object address. */
15125 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15126 add_loc_descr (&loc_descr, tmp);
15128 /* Extract the vtable address. */
15129 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15130 add_loc_descr (&loc_descr, tmp);
15132 /* Calculate the address of the offset. */
15133 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15134 gcc_assert (offset < 0);
15136 tmp = int_loc_descriptor (-offset);
15137 add_loc_descr (&loc_descr, tmp);
15138 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15139 add_loc_descr (&loc_descr, tmp);
15141 /* Extract the offset. */
15142 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15143 add_loc_descr (&loc_descr, tmp);
15145 /* Add it to the object address. */
15146 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15147 add_loc_descr (&loc_descr, tmp);
15149 else
15150 offset = tree_to_shwi (BINFO_OFFSET (decl));
15152 else
15153 offset = field_byte_offset (decl);
15155 if (! loc_descr)
15157 if (dwarf_version > 2)
15159 /* Don't need to output a location expression, just the constant. */
15160 if (offset < 0)
15161 add_AT_int (die, DW_AT_data_member_location, offset);
15162 else
15163 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15164 return;
15166 else
15168 enum dwarf_location_atom op;
15170 /* The DWARF2 standard says that we should assume that the structure
15171 address is already on the stack, so we can specify a structure
15172 field address by using DW_OP_plus_uconst. */
15173 op = DW_OP_plus_uconst;
15174 loc_descr = new_loc_descr (op, offset, 0);
15178 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15181 /* Writes integer values to dw_vec_const array. */
15183 static void
15184 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15186 while (size != 0)
15188 *dest++ = val & 0xff;
15189 val >>= 8;
15190 --size;
15194 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15196 static HOST_WIDE_INT
15197 extract_int (const unsigned char *src, unsigned int size)
15199 HOST_WIDE_INT val = 0;
15201 src += size;
15202 while (size != 0)
15204 val <<= 8;
15205 val |= *--src & 0xff;
15206 --size;
15208 return val;
15211 /* Writes wide_int values to dw_vec_const array. */
15213 static void
15214 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15216 int i;
15218 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15220 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15221 return;
15224 /* We'd have to extend this code to support odd sizes. */
15225 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15227 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15229 if (WORDS_BIG_ENDIAN)
15230 for (i = n - 1; i >= 0; i--)
15232 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15233 dest += sizeof (HOST_WIDE_INT);
15235 else
15236 for (i = 0; i < n; i++)
15238 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15239 dest += sizeof (HOST_WIDE_INT);
15243 /* Writes floating point values to dw_vec_const array. */
15245 static void
15246 insert_float (const_rtx rtl, unsigned char *array)
15248 REAL_VALUE_TYPE rv;
15249 long val[4];
15250 int i;
15252 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15253 real_to_target (val, &rv, GET_MODE (rtl));
15255 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15256 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15258 insert_int (val[i], 4, array);
15259 array += 4;
15263 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15264 does not have a "location" either in memory or in a register. These
15265 things can arise in GNU C when a constant is passed as an actual parameter
15266 to an inlined function. They can also arise in C++ where declared
15267 constants do not necessarily get memory "homes". */
15269 static bool
15270 add_const_value_attribute (dw_die_ref die, rtx rtl)
15272 switch (GET_CODE (rtl))
15274 case CONST_INT:
15276 HOST_WIDE_INT val = INTVAL (rtl);
15278 if (val < 0)
15279 add_AT_int (die, DW_AT_const_value, val);
15280 else
15281 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15283 return true;
15285 case CONST_WIDE_INT:
15286 add_AT_wide (die, DW_AT_const_value,
15287 std::make_pair (rtl, GET_MODE (rtl)));
15288 return true;
15290 case CONST_DOUBLE:
15291 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15292 floating-point constant. A CONST_DOUBLE is used whenever the
15293 constant requires more than one word in order to be adequately
15294 represented. */
15296 machine_mode mode = GET_MODE (rtl);
15298 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15299 add_AT_double (die, DW_AT_const_value,
15300 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15301 else
15303 unsigned int length = GET_MODE_SIZE (mode);
15304 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15306 insert_float (rtl, array);
15307 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15310 return true;
15312 case CONST_VECTOR:
15314 machine_mode mode = GET_MODE (rtl);
15315 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15316 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15317 unsigned char *array
15318 = ggc_vec_alloc<unsigned char> (length * elt_size);
15319 unsigned int i;
15320 unsigned char *p;
15321 machine_mode imode = GET_MODE_INNER (mode);
15323 switch (GET_MODE_CLASS (mode))
15325 case MODE_VECTOR_INT:
15326 for (i = 0, p = array; i < length; i++, p += elt_size)
15328 rtx elt = CONST_VECTOR_ELT (rtl, i);
15329 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15331 break;
15333 case MODE_VECTOR_FLOAT:
15334 for (i = 0, p = array; i < length; i++, p += elt_size)
15336 rtx elt = CONST_VECTOR_ELT (rtl, i);
15337 insert_float (elt, p);
15339 break;
15341 default:
15342 gcc_unreachable ();
15345 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15347 return true;
15349 case CONST_STRING:
15350 if (dwarf_version >= 4 || !dwarf_strict)
15352 dw_loc_descr_ref loc_result;
15353 resolve_one_addr (&rtl);
15354 rtl_addr:
15355 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15356 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15357 add_AT_loc (die, DW_AT_location, loc_result);
15358 vec_safe_push (used_rtx_array, rtl);
15359 return true;
15361 return false;
15363 case CONST:
15364 if (CONSTANT_P (XEXP (rtl, 0)))
15365 return add_const_value_attribute (die, XEXP (rtl, 0));
15366 /* FALLTHROUGH */
15367 case SYMBOL_REF:
15368 if (!const_ok_for_output (rtl))
15369 return false;
15370 case LABEL_REF:
15371 if (dwarf_version >= 4 || !dwarf_strict)
15372 goto rtl_addr;
15373 return false;
15375 case PLUS:
15376 /* In cases where an inlined instance of an inline function is passed
15377 the address of an `auto' variable (which is local to the caller) we
15378 can get a situation where the DECL_RTL of the artificial local
15379 variable (for the inlining) which acts as a stand-in for the
15380 corresponding formal parameter (of the inline function) will look
15381 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15382 exactly a compile-time constant expression, but it isn't the address
15383 of the (artificial) local variable either. Rather, it represents the
15384 *value* which the artificial local variable always has during its
15385 lifetime. We currently have no way to represent such quasi-constant
15386 values in Dwarf, so for now we just punt and generate nothing. */
15387 return false;
15389 case HIGH:
15390 case CONST_FIXED:
15391 return false;
15393 case MEM:
15394 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15395 && MEM_READONLY_P (rtl)
15396 && GET_MODE (rtl) == BLKmode)
15398 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15399 return true;
15401 return false;
15403 default:
15404 /* No other kinds of rtx should be possible here. */
15405 gcc_unreachable ();
15407 return false;
15410 /* Determine whether the evaluation of EXPR references any variables
15411 or functions which aren't otherwise used (and therefore may not be
15412 output). */
15413 static tree
15414 reference_to_unused (tree * tp, int * walk_subtrees,
15415 void * data ATTRIBUTE_UNUSED)
15417 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15418 *walk_subtrees = 0;
15420 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15421 && ! TREE_ASM_WRITTEN (*tp))
15422 return *tp;
15423 /* ??? The C++ FE emits debug information for using decls, so
15424 putting gcc_unreachable here falls over. See PR31899. For now
15425 be conservative. */
15426 else if (!symtab->global_info_ready
15427 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15428 return *tp;
15429 else if (TREE_CODE (*tp) == VAR_DECL)
15431 varpool_node *node = varpool_node::get (*tp);
15432 if (!node || !node->definition)
15433 return *tp;
15435 else if (TREE_CODE (*tp) == FUNCTION_DECL
15436 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15438 /* The call graph machinery must have finished analyzing,
15439 optimizing and gimplifying the CU by now.
15440 So if *TP has no call graph node associated
15441 to it, it means *TP will not be emitted. */
15442 if (!cgraph_node::get (*tp))
15443 return *tp;
15445 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15446 return *tp;
15448 return NULL_TREE;
15451 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15452 for use in a later add_const_value_attribute call. */
15454 static rtx
15455 rtl_for_decl_init (tree init, tree type)
15457 rtx rtl = NULL_RTX;
15459 STRIP_NOPS (init);
15461 /* If a variable is initialized with a string constant without embedded
15462 zeros, build CONST_STRING. */
15463 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15465 tree enttype = TREE_TYPE (type);
15466 tree domain = TYPE_DOMAIN (type);
15467 machine_mode mode = TYPE_MODE (enttype);
15469 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15470 && domain
15471 && integer_zerop (TYPE_MIN_VALUE (domain))
15472 && compare_tree_int (TYPE_MAX_VALUE (domain),
15473 TREE_STRING_LENGTH (init) - 1) == 0
15474 && ((size_t) TREE_STRING_LENGTH (init)
15475 == strlen (TREE_STRING_POINTER (init)) + 1))
15477 rtl = gen_rtx_CONST_STRING (VOIDmode,
15478 ggc_strdup (TREE_STRING_POINTER (init)));
15479 rtl = gen_rtx_MEM (BLKmode, rtl);
15480 MEM_READONLY_P (rtl) = 1;
15483 /* Other aggregates, and complex values, could be represented using
15484 CONCAT: FIXME! */
15485 else if (AGGREGATE_TYPE_P (type)
15486 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15487 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15488 || TREE_CODE (type) == COMPLEX_TYPE)
15490 /* Vectors only work if their mode is supported by the target.
15491 FIXME: generic vectors ought to work too. */
15492 else if (TREE_CODE (type) == VECTOR_TYPE
15493 && !VECTOR_MODE_P (TYPE_MODE (type)))
15495 /* If the initializer is something that we know will expand into an
15496 immediate RTL constant, expand it now. We must be careful not to
15497 reference variables which won't be output. */
15498 else if (initializer_constant_valid_p (init, type)
15499 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15501 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15502 possible. */
15503 if (TREE_CODE (type) == VECTOR_TYPE)
15504 switch (TREE_CODE (init))
15506 case VECTOR_CST:
15507 break;
15508 case CONSTRUCTOR:
15509 if (TREE_CONSTANT (init))
15511 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15512 bool constant_p = true;
15513 tree value;
15514 unsigned HOST_WIDE_INT ix;
15516 /* Even when ctor is constant, it might contain non-*_CST
15517 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15518 belong into VECTOR_CST nodes. */
15519 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15520 if (!CONSTANT_CLASS_P (value))
15522 constant_p = false;
15523 break;
15526 if (constant_p)
15528 init = build_vector_from_ctor (type, elts);
15529 break;
15532 /* FALLTHRU */
15534 default:
15535 return NULL;
15538 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15540 /* If expand_expr returns a MEM, it wasn't immediate. */
15541 gcc_assert (!rtl || !MEM_P (rtl));
15544 return rtl;
15547 /* Generate RTL for the variable DECL to represent its location. */
15549 static rtx
15550 rtl_for_decl_location (tree decl)
15552 rtx rtl;
15554 /* Here we have to decide where we are going to say the parameter "lives"
15555 (as far as the debugger is concerned). We only have a couple of
15556 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15558 DECL_RTL normally indicates where the parameter lives during most of the
15559 activation of the function. If optimization is enabled however, this
15560 could be either NULL or else a pseudo-reg. Both of those cases indicate
15561 that the parameter doesn't really live anywhere (as far as the code
15562 generation parts of GCC are concerned) during most of the function's
15563 activation. That will happen (for example) if the parameter is never
15564 referenced within the function.
15566 We could just generate a location descriptor here for all non-NULL
15567 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15568 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15569 where DECL_RTL is NULL or is a pseudo-reg.
15571 Note however that we can only get away with using DECL_INCOMING_RTL as
15572 a backup substitute for DECL_RTL in certain limited cases. In cases
15573 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15574 we can be sure that the parameter was passed using the same type as it is
15575 declared to have within the function, and that its DECL_INCOMING_RTL
15576 points us to a place where a value of that type is passed.
15578 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15579 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15580 because in these cases DECL_INCOMING_RTL points us to a value of some
15581 type which is *different* from the type of the parameter itself. Thus,
15582 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15583 such cases, the debugger would end up (for example) trying to fetch a
15584 `float' from a place which actually contains the first part of a
15585 `double'. That would lead to really incorrect and confusing
15586 output at debug-time.
15588 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15589 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15590 are a couple of exceptions however. On little-endian machines we can
15591 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15592 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15593 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15594 when (on a little-endian machine) a non-prototyped function has a
15595 parameter declared to be of type `short' or `char'. In such cases,
15596 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15597 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15598 passed `int' value. If the debugger then uses that address to fetch
15599 a `short' or a `char' (on a little-endian machine) the result will be
15600 the correct data, so we allow for such exceptional cases below.
15602 Note that our goal here is to describe the place where the given formal
15603 parameter lives during most of the function's activation (i.e. between the
15604 end of the prologue and the start of the epilogue). We'll do that as best
15605 as we can. Note however that if the given formal parameter is modified
15606 sometime during the execution of the function, then a stack backtrace (at
15607 debug-time) will show the function as having been called with the *new*
15608 value rather than the value which was originally passed in. This happens
15609 rarely enough that it is not a major problem, but it *is* a problem, and
15610 I'd like to fix it.
15612 A future version of dwarf2out.c may generate two additional attributes for
15613 any given DW_TAG_formal_parameter DIE which will describe the "passed
15614 type" and the "passed location" for the given formal parameter in addition
15615 to the attributes we now generate to indicate the "declared type" and the
15616 "active location" for each parameter. This additional set of attributes
15617 could be used by debuggers for stack backtraces. Separately, note that
15618 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15619 This happens (for example) for inlined-instances of inline function formal
15620 parameters which are never referenced. This really shouldn't be
15621 happening. All PARM_DECL nodes should get valid non-NULL
15622 DECL_INCOMING_RTL values. FIXME. */
15624 /* Use DECL_RTL as the "location" unless we find something better. */
15625 rtl = DECL_RTL_IF_SET (decl);
15627 /* When generating abstract instances, ignore everything except
15628 constants, symbols living in memory, and symbols living in
15629 fixed registers. */
15630 if (! reload_completed)
15632 if (rtl
15633 && (CONSTANT_P (rtl)
15634 || (MEM_P (rtl)
15635 && CONSTANT_P (XEXP (rtl, 0)))
15636 || (REG_P (rtl)
15637 && TREE_CODE (decl) == VAR_DECL
15638 && TREE_STATIC (decl))))
15640 rtl = targetm.delegitimize_address (rtl);
15641 return rtl;
15643 rtl = NULL_RTX;
15645 else if (TREE_CODE (decl) == PARM_DECL)
15647 if (rtl == NULL_RTX
15648 || is_pseudo_reg (rtl)
15649 || (MEM_P (rtl)
15650 && is_pseudo_reg (XEXP (rtl, 0))
15651 && DECL_INCOMING_RTL (decl)
15652 && MEM_P (DECL_INCOMING_RTL (decl))
15653 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15655 tree declared_type = TREE_TYPE (decl);
15656 tree passed_type = DECL_ARG_TYPE (decl);
15657 machine_mode dmode = TYPE_MODE (declared_type);
15658 machine_mode pmode = TYPE_MODE (passed_type);
15660 /* This decl represents a formal parameter which was optimized out.
15661 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15662 all cases where (rtl == NULL_RTX) just below. */
15663 if (dmode == pmode)
15664 rtl = DECL_INCOMING_RTL (decl);
15665 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15666 && SCALAR_INT_MODE_P (dmode)
15667 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15668 && DECL_INCOMING_RTL (decl))
15670 rtx inc = DECL_INCOMING_RTL (decl);
15671 if (REG_P (inc))
15672 rtl = inc;
15673 else if (MEM_P (inc))
15675 if (BYTES_BIG_ENDIAN)
15676 rtl = adjust_address_nv (inc, dmode,
15677 GET_MODE_SIZE (pmode)
15678 - GET_MODE_SIZE (dmode));
15679 else
15680 rtl = inc;
15685 /* If the parm was passed in registers, but lives on the stack, then
15686 make a big endian correction if the mode of the type of the
15687 parameter is not the same as the mode of the rtl. */
15688 /* ??? This is the same series of checks that are made in dbxout.c before
15689 we reach the big endian correction code there. It isn't clear if all
15690 of these checks are necessary here, but keeping them all is the safe
15691 thing to do. */
15692 else if (MEM_P (rtl)
15693 && XEXP (rtl, 0) != const0_rtx
15694 && ! CONSTANT_P (XEXP (rtl, 0))
15695 /* Not passed in memory. */
15696 && !MEM_P (DECL_INCOMING_RTL (decl))
15697 /* Not passed by invisible reference. */
15698 && (!REG_P (XEXP (rtl, 0))
15699 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15700 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15701 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15702 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15703 #endif
15705 /* Big endian correction check. */
15706 && BYTES_BIG_ENDIAN
15707 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15708 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15709 < UNITS_PER_WORD))
15711 machine_mode addr_mode = get_address_mode (rtl);
15712 int offset = (UNITS_PER_WORD
15713 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15715 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15716 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15719 else if (TREE_CODE (decl) == VAR_DECL
15720 && rtl
15721 && MEM_P (rtl)
15722 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15723 && BYTES_BIG_ENDIAN)
15725 machine_mode addr_mode = get_address_mode (rtl);
15726 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15727 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15729 /* If a variable is declared "register" yet is smaller than
15730 a register, then if we store the variable to memory, it
15731 looks like we're storing a register-sized value, when in
15732 fact we are not. We need to adjust the offset of the
15733 storage location to reflect the actual value's bytes,
15734 else gdb will not be able to display it. */
15735 if (rsize > dsize)
15736 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15737 plus_constant (addr_mode, XEXP (rtl, 0),
15738 rsize - dsize));
15741 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15742 and will have been substituted directly into all expressions that use it.
15743 C does not have such a concept, but C++ and other languages do. */
15744 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15745 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15747 if (rtl)
15748 rtl = targetm.delegitimize_address (rtl);
15750 /* If we don't look past the constant pool, we risk emitting a
15751 reference to a constant pool entry that isn't referenced from
15752 code, and thus is not emitted. */
15753 if (rtl)
15754 rtl = avoid_constant_pool_reference (rtl);
15756 /* Try harder to get a rtl. If this symbol ends up not being emitted
15757 in the current CU, resolve_addr will remove the expression referencing
15758 it. */
15759 if (rtl == NULL_RTX
15760 && TREE_CODE (decl) == VAR_DECL
15761 && !DECL_EXTERNAL (decl)
15762 && TREE_STATIC (decl)
15763 && DECL_NAME (decl)
15764 && !DECL_HARD_REGISTER (decl)
15765 && DECL_MODE (decl) != VOIDmode)
15767 rtl = make_decl_rtl_for_debug (decl);
15768 if (!MEM_P (rtl)
15769 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15770 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15771 rtl = NULL_RTX;
15774 return rtl;
15777 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15778 returned. If so, the decl for the COMMON block is returned, and the
15779 value is the offset into the common block for the symbol. */
15781 static tree
15782 fortran_common (tree decl, HOST_WIDE_INT *value)
15784 tree val_expr, cvar;
15785 machine_mode mode;
15786 HOST_WIDE_INT bitsize, bitpos;
15787 tree offset;
15788 int unsignedp, volatilep = 0;
15790 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15791 it does not have a value (the offset into the common area), or if it
15792 is thread local (as opposed to global) then it isn't common, and shouldn't
15793 be handled as such. */
15794 if (TREE_CODE (decl) != VAR_DECL
15795 || !TREE_STATIC (decl)
15796 || !DECL_HAS_VALUE_EXPR_P (decl)
15797 || !is_fortran ())
15798 return NULL_TREE;
15800 val_expr = DECL_VALUE_EXPR (decl);
15801 if (TREE_CODE (val_expr) != COMPONENT_REF)
15802 return NULL_TREE;
15804 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15805 &mode, &unsignedp, &volatilep, true);
15807 if (cvar == NULL_TREE
15808 || TREE_CODE (cvar) != VAR_DECL
15809 || DECL_ARTIFICIAL (cvar)
15810 || !TREE_PUBLIC (cvar))
15811 return NULL_TREE;
15813 *value = 0;
15814 if (offset != NULL)
15816 if (!tree_fits_shwi_p (offset))
15817 return NULL_TREE;
15818 *value = tree_to_shwi (offset);
15820 if (bitpos != 0)
15821 *value += bitpos / BITS_PER_UNIT;
15823 return cvar;
15826 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15827 data attribute for a variable or a parameter. We generate the
15828 DW_AT_const_value attribute only in those cases where the given variable
15829 or parameter does not have a true "location" either in memory or in a
15830 register. This can happen (for example) when a constant is passed as an
15831 actual argument in a call to an inline function. (It's possible that
15832 these things can crop up in other ways also.) Note that one type of
15833 constant value which can be passed into an inlined function is a constant
15834 pointer. This can happen for example if an actual argument in an inlined
15835 function call evaluates to a compile-time constant address.
15837 CACHE_P is true if it is worth caching the location list for DECL,
15838 so that future calls can reuse it rather than regenerate it from scratch.
15839 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15840 since we will need to refer to them each time the function is inlined. */
15842 static bool
15843 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15844 enum dwarf_attribute attr)
15846 rtx rtl;
15847 dw_loc_list_ref list;
15848 var_loc_list *loc_list;
15849 cached_dw_loc_list *cache;
15851 if (TREE_CODE (decl) == ERROR_MARK)
15852 return false;
15854 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15855 || TREE_CODE (decl) == RESULT_DECL);
15857 /* Try to get some constant RTL for this decl, and use that as the value of
15858 the location. */
15860 rtl = rtl_for_decl_location (decl);
15861 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15862 && add_const_value_attribute (die, rtl))
15863 return true;
15865 /* See if we have single element location list that is equivalent to
15866 a constant value. That way we are better to use add_const_value_attribute
15867 rather than expanding constant value equivalent. */
15868 loc_list = lookup_decl_loc (decl);
15869 if (loc_list
15870 && loc_list->first
15871 && loc_list->first->next == NULL
15872 && NOTE_P (loc_list->first->loc)
15873 && NOTE_VAR_LOCATION (loc_list->first->loc)
15874 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15876 struct var_loc_node *node;
15878 node = loc_list->first;
15879 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15880 if (GET_CODE (rtl) == EXPR_LIST)
15881 rtl = XEXP (rtl, 0);
15882 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15883 && add_const_value_attribute (die, rtl))
15884 return true;
15886 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15887 list several times. See if we've already cached the contents. */
15888 list = NULL;
15889 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15890 cache_p = false;
15891 if (cache_p)
15893 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
15894 if (cache)
15895 list = cache->loc_list;
15897 if (list == NULL)
15899 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15900 /* It is usually worth caching this result if the decl is from
15901 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15902 if (cache_p && list && list->dw_loc_next)
15904 cached_dw_loc_list **slot
15905 = cached_dw_loc_list_table->find_slot_with_hash (decl,
15906 DECL_UID (decl),
15907 INSERT);
15908 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15909 cache->decl_id = DECL_UID (decl);
15910 cache->loc_list = list;
15911 *slot = cache;
15914 if (list)
15916 add_AT_location_description (die, attr, list);
15917 return true;
15919 /* None of that worked, so it must not really have a location;
15920 try adding a constant value attribute from the DECL_INITIAL. */
15921 return tree_add_const_value_attribute_for_decl (die, decl);
15924 /* Add VARIABLE and DIE into deferred locations list. */
15926 static void
15927 defer_location (tree variable, dw_die_ref die)
15929 deferred_locations entry;
15930 entry.variable = variable;
15931 entry.die = die;
15932 vec_safe_push (deferred_locations_list, entry);
15935 /* Helper function for tree_add_const_value_attribute. Natively encode
15936 initializer INIT into an array. Return true if successful. */
15938 static bool
15939 native_encode_initializer (tree init, unsigned char *array, int size)
15941 tree type;
15943 if (init == NULL_TREE)
15944 return false;
15946 STRIP_NOPS (init);
15947 switch (TREE_CODE (init))
15949 case STRING_CST:
15950 type = TREE_TYPE (init);
15951 if (TREE_CODE (type) == ARRAY_TYPE)
15953 tree enttype = TREE_TYPE (type);
15954 machine_mode mode = TYPE_MODE (enttype);
15956 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15957 return false;
15958 if (int_size_in_bytes (type) != size)
15959 return false;
15960 if (size > TREE_STRING_LENGTH (init))
15962 memcpy (array, TREE_STRING_POINTER (init),
15963 TREE_STRING_LENGTH (init));
15964 memset (array + TREE_STRING_LENGTH (init),
15965 '\0', size - TREE_STRING_LENGTH (init));
15967 else
15968 memcpy (array, TREE_STRING_POINTER (init), size);
15969 return true;
15971 return false;
15972 case CONSTRUCTOR:
15973 type = TREE_TYPE (init);
15974 if (int_size_in_bytes (type) != size)
15975 return false;
15976 if (TREE_CODE (type) == ARRAY_TYPE)
15978 HOST_WIDE_INT min_index;
15979 unsigned HOST_WIDE_INT cnt;
15980 int curpos = 0, fieldsize;
15981 constructor_elt *ce;
15983 if (TYPE_DOMAIN (type) == NULL_TREE
15984 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15985 return false;
15987 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15988 if (fieldsize <= 0)
15989 return false;
15991 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15992 memset (array, '\0', size);
15993 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15995 tree val = ce->value;
15996 tree index = ce->index;
15997 int pos = curpos;
15998 if (index && TREE_CODE (index) == RANGE_EXPR)
15999 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16000 * fieldsize;
16001 else if (index)
16002 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16004 if (val)
16006 STRIP_NOPS (val);
16007 if (!native_encode_initializer (val, array + pos, fieldsize))
16008 return false;
16010 curpos = pos + fieldsize;
16011 if (index && TREE_CODE (index) == RANGE_EXPR)
16013 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16014 - tree_to_shwi (TREE_OPERAND (index, 0));
16015 while (count-- > 0)
16017 if (val)
16018 memcpy (array + curpos, array + pos, fieldsize);
16019 curpos += fieldsize;
16022 gcc_assert (curpos <= size);
16024 return true;
16026 else if (TREE_CODE (type) == RECORD_TYPE
16027 || TREE_CODE (type) == UNION_TYPE)
16029 tree field = NULL_TREE;
16030 unsigned HOST_WIDE_INT cnt;
16031 constructor_elt *ce;
16033 if (int_size_in_bytes (type) != size)
16034 return false;
16036 if (TREE_CODE (type) == RECORD_TYPE)
16037 field = TYPE_FIELDS (type);
16039 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16041 tree val = ce->value;
16042 int pos, fieldsize;
16044 if (ce->index != 0)
16045 field = ce->index;
16047 if (val)
16048 STRIP_NOPS (val);
16050 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16051 return false;
16053 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16054 && TYPE_DOMAIN (TREE_TYPE (field))
16055 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16056 return false;
16057 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16058 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16059 return false;
16060 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16061 pos = int_byte_position (field);
16062 gcc_assert (pos + fieldsize <= size);
16063 if (val
16064 && !native_encode_initializer (val, array + pos, fieldsize))
16065 return false;
16067 return true;
16069 return false;
16070 case VIEW_CONVERT_EXPR:
16071 case NON_LVALUE_EXPR:
16072 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16073 default:
16074 return native_encode_expr (init, array, size) == size;
16078 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16079 attribute is the const value T. */
16081 static bool
16082 tree_add_const_value_attribute (dw_die_ref die, tree t)
16084 tree init;
16085 tree type = TREE_TYPE (t);
16086 rtx rtl;
16088 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16089 return false;
16091 init = t;
16092 gcc_assert (!DECL_P (init));
16094 rtl = rtl_for_decl_init (init, type);
16095 if (rtl)
16096 return add_const_value_attribute (die, rtl);
16097 /* If the host and target are sane, try harder. */
16098 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16099 && initializer_constant_valid_p (init, type))
16101 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16102 if (size > 0 && (int) size == size)
16104 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16106 if (native_encode_initializer (init, array, size))
16108 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16109 return true;
16111 ggc_free (array);
16114 return false;
16117 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16118 attribute is the const value of T, where T is an integral constant
16119 variable with static storage duration
16120 (so it can't be a PARM_DECL or a RESULT_DECL). */
16122 static bool
16123 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16126 if (!decl
16127 || (TREE_CODE (decl) != VAR_DECL
16128 && TREE_CODE (decl) != CONST_DECL)
16129 || (TREE_CODE (decl) == VAR_DECL
16130 && !TREE_STATIC (decl)))
16131 return false;
16133 if (TREE_READONLY (decl)
16134 && ! TREE_THIS_VOLATILE (decl)
16135 && DECL_INITIAL (decl))
16136 /* OK */;
16137 else
16138 return false;
16140 /* Don't add DW_AT_const_value if abstract origin already has one. */
16141 if (get_AT (var_die, DW_AT_const_value))
16142 return false;
16144 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16147 /* Convert the CFI instructions for the current function into a
16148 location list. This is used for DW_AT_frame_base when we targeting
16149 a dwarf2 consumer that does not support the dwarf3
16150 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16151 expressions. */
16153 static dw_loc_list_ref
16154 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16156 int ix;
16157 dw_fde_ref fde;
16158 dw_loc_list_ref list, *list_tail;
16159 dw_cfi_ref cfi;
16160 dw_cfa_location last_cfa, next_cfa;
16161 const char *start_label, *last_label, *section;
16162 dw_cfa_location remember;
16164 fde = cfun->fde;
16165 gcc_assert (fde != NULL);
16167 section = secname_for_decl (current_function_decl);
16168 list_tail = &list;
16169 list = NULL;
16171 memset (&next_cfa, 0, sizeof (next_cfa));
16172 next_cfa.reg = INVALID_REGNUM;
16173 remember = next_cfa;
16175 start_label = fde->dw_fde_begin;
16177 /* ??? Bald assumption that the CIE opcode list does not contain
16178 advance opcodes. */
16179 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16180 lookup_cfa_1 (cfi, &next_cfa, &remember);
16182 last_cfa = next_cfa;
16183 last_label = start_label;
16185 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16187 /* If the first partition contained no CFI adjustments, the
16188 CIE opcodes apply to the whole first partition. */
16189 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16190 fde->dw_fde_begin, fde->dw_fde_end, section);
16191 list_tail =&(*list_tail)->dw_loc_next;
16192 start_label = last_label = fde->dw_fde_second_begin;
16195 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16197 switch (cfi->dw_cfi_opc)
16199 case DW_CFA_set_loc:
16200 case DW_CFA_advance_loc1:
16201 case DW_CFA_advance_loc2:
16202 case DW_CFA_advance_loc4:
16203 if (!cfa_equal_p (&last_cfa, &next_cfa))
16205 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16206 start_label, last_label, section);
16208 list_tail = &(*list_tail)->dw_loc_next;
16209 last_cfa = next_cfa;
16210 start_label = last_label;
16212 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16213 break;
16215 case DW_CFA_advance_loc:
16216 /* The encoding is complex enough that we should never emit this. */
16217 gcc_unreachable ();
16219 default:
16220 lookup_cfa_1 (cfi, &next_cfa, &remember);
16221 break;
16223 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16225 if (!cfa_equal_p (&last_cfa, &next_cfa))
16227 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16228 start_label, last_label, section);
16230 list_tail = &(*list_tail)->dw_loc_next;
16231 last_cfa = next_cfa;
16232 start_label = last_label;
16234 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16235 start_label, fde->dw_fde_end, section);
16236 list_tail = &(*list_tail)->dw_loc_next;
16237 start_label = last_label = fde->dw_fde_second_begin;
16241 if (!cfa_equal_p (&last_cfa, &next_cfa))
16243 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16244 start_label, last_label, section);
16245 list_tail = &(*list_tail)->dw_loc_next;
16246 start_label = last_label;
16249 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16250 start_label,
16251 fde->dw_fde_second_begin
16252 ? fde->dw_fde_second_end : fde->dw_fde_end,
16253 section);
16255 if (list && list->dw_loc_next)
16256 gen_llsym (list);
16258 return list;
16261 /* Compute a displacement from the "steady-state frame pointer" to the
16262 frame base (often the same as the CFA), and store it in
16263 frame_pointer_fb_offset. OFFSET is added to the displacement
16264 before the latter is negated. */
16266 static void
16267 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16269 rtx reg, elim;
16271 #ifdef FRAME_POINTER_CFA_OFFSET
16272 reg = frame_pointer_rtx;
16273 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16274 #else
16275 reg = arg_pointer_rtx;
16276 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16277 #endif
16279 elim = (ira_use_lra_p
16280 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16281 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16282 if (GET_CODE (elim) == PLUS)
16284 offset += INTVAL (XEXP (elim, 1));
16285 elim = XEXP (elim, 0);
16288 frame_pointer_fb_offset = -offset;
16290 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16291 in which to eliminate. This is because it's stack pointer isn't
16292 directly accessible as a register within the ISA. To work around
16293 this, assume that while we cannot provide a proper value for
16294 frame_pointer_fb_offset, we won't need one either. */
16295 frame_pointer_fb_offset_valid
16296 = ((SUPPORTS_STACK_ALIGNMENT
16297 && (elim == hard_frame_pointer_rtx
16298 || elim == stack_pointer_rtx))
16299 || elim == (frame_pointer_needed
16300 ? hard_frame_pointer_rtx
16301 : stack_pointer_rtx));
16304 /* Generate a DW_AT_name attribute given some string value to be included as
16305 the value of the attribute. */
16307 static void
16308 add_name_attribute (dw_die_ref die, const char *name_string)
16310 if (name_string != NULL && *name_string != 0)
16312 if (demangle_name_func)
16313 name_string = (*demangle_name_func) (name_string);
16315 add_AT_string (die, DW_AT_name, name_string);
16319 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16320 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16321 of TYPE accordingly.
16323 ??? This is a temporary measure until after we're able to generate
16324 regular DWARF for the complex Ada type system. */
16326 static void
16327 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16328 dw_die_ref context_die)
16330 tree dtype;
16331 dw_die_ref dtype_die;
16333 if (!lang_hooks.types.descriptive_type)
16334 return;
16336 dtype = lang_hooks.types.descriptive_type (type);
16337 if (!dtype)
16338 return;
16340 dtype_die = lookup_type_die (dtype);
16341 if (!dtype_die)
16343 gen_type_die (dtype, context_die);
16344 dtype_die = lookup_type_die (dtype);
16345 gcc_assert (dtype_die);
16348 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16351 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16353 static const char *
16354 comp_dir_string (void)
16356 const char *wd;
16357 char *wd1;
16358 static const char *cached_wd = NULL;
16360 if (cached_wd != NULL)
16361 return cached_wd;
16363 wd = get_src_pwd ();
16364 if (wd == NULL)
16365 return NULL;
16367 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16369 int wdlen;
16371 wdlen = strlen (wd);
16372 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16373 strcpy (wd1, wd);
16374 wd1 [wdlen] = DIR_SEPARATOR;
16375 wd1 [wdlen + 1] = 0;
16376 wd = wd1;
16379 cached_wd = remap_debug_filename (wd);
16380 return cached_wd;
16383 /* Generate a DW_AT_comp_dir attribute for DIE. */
16385 static void
16386 add_comp_dir_attribute (dw_die_ref die)
16388 const char * wd = comp_dir_string ();
16389 if (wd != NULL)
16390 add_AT_string (die, DW_AT_comp_dir, wd);
16393 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16394 default. */
16396 static int
16397 lower_bound_default (void)
16399 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16401 case DW_LANG_C:
16402 case DW_LANG_C89:
16403 case DW_LANG_C99:
16404 case DW_LANG_C_plus_plus:
16405 case DW_LANG_ObjC:
16406 case DW_LANG_ObjC_plus_plus:
16407 case DW_LANG_Java:
16408 return 0;
16409 case DW_LANG_Fortran77:
16410 case DW_LANG_Fortran90:
16411 case DW_LANG_Fortran95:
16412 return 1;
16413 case DW_LANG_UPC:
16414 case DW_LANG_D:
16415 case DW_LANG_Python:
16416 return dwarf_version >= 4 ? 0 : -1;
16417 case DW_LANG_Ada95:
16418 case DW_LANG_Ada83:
16419 case DW_LANG_Cobol74:
16420 case DW_LANG_Cobol85:
16421 case DW_LANG_Pascal83:
16422 case DW_LANG_Modula2:
16423 case DW_LANG_PLI:
16424 return dwarf_version >= 4 ? 1 : -1;
16425 default:
16426 return -1;
16430 /* Given a tree node describing an array bound (either lower or upper) output
16431 a representation for that bound. */
16433 static void
16434 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16436 switch (TREE_CODE (bound))
16438 case ERROR_MARK:
16439 return;
16441 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16442 case INTEGER_CST:
16444 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16445 int dflt;
16447 /* Use the default if possible. */
16448 if (bound_attr == DW_AT_lower_bound
16449 && tree_fits_shwi_p (bound)
16450 && (dflt = lower_bound_default ()) != -1
16451 && tree_to_shwi (bound) == dflt)
16454 /* If HOST_WIDE_INT is big enough then represent the bound as
16455 a constant value. We need to choose a form based on
16456 whether the type is signed or unsigned. We cannot just
16457 call add_AT_unsigned if the value itself is positive
16458 (add_AT_unsigned might add the unsigned value encoded as
16459 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16460 bounds type and then sign extend any unsigned values found
16461 for signed types. This is needed only for
16462 DW_AT_{lower,upper}_bound, since for most other attributes,
16463 consumers will treat DW_FORM_data[1248] as unsigned values,
16464 regardless of the underlying type. */
16465 else if (prec <= HOST_BITS_PER_WIDE_INT
16466 || tree_fits_uhwi_p (bound))
16468 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16469 add_AT_unsigned (subrange_die, bound_attr,
16470 TREE_INT_CST_LOW (bound));
16471 else
16472 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16474 else
16475 /* Otherwise represent the bound as an unsigned value with
16476 the precision of its type. The precision and signedness
16477 of the type will be necessary to re-interpret it
16478 unambiguously. */
16479 add_AT_wide (subrange_die, bound_attr, bound);
16481 break;
16483 CASE_CONVERT:
16484 case VIEW_CONVERT_EXPR:
16485 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16486 break;
16488 case SAVE_EXPR:
16489 break;
16491 case VAR_DECL:
16492 case PARM_DECL:
16493 case RESULT_DECL:
16495 dw_die_ref decl_die = lookup_decl_die (bound);
16497 /* ??? Can this happen, or should the variable have been bound
16498 first? Probably it can, since I imagine that we try to create
16499 the types of parameters in the order in which they exist in
16500 the list, and won't have created a forward reference to a
16501 later parameter. */
16502 if (decl_die != NULL)
16504 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16505 break;
16508 /* FALLTHRU */
16510 default:
16512 /* Otherwise try to create a stack operation procedure to
16513 evaluate the value of the array bound. */
16515 dw_die_ref ctx, decl_die;
16516 dw_loc_list_ref list;
16518 list = loc_list_from_tree (bound, 2);
16519 if (list == NULL || single_element_loc_list_p (list))
16521 /* If DW_AT_*bound is not a reference nor constant, it is
16522 a DWARF expression rather than location description.
16523 For that loc_list_from_tree (bound, 0) is needed.
16524 If that fails to give a single element list,
16525 fall back to outputting this as a reference anyway. */
16526 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16527 if (list2 && single_element_loc_list_p (list2))
16529 add_AT_loc (subrange_die, bound_attr, list2->expr);
16530 break;
16533 if (list == NULL)
16534 break;
16536 if (current_function_decl == 0)
16537 ctx = comp_unit_die ();
16538 else
16539 ctx = lookup_decl_die (current_function_decl);
16541 decl_die = new_die (DW_TAG_variable, ctx, bound);
16542 add_AT_flag (decl_die, DW_AT_artificial, 1);
16543 add_type_attribute (decl_die, TREE_TYPE (bound), TYPE_QUAL_CONST, ctx);
16544 add_AT_location_description (decl_die, DW_AT_location, list);
16545 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16546 break;
16551 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16552 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16553 Note that the block of subscript information for an array type also
16554 includes information about the element type of the given array type. */
16556 static void
16557 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16559 unsigned dimension_number;
16560 tree lower, upper;
16561 dw_die_ref subrange_die;
16563 for (dimension_number = 0;
16564 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16565 type = TREE_TYPE (type), dimension_number++)
16567 tree domain = TYPE_DOMAIN (type);
16569 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16570 break;
16572 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16573 and (in GNU C only) variable bounds. Handle all three forms
16574 here. */
16575 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16576 if (domain)
16578 /* We have an array type with specified bounds. */
16579 lower = TYPE_MIN_VALUE (domain);
16580 upper = TYPE_MAX_VALUE (domain);
16582 /* Define the index type. */
16583 if (TREE_TYPE (domain))
16585 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16586 TREE_TYPE field. We can't emit debug info for this
16587 because it is an unnamed integral type. */
16588 if (TREE_CODE (domain) == INTEGER_TYPE
16589 && TYPE_NAME (domain) == NULL_TREE
16590 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16591 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16593 else
16594 add_type_attribute (subrange_die, TREE_TYPE (domain),
16595 TYPE_UNQUALIFIED, type_die);
16598 /* ??? If upper is NULL, the array has unspecified length,
16599 but it does have a lower bound. This happens with Fortran
16600 dimension arr(N:*)
16601 Since the debugger is definitely going to need to know N
16602 to produce useful results, go ahead and output the lower
16603 bound solo, and hope the debugger can cope. */
16605 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16606 if (upper)
16607 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16610 /* Otherwise we have an array type with an unspecified length. The
16611 DWARF-2 spec does not say how to handle this; let's just leave out the
16612 bounds. */
16616 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16618 static void
16619 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16621 dw_die_ref decl_die;
16622 HOST_WIDE_INT size;
16624 switch (TREE_CODE (tree_node))
16626 case ERROR_MARK:
16627 size = 0;
16628 break;
16629 case ENUMERAL_TYPE:
16630 case RECORD_TYPE:
16631 case UNION_TYPE:
16632 case QUAL_UNION_TYPE:
16633 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16634 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16636 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16637 return;
16639 size = int_size_in_bytes (tree_node);
16640 break;
16641 case FIELD_DECL:
16642 /* For a data member of a struct or union, the DW_AT_byte_size is
16643 generally given as the number of bytes normally allocated for an
16644 object of the *declared* type of the member itself. This is true
16645 even for bit-fields. */
16646 size = int_size_in_bytes (field_type (tree_node));
16647 break;
16648 default:
16649 gcc_unreachable ();
16652 /* Note that `size' might be -1 when we get to this point. If it is, that
16653 indicates that the byte size of the entity in question is variable. We
16654 have no good way of expressing this fact in Dwarf at the present time,
16655 when location description was not used by the caller code instead. */
16656 if (size >= 0)
16657 add_AT_unsigned (die, DW_AT_byte_size, size);
16660 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16661 which specifies the distance in bits from the highest order bit of the
16662 "containing object" for the bit-field to the highest order bit of the
16663 bit-field itself.
16665 For any given bit-field, the "containing object" is a hypothetical object
16666 (of some integral or enum type) within which the given bit-field lives. The
16667 type of this hypothetical "containing object" is always the same as the
16668 declared type of the individual bit-field itself. The determination of the
16669 exact location of the "containing object" for a bit-field is rather
16670 complicated. It's handled by the `field_byte_offset' function (above).
16672 Note that it is the size (in bytes) of the hypothetical "containing object"
16673 which will be given in the DW_AT_byte_size attribute for this bit-field.
16674 (See `byte_size_attribute' above). */
16676 static inline void
16677 add_bit_offset_attribute (dw_die_ref die, tree decl)
16679 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16680 tree type = DECL_BIT_FIELD_TYPE (decl);
16681 HOST_WIDE_INT bitpos_int;
16682 HOST_WIDE_INT highest_order_object_bit_offset;
16683 HOST_WIDE_INT highest_order_field_bit_offset;
16684 HOST_WIDE_INT bit_offset;
16686 /* Must be a field and a bit field. */
16687 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16689 /* We can't yet handle bit-fields whose offsets are variable, so if we
16690 encounter such things, just return without generating any attribute
16691 whatsoever. Likewise for variable or too large size. */
16692 if (! tree_fits_shwi_p (bit_position (decl))
16693 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16694 return;
16696 bitpos_int = int_bit_position (decl);
16698 /* Note that the bit offset is always the distance (in bits) from the
16699 highest-order bit of the "containing object" to the highest-order bit of
16700 the bit-field itself. Since the "high-order end" of any object or field
16701 is different on big-endian and little-endian machines, the computation
16702 below must take account of these differences. */
16703 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16704 highest_order_field_bit_offset = bitpos_int;
16706 if (! BYTES_BIG_ENDIAN)
16708 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16709 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16712 bit_offset
16713 = (! BYTES_BIG_ENDIAN
16714 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16715 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16717 if (bit_offset < 0)
16718 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16719 else
16720 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16723 /* For a FIELD_DECL node which represents a bit field, output an attribute
16724 which specifies the length in bits of the given field. */
16726 static inline void
16727 add_bit_size_attribute (dw_die_ref die, tree decl)
16729 /* Must be a field and a bit field. */
16730 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16731 && DECL_BIT_FIELD_TYPE (decl));
16733 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16734 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16737 /* If the compiled language is ANSI C, then add a 'prototyped'
16738 attribute, if arg types are given for the parameters of a function. */
16740 static inline void
16741 add_prototyped_attribute (dw_die_ref die, tree func_type)
16743 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16744 && prototype_p (func_type))
16745 add_AT_flag (die, DW_AT_prototyped, 1);
16748 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16749 by looking in either the type declaration or object declaration
16750 equate table. */
16752 static inline dw_die_ref
16753 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16755 dw_die_ref origin_die = NULL;
16757 if (TREE_CODE (origin) != FUNCTION_DECL)
16759 /* We may have gotten separated from the block for the inlined
16760 function, if we're in an exception handler or some such; make
16761 sure that the abstract function has been written out.
16763 Doing this for nested functions is wrong, however; functions are
16764 distinct units, and our context might not even be inline. */
16765 tree fn = origin;
16767 if (TYPE_P (fn))
16768 fn = TYPE_STUB_DECL (fn);
16770 fn = decl_function_context (fn);
16771 if (fn)
16772 dwarf2out_abstract_function (fn);
16775 if (DECL_P (origin))
16776 origin_die = lookup_decl_die (origin);
16777 else if (TYPE_P (origin))
16778 origin_die = lookup_type_die (origin);
16780 /* XXX: Functions that are never lowered don't always have correct block
16781 trees (in the case of java, they simply have no block tree, in some other
16782 languages). For these functions, there is nothing we can really do to
16783 output correct debug info for inlined functions in all cases. Rather
16784 than die, we'll just produce deficient debug info now, in that we will
16785 have variables without a proper abstract origin. In the future, when all
16786 functions are lowered, we should re-add a gcc_assert (origin_die)
16787 here. */
16789 if (origin_die)
16790 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16791 return origin_die;
16794 /* We do not currently support the pure_virtual attribute. */
16796 static inline void
16797 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16799 if (DECL_VINDEX (func_decl))
16801 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16803 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16804 add_AT_loc (die, DW_AT_vtable_elem_location,
16805 new_loc_descr (DW_OP_constu,
16806 tree_to_shwi (DECL_VINDEX (func_decl)),
16807 0));
16809 /* GNU extension: Record what type this method came from originally. */
16810 if (debug_info_level > DINFO_LEVEL_TERSE
16811 && DECL_CONTEXT (func_decl))
16812 add_AT_die_ref (die, DW_AT_containing_type,
16813 lookup_type_die (DECL_CONTEXT (func_decl)));
16817 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16818 given decl. This used to be a vendor extension until after DWARF 4
16819 standardized it. */
16821 static void
16822 add_linkage_attr (dw_die_ref die, tree decl)
16824 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16826 /* Mimic what assemble_name_raw does with a leading '*'. */
16827 if (name[0] == '*')
16828 name = &name[1];
16830 if (dwarf_version >= 4)
16831 add_AT_string (die, DW_AT_linkage_name, name);
16832 else
16833 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16836 /* Add source coordinate attributes for the given decl. */
16838 static void
16839 add_src_coords_attributes (dw_die_ref die, tree decl)
16841 expanded_location s;
16843 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16844 return;
16845 s = expand_location (DECL_SOURCE_LOCATION (decl));
16846 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16847 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16850 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16852 static void
16853 add_linkage_name (dw_die_ref die, tree decl)
16855 if (debug_info_level > DINFO_LEVEL_NONE
16856 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16857 && TREE_PUBLIC (decl)
16858 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16859 && die->die_tag != DW_TAG_member)
16861 /* Defer until we have an assembler name set. */
16862 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16864 limbo_die_node *asm_name;
16866 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16867 asm_name->die = die;
16868 asm_name->created_for = decl;
16869 asm_name->next = deferred_asm_name;
16870 deferred_asm_name = asm_name;
16872 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16873 add_linkage_attr (die, decl);
16877 /* Add a DW_AT_name attribute and source coordinate attribute for the
16878 given decl, but only if it actually has a name. */
16880 static void
16881 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16883 tree decl_name;
16885 decl_name = DECL_NAME (decl);
16886 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16888 const char *name = dwarf2_name (decl, 0);
16889 if (name)
16890 add_name_attribute (die, name);
16891 if (! DECL_ARTIFICIAL (decl))
16892 add_src_coords_attributes (die, decl);
16894 add_linkage_name (die, decl);
16897 #ifdef VMS_DEBUGGING_INFO
16898 /* Get the function's name, as described by its RTL. This may be different
16899 from the DECL_NAME name used in the source file. */
16900 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16902 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16903 XEXP (DECL_RTL (decl), 0), false);
16904 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16906 #endif /* VMS_DEBUGGING_INFO */
16909 #ifdef VMS_DEBUGGING_INFO
16910 /* Output the debug main pointer die for VMS */
16912 void
16913 dwarf2out_vms_debug_main_pointer (void)
16915 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16916 dw_die_ref die;
16918 /* Allocate the VMS debug main subprogram die. */
16919 die = ggc_cleared_alloc<die_node> ();
16920 die->die_tag = DW_TAG_subprogram;
16921 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16922 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16923 current_function_funcdef_no);
16924 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16926 /* Make it the first child of comp_unit_die (). */
16927 die->die_parent = comp_unit_die ();
16928 if (comp_unit_die ()->die_child)
16930 die->die_sib = comp_unit_die ()->die_child->die_sib;
16931 comp_unit_die ()->die_child->die_sib = die;
16933 else
16935 die->die_sib = die;
16936 comp_unit_die ()->die_child = die;
16939 #endif /* VMS_DEBUGGING_INFO */
16941 /* Push a new declaration scope. */
16943 static void
16944 push_decl_scope (tree scope)
16946 vec_safe_push (decl_scope_table, scope);
16949 /* Pop a declaration scope. */
16951 static inline void
16952 pop_decl_scope (void)
16954 decl_scope_table->pop ();
16957 /* walk_tree helper function for uses_local_type, below. */
16959 static tree
16960 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16962 if (!TYPE_P (*tp))
16963 *walk_subtrees = 0;
16964 else
16966 tree name = TYPE_NAME (*tp);
16967 if (name && DECL_P (name) && decl_function_context (name))
16968 return *tp;
16970 return NULL_TREE;
16973 /* If TYPE involves a function-local type (including a local typedef to a
16974 non-local type), returns that type; otherwise returns NULL_TREE. */
16976 static tree
16977 uses_local_type (tree type)
16979 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16980 return used;
16983 /* Return the DIE for the scope that immediately contains this type.
16984 Non-named types that do not involve a function-local type get global
16985 scope. Named types nested in namespaces or other types get their
16986 containing scope. All other types (i.e. function-local named types) get
16987 the current active scope. */
16989 static dw_die_ref
16990 scope_die_for (tree t, dw_die_ref context_die)
16992 dw_die_ref scope_die = NULL;
16993 tree containing_scope;
16995 /* Non-types always go in the current scope. */
16996 gcc_assert (TYPE_P (t));
16998 /* Use the scope of the typedef, rather than the scope of the type
16999 it refers to. */
17000 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17001 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17002 else
17003 containing_scope = TYPE_CONTEXT (t);
17005 /* Use the containing namespace if there is one. */
17006 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17008 if (context_die == lookup_decl_die (containing_scope))
17009 /* OK */;
17010 else if (debug_info_level > DINFO_LEVEL_TERSE)
17011 context_die = get_context_die (containing_scope);
17012 else
17013 containing_scope = NULL_TREE;
17016 /* Ignore function type "scopes" from the C frontend. They mean that
17017 a tagged type is local to a parmlist of a function declarator, but
17018 that isn't useful to DWARF. */
17019 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17020 containing_scope = NULL_TREE;
17022 if (SCOPE_FILE_SCOPE_P (containing_scope))
17024 /* If T uses a local type keep it local as well, to avoid references
17025 to function-local DIEs from outside the function. */
17026 if (current_function_decl && uses_local_type (t))
17027 scope_die = context_die;
17028 else
17029 scope_die = comp_unit_die ();
17031 else if (TYPE_P (containing_scope))
17033 /* For types, we can just look up the appropriate DIE. */
17034 if (debug_info_level > DINFO_LEVEL_TERSE)
17035 scope_die = get_context_die (containing_scope);
17036 else
17038 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17039 if (scope_die == NULL)
17040 scope_die = comp_unit_die ();
17043 else
17044 scope_die = context_die;
17046 return scope_die;
17049 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17051 static inline int
17052 local_scope_p (dw_die_ref context_die)
17054 for (; context_die; context_die = context_die->die_parent)
17055 if (context_die->die_tag == DW_TAG_inlined_subroutine
17056 || context_die->die_tag == DW_TAG_subprogram)
17057 return 1;
17059 return 0;
17062 /* Returns nonzero if CONTEXT_DIE is a class. */
17064 static inline int
17065 class_scope_p (dw_die_ref context_die)
17067 return (context_die
17068 && (context_die->die_tag == DW_TAG_structure_type
17069 || context_die->die_tag == DW_TAG_class_type
17070 || context_die->die_tag == DW_TAG_interface_type
17071 || context_die->die_tag == DW_TAG_union_type));
17074 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17075 whether or not to treat a DIE in this context as a declaration. */
17077 static inline int
17078 class_or_namespace_scope_p (dw_die_ref context_die)
17080 return (class_scope_p (context_die)
17081 || (context_die && context_die->die_tag == DW_TAG_namespace));
17084 /* Many forms of DIEs require a "type description" attribute. This
17085 routine locates the proper "type descriptor" die for the type given
17086 by 'type' plus any additional qualifiers given by 'cv_quals', and
17087 adds a DW_AT_type attribute below the given die. */
17089 static void
17090 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17091 dw_die_ref context_die)
17093 enum tree_code code = TREE_CODE (type);
17094 dw_die_ref type_die = NULL;
17096 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17097 or fixed-point type, use the inner type. This is because we have no
17098 support for unnamed types in base_type_die. This can happen if this is
17099 an Ada subrange type. Correct solution is emit a subrange type die. */
17100 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17101 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17102 type = TREE_TYPE (type), code = TREE_CODE (type);
17104 if (code == ERROR_MARK
17105 /* Handle a special case. For functions whose return type is void, we
17106 generate *no* type attribute. (Note that no object may have type
17107 `void', so this only applies to function return types). */
17108 || code == VOID_TYPE)
17109 return;
17111 type_die = modified_type_die (type,
17112 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17113 context_die);
17115 if (type_die != NULL)
17116 add_AT_die_ref (object_die, DW_AT_type, type_die);
17119 /* Given an object die, add the calling convention attribute for the
17120 function call type. */
17121 static void
17122 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17124 enum dwarf_calling_convention value = DW_CC_normal;
17126 value = ((enum dwarf_calling_convention)
17127 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17129 if (is_fortran ()
17130 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17132 /* DWARF 2 doesn't provide a way to identify a program's source-level
17133 entry point. DW_AT_calling_convention attributes are only meant
17134 to describe functions' calling conventions. However, lacking a
17135 better way to signal the Fortran main program, we used this for
17136 a long time, following existing custom. Now, DWARF 4 has
17137 DW_AT_main_subprogram, which we add below, but some tools still
17138 rely on the old way, which we thus keep. */
17139 value = DW_CC_program;
17141 if (dwarf_version >= 4 || !dwarf_strict)
17142 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17145 /* Only add the attribute if the backend requests it, and
17146 is not DW_CC_normal. */
17147 if (value && (value != DW_CC_normal))
17148 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17151 /* Given a tree pointer to a struct, class, union, or enum type node, return
17152 a pointer to the (string) tag name for the given type, or zero if the type
17153 was declared without a tag. */
17155 static const char *
17156 type_tag (const_tree type)
17158 const char *name = 0;
17160 if (TYPE_NAME (type) != 0)
17162 tree t = 0;
17164 /* Find the IDENTIFIER_NODE for the type name. */
17165 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17166 && !TYPE_NAMELESS (type))
17167 t = TYPE_NAME (type);
17169 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17170 a TYPE_DECL node, regardless of whether or not a `typedef' was
17171 involved. */
17172 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17173 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17175 /* We want to be extra verbose. Don't call dwarf_name if
17176 DECL_NAME isn't set. The default hook for decl_printable_name
17177 doesn't like that, and in this context it's correct to return
17178 0, instead of "<anonymous>" or the like. */
17179 if (DECL_NAME (TYPE_NAME (type))
17180 && !DECL_NAMELESS (TYPE_NAME (type)))
17181 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17184 /* Now get the name as a string, or invent one. */
17185 if (!name && t != 0)
17186 name = IDENTIFIER_POINTER (t);
17189 return (name == 0 || *name == '\0') ? 0 : name;
17192 /* Return the type associated with a data member, make a special check
17193 for bit field types. */
17195 static inline tree
17196 member_declared_type (const_tree member)
17198 return (DECL_BIT_FIELD_TYPE (member)
17199 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17202 /* Get the decl's label, as described by its RTL. This may be different
17203 from the DECL_NAME name used in the source file. */
17205 #if 0
17206 static const char *
17207 decl_start_label (tree decl)
17209 rtx x;
17210 const char *fnname;
17212 x = DECL_RTL (decl);
17213 gcc_assert (MEM_P (x));
17215 x = XEXP (x, 0);
17216 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17218 fnname = XSTR (x, 0);
17219 return fnname;
17221 #endif
17223 /* These routines generate the internal representation of the DIE's for
17224 the compilation unit. Debugging information is collected by walking
17225 the declaration trees passed in from dwarf2out_decl(). */
17227 static void
17228 gen_array_type_die (tree type, dw_die_ref context_die)
17230 dw_die_ref scope_die = scope_die_for (type, context_die);
17231 dw_die_ref array_die;
17233 /* GNU compilers represent multidimensional array types as sequences of one
17234 dimensional array types whose element types are themselves array types.
17235 We sometimes squish that down to a single array_type DIE with multiple
17236 subscripts in the Dwarf debugging info. The draft Dwarf specification
17237 say that we are allowed to do this kind of compression in C, because
17238 there is no difference between an array of arrays and a multidimensional
17239 array. We don't do this for Ada to remain as close as possible to the
17240 actual representation, which is especially important against the language
17241 flexibilty wrt arrays of variable size. */
17243 bool collapse_nested_arrays = !is_ada ();
17244 tree element_type;
17246 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17247 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17248 if (TYPE_STRING_FLAG (type)
17249 && TREE_CODE (type) == ARRAY_TYPE
17250 && is_fortran ()
17251 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17253 HOST_WIDE_INT size;
17255 array_die = new_die (DW_TAG_string_type, scope_die, type);
17256 add_name_attribute (array_die, type_tag (type));
17257 equate_type_number_to_die (type, array_die);
17258 size = int_size_in_bytes (type);
17259 if (size >= 0)
17260 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17261 else if (TYPE_DOMAIN (type) != NULL_TREE
17262 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17263 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17265 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17266 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17268 size = int_size_in_bytes (TREE_TYPE (szdecl));
17269 if (loc && size > 0)
17271 add_AT_location_description (array_die, DW_AT_string_length, loc);
17272 if (size != DWARF2_ADDR_SIZE)
17273 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17276 return;
17279 array_die = new_die (DW_TAG_array_type, scope_die, type);
17280 add_name_attribute (array_die, type_tag (type));
17281 equate_type_number_to_die (type, array_die);
17283 if (TREE_CODE (type) == VECTOR_TYPE)
17284 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17286 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17287 if (is_fortran ()
17288 && TREE_CODE (type) == ARRAY_TYPE
17289 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17290 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17291 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17293 #if 0
17294 /* We default the array ordering. SDB will probably do
17295 the right things even if DW_AT_ordering is not present. It's not even
17296 an issue until we start to get into multidimensional arrays anyway. If
17297 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17298 then we'll have to put the DW_AT_ordering attribute back in. (But if
17299 and when we find out that we need to put these in, we will only do so
17300 for multidimensional arrays. */
17301 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17302 #endif
17304 if (TREE_CODE (type) == VECTOR_TYPE)
17306 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17307 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17308 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17309 add_bound_info (subrange_die, DW_AT_upper_bound,
17310 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17312 else
17313 add_subscript_info (array_die, type, collapse_nested_arrays);
17315 /* Add representation of the type of the elements of this array type and
17316 emit the corresponding DIE if we haven't done it already. */
17317 element_type = TREE_TYPE (type);
17318 if (collapse_nested_arrays)
17319 while (TREE_CODE (element_type) == ARRAY_TYPE)
17321 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17322 break;
17323 element_type = TREE_TYPE (element_type);
17326 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17328 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17329 if (TYPE_ARTIFICIAL (type))
17330 add_AT_flag (array_die, DW_AT_artificial, 1);
17332 if (get_AT (array_die, DW_AT_name))
17333 add_pubtype (type, array_die);
17336 static dw_loc_descr_ref
17337 descr_info_loc (tree val, tree base_decl)
17339 HOST_WIDE_INT size;
17340 dw_loc_descr_ref loc, loc2;
17341 enum dwarf_location_atom op;
17343 if (val == base_decl)
17344 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17346 switch (TREE_CODE (val))
17348 CASE_CONVERT:
17349 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17350 case VAR_DECL:
17351 return loc_descriptor_from_tree (val, 0);
17352 case INTEGER_CST:
17353 if (tree_fits_shwi_p (val))
17354 return int_loc_descriptor (tree_to_shwi (val));
17355 break;
17356 case INDIRECT_REF:
17357 size = int_size_in_bytes (TREE_TYPE (val));
17358 if (size < 0)
17359 break;
17360 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17361 if (!loc)
17362 break;
17363 if (size == DWARF2_ADDR_SIZE)
17364 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17365 else
17366 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17367 return loc;
17368 case POINTER_PLUS_EXPR:
17369 case PLUS_EXPR:
17370 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17371 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17373 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17374 if (!loc)
17375 break;
17376 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17378 else
17380 op = DW_OP_plus;
17381 do_binop:
17382 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17383 if (!loc)
17384 break;
17385 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17386 if (!loc2)
17387 break;
17388 add_loc_descr (&loc, loc2);
17389 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17391 return loc;
17392 case MINUS_EXPR:
17393 op = DW_OP_minus;
17394 goto do_binop;
17395 case MULT_EXPR:
17396 op = DW_OP_mul;
17397 goto do_binop;
17398 case EQ_EXPR:
17399 op = DW_OP_eq;
17400 goto do_binop;
17401 case NE_EXPR:
17402 op = DW_OP_ne;
17403 goto do_binop;
17404 default:
17405 break;
17407 return NULL;
17410 static void
17411 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17412 tree val, tree base_decl)
17414 dw_loc_descr_ref loc;
17416 if (tree_fits_shwi_p (val))
17418 add_AT_unsigned (die, attr, tree_to_shwi (val));
17419 return;
17422 loc = descr_info_loc (val, base_decl);
17423 if (!loc)
17424 return;
17426 add_AT_loc (die, attr, loc);
17429 /* This routine generates DIE for array with hidden descriptor, details
17430 are filled into *info by a langhook. */
17432 static void
17433 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17434 dw_die_ref context_die)
17436 dw_die_ref scope_die = scope_die_for (type, context_die);
17437 dw_die_ref array_die;
17438 int dim;
17440 array_die = new_die (DW_TAG_array_type, scope_die, type);
17441 add_name_attribute (array_die, type_tag (type));
17442 equate_type_number_to_die (type, array_die);
17444 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17445 if (is_fortran ()
17446 && info->ndimensions >= 2)
17447 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17449 if (info->data_location)
17450 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17451 info->base_decl);
17452 if (info->associated)
17453 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17454 info->base_decl);
17455 if (info->allocated)
17456 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17457 info->base_decl);
17459 for (dim = 0; dim < info->ndimensions; dim++)
17461 dw_die_ref subrange_die
17462 = new_die (DW_TAG_subrange_type, array_die, NULL);
17464 if (info->dimen[dim].lower_bound)
17466 /* If it is the default value, omit it. */
17467 int dflt;
17469 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17470 && (dflt = lower_bound_default ()) != -1
17471 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17473 else
17474 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17475 info->dimen[dim].lower_bound,
17476 info->base_decl);
17478 if (info->dimen[dim].upper_bound)
17479 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17480 info->dimen[dim].upper_bound,
17481 info->base_decl);
17482 if (info->dimen[dim].stride)
17483 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17484 info->dimen[dim].stride,
17485 info->base_decl);
17488 gen_type_die (info->element_type, context_die);
17489 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17490 context_die);
17492 if (get_AT (array_die, DW_AT_name))
17493 add_pubtype (type, array_die);
17496 #if 0
17497 static void
17498 gen_entry_point_die (tree decl, dw_die_ref context_die)
17500 tree origin = decl_ultimate_origin (decl);
17501 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17503 if (origin != NULL)
17504 add_abstract_origin_attribute (decl_die, origin);
17505 else
17507 add_name_and_src_coords_attributes (decl_die, decl);
17508 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17509 TYPE_UNQUALIFIED, context_die);
17512 if (DECL_ABSTRACT_P (decl))
17513 equate_decl_number_to_die (decl, decl_die);
17514 else
17515 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17517 #endif
17519 /* Walk through the list of incomplete types again, trying once more to
17520 emit full debugging info for them. */
17522 static void
17523 retry_incomplete_types (void)
17525 int i;
17527 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17528 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17529 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17532 /* Determine what tag to use for a record type. */
17534 static enum dwarf_tag
17535 record_type_tag (tree type)
17537 if (! lang_hooks.types.classify_record)
17538 return DW_TAG_structure_type;
17540 switch (lang_hooks.types.classify_record (type))
17542 case RECORD_IS_STRUCT:
17543 return DW_TAG_structure_type;
17545 case RECORD_IS_CLASS:
17546 return DW_TAG_class_type;
17548 case RECORD_IS_INTERFACE:
17549 if (dwarf_version >= 3 || !dwarf_strict)
17550 return DW_TAG_interface_type;
17551 return DW_TAG_structure_type;
17553 default:
17554 gcc_unreachable ();
17558 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17559 include all of the information about the enumeration values also. Each
17560 enumerated type name/value is listed as a child of the enumerated type
17561 DIE. */
17563 static dw_die_ref
17564 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17566 dw_die_ref type_die = lookup_type_die (type);
17568 if (type_die == NULL)
17570 type_die = new_die (DW_TAG_enumeration_type,
17571 scope_die_for (type, context_die), type);
17572 equate_type_number_to_die (type, type_die);
17573 add_name_attribute (type_die, type_tag (type));
17574 if (dwarf_version >= 4 || !dwarf_strict)
17576 if (ENUM_IS_SCOPED (type))
17577 add_AT_flag (type_die, DW_AT_enum_class, 1);
17578 if (ENUM_IS_OPAQUE (type))
17579 add_AT_flag (type_die, DW_AT_declaration, 1);
17582 else if (! TYPE_SIZE (type))
17583 return type_die;
17584 else
17585 remove_AT (type_die, DW_AT_declaration);
17587 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17588 given enum type is incomplete, do not generate the DW_AT_byte_size
17589 attribute or the DW_AT_element_list attribute. */
17590 if (TYPE_SIZE (type))
17592 tree link;
17594 TREE_ASM_WRITTEN (type) = 1;
17595 add_byte_size_attribute (type_die, type);
17596 if (dwarf_version >= 3 || !dwarf_strict)
17598 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17599 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17600 context_die);
17602 if (TYPE_STUB_DECL (type) != NULL_TREE)
17604 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17605 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17608 /* If the first reference to this type was as the return type of an
17609 inline function, then it may not have a parent. Fix this now. */
17610 if (type_die->die_parent == NULL)
17611 add_child_die (scope_die_for (type, context_die), type_die);
17613 for (link = TYPE_VALUES (type);
17614 link != NULL; link = TREE_CHAIN (link))
17616 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17617 tree value = TREE_VALUE (link);
17619 add_name_attribute (enum_die,
17620 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17622 if (TREE_CODE (value) == CONST_DECL)
17623 value = DECL_INITIAL (value);
17625 if (simple_type_size_in_bits (TREE_TYPE (value))
17626 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17628 /* For constant forms created by add_AT_unsigned DWARF
17629 consumers (GDB, elfutils, etc.) always zero extend
17630 the value. Only when the actual value is negative
17631 do we need to use add_AT_int to generate a constant
17632 form that can represent negative values. */
17633 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17634 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17635 add_AT_unsigned (enum_die, DW_AT_const_value,
17636 (unsigned HOST_WIDE_INT) val);
17637 else
17638 add_AT_int (enum_die, DW_AT_const_value, val);
17640 else
17641 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17642 that here. TODO: This should be re-worked to use correct
17643 signed/unsigned double tags for all cases. */
17644 add_AT_wide (enum_die, DW_AT_const_value, value);
17647 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17648 if (TYPE_ARTIFICIAL (type))
17649 add_AT_flag (type_die, DW_AT_artificial, 1);
17651 else
17652 add_AT_flag (type_die, DW_AT_declaration, 1);
17654 add_pubtype (type, type_die);
17656 return type_die;
17659 /* Generate a DIE to represent either a real live formal parameter decl or to
17660 represent just the type of some formal parameter position in some function
17661 type.
17663 Note that this routine is a bit unusual because its argument may be a
17664 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17665 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17666 node. If it's the former then this function is being called to output a
17667 DIE to represent a formal parameter object (or some inlining thereof). If
17668 it's the latter, then this function is only being called to output a
17669 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17670 argument type of some subprogram type.
17671 If EMIT_NAME_P is true, name and source coordinate attributes
17672 are emitted. */
17674 static dw_die_ref
17675 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17676 dw_die_ref context_die)
17678 tree node_or_origin = node ? node : origin;
17679 tree ultimate_origin;
17680 dw_die_ref parm_die
17681 = new_die (DW_TAG_formal_parameter, context_die, node);
17683 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17685 case tcc_declaration:
17686 ultimate_origin = decl_ultimate_origin (node_or_origin);
17687 if (node || ultimate_origin)
17688 origin = ultimate_origin;
17689 if (origin != NULL)
17690 add_abstract_origin_attribute (parm_die, origin);
17691 else if (emit_name_p)
17692 add_name_and_src_coords_attributes (parm_die, node);
17693 if (origin == NULL
17694 || (! DECL_ABSTRACT_P (node_or_origin)
17695 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17696 decl_function_context
17697 (node_or_origin))))
17699 tree type = TREE_TYPE (node_or_origin);
17700 if (decl_by_reference_p (node_or_origin))
17701 add_type_attribute (parm_die, TREE_TYPE (type),
17702 TYPE_UNQUALIFIED, context_die);
17703 else
17704 add_type_attribute (parm_die, type,
17705 decl_quals (node_or_origin),
17706 context_die);
17708 if (origin == NULL && DECL_ARTIFICIAL (node))
17709 add_AT_flag (parm_die, DW_AT_artificial, 1);
17711 if (node && node != origin)
17712 equate_decl_number_to_die (node, parm_die);
17713 if (! DECL_ABSTRACT_P (node_or_origin))
17714 add_location_or_const_value_attribute (parm_die, node_or_origin,
17715 node == NULL, DW_AT_location);
17717 break;
17719 case tcc_type:
17720 /* We were called with some kind of a ..._TYPE node. */
17721 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17722 context_die);
17723 break;
17725 default:
17726 gcc_unreachable ();
17729 return parm_die;
17732 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17733 children DW_TAG_formal_parameter DIEs representing the arguments of the
17734 parameter pack.
17736 PARM_PACK must be a function parameter pack.
17737 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17738 must point to the subsequent arguments of the function PACK_ARG belongs to.
17739 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17740 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17741 following the last one for which a DIE was generated. */
17743 static dw_die_ref
17744 gen_formal_parameter_pack_die (tree parm_pack,
17745 tree pack_arg,
17746 dw_die_ref subr_die,
17747 tree *next_arg)
17749 tree arg;
17750 dw_die_ref parm_pack_die;
17752 gcc_assert (parm_pack
17753 && lang_hooks.function_parameter_pack_p (parm_pack)
17754 && subr_die);
17756 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17757 add_src_coords_attributes (parm_pack_die, parm_pack);
17759 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17761 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17762 parm_pack))
17763 break;
17764 gen_formal_parameter_die (arg, NULL,
17765 false /* Don't emit name attribute. */,
17766 parm_pack_die);
17768 if (next_arg)
17769 *next_arg = arg;
17770 return parm_pack_die;
17773 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17774 at the end of an (ANSI prototyped) formal parameters list. */
17776 static void
17777 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17779 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17782 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17783 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17784 parameters as specified in some function type specification (except for
17785 those which appear as part of a function *definition*). */
17787 static void
17788 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17790 tree link;
17791 tree formal_type = NULL;
17792 tree first_parm_type;
17793 tree arg;
17795 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17797 arg = DECL_ARGUMENTS (function_or_method_type);
17798 function_or_method_type = TREE_TYPE (function_or_method_type);
17800 else
17801 arg = NULL_TREE;
17803 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17805 /* Make our first pass over the list of formal parameter types and output a
17806 DW_TAG_formal_parameter DIE for each one. */
17807 for (link = first_parm_type; link; )
17809 dw_die_ref parm_die;
17811 formal_type = TREE_VALUE (link);
17812 if (formal_type == void_type_node)
17813 break;
17815 /* Output a (nameless) DIE to represent the formal parameter itself. */
17816 parm_die = gen_formal_parameter_die (formal_type, NULL,
17817 true /* Emit name attribute. */,
17818 context_die);
17819 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17820 && link == first_parm_type)
17822 add_AT_flag (parm_die, DW_AT_artificial, 1);
17823 if (dwarf_version >= 3 || !dwarf_strict)
17824 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17826 else if (arg && DECL_ARTIFICIAL (arg))
17827 add_AT_flag (parm_die, DW_AT_artificial, 1);
17829 link = TREE_CHAIN (link);
17830 if (arg)
17831 arg = DECL_CHAIN (arg);
17834 /* If this function type has an ellipsis, add a
17835 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17836 if (formal_type != void_type_node)
17837 gen_unspecified_parameters_die (function_or_method_type, context_die);
17839 /* Make our second (and final) pass over the list of formal parameter types
17840 and output DIEs to represent those types (as necessary). */
17841 for (link = TYPE_ARG_TYPES (function_or_method_type);
17842 link && TREE_VALUE (link);
17843 link = TREE_CHAIN (link))
17844 gen_type_die (TREE_VALUE (link), context_die);
17847 /* We want to generate the DIE for TYPE so that we can generate the
17848 die for MEMBER, which has been defined; we will need to refer back
17849 to the member declaration nested within TYPE. If we're trying to
17850 generate minimal debug info for TYPE, processing TYPE won't do the
17851 trick; we need to attach the member declaration by hand. */
17853 static void
17854 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17856 gen_type_die (type, context_die);
17858 /* If we're trying to avoid duplicate debug info, we may not have
17859 emitted the member decl for this function. Emit it now. */
17860 if (TYPE_STUB_DECL (type)
17861 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17862 && ! lookup_decl_die (member))
17864 dw_die_ref type_die;
17865 gcc_assert (!decl_ultimate_origin (member));
17867 push_decl_scope (type);
17868 type_die = lookup_type_die_strip_naming_typedef (type);
17869 if (TREE_CODE (member) == FUNCTION_DECL)
17870 gen_subprogram_die (member, type_die);
17871 else if (TREE_CODE (member) == FIELD_DECL)
17873 /* Ignore the nameless fields that are used to skip bits but handle
17874 C++ anonymous unions and structs. */
17875 if (DECL_NAME (member) != NULL_TREE
17876 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17877 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17879 gen_type_die (member_declared_type (member), type_die);
17880 gen_field_die (member, type_die);
17883 else
17884 gen_variable_die (member, NULL_TREE, type_die);
17886 pop_decl_scope ();
17890 /* Forward declare these functions, because they are mutually recursive
17891 with their set_block_* pairing functions. */
17892 static void set_decl_origin_self (tree);
17893 static void set_decl_abstract_flags (tree, int);
17895 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17896 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17897 that it points to the node itself, thus indicating that the node is its
17898 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17899 the given node is NULL, recursively descend the decl/block tree which
17900 it is the root of, and for each other ..._DECL or BLOCK node contained
17901 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17902 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17903 values to point to themselves. */
17905 static void
17906 set_block_origin_self (tree stmt)
17908 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17910 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17913 tree local_decl;
17915 for (local_decl = BLOCK_VARS (stmt);
17916 local_decl != NULL_TREE;
17917 local_decl = DECL_CHAIN (local_decl))
17918 if (! DECL_EXTERNAL (local_decl))
17919 set_decl_origin_self (local_decl); /* Potential recursion. */
17923 tree subblock;
17925 for (subblock = BLOCK_SUBBLOCKS (stmt);
17926 subblock != NULL_TREE;
17927 subblock = BLOCK_CHAIN (subblock))
17928 set_block_origin_self (subblock); /* Recurse. */
17933 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17934 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17935 node to so that it points to the node itself, thus indicating that the
17936 node represents its own (abstract) origin. Additionally, if the
17937 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17938 the decl/block tree of which the given node is the root of, and for
17939 each other ..._DECL or BLOCK node contained therein whose
17940 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17941 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17942 point to themselves. */
17944 static void
17945 set_decl_origin_self (tree decl)
17947 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17949 DECL_ABSTRACT_ORIGIN (decl) = decl;
17950 if (TREE_CODE (decl) == FUNCTION_DECL)
17952 tree arg;
17954 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17955 DECL_ABSTRACT_ORIGIN (arg) = arg;
17956 if (DECL_INITIAL (decl) != NULL_TREE
17957 && DECL_INITIAL (decl) != error_mark_node)
17958 set_block_origin_self (DECL_INITIAL (decl));
17963 /* Given a pointer to some BLOCK node, and a boolean value to set the
17964 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17965 the given block, and for all local decls and all local sub-blocks
17966 (recursively) which are contained therein. */
17968 static void
17969 set_block_abstract_flags (tree stmt, int setting)
17971 tree local_decl;
17972 tree subblock;
17973 unsigned int i;
17975 BLOCK_ABSTRACT (stmt) = setting;
17977 for (local_decl = BLOCK_VARS (stmt);
17978 local_decl != NULL_TREE;
17979 local_decl = DECL_CHAIN (local_decl))
17980 if (! DECL_EXTERNAL (local_decl))
17981 set_decl_abstract_flags (local_decl, setting);
17983 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17985 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17986 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17987 || TREE_CODE (local_decl) == PARM_DECL)
17988 set_decl_abstract_flags (local_decl, setting);
17991 for (subblock = BLOCK_SUBBLOCKS (stmt);
17992 subblock != NULL_TREE;
17993 subblock = BLOCK_CHAIN (subblock))
17994 set_block_abstract_flags (subblock, setting);
17997 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17998 "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
17999 given decl, and (in the case where the decl is a FUNCTION_DECL) also
18000 set the abstract flags for all of the parameters, local vars, local
18001 blocks and sub-blocks (recursively) to the same setting. */
18003 static void
18004 set_decl_abstract_flags (tree decl, int setting)
18006 DECL_ABSTRACT_P (decl) = setting;
18007 if (TREE_CODE (decl) == FUNCTION_DECL)
18009 tree arg;
18011 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18012 DECL_ABSTRACT_P (arg) = setting;
18013 if (DECL_INITIAL (decl) != NULL_TREE
18014 && DECL_INITIAL (decl) != error_mark_node)
18015 set_block_abstract_flags (DECL_INITIAL (decl), setting);
18019 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18020 may later generate inlined and/or out-of-line instances of. */
18022 static void
18023 dwarf2out_abstract_function (tree decl)
18025 dw_die_ref old_die;
18026 tree save_fn;
18027 tree context;
18028 int was_abstract;
18029 hash_table<decl_loc_hasher> *old_decl_loc_table;
18030 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18031 int old_call_site_count, old_tail_call_site_count;
18032 struct call_arg_loc_node *old_call_arg_locations;
18034 /* Make sure we have the actual abstract inline, not a clone. */
18035 decl = DECL_ORIGIN (decl);
18037 old_die = lookup_decl_die (decl);
18038 if (old_die && get_AT (old_die, DW_AT_inline))
18039 /* We've already generated the abstract instance. */
18040 return;
18042 /* We can be called while recursively when seeing block defining inlined subroutine
18043 DIE. Be sure to not clobber the outer location table nor use it or we would
18044 get locations in abstract instantces. */
18045 old_decl_loc_table = decl_loc_table;
18046 decl_loc_table = NULL;
18047 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18048 cached_dw_loc_list_table = NULL;
18049 old_call_arg_locations = call_arg_locations;
18050 call_arg_locations = NULL;
18051 old_call_site_count = call_site_count;
18052 call_site_count = -1;
18053 old_tail_call_site_count = tail_call_site_count;
18054 tail_call_site_count = -1;
18056 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18057 we don't get confused by DECL_ABSTRACT_P. */
18058 if (debug_info_level > DINFO_LEVEL_TERSE)
18060 context = decl_class_context (decl);
18061 if (context)
18062 gen_type_die_for_member
18063 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18066 /* Pretend we've just finished compiling this function. */
18067 save_fn = current_function_decl;
18068 current_function_decl = decl;
18070 was_abstract = DECL_ABSTRACT_P (decl);
18071 set_decl_abstract_flags (decl, 1);
18072 dwarf2out_decl (decl);
18073 if (! was_abstract)
18074 set_decl_abstract_flags (decl, 0);
18076 current_function_decl = save_fn;
18077 decl_loc_table = old_decl_loc_table;
18078 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18079 call_arg_locations = old_call_arg_locations;
18080 call_site_count = old_call_site_count;
18081 tail_call_site_count = old_tail_call_site_count;
18084 /* Helper function of premark_used_types() which gets called through
18085 htab_traverse.
18087 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18088 marked as unused by prune_unused_types. */
18090 bool
18091 premark_used_types_helper (tree const &type, void *)
18093 dw_die_ref die;
18095 die = lookup_type_die (type);
18096 if (die != NULL)
18097 die->die_perennial_p = 1;
18098 return true;
18101 /* Helper function of premark_types_used_by_global_vars which gets called
18102 through htab_traverse.
18104 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18105 marked as unused by prune_unused_types. The DIE of the type is marked
18106 only if the global variable using the type will actually be emitted. */
18109 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18110 void *)
18112 struct types_used_by_vars_entry *entry;
18113 dw_die_ref die;
18115 entry = (struct types_used_by_vars_entry *) *slot;
18116 gcc_assert (entry->type != NULL
18117 && entry->var_decl != NULL);
18118 die = lookup_type_die (entry->type);
18119 if (die)
18121 /* Ask cgraph if the global variable really is to be emitted.
18122 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18123 varpool_node *node = varpool_node::get (entry->var_decl);
18124 if (node && node->definition)
18126 die->die_perennial_p = 1;
18127 /* Keep the parent DIEs as well. */
18128 while ((die = die->die_parent) && die->die_perennial_p == 0)
18129 die->die_perennial_p = 1;
18132 return 1;
18135 /* Mark all members of used_types_hash as perennial. */
18137 static void
18138 premark_used_types (struct function *fun)
18140 if (fun && fun->used_types_hash)
18141 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18144 /* Mark all members of types_used_by_vars_entry as perennial. */
18146 static void
18147 premark_types_used_by_global_vars (void)
18149 if (types_used_by_vars_hash)
18150 types_used_by_vars_hash
18151 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18154 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18155 for CA_LOC call arg loc node. */
18157 static dw_die_ref
18158 gen_call_site_die (tree decl, dw_die_ref subr_die,
18159 struct call_arg_loc_node *ca_loc)
18161 dw_die_ref stmt_die = NULL, die;
18162 tree block = ca_loc->block;
18164 while (block
18165 && block != DECL_INITIAL (decl)
18166 && TREE_CODE (block) == BLOCK)
18168 if (block_map.length () > BLOCK_NUMBER (block))
18169 stmt_die = block_map[BLOCK_NUMBER (block)];
18170 if (stmt_die)
18171 break;
18172 block = BLOCK_SUPERCONTEXT (block);
18174 if (stmt_die == NULL)
18175 stmt_die = subr_die;
18176 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18177 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18178 if (ca_loc->tail_call_p)
18179 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18180 if (ca_loc->symbol_ref)
18182 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18183 if (tdie)
18184 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18185 else
18186 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18188 return die;
18191 /* Generate a DIE to represent a declared function (either file-scope or
18192 block-local). */
18194 static void
18195 gen_subprogram_die (tree decl, dw_die_ref context_die)
18197 tree origin = decl_ultimate_origin (decl);
18198 dw_die_ref subr_die;
18199 tree outer_scope;
18200 dw_die_ref old_die = lookup_decl_die (decl);
18201 int declaration = (current_function_decl != decl
18202 || class_or_namespace_scope_p (context_die));
18204 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18206 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18207 started to generate the abstract instance of an inline, decided to output
18208 its containing class, and proceeded to emit the declaration of the inline
18209 from the member list for the class. If so, DECLARATION takes priority;
18210 we'll get back to the abstract instance when done with the class. */
18212 /* The class-scope declaration DIE must be the primary DIE. */
18213 if (origin && declaration && class_or_namespace_scope_p (context_die))
18215 origin = NULL;
18216 gcc_assert (!old_die);
18219 /* Now that the C++ front end lazily declares artificial member fns, we
18220 might need to retrofit the declaration into its class. */
18221 if (!declaration && !origin && !old_die
18222 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18223 && !class_or_namespace_scope_p (context_die)
18224 && debug_info_level > DINFO_LEVEL_TERSE)
18225 old_die = force_decl_die (decl);
18227 if (origin != NULL)
18229 gcc_assert (!declaration || local_scope_p (context_die));
18231 /* Fixup die_parent for the abstract instance of a nested
18232 inline function. */
18233 if (old_die && old_die->die_parent == NULL)
18234 add_child_die (context_die, old_die);
18236 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18237 add_abstract_origin_attribute (subr_die, origin);
18238 /* This is where the actual code for a cloned function is.
18239 Let's emit linkage name attribute for it. This helps
18240 debuggers to e.g, set breakpoints into
18241 constructors/destructors when the user asks "break
18242 K::K". */
18243 add_linkage_name (subr_die, decl);
18245 else if (old_die)
18247 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18248 struct dwarf_file_data * file_index = lookup_filename (s.file);
18250 if (!get_AT_flag (old_die, DW_AT_declaration)
18251 /* We can have a normal definition following an inline one in the
18252 case of redefinition of GNU C extern inlines.
18253 It seems reasonable to use AT_specification in this case. */
18254 && !get_AT (old_die, DW_AT_inline))
18256 /* Detect and ignore this case, where we are trying to output
18257 something we have already output. */
18258 return;
18261 /* If the definition comes from the same place as the declaration,
18262 maybe use the old DIE. We always want the DIE for this function
18263 that has the *_pc attributes to be under comp_unit_die so the
18264 debugger can find it. We also need to do this for abstract
18265 instances of inlines, since the spec requires the out-of-line copy
18266 to have the same parent. For local class methods, this doesn't
18267 apply; we just use the old DIE. */
18268 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18269 && (DECL_ARTIFICIAL (decl)
18270 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18271 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18272 == (unsigned) s.line))))
18274 subr_die = old_die;
18276 /* Clear out the declaration attribute and the formal parameters.
18277 Do not remove all children, because it is possible that this
18278 declaration die was forced using force_decl_die(). In such
18279 cases die that forced declaration die (e.g. TAG_imported_module)
18280 is one of the children that we do not want to remove. */
18281 remove_AT (subr_die, DW_AT_declaration);
18282 remove_AT (subr_die, DW_AT_object_pointer);
18283 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18285 else
18287 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18288 add_AT_specification (subr_die, old_die);
18289 add_pubname (decl, subr_die);
18290 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18291 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18292 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18293 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18295 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18296 emit the real type on the definition die. */
18297 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18299 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18300 if (die == auto_die || die == decltype_auto_die)
18301 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18302 TYPE_UNQUALIFIED, context_die);
18306 else
18308 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18310 if (TREE_PUBLIC (decl))
18311 add_AT_flag (subr_die, DW_AT_external, 1);
18313 add_name_and_src_coords_attributes (subr_die, decl);
18314 add_pubname (decl, subr_die);
18315 if (debug_info_level > DINFO_LEVEL_TERSE)
18317 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18318 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18319 TYPE_UNQUALIFIED, context_die);
18322 add_pure_or_virtual_attribute (subr_die, decl);
18323 if (DECL_ARTIFICIAL (decl))
18324 add_AT_flag (subr_die, DW_AT_artificial, 1);
18326 add_accessibility_attribute (subr_die, decl);
18329 if (declaration)
18331 if (!old_die || !get_AT (old_die, DW_AT_inline))
18333 add_AT_flag (subr_die, DW_AT_declaration, 1);
18335 /* If this is an explicit function declaration then generate
18336 a DW_AT_explicit attribute. */
18337 if (lang_hooks.decls.function_decl_explicit_p (decl)
18338 && (dwarf_version >= 3 || !dwarf_strict))
18339 add_AT_flag (subr_die, DW_AT_explicit, 1);
18341 /* If this is a C++11 deleted special function member then generate
18342 a DW_AT_GNU_deleted attribute. */
18343 if (lang_hooks.decls.function_decl_deleted_p (decl)
18344 && (! dwarf_strict))
18345 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18347 /* The first time we see a member function, it is in the context of
18348 the class to which it belongs. We make sure of this by emitting
18349 the class first. The next time is the definition, which is
18350 handled above. The two may come from the same source text.
18352 Note that force_decl_die() forces function declaration die. It is
18353 later reused to represent definition. */
18354 equate_decl_number_to_die (decl, subr_die);
18357 else if (DECL_ABSTRACT_P (decl))
18359 if (DECL_DECLARED_INLINE_P (decl))
18361 if (cgraph_function_possibly_inlined_p (decl))
18362 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18363 else
18364 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18366 else
18368 if (cgraph_function_possibly_inlined_p (decl))
18369 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18370 else
18371 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18374 if (DECL_DECLARED_INLINE_P (decl)
18375 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18376 add_AT_flag (subr_die, DW_AT_artificial, 1);
18378 equate_decl_number_to_die (decl, subr_die);
18380 else if (!DECL_EXTERNAL (decl))
18382 HOST_WIDE_INT cfa_fb_offset;
18383 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18385 if (!old_die || !get_AT (old_die, DW_AT_inline))
18386 equate_decl_number_to_die (decl, subr_die);
18388 gcc_checking_assert (fun);
18389 if (!flag_reorder_blocks_and_partition)
18391 dw_fde_ref fde = fun->fde;
18392 if (fde->dw_fde_begin)
18394 /* We have already generated the labels. */
18395 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18396 fde->dw_fde_end, false);
18398 else
18400 /* Create start/end labels and add the range. */
18401 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18402 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18403 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18404 current_function_funcdef_no);
18405 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18406 current_function_funcdef_no);
18407 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18408 false);
18411 #if VMS_DEBUGGING_INFO
18412 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18413 Section 2.3 Prologue and Epilogue Attributes:
18414 When a breakpoint is set on entry to a function, it is generally
18415 desirable for execution to be suspended, not on the very first
18416 instruction of the function, but rather at a point after the
18417 function's frame has been set up, after any language defined local
18418 declaration processing has been completed, and before execution of
18419 the first statement of the function begins. Debuggers generally
18420 cannot properly determine where this point is. Similarly for a
18421 breakpoint set on exit from a function. The prologue and epilogue
18422 attributes allow a compiler to communicate the location(s) to use. */
18425 if (fde->dw_fde_vms_end_prologue)
18426 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18427 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18429 if (fde->dw_fde_vms_begin_epilogue)
18430 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18431 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18433 #endif
18436 else
18438 /* Generate pubnames entries for the split function code ranges. */
18439 dw_fde_ref fde = fun->fde;
18441 if (fde->dw_fde_second_begin)
18443 if (dwarf_version >= 3 || !dwarf_strict)
18445 /* We should use ranges for non-contiguous code section
18446 addresses. Use the actual code range for the initial
18447 section, since the HOT/COLD labels might precede an
18448 alignment offset. */
18449 bool range_list_added = false;
18450 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18451 fde->dw_fde_end, &range_list_added,
18452 false);
18453 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18454 fde->dw_fde_second_end,
18455 &range_list_added, false);
18456 if (range_list_added)
18457 add_ranges (NULL);
18459 else
18461 /* There is no real support in DW2 for this .. so we make
18462 a work-around. First, emit the pub name for the segment
18463 containing the function label. Then make and emit a
18464 simplified subprogram DIE for the second segment with the
18465 name pre-fixed by __hot/cold_sect_of_. We use the same
18466 linkage name for the second die so that gdb will find both
18467 sections when given "b foo". */
18468 const char *name = NULL;
18469 tree decl_name = DECL_NAME (decl);
18470 dw_die_ref seg_die;
18472 /* Do the 'primary' section. */
18473 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18474 fde->dw_fde_end, false);
18476 /* Build a minimal DIE for the secondary section. */
18477 seg_die = new_die (DW_TAG_subprogram,
18478 subr_die->die_parent, decl);
18480 if (TREE_PUBLIC (decl))
18481 add_AT_flag (seg_die, DW_AT_external, 1);
18483 if (decl_name != NULL
18484 && IDENTIFIER_POINTER (decl_name) != NULL)
18486 name = dwarf2_name (decl, 1);
18487 if (! DECL_ARTIFICIAL (decl))
18488 add_src_coords_attributes (seg_die, decl);
18490 add_linkage_name (seg_die, decl);
18492 gcc_assert (name != NULL);
18493 add_pure_or_virtual_attribute (seg_die, decl);
18494 if (DECL_ARTIFICIAL (decl))
18495 add_AT_flag (seg_die, DW_AT_artificial, 1);
18497 name = concat ("__second_sect_of_", name, NULL);
18498 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18499 fde->dw_fde_second_end, false);
18500 add_name_attribute (seg_die, name);
18501 if (want_pubnames ())
18502 add_pubname_string (name, seg_die);
18505 else
18506 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18507 false);
18510 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18512 /* We define the "frame base" as the function's CFA. This is more
18513 convenient for several reasons: (1) It's stable across the prologue
18514 and epilogue, which makes it better than just a frame pointer,
18515 (2) With dwarf3, there exists a one-byte encoding that allows us
18516 to reference the .debug_frame data by proxy, but failing that,
18517 (3) We can at least reuse the code inspection and interpretation
18518 code that determines the CFA position at various points in the
18519 function. */
18520 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18522 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18523 add_AT_loc (subr_die, DW_AT_frame_base, op);
18525 else
18527 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18528 if (list->dw_loc_next)
18529 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18530 else
18531 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18534 /* Compute a displacement from the "steady-state frame pointer" to
18535 the CFA. The former is what all stack slots and argument slots
18536 will reference in the rtl; the latter is what we've told the
18537 debugger about. We'll need to adjust all frame_base references
18538 by this displacement. */
18539 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18541 if (fun->static_chain_decl)
18542 add_AT_location_description (subr_die, DW_AT_static_link,
18543 loc_list_from_tree (fun->static_chain_decl, 2));
18546 /* Generate child dies for template paramaters. */
18547 if (debug_info_level > DINFO_LEVEL_TERSE)
18548 gen_generic_params_dies (decl);
18550 /* Now output descriptions of the arguments for this function. This gets
18551 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18552 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18553 `...' at the end of the formal parameter list. In order to find out if
18554 there was a trailing ellipsis or not, we must instead look at the type
18555 associated with the FUNCTION_DECL. This will be a node of type
18556 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18557 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18558 an ellipsis at the end. */
18560 /* In the case where we are describing a mere function declaration, all we
18561 need to do here (and all we *can* do here) is to describe the *types* of
18562 its formal parameters. */
18563 if (debug_info_level <= DINFO_LEVEL_TERSE)
18565 else if (declaration)
18566 gen_formal_types_die (decl, subr_die);
18567 else
18569 /* Generate DIEs to represent all known formal parameters. */
18570 tree parm = DECL_ARGUMENTS (decl);
18571 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18572 tree generic_decl_parm = generic_decl
18573 ? DECL_ARGUMENTS (generic_decl)
18574 : NULL;
18576 /* Now we want to walk the list of parameters of the function and
18577 emit their relevant DIEs.
18579 We consider the case of DECL being an instance of a generic function
18580 as well as it being a normal function.
18582 If DECL is an instance of a generic function we walk the
18583 parameters of the generic function declaration _and_ the parameters of
18584 DECL itself. This is useful because we want to emit specific DIEs for
18585 function parameter packs and those are declared as part of the
18586 generic function declaration. In that particular case,
18587 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18588 That DIE has children DIEs representing the set of arguments
18589 of the pack. Note that the set of pack arguments can be empty.
18590 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18591 children DIE.
18593 Otherwise, we just consider the parameters of DECL. */
18594 while (generic_decl_parm || parm)
18596 if (generic_decl_parm
18597 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18598 gen_formal_parameter_pack_die (generic_decl_parm,
18599 parm, subr_die,
18600 &parm);
18601 else if (parm)
18603 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18605 if (parm == DECL_ARGUMENTS (decl)
18606 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18607 && parm_die
18608 && (dwarf_version >= 3 || !dwarf_strict))
18609 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18611 parm = DECL_CHAIN (parm);
18614 if (generic_decl_parm)
18615 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18618 /* Decide whether we need an unspecified_parameters DIE at the end.
18619 There are 2 more cases to do this for: 1) the ansi ... declaration -
18620 this is detectable when the end of the arg list is not a
18621 void_type_node 2) an unprototyped function declaration (not a
18622 definition). This just means that we have no info about the
18623 parameters at all. */
18624 if (prototype_p (TREE_TYPE (decl)))
18626 /* This is the prototyped case, check for.... */
18627 if (stdarg_p (TREE_TYPE (decl)))
18628 gen_unspecified_parameters_die (decl, subr_die);
18630 else if (DECL_INITIAL (decl) == NULL_TREE)
18631 gen_unspecified_parameters_die (decl, subr_die);
18634 /* Output Dwarf info for all of the stuff within the body of the function
18635 (if it has one - it may be just a declaration). */
18636 outer_scope = DECL_INITIAL (decl);
18638 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18639 a function. This BLOCK actually represents the outermost binding contour
18640 for the function, i.e. the contour in which the function's formal
18641 parameters and labels get declared. Curiously, it appears that the front
18642 end doesn't actually put the PARM_DECL nodes for the current function onto
18643 the BLOCK_VARS list for this outer scope, but are strung off of the
18644 DECL_ARGUMENTS list for the function instead.
18646 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18647 the LABEL_DECL nodes for the function however, and we output DWARF info
18648 for those in decls_for_scope. Just within the `outer_scope' there will be
18649 a BLOCK node representing the function's outermost pair of curly braces,
18650 and any blocks used for the base and member initializers of a C++
18651 constructor function. */
18652 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18654 int call_site_note_count = 0;
18655 int tail_call_site_note_count = 0;
18657 /* Emit a DW_TAG_variable DIE for a named return value. */
18658 if (DECL_NAME (DECL_RESULT (decl)))
18659 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18661 decls_for_scope (outer_scope, subr_die, 0);
18663 if (call_arg_locations && !dwarf_strict)
18665 struct call_arg_loc_node *ca_loc;
18666 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18668 dw_die_ref die = NULL;
18669 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18670 rtx arg, next_arg;
18672 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18673 arg; arg = next_arg)
18675 dw_loc_descr_ref reg, val;
18676 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18677 dw_die_ref cdie, tdie = NULL;
18679 next_arg = XEXP (arg, 1);
18680 if (REG_P (XEXP (XEXP (arg, 0), 0))
18681 && next_arg
18682 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18683 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18684 && REGNO (XEXP (XEXP (arg, 0), 0))
18685 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18686 next_arg = XEXP (next_arg, 1);
18687 if (mode == VOIDmode)
18689 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18690 if (mode == VOIDmode)
18691 mode = GET_MODE (XEXP (arg, 0));
18693 if (mode == VOIDmode || mode == BLKmode)
18694 continue;
18695 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18697 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18698 tloc = XEXP (XEXP (arg, 0), 1);
18699 continue;
18701 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18702 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18704 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18705 tlocc = XEXP (XEXP (arg, 0), 1);
18706 continue;
18708 reg = NULL;
18709 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18710 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18711 VAR_INIT_STATUS_INITIALIZED);
18712 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18714 rtx mem = XEXP (XEXP (arg, 0), 0);
18715 reg = mem_loc_descriptor (XEXP (mem, 0),
18716 get_address_mode (mem),
18717 GET_MODE (mem),
18718 VAR_INIT_STATUS_INITIALIZED);
18720 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18721 == DEBUG_PARAMETER_REF)
18723 tree tdecl
18724 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18725 tdie = lookup_decl_die (tdecl);
18726 if (tdie == NULL)
18727 continue;
18729 else
18730 continue;
18731 if (reg == NULL
18732 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18733 != DEBUG_PARAMETER_REF)
18734 continue;
18735 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18736 VOIDmode,
18737 VAR_INIT_STATUS_INITIALIZED);
18738 if (val == NULL)
18739 continue;
18740 if (die == NULL)
18741 die = gen_call_site_die (decl, subr_die, ca_loc);
18742 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18743 NULL_TREE);
18744 if (reg != NULL)
18745 add_AT_loc (cdie, DW_AT_location, reg);
18746 else if (tdie != NULL)
18747 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18748 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18749 if (next_arg != XEXP (arg, 1))
18751 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18752 if (mode == VOIDmode)
18753 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18754 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18755 0), 1),
18756 mode, VOIDmode,
18757 VAR_INIT_STATUS_INITIALIZED);
18758 if (val != NULL)
18759 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18762 if (die == NULL
18763 && (ca_loc->symbol_ref || tloc))
18764 die = gen_call_site_die (decl, subr_die, ca_loc);
18765 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18767 dw_loc_descr_ref tval = NULL;
18769 if (tloc != NULL_RTX)
18770 tval = mem_loc_descriptor (tloc,
18771 GET_MODE (tloc) == VOIDmode
18772 ? Pmode : GET_MODE (tloc),
18773 VOIDmode,
18774 VAR_INIT_STATUS_INITIALIZED);
18775 if (tval)
18776 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18777 else if (tlocc != NULL_RTX)
18779 tval = mem_loc_descriptor (tlocc,
18780 GET_MODE (tlocc) == VOIDmode
18781 ? Pmode : GET_MODE (tlocc),
18782 VOIDmode,
18783 VAR_INIT_STATUS_INITIALIZED);
18784 if (tval)
18785 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18786 tval);
18789 if (die != NULL)
18791 call_site_note_count++;
18792 if (ca_loc->tail_call_p)
18793 tail_call_site_note_count++;
18797 call_arg_locations = NULL;
18798 call_arg_loc_last = NULL;
18799 if (tail_call_site_count >= 0
18800 && tail_call_site_count == tail_call_site_note_count
18801 && !dwarf_strict)
18803 if (call_site_count >= 0
18804 && call_site_count == call_site_note_count)
18805 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18806 else
18807 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18809 call_site_count = -1;
18810 tail_call_site_count = -1;
18813 if (subr_die != old_die)
18814 /* Add the calling convention attribute if requested. */
18815 add_calling_convention_attribute (subr_die, decl);
18818 /* Returns a hash value for X (which really is a die_struct). */
18820 hashval_t
18821 block_die_hasher::hash (die_struct *d)
18823 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18826 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18827 as decl_id and die_parent of die_struct Y. */
18829 bool
18830 block_die_hasher::equal (die_struct *x, die_struct *y)
18832 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
18835 /* Generate a DIE to represent a declared data object.
18836 Either DECL or ORIGIN must be non-null. */
18838 static void
18839 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18841 HOST_WIDE_INT off = 0;
18842 tree com_decl;
18843 tree decl_or_origin = decl ? decl : origin;
18844 tree ultimate_origin;
18845 dw_die_ref var_die;
18846 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18847 dw_die_ref origin_die;
18848 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18849 || class_or_namespace_scope_p (context_die));
18850 bool specialization_p = false;
18852 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18853 if (decl || ultimate_origin)
18854 origin = ultimate_origin;
18855 com_decl = fortran_common (decl_or_origin, &off);
18857 /* Symbol in common gets emitted as a child of the common block, in the form
18858 of a data member. */
18859 if (com_decl)
18861 dw_die_ref com_die;
18862 dw_loc_list_ref loc;
18863 die_node com_die_arg;
18865 var_die = lookup_decl_die (decl_or_origin);
18866 if (var_die)
18868 if (get_AT (var_die, DW_AT_location) == NULL)
18870 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18871 if (loc)
18873 if (off)
18875 /* Optimize the common case. */
18876 if (single_element_loc_list_p (loc)
18877 && loc->expr->dw_loc_opc == DW_OP_addr
18878 && loc->expr->dw_loc_next == NULL
18879 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18880 == SYMBOL_REF)
18882 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18883 loc->expr->dw_loc_oprnd1.v.val_addr
18884 = plus_constant (GET_MODE (x), x , off);
18886 else
18887 loc_list_plus_const (loc, off);
18889 add_AT_location_description (var_die, DW_AT_location, loc);
18890 remove_AT (var_die, DW_AT_declaration);
18893 return;
18896 if (common_block_die_table == NULL)
18897 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
18899 com_die_arg.decl_id = DECL_UID (com_decl);
18900 com_die_arg.die_parent = context_die;
18901 com_die = common_block_die_table->find (&com_die_arg);
18902 loc = loc_list_from_tree (com_decl, 2);
18903 if (com_die == NULL)
18905 const char *cnam
18906 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18907 die_node **slot;
18909 com_die = new_die (DW_TAG_common_block, context_die, decl);
18910 add_name_and_src_coords_attributes (com_die, com_decl);
18911 if (loc)
18913 add_AT_location_description (com_die, DW_AT_location, loc);
18914 /* Avoid sharing the same loc descriptor between
18915 DW_TAG_common_block and DW_TAG_variable. */
18916 loc = loc_list_from_tree (com_decl, 2);
18918 else if (DECL_EXTERNAL (decl))
18919 add_AT_flag (com_die, DW_AT_declaration, 1);
18920 if (want_pubnames ())
18921 add_pubname_string (cnam, com_die); /* ??? needed? */
18922 com_die->decl_id = DECL_UID (com_decl);
18923 slot = common_block_die_table->find_slot (com_die, INSERT);
18924 *slot = com_die;
18926 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18928 add_AT_location_description (com_die, DW_AT_location, loc);
18929 loc = loc_list_from_tree (com_decl, 2);
18930 remove_AT (com_die, DW_AT_declaration);
18932 var_die = new_die (DW_TAG_variable, com_die, decl);
18933 add_name_and_src_coords_attributes (var_die, decl);
18934 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
18935 context_die);
18936 add_AT_flag (var_die, DW_AT_external, 1);
18937 if (loc)
18939 if (off)
18941 /* Optimize the common case. */
18942 if (single_element_loc_list_p (loc)
18943 && loc->expr->dw_loc_opc == DW_OP_addr
18944 && loc->expr->dw_loc_next == NULL
18945 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18947 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18948 loc->expr->dw_loc_oprnd1.v.val_addr
18949 = plus_constant (GET_MODE (x), x, off);
18951 else
18952 loc_list_plus_const (loc, off);
18954 add_AT_location_description (var_die, DW_AT_location, loc);
18956 else if (DECL_EXTERNAL (decl))
18957 add_AT_flag (var_die, DW_AT_declaration, 1);
18958 equate_decl_number_to_die (decl, var_die);
18959 return;
18962 /* If the compiler emitted a definition for the DECL declaration
18963 and if we already emitted a DIE for it, don't emit a second
18964 DIE for it again. Allow re-declarations of DECLs that are
18965 inside functions, though. */
18966 if (old_die && declaration && !local_scope_p (context_die))
18967 return;
18969 /* For static data members, the declaration in the class is supposed
18970 to have DW_TAG_member tag; the specification should still be
18971 DW_TAG_variable referencing the DW_TAG_member DIE. */
18972 if (declaration && class_scope_p (context_die))
18973 var_die = new_die (DW_TAG_member, context_die, decl);
18974 else
18975 var_die = new_die (DW_TAG_variable, context_die, decl);
18977 origin_die = NULL;
18978 if (origin != NULL)
18979 origin_die = add_abstract_origin_attribute (var_die, origin);
18981 /* Loop unrolling can create multiple blocks that refer to the same
18982 static variable, so we must test for the DW_AT_declaration flag.
18984 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18985 copy decls and set the DECL_ABSTRACT_P flag on them instead of
18986 sharing them.
18988 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18990 ??? The declare_in_namespace support causes us to get two DIEs for one
18991 variable, both of which are declarations. We want to avoid considering
18992 one to be a specification, so we must test that this DIE is not a
18993 declaration. */
18994 else if (old_die && TREE_STATIC (decl) && ! declaration
18995 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18997 /* This is a definition of a C++ class level static. */
18998 add_AT_specification (var_die, old_die);
18999 specialization_p = true;
19000 if (DECL_NAME (decl))
19002 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19003 struct dwarf_file_data * file_index = lookup_filename (s.file);
19005 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19006 add_AT_file (var_die, DW_AT_decl_file, file_index);
19008 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19009 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19011 if (old_die->die_tag == DW_TAG_member)
19012 add_linkage_name (var_die, decl);
19015 else
19016 add_name_and_src_coords_attributes (var_die, decl);
19018 if ((origin == NULL && !specialization_p)
19019 || (origin != NULL
19020 && !DECL_ABSTRACT_P (decl_or_origin)
19021 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19022 decl_function_context
19023 (decl_or_origin))))
19025 tree type = TREE_TYPE (decl_or_origin);
19027 if (decl_by_reference_p (decl_or_origin))
19028 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19029 context_die);
19030 else
19031 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19032 context_die);
19035 if (origin == NULL && !specialization_p)
19037 if (TREE_PUBLIC (decl))
19038 add_AT_flag (var_die, DW_AT_external, 1);
19040 if (DECL_ARTIFICIAL (decl))
19041 add_AT_flag (var_die, DW_AT_artificial, 1);
19043 add_accessibility_attribute (var_die, decl);
19046 if (declaration)
19047 add_AT_flag (var_die, DW_AT_declaration, 1);
19049 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19050 equate_decl_number_to_die (decl, var_die);
19052 if (! declaration
19053 && (! DECL_ABSTRACT_P (decl_or_origin)
19054 /* Local static vars are shared between all clones/inlines,
19055 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19056 already set. */
19057 || (TREE_CODE (decl_or_origin) == VAR_DECL
19058 && TREE_STATIC (decl_or_origin)
19059 && DECL_RTL_SET_P (decl_or_origin)))
19060 /* When abstract origin already has DW_AT_location attribute, no need
19061 to add it again. */
19062 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19064 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19065 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19066 defer_location (decl_or_origin, var_die);
19067 else
19068 add_location_or_const_value_attribute (var_die, decl_or_origin,
19069 decl == NULL, DW_AT_location);
19070 add_pubname (decl_or_origin, var_die);
19072 else
19073 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19076 /* Generate a DIE to represent a named constant. */
19078 static void
19079 gen_const_die (tree decl, dw_die_ref context_die)
19081 dw_die_ref const_die;
19082 tree type = TREE_TYPE (decl);
19084 const_die = new_die (DW_TAG_constant, context_die, decl);
19085 add_name_and_src_coords_attributes (const_die, decl);
19086 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19087 if (TREE_PUBLIC (decl))
19088 add_AT_flag (const_die, DW_AT_external, 1);
19089 if (DECL_ARTIFICIAL (decl))
19090 add_AT_flag (const_die, DW_AT_artificial, 1);
19091 tree_add_const_value_attribute_for_decl (const_die, decl);
19094 /* Generate a DIE to represent a label identifier. */
19096 static void
19097 gen_label_die (tree decl, dw_die_ref context_die)
19099 tree origin = decl_ultimate_origin (decl);
19100 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19101 rtx insn;
19102 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19104 if (origin != NULL)
19105 add_abstract_origin_attribute (lbl_die, origin);
19106 else
19107 add_name_and_src_coords_attributes (lbl_die, decl);
19109 if (DECL_ABSTRACT_P (decl))
19110 equate_decl_number_to_die (decl, lbl_die);
19111 else
19113 insn = DECL_RTL_IF_SET (decl);
19115 /* Deleted labels are programmer specified labels which have been
19116 eliminated because of various optimizations. We still emit them
19117 here so that it is possible to put breakpoints on them. */
19118 if (insn
19119 && (LABEL_P (insn)
19120 || ((NOTE_P (insn)
19121 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19123 /* When optimization is enabled (via -O) some parts of the compiler
19124 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19125 represent source-level labels which were explicitly declared by
19126 the user. This really shouldn't be happening though, so catch
19127 it if it ever does happen. */
19128 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19130 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19131 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19133 else if (insn
19134 && NOTE_P (insn)
19135 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19136 && CODE_LABEL_NUMBER (insn) != -1)
19138 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19139 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19144 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19145 attributes to the DIE for a block STMT, to describe where the inlined
19146 function was called from. This is similar to add_src_coords_attributes. */
19148 static inline void
19149 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19151 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19153 if (dwarf_version >= 3 || !dwarf_strict)
19155 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19156 add_AT_unsigned (die, DW_AT_call_line, s.line);
19161 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19162 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19164 static inline void
19165 add_high_low_attributes (tree stmt, dw_die_ref die)
19167 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19169 if (BLOCK_FRAGMENT_CHAIN (stmt)
19170 && (dwarf_version >= 3 || !dwarf_strict))
19172 tree chain, superblock = NULL_TREE;
19173 dw_die_ref pdie;
19174 dw_attr_ref attr = NULL;
19176 if (inlined_function_outer_scope_p (stmt))
19178 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19179 BLOCK_NUMBER (stmt));
19180 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19183 /* Optimize duplicate .debug_ranges lists or even tails of
19184 lists. If this BLOCK has same ranges as its supercontext,
19185 lookup DW_AT_ranges attribute in the supercontext (and
19186 recursively so), verify that the ranges_table contains the
19187 right values and use it instead of adding a new .debug_range. */
19188 for (chain = stmt, pdie = die;
19189 BLOCK_SAME_RANGE (chain);
19190 chain = BLOCK_SUPERCONTEXT (chain))
19192 dw_attr_ref new_attr;
19194 pdie = pdie->die_parent;
19195 if (pdie == NULL)
19196 break;
19197 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19198 break;
19199 new_attr = get_AT (pdie, DW_AT_ranges);
19200 if (new_attr == NULL
19201 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19202 break;
19203 attr = new_attr;
19204 superblock = BLOCK_SUPERCONTEXT (chain);
19206 if (attr != NULL
19207 && (ranges_table[attr->dw_attr_val.v.val_offset
19208 / 2 / DWARF2_ADDR_SIZE].num
19209 == BLOCK_NUMBER (superblock))
19210 && BLOCK_FRAGMENT_CHAIN (superblock))
19212 unsigned long off = attr->dw_attr_val.v.val_offset
19213 / 2 / DWARF2_ADDR_SIZE;
19214 unsigned long supercnt = 0, thiscnt = 0;
19215 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19216 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19218 ++supercnt;
19219 gcc_checking_assert (ranges_table[off + supercnt].num
19220 == BLOCK_NUMBER (chain));
19222 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19223 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19224 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19225 ++thiscnt;
19226 gcc_assert (supercnt >= thiscnt);
19227 add_AT_range_list (die, DW_AT_ranges,
19228 ((off + supercnt - thiscnt)
19229 * 2 * DWARF2_ADDR_SIZE),
19230 false);
19231 return;
19234 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19236 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19239 add_ranges (chain);
19240 chain = BLOCK_FRAGMENT_CHAIN (chain);
19242 while (chain);
19243 add_ranges (NULL);
19245 else
19247 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19248 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19249 BLOCK_NUMBER (stmt));
19250 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19251 BLOCK_NUMBER (stmt));
19252 add_AT_low_high_pc (die, label, label_high, false);
19256 /* Generate a DIE for a lexical block. */
19258 static void
19259 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19261 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19263 if (call_arg_locations)
19265 if (block_map.length () <= BLOCK_NUMBER (stmt))
19266 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19267 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19270 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19271 add_high_low_attributes (stmt, stmt_die);
19273 decls_for_scope (stmt, stmt_die, depth);
19276 /* Generate a DIE for an inlined subprogram. */
19278 static void
19279 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19281 tree decl;
19283 /* The instance of function that is effectively being inlined shall not
19284 be abstract. */
19285 gcc_assert (! BLOCK_ABSTRACT (stmt));
19287 decl = block_ultimate_origin (stmt);
19289 /* Emit info for the abstract instance first, if we haven't yet. We
19290 must emit this even if the block is abstract, otherwise when we
19291 emit the block below (or elsewhere), we may end up trying to emit
19292 a die whose origin die hasn't been emitted, and crashing. */
19293 dwarf2out_abstract_function (decl);
19295 if (! BLOCK_ABSTRACT (stmt))
19297 dw_die_ref subr_die
19298 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19300 if (call_arg_locations)
19302 if (block_map.length () <= BLOCK_NUMBER (stmt))
19303 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19304 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19306 add_abstract_origin_attribute (subr_die, decl);
19307 if (TREE_ASM_WRITTEN (stmt))
19308 add_high_low_attributes (stmt, subr_die);
19309 add_call_src_coords_attributes (stmt, subr_die);
19311 decls_for_scope (stmt, subr_die, depth);
19315 /* Generate a DIE for a field in a record, or structure. */
19317 static void
19318 gen_field_die (tree decl, dw_die_ref context_die)
19320 dw_die_ref decl_die;
19322 if (TREE_TYPE (decl) == error_mark_node)
19323 return;
19325 decl_die = new_die (DW_TAG_member, context_die, decl);
19326 add_name_and_src_coords_attributes (decl_die, decl);
19327 add_type_attribute (decl_die, member_declared_type (decl),
19328 decl_quals (decl), context_die);
19330 if (DECL_BIT_FIELD_TYPE (decl))
19332 add_byte_size_attribute (decl_die, decl);
19333 add_bit_size_attribute (decl_die, decl);
19334 add_bit_offset_attribute (decl_die, decl);
19337 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19338 add_data_member_location_attribute (decl_die, decl);
19340 if (DECL_ARTIFICIAL (decl))
19341 add_AT_flag (decl_die, DW_AT_artificial, 1);
19343 add_accessibility_attribute (decl_die, decl);
19345 /* Equate decl number to die, so that we can look up this decl later on. */
19346 equate_decl_number_to_die (decl, decl_die);
19349 #if 0
19350 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19351 Use modified_type_die instead.
19352 We keep this code here just in case these types of DIEs may be needed to
19353 represent certain things in other languages (e.g. Pascal) someday. */
19355 static void
19356 gen_pointer_type_die (tree type, dw_die_ref context_die)
19358 dw_die_ref ptr_die
19359 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19361 equate_type_number_to_die (type, ptr_die);
19362 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19363 context_die);
19364 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19367 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19368 Use modified_type_die instead.
19369 We keep this code here just in case these types of DIEs may be needed to
19370 represent certain things in other languages (e.g. Pascal) someday. */
19372 static void
19373 gen_reference_type_die (tree type, dw_die_ref context_die)
19375 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19377 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19378 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19379 else
19380 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19382 equate_type_number_to_die (type, ref_die);
19383 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19384 context_die);
19385 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19387 #endif
19389 /* Generate a DIE for a pointer to a member type. */
19391 static void
19392 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19394 dw_die_ref ptr_die
19395 = new_die (DW_TAG_ptr_to_member_type,
19396 scope_die_for (type, context_die), type);
19398 equate_type_number_to_die (type, ptr_die);
19399 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19400 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19401 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19402 context_die);
19405 typedef const char *dchar_p; /* For DEF_VEC_P. */
19407 static char *producer_string;
19409 /* Return a heap allocated producer string including command line options
19410 if -grecord-gcc-switches. */
19412 static char *
19413 gen_producer_string (void)
19415 size_t j;
19416 auto_vec<dchar_p> switches;
19417 const char *language_string = lang_hooks.name;
19418 char *producer, *tail;
19419 const char *p;
19420 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19421 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19423 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19424 switch (save_decoded_options[j].opt_index)
19426 case OPT_o:
19427 case OPT_d:
19428 case OPT_dumpbase:
19429 case OPT_dumpdir:
19430 case OPT_auxbase:
19431 case OPT_auxbase_strip:
19432 case OPT_quiet:
19433 case OPT_version:
19434 case OPT_v:
19435 case OPT_w:
19436 case OPT_L:
19437 case OPT_D:
19438 case OPT_I:
19439 case OPT_U:
19440 case OPT_SPECIAL_unknown:
19441 case OPT_SPECIAL_ignore:
19442 case OPT_SPECIAL_program_name:
19443 case OPT_SPECIAL_input_file:
19444 case OPT_grecord_gcc_switches:
19445 case OPT_gno_record_gcc_switches:
19446 case OPT__output_pch_:
19447 case OPT_fdiagnostics_show_location_:
19448 case OPT_fdiagnostics_show_option:
19449 case OPT_fdiagnostics_show_caret:
19450 case OPT_fdiagnostics_color_:
19451 case OPT_fverbose_asm:
19452 case OPT____:
19453 case OPT__sysroot_:
19454 case OPT_nostdinc:
19455 case OPT_nostdinc__:
19456 /* Ignore these. */
19457 continue;
19458 default:
19459 if (cl_options[save_decoded_options[j].opt_index].flags
19460 & CL_NO_DWARF_RECORD)
19461 continue;
19462 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19463 == '-');
19464 switch (save_decoded_options[j].canonical_option[0][1])
19466 case 'M':
19467 case 'i':
19468 case 'W':
19469 continue;
19470 case 'f':
19471 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19472 "dump", 4) == 0)
19473 continue;
19474 break;
19475 default:
19476 break;
19478 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19479 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19480 break;
19483 producer = XNEWVEC (char, plen + 1 + len + 1);
19484 tail = producer;
19485 sprintf (tail, "%s %s", language_string, version_string);
19486 tail += plen;
19488 FOR_EACH_VEC_ELT (switches, j, p)
19490 len = strlen (p);
19491 *tail = ' ';
19492 memcpy (tail + 1, p, len);
19493 tail += len + 1;
19496 *tail = '\0';
19497 return producer;
19500 /* Generate the DIE for the compilation unit. */
19502 static dw_die_ref
19503 gen_compile_unit_die (const char *filename)
19505 dw_die_ref die;
19506 const char *language_string = lang_hooks.name;
19507 int language;
19509 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19511 if (filename)
19513 add_name_attribute (die, filename);
19514 /* Don't add cwd for <built-in>. */
19515 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19516 add_comp_dir_attribute (die);
19519 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19521 /* If our producer is LTO try to figure out a common language to use
19522 from the global list of translation units. */
19523 if (strcmp (language_string, "GNU GIMPLE") == 0)
19525 unsigned i;
19526 tree t;
19527 const char *common_lang = NULL;
19529 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19531 if (!TRANSLATION_UNIT_LANGUAGE (t))
19532 continue;
19533 if (!common_lang)
19534 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19535 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19537 else if (strncmp (common_lang, "GNU C", 5) == 0
19538 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19539 /* Mixing C and C++ is ok, use C++ in that case. */
19540 common_lang = "GNU C++";
19541 else
19543 /* Fall back to C. */
19544 common_lang = NULL;
19545 break;
19549 if (common_lang)
19550 language_string = common_lang;
19553 language = DW_LANG_C89;
19554 if (strcmp (language_string, "GNU C++") == 0)
19555 language = DW_LANG_C_plus_plus;
19556 else if (strcmp (language_string, "GNU F77") == 0)
19557 language = DW_LANG_Fortran77;
19558 else if (strcmp (language_string, "GNU Pascal") == 0)
19559 language = DW_LANG_Pascal83;
19560 else if (dwarf_version >= 3 || !dwarf_strict)
19562 if (strcmp (language_string, "GNU Ada") == 0)
19563 language = DW_LANG_Ada95;
19564 else if (strcmp (language_string, "GNU Fortran") == 0)
19565 language = DW_LANG_Fortran95;
19566 else if (strcmp (language_string, "GNU Java") == 0)
19567 language = DW_LANG_Java;
19568 else if (strcmp (language_string, "GNU Objective-C") == 0)
19569 language = DW_LANG_ObjC;
19570 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19571 language = DW_LANG_ObjC_plus_plus;
19572 else if (dwarf_version >= 5 || !dwarf_strict)
19574 if (strcmp (language_string, "GNU Go") == 0)
19575 language = DW_LANG_Go;
19578 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19579 else if (strcmp (language_string, "GNU Fortran") == 0)
19580 language = DW_LANG_Fortran90;
19582 add_AT_unsigned (die, DW_AT_language, language);
19584 switch (language)
19586 case DW_LANG_Fortran77:
19587 case DW_LANG_Fortran90:
19588 case DW_LANG_Fortran95:
19589 /* Fortran has case insensitive identifiers and the front-end
19590 lowercases everything. */
19591 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19592 break;
19593 default:
19594 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19595 break;
19597 return die;
19600 /* Generate the DIE for a base class. */
19602 static void
19603 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19605 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19607 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19608 add_data_member_location_attribute (die, binfo);
19610 if (BINFO_VIRTUAL_P (binfo))
19611 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19613 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19614 children, otherwise the default is DW_ACCESS_public. In DWARF2
19615 the default has always been DW_ACCESS_private. */
19616 if (access == access_public_node)
19618 if (dwarf_version == 2
19619 || context_die->die_tag == DW_TAG_class_type)
19620 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19622 else if (access == access_protected_node)
19623 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19624 else if (dwarf_version > 2
19625 && context_die->die_tag != DW_TAG_class_type)
19626 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19629 /* Generate a DIE for a class member. */
19631 static void
19632 gen_member_die (tree type, dw_die_ref context_die)
19634 tree member;
19635 tree binfo = TYPE_BINFO (type);
19636 dw_die_ref child;
19638 /* If this is not an incomplete type, output descriptions of each of its
19639 members. Note that as we output the DIEs necessary to represent the
19640 members of this record or union type, we will also be trying to output
19641 DIEs to represent the *types* of those members. However the `type'
19642 function (above) will specifically avoid generating type DIEs for member
19643 types *within* the list of member DIEs for this (containing) type except
19644 for those types (of members) which are explicitly marked as also being
19645 members of this (containing) type themselves. The g++ front- end can
19646 force any given type to be treated as a member of some other (containing)
19647 type by setting the TYPE_CONTEXT of the given (member) type to point to
19648 the TREE node representing the appropriate (containing) type. */
19650 /* First output info about the base classes. */
19651 if (binfo)
19653 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19654 int i;
19655 tree base;
19657 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19658 gen_inheritance_die (base,
19659 (accesses ? (*accesses)[i] : access_public_node),
19660 context_die);
19663 /* Now output info about the data members and type members. */
19664 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19666 /* If we thought we were generating minimal debug info for TYPE
19667 and then changed our minds, some of the member declarations
19668 may have already been defined. Don't define them again, but
19669 do put them in the right order. */
19671 child = lookup_decl_die (member);
19672 if (child)
19673 splice_child_die (context_die, child);
19674 else
19675 gen_decl_die (member, NULL, context_die);
19678 /* Now output info about the function members (if any). */
19679 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19681 /* Don't include clones in the member list. */
19682 if (DECL_ABSTRACT_ORIGIN (member))
19683 continue;
19685 child = lookup_decl_die (member);
19686 if (child)
19687 splice_child_die (context_die, child);
19688 else
19689 gen_decl_die (member, NULL, context_die);
19693 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19694 is set, we pretend that the type was never defined, so we only get the
19695 member DIEs needed by later specification DIEs. */
19697 static void
19698 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19699 enum debug_info_usage usage)
19701 dw_die_ref type_die = lookup_type_die (type);
19702 dw_die_ref scope_die = 0;
19703 int nested = 0;
19704 int complete = (TYPE_SIZE (type)
19705 && (! TYPE_STUB_DECL (type)
19706 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19707 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19708 complete = complete && should_emit_struct_debug (type, usage);
19710 if (type_die && ! complete)
19711 return;
19713 if (TYPE_CONTEXT (type) != NULL_TREE
19714 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19715 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19716 nested = 1;
19718 scope_die = scope_die_for (type, context_die);
19720 /* Generate child dies for template paramaters. */
19721 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19722 schedule_generic_params_dies_gen (type);
19724 if (! type_die || (nested && is_cu_die (scope_die)))
19725 /* First occurrence of type or toplevel definition of nested class. */
19727 dw_die_ref old_die = type_die;
19729 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19730 ? record_type_tag (type) : DW_TAG_union_type,
19731 scope_die, type);
19732 equate_type_number_to_die (type, type_die);
19733 if (old_die)
19734 add_AT_specification (type_die, old_die);
19735 else
19736 add_name_attribute (type_die, type_tag (type));
19738 else
19739 remove_AT (type_die, DW_AT_declaration);
19741 /* If this type has been completed, then give it a byte_size attribute and
19742 then give a list of members. */
19743 if (complete && !ns_decl)
19745 /* Prevent infinite recursion in cases where the type of some member of
19746 this type is expressed in terms of this type itself. */
19747 TREE_ASM_WRITTEN (type) = 1;
19748 add_byte_size_attribute (type_die, type);
19749 if (TYPE_STUB_DECL (type) != NULL_TREE)
19751 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19752 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19755 /* If the first reference to this type was as the return type of an
19756 inline function, then it may not have a parent. Fix this now. */
19757 if (type_die->die_parent == NULL)
19758 add_child_die (scope_die, type_die);
19760 push_decl_scope (type);
19761 gen_member_die (type, type_die);
19762 pop_decl_scope ();
19764 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19765 if (TYPE_ARTIFICIAL (type))
19766 add_AT_flag (type_die, DW_AT_artificial, 1);
19768 /* GNU extension: Record what type our vtable lives in. */
19769 if (TYPE_VFIELD (type))
19771 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19773 gen_type_die (vtype, context_die);
19774 add_AT_die_ref (type_die, DW_AT_containing_type,
19775 lookup_type_die (vtype));
19778 else
19780 add_AT_flag (type_die, DW_AT_declaration, 1);
19782 /* We don't need to do this for function-local types. */
19783 if (TYPE_STUB_DECL (type)
19784 && ! decl_function_context (TYPE_STUB_DECL (type)))
19785 vec_safe_push (incomplete_types, type);
19788 if (get_AT (type_die, DW_AT_name))
19789 add_pubtype (type, type_die);
19792 /* Generate a DIE for a subroutine _type_. */
19794 static void
19795 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19797 tree return_type = TREE_TYPE (type);
19798 dw_die_ref subr_die
19799 = new_die (DW_TAG_subroutine_type,
19800 scope_die_for (type, context_die), type);
19802 equate_type_number_to_die (type, subr_die);
19803 add_prototyped_attribute (subr_die, type);
19804 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
19805 gen_formal_types_die (type, subr_die);
19807 if (get_AT (subr_die, DW_AT_name))
19808 add_pubtype (type, subr_die);
19811 /* Generate a DIE for a type definition. */
19813 static void
19814 gen_typedef_die (tree decl, dw_die_ref context_die)
19816 dw_die_ref type_die;
19817 tree origin;
19819 if (TREE_ASM_WRITTEN (decl))
19820 return;
19822 TREE_ASM_WRITTEN (decl) = 1;
19823 type_die = new_die (DW_TAG_typedef, context_die, decl);
19824 origin = decl_ultimate_origin (decl);
19825 if (origin != NULL)
19826 add_abstract_origin_attribute (type_die, origin);
19827 else
19829 tree type;
19831 add_name_and_src_coords_attributes (type_die, decl);
19832 if (DECL_ORIGINAL_TYPE (decl))
19834 type = DECL_ORIGINAL_TYPE (decl);
19836 gcc_assert (type != TREE_TYPE (decl));
19837 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19839 else
19841 type = TREE_TYPE (decl);
19843 if (is_naming_typedef_decl (TYPE_NAME (type)))
19845 /* Here, we are in the case of decl being a typedef naming
19846 an anonymous type, e.g:
19847 typedef struct {...} foo;
19848 In that case TREE_TYPE (decl) is not a typedef variant
19849 type and TYPE_NAME of the anonymous type is set to the
19850 TYPE_DECL of the typedef. This construct is emitted by
19851 the C++ FE.
19853 TYPE is the anonymous struct named by the typedef
19854 DECL. As we need the DW_AT_type attribute of the
19855 DW_TAG_typedef to point to the DIE of TYPE, let's
19856 generate that DIE right away. add_type_attribute
19857 called below will then pick (via lookup_type_die) that
19858 anonymous struct DIE. */
19859 if (!TREE_ASM_WRITTEN (type))
19860 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19862 /* This is a GNU Extension. We are adding a
19863 DW_AT_linkage_name attribute to the DIE of the
19864 anonymous struct TYPE. The value of that attribute
19865 is the name of the typedef decl naming the anonymous
19866 struct. This greatly eases the work of consumers of
19867 this debug info. */
19868 add_linkage_attr (lookup_type_die (type), decl);
19872 add_type_attribute (type_die, type, decl_quals (decl), context_die);
19874 if (is_naming_typedef_decl (decl))
19875 /* We want that all subsequent calls to lookup_type_die with
19876 TYPE in argument yield the DW_TAG_typedef we have just
19877 created. */
19878 equate_type_number_to_die (type, type_die);
19880 add_accessibility_attribute (type_die, decl);
19883 if (DECL_ABSTRACT_P (decl))
19884 equate_decl_number_to_die (decl, type_die);
19886 if (get_AT (type_die, DW_AT_name))
19887 add_pubtype (decl, type_die);
19890 /* Generate a DIE for a struct, class, enum or union type. */
19892 static void
19893 gen_tagged_type_die (tree type,
19894 dw_die_ref context_die,
19895 enum debug_info_usage usage)
19897 int need_pop;
19899 if (type == NULL_TREE
19900 || !is_tagged_type (type))
19901 return;
19903 /* If this is a nested type whose containing class hasn't been written
19904 out yet, writing it out will cover this one, too. This does not apply
19905 to instantiations of member class templates; they need to be added to
19906 the containing class as they are generated. FIXME: This hurts the
19907 idea of combining type decls from multiple TUs, since we can't predict
19908 what set of template instantiations we'll get. */
19909 if (TYPE_CONTEXT (type)
19910 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19911 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19913 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19915 if (TREE_ASM_WRITTEN (type))
19916 return;
19918 /* If that failed, attach ourselves to the stub. */
19919 push_decl_scope (TYPE_CONTEXT (type));
19920 context_die = lookup_type_die (TYPE_CONTEXT (type));
19921 need_pop = 1;
19923 else if (TYPE_CONTEXT (type) != NULL_TREE
19924 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19926 /* If this type is local to a function that hasn't been written
19927 out yet, use a NULL context for now; it will be fixed up in
19928 decls_for_scope. */
19929 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19930 /* A declaration DIE doesn't count; nested types need to go in the
19931 specification. */
19932 if (context_die && is_declaration_die (context_die))
19933 context_die = NULL;
19934 need_pop = 0;
19936 else
19938 context_die = declare_in_namespace (type, context_die);
19939 need_pop = 0;
19942 if (TREE_CODE (type) == ENUMERAL_TYPE)
19944 /* This might have been written out by the call to
19945 declare_in_namespace. */
19946 if (!TREE_ASM_WRITTEN (type))
19947 gen_enumeration_type_die (type, context_die);
19949 else
19950 gen_struct_or_union_type_die (type, context_die, usage);
19952 if (need_pop)
19953 pop_decl_scope ();
19955 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19956 it up if it is ever completed. gen_*_type_die will set it for us
19957 when appropriate. */
19960 /* Generate a type description DIE. */
19962 static void
19963 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19964 enum debug_info_usage usage)
19966 struct array_descr_info info;
19968 if (type == NULL_TREE || type == error_mark_node)
19969 return;
19971 if (TYPE_NAME (type) != NULL_TREE
19972 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19973 && is_redundant_typedef (TYPE_NAME (type))
19974 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19975 /* The DECL of this type is a typedef we don't want to emit debug
19976 info for but we want debug info for its underlying typedef.
19977 This can happen for e.g, the injected-class-name of a C++
19978 type. */
19979 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19981 /* If TYPE is a typedef type variant, let's generate debug info
19982 for the parent typedef which TYPE is a type of. */
19983 if (typedef_variant_p (type))
19985 if (TREE_ASM_WRITTEN (type))
19986 return;
19988 /* Prevent broken recursion; we can't hand off to the same type. */
19989 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19991 /* Give typedefs the right scope. */
19992 context_die = scope_die_for (type, context_die);
19994 TREE_ASM_WRITTEN (type) = 1;
19996 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19997 return;
20000 /* If type is an anonymous tagged type named by a typedef, let's
20001 generate debug info for the typedef. */
20002 if (is_naming_typedef_decl (TYPE_NAME (type)))
20004 /* Use the DIE of the containing namespace as the parent DIE of
20005 the type description DIE we want to generate. */
20006 if (DECL_CONTEXT (TYPE_NAME (type))
20007 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20008 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20010 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20011 return;
20014 /* If this is an array type with hidden descriptor, handle it first. */
20015 if (!TREE_ASM_WRITTEN (type)
20016 && lang_hooks.types.get_array_descr_info
20017 && lang_hooks.types.get_array_descr_info (type, &info)
20018 && (dwarf_version >= 3 || !dwarf_strict))
20020 gen_descr_array_type_die (type, &info, context_die);
20021 TREE_ASM_WRITTEN (type) = 1;
20022 return;
20025 /* We are going to output a DIE to represent the unqualified version
20026 of this type (i.e. without any const or volatile qualifiers) so
20027 get the main variant (i.e. the unqualified version) of this type
20028 now. (Vectors are special because the debugging info is in the
20029 cloned type itself). */
20030 if (TREE_CODE (type) != VECTOR_TYPE)
20031 type = type_main_variant (type);
20033 if (TREE_ASM_WRITTEN (type))
20034 return;
20036 switch (TREE_CODE (type))
20038 case ERROR_MARK:
20039 break;
20041 case POINTER_TYPE:
20042 case REFERENCE_TYPE:
20043 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20044 ensures that the gen_type_die recursion will terminate even if the
20045 type is recursive. Recursive types are possible in Ada. */
20046 /* ??? We could perhaps do this for all types before the switch
20047 statement. */
20048 TREE_ASM_WRITTEN (type) = 1;
20050 /* For these types, all that is required is that we output a DIE (or a
20051 set of DIEs) to represent the "basis" type. */
20052 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20053 DINFO_USAGE_IND_USE);
20054 break;
20056 case OFFSET_TYPE:
20057 /* This code is used for C++ pointer-to-data-member types.
20058 Output a description of the relevant class type. */
20059 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20060 DINFO_USAGE_IND_USE);
20062 /* Output a description of the type of the object pointed to. */
20063 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20064 DINFO_USAGE_IND_USE);
20066 /* Now output a DIE to represent this pointer-to-data-member type
20067 itself. */
20068 gen_ptr_to_mbr_type_die (type, context_die);
20069 break;
20071 case FUNCTION_TYPE:
20072 /* Force out return type (in case it wasn't forced out already). */
20073 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20074 DINFO_USAGE_DIR_USE);
20075 gen_subroutine_type_die (type, context_die);
20076 break;
20078 case METHOD_TYPE:
20079 /* Force out return type (in case it wasn't forced out already). */
20080 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20081 DINFO_USAGE_DIR_USE);
20082 gen_subroutine_type_die (type, context_die);
20083 break;
20085 case ARRAY_TYPE:
20086 gen_array_type_die (type, context_die);
20087 break;
20089 case VECTOR_TYPE:
20090 gen_array_type_die (type, context_die);
20091 break;
20093 case ENUMERAL_TYPE:
20094 case RECORD_TYPE:
20095 case UNION_TYPE:
20096 case QUAL_UNION_TYPE:
20097 gen_tagged_type_die (type, context_die, usage);
20098 return;
20100 case VOID_TYPE:
20101 case INTEGER_TYPE:
20102 case REAL_TYPE:
20103 case FIXED_POINT_TYPE:
20104 case COMPLEX_TYPE:
20105 case BOOLEAN_TYPE:
20106 /* No DIEs needed for fundamental types. */
20107 break;
20109 case NULLPTR_TYPE:
20110 case LANG_TYPE:
20111 /* Just use DW_TAG_unspecified_type. */
20113 dw_die_ref type_die = lookup_type_die (type);
20114 if (type_die == NULL)
20116 tree name = TYPE_IDENTIFIER (type);
20117 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20118 type);
20119 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20120 equate_type_number_to_die (type, type_die);
20123 break;
20125 default:
20126 if (is_cxx_auto (type))
20128 tree name = TYPE_IDENTIFIER (type);
20129 dw_die_ref *die = (name == get_identifier ("auto")
20130 ? &auto_die : &decltype_auto_die);
20131 if (!*die)
20133 *die = new_die (DW_TAG_unspecified_type,
20134 comp_unit_die (), NULL_TREE);
20135 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20137 equate_type_number_to_die (type, *die);
20138 break;
20140 gcc_unreachable ();
20143 TREE_ASM_WRITTEN (type) = 1;
20146 static void
20147 gen_type_die (tree type, dw_die_ref context_die)
20149 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20152 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20153 things which are local to the given block. */
20155 static void
20156 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20158 int must_output_die = 0;
20159 bool inlined_func;
20161 /* Ignore blocks that are NULL. */
20162 if (stmt == NULL_TREE)
20163 return;
20165 inlined_func = inlined_function_outer_scope_p (stmt);
20167 /* If the block is one fragment of a non-contiguous block, do not
20168 process the variables, since they will have been done by the
20169 origin block. Do process subblocks. */
20170 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20172 tree sub;
20174 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20175 gen_block_die (sub, context_die, depth + 1);
20177 return;
20180 /* Determine if we need to output any Dwarf DIEs at all to represent this
20181 block. */
20182 if (inlined_func)
20183 /* The outer scopes for inlinings *must* always be represented. We
20184 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20185 must_output_die = 1;
20186 else
20188 /* Determine if this block directly contains any "significant"
20189 local declarations which we will need to output DIEs for. */
20190 if (debug_info_level > DINFO_LEVEL_TERSE)
20191 /* We are not in terse mode so *any* local declaration counts
20192 as being a "significant" one. */
20193 must_output_die = ((BLOCK_VARS (stmt) != NULL
20194 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20195 && (TREE_USED (stmt)
20196 || TREE_ASM_WRITTEN (stmt)
20197 || BLOCK_ABSTRACT (stmt)));
20198 else if ((TREE_USED (stmt)
20199 || TREE_ASM_WRITTEN (stmt)
20200 || BLOCK_ABSTRACT (stmt))
20201 && !dwarf2out_ignore_block (stmt))
20202 must_output_die = 1;
20205 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20206 DIE for any block which contains no significant local declarations at
20207 all. Rather, in such cases we just call `decls_for_scope' so that any
20208 needed Dwarf info for any sub-blocks will get properly generated. Note
20209 that in terse mode, our definition of what constitutes a "significant"
20210 local declaration gets restricted to include only inlined function
20211 instances and local (nested) function definitions. */
20212 if (must_output_die)
20214 if (inlined_func)
20216 /* If STMT block is abstract, that means we have been called
20217 indirectly from dwarf2out_abstract_function.
20218 That function rightfully marks the descendent blocks (of
20219 the abstract function it is dealing with) as being abstract,
20220 precisely to prevent us from emitting any
20221 DW_TAG_inlined_subroutine DIE as a descendent
20222 of an abstract function instance. So in that case, we should
20223 not call gen_inlined_subroutine_die.
20225 Later though, when cgraph asks dwarf2out to emit info
20226 for the concrete instance of the function decl into which
20227 the concrete instance of STMT got inlined, the later will lead
20228 to the generation of a DW_TAG_inlined_subroutine DIE. */
20229 if (! BLOCK_ABSTRACT (stmt))
20230 gen_inlined_subroutine_die (stmt, context_die, depth);
20232 else
20233 gen_lexical_block_die (stmt, context_die, depth);
20235 else
20236 decls_for_scope (stmt, context_die, depth);
20239 /* Process variable DECL (or variable with origin ORIGIN) within
20240 block STMT and add it to CONTEXT_DIE. */
20241 static void
20242 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20244 dw_die_ref die;
20245 tree decl_or_origin = decl ? decl : origin;
20247 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20248 die = lookup_decl_die (decl_or_origin);
20249 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20250 && TYPE_DECL_IS_STUB (decl_or_origin))
20251 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20252 else
20253 die = NULL;
20255 if (die != NULL && die->die_parent == NULL)
20256 add_child_die (context_die, die);
20257 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20258 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20259 stmt, context_die);
20260 else
20261 gen_decl_die (decl, origin, context_die);
20264 /* Generate all of the decls declared within a given scope and (recursively)
20265 all of its sub-blocks. */
20267 static void
20268 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20270 tree decl;
20271 unsigned int i;
20272 tree subblocks;
20274 /* Ignore NULL blocks. */
20275 if (stmt == NULL_TREE)
20276 return;
20278 /* Output the DIEs to represent all of the data objects and typedefs
20279 declared directly within this block but not within any nested
20280 sub-blocks. Also, nested function and tag DIEs have been
20281 generated with a parent of NULL; fix that up now. We don't
20282 have to do this if we're at -g1. */
20283 if (debug_info_level > DINFO_LEVEL_TERSE)
20285 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20286 process_scope_var (stmt, decl, NULL_TREE, context_die);
20287 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20288 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20289 context_die);
20292 /* Even if we're at -g1, we need to process the subblocks in order to get
20293 inlined call information. */
20295 /* Output the DIEs to represent all sub-blocks (and the items declared
20296 therein) of this block. */
20297 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20298 subblocks != NULL;
20299 subblocks = BLOCK_CHAIN (subblocks))
20300 gen_block_die (subblocks, context_die, depth + 1);
20303 /* Is this a typedef we can avoid emitting? */
20305 static inline int
20306 is_redundant_typedef (const_tree decl)
20308 if (TYPE_DECL_IS_STUB (decl))
20309 return 1;
20311 if (DECL_ARTIFICIAL (decl)
20312 && DECL_CONTEXT (decl)
20313 && is_tagged_type (DECL_CONTEXT (decl))
20314 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20315 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20316 /* Also ignore the artificial member typedef for the class name. */
20317 return 1;
20319 return 0;
20322 /* Return TRUE if TYPE is a typedef that names a type for linkage
20323 purposes. This kind of typedefs is produced by the C++ FE for
20324 constructs like:
20326 typedef struct {...} foo;
20328 In that case, there is no typedef variant type produced for foo.
20329 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20330 struct type. */
20332 static bool
20333 is_naming_typedef_decl (const_tree decl)
20335 if (decl == NULL_TREE
20336 || TREE_CODE (decl) != TYPE_DECL
20337 || !is_tagged_type (TREE_TYPE (decl))
20338 || DECL_IS_BUILTIN (decl)
20339 || is_redundant_typedef (decl)
20340 /* It looks like Ada produces TYPE_DECLs that are very similar
20341 to C++ naming typedefs but that have different
20342 semantics. Let's be specific to c++ for now. */
20343 || !is_cxx ())
20344 return FALSE;
20346 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20347 && TYPE_NAME (TREE_TYPE (decl)) == decl
20348 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20349 != TYPE_NAME (TREE_TYPE (decl))));
20352 /* Returns the DIE for a context. */
20354 static inline dw_die_ref
20355 get_context_die (tree context)
20357 if (context)
20359 /* Find die that represents this context. */
20360 if (TYPE_P (context))
20362 context = TYPE_MAIN_VARIANT (context);
20363 return strip_naming_typedef (context, force_type_die (context));
20365 else
20366 return force_decl_die (context);
20368 return comp_unit_die ();
20371 /* Returns the DIE for decl. A DIE will always be returned. */
20373 static dw_die_ref
20374 force_decl_die (tree decl)
20376 dw_die_ref decl_die;
20377 unsigned saved_external_flag;
20378 tree save_fn = NULL_TREE;
20379 decl_die = lookup_decl_die (decl);
20380 if (!decl_die)
20382 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20384 decl_die = lookup_decl_die (decl);
20385 if (decl_die)
20386 return decl_die;
20388 switch (TREE_CODE (decl))
20390 case FUNCTION_DECL:
20391 /* Clear current_function_decl, so that gen_subprogram_die thinks
20392 that this is a declaration. At this point, we just want to force
20393 declaration die. */
20394 save_fn = current_function_decl;
20395 current_function_decl = NULL_TREE;
20396 gen_subprogram_die (decl, context_die);
20397 current_function_decl = save_fn;
20398 break;
20400 case VAR_DECL:
20401 /* Set external flag to force declaration die. Restore it after
20402 gen_decl_die() call. */
20403 saved_external_flag = DECL_EXTERNAL (decl);
20404 DECL_EXTERNAL (decl) = 1;
20405 gen_decl_die (decl, NULL, context_die);
20406 DECL_EXTERNAL (decl) = saved_external_flag;
20407 break;
20409 case NAMESPACE_DECL:
20410 if (dwarf_version >= 3 || !dwarf_strict)
20411 dwarf2out_decl (decl);
20412 else
20413 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20414 decl_die = comp_unit_die ();
20415 break;
20417 case TRANSLATION_UNIT_DECL:
20418 decl_die = comp_unit_die ();
20419 break;
20421 default:
20422 gcc_unreachable ();
20425 /* We should be able to find the DIE now. */
20426 if (!decl_die)
20427 decl_die = lookup_decl_die (decl);
20428 gcc_assert (decl_die);
20431 return decl_die;
20434 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20435 always returned. */
20437 static dw_die_ref
20438 force_type_die (tree type)
20440 dw_die_ref type_die;
20442 type_die = lookup_type_die (type);
20443 if (!type_die)
20445 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20447 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20448 context_die);
20449 gcc_assert (type_die);
20451 return type_die;
20454 /* Force out any required namespaces to be able to output DECL,
20455 and return the new context_die for it, if it's changed. */
20457 static dw_die_ref
20458 setup_namespace_context (tree thing, dw_die_ref context_die)
20460 tree context = (DECL_P (thing)
20461 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20462 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20463 /* Force out the namespace. */
20464 context_die = force_decl_die (context);
20466 return context_die;
20469 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20470 type) within its namespace, if appropriate.
20472 For compatibility with older debuggers, namespace DIEs only contain
20473 declarations; all definitions are emitted at CU scope. */
20475 static dw_die_ref
20476 declare_in_namespace (tree thing, dw_die_ref context_die)
20478 dw_die_ref ns_context;
20480 if (debug_info_level <= DINFO_LEVEL_TERSE)
20481 return context_die;
20483 /* External declarations in the local scope only need to be emitted
20484 once, not once in the namespace and once in the scope.
20486 This avoids declaring the `extern' below in the
20487 namespace DIE as well as in the innermost scope:
20489 namespace S
20491 int i=5;
20492 int foo()
20494 int i=8;
20495 extern int i;
20496 return i;
20500 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20501 return context_die;
20503 /* If this decl is from an inlined function, then don't try to emit it in its
20504 namespace, as we will get confused. It would have already been emitted
20505 when the abstract instance of the inline function was emitted anyways. */
20506 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20507 return context_die;
20509 ns_context = setup_namespace_context (thing, context_die);
20511 if (ns_context != context_die)
20513 if (is_fortran ())
20514 return ns_context;
20515 if (DECL_P (thing))
20516 gen_decl_die (thing, NULL, ns_context);
20517 else
20518 gen_type_die (thing, ns_context);
20520 return context_die;
20523 /* Generate a DIE for a namespace or namespace alias. */
20525 static void
20526 gen_namespace_die (tree decl, dw_die_ref context_die)
20528 dw_die_ref namespace_die;
20530 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20531 they are an alias of. */
20532 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20534 /* Output a real namespace or module. */
20535 context_die = setup_namespace_context (decl, comp_unit_die ());
20536 namespace_die = new_die (is_fortran ()
20537 ? DW_TAG_module : DW_TAG_namespace,
20538 context_die, decl);
20539 /* For Fortran modules defined in different CU don't add src coords. */
20540 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20542 const char *name = dwarf2_name (decl, 0);
20543 if (name)
20544 add_name_attribute (namespace_die, name);
20546 else
20547 add_name_and_src_coords_attributes (namespace_die, decl);
20548 if (DECL_EXTERNAL (decl))
20549 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20550 equate_decl_number_to_die (decl, namespace_die);
20552 else
20554 /* Output a namespace alias. */
20556 /* Force out the namespace we are an alias of, if necessary. */
20557 dw_die_ref origin_die
20558 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20560 if (DECL_FILE_SCOPE_P (decl)
20561 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20562 context_die = setup_namespace_context (decl, comp_unit_die ());
20563 /* Now create the namespace alias DIE. */
20564 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20565 add_name_and_src_coords_attributes (namespace_die, decl);
20566 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20567 equate_decl_number_to_die (decl, namespace_die);
20569 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20570 if (want_pubnames ())
20571 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20574 /* Generate Dwarf debug information for a decl described by DECL.
20575 The return value is currently only meaningful for PARM_DECLs,
20576 for all other decls it returns NULL. */
20578 static dw_die_ref
20579 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20581 tree decl_or_origin = decl ? decl : origin;
20582 tree class_origin = NULL, ultimate_origin;
20584 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20585 return NULL;
20587 switch (TREE_CODE (decl_or_origin))
20589 case ERROR_MARK:
20590 break;
20592 case CONST_DECL:
20593 if (!is_fortran () && !is_ada ())
20595 /* The individual enumerators of an enum type get output when we output
20596 the Dwarf representation of the relevant enum type itself. */
20597 break;
20600 /* Emit its type. */
20601 gen_type_die (TREE_TYPE (decl), context_die);
20603 /* And its containing namespace. */
20604 context_die = declare_in_namespace (decl, context_die);
20606 gen_const_die (decl, context_die);
20607 break;
20609 case FUNCTION_DECL:
20610 /* Don't output any DIEs to represent mere function declarations,
20611 unless they are class members or explicit block externs. */
20612 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20613 && DECL_FILE_SCOPE_P (decl_or_origin)
20614 && (current_function_decl == NULL_TREE
20615 || DECL_ARTIFICIAL (decl_or_origin)))
20616 break;
20618 #if 0
20619 /* FIXME */
20620 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20621 on local redeclarations of global functions. That seems broken. */
20622 if (current_function_decl != decl)
20623 /* This is only a declaration. */;
20624 #endif
20626 /* If we're emitting a clone, emit info for the abstract instance. */
20627 if (origin || DECL_ORIGIN (decl) != decl)
20628 dwarf2out_abstract_function (origin
20629 ? DECL_ORIGIN (origin)
20630 : DECL_ABSTRACT_ORIGIN (decl));
20632 /* If we're emitting an out-of-line copy of an inline function,
20633 emit info for the abstract instance and set up to refer to it. */
20634 else if (cgraph_function_possibly_inlined_p (decl)
20635 && ! DECL_ABSTRACT_P (decl)
20636 && ! class_or_namespace_scope_p (context_die)
20637 /* dwarf2out_abstract_function won't emit a die if this is just
20638 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20639 that case, because that works only if we have a die. */
20640 && DECL_INITIAL (decl) != NULL_TREE)
20642 dwarf2out_abstract_function (decl);
20643 set_decl_origin_self (decl);
20646 /* Otherwise we're emitting the primary DIE for this decl. */
20647 else if (debug_info_level > DINFO_LEVEL_TERSE)
20649 /* Before we describe the FUNCTION_DECL itself, make sure that we
20650 have its containing type. */
20651 if (!origin)
20652 origin = decl_class_context (decl);
20653 if (origin != NULL_TREE)
20654 gen_type_die (origin, context_die);
20656 /* And its return type. */
20657 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20659 /* And its virtual context. */
20660 if (DECL_VINDEX (decl) != NULL_TREE)
20661 gen_type_die (DECL_CONTEXT (decl), context_die);
20663 /* Make sure we have a member DIE for decl. */
20664 if (origin != NULL_TREE)
20665 gen_type_die_for_member (origin, decl, context_die);
20667 /* And its containing namespace. */
20668 context_die = declare_in_namespace (decl, context_die);
20671 /* Now output a DIE to represent the function itself. */
20672 if (decl)
20673 gen_subprogram_die (decl, context_die);
20674 break;
20676 case TYPE_DECL:
20677 /* If we are in terse mode, don't generate any DIEs to represent any
20678 actual typedefs. */
20679 if (debug_info_level <= DINFO_LEVEL_TERSE)
20680 break;
20682 /* In the special case of a TYPE_DECL node representing the declaration
20683 of some type tag, if the given TYPE_DECL is marked as having been
20684 instantiated from some other (original) TYPE_DECL node (e.g. one which
20685 was generated within the original definition of an inline function) we
20686 used to generate a special (abbreviated) DW_TAG_structure_type,
20687 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20688 should be actually referencing those DIEs, as variable DIEs with that
20689 type would be emitted already in the abstract origin, so it was always
20690 removed during unused type prunning. Don't add anything in this
20691 case. */
20692 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20693 break;
20695 if (is_redundant_typedef (decl))
20696 gen_type_die (TREE_TYPE (decl), context_die);
20697 else
20698 /* Output a DIE to represent the typedef itself. */
20699 gen_typedef_die (decl, context_die);
20700 break;
20702 case LABEL_DECL:
20703 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20704 gen_label_die (decl, context_die);
20705 break;
20707 case VAR_DECL:
20708 case RESULT_DECL:
20709 /* If we are in terse mode, don't generate any DIEs to represent any
20710 variable declarations or definitions. */
20711 if (debug_info_level <= DINFO_LEVEL_TERSE)
20712 break;
20714 /* Output any DIEs that are needed to specify the type of this data
20715 object. */
20716 if (decl_by_reference_p (decl_or_origin))
20717 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20718 else
20719 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20721 /* And its containing type. */
20722 class_origin = decl_class_context (decl_or_origin);
20723 if (class_origin != NULL_TREE)
20724 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20726 /* And its containing namespace. */
20727 context_die = declare_in_namespace (decl_or_origin, context_die);
20729 /* Now output the DIE to represent the data object itself. This gets
20730 complicated because of the possibility that the VAR_DECL really
20731 represents an inlined instance of a formal parameter for an inline
20732 function. */
20733 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20734 if (ultimate_origin != NULL_TREE
20735 && TREE_CODE (ultimate_origin) == PARM_DECL)
20736 gen_formal_parameter_die (decl, origin,
20737 true /* Emit name attribute. */,
20738 context_die);
20739 else
20740 gen_variable_die (decl, origin, context_die);
20741 break;
20743 case FIELD_DECL:
20744 /* Ignore the nameless fields that are used to skip bits but handle C++
20745 anonymous unions and structs. */
20746 if (DECL_NAME (decl) != NULL_TREE
20747 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20748 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20750 gen_type_die (member_declared_type (decl), context_die);
20751 gen_field_die (decl, context_die);
20753 break;
20755 case PARM_DECL:
20756 if (DECL_BY_REFERENCE (decl_or_origin))
20757 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20758 else
20759 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20760 return gen_formal_parameter_die (decl, origin,
20761 true /* Emit name attribute. */,
20762 context_die);
20764 case NAMESPACE_DECL:
20765 case IMPORTED_DECL:
20766 if (dwarf_version >= 3 || !dwarf_strict)
20767 gen_namespace_die (decl, context_die);
20768 break;
20770 case NAMELIST_DECL:
20771 gen_namelist_decl (DECL_NAME (decl), context_die,
20772 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20773 break;
20775 default:
20776 /* Probably some frontend-internal decl. Assume we don't care. */
20777 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20778 break;
20781 return NULL;
20784 /* Output debug information for global decl DECL. Called from toplev.c after
20785 compilation proper has finished. */
20787 static void
20788 dwarf2out_global_decl (tree decl)
20790 /* Output DWARF2 information for file-scope tentative data object
20791 declarations, file-scope (extern) function declarations (which
20792 had no corresponding body) and file-scope tagged type declarations
20793 and definitions which have not yet been forced out. */
20794 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20795 dwarf2out_decl (decl);
20798 /* Output debug information for type decl DECL. Called from toplev.c
20799 and from language front ends (to record built-in types). */
20800 static void
20801 dwarf2out_type_decl (tree decl, int local)
20803 if (!local)
20804 dwarf2out_decl (decl);
20807 /* Output debug information for imported module or decl DECL.
20808 NAME is non-NULL name in the lexical block if the decl has been renamed.
20809 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20810 that DECL belongs to.
20811 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20812 static void
20813 dwarf2out_imported_module_or_decl_1 (tree decl,
20814 tree name,
20815 tree lexical_block,
20816 dw_die_ref lexical_block_die)
20818 expanded_location xloc;
20819 dw_die_ref imported_die = NULL;
20820 dw_die_ref at_import_die;
20822 if (TREE_CODE (decl) == IMPORTED_DECL)
20824 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20825 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20826 gcc_assert (decl);
20828 else
20829 xloc = expand_location (input_location);
20831 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20833 at_import_die = force_type_die (TREE_TYPE (decl));
20834 /* For namespace N { typedef void T; } using N::T; base_type_die
20835 returns NULL, but DW_TAG_imported_declaration requires
20836 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20837 if (!at_import_die)
20839 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20840 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20841 at_import_die = lookup_type_die (TREE_TYPE (decl));
20842 gcc_assert (at_import_die);
20845 else
20847 at_import_die = lookup_decl_die (decl);
20848 if (!at_import_die)
20850 /* If we're trying to avoid duplicate debug info, we may not have
20851 emitted the member decl for this field. Emit it now. */
20852 if (TREE_CODE (decl) == FIELD_DECL)
20854 tree type = DECL_CONTEXT (decl);
20856 if (TYPE_CONTEXT (type)
20857 && TYPE_P (TYPE_CONTEXT (type))
20858 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20859 DINFO_USAGE_DIR_USE))
20860 return;
20861 gen_type_die_for_member (type, decl,
20862 get_context_die (TYPE_CONTEXT (type)));
20864 if (TREE_CODE (decl) == NAMELIST_DECL)
20865 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20866 get_context_die (DECL_CONTEXT (decl)),
20867 NULL_TREE);
20868 else
20869 at_import_die = force_decl_die (decl);
20873 if (TREE_CODE (decl) == NAMESPACE_DECL)
20875 if (dwarf_version >= 3 || !dwarf_strict)
20876 imported_die = new_die (DW_TAG_imported_module,
20877 lexical_block_die,
20878 lexical_block);
20879 else
20880 return;
20882 else
20883 imported_die = new_die (DW_TAG_imported_declaration,
20884 lexical_block_die,
20885 lexical_block);
20887 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20888 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20889 if (name)
20890 add_AT_string (imported_die, DW_AT_name,
20891 IDENTIFIER_POINTER (name));
20892 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20895 /* Output debug information for imported module or decl DECL.
20896 NAME is non-NULL name in context if the decl has been renamed.
20897 CHILD is true if decl is one of the renamed decls as part of
20898 importing whole module. */
20900 static void
20901 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20902 bool child)
20904 /* dw_die_ref at_import_die; */
20905 dw_die_ref scope_die;
20907 if (debug_info_level <= DINFO_LEVEL_TERSE)
20908 return;
20910 gcc_assert (decl);
20912 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20913 We need decl DIE for reference and scope die. First, get DIE for the decl
20914 itself. */
20916 /* Get the scope die for decl context. Use comp_unit_die for global module
20917 or decl. If die is not found for non globals, force new die. */
20918 if (context
20919 && TYPE_P (context)
20920 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20921 return;
20923 if (!(dwarf_version >= 3 || !dwarf_strict))
20924 return;
20926 scope_die = get_context_die (context);
20928 if (child)
20930 gcc_assert (scope_die->die_child);
20931 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20932 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20933 scope_die = scope_die->die_child;
20936 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20937 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20941 /* Output debug information for namelists. */
20943 static dw_die_ref
20944 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20946 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20947 tree value;
20948 unsigned i;
20950 if (debug_info_level <= DINFO_LEVEL_TERSE)
20951 return NULL;
20953 gcc_assert (scope_die != NULL);
20954 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20955 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20957 /* If there are no item_decls, we have a nondefining namelist, e.g.
20958 with USE association; hence, set DW_AT_declaration. */
20959 if (item_decls == NULL_TREE)
20961 add_AT_flag (nml_die, DW_AT_declaration, 1);
20962 return nml_die;
20965 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20967 nml_item_ref_die = lookup_decl_die (value);
20968 if (!nml_item_ref_die)
20969 nml_item_ref_die = force_decl_die (value);
20971 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20972 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20974 return nml_die;
20978 /* Write the debugging output for DECL. */
20980 static void
20981 dwarf2out_decl (tree decl)
20983 dw_die_ref context_die = comp_unit_die ();
20985 switch (TREE_CODE (decl))
20987 case ERROR_MARK:
20988 return;
20990 case FUNCTION_DECL:
20991 /* What we would really like to do here is to filter out all mere
20992 file-scope declarations of file-scope functions which are never
20993 referenced later within this translation unit (and keep all of ones
20994 that *are* referenced later on) but we aren't clairvoyant, so we have
20995 no idea which functions will be referenced in the future (i.e. later
20996 on within the current translation unit). So here we just ignore all
20997 file-scope function declarations which are not also definitions. If
20998 and when the debugger needs to know something about these functions,
20999 it will have to hunt around and find the DWARF information associated
21000 with the definition of the function.
21002 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21003 nodes represent definitions and which ones represent mere
21004 declarations. We have to check DECL_INITIAL instead. That's because
21005 the C front-end supports some weird semantics for "extern inline"
21006 function definitions. These can get inlined within the current
21007 translation unit (and thus, we need to generate Dwarf info for their
21008 abstract instances so that the Dwarf info for the concrete inlined
21009 instances can have something to refer to) but the compiler never
21010 generates any out-of-lines instances of such things (despite the fact
21011 that they *are* definitions).
21013 The important point is that the C front-end marks these "extern
21014 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21015 them anyway. Note that the C++ front-end also plays some similar games
21016 for inline function definitions appearing within include files which
21017 also contain `#pragma interface' pragmas.
21019 If we are called from dwarf2out_abstract_function output a DIE
21020 anyway. We can end up here this way with early inlining and LTO
21021 where the inlined function is output in a different LTRANS unit
21022 or not at all. */
21023 if (DECL_INITIAL (decl) == NULL_TREE
21024 && ! DECL_ABSTRACT_P (decl))
21025 return;
21027 /* If we're a nested function, initially use a parent of NULL; if we're
21028 a plain function, this will be fixed up in decls_for_scope. If
21029 we're a method, it will be ignored, since we already have a DIE. */
21030 if (decl_function_context (decl)
21031 /* But if we're in terse mode, we don't care about scope. */
21032 && debug_info_level > DINFO_LEVEL_TERSE)
21033 context_die = NULL;
21034 break;
21036 case VAR_DECL:
21037 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21038 declaration and if the declaration was never even referenced from
21039 within this entire compilation unit. We suppress these DIEs in
21040 order to save space in the .debug section (by eliminating entries
21041 which are probably useless). Note that we must not suppress
21042 block-local extern declarations (whether used or not) because that
21043 would screw-up the debugger's name lookup mechanism and cause it to
21044 miss things which really ought to be in scope at a given point. */
21045 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21046 return;
21048 /* For local statics lookup proper context die. */
21049 if (TREE_STATIC (decl)
21050 && DECL_CONTEXT (decl)
21051 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21052 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21054 /* If we are in terse mode, don't generate any DIEs to represent any
21055 variable declarations or definitions. */
21056 if (debug_info_level <= DINFO_LEVEL_TERSE)
21057 return;
21058 break;
21060 case CONST_DECL:
21061 if (debug_info_level <= DINFO_LEVEL_TERSE)
21062 return;
21063 if (!is_fortran () && !is_ada ())
21064 return;
21065 if (TREE_STATIC (decl) && decl_function_context (decl))
21066 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21067 break;
21069 case NAMESPACE_DECL:
21070 case IMPORTED_DECL:
21071 if (debug_info_level <= DINFO_LEVEL_TERSE)
21072 return;
21073 if (lookup_decl_die (decl) != NULL)
21074 return;
21075 break;
21077 case TYPE_DECL:
21078 /* Don't emit stubs for types unless they are needed by other DIEs. */
21079 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21080 return;
21082 /* Don't bother trying to generate any DIEs to represent any of the
21083 normal built-in types for the language we are compiling. */
21084 if (DECL_IS_BUILTIN (decl))
21085 return;
21087 /* If we are in terse mode, don't generate any DIEs for types. */
21088 if (debug_info_level <= DINFO_LEVEL_TERSE)
21089 return;
21091 /* If we're a function-scope tag, initially use a parent of NULL;
21092 this will be fixed up in decls_for_scope. */
21093 if (decl_function_context (decl))
21094 context_die = NULL;
21096 break;
21098 case NAMELIST_DECL:
21099 break;
21101 default:
21102 return;
21105 gen_decl_die (decl, NULL, context_die);
21108 /* Write the debugging output for DECL. */
21110 static void
21111 dwarf2out_function_decl (tree decl)
21113 dwarf2out_decl (decl);
21114 call_arg_locations = NULL;
21115 call_arg_loc_last = NULL;
21116 call_site_count = -1;
21117 tail_call_site_count = -1;
21118 block_map.release ();
21119 decl_loc_table->empty ();
21120 cached_dw_loc_list_table->empty ();
21123 /* Output a marker (i.e. a label) for the beginning of the generated code for
21124 a lexical block. */
21126 static void
21127 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21128 unsigned int blocknum)
21130 switch_to_section (current_function_section ());
21131 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21134 /* Output a marker (i.e. a label) for the end of the generated code for a
21135 lexical block. */
21137 static void
21138 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21140 switch_to_section (current_function_section ());
21141 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21144 /* Returns nonzero if it is appropriate not to emit any debugging
21145 information for BLOCK, because it doesn't contain any instructions.
21147 Don't allow this for blocks with nested functions or local classes
21148 as we would end up with orphans, and in the presence of scheduling
21149 we may end up calling them anyway. */
21151 static bool
21152 dwarf2out_ignore_block (const_tree block)
21154 tree decl;
21155 unsigned int i;
21157 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21158 if (TREE_CODE (decl) == FUNCTION_DECL
21159 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21160 return 0;
21161 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21163 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21164 if (TREE_CODE (decl) == FUNCTION_DECL
21165 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21166 return 0;
21169 return 1;
21172 /* Hash table routines for file_hash. */
21174 bool
21175 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21177 return filename_cmp (p1->filename, p2) == 0;
21180 hashval_t
21181 dwarf_file_hasher::hash (dwarf_file_data *p)
21183 return htab_hash_string (p->filename);
21186 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21187 dwarf2out.c) and return its "index". The index of each (known) filename is
21188 just a unique number which is associated with only that one filename. We
21189 need such numbers for the sake of generating labels (in the .debug_sfnames
21190 section) and references to those files numbers (in the .debug_srcinfo
21191 and.debug_macinfo sections). If the filename given as an argument is not
21192 found in our current list, add it to the list and assign it the next
21193 available unique index number. In order to speed up searches, we remember
21194 the index of the filename was looked up last. This handles the majority of
21195 all searches. */
21197 static struct dwarf_file_data *
21198 lookup_filename (const char *file_name)
21200 struct dwarf_file_data * created;
21202 /* Check to see if the file name that was searched on the previous
21203 call matches this file name. If so, return the index. */
21204 if (file_table_last_lookup
21205 && (file_name == file_table_last_lookup->filename
21206 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21207 return file_table_last_lookup;
21209 /* Didn't match the previous lookup, search the table. */
21210 dwarf_file_data **slot
21211 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21212 INSERT);
21213 if (*slot)
21214 return *slot;
21216 created = ggc_alloc<dwarf_file_data> ();
21217 created->filename = file_name;
21218 created->emitted_number = 0;
21219 *slot = created;
21220 return created;
21223 /* If the assembler will construct the file table, then translate the compiler
21224 internal file table number into the assembler file table number, and emit
21225 a .file directive if we haven't already emitted one yet. The file table
21226 numbers are different because we prune debug info for unused variables and
21227 types, which may include filenames. */
21229 static int
21230 maybe_emit_file (struct dwarf_file_data * fd)
21232 if (! fd->emitted_number)
21234 if (last_emitted_file)
21235 fd->emitted_number = last_emitted_file->emitted_number + 1;
21236 else
21237 fd->emitted_number = 1;
21238 last_emitted_file = fd;
21240 if (DWARF2_ASM_LINE_DEBUG_INFO)
21242 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21243 output_quoted_string (asm_out_file,
21244 remap_debug_filename (fd->filename));
21245 fputc ('\n', asm_out_file);
21249 return fd->emitted_number;
21252 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21253 That generation should happen after function debug info has been
21254 generated. The value of the attribute is the constant value of ARG. */
21256 static void
21257 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21259 die_arg_entry entry;
21261 if (!die || !arg)
21262 return;
21264 if (!tmpl_value_parm_die_table)
21265 vec_alloc (tmpl_value_parm_die_table, 32);
21267 entry.die = die;
21268 entry.arg = arg;
21269 vec_safe_push (tmpl_value_parm_die_table, entry);
21272 /* Return TRUE if T is an instance of generic type, FALSE
21273 otherwise. */
21275 static bool
21276 generic_type_p (tree t)
21278 if (t == NULL_TREE || !TYPE_P (t))
21279 return false;
21280 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21283 /* Schedule the generation of the generic parameter dies for the
21284 instance of generic type T. The proper generation itself is later
21285 done by gen_scheduled_generic_parms_dies. */
21287 static void
21288 schedule_generic_params_dies_gen (tree t)
21290 if (!generic_type_p (t))
21291 return;
21293 if (!generic_type_instances)
21294 vec_alloc (generic_type_instances, 256);
21296 vec_safe_push (generic_type_instances, t);
21299 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21300 by append_entry_to_tmpl_value_parm_die_table. This function must
21301 be called after function DIEs have been generated. */
21303 static void
21304 gen_remaining_tmpl_value_param_die_attribute (void)
21306 if (tmpl_value_parm_die_table)
21308 unsigned i;
21309 die_arg_entry *e;
21311 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21312 tree_add_const_value_attribute (e->die, e->arg);
21316 /* Generate generic parameters DIEs for instances of generic types
21317 that have been previously scheduled by
21318 schedule_generic_params_dies_gen. This function must be called
21319 after all the types of the CU have been laid out. */
21321 static void
21322 gen_scheduled_generic_parms_dies (void)
21324 unsigned i;
21325 tree t;
21327 if (!generic_type_instances)
21328 return;
21330 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21331 if (COMPLETE_TYPE_P (t))
21332 gen_generic_params_dies (t);
21336 /* Replace DW_AT_name for the decl with name. */
21338 static void
21339 dwarf2out_set_name (tree decl, tree name)
21341 dw_die_ref die;
21342 dw_attr_ref attr;
21343 const char *dname;
21345 die = TYPE_SYMTAB_DIE (decl);
21346 if (!die)
21347 return;
21349 dname = dwarf2_name (name, 0);
21350 if (!dname)
21351 return;
21353 attr = get_AT (die, DW_AT_name);
21354 if (attr)
21356 struct indirect_string_node *node;
21358 node = find_AT_string (dname);
21359 /* replace the string. */
21360 attr->dw_attr_val.v.val_str = node;
21363 else
21364 add_name_attribute (die, dname);
21367 /* True if before or during processing of the first function being emitted. */
21368 static bool in_first_function_p = true;
21369 /* True if loc_note during dwarf2out_var_location call might still be
21370 before first real instruction at address equal to .Ltext0. */
21371 static bool maybe_at_text_label_p = true;
21372 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21373 static unsigned int first_loclabel_num_not_at_text_label;
21375 /* Called by the final INSN scan whenever we see a var location. We
21376 use it to drop labels in the right places, and throw the location in
21377 our lookup table. */
21379 static void
21380 dwarf2out_var_location (rtx_insn *loc_note)
21382 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21383 struct var_loc_node *newloc;
21384 rtx_insn *next_real, *next_note;
21385 static const char *last_label;
21386 static const char *last_postcall_label;
21387 static bool last_in_cold_section_p;
21388 static rtx_insn *expected_next_loc_note;
21389 tree decl;
21390 bool var_loc_p;
21392 if (!NOTE_P (loc_note))
21394 if (CALL_P (loc_note))
21396 call_site_count++;
21397 if (SIBLING_CALL_P (loc_note))
21398 tail_call_site_count++;
21400 return;
21403 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21404 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21405 return;
21407 /* Optimize processing a large consecutive sequence of location
21408 notes so we don't spend too much time in next_real_insn. If the
21409 next insn is another location note, remember the next_real_insn
21410 calculation for next time. */
21411 next_real = cached_next_real_insn;
21412 if (next_real)
21414 if (expected_next_loc_note != loc_note)
21415 next_real = NULL;
21418 next_note = NEXT_INSN (loc_note);
21419 if (! next_note
21420 || next_note->deleted ()
21421 || ! NOTE_P (next_note)
21422 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21423 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21424 next_note = NULL;
21426 if (! next_real)
21427 next_real = next_real_insn (loc_note);
21429 if (next_note)
21431 expected_next_loc_note = next_note;
21432 cached_next_real_insn = next_real;
21434 else
21435 cached_next_real_insn = NULL;
21437 /* If there are no instructions which would be affected by this note,
21438 don't do anything. */
21439 if (var_loc_p
21440 && next_real == NULL_RTX
21441 && !NOTE_DURING_CALL_P (loc_note))
21442 return;
21444 if (next_real == NULL_RTX)
21445 next_real = get_last_insn ();
21447 /* If there were any real insns between note we processed last time
21448 and this note (or if it is the first note), clear
21449 last_{,postcall_}label so that they are not reused this time. */
21450 if (last_var_location_insn == NULL_RTX
21451 || last_var_location_insn != next_real
21452 || last_in_cold_section_p != in_cold_section_p)
21454 last_label = NULL;
21455 last_postcall_label = NULL;
21458 if (var_loc_p)
21460 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21461 newloc = add_var_loc_to_decl (decl, loc_note,
21462 NOTE_DURING_CALL_P (loc_note)
21463 ? last_postcall_label : last_label);
21464 if (newloc == NULL)
21465 return;
21467 else
21469 decl = NULL_TREE;
21470 newloc = NULL;
21473 /* If there were no real insns between note we processed last time
21474 and this note, use the label we emitted last time. Otherwise
21475 create a new label and emit it. */
21476 if (last_label == NULL)
21478 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21479 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21480 loclabel_num++;
21481 last_label = ggc_strdup (loclabel);
21482 /* See if loclabel might be equal to .Ltext0. If yes,
21483 bump first_loclabel_num_not_at_text_label. */
21484 if (!have_multiple_function_sections
21485 && in_first_function_p
21486 && maybe_at_text_label_p)
21488 static rtx_insn *last_start;
21489 rtx_insn *insn;
21490 for (insn = loc_note; insn; insn = previous_insn (insn))
21491 if (insn == last_start)
21492 break;
21493 else if (!NONDEBUG_INSN_P (insn))
21494 continue;
21495 else
21497 rtx body = PATTERN (insn);
21498 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21499 continue;
21500 /* Inline asm could occupy zero bytes. */
21501 else if (GET_CODE (body) == ASM_INPUT
21502 || asm_noperands (body) >= 0)
21503 continue;
21504 #ifdef HAVE_attr_length
21505 else if (get_attr_min_length (insn) == 0)
21506 continue;
21507 #endif
21508 else
21510 /* Assume insn has non-zero length. */
21511 maybe_at_text_label_p = false;
21512 break;
21515 if (maybe_at_text_label_p)
21517 last_start = loc_note;
21518 first_loclabel_num_not_at_text_label = loclabel_num;
21523 if (!var_loc_p)
21525 struct call_arg_loc_node *ca_loc
21526 = ggc_cleared_alloc<call_arg_loc_node> ();
21527 rtx_insn *prev = prev_real_insn (loc_note);
21528 rtx x;
21529 ca_loc->call_arg_loc_note = loc_note;
21530 ca_loc->next = NULL;
21531 ca_loc->label = last_label;
21532 gcc_assert (prev
21533 && (CALL_P (prev)
21534 || (NONJUMP_INSN_P (prev)
21535 && GET_CODE (PATTERN (prev)) == SEQUENCE
21536 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21537 if (!CALL_P (prev))
21538 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21539 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21540 x = get_call_rtx_from (PATTERN (prev));
21541 if (x)
21543 x = XEXP (XEXP (x, 0), 0);
21544 if (GET_CODE (x) == SYMBOL_REF
21545 && SYMBOL_REF_DECL (x)
21546 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21547 ca_loc->symbol_ref = x;
21549 ca_loc->block = insn_scope (prev);
21550 if (call_arg_locations)
21551 call_arg_loc_last->next = ca_loc;
21552 else
21553 call_arg_locations = ca_loc;
21554 call_arg_loc_last = ca_loc;
21556 else if (!NOTE_DURING_CALL_P (loc_note))
21557 newloc->label = last_label;
21558 else
21560 if (!last_postcall_label)
21562 sprintf (loclabel, "%s-1", last_label);
21563 last_postcall_label = ggc_strdup (loclabel);
21565 newloc->label = last_postcall_label;
21568 last_var_location_insn = next_real;
21569 last_in_cold_section_p = in_cold_section_p;
21572 /* Note in one location list that text section has changed. */
21575 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21577 var_loc_list *list = *slot;
21578 if (list->first)
21579 list->last_before_switch
21580 = list->last->next ? list->last->next : list->last;
21581 return 1;
21584 /* Note in all location lists that text section has changed. */
21586 static void
21587 var_location_switch_text_section (void)
21589 if (decl_loc_table == NULL)
21590 return;
21592 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21595 /* Create a new line number table. */
21597 static dw_line_info_table *
21598 new_line_info_table (void)
21600 dw_line_info_table *table;
21602 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21603 table->file_num = 1;
21604 table->line_num = 1;
21605 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21607 return table;
21610 /* Lookup the "current" table into which we emit line info, so
21611 that we don't have to do it for every source line. */
21613 static void
21614 set_cur_line_info_table (section *sec)
21616 dw_line_info_table *table;
21618 if (sec == text_section)
21619 table = text_section_line_info;
21620 else if (sec == cold_text_section)
21622 table = cold_text_section_line_info;
21623 if (!table)
21625 cold_text_section_line_info = table = new_line_info_table ();
21626 table->end_label = cold_end_label;
21629 else
21631 const char *end_label;
21633 if (flag_reorder_blocks_and_partition)
21635 if (in_cold_section_p)
21636 end_label = crtl->subsections.cold_section_end_label;
21637 else
21638 end_label = crtl->subsections.hot_section_end_label;
21640 else
21642 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21643 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21644 current_function_funcdef_no);
21645 end_label = ggc_strdup (label);
21648 table = new_line_info_table ();
21649 table->end_label = end_label;
21651 vec_safe_push (separate_line_info, table);
21654 if (DWARF2_ASM_LINE_DEBUG_INFO)
21655 table->is_stmt = (cur_line_info_table
21656 ? cur_line_info_table->is_stmt
21657 : DWARF_LINE_DEFAULT_IS_STMT_START);
21658 cur_line_info_table = table;
21662 /* We need to reset the locations at the beginning of each
21663 function. We can't do this in the end_function hook, because the
21664 declarations that use the locations won't have been output when
21665 that hook is called. Also compute have_multiple_function_sections here. */
21667 static void
21668 dwarf2out_begin_function (tree fun)
21670 section *sec = function_section (fun);
21672 if (sec != text_section)
21673 have_multiple_function_sections = true;
21675 if (flag_reorder_blocks_and_partition && !cold_text_section)
21677 gcc_assert (current_function_decl == fun);
21678 cold_text_section = unlikely_text_section ();
21679 switch_to_section (cold_text_section);
21680 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21681 switch_to_section (sec);
21684 dwarf2out_note_section_used ();
21685 call_site_count = 0;
21686 tail_call_site_count = 0;
21688 set_cur_line_info_table (sec);
21691 /* Helper function of dwarf2out_end_function, called only after emitting
21692 the very first function into assembly. Check if some .debug_loc range
21693 might end with a .LVL* label that could be equal to .Ltext0.
21694 In that case we must force using absolute addresses in .debug_loc ranges,
21695 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21696 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21697 list terminator.
21698 Set have_multiple_function_sections to true in that case and
21699 terminate htab traversal. */
21702 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21704 var_loc_list *entry = *slot;
21705 struct var_loc_node *node;
21707 node = entry->first;
21708 if (node && node->next && node->next->label)
21710 unsigned int i;
21711 const char *label = node->next->label;
21712 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21714 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21716 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21717 if (strcmp (label, loclabel) == 0)
21719 have_multiple_function_sections = true;
21720 return 0;
21724 return 1;
21727 /* Hook called after emitting a function into assembly.
21728 This does something only for the very first function emitted. */
21730 static void
21731 dwarf2out_end_function (unsigned int)
21733 if (in_first_function_p
21734 && !have_multiple_function_sections
21735 && first_loclabel_num_not_at_text_label
21736 && decl_loc_table)
21737 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
21738 in_first_function_p = false;
21739 maybe_at_text_label_p = false;
21742 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21744 static void
21745 push_dw_line_info_entry (dw_line_info_table *table,
21746 enum dw_line_info_opcode opcode, unsigned int val)
21748 dw_line_info_entry e;
21749 e.opcode = opcode;
21750 e.val = val;
21751 vec_safe_push (table->entries, e);
21754 /* Output a label to mark the beginning of a source code line entry
21755 and record information relating to this source line, in
21756 'line_info_table' for later output of the .debug_line section. */
21757 /* ??? The discriminator parameter ought to be unsigned. */
21759 static void
21760 dwarf2out_source_line (unsigned int line, const char *filename,
21761 int discriminator, bool is_stmt)
21763 unsigned int file_num;
21764 dw_line_info_table *table;
21766 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21767 return;
21769 /* The discriminator column was added in dwarf4. Simplify the below
21770 by simply removing it if we're not supposed to output it. */
21771 if (dwarf_version < 4 && dwarf_strict)
21772 discriminator = 0;
21774 table = cur_line_info_table;
21775 file_num = maybe_emit_file (lookup_filename (filename));
21777 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21778 the debugger has used the second (possibly duplicate) line number
21779 at the beginning of the function to mark the end of the prologue.
21780 We could eliminate any other duplicates within the function. For
21781 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21782 that second line number entry. */
21783 /* Recall that this end-of-prologue indication is *not* the same thing
21784 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21785 to which the hook corresponds, follows the last insn that was
21786 emitted by gen_prologue. What we need is to precede the first insn
21787 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21788 insn that corresponds to something the user wrote. These may be
21789 very different locations once scheduling is enabled. */
21791 if (0 && file_num == table->file_num
21792 && line == table->line_num
21793 && discriminator == table->discrim_num
21794 && is_stmt == table->is_stmt)
21795 return;
21797 switch_to_section (current_function_section ());
21799 /* If requested, emit something human-readable. */
21800 if (flag_debug_asm)
21801 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21803 if (DWARF2_ASM_LINE_DEBUG_INFO)
21805 /* Emit the .loc directive understood by GNU as. */
21806 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21807 file_num, line, is_stmt, discriminator */
21808 fputs ("\t.loc ", asm_out_file);
21809 fprint_ul (asm_out_file, file_num);
21810 putc (' ', asm_out_file);
21811 fprint_ul (asm_out_file, line);
21812 putc (' ', asm_out_file);
21813 putc ('0', asm_out_file);
21815 if (is_stmt != table->is_stmt)
21817 fputs (" is_stmt ", asm_out_file);
21818 putc (is_stmt ? '1' : '0', asm_out_file);
21820 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21822 gcc_assert (discriminator > 0);
21823 fputs (" discriminator ", asm_out_file);
21824 fprint_ul (asm_out_file, (unsigned long) discriminator);
21826 putc ('\n', asm_out_file);
21828 else
21830 unsigned int label_num = ++line_info_label_num;
21832 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21834 push_dw_line_info_entry (table, LI_set_address, label_num);
21835 if (file_num != table->file_num)
21836 push_dw_line_info_entry (table, LI_set_file, file_num);
21837 if (discriminator != table->discrim_num)
21838 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21839 if (is_stmt != table->is_stmt)
21840 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21841 push_dw_line_info_entry (table, LI_set_line, line);
21844 table->file_num = file_num;
21845 table->line_num = line;
21846 table->discrim_num = discriminator;
21847 table->is_stmt = is_stmt;
21848 table->in_use = true;
21851 /* Record the beginning of a new source file. */
21853 static void
21854 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21856 if (flag_eliminate_dwarf2_dups)
21858 /* Record the beginning of the file for break_out_includes. */
21859 dw_die_ref bincl_die;
21861 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21862 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21865 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21867 macinfo_entry e;
21868 e.code = DW_MACINFO_start_file;
21869 e.lineno = lineno;
21870 e.info = ggc_strdup (filename);
21871 vec_safe_push (macinfo_table, e);
21875 /* Record the end of a source file. */
21877 static void
21878 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21880 if (flag_eliminate_dwarf2_dups)
21881 /* Record the end of the file for break_out_includes. */
21882 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21884 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21886 macinfo_entry e;
21887 e.code = DW_MACINFO_end_file;
21888 e.lineno = lineno;
21889 e.info = NULL;
21890 vec_safe_push (macinfo_table, e);
21894 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21895 the tail part of the directive line, i.e. the part which is past the
21896 initial whitespace, #, whitespace, directive-name, whitespace part. */
21898 static void
21899 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21900 const char *buffer ATTRIBUTE_UNUSED)
21902 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21904 macinfo_entry e;
21905 /* Insert a dummy first entry to be able to optimize the whole
21906 predefined macro block using DW_MACRO_GNU_transparent_include. */
21907 if (macinfo_table->is_empty () && lineno <= 1)
21909 e.code = 0;
21910 e.lineno = 0;
21911 e.info = NULL;
21912 vec_safe_push (macinfo_table, e);
21914 e.code = DW_MACINFO_define;
21915 e.lineno = lineno;
21916 e.info = ggc_strdup (buffer);
21917 vec_safe_push (macinfo_table, e);
21921 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21922 the tail part of the directive line, i.e. the part which is past the
21923 initial whitespace, #, whitespace, directive-name, whitespace part. */
21925 static void
21926 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21927 const char *buffer ATTRIBUTE_UNUSED)
21929 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21931 macinfo_entry e;
21932 /* Insert a dummy first entry to be able to optimize the whole
21933 predefined macro block using DW_MACRO_GNU_transparent_include. */
21934 if (macinfo_table->is_empty () && lineno <= 1)
21936 e.code = 0;
21937 e.lineno = 0;
21938 e.info = NULL;
21939 vec_safe_push (macinfo_table, e);
21941 e.code = DW_MACINFO_undef;
21942 e.lineno = lineno;
21943 e.info = ggc_strdup (buffer);
21944 vec_safe_push (macinfo_table, e);
21948 /* Helpers to manipulate hash table of CUs. */
21950 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21952 typedef macinfo_entry value_type;
21953 typedef macinfo_entry compare_type;
21954 static inline hashval_t hash (const value_type *);
21955 static inline bool equal (const value_type *, const compare_type *);
21958 inline hashval_t
21959 macinfo_entry_hasher::hash (const value_type *entry)
21961 return htab_hash_string (entry->info);
21964 inline bool
21965 macinfo_entry_hasher::equal (const value_type *entry1,
21966 const compare_type *entry2)
21968 return !strcmp (entry1->info, entry2->info);
21971 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
21973 /* Output a single .debug_macinfo entry. */
21975 static void
21976 output_macinfo_op (macinfo_entry *ref)
21978 int file_num;
21979 size_t len;
21980 struct indirect_string_node *node;
21981 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21982 struct dwarf_file_data *fd;
21984 switch (ref->code)
21986 case DW_MACINFO_start_file:
21987 fd = lookup_filename (ref->info);
21988 file_num = maybe_emit_file (fd);
21989 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21990 dw2_asm_output_data_uleb128 (ref->lineno,
21991 "Included from line number %lu",
21992 (unsigned long) ref->lineno);
21993 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21994 break;
21995 case DW_MACINFO_end_file:
21996 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21997 break;
21998 case DW_MACINFO_define:
21999 case DW_MACINFO_undef:
22000 len = strlen (ref->info) + 1;
22001 if (!dwarf_strict
22002 && len > DWARF_OFFSET_SIZE
22003 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22004 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22006 ref->code = ref->code == DW_MACINFO_define
22007 ? DW_MACRO_GNU_define_indirect
22008 : DW_MACRO_GNU_undef_indirect;
22009 output_macinfo_op (ref);
22010 return;
22012 dw2_asm_output_data (1, ref->code,
22013 ref->code == DW_MACINFO_define
22014 ? "Define macro" : "Undefine macro");
22015 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22016 (unsigned long) ref->lineno);
22017 dw2_asm_output_nstring (ref->info, -1, "The macro");
22018 break;
22019 case DW_MACRO_GNU_define_indirect:
22020 case DW_MACRO_GNU_undef_indirect:
22021 node = find_AT_string (ref->info);
22022 gcc_assert (node
22023 && ((node->form == DW_FORM_strp)
22024 || (node->form == DW_FORM_GNU_str_index)));
22025 dw2_asm_output_data (1, ref->code,
22026 ref->code == DW_MACRO_GNU_define_indirect
22027 ? "Define macro indirect"
22028 : "Undefine macro indirect");
22029 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22030 (unsigned long) ref->lineno);
22031 if (node->form == DW_FORM_strp)
22032 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22033 debug_str_section, "The macro: \"%s\"",
22034 ref->info);
22035 else
22036 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22037 ref->info);
22038 break;
22039 case DW_MACRO_GNU_transparent_include:
22040 dw2_asm_output_data (1, ref->code, "Transparent include");
22041 ASM_GENERATE_INTERNAL_LABEL (label,
22042 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22043 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22044 break;
22045 default:
22046 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22047 ASM_COMMENT_START, (unsigned long) ref->code);
22048 break;
22052 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22053 other compilation unit .debug_macinfo sections. IDX is the first
22054 index of a define/undef, return the number of ops that should be
22055 emitted in a comdat .debug_macinfo section and emit
22056 a DW_MACRO_GNU_transparent_include entry referencing it.
22057 If the define/undef entry should be emitted normally, return 0. */
22059 static unsigned
22060 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22061 macinfo_hash_type **macinfo_htab)
22063 macinfo_entry *first, *second, *cur, *inc;
22064 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22065 unsigned char checksum[16];
22066 struct md5_ctx ctx;
22067 char *grp_name, *tail;
22068 const char *base;
22069 unsigned int i, count, encoded_filename_len, linebuf_len;
22070 macinfo_entry **slot;
22072 first = &(*macinfo_table)[idx];
22073 second = &(*macinfo_table)[idx + 1];
22075 /* Optimize only if there are at least two consecutive define/undef ops,
22076 and either all of them are before first DW_MACINFO_start_file
22077 with lineno {0,1} (i.e. predefined macro block), or all of them are
22078 in some included header file. */
22079 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22080 return 0;
22081 if (vec_safe_is_empty (files))
22083 if (first->lineno > 1 || second->lineno > 1)
22084 return 0;
22086 else if (first->lineno == 0)
22087 return 0;
22089 /* Find the last define/undef entry that can be grouped together
22090 with first and at the same time compute md5 checksum of their
22091 codes, linenumbers and strings. */
22092 md5_init_ctx (&ctx);
22093 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22094 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22095 break;
22096 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22097 break;
22098 else
22100 unsigned char code = cur->code;
22101 md5_process_bytes (&code, 1, &ctx);
22102 checksum_uleb128 (cur->lineno, &ctx);
22103 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22105 md5_finish_ctx (&ctx, checksum);
22106 count = i - idx;
22108 /* From the containing include filename (if any) pick up just
22109 usable characters from its basename. */
22110 if (vec_safe_is_empty (files))
22111 base = "";
22112 else
22113 base = lbasename (files->last ().info);
22114 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22115 if (ISIDNUM (base[i]) || base[i] == '.')
22116 encoded_filename_len++;
22117 /* Count . at the end. */
22118 if (encoded_filename_len)
22119 encoded_filename_len++;
22121 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22122 linebuf_len = strlen (linebuf);
22124 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22125 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22126 + 16 * 2 + 1);
22127 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22128 tail = grp_name + 4;
22129 if (encoded_filename_len)
22131 for (i = 0; base[i]; i++)
22132 if (ISIDNUM (base[i]) || base[i] == '.')
22133 *tail++ = base[i];
22134 *tail++ = '.';
22136 memcpy (tail, linebuf, linebuf_len);
22137 tail += linebuf_len;
22138 *tail++ = '.';
22139 for (i = 0; i < 16; i++)
22140 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22142 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22143 in the empty vector entry before the first define/undef. */
22144 inc = &(*macinfo_table)[idx - 1];
22145 inc->code = DW_MACRO_GNU_transparent_include;
22146 inc->lineno = 0;
22147 inc->info = ggc_strdup (grp_name);
22148 if (!*macinfo_htab)
22149 *macinfo_htab = new macinfo_hash_type (10);
22150 /* Avoid emitting duplicates. */
22151 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22152 if (*slot != NULL)
22154 inc->code = 0;
22155 inc->info = NULL;
22156 /* If such an entry has been used before, just emit
22157 a DW_MACRO_GNU_transparent_include op. */
22158 inc = *slot;
22159 output_macinfo_op (inc);
22160 /* And clear all macinfo_entry in the range to avoid emitting them
22161 in the second pass. */
22162 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22164 cur->code = 0;
22165 cur->info = NULL;
22168 else
22170 *slot = inc;
22171 inc->lineno = (*macinfo_htab)->elements ();
22172 output_macinfo_op (inc);
22174 return count;
22177 /* Save any strings needed by the macinfo table in the debug str
22178 table. All strings must be collected into the table by the time
22179 index_string is called. */
22181 static void
22182 save_macinfo_strings (void)
22184 unsigned len;
22185 unsigned i;
22186 macinfo_entry *ref;
22188 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22190 switch (ref->code)
22192 /* Match the logic in output_macinfo_op to decide on
22193 indirect strings. */
22194 case DW_MACINFO_define:
22195 case DW_MACINFO_undef:
22196 len = strlen (ref->info) + 1;
22197 if (!dwarf_strict
22198 && len > DWARF_OFFSET_SIZE
22199 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22200 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22201 set_indirect_string (find_AT_string (ref->info));
22202 break;
22203 case DW_MACRO_GNU_define_indirect:
22204 case DW_MACRO_GNU_undef_indirect:
22205 set_indirect_string (find_AT_string (ref->info));
22206 break;
22207 default:
22208 break;
22213 /* Output macinfo section(s). */
22215 static void
22216 output_macinfo (void)
22218 unsigned i;
22219 unsigned long length = vec_safe_length (macinfo_table);
22220 macinfo_entry *ref;
22221 vec<macinfo_entry, va_gc> *files = NULL;
22222 macinfo_hash_type *macinfo_htab = NULL;
22224 if (! length)
22225 return;
22227 /* output_macinfo* uses these interchangeably. */
22228 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22229 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22230 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22231 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22233 /* For .debug_macro emit the section header. */
22234 if (!dwarf_strict)
22236 dw2_asm_output_data (2, 4, "DWARF macro version number");
22237 if (DWARF_OFFSET_SIZE == 8)
22238 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22239 else
22240 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22241 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22242 (!dwarf_split_debug_info ? debug_line_section_label
22243 : debug_skeleton_line_section_label),
22244 debug_line_section, NULL);
22247 /* In the first loop, it emits the primary .debug_macinfo section
22248 and after each emitted op the macinfo_entry is cleared.
22249 If a longer range of define/undef ops can be optimized using
22250 DW_MACRO_GNU_transparent_include, the
22251 DW_MACRO_GNU_transparent_include op is emitted and kept in
22252 the vector before the first define/undef in the range and the
22253 whole range of define/undef ops is not emitted and kept. */
22254 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22256 switch (ref->code)
22258 case DW_MACINFO_start_file:
22259 vec_safe_push (files, *ref);
22260 break;
22261 case DW_MACINFO_end_file:
22262 if (!vec_safe_is_empty (files))
22263 files->pop ();
22264 break;
22265 case DW_MACINFO_define:
22266 case DW_MACINFO_undef:
22267 if (!dwarf_strict
22268 && HAVE_COMDAT_GROUP
22269 && vec_safe_length (files) != 1
22270 && i > 0
22271 && i + 1 < length
22272 && (*macinfo_table)[i - 1].code == 0)
22274 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22275 if (count)
22277 i += count - 1;
22278 continue;
22281 break;
22282 case 0:
22283 /* A dummy entry may be inserted at the beginning to be able
22284 to optimize the whole block of predefined macros. */
22285 if (i == 0)
22286 continue;
22287 default:
22288 break;
22290 output_macinfo_op (ref);
22291 ref->info = NULL;
22292 ref->code = 0;
22295 if (!macinfo_htab)
22296 return;
22298 delete macinfo_htab;
22299 macinfo_htab = NULL;
22301 /* If any DW_MACRO_GNU_transparent_include were used, on those
22302 DW_MACRO_GNU_transparent_include entries terminate the
22303 current chain and switch to a new comdat .debug_macinfo
22304 section and emit the define/undef entries within it. */
22305 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22306 switch (ref->code)
22308 case 0:
22309 continue;
22310 case DW_MACRO_GNU_transparent_include:
22312 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22313 tree comdat_key = get_identifier (ref->info);
22314 /* Terminate the previous .debug_macinfo section. */
22315 dw2_asm_output_data (1, 0, "End compilation unit");
22316 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22317 SECTION_DEBUG
22318 | SECTION_LINKONCE,
22319 comdat_key);
22320 ASM_GENERATE_INTERNAL_LABEL (label,
22321 DEBUG_MACRO_SECTION_LABEL,
22322 ref->lineno);
22323 ASM_OUTPUT_LABEL (asm_out_file, label);
22324 ref->code = 0;
22325 ref->info = NULL;
22326 dw2_asm_output_data (2, 4, "DWARF macro version number");
22327 if (DWARF_OFFSET_SIZE == 8)
22328 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22329 else
22330 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22332 break;
22333 case DW_MACINFO_define:
22334 case DW_MACINFO_undef:
22335 output_macinfo_op (ref);
22336 ref->code = 0;
22337 ref->info = NULL;
22338 break;
22339 default:
22340 gcc_unreachable ();
22344 /* Set up for Dwarf output at the start of compilation. */
22346 static void
22347 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22349 /* Allocate the file_table. */
22350 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22352 /* Allocate the decl_die_table. */
22353 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22355 /* Allocate the decl_loc_table. */
22356 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22358 /* Allocate the cached_dw_loc_list_table. */
22359 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22361 /* Allocate the initial hunk of the decl_scope_table. */
22362 vec_alloc (decl_scope_table, 256);
22364 /* Allocate the initial hunk of the abbrev_die_table. */
22365 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22366 (ABBREV_DIE_TABLE_INCREMENT);
22367 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22368 /* Zero-th entry is allocated, but unused. */
22369 abbrev_die_table_in_use = 1;
22371 /* Allocate the pubtypes and pubnames vectors. */
22372 vec_alloc (pubname_table, 32);
22373 vec_alloc (pubtype_table, 32);
22375 vec_alloc (incomplete_types, 64);
22377 vec_alloc (used_rtx_array, 32);
22379 if (!dwarf_split_debug_info)
22381 debug_info_section = get_section (DEBUG_INFO_SECTION,
22382 SECTION_DEBUG, NULL);
22383 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22384 SECTION_DEBUG, NULL);
22385 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22386 SECTION_DEBUG, NULL);
22388 else
22390 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22391 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22392 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22393 SECTION_DEBUG | SECTION_EXCLUDE,
22394 NULL);
22395 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22396 SECTION_DEBUG, NULL);
22397 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22398 SECTION_DEBUG, NULL);
22399 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22400 SECTION_DEBUG, NULL);
22401 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22402 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22404 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22405 the main .o, but the skeleton_line goes into the split off dwo. */
22406 debug_skeleton_line_section
22407 = get_section (DEBUG_DWO_LINE_SECTION,
22408 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22409 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22410 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22411 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22412 SECTION_DEBUG | SECTION_EXCLUDE,
22413 NULL);
22414 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22415 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22416 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22417 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22418 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22419 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22421 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22422 SECTION_DEBUG, NULL);
22423 debug_macinfo_section = get_section (dwarf_strict
22424 ? DEBUG_MACINFO_SECTION
22425 : DEBUG_MACRO_SECTION,
22426 DEBUG_MACRO_SECTION_FLAGS, NULL);
22427 debug_line_section = get_section (DEBUG_LINE_SECTION,
22428 SECTION_DEBUG, NULL);
22429 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22430 SECTION_DEBUG, NULL);
22431 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22432 SECTION_DEBUG, NULL);
22433 debug_str_section = get_section (DEBUG_STR_SECTION,
22434 DEBUG_STR_SECTION_FLAGS, NULL);
22435 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22436 SECTION_DEBUG, NULL);
22437 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22438 SECTION_DEBUG, NULL);
22440 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22441 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22442 DEBUG_ABBREV_SECTION_LABEL, 0);
22443 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22444 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22445 COLD_TEXT_SECTION_LABEL, 0);
22446 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22448 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22449 DEBUG_INFO_SECTION_LABEL, 0);
22450 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22451 DEBUG_LINE_SECTION_LABEL, 0);
22452 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22453 DEBUG_RANGES_SECTION_LABEL, 0);
22454 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22455 DEBUG_ADDR_SECTION_LABEL, 0);
22456 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22457 dwarf_strict
22458 ? DEBUG_MACINFO_SECTION_LABEL
22459 : DEBUG_MACRO_SECTION_LABEL, 0);
22460 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22462 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22463 vec_alloc (macinfo_table, 64);
22465 switch_to_section (text_section);
22466 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22468 /* Make sure the line number table for .text always exists. */
22469 text_section_line_info = new_line_info_table ();
22470 text_section_line_info->end_label = text_end_label;
22473 /* Called before compile () starts outputtting functions, variables
22474 and toplevel asms into assembly. */
22476 static void
22477 dwarf2out_assembly_start (void)
22479 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22480 && dwarf2out_do_cfi_asm ()
22481 && (!(flag_unwind_tables || flag_exceptions)
22482 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22483 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22486 /* A helper function for dwarf2out_finish called through
22487 htab_traverse. Assign a string its index. All strings must be
22488 collected into the table by the time index_string is called,
22489 because the indexing code relies on htab_traverse to traverse nodes
22490 in the same order for each run. */
22493 index_string (indirect_string_node **h, unsigned int *index)
22495 indirect_string_node *node = *h;
22497 find_string_form (node);
22498 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22500 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22501 node->index = *index;
22502 *index += 1;
22504 return 1;
22507 /* A helper function for output_indirect_strings called through
22508 htab_traverse. Output the offset to a string and update the
22509 current offset. */
22512 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22514 indirect_string_node *node = *h;
22516 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22518 /* Assert that this node has been assigned an index. */
22519 gcc_assert (node->index != NO_INDEX_ASSIGNED
22520 && node->index != NOT_INDEXED);
22521 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22522 "indexed string 0x%x: %s", node->index, node->str);
22523 *offset += strlen (node->str) + 1;
22525 return 1;
22528 /* A helper function for dwarf2out_finish called through
22529 htab_traverse. Output the indexed string. */
22532 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22534 struct indirect_string_node *node = *h;
22536 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22538 /* Assert that the strings are output in the same order as their
22539 indexes were assigned. */
22540 gcc_assert (*cur_idx == node->index);
22541 assemble_string (node->str, strlen (node->str) + 1);
22542 *cur_idx += 1;
22544 return 1;
22547 /* A helper function for dwarf2out_finish called through
22548 htab_traverse. Emit one queued .debug_str string. */
22551 output_indirect_string (indirect_string_node **h, void *)
22553 struct indirect_string_node *node = *h;
22555 node->form = find_string_form (node);
22556 if (node->form == DW_FORM_strp && node->refcount > 0)
22558 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22559 assemble_string (node->str, strlen (node->str) + 1);
22562 return 1;
22565 /* Output the indexed string table. */
22567 static void
22568 output_indirect_strings (void)
22570 switch_to_section (debug_str_section);
22571 if (!dwarf_split_debug_info)
22572 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22573 else
22575 unsigned int offset = 0;
22576 unsigned int cur_idx = 0;
22578 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22580 switch_to_section (debug_str_offsets_section);
22581 debug_str_hash->traverse_noresize
22582 <unsigned int *, output_index_string_offset> (&offset);
22583 switch_to_section (debug_str_dwo_section);
22584 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22585 (&cur_idx);
22589 /* Callback for htab_traverse to assign an index to an entry in the
22590 table, and to write that entry to the .debug_addr section. */
22593 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22595 addr_table_entry *entry = *slot;
22597 if (entry->refcount == 0)
22599 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22600 || entry->index == NOT_INDEXED);
22601 return 1;
22604 gcc_assert (entry->index == *cur_index);
22605 (*cur_index)++;
22607 switch (entry->kind)
22609 case ate_kind_rtx:
22610 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22611 "0x%x", entry->index);
22612 break;
22613 case ate_kind_rtx_dtprel:
22614 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22615 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22616 DWARF2_ADDR_SIZE,
22617 entry->addr.rtl);
22618 fputc ('\n', asm_out_file);
22619 break;
22620 case ate_kind_label:
22621 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22622 "0x%x", entry->index);
22623 break;
22624 default:
22625 gcc_unreachable ();
22627 return 1;
22630 /* Produce the .debug_addr section. */
22632 static void
22633 output_addr_table (void)
22635 unsigned int index = 0;
22636 if (addr_index_table == NULL || addr_index_table->size () == 0)
22637 return;
22639 switch_to_section (debug_addr_section);
22640 addr_index_table
22641 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22644 #if ENABLE_ASSERT_CHECKING
22645 /* Verify that all marks are clear. */
22647 static void
22648 verify_marks_clear (dw_die_ref die)
22650 dw_die_ref c;
22652 gcc_assert (! die->die_mark);
22653 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22655 #endif /* ENABLE_ASSERT_CHECKING */
22657 /* Clear the marks for a die and its children.
22658 Be cool if the mark isn't set. */
22660 static void
22661 prune_unmark_dies (dw_die_ref die)
22663 dw_die_ref c;
22665 if (die->die_mark)
22666 die->die_mark = 0;
22667 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22670 /* Given DIE that we're marking as used, find any other dies
22671 it references as attributes and mark them as used. */
22673 static void
22674 prune_unused_types_walk_attribs (dw_die_ref die)
22676 dw_attr_ref a;
22677 unsigned ix;
22679 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22681 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22683 /* A reference to another DIE.
22684 Make sure that it will get emitted.
22685 If it was broken out into a comdat group, don't follow it. */
22686 if (! AT_ref (a)->comdat_type_p
22687 || a->dw_attr == DW_AT_specification)
22688 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22690 /* Set the string's refcount to 0 so that prune_unused_types_mark
22691 accounts properly for it. */
22692 if (AT_class (a) == dw_val_class_str)
22693 a->dw_attr_val.v.val_str->refcount = 0;
22697 /* Mark the generic parameters and arguments children DIEs of DIE. */
22699 static void
22700 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22702 dw_die_ref c;
22704 if (die == NULL || die->die_child == NULL)
22705 return;
22706 c = die->die_child;
22709 if (is_template_parameter (c))
22710 prune_unused_types_mark (c, 1);
22711 c = c->die_sib;
22712 } while (c && c != die->die_child);
22715 /* Mark DIE as being used. If DOKIDS is true, then walk down
22716 to DIE's children. */
22718 static void
22719 prune_unused_types_mark (dw_die_ref die, int dokids)
22721 dw_die_ref c;
22723 if (die->die_mark == 0)
22725 /* We haven't done this node yet. Mark it as used. */
22726 die->die_mark = 1;
22727 /* If this is the DIE of a generic type instantiation,
22728 mark the children DIEs that describe its generic parms and
22729 args. */
22730 prune_unused_types_mark_generic_parms_dies (die);
22732 /* We also have to mark its parents as used.
22733 (But we don't want to mark our parent's kids due to this,
22734 unless it is a class.) */
22735 if (die->die_parent)
22736 prune_unused_types_mark (die->die_parent,
22737 class_scope_p (die->die_parent));
22739 /* Mark any referenced nodes. */
22740 prune_unused_types_walk_attribs (die);
22742 /* If this node is a specification,
22743 also mark the definition, if it exists. */
22744 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22745 prune_unused_types_mark (die->die_definition, 1);
22748 if (dokids && die->die_mark != 2)
22750 /* We need to walk the children, but haven't done so yet.
22751 Remember that we've walked the kids. */
22752 die->die_mark = 2;
22754 /* If this is an array type, we need to make sure our
22755 kids get marked, even if they're types. If we're
22756 breaking out types into comdat sections, do this
22757 for all type definitions. */
22758 if (die->die_tag == DW_TAG_array_type
22759 || (use_debug_types
22760 && is_type_die (die) && ! is_declaration_die (die)))
22761 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22762 else
22763 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22767 /* For local classes, look if any static member functions were emitted
22768 and if so, mark them. */
22770 static void
22771 prune_unused_types_walk_local_classes (dw_die_ref die)
22773 dw_die_ref c;
22775 if (die->die_mark == 2)
22776 return;
22778 switch (die->die_tag)
22780 case DW_TAG_structure_type:
22781 case DW_TAG_union_type:
22782 case DW_TAG_class_type:
22783 break;
22785 case DW_TAG_subprogram:
22786 if (!get_AT_flag (die, DW_AT_declaration)
22787 || die->die_definition != NULL)
22788 prune_unused_types_mark (die, 1);
22789 return;
22791 default:
22792 return;
22795 /* Mark children. */
22796 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22799 /* Walk the tree DIE and mark types that we actually use. */
22801 static void
22802 prune_unused_types_walk (dw_die_ref die)
22804 dw_die_ref c;
22806 /* Don't do anything if this node is already marked and
22807 children have been marked as well. */
22808 if (die->die_mark == 2)
22809 return;
22811 switch (die->die_tag)
22813 case DW_TAG_structure_type:
22814 case DW_TAG_union_type:
22815 case DW_TAG_class_type:
22816 if (die->die_perennial_p)
22817 break;
22819 for (c = die->die_parent; c; c = c->die_parent)
22820 if (c->die_tag == DW_TAG_subprogram)
22821 break;
22823 /* Finding used static member functions inside of classes
22824 is needed just for local classes, because for other classes
22825 static member function DIEs with DW_AT_specification
22826 are emitted outside of the DW_TAG_*_type. If we ever change
22827 it, we'd need to call this even for non-local classes. */
22828 if (c)
22829 prune_unused_types_walk_local_classes (die);
22831 /* It's a type node --- don't mark it. */
22832 return;
22834 case DW_TAG_const_type:
22835 case DW_TAG_packed_type:
22836 case DW_TAG_pointer_type:
22837 case DW_TAG_reference_type:
22838 case DW_TAG_rvalue_reference_type:
22839 case DW_TAG_volatile_type:
22840 case DW_TAG_typedef:
22841 case DW_TAG_array_type:
22842 case DW_TAG_interface_type:
22843 case DW_TAG_friend:
22844 case DW_TAG_variant_part:
22845 case DW_TAG_enumeration_type:
22846 case DW_TAG_subroutine_type:
22847 case DW_TAG_string_type:
22848 case DW_TAG_set_type:
22849 case DW_TAG_subrange_type:
22850 case DW_TAG_ptr_to_member_type:
22851 case DW_TAG_file_type:
22852 if (die->die_perennial_p)
22853 break;
22855 /* It's a type node --- don't mark it. */
22856 return;
22858 default:
22859 /* Mark everything else. */
22860 break;
22863 if (die->die_mark == 0)
22865 die->die_mark = 1;
22867 /* Now, mark any dies referenced from here. */
22868 prune_unused_types_walk_attribs (die);
22871 die->die_mark = 2;
22873 /* Mark children. */
22874 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22877 /* Increment the string counts on strings referred to from DIE's
22878 attributes. */
22880 static void
22881 prune_unused_types_update_strings (dw_die_ref die)
22883 dw_attr_ref a;
22884 unsigned ix;
22886 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22887 if (AT_class (a) == dw_val_class_str)
22889 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22890 s->refcount++;
22891 /* Avoid unnecessarily putting strings that are used less than
22892 twice in the hash table. */
22893 if (s->refcount
22894 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22896 indirect_string_node **slot
22897 = debug_str_hash->find_slot_with_hash (s->str,
22898 htab_hash_string (s->str),
22899 INSERT);
22900 gcc_assert (*slot == NULL);
22901 *slot = s;
22906 /* Remove from the tree DIE any dies that aren't marked. */
22908 static void
22909 prune_unused_types_prune (dw_die_ref die)
22911 dw_die_ref c;
22913 gcc_assert (die->die_mark);
22914 prune_unused_types_update_strings (die);
22916 if (! die->die_child)
22917 return;
22919 c = die->die_child;
22920 do {
22921 dw_die_ref prev = c;
22922 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22923 if (c == die->die_child)
22925 /* No marked children between 'prev' and the end of the list. */
22926 if (prev == c)
22927 /* No marked children at all. */
22928 die->die_child = NULL;
22929 else
22931 prev->die_sib = c->die_sib;
22932 die->die_child = prev;
22934 return;
22937 if (c != prev->die_sib)
22938 prev->die_sib = c;
22939 prune_unused_types_prune (c);
22940 } while (c != die->die_child);
22943 /* Remove dies representing declarations that we never use. */
22945 static void
22946 prune_unused_types (void)
22948 unsigned int i;
22949 limbo_die_node *node;
22950 comdat_type_node *ctnode;
22951 pubname_ref pub;
22952 dw_die_ref base_type;
22954 #if ENABLE_ASSERT_CHECKING
22955 /* All the marks should already be clear. */
22956 verify_marks_clear (comp_unit_die ());
22957 for (node = limbo_die_list; node; node = node->next)
22958 verify_marks_clear (node->die);
22959 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22960 verify_marks_clear (ctnode->root_die);
22961 #endif /* ENABLE_ASSERT_CHECKING */
22963 /* Mark types that are used in global variables. */
22964 premark_types_used_by_global_vars ();
22966 /* Set the mark on nodes that are actually used. */
22967 prune_unused_types_walk (comp_unit_die ());
22968 for (node = limbo_die_list; node; node = node->next)
22969 prune_unused_types_walk (node->die);
22970 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22972 prune_unused_types_walk (ctnode->root_die);
22973 prune_unused_types_mark (ctnode->type_die, 1);
22976 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22977 are unusual in that they are pubnames that are the children of pubtypes.
22978 They should only be marked via their parent DW_TAG_enumeration_type die,
22979 not as roots in themselves. */
22980 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22981 if (pub->die->die_tag != DW_TAG_enumerator)
22982 prune_unused_types_mark (pub->die, 1);
22983 for (i = 0; base_types.iterate (i, &base_type); i++)
22984 prune_unused_types_mark (base_type, 1);
22986 if (debug_str_hash)
22987 debug_str_hash->empty ();
22988 if (skeleton_debug_str_hash)
22989 skeleton_debug_str_hash->empty ();
22990 prune_unused_types_prune (comp_unit_die ());
22991 for (node = limbo_die_list; node; node = node->next)
22992 prune_unused_types_prune (node->die);
22993 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22994 prune_unused_types_prune (ctnode->root_die);
22996 /* Leave the marks clear. */
22997 prune_unmark_dies (comp_unit_die ());
22998 for (node = limbo_die_list; node; node = node->next)
22999 prune_unmark_dies (node->die);
23000 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23001 prune_unmark_dies (ctnode->root_die);
23004 /* Set the parameter to true if there are any relative pathnames in
23005 the file table. */
23007 file_table_relative_p (dwarf_file_data **slot, bool *p)
23009 struct dwarf_file_data *d = *slot;
23010 if (!IS_ABSOLUTE_PATH (d->filename))
23012 *p = true;
23013 return 0;
23015 return 1;
23018 /* Helpers to manipulate hash table of comdat type units. */
23020 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23022 typedef comdat_type_node value_type;
23023 typedef comdat_type_node compare_type;
23024 static inline hashval_t hash (const value_type *);
23025 static inline bool equal (const value_type *, const compare_type *);
23028 inline hashval_t
23029 comdat_type_hasher::hash (const value_type *type_node)
23031 hashval_t h;
23032 memcpy (&h, type_node->signature, sizeof (h));
23033 return h;
23036 inline bool
23037 comdat_type_hasher::equal (const value_type *type_node_1,
23038 const compare_type *type_node_2)
23040 return (! memcmp (type_node_1->signature, type_node_2->signature,
23041 DWARF_TYPE_SIGNATURE_SIZE));
23044 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23045 to the location it would have been added, should we know its
23046 DECL_ASSEMBLER_NAME when we added other attributes. This will
23047 probably improve compactness of debug info, removing equivalent
23048 abbrevs, and hide any differences caused by deferring the
23049 computation of the assembler name, triggered by e.g. PCH. */
23051 static inline void
23052 move_linkage_attr (dw_die_ref die)
23054 unsigned ix = vec_safe_length (die->die_attr);
23055 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23057 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23058 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23060 while (--ix > 0)
23062 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23064 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23065 break;
23068 if (ix != vec_safe_length (die->die_attr) - 1)
23070 die->die_attr->pop ();
23071 die->die_attr->quick_insert (ix, linkage);
23075 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23076 referenced from typed stack ops and count how often they are used. */
23078 static void
23079 mark_base_types (dw_loc_descr_ref loc)
23081 dw_die_ref base_type = NULL;
23083 for (; loc; loc = loc->dw_loc_next)
23085 switch (loc->dw_loc_opc)
23087 case DW_OP_GNU_regval_type:
23088 case DW_OP_GNU_deref_type:
23089 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23090 break;
23091 case DW_OP_GNU_convert:
23092 case DW_OP_GNU_reinterpret:
23093 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23094 continue;
23095 /* FALLTHRU */
23096 case DW_OP_GNU_const_type:
23097 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23098 break;
23099 case DW_OP_GNU_entry_value:
23100 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23101 continue;
23102 default:
23103 continue;
23105 gcc_assert (base_type->die_parent == comp_unit_die ());
23106 if (base_type->die_mark)
23107 base_type->die_mark++;
23108 else
23110 base_types.safe_push (base_type);
23111 base_type->die_mark = 1;
23116 /* Comparison function for sorting marked base types. */
23118 static int
23119 base_type_cmp (const void *x, const void *y)
23121 dw_die_ref dx = *(const dw_die_ref *) x;
23122 dw_die_ref dy = *(const dw_die_ref *) y;
23123 unsigned int byte_size1, byte_size2;
23124 unsigned int encoding1, encoding2;
23125 if (dx->die_mark > dy->die_mark)
23126 return -1;
23127 if (dx->die_mark < dy->die_mark)
23128 return 1;
23129 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23130 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23131 if (byte_size1 < byte_size2)
23132 return 1;
23133 if (byte_size1 > byte_size2)
23134 return -1;
23135 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23136 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23137 if (encoding1 < encoding2)
23138 return 1;
23139 if (encoding1 > encoding2)
23140 return -1;
23141 return 0;
23144 /* Move base types marked by mark_base_types as early as possible
23145 in the CU, sorted by decreasing usage count both to make the
23146 uleb128 references as small as possible and to make sure they
23147 will have die_offset already computed by calc_die_sizes when
23148 sizes of typed stack loc ops is computed. */
23150 static void
23151 move_marked_base_types (void)
23153 unsigned int i;
23154 dw_die_ref base_type, die, c;
23156 if (base_types.is_empty ())
23157 return;
23159 /* Sort by decreasing usage count, they will be added again in that
23160 order later on. */
23161 base_types.qsort (base_type_cmp);
23162 die = comp_unit_die ();
23163 c = die->die_child;
23166 dw_die_ref prev = c;
23167 c = c->die_sib;
23168 while (c->die_mark)
23170 remove_child_with_prev (c, prev);
23171 /* As base types got marked, there must be at least
23172 one node other than DW_TAG_base_type. */
23173 gcc_assert (c != c->die_sib);
23174 c = c->die_sib;
23177 while (c != die->die_child);
23178 gcc_assert (die->die_child);
23179 c = die->die_child;
23180 for (i = 0; base_types.iterate (i, &base_type); i++)
23182 base_type->die_mark = 0;
23183 base_type->die_sib = c->die_sib;
23184 c->die_sib = base_type;
23185 c = base_type;
23189 /* Helper function for resolve_addr, attempt to resolve
23190 one CONST_STRING, return true if successful. Similarly verify that
23191 SYMBOL_REFs refer to variables emitted in the current CU. */
23193 static bool
23194 resolve_one_addr (rtx *addr)
23196 rtx rtl = *addr;
23198 if (GET_CODE (rtl) == CONST_STRING)
23200 size_t len = strlen (XSTR (rtl, 0)) + 1;
23201 tree t = build_string (len, XSTR (rtl, 0));
23202 tree tlen = size_int (len - 1);
23203 TREE_TYPE (t)
23204 = build_array_type (char_type_node, build_index_type (tlen));
23205 rtl = lookup_constant_def (t);
23206 if (!rtl || !MEM_P (rtl))
23207 return false;
23208 rtl = XEXP (rtl, 0);
23209 if (GET_CODE (rtl) == SYMBOL_REF
23210 && SYMBOL_REF_DECL (rtl)
23211 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23212 return false;
23213 vec_safe_push (used_rtx_array, rtl);
23214 *addr = rtl;
23215 return true;
23218 if (GET_CODE (rtl) == SYMBOL_REF
23219 && SYMBOL_REF_DECL (rtl))
23221 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23223 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23224 return false;
23226 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23227 return false;
23230 if (GET_CODE (rtl) == CONST)
23232 subrtx_ptr_iterator::array_type array;
23233 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23234 if (!resolve_one_addr (*iter))
23235 return false;
23238 return true;
23241 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23242 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23243 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23245 static rtx
23246 string_cst_pool_decl (tree t)
23248 rtx rtl = output_constant_def (t, 1);
23249 unsigned char *array;
23250 dw_loc_descr_ref l;
23251 tree decl;
23252 size_t len;
23253 dw_die_ref ref;
23255 if (!rtl || !MEM_P (rtl))
23256 return NULL_RTX;
23257 rtl = XEXP (rtl, 0);
23258 if (GET_CODE (rtl) != SYMBOL_REF
23259 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23260 return NULL_RTX;
23262 decl = SYMBOL_REF_DECL (rtl);
23263 if (!lookup_decl_die (decl))
23265 len = TREE_STRING_LENGTH (t);
23266 vec_safe_push (used_rtx_array, rtl);
23267 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23268 array = ggc_vec_alloc<unsigned char> (len);
23269 memcpy (array, TREE_STRING_POINTER (t), len);
23270 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23271 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23272 l->dw_loc_oprnd2.v.val_vec.length = len;
23273 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23274 l->dw_loc_oprnd2.v.val_vec.array = array;
23275 add_AT_loc (ref, DW_AT_location, l);
23276 equate_decl_number_to_die (decl, ref);
23278 return rtl;
23281 /* Helper function of resolve_addr_in_expr. LOC is
23282 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23283 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23284 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23285 with DW_OP_GNU_implicit_pointer if possible
23286 and return true, if unsuccessful, return false. */
23288 static bool
23289 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23291 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23292 HOST_WIDE_INT offset = 0;
23293 dw_die_ref ref = NULL;
23294 tree decl;
23296 if (GET_CODE (rtl) == CONST
23297 && GET_CODE (XEXP (rtl, 0)) == PLUS
23298 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23300 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23301 rtl = XEXP (XEXP (rtl, 0), 0);
23303 if (GET_CODE (rtl) == CONST_STRING)
23305 size_t len = strlen (XSTR (rtl, 0)) + 1;
23306 tree t = build_string (len, XSTR (rtl, 0));
23307 tree tlen = size_int (len - 1);
23309 TREE_TYPE (t)
23310 = build_array_type (char_type_node, build_index_type (tlen));
23311 rtl = string_cst_pool_decl (t);
23312 if (!rtl)
23313 return false;
23315 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23317 decl = SYMBOL_REF_DECL (rtl);
23318 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23320 ref = lookup_decl_die (decl);
23321 if (ref && (get_AT (ref, DW_AT_location)
23322 || get_AT (ref, DW_AT_const_value)))
23324 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23325 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23326 loc->dw_loc_oprnd1.val_entry = NULL;
23327 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23328 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23329 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23330 loc->dw_loc_oprnd2.v.val_int = offset;
23331 return true;
23335 return false;
23338 /* Helper function for resolve_addr, handle one location
23339 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23340 the location list couldn't be resolved. */
23342 static bool
23343 resolve_addr_in_expr (dw_loc_descr_ref loc)
23345 dw_loc_descr_ref keep = NULL;
23346 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23347 switch (loc->dw_loc_opc)
23349 case DW_OP_addr:
23350 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23352 if ((prev == NULL
23353 || prev->dw_loc_opc == DW_OP_piece
23354 || prev->dw_loc_opc == DW_OP_bit_piece)
23355 && loc->dw_loc_next
23356 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23357 && !dwarf_strict
23358 && optimize_one_addr_into_implicit_ptr (loc))
23359 break;
23360 return false;
23362 break;
23363 case DW_OP_GNU_addr_index:
23364 case DW_OP_GNU_const_index:
23365 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23366 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23368 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23369 if (!resolve_one_addr (&rtl))
23370 return false;
23371 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23372 loc->dw_loc_oprnd1.val_entry =
23373 add_addr_table_entry (rtl, ate_kind_rtx);
23375 break;
23376 case DW_OP_const4u:
23377 case DW_OP_const8u:
23378 if (loc->dtprel
23379 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23380 return false;
23381 break;
23382 case DW_OP_plus_uconst:
23383 if (size_of_loc_descr (loc)
23384 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23386 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23388 dw_loc_descr_ref repl
23389 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23390 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23391 add_loc_descr (&repl, loc->dw_loc_next);
23392 *loc = *repl;
23394 break;
23395 case DW_OP_implicit_value:
23396 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23397 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23398 return false;
23399 break;
23400 case DW_OP_GNU_implicit_pointer:
23401 case DW_OP_GNU_parameter_ref:
23402 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23404 dw_die_ref ref
23405 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23406 if (ref == NULL)
23407 return false;
23408 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23409 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23410 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23412 break;
23413 case DW_OP_GNU_const_type:
23414 case DW_OP_GNU_regval_type:
23415 case DW_OP_GNU_deref_type:
23416 case DW_OP_GNU_convert:
23417 case DW_OP_GNU_reinterpret:
23418 while (loc->dw_loc_next
23419 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23421 dw_die_ref base1, base2;
23422 unsigned enc1, enc2, size1, size2;
23423 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23424 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23425 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23426 else if (loc->dw_loc_oprnd1.val_class
23427 == dw_val_class_unsigned_const)
23428 break;
23429 else
23430 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23431 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23432 == dw_val_class_unsigned_const)
23433 break;
23434 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23435 gcc_assert (base1->die_tag == DW_TAG_base_type
23436 && base2->die_tag == DW_TAG_base_type);
23437 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23438 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23439 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23440 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23441 if (size1 == size2
23442 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23443 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23444 && loc != keep)
23445 || enc1 == enc2))
23447 /* Optimize away next DW_OP_GNU_convert after
23448 adjusting LOC's base type die reference. */
23449 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23450 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23451 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23452 else
23453 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23454 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23455 continue;
23457 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23458 point typed stack entry. */
23459 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23460 keep = loc->dw_loc_next;
23461 break;
23463 break;
23464 default:
23465 break;
23467 return true;
23470 /* Helper function of resolve_addr. DIE had DW_AT_location of
23471 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23472 and DW_OP_addr couldn't be resolved. resolve_addr has already
23473 removed the DW_AT_location attribute. This function attempts to
23474 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23475 to it or DW_AT_const_value attribute, if possible. */
23477 static void
23478 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23480 if (TREE_CODE (decl) != VAR_DECL
23481 || lookup_decl_die (decl) != die
23482 || DECL_EXTERNAL (decl)
23483 || !TREE_STATIC (decl)
23484 || DECL_INITIAL (decl) == NULL_TREE
23485 || DECL_P (DECL_INITIAL (decl))
23486 || get_AT (die, DW_AT_const_value))
23487 return;
23489 tree init = DECL_INITIAL (decl);
23490 HOST_WIDE_INT offset = 0;
23491 /* For variables that have been optimized away and thus
23492 don't have a memory location, see if we can emit
23493 DW_AT_const_value instead. */
23494 if (tree_add_const_value_attribute (die, init))
23495 return;
23496 if (dwarf_strict)
23497 return;
23498 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23499 and ADDR_EXPR refers to a decl that has DW_AT_location or
23500 DW_AT_const_value (but isn't addressable, otherwise
23501 resolving the original DW_OP_addr wouldn't fail), see if
23502 we can add DW_OP_GNU_implicit_pointer. */
23503 STRIP_NOPS (init);
23504 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23505 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23507 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23508 init = TREE_OPERAND (init, 0);
23509 STRIP_NOPS (init);
23511 if (TREE_CODE (init) != ADDR_EXPR)
23512 return;
23513 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23514 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23515 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23516 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23517 && TREE_OPERAND (init, 0) != decl))
23519 dw_die_ref ref;
23520 dw_loc_descr_ref l;
23522 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23524 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23525 if (!rtl)
23526 return;
23527 decl = SYMBOL_REF_DECL (rtl);
23529 else
23530 decl = TREE_OPERAND (init, 0);
23531 ref = lookup_decl_die (decl);
23532 if (ref == NULL
23533 || (!get_AT (ref, DW_AT_location)
23534 && !get_AT (ref, DW_AT_const_value)))
23535 return;
23536 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23537 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23538 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23539 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23540 add_AT_loc (die, DW_AT_location, l);
23544 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23545 an address in .rodata section if the string literal is emitted there,
23546 or remove the containing location list or replace DW_AT_const_value
23547 with DW_AT_location and empty location expression, if it isn't found
23548 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23549 to something that has been emitted in the current CU. */
23551 static void
23552 resolve_addr (dw_die_ref die)
23554 dw_die_ref c;
23555 dw_attr_ref a;
23556 dw_loc_list_ref *curr, *start, loc;
23557 unsigned ix;
23559 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23560 switch (AT_class (a))
23562 case dw_val_class_loc_list:
23563 start = curr = AT_loc_list_ptr (a);
23564 loc = *curr;
23565 gcc_assert (loc);
23566 /* The same list can be referenced more than once. See if we have
23567 already recorded the result from a previous pass. */
23568 if (loc->replaced)
23569 *curr = loc->dw_loc_next;
23570 else if (!loc->resolved_addr)
23572 /* As things stand, we do not expect or allow one die to
23573 reference a suffix of another die's location list chain.
23574 References must be identical or completely separate.
23575 There is therefore no need to cache the result of this
23576 pass on any list other than the first; doing so
23577 would lead to unnecessary writes. */
23578 while (*curr)
23580 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23581 if (!resolve_addr_in_expr ((*curr)->expr))
23583 dw_loc_list_ref next = (*curr)->dw_loc_next;
23584 dw_loc_descr_ref l = (*curr)->expr;
23586 if (next && (*curr)->ll_symbol)
23588 gcc_assert (!next->ll_symbol);
23589 next->ll_symbol = (*curr)->ll_symbol;
23591 if (dwarf_split_debug_info)
23592 remove_loc_list_addr_table_entries (l);
23593 *curr = next;
23595 else
23597 mark_base_types ((*curr)->expr);
23598 curr = &(*curr)->dw_loc_next;
23601 if (loc == *start)
23602 loc->resolved_addr = 1;
23603 else
23605 loc->replaced = 1;
23606 loc->dw_loc_next = *start;
23609 if (!*start)
23611 remove_AT (die, a->dw_attr);
23612 ix--;
23614 break;
23615 case dw_val_class_loc:
23617 dw_loc_descr_ref l = AT_loc (a);
23618 /* For -gdwarf-2 don't attempt to optimize
23619 DW_AT_data_member_location containing
23620 DW_OP_plus_uconst - older consumers might
23621 rely on it being that op instead of a more complex,
23622 but shorter, location description. */
23623 if ((dwarf_version > 2
23624 || a->dw_attr != DW_AT_data_member_location
23625 || l == NULL
23626 || l->dw_loc_opc != DW_OP_plus_uconst
23627 || l->dw_loc_next != NULL)
23628 && !resolve_addr_in_expr (l))
23630 if (dwarf_split_debug_info)
23631 remove_loc_list_addr_table_entries (l);
23632 if (l != NULL
23633 && l->dw_loc_next == NULL
23634 && l->dw_loc_opc == DW_OP_addr
23635 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23636 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23637 && a->dw_attr == DW_AT_location)
23639 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23640 remove_AT (die, a->dw_attr);
23641 ix--;
23642 optimize_location_into_implicit_ptr (die, decl);
23643 break;
23645 remove_AT (die, a->dw_attr);
23646 ix--;
23648 else
23649 mark_base_types (l);
23651 break;
23652 case dw_val_class_addr:
23653 if (a->dw_attr == DW_AT_const_value
23654 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23656 if (AT_index (a) != NOT_INDEXED)
23657 remove_addr_table_entry (a->dw_attr_val.val_entry);
23658 remove_AT (die, a->dw_attr);
23659 ix--;
23661 if (die->die_tag == DW_TAG_GNU_call_site
23662 && a->dw_attr == DW_AT_abstract_origin)
23664 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23665 dw_die_ref tdie = lookup_decl_die (tdecl);
23666 if (tdie == NULL
23667 && DECL_EXTERNAL (tdecl)
23668 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23670 force_decl_die (tdecl);
23671 tdie = lookup_decl_die (tdecl);
23673 if (tdie)
23675 a->dw_attr_val.val_class = dw_val_class_die_ref;
23676 a->dw_attr_val.v.val_die_ref.die = tdie;
23677 a->dw_attr_val.v.val_die_ref.external = 0;
23679 else
23681 if (AT_index (a) != NOT_INDEXED)
23682 remove_addr_table_entry (a->dw_attr_val.val_entry);
23683 remove_AT (die, a->dw_attr);
23684 ix--;
23687 break;
23688 default:
23689 break;
23692 FOR_EACH_CHILD (die, c, resolve_addr (c));
23695 /* Helper routines for optimize_location_lists.
23696 This pass tries to share identical local lists in .debug_loc
23697 section. */
23699 /* Iteratively hash operands of LOC opcode into HSTATE. */
23701 static void
23702 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23704 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23705 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23707 switch (loc->dw_loc_opc)
23709 case DW_OP_const4u:
23710 case DW_OP_const8u:
23711 if (loc->dtprel)
23712 goto hash_addr;
23713 /* FALLTHRU */
23714 case DW_OP_const1u:
23715 case DW_OP_const1s:
23716 case DW_OP_const2u:
23717 case DW_OP_const2s:
23718 case DW_OP_const4s:
23719 case DW_OP_const8s:
23720 case DW_OP_constu:
23721 case DW_OP_consts:
23722 case DW_OP_pick:
23723 case DW_OP_plus_uconst:
23724 case DW_OP_breg0:
23725 case DW_OP_breg1:
23726 case DW_OP_breg2:
23727 case DW_OP_breg3:
23728 case DW_OP_breg4:
23729 case DW_OP_breg5:
23730 case DW_OP_breg6:
23731 case DW_OP_breg7:
23732 case DW_OP_breg8:
23733 case DW_OP_breg9:
23734 case DW_OP_breg10:
23735 case DW_OP_breg11:
23736 case DW_OP_breg12:
23737 case DW_OP_breg13:
23738 case DW_OP_breg14:
23739 case DW_OP_breg15:
23740 case DW_OP_breg16:
23741 case DW_OP_breg17:
23742 case DW_OP_breg18:
23743 case DW_OP_breg19:
23744 case DW_OP_breg20:
23745 case DW_OP_breg21:
23746 case DW_OP_breg22:
23747 case DW_OP_breg23:
23748 case DW_OP_breg24:
23749 case DW_OP_breg25:
23750 case DW_OP_breg26:
23751 case DW_OP_breg27:
23752 case DW_OP_breg28:
23753 case DW_OP_breg29:
23754 case DW_OP_breg30:
23755 case DW_OP_breg31:
23756 case DW_OP_regx:
23757 case DW_OP_fbreg:
23758 case DW_OP_piece:
23759 case DW_OP_deref_size:
23760 case DW_OP_xderef_size:
23761 hstate.add_object (val1->v.val_int);
23762 break;
23763 case DW_OP_skip:
23764 case DW_OP_bra:
23766 int offset;
23768 gcc_assert (val1->val_class == dw_val_class_loc);
23769 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23770 hstate.add_object (offset);
23772 break;
23773 case DW_OP_implicit_value:
23774 hstate.add_object (val1->v.val_unsigned);
23775 switch (val2->val_class)
23777 case dw_val_class_const:
23778 hstate.add_object (val2->v.val_int);
23779 break;
23780 case dw_val_class_vec:
23782 unsigned int elt_size = val2->v.val_vec.elt_size;
23783 unsigned int len = val2->v.val_vec.length;
23785 hstate.add_int (elt_size);
23786 hstate.add_int (len);
23787 hstate.add (val2->v.val_vec.array, len * elt_size);
23789 break;
23790 case dw_val_class_const_double:
23791 hstate.add_object (val2->v.val_double.low);
23792 hstate.add_object (val2->v.val_double.high);
23793 break;
23794 case dw_val_class_wide_int:
23795 hstate.add_object (*val2->v.val_wide);
23796 break;
23797 case dw_val_class_addr:
23798 inchash::add_rtx (val2->v.val_addr, hstate);
23799 break;
23800 default:
23801 gcc_unreachable ();
23803 break;
23804 case DW_OP_bregx:
23805 case DW_OP_bit_piece:
23806 hstate.add_object (val1->v.val_int);
23807 hstate.add_object (val2->v.val_int);
23808 break;
23809 case DW_OP_addr:
23810 hash_addr:
23811 if (loc->dtprel)
23813 unsigned char dtprel = 0xd1;
23814 hstate.add_object (dtprel);
23816 inchash::add_rtx (val1->v.val_addr, hstate);
23817 break;
23818 case DW_OP_GNU_addr_index:
23819 case DW_OP_GNU_const_index:
23821 if (loc->dtprel)
23823 unsigned char dtprel = 0xd1;
23824 hstate.add_object (dtprel);
23826 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
23828 break;
23829 case DW_OP_GNU_implicit_pointer:
23830 hstate.add_int (val2->v.val_int);
23831 break;
23832 case DW_OP_GNU_entry_value:
23833 hstate.add_object (val1->v.val_loc);
23834 break;
23835 case DW_OP_GNU_regval_type:
23836 case DW_OP_GNU_deref_type:
23838 unsigned int byte_size
23839 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23840 unsigned int encoding
23841 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23842 hstate.add_object (val1->v.val_int);
23843 hstate.add_object (byte_size);
23844 hstate.add_object (encoding);
23846 break;
23847 case DW_OP_GNU_convert:
23848 case DW_OP_GNU_reinterpret:
23849 if (val1->val_class == dw_val_class_unsigned_const)
23851 hstate.add_object (val1->v.val_unsigned);
23852 break;
23854 /* FALLTHRU */
23855 case DW_OP_GNU_const_type:
23857 unsigned int byte_size
23858 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23859 unsigned int encoding
23860 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23861 hstate.add_object (byte_size);
23862 hstate.add_object (encoding);
23863 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23864 break;
23865 hstate.add_object (val2->val_class);
23866 switch (val2->val_class)
23868 case dw_val_class_const:
23869 hstate.add_object (val2->v.val_int);
23870 break;
23871 case dw_val_class_vec:
23873 unsigned int elt_size = val2->v.val_vec.elt_size;
23874 unsigned int len = val2->v.val_vec.length;
23876 hstate.add_object (elt_size);
23877 hstate.add_object (len);
23878 hstate.add (val2->v.val_vec.array, len * elt_size);
23880 break;
23881 case dw_val_class_const_double:
23882 hstate.add_object (val2->v.val_double.low);
23883 hstate.add_object (val2->v.val_double.high);
23884 break;
23885 case dw_val_class_wide_int:
23886 hstate.add_object (*val2->v.val_wide);
23887 break;
23888 default:
23889 gcc_unreachable ();
23892 break;
23894 default:
23895 /* Other codes have no operands. */
23896 break;
23900 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
23902 static inline void
23903 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
23905 dw_loc_descr_ref l;
23906 bool sizes_computed = false;
23907 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23908 size_of_locs (loc);
23910 for (l = loc; l != NULL; l = l->dw_loc_next)
23912 enum dwarf_location_atom opc = l->dw_loc_opc;
23913 hstate.add_object (opc);
23914 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23916 size_of_locs (loc);
23917 sizes_computed = true;
23919 hash_loc_operands (l, hstate);
23923 /* Compute hash of the whole location list LIST_HEAD. */
23925 static inline void
23926 hash_loc_list (dw_loc_list_ref list_head)
23928 dw_loc_list_ref curr = list_head;
23929 inchash::hash hstate;
23931 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23933 hstate.add (curr->begin, strlen (curr->begin) + 1);
23934 hstate.add (curr->end, strlen (curr->end) + 1);
23935 if (curr->section)
23936 hstate.add (curr->section, strlen (curr->section) + 1);
23937 hash_locs (curr->expr, hstate);
23939 list_head->hash = hstate.end ();
23942 /* Return true if X and Y opcodes have the same operands. */
23944 static inline bool
23945 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23947 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23948 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23949 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23950 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23952 switch (x->dw_loc_opc)
23954 case DW_OP_const4u:
23955 case DW_OP_const8u:
23956 if (x->dtprel)
23957 goto hash_addr;
23958 /* FALLTHRU */
23959 case DW_OP_const1u:
23960 case DW_OP_const1s:
23961 case DW_OP_const2u:
23962 case DW_OP_const2s:
23963 case DW_OP_const4s:
23964 case DW_OP_const8s:
23965 case DW_OP_constu:
23966 case DW_OP_consts:
23967 case DW_OP_pick:
23968 case DW_OP_plus_uconst:
23969 case DW_OP_breg0:
23970 case DW_OP_breg1:
23971 case DW_OP_breg2:
23972 case DW_OP_breg3:
23973 case DW_OP_breg4:
23974 case DW_OP_breg5:
23975 case DW_OP_breg6:
23976 case DW_OP_breg7:
23977 case DW_OP_breg8:
23978 case DW_OP_breg9:
23979 case DW_OP_breg10:
23980 case DW_OP_breg11:
23981 case DW_OP_breg12:
23982 case DW_OP_breg13:
23983 case DW_OP_breg14:
23984 case DW_OP_breg15:
23985 case DW_OP_breg16:
23986 case DW_OP_breg17:
23987 case DW_OP_breg18:
23988 case DW_OP_breg19:
23989 case DW_OP_breg20:
23990 case DW_OP_breg21:
23991 case DW_OP_breg22:
23992 case DW_OP_breg23:
23993 case DW_OP_breg24:
23994 case DW_OP_breg25:
23995 case DW_OP_breg26:
23996 case DW_OP_breg27:
23997 case DW_OP_breg28:
23998 case DW_OP_breg29:
23999 case DW_OP_breg30:
24000 case DW_OP_breg31:
24001 case DW_OP_regx:
24002 case DW_OP_fbreg:
24003 case DW_OP_piece:
24004 case DW_OP_deref_size:
24005 case DW_OP_xderef_size:
24006 return valx1->v.val_int == valy1->v.val_int;
24007 case DW_OP_skip:
24008 case DW_OP_bra:
24009 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24010 can cause irrelevant differences in dw_loc_addr. */
24011 gcc_assert (valx1->val_class == dw_val_class_loc
24012 && valy1->val_class == dw_val_class_loc
24013 && (dwarf_split_debug_info
24014 || x->dw_loc_addr == y->dw_loc_addr));
24015 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24016 case DW_OP_implicit_value:
24017 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24018 || valx2->val_class != valy2->val_class)
24019 return false;
24020 switch (valx2->val_class)
24022 case dw_val_class_const:
24023 return valx2->v.val_int == valy2->v.val_int;
24024 case dw_val_class_vec:
24025 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24026 && valx2->v.val_vec.length == valy2->v.val_vec.length
24027 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24028 valx2->v.val_vec.elt_size
24029 * valx2->v.val_vec.length) == 0;
24030 case dw_val_class_const_double:
24031 return valx2->v.val_double.low == valy2->v.val_double.low
24032 && valx2->v.val_double.high == valy2->v.val_double.high;
24033 case dw_val_class_wide_int:
24034 return *valx2->v.val_wide == *valy2->v.val_wide;
24035 case dw_val_class_addr:
24036 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24037 default:
24038 gcc_unreachable ();
24040 case DW_OP_bregx:
24041 case DW_OP_bit_piece:
24042 return valx1->v.val_int == valy1->v.val_int
24043 && valx2->v.val_int == valy2->v.val_int;
24044 case DW_OP_addr:
24045 hash_addr:
24046 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24047 case DW_OP_GNU_addr_index:
24048 case DW_OP_GNU_const_index:
24050 rtx ax1 = valx1->val_entry->addr.rtl;
24051 rtx ay1 = valy1->val_entry->addr.rtl;
24052 return rtx_equal_p (ax1, ay1);
24054 case DW_OP_GNU_implicit_pointer:
24055 return valx1->val_class == dw_val_class_die_ref
24056 && valx1->val_class == valy1->val_class
24057 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24058 && valx2->v.val_int == valy2->v.val_int;
24059 case DW_OP_GNU_entry_value:
24060 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24061 case DW_OP_GNU_const_type:
24062 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24063 || valx2->val_class != valy2->val_class)
24064 return false;
24065 switch (valx2->val_class)
24067 case dw_val_class_const:
24068 return valx2->v.val_int == valy2->v.val_int;
24069 case dw_val_class_vec:
24070 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24071 && valx2->v.val_vec.length == valy2->v.val_vec.length
24072 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24073 valx2->v.val_vec.elt_size
24074 * valx2->v.val_vec.length) == 0;
24075 case dw_val_class_const_double:
24076 return valx2->v.val_double.low == valy2->v.val_double.low
24077 && valx2->v.val_double.high == valy2->v.val_double.high;
24078 case dw_val_class_wide_int:
24079 return *valx2->v.val_wide == *valy2->v.val_wide;
24080 default:
24081 gcc_unreachable ();
24083 case DW_OP_GNU_regval_type:
24084 case DW_OP_GNU_deref_type:
24085 return valx1->v.val_int == valy1->v.val_int
24086 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24087 case DW_OP_GNU_convert:
24088 case DW_OP_GNU_reinterpret:
24089 if (valx1->val_class != valy1->val_class)
24090 return false;
24091 if (valx1->val_class == dw_val_class_unsigned_const)
24092 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24093 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24094 case DW_OP_GNU_parameter_ref:
24095 return valx1->val_class == dw_val_class_die_ref
24096 && valx1->val_class == valy1->val_class
24097 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24098 default:
24099 /* Other codes have no operands. */
24100 return true;
24104 /* Return true if DWARF location expressions X and Y are the same. */
24106 static inline bool
24107 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24109 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24110 if (x->dw_loc_opc != y->dw_loc_opc
24111 || x->dtprel != y->dtprel
24112 || !compare_loc_operands (x, y))
24113 break;
24114 return x == NULL && y == NULL;
24117 /* Hashtable helpers. */
24119 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24121 typedef dw_loc_list_struct value_type;
24122 typedef dw_loc_list_struct compare_type;
24123 static inline hashval_t hash (const value_type *);
24124 static inline bool equal (const value_type *, const compare_type *);
24127 /* Return precomputed hash of location list X. */
24129 inline hashval_t
24130 loc_list_hasher::hash (const value_type *x)
24132 return x->hash;
24135 /* Return true if location lists A and B are the same. */
24137 inline bool
24138 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24140 if (a == b)
24141 return 1;
24142 if (a->hash != b->hash)
24143 return 0;
24144 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24145 if (strcmp (a->begin, b->begin) != 0
24146 || strcmp (a->end, b->end) != 0
24147 || (a->section == NULL) != (b->section == NULL)
24148 || (a->section && strcmp (a->section, b->section) != 0)
24149 || !compare_locs (a->expr, b->expr))
24150 break;
24151 return a == NULL && b == NULL;
24154 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24157 /* Recursively optimize location lists referenced from DIE
24158 children and share them whenever possible. */
24160 static void
24161 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24163 dw_die_ref c;
24164 dw_attr_ref a;
24165 unsigned ix;
24166 dw_loc_list_struct **slot;
24168 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24169 if (AT_class (a) == dw_val_class_loc_list)
24171 dw_loc_list_ref list = AT_loc_list (a);
24172 /* TODO: perform some optimizations here, before hashing
24173 it and storing into the hash table. */
24174 hash_loc_list (list);
24175 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24176 if (*slot == NULL)
24177 *slot = list;
24178 else
24179 a->dw_attr_val.v.val_loc_list = *slot;
24182 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24186 /* Recursively assign each location list a unique index into the debug_addr
24187 section. */
24189 static void
24190 index_location_lists (dw_die_ref die)
24192 dw_die_ref c;
24193 dw_attr_ref a;
24194 unsigned ix;
24196 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24197 if (AT_class (a) == dw_val_class_loc_list)
24199 dw_loc_list_ref list = AT_loc_list (a);
24200 dw_loc_list_ref curr;
24201 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24203 /* Don't index an entry that has already been indexed
24204 or won't be output. */
24205 if (curr->begin_entry != NULL
24206 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24207 continue;
24209 curr->begin_entry
24210 = add_addr_table_entry (xstrdup (curr->begin),
24211 ate_kind_label);
24215 FOR_EACH_CHILD (die, c, index_location_lists (c));
24218 /* Optimize location lists referenced from DIE
24219 children and share them whenever possible. */
24221 static void
24222 optimize_location_lists (dw_die_ref die)
24224 loc_list_hash_type htab (500);
24225 optimize_location_lists_1 (die, &htab);
24228 /* Output stuff that dwarf requires at the end of every file,
24229 and generate the DWARF-2 debugging info. */
24231 static void
24232 dwarf2out_finish (const char *filename)
24234 limbo_die_node *node, *next_node;
24235 comdat_type_node *ctnode;
24236 unsigned int i;
24237 dw_die_ref main_comp_unit_die;
24239 /* PCH might result in DW_AT_producer string being restored from the
24240 header compilation, so always fill it with empty string initially
24241 and overwrite only here. */
24242 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24243 producer_string = gen_producer_string ();
24244 producer->dw_attr_val.v.val_str->refcount--;
24245 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24247 gen_scheduled_generic_parms_dies ();
24248 gen_remaining_tmpl_value_param_die_attribute ();
24250 /* Add the name for the main input file now. We delayed this from
24251 dwarf2out_init to avoid complications with PCH. */
24252 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24253 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24254 add_comp_dir_attribute (comp_unit_die ());
24255 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24257 bool p = false;
24258 file_table->traverse<bool *, file_table_relative_p> (&p);
24259 if (p)
24260 add_comp_dir_attribute (comp_unit_die ());
24263 if (deferred_locations_list)
24264 for (i = 0; i < deferred_locations_list->length (); i++)
24266 add_location_or_const_value_attribute (
24267 (*deferred_locations_list)[i].die,
24268 (*deferred_locations_list)[i].variable,
24269 false,
24270 DW_AT_location);
24273 /* Traverse the limbo die list, and add parent/child links. The only
24274 dies without parents that should be here are concrete instances of
24275 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24276 For concrete instances, we can get the parent die from the abstract
24277 instance. */
24278 for (node = limbo_die_list; node; node = next_node)
24280 dw_die_ref die = node->die;
24281 next_node = node->next;
24283 if (die->die_parent == NULL)
24285 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24287 if (origin && origin->die_parent)
24288 add_child_die (origin->die_parent, die);
24289 else if (is_cu_die (die))
24291 else if (seen_error ())
24292 /* It's OK to be confused by errors in the input. */
24293 add_child_die (comp_unit_die (), die);
24294 else
24296 /* In certain situations, the lexical block containing a
24297 nested function can be optimized away, which results
24298 in the nested function die being orphaned. Likewise
24299 with the return type of that nested function. Force
24300 this to be a child of the containing function.
24302 It may happen that even the containing function got fully
24303 inlined and optimized out. In that case we are lost and
24304 assign the empty child. This should not be big issue as
24305 the function is likely unreachable too. */
24306 gcc_assert (node->created_for);
24308 if (DECL_P (node->created_for))
24309 origin = get_context_die (DECL_CONTEXT (node->created_for));
24310 else if (TYPE_P (node->created_for))
24311 origin = scope_die_for (node->created_for, comp_unit_die ());
24312 else
24313 origin = comp_unit_die ();
24315 add_child_die (origin, die);
24320 limbo_die_list = NULL;
24322 #if ENABLE_ASSERT_CHECKING
24324 dw_die_ref die = comp_unit_die (), c;
24325 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24327 #endif
24328 resolve_addr (comp_unit_die ());
24329 move_marked_base_types ();
24331 for (node = deferred_asm_name; node; node = node->next)
24333 tree decl = node->created_for;
24334 /* When generating LTO bytecode we can not generate new assembler
24335 names at this point and all important decls got theirs via
24336 free-lang-data. */
24337 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24338 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24340 add_linkage_attr (node->die, decl);
24341 move_linkage_attr (node->die);
24345 deferred_asm_name = NULL;
24347 /* Walk through the list of incomplete types again, trying once more to
24348 emit full debugging info for them. */
24349 retry_incomplete_types ();
24351 if (flag_eliminate_unused_debug_types)
24352 prune_unused_types ();
24354 /* Generate separate COMDAT sections for type DIEs. */
24355 if (use_debug_types)
24357 break_out_comdat_types (comp_unit_die ());
24359 /* Each new type_unit DIE was added to the limbo die list when created.
24360 Since these have all been added to comdat_type_list, clear the
24361 limbo die list. */
24362 limbo_die_list = NULL;
24364 /* For each new comdat type unit, copy declarations for incomplete
24365 types to make the new unit self-contained (i.e., no direct
24366 references to the main compile unit). */
24367 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24368 copy_decls_for_unworthy_types (ctnode->root_die);
24369 copy_decls_for_unworthy_types (comp_unit_die ());
24371 /* In the process of copying declarations from one unit to another,
24372 we may have left some declarations behind that are no longer
24373 referenced. Prune them. */
24374 prune_unused_types ();
24377 /* Generate separate CUs for each of the include files we've seen.
24378 They will go into limbo_die_list. */
24379 if (flag_eliminate_dwarf2_dups)
24380 break_out_includes (comp_unit_die ());
24382 /* Traverse the DIE's and add add sibling attributes to those DIE's
24383 that have children. */
24384 add_sibling_attributes (comp_unit_die ());
24385 for (node = limbo_die_list; node; node = node->next)
24386 add_sibling_attributes (node->die);
24387 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24388 add_sibling_attributes (ctnode->root_die);
24390 /* When splitting DWARF info, we put some attributes in the
24391 skeleton compile_unit DIE that remains in the .o, while
24392 most attributes go in the DWO compile_unit_die. */
24393 if (dwarf_split_debug_info)
24394 main_comp_unit_die = gen_compile_unit_die (NULL);
24395 else
24396 main_comp_unit_die = comp_unit_die ();
24398 /* Output a terminator label for the .text section. */
24399 switch_to_section (text_section);
24400 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24401 if (cold_text_section)
24403 switch_to_section (cold_text_section);
24404 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24407 /* We can only use the low/high_pc attributes if all of the code was
24408 in .text. */
24409 if (!have_multiple_function_sections
24410 || (dwarf_version < 3 && dwarf_strict))
24412 /* Don't add if the CU has no associated code. */
24413 if (text_section_used)
24414 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24415 text_end_label, true);
24417 else
24419 unsigned fde_idx;
24420 dw_fde_ref fde;
24421 bool range_list_added = false;
24423 if (text_section_used)
24424 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24425 text_end_label, &range_list_added, true);
24426 if (cold_text_section_used)
24427 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24428 cold_end_label, &range_list_added, true);
24430 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24432 if (DECL_IGNORED_P (fde->decl))
24433 continue;
24434 if (!fde->in_std_section)
24435 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24436 fde->dw_fde_end, &range_list_added,
24437 true);
24438 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24439 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24440 fde->dw_fde_second_end, &range_list_added,
24441 true);
24444 if (range_list_added)
24446 /* We need to give .debug_loc and .debug_ranges an appropriate
24447 "base address". Use zero so that these addresses become
24448 absolute. Historically, we've emitted the unexpected
24449 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24450 Emit both to give time for other tools to adapt. */
24451 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24452 if (! dwarf_strict && dwarf_version < 4)
24453 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24455 add_ranges (NULL);
24459 if (debug_info_level >= DINFO_LEVEL_TERSE)
24460 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24461 debug_line_section_label);
24463 if (have_macinfo)
24464 add_AT_macptr (comp_unit_die (),
24465 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24466 macinfo_section_label);
24468 if (dwarf_split_debug_info)
24470 /* optimize_location_lists calculates the size of the lists,
24471 so index them first, and assign indices to the entries.
24472 Although optimize_location_lists will remove entries from
24473 the table, it only does so for duplicates, and therefore
24474 only reduces ref_counts to 1. */
24475 index_location_lists (comp_unit_die ());
24477 if (addr_index_table != NULL)
24479 unsigned int index = 0;
24480 addr_index_table
24481 ->traverse_noresize<unsigned int *, index_addr_table_entry>
24482 (&index);
24486 if (have_location_lists)
24487 optimize_location_lists (comp_unit_die ());
24489 save_macinfo_strings ();
24491 if (dwarf_split_debug_info)
24493 unsigned int index = 0;
24495 /* Add attributes common to skeleton compile_units and
24496 type_units. Because these attributes include strings, it
24497 must be done before freezing the string table. Top-level
24498 skeleton die attrs are added when the skeleton type unit is
24499 created, so ensure it is created by this point. */
24500 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24501 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24504 /* Output all of the compilation units. We put the main one last so that
24505 the offsets are available to output_pubnames. */
24506 for (node = limbo_die_list; node; node = node->next)
24507 output_comp_unit (node->die, 0);
24509 hash_table<comdat_type_hasher> comdat_type_table (100);
24510 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24512 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24514 /* Don't output duplicate types. */
24515 if (*slot != HTAB_EMPTY_ENTRY)
24516 continue;
24518 /* Add a pointer to the line table for the main compilation unit
24519 so that the debugger can make sense of DW_AT_decl_file
24520 attributes. */
24521 if (debug_info_level >= DINFO_LEVEL_TERSE)
24522 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24523 (!dwarf_split_debug_info
24524 ? debug_line_section_label
24525 : debug_skeleton_line_section_label));
24527 output_comdat_type_unit (ctnode);
24528 *slot = ctnode;
24531 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24532 both the main_cu and all skeleton TUs. Making this call unconditional
24533 would end up either adding a second copy of the AT_pubnames attribute, or
24534 requiring a special case in add_top_level_skeleton_die_attrs. */
24535 if (!dwarf_split_debug_info)
24536 add_AT_pubnames (comp_unit_die ());
24538 if (dwarf_split_debug_info)
24540 int mark;
24541 unsigned char checksum[16];
24542 struct md5_ctx ctx;
24544 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24545 md5_init_ctx (&ctx);
24546 mark = 0;
24547 die_checksum (comp_unit_die (), &ctx, &mark);
24548 unmark_all_dies (comp_unit_die ());
24549 md5_finish_ctx (&ctx, checksum);
24551 /* Use the first 8 bytes of the checksum as the dwo_id,
24552 and add it to both comp-unit DIEs. */
24553 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24554 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24556 /* Add the base offset of the ranges table to the skeleton
24557 comp-unit DIE. */
24558 if (ranges_table_in_use)
24559 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24560 ranges_section_label);
24562 switch_to_section (debug_addr_section);
24563 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24564 output_addr_table ();
24567 /* Output the main compilation unit if non-empty or if .debug_macinfo
24568 or .debug_macro will be emitted. */
24569 output_comp_unit (comp_unit_die (), have_macinfo);
24571 if (dwarf_split_debug_info && info_section_emitted)
24572 output_skeleton_debug_sections (main_comp_unit_die);
24574 /* Output the abbreviation table. */
24575 if (abbrev_die_table_in_use != 1)
24577 switch_to_section (debug_abbrev_section);
24578 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24579 output_abbrev_section ();
24582 /* Output location list section if necessary. */
24583 if (have_location_lists)
24585 /* Output the location lists info. */
24586 switch_to_section (debug_loc_section);
24587 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24588 output_location_lists (comp_unit_die ());
24591 output_pubtables ();
24593 /* Output the address range information if a CU (.debug_info section)
24594 was emitted. We output an empty table even if we had no functions
24595 to put in it. This because the consumer has no way to tell the
24596 difference between an empty table that we omitted and failure to
24597 generate a table that would have contained data. */
24598 if (info_section_emitted)
24600 unsigned long aranges_length = size_of_aranges ();
24602 switch_to_section (debug_aranges_section);
24603 output_aranges (aranges_length);
24606 /* Output ranges section if necessary. */
24607 if (ranges_table_in_use)
24609 switch_to_section (debug_ranges_section);
24610 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24611 output_ranges ();
24614 /* Have to end the macro section. */
24615 if (have_macinfo)
24617 switch_to_section (debug_macinfo_section);
24618 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24619 output_macinfo ();
24620 dw2_asm_output_data (1, 0, "End compilation unit");
24623 /* Output the source line correspondence table. We must do this
24624 even if there is no line information. Otherwise, on an empty
24625 translation unit, we will generate a present, but empty,
24626 .debug_info section. IRIX 6.5 `nm' will then complain when
24627 examining the file. This is done late so that any filenames
24628 used by the debug_info section are marked as 'used'. */
24629 switch_to_section (debug_line_section);
24630 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24631 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24632 output_line_info (false);
24634 if (dwarf_split_debug_info && info_section_emitted)
24636 switch_to_section (debug_skeleton_line_section);
24637 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24638 output_line_info (true);
24641 /* If we emitted any indirect strings, output the string table too. */
24642 if (debug_str_hash || skeleton_debug_str_hash)
24643 output_indirect_strings ();
24646 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24647 within the same process. For use by toplev::finalize. */
24649 void
24650 dwarf2out_c_finalize (void)
24652 last_var_location_insn = NULL;
24653 cached_next_real_insn = NULL;
24654 used_rtx_array = NULL;
24655 incomplete_types = NULL;
24656 decl_scope_table = NULL;
24657 debug_info_section = NULL;
24658 debug_skeleton_info_section = NULL;
24659 debug_abbrev_section = NULL;
24660 debug_skeleton_abbrev_section = NULL;
24661 debug_aranges_section = NULL;
24662 debug_addr_section = NULL;
24663 debug_macinfo_section = NULL;
24664 debug_line_section = NULL;
24665 debug_skeleton_line_section = NULL;
24666 debug_loc_section = NULL;
24667 debug_pubnames_section = NULL;
24668 debug_pubtypes_section = NULL;
24669 debug_str_section = NULL;
24670 debug_str_dwo_section = NULL;
24671 debug_str_offsets_section = NULL;
24672 debug_ranges_section = NULL;
24673 debug_frame_section = NULL;
24674 fde_vec = NULL;
24675 debug_str_hash = NULL;
24676 skeleton_debug_str_hash = NULL;
24677 dw2_string_counter = 0;
24678 have_multiple_function_sections = false;
24679 text_section_used = false;
24680 cold_text_section_used = false;
24681 cold_text_section = NULL;
24682 current_unit_personality = NULL;
24684 deferred_locations_list = NULL;
24686 next_die_offset = 0;
24687 single_comp_unit_die = NULL;
24688 comdat_type_list = NULL;
24689 limbo_die_list = NULL;
24690 deferred_asm_name = NULL;
24691 file_table = NULL;
24692 decl_die_table = NULL;
24693 common_block_die_table = NULL;
24694 decl_loc_table = NULL;
24695 call_arg_locations = NULL;
24696 call_arg_loc_last = NULL;
24697 call_site_count = -1;
24698 tail_call_site_count = -1;
24699 //block_map = NULL;
24700 cached_dw_loc_list_table = NULL;
24701 abbrev_die_table = NULL;
24702 abbrev_die_table_allocated = 0;
24703 abbrev_die_table_in_use = 0;
24704 line_info_label_num = 0;
24705 cur_line_info_table = NULL;
24706 text_section_line_info = NULL;
24707 cold_text_section_line_info = NULL;
24708 separate_line_info = NULL;
24709 info_section_emitted = false;
24710 pubname_table = NULL;
24711 pubtype_table = NULL;
24712 macinfo_table = NULL;
24713 ranges_table = NULL;
24714 ranges_table_allocated = 0;
24715 ranges_table_in_use = 0;
24716 ranges_by_label = 0;
24717 ranges_by_label_allocated = 0;
24718 ranges_by_label_in_use = 0;
24719 have_location_lists = false;
24720 loclabel_num = 0;
24721 poc_label_num = 0;
24722 last_emitted_file = NULL;
24723 label_num = 0;
24724 file_table_last_lookup = NULL;
24725 tmpl_value_parm_die_table = NULL;
24726 generic_type_instances = NULL;
24727 frame_pointer_fb_offset = 0;
24728 frame_pointer_fb_offset_valid = false;
24729 base_types.release ();
24732 #include "gt-dwarf2out.h"