PR ipa/64481
[official-gcc.git] / gcc / dwarf2out.c
blob4fbcb97c6d68e961c349994de537c1b83bc9b326
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2015 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 "hash-set.h"
64 #include "machmode.h"
65 #include "vec.h"
66 #include "double-int.h"
67 #include "input.h"
68 #include "alias.h"
69 #include "symtab.h"
70 #include "wide-int.h"
71 #include "inchash.h"
72 #include "real.h"
73 #include "tree.h"
74 #include "fold-const.h"
75 #include "stringpool.h"
76 #include "stor-layout.h"
77 #include "varasm.h"
78 #include "hashtab.h"
79 #include "hash-set.h"
80 #include "vec.h"
81 #include "machmode.h"
82 #include "hard-reg-set.h"
83 #include "input.h"
84 #include "function.h"
85 #include "emit-rtl.h"
86 #include "hash-table.h"
87 #include "version.h"
88 #include "flags.h"
89 #include "regs.h"
90 #include "rtlhash.h"
91 #include "insn-config.h"
92 #include "reload.h"
93 #include "output.h"
94 #include "expr.h"
95 #include "except.h"
96 #include "dwarf2.h"
97 #include "dwarf2out.h"
98 #include "dwarf2asm.h"
99 #include "toplev.h"
100 #include "md5.h"
101 #include "tm_p.h"
102 #include "diagnostic.h"
103 #include "tree-pretty-print.h"
104 #include "debug.h"
105 #include "target.h"
106 #include "common/common-target.h"
107 #include "langhooks.h"
108 #include "hash-map.h"
109 #include "is-a.h"
110 #include "plugin-api.h"
111 #include "ipa-ref.h"
112 #include "cgraph.h"
113 #include "ira.h"
114 #include "lra.h"
115 #include "dumpfile.h"
116 #include "opts.h"
117 #include "tree-dfa.h"
118 #include "gdb/gdb-index.h"
119 #include "rtl-iter.h"
121 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
122 static rtx_insn *last_var_location_insn;
123 static rtx_insn *cached_next_real_insn;
124 static void dwarf2out_decl (tree);
126 #ifdef VMS_DEBUGGING_INFO
127 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
129 /* Define this macro to be a nonzero value if the directory specifications
130 which are output in the debug info should end with a separator. */
131 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
132 /* Define this macro to evaluate to a nonzero value if GCC should refrain
133 from generating indirect strings in DWARF2 debug information, for instance
134 if your target is stuck with an old version of GDB that is unable to
135 process them properly or uses VMS Debug. */
136 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
137 #else
138 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
139 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
140 #endif
142 /* ??? Poison these here until it can be done generically. They've been
143 totally replaced in this file; make sure it stays that way. */
144 #undef DWARF2_UNWIND_INFO
145 #undef DWARF2_FRAME_INFO
146 #if (GCC_VERSION >= 3000)
147 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
148 #endif
150 /* The size of the target's pointer type. */
151 #ifndef PTR_SIZE
152 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
153 #endif
155 /* Array of RTXes referenced by the debugging information, which therefore
156 must be kept around forever. */
157 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
159 /* A pointer to the base of a list of incomplete types which might be
160 completed at some later time. incomplete_types_list needs to be a
161 vec<tree, va_gc> *because we want to tell the garbage collector about
162 it. */
163 static GTY(()) vec<tree, va_gc> *incomplete_types;
165 /* A pointer to the base of a table of references to declaration
166 scopes. This table is a display which tracks the nesting
167 of declaration scopes at the current scope and containing
168 scopes. This table is used to find the proper place to
169 define type declaration DIE's. */
170 static GTY(()) vec<tree, va_gc> *decl_scope_table;
172 /* Pointers to various DWARF2 sections. */
173 static GTY(()) section *debug_info_section;
174 static GTY(()) section *debug_skeleton_info_section;
175 static GTY(()) section *debug_abbrev_section;
176 static GTY(()) section *debug_skeleton_abbrev_section;
177 static GTY(()) section *debug_aranges_section;
178 static GTY(()) section *debug_addr_section;
179 static GTY(()) section *debug_macinfo_section;
180 static GTY(()) section *debug_line_section;
181 static GTY(()) section *debug_skeleton_line_section;
182 static GTY(()) section *debug_loc_section;
183 static GTY(()) section *debug_pubnames_section;
184 static GTY(()) section *debug_pubtypes_section;
185 static GTY(()) section *debug_str_section;
186 static GTY(()) section *debug_str_dwo_section;
187 static GTY(()) section *debug_str_offsets_section;
188 static GTY(()) section *debug_ranges_section;
189 static GTY(()) section *debug_frame_section;
191 /* Maximum size (in bytes) of an artificially generated label. */
192 #define MAX_ARTIFICIAL_LABEL_BYTES 30
194 /* According to the (draft) DWARF 3 specification, the initial length
195 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
196 bytes are 0xffffffff, followed by the length stored in the next 8
197 bytes.
199 However, the SGI/MIPS ABI uses an initial length which is equal to
200 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
202 #ifndef DWARF_INITIAL_LENGTH_SIZE
203 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
204 #endif
206 /* Round SIZE up to the nearest BOUNDARY. */
207 #define DWARF_ROUND(SIZE,BOUNDARY) \
208 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
210 /* CIE identifier. */
211 #if HOST_BITS_PER_WIDE_INT >= 64
212 #define DWARF_CIE_ID \
213 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
214 #else
215 #define DWARF_CIE_ID DW_CIE_ID
216 #endif
219 /* A vector for a table that contains frame description
220 information for each routine. */
221 #define NOT_INDEXED (-1U)
222 #define NO_INDEX_ASSIGNED (-2U)
224 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
226 struct GTY((for_user)) indirect_string_node {
227 const char *str;
228 unsigned int refcount;
229 enum dwarf_form form;
230 char *label;
231 unsigned int index;
234 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
236 typedef const char *compare_type;
238 static hashval_t hash (indirect_string_node *);
239 static bool equal (indirect_string_node *, const char *);
242 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
244 /* With split_debug_info, both the comp_dir and dwo_name go in the
245 main object file, rather than the dwo, similar to the force_direct
246 parameter elsewhere but with additional complications:
248 1) The string is needed in both the main object file and the dwo.
249 That is, the comp_dir and dwo_name will appear in both places.
251 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
252 DW_FORM_GNU_str_index.
254 3) GCC chooses the form to use late, depending on the size and
255 reference count.
257 Rather than forcing the all debug string handling functions and
258 callers to deal with these complications, simply use a separate,
259 special-cased string table for any attribute that should go in the
260 main object file. This limits the complexity to just the places
261 that need it. */
263 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
265 static GTY(()) int dw2_string_counter;
267 /* True if the compilation unit places functions in more than one section. */
268 static GTY(()) bool have_multiple_function_sections = false;
270 /* Whether the default text and cold text sections have been used at all. */
272 static GTY(()) bool text_section_used = false;
273 static GTY(()) bool cold_text_section_used = false;
275 /* The default cold text section. */
276 static GTY(()) section *cold_text_section;
278 /* The DIE for C++14 'auto' in a function return type. */
279 static GTY(()) dw_die_ref auto_die;
281 /* The DIE for C++14 'decltype(auto)' in a function return type. */
282 static GTY(()) dw_die_ref decltype_auto_die;
284 /* Forward declarations for functions defined in this file. */
286 static char *stripattributes (const char *);
287 static void output_call_frame_info (int);
288 static void dwarf2out_note_section_used (void);
290 /* Personality decl of current unit. Used only when assembler does not support
291 personality CFI. */
292 static GTY(()) rtx current_unit_personality;
294 /* Data and reference forms for relocatable data. */
295 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
296 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
298 #ifndef DEBUG_FRAME_SECTION
299 #define DEBUG_FRAME_SECTION ".debug_frame"
300 #endif
302 #ifndef FUNC_BEGIN_LABEL
303 #define FUNC_BEGIN_LABEL "LFB"
304 #endif
306 #ifndef FUNC_END_LABEL
307 #define FUNC_END_LABEL "LFE"
308 #endif
310 #ifndef PROLOGUE_END_LABEL
311 #define PROLOGUE_END_LABEL "LPE"
312 #endif
314 #ifndef EPILOGUE_BEGIN_LABEL
315 #define EPILOGUE_BEGIN_LABEL "LEB"
316 #endif
318 #ifndef FRAME_BEGIN_LABEL
319 #define FRAME_BEGIN_LABEL "Lframe"
320 #endif
321 #define CIE_AFTER_SIZE_LABEL "LSCIE"
322 #define CIE_END_LABEL "LECIE"
323 #define FDE_LABEL "LSFDE"
324 #define FDE_AFTER_SIZE_LABEL "LASFDE"
325 #define FDE_END_LABEL "LEFDE"
326 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
327 #define LINE_NUMBER_END_LABEL "LELT"
328 #define LN_PROLOG_AS_LABEL "LASLTP"
329 #define LN_PROLOG_END_LABEL "LELTP"
330 #define DIE_LABEL_PREFIX "DW"
332 /* Match the base name of a file to the base name of a compilation unit. */
334 static int
335 matches_main_base (const char *path)
337 /* Cache the last query. */
338 static const char *last_path = NULL;
339 static int last_match = 0;
340 if (path != last_path)
342 const char *base;
343 int length = base_of_path (path, &base);
344 last_path = path;
345 last_match = (length == main_input_baselength
346 && memcmp (base, main_input_basename, length) == 0);
348 return last_match;
351 #ifdef DEBUG_DEBUG_STRUCT
353 static int
354 dump_struct_debug (tree type, enum debug_info_usage usage,
355 enum debug_struct_file criterion, int generic,
356 int matches, int result)
358 /* Find the type name. */
359 tree type_decl = TYPE_STUB_DECL (type);
360 tree t = type_decl;
361 const char *name = 0;
362 if (TREE_CODE (t) == TYPE_DECL)
363 t = DECL_NAME (t);
364 if (t)
365 name = IDENTIFIER_POINTER (t);
367 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
368 criterion,
369 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
370 matches ? "bas" : "hdr",
371 generic ? "gen" : "ord",
372 usage == DINFO_USAGE_DFN ? ";" :
373 usage == DINFO_USAGE_DIR_USE ? "." : "*",
374 result,
375 (void*) type_decl, name);
376 return result;
378 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
379 dump_struct_debug (type, usage, criterion, generic, matches, result)
381 #else
383 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
384 (result)
386 #endif
388 /* Get the number of HOST_WIDE_INTs needed to represent the precision
389 of the number. */
391 static unsigned int
392 get_full_len (const wide_int &op)
394 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
395 / HOST_BITS_PER_WIDE_INT);
398 static bool
399 should_emit_struct_debug (tree type, enum debug_info_usage usage)
401 enum debug_struct_file criterion;
402 tree type_decl;
403 bool generic = lang_hooks.types.generic_p (type);
405 if (generic)
406 criterion = debug_struct_generic[usage];
407 else
408 criterion = debug_struct_ordinary[usage];
410 if (criterion == DINFO_STRUCT_FILE_NONE)
411 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
412 if (criterion == DINFO_STRUCT_FILE_ANY)
413 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
415 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
417 if (type_decl != NULL)
419 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
420 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
422 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
423 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
426 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
429 /* Return a pointer to a copy of the section string name S with all
430 attributes stripped off, and an asterisk prepended (for assemble_name). */
432 static inline char *
433 stripattributes (const char *s)
435 char *stripped = XNEWVEC (char, strlen (s) + 2);
436 char *p = stripped;
438 *p++ = '*';
440 while (*s && *s != ',')
441 *p++ = *s++;
443 *p = '\0';
444 return stripped;
447 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
448 switch to the data section instead, and write out a synthetic start label
449 for collect2 the first time around. */
451 static void
452 switch_to_eh_frame_section (bool back)
454 tree label;
456 #ifdef EH_FRAME_SECTION_NAME
457 if (eh_frame_section == 0)
459 int flags;
461 if (EH_TABLES_CAN_BE_READ_ONLY)
463 int fde_encoding;
464 int per_encoding;
465 int lsda_encoding;
467 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
468 /*global=*/0);
469 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
470 /*global=*/1);
471 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
472 /*global=*/0);
473 flags = ((! flag_pic
474 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
475 && (fde_encoding & 0x70) != DW_EH_PE_aligned
476 && (per_encoding & 0x70) != DW_EH_PE_absptr
477 && (per_encoding & 0x70) != DW_EH_PE_aligned
478 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
479 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
480 ? 0 : SECTION_WRITE);
482 else
483 flags = SECTION_WRITE;
484 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
486 #endif /* EH_FRAME_SECTION_NAME */
488 if (eh_frame_section)
489 switch_to_section (eh_frame_section);
490 else
492 /* We have no special eh_frame section. Put the information in
493 the data section and emit special labels to guide collect2. */
494 switch_to_section (data_section);
496 if (!back)
498 label = get_file_function_name ("F");
499 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
500 targetm.asm_out.globalize_label (asm_out_file,
501 IDENTIFIER_POINTER (label));
502 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
507 /* Switch [BACK] to the eh or debug frame table section, depending on
508 FOR_EH. */
510 static void
511 switch_to_frame_table_section (int for_eh, bool back)
513 if (for_eh)
514 switch_to_eh_frame_section (back);
515 else
517 if (!debug_frame_section)
518 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
519 SECTION_DEBUG, NULL);
520 switch_to_section (debug_frame_section);
524 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
526 enum dw_cfi_oprnd_type
527 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
529 switch (cfi)
531 case DW_CFA_nop:
532 case DW_CFA_GNU_window_save:
533 case DW_CFA_remember_state:
534 case DW_CFA_restore_state:
535 return dw_cfi_oprnd_unused;
537 case DW_CFA_set_loc:
538 case DW_CFA_advance_loc1:
539 case DW_CFA_advance_loc2:
540 case DW_CFA_advance_loc4:
541 case DW_CFA_MIPS_advance_loc8:
542 return dw_cfi_oprnd_addr;
544 case DW_CFA_offset:
545 case DW_CFA_offset_extended:
546 case DW_CFA_def_cfa:
547 case DW_CFA_offset_extended_sf:
548 case DW_CFA_def_cfa_sf:
549 case DW_CFA_restore:
550 case DW_CFA_restore_extended:
551 case DW_CFA_undefined:
552 case DW_CFA_same_value:
553 case DW_CFA_def_cfa_register:
554 case DW_CFA_register:
555 case DW_CFA_expression:
556 return dw_cfi_oprnd_reg_num;
558 case DW_CFA_def_cfa_offset:
559 case DW_CFA_GNU_args_size:
560 case DW_CFA_def_cfa_offset_sf:
561 return dw_cfi_oprnd_offset;
563 case DW_CFA_def_cfa_expression:
564 return dw_cfi_oprnd_loc;
566 default:
567 gcc_unreachable ();
571 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
573 enum dw_cfi_oprnd_type
574 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
576 switch (cfi)
578 case DW_CFA_def_cfa:
579 case DW_CFA_def_cfa_sf:
580 case DW_CFA_offset:
581 case DW_CFA_offset_extended_sf:
582 case DW_CFA_offset_extended:
583 return dw_cfi_oprnd_offset;
585 case DW_CFA_register:
586 return dw_cfi_oprnd_reg_num;
588 case DW_CFA_expression:
589 return dw_cfi_oprnd_loc;
591 default:
592 return dw_cfi_oprnd_unused;
596 /* Output one FDE. */
598 static void
599 output_fde (dw_fde_ref fde, bool for_eh, bool second,
600 char *section_start_label, int fde_encoding, char *augmentation,
601 bool any_lsda_needed, int lsda_encoding)
603 const char *begin, *end;
604 static unsigned int j;
605 char l1[20], l2[20];
607 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
608 /* empty */ 0);
609 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
610 for_eh + j);
611 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
612 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
613 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
614 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
615 " indicating 64-bit DWARF extension");
616 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
617 "FDE Length");
618 ASM_OUTPUT_LABEL (asm_out_file, l1);
620 if (for_eh)
621 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
622 else
623 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
624 debug_frame_section, "FDE CIE offset");
626 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
627 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
629 if (for_eh)
631 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
632 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
633 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
634 "FDE initial location");
635 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
636 end, begin, "FDE address range");
638 else
640 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
641 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
644 if (augmentation[0])
646 if (any_lsda_needed)
648 int size = size_of_encoded_value (lsda_encoding);
650 if (lsda_encoding == DW_EH_PE_aligned)
652 int offset = ( 4 /* Length */
653 + 4 /* CIE offset */
654 + 2 * size_of_encoded_value (fde_encoding)
655 + 1 /* Augmentation size */ );
656 int pad = -offset & (PTR_SIZE - 1);
658 size += pad;
659 gcc_assert (size_of_uleb128 (size) == 1);
662 dw2_asm_output_data_uleb128 (size, "Augmentation size");
664 if (fde->uses_eh_lsda)
666 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
667 fde->funcdef_number);
668 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
669 gen_rtx_SYMBOL_REF (Pmode, l1),
670 false,
671 "Language Specific Data Area");
673 else
675 if (lsda_encoding == DW_EH_PE_aligned)
676 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
677 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
678 "Language Specific Data Area (none)");
681 else
682 dw2_asm_output_data_uleb128 (0, "Augmentation size");
685 /* Loop through the Call Frame Instructions associated with this FDE. */
686 fde->dw_fde_current_label = begin;
688 size_t from, until, i;
690 from = 0;
691 until = vec_safe_length (fde->dw_fde_cfi);
693 if (fde->dw_fde_second_begin == NULL)
695 else if (!second)
696 until = fde->dw_fde_switch_cfi_index;
697 else
698 from = fde->dw_fde_switch_cfi_index;
700 for (i = from; i < until; i++)
701 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
704 /* If we are to emit a ref/link from function bodies to their frame tables,
705 do it now. This is typically performed to make sure that tables
706 associated with functions are dragged with them and not discarded in
707 garbage collecting links. We need to do this on a per function basis to
708 cope with -ffunction-sections. */
710 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
711 /* Switch to the function section, emit the ref to the tables, and
712 switch *back* into the table section. */
713 switch_to_section (function_section (fde->decl));
714 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
715 switch_to_frame_table_section (for_eh, true);
716 #endif
718 /* Pad the FDE out to an address sized boundary. */
719 ASM_OUTPUT_ALIGN (asm_out_file,
720 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
721 ASM_OUTPUT_LABEL (asm_out_file, l2);
723 j += 2;
726 /* Return true if frame description entry FDE is needed for EH. */
728 static bool
729 fde_needed_for_eh_p (dw_fde_ref fde)
731 if (flag_asynchronous_unwind_tables)
732 return true;
734 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
735 return true;
737 if (fde->uses_eh_lsda)
738 return true;
740 /* If exceptions are enabled, we have collected nothrow info. */
741 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
742 return false;
744 return true;
747 /* Output the call frame information used to record information
748 that relates to calculating the frame pointer, and records the
749 location of saved registers. */
751 static void
752 output_call_frame_info (int for_eh)
754 unsigned int i;
755 dw_fde_ref fde;
756 dw_cfi_ref cfi;
757 char l1[20], l2[20], section_start_label[20];
758 bool any_lsda_needed = false;
759 char augmentation[6];
760 int augmentation_size;
761 int fde_encoding = DW_EH_PE_absptr;
762 int per_encoding = DW_EH_PE_absptr;
763 int lsda_encoding = DW_EH_PE_absptr;
764 int return_reg;
765 rtx personality = NULL;
766 int dw_cie_version;
768 /* Don't emit a CIE if there won't be any FDEs. */
769 if (!fde_vec)
770 return;
772 /* Nothing to do if the assembler's doing it all. */
773 if (dwarf2out_do_cfi_asm ())
774 return;
776 /* If we don't have any functions we'll want to unwind out of, don't emit
777 any EH unwind information. If we make FDEs linkonce, we may have to
778 emit an empty label for an FDE that wouldn't otherwise be emitted. We
779 want to avoid having an FDE kept around when the function it refers to
780 is discarded. Example where this matters: a primary function template
781 in C++ requires EH information, an explicit specialization doesn't. */
782 if (for_eh)
784 bool any_eh_needed = false;
786 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
788 if (fde->uses_eh_lsda)
789 any_eh_needed = any_lsda_needed = true;
790 else if (fde_needed_for_eh_p (fde))
791 any_eh_needed = true;
792 else if (TARGET_USES_WEAK_UNWIND_INFO)
793 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
796 if (!any_eh_needed)
797 return;
800 /* We're going to be generating comments, so turn on app. */
801 if (flag_debug_asm)
802 app_enable ();
804 /* Switch to the proper frame section, first time. */
805 switch_to_frame_table_section (for_eh, false);
807 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
808 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
810 /* Output the CIE. */
811 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
812 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
813 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
814 dw2_asm_output_data (4, 0xffffffff,
815 "Initial length escape value indicating 64-bit DWARF extension");
816 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
817 "Length of Common Information Entry");
818 ASM_OUTPUT_LABEL (asm_out_file, l1);
820 /* Now that the CIE pointer is PC-relative for EH,
821 use 0 to identify the CIE. */
822 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
823 (for_eh ? 0 : DWARF_CIE_ID),
824 "CIE Identifier Tag");
826 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
827 use CIE version 1, unless that would produce incorrect results
828 due to overflowing the return register column. */
829 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
830 dw_cie_version = 1;
831 if (return_reg >= 256 || dwarf_version > 2)
832 dw_cie_version = 3;
833 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
835 augmentation[0] = 0;
836 augmentation_size = 0;
838 personality = current_unit_personality;
839 if (for_eh)
841 char *p;
843 /* Augmentation:
844 z Indicates that a uleb128 is present to size the
845 augmentation section.
846 L Indicates the encoding (and thus presence) of
847 an LSDA pointer in the FDE augmentation.
848 R Indicates a non-default pointer encoding for
849 FDE code pointers.
850 P Indicates the presence of an encoding + language
851 personality routine in the CIE augmentation. */
853 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
854 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
855 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
857 p = augmentation + 1;
858 if (personality)
860 *p++ = 'P';
861 augmentation_size += 1 + size_of_encoded_value (per_encoding);
862 assemble_external_libcall (personality);
864 if (any_lsda_needed)
866 *p++ = 'L';
867 augmentation_size += 1;
869 if (fde_encoding != DW_EH_PE_absptr)
871 *p++ = 'R';
872 augmentation_size += 1;
874 if (p > augmentation + 1)
876 augmentation[0] = 'z';
877 *p = '\0';
880 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
881 if (personality && per_encoding == DW_EH_PE_aligned)
883 int offset = ( 4 /* Length */
884 + 4 /* CIE Id */
885 + 1 /* CIE version */
886 + strlen (augmentation) + 1 /* Augmentation */
887 + size_of_uleb128 (1) /* Code alignment */
888 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
889 + 1 /* RA column */
890 + 1 /* Augmentation size */
891 + 1 /* Personality encoding */ );
892 int pad = -offset & (PTR_SIZE - 1);
894 augmentation_size += pad;
896 /* Augmentations should be small, so there's scarce need to
897 iterate for a solution. Die if we exceed one uleb128 byte. */
898 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
902 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
903 if (dw_cie_version >= 4)
905 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
906 dw2_asm_output_data (1, 0, "CIE Segment Size");
908 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
909 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
910 "CIE Data Alignment Factor");
912 if (dw_cie_version == 1)
913 dw2_asm_output_data (1, return_reg, "CIE RA Column");
914 else
915 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
917 if (augmentation[0])
919 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
920 if (personality)
922 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
923 eh_data_format_name (per_encoding));
924 dw2_asm_output_encoded_addr_rtx (per_encoding,
925 personality,
926 true, NULL);
929 if (any_lsda_needed)
930 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
931 eh_data_format_name (lsda_encoding));
933 if (fde_encoding != DW_EH_PE_absptr)
934 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
935 eh_data_format_name (fde_encoding));
938 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
939 output_cfi (cfi, NULL, for_eh);
941 /* Pad the CIE out to an address sized boundary. */
942 ASM_OUTPUT_ALIGN (asm_out_file,
943 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
944 ASM_OUTPUT_LABEL (asm_out_file, l2);
946 /* Loop through all of the FDE's. */
947 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
949 unsigned int k;
951 /* Don't emit EH unwind info for leaf functions that don't need it. */
952 if (for_eh && !fde_needed_for_eh_p (fde))
953 continue;
955 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
956 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
957 augmentation, any_lsda_needed, lsda_encoding);
960 if (for_eh && targetm.terminate_dw2_eh_frame_info)
961 dw2_asm_output_data (4, 0, "End of Table");
963 /* Turn off app to make assembly quicker. */
964 if (flag_debug_asm)
965 app_disable ();
968 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
970 static void
971 dwarf2out_do_cfi_startproc (bool second)
973 int enc;
974 rtx ref;
975 rtx personality = get_personality_function (current_function_decl);
977 fprintf (asm_out_file, "\t.cfi_startproc\n");
979 if (personality)
981 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
982 ref = personality;
984 /* ??? The GAS support isn't entirely consistent. We have to
985 handle indirect support ourselves, but PC-relative is done
986 in the assembler. Further, the assembler can't handle any
987 of the weirder relocation types. */
988 if (enc & DW_EH_PE_indirect)
989 ref = dw2_force_const_mem (ref, true);
991 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
992 output_addr_const (asm_out_file, ref);
993 fputc ('\n', asm_out_file);
996 if (crtl->uses_eh_lsda)
998 char lab[20];
1000 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1001 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1002 current_function_funcdef_no);
1003 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1004 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1006 if (enc & DW_EH_PE_indirect)
1007 ref = dw2_force_const_mem (ref, true);
1009 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1010 output_addr_const (asm_out_file, ref);
1011 fputc ('\n', asm_out_file);
1015 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1016 this allocation may be done before pass_final. */
1018 dw_fde_ref
1019 dwarf2out_alloc_current_fde (void)
1021 dw_fde_ref fde;
1023 fde = ggc_cleared_alloc<dw_fde_node> ();
1024 fde->decl = current_function_decl;
1025 fde->funcdef_number = current_function_funcdef_no;
1026 fde->fde_index = vec_safe_length (fde_vec);
1027 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1028 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1029 fde->nothrow = crtl->nothrow;
1030 fde->drap_reg = INVALID_REGNUM;
1031 fde->vdrap_reg = INVALID_REGNUM;
1033 /* Record the FDE associated with this function. */
1034 cfun->fde = fde;
1035 vec_safe_push (fde_vec, fde);
1037 return fde;
1040 /* Output a marker (i.e. a label) for the beginning of a function, before
1041 the prologue. */
1043 void
1044 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1045 const char *file ATTRIBUTE_UNUSED)
1047 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1048 char * dup_label;
1049 dw_fde_ref fde;
1050 section *fnsec;
1051 bool do_frame;
1053 current_function_func_begin_label = NULL;
1055 do_frame = dwarf2out_do_frame ();
1057 /* ??? current_function_func_begin_label is also used by except.c for
1058 call-site information. We must emit this label if it might be used. */
1059 if (!do_frame
1060 && (!flag_exceptions
1061 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1062 return;
1064 fnsec = function_section (current_function_decl);
1065 switch_to_section (fnsec);
1066 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1067 current_function_funcdef_no);
1068 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1069 current_function_funcdef_no);
1070 dup_label = xstrdup (label);
1071 current_function_func_begin_label = dup_label;
1073 /* We can elide the fde allocation if we're not emitting debug info. */
1074 if (!do_frame)
1075 return;
1077 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1078 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1079 would include pass_dwarf2_frame. If we've not created the FDE yet,
1080 do so now. */
1081 fde = cfun->fde;
1082 if (fde == NULL)
1083 fde = dwarf2out_alloc_current_fde ();
1085 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1086 fde->dw_fde_begin = dup_label;
1087 fde->dw_fde_current_label = dup_label;
1088 fde->in_std_section = (fnsec == text_section
1089 || (cold_text_section && fnsec == cold_text_section));
1091 /* We only want to output line number information for the genuine dwarf2
1092 prologue case, not the eh frame case. */
1093 #ifdef DWARF2_DEBUGGING_INFO
1094 if (file)
1095 dwarf2out_source_line (line, file, 0, true);
1096 #endif
1098 if (dwarf2out_do_cfi_asm ())
1099 dwarf2out_do_cfi_startproc (false);
1100 else
1102 rtx personality = get_personality_function (current_function_decl);
1103 if (!current_unit_personality)
1104 current_unit_personality = personality;
1106 /* We cannot keep a current personality per function as without CFI
1107 asm, at the point where we emit the CFI data, there is no current
1108 function anymore. */
1109 if (personality && current_unit_personality != personality)
1110 sorry ("multiple EH personalities are supported only with assemblers "
1111 "supporting .cfi_personality directive");
1115 /* Output a marker (i.e. a label) for the end of the generated code
1116 for a function prologue. This gets called *after* the prologue code has
1117 been generated. */
1119 void
1120 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1121 const char *file ATTRIBUTE_UNUSED)
1123 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1125 /* Output a label to mark the endpoint of the code generated for this
1126 function. */
1127 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1128 current_function_funcdef_no);
1129 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1130 current_function_funcdef_no);
1131 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1134 /* Output a marker (i.e. a label) for the beginning of the generated code
1135 for a function epilogue. This gets called *before* the prologue code has
1136 been generated. */
1138 void
1139 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1140 const char *file ATTRIBUTE_UNUSED)
1142 dw_fde_ref fde = cfun->fde;
1143 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1145 if (fde->dw_fde_vms_begin_epilogue)
1146 return;
1148 /* Output a label to mark the endpoint of the code generated for this
1149 function. */
1150 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1151 current_function_funcdef_no);
1152 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1153 current_function_funcdef_no);
1154 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1157 /* Output a marker (i.e. a label) for the absolute end of the generated code
1158 for a function definition. This gets called *after* the epilogue code has
1159 been generated. */
1161 void
1162 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1163 const char *file ATTRIBUTE_UNUSED)
1165 dw_fde_ref fde;
1166 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1168 last_var_location_insn = NULL;
1169 cached_next_real_insn = NULL;
1171 if (dwarf2out_do_cfi_asm ())
1172 fprintf (asm_out_file, "\t.cfi_endproc\n");
1174 /* Output a label to mark the endpoint of the code generated for this
1175 function. */
1176 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1177 current_function_funcdef_no);
1178 ASM_OUTPUT_LABEL (asm_out_file, label);
1179 fde = cfun->fde;
1180 gcc_assert (fde != NULL);
1181 if (fde->dw_fde_second_begin == NULL)
1182 fde->dw_fde_end = xstrdup (label);
1185 void
1186 dwarf2out_frame_finish (void)
1188 /* Output call frame information. */
1189 if (targetm.debug_unwind_info () == UI_DWARF2)
1190 output_call_frame_info (0);
1192 /* Output another copy for the unwinder. */
1193 if ((flag_unwind_tables || flag_exceptions)
1194 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1195 output_call_frame_info (1);
1198 /* Note that the current function section is being used for code. */
1200 static void
1201 dwarf2out_note_section_used (void)
1203 section *sec = current_function_section ();
1204 if (sec == text_section)
1205 text_section_used = true;
1206 else if (sec == cold_text_section)
1207 cold_text_section_used = true;
1210 static void var_location_switch_text_section (void);
1211 static void set_cur_line_info_table (section *);
1213 void
1214 dwarf2out_switch_text_section (void)
1216 section *sect;
1217 dw_fde_ref fde = cfun->fde;
1219 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1221 if (!in_cold_section_p)
1223 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1224 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1225 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1227 else
1229 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1230 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1231 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1233 have_multiple_function_sections = true;
1235 /* There is no need to mark used sections when not debugging. */
1236 if (cold_text_section != NULL)
1237 dwarf2out_note_section_used ();
1239 if (dwarf2out_do_cfi_asm ())
1240 fprintf (asm_out_file, "\t.cfi_endproc\n");
1242 /* Now do the real section switch. */
1243 sect = current_function_section ();
1244 switch_to_section (sect);
1246 fde->second_in_std_section
1247 = (sect == text_section
1248 || (cold_text_section && sect == cold_text_section));
1250 if (dwarf2out_do_cfi_asm ())
1251 dwarf2out_do_cfi_startproc (true);
1253 var_location_switch_text_section ();
1255 if (cold_text_section != NULL)
1256 set_cur_line_info_table (sect);
1259 /* And now, the subset of the debugging information support code necessary
1260 for emitting location expressions. */
1262 /* Data about a single source file. */
1263 struct GTY((for_user)) dwarf_file_data {
1264 const char * filename;
1265 int emitted_number;
1268 typedef struct GTY(()) deferred_locations_struct
1270 tree variable;
1271 dw_die_ref die;
1272 } deferred_locations;
1275 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1278 /* Describe an entry into the .debug_addr section. */
1280 enum ate_kind {
1281 ate_kind_rtx,
1282 ate_kind_rtx_dtprel,
1283 ate_kind_label
1286 typedef struct GTY((for_user)) addr_table_entry_struct {
1287 enum ate_kind kind;
1288 unsigned int refcount;
1289 unsigned int index;
1290 union addr_table_entry_struct_union
1292 rtx GTY ((tag ("0"))) rtl;
1293 char * GTY ((tag ("1"))) label;
1295 GTY ((desc ("%1.kind"))) addr;
1297 addr_table_entry;
1299 /* Location lists are ranges + location descriptions for that range,
1300 so you can track variables that are in different places over
1301 their entire life. */
1302 typedef struct GTY(()) dw_loc_list_struct {
1303 dw_loc_list_ref dw_loc_next;
1304 const char *begin; /* Label and addr_entry for start of range */
1305 addr_table_entry *begin_entry;
1306 const char *end; /* Label for end of range */
1307 char *ll_symbol; /* Label for beginning of location list.
1308 Only on head of list */
1309 const char *section; /* Section this loclist is relative to */
1310 dw_loc_descr_ref expr;
1311 hashval_t hash;
1312 /* True if all addresses in this and subsequent lists are known to be
1313 resolved. */
1314 bool resolved_addr;
1315 /* True if this list has been replaced by dw_loc_next. */
1316 bool replaced;
1317 bool emitted;
1318 /* True if the range should be emitted even if begin and end
1319 are the same. */
1320 bool force;
1321 } dw_loc_list_node;
1323 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1325 /* Convert a DWARF stack opcode into its string name. */
1327 static const char *
1328 dwarf_stack_op_name (unsigned int op)
1330 const char *name = get_DW_OP_name (op);
1332 if (name != NULL)
1333 return name;
1335 return "OP_<unknown>";
1338 /* Return a pointer to a newly allocated location description. Location
1339 descriptions are simple expression terms that can be strung
1340 together to form more complicated location (address) descriptions. */
1342 static inline dw_loc_descr_ref
1343 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1344 unsigned HOST_WIDE_INT oprnd2)
1346 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1348 descr->dw_loc_opc = op;
1349 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1350 descr->dw_loc_oprnd1.val_entry = NULL;
1351 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1352 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1353 descr->dw_loc_oprnd2.val_entry = NULL;
1354 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1356 return descr;
1359 /* Return a pointer to a newly allocated location description for
1360 REG and OFFSET. */
1362 static inline dw_loc_descr_ref
1363 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1365 if (reg <= 31)
1366 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1367 offset, 0);
1368 else
1369 return new_loc_descr (DW_OP_bregx, reg, offset);
1372 /* Add a location description term to a location description expression. */
1374 static inline void
1375 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1377 dw_loc_descr_ref *d;
1379 /* Find the end of the chain. */
1380 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1383 *d = descr;
1386 /* Compare two location operands for exact equality. */
1388 static bool
1389 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1391 if (a->val_class != b->val_class)
1392 return false;
1393 switch (a->val_class)
1395 case dw_val_class_none:
1396 return true;
1397 case dw_val_class_addr:
1398 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1400 case dw_val_class_offset:
1401 case dw_val_class_unsigned_const:
1402 case dw_val_class_const:
1403 case dw_val_class_range_list:
1404 case dw_val_class_lineptr:
1405 case dw_val_class_macptr:
1406 /* These are all HOST_WIDE_INT, signed or unsigned. */
1407 return a->v.val_unsigned == b->v.val_unsigned;
1409 case dw_val_class_loc:
1410 return a->v.val_loc == b->v.val_loc;
1411 case dw_val_class_loc_list:
1412 return a->v.val_loc_list == b->v.val_loc_list;
1413 case dw_val_class_die_ref:
1414 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1415 case dw_val_class_fde_ref:
1416 return a->v.val_fde_index == b->v.val_fde_index;
1417 case dw_val_class_lbl_id:
1418 case dw_val_class_high_pc:
1419 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1420 case dw_val_class_str:
1421 return a->v.val_str == b->v.val_str;
1422 case dw_val_class_flag:
1423 return a->v.val_flag == b->v.val_flag;
1424 case dw_val_class_file:
1425 return a->v.val_file == b->v.val_file;
1426 case dw_val_class_decl_ref:
1427 return a->v.val_decl_ref == b->v.val_decl_ref;
1429 case dw_val_class_const_double:
1430 return (a->v.val_double.high == b->v.val_double.high
1431 && a->v.val_double.low == b->v.val_double.low);
1433 case dw_val_class_wide_int:
1434 return *a->v.val_wide == *b->v.val_wide;
1436 case dw_val_class_vec:
1438 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1439 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1441 return (a_len == b_len
1442 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1445 case dw_val_class_data8:
1446 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1448 case dw_val_class_vms_delta:
1449 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1450 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1452 gcc_unreachable ();
1455 /* Compare two location atoms for exact equality. */
1457 static bool
1458 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1460 if (a->dw_loc_opc != b->dw_loc_opc)
1461 return false;
1463 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1464 address size, but since we always allocate cleared storage it
1465 should be zero for other types of locations. */
1466 if (a->dtprel != b->dtprel)
1467 return false;
1469 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1470 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1473 /* Compare two complete location expressions for exact equality. */
1475 bool
1476 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1478 while (1)
1480 if (a == b)
1481 return true;
1482 if (a == NULL || b == NULL)
1483 return false;
1484 if (!loc_descr_equal_p_1 (a, b))
1485 return false;
1487 a = a->dw_loc_next;
1488 b = b->dw_loc_next;
1493 /* Add a constant OFFSET to a location expression. */
1495 static void
1496 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1498 dw_loc_descr_ref loc;
1499 HOST_WIDE_INT *p;
1501 gcc_assert (*list_head != NULL);
1503 if (!offset)
1504 return;
1506 /* Find the end of the chain. */
1507 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1510 p = NULL;
1511 if (loc->dw_loc_opc == DW_OP_fbreg
1512 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1513 p = &loc->dw_loc_oprnd1.v.val_int;
1514 else if (loc->dw_loc_opc == DW_OP_bregx)
1515 p = &loc->dw_loc_oprnd2.v.val_int;
1517 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1518 offset. Don't optimize if an signed integer overflow would happen. */
1519 if (p != NULL
1520 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1521 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1522 *p += offset;
1524 else if (offset > 0)
1525 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1527 else
1529 loc->dw_loc_next = int_loc_descriptor (-offset);
1530 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1534 /* Add a constant OFFSET to a location list. */
1536 static void
1537 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1539 dw_loc_list_ref d;
1540 for (d = list_head; d != NULL; d = d->dw_loc_next)
1541 loc_descr_plus_const (&d->expr, offset);
1544 #define DWARF_REF_SIZE \
1545 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1547 static unsigned long int get_base_type_offset (dw_die_ref);
1549 /* Return the size of a location descriptor. */
1551 static unsigned long
1552 size_of_loc_descr (dw_loc_descr_ref loc)
1554 unsigned long size = 1;
1556 switch (loc->dw_loc_opc)
1558 case DW_OP_addr:
1559 size += DWARF2_ADDR_SIZE;
1560 break;
1561 case DW_OP_GNU_addr_index:
1562 case DW_OP_GNU_const_index:
1563 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1564 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1565 break;
1566 case DW_OP_const1u:
1567 case DW_OP_const1s:
1568 size += 1;
1569 break;
1570 case DW_OP_const2u:
1571 case DW_OP_const2s:
1572 size += 2;
1573 break;
1574 case DW_OP_const4u:
1575 case DW_OP_const4s:
1576 size += 4;
1577 break;
1578 case DW_OP_const8u:
1579 case DW_OP_const8s:
1580 size += 8;
1581 break;
1582 case DW_OP_constu:
1583 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1584 break;
1585 case DW_OP_consts:
1586 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1587 break;
1588 case DW_OP_pick:
1589 size += 1;
1590 break;
1591 case DW_OP_plus_uconst:
1592 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1593 break;
1594 case DW_OP_skip:
1595 case DW_OP_bra:
1596 size += 2;
1597 break;
1598 case DW_OP_breg0:
1599 case DW_OP_breg1:
1600 case DW_OP_breg2:
1601 case DW_OP_breg3:
1602 case DW_OP_breg4:
1603 case DW_OP_breg5:
1604 case DW_OP_breg6:
1605 case DW_OP_breg7:
1606 case DW_OP_breg8:
1607 case DW_OP_breg9:
1608 case DW_OP_breg10:
1609 case DW_OP_breg11:
1610 case DW_OP_breg12:
1611 case DW_OP_breg13:
1612 case DW_OP_breg14:
1613 case DW_OP_breg15:
1614 case DW_OP_breg16:
1615 case DW_OP_breg17:
1616 case DW_OP_breg18:
1617 case DW_OP_breg19:
1618 case DW_OP_breg20:
1619 case DW_OP_breg21:
1620 case DW_OP_breg22:
1621 case DW_OP_breg23:
1622 case DW_OP_breg24:
1623 case DW_OP_breg25:
1624 case DW_OP_breg26:
1625 case DW_OP_breg27:
1626 case DW_OP_breg28:
1627 case DW_OP_breg29:
1628 case DW_OP_breg30:
1629 case DW_OP_breg31:
1630 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1631 break;
1632 case DW_OP_regx:
1633 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1634 break;
1635 case DW_OP_fbreg:
1636 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1637 break;
1638 case DW_OP_bregx:
1639 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1640 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1641 break;
1642 case DW_OP_piece:
1643 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1644 break;
1645 case DW_OP_bit_piece:
1646 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1647 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1648 break;
1649 case DW_OP_deref_size:
1650 case DW_OP_xderef_size:
1651 size += 1;
1652 break;
1653 case DW_OP_call2:
1654 size += 2;
1655 break;
1656 case DW_OP_call4:
1657 size += 4;
1658 break;
1659 case DW_OP_call_ref:
1660 size += DWARF_REF_SIZE;
1661 break;
1662 case DW_OP_implicit_value:
1663 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1664 + loc->dw_loc_oprnd1.v.val_unsigned;
1665 break;
1666 case DW_OP_GNU_implicit_pointer:
1667 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1668 break;
1669 case DW_OP_GNU_entry_value:
1671 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1672 size += size_of_uleb128 (op_size) + op_size;
1673 break;
1675 case DW_OP_GNU_const_type:
1677 unsigned long o
1678 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1679 size += size_of_uleb128 (o) + 1;
1680 switch (loc->dw_loc_oprnd2.val_class)
1682 case dw_val_class_vec:
1683 size += loc->dw_loc_oprnd2.v.val_vec.length
1684 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1685 break;
1686 case dw_val_class_const:
1687 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1688 break;
1689 case dw_val_class_const_double:
1690 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1691 break;
1692 case dw_val_class_wide_int:
1693 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1694 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1695 break;
1696 default:
1697 gcc_unreachable ();
1699 break;
1701 case DW_OP_GNU_regval_type:
1703 unsigned long o
1704 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1705 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1706 + size_of_uleb128 (o);
1708 break;
1709 case DW_OP_GNU_deref_type:
1711 unsigned long o
1712 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1713 size += 1 + size_of_uleb128 (o);
1715 break;
1716 case DW_OP_GNU_convert:
1717 case DW_OP_GNU_reinterpret:
1718 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1719 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1720 else
1722 unsigned long o
1723 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1724 size += size_of_uleb128 (o);
1726 break;
1727 case DW_OP_GNU_parameter_ref:
1728 size += 4;
1729 break;
1730 default:
1731 break;
1734 return size;
1737 /* Return the size of a series of location descriptors. */
1739 unsigned long
1740 size_of_locs (dw_loc_descr_ref loc)
1742 dw_loc_descr_ref l;
1743 unsigned long size;
1745 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1746 field, to avoid writing to a PCH file. */
1747 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1749 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1750 break;
1751 size += size_of_loc_descr (l);
1753 if (! l)
1754 return size;
1756 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1758 l->dw_loc_addr = size;
1759 size += size_of_loc_descr (l);
1762 return size;
1765 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1766 static void get_ref_die_offset_label (char *, dw_die_ref);
1767 static unsigned long int get_ref_die_offset (dw_die_ref);
1769 /* Output location description stack opcode's operands (if any).
1770 The for_eh_or_skip parameter controls whether register numbers are
1771 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1772 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1773 info). This should be suppressed for the cases that have not been converted
1774 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1776 static void
1777 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1779 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1780 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1782 switch (loc->dw_loc_opc)
1784 #ifdef DWARF2_DEBUGGING_INFO
1785 case DW_OP_const2u:
1786 case DW_OP_const2s:
1787 dw2_asm_output_data (2, val1->v.val_int, NULL);
1788 break;
1789 case DW_OP_const4u:
1790 if (loc->dtprel)
1792 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1793 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1794 val1->v.val_addr);
1795 fputc ('\n', asm_out_file);
1796 break;
1798 /* FALLTHRU */
1799 case DW_OP_const4s:
1800 dw2_asm_output_data (4, val1->v.val_int, NULL);
1801 break;
1802 case DW_OP_const8u:
1803 if (loc->dtprel)
1805 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1806 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1807 val1->v.val_addr);
1808 fputc ('\n', asm_out_file);
1809 break;
1811 /* FALLTHRU */
1812 case DW_OP_const8s:
1813 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1814 dw2_asm_output_data (8, val1->v.val_int, NULL);
1815 break;
1816 case DW_OP_skip:
1817 case DW_OP_bra:
1819 int offset;
1821 gcc_assert (val1->val_class == dw_val_class_loc);
1822 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1824 dw2_asm_output_data (2, offset, NULL);
1826 break;
1827 case DW_OP_implicit_value:
1828 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1829 switch (val2->val_class)
1831 case dw_val_class_const:
1832 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1833 break;
1834 case dw_val_class_vec:
1836 unsigned int elt_size = val2->v.val_vec.elt_size;
1837 unsigned int len = val2->v.val_vec.length;
1838 unsigned int i;
1839 unsigned char *p;
1841 if (elt_size > sizeof (HOST_WIDE_INT))
1843 elt_size /= 2;
1844 len *= 2;
1846 for (i = 0, p = val2->v.val_vec.array;
1847 i < len;
1848 i++, p += elt_size)
1849 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1850 "fp or vector constant word %u", i);
1852 break;
1853 case dw_val_class_const_double:
1855 unsigned HOST_WIDE_INT first, second;
1857 if (WORDS_BIG_ENDIAN)
1859 first = val2->v.val_double.high;
1860 second = val2->v.val_double.low;
1862 else
1864 first = val2->v.val_double.low;
1865 second = val2->v.val_double.high;
1867 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1868 first, NULL);
1869 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1870 second, NULL);
1872 break;
1873 case dw_val_class_wide_int:
1875 int i;
1876 int len = get_full_len (*val2->v.val_wide);
1877 if (WORDS_BIG_ENDIAN)
1878 for (i = len - 1; i >= 0; --i)
1879 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1880 val2->v.val_wide->elt (i), NULL);
1881 else
1882 for (i = 0; i < len; ++i)
1883 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1884 val2->v.val_wide->elt (i), NULL);
1886 break;
1887 case dw_val_class_addr:
1888 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1889 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1890 break;
1891 default:
1892 gcc_unreachable ();
1894 break;
1895 #else
1896 case DW_OP_const2u:
1897 case DW_OP_const2s:
1898 case DW_OP_const4u:
1899 case DW_OP_const4s:
1900 case DW_OP_const8u:
1901 case DW_OP_const8s:
1902 case DW_OP_skip:
1903 case DW_OP_bra:
1904 case DW_OP_implicit_value:
1905 /* We currently don't make any attempt to make sure these are
1906 aligned properly like we do for the main unwind info, so
1907 don't support emitting things larger than a byte if we're
1908 only doing unwinding. */
1909 gcc_unreachable ();
1910 #endif
1911 case DW_OP_const1u:
1912 case DW_OP_const1s:
1913 dw2_asm_output_data (1, val1->v.val_int, NULL);
1914 break;
1915 case DW_OP_constu:
1916 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1917 break;
1918 case DW_OP_consts:
1919 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1920 break;
1921 case DW_OP_pick:
1922 dw2_asm_output_data (1, val1->v.val_int, NULL);
1923 break;
1924 case DW_OP_plus_uconst:
1925 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1926 break;
1927 case DW_OP_breg0:
1928 case DW_OP_breg1:
1929 case DW_OP_breg2:
1930 case DW_OP_breg3:
1931 case DW_OP_breg4:
1932 case DW_OP_breg5:
1933 case DW_OP_breg6:
1934 case DW_OP_breg7:
1935 case DW_OP_breg8:
1936 case DW_OP_breg9:
1937 case DW_OP_breg10:
1938 case DW_OP_breg11:
1939 case DW_OP_breg12:
1940 case DW_OP_breg13:
1941 case DW_OP_breg14:
1942 case DW_OP_breg15:
1943 case DW_OP_breg16:
1944 case DW_OP_breg17:
1945 case DW_OP_breg18:
1946 case DW_OP_breg19:
1947 case DW_OP_breg20:
1948 case DW_OP_breg21:
1949 case DW_OP_breg22:
1950 case DW_OP_breg23:
1951 case DW_OP_breg24:
1952 case DW_OP_breg25:
1953 case DW_OP_breg26:
1954 case DW_OP_breg27:
1955 case DW_OP_breg28:
1956 case DW_OP_breg29:
1957 case DW_OP_breg30:
1958 case DW_OP_breg31:
1959 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1960 break;
1961 case DW_OP_regx:
1963 unsigned r = val1->v.val_unsigned;
1964 if (for_eh_or_skip >= 0)
1965 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1966 gcc_assert (size_of_uleb128 (r)
1967 == size_of_uleb128 (val1->v.val_unsigned));
1968 dw2_asm_output_data_uleb128 (r, NULL);
1970 break;
1971 case DW_OP_fbreg:
1972 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1973 break;
1974 case DW_OP_bregx:
1976 unsigned r = val1->v.val_unsigned;
1977 if (for_eh_or_skip >= 0)
1978 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1979 gcc_assert (size_of_uleb128 (r)
1980 == size_of_uleb128 (val1->v.val_unsigned));
1981 dw2_asm_output_data_uleb128 (r, NULL);
1982 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1984 break;
1985 case DW_OP_piece:
1986 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1987 break;
1988 case DW_OP_bit_piece:
1989 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1990 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1991 break;
1992 case DW_OP_deref_size:
1993 case DW_OP_xderef_size:
1994 dw2_asm_output_data (1, val1->v.val_int, NULL);
1995 break;
1997 case DW_OP_addr:
1998 if (loc->dtprel)
2000 if (targetm.asm_out.output_dwarf_dtprel)
2002 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2003 DWARF2_ADDR_SIZE,
2004 val1->v.val_addr);
2005 fputc ('\n', asm_out_file);
2007 else
2008 gcc_unreachable ();
2010 else
2012 #ifdef DWARF2_DEBUGGING_INFO
2013 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2014 #else
2015 gcc_unreachable ();
2016 #endif
2018 break;
2020 case DW_OP_GNU_addr_index:
2021 case DW_OP_GNU_const_index:
2022 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2023 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2024 "(index into .debug_addr)");
2025 break;
2027 case DW_OP_GNU_implicit_pointer:
2029 char label[MAX_ARTIFICIAL_LABEL_BYTES
2030 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2031 gcc_assert (val1->val_class == dw_val_class_die_ref);
2032 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2033 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2034 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2036 break;
2038 case DW_OP_GNU_entry_value:
2039 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2040 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2041 break;
2043 case DW_OP_GNU_const_type:
2045 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2046 gcc_assert (o);
2047 dw2_asm_output_data_uleb128 (o, NULL);
2048 switch (val2->val_class)
2050 case dw_val_class_const:
2051 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2052 dw2_asm_output_data (1, l, NULL);
2053 dw2_asm_output_data (l, val2->v.val_int, NULL);
2054 break;
2055 case dw_val_class_vec:
2057 unsigned int elt_size = val2->v.val_vec.elt_size;
2058 unsigned int len = val2->v.val_vec.length;
2059 unsigned int i;
2060 unsigned char *p;
2062 l = len * elt_size;
2063 dw2_asm_output_data (1, l, NULL);
2064 if (elt_size > sizeof (HOST_WIDE_INT))
2066 elt_size /= 2;
2067 len *= 2;
2069 for (i = 0, p = val2->v.val_vec.array;
2070 i < len;
2071 i++, p += elt_size)
2072 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2073 "fp or vector constant word %u", i);
2075 break;
2076 case dw_val_class_const_double:
2078 unsigned HOST_WIDE_INT first, second;
2079 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2081 dw2_asm_output_data (1, 2 * l, NULL);
2082 if (WORDS_BIG_ENDIAN)
2084 first = val2->v.val_double.high;
2085 second = val2->v.val_double.low;
2087 else
2089 first = val2->v.val_double.low;
2090 second = val2->v.val_double.high;
2092 dw2_asm_output_data (l, first, NULL);
2093 dw2_asm_output_data (l, second, NULL);
2095 break;
2096 case dw_val_class_wide_int:
2098 int i;
2099 int len = get_full_len (*val2->v.val_wide);
2100 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2102 dw2_asm_output_data (1, len * l, NULL);
2103 if (WORDS_BIG_ENDIAN)
2104 for (i = len - 1; i >= 0; --i)
2105 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2106 else
2107 for (i = 0; i < len; ++i)
2108 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2110 break;
2111 default:
2112 gcc_unreachable ();
2115 break;
2116 case DW_OP_GNU_regval_type:
2118 unsigned r = val1->v.val_unsigned;
2119 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2120 gcc_assert (o);
2121 if (for_eh_or_skip >= 0)
2123 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2124 gcc_assert (size_of_uleb128 (r)
2125 == size_of_uleb128 (val1->v.val_unsigned));
2127 dw2_asm_output_data_uleb128 (r, NULL);
2128 dw2_asm_output_data_uleb128 (o, NULL);
2130 break;
2131 case DW_OP_GNU_deref_type:
2133 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2134 gcc_assert (o);
2135 dw2_asm_output_data (1, val1->v.val_int, NULL);
2136 dw2_asm_output_data_uleb128 (o, NULL);
2138 break;
2139 case DW_OP_GNU_convert:
2140 case DW_OP_GNU_reinterpret:
2141 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2142 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2143 else
2145 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2146 gcc_assert (o);
2147 dw2_asm_output_data_uleb128 (o, NULL);
2149 break;
2151 case DW_OP_GNU_parameter_ref:
2153 unsigned long o;
2154 gcc_assert (val1->val_class == dw_val_class_die_ref);
2155 o = get_ref_die_offset (val1->v.val_die_ref.die);
2156 dw2_asm_output_data (4, o, NULL);
2158 break;
2160 default:
2161 /* Other codes have no operands. */
2162 break;
2166 /* Output a sequence of location operations.
2167 The for_eh_or_skip parameter controls whether register numbers are
2168 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2169 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2170 info). This should be suppressed for the cases that have not been converted
2171 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2173 void
2174 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2176 for (; loc != NULL; loc = loc->dw_loc_next)
2178 enum dwarf_location_atom opc = loc->dw_loc_opc;
2179 /* Output the opcode. */
2180 if (for_eh_or_skip >= 0
2181 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2183 unsigned r = (opc - DW_OP_breg0);
2184 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2185 gcc_assert (r <= 31);
2186 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2188 else if (for_eh_or_skip >= 0
2189 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2191 unsigned r = (opc - DW_OP_reg0);
2192 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2193 gcc_assert (r <= 31);
2194 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2197 dw2_asm_output_data (1, opc,
2198 "%s", dwarf_stack_op_name (opc));
2200 /* Output the operand(s) (if any). */
2201 output_loc_operands (loc, for_eh_or_skip);
2205 /* Output location description stack opcode's operands (if any).
2206 The output is single bytes on a line, suitable for .cfi_escape. */
2208 static void
2209 output_loc_operands_raw (dw_loc_descr_ref loc)
2211 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2212 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2214 switch (loc->dw_loc_opc)
2216 case DW_OP_addr:
2217 case DW_OP_GNU_addr_index:
2218 case DW_OP_GNU_const_index:
2219 case DW_OP_implicit_value:
2220 /* We cannot output addresses in .cfi_escape, only bytes. */
2221 gcc_unreachable ();
2223 case DW_OP_const1u:
2224 case DW_OP_const1s:
2225 case DW_OP_pick:
2226 case DW_OP_deref_size:
2227 case DW_OP_xderef_size:
2228 fputc (',', asm_out_file);
2229 dw2_asm_output_data_raw (1, val1->v.val_int);
2230 break;
2232 case DW_OP_const2u:
2233 case DW_OP_const2s:
2234 fputc (',', asm_out_file);
2235 dw2_asm_output_data_raw (2, val1->v.val_int);
2236 break;
2238 case DW_OP_const4u:
2239 case DW_OP_const4s:
2240 fputc (',', asm_out_file);
2241 dw2_asm_output_data_raw (4, val1->v.val_int);
2242 break;
2244 case DW_OP_const8u:
2245 case DW_OP_const8s:
2246 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2247 fputc (',', asm_out_file);
2248 dw2_asm_output_data_raw (8, val1->v.val_int);
2249 break;
2251 case DW_OP_skip:
2252 case DW_OP_bra:
2254 int offset;
2256 gcc_assert (val1->val_class == dw_val_class_loc);
2257 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2259 fputc (',', asm_out_file);
2260 dw2_asm_output_data_raw (2, offset);
2262 break;
2264 case DW_OP_regx:
2266 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2267 gcc_assert (size_of_uleb128 (r)
2268 == size_of_uleb128 (val1->v.val_unsigned));
2269 fputc (',', asm_out_file);
2270 dw2_asm_output_data_uleb128_raw (r);
2272 break;
2274 case DW_OP_constu:
2275 case DW_OP_plus_uconst:
2276 case DW_OP_piece:
2277 fputc (',', asm_out_file);
2278 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2279 break;
2281 case DW_OP_bit_piece:
2282 fputc (',', asm_out_file);
2283 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2284 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2285 break;
2287 case DW_OP_consts:
2288 case DW_OP_breg0:
2289 case DW_OP_breg1:
2290 case DW_OP_breg2:
2291 case DW_OP_breg3:
2292 case DW_OP_breg4:
2293 case DW_OP_breg5:
2294 case DW_OP_breg6:
2295 case DW_OP_breg7:
2296 case DW_OP_breg8:
2297 case DW_OP_breg9:
2298 case DW_OP_breg10:
2299 case DW_OP_breg11:
2300 case DW_OP_breg12:
2301 case DW_OP_breg13:
2302 case DW_OP_breg14:
2303 case DW_OP_breg15:
2304 case DW_OP_breg16:
2305 case DW_OP_breg17:
2306 case DW_OP_breg18:
2307 case DW_OP_breg19:
2308 case DW_OP_breg20:
2309 case DW_OP_breg21:
2310 case DW_OP_breg22:
2311 case DW_OP_breg23:
2312 case DW_OP_breg24:
2313 case DW_OP_breg25:
2314 case DW_OP_breg26:
2315 case DW_OP_breg27:
2316 case DW_OP_breg28:
2317 case DW_OP_breg29:
2318 case DW_OP_breg30:
2319 case DW_OP_breg31:
2320 case DW_OP_fbreg:
2321 fputc (',', asm_out_file);
2322 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2323 break;
2325 case DW_OP_bregx:
2327 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2328 gcc_assert (size_of_uleb128 (r)
2329 == size_of_uleb128 (val1->v.val_unsigned));
2330 fputc (',', asm_out_file);
2331 dw2_asm_output_data_uleb128_raw (r);
2332 fputc (',', asm_out_file);
2333 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2335 break;
2337 case DW_OP_GNU_implicit_pointer:
2338 case DW_OP_GNU_entry_value:
2339 case DW_OP_GNU_const_type:
2340 case DW_OP_GNU_regval_type:
2341 case DW_OP_GNU_deref_type:
2342 case DW_OP_GNU_convert:
2343 case DW_OP_GNU_reinterpret:
2344 case DW_OP_GNU_parameter_ref:
2345 gcc_unreachable ();
2346 break;
2348 default:
2349 /* Other codes have no operands. */
2350 break;
2354 void
2355 output_loc_sequence_raw (dw_loc_descr_ref loc)
2357 while (1)
2359 enum dwarf_location_atom opc = loc->dw_loc_opc;
2360 /* Output the opcode. */
2361 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2363 unsigned r = (opc - DW_OP_breg0);
2364 r = DWARF2_FRAME_REG_OUT (r, 1);
2365 gcc_assert (r <= 31);
2366 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2368 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2370 unsigned r = (opc - DW_OP_reg0);
2371 r = DWARF2_FRAME_REG_OUT (r, 1);
2372 gcc_assert (r <= 31);
2373 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2375 /* Output the opcode. */
2376 fprintf (asm_out_file, "%#x", opc);
2377 output_loc_operands_raw (loc);
2379 if (!loc->dw_loc_next)
2380 break;
2381 loc = loc->dw_loc_next;
2383 fputc (',', asm_out_file);
2387 /* This function builds a dwarf location descriptor sequence from a
2388 dw_cfa_location, adding the given OFFSET to the result of the
2389 expression. */
2391 struct dw_loc_descr_node *
2392 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2394 struct dw_loc_descr_node *head, *tmp;
2396 offset += cfa->offset;
2398 if (cfa->indirect)
2400 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2401 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2402 head->dw_loc_oprnd1.val_entry = NULL;
2403 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2404 add_loc_descr (&head, tmp);
2405 if (offset != 0)
2407 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2408 add_loc_descr (&head, tmp);
2411 else
2412 head = new_reg_loc_descr (cfa->reg, offset);
2414 return head;
2417 /* This function builds a dwarf location descriptor sequence for
2418 the address at OFFSET from the CFA when stack is aligned to
2419 ALIGNMENT byte. */
2421 struct dw_loc_descr_node *
2422 build_cfa_aligned_loc (dw_cfa_location *cfa,
2423 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2425 struct dw_loc_descr_node *head;
2426 unsigned int dwarf_fp
2427 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2429 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2430 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2432 head = new_reg_loc_descr (dwarf_fp, 0);
2433 add_loc_descr (&head, int_loc_descriptor (alignment));
2434 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2435 loc_descr_plus_const (&head, offset);
2437 else
2438 head = new_reg_loc_descr (dwarf_fp, offset);
2439 return head;
2442 /* And now, the support for symbolic debugging information. */
2444 /* .debug_str support. */
2446 static void dwarf2out_init (const char *);
2447 static void dwarf2out_finish (const char *);
2448 static void dwarf2out_assembly_start (void);
2449 static void dwarf2out_define (unsigned int, const char *);
2450 static void dwarf2out_undef (unsigned int, const char *);
2451 static void dwarf2out_start_source_file (unsigned, const char *);
2452 static void dwarf2out_end_source_file (unsigned);
2453 static void dwarf2out_function_decl (tree);
2454 static void dwarf2out_begin_block (unsigned, unsigned);
2455 static void dwarf2out_end_block (unsigned, unsigned);
2456 static bool dwarf2out_ignore_block (const_tree);
2457 static void dwarf2out_global_decl (tree);
2458 static void dwarf2out_type_decl (tree, int);
2459 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2460 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2461 dw_die_ref);
2462 static void dwarf2out_abstract_function (tree);
2463 static void dwarf2out_var_location (rtx_insn *);
2464 static void dwarf2out_begin_function (tree);
2465 static void dwarf2out_end_function (unsigned int);
2466 static void dwarf2out_set_name (tree, tree);
2468 /* The debug hooks structure. */
2470 const struct gcc_debug_hooks dwarf2_debug_hooks =
2472 dwarf2out_init,
2473 dwarf2out_finish,
2474 dwarf2out_assembly_start,
2475 dwarf2out_define,
2476 dwarf2out_undef,
2477 dwarf2out_start_source_file,
2478 dwarf2out_end_source_file,
2479 dwarf2out_begin_block,
2480 dwarf2out_end_block,
2481 dwarf2out_ignore_block,
2482 dwarf2out_source_line,
2483 dwarf2out_begin_prologue,
2484 #if VMS_DEBUGGING_INFO
2485 dwarf2out_vms_end_prologue,
2486 dwarf2out_vms_begin_epilogue,
2487 #else
2488 debug_nothing_int_charstar,
2489 debug_nothing_int_charstar,
2490 #endif
2491 dwarf2out_end_epilogue,
2492 dwarf2out_begin_function,
2493 dwarf2out_end_function, /* end_function */
2494 dwarf2out_function_decl, /* function_decl */
2495 dwarf2out_global_decl,
2496 dwarf2out_type_decl, /* type_decl */
2497 dwarf2out_imported_module_or_decl,
2498 debug_nothing_tree, /* deferred_inline_function */
2499 /* The DWARF 2 backend tries to reduce debugging bloat by not
2500 emitting the abstract description of inline functions until
2501 something tries to reference them. */
2502 dwarf2out_abstract_function, /* outlining_inline_function */
2503 debug_nothing_rtx_code_label, /* label */
2504 debug_nothing_int, /* handle_pch */
2505 dwarf2out_var_location,
2506 dwarf2out_switch_text_section,
2507 dwarf2out_set_name,
2508 1, /* start_end_main_source_file */
2509 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2512 /* NOTE: In the comments in this file, many references are made to
2513 "Debugging Information Entries". This term is abbreviated as `DIE'
2514 throughout the remainder of this file. */
2516 /* An internal representation of the DWARF output is built, and then
2517 walked to generate the DWARF debugging info. The walk of the internal
2518 representation is done after the entire program has been compiled.
2519 The types below are used to describe the internal representation. */
2521 /* Whether to put type DIEs into their own section .debug_types instead
2522 of making them part of the .debug_info section. Only supported for
2523 Dwarf V4 or higher and the user didn't disable them through
2524 -fno-debug-types-section. It is more efficient to put them in a
2525 separate comdat sections since the linker will then be able to
2526 remove duplicates. But not all tools support .debug_types sections
2527 yet. */
2529 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2531 /* Various DIE's use offsets relative to the beginning of the
2532 .debug_info section to refer to each other. */
2534 typedef long int dw_offset;
2536 /* Define typedefs here to avoid circular dependencies. */
2538 typedef struct dw_attr_struct *dw_attr_ref;
2539 typedef struct dw_line_info_struct *dw_line_info_ref;
2540 typedef struct pubname_struct *pubname_ref;
2541 typedef struct dw_ranges_struct *dw_ranges_ref;
2542 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2543 typedef struct comdat_type_struct *comdat_type_node_ref;
2545 /* The entries in the line_info table more-or-less mirror the opcodes
2546 that are used in the real dwarf line table. Arrays of these entries
2547 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2548 supported. */
2550 enum dw_line_info_opcode {
2551 /* Emit DW_LNE_set_address; the operand is the label index. */
2552 LI_set_address,
2554 /* Emit a row to the matrix with the given line. This may be done
2555 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2556 special opcodes. */
2557 LI_set_line,
2559 /* Emit a DW_LNS_set_file. */
2560 LI_set_file,
2562 /* Emit a DW_LNS_set_column. */
2563 LI_set_column,
2565 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2566 LI_negate_stmt,
2568 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2569 LI_set_prologue_end,
2570 LI_set_epilogue_begin,
2572 /* Emit a DW_LNE_set_discriminator. */
2573 LI_set_discriminator
2576 typedef struct GTY(()) dw_line_info_struct {
2577 enum dw_line_info_opcode opcode;
2578 unsigned int val;
2579 } dw_line_info_entry;
2582 typedef struct GTY(()) dw_line_info_table_struct {
2583 /* The label that marks the end of this section. */
2584 const char *end_label;
2586 /* The values for the last row of the matrix, as collected in the table.
2587 These are used to minimize the changes to the next row. */
2588 unsigned int file_num;
2589 unsigned int line_num;
2590 unsigned int column_num;
2591 int discrim_num;
2592 bool is_stmt;
2593 bool in_use;
2595 vec<dw_line_info_entry, va_gc> *entries;
2596 } dw_line_info_table;
2598 typedef dw_line_info_table *dw_line_info_table_p;
2601 /* Each DIE attribute has a field specifying the attribute kind,
2602 a link to the next attribute in the chain, and an attribute value.
2603 Attributes are typically linked below the DIE they modify. */
2605 typedef struct GTY(()) dw_attr_struct {
2606 enum dwarf_attribute dw_attr;
2607 dw_val_node dw_attr_val;
2609 dw_attr_node;
2612 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2613 The children of each node form a circular list linked by
2614 die_sib. die_child points to the node *before* the "first" child node. */
2616 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2617 union die_symbol_or_type_node
2619 const char * GTY ((tag ("0"))) die_symbol;
2620 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2622 GTY ((desc ("%0.comdat_type_p"))) die_id;
2623 vec<dw_attr_node, va_gc> *die_attr;
2624 dw_die_ref die_parent;
2625 dw_die_ref die_child;
2626 dw_die_ref die_sib;
2627 dw_die_ref die_definition; /* ref from a specification to its definition */
2628 dw_offset die_offset;
2629 unsigned long die_abbrev;
2630 int die_mark;
2631 unsigned int decl_id;
2632 enum dwarf_tag die_tag;
2633 /* Die is used and must not be pruned as unused. */
2634 BOOL_BITFIELD die_perennial_p : 1;
2635 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2636 /* Lots of spare bits. */
2638 die_node;
2640 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2641 #define FOR_EACH_CHILD(die, c, expr) do { \
2642 c = die->die_child; \
2643 if (c) do { \
2644 c = c->die_sib; \
2645 expr; \
2646 } while (c != die->die_child); \
2647 } while (0)
2649 /* The pubname structure */
2651 typedef struct GTY(()) pubname_struct {
2652 dw_die_ref die;
2653 const char *name;
2655 pubname_entry;
2658 struct GTY(()) dw_ranges_struct {
2659 /* If this is positive, it's a block number, otherwise it's a
2660 bitwise-negated index into dw_ranges_by_label. */
2661 int num;
2664 /* A structure to hold a macinfo entry. */
2666 typedef struct GTY(()) macinfo_struct {
2667 unsigned char code;
2668 unsigned HOST_WIDE_INT lineno;
2669 const char *info;
2671 macinfo_entry;
2674 struct GTY(()) dw_ranges_by_label_struct {
2675 const char *begin;
2676 const char *end;
2679 /* The comdat type node structure. */
2680 typedef struct GTY(()) comdat_type_struct
2682 dw_die_ref root_die;
2683 dw_die_ref type_die;
2684 dw_die_ref skeleton_die;
2685 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2686 struct comdat_type_struct *next;
2688 comdat_type_node;
2690 /* The limbo die list structure. */
2691 typedef struct GTY(()) limbo_die_struct {
2692 dw_die_ref die;
2693 tree created_for;
2694 struct limbo_die_struct *next;
2696 limbo_die_node;
2698 typedef struct skeleton_chain_struct
2700 dw_die_ref old_die;
2701 dw_die_ref new_die;
2702 struct skeleton_chain_struct *parent;
2704 skeleton_chain_node;
2706 /* Define a macro which returns nonzero for a TYPE_DECL which was
2707 implicitly generated for a type.
2709 Note that, unlike the C front-end (which generates a NULL named
2710 TYPE_DECL node for each complete tagged type, each array type,
2711 and each function type node created) the C++ front-end generates
2712 a _named_ TYPE_DECL node for each tagged type node created.
2713 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2714 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2715 front-end, but for each type, tagged or not. */
2717 #define TYPE_DECL_IS_STUB(decl) \
2718 (DECL_NAME (decl) == NULL_TREE \
2719 || (DECL_ARTIFICIAL (decl) \
2720 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2721 /* This is necessary for stub decls that \
2722 appear in nested inline functions. */ \
2723 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2724 && (decl_ultimate_origin (decl) \
2725 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2727 /* Information concerning the compilation unit's programming
2728 language, and compiler version. */
2730 /* Fixed size portion of the DWARF compilation unit header. */
2731 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2732 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2734 /* Fixed size portion of the DWARF comdat type unit header. */
2735 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2736 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2737 + DWARF_OFFSET_SIZE)
2739 /* Fixed size portion of public names info. */
2740 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2742 /* Fixed size portion of the address range info. */
2743 #define DWARF_ARANGES_HEADER_SIZE \
2744 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2745 DWARF2_ADDR_SIZE * 2) \
2746 - DWARF_INITIAL_LENGTH_SIZE)
2748 /* Size of padding portion in the address range info. It must be
2749 aligned to twice the pointer size. */
2750 #define DWARF_ARANGES_PAD_SIZE \
2751 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2752 DWARF2_ADDR_SIZE * 2) \
2753 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2755 /* Use assembler line directives if available. */
2756 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2757 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2758 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2759 #else
2760 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2761 #endif
2762 #endif
2764 /* Minimum line offset in a special line info. opcode.
2765 This value was chosen to give a reasonable range of values. */
2766 #define DWARF_LINE_BASE -10
2768 /* First special line opcode - leave room for the standard opcodes. */
2769 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2771 /* Range of line offsets in a special line info. opcode. */
2772 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2774 /* Flag that indicates the initial value of the is_stmt_start flag.
2775 In the present implementation, we do not mark any lines as
2776 the beginning of a source statement, because that information
2777 is not made available by the GCC front-end. */
2778 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2780 /* Maximum number of operations per instruction bundle. */
2781 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2782 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2783 #endif
2785 /* This location is used by calc_die_sizes() to keep track
2786 the offset of each DIE within the .debug_info section. */
2787 static unsigned long next_die_offset;
2789 /* Record the root of the DIE's built for the current compilation unit. */
2790 static GTY(()) dw_die_ref single_comp_unit_die;
2792 /* A list of type DIEs that have been separated into comdat sections. */
2793 static GTY(()) comdat_type_node *comdat_type_list;
2795 /* A list of DIEs with a NULL parent waiting to be relocated. */
2796 static GTY(()) limbo_die_node *limbo_die_list;
2798 /* A list of DIEs for which we may have to generate
2799 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2800 static GTY(()) limbo_die_node *deferred_asm_name;
2802 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2804 typedef const char *compare_type;
2806 static hashval_t hash (dwarf_file_data *);
2807 static bool equal (dwarf_file_data *, const char *);
2810 /* Filenames referenced by this compilation unit. */
2811 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2813 struct decl_die_hasher : ggc_hasher<die_node *>
2815 typedef tree compare_type;
2817 static hashval_t hash (die_node *);
2818 static bool equal (die_node *, tree);
2820 /* A hash table of references to DIE's that describe declarations.
2821 The key is a DECL_UID() which is a unique number identifying each decl. */
2822 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2824 struct block_die_hasher : ggc_hasher<die_struct *>
2826 static hashval_t hash (die_struct *);
2827 static bool equal (die_struct *, die_struct *);
2830 /* A hash table of references to DIE's that describe COMMON blocks.
2831 The key is DECL_UID() ^ die_parent. */
2832 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2834 typedef struct GTY(()) die_arg_entry_struct {
2835 dw_die_ref die;
2836 tree arg;
2837 } die_arg_entry;
2840 /* Node of the variable location list. */
2841 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2842 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2843 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2844 in mode of the EXPR_LIST node and first EXPR_LIST operand
2845 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2846 location or NULL for padding. For larger bitsizes,
2847 mode is 0 and first operand is a CONCAT with bitsize
2848 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2849 NULL as second operand. */
2850 rtx GTY (()) loc;
2851 const char * GTY (()) label;
2852 struct var_loc_node * GTY (()) next;
2855 /* Variable location list. */
2856 struct GTY ((for_user)) var_loc_list_def {
2857 struct var_loc_node * GTY (()) first;
2859 /* Pointer to the last but one or last element of the
2860 chained list. If the list is empty, both first and
2861 last are NULL, if the list contains just one node
2862 or the last node certainly is not redundant, it points
2863 to the last node, otherwise points to the last but one.
2864 Do not mark it for GC because it is marked through the chain. */
2865 struct var_loc_node * GTY ((skip ("%h"))) last;
2867 /* Pointer to the last element before section switch,
2868 if NULL, either sections weren't switched or first
2869 is after section switch. */
2870 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2872 /* DECL_UID of the variable decl. */
2873 unsigned int decl_id;
2875 typedef struct var_loc_list_def var_loc_list;
2877 /* Call argument location list. */
2878 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2879 rtx GTY (()) call_arg_loc_note;
2880 const char * GTY (()) label;
2881 tree GTY (()) block;
2882 bool tail_call_p;
2883 rtx GTY (()) symbol_ref;
2884 struct call_arg_loc_node * GTY (()) next;
2888 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2890 typedef const_tree compare_type;
2892 static hashval_t hash (var_loc_list *);
2893 static bool equal (var_loc_list *, const_tree);
2896 /* Table of decl location linked lists. */
2897 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2899 /* Head and tail of call_arg_loc chain. */
2900 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2901 static struct call_arg_loc_node *call_arg_loc_last;
2903 /* Number of call sites in the current function. */
2904 static int call_site_count = -1;
2905 /* Number of tail call sites in the current function. */
2906 static int tail_call_site_count = -1;
2908 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2909 DIEs. */
2910 static vec<dw_die_ref> block_map;
2912 /* A cached location list. */
2913 struct GTY ((for_user)) cached_dw_loc_list_def {
2914 /* The DECL_UID of the decl that this entry describes. */
2915 unsigned int decl_id;
2917 /* The cached location list. */
2918 dw_loc_list_ref loc_list;
2920 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2922 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2925 typedef const_tree compare_type;
2927 static hashval_t hash (cached_dw_loc_list *);
2928 static bool equal (cached_dw_loc_list *, const_tree);
2931 /* Table of cached location lists. */
2932 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2934 /* A pointer to the base of a list of references to DIE's that
2935 are uniquely identified by their tag, presence/absence of
2936 children DIE's, and list of attribute/value pairs. */
2937 static GTY((length ("abbrev_die_table_allocated")))
2938 dw_die_ref *abbrev_die_table;
2940 /* Number of elements currently allocated for abbrev_die_table. */
2941 static GTY(()) unsigned abbrev_die_table_allocated;
2943 /* Number of elements in type_die_table currently in use. */
2944 static GTY(()) unsigned abbrev_die_table_in_use;
2946 /* Size (in elements) of increments by which we may expand the
2947 abbrev_die_table. */
2948 #define ABBREV_DIE_TABLE_INCREMENT 256
2950 /* A global counter for generating labels for line number data. */
2951 static unsigned int line_info_label_num;
2953 /* The current table to which we should emit line number information
2954 for the current function. This will be set up at the beginning of
2955 assembly for the function. */
2956 static dw_line_info_table *cur_line_info_table;
2958 /* The two default tables of line number info. */
2959 static GTY(()) dw_line_info_table *text_section_line_info;
2960 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2962 /* The set of all non-default tables of line number info. */
2963 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2965 /* A flag to tell pubnames/types export if there is an info section to
2966 refer to. */
2967 static bool info_section_emitted;
2969 /* A pointer to the base of a table that contains a list of publicly
2970 accessible names. */
2971 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2973 /* A pointer to the base of a table that contains a list of publicly
2974 accessible types. */
2975 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2977 /* A pointer to the base of a table that contains a list of macro
2978 defines/undefines (and file start/end markers). */
2979 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2981 /* True if .debug_macinfo or .debug_macros section is going to be
2982 emitted. */
2983 #define have_macinfo \
2984 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2985 && !macinfo_table->is_empty ())
2987 /* Array of dies for which we should generate .debug_ranges info. */
2988 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2990 /* Number of elements currently allocated for ranges_table. */
2991 static GTY(()) unsigned ranges_table_allocated;
2993 /* Number of elements in ranges_table currently in use. */
2994 static GTY(()) unsigned ranges_table_in_use;
2996 /* Array of pairs of labels referenced in ranges_table. */
2997 static GTY ((length ("ranges_by_label_allocated")))
2998 dw_ranges_by_label_ref ranges_by_label;
3000 /* Number of elements currently allocated for ranges_by_label. */
3001 static GTY(()) unsigned ranges_by_label_allocated;
3003 /* Number of elements in ranges_by_label currently in use. */
3004 static GTY(()) unsigned ranges_by_label_in_use;
3006 /* Size (in elements) of increments by which we may expand the
3007 ranges_table. */
3008 #define RANGES_TABLE_INCREMENT 64
3010 /* Whether we have location lists that need outputting */
3011 static GTY(()) bool have_location_lists;
3013 /* Unique label counter. */
3014 static GTY(()) unsigned int loclabel_num;
3016 /* Unique label counter for point-of-call tables. */
3017 static GTY(()) unsigned int poc_label_num;
3019 /* The last file entry emitted by maybe_emit_file(). */
3020 static GTY(()) struct dwarf_file_data * last_emitted_file;
3022 /* Number of internal labels generated by gen_internal_sym(). */
3023 static GTY(()) int label_num;
3025 /* Cached result of previous call to lookup_filename. */
3026 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3028 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3030 /* Instances of generic types for which we need to generate debug
3031 info that describe their generic parameters and arguments. That
3032 generation needs to happen once all types are properly laid out so
3033 we do it at the end of compilation. */
3034 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3036 /* Offset from the "steady-state frame pointer" to the frame base,
3037 within the current function. */
3038 static HOST_WIDE_INT frame_pointer_fb_offset;
3039 static bool frame_pointer_fb_offset_valid;
3041 static vec<dw_die_ref> base_types;
3043 /* Flags to represent a set of attribute classes for attributes that represent
3044 a scalar value (bounds, pointers, ...). */
3045 enum dw_scalar_form
3047 dw_scalar_form_constant = 0x01,
3048 dw_scalar_form_exprloc = 0x02,
3049 dw_scalar_form_reference = 0x04
3052 /* Forward declarations for functions defined in this file. */
3054 static int is_pseudo_reg (const_rtx);
3055 static tree type_main_variant (tree);
3056 static int is_tagged_type (const_tree);
3057 static const char *dwarf_tag_name (unsigned);
3058 static const char *dwarf_attr_name (unsigned);
3059 static const char *dwarf_form_name (unsigned);
3060 static tree decl_ultimate_origin (const_tree);
3061 static tree decl_class_context (tree);
3062 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3063 static inline enum dw_val_class AT_class (dw_attr_ref);
3064 static inline unsigned int AT_index (dw_attr_ref);
3065 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3066 static inline unsigned AT_flag (dw_attr_ref);
3067 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3068 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3069 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3070 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3071 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3072 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3073 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3074 unsigned int, unsigned char *);
3075 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3076 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3077 static inline const char *AT_string (dw_attr_ref);
3078 static enum dwarf_form AT_string_form (dw_attr_ref);
3079 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3080 static void add_AT_specification (dw_die_ref, dw_die_ref);
3081 static inline dw_die_ref AT_ref (dw_attr_ref);
3082 static inline int AT_ref_external (dw_attr_ref);
3083 static inline void set_AT_ref_external (dw_attr_ref, int);
3084 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3085 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3086 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3087 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3088 dw_loc_list_ref);
3089 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3090 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3091 static void remove_addr_table_entry (addr_table_entry *);
3092 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3093 static inline rtx AT_addr (dw_attr_ref);
3094 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3095 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3096 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3097 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3098 unsigned HOST_WIDE_INT);
3099 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3100 unsigned long, bool);
3101 static inline const char *AT_lbl (dw_attr_ref);
3102 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3103 static const char *get_AT_low_pc (dw_die_ref);
3104 static const char *get_AT_hi_pc (dw_die_ref);
3105 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3106 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3107 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3108 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3109 static bool is_cxx (void);
3110 static bool is_fortran (void);
3111 static bool is_ada (void);
3112 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3113 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3114 static void add_child_die (dw_die_ref, dw_die_ref);
3115 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3116 static dw_die_ref lookup_type_die (tree);
3117 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3118 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3119 static void equate_type_number_to_die (tree, dw_die_ref);
3120 static dw_die_ref lookup_decl_die (tree);
3121 static var_loc_list *lookup_decl_loc (const_tree);
3122 static void equate_decl_number_to_die (tree, dw_die_ref);
3123 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3124 static void print_spaces (FILE *);
3125 static void print_die (dw_die_ref, FILE *);
3126 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3127 static dw_die_ref pop_compile_unit (dw_die_ref);
3128 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3129 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3130 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3131 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3132 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3133 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3134 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3135 struct md5_ctx *, int *);
3136 struct checksum_attributes;
3137 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3138 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3139 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3140 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3141 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3142 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3143 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3144 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3145 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3146 static void compute_section_prefix (dw_die_ref);
3147 static int is_type_die (dw_die_ref);
3148 static int is_comdat_die (dw_die_ref);
3149 static int is_symbol_die (dw_die_ref);
3150 static inline bool is_template_instantiation (dw_die_ref);
3151 static void assign_symbol_names (dw_die_ref);
3152 static void break_out_includes (dw_die_ref);
3153 static int is_declaration_die (dw_die_ref);
3154 static int should_move_die_to_comdat (dw_die_ref);
3155 static dw_die_ref clone_as_declaration (dw_die_ref);
3156 static dw_die_ref clone_die (dw_die_ref);
3157 static dw_die_ref clone_tree (dw_die_ref);
3158 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3159 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3160 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3161 static dw_die_ref generate_skeleton (dw_die_ref);
3162 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3163 dw_die_ref,
3164 dw_die_ref);
3165 static void break_out_comdat_types (dw_die_ref);
3166 static void copy_decls_for_unworthy_types (dw_die_ref);
3168 static void add_sibling_attributes (dw_die_ref);
3169 static void output_location_lists (dw_die_ref);
3170 static int constant_size (unsigned HOST_WIDE_INT);
3171 static unsigned long size_of_die (dw_die_ref);
3172 static void calc_die_sizes (dw_die_ref);
3173 static void calc_base_type_die_sizes (void);
3174 static void mark_dies (dw_die_ref);
3175 static void unmark_dies (dw_die_ref);
3176 static void unmark_all_dies (dw_die_ref);
3177 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3178 static unsigned long size_of_aranges (void);
3179 static enum dwarf_form value_format (dw_attr_ref);
3180 static void output_value_format (dw_attr_ref);
3181 static void output_abbrev_section (void);
3182 static void output_die_abbrevs (unsigned long, dw_die_ref);
3183 static void output_die_symbol (dw_die_ref);
3184 static void output_die (dw_die_ref);
3185 static void output_compilation_unit_header (void);
3186 static void output_comp_unit (dw_die_ref, int);
3187 static void output_comdat_type_unit (comdat_type_node *);
3188 static const char *dwarf2_name (tree, int);
3189 static void add_pubname (tree, dw_die_ref);
3190 static void add_enumerator_pubname (const char *, dw_die_ref);
3191 static void add_pubname_string (const char *, dw_die_ref);
3192 static void add_pubtype (tree, dw_die_ref);
3193 static void output_pubnames (vec<pubname_entry, va_gc> *);
3194 static void output_aranges (unsigned long);
3195 static unsigned int add_ranges_num (int);
3196 static unsigned int add_ranges (const_tree);
3197 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3198 bool *, bool);
3199 static void output_ranges (void);
3200 static dw_line_info_table *new_line_info_table (void);
3201 static void output_line_info (bool);
3202 static void output_file_names (void);
3203 static dw_die_ref base_type_die (tree);
3204 static int is_base_type (tree);
3205 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3206 static int decl_quals (const_tree);
3207 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3208 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3209 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3210 static int type_is_enum (const_tree);
3211 static unsigned int dbx_reg_number (const_rtx);
3212 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3213 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3214 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3215 enum var_init_status);
3216 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3217 enum var_init_status);
3218 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3219 enum var_init_status);
3220 static int is_based_loc (const_rtx);
3221 static bool resolve_one_addr (rtx *);
3222 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3223 enum var_init_status);
3224 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3225 enum var_init_status);
3226 struct loc_descr_context;
3227 static dw_loc_list_ref loc_list_from_tree (tree, int,
3228 const struct loc_descr_context *);
3229 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3230 const struct loc_descr_context *);
3231 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3232 static tree field_type (const_tree);
3233 static unsigned int simple_type_align_in_bits (const_tree);
3234 static unsigned int simple_decl_align_in_bits (const_tree);
3235 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3236 static HOST_WIDE_INT field_byte_offset (const_tree);
3237 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3238 dw_loc_list_ref);
3239 static void add_data_member_location_attribute (dw_die_ref, tree);
3240 static bool add_const_value_attribute (dw_die_ref, rtx);
3241 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3242 static void insert_wide_int (const wide_int &, unsigned char *, int);
3243 static void insert_float (const_rtx, unsigned char *);
3244 static rtx rtl_for_decl_location (tree);
3245 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3246 enum dwarf_attribute);
3247 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3248 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3249 static void add_name_attribute (dw_die_ref, const char *);
3250 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3251 static void add_comp_dir_attribute (dw_die_ref);
3252 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3253 const struct loc_descr_context *);
3254 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3255 const struct loc_descr_context *);
3256 static void add_subscript_info (dw_die_ref, tree, bool);
3257 static void add_byte_size_attribute (dw_die_ref, tree);
3258 static void add_bit_offset_attribute (dw_die_ref, tree);
3259 static void add_bit_size_attribute (dw_die_ref, tree);
3260 static void add_prototyped_attribute (dw_die_ref, tree);
3261 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3262 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3263 static void add_src_coords_attributes (dw_die_ref, tree);
3264 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3265 static void push_decl_scope (tree);
3266 static void pop_decl_scope (void);
3267 static dw_die_ref scope_die_for (tree, dw_die_ref);
3268 static inline int local_scope_p (dw_die_ref);
3269 static inline int class_scope_p (dw_die_ref);
3270 static inline int class_or_namespace_scope_p (dw_die_ref);
3271 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3272 static void add_calling_convention_attribute (dw_die_ref, tree);
3273 static const char *type_tag (const_tree);
3274 static tree member_declared_type (const_tree);
3275 #if 0
3276 static const char *decl_start_label (tree);
3277 #endif
3278 static void gen_array_type_die (tree, dw_die_ref);
3279 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3280 #if 0
3281 static void gen_entry_point_die (tree, dw_die_ref);
3282 #endif
3283 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3284 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3285 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3286 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3287 static void gen_formal_types_die (tree, dw_die_ref);
3288 static void gen_subprogram_die (tree, dw_die_ref);
3289 static void gen_variable_die (tree, tree, dw_die_ref);
3290 static void gen_const_die (tree, dw_die_ref);
3291 static void gen_label_die (tree, dw_die_ref);
3292 static void gen_lexical_block_die (tree, dw_die_ref);
3293 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3294 static void gen_field_die (tree, dw_die_ref);
3295 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3296 static dw_die_ref gen_compile_unit_die (const char *);
3297 static void gen_inheritance_die (tree, tree, dw_die_ref);
3298 static void gen_member_die (tree, dw_die_ref);
3299 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3300 enum debug_info_usage);
3301 static void gen_subroutine_type_die (tree, dw_die_ref);
3302 static void gen_typedef_die (tree, dw_die_ref);
3303 static void gen_type_die (tree, dw_die_ref);
3304 static void gen_block_die (tree, dw_die_ref);
3305 static void decls_for_scope (tree, dw_die_ref);
3306 static inline int is_redundant_typedef (const_tree);
3307 static bool is_naming_typedef_decl (const_tree);
3308 static inline dw_die_ref get_context_die (tree);
3309 static void gen_namespace_die (tree, dw_die_ref);
3310 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3311 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3312 static dw_die_ref force_decl_die (tree);
3313 static dw_die_ref force_type_die (tree);
3314 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3315 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3316 static struct dwarf_file_data * lookup_filename (const char *);
3317 static void retry_incomplete_types (void);
3318 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3319 static void gen_generic_params_dies (tree);
3320 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3321 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3322 static void splice_child_die (dw_die_ref, dw_die_ref);
3323 static int file_info_cmp (const void *, const void *);
3324 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3325 const char *, const char *);
3326 static void output_loc_list (dw_loc_list_ref);
3327 static char *gen_internal_sym (const char *);
3328 static bool want_pubnames (void);
3330 static void prune_unmark_dies (dw_die_ref);
3331 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3332 static void prune_unused_types_mark (dw_die_ref, int);
3333 static void prune_unused_types_walk (dw_die_ref);
3334 static void prune_unused_types_walk_attribs (dw_die_ref);
3335 static void prune_unused_types_prune (dw_die_ref);
3336 static void prune_unused_types (void);
3337 static int maybe_emit_file (struct dwarf_file_data *fd);
3338 static inline const char *AT_vms_delta1 (dw_attr_ref);
3339 static inline const char *AT_vms_delta2 (dw_attr_ref);
3340 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3341 const char *, const char *);
3342 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3343 static void gen_remaining_tmpl_value_param_die_attribute (void);
3344 static bool generic_type_p (tree);
3345 static void schedule_generic_params_dies_gen (tree t);
3346 static void gen_scheduled_generic_parms_dies (void);
3348 static const char *comp_dir_string (void);
3350 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3352 /* enum for tracking thread-local variables whose address is really an offset
3353 relative to the TLS pointer, which will need link-time relocation, but will
3354 not need relocation by the DWARF consumer. */
3356 enum dtprel_bool
3358 dtprel_false = 0,
3359 dtprel_true = 1
3362 /* Return the operator to use for an address of a variable. For dtprel_true, we
3363 use DW_OP_const*. For regular variables, which need both link-time
3364 relocation and consumer-level relocation (e.g., to account for shared objects
3365 loaded at a random address), we use DW_OP_addr*. */
3367 static inline enum dwarf_location_atom
3368 dw_addr_op (enum dtprel_bool dtprel)
3370 if (dtprel == dtprel_true)
3371 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3372 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3373 else
3374 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3377 /* Return a pointer to a newly allocated address location description. If
3378 dwarf_split_debug_info is true, then record the address with the appropriate
3379 relocation. */
3380 static inline dw_loc_descr_ref
3381 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3383 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3385 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3386 ref->dw_loc_oprnd1.v.val_addr = addr;
3387 ref->dtprel = dtprel;
3388 if (dwarf_split_debug_info)
3389 ref->dw_loc_oprnd1.val_entry
3390 = add_addr_table_entry (addr,
3391 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3392 else
3393 ref->dw_loc_oprnd1.val_entry = NULL;
3395 return ref;
3398 /* Section names used to hold DWARF debugging information. */
3400 #ifndef DEBUG_INFO_SECTION
3401 #define DEBUG_INFO_SECTION ".debug_info"
3402 #endif
3403 #ifndef DEBUG_DWO_INFO_SECTION
3404 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3405 #endif
3406 #ifndef DEBUG_ABBREV_SECTION
3407 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3408 #endif
3409 #ifndef DEBUG_DWO_ABBREV_SECTION
3410 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3411 #endif
3412 #ifndef DEBUG_ARANGES_SECTION
3413 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3414 #endif
3415 #ifndef DEBUG_ADDR_SECTION
3416 #define DEBUG_ADDR_SECTION ".debug_addr"
3417 #endif
3418 #ifndef DEBUG_NORM_MACINFO_SECTION
3419 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3420 #endif
3421 #ifndef DEBUG_DWO_MACINFO_SECTION
3422 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3423 #endif
3424 #ifndef DEBUG_MACINFO_SECTION
3425 #define DEBUG_MACINFO_SECTION \
3426 (!dwarf_split_debug_info \
3427 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3428 #endif
3429 #ifndef DEBUG_NORM_MACRO_SECTION
3430 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3431 #endif
3432 #ifndef DEBUG_DWO_MACRO_SECTION
3433 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3434 #endif
3435 #ifndef DEBUG_MACRO_SECTION
3436 #define DEBUG_MACRO_SECTION \
3437 (!dwarf_split_debug_info \
3438 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3439 #endif
3440 #ifndef DEBUG_LINE_SECTION
3441 #define DEBUG_LINE_SECTION ".debug_line"
3442 #endif
3443 #ifndef DEBUG_DWO_LINE_SECTION
3444 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3445 #endif
3446 #ifndef DEBUG_LOC_SECTION
3447 #define DEBUG_LOC_SECTION ".debug_loc"
3448 #endif
3449 #ifndef DEBUG_DWO_LOC_SECTION
3450 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3451 #endif
3452 #ifndef DEBUG_PUBNAMES_SECTION
3453 #define DEBUG_PUBNAMES_SECTION \
3454 ((debug_generate_pub_sections == 2) \
3455 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3456 #endif
3457 #ifndef DEBUG_PUBTYPES_SECTION
3458 #define DEBUG_PUBTYPES_SECTION \
3459 ((debug_generate_pub_sections == 2) \
3460 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3461 #endif
3462 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3463 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3464 #ifndef DEBUG_STR_OFFSETS_SECTION
3465 #define DEBUG_STR_OFFSETS_SECTION \
3466 (!dwarf_split_debug_info \
3467 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3468 #endif
3469 #ifndef DEBUG_STR_DWO_SECTION
3470 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3471 #endif
3472 #ifndef DEBUG_STR_SECTION
3473 #define DEBUG_STR_SECTION ".debug_str"
3474 #endif
3475 #ifndef DEBUG_RANGES_SECTION
3476 #define DEBUG_RANGES_SECTION ".debug_ranges"
3477 #endif
3479 /* Standard ELF section names for compiled code and data. */
3480 #ifndef TEXT_SECTION_NAME
3481 #define TEXT_SECTION_NAME ".text"
3482 #endif
3484 /* Section flags for .debug_macinfo/.debug_macro section. */
3485 #define DEBUG_MACRO_SECTION_FLAGS \
3486 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3488 /* Section flags for .debug_str section. */
3489 #define DEBUG_STR_SECTION_FLAGS \
3490 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3491 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3492 : SECTION_DEBUG)
3494 /* Section flags for .debug_str.dwo section. */
3495 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3497 /* Labels we insert at beginning sections we can reference instead of
3498 the section names themselves. */
3500 #ifndef TEXT_SECTION_LABEL
3501 #define TEXT_SECTION_LABEL "Ltext"
3502 #endif
3503 #ifndef COLD_TEXT_SECTION_LABEL
3504 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3505 #endif
3506 #ifndef DEBUG_LINE_SECTION_LABEL
3507 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3508 #endif
3509 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3510 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3511 #endif
3512 #ifndef DEBUG_INFO_SECTION_LABEL
3513 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3514 #endif
3515 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3516 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3517 #endif
3518 #ifndef DEBUG_ABBREV_SECTION_LABEL
3519 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3520 #endif
3521 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3522 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3523 #endif
3524 #ifndef DEBUG_ADDR_SECTION_LABEL
3525 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3526 #endif
3527 #ifndef DEBUG_LOC_SECTION_LABEL
3528 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3529 #endif
3530 #ifndef DEBUG_RANGES_SECTION_LABEL
3531 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3532 #endif
3533 #ifndef DEBUG_MACINFO_SECTION_LABEL
3534 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3535 #endif
3536 #ifndef DEBUG_MACRO_SECTION_LABEL
3537 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3538 #endif
3539 #define SKELETON_COMP_DIE_ABBREV 1
3540 #define SKELETON_TYPE_DIE_ABBREV 2
3542 /* Definitions of defaults for formats and names of various special
3543 (artificial) labels which may be generated within this file (when the -g
3544 options is used and DWARF2_DEBUGGING_INFO is in effect.
3545 If necessary, these may be overridden from within the tm.h file, but
3546 typically, overriding these defaults is unnecessary. */
3548 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3549 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3550 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3551 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3552 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3553 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3554 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3555 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3556 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3557 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3558 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3559 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3560 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3561 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3563 #ifndef TEXT_END_LABEL
3564 #define TEXT_END_LABEL "Letext"
3565 #endif
3566 #ifndef COLD_END_LABEL
3567 #define COLD_END_LABEL "Letext_cold"
3568 #endif
3569 #ifndef BLOCK_BEGIN_LABEL
3570 #define BLOCK_BEGIN_LABEL "LBB"
3571 #endif
3572 #ifndef BLOCK_END_LABEL
3573 #define BLOCK_END_LABEL "LBE"
3574 #endif
3575 #ifndef LINE_CODE_LABEL
3576 #define LINE_CODE_LABEL "LM"
3577 #endif
3580 /* Return the root of the DIE's built for the current compilation unit. */
3581 static dw_die_ref
3582 comp_unit_die (void)
3584 if (!single_comp_unit_die)
3585 single_comp_unit_die = gen_compile_unit_die (NULL);
3586 return single_comp_unit_die;
3589 /* We allow a language front-end to designate a function that is to be
3590 called to "demangle" any name before it is put into a DIE. */
3592 static const char *(*demangle_name_func) (const char *);
3594 void
3595 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3597 demangle_name_func = func;
3600 /* Test if rtl node points to a pseudo register. */
3602 static inline int
3603 is_pseudo_reg (const_rtx rtl)
3605 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3606 || (GET_CODE (rtl) == SUBREG
3607 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3610 /* Return a reference to a type, with its const and volatile qualifiers
3611 removed. */
3613 static inline tree
3614 type_main_variant (tree type)
3616 type = TYPE_MAIN_VARIANT (type);
3618 /* ??? There really should be only one main variant among any group of
3619 variants of a given type (and all of the MAIN_VARIANT values for all
3620 members of the group should point to that one type) but sometimes the C
3621 front-end messes this up for array types, so we work around that bug
3622 here. */
3623 if (TREE_CODE (type) == ARRAY_TYPE)
3624 while (type != TYPE_MAIN_VARIANT (type))
3625 type = TYPE_MAIN_VARIANT (type);
3627 return type;
3630 /* Return nonzero if the given type node represents a tagged type. */
3632 static inline int
3633 is_tagged_type (const_tree type)
3635 enum tree_code code = TREE_CODE (type);
3637 return (code == RECORD_TYPE || code == UNION_TYPE
3638 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3641 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3643 static void
3644 get_ref_die_offset_label (char *label, dw_die_ref ref)
3646 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3649 /* Return die_offset of a DIE reference to a base type. */
3651 static unsigned long int
3652 get_base_type_offset (dw_die_ref ref)
3654 if (ref->die_offset)
3655 return ref->die_offset;
3656 if (comp_unit_die ()->die_abbrev)
3658 calc_base_type_die_sizes ();
3659 gcc_assert (ref->die_offset);
3661 return ref->die_offset;
3664 /* Return die_offset of a DIE reference other than base type. */
3666 static unsigned long int
3667 get_ref_die_offset (dw_die_ref ref)
3669 gcc_assert (ref->die_offset);
3670 return ref->die_offset;
3673 /* Convert a DIE tag into its string name. */
3675 static const char *
3676 dwarf_tag_name (unsigned int tag)
3678 const char *name = get_DW_TAG_name (tag);
3680 if (name != NULL)
3681 return name;
3683 return "DW_TAG_<unknown>";
3686 /* Convert a DWARF attribute code into its string name. */
3688 static const char *
3689 dwarf_attr_name (unsigned int attr)
3691 const char *name;
3693 switch (attr)
3695 #if VMS_DEBUGGING_INFO
3696 case DW_AT_HP_prologue:
3697 return "DW_AT_HP_prologue";
3698 #else
3699 case DW_AT_MIPS_loop_unroll_factor:
3700 return "DW_AT_MIPS_loop_unroll_factor";
3701 #endif
3703 #if VMS_DEBUGGING_INFO
3704 case DW_AT_HP_epilogue:
3705 return "DW_AT_HP_epilogue";
3706 #else
3707 case DW_AT_MIPS_stride:
3708 return "DW_AT_MIPS_stride";
3709 #endif
3712 name = get_DW_AT_name (attr);
3714 if (name != NULL)
3715 return name;
3717 return "DW_AT_<unknown>";
3720 /* Convert a DWARF value form code into its string name. */
3722 static const char *
3723 dwarf_form_name (unsigned int form)
3725 const char *name = get_DW_FORM_name (form);
3727 if (name != NULL)
3728 return name;
3730 return "DW_FORM_<unknown>";
3733 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3734 instance of an inlined instance of a decl which is local to an inline
3735 function, so we have to trace all of the way back through the origin chain
3736 to find out what sort of node actually served as the original seed for the
3737 given block. */
3739 static tree
3740 decl_ultimate_origin (const_tree decl)
3742 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3743 return NULL_TREE;
3745 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3746 we're trying to output the abstract instance of this function. */
3747 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3748 return NULL_TREE;
3750 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3751 most distant ancestor, this should never happen. */
3752 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3754 return DECL_ABSTRACT_ORIGIN (decl);
3757 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3758 of a virtual function may refer to a base class, so we check the 'this'
3759 parameter. */
3761 static tree
3762 decl_class_context (tree decl)
3764 tree context = NULL_TREE;
3766 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3767 context = DECL_CONTEXT (decl);
3768 else
3769 context = TYPE_MAIN_VARIANT
3770 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3772 if (context && !TYPE_P (context))
3773 context = NULL_TREE;
3775 return context;
3778 /* Add an attribute/value pair to a DIE. */
3780 static inline void
3781 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3783 /* Maybe this should be an assert? */
3784 if (die == NULL)
3785 return;
3787 vec_safe_reserve (die->die_attr, 1);
3788 vec_safe_push (die->die_attr, *attr);
3791 static inline enum dw_val_class
3792 AT_class (dw_attr_ref a)
3794 return a->dw_attr_val.val_class;
3797 /* Return the index for any attribute that will be referenced with a
3798 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3799 are stored in dw_attr_val.v.val_str for reference counting
3800 pruning. */
3802 static inline unsigned int
3803 AT_index (dw_attr_ref a)
3805 if (AT_class (a) == dw_val_class_str)
3806 return a->dw_attr_val.v.val_str->index;
3807 else if (a->dw_attr_val.val_entry != NULL)
3808 return a->dw_attr_val.val_entry->index;
3809 return NOT_INDEXED;
3812 /* Add a flag value attribute to a DIE. */
3814 static inline void
3815 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3817 dw_attr_node attr;
3819 attr.dw_attr = attr_kind;
3820 attr.dw_attr_val.val_class = dw_val_class_flag;
3821 attr.dw_attr_val.val_entry = NULL;
3822 attr.dw_attr_val.v.val_flag = flag;
3823 add_dwarf_attr (die, &attr);
3826 static inline unsigned
3827 AT_flag (dw_attr_ref a)
3829 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3830 return a->dw_attr_val.v.val_flag;
3833 /* Add a signed integer attribute value to a DIE. */
3835 static inline void
3836 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3838 dw_attr_node attr;
3840 attr.dw_attr = attr_kind;
3841 attr.dw_attr_val.val_class = dw_val_class_const;
3842 attr.dw_attr_val.val_entry = NULL;
3843 attr.dw_attr_val.v.val_int = int_val;
3844 add_dwarf_attr (die, &attr);
3847 static inline HOST_WIDE_INT
3848 AT_int (dw_attr_ref a)
3850 gcc_assert (a && AT_class (a) == dw_val_class_const);
3851 return a->dw_attr_val.v.val_int;
3854 /* Add an unsigned integer attribute value to a DIE. */
3856 static inline void
3857 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3858 unsigned HOST_WIDE_INT unsigned_val)
3860 dw_attr_node attr;
3862 attr.dw_attr = attr_kind;
3863 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3864 attr.dw_attr_val.val_entry = NULL;
3865 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3866 add_dwarf_attr (die, &attr);
3869 static inline unsigned HOST_WIDE_INT
3870 AT_unsigned (dw_attr_ref a)
3872 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3873 return a->dw_attr_val.v.val_unsigned;
3876 /* Add an unsigned wide integer attribute value to a DIE. */
3878 static inline void
3879 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3880 const wide_int& w)
3882 dw_attr_node attr;
3884 attr.dw_attr = attr_kind;
3885 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3886 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3887 *attr.dw_attr_val.v.val_wide = w;
3888 add_dwarf_attr (die, &attr);
3891 /* Add an unsigned double integer attribute value to a DIE. */
3893 static inline void
3894 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3895 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3897 dw_attr_node attr;
3899 attr.dw_attr = attr_kind;
3900 attr.dw_attr_val.val_class = dw_val_class_const_double;
3901 attr.dw_attr_val.val_entry = NULL;
3902 attr.dw_attr_val.v.val_double.high = high;
3903 attr.dw_attr_val.v.val_double.low = low;
3904 add_dwarf_attr (die, &attr);
3907 /* Add a floating point attribute value to a DIE and return it. */
3909 static inline void
3910 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3911 unsigned int length, unsigned int elt_size, unsigned char *array)
3913 dw_attr_node attr;
3915 attr.dw_attr = attr_kind;
3916 attr.dw_attr_val.val_class = dw_val_class_vec;
3917 attr.dw_attr_val.val_entry = NULL;
3918 attr.dw_attr_val.v.val_vec.length = length;
3919 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3920 attr.dw_attr_val.v.val_vec.array = array;
3921 add_dwarf_attr (die, &attr);
3924 /* Add an 8-byte data attribute value to a DIE. */
3926 static inline void
3927 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3928 unsigned char data8[8])
3930 dw_attr_node attr;
3932 attr.dw_attr = attr_kind;
3933 attr.dw_attr_val.val_class = dw_val_class_data8;
3934 attr.dw_attr_val.val_entry = NULL;
3935 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3936 add_dwarf_attr (die, &attr);
3939 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3940 dwarf_split_debug_info, address attributes in dies destined for the
3941 final executable have force_direct set to avoid using indexed
3942 references. */
3944 static inline void
3945 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3946 bool force_direct)
3948 dw_attr_node attr;
3949 char * lbl_id;
3951 lbl_id = xstrdup (lbl_low);
3952 attr.dw_attr = DW_AT_low_pc;
3953 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3954 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3955 if (dwarf_split_debug_info && !force_direct)
3956 attr.dw_attr_val.val_entry
3957 = add_addr_table_entry (lbl_id, ate_kind_label);
3958 else
3959 attr.dw_attr_val.val_entry = NULL;
3960 add_dwarf_attr (die, &attr);
3962 attr.dw_attr = DW_AT_high_pc;
3963 if (dwarf_version < 4)
3964 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3965 else
3966 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3967 lbl_id = xstrdup (lbl_high);
3968 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3969 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3970 && dwarf_split_debug_info && !force_direct)
3971 attr.dw_attr_val.val_entry
3972 = add_addr_table_entry (lbl_id, ate_kind_label);
3973 else
3974 attr.dw_attr_val.val_entry = NULL;
3975 add_dwarf_attr (die, &attr);
3978 /* Hash and equality functions for debug_str_hash. */
3980 hashval_t
3981 indirect_string_hasher::hash (indirect_string_node *x)
3983 return htab_hash_string (x->str);
3986 bool
3987 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3989 return strcmp (x1->str, x2) == 0;
3992 /* Add STR to the given string hash table. */
3994 static struct indirect_string_node *
3995 find_AT_string_in_table (const char *str,
3996 hash_table<indirect_string_hasher> *table)
3998 struct indirect_string_node *node;
4000 indirect_string_node **slot
4001 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4002 if (*slot == NULL)
4004 node = ggc_cleared_alloc<indirect_string_node> ();
4005 node->str = ggc_strdup (str);
4006 *slot = node;
4008 else
4009 node = *slot;
4011 node->refcount++;
4012 return node;
4015 /* Add STR to the indirect string hash table. */
4017 static struct indirect_string_node *
4018 find_AT_string (const char *str)
4020 if (! debug_str_hash)
4021 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4023 return find_AT_string_in_table (str, debug_str_hash);
4026 /* Add a string attribute value to a DIE. */
4028 static inline void
4029 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4031 dw_attr_node attr;
4032 struct indirect_string_node *node;
4034 node = find_AT_string (str);
4036 attr.dw_attr = attr_kind;
4037 attr.dw_attr_val.val_class = dw_val_class_str;
4038 attr.dw_attr_val.val_entry = NULL;
4039 attr.dw_attr_val.v.val_str = node;
4040 add_dwarf_attr (die, &attr);
4043 static inline const char *
4044 AT_string (dw_attr_ref a)
4046 gcc_assert (a && AT_class (a) == dw_val_class_str);
4047 return a->dw_attr_val.v.val_str->str;
4050 /* Call this function directly to bypass AT_string_form's logic to put
4051 the string inline in the die. */
4053 static void
4054 set_indirect_string (struct indirect_string_node *node)
4056 char label[32];
4057 /* Already indirect is a no op. */
4058 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4060 gcc_assert (node->label);
4061 return;
4063 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4064 ++dw2_string_counter;
4065 node->label = xstrdup (label);
4067 if (!dwarf_split_debug_info)
4069 node->form = DW_FORM_strp;
4070 node->index = NOT_INDEXED;
4072 else
4074 node->form = DW_FORM_GNU_str_index;
4075 node->index = NO_INDEX_ASSIGNED;
4079 /* Find out whether a string should be output inline in DIE
4080 or out-of-line in .debug_str section. */
4082 static enum dwarf_form
4083 find_string_form (struct indirect_string_node *node)
4085 unsigned int len;
4087 if (node->form)
4088 return node->form;
4090 len = strlen (node->str) + 1;
4092 /* If the string is shorter or equal to the size of the reference, it is
4093 always better to put it inline. */
4094 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4095 return node->form = DW_FORM_string;
4097 /* If we cannot expect the linker to merge strings in .debug_str
4098 section, only put it into .debug_str if it is worth even in this
4099 single module. */
4100 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4101 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4102 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4103 return node->form = DW_FORM_string;
4105 set_indirect_string (node);
4107 return node->form;
4110 /* Find out whether the string referenced from the attribute should be
4111 output inline in DIE or out-of-line in .debug_str section. */
4113 static enum dwarf_form
4114 AT_string_form (dw_attr_ref a)
4116 gcc_assert (a && AT_class (a) == dw_val_class_str);
4117 return find_string_form (a->dw_attr_val.v.val_str);
4120 /* Add a DIE reference attribute value to a DIE. */
4122 static inline void
4123 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4125 dw_attr_node attr;
4127 #ifdef ENABLE_CHECKING
4128 gcc_assert (targ_die != NULL);
4129 #else
4130 /* With LTO we can end up trying to reference something we didn't create
4131 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4132 if (targ_die == NULL)
4133 return;
4134 #endif
4136 attr.dw_attr = attr_kind;
4137 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4138 attr.dw_attr_val.val_entry = NULL;
4139 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4140 attr.dw_attr_val.v.val_die_ref.external = 0;
4141 add_dwarf_attr (die, &attr);
4144 /* Change DIE reference REF to point to NEW_DIE instead. */
4146 static inline void
4147 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4149 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4150 ref->dw_attr_val.v.val_die_ref.die = new_die;
4151 ref->dw_attr_val.v.val_die_ref.external = 0;
4154 /* Add an AT_specification attribute to a DIE, and also make the back
4155 pointer from the specification to the definition. */
4157 static inline void
4158 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4160 add_AT_die_ref (die, DW_AT_specification, targ_die);
4161 gcc_assert (!targ_die->die_definition);
4162 targ_die->die_definition = die;
4165 static inline dw_die_ref
4166 AT_ref (dw_attr_ref a)
4168 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4169 return a->dw_attr_val.v.val_die_ref.die;
4172 static inline int
4173 AT_ref_external (dw_attr_ref a)
4175 if (a && AT_class (a) == dw_val_class_die_ref)
4176 return a->dw_attr_val.v.val_die_ref.external;
4178 return 0;
4181 static inline void
4182 set_AT_ref_external (dw_attr_ref a, int i)
4184 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4185 a->dw_attr_val.v.val_die_ref.external = i;
4188 /* Add an FDE reference attribute value to a DIE. */
4190 static inline void
4191 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4193 dw_attr_node attr;
4195 attr.dw_attr = attr_kind;
4196 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4197 attr.dw_attr_val.val_entry = NULL;
4198 attr.dw_attr_val.v.val_fde_index = targ_fde;
4199 add_dwarf_attr (die, &attr);
4202 /* Add a location description attribute value to a DIE. */
4204 static inline void
4205 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4207 dw_attr_node attr;
4209 attr.dw_attr = attr_kind;
4210 attr.dw_attr_val.val_class = dw_val_class_loc;
4211 attr.dw_attr_val.val_entry = NULL;
4212 attr.dw_attr_val.v.val_loc = loc;
4213 add_dwarf_attr (die, &attr);
4216 static inline dw_loc_descr_ref
4217 AT_loc (dw_attr_ref a)
4219 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4220 return a->dw_attr_val.v.val_loc;
4223 static inline void
4224 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4226 dw_attr_node attr;
4228 attr.dw_attr = attr_kind;
4229 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4230 attr.dw_attr_val.val_entry = NULL;
4231 attr.dw_attr_val.v.val_loc_list = loc_list;
4232 add_dwarf_attr (die, &attr);
4233 have_location_lists = true;
4236 static inline dw_loc_list_ref
4237 AT_loc_list (dw_attr_ref a)
4239 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4240 return a->dw_attr_val.v.val_loc_list;
4243 static inline dw_loc_list_ref *
4244 AT_loc_list_ptr (dw_attr_ref a)
4246 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4247 return &a->dw_attr_val.v.val_loc_list;
4250 struct addr_hasher : ggc_hasher<addr_table_entry *>
4252 static hashval_t hash (addr_table_entry *);
4253 static bool equal (addr_table_entry *, addr_table_entry *);
4256 /* Table of entries into the .debug_addr section. */
4258 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4260 /* Hash an address_table_entry. */
4262 hashval_t
4263 addr_hasher::hash (addr_table_entry *a)
4265 inchash::hash hstate;
4266 switch (a->kind)
4268 case ate_kind_rtx:
4269 hstate.add_int (0);
4270 break;
4271 case ate_kind_rtx_dtprel:
4272 hstate.add_int (1);
4273 break;
4274 case ate_kind_label:
4275 return htab_hash_string (a->addr.label);
4276 default:
4277 gcc_unreachable ();
4279 inchash::add_rtx (a->addr.rtl, hstate);
4280 return hstate.end ();
4283 /* Determine equality for two address_table_entries. */
4285 bool
4286 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4288 if (a1->kind != a2->kind)
4289 return 0;
4290 switch (a1->kind)
4292 case ate_kind_rtx:
4293 case ate_kind_rtx_dtprel:
4294 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4295 case ate_kind_label:
4296 return strcmp (a1->addr.label, a2->addr.label) == 0;
4297 default:
4298 gcc_unreachable ();
4302 /* Initialize an addr_table_entry. */
4304 void
4305 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4307 e->kind = kind;
4308 switch (kind)
4310 case ate_kind_rtx:
4311 case ate_kind_rtx_dtprel:
4312 e->addr.rtl = (rtx) addr;
4313 break;
4314 case ate_kind_label:
4315 e->addr.label = (char *) addr;
4316 break;
4318 e->refcount = 0;
4319 e->index = NO_INDEX_ASSIGNED;
4322 /* Add attr to the address table entry to the table. Defer setting an
4323 index until output time. */
4325 static addr_table_entry *
4326 add_addr_table_entry (void *addr, enum ate_kind kind)
4328 addr_table_entry *node;
4329 addr_table_entry finder;
4331 gcc_assert (dwarf_split_debug_info);
4332 if (! addr_index_table)
4333 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4334 init_addr_table_entry (&finder, kind, addr);
4335 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4337 if (*slot == HTAB_EMPTY_ENTRY)
4339 node = ggc_cleared_alloc<addr_table_entry> ();
4340 init_addr_table_entry (node, kind, addr);
4341 *slot = node;
4343 else
4344 node = *slot;
4346 node->refcount++;
4347 return node;
4350 /* Remove an entry from the addr table by decrementing its refcount.
4351 Strictly, decrementing the refcount would be enough, but the
4352 assertion that the entry is actually in the table has found
4353 bugs. */
4355 static void
4356 remove_addr_table_entry (addr_table_entry *entry)
4358 gcc_assert (dwarf_split_debug_info && addr_index_table);
4359 /* After an index is assigned, the table is frozen. */
4360 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4361 entry->refcount--;
4364 /* Given a location list, remove all addresses it refers to from the
4365 address_table. */
4367 static void
4368 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4370 for (; descr; descr = descr->dw_loc_next)
4371 if (descr->dw_loc_oprnd1.val_entry != NULL)
4373 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4374 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4378 /* A helper function for dwarf2out_finish called through
4379 htab_traverse. Assign an addr_table_entry its index. All entries
4380 must be collected into the table when this function is called,
4381 because the indexing code relies on htab_traverse to traverse nodes
4382 in the same order for each run. */
4385 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4387 addr_table_entry *node = *h;
4389 /* Don't index unreferenced nodes. */
4390 if (node->refcount == 0)
4391 return 1;
4393 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4394 node->index = *index;
4395 *index += 1;
4397 return 1;
4400 /* Add an address constant attribute value to a DIE. When using
4401 dwarf_split_debug_info, address attributes in dies destined for the
4402 final executable should be direct references--setting the parameter
4403 force_direct ensures this behavior. */
4405 static inline void
4406 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4407 bool force_direct)
4409 dw_attr_node attr;
4411 attr.dw_attr = attr_kind;
4412 attr.dw_attr_val.val_class = dw_val_class_addr;
4413 attr.dw_attr_val.v.val_addr = addr;
4414 if (dwarf_split_debug_info && !force_direct)
4415 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4416 else
4417 attr.dw_attr_val.val_entry = NULL;
4418 add_dwarf_attr (die, &attr);
4421 /* Get the RTX from to an address DIE attribute. */
4423 static inline rtx
4424 AT_addr (dw_attr_ref a)
4426 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4427 return a->dw_attr_val.v.val_addr;
4430 /* Add a file attribute value to a DIE. */
4432 static inline void
4433 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4434 struct dwarf_file_data *fd)
4436 dw_attr_node attr;
4438 attr.dw_attr = attr_kind;
4439 attr.dw_attr_val.val_class = dw_val_class_file;
4440 attr.dw_attr_val.val_entry = NULL;
4441 attr.dw_attr_val.v.val_file = fd;
4442 add_dwarf_attr (die, &attr);
4445 /* Get the dwarf_file_data from a file DIE attribute. */
4447 static inline struct dwarf_file_data *
4448 AT_file (dw_attr_ref a)
4450 gcc_assert (a && AT_class (a) == dw_val_class_file);
4451 return a->dw_attr_val.v.val_file;
4454 /* Add a vms delta attribute value to a DIE. */
4456 static inline void
4457 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4458 const char *lbl1, const char *lbl2)
4460 dw_attr_node attr;
4462 attr.dw_attr = attr_kind;
4463 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4464 attr.dw_attr_val.val_entry = NULL;
4465 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4466 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4467 add_dwarf_attr (die, &attr);
4470 /* Add a label identifier attribute value to a DIE. */
4472 static inline void
4473 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4474 const char *lbl_id)
4476 dw_attr_node attr;
4478 attr.dw_attr = attr_kind;
4479 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4480 attr.dw_attr_val.val_entry = NULL;
4481 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4482 if (dwarf_split_debug_info)
4483 attr.dw_attr_val.val_entry
4484 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4485 ate_kind_label);
4486 add_dwarf_attr (die, &attr);
4489 /* Add a section offset attribute value to a DIE, an offset into the
4490 debug_line section. */
4492 static inline void
4493 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4494 const char *label)
4496 dw_attr_node attr;
4498 attr.dw_attr = attr_kind;
4499 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4500 attr.dw_attr_val.val_entry = NULL;
4501 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4502 add_dwarf_attr (die, &attr);
4505 /* Add a section offset attribute value to a DIE, an offset into the
4506 debug_macinfo section. */
4508 static inline void
4509 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4510 const char *label)
4512 dw_attr_node attr;
4514 attr.dw_attr = attr_kind;
4515 attr.dw_attr_val.val_class = dw_val_class_macptr;
4516 attr.dw_attr_val.val_entry = NULL;
4517 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4518 add_dwarf_attr (die, &attr);
4521 /* Add an offset attribute value to a DIE. */
4523 static inline void
4524 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4525 unsigned HOST_WIDE_INT offset)
4527 dw_attr_node attr;
4529 attr.dw_attr = attr_kind;
4530 attr.dw_attr_val.val_class = dw_val_class_offset;
4531 attr.dw_attr_val.val_entry = NULL;
4532 attr.dw_attr_val.v.val_offset = offset;
4533 add_dwarf_attr (die, &attr);
4536 /* Add a range_list attribute value to a DIE. When using
4537 dwarf_split_debug_info, address attributes in dies destined for the
4538 final executable should be direct references--setting the parameter
4539 force_direct ensures this behavior. */
4541 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4542 #define RELOCATED_OFFSET (NULL)
4544 static void
4545 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4546 long unsigned int offset, bool force_direct)
4548 dw_attr_node attr;
4550 attr.dw_attr = attr_kind;
4551 attr.dw_attr_val.val_class = dw_val_class_range_list;
4552 /* For the range_list attribute, use val_entry to store whether the
4553 offset should follow split-debug-info or normal semantics. This
4554 value is read in output_range_list_offset. */
4555 if (dwarf_split_debug_info && !force_direct)
4556 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4557 else
4558 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4559 attr.dw_attr_val.v.val_offset = offset;
4560 add_dwarf_attr (die, &attr);
4563 /* Return the start label of a delta attribute. */
4565 static inline const char *
4566 AT_vms_delta1 (dw_attr_ref a)
4568 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4569 return a->dw_attr_val.v.val_vms_delta.lbl1;
4572 /* Return the end label of a delta attribute. */
4574 static inline const char *
4575 AT_vms_delta2 (dw_attr_ref a)
4577 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4578 return a->dw_attr_val.v.val_vms_delta.lbl2;
4581 static inline const char *
4582 AT_lbl (dw_attr_ref a)
4584 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4585 || AT_class (a) == dw_val_class_lineptr
4586 || AT_class (a) == dw_val_class_macptr
4587 || AT_class (a) == dw_val_class_high_pc));
4588 return a->dw_attr_val.v.val_lbl_id;
4591 /* Get the attribute of type attr_kind. */
4593 static dw_attr_ref
4594 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4596 dw_attr_ref a;
4597 unsigned ix;
4598 dw_die_ref spec = NULL;
4600 if (! die)
4601 return NULL;
4603 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4604 if (a->dw_attr == attr_kind)
4605 return a;
4606 else if (a->dw_attr == DW_AT_specification
4607 || a->dw_attr == DW_AT_abstract_origin)
4608 spec = AT_ref (a);
4610 if (spec)
4611 return get_AT (spec, attr_kind);
4613 return NULL;
4616 /* Returns the parent of the declaration of DIE. */
4618 static dw_die_ref
4619 get_die_parent (dw_die_ref die)
4621 dw_die_ref t;
4623 if (!die)
4624 return NULL;
4626 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4627 || (t = get_AT_ref (die, DW_AT_specification)))
4628 die = t;
4630 return die->die_parent;
4633 /* Return the "low pc" attribute value, typically associated with a subprogram
4634 DIE. Return null if the "low pc" attribute is either not present, or if it
4635 cannot be represented as an assembler label identifier. */
4637 static inline const char *
4638 get_AT_low_pc (dw_die_ref die)
4640 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4642 return a ? AT_lbl (a) : NULL;
4645 /* Return the "high pc" attribute value, typically associated with a subprogram
4646 DIE. Return null if the "high pc" attribute is either not present, or if it
4647 cannot be represented as an assembler label identifier. */
4649 static inline const char *
4650 get_AT_hi_pc (dw_die_ref die)
4652 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4654 return a ? AT_lbl (a) : NULL;
4657 /* Return the value of the string attribute designated by ATTR_KIND, or
4658 NULL if it is not present. */
4660 static inline const char *
4661 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4663 dw_attr_ref a = get_AT (die, attr_kind);
4665 return a ? AT_string (a) : NULL;
4668 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4669 if it is not present. */
4671 static inline int
4672 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4674 dw_attr_ref a = get_AT (die, attr_kind);
4676 return a ? AT_flag (a) : 0;
4679 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4680 if it is not present. */
4682 static inline unsigned
4683 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4685 dw_attr_ref a = get_AT (die, attr_kind);
4687 return a ? AT_unsigned (a) : 0;
4690 static inline dw_die_ref
4691 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4693 dw_attr_ref a = get_AT (die, attr_kind);
4695 return a ? AT_ref (a) : NULL;
4698 static inline struct dwarf_file_data *
4699 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4701 dw_attr_ref a = get_AT (die, attr_kind);
4703 return a ? AT_file (a) : NULL;
4706 /* Return TRUE if the language is C++. */
4708 static inline bool
4709 is_cxx (void)
4711 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4713 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4714 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4717 /* Return TRUE if the language is Java. */
4719 static inline bool
4720 is_java (void)
4722 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4724 return lang == DW_LANG_Java;
4727 /* Return TRUE if the language is Fortran. */
4729 static inline bool
4730 is_fortran (void)
4732 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4734 return (lang == DW_LANG_Fortran77
4735 || lang == DW_LANG_Fortran90
4736 || lang == DW_LANG_Fortran95);
4739 /* Return TRUE if the language is Ada. */
4741 static inline bool
4742 is_ada (void)
4744 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4746 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4749 /* Remove the specified attribute if present. */
4751 static void
4752 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4754 dw_attr_ref a;
4755 unsigned ix;
4757 if (! die)
4758 return;
4760 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4761 if (a->dw_attr == attr_kind)
4763 if (AT_class (a) == dw_val_class_str)
4764 if (a->dw_attr_val.v.val_str->refcount)
4765 a->dw_attr_val.v.val_str->refcount--;
4767 /* vec::ordered_remove should help reduce the number of abbrevs
4768 that are needed. */
4769 die->die_attr->ordered_remove (ix);
4770 return;
4774 /* Remove CHILD from its parent. PREV must have the property that
4775 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4777 static void
4778 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4780 gcc_assert (child->die_parent == prev->die_parent);
4781 gcc_assert (prev->die_sib == child);
4782 if (prev == child)
4784 gcc_assert (child->die_parent->die_child == child);
4785 prev = NULL;
4787 else
4788 prev->die_sib = child->die_sib;
4789 if (child->die_parent->die_child == child)
4790 child->die_parent->die_child = prev;
4793 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4794 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4796 static void
4797 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4799 dw_die_ref parent = old_child->die_parent;
4801 gcc_assert (parent == prev->die_parent);
4802 gcc_assert (prev->die_sib == old_child);
4804 new_child->die_parent = parent;
4805 if (prev == old_child)
4807 gcc_assert (parent->die_child == old_child);
4808 new_child->die_sib = new_child;
4810 else
4812 prev->die_sib = new_child;
4813 new_child->die_sib = old_child->die_sib;
4815 if (old_child->die_parent->die_child == old_child)
4816 old_child->die_parent->die_child = new_child;
4819 /* Move all children from OLD_PARENT to NEW_PARENT. */
4821 static void
4822 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4824 dw_die_ref c;
4825 new_parent->die_child = old_parent->die_child;
4826 old_parent->die_child = NULL;
4827 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4830 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4831 matches TAG. */
4833 static void
4834 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4836 dw_die_ref c;
4838 c = die->die_child;
4839 if (c) do {
4840 dw_die_ref prev = c;
4841 c = c->die_sib;
4842 while (c->die_tag == tag)
4844 remove_child_with_prev (c, prev);
4845 /* Might have removed every child. */
4846 if (c == c->die_sib)
4847 return;
4848 c = c->die_sib;
4850 } while (c != die->die_child);
4853 /* Add a CHILD_DIE as the last child of DIE. */
4855 static void
4856 add_child_die (dw_die_ref die, dw_die_ref child_die)
4858 /* FIXME this should probably be an assert. */
4859 if (! die || ! child_die)
4860 return;
4861 gcc_assert (die != child_die);
4863 child_die->die_parent = die;
4864 if (die->die_child)
4866 child_die->die_sib = die->die_child->die_sib;
4867 die->die_child->die_sib = child_die;
4869 else
4870 child_die->die_sib = child_die;
4871 die->die_child = child_die;
4874 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4875 is the specification, to the end of PARENT's list of children.
4876 This is done by removing and re-adding it. */
4878 static void
4879 splice_child_die (dw_die_ref parent, dw_die_ref child)
4881 dw_die_ref p;
4883 /* We want the declaration DIE from inside the class, not the
4884 specification DIE at toplevel. */
4885 if (child->die_parent != parent)
4887 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4889 if (tmp)
4890 child = tmp;
4893 gcc_assert (child->die_parent == parent
4894 || (child->die_parent
4895 == get_AT_ref (parent, DW_AT_specification)));
4897 for (p = child->die_parent->die_child; ; p = p->die_sib)
4898 if (p->die_sib == child)
4900 remove_child_with_prev (child, p);
4901 break;
4904 add_child_die (parent, child);
4907 /* Return a pointer to a newly created DIE node. */
4909 static inline dw_die_ref
4910 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4912 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4914 die->die_tag = tag_value;
4916 if (parent_die != NULL)
4917 add_child_die (parent_die, die);
4918 else
4920 limbo_die_node *limbo_node;
4922 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4923 limbo_node->die = die;
4924 limbo_node->created_for = t;
4925 limbo_node->next = limbo_die_list;
4926 limbo_die_list = limbo_node;
4929 return die;
4932 /* Return the DIE associated with the given type specifier. */
4934 static inline dw_die_ref
4935 lookup_type_die (tree type)
4937 return TYPE_SYMTAB_DIE (type);
4940 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4941 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4942 anonymous type instead the one of the naming typedef. */
4944 static inline dw_die_ref
4945 strip_naming_typedef (tree type, dw_die_ref type_die)
4947 if (type
4948 && TREE_CODE (type) == RECORD_TYPE
4949 && type_die
4950 && type_die->die_tag == DW_TAG_typedef
4951 && is_naming_typedef_decl (TYPE_NAME (type)))
4952 type_die = get_AT_ref (type_die, DW_AT_type);
4953 return type_die;
4956 /* Like lookup_type_die, but if type is an anonymous type named by a
4957 typedef[1], return the DIE of the anonymous type instead the one of
4958 the naming typedef. This is because in gen_typedef_die, we did
4959 equate the anonymous struct named by the typedef with the DIE of
4960 the naming typedef. So by default, lookup_type_die on an anonymous
4961 struct yields the DIE of the naming typedef.
4963 [1]: Read the comment of is_naming_typedef_decl to learn about what
4964 a naming typedef is. */
4966 static inline dw_die_ref
4967 lookup_type_die_strip_naming_typedef (tree type)
4969 dw_die_ref die = lookup_type_die (type);
4970 return strip_naming_typedef (type, die);
4973 /* Equate a DIE to a given type specifier. */
4975 static inline void
4976 equate_type_number_to_die (tree type, dw_die_ref type_die)
4978 TYPE_SYMTAB_DIE (type) = type_die;
4981 /* Returns a hash value for X (which really is a die_struct). */
4983 inline hashval_t
4984 decl_die_hasher::hash (die_node *x)
4986 return (hashval_t) x->decl_id;
4989 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4991 inline bool
4992 decl_die_hasher::equal (die_node *x, tree y)
4994 return (x->decl_id == DECL_UID (y));
4997 /* Return the DIE associated with a given declaration. */
4999 static inline dw_die_ref
5000 lookup_decl_die (tree decl)
5002 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5005 /* Returns a hash value for X (which really is a var_loc_list). */
5007 inline hashval_t
5008 decl_loc_hasher::hash (var_loc_list *x)
5010 return (hashval_t) x->decl_id;
5013 /* Return nonzero if decl_id of var_loc_list X is the same as
5014 UID of decl *Y. */
5016 inline bool
5017 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5019 return (x->decl_id == DECL_UID (y));
5022 /* Return the var_loc list associated with a given declaration. */
5024 static inline var_loc_list *
5025 lookup_decl_loc (const_tree decl)
5027 if (!decl_loc_table)
5028 return NULL;
5029 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5032 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5034 inline hashval_t
5035 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5037 return (hashval_t) x->decl_id;
5040 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5041 UID of decl *Y. */
5043 inline bool
5044 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5046 return (x->decl_id == DECL_UID (y));
5049 /* Equate a DIE to a particular declaration. */
5051 static void
5052 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5054 unsigned int decl_id = DECL_UID (decl);
5056 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5057 decl_die->decl_id = decl_id;
5060 /* Return how many bits covers PIECE EXPR_LIST. */
5062 static int
5063 decl_piece_bitsize (rtx piece)
5065 int ret = (int) GET_MODE (piece);
5066 if (ret)
5067 return ret;
5068 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5069 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5070 return INTVAL (XEXP (XEXP (piece, 0), 0));
5073 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5075 static rtx *
5076 decl_piece_varloc_ptr (rtx piece)
5078 if ((int) GET_MODE (piece))
5079 return &XEXP (piece, 0);
5080 else
5081 return &XEXP (XEXP (piece, 0), 1);
5084 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5085 Next is the chain of following piece nodes. */
5087 static rtx_expr_list *
5088 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5090 if (bitsize <= (int) MAX_MACHINE_MODE)
5091 return alloc_EXPR_LIST (bitsize, loc_note, next);
5092 else
5093 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5094 GEN_INT (bitsize),
5095 loc_note), next);
5098 /* Return rtx that should be stored into loc field for
5099 LOC_NOTE and BITPOS/BITSIZE. */
5101 static rtx
5102 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5103 HOST_WIDE_INT bitsize)
5105 if (bitsize != -1)
5107 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5108 if (bitpos != 0)
5109 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5111 return loc_note;
5114 /* This function either modifies location piece list *DEST in
5115 place (if SRC and INNER is NULL), or copies location piece list
5116 *SRC to *DEST while modifying it. Location BITPOS is modified
5117 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5118 not copied and if needed some padding around it is added.
5119 When modifying in place, DEST should point to EXPR_LIST where
5120 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5121 to the start of the whole list and INNER points to the EXPR_LIST
5122 where earlier pieces cover PIECE_BITPOS bits. */
5124 static void
5125 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5126 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5127 HOST_WIDE_INT bitsize, rtx loc_note)
5129 int diff;
5130 bool copy = inner != NULL;
5132 if (copy)
5134 /* First copy all nodes preceding the current bitpos. */
5135 while (src != inner)
5137 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5138 decl_piece_bitsize (*src), NULL_RTX);
5139 dest = &XEXP (*dest, 1);
5140 src = &XEXP (*src, 1);
5143 /* Add padding if needed. */
5144 if (bitpos != piece_bitpos)
5146 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5147 copy ? NULL_RTX : *dest);
5148 dest = &XEXP (*dest, 1);
5150 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5152 gcc_assert (!copy);
5153 /* A piece with correct bitpos and bitsize already exist,
5154 just update the location for it and return. */
5155 *decl_piece_varloc_ptr (*dest) = loc_note;
5156 return;
5158 /* Add the piece that changed. */
5159 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5160 dest = &XEXP (*dest, 1);
5161 /* Skip over pieces that overlap it. */
5162 diff = bitpos - piece_bitpos + bitsize;
5163 if (!copy)
5164 src = dest;
5165 while (diff > 0 && *src)
5167 rtx piece = *src;
5168 diff -= decl_piece_bitsize (piece);
5169 if (copy)
5170 src = &XEXP (piece, 1);
5171 else
5173 *src = XEXP (piece, 1);
5174 free_EXPR_LIST_node (piece);
5177 /* Add padding if needed. */
5178 if (diff < 0 && *src)
5180 if (!copy)
5181 dest = src;
5182 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5183 dest = &XEXP (*dest, 1);
5185 if (!copy)
5186 return;
5187 /* Finally copy all nodes following it. */
5188 while (*src)
5190 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5191 decl_piece_bitsize (*src), NULL_RTX);
5192 dest = &XEXP (*dest, 1);
5193 src = &XEXP (*src, 1);
5197 /* Add a variable location node to the linked list for DECL. */
5199 static struct var_loc_node *
5200 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5202 unsigned int decl_id;
5203 var_loc_list *temp;
5204 struct var_loc_node *loc = NULL;
5205 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5207 if (TREE_CODE (decl) == VAR_DECL
5208 && DECL_HAS_DEBUG_EXPR_P (decl))
5210 tree realdecl = DECL_DEBUG_EXPR (decl);
5211 if (handled_component_p (realdecl)
5212 || (TREE_CODE (realdecl) == MEM_REF
5213 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5215 HOST_WIDE_INT maxsize;
5216 tree innerdecl;
5217 innerdecl
5218 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5219 if (!DECL_P (innerdecl)
5220 || DECL_IGNORED_P (innerdecl)
5221 || TREE_STATIC (innerdecl)
5222 || bitsize <= 0
5223 || bitpos + bitsize > 256
5224 || bitsize != maxsize)
5225 return NULL;
5226 decl = innerdecl;
5230 decl_id = DECL_UID (decl);
5231 var_loc_list **slot
5232 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5233 if (*slot == NULL)
5235 temp = ggc_cleared_alloc<var_loc_list> ();
5236 temp->decl_id = decl_id;
5237 *slot = temp;
5239 else
5240 temp = *slot;
5242 /* For PARM_DECLs try to keep around the original incoming value,
5243 even if that means we'll emit a zero-range .debug_loc entry. */
5244 if (temp->last
5245 && temp->first == temp->last
5246 && TREE_CODE (decl) == PARM_DECL
5247 && NOTE_P (temp->first->loc)
5248 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5249 && DECL_INCOMING_RTL (decl)
5250 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5251 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5252 == GET_CODE (DECL_INCOMING_RTL (decl))
5253 && prev_real_insn (temp->first->loc) == NULL_RTX
5254 && (bitsize != -1
5255 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5256 NOTE_VAR_LOCATION_LOC (loc_note))
5257 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5258 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5260 loc = ggc_cleared_alloc<var_loc_node> ();
5261 temp->first->next = loc;
5262 temp->last = loc;
5263 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5265 else if (temp->last)
5267 struct var_loc_node *last = temp->last, *unused = NULL;
5268 rtx *piece_loc = NULL, last_loc_note;
5269 int piece_bitpos = 0;
5270 if (last->next)
5272 last = last->next;
5273 gcc_assert (last->next == NULL);
5275 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5277 piece_loc = &last->loc;
5280 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5281 if (piece_bitpos + cur_bitsize > bitpos)
5282 break;
5283 piece_bitpos += cur_bitsize;
5284 piece_loc = &XEXP (*piece_loc, 1);
5286 while (*piece_loc);
5288 /* TEMP->LAST here is either pointer to the last but one or
5289 last element in the chained list, LAST is pointer to the
5290 last element. */
5291 if (label && strcmp (last->label, label) == 0)
5293 /* For SRA optimized variables if there weren't any real
5294 insns since last note, just modify the last node. */
5295 if (piece_loc != NULL)
5297 adjust_piece_list (piece_loc, NULL, NULL,
5298 bitpos, piece_bitpos, bitsize, loc_note);
5299 return NULL;
5301 /* If the last note doesn't cover any instructions, remove it. */
5302 if (temp->last != last)
5304 temp->last->next = NULL;
5305 unused = last;
5306 last = temp->last;
5307 gcc_assert (strcmp (last->label, label) != 0);
5309 else
5311 gcc_assert (temp->first == temp->last
5312 || (temp->first->next == temp->last
5313 && TREE_CODE (decl) == PARM_DECL));
5314 memset (temp->last, '\0', sizeof (*temp->last));
5315 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5316 return temp->last;
5319 if (bitsize == -1 && NOTE_P (last->loc))
5320 last_loc_note = last->loc;
5321 else if (piece_loc != NULL
5322 && *piece_loc != NULL_RTX
5323 && piece_bitpos == bitpos
5324 && decl_piece_bitsize (*piece_loc) == bitsize)
5325 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5326 else
5327 last_loc_note = NULL_RTX;
5328 /* If the current location is the same as the end of the list,
5329 and either both or neither of the locations is uninitialized,
5330 we have nothing to do. */
5331 if (last_loc_note == NULL_RTX
5332 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5333 NOTE_VAR_LOCATION_LOC (loc_note)))
5334 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5335 != NOTE_VAR_LOCATION_STATUS (loc_note))
5336 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5337 == VAR_INIT_STATUS_UNINITIALIZED)
5338 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5339 == VAR_INIT_STATUS_UNINITIALIZED))))
5341 /* Add LOC to the end of list and update LAST. If the last
5342 element of the list has been removed above, reuse its
5343 memory for the new node, otherwise allocate a new one. */
5344 if (unused)
5346 loc = unused;
5347 memset (loc, '\0', sizeof (*loc));
5349 else
5350 loc = ggc_cleared_alloc<var_loc_node> ();
5351 if (bitsize == -1 || piece_loc == NULL)
5352 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5353 else
5354 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5355 bitpos, piece_bitpos, bitsize, loc_note);
5356 last->next = loc;
5357 /* Ensure TEMP->LAST will point either to the new last but one
5358 element of the chain, or to the last element in it. */
5359 if (last != temp->last)
5360 temp->last = last;
5362 else if (unused)
5363 ggc_free (unused);
5365 else
5367 loc = ggc_cleared_alloc<var_loc_node> ();
5368 temp->first = loc;
5369 temp->last = loc;
5370 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5372 return loc;
5375 /* Keep track of the number of spaces used to indent the
5376 output of the debugging routines that print the structure of
5377 the DIE internal representation. */
5378 static int print_indent;
5380 /* Indent the line the number of spaces given by print_indent. */
5382 static inline void
5383 print_spaces (FILE *outfile)
5385 fprintf (outfile, "%*s", print_indent, "");
5388 /* Print a type signature in hex. */
5390 static inline void
5391 print_signature (FILE *outfile, char *sig)
5393 int i;
5395 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5396 fprintf (outfile, "%02x", sig[i] & 0xff);
5399 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5401 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5402 RECURSE, output location descriptor operations. */
5404 static void
5405 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5407 switch (val->val_class)
5409 case dw_val_class_addr:
5410 fprintf (outfile, "address");
5411 break;
5412 case dw_val_class_offset:
5413 fprintf (outfile, "offset");
5414 break;
5415 case dw_val_class_loc:
5416 fprintf (outfile, "location descriptor");
5417 if (val->v.val_loc == NULL)
5418 fprintf (outfile, " -> <null>\n");
5419 else if (recurse)
5421 fprintf (outfile, ":\n");
5422 print_indent += 4;
5423 print_loc_descr (val->v.val_loc, outfile);
5424 print_indent -= 4;
5426 else
5427 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5428 break;
5429 case dw_val_class_loc_list:
5430 fprintf (outfile, "location list -> label:%s",
5431 val->v.val_loc_list->ll_symbol);
5432 break;
5433 case dw_val_class_range_list:
5434 fprintf (outfile, "range list");
5435 break;
5436 case dw_val_class_const:
5437 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5438 break;
5439 case dw_val_class_unsigned_const:
5440 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5441 break;
5442 case dw_val_class_const_double:
5443 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5444 HOST_WIDE_INT_PRINT_UNSIGNED")",
5445 val->v.val_double.high,
5446 val->v.val_double.low);
5447 break;
5448 case dw_val_class_wide_int:
5450 int i = val->v.val_wide->get_len ();
5451 fprintf (outfile, "constant (");
5452 gcc_assert (i > 0);
5453 if (val->v.val_wide->elt (i - 1) == 0)
5454 fprintf (outfile, "0x");
5455 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5456 val->v.val_wide->elt (--i));
5457 while (--i >= 0)
5458 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5459 val->v.val_wide->elt (i));
5460 fprintf (outfile, ")");
5461 break;
5463 case dw_val_class_vec:
5464 fprintf (outfile, "floating-point or vector constant");
5465 break;
5466 case dw_val_class_flag:
5467 fprintf (outfile, "%u", val->v.val_flag);
5468 break;
5469 case dw_val_class_die_ref:
5470 if (val->v.val_die_ref.die != NULL)
5472 dw_die_ref die = val->v.val_die_ref.die;
5474 if (die->comdat_type_p)
5476 fprintf (outfile, "die -> signature: ");
5477 print_signature (outfile,
5478 die->die_id.die_type_node->signature);
5480 else if (die->die_id.die_symbol)
5481 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5482 else
5483 fprintf (outfile, "die -> %ld", die->die_offset);
5484 fprintf (outfile, " (%p)", (void *) die);
5486 else
5487 fprintf (outfile, "die -> <null>");
5488 break;
5489 case dw_val_class_vms_delta:
5490 fprintf (outfile, "delta: @slotcount(%s-%s)",
5491 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5492 break;
5493 case dw_val_class_lbl_id:
5494 case dw_val_class_lineptr:
5495 case dw_val_class_macptr:
5496 case dw_val_class_high_pc:
5497 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5498 break;
5499 case dw_val_class_str:
5500 if (val->v.val_str->str != NULL)
5501 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5502 else
5503 fprintf (outfile, "<null>");
5504 break;
5505 case dw_val_class_file:
5506 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5507 val->v.val_file->emitted_number);
5508 break;
5509 case dw_val_class_data8:
5511 int i;
5513 for (i = 0; i < 8; i++)
5514 fprintf (outfile, "%02x", val->v.val_data8[i]);
5515 break;
5517 default:
5518 break;
5522 /* Likewise, for a DIE attribute. */
5524 static void
5525 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5527 print_dw_val (&a->dw_attr_val, recurse, outfile);
5531 /* Print the list of operands in the LOC location description to OUTFILE. This
5532 routine is a debugging aid only. */
5534 static void
5535 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5537 dw_loc_descr_ref l = loc;
5539 if (loc == NULL)
5541 print_spaces (outfile);
5542 fprintf (outfile, "<null>\n");
5543 return;
5546 for (l = loc; l != NULL; l = l->dw_loc_next)
5548 print_spaces (outfile);
5549 fprintf (outfile, "(%p) %s",
5550 (void *) l,
5551 dwarf_stack_op_name (l->dw_loc_opc));
5552 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5554 fprintf (outfile, " ");
5555 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5557 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5559 fprintf (outfile, ", ");
5560 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5562 fprintf (outfile, "\n");
5566 /* Print the information associated with a given DIE, and its children.
5567 This routine is a debugging aid only. */
5569 static void
5570 print_die (dw_die_ref die, FILE *outfile)
5572 dw_attr_ref a;
5573 dw_die_ref c;
5574 unsigned ix;
5576 print_spaces (outfile);
5577 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5578 die->die_offset, dwarf_tag_name (die->die_tag),
5579 (void*) die);
5580 print_spaces (outfile);
5581 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5582 fprintf (outfile, " offset: %ld", die->die_offset);
5583 fprintf (outfile, " mark: %d\n", die->die_mark);
5585 if (die->comdat_type_p)
5587 print_spaces (outfile);
5588 fprintf (outfile, " signature: ");
5589 print_signature (outfile, die->die_id.die_type_node->signature);
5590 fprintf (outfile, "\n");
5593 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5595 print_spaces (outfile);
5596 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5598 print_attribute (a, true, outfile);
5599 fprintf (outfile, "\n");
5602 if (die->die_child != NULL)
5604 print_indent += 4;
5605 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5606 print_indent -= 4;
5608 if (print_indent == 0)
5609 fprintf (outfile, "\n");
5612 /* Print the list of operations in the LOC location description. */
5614 DEBUG_FUNCTION void
5615 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5617 print_loc_descr (loc, stderr);
5620 /* Print the information collected for a given DIE. */
5622 DEBUG_FUNCTION void
5623 debug_dwarf_die (dw_die_ref die)
5625 print_die (die, stderr);
5628 DEBUG_FUNCTION void
5629 debug (die_struct &ref)
5631 print_die (&ref, stderr);
5634 DEBUG_FUNCTION void
5635 debug (die_struct *ptr)
5637 if (ptr)
5638 debug (*ptr);
5639 else
5640 fprintf (stderr, "<nil>\n");
5644 /* Print all DWARF information collected for the compilation unit.
5645 This routine is a debugging aid only. */
5647 DEBUG_FUNCTION void
5648 debug_dwarf (void)
5650 print_indent = 0;
5651 print_die (comp_unit_die (), stderr);
5654 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5655 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5656 DIE that marks the start of the DIEs for this include file. */
5658 static dw_die_ref
5659 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5661 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5662 dw_die_ref new_unit = gen_compile_unit_die (filename);
5664 new_unit->die_sib = old_unit;
5665 return new_unit;
5668 /* Close an include-file CU and reopen the enclosing one. */
5670 static dw_die_ref
5671 pop_compile_unit (dw_die_ref old_unit)
5673 dw_die_ref new_unit = old_unit->die_sib;
5675 old_unit->die_sib = NULL;
5676 return new_unit;
5679 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5680 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5681 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5683 /* Calculate the checksum of a location expression. */
5685 static inline void
5686 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5688 int tem;
5689 inchash::hash hstate;
5690 hashval_t hash;
5692 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5693 CHECKSUM (tem);
5694 hash_loc_operands (loc, hstate);
5695 hash = hstate.end();
5696 CHECKSUM (hash);
5699 /* Calculate the checksum of an attribute. */
5701 static void
5702 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5704 dw_loc_descr_ref loc;
5705 rtx r;
5707 CHECKSUM (at->dw_attr);
5709 /* We don't care that this was compiled with a different compiler
5710 snapshot; if the output is the same, that's what matters. */
5711 if (at->dw_attr == DW_AT_producer)
5712 return;
5714 switch (AT_class (at))
5716 case dw_val_class_const:
5717 CHECKSUM (at->dw_attr_val.v.val_int);
5718 break;
5719 case dw_val_class_unsigned_const:
5720 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5721 break;
5722 case dw_val_class_const_double:
5723 CHECKSUM (at->dw_attr_val.v.val_double);
5724 break;
5725 case dw_val_class_wide_int:
5726 CHECKSUM (*at->dw_attr_val.v.val_wide);
5727 break;
5728 case dw_val_class_vec:
5729 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5730 (at->dw_attr_val.v.val_vec.length
5731 * at->dw_attr_val.v.val_vec.elt_size));
5732 break;
5733 case dw_val_class_flag:
5734 CHECKSUM (at->dw_attr_val.v.val_flag);
5735 break;
5736 case dw_val_class_str:
5737 CHECKSUM_STRING (AT_string (at));
5738 break;
5740 case dw_val_class_addr:
5741 r = AT_addr (at);
5742 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5743 CHECKSUM_STRING (XSTR (r, 0));
5744 break;
5746 case dw_val_class_offset:
5747 CHECKSUM (at->dw_attr_val.v.val_offset);
5748 break;
5750 case dw_val_class_loc:
5751 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5752 loc_checksum (loc, ctx);
5753 break;
5755 case dw_val_class_die_ref:
5756 die_checksum (AT_ref (at), ctx, mark);
5757 break;
5759 case dw_val_class_fde_ref:
5760 case dw_val_class_vms_delta:
5761 case dw_val_class_lbl_id:
5762 case dw_val_class_lineptr:
5763 case dw_val_class_macptr:
5764 case dw_val_class_high_pc:
5765 break;
5767 case dw_val_class_file:
5768 CHECKSUM_STRING (AT_file (at)->filename);
5769 break;
5771 case dw_val_class_data8:
5772 CHECKSUM (at->dw_attr_val.v.val_data8);
5773 break;
5775 default:
5776 break;
5780 /* Calculate the checksum of a DIE. */
5782 static void
5783 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5785 dw_die_ref c;
5786 dw_attr_ref a;
5787 unsigned ix;
5789 /* To avoid infinite recursion. */
5790 if (die->die_mark)
5792 CHECKSUM (die->die_mark);
5793 return;
5795 die->die_mark = ++(*mark);
5797 CHECKSUM (die->die_tag);
5799 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5800 attr_checksum (a, ctx, mark);
5802 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5805 #undef CHECKSUM
5806 #undef CHECKSUM_BLOCK
5807 #undef CHECKSUM_STRING
5809 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5810 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5811 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5812 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5813 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5814 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5815 #define CHECKSUM_ATTR(FOO) \
5816 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5818 /* Calculate the checksum of a number in signed LEB128 format. */
5820 static void
5821 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5823 unsigned char byte;
5824 bool more;
5826 while (1)
5828 byte = (value & 0x7f);
5829 value >>= 7;
5830 more = !((value == 0 && (byte & 0x40) == 0)
5831 || (value == -1 && (byte & 0x40) != 0));
5832 if (more)
5833 byte |= 0x80;
5834 CHECKSUM (byte);
5835 if (!more)
5836 break;
5840 /* Calculate the checksum of a number in unsigned LEB128 format. */
5842 static void
5843 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5845 while (1)
5847 unsigned char byte = (value & 0x7f);
5848 value >>= 7;
5849 if (value != 0)
5850 /* More bytes to follow. */
5851 byte |= 0x80;
5852 CHECKSUM (byte);
5853 if (value == 0)
5854 break;
5858 /* Checksum the context of the DIE. This adds the names of any
5859 surrounding namespaces or structures to the checksum. */
5861 static void
5862 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5864 const char *name;
5865 dw_die_ref spec;
5866 int tag = die->die_tag;
5868 if (tag != DW_TAG_namespace
5869 && tag != DW_TAG_structure_type
5870 && tag != DW_TAG_class_type)
5871 return;
5873 name = get_AT_string (die, DW_AT_name);
5875 spec = get_AT_ref (die, DW_AT_specification);
5876 if (spec != NULL)
5877 die = spec;
5879 if (die->die_parent != NULL)
5880 checksum_die_context (die->die_parent, ctx);
5882 CHECKSUM_ULEB128 ('C');
5883 CHECKSUM_ULEB128 (tag);
5884 if (name != NULL)
5885 CHECKSUM_STRING (name);
5888 /* Calculate the checksum of a location expression. */
5890 static inline void
5891 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5893 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5894 were emitted as a DW_FORM_sdata instead of a location expression. */
5895 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5897 CHECKSUM_ULEB128 (DW_FORM_sdata);
5898 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5899 return;
5902 /* Otherwise, just checksum the raw location expression. */
5903 while (loc != NULL)
5905 inchash::hash hstate;
5906 hashval_t hash;
5908 CHECKSUM_ULEB128 (loc->dtprel);
5909 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5910 hash_loc_operands (loc, hstate);
5911 hash = hstate.end ();
5912 CHECKSUM (hash);
5913 loc = loc->dw_loc_next;
5917 /* Calculate the checksum of an attribute. */
5919 static void
5920 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5921 struct md5_ctx *ctx, int *mark)
5923 dw_loc_descr_ref loc;
5924 rtx r;
5926 if (AT_class (at) == dw_val_class_die_ref)
5928 dw_die_ref target_die = AT_ref (at);
5930 /* For pointer and reference types, we checksum only the (qualified)
5931 name of the target type (if there is a name). For friend entries,
5932 we checksum only the (qualified) name of the target type or function.
5933 This allows the checksum to remain the same whether the target type
5934 is complete or not. */
5935 if ((at->dw_attr == DW_AT_type
5936 && (tag == DW_TAG_pointer_type
5937 || tag == DW_TAG_reference_type
5938 || tag == DW_TAG_rvalue_reference_type
5939 || tag == DW_TAG_ptr_to_member_type))
5940 || (at->dw_attr == DW_AT_friend
5941 && tag == DW_TAG_friend))
5943 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5945 if (name_attr != NULL)
5947 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5949 if (decl == NULL)
5950 decl = target_die;
5951 CHECKSUM_ULEB128 ('N');
5952 CHECKSUM_ULEB128 (at->dw_attr);
5953 if (decl->die_parent != NULL)
5954 checksum_die_context (decl->die_parent, ctx);
5955 CHECKSUM_ULEB128 ('E');
5956 CHECKSUM_STRING (AT_string (name_attr));
5957 return;
5961 /* For all other references to another DIE, we check to see if the
5962 target DIE has already been visited. If it has, we emit a
5963 backward reference; if not, we descend recursively. */
5964 if (target_die->die_mark > 0)
5966 CHECKSUM_ULEB128 ('R');
5967 CHECKSUM_ULEB128 (at->dw_attr);
5968 CHECKSUM_ULEB128 (target_die->die_mark);
5970 else
5972 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5974 if (decl == NULL)
5975 decl = target_die;
5976 target_die->die_mark = ++(*mark);
5977 CHECKSUM_ULEB128 ('T');
5978 CHECKSUM_ULEB128 (at->dw_attr);
5979 if (decl->die_parent != NULL)
5980 checksum_die_context (decl->die_parent, ctx);
5981 die_checksum_ordered (target_die, ctx, mark);
5983 return;
5986 CHECKSUM_ULEB128 ('A');
5987 CHECKSUM_ULEB128 (at->dw_attr);
5989 switch (AT_class (at))
5991 case dw_val_class_const:
5992 CHECKSUM_ULEB128 (DW_FORM_sdata);
5993 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5994 break;
5996 case dw_val_class_unsigned_const:
5997 CHECKSUM_ULEB128 (DW_FORM_sdata);
5998 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5999 break;
6001 case dw_val_class_const_double:
6002 CHECKSUM_ULEB128 (DW_FORM_block);
6003 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6004 CHECKSUM (at->dw_attr_val.v.val_double);
6005 break;
6007 case dw_val_class_wide_int:
6008 CHECKSUM_ULEB128 (DW_FORM_block);
6009 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
6010 CHECKSUM (*at->dw_attr_val.v.val_wide);
6011 break;
6013 case dw_val_class_vec:
6014 CHECKSUM_ULEB128 (DW_FORM_block);
6015 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6016 * at->dw_attr_val.v.val_vec.elt_size);
6017 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6018 (at->dw_attr_val.v.val_vec.length
6019 * at->dw_attr_val.v.val_vec.elt_size));
6020 break;
6022 case dw_val_class_flag:
6023 CHECKSUM_ULEB128 (DW_FORM_flag);
6024 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6025 break;
6027 case dw_val_class_str:
6028 CHECKSUM_ULEB128 (DW_FORM_string);
6029 CHECKSUM_STRING (AT_string (at));
6030 break;
6032 case dw_val_class_addr:
6033 r = AT_addr (at);
6034 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6035 CHECKSUM_ULEB128 (DW_FORM_string);
6036 CHECKSUM_STRING (XSTR (r, 0));
6037 break;
6039 case dw_val_class_offset:
6040 CHECKSUM_ULEB128 (DW_FORM_sdata);
6041 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6042 break;
6044 case dw_val_class_loc:
6045 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6046 loc_checksum_ordered (loc, ctx);
6047 break;
6049 case dw_val_class_fde_ref:
6050 case dw_val_class_lbl_id:
6051 case dw_val_class_lineptr:
6052 case dw_val_class_macptr:
6053 case dw_val_class_high_pc:
6054 break;
6056 case dw_val_class_file:
6057 CHECKSUM_ULEB128 (DW_FORM_string);
6058 CHECKSUM_STRING (AT_file (at)->filename);
6059 break;
6061 case dw_val_class_data8:
6062 CHECKSUM (at->dw_attr_val.v.val_data8);
6063 break;
6065 default:
6066 break;
6070 struct checksum_attributes
6072 dw_attr_ref at_name;
6073 dw_attr_ref at_type;
6074 dw_attr_ref at_friend;
6075 dw_attr_ref at_accessibility;
6076 dw_attr_ref at_address_class;
6077 dw_attr_ref at_allocated;
6078 dw_attr_ref at_artificial;
6079 dw_attr_ref at_associated;
6080 dw_attr_ref at_binary_scale;
6081 dw_attr_ref at_bit_offset;
6082 dw_attr_ref at_bit_size;
6083 dw_attr_ref at_bit_stride;
6084 dw_attr_ref at_byte_size;
6085 dw_attr_ref at_byte_stride;
6086 dw_attr_ref at_const_value;
6087 dw_attr_ref at_containing_type;
6088 dw_attr_ref at_count;
6089 dw_attr_ref at_data_location;
6090 dw_attr_ref at_data_member_location;
6091 dw_attr_ref at_decimal_scale;
6092 dw_attr_ref at_decimal_sign;
6093 dw_attr_ref at_default_value;
6094 dw_attr_ref at_digit_count;
6095 dw_attr_ref at_discr;
6096 dw_attr_ref at_discr_list;
6097 dw_attr_ref at_discr_value;
6098 dw_attr_ref at_encoding;
6099 dw_attr_ref at_endianity;
6100 dw_attr_ref at_explicit;
6101 dw_attr_ref at_is_optional;
6102 dw_attr_ref at_location;
6103 dw_attr_ref at_lower_bound;
6104 dw_attr_ref at_mutable;
6105 dw_attr_ref at_ordering;
6106 dw_attr_ref at_picture_string;
6107 dw_attr_ref at_prototyped;
6108 dw_attr_ref at_small;
6109 dw_attr_ref at_segment;
6110 dw_attr_ref at_string_length;
6111 dw_attr_ref at_threads_scaled;
6112 dw_attr_ref at_upper_bound;
6113 dw_attr_ref at_use_location;
6114 dw_attr_ref at_use_UTF8;
6115 dw_attr_ref at_variable_parameter;
6116 dw_attr_ref at_virtuality;
6117 dw_attr_ref at_visibility;
6118 dw_attr_ref at_vtable_elem_location;
6121 /* Collect the attributes that we will want to use for the checksum. */
6123 static void
6124 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6126 dw_attr_ref a;
6127 unsigned ix;
6129 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6131 switch (a->dw_attr)
6133 case DW_AT_name:
6134 attrs->at_name = a;
6135 break;
6136 case DW_AT_type:
6137 attrs->at_type = a;
6138 break;
6139 case DW_AT_friend:
6140 attrs->at_friend = a;
6141 break;
6142 case DW_AT_accessibility:
6143 attrs->at_accessibility = a;
6144 break;
6145 case DW_AT_address_class:
6146 attrs->at_address_class = a;
6147 break;
6148 case DW_AT_allocated:
6149 attrs->at_allocated = a;
6150 break;
6151 case DW_AT_artificial:
6152 attrs->at_artificial = a;
6153 break;
6154 case DW_AT_associated:
6155 attrs->at_associated = a;
6156 break;
6157 case DW_AT_binary_scale:
6158 attrs->at_binary_scale = a;
6159 break;
6160 case DW_AT_bit_offset:
6161 attrs->at_bit_offset = a;
6162 break;
6163 case DW_AT_bit_size:
6164 attrs->at_bit_size = a;
6165 break;
6166 case DW_AT_bit_stride:
6167 attrs->at_bit_stride = a;
6168 break;
6169 case DW_AT_byte_size:
6170 attrs->at_byte_size = a;
6171 break;
6172 case DW_AT_byte_stride:
6173 attrs->at_byte_stride = a;
6174 break;
6175 case DW_AT_const_value:
6176 attrs->at_const_value = a;
6177 break;
6178 case DW_AT_containing_type:
6179 attrs->at_containing_type = a;
6180 break;
6181 case DW_AT_count:
6182 attrs->at_count = a;
6183 break;
6184 case DW_AT_data_location:
6185 attrs->at_data_location = a;
6186 break;
6187 case DW_AT_data_member_location:
6188 attrs->at_data_member_location = a;
6189 break;
6190 case DW_AT_decimal_scale:
6191 attrs->at_decimal_scale = a;
6192 break;
6193 case DW_AT_decimal_sign:
6194 attrs->at_decimal_sign = a;
6195 break;
6196 case DW_AT_default_value:
6197 attrs->at_default_value = a;
6198 break;
6199 case DW_AT_digit_count:
6200 attrs->at_digit_count = a;
6201 break;
6202 case DW_AT_discr:
6203 attrs->at_discr = a;
6204 break;
6205 case DW_AT_discr_list:
6206 attrs->at_discr_list = a;
6207 break;
6208 case DW_AT_discr_value:
6209 attrs->at_discr_value = a;
6210 break;
6211 case DW_AT_encoding:
6212 attrs->at_encoding = a;
6213 break;
6214 case DW_AT_endianity:
6215 attrs->at_endianity = a;
6216 break;
6217 case DW_AT_explicit:
6218 attrs->at_explicit = a;
6219 break;
6220 case DW_AT_is_optional:
6221 attrs->at_is_optional = a;
6222 break;
6223 case DW_AT_location:
6224 attrs->at_location = a;
6225 break;
6226 case DW_AT_lower_bound:
6227 attrs->at_lower_bound = a;
6228 break;
6229 case DW_AT_mutable:
6230 attrs->at_mutable = a;
6231 break;
6232 case DW_AT_ordering:
6233 attrs->at_ordering = a;
6234 break;
6235 case DW_AT_picture_string:
6236 attrs->at_picture_string = a;
6237 break;
6238 case DW_AT_prototyped:
6239 attrs->at_prototyped = a;
6240 break;
6241 case DW_AT_small:
6242 attrs->at_small = a;
6243 break;
6244 case DW_AT_segment:
6245 attrs->at_segment = a;
6246 break;
6247 case DW_AT_string_length:
6248 attrs->at_string_length = a;
6249 break;
6250 case DW_AT_threads_scaled:
6251 attrs->at_threads_scaled = a;
6252 break;
6253 case DW_AT_upper_bound:
6254 attrs->at_upper_bound = a;
6255 break;
6256 case DW_AT_use_location:
6257 attrs->at_use_location = a;
6258 break;
6259 case DW_AT_use_UTF8:
6260 attrs->at_use_UTF8 = a;
6261 break;
6262 case DW_AT_variable_parameter:
6263 attrs->at_variable_parameter = a;
6264 break;
6265 case DW_AT_virtuality:
6266 attrs->at_virtuality = a;
6267 break;
6268 case DW_AT_visibility:
6269 attrs->at_visibility = a;
6270 break;
6271 case DW_AT_vtable_elem_location:
6272 attrs->at_vtable_elem_location = a;
6273 break;
6274 default:
6275 break;
6280 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6282 static void
6283 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6285 dw_die_ref c;
6286 dw_die_ref decl;
6287 struct checksum_attributes attrs;
6289 CHECKSUM_ULEB128 ('D');
6290 CHECKSUM_ULEB128 (die->die_tag);
6292 memset (&attrs, 0, sizeof (attrs));
6294 decl = get_AT_ref (die, DW_AT_specification);
6295 if (decl != NULL)
6296 collect_checksum_attributes (&attrs, decl);
6297 collect_checksum_attributes (&attrs, die);
6299 CHECKSUM_ATTR (attrs.at_name);
6300 CHECKSUM_ATTR (attrs.at_accessibility);
6301 CHECKSUM_ATTR (attrs.at_address_class);
6302 CHECKSUM_ATTR (attrs.at_allocated);
6303 CHECKSUM_ATTR (attrs.at_artificial);
6304 CHECKSUM_ATTR (attrs.at_associated);
6305 CHECKSUM_ATTR (attrs.at_binary_scale);
6306 CHECKSUM_ATTR (attrs.at_bit_offset);
6307 CHECKSUM_ATTR (attrs.at_bit_size);
6308 CHECKSUM_ATTR (attrs.at_bit_stride);
6309 CHECKSUM_ATTR (attrs.at_byte_size);
6310 CHECKSUM_ATTR (attrs.at_byte_stride);
6311 CHECKSUM_ATTR (attrs.at_const_value);
6312 CHECKSUM_ATTR (attrs.at_containing_type);
6313 CHECKSUM_ATTR (attrs.at_count);
6314 CHECKSUM_ATTR (attrs.at_data_location);
6315 CHECKSUM_ATTR (attrs.at_data_member_location);
6316 CHECKSUM_ATTR (attrs.at_decimal_scale);
6317 CHECKSUM_ATTR (attrs.at_decimal_sign);
6318 CHECKSUM_ATTR (attrs.at_default_value);
6319 CHECKSUM_ATTR (attrs.at_digit_count);
6320 CHECKSUM_ATTR (attrs.at_discr);
6321 CHECKSUM_ATTR (attrs.at_discr_list);
6322 CHECKSUM_ATTR (attrs.at_discr_value);
6323 CHECKSUM_ATTR (attrs.at_encoding);
6324 CHECKSUM_ATTR (attrs.at_endianity);
6325 CHECKSUM_ATTR (attrs.at_explicit);
6326 CHECKSUM_ATTR (attrs.at_is_optional);
6327 CHECKSUM_ATTR (attrs.at_location);
6328 CHECKSUM_ATTR (attrs.at_lower_bound);
6329 CHECKSUM_ATTR (attrs.at_mutable);
6330 CHECKSUM_ATTR (attrs.at_ordering);
6331 CHECKSUM_ATTR (attrs.at_picture_string);
6332 CHECKSUM_ATTR (attrs.at_prototyped);
6333 CHECKSUM_ATTR (attrs.at_small);
6334 CHECKSUM_ATTR (attrs.at_segment);
6335 CHECKSUM_ATTR (attrs.at_string_length);
6336 CHECKSUM_ATTR (attrs.at_threads_scaled);
6337 CHECKSUM_ATTR (attrs.at_upper_bound);
6338 CHECKSUM_ATTR (attrs.at_use_location);
6339 CHECKSUM_ATTR (attrs.at_use_UTF8);
6340 CHECKSUM_ATTR (attrs.at_variable_parameter);
6341 CHECKSUM_ATTR (attrs.at_virtuality);
6342 CHECKSUM_ATTR (attrs.at_visibility);
6343 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6344 CHECKSUM_ATTR (attrs.at_type);
6345 CHECKSUM_ATTR (attrs.at_friend);
6347 /* Checksum the child DIEs. */
6348 c = die->die_child;
6349 if (c) do {
6350 dw_attr_ref name_attr;
6352 c = c->die_sib;
6353 name_attr = get_AT (c, DW_AT_name);
6354 if (is_template_instantiation (c))
6356 /* Ignore instantiations of member type and function templates. */
6358 else if (name_attr != NULL
6359 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6361 /* Use a shallow checksum for named nested types and member
6362 functions. */
6363 CHECKSUM_ULEB128 ('S');
6364 CHECKSUM_ULEB128 (c->die_tag);
6365 CHECKSUM_STRING (AT_string (name_attr));
6367 else
6369 /* Use a deep checksum for other children. */
6370 /* Mark this DIE so it gets processed when unmarking. */
6371 if (c->die_mark == 0)
6372 c->die_mark = -1;
6373 die_checksum_ordered (c, ctx, mark);
6375 } while (c != die->die_child);
6377 CHECKSUM_ULEB128 (0);
6380 /* Add a type name and tag to a hash. */
6381 static void
6382 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6384 CHECKSUM_ULEB128 (tag);
6385 CHECKSUM_STRING (name);
6388 #undef CHECKSUM
6389 #undef CHECKSUM_STRING
6390 #undef CHECKSUM_ATTR
6391 #undef CHECKSUM_LEB128
6392 #undef CHECKSUM_ULEB128
6394 /* Generate the type signature for DIE. This is computed by generating an
6395 MD5 checksum over the DIE's tag, its relevant attributes, and its
6396 children. Attributes that are references to other DIEs are processed
6397 by recursion, using the MARK field to prevent infinite recursion.
6398 If the DIE is nested inside a namespace or another type, we also
6399 need to include that context in the signature. The lower 64 bits
6400 of the resulting MD5 checksum comprise the signature. */
6402 static void
6403 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6405 int mark;
6406 const char *name;
6407 unsigned char checksum[16];
6408 struct md5_ctx ctx;
6409 dw_die_ref decl;
6410 dw_die_ref parent;
6412 name = get_AT_string (die, DW_AT_name);
6413 decl = get_AT_ref (die, DW_AT_specification);
6414 parent = get_die_parent (die);
6416 /* First, compute a signature for just the type name (and its surrounding
6417 context, if any. This is stored in the type unit DIE for link-time
6418 ODR (one-definition rule) checking. */
6420 if (is_cxx () && name != NULL)
6422 md5_init_ctx (&ctx);
6424 /* Checksum the names of surrounding namespaces and structures. */
6425 if (parent != NULL)
6426 checksum_die_context (parent, &ctx);
6428 /* Checksum the current DIE. */
6429 die_odr_checksum (die->die_tag, name, &ctx);
6430 md5_finish_ctx (&ctx, checksum);
6432 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6435 /* Next, compute the complete type signature. */
6437 md5_init_ctx (&ctx);
6438 mark = 1;
6439 die->die_mark = mark;
6441 /* Checksum the names of surrounding namespaces and structures. */
6442 if (parent != NULL)
6443 checksum_die_context (parent, &ctx);
6445 /* Checksum the DIE and its children. */
6446 die_checksum_ordered (die, &ctx, &mark);
6447 unmark_all_dies (die);
6448 md5_finish_ctx (&ctx, checksum);
6450 /* Store the signature in the type node and link the type DIE and the
6451 type node together. */
6452 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6453 DWARF_TYPE_SIGNATURE_SIZE);
6454 die->comdat_type_p = true;
6455 die->die_id.die_type_node = type_node;
6456 type_node->type_die = die;
6458 /* If the DIE is a specification, link its declaration to the type node
6459 as well. */
6460 if (decl != NULL)
6462 decl->comdat_type_p = true;
6463 decl->die_id.die_type_node = type_node;
6467 /* Do the location expressions look same? */
6468 static inline int
6469 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6471 return loc1->dw_loc_opc == loc2->dw_loc_opc
6472 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6473 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6476 /* Do the values look the same? */
6477 static int
6478 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6480 dw_loc_descr_ref loc1, loc2;
6481 rtx r1, r2;
6483 if (v1->val_class != v2->val_class)
6484 return 0;
6486 switch (v1->val_class)
6488 case dw_val_class_const:
6489 return v1->v.val_int == v2->v.val_int;
6490 case dw_val_class_unsigned_const:
6491 return v1->v.val_unsigned == v2->v.val_unsigned;
6492 case dw_val_class_const_double:
6493 return v1->v.val_double.high == v2->v.val_double.high
6494 && v1->v.val_double.low == v2->v.val_double.low;
6495 case dw_val_class_wide_int:
6496 return *v1->v.val_wide == *v2->v.val_wide;
6497 case dw_val_class_vec:
6498 if (v1->v.val_vec.length != v2->v.val_vec.length
6499 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6500 return 0;
6501 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6502 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6503 return 0;
6504 return 1;
6505 case dw_val_class_flag:
6506 return v1->v.val_flag == v2->v.val_flag;
6507 case dw_val_class_str:
6508 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6510 case dw_val_class_addr:
6511 r1 = v1->v.val_addr;
6512 r2 = v2->v.val_addr;
6513 if (GET_CODE (r1) != GET_CODE (r2))
6514 return 0;
6515 return !rtx_equal_p (r1, r2);
6517 case dw_val_class_offset:
6518 return v1->v.val_offset == v2->v.val_offset;
6520 case dw_val_class_loc:
6521 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6522 loc1 && loc2;
6523 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6524 if (!same_loc_p (loc1, loc2, mark))
6525 return 0;
6526 return !loc1 && !loc2;
6528 case dw_val_class_die_ref:
6529 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6531 case dw_val_class_fde_ref:
6532 case dw_val_class_vms_delta:
6533 case dw_val_class_lbl_id:
6534 case dw_val_class_lineptr:
6535 case dw_val_class_macptr:
6536 case dw_val_class_high_pc:
6537 return 1;
6539 case dw_val_class_file:
6540 return v1->v.val_file == v2->v.val_file;
6542 case dw_val_class_data8:
6543 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6545 default:
6546 return 1;
6550 /* Do the attributes look the same? */
6552 static int
6553 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6555 if (at1->dw_attr != at2->dw_attr)
6556 return 0;
6558 /* We don't care that this was compiled with a different compiler
6559 snapshot; if the output is the same, that's what matters. */
6560 if (at1->dw_attr == DW_AT_producer)
6561 return 1;
6563 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6566 /* Do the dies look the same? */
6568 static int
6569 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6571 dw_die_ref c1, c2;
6572 dw_attr_ref a1;
6573 unsigned ix;
6575 /* To avoid infinite recursion. */
6576 if (die1->die_mark)
6577 return die1->die_mark == die2->die_mark;
6578 die1->die_mark = die2->die_mark = ++(*mark);
6580 if (die1->die_tag != die2->die_tag)
6581 return 0;
6583 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6584 return 0;
6586 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6587 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6588 return 0;
6590 c1 = die1->die_child;
6591 c2 = die2->die_child;
6592 if (! c1)
6594 if (c2)
6595 return 0;
6597 else
6598 for (;;)
6600 if (!same_die_p (c1, c2, mark))
6601 return 0;
6602 c1 = c1->die_sib;
6603 c2 = c2->die_sib;
6604 if (c1 == die1->die_child)
6606 if (c2 == die2->die_child)
6607 break;
6608 else
6609 return 0;
6613 return 1;
6616 /* Do the dies look the same? Wrapper around same_die_p. */
6618 static int
6619 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6621 int mark = 0;
6622 int ret = same_die_p (die1, die2, &mark);
6624 unmark_all_dies (die1);
6625 unmark_all_dies (die2);
6627 return ret;
6630 /* The prefix to attach to symbols on DIEs in the current comdat debug
6631 info section. */
6632 static const char *comdat_symbol_id;
6634 /* The index of the current symbol within the current comdat CU. */
6635 static unsigned int comdat_symbol_number;
6637 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6638 children, and set comdat_symbol_id accordingly. */
6640 static void
6641 compute_section_prefix (dw_die_ref unit_die)
6643 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6644 const char *base = die_name ? lbasename (die_name) : "anonymous";
6645 char *name = XALLOCAVEC (char, strlen (base) + 64);
6646 char *p;
6647 int i, mark;
6648 unsigned char checksum[16];
6649 struct md5_ctx ctx;
6651 /* Compute the checksum of the DIE, then append part of it as hex digits to
6652 the name filename of the unit. */
6654 md5_init_ctx (&ctx);
6655 mark = 0;
6656 die_checksum (unit_die, &ctx, &mark);
6657 unmark_all_dies (unit_die);
6658 md5_finish_ctx (&ctx, checksum);
6660 sprintf (name, "%s.", base);
6661 clean_symbol_name (name);
6663 p = name + strlen (name);
6664 for (i = 0; i < 4; i++)
6666 sprintf (p, "%.2x", checksum[i]);
6667 p += 2;
6670 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6671 comdat_symbol_number = 0;
6674 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6676 static int
6677 is_type_die (dw_die_ref die)
6679 switch (die->die_tag)
6681 case DW_TAG_array_type:
6682 case DW_TAG_class_type:
6683 case DW_TAG_interface_type:
6684 case DW_TAG_enumeration_type:
6685 case DW_TAG_pointer_type:
6686 case DW_TAG_reference_type:
6687 case DW_TAG_rvalue_reference_type:
6688 case DW_TAG_string_type:
6689 case DW_TAG_structure_type:
6690 case DW_TAG_subroutine_type:
6691 case DW_TAG_union_type:
6692 case DW_TAG_ptr_to_member_type:
6693 case DW_TAG_set_type:
6694 case DW_TAG_subrange_type:
6695 case DW_TAG_base_type:
6696 case DW_TAG_const_type:
6697 case DW_TAG_file_type:
6698 case DW_TAG_packed_type:
6699 case DW_TAG_volatile_type:
6700 case DW_TAG_typedef:
6701 return 1;
6702 default:
6703 return 0;
6707 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6708 Basically, we want to choose the bits that are likely to be shared between
6709 compilations (types) and leave out the bits that are specific to individual
6710 compilations (functions). */
6712 static int
6713 is_comdat_die (dw_die_ref c)
6715 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6716 we do for stabs. The advantage is a greater likelihood of sharing between
6717 objects that don't include headers in the same order (and therefore would
6718 put the base types in a different comdat). jason 8/28/00 */
6720 if (c->die_tag == DW_TAG_base_type)
6721 return 0;
6723 if (c->die_tag == DW_TAG_pointer_type
6724 || c->die_tag == DW_TAG_reference_type
6725 || c->die_tag == DW_TAG_rvalue_reference_type
6726 || c->die_tag == DW_TAG_const_type
6727 || c->die_tag == DW_TAG_volatile_type)
6729 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6731 return t ? is_comdat_die (t) : 0;
6734 return is_type_die (c);
6737 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6738 compilation unit. */
6740 static int
6741 is_symbol_die (dw_die_ref c)
6743 return (is_type_die (c)
6744 || is_declaration_die (c)
6745 || c->die_tag == DW_TAG_namespace
6746 || c->die_tag == DW_TAG_module);
6749 /* Returns true iff C is a compile-unit DIE. */
6751 static inline bool
6752 is_cu_die (dw_die_ref c)
6754 return c && c->die_tag == DW_TAG_compile_unit;
6757 /* Returns true iff C is a unit DIE of some sort. */
6759 static inline bool
6760 is_unit_die (dw_die_ref c)
6762 return c && (c->die_tag == DW_TAG_compile_unit
6763 || c->die_tag == DW_TAG_partial_unit
6764 || c->die_tag == DW_TAG_type_unit);
6767 /* Returns true iff C is a namespace DIE. */
6769 static inline bool
6770 is_namespace_die (dw_die_ref c)
6772 return c && c->die_tag == DW_TAG_namespace;
6775 /* Returns true iff C is a class or structure DIE. */
6777 static inline bool
6778 is_class_die (dw_die_ref c)
6780 return c && (c->die_tag == DW_TAG_class_type
6781 || c->die_tag == DW_TAG_structure_type);
6784 /* Return non-zero if this DIE is a template parameter. */
6786 static inline bool
6787 is_template_parameter (dw_die_ref die)
6789 switch (die->die_tag)
6791 case DW_TAG_template_type_param:
6792 case DW_TAG_template_value_param:
6793 case DW_TAG_GNU_template_template_param:
6794 case DW_TAG_GNU_template_parameter_pack:
6795 return true;
6796 default:
6797 return false;
6801 /* Return non-zero if this DIE represents a template instantiation. */
6803 static inline bool
6804 is_template_instantiation (dw_die_ref die)
6806 dw_die_ref c;
6808 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6809 return false;
6810 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6811 return false;
6814 static char *
6815 gen_internal_sym (const char *prefix)
6817 char buf[256];
6819 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6820 return xstrdup (buf);
6823 /* Assign symbols to all worthy DIEs under DIE. */
6825 static void
6826 assign_symbol_names (dw_die_ref die)
6828 dw_die_ref c;
6830 if (is_symbol_die (die) && !die->comdat_type_p)
6832 if (comdat_symbol_id)
6834 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6836 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6837 comdat_symbol_id, comdat_symbol_number++);
6838 die->die_id.die_symbol = xstrdup (p);
6840 else
6841 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6844 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6847 struct cu_hash_table_entry
6849 dw_die_ref cu;
6850 unsigned min_comdat_num, max_comdat_num;
6851 struct cu_hash_table_entry *next;
6854 /* Helpers to manipulate hash table of CUs. */
6856 struct cu_hash_table_entry_hasher
6858 typedef cu_hash_table_entry value_type;
6859 typedef die_struct compare_type;
6860 static inline hashval_t hash (const value_type *);
6861 static inline bool equal (const value_type *, const compare_type *);
6862 static inline void remove (value_type *);
6865 inline hashval_t
6866 cu_hash_table_entry_hasher::hash (const value_type *entry)
6868 return htab_hash_string (entry->cu->die_id.die_symbol);
6871 inline bool
6872 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6873 const compare_type *entry2)
6875 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6878 inline void
6879 cu_hash_table_entry_hasher::remove (value_type *entry)
6881 struct cu_hash_table_entry *next;
6883 while (entry)
6885 next = entry->next;
6886 free (entry);
6887 entry = next;
6891 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6893 /* Check whether we have already seen this CU and set up SYM_NUM
6894 accordingly. */
6895 static int
6896 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6898 struct cu_hash_table_entry dummy;
6899 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6901 dummy.max_comdat_num = 0;
6903 slot = htable->find_slot_with_hash (cu,
6904 htab_hash_string (cu->die_id.die_symbol),
6905 INSERT);
6906 entry = *slot;
6908 for (; entry; last = entry, entry = entry->next)
6910 if (same_die_p_wrap (cu, entry->cu))
6911 break;
6914 if (entry)
6916 *sym_num = entry->min_comdat_num;
6917 return 1;
6920 entry = XCNEW (struct cu_hash_table_entry);
6921 entry->cu = cu;
6922 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6923 entry->next = *slot;
6924 *slot = entry;
6926 return 0;
6929 /* Record SYM_NUM to record of CU in HTABLE. */
6930 static void
6931 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6932 unsigned int sym_num)
6934 struct cu_hash_table_entry **slot, *entry;
6936 slot = htable->find_slot_with_hash (cu,
6937 htab_hash_string (cu->die_id.die_symbol),
6938 NO_INSERT);
6939 entry = *slot;
6941 entry->max_comdat_num = sym_num;
6944 /* Traverse the DIE (which is always comp_unit_die), and set up
6945 additional compilation units for each of the include files we see
6946 bracketed by BINCL/EINCL. */
6948 static void
6949 break_out_includes (dw_die_ref die)
6951 dw_die_ref c;
6952 dw_die_ref unit = NULL;
6953 limbo_die_node *node, **pnode;
6955 c = die->die_child;
6956 if (c) do {
6957 dw_die_ref prev = c;
6958 c = c->die_sib;
6959 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6960 || (unit && is_comdat_die (c)))
6962 dw_die_ref next = c->die_sib;
6964 /* This DIE is for a secondary CU; remove it from the main one. */
6965 remove_child_with_prev (c, prev);
6967 if (c->die_tag == DW_TAG_GNU_BINCL)
6968 unit = push_new_compile_unit (unit, c);
6969 else if (c->die_tag == DW_TAG_GNU_EINCL)
6970 unit = pop_compile_unit (unit);
6971 else
6972 add_child_die (unit, c);
6973 c = next;
6974 if (c == die->die_child)
6975 break;
6977 } while (c != die->die_child);
6979 #if 0
6980 /* We can only use this in debugging, since the frontend doesn't check
6981 to make sure that we leave every include file we enter. */
6982 gcc_assert (!unit);
6983 #endif
6985 assign_symbol_names (die);
6986 cu_hash_type cu_hash_table (10);
6987 for (node = limbo_die_list, pnode = &limbo_die_list;
6988 node;
6989 node = node->next)
6991 int is_dupl;
6993 compute_section_prefix (node->die);
6994 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6995 &comdat_symbol_number);
6996 assign_symbol_names (node->die);
6997 if (is_dupl)
6998 *pnode = node->next;
6999 else
7001 pnode = &node->next;
7002 record_comdat_symbol_number (node->die, &cu_hash_table,
7003 comdat_symbol_number);
7008 /* Return non-zero if this DIE is a declaration. */
7010 static int
7011 is_declaration_die (dw_die_ref die)
7013 dw_attr_ref a;
7014 unsigned ix;
7016 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7017 if (a->dw_attr == DW_AT_declaration)
7018 return 1;
7020 return 0;
7023 /* Return non-zero if this DIE is nested inside a subprogram. */
7025 static int
7026 is_nested_in_subprogram (dw_die_ref die)
7028 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7030 if (decl == NULL)
7031 decl = die;
7032 return local_scope_p (decl);
7035 /* Return non-zero if this DIE contains a defining declaration of a
7036 subprogram. */
7038 static int
7039 contains_subprogram_definition (dw_die_ref die)
7041 dw_die_ref c;
7043 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7044 return 1;
7045 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7046 return 0;
7049 /* Return non-zero if this is a type DIE that should be moved to a
7050 COMDAT .debug_types section. */
7052 static int
7053 should_move_die_to_comdat (dw_die_ref die)
7055 switch (die->die_tag)
7057 case DW_TAG_class_type:
7058 case DW_TAG_structure_type:
7059 case DW_TAG_enumeration_type:
7060 case DW_TAG_union_type:
7061 /* Don't move declarations, inlined instances, types nested in a
7062 subprogram, or types that contain subprogram definitions. */
7063 if (is_declaration_die (die)
7064 || get_AT (die, DW_AT_abstract_origin)
7065 || is_nested_in_subprogram (die)
7066 || contains_subprogram_definition (die))
7067 return 0;
7068 return 1;
7069 case DW_TAG_array_type:
7070 case DW_TAG_interface_type:
7071 case DW_TAG_pointer_type:
7072 case DW_TAG_reference_type:
7073 case DW_TAG_rvalue_reference_type:
7074 case DW_TAG_string_type:
7075 case DW_TAG_subroutine_type:
7076 case DW_TAG_ptr_to_member_type:
7077 case DW_TAG_set_type:
7078 case DW_TAG_subrange_type:
7079 case DW_TAG_base_type:
7080 case DW_TAG_const_type:
7081 case DW_TAG_file_type:
7082 case DW_TAG_packed_type:
7083 case DW_TAG_volatile_type:
7084 case DW_TAG_typedef:
7085 default:
7086 return 0;
7090 /* Make a clone of DIE. */
7092 static dw_die_ref
7093 clone_die (dw_die_ref die)
7095 dw_die_ref clone;
7096 dw_attr_ref a;
7097 unsigned ix;
7099 clone = ggc_cleared_alloc<die_node> ();
7100 clone->die_tag = die->die_tag;
7102 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7103 add_dwarf_attr (clone, a);
7105 return clone;
7108 /* Make a clone of the tree rooted at DIE. */
7110 static dw_die_ref
7111 clone_tree (dw_die_ref die)
7113 dw_die_ref c;
7114 dw_die_ref clone = clone_die (die);
7116 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7118 return clone;
7121 /* Make a clone of DIE as a declaration. */
7123 static dw_die_ref
7124 clone_as_declaration (dw_die_ref die)
7126 dw_die_ref clone;
7127 dw_die_ref decl;
7128 dw_attr_ref a;
7129 unsigned ix;
7131 /* If the DIE is already a declaration, just clone it. */
7132 if (is_declaration_die (die))
7133 return clone_die (die);
7135 /* If the DIE is a specification, just clone its declaration DIE. */
7136 decl = get_AT_ref (die, DW_AT_specification);
7137 if (decl != NULL)
7139 clone = clone_die (decl);
7140 if (die->comdat_type_p)
7141 add_AT_die_ref (clone, DW_AT_signature, die);
7142 return clone;
7145 clone = ggc_cleared_alloc<die_node> ();
7146 clone->die_tag = die->die_tag;
7148 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7150 /* We don't want to copy over all attributes.
7151 For example we don't want DW_AT_byte_size because otherwise we will no
7152 longer have a declaration and GDB will treat it as a definition. */
7154 switch (a->dw_attr)
7156 case DW_AT_abstract_origin:
7157 case DW_AT_artificial:
7158 case DW_AT_containing_type:
7159 case DW_AT_external:
7160 case DW_AT_name:
7161 case DW_AT_type:
7162 case DW_AT_virtuality:
7163 case DW_AT_linkage_name:
7164 case DW_AT_MIPS_linkage_name:
7165 add_dwarf_attr (clone, a);
7166 break;
7167 case DW_AT_byte_size:
7168 default:
7169 break;
7173 if (die->comdat_type_p)
7174 add_AT_die_ref (clone, DW_AT_signature, die);
7176 add_AT_flag (clone, DW_AT_declaration, 1);
7177 return clone;
7181 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7183 struct decl_table_entry
7185 dw_die_ref orig;
7186 dw_die_ref copy;
7189 /* Helpers to manipulate hash table of copied declarations. */
7191 /* Hashtable helpers. */
7193 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7195 typedef decl_table_entry value_type;
7196 typedef die_struct compare_type;
7197 static inline hashval_t hash (const value_type *);
7198 static inline bool equal (const value_type *, const compare_type *);
7201 inline hashval_t
7202 decl_table_entry_hasher::hash (const value_type *entry)
7204 return htab_hash_pointer (entry->orig);
7207 inline bool
7208 decl_table_entry_hasher::equal (const value_type *entry1,
7209 const compare_type *entry2)
7211 return entry1->orig == entry2;
7214 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7216 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7217 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7218 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7219 to check if the ancestor has already been copied into UNIT. */
7221 static dw_die_ref
7222 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7223 decl_hash_type *decl_table)
7225 dw_die_ref parent = die->die_parent;
7226 dw_die_ref new_parent = unit;
7227 dw_die_ref copy;
7228 decl_table_entry **slot = NULL;
7229 struct decl_table_entry *entry = NULL;
7231 if (decl_table)
7233 /* Check if the entry has already been copied to UNIT. */
7234 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7235 INSERT);
7236 if (*slot != HTAB_EMPTY_ENTRY)
7238 entry = *slot;
7239 return entry->copy;
7242 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7243 entry = XCNEW (struct decl_table_entry);
7244 entry->orig = die;
7245 entry->copy = NULL;
7246 *slot = entry;
7249 if (parent != NULL)
7251 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7252 if (spec != NULL)
7253 parent = spec;
7254 if (!is_unit_die (parent))
7255 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7258 copy = clone_as_declaration (die);
7259 add_child_die (new_parent, copy);
7261 if (decl_table)
7263 /* Record the pointer to the copy. */
7264 entry->copy = copy;
7267 return copy;
7269 /* Copy the declaration context to the new type unit DIE. This includes
7270 any surrounding namespace or type declarations. If the DIE has an
7271 AT_specification attribute, it also includes attributes and children
7272 attached to the specification, and returns a pointer to the original
7273 parent of the declaration DIE. Returns NULL otherwise. */
7275 static dw_die_ref
7276 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7278 dw_die_ref decl;
7279 dw_die_ref new_decl;
7280 dw_die_ref orig_parent = NULL;
7282 decl = get_AT_ref (die, DW_AT_specification);
7283 if (decl == NULL)
7284 decl = die;
7285 else
7287 unsigned ix;
7288 dw_die_ref c;
7289 dw_attr_ref a;
7291 /* The original DIE will be changed to a declaration, and must
7292 be moved to be a child of the original declaration DIE. */
7293 orig_parent = decl->die_parent;
7295 /* Copy the type node pointer from the new DIE to the original
7296 declaration DIE so we can forward references later. */
7297 decl->comdat_type_p = true;
7298 decl->die_id.die_type_node = die->die_id.die_type_node;
7300 remove_AT (die, DW_AT_specification);
7302 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7304 if (a->dw_attr != DW_AT_name
7305 && a->dw_attr != DW_AT_declaration
7306 && a->dw_attr != DW_AT_external)
7307 add_dwarf_attr (die, a);
7310 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7313 if (decl->die_parent != NULL
7314 && !is_unit_die (decl->die_parent))
7316 new_decl = copy_ancestor_tree (unit, decl, NULL);
7317 if (new_decl != NULL)
7319 remove_AT (new_decl, DW_AT_signature);
7320 add_AT_specification (die, new_decl);
7324 return orig_parent;
7327 /* Generate the skeleton ancestor tree for the given NODE, then clone
7328 the DIE and add the clone into the tree. */
7330 static void
7331 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7333 if (node->new_die != NULL)
7334 return;
7336 node->new_die = clone_as_declaration (node->old_die);
7338 if (node->parent != NULL)
7340 generate_skeleton_ancestor_tree (node->parent);
7341 add_child_die (node->parent->new_die, node->new_die);
7345 /* Generate a skeleton tree of DIEs containing any declarations that are
7346 found in the original tree. We traverse the tree looking for declaration
7347 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7349 static void
7350 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7352 skeleton_chain_node node;
7353 dw_die_ref c;
7354 dw_die_ref first;
7355 dw_die_ref prev = NULL;
7356 dw_die_ref next = NULL;
7358 node.parent = parent;
7360 first = c = parent->old_die->die_child;
7361 if (c)
7362 next = c->die_sib;
7363 if (c) do {
7364 if (prev == NULL || prev->die_sib == c)
7365 prev = c;
7366 c = next;
7367 next = (c == first ? NULL : c->die_sib);
7368 node.old_die = c;
7369 node.new_die = NULL;
7370 if (is_declaration_die (c))
7372 if (is_template_instantiation (c))
7374 /* Instantiated templates do not need to be cloned into the
7375 type unit. Just move the DIE and its children back to
7376 the skeleton tree (in the main CU). */
7377 remove_child_with_prev (c, prev);
7378 add_child_die (parent->new_die, c);
7379 c = prev;
7381 else
7383 /* Clone the existing DIE, move the original to the skeleton
7384 tree (which is in the main CU), and put the clone, with
7385 all the original's children, where the original came from
7386 (which is about to be moved to the type unit). */
7387 dw_die_ref clone = clone_die (c);
7388 move_all_children (c, clone);
7390 /* If the original has a DW_AT_object_pointer attribute,
7391 it would now point to a child DIE just moved to the
7392 cloned tree, so we need to remove that attribute from
7393 the original. */
7394 remove_AT (c, DW_AT_object_pointer);
7396 replace_child (c, clone, prev);
7397 generate_skeleton_ancestor_tree (parent);
7398 add_child_die (parent->new_die, c);
7399 node.new_die = c;
7400 c = clone;
7403 generate_skeleton_bottom_up (&node);
7404 } while (next != NULL);
7407 /* Wrapper function for generate_skeleton_bottom_up. */
7409 static dw_die_ref
7410 generate_skeleton (dw_die_ref die)
7412 skeleton_chain_node node;
7414 node.old_die = die;
7415 node.new_die = NULL;
7416 node.parent = NULL;
7418 /* If this type definition is nested inside another type,
7419 and is not an instantiation of a template, always leave
7420 at least a declaration in its place. */
7421 if (die->die_parent != NULL
7422 && is_type_die (die->die_parent)
7423 && !is_template_instantiation (die))
7424 node.new_die = clone_as_declaration (die);
7426 generate_skeleton_bottom_up (&node);
7427 return node.new_die;
7430 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7431 declaration. The original DIE is moved to a new compile unit so that
7432 existing references to it follow it to the new location. If any of the
7433 original DIE's descendants is a declaration, we need to replace the
7434 original DIE with a skeleton tree and move the declarations back into the
7435 skeleton tree. */
7437 static dw_die_ref
7438 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7439 dw_die_ref prev)
7441 dw_die_ref skeleton, orig_parent;
7443 /* Copy the declaration context to the type unit DIE. If the returned
7444 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7445 that DIE. */
7446 orig_parent = copy_declaration_context (unit, child);
7448 skeleton = generate_skeleton (child);
7449 if (skeleton == NULL)
7450 remove_child_with_prev (child, prev);
7451 else
7453 skeleton->comdat_type_p = true;
7454 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7456 /* If the original DIE was a specification, we need to put
7457 the skeleton under the parent DIE of the declaration.
7458 This leaves the original declaration in the tree, but
7459 it will be pruned later since there are no longer any
7460 references to it. */
7461 if (orig_parent != NULL)
7463 remove_child_with_prev (child, prev);
7464 add_child_die (orig_parent, skeleton);
7466 else
7467 replace_child (child, skeleton, prev);
7470 return skeleton;
7473 /* Traverse the DIE and set up additional .debug_types sections for each
7474 type worthy of being placed in a COMDAT section. */
7476 static void
7477 break_out_comdat_types (dw_die_ref die)
7479 dw_die_ref c;
7480 dw_die_ref first;
7481 dw_die_ref prev = NULL;
7482 dw_die_ref next = NULL;
7483 dw_die_ref unit = NULL;
7485 first = c = die->die_child;
7486 if (c)
7487 next = c->die_sib;
7488 if (c) do {
7489 if (prev == NULL || prev->die_sib == c)
7490 prev = c;
7491 c = next;
7492 next = (c == first ? NULL : c->die_sib);
7493 if (should_move_die_to_comdat (c))
7495 dw_die_ref replacement;
7496 comdat_type_node_ref type_node;
7498 /* Break out nested types into their own type units. */
7499 break_out_comdat_types (c);
7501 /* Create a new type unit DIE as the root for the new tree, and
7502 add it to the list of comdat types. */
7503 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7504 add_AT_unsigned (unit, DW_AT_language,
7505 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7506 type_node = ggc_cleared_alloc<comdat_type_node> ();
7507 type_node->root_die = unit;
7508 type_node->next = comdat_type_list;
7509 comdat_type_list = type_node;
7511 /* Generate the type signature. */
7512 generate_type_signature (c, type_node);
7514 /* Copy the declaration context, attributes, and children of the
7515 declaration into the new type unit DIE, then remove this DIE
7516 from the main CU (or replace it with a skeleton if necessary). */
7517 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7518 type_node->skeleton_die = replacement;
7520 /* Add the DIE to the new compunit. */
7521 add_child_die (unit, c);
7523 if (replacement != NULL)
7524 c = replacement;
7526 else if (c->die_tag == DW_TAG_namespace
7527 || c->die_tag == DW_TAG_class_type
7528 || c->die_tag == DW_TAG_structure_type
7529 || c->die_tag == DW_TAG_union_type)
7531 /* Look for nested types that can be broken out. */
7532 break_out_comdat_types (c);
7534 } while (next != NULL);
7537 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7538 Enter all the cloned children into the hash table decl_table. */
7540 static dw_die_ref
7541 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7543 dw_die_ref c;
7544 dw_die_ref clone;
7545 struct decl_table_entry *entry;
7546 decl_table_entry **slot;
7548 if (die->die_tag == DW_TAG_subprogram)
7549 clone = clone_as_declaration (die);
7550 else
7551 clone = clone_die (die);
7553 slot = decl_table->find_slot_with_hash (die,
7554 htab_hash_pointer (die), INSERT);
7556 /* Assert that DIE isn't in the hash table yet. If it would be there
7557 before, the ancestors would be necessarily there as well, therefore
7558 clone_tree_partial wouldn't be called. */
7559 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7561 entry = XCNEW (struct decl_table_entry);
7562 entry->orig = die;
7563 entry->copy = clone;
7564 *slot = entry;
7566 if (die->die_tag != DW_TAG_subprogram)
7567 FOR_EACH_CHILD (die, c,
7568 add_child_die (clone, clone_tree_partial (c, decl_table)));
7570 return clone;
7573 /* Walk the DIE and its children, looking for references to incomplete
7574 or trivial types that are unmarked (i.e., that are not in the current
7575 type_unit). */
7577 static void
7578 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7580 dw_die_ref c;
7581 dw_attr_ref a;
7582 unsigned ix;
7584 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7586 if (AT_class (a) == dw_val_class_die_ref)
7588 dw_die_ref targ = AT_ref (a);
7589 decl_table_entry **slot;
7590 struct decl_table_entry *entry;
7592 if (targ->die_mark != 0 || targ->comdat_type_p)
7593 continue;
7595 slot = decl_table->find_slot_with_hash (targ,
7596 htab_hash_pointer (targ),
7597 INSERT);
7599 if (*slot != HTAB_EMPTY_ENTRY)
7601 /* TARG has already been copied, so we just need to
7602 modify the reference to point to the copy. */
7603 entry = *slot;
7604 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7606 else
7608 dw_die_ref parent = unit;
7609 dw_die_ref copy = clone_die (targ);
7611 /* Record in DECL_TABLE that TARG has been copied.
7612 Need to do this now, before the recursive call,
7613 because DECL_TABLE may be expanded and SLOT
7614 would no longer be a valid pointer. */
7615 entry = XCNEW (struct decl_table_entry);
7616 entry->orig = targ;
7617 entry->copy = copy;
7618 *slot = entry;
7620 /* If TARG is not a declaration DIE, we need to copy its
7621 children. */
7622 if (!is_declaration_die (targ))
7624 FOR_EACH_CHILD (
7625 targ, c,
7626 add_child_die (copy,
7627 clone_tree_partial (c, decl_table)));
7630 /* Make sure the cloned tree is marked as part of the
7631 type unit. */
7632 mark_dies (copy);
7634 /* If TARG has surrounding context, copy its ancestor tree
7635 into the new type unit. */
7636 if (targ->die_parent != NULL
7637 && !is_unit_die (targ->die_parent))
7638 parent = copy_ancestor_tree (unit, targ->die_parent,
7639 decl_table);
7641 add_child_die (parent, copy);
7642 a->dw_attr_val.v.val_die_ref.die = copy;
7644 /* Make sure the newly-copied DIE is walked. If it was
7645 installed in a previously-added context, it won't
7646 get visited otherwise. */
7647 if (parent != unit)
7649 /* Find the highest point of the newly-added tree,
7650 mark each node along the way, and walk from there. */
7651 parent->die_mark = 1;
7652 while (parent->die_parent
7653 && parent->die_parent->die_mark == 0)
7655 parent = parent->die_parent;
7656 parent->die_mark = 1;
7658 copy_decls_walk (unit, parent, decl_table);
7664 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7667 /* Copy declarations for "unworthy" types into the new comdat section.
7668 Incomplete types, modified types, and certain other types aren't broken
7669 out into comdat sections of their own, so they don't have a signature,
7670 and we need to copy the declaration into the same section so that we
7671 don't have an external reference. */
7673 static void
7674 copy_decls_for_unworthy_types (dw_die_ref unit)
7676 mark_dies (unit);
7677 decl_hash_type decl_table (10);
7678 copy_decls_walk (unit, unit, &decl_table);
7679 unmark_dies (unit);
7682 /* Traverse the DIE and add a sibling attribute if it may have the
7683 effect of speeding up access to siblings. To save some space,
7684 avoid generating sibling attributes for DIE's without children. */
7686 static void
7687 add_sibling_attributes (dw_die_ref die)
7689 dw_die_ref c;
7691 if (! die->die_child)
7692 return;
7694 if (die->die_parent && die != die->die_parent->die_child)
7695 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7697 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7700 /* Output all location lists for the DIE and its children. */
7702 static void
7703 output_location_lists (dw_die_ref die)
7705 dw_die_ref c;
7706 dw_attr_ref a;
7707 unsigned ix;
7709 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7710 if (AT_class (a) == dw_val_class_loc_list)
7711 output_loc_list (AT_loc_list (a));
7713 FOR_EACH_CHILD (die, c, output_location_lists (c));
7716 /* We want to limit the number of external references, because they are
7717 larger than local references: a relocation takes multiple words, and
7718 even a sig8 reference is always eight bytes, whereas a local reference
7719 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7720 So if we encounter multiple external references to the same type DIE, we
7721 make a local typedef stub for it and redirect all references there.
7723 This is the element of the hash table for keeping track of these
7724 references. */
7726 struct external_ref
7728 dw_die_ref type;
7729 dw_die_ref stub;
7730 unsigned n_refs;
7733 /* Hashtable helpers. */
7735 struct external_ref_hasher : typed_free_remove <external_ref>
7737 typedef external_ref value_type;
7738 typedef external_ref compare_type;
7739 static inline hashval_t hash (const value_type *);
7740 static inline bool equal (const value_type *, const compare_type *);
7743 inline hashval_t
7744 external_ref_hasher::hash (const value_type *r)
7746 dw_die_ref die = r->type;
7747 hashval_t h = 0;
7749 /* We can't use the address of the DIE for hashing, because
7750 that will make the order of the stub DIEs non-deterministic. */
7751 if (! die->comdat_type_p)
7752 /* We have a symbol; use it to compute a hash. */
7753 h = htab_hash_string (die->die_id.die_symbol);
7754 else
7756 /* We have a type signature; use a subset of the bits as the hash.
7757 The 8-byte signature is at least as large as hashval_t. */
7758 comdat_type_node_ref type_node = die->die_id.die_type_node;
7759 memcpy (&h, type_node->signature, sizeof (h));
7761 return h;
7764 inline bool
7765 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7767 return r1->type == r2->type;
7770 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7772 /* Return a pointer to the external_ref for references to DIE. */
7774 static struct external_ref *
7775 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7777 struct external_ref ref, *ref_p;
7778 external_ref **slot;
7780 ref.type = die;
7781 slot = map->find_slot (&ref, INSERT);
7782 if (*slot != HTAB_EMPTY_ENTRY)
7783 return *slot;
7785 ref_p = XCNEW (struct external_ref);
7786 ref_p->type = die;
7787 *slot = ref_p;
7788 return ref_p;
7791 /* Subroutine of optimize_external_refs, below.
7793 If we see a type skeleton, record it as our stub. If we see external
7794 references, remember how many we've seen. */
7796 static void
7797 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7799 dw_die_ref c;
7800 dw_attr_ref a;
7801 unsigned ix;
7802 struct external_ref *ref_p;
7804 if (is_type_die (die)
7805 && (c = get_AT_ref (die, DW_AT_signature)))
7807 /* This is a local skeleton; use it for local references. */
7808 ref_p = lookup_external_ref (map, c);
7809 ref_p->stub = die;
7812 /* Scan the DIE references, and remember any that refer to DIEs from
7813 other CUs (i.e. those which are not marked). */
7814 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7815 if (AT_class (a) == dw_val_class_die_ref
7816 && (c = AT_ref (a))->die_mark == 0
7817 && is_type_die (c))
7819 ref_p = lookup_external_ref (map, c);
7820 ref_p->n_refs++;
7823 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7826 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7827 points to an external_ref, DATA is the CU we're processing. If we don't
7828 already have a local stub, and we have multiple refs, build a stub. */
7831 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7833 struct external_ref *ref_p = *slot;
7835 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7837 /* We have multiple references to this type, so build a small stub.
7838 Both of these forms are a bit dodgy from the perspective of the
7839 DWARF standard, since technically they should have names. */
7840 dw_die_ref cu = data;
7841 dw_die_ref type = ref_p->type;
7842 dw_die_ref stub = NULL;
7844 if (type->comdat_type_p)
7846 /* If we refer to this type via sig8, use AT_signature. */
7847 stub = new_die (type->die_tag, cu, NULL_TREE);
7848 add_AT_die_ref (stub, DW_AT_signature, type);
7850 else
7852 /* Otherwise, use a typedef with no name. */
7853 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7854 add_AT_die_ref (stub, DW_AT_type, type);
7857 stub->die_mark++;
7858 ref_p->stub = stub;
7860 return 1;
7863 /* DIE is a unit; look through all the DIE references to see if there are
7864 any external references to types, and if so, create local stubs for
7865 them which will be applied in build_abbrev_table. This is useful because
7866 references to local DIEs are smaller. */
7868 static external_ref_hash_type *
7869 optimize_external_refs (dw_die_ref die)
7871 external_ref_hash_type *map = new external_ref_hash_type (10);
7872 optimize_external_refs_1 (die, map);
7873 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7874 return map;
7877 /* The format of each DIE (and its attribute value pairs) is encoded in an
7878 abbreviation table. This routine builds the abbreviation table and assigns
7879 a unique abbreviation id for each abbreviation entry. The children of each
7880 die are visited recursively. */
7882 static void
7883 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7885 unsigned long abbrev_id;
7886 unsigned int n_alloc;
7887 dw_die_ref c;
7888 dw_attr_ref a;
7889 unsigned ix;
7891 /* Scan the DIE references, and replace any that refer to
7892 DIEs from other CUs (i.e. those which are not marked) with
7893 the local stubs we built in optimize_external_refs. */
7894 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7895 if (AT_class (a) == dw_val_class_die_ref
7896 && (c = AT_ref (a))->die_mark == 0)
7898 struct external_ref *ref_p;
7899 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7901 ref_p = lookup_external_ref (extern_map, c);
7902 if (ref_p->stub && ref_p->stub != die)
7903 change_AT_die_ref (a, ref_p->stub);
7904 else
7905 /* We aren't changing this reference, so mark it external. */
7906 set_AT_ref_external (a, 1);
7909 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7911 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7912 dw_attr_ref die_a, abbrev_a;
7913 unsigned ix;
7914 bool ok = true;
7916 if (abbrev->die_tag != die->die_tag)
7917 continue;
7918 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7919 continue;
7921 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7922 continue;
7924 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7926 abbrev_a = &(*abbrev->die_attr)[ix];
7927 if ((abbrev_a->dw_attr != die_a->dw_attr)
7928 || (value_format (abbrev_a) != value_format (die_a)))
7930 ok = false;
7931 break;
7934 if (ok)
7935 break;
7938 if (abbrev_id >= abbrev_die_table_in_use)
7940 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7942 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7943 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7944 n_alloc);
7946 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7947 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7948 abbrev_die_table_allocated = n_alloc;
7951 ++abbrev_die_table_in_use;
7952 abbrev_die_table[abbrev_id] = die;
7955 die->die_abbrev = abbrev_id;
7956 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7959 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7961 static int
7962 constant_size (unsigned HOST_WIDE_INT value)
7964 int log;
7966 if (value == 0)
7967 log = 0;
7968 else
7969 log = floor_log2 (value);
7971 log = log / 8;
7972 log = 1 << (floor_log2 (log) + 1);
7974 return log;
7977 /* Return the size of a DIE as it is represented in the
7978 .debug_info section. */
7980 static unsigned long
7981 size_of_die (dw_die_ref die)
7983 unsigned long size = 0;
7984 dw_attr_ref a;
7985 unsigned ix;
7986 enum dwarf_form form;
7988 size += size_of_uleb128 (die->die_abbrev);
7989 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7991 switch (AT_class (a))
7993 case dw_val_class_addr:
7994 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7996 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7997 size += size_of_uleb128 (AT_index (a));
7999 else
8000 size += DWARF2_ADDR_SIZE;
8001 break;
8002 case dw_val_class_offset:
8003 size += DWARF_OFFSET_SIZE;
8004 break;
8005 case dw_val_class_loc:
8007 unsigned long lsize = size_of_locs (AT_loc (a));
8009 /* Block length. */
8010 if (dwarf_version >= 4)
8011 size += size_of_uleb128 (lsize);
8012 else
8013 size += constant_size (lsize);
8014 size += lsize;
8016 break;
8017 case dw_val_class_loc_list:
8018 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8020 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8021 size += size_of_uleb128 (AT_index (a));
8023 else
8024 size += DWARF_OFFSET_SIZE;
8025 break;
8026 case dw_val_class_range_list:
8027 size += DWARF_OFFSET_SIZE;
8028 break;
8029 case dw_val_class_const:
8030 size += size_of_sleb128 (AT_int (a));
8031 break;
8032 case dw_val_class_unsigned_const:
8034 int csize = constant_size (AT_unsigned (a));
8035 if (dwarf_version == 3
8036 && a->dw_attr == DW_AT_data_member_location
8037 && csize >= 4)
8038 size += size_of_uleb128 (AT_unsigned (a));
8039 else
8040 size += csize;
8042 break;
8043 case dw_val_class_const_double:
8044 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8045 if (HOST_BITS_PER_WIDE_INT >= 64)
8046 size++; /* block */
8047 break;
8048 case dw_val_class_wide_int:
8049 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8050 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8051 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8052 > 64)
8053 size++; /* block */
8054 break;
8055 case dw_val_class_vec:
8056 size += constant_size (a->dw_attr_val.v.val_vec.length
8057 * a->dw_attr_val.v.val_vec.elt_size)
8058 + a->dw_attr_val.v.val_vec.length
8059 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8060 break;
8061 case dw_val_class_flag:
8062 if (dwarf_version >= 4)
8063 /* Currently all add_AT_flag calls pass in 1 as last argument,
8064 so DW_FORM_flag_present can be used. If that ever changes,
8065 we'll need to use DW_FORM_flag and have some optimization
8066 in build_abbrev_table that will change those to
8067 DW_FORM_flag_present if it is set to 1 in all DIEs using
8068 the same abbrev entry. */
8069 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8070 else
8071 size += 1;
8072 break;
8073 case dw_val_class_die_ref:
8074 if (AT_ref_external (a))
8076 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8077 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8078 is sized by target address length, whereas in DWARF3
8079 it's always sized as an offset. */
8080 if (use_debug_types)
8081 size += DWARF_TYPE_SIGNATURE_SIZE;
8082 else if (dwarf_version == 2)
8083 size += DWARF2_ADDR_SIZE;
8084 else
8085 size += DWARF_OFFSET_SIZE;
8087 else
8088 size += DWARF_OFFSET_SIZE;
8089 break;
8090 case dw_val_class_fde_ref:
8091 size += DWARF_OFFSET_SIZE;
8092 break;
8093 case dw_val_class_lbl_id:
8094 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8096 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8097 size += size_of_uleb128 (AT_index (a));
8099 else
8100 size += DWARF2_ADDR_SIZE;
8101 break;
8102 case dw_val_class_lineptr:
8103 case dw_val_class_macptr:
8104 size += DWARF_OFFSET_SIZE;
8105 break;
8106 case dw_val_class_str:
8107 form = AT_string_form (a);
8108 if (form == DW_FORM_strp)
8109 size += DWARF_OFFSET_SIZE;
8110 else if (form == DW_FORM_GNU_str_index)
8111 size += size_of_uleb128 (AT_index (a));
8112 else
8113 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8114 break;
8115 case dw_val_class_file:
8116 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8117 break;
8118 case dw_val_class_data8:
8119 size += 8;
8120 break;
8121 case dw_val_class_vms_delta:
8122 size += DWARF_OFFSET_SIZE;
8123 break;
8124 case dw_val_class_high_pc:
8125 size += DWARF2_ADDR_SIZE;
8126 break;
8127 default:
8128 gcc_unreachable ();
8132 return size;
8135 /* Size the debugging information associated with a given DIE. Visits the
8136 DIE's children recursively. Updates the global variable next_die_offset, on
8137 each time through. Uses the current value of next_die_offset to update the
8138 die_offset field in each DIE. */
8140 static void
8141 calc_die_sizes (dw_die_ref die)
8143 dw_die_ref c;
8145 gcc_assert (die->die_offset == 0
8146 || (unsigned long int) die->die_offset == next_die_offset);
8147 die->die_offset = next_die_offset;
8148 next_die_offset += size_of_die (die);
8150 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8152 if (die->die_child != NULL)
8153 /* Count the null byte used to terminate sibling lists. */
8154 next_die_offset += 1;
8157 /* Size just the base type children at the start of the CU.
8158 This is needed because build_abbrev needs to size locs
8159 and sizing of type based stack ops needs to know die_offset
8160 values for the base types. */
8162 static void
8163 calc_base_type_die_sizes (void)
8165 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8166 unsigned int i;
8167 dw_die_ref base_type;
8168 #if ENABLE_ASSERT_CHECKING
8169 dw_die_ref prev = comp_unit_die ()->die_child;
8170 #endif
8172 die_offset += size_of_die (comp_unit_die ());
8173 for (i = 0; base_types.iterate (i, &base_type); i++)
8175 #if ENABLE_ASSERT_CHECKING
8176 gcc_assert (base_type->die_offset == 0
8177 && prev->die_sib == base_type
8178 && base_type->die_child == NULL
8179 && base_type->die_abbrev);
8180 prev = base_type;
8181 #endif
8182 base_type->die_offset = die_offset;
8183 die_offset += size_of_die (base_type);
8187 /* Set the marks for a die and its children. We do this so
8188 that we know whether or not a reference needs to use FORM_ref_addr; only
8189 DIEs in the same CU will be marked. We used to clear out the offset
8190 and use that as the flag, but ran into ordering problems. */
8192 static void
8193 mark_dies (dw_die_ref die)
8195 dw_die_ref c;
8197 gcc_assert (!die->die_mark);
8199 die->die_mark = 1;
8200 FOR_EACH_CHILD (die, c, mark_dies (c));
8203 /* Clear the marks for a die and its children. */
8205 static void
8206 unmark_dies (dw_die_ref die)
8208 dw_die_ref c;
8210 if (! use_debug_types)
8211 gcc_assert (die->die_mark);
8213 die->die_mark = 0;
8214 FOR_EACH_CHILD (die, c, unmark_dies (c));
8217 /* Clear the marks for a die, its children and referred dies. */
8219 static void
8220 unmark_all_dies (dw_die_ref die)
8222 dw_die_ref c;
8223 dw_attr_ref a;
8224 unsigned ix;
8226 if (!die->die_mark)
8227 return;
8228 die->die_mark = 0;
8230 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8232 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8233 if (AT_class (a) == dw_val_class_die_ref)
8234 unmark_all_dies (AT_ref (a));
8237 /* Calculate if the entry should appear in the final output file. It may be
8238 from a pruned a type. */
8240 static bool
8241 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8243 /* By limiting gnu pubnames to definitions only, gold can generate a
8244 gdb index without entries for declarations, which don't include
8245 enough information to be useful. */
8246 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8247 return false;
8249 if (table == pubname_table)
8251 /* Enumerator names are part of the pubname table, but the
8252 parent DW_TAG_enumeration_type die may have been pruned.
8253 Don't output them if that is the case. */
8254 if (p->die->die_tag == DW_TAG_enumerator &&
8255 (p->die->die_parent == NULL
8256 || !p->die->die_parent->die_perennial_p))
8257 return false;
8259 /* Everything else in the pubname table is included. */
8260 return true;
8263 /* The pubtypes table shouldn't include types that have been
8264 pruned. */
8265 return (p->die->die_offset != 0
8266 || !flag_eliminate_unused_debug_types);
8269 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8270 generated for the compilation unit. */
8272 static unsigned long
8273 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8275 unsigned long size;
8276 unsigned i;
8277 pubname_ref p;
8278 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8280 size = DWARF_PUBNAMES_HEADER_SIZE;
8281 FOR_EACH_VEC_ELT (*names, i, p)
8282 if (include_pubname_in_output (names, p))
8283 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8285 size += DWARF_OFFSET_SIZE;
8286 return size;
8289 /* Return the size of the information in the .debug_aranges section. */
8291 static unsigned long
8292 size_of_aranges (void)
8294 unsigned long size;
8296 size = DWARF_ARANGES_HEADER_SIZE;
8298 /* Count the address/length pair for this compilation unit. */
8299 if (text_section_used)
8300 size += 2 * DWARF2_ADDR_SIZE;
8301 if (cold_text_section_used)
8302 size += 2 * DWARF2_ADDR_SIZE;
8303 if (have_multiple_function_sections)
8305 unsigned fde_idx;
8306 dw_fde_ref fde;
8308 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8310 if (DECL_IGNORED_P (fde->decl))
8311 continue;
8312 if (!fde->in_std_section)
8313 size += 2 * DWARF2_ADDR_SIZE;
8314 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8315 size += 2 * DWARF2_ADDR_SIZE;
8319 /* Count the two zero words used to terminated the address range table. */
8320 size += 2 * DWARF2_ADDR_SIZE;
8321 return size;
8324 /* Select the encoding of an attribute value. */
8326 static enum dwarf_form
8327 value_format (dw_attr_ref a)
8329 switch (AT_class (a))
8331 case dw_val_class_addr:
8332 /* Only very few attributes allow DW_FORM_addr. */
8333 switch (a->dw_attr)
8335 case DW_AT_low_pc:
8336 case DW_AT_high_pc:
8337 case DW_AT_entry_pc:
8338 case DW_AT_trampoline:
8339 return (AT_index (a) == NOT_INDEXED
8340 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8341 default:
8342 break;
8344 switch (DWARF2_ADDR_SIZE)
8346 case 1:
8347 return DW_FORM_data1;
8348 case 2:
8349 return DW_FORM_data2;
8350 case 4:
8351 return DW_FORM_data4;
8352 case 8:
8353 return DW_FORM_data8;
8354 default:
8355 gcc_unreachable ();
8357 case dw_val_class_range_list:
8358 case dw_val_class_loc_list:
8359 if (dwarf_version >= 4)
8360 return DW_FORM_sec_offset;
8361 /* FALLTHRU */
8362 case dw_val_class_vms_delta:
8363 case dw_val_class_offset:
8364 switch (DWARF_OFFSET_SIZE)
8366 case 4:
8367 return DW_FORM_data4;
8368 case 8:
8369 return DW_FORM_data8;
8370 default:
8371 gcc_unreachable ();
8373 case dw_val_class_loc:
8374 if (dwarf_version >= 4)
8375 return DW_FORM_exprloc;
8376 switch (constant_size (size_of_locs (AT_loc (a))))
8378 case 1:
8379 return DW_FORM_block1;
8380 case 2:
8381 return DW_FORM_block2;
8382 case 4:
8383 return DW_FORM_block4;
8384 default:
8385 gcc_unreachable ();
8387 case dw_val_class_const:
8388 return DW_FORM_sdata;
8389 case dw_val_class_unsigned_const:
8390 switch (constant_size (AT_unsigned (a)))
8392 case 1:
8393 return DW_FORM_data1;
8394 case 2:
8395 return DW_FORM_data2;
8396 case 4:
8397 /* In DWARF3 DW_AT_data_member_location with
8398 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8399 constant, so we need to use DW_FORM_udata if we need
8400 a large constant. */
8401 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8402 return DW_FORM_udata;
8403 return DW_FORM_data4;
8404 case 8:
8405 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8406 return DW_FORM_udata;
8407 return DW_FORM_data8;
8408 default:
8409 gcc_unreachable ();
8411 case dw_val_class_const_double:
8412 switch (HOST_BITS_PER_WIDE_INT)
8414 case 8:
8415 return DW_FORM_data2;
8416 case 16:
8417 return DW_FORM_data4;
8418 case 32:
8419 return DW_FORM_data8;
8420 case 64:
8421 default:
8422 return DW_FORM_block1;
8424 case dw_val_class_wide_int:
8425 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8427 case 8:
8428 return DW_FORM_data1;
8429 case 16:
8430 return DW_FORM_data2;
8431 case 32:
8432 return DW_FORM_data4;
8433 case 64:
8434 return DW_FORM_data8;
8435 default:
8436 return DW_FORM_block1;
8438 case dw_val_class_vec:
8439 switch (constant_size (a->dw_attr_val.v.val_vec.length
8440 * a->dw_attr_val.v.val_vec.elt_size))
8442 case 1:
8443 return DW_FORM_block1;
8444 case 2:
8445 return DW_FORM_block2;
8446 case 4:
8447 return DW_FORM_block4;
8448 default:
8449 gcc_unreachable ();
8451 case dw_val_class_flag:
8452 if (dwarf_version >= 4)
8454 /* Currently all add_AT_flag calls pass in 1 as last argument,
8455 so DW_FORM_flag_present can be used. If that ever changes,
8456 we'll need to use DW_FORM_flag and have some optimization
8457 in build_abbrev_table that will change those to
8458 DW_FORM_flag_present if it is set to 1 in all DIEs using
8459 the same abbrev entry. */
8460 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8461 return DW_FORM_flag_present;
8463 return DW_FORM_flag;
8464 case dw_val_class_die_ref:
8465 if (AT_ref_external (a))
8466 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8467 else
8468 return DW_FORM_ref;
8469 case dw_val_class_fde_ref:
8470 return DW_FORM_data;
8471 case dw_val_class_lbl_id:
8472 return (AT_index (a) == NOT_INDEXED
8473 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8474 case dw_val_class_lineptr:
8475 case dw_val_class_macptr:
8476 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8477 case dw_val_class_str:
8478 return AT_string_form (a);
8479 case dw_val_class_file:
8480 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8482 case 1:
8483 return DW_FORM_data1;
8484 case 2:
8485 return DW_FORM_data2;
8486 case 4:
8487 return DW_FORM_data4;
8488 default:
8489 gcc_unreachable ();
8492 case dw_val_class_data8:
8493 return DW_FORM_data8;
8495 case dw_val_class_high_pc:
8496 switch (DWARF2_ADDR_SIZE)
8498 case 1:
8499 return DW_FORM_data1;
8500 case 2:
8501 return DW_FORM_data2;
8502 case 4:
8503 return DW_FORM_data4;
8504 case 8:
8505 return DW_FORM_data8;
8506 default:
8507 gcc_unreachable ();
8510 default:
8511 gcc_unreachable ();
8515 /* Output the encoding of an attribute value. */
8517 static void
8518 output_value_format (dw_attr_ref a)
8520 enum dwarf_form form = value_format (a);
8522 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8525 /* Given a die and id, produce the appropriate abbreviations. */
8527 static void
8528 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8530 unsigned ix;
8531 dw_attr_ref a_attr;
8533 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8534 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8535 dwarf_tag_name (abbrev->die_tag));
8537 if (abbrev->die_child != NULL)
8538 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8539 else
8540 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8542 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8544 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8545 dwarf_attr_name (a_attr->dw_attr));
8546 output_value_format (a_attr);
8549 dw2_asm_output_data (1, 0, NULL);
8550 dw2_asm_output_data (1, 0, NULL);
8554 /* Output the .debug_abbrev section which defines the DIE abbreviation
8555 table. */
8557 static void
8558 output_abbrev_section (void)
8560 unsigned long abbrev_id;
8562 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8563 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8565 /* Terminate the table. */
8566 dw2_asm_output_data (1, 0, NULL);
8569 /* Output a symbol we can use to refer to this DIE from another CU. */
8571 static inline void
8572 output_die_symbol (dw_die_ref die)
8574 const char *sym = die->die_id.die_symbol;
8576 gcc_assert (!die->comdat_type_p);
8578 if (sym == 0)
8579 return;
8581 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8582 /* We make these global, not weak; if the target doesn't support
8583 .linkonce, it doesn't support combining the sections, so debugging
8584 will break. */
8585 targetm.asm_out.globalize_label (asm_out_file, sym);
8587 ASM_OUTPUT_LABEL (asm_out_file, sym);
8590 /* Return a new location list, given the begin and end range, and the
8591 expression. */
8593 static inline dw_loc_list_ref
8594 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8595 const char *section)
8597 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8599 retlist->begin = begin;
8600 retlist->begin_entry = NULL;
8601 retlist->end = end;
8602 retlist->expr = expr;
8603 retlist->section = section;
8605 return retlist;
8608 /* Generate a new internal symbol for this location list node, if it
8609 hasn't got one yet. */
8611 static inline void
8612 gen_llsym (dw_loc_list_ref list)
8614 gcc_assert (!list->ll_symbol);
8615 list->ll_symbol = gen_internal_sym ("LLST");
8618 /* Output the location list given to us. */
8620 static void
8621 output_loc_list (dw_loc_list_ref list_head)
8623 dw_loc_list_ref curr = list_head;
8625 if (list_head->emitted)
8626 return;
8627 list_head->emitted = true;
8629 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8631 /* Walk the location list, and output each range + expression. */
8632 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8634 unsigned long size;
8635 /* Don't output an entry that starts and ends at the same address. */
8636 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8637 continue;
8638 size = size_of_locs (curr->expr);
8639 /* If the expression is too large, drop it on the floor. We could
8640 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8641 in the expression, but >= 64KB expressions for a single value
8642 in a single range are unlikely very useful. */
8643 if (size > 0xffff)
8644 continue;
8645 if (dwarf_split_debug_info)
8647 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8648 "Location list start/length entry (%s)",
8649 list_head->ll_symbol);
8650 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8651 "Location list range start index (%s)",
8652 curr->begin);
8653 /* The length field is 4 bytes. If we ever need to support
8654 an 8-byte length, we can add a new DW_LLE code or fall back
8655 to DW_LLE_GNU_start_end_entry. */
8656 dw2_asm_output_delta (4, curr->end, curr->begin,
8657 "Location list range length (%s)",
8658 list_head->ll_symbol);
8660 else if (!have_multiple_function_sections)
8662 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8663 "Location list begin address (%s)",
8664 list_head->ll_symbol);
8665 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8666 "Location list end address (%s)",
8667 list_head->ll_symbol);
8669 else
8671 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8672 "Location list begin address (%s)",
8673 list_head->ll_symbol);
8674 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8675 "Location list end address (%s)",
8676 list_head->ll_symbol);
8679 /* Output the block length for this list of location operations. */
8680 gcc_assert (size <= 0xffff);
8681 dw2_asm_output_data (2, size, "%s", "Location expression size");
8683 output_loc_sequence (curr->expr, -1);
8686 if (dwarf_split_debug_info)
8687 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8688 "Location list terminator (%s)",
8689 list_head->ll_symbol);
8690 else
8692 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8693 "Location list terminator begin (%s)",
8694 list_head->ll_symbol);
8695 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8696 "Location list terminator end (%s)",
8697 list_head->ll_symbol);
8701 /* Output a range_list offset into the debug_range section. Emit a
8702 relocated reference if val_entry is NULL, otherwise, emit an
8703 indirect reference. */
8705 static void
8706 output_range_list_offset (dw_attr_ref a)
8708 const char *name = dwarf_attr_name (a->dw_attr);
8710 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8712 char *p = strchr (ranges_section_label, '\0');
8713 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8714 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8715 debug_ranges_section, "%s", name);
8716 *p = '\0';
8718 else
8719 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8720 "%s (offset from %s)", name, ranges_section_label);
8723 /* Output the offset into the debug_loc section. */
8725 static void
8726 output_loc_list_offset (dw_attr_ref a)
8728 char *sym = AT_loc_list (a)->ll_symbol;
8730 gcc_assert (sym);
8731 if (dwarf_split_debug_info)
8732 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8733 "%s", dwarf_attr_name (a->dw_attr));
8734 else
8735 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8736 "%s", dwarf_attr_name (a->dw_attr));
8739 /* Output an attribute's index or value appropriately. */
8741 static void
8742 output_attr_index_or_value (dw_attr_ref a)
8744 const char *name = dwarf_attr_name (a->dw_attr);
8746 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8748 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8749 return;
8751 switch (AT_class (a))
8753 case dw_val_class_addr:
8754 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8755 break;
8756 case dw_val_class_high_pc:
8757 case dw_val_class_lbl_id:
8758 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8759 break;
8760 case dw_val_class_loc_list:
8761 output_loc_list_offset (a);
8762 break;
8763 default:
8764 gcc_unreachable ();
8768 /* Output a type signature. */
8770 static inline void
8771 output_signature (const char *sig, const char *name)
8773 int i;
8775 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8776 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8779 /* Output the DIE and its attributes. Called recursively to generate
8780 the definitions of each child DIE. */
8782 static void
8783 output_die (dw_die_ref die)
8785 dw_attr_ref a;
8786 dw_die_ref c;
8787 unsigned long size;
8788 unsigned ix;
8790 /* If someone in another CU might refer to us, set up a symbol for
8791 them to point to. */
8792 if (! die->comdat_type_p && die->die_id.die_symbol)
8793 output_die_symbol (die);
8795 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8796 (unsigned long)die->die_offset,
8797 dwarf_tag_name (die->die_tag));
8799 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8801 const char *name = dwarf_attr_name (a->dw_attr);
8803 switch (AT_class (a))
8805 case dw_val_class_addr:
8806 output_attr_index_or_value (a);
8807 break;
8809 case dw_val_class_offset:
8810 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8811 "%s", name);
8812 break;
8814 case dw_val_class_range_list:
8815 output_range_list_offset (a);
8816 break;
8818 case dw_val_class_loc:
8819 size = size_of_locs (AT_loc (a));
8821 /* Output the block length for this list of location operations. */
8822 if (dwarf_version >= 4)
8823 dw2_asm_output_data_uleb128 (size, "%s", name);
8824 else
8825 dw2_asm_output_data (constant_size (size), size, "%s", name);
8827 output_loc_sequence (AT_loc (a), -1);
8828 break;
8830 case dw_val_class_const:
8831 /* ??? It would be slightly more efficient to use a scheme like is
8832 used for unsigned constants below, but gdb 4.x does not sign
8833 extend. Gdb 5.x does sign extend. */
8834 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8835 break;
8837 case dw_val_class_unsigned_const:
8839 int csize = constant_size (AT_unsigned (a));
8840 if (dwarf_version == 3
8841 && a->dw_attr == DW_AT_data_member_location
8842 && csize >= 4)
8843 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8844 else
8845 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8847 break;
8849 case dw_val_class_const_double:
8851 unsigned HOST_WIDE_INT first, second;
8853 if (HOST_BITS_PER_WIDE_INT >= 64)
8854 dw2_asm_output_data (1,
8855 HOST_BITS_PER_DOUBLE_INT
8856 / HOST_BITS_PER_CHAR,
8857 NULL);
8859 if (WORDS_BIG_ENDIAN)
8861 first = a->dw_attr_val.v.val_double.high;
8862 second = a->dw_attr_val.v.val_double.low;
8864 else
8866 first = a->dw_attr_val.v.val_double.low;
8867 second = a->dw_attr_val.v.val_double.high;
8870 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8871 first, "%s", name);
8872 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8873 second, NULL);
8875 break;
8877 case dw_val_class_wide_int:
8879 int i;
8880 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8881 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8882 if (len * HOST_BITS_PER_WIDE_INT > 64)
8883 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8884 NULL);
8886 if (WORDS_BIG_ENDIAN)
8887 for (i = len - 1; i >= 0; --i)
8889 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8890 name);
8891 name = NULL;
8893 else
8894 for (i = 0; i < len; ++i)
8896 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8897 name);
8898 name = NULL;
8901 break;
8903 case dw_val_class_vec:
8905 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8906 unsigned int len = a->dw_attr_val.v.val_vec.length;
8907 unsigned int i;
8908 unsigned char *p;
8910 dw2_asm_output_data (constant_size (len * elt_size),
8911 len * elt_size, "%s", name);
8912 if (elt_size > sizeof (HOST_WIDE_INT))
8914 elt_size /= 2;
8915 len *= 2;
8917 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8918 i < len;
8919 i++, p += elt_size)
8920 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8921 "fp or vector constant word %u", i);
8922 break;
8925 case dw_val_class_flag:
8926 if (dwarf_version >= 4)
8928 /* Currently all add_AT_flag calls pass in 1 as last argument,
8929 so DW_FORM_flag_present can be used. If that ever changes,
8930 we'll need to use DW_FORM_flag and have some optimization
8931 in build_abbrev_table that will change those to
8932 DW_FORM_flag_present if it is set to 1 in all DIEs using
8933 the same abbrev entry. */
8934 gcc_assert (AT_flag (a) == 1);
8935 if (flag_debug_asm)
8936 fprintf (asm_out_file, "\t\t\t%s %s\n",
8937 ASM_COMMENT_START, name);
8938 break;
8940 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8941 break;
8943 case dw_val_class_loc_list:
8944 output_attr_index_or_value (a);
8945 break;
8947 case dw_val_class_die_ref:
8948 if (AT_ref_external (a))
8950 if (AT_ref (a)->comdat_type_p)
8952 comdat_type_node_ref type_node =
8953 AT_ref (a)->die_id.die_type_node;
8955 gcc_assert (type_node);
8956 output_signature (type_node->signature, name);
8958 else
8960 const char *sym = AT_ref (a)->die_id.die_symbol;
8961 int size;
8963 gcc_assert (sym);
8964 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8965 length, whereas in DWARF3 it's always sized as an
8966 offset. */
8967 if (dwarf_version == 2)
8968 size = DWARF2_ADDR_SIZE;
8969 else
8970 size = DWARF_OFFSET_SIZE;
8971 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8972 name);
8975 else
8977 gcc_assert (AT_ref (a)->die_offset);
8978 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8979 "%s", name);
8981 break;
8983 case dw_val_class_fde_ref:
8985 char l1[20];
8987 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8988 a->dw_attr_val.v.val_fde_index * 2);
8989 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8990 "%s", name);
8992 break;
8994 case dw_val_class_vms_delta:
8995 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8996 AT_vms_delta2 (a), AT_vms_delta1 (a),
8997 "%s", name);
8998 break;
9000 case dw_val_class_lbl_id:
9001 output_attr_index_or_value (a);
9002 break;
9004 case dw_val_class_lineptr:
9005 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9006 debug_line_section, "%s", name);
9007 break;
9009 case dw_val_class_macptr:
9010 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9011 debug_macinfo_section, "%s", name);
9012 break;
9014 case dw_val_class_str:
9015 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9016 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9017 a->dw_attr_val.v.val_str->label,
9018 debug_str_section,
9019 "%s: \"%s\"", name, AT_string (a));
9020 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9021 dw2_asm_output_data_uleb128 (AT_index (a),
9022 "%s: \"%s\"", name, AT_string (a));
9023 else
9024 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9025 break;
9027 case dw_val_class_file:
9029 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9031 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9032 a->dw_attr_val.v.val_file->filename);
9033 break;
9036 case dw_val_class_data8:
9038 int i;
9040 for (i = 0; i < 8; i++)
9041 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9042 i == 0 ? "%s" : NULL, name);
9043 break;
9046 case dw_val_class_high_pc:
9047 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9048 get_AT_low_pc (die), "DW_AT_high_pc");
9049 break;
9051 default:
9052 gcc_unreachable ();
9056 FOR_EACH_CHILD (die, c, output_die (c));
9058 /* Add null byte to terminate sibling list. */
9059 if (die->die_child != NULL)
9060 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9061 (unsigned long) die->die_offset);
9064 /* Output the compilation unit that appears at the beginning of the
9065 .debug_info section, and precedes the DIE descriptions. */
9067 static void
9068 output_compilation_unit_header (void)
9070 /* We don't support actual DWARFv5 units yet, we just use some
9071 DWARFv5 draft DIE tags in DWARFv4 format. */
9072 int ver = dwarf_version < 5 ? dwarf_version : 4;
9074 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9075 dw2_asm_output_data (4, 0xffffffff,
9076 "Initial length escape value indicating 64-bit DWARF extension");
9077 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9078 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9079 "Length of Compilation Unit Info");
9080 dw2_asm_output_data (2, ver, "DWARF version number");
9081 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9082 debug_abbrev_section,
9083 "Offset Into Abbrev. Section");
9084 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9087 /* Output the compilation unit DIE and its children. */
9089 static void
9090 output_comp_unit (dw_die_ref die, int output_if_empty)
9092 const char *secname, *oldsym;
9093 char *tmp;
9095 /* Unless we are outputting main CU, we may throw away empty ones. */
9096 if (!output_if_empty && die->die_child == NULL)
9097 return;
9099 /* Even if there are no children of this DIE, we must output the information
9100 about the compilation unit. Otherwise, on an empty translation unit, we
9101 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9102 will then complain when examining the file. First mark all the DIEs in
9103 this CU so we know which get local refs. */
9104 mark_dies (die);
9106 external_ref_hash_type *extern_map = optimize_external_refs (die);
9108 build_abbrev_table (die, extern_map);
9110 delete extern_map;
9112 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9113 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9114 calc_die_sizes (die);
9116 oldsym = die->die_id.die_symbol;
9117 if (oldsym)
9119 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9121 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9122 secname = tmp;
9123 die->die_id.die_symbol = NULL;
9124 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9126 else
9128 switch_to_section (debug_info_section);
9129 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9130 info_section_emitted = true;
9133 /* Output debugging information. */
9134 output_compilation_unit_header ();
9135 output_die (die);
9137 /* Leave the marks on the main CU, so we can check them in
9138 output_pubnames. */
9139 if (oldsym)
9141 unmark_dies (die);
9142 die->die_id.die_symbol = oldsym;
9146 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9147 and .debug_pubtypes. This is configured per-target, but can be
9148 overridden by the -gpubnames or -gno-pubnames options. */
9150 static inline bool
9151 want_pubnames (void)
9153 if (debug_info_level <= DINFO_LEVEL_TERSE)
9154 return false;
9155 if (debug_generate_pub_sections != -1)
9156 return debug_generate_pub_sections;
9157 return targetm.want_debug_pub_sections;
9160 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9162 static void
9163 add_AT_pubnames (dw_die_ref die)
9165 if (want_pubnames ())
9166 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9169 /* Add a string attribute value to a skeleton DIE. */
9171 static inline void
9172 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9173 const char *str)
9175 dw_attr_node attr;
9176 struct indirect_string_node *node;
9178 if (! skeleton_debug_str_hash)
9179 skeleton_debug_str_hash
9180 = hash_table<indirect_string_hasher>::create_ggc (10);
9182 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9183 find_string_form (node);
9184 if (node->form == DW_FORM_GNU_str_index)
9185 node->form = DW_FORM_strp;
9187 attr.dw_attr = attr_kind;
9188 attr.dw_attr_val.val_class = dw_val_class_str;
9189 attr.dw_attr_val.val_entry = NULL;
9190 attr.dw_attr_val.v.val_str = node;
9191 add_dwarf_attr (die, &attr);
9194 /* Helper function to generate top-level dies for skeleton debug_info and
9195 debug_types. */
9197 static void
9198 add_top_level_skeleton_die_attrs (dw_die_ref die)
9200 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9201 const char *comp_dir = comp_dir_string ();
9203 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9204 if (comp_dir != NULL)
9205 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9206 add_AT_pubnames (die);
9207 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9210 /* Output skeleton debug sections that point to the dwo file. */
9212 static void
9213 output_skeleton_debug_sections (dw_die_ref comp_unit)
9215 /* We don't support actual DWARFv5 units yet, we just use some
9216 DWARFv5 draft DIE tags in DWARFv4 format. */
9217 int ver = dwarf_version < 5 ? dwarf_version : 4;
9219 /* These attributes will be found in the full debug_info section. */
9220 remove_AT (comp_unit, DW_AT_producer);
9221 remove_AT (comp_unit, DW_AT_language);
9223 switch_to_section (debug_skeleton_info_section);
9224 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9226 /* Produce the skeleton compilation-unit header. This one differs enough from
9227 a normal CU header that it's better not to call output_compilation_unit
9228 header. */
9229 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9230 dw2_asm_output_data (4, 0xffffffff,
9231 "Initial length escape value indicating 64-bit DWARF extension");
9233 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9234 DWARF_COMPILE_UNIT_HEADER_SIZE
9235 - DWARF_INITIAL_LENGTH_SIZE
9236 + size_of_die (comp_unit),
9237 "Length of Compilation Unit Info");
9238 dw2_asm_output_data (2, ver, "DWARF version number");
9239 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9240 debug_abbrev_section,
9241 "Offset Into Abbrev. Section");
9242 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9244 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9245 output_die (comp_unit);
9247 /* Build the skeleton debug_abbrev section. */
9248 switch_to_section (debug_skeleton_abbrev_section);
9249 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9251 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9253 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9256 /* Output a comdat type unit DIE and its children. */
9258 static void
9259 output_comdat_type_unit (comdat_type_node *node)
9261 const char *secname;
9262 char *tmp;
9263 int i;
9264 #if defined (OBJECT_FORMAT_ELF)
9265 tree comdat_key;
9266 #endif
9268 /* First mark all the DIEs in this CU so we know which get local refs. */
9269 mark_dies (node->root_die);
9271 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9273 build_abbrev_table (node->root_die, extern_map);
9275 delete extern_map;
9276 extern_map = NULL;
9278 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9279 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9280 calc_die_sizes (node->root_die);
9282 #if defined (OBJECT_FORMAT_ELF)
9283 if (!dwarf_split_debug_info)
9284 secname = ".debug_types";
9285 else
9286 secname = ".debug_types.dwo";
9288 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9289 sprintf (tmp, "wt.");
9290 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9291 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9292 comdat_key = get_identifier (tmp);
9293 targetm.asm_out.named_section (secname,
9294 SECTION_DEBUG | SECTION_LINKONCE,
9295 comdat_key);
9296 #else
9297 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9298 sprintf (tmp, ".gnu.linkonce.wt.");
9299 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9300 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9301 secname = tmp;
9302 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9303 #endif
9305 /* Output debugging information. */
9306 output_compilation_unit_header ();
9307 output_signature (node->signature, "Type Signature");
9308 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9309 "Offset to Type DIE");
9310 output_die (node->root_die);
9312 unmark_dies (node->root_die);
9315 /* Return the DWARF2/3 pubname associated with a decl. */
9317 static const char *
9318 dwarf2_name (tree decl, int scope)
9320 if (DECL_NAMELESS (decl))
9321 return NULL;
9322 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9325 /* Add a new entry to .debug_pubnames if appropriate. */
9327 static void
9328 add_pubname_string (const char *str, dw_die_ref die)
9330 pubname_entry e;
9332 e.die = die;
9333 e.name = xstrdup (str);
9334 vec_safe_push (pubname_table, e);
9337 static void
9338 add_pubname (tree decl, dw_die_ref die)
9340 if (!want_pubnames ())
9341 return;
9343 /* Don't add items to the table when we expect that the consumer will have
9344 just read the enclosing die. For example, if the consumer is looking at a
9345 class_member, it will either be inside the class already, or will have just
9346 looked up the class to find the member. Either way, searching the class is
9347 faster than searching the index. */
9348 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9349 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9351 const char *name = dwarf2_name (decl, 1);
9353 if (name)
9354 add_pubname_string (name, die);
9358 /* Add an enumerator to the pubnames section. */
9360 static void
9361 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9363 pubname_entry e;
9365 gcc_assert (scope_name);
9366 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9367 e.die = die;
9368 vec_safe_push (pubname_table, e);
9371 /* Add a new entry to .debug_pubtypes if appropriate. */
9373 static void
9374 add_pubtype (tree decl, dw_die_ref die)
9376 pubname_entry e;
9378 if (!want_pubnames ())
9379 return;
9381 if ((TREE_PUBLIC (decl)
9382 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9383 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9385 tree scope = NULL;
9386 const char *scope_name = "";
9387 const char *sep = is_cxx () ? "::" : ".";
9388 const char *name;
9390 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9391 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9393 scope_name = lang_hooks.dwarf_name (scope, 1);
9394 if (scope_name != NULL && scope_name[0] != '\0')
9395 scope_name = concat (scope_name, sep, NULL);
9396 else
9397 scope_name = "";
9400 if (TYPE_P (decl))
9401 name = type_tag (decl);
9402 else
9403 name = lang_hooks.dwarf_name (decl, 1);
9405 /* If we don't have a name for the type, there's no point in adding
9406 it to the table. */
9407 if (name != NULL && name[0] != '\0')
9409 e.die = die;
9410 e.name = concat (scope_name, name, NULL);
9411 vec_safe_push (pubtype_table, e);
9414 /* Although it might be more consistent to add the pubinfo for the
9415 enumerators as their dies are created, they should only be added if the
9416 enum type meets the criteria above. So rather than re-check the parent
9417 enum type whenever an enumerator die is created, just output them all
9418 here. This isn't protected by the name conditional because anonymous
9419 enums don't have names. */
9420 if (die->die_tag == DW_TAG_enumeration_type)
9422 dw_die_ref c;
9424 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9429 /* Output a single entry in the pubnames table. */
9431 static void
9432 output_pubname (dw_offset die_offset, pubname_entry *entry)
9434 dw_die_ref die = entry->die;
9435 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9437 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9439 if (debug_generate_pub_sections == 2)
9441 /* This logic follows gdb's method for determining the value of the flag
9442 byte. */
9443 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9444 switch (die->die_tag)
9446 case DW_TAG_typedef:
9447 case DW_TAG_base_type:
9448 case DW_TAG_subrange_type:
9449 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9450 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9451 break;
9452 case DW_TAG_enumerator:
9453 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9454 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9455 if (!is_cxx () && !is_java ())
9456 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9457 break;
9458 case DW_TAG_subprogram:
9459 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9460 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9461 if (!is_ada ())
9462 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9463 break;
9464 case DW_TAG_constant:
9465 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9466 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9467 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9468 break;
9469 case DW_TAG_variable:
9470 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9471 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9472 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9473 break;
9474 case DW_TAG_namespace:
9475 case DW_TAG_imported_declaration:
9476 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9477 break;
9478 case DW_TAG_class_type:
9479 case DW_TAG_interface_type:
9480 case DW_TAG_structure_type:
9481 case DW_TAG_union_type:
9482 case DW_TAG_enumeration_type:
9483 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9484 if (!is_cxx () && !is_java ())
9485 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9486 break;
9487 default:
9488 /* An unusual tag. Leave the flag-byte empty. */
9489 break;
9491 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9492 "GDB-index flags");
9495 dw2_asm_output_nstring (entry->name, -1, "external name");
9499 /* Output the public names table used to speed up access to externally
9500 visible names; or the public types table used to find type definitions. */
9502 static void
9503 output_pubnames (vec<pubname_entry, va_gc> *names)
9505 unsigned i;
9506 unsigned long pubnames_length = size_of_pubnames (names);
9507 pubname_ref pub;
9509 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9510 dw2_asm_output_data (4, 0xffffffff,
9511 "Initial length escape value indicating 64-bit DWARF extension");
9512 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9514 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9515 dw2_asm_output_data (2, 2, "DWARF Version");
9517 if (dwarf_split_debug_info)
9518 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9519 debug_skeleton_info_section,
9520 "Offset of Compilation Unit Info");
9521 else
9522 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9523 debug_info_section,
9524 "Offset of Compilation Unit Info");
9525 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9526 "Compilation Unit Length");
9528 FOR_EACH_VEC_ELT (*names, i, pub)
9530 if (include_pubname_in_output (names, pub))
9532 dw_offset die_offset = pub->die->die_offset;
9534 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9535 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9536 gcc_assert (pub->die->die_mark);
9538 /* If we're putting types in their own .debug_types sections,
9539 the .debug_pubtypes table will still point to the compile
9540 unit (not the type unit), so we want to use the offset of
9541 the skeleton DIE (if there is one). */
9542 if (pub->die->comdat_type_p && names == pubtype_table)
9544 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9546 if (type_node != NULL)
9547 die_offset = (type_node->skeleton_die != NULL
9548 ? type_node->skeleton_die->die_offset
9549 : comp_unit_die ()->die_offset);
9552 output_pubname (die_offset, pub);
9556 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9559 /* Output public names and types tables if necessary. */
9561 static void
9562 output_pubtables (void)
9564 if (!want_pubnames () || !info_section_emitted)
9565 return;
9567 switch_to_section (debug_pubnames_section);
9568 output_pubnames (pubname_table);
9569 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9570 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9571 simply won't look for the section. */
9572 switch_to_section (debug_pubtypes_section);
9573 output_pubnames (pubtype_table);
9577 /* Output the information that goes into the .debug_aranges table.
9578 Namely, define the beginning and ending address range of the
9579 text section generated for this compilation unit. */
9581 static void
9582 output_aranges (unsigned long aranges_length)
9584 unsigned i;
9586 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9587 dw2_asm_output_data (4, 0xffffffff,
9588 "Initial length escape value indicating 64-bit DWARF extension");
9589 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9590 "Length of Address Ranges Info");
9591 /* Version number for aranges is still 2, even up to DWARF5. */
9592 dw2_asm_output_data (2, 2, "DWARF Version");
9593 if (dwarf_split_debug_info)
9594 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9595 debug_skeleton_info_section,
9596 "Offset of Compilation Unit Info");
9597 else
9598 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9599 debug_info_section,
9600 "Offset of Compilation Unit Info");
9601 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9602 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9604 /* We need to align to twice the pointer size here. */
9605 if (DWARF_ARANGES_PAD_SIZE)
9607 /* Pad using a 2 byte words so that padding is correct for any
9608 pointer size. */
9609 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9610 2 * DWARF2_ADDR_SIZE);
9611 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9612 dw2_asm_output_data (2, 0, NULL);
9615 /* It is necessary not to output these entries if the sections were
9616 not used; if the sections were not used, the length will be 0 and
9617 the address may end up as 0 if the section is discarded by ld
9618 --gc-sections, leaving an invalid (0, 0) entry that can be
9619 confused with the terminator. */
9620 if (text_section_used)
9622 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9623 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9624 text_section_label, "Length");
9626 if (cold_text_section_used)
9628 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9629 "Address");
9630 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9631 cold_text_section_label, "Length");
9634 if (have_multiple_function_sections)
9636 unsigned fde_idx;
9637 dw_fde_ref fde;
9639 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9641 if (DECL_IGNORED_P (fde->decl))
9642 continue;
9643 if (!fde->in_std_section)
9645 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9646 "Address");
9647 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9648 fde->dw_fde_begin, "Length");
9650 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9652 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9653 "Address");
9654 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9655 fde->dw_fde_second_begin, "Length");
9660 /* Output the terminator words. */
9661 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9662 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9665 /* Add a new entry to .debug_ranges. Return the offset at which it
9666 was placed. */
9668 static unsigned int
9669 add_ranges_num (int num)
9671 unsigned int in_use = ranges_table_in_use;
9673 if (in_use == ranges_table_allocated)
9675 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9676 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9677 ranges_table_allocated);
9678 memset (ranges_table + ranges_table_in_use, 0,
9679 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9682 ranges_table[in_use].num = num;
9683 ranges_table_in_use = in_use + 1;
9685 return in_use * 2 * DWARF2_ADDR_SIZE;
9688 /* Add a new entry to .debug_ranges corresponding to a block, or a
9689 range terminator if BLOCK is NULL. */
9691 static unsigned int
9692 add_ranges (const_tree block)
9694 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9697 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9698 When using dwarf_split_debug_info, address attributes in dies destined
9699 for the final executable should be direct references--setting the
9700 parameter force_direct ensures this behavior. */
9702 static void
9703 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9704 bool *added, bool force_direct)
9706 unsigned int in_use = ranges_by_label_in_use;
9707 unsigned int offset;
9709 if (in_use == ranges_by_label_allocated)
9711 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9712 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9713 ranges_by_label,
9714 ranges_by_label_allocated);
9715 memset (ranges_by_label + ranges_by_label_in_use, 0,
9716 RANGES_TABLE_INCREMENT
9717 * sizeof (struct dw_ranges_by_label_struct));
9720 ranges_by_label[in_use].begin = begin;
9721 ranges_by_label[in_use].end = end;
9722 ranges_by_label_in_use = in_use + 1;
9724 offset = add_ranges_num (-(int)in_use - 1);
9725 if (!*added)
9727 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9728 *added = true;
9732 static void
9733 output_ranges (void)
9735 unsigned i;
9736 static const char *const start_fmt = "Offset %#x";
9737 const char *fmt = start_fmt;
9739 for (i = 0; i < ranges_table_in_use; i++)
9741 int block_num = ranges_table[i].num;
9743 if (block_num > 0)
9745 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9746 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9748 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9749 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9751 /* If all code is in the text section, then the compilation
9752 unit base address defaults to DW_AT_low_pc, which is the
9753 base of the text section. */
9754 if (!have_multiple_function_sections)
9756 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9757 text_section_label,
9758 fmt, i * 2 * DWARF2_ADDR_SIZE);
9759 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9760 text_section_label, NULL);
9763 /* Otherwise, the compilation unit base address is zero,
9764 which allows us to use absolute addresses, and not worry
9765 about whether the target supports cross-section
9766 arithmetic. */
9767 else
9769 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9770 fmt, i * 2 * DWARF2_ADDR_SIZE);
9771 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9774 fmt = NULL;
9777 /* Negative block_num stands for an index into ranges_by_label. */
9778 else if (block_num < 0)
9780 int lab_idx = - block_num - 1;
9782 if (!have_multiple_function_sections)
9784 gcc_unreachable ();
9785 #if 0
9786 /* If we ever use add_ranges_by_labels () for a single
9787 function section, all we have to do is to take out
9788 the #if 0 above. */
9789 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9790 ranges_by_label[lab_idx].begin,
9791 text_section_label,
9792 fmt, i * 2 * DWARF2_ADDR_SIZE);
9793 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9794 ranges_by_label[lab_idx].end,
9795 text_section_label, NULL);
9796 #endif
9798 else
9800 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9801 ranges_by_label[lab_idx].begin,
9802 fmt, i * 2 * DWARF2_ADDR_SIZE);
9803 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9804 ranges_by_label[lab_idx].end,
9805 NULL);
9808 else
9810 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9811 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9812 fmt = start_fmt;
9817 /* Data structure containing information about input files. */
9818 struct file_info
9820 const char *path; /* Complete file name. */
9821 const char *fname; /* File name part. */
9822 int length; /* Length of entire string. */
9823 struct dwarf_file_data * file_idx; /* Index in input file table. */
9824 int dir_idx; /* Index in directory table. */
9827 /* Data structure containing information about directories with source
9828 files. */
9829 struct dir_info
9831 const char *path; /* Path including directory name. */
9832 int length; /* Path length. */
9833 int prefix; /* Index of directory entry which is a prefix. */
9834 int count; /* Number of files in this directory. */
9835 int dir_idx; /* Index of directory used as base. */
9838 /* Callback function for file_info comparison. We sort by looking at
9839 the directories in the path. */
9841 static int
9842 file_info_cmp (const void *p1, const void *p2)
9844 const struct file_info *const s1 = (const struct file_info *) p1;
9845 const struct file_info *const s2 = (const struct file_info *) p2;
9846 const unsigned char *cp1;
9847 const unsigned char *cp2;
9849 /* Take care of file names without directories. We need to make sure that
9850 we return consistent values to qsort since some will get confused if
9851 we return the same value when identical operands are passed in opposite
9852 orders. So if neither has a directory, return 0 and otherwise return
9853 1 or -1 depending on which one has the directory. */
9854 if ((s1->path == s1->fname || s2->path == s2->fname))
9855 return (s2->path == s2->fname) - (s1->path == s1->fname);
9857 cp1 = (const unsigned char *) s1->path;
9858 cp2 = (const unsigned char *) s2->path;
9860 while (1)
9862 ++cp1;
9863 ++cp2;
9864 /* Reached the end of the first path? If so, handle like above. */
9865 if ((cp1 == (const unsigned char *) s1->fname)
9866 || (cp2 == (const unsigned char *) s2->fname))
9867 return ((cp2 == (const unsigned char *) s2->fname)
9868 - (cp1 == (const unsigned char *) s1->fname));
9870 /* Character of current path component the same? */
9871 else if (*cp1 != *cp2)
9872 return *cp1 - *cp2;
9876 struct file_name_acquire_data
9878 struct file_info *files;
9879 int used_files;
9880 int max_files;
9883 /* Traversal function for the hash table. */
9886 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9888 struct dwarf_file_data *d = *slot;
9889 struct file_info *fi;
9890 const char *f;
9892 gcc_assert (fnad->max_files >= d->emitted_number);
9894 if (! d->emitted_number)
9895 return 1;
9897 gcc_assert (fnad->max_files != fnad->used_files);
9899 fi = fnad->files + fnad->used_files++;
9901 /* Skip all leading "./". */
9902 f = d->filename;
9903 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9904 f += 2;
9906 /* Create a new array entry. */
9907 fi->path = f;
9908 fi->length = strlen (f);
9909 fi->file_idx = d;
9911 /* Search for the file name part. */
9912 f = strrchr (f, DIR_SEPARATOR);
9913 #if defined (DIR_SEPARATOR_2)
9915 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9917 if (g != NULL)
9919 if (f == NULL || f < g)
9920 f = g;
9923 #endif
9925 fi->fname = f == NULL ? fi->path : f + 1;
9926 return 1;
9929 /* Output the directory table and the file name table. We try to minimize
9930 the total amount of memory needed. A heuristic is used to avoid large
9931 slowdowns with many input files. */
9933 static void
9934 output_file_names (void)
9936 struct file_name_acquire_data fnad;
9937 int numfiles;
9938 struct file_info *files;
9939 struct dir_info *dirs;
9940 int *saved;
9941 int *savehere;
9942 int *backmap;
9943 int ndirs;
9944 int idx_offset;
9945 int i;
9947 if (!last_emitted_file)
9949 dw2_asm_output_data (1, 0, "End directory table");
9950 dw2_asm_output_data (1, 0, "End file name table");
9951 return;
9954 numfiles = last_emitted_file->emitted_number;
9956 /* Allocate the various arrays we need. */
9957 files = XALLOCAVEC (struct file_info, numfiles);
9958 dirs = XALLOCAVEC (struct dir_info, numfiles);
9960 fnad.files = files;
9961 fnad.used_files = 0;
9962 fnad.max_files = numfiles;
9963 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9964 gcc_assert (fnad.used_files == fnad.max_files);
9966 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9968 /* Find all the different directories used. */
9969 dirs[0].path = files[0].path;
9970 dirs[0].length = files[0].fname - files[0].path;
9971 dirs[0].prefix = -1;
9972 dirs[0].count = 1;
9973 dirs[0].dir_idx = 0;
9974 files[0].dir_idx = 0;
9975 ndirs = 1;
9977 for (i = 1; i < numfiles; i++)
9978 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9979 && memcmp (dirs[ndirs - 1].path, files[i].path,
9980 dirs[ndirs - 1].length) == 0)
9982 /* Same directory as last entry. */
9983 files[i].dir_idx = ndirs - 1;
9984 ++dirs[ndirs - 1].count;
9986 else
9988 int j;
9990 /* This is a new directory. */
9991 dirs[ndirs].path = files[i].path;
9992 dirs[ndirs].length = files[i].fname - files[i].path;
9993 dirs[ndirs].count = 1;
9994 dirs[ndirs].dir_idx = ndirs;
9995 files[i].dir_idx = ndirs;
9997 /* Search for a prefix. */
9998 dirs[ndirs].prefix = -1;
9999 for (j = 0; j < ndirs; j++)
10000 if (dirs[j].length < dirs[ndirs].length
10001 && dirs[j].length > 1
10002 && (dirs[ndirs].prefix == -1
10003 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10004 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10005 dirs[ndirs].prefix = j;
10007 ++ndirs;
10010 /* Now to the actual work. We have to find a subset of the directories which
10011 allow expressing the file name using references to the directory table
10012 with the least amount of characters. We do not do an exhaustive search
10013 where we would have to check out every combination of every single
10014 possible prefix. Instead we use a heuristic which provides nearly optimal
10015 results in most cases and never is much off. */
10016 saved = XALLOCAVEC (int, ndirs);
10017 savehere = XALLOCAVEC (int, ndirs);
10019 memset (saved, '\0', ndirs * sizeof (saved[0]));
10020 for (i = 0; i < ndirs; i++)
10022 int j;
10023 int total;
10025 /* We can always save some space for the current directory. But this
10026 does not mean it will be enough to justify adding the directory. */
10027 savehere[i] = dirs[i].length;
10028 total = (savehere[i] - saved[i]) * dirs[i].count;
10030 for (j = i + 1; j < ndirs; j++)
10032 savehere[j] = 0;
10033 if (saved[j] < dirs[i].length)
10035 /* Determine whether the dirs[i] path is a prefix of the
10036 dirs[j] path. */
10037 int k;
10039 k = dirs[j].prefix;
10040 while (k != -1 && k != (int) i)
10041 k = dirs[k].prefix;
10043 if (k == (int) i)
10045 /* Yes it is. We can possibly save some memory by
10046 writing the filenames in dirs[j] relative to
10047 dirs[i]. */
10048 savehere[j] = dirs[i].length;
10049 total += (savehere[j] - saved[j]) * dirs[j].count;
10054 /* Check whether we can save enough to justify adding the dirs[i]
10055 directory. */
10056 if (total > dirs[i].length + 1)
10058 /* It's worthwhile adding. */
10059 for (j = i; j < ndirs; j++)
10060 if (savehere[j] > 0)
10062 /* Remember how much we saved for this directory so far. */
10063 saved[j] = savehere[j];
10065 /* Remember the prefix directory. */
10066 dirs[j].dir_idx = i;
10071 /* Emit the directory name table. */
10072 idx_offset = dirs[0].length > 0 ? 1 : 0;
10073 for (i = 1 - idx_offset; i < ndirs; i++)
10074 dw2_asm_output_nstring (dirs[i].path,
10075 dirs[i].length
10076 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10077 "Directory Entry: %#x", i + idx_offset);
10079 dw2_asm_output_data (1, 0, "End directory table");
10081 /* We have to emit them in the order of emitted_number since that's
10082 used in the debug info generation. To do this efficiently we
10083 generate a back-mapping of the indices first. */
10084 backmap = XALLOCAVEC (int, numfiles);
10085 for (i = 0; i < numfiles; i++)
10086 backmap[files[i].file_idx->emitted_number - 1] = i;
10088 /* Now write all the file names. */
10089 for (i = 0; i < numfiles; i++)
10091 int file_idx = backmap[i];
10092 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10094 #ifdef VMS_DEBUGGING_INFO
10095 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10097 /* Setting these fields can lead to debugger miscomparisons,
10098 but VMS Debug requires them to be set correctly. */
10100 int ver;
10101 long long cdt;
10102 long siz;
10103 int maxfilelen = strlen (files[file_idx].path)
10104 + dirs[dir_idx].length
10105 + MAX_VMS_VERSION_LEN + 1;
10106 char *filebuf = XALLOCAVEC (char, maxfilelen);
10108 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10109 snprintf (filebuf, maxfilelen, "%s;%d",
10110 files[file_idx].path + dirs[dir_idx].length, ver);
10112 dw2_asm_output_nstring
10113 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10115 /* Include directory index. */
10116 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10118 /* Modification time. */
10119 dw2_asm_output_data_uleb128
10120 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10121 ? cdt : 0,
10122 NULL);
10124 /* File length in bytes. */
10125 dw2_asm_output_data_uleb128
10126 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10127 ? siz : 0,
10128 NULL);
10129 #else
10130 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10131 "File Entry: %#x", (unsigned) i + 1);
10133 /* Include directory index. */
10134 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10136 /* Modification time. */
10137 dw2_asm_output_data_uleb128 (0, NULL);
10139 /* File length in bytes. */
10140 dw2_asm_output_data_uleb128 (0, NULL);
10141 #endif /* VMS_DEBUGGING_INFO */
10144 dw2_asm_output_data (1, 0, "End file name table");
10148 /* Output one line number table into the .debug_line section. */
10150 static void
10151 output_one_line_info_table (dw_line_info_table *table)
10153 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10154 unsigned int current_line = 1;
10155 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10156 dw_line_info_entry *ent;
10157 size_t i;
10159 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10161 switch (ent->opcode)
10163 case LI_set_address:
10164 /* ??? Unfortunately, we have little choice here currently, and
10165 must always use the most general form. GCC does not know the
10166 address delta itself, so we can't use DW_LNS_advance_pc. Many
10167 ports do have length attributes which will give an upper bound
10168 on the address range. We could perhaps use length attributes
10169 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10170 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10172 /* This can handle any delta. This takes
10173 4+DWARF2_ADDR_SIZE bytes. */
10174 dw2_asm_output_data (1, 0, "set address %s", line_label);
10175 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10176 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10177 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10178 break;
10180 case LI_set_line:
10181 if (ent->val == current_line)
10183 /* We still need to start a new row, so output a copy insn. */
10184 dw2_asm_output_data (1, DW_LNS_copy,
10185 "copy line %u", current_line);
10187 else
10189 int line_offset = ent->val - current_line;
10190 int line_delta = line_offset - DWARF_LINE_BASE;
10192 current_line = ent->val;
10193 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10195 /* This can handle deltas from -10 to 234, using the current
10196 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10197 This takes 1 byte. */
10198 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10199 "line %u", current_line);
10201 else
10203 /* This can handle any delta. This takes at least 4 bytes,
10204 depending on the value being encoded. */
10205 dw2_asm_output_data (1, DW_LNS_advance_line,
10206 "advance to line %u", current_line);
10207 dw2_asm_output_data_sleb128 (line_offset, NULL);
10208 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10211 break;
10213 case LI_set_file:
10214 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10215 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10216 break;
10218 case LI_set_column:
10219 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10220 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10221 break;
10223 case LI_negate_stmt:
10224 current_is_stmt = !current_is_stmt;
10225 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10226 "is_stmt %d", current_is_stmt);
10227 break;
10229 case LI_set_prologue_end:
10230 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10231 "set prologue end");
10232 break;
10234 case LI_set_epilogue_begin:
10235 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10236 "set epilogue begin");
10237 break;
10239 case LI_set_discriminator:
10240 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10241 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10242 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10243 dw2_asm_output_data_uleb128 (ent->val, NULL);
10244 break;
10248 /* Emit debug info for the address of the end of the table. */
10249 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10250 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10251 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10252 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10254 dw2_asm_output_data (1, 0, "end sequence");
10255 dw2_asm_output_data_uleb128 (1, NULL);
10256 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10259 /* Output the source line number correspondence information. This
10260 information goes into the .debug_line section. */
10262 static void
10263 output_line_info (bool prologue_only)
10265 char l1[20], l2[20], p1[20], p2[20];
10266 /* We don't support DWARFv5 line tables yet. */
10267 int ver = dwarf_version < 5 ? dwarf_version : 4;
10268 bool saw_one = false;
10269 int opc;
10271 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10272 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10273 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10274 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10276 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10277 dw2_asm_output_data (4, 0xffffffff,
10278 "Initial length escape value indicating 64-bit DWARF extension");
10279 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10280 "Length of Source Line Info");
10281 ASM_OUTPUT_LABEL (asm_out_file, l1);
10283 dw2_asm_output_data (2, ver, "DWARF Version");
10284 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10285 ASM_OUTPUT_LABEL (asm_out_file, p1);
10287 /* Define the architecture-dependent minimum instruction length (in bytes).
10288 In this implementation of DWARF, this field is used for information
10289 purposes only. Since GCC generates assembly language, we have no
10290 a priori knowledge of how many instruction bytes are generated for each
10291 source line, and therefore can use only the DW_LNE_set_address and
10292 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10293 this as '1', which is "correct enough" for all architectures,
10294 and don't let the target override. */
10295 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10297 if (ver >= 4)
10298 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10299 "Maximum Operations Per Instruction");
10300 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10301 "Default is_stmt_start flag");
10302 dw2_asm_output_data (1, DWARF_LINE_BASE,
10303 "Line Base Value (Special Opcodes)");
10304 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10305 "Line Range Value (Special Opcodes)");
10306 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10307 "Special Opcode Base");
10309 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10311 int n_op_args;
10312 switch (opc)
10314 case DW_LNS_advance_pc:
10315 case DW_LNS_advance_line:
10316 case DW_LNS_set_file:
10317 case DW_LNS_set_column:
10318 case DW_LNS_fixed_advance_pc:
10319 case DW_LNS_set_isa:
10320 n_op_args = 1;
10321 break;
10322 default:
10323 n_op_args = 0;
10324 break;
10327 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10328 opc, n_op_args);
10331 /* Write out the information about the files we use. */
10332 output_file_names ();
10333 ASM_OUTPUT_LABEL (asm_out_file, p2);
10334 if (prologue_only)
10336 /* Output the marker for the end of the line number info. */
10337 ASM_OUTPUT_LABEL (asm_out_file, l2);
10338 return;
10341 if (separate_line_info)
10343 dw_line_info_table *table;
10344 size_t i;
10346 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10347 if (table->in_use)
10349 output_one_line_info_table (table);
10350 saw_one = true;
10353 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10355 output_one_line_info_table (cold_text_section_line_info);
10356 saw_one = true;
10359 /* ??? Some Darwin linkers crash on a .debug_line section with no
10360 sequences. Further, merely a DW_LNE_end_sequence entry is not
10361 sufficient -- the address column must also be initialized.
10362 Make sure to output at least one set_address/end_sequence pair,
10363 choosing .text since that section is always present. */
10364 if (text_section_line_info->in_use || !saw_one)
10365 output_one_line_info_table (text_section_line_info);
10367 /* Output the marker for the end of the line number info. */
10368 ASM_OUTPUT_LABEL (asm_out_file, l2);
10371 /* Given a pointer to a tree node for some base type, return a pointer to
10372 a DIE that describes the given type.
10374 This routine must only be called for GCC type nodes that correspond to
10375 Dwarf base (fundamental) types. */
10377 static dw_die_ref
10378 base_type_die (tree type)
10380 dw_die_ref base_type_result;
10381 enum dwarf_type encoding;
10383 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10384 return 0;
10386 /* If this is a subtype that should not be emitted as a subrange type,
10387 use the base type. See subrange_type_for_debug_p. */
10388 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10389 type = TREE_TYPE (type);
10391 switch (TREE_CODE (type))
10393 case INTEGER_TYPE:
10394 if ((dwarf_version >= 4 || !dwarf_strict)
10395 && TYPE_NAME (type)
10396 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10397 && DECL_IS_BUILTIN (TYPE_NAME (type))
10398 && DECL_NAME (TYPE_NAME (type)))
10400 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10401 if (strcmp (name, "char16_t") == 0
10402 || strcmp (name, "char32_t") == 0)
10404 encoding = DW_ATE_UTF;
10405 break;
10408 if (TYPE_STRING_FLAG (type))
10410 if (TYPE_UNSIGNED (type))
10411 encoding = DW_ATE_unsigned_char;
10412 else
10413 encoding = DW_ATE_signed_char;
10415 else if (TYPE_UNSIGNED (type))
10416 encoding = DW_ATE_unsigned;
10417 else
10418 encoding = DW_ATE_signed;
10419 break;
10421 case REAL_TYPE:
10422 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10424 if (dwarf_version >= 3 || !dwarf_strict)
10425 encoding = DW_ATE_decimal_float;
10426 else
10427 encoding = DW_ATE_lo_user;
10429 else
10430 encoding = DW_ATE_float;
10431 break;
10433 case FIXED_POINT_TYPE:
10434 if (!(dwarf_version >= 3 || !dwarf_strict))
10435 encoding = DW_ATE_lo_user;
10436 else if (TYPE_UNSIGNED (type))
10437 encoding = DW_ATE_unsigned_fixed;
10438 else
10439 encoding = DW_ATE_signed_fixed;
10440 break;
10442 /* Dwarf2 doesn't know anything about complex ints, so use
10443 a user defined type for it. */
10444 case COMPLEX_TYPE:
10445 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10446 encoding = DW_ATE_complex_float;
10447 else
10448 encoding = DW_ATE_lo_user;
10449 break;
10451 case BOOLEAN_TYPE:
10452 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10453 encoding = DW_ATE_boolean;
10454 break;
10456 default:
10457 /* No other TREE_CODEs are Dwarf fundamental types. */
10458 gcc_unreachable ();
10461 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10463 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10464 int_size_in_bytes (type));
10465 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10466 add_pubtype (type, base_type_result);
10468 return base_type_result;
10471 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10472 named 'auto' in its type: return true for it, false otherwise. */
10474 static inline bool
10475 is_cxx_auto (tree type)
10477 if (is_cxx ())
10479 tree name = TYPE_IDENTIFIER (type);
10480 if (name == get_identifier ("auto")
10481 || name == get_identifier ("decltype(auto)"))
10482 return true;
10484 return false;
10487 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10488 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10490 static inline int
10491 is_base_type (tree type)
10493 switch (TREE_CODE (type))
10495 case ERROR_MARK:
10496 case VOID_TYPE:
10497 case INTEGER_TYPE:
10498 case REAL_TYPE:
10499 case FIXED_POINT_TYPE:
10500 case COMPLEX_TYPE:
10501 case BOOLEAN_TYPE:
10502 case POINTER_BOUNDS_TYPE:
10503 return 1;
10505 case ARRAY_TYPE:
10506 case RECORD_TYPE:
10507 case UNION_TYPE:
10508 case QUAL_UNION_TYPE:
10509 case ENUMERAL_TYPE:
10510 case FUNCTION_TYPE:
10511 case METHOD_TYPE:
10512 case POINTER_TYPE:
10513 case REFERENCE_TYPE:
10514 case NULLPTR_TYPE:
10515 case OFFSET_TYPE:
10516 case LANG_TYPE:
10517 case VECTOR_TYPE:
10518 return 0;
10520 default:
10521 if (is_cxx_auto (type))
10522 return 0;
10523 gcc_unreachable ();
10526 return 0;
10529 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10530 node, return the size in bits for the type if it is a constant, or else
10531 return the alignment for the type if the type's size is not constant, or
10532 else return BITS_PER_WORD if the type actually turns out to be an
10533 ERROR_MARK node. */
10535 static inline unsigned HOST_WIDE_INT
10536 simple_type_size_in_bits (const_tree type)
10538 if (TREE_CODE (type) == ERROR_MARK)
10539 return BITS_PER_WORD;
10540 else if (TYPE_SIZE (type) == NULL_TREE)
10541 return 0;
10542 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10543 return tree_to_uhwi (TYPE_SIZE (type));
10544 else
10545 return TYPE_ALIGN (type);
10548 /* Similarly, but return an offset_int instead of UHWI. */
10550 static inline offset_int
10551 offset_int_type_size_in_bits (const_tree type)
10553 if (TREE_CODE (type) == ERROR_MARK)
10554 return BITS_PER_WORD;
10555 else if (TYPE_SIZE (type) == NULL_TREE)
10556 return 0;
10557 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10558 return wi::to_offset (TYPE_SIZE (type));
10559 else
10560 return TYPE_ALIGN (type);
10563 /* Given a pointer to a tree node for a subrange type, return a pointer
10564 to a DIE that describes the given type. */
10566 static dw_die_ref
10567 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10569 dw_die_ref subrange_die;
10570 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10572 if (context_die == NULL)
10573 context_die = comp_unit_die ();
10575 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10577 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10579 /* The size of the subrange type and its base type do not match,
10580 so we need to generate a size attribute for the subrange type. */
10581 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10584 if (low)
10585 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10586 if (high)
10587 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10589 return subrange_die;
10592 /* Returns the (const and/or volatile) cv_qualifiers associated with
10593 the decl node. This will normally be augmented with the
10594 cv_qualifiers of the underlying type in add_type_attribute. */
10596 static int
10597 decl_quals (const_tree decl)
10599 return ((TREE_READONLY (decl)
10600 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10601 | (TREE_THIS_VOLATILE (decl)
10602 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10605 /* Determine the TYPE whose qualifiers match the largest strict subset
10606 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10607 qualifiers outside QUAL_MASK. */
10609 static int
10610 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10612 tree t;
10613 int best_rank = 0, best_qual = 0, max_rank;
10615 type_quals &= qual_mask;
10616 max_rank = popcount_hwi (type_quals) - 1;
10618 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10619 t = TYPE_NEXT_VARIANT (t))
10621 int q = TYPE_QUALS (t) & qual_mask;
10623 if ((q & type_quals) == q && q != type_quals
10624 && check_base_type (t, type))
10626 int rank = popcount_hwi (q);
10628 if (rank > best_rank)
10630 best_rank = rank;
10631 best_qual = q;
10636 return best_qual;
10639 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10640 entry that chains various modifiers in front of the given type. */
10642 static dw_die_ref
10643 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10645 enum tree_code code = TREE_CODE (type);
10646 dw_die_ref mod_type_die;
10647 dw_die_ref sub_die = NULL;
10648 tree item_type = NULL;
10649 tree qualified_type;
10650 tree name, low, high;
10651 dw_die_ref mod_scope;
10652 /* Only these cv-qualifiers are currently handled. */
10653 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10654 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10656 if (code == ERROR_MARK)
10657 return NULL;
10659 cv_quals &= cv_qual_mask;
10661 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10662 tag modifier (and not an attribute) old consumers won't be able
10663 to handle it. */
10664 if (dwarf_version < 3)
10665 cv_quals &= ~TYPE_QUAL_RESTRICT;
10667 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10668 if (dwarf_version < 5)
10669 cv_quals &= ~TYPE_QUAL_ATOMIC;
10671 /* See if we already have the appropriately qualified variant of
10672 this type. */
10673 qualified_type = get_qualified_type (type, cv_quals);
10675 if (qualified_type == sizetype
10676 && TYPE_NAME (qualified_type)
10677 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10679 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10681 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10682 && TYPE_PRECISION (t)
10683 == TYPE_PRECISION (qualified_type)
10684 && TYPE_UNSIGNED (t)
10685 == TYPE_UNSIGNED (qualified_type));
10686 qualified_type = t;
10689 /* If we do, then we can just use its DIE, if it exists. */
10690 if (qualified_type)
10692 mod_type_die = lookup_type_die (qualified_type);
10693 if (mod_type_die)
10694 return mod_type_die;
10697 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10699 /* Handle C typedef types. */
10700 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10701 && !DECL_ARTIFICIAL (name))
10703 tree dtype = TREE_TYPE (name);
10705 if (qualified_type == dtype)
10707 /* For a named type, use the typedef. */
10708 gen_type_die (qualified_type, context_die);
10709 return lookup_type_die (qualified_type);
10711 else
10713 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10714 dquals &= cv_qual_mask;
10715 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10716 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10717 /* cv-unqualified version of named type. Just use
10718 the unnamed type to which it refers. */
10719 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10720 cv_quals, context_die);
10721 /* Else cv-qualified version of named type; fall through. */
10725 mod_scope = scope_die_for (type, context_die);
10727 if (cv_quals)
10729 struct qual_info { int q; enum dwarf_tag t; };
10730 static const struct qual_info qual_info[] =
10732 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10733 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10734 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10735 { TYPE_QUAL_CONST, DW_TAG_const_type },
10737 int sub_quals;
10738 unsigned i;
10740 /* Determine a lesser qualified type that most closely matches
10741 this one. Then generate DW_TAG_* entries for the remaining
10742 qualifiers. */
10743 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10744 cv_qual_mask);
10745 mod_type_die = modified_type_die (type, sub_quals, context_die);
10747 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10748 if (qual_info[i].q & cv_quals & ~sub_quals)
10750 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10751 if (mod_type_die)
10752 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10753 mod_type_die = d;
10756 else if (code == POINTER_TYPE)
10758 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10759 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10760 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10761 item_type = TREE_TYPE (type);
10762 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10763 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10764 TYPE_ADDR_SPACE (item_type));
10766 else if (code == REFERENCE_TYPE)
10768 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10769 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10770 type);
10771 else
10772 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10773 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10774 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10775 item_type = TREE_TYPE (type);
10776 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10777 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10778 TYPE_ADDR_SPACE (item_type));
10780 else if (code == INTEGER_TYPE
10781 && TREE_TYPE (type) != NULL_TREE
10782 && subrange_type_for_debug_p (type, &low, &high))
10784 mod_type_die = subrange_type_die (type, low, high, context_die);
10785 item_type = TREE_TYPE (type);
10787 else if (is_base_type (type))
10788 mod_type_die = base_type_die (type);
10789 else
10791 gen_type_die (type, context_die);
10793 /* We have to get the type_main_variant here (and pass that to the
10794 `lookup_type_die' routine) because the ..._TYPE node we have
10795 might simply be a *copy* of some original type node (where the
10796 copy was created to help us keep track of typedef names) and
10797 that copy might have a different TYPE_UID from the original
10798 ..._TYPE node. */
10799 if (TREE_CODE (type) != VECTOR_TYPE)
10800 return lookup_type_die (type_main_variant (type));
10801 else
10802 /* Vectors have the debugging information in the type,
10803 not the main variant. */
10804 return lookup_type_die (type);
10807 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10808 don't output a DW_TAG_typedef, since there isn't one in the
10809 user's program; just attach a DW_AT_name to the type.
10810 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10811 if the base type already has the same name. */
10812 if (name
10813 && ((TREE_CODE (name) != TYPE_DECL
10814 && (qualified_type == TYPE_MAIN_VARIANT (type)
10815 || (cv_quals == TYPE_UNQUALIFIED)))
10816 || (TREE_CODE (name) == TYPE_DECL
10817 && TREE_TYPE (name) == qualified_type
10818 && DECL_NAME (name))))
10820 if (TREE_CODE (name) == TYPE_DECL)
10821 /* Could just call add_name_and_src_coords_attributes here,
10822 but since this is a builtin type it doesn't have any
10823 useful source coordinates anyway. */
10824 name = DECL_NAME (name);
10825 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10827 /* This probably indicates a bug. */
10828 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10830 name = TYPE_IDENTIFIER (type);
10831 add_name_attribute (mod_type_die,
10832 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10835 if (qualified_type)
10836 equate_type_number_to_die (qualified_type, mod_type_die);
10838 if (item_type)
10839 /* We must do this after the equate_type_number_to_die call, in case
10840 this is a recursive type. This ensures that the modified_type_die
10841 recursion will terminate even if the type is recursive. Recursive
10842 types are possible in Ada. */
10843 sub_die = modified_type_die (item_type,
10844 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10845 context_die);
10847 if (sub_die != NULL)
10848 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10850 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10851 if (TYPE_ARTIFICIAL (type))
10852 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10854 return mod_type_die;
10857 /* Generate DIEs for the generic parameters of T.
10858 T must be either a generic type or a generic function.
10859 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10861 static void
10862 gen_generic_params_dies (tree t)
10864 tree parms, args;
10865 int parms_num, i;
10866 dw_die_ref die = NULL;
10867 int non_default;
10869 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10870 return;
10872 if (TYPE_P (t))
10873 die = lookup_type_die (t);
10874 else if (DECL_P (t))
10875 die = lookup_decl_die (t);
10877 gcc_assert (die);
10879 parms = lang_hooks.get_innermost_generic_parms (t);
10880 if (!parms)
10881 /* T has no generic parameter. It means T is neither a generic type
10882 or function. End of story. */
10883 return;
10885 parms_num = TREE_VEC_LENGTH (parms);
10886 args = lang_hooks.get_innermost_generic_args (t);
10887 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10888 non_default = int_cst_value (TREE_CHAIN (args));
10889 else
10890 non_default = TREE_VEC_LENGTH (args);
10891 for (i = 0; i < parms_num; i++)
10893 tree parm, arg, arg_pack_elems;
10894 dw_die_ref parm_die;
10896 parm = TREE_VEC_ELT (parms, i);
10897 arg = TREE_VEC_ELT (args, i);
10898 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10899 gcc_assert (parm && TREE_VALUE (parm) && arg);
10901 if (parm && TREE_VALUE (parm) && arg)
10903 /* If PARM represents a template parameter pack,
10904 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10905 by DW_TAG_template_*_parameter DIEs for the argument
10906 pack elements of ARG. Note that ARG would then be
10907 an argument pack. */
10908 if (arg_pack_elems)
10909 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10910 arg_pack_elems,
10911 die);
10912 else
10913 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10914 true /* emit name */, die);
10915 if (i >= non_default)
10916 add_AT_flag (parm_die, DW_AT_default_value, 1);
10921 /* Create and return a DIE for PARM which should be
10922 the representation of a generic type parameter.
10923 For instance, in the C++ front end, PARM would be a template parameter.
10924 ARG is the argument to PARM.
10925 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10926 name of the PARM.
10927 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10928 as a child node. */
10930 static dw_die_ref
10931 generic_parameter_die (tree parm, tree arg,
10932 bool emit_name_p,
10933 dw_die_ref parent_die)
10935 dw_die_ref tmpl_die = NULL;
10936 const char *name = NULL;
10938 if (!parm || !DECL_NAME (parm) || !arg)
10939 return NULL;
10941 /* We support non-type generic parameters and arguments,
10942 type generic parameters and arguments, as well as
10943 generic generic parameters (a.k.a. template template parameters in C++)
10944 and arguments. */
10945 if (TREE_CODE (parm) == PARM_DECL)
10946 /* PARM is a nontype generic parameter */
10947 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10948 else if (TREE_CODE (parm) == TYPE_DECL)
10949 /* PARM is a type generic parameter. */
10950 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10951 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10952 /* PARM is a generic generic parameter.
10953 Its DIE is a GNU extension. It shall have a
10954 DW_AT_name attribute to represent the name of the template template
10955 parameter, and a DW_AT_GNU_template_name attribute to represent the
10956 name of the template template argument. */
10957 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10958 parent_die, parm);
10959 else
10960 gcc_unreachable ();
10962 if (tmpl_die)
10964 tree tmpl_type;
10966 /* If PARM is a generic parameter pack, it means we are
10967 emitting debug info for a template argument pack element.
10968 In other terms, ARG is a template argument pack element.
10969 In that case, we don't emit any DW_AT_name attribute for
10970 the die. */
10971 if (emit_name_p)
10973 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10974 gcc_assert (name);
10975 add_AT_string (tmpl_die, DW_AT_name, name);
10978 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10980 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10981 TMPL_DIE should have a child DW_AT_type attribute that is set
10982 to the type of the argument to PARM, which is ARG.
10983 If PARM is a type generic parameter, TMPL_DIE should have a
10984 child DW_AT_type that is set to ARG. */
10985 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10986 add_type_attribute (tmpl_die, tmpl_type,
10987 (TREE_THIS_VOLATILE (tmpl_type)
10988 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10989 parent_die);
10991 else
10993 /* So TMPL_DIE is a DIE representing a
10994 a generic generic template parameter, a.k.a template template
10995 parameter in C++ and arg is a template. */
10997 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10998 to the name of the argument. */
10999 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11000 if (name)
11001 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11004 if (TREE_CODE (parm) == PARM_DECL)
11005 /* So PARM is a non-type generic parameter.
11006 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11007 attribute of TMPL_DIE which value represents the value
11008 of ARG.
11009 We must be careful here:
11010 The value of ARG might reference some function decls.
11011 We might currently be emitting debug info for a generic
11012 type and types are emitted before function decls, we don't
11013 know if the function decls referenced by ARG will actually be
11014 emitted after cgraph computations.
11015 So must defer the generation of the DW_AT_const_value to
11016 after cgraph is ready. */
11017 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11020 return tmpl_die;
11023 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11024 PARM_PACK must be a template parameter pack. The returned DIE
11025 will be child DIE of PARENT_DIE. */
11027 static dw_die_ref
11028 template_parameter_pack_die (tree parm_pack,
11029 tree parm_pack_args,
11030 dw_die_ref parent_die)
11032 dw_die_ref die;
11033 int j;
11035 gcc_assert (parent_die && parm_pack);
11037 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11038 add_name_and_src_coords_attributes (die, parm_pack);
11039 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11040 generic_parameter_die (parm_pack,
11041 TREE_VEC_ELT (parm_pack_args, j),
11042 false /* Don't emit DW_AT_name */,
11043 die);
11044 return die;
11047 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11048 an enumerated type. */
11050 static inline int
11051 type_is_enum (const_tree type)
11053 return TREE_CODE (type) == ENUMERAL_TYPE;
11056 /* Return the DBX register number described by a given RTL node. */
11058 static unsigned int
11059 dbx_reg_number (const_rtx rtl)
11061 unsigned regno = REGNO (rtl);
11063 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11065 #ifdef LEAF_REG_REMAP
11066 if (crtl->uses_only_leaf_regs)
11068 int leaf_reg = LEAF_REG_REMAP (regno);
11069 if (leaf_reg != -1)
11070 regno = (unsigned) leaf_reg;
11072 #endif
11074 regno = DBX_REGISTER_NUMBER (regno);
11075 gcc_assert (regno != INVALID_REGNUM);
11076 return regno;
11079 /* Optionally add a DW_OP_piece term to a location description expression.
11080 DW_OP_piece is only added if the location description expression already
11081 doesn't end with DW_OP_piece. */
11083 static void
11084 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11086 dw_loc_descr_ref loc;
11088 if (*list_head != NULL)
11090 /* Find the end of the chain. */
11091 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11094 if (loc->dw_loc_opc != DW_OP_piece)
11095 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11099 /* Return a location descriptor that designates a machine register or
11100 zero if there is none. */
11102 static dw_loc_descr_ref
11103 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11105 rtx regs;
11107 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11108 return 0;
11110 /* We only use "frame base" when we're sure we're talking about the
11111 post-prologue local stack frame. We do this by *not* running
11112 register elimination until this point, and recognizing the special
11113 argument pointer and soft frame pointer rtx's.
11114 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11115 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11116 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11118 dw_loc_descr_ref result = NULL;
11120 if (dwarf_version >= 4 || !dwarf_strict)
11122 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11123 initialized);
11124 if (result)
11125 add_loc_descr (&result,
11126 new_loc_descr (DW_OP_stack_value, 0, 0));
11128 return result;
11131 regs = targetm.dwarf_register_span (rtl);
11133 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11134 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11135 else
11137 unsigned int dbx_regnum = dbx_reg_number (rtl);
11138 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11139 return 0;
11140 return one_reg_loc_descriptor (dbx_regnum, initialized);
11144 /* Return a location descriptor that designates a machine register for
11145 a given hard register number. */
11147 static dw_loc_descr_ref
11148 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11150 dw_loc_descr_ref reg_loc_descr;
11152 if (regno <= 31)
11153 reg_loc_descr
11154 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11155 else
11156 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11158 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11159 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11161 return reg_loc_descr;
11164 /* Given an RTL of a register, return a location descriptor that
11165 designates a value that spans more than one register. */
11167 static dw_loc_descr_ref
11168 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11169 enum var_init_status initialized)
11171 int size, i;
11172 dw_loc_descr_ref loc_result = NULL;
11174 /* Simple, contiguous registers. */
11175 if (regs == NULL_RTX)
11177 unsigned reg = REGNO (rtl);
11178 int nregs;
11180 #ifdef LEAF_REG_REMAP
11181 if (crtl->uses_only_leaf_regs)
11183 int leaf_reg = LEAF_REG_REMAP (reg);
11184 if (leaf_reg != -1)
11185 reg = (unsigned) leaf_reg;
11187 #endif
11189 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11190 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11192 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11194 loc_result = NULL;
11195 while (nregs--)
11197 dw_loc_descr_ref t;
11199 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11200 VAR_INIT_STATUS_INITIALIZED);
11201 add_loc_descr (&loc_result, t);
11202 add_loc_descr_op_piece (&loc_result, size);
11203 ++reg;
11205 return loc_result;
11208 /* Now onto stupid register sets in non contiguous locations. */
11210 gcc_assert (GET_CODE (regs) == PARALLEL);
11212 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11213 loc_result = NULL;
11215 for (i = 0; i < XVECLEN (regs, 0); ++i)
11217 dw_loc_descr_ref t;
11219 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11220 VAR_INIT_STATUS_INITIALIZED);
11221 add_loc_descr (&loc_result, t);
11222 add_loc_descr_op_piece (&loc_result, size);
11225 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11226 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11227 return loc_result;
11230 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11232 /* Return a location descriptor that designates a constant i,
11233 as a compound operation from constant (i >> shift), constant shift
11234 and DW_OP_shl. */
11236 static dw_loc_descr_ref
11237 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11239 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11240 add_loc_descr (&ret, int_loc_descriptor (shift));
11241 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11242 return ret;
11245 /* Return a location descriptor that designates a constant. */
11247 static dw_loc_descr_ref
11248 int_loc_descriptor (HOST_WIDE_INT i)
11250 enum dwarf_location_atom op;
11252 /* Pick the smallest representation of a constant, rather than just
11253 defaulting to the LEB encoding. */
11254 if (i >= 0)
11256 int clz = clz_hwi (i);
11257 int ctz = ctz_hwi (i);
11258 if (i <= 31)
11259 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11260 else if (i <= 0xff)
11261 op = DW_OP_const1u;
11262 else if (i <= 0xffff)
11263 op = DW_OP_const2u;
11264 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11265 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11266 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11267 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11268 while DW_OP_const4u is 5 bytes. */
11269 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11270 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11271 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11272 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11273 while DW_OP_const4u is 5 bytes. */
11274 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11275 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11276 op = DW_OP_const4u;
11277 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11278 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11279 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11280 while DW_OP_constu of constant >= 0x100000000 takes at least
11281 6 bytes. */
11282 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11283 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11284 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11285 >= HOST_BITS_PER_WIDE_INT)
11286 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11287 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11288 while DW_OP_constu takes in this case at least 6 bytes. */
11289 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11290 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11291 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11292 && size_of_uleb128 (i) > 6)
11293 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11294 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11295 else
11296 op = DW_OP_constu;
11298 else
11300 if (i >= -0x80)
11301 op = DW_OP_const1s;
11302 else if (i >= -0x8000)
11303 op = DW_OP_const2s;
11304 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11306 if (size_of_int_loc_descriptor (i) < 5)
11308 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11309 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11310 return ret;
11312 op = DW_OP_const4s;
11314 else
11316 if (size_of_int_loc_descriptor (i)
11317 < (unsigned long) 1 + size_of_sleb128 (i))
11319 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11320 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11321 return ret;
11323 op = DW_OP_consts;
11327 return new_loc_descr (op, i, 0);
11330 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11331 without actually allocating it. */
11333 static unsigned long
11334 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11336 return size_of_int_loc_descriptor (i >> shift)
11337 + size_of_int_loc_descriptor (shift)
11338 + 1;
11341 /* Return size_of_locs (int_loc_descriptor (i)) without
11342 actually allocating it. */
11344 static unsigned long
11345 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11347 unsigned long s;
11349 if (i >= 0)
11351 int clz, ctz;
11352 if (i <= 31)
11353 return 1;
11354 else if (i <= 0xff)
11355 return 2;
11356 else if (i <= 0xffff)
11357 return 3;
11358 clz = clz_hwi (i);
11359 ctz = ctz_hwi (i);
11360 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11361 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11362 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11363 - clz - 5);
11364 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11365 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11366 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11367 - clz - 8);
11368 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11369 return 5;
11370 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11371 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11372 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11373 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11374 - clz - 8);
11375 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11376 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11377 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11378 - clz - 16);
11379 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11380 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11381 && s > 6)
11382 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11383 - clz - 32);
11384 else
11385 return 1 + s;
11387 else
11389 if (i >= -0x80)
11390 return 2;
11391 else if (i >= -0x8000)
11392 return 3;
11393 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11395 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11397 s = size_of_int_loc_descriptor (-i) + 1;
11398 if (s < 5)
11399 return s;
11401 return 5;
11403 else
11405 unsigned long r = 1 + size_of_sleb128 (i);
11406 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11408 s = size_of_int_loc_descriptor (-i) + 1;
11409 if (s < r)
11410 return s;
11412 return r;
11417 /* Return loc description representing "address" of integer value.
11418 This can appear only as toplevel expression. */
11420 static dw_loc_descr_ref
11421 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11423 int litsize;
11424 dw_loc_descr_ref loc_result = NULL;
11426 if (!(dwarf_version >= 4 || !dwarf_strict))
11427 return NULL;
11429 litsize = size_of_int_loc_descriptor (i);
11430 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11431 is more compact. For DW_OP_stack_value we need:
11432 litsize + 1 (DW_OP_stack_value)
11433 and for DW_OP_implicit_value:
11434 1 (DW_OP_implicit_value) + 1 (length) + size. */
11435 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11437 loc_result = int_loc_descriptor (i);
11438 add_loc_descr (&loc_result,
11439 new_loc_descr (DW_OP_stack_value, 0, 0));
11440 return loc_result;
11443 loc_result = new_loc_descr (DW_OP_implicit_value,
11444 size, 0);
11445 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11446 loc_result->dw_loc_oprnd2.v.val_int = i;
11447 return loc_result;
11450 /* Return a location descriptor that designates a base+offset location. */
11452 static dw_loc_descr_ref
11453 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11454 enum var_init_status initialized)
11456 unsigned int regno;
11457 dw_loc_descr_ref result;
11458 dw_fde_ref fde = cfun->fde;
11460 /* We only use "frame base" when we're sure we're talking about the
11461 post-prologue local stack frame. We do this by *not* running
11462 register elimination until this point, and recognizing the special
11463 argument pointer and soft frame pointer rtx's. */
11464 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11466 rtx elim = (ira_use_lra_p
11467 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11468 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11470 if (elim != reg)
11472 if (GET_CODE (elim) == PLUS)
11474 offset += INTVAL (XEXP (elim, 1));
11475 elim = XEXP (elim, 0);
11477 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11478 && (elim == hard_frame_pointer_rtx
11479 || elim == stack_pointer_rtx))
11480 || elim == (frame_pointer_needed
11481 ? hard_frame_pointer_rtx
11482 : stack_pointer_rtx));
11484 /* If drap register is used to align stack, use frame
11485 pointer + offset to access stack variables. If stack
11486 is aligned without drap, use stack pointer + offset to
11487 access stack variables. */
11488 if (crtl->stack_realign_tried
11489 && reg == frame_pointer_rtx)
11491 int base_reg
11492 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11493 ? HARD_FRAME_POINTER_REGNUM
11494 : REGNO (elim));
11495 return new_reg_loc_descr (base_reg, offset);
11498 gcc_assert (frame_pointer_fb_offset_valid);
11499 offset += frame_pointer_fb_offset;
11500 return new_loc_descr (DW_OP_fbreg, offset, 0);
11504 regno = REGNO (reg);
11505 #ifdef LEAF_REG_REMAP
11506 if (crtl->uses_only_leaf_regs)
11508 int leaf_reg = LEAF_REG_REMAP (regno);
11509 if (leaf_reg != -1)
11510 regno = (unsigned) leaf_reg;
11512 #endif
11513 regno = DWARF_FRAME_REGNUM (regno);
11515 if (!optimize && fde
11516 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11518 /* Use cfa+offset to represent the location of arguments passed
11519 on the stack when drap is used to align stack.
11520 Only do this when not optimizing, for optimized code var-tracking
11521 is supposed to track where the arguments live and the register
11522 used as vdrap or drap in some spot might be used for something
11523 else in other part of the routine. */
11524 return new_loc_descr (DW_OP_fbreg, offset, 0);
11527 if (regno <= 31)
11528 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11529 offset, 0);
11530 else
11531 result = new_loc_descr (DW_OP_bregx, regno, offset);
11533 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11534 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11536 return result;
11539 /* Return true if this RTL expression describes a base+offset calculation. */
11541 static inline int
11542 is_based_loc (const_rtx rtl)
11544 return (GET_CODE (rtl) == PLUS
11545 && ((REG_P (XEXP (rtl, 0))
11546 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11547 && CONST_INT_P (XEXP (rtl, 1)))));
11550 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11551 failed. */
11553 static dw_loc_descr_ref
11554 tls_mem_loc_descriptor (rtx mem)
11556 tree base;
11557 dw_loc_descr_ref loc_result;
11559 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11560 return NULL;
11562 base = get_base_address (MEM_EXPR (mem));
11563 if (base == NULL
11564 || TREE_CODE (base) != VAR_DECL
11565 || !DECL_THREAD_LOCAL_P (base))
11566 return NULL;
11568 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11569 if (loc_result == NULL)
11570 return NULL;
11572 if (MEM_OFFSET (mem))
11573 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11575 return loc_result;
11578 /* Output debug info about reason why we failed to expand expression as dwarf
11579 expression. */
11581 static void
11582 expansion_failed (tree expr, rtx rtl, char const *reason)
11584 if (dump_file && (dump_flags & TDF_DETAILS))
11586 fprintf (dump_file, "Failed to expand as dwarf: ");
11587 if (expr)
11588 print_generic_expr (dump_file, expr, dump_flags);
11589 if (rtl)
11591 fprintf (dump_file, "\n");
11592 print_rtl (dump_file, rtl);
11594 fprintf (dump_file, "\nReason: %s\n", reason);
11598 /* Helper function for const_ok_for_output. */
11600 static bool
11601 const_ok_for_output_1 (rtx rtl)
11603 if (GET_CODE (rtl) == UNSPEC)
11605 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11606 we can't express it in the debug info. */
11607 #ifdef ENABLE_CHECKING
11608 /* Don't complain about TLS UNSPECs, those are just too hard to
11609 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11610 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11611 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11612 if (XVECLEN (rtl, 0) == 0
11613 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11614 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11615 inform (current_function_decl
11616 ? DECL_SOURCE_LOCATION (current_function_decl)
11617 : UNKNOWN_LOCATION,
11618 #if NUM_UNSPEC_VALUES > 0
11619 "non-delegitimized UNSPEC %s (%d) found in variable location",
11620 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11621 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11622 XINT (rtl, 1));
11623 #else
11624 "non-delegitimized UNSPEC %d found in variable location",
11625 XINT (rtl, 1));
11626 #endif
11627 #endif
11628 expansion_failed (NULL_TREE, rtl,
11629 "UNSPEC hasn't been delegitimized.\n");
11630 return false;
11633 if (targetm.const_not_ok_for_debug_p (rtl))
11635 expansion_failed (NULL_TREE, rtl,
11636 "Expression rejected for debug by the backend.\n");
11637 return false;
11640 /* FIXME: Refer to PR60655. It is possible for simplification
11641 of rtl expressions in var tracking to produce such expressions.
11642 We should really identify / validate expressions
11643 enclosed in CONST that can be handled by assemblers on various
11644 targets and only handle legitimate cases here. */
11645 if (GET_CODE (rtl) != SYMBOL_REF)
11647 if (GET_CODE (rtl) == NOT)
11648 return false;
11649 return true;
11652 if (CONSTANT_POOL_ADDRESS_P (rtl))
11654 bool marked;
11655 get_pool_constant_mark (rtl, &marked);
11656 /* If all references to this pool constant were optimized away,
11657 it was not output and thus we can't represent it. */
11658 if (!marked)
11660 expansion_failed (NULL_TREE, rtl,
11661 "Constant was removed from constant pool.\n");
11662 return false;
11666 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11667 return false;
11669 /* Avoid references to external symbols in debug info, on several targets
11670 the linker might even refuse to link when linking a shared library,
11671 and in many other cases the relocations for .debug_info/.debug_loc are
11672 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11673 to be defined within the same shared library or executable are fine. */
11674 if (SYMBOL_REF_EXTERNAL_P (rtl))
11676 tree decl = SYMBOL_REF_DECL (rtl);
11678 if (decl == NULL || !targetm.binds_local_p (decl))
11680 expansion_failed (NULL_TREE, rtl,
11681 "Symbol not defined in current TU.\n");
11682 return false;
11686 return true;
11689 /* Return true if constant RTL can be emitted in DW_OP_addr or
11690 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11691 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11693 static bool
11694 const_ok_for_output (rtx rtl)
11696 if (GET_CODE (rtl) == SYMBOL_REF)
11697 return const_ok_for_output_1 (rtl);
11699 if (GET_CODE (rtl) == CONST)
11701 subrtx_var_iterator::array_type array;
11702 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11703 if (!const_ok_for_output_1 (*iter))
11704 return false;
11705 return true;
11708 return true;
11711 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11712 if possible, NULL otherwise. */
11714 static dw_die_ref
11715 base_type_for_mode (machine_mode mode, bool unsignedp)
11717 dw_die_ref type_die;
11718 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11720 if (type == NULL)
11721 return NULL;
11722 switch (TREE_CODE (type))
11724 case INTEGER_TYPE:
11725 case REAL_TYPE:
11726 break;
11727 default:
11728 return NULL;
11730 type_die = lookup_type_die (type);
11731 if (!type_die)
11732 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11733 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11734 return NULL;
11735 return type_die;
11738 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11739 type matching MODE, or, if MODE is narrower than or as wide as
11740 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11741 possible. */
11743 static dw_loc_descr_ref
11744 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11746 machine_mode outer_mode = mode;
11747 dw_die_ref type_die;
11748 dw_loc_descr_ref cvt;
11750 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11752 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11753 return op;
11755 type_die = base_type_for_mode (outer_mode, 1);
11756 if (type_die == NULL)
11757 return NULL;
11758 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11759 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11760 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11761 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11762 add_loc_descr (&op, cvt);
11763 return op;
11766 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11768 static dw_loc_descr_ref
11769 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11770 dw_loc_descr_ref op1)
11772 dw_loc_descr_ref ret = op0;
11773 add_loc_descr (&ret, op1);
11774 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11775 if (STORE_FLAG_VALUE != 1)
11777 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11778 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11780 return ret;
11783 /* Return location descriptor for signed comparison OP RTL. */
11785 static dw_loc_descr_ref
11786 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11787 machine_mode mem_mode)
11789 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11790 dw_loc_descr_ref op0, op1;
11791 int shift;
11793 if (op_mode == VOIDmode)
11794 op_mode = GET_MODE (XEXP (rtl, 1));
11795 if (op_mode == VOIDmode)
11796 return NULL;
11798 if (dwarf_strict
11799 && (GET_MODE_CLASS (op_mode) != MODE_INT
11800 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11801 return NULL;
11803 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11804 VAR_INIT_STATUS_INITIALIZED);
11805 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11806 VAR_INIT_STATUS_INITIALIZED);
11808 if (op0 == NULL || op1 == NULL)
11809 return NULL;
11811 if (GET_MODE_CLASS (op_mode) != MODE_INT
11812 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11813 return compare_loc_descriptor (op, op0, op1);
11815 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11817 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11818 dw_loc_descr_ref cvt;
11820 if (type_die == NULL)
11821 return NULL;
11822 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11823 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11824 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11825 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11826 add_loc_descr (&op0, cvt);
11827 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11828 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11829 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11830 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11831 add_loc_descr (&op1, cvt);
11832 return compare_loc_descriptor (op, op0, op1);
11835 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11836 /* For eq/ne, if the operands are known to be zero-extended,
11837 there is no need to do the fancy shifting up. */
11838 if (op == DW_OP_eq || op == DW_OP_ne)
11840 dw_loc_descr_ref last0, last1;
11841 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11843 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11845 /* deref_size zero extends, and for constants we can check
11846 whether they are zero extended or not. */
11847 if (((last0->dw_loc_opc == DW_OP_deref_size
11848 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11849 || (CONST_INT_P (XEXP (rtl, 0))
11850 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11851 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11852 && ((last1->dw_loc_opc == DW_OP_deref_size
11853 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11854 || (CONST_INT_P (XEXP (rtl, 1))
11855 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11856 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11857 return compare_loc_descriptor (op, op0, op1);
11859 /* EQ/NE comparison against constant in narrower type than
11860 DWARF2_ADDR_SIZE can be performed either as
11861 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11862 DW_OP_{eq,ne}
11864 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11865 DW_OP_{eq,ne}. Pick whatever is shorter. */
11866 if (CONST_INT_P (XEXP (rtl, 1))
11867 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11868 && (size_of_int_loc_descriptor (shift) + 1
11869 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11870 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11871 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11872 & GET_MODE_MASK (op_mode))))
11874 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11875 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11876 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11877 & GET_MODE_MASK (op_mode));
11878 return compare_loc_descriptor (op, op0, op1);
11881 add_loc_descr (&op0, int_loc_descriptor (shift));
11882 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11883 if (CONST_INT_P (XEXP (rtl, 1)))
11884 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11885 else
11887 add_loc_descr (&op1, int_loc_descriptor (shift));
11888 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11890 return compare_loc_descriptor (op, op0, op1);
11893 /* Return location descriptor for unsigned comparison OP RTL. */
11895 static dw_loc_descr_ref
11896 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11897 machine_mode mem_mode)
11899 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11900 dw_loc_descr_ref op0, op1;
11902 if (op_mode == VOIDmode)
11903 op_mode = GET_MODE (XEXP (rtl, 1));
11904 if (op_mode == VOIDmode)
11905 return NULL;
11906 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11907 return NULL;
11909 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11910 return NULL;
11912 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11913 VAR_INIT_STATUS_INITIALIZED);
11914 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11915 VAR_INIT_STATUS_INITIALIZED);
11917 if (op0 == NULL || op1 == NULL)
11918 return NULL;
11920 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11922 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11923 dw_loc_descr_ref last0, last1;
11924 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11926 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11928 if (CONST_INT_P (XEXP (rtl, 0)))
11929 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11930 /* deref_size zero extends, so no need to mask it again. */
11931 else if (last0->dw_loc_opc != DW_OP_deref_size
11932 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11934 add_loc_descr (&op0, int_loc_descriptor (mask));
11935 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11937 if (CONST_INT_P (XEXP (rtl, 1)))
11938 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11939 /* deref_size zero extends, so no need to mask it again. */
11940 else if (last1->dw_loc_opc != DW_OP_deref_size
11941 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11943 add_loc_descr (&op1, int_loc_descriptor (mask));
11944 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11947 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11949 HOST_WIDE_INT bias = 1;
11950 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11951 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11952 if (CONST_INT_P (XEXP (rtl, 1)))
11953 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11954 + INTVAL (XEXP (rtl, 1)));
11955 else
11956 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11957 bias, 0));
11959 return compare_loc_descriptor (op, op0, op1);
11962 /* Return location descriptor for {U,S}{MIN,MAX}. */
11964 static dw_loc_descr_ref
11965 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11966 machine_mode mem_mode)
11968 enum dwarf_location_atom op;
11969 dw_loc_descr_ref op0, op1, ret;
11970 dw_loc_descr_ref bra_node, drop_node;
11972 if (dwarf_strict
11973 && (GET_MODE_CLASS (mode) != MODE_INT
11974 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11975 return NULL;
11977 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11978 VAR_INIT_STATUS_INITIALIZED);
11979 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11980 VAR_INIT_STATUS_INITIALIZED);
11982 if (op0 == NULL || op1 == NULL)
11983 return NULL;
11985 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11986 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11987 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11988 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11990 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11992 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11993 add_loc_descr (&op0, int_loc_descriptor (mask));
11994 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11995 add_loc_descr (&op1, int_loc_descriptor (mask));
11996 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11998 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12000 HOST_WIDE_INT bias = 1;
12001 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12002 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12003 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12006 else if (GET_MODE_CLASS (mode) == MODE_INT
12007 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12009 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12010 add_loc_descr (&op0, int_loc_descriptor (shift));
12011 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12012 add_loc_descr (&op1, int_loc_descriptor (shift));
12013 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12015 else if (GET_MODE_CLASS (mode) == MODE_INT
12016 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12018 dw_die_ref type_die = base_type_for_mode (mode, 0);
12019 dw_loc_descr_ref cvt;
12020 if (type_die == NULL)
12021 return NULL;
12022 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12023 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12024 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12025 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12026 add_loc_descr (&op0, cvt);
12027 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12028 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12029 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12030 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12031 add_loc_descr (&op1, cvt);
12034 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12035 op = DW_OP_lt;
12036 else
12037 op = DW_OP_gt;
12038 ret = op0;
12039 add_loc_descr (&ret, op1);
12040 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12041 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12042 add_loc_descr (&ret, bra_node);
12043 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12044 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12045 add_loc_descr (&ret, drop_node);
12046 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12047 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12048 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12049 && GET_MODE_CLASS (mode) == MODE_INT
12050 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12051 ret = convert_descriptor_to_mode (mode, ret);
12052 return ret;
12055 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12056 but after converting arguments to type_die, afterwards
12057 convert back to unsigned. */
12059 static dw_loc_descr_ref
12060 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12061 machine_mode mode, machine_mode mem_mode)
12063 dw_loc_descr_ref cvt, op0, op1;
12065 if (type_die == NULL)
12066 return NULL;
12067 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12068 VAR_INIT_STATUS_INITIALIZED);
12069 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12070 VAR_INIT_STATUS_INITIALIZED);
12071 if (op0 == NULL || op1 == NULL)
12072 return NULL;
12073 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12074 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12075 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12076 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12077 add_loc_descr (&op0, cvt);
12078 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12079 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12080 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12081 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12082 add_loc_descr (&op1, cvt);
12083 add_loc_descr (&op0, op1);
12084 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12085 return convert_descriptor_to_mode (mode, op0);
12088 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12089 const0 is DW_OP_lit0 or corresponding typed constant,
12090 const1 is DW_OP_lit1 or corresponding typed constant
12091 and constMSB is constant with just the MSB bit set
12092 for the mode):
12093 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12094 L1: const0 DW_OP_swap
12095 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12096 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12097 L3: DW_OP_drop
12098 L4: DW_OP_nop
12100 CTZ is similar:
12101 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12102 L1: const0 DW_OP_swap
12103 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12104 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12105 L3: DW_OP_drop
12106 L4: DW_OP_nop
12108 FFS is similar:
12109 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12110 L1: const1 DW_OP_swap
12111 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12112 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12113 L3: DW_OP_drop
12114 L4: DW_OP_nop */
12116 static dw_loc_descr_ref
12117 clz_loc_descriptor (rtx rtl, machine_mode mode,
12118 machine_mode mem_mode)
12120 dw_loc_descr_ref op0, ret, tmp;
12121 HOST_WIDE_INT valv;
12122 dw_loc_descr_ref l1jump, l1label;
12123 dw_loc_descr_ref l2jump, l2label;
12124 dw_loc_descr_ref l3jump, l3label;
12125 dw_loc_descr_ref l4jump, l4label;
12126 rtx msb;
12128 if (GET_MODE_CLASS (mode) != MODE_INT
12129 || GET_MODE (XEXP (rtl, 0)) != mode)
12130 return NULL;
12132 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12133 VAR_INIT_STATUS_INITIALIZED);
12134 if (op0 == NULL)
12135 return NULL;
12136 ret = op0;
12137 if (GET_CODE (rtl) == CLZ)
12139 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12140 valv = GET_MODE_BITSIZE (mode);
12142 else if (GET_CODE (rtl) == FFS)
12143 valv = 0;
12144 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12145 valv = GET_MODE_BITSIZE (mode);
12146 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12147 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12148 add_loc_descr (&ret, l1jump);
12149 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12150 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12151 VAR_INIT_STATUS_INITIALIZED);
12152 if (tmp == NULL)
12153 return NULL;
12154 add_loc_descr (&ret, tmp);
12155 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12156 add_loc_descr (&ret, l4jump);
12157 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12158 ? const1_rtx : const0_rtx,
12159 mode, mem_mode,
12160 VAR_INIT_STATUS_INITIALIZED);
12161 if (l1label == NULL)
12162 return NULL;
12163 add_loc_descr (&ret, l1label);
12164 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12165 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12166 add_loc_descr (&ret, l2label);
12167 if (GET_CODE (rtl) != CLZ)
12168 msb = const1_rtx;
12169 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12170 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12171 << (GET_MODE_BITSIZE (mode) - 1));
12172 else
12173 msb = immed_wide_int_const
12174 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12175 GET_MODE_PRECISION (mode)), mode);
12176 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12177 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12178 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12179 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12180 else
12181 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12182 VAR_INIT_STATUS_INITIALIZED);
12183 if (tmp == NULL)
12184 return NULL;
12185 add_loc_descr (&ret, tmp);
12186 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12187 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12188 add_loc_descr (&ret, l3jump);
12189 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12190 VAR_INIT_STATUS_INITIALIZED);
12191 if (tmp == NULL)
12192 return NULL;
12193 add_loc_descr (&ret, tmp);
12194 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12195 ? DW_OP_shl : DW_OP_shr, 0, 0));
12196 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12197 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12198 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12199 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12200 add_loc_descr (&ret, l2jump);
12201 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12202 add_loc_descr (&ret, l3label);
12203 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12204 add_loc_descr (&ret, l4label);
12205 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12206 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12207 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12208 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12209 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12210 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12211 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12212 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12213 return ret;
12216 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12217 const1 is DW_OP_lit1 or corresponding typed constant):
12218 const0 DW_OP_swap
12219 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12220 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12221 L2: DW_OP_drop
12223 PARITY is similar:
12224 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12225 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12226 L2: DW_OP_drop */
12228 static dw_loc_descr_ref
12229 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12230 machine_mode mem_mode)
12232 dw_loc_descr_ref op0, ret, tmp;
12233 dw_loc_descr_ref l1jump, l1label;
12234 dw_loc_descr_ref l2jump, l2label;
12236 if (GET_MODE_CLASS (mode) != MODE_INT
12237 || GET_MODE (XEXP (rtl, 0)) != mode)
12238 return NULL;
12240 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12241 VAR_INIT_STATUS_INITIALIZED);
12242 if (op0 == NULL)
12243 return NULL;
12244 ret = op0;
12245 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12246 VAR_INIT_STATUS_INITIALIZED);
12247 if (tmp == NULL)
12248 return NULL;
12249 add_loc_descr (&ret, tmp);
12250 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12251 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12252 add_loc_descr (&ret, l1label);
12253 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12254 add_loc_descr (&ret, l2jump);
12255 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12256 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12257 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12258 VAR_INIT_STATUS_INITIALIZED);
12259 if (tmp == NULL)
12260 return NULL;
12261 add_loc_descr (&ret, tmp);
12262 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12263 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12264 ? DW_OP_plus : DW_OP_xor, 0, 0));
12265 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12266 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12267 VAR_INIT_STATUS_INITIALIZED);
12268 add_loc_descr (&ret, tmp);
12269 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12270 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12271 add_loc_descr (&ret, l1jump);
12272 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12273 add_loc_descr (&ret, l2label);
12274 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12275 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12276 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12277 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12278 return ret;
12281 /* BSWAP (constS is initial shift count, either 56 or 24):
12282 constS const0
12283 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12284 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12285 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12286 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12287 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12289 static dw_loc_descr_ref
12290 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12291 machine_mode mem_mode)
12293 dw_loc_descr_ref op0, ret, tmp;
12294 dw_loc_descr_ref l1jump, l1label;
12295 dw_loc_descr_ref l2jump, l2label;
12297 if (GET_MODE_CLASS (mode) != MODE_INT
12298 || BITS_PER_UNIT != 8
12299 || (GET_MODE_BITSIZE (mode) != 32
12300 && GET_MODE_BITSIZE (mode) != 64))
12301 return NULL;
12303 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12304 VAR_INIT_STATUS_INITIALIZED);
12305 if (op0 == NULL)
12306 return NULL;
12308 ret = op0;
12309 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12310 mode, mem_mode,
12311 VAR_INIT_STATUS_INITIALIZED);
12312 if (tmp == NULL)
12313 return NULL;
12314 add_loc_descr (&ret, tmp);
12315 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12316 VAR_INIT_STATUS_INITIALIZED);
12317 if (tmp == NULL)
12318 return NULL;
12319 add_loc_descr (&ret, tmp);
12320 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12321 add_loc_descr (&ret, l1label);
12322 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12323 mode, mem_mode,
12324 VAR_INIT_STATUS_INITIALIZED);
12325 add_loc_descr (&ret, tmp);
12326 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12327 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12328 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12329 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12330 VAR_INIT_STATUS_INITIALIZED);
12331 if (tmp == NULL)
12332 return NULL;
12333 add_loc_descr (&ret, tmp);
12334 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12335 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12336 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12337 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12338 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12339 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12340 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12341 VAR_INIT_STATUS_INITIALIZED);
12342 add_loc_descr (&ret, tmp);
12343 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12344 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12345 add_loc_descr (&ret, l2jump);
12346 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12347 VAR_INIT_STATUS_INITIALIZED);
12348 add_loc_descr (&ret, tmp);
12349 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12350 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12351 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12352 add_loc_descr (&ret, l1jump);
12353 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12354 add_loc_descr (&ret, l2label);
12355 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12356 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12357 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12358 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12359 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12360 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12361 return ret;
12364 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12365 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12366 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12367 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12369 ROTATERT is similar:
12370 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12371 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12372 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12374 static dw_loc_descr_ref
12375 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12376 machine_mode mem_mode)
12378 rtx rtlop1 = XEXP (rtl, 1);
12379 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12380 int i;
12382 if (GET_MODE_CLASS (mode) != MODE_INT)
12383 return NULL;
12385 if (GET_MODE (rtlop1) != VOIDmode
12386 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12387 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12388 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12389 VAR_INIT_STATUS_INITIALIZED);
12390 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12391 VAR_INIT_STATUS_INITIALIZED);
12392 if (op0 == NULL || op1 == NULL)
12393 return NULL;
12394 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12395 for (i = 0; i < 2; i++)
12397 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12398 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12399 mode, mem_mode,
12400 VAR_INIT_STATUS_INITIALIZED);
12401 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12402 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12403 ? DW_OP_const4u
12404 : HOST_BITS_PER_WIDE_INT == 64
12405 ? DW_OP_const8u : DW_OP_constu,
12406 GET_MODE_MASK (mode), 0);
12407 else
12408 mask[i] = NULL;
12409 if (mask[i] == NULL)
12410 return NULL;
12411 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12413 ret = op0;
12414 add_loc_descr (&ret, op1);
12415 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12416 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12417 if (GET_CODE (rtl) == ROTATERT)
12419 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12420 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12421 GET_MODE_BITSIZE (mode), 0));
12423 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12424 if (mask[0] != NULL)
12425 add_loc_descr (&ret, mask[0]);
12426 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12427 if (mask[1] != NULL)
12429 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12430 add_loc_descr (&ret, mask[1]);
12431 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12433 if (GET_CODE (rtl) == ROTATE)
12435 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12436 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12437 GET_MODE_BITSIZE (mode), 0));
12439 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12440 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12441 return ret;
12444 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12445 for DEBUG_PARAMETER_REF RTL. */
12447 static dw_loc_descr_ref
12448 parameter_ref_descriptor (rtx rtl)
12450 dw_loc_descr_ref ret;
12451 dw_die_ref ref;
12453 if (dwarf_strict)
12454 return NULL;
12455 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12456 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12457 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12458 if (ref)
12460 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12461 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12462 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12464 else
12466 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12467 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12469 return ret;
12472 /* The following routine converts the RTL for a variable or parameter
12473 (resident in memory) into an equivalent Dwarf representation of a
12474 mechanism for getting the address of that same variable onto the top of a
12475 hypothetical "address evaluation" stack.
12477 When creating memory location descriptors, we are effectively transforming
12478 the RTL for a memory-resident object into its Dwarf postfix expression
12479 equivalent. This routine recursively descends an RTL tree, turning
12480 it into Dwarf postfix code as it goes.
12482 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12484 MEM_MODE is the mode of the memory reference, needed to handle some
12485 autoincrement addressing modes.
12487 Return 0 if we can't represent the location. */
12489 dw_loc_descr_ref
12490 mem_loc_descriptor (rtx rtl, machine_mode mode,
12491 machine_mode mem_mode,
12492 enum var_init_status initialized)
12494 dw_loc_descr_ref mem_loc_result = NULL;
12495 enum dwarf_location_atom op;
12496 dw_loc_descr_ref op0, op1;
12497 rtx inner = NULL_RTX;
12499 if (mode == VOIDmode)
12500 mode = GET_MODE (rtl);
12502 /* Note that for a dynamically sized array, the location we will generate a
12503 description of here will be the lowest numbered location which is
12504 actually within the array. That's *not* necessarily the same as the
12505 zeroth element of the array. */
12507 rtl = targetm.delegitimize_address (rtl);
12509 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12510 return NULL;
12512 switch (GET_CODE (rtl))
12514 case POST_INC:
12515 case POST_DEC:
12516 case POST_MODIFY:
12517 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12519 case SUBREG:
12520 /* The case of a subreg may arise when we have a local (register)
12521 variable or a formal (register) parameter which doesn't quite fill
12522 up an entire register. For now, just assume that it is
12523 legitimate to make the Dwarf info refer to the whole register which
12524 contains the given subreg. */
12525 if (!subreg_lowpart_p (rtl))
12526 break;
12527 inner = SUBREG_REG (rtl);
12528 case TRUNCATE:
12529 if (inner == NULL_RTX)
12530 inner = XEXP (rtl, 0);
12531 if (GET_MODE_CLASS (mode) == MODE_INT
12532 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12533 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12534 #ifdef POINTERS_EXTEND_UNSIGNED
12535 || (mode == Pmode && mem_mode != VOIDmode)
12536 #endif
12538 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12540 mem_loc_result = mem_loc_descriptor (inner,
12541 GET_MODE (inner),
12542 mem_mode, initialized);
12543 break;
12545 if (dwarf_strict)
12546 break;
12547 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12548 break;
12549 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12550 && (GET_MODE_CLASS (mode) != MODE_INT
12551 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12552 break;
12553 else
12555 dw_die_ref type_die;
12556 dw_loc_descr_ref cvt;
12558 mem_loc_result = mem_loc_descriptor (inner,
12559 GET_MODE (inner),
12560 mem_mode, initialized);
12561 if (mem_loc_result == NULL)
12562 break;
12563 type_die = base_type_for_mode (mode,
12564 GET_MODE_CLASS (mode) == MODE_INT);
12565 if (type_die == NULL)
12567 mem_loc_result = NULL;
12568 break;
12570 if (GET_MODE_SIZE (mode)
12571 != GET_MODE_SIZE (GET_MODE (inner)))
12572 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12573 else
12574 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12575 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12576 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12577 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12578 add_loc_descr (&mem_loc_result, cvt);
12580 break;
12582 case REG:
12583 if (GET_MODE_CLASS (mode) != MODE_INT
12584 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12585 && rtl != arg_pointer_rtx
12586 && rtl != frame_pointer_rtx
12587 #ifdef POINTERS_EXTEND_UNSIGNED
12588 && (mode != Pmode || mem_mode == VOIDmode)
12589 #endif
12592 dw_die_ref type_die;
12593 unsigned int dbx_regnum;
12595 if (dwarf_strict)
12596 break;
12597 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12598 break;
12599 type_die = base_type_for_mode (mode,
12600 GET_MODE_CLASS (mode) == MODE_INT);
12601 if (type_die == NULL)
12602 break;
12604 dbx_regnum = dbx_reg_number (rtl);
12605 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12606 break;
12607 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12608 dbx_regnum, 0);
12609 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12610 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12611 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12612 break;
12614 /* Whenever a register number forms a part of the description of the
12615 method for calculating the (dynamic) address of a memory resident
12616 object, DWARF rules require the register number be referred to as
12617 a "base register". This distinction is not based in any way upon
12618 what category of register the hardware believes the given register
12619 belongs to. This is strictly DWARF terminology we're dealing with
12620 here. Note that in cases where the location of a memory-resident
12621 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12622 OP_CONST (0)) the actual DWARF location descriptor that we generate
12623 may just be OP_BASEREG (basereg). This may look deceptively like
12624 the object in question was allocated to a register (rather than in
12625 memory) so DWARF consumers need to be aware of the subtle
12626 distinction between OP_REG and OP_BASEREG. */
12627 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12628 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12629 else if (stack_realign_drap
12630 && crtl->drap_reg
12631 && crtl->args.internal_arg_pointer == rtl
12632 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12634 /* If RTL is internal_arg_pointer, which has been optimized
12635 out, use DRAP instead. */
12636 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12637 VAR_INIT_STATUS_INITIALIZED);
12639 break;
12641 case SIGN_EXTEND:
12642 case ZERO_EXTEND:
12643 if (GET_MODE_CLASS (mode) != MODE_INT)
12644 break;
12645 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12646 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12647 if (op0 == 0)
12648 break;
12649 else if (GET_CODE (rtl) == ZERO_EXTEND
12650 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12651 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12652 < HOST_BITS_PER_WIDE_INT
12653 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12654 to expand zero extend as two shifts instead of
12655 masking. */
12656 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12658 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12659 mem_loc_result = op0;
12660 add_loc_descr (&mem_loc_result,
12661 int_loc_descriptor (GET_MODE_MASK (imode)));
12662 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12664 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12666 int shift = DWARF2_ADDR_SIZE
12667 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12668 shift *= BITS_PER_UNIT;
12669 if (GET_CODE (rtl) == SIGN_EXTEND)
12670 op = DW_OP_shra;
12671 else
12672 op = DW_OP_shr;
12673 mem_loc_result = op0;
12674 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12675 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12676 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12677 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12679 else if (!dwarf_strict)
12681 dw_die_ref type_die1, type_die2;
12682 dw_loc_descr_ref cvt;
12684 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12685 GET_CODE (rtl) == ZERO_EXTEND);
12686 if (type_die1 == NULL)
12687 break;
12688 type_die2 = base_type_for_mode (mode, 1);
12689 if (type_die2 == NULL)
12690 break;
12691 mem_loc_result = op0;
12692 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12693 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12694 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12695 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12696 add_loc_descr (&mem_loc_result, cvt);
12697 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12698 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12699 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12700 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12701 add_loc_descr (&mem_loc_result, cvt);
12703 break;
12705 case MEM:
12707 rtx new_rtl = avoid_constant_pool_reference (rtl);
12708 if (new_rtl != rtl)
12710 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12711 initialized);
12712 if (mem_loc_result != NULL)
12713 return mem_loc_result;
12716 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12717 get_address_mode (rtl), mode,
12718 VAR_INIT_STATUS_INITIALIZED);
12719 if (mem_loc_result == NULL)
12720 mem_loc_result = tls_mem_loc_descriptor (rtl);
12721 if (mem_loc_result != NULL)
12723 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12724 || GET_MODE_CLASS (mode) != MODE_INT)
12726 dw_die_ref type_die;
12727 dw_loc_descr_ref deref;
12729 if (dwarf_strict)
12730 return NULL;
12731 type_die
12732 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12733 if (type_die == NULL)
12734 return NULL;
12735 deref = new_loc_descr (DW_OP_GNU_deref_type,
12736 GET_MODE_SIZE (mode), 0);
12737 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12738 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12739 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12740 add_loc_descr (&mem_loc_result, deref);
12742 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12743 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12744 else
12745 add_loc_descr (&mem_loc_result,
12746 new_loc_descr (DW_OP_deref_size,
12747 GET_MODE_SIZE (mode), 0));
12749 break;
12751 case LO_SUM:
12752 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12754 case LABEL_REF:
12755 /* Some ports can transform a symbol ref into a label ref, because
12756 the symbol ref is too far away and has to be dumped into a constant
12757 pool. */
12758 case CONST:
12759 case SYMBOL_REF:
12760 if ((GET_MODE_CLASS (mode) != MODE_INT
12761 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12762 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12763 #ifdef POINTERS_EXTEND_UNSIGNED
12764 && (mode != Pmode || mem_mode == VOIDmode)
12765 #endif
12767 break;
12768 if (GET_CODE (rtl) == SYMBOL_REF
12769 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12771 dw_loc_descr_ref temp;
12773 /* If this is not defined, we have no way to emit the data. */
12774 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12775 break;
12777 temp = new_addr_loc_descr (rtl, dtprel_true);
12779 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12780 add_loc_descr (&mem_loc_result, temp);
12782 break;
12785 if (!const_ok_for_output (rtl))
12786 break;
12788 symref:
12789 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12790 vec_safe_push (used_rtx_array, rtl);
12791 break;
12793 case CONCAT:
12794 case CONCATN:
12795 case VAR_LOCATION:
12796 case DEBUG_IMPLICIT_PTR:
12797 expansion_failed (NULL_TREE, rtl,
12798 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12799 return 0;
12801 case ENTRY_VALUE:
12802 if (dwarf_strict)
12803 return NULL;
12804 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12806 if (GET_MODE_CLASS (mode) != MODE_INT
12807 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12808 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12809 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12810 else
12812 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12813 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12814 return NULL;
12815 op0 = one_reg_loc_descriptor (dbx_regnum,
12816 VAR_INIT_STATUS_INITIALIZED);
12819 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12820 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12822 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12823 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12824 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12825 return NULL;
12827 else
12828 gcc_unreachable ();
12829 if (op0 == NULL)
12830 return NULL;
12831 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12832 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12833 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12834 break;
12836 case DEBUG_PARAMETER_REF:
12837 mem_loc_result = parameter_ref_descriptor (rtl);
12838 break;
12840 case PRE_MODIFY:
12841 /* Extract the PLUS expression nested inside and fall into
12842 PLUS code below. */
12843 rtl = XEXP (rtl, 1);
12844 goto plus;
12846 case PRE_INC:
12847 case PRE_DEC:
12848 /* Turn these into a PLUS expression and fall into the PLUS code
12849 below. */
12850 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12851 gen_int_mode (GET_CODE (rtl) == PRE_INC
12852 ? GET_MODE_UNIT_SIZE (mem_mode)
12853 : -GET_MODE_UNIT_SIZE (mem_mode),
12854 mode));
12856 /* ... fall through ... */
12858 case PLUS:
12859 plus:
12860 if (is_based_loc (rtl)
12861 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12862 || XEXP (rtl, 0) == arg_pointer_rtx
12863 || XEXP (rtl, 0) == frame_pointer_rtx)
12864 && GET_MODE_CLASS (mode) == MODE_INT)
12865 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12866 INTVAL (XEXP (rtl, 1)),
12867 VAR_INIT_STATUS_INITIALIZED);
12868 else
12870 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12871 VAR_INIT_STATUS_INITIALIZED);
12872 if (mem_loc_result == 0)
12873 break;
12875 if (CONST_INT_P (XEXP (rtl, 1))
12876 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12877 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12878 else
12880 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12881 VAR_INIT_STATUS_INITIALIZED);
12882 if (op1 == 0)
12883 return NULL;
12884 add_loc_descr (&mem_loc_result, op1);
12885 add_loc_descr (&mem_loc_result,
12886 new_loc_descr (DW_OP_plus, 0, 0));
12889 break;
12891 /* If a pseudo-reg is optimized away, it is possible for it to
12892 be replaced with a MEM containing a multiply or shift. */
12893 case MINUS:
12894 op = DW_OP_minus;
12895 goto do_binop;
12897 case MULT:
12898 op = DW_OP_mul;
12899 goto do_binop;
12901 case DIV:
12902 if (!dwarf_strict
12903 && GET_MODE_CLASS (mode) == MODE_INT
12904 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12906 mem_loc_result = typed_binop (DW_OP_div, rtl,
12907 base_type_for_mode (mode, 0),
12908 mode, mem_mode);
12909 break;
12911 op = DW_OP_div;
12912 goto do_binop;
12914 case UMOD:
12915 op = DW_OP_mod;
12916 goto do_binop;
12918 case ASHIFT:
12919 op = DW_OP_shl;
12920 goto do_shift;
12922 case ASHIFTRT:
12923 op = DW_OP_shra;
12924 goto do_shift;
12926 case LSHIFTRT:
12927 op = DW_OP_shr;
12928 goto do_shift;
12930 do_shift:
12931 if (GET_MODE_CLASS (mode) != MODE_INT)
12932 break;
12933 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12934 VAR_INIT_STATUS_INITIALIZED);
12936 rtx rtlop1 = XEXP (rtl, 1);
12937 if (GET_MODE (rtlop1) != VOIDmode
12938 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12939 < GET_MODE_BITSIZE (mode))
12940 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12941 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12942 VAR_INIT_STATUS_INITIALIZED);
12945 if (op0 == 0 || op1 == 0)
12946 break;
12948 mem_loc_result = op0;
12949 add_loc_descr (&mem_loc_result, op1);
12950 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12951 break;
12953 case AND:
12954 op = DW_OP_and;
12955 goto do_binop;
12957 case IOR:
12958 op = DW_OP_or;
12959 goto do_binop;
12961 case XOR:
12962 op = DW_OP_xor;
12963 goto do_binop;
12965 do_binop:
12966 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12967 VAR_INIT_STATUS_INITIALIZED);
12968 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12969 VAR_INIT_STATUS_INITIALIZED);
12971 if (op0 == 0 || op1 == 0)
12972 break;
12974 mem_loc_result = op0;
12975 add_loc_descr (&mem_loc_result, op1);
12976 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12977 break;
12979 case MOD:
12980 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12982 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12983 base_type_for_mode (mode, 0),
12984 mode, mem_mode);
12985 break;
12988 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12989 VAR_INIT_STATUS_INITIALIZED);
12990 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12991 VAR_INIT_STATUS_INITIALIZED);
12993 if (op0 == 0 || op1 == 0)
12994 break;
12996 mem_loc_result = op0;
12997 add_loc_descr (&mem_loc_result, op1);
12998 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12999 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13000 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13001 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13002 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13003 break;
13005 case UDIV:
13006 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13008 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13010 op = DW_OP_div;
13011 goto do_binop;
13013 mem_loc_result = typed_binop (DW_OP_div, rtl,
13014 base_type_for_mode (mode, 1),
13015 mode, mem_mode);
13017 break;
13019 case NOT:
13020 op = DW_OP_not;
13021 goto do_unop;
13023 case ABS:
13024 op = DW_OP_abs;
13025 goto do_unop;
13027 case NEG:
13028 op = DW_OP_neg;
13029 goto do_unop;
13031 do_unop:
13032 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13033 VAR_INIT_STATUS_INITIALIZED);
13035 if (op0 == 0)
13036 break;
13038 mem_loc_result = op0;
13039 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13040 break;
13042 case CONST_INT:
13043 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13044 #ifdef POINTERS_EXTEND_UNSIGNED
13045 || (mode == Pmode
13046 && mem_mode != VOIDmode
13047 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13048 #endif
13051 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13052 break;
13054 if (!dwarf_strict
13055 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13056 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13058 dw_die_ref type_die = base_type_for_mode (mode, 1);
13059 machine_mode amode;
13060 if (type_die == NULL)
13061 return NULL;
13062 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13063 MODE_INT, 0);
13064 if (INTVAL (rtl) >= 0
13065 && amode != BLKmode
13066 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13067 /* const DW_OP_GNU_convert <XXX> vs.
13068 DW_OP_GNU_const_type <XXX, 1, const>. */
13069 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13070 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13072 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13073 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13074 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13075 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13076 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13077 add_loc_descr (&mem_loc_result, op0);
13078 return mem_loc_result;
13080 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13081 INTVAL (rtl));
13082 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13083 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13084 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13085 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13086 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13087 else
13089 mem_loc_result->dw_loc_oprnd2.val_class
13090 = dw_val_class_const_double;
13091 mem_loc_result->dw_loc_oprnd2.v.val_double
13092 = double_int::from_shwi (INTVAL (rtl));
13095 break;
13097 case CONST_DOUBLE:
13098 if (!dwarf_strict)
13100 dw_die_ref type_die;
13102 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13103 CONST_DOUBLE rtx could represent either a large integer
13104 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13105 the value is always a floating point constant.
13107 When it is an integer, a CONST_DOUBLE is used whenever
13108 the constant requires 2 HWIs to be adequately represented.
13109 We output CONST_DOUBLEs as blocks. */
13110 if (mode == VOIDmode
13111 || (GET_MODE (rtl) == VOIDmode
13112 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13113 break;
13114 type_die = base_type_for_mode (mode,
13115 GET_MODE_CLASS (mode) == MODE_INT);
13116 if (type_die == NULL)
13117 return NULL;
13118 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13119 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13120 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13121 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13122 #if TARGET_SUPPORTS_WIDE_INT == 0
13123 if (!SCALAR_FLOAT_MODE_P (mode))
13125 mem_loc_result->dw_loc_oprnd2.val_class
13126 = dw_val_class_const_double;
13127 mem_loc_result->dw_loc_oprnd2.v.val_double
13128 = rtx_to_double_int (rtl);
13130 else
13131 #endif
13133 unsigned int length = GET_MODE_SIZE (mode);
13134 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13136 insert_float (rtl, array);
13137 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13138 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13139 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13140 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13143 break;
13145 case CONST_WIDE_INT:
13146 if (!dwarf_strict)
13148 dw_die_ref type_die;
13150 type_die = base_type_for_mode (mode,
13151 GET_MODE_CLASS (mode) == MODE_INT);
13152 if (type_die == NULL)
13153 return NULL;
13154 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13155 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13156 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13157 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13158 mem_loc_result->dw_loc_oprnd2.val_class
13159 = dw_val_class_wide_int;
13160 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13161 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13163 break;
13165 case EQ:
13166 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13167 break;
13169 case GE:
13170 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13171 break;
13173 case GT:
13174 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13175 break;
13177 case LE:
13178 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13179 break;
13181 case LT:
13182 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13183 break;
13185 case NE:
13186 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13187 break;
13189 case GEU:
13190 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13191 break;
13193 case GTU:
13194 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13195 break;
13197 case LEU:
13198 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13199 break;
13201 case LTU:
13202 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13203 break;
13205 case UMIN:
13206 case UMAX:
13207 if (GET_MODE_CLASS (mode) != MODE_INT)
13208 break;
13209 /* FALLTHRU */
13210 case SMIN:
13211 case SMAX:
13212 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13213 break;
13215 case ZERO_EXTRACT:
13216 case SIGN_EXTRACT:
13217 if (CONST_INT_P (XEXP (rtl, 1))
13218 && CONST_INT_P (XEXP (rtl, 2))
13219 && ((unsigned) INTVAL (XEXP (rtl, 1))
13220 + (unsigned) INTVAL (XEXP (rtl, 2))
13221 <= GET_MODE_BITSIZE (mode))
13222 && GET_MODE_CLASS (mode) == MODE_INT
13223 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13224 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13226 int shift, size;
13227 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13228 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13229 if (op0 == 0)
13230 break;
13231 if (GET_CODE (rtl) == SIGN_EXTRACT)
13232 op = DW_OP_shra;
13233 else
13234 op = DW_OP_shr;
13235 mem_loc_result = op0;
13236 size = INTVAL (XEXP (rtl, 1));
13237 shift = INTVAL (XEXP (rtl, 2));
13238 if (BITS_BIG_ENDIAN)
13239 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13240 - shift - size;
13241 if (shift + size != (int) DWARF2_ADDR_SIZE)
13243 add_loc_descr (&mem_loc_result,
13244 int_loc_descriptor (DWARF2_ADDR_SIZE
13245 - shift - size));
13246 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13248 if (size != (int) DWARF2_ADDR_SIZE)
13250 add_loc_descr (&mem_loc_result,
13251 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13252 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13255 break;
13257 case IF_THEN_ELSE:
13259 dw_loc_descr_ref op2, bra_node, drop_node;
13260 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13261 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13262 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13263 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13264 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13265 VAR_INIT_STATUS_INITIALIZED);
13266 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13267 VAR_INIT_STATUS_INITIALIZED);
13268 if (op0 == NULL || op1 == NULL || op2 == NULL)
13269 break;
13271 mem_loc_result = op1;
13272 add_loc_descr (&mem_loc_result, op2);
13273 add_loc_descr (&mem_loc_result, op0);
13274 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13275 add_loc_descr (&mem_loc_result, bra_node);
13276 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13277 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13278 add_loc_descr (&mem_loc_result, drop_node);
13279 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13280 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13282 break;
13284 case FLOAT_EXTEND:
13285 case FLOAT_TRUNCATE:
13286 case FLOAT:
13287 case UNSIGNED_FLOAT:
13288 case FIX:
13289 case UNSIGNED_FIX:
13290 if (!dwarf_strict)
13292 dw_die_ref type_die;
13293 dw_loc_descr_ref cvt;
13295 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13296 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13297 if (op0 == NULL)
13298 break;
13299 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13300 && (GET_CODE (rtl) == FLOAT
13301 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13302 <= DWARF2_ADDR_SIZE))
13304 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13305 GET_CODE (rtl) == UNSIGNED_FLOAT);
13306 if (type_die == NULL)
13307 break;
13308 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13309 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13310 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13311 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13312 add_loc_descr (&op0, cvt);
13314 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13315 if (type_die == NULL)
13316 break;
13317 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13318 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13319 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13320 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13321 add_loc_descr (&op0, cvt);
13322 if (GET_MODE_CLASS (mode) == MODE_INT
13323 && (GET_CODE (rtl) == FIX
13324 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13326 op0 = convert_descriptor_to_mode (mode, op0);
13327 if (op0 == NULL)
13328 break;
13330 mem_loc_result = op0;
13332 break;
13334 case CLZ:
13335 case CTZ:
13336 case FFS:
13337 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13338 break;
13340 case POPCOUNT:
13341 case PARITY:
13342 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13343 break;
13345 case BSWAP:
13346 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13347 break;
13349 case ROTATE:
13350 case ROTATERT:
13351 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13352 break;
13354 case COMPARE:
13355 /* In theory, we could implement the above. */
13356 /* DWARF cannot represent the unsigned compare operations
13357 natively. */
13358 case SS_MULT:
13359 case US_MULT:
13360 case SS_DIV:
13361 case US_DIV:
13362 case SS_PLUS:
13363 case US_PLUS:
13364 case SS_MINUS:
13365 case US_MINUS:
13366 case SS_NEG:
13367 case US_NEG:
13368 case SS_ABS:
13369 case SS_ASHIFT:
13370 case US_ASHIFT:
13371 case SS_TRUNCATE:
13372 case US_TRUNCATE:
13373 case UNORDERED:
13374 case ORDERED:
13375 case UNEQ:
13376 case UNGE:
13377 case UNGT:
13378 case UNLE:
13379 case UNLT:
13380 case LTGT:
13381 case FRACT_CONVERT:
13382 case UNSIGNED_FRACT_CONVERT:
13383 case SAT_FRACT:
13384 case UNSIGNED_SAT_FRACT:
13385 case SQRT:
13386 case ASM_OPERANDS:
13387 case VEC_MERGE:
13388 case VEC_SELECT:
13389 case VEC_CONCAT:
13390 case VEC_DUPLICATE:
13391 case UNSPEC:
13392 case HIGH:
13393 case FMA:
13394 case STRICT_LOW_PART:
13395 case CONST_VECTOR:
13396 case CONST_FIXED:
13397 case CLRSB:
13398 case CLOBBER:
13399 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13400 can't express it in the debug info. This can happen e.g. with some
13401 TLS UNSPECs. */
13402 break;
13404 case CONST_STRING:
13405 resolve_one_addr (&rtl);
13406 goto symref;
13408 default:
13409 #ifdef ENABLE_CHECKING
13410 print_rtl (stderr, rtl);
13411 gcc_unreachable ();
13412 #else
13413 break;
13414 #endif
13417 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13418 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13420 return mem_loc_result;
13423 /* Return a descriptor that describes the concatenation of two locations.
13424 This is typically a complex variable. */
13426 static dw_loc_descr_ref
13427 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13429 dw_loc_descr_ref cc_loc_result = NULL;
13430 dw_loc_descr_ref x0_ref
13431 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13432 dw_loc_descr_ref x1_ref
13433 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13435 if (x0_ref == 0 || x1_ref == 0)
13436 return 0;
13438 cc_loc_result = x0_ref;
13439 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13441 add_loc_descr (&cc_loc_result, x1_ref);
13442 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13444 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13445 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13447 return cc_loc_result;
13450 /* Return a descriptor that describes the concatenation of N
13451 locations. */
13453 static dw_loc_descr_ref
13454 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13456 unsigned int i;
13457 dw_loc_descr_ref cc_loc_result = NULL;
13458 unsigned int n = XVECLEN (concatn, 0);
13460 for (i = 0; i < n; ++i)
13462 dw_loc_descr_ref ref;
13463 rtx x = XVECEXP (concatn, 0, i);
13465 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13466 if (ref == NULL)
13467 return NULL;
13469 add_loc_descr (&cc_loc_result, ref);
13470 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13473 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13474 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13476 return cc_loc_result;
13479 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13480 for DEBUG_IMPLICIT_PTR RTL. */
13482 static dw_loc_descr_ref
13483 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13485 dw_loc_descr_ref ret;
13486 dw_die_ref ref;
13488 if (dwarf_strict)
13489 return NULL;
13490 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13491 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13492 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13493 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13494 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13495 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13496 if (ref)
13498 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13499 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13500 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13502 else
13504 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13505 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13507 return ret;
13510 /* Output a proper Dwarf location descriptor for a variable or parameter
13511 which is either allocated in a register or in a memory location. For a
13512 register, we just generate an OP_REG and the register number. For a
13513 memory location we provide a Dwarf postfix expression describing how to
13514 generate the (dynamic) address of the object onto the address stack.
13516 MODE is mode of the decl if this loc_descriptor is going to be used in
13517 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13518 allowed, VOIDmode otherwise.
13520 If we don't know how to describe it, return 0. */
13522 static dw_loc_descr_ref
13523 loc_descriptor (rtx rtl, machine_mode mode,
13524 enum var_init_status initialized)
13526 dw_loc_descr_ref loc_result = NULL;
13528 switch (GET_CODE (rtl))
13530 case SUBREG:
13531 /* The case of a subreg may arise when we have a local (register)
13532 variable or a formal (register) parameter which doesn't quite fill
13533 up an entire register. For now, just assume that it is
13534 legitimate to make the Dwarf info refer to the whole register which
13535 contains the given subreg. */
13536 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13537 loc_result = loc_descriptor (SUBREG_REG (rtl),
13538 GET_MODE (SUBREG_REG (rtl)), initialized);
13539 else
13540 goto do_default;
13541 break;
13543 case REG:
13544 loc_result = reg_loc_descriptor (rtl, initialized);
13545 break;
13547 case MEM:
13548 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13549 GET_MODE (rtl), initialized);
13550 if (loc_result == NULL)
13551 loc_result = tls_mem_loc_descriptor (rtl);
13552 if (loc_result == NULL)
13554 rtx new_rtl = avoid_constant_pool_reference (rtl);
13555 if (new_rtl != rtl)
13556 loc_result = loc_descriptor (new_rtl, mode, initialized);
13558 break;
13560 case CONCAT:
13561 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13562 initialized);
13563 break;
13565 case CONCATN:
13566 loc_result = concatn_loc_descriptor (rtl, initialized);
13567 break;
13569 case VAR_LOCATION:
13570 /* Single part. */
13571 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13573 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13574 if (GET_CODE (loc) == EXPR_LIST)
13575 loc = XEXP (loc, 0);
13576 loc_result = loc_descriptor (loc, mode, initialized);
13577 break;
13580 rtl = XEXP (rtl, 1);
13581 /* FALLTHRU */
13583 case PARALLEL:
13585 rtvec par_elems = XVEC (rtl, 0);
13586 int num_elem = GET_NUM_ELEM (par_elems);
13587 machine_mode mode;
13588 int i;
13590 /* Create the first one, so we have something to add to. */
13591 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13592 VOIDmode, initialized);
13593 if (loc_result == NULL)
13594 return NULL;
13595 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13596 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13597 for (i = 1; i < num_elem; i++)
13599 dw_loc_descr_ref temp;
13601 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13602 VOIDmode, initialized);
13603 if (temp == NULL)
13604 return NULL;
13605 add_loc_descr (&loc_result, temp);
13606 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13607 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13610 break;
13612 case CONST_INT:
13613 if (mode != VOIDmode && mode != BLKmode)
13614 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13615 INTVAL (rtl));
13616 break;
13618 case CONST_DOUBLE:
13619 if (mode == VOIDmode)
13620 mode = GET_MODE (rtl);
13622 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13624 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13626 /* Note that a CONST_DOUBLE rtx could represent either an integer
13627 or a floating-point constant. A CONST_DOUBLE is used whenever
13628 the constant requires more than one word in order to be
13629 adequately represented. We output CONST_DOUBLEs as blocks. */
13630 loc_result = new_loc_descr (DW_OP_implicit_value,
13631 GET_MODE_SIZE (mode), 0);
13632 #if TARGET_SUPPORTS_WIDE_INT == 0
13633 if (!SCALAR_FLOAT_MODE_P (mode))
13635 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13636 loc_result->dw_loc_oprnd2.v.val_double
13637 = rtx_to_double_int (rtl);
13639 else
13640 #endif
13642 unsigned int length = GET_MODE_SIZE (mode);
13643 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13645 insert_float (rtl, array);
13646 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13647 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13648 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13649 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13652 break;
13654 case CONST_WIDE_INT:
13655 if (mode == VOIDmode)
13656 mode = GET_MODE (rtl);
13658 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13660 loc_result = new_loc_descr (DW_OP_implicit_value,
13661 GET_MODE_SIZE (mode), 0);
13662 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13663 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13664 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13666 break;
13668 case CONST_VECTOR:
13669 if (mode == VOIDmode)
13670 mode = GET_MODE (rtl);
13672 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13674 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13675 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13676 unsigned char *array
13677 = ggc_vec_alloc<unsigned char> (length * elt_size);
13678 unsigned int i;
13679 unsigned char *p;
13680 machine_mode imode = GET_MODE_INNER (mode);
13682 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13683 switch (GET_MODE_CLASS (mode))
13685 case MODE_VECTOR_INT:
13686 for (i = 0, p = array; i < length; i++, p += elt_size)
13688 rtx elt = CONST_VECTOR_ELT (rtl, i);
13689 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13691 break;
13693 case MODE_VECTOR_FLOAT:
13694 for (i = 0, p = array; i < length; i++, p += elt_size)
13696 rtx elt = CONST_VECTOR_ELT (rtl, i);
13697 insert_float (elt, p);
13699 break;
13701 default:
13702 gcc_unreachable ();
13705 loc_result = new_loc_descr (DW_OP_implicit_value,
13706 length * elt_size, 0);
13707 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13708 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13709 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13710 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13712 break;
13714 case CONST:
13715 if (mode == VOIDmode
13716 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13717 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13718 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13720 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13721 break;
13723 /* FALLTHROUGH */
13724 case SYMBOL_REF:
13725 if (!const_ok_for_output (rtl))
13726 break;
13727 case LABEL_REF:
13728 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13729 && (dwarf_version >= 4 || !dwarf_strict))
13731 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13732 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13733 vec_safe_push (used_rtx_array, rtl);
13735 break;
13737 case DEBUG_IMPLICIT_PTR:
13738 loc_result = implicit_ptr_descriptor (rtl, 0);
13739 break;
13741 case PLUS:
13742 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13743 && CONST_INT_P (XEXP (rtl, 1)))
13745 loc_result
13746 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13747 break;
13749 /* FALLTHRU */
13750 do_default:
13751 default:
13752 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13753 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13754 && dwarf_version >= 4)
13755 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13757 /* Value expression. */
13758 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13759 if (loc_result)
13760 add_loc_descr (&loc_result,
13761 new_loc_descr (DW_OP_stack_value, 0, 0));
13763 break;
13766 return loc_result;
13769 /* We need to figure out what section we should use as the base for the
13770 address ranges where a given location is valid.
13771 1. If this particular DECL has a section associated with it, use that.
13772 2. If this function has a section associated with it, use that.
13773 3. Otherwise, use the text section.
13774 XXX: If you split a variable across multiple sections, we won't notice. */
13776 static const char *
13777 secname_for_decl (const_tree decl)
13779 const char *secname;
13781 if (VAR_OR_FUNCTION_DECL_P (decl)
13782 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13783 && DECL_SECTION_NAME (decl))
13784 secname = DECL_SECTION_NAME (decl);
13785 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13786 secname = DECL_SECTION_NAME (current_function_decl);
13787 else if (cfun && in_cold_section_p)
13788 secname = crtl->subsections.cold_section_label;
13789 else
13790 secname = text_section_label;
13792 return secname;
13795 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13797 static bool
13798 decl_by_reference_p (tree decl)
13800 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13801 || TREE_CODE (decl) == VAR_DECL)
13802 && DECL_BY_REFERENCE (decl));
13805 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13806 for VARLOC. */
13808 static dw_loc_descr_ref
13809 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13810 enum var_init_status initialized)
13812 int have_address = 0;
13813 dw_loc_descr_ref descr;
13814 machine_mode mode;
13816 if (want_address != 2)
13818 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13819 /* Single part. */
13820 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13822 varloc = PAT_VAR_LOCATION_LOC (varloc);
13823 if (GET_CODE (varloc) == EXPR_LIST)
13824 varloc = XEXP (varloc, 0);
13825 mode = GET_MODE (varloc);
13826 if (MEM_P (varloc))
13828 rtx addr = XEXP (varloc, 0);
13829 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13830 mode, initialized);
13831 if (descr)
13832 have_address = 1;
13833 else
13835 rtx x = avoid_constant_pool_reference (varloc);
13836 if (x != varloc)
13837 descr = mem_loc_descriptor (x, mode, VOIDmode,
13838 initialized);
13841 else
13842 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13844 else
13845 return 0;
13847 else
13849 if (GET_CODE (varloc) == VAR_LOCATION)
13850 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13851 else
13852 mode = DECL_MODE (loc);
13853 descr = loc_descriptor (varloc, mode, initialized);
13854 have_address = 1;
13857 if (!descr)
13858 return 0;
13860 if (want_address == 2 && !have_address
13861 && (dwarf_version >= 4 || !dwarf_strict))
13863 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13865 expansion_failed (loc, NULL_RTX,
13866 "DWARF address size mismatch");
13867 return 0;
13869 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13870 have_address = 1;
13872 /* Show if we can't fill the request for an address. */
13873 if (want_address && !have_address)
13875 expansion_failed (loc, NULL_RTX,
13876 "Want address and only have value");
13877 return 0;
13880 /* If we've got an address and don't want one, dereference. */
13881 if (!want_address && have_address)
13883 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13884 enum dwarf_location_atom op;
13886 if (size > DWARF2_ADDR_SIZE || size == -1)
13888 expansion_failed (loc, NULL_RTX,
13889 "DWARF address size mismatch");
13890 return 0;
13892 else if (size == DWARF2_ADDR_SIZE)
13893 op = DW_OP_deref;
13894 else
13895 op = DW_OP_deref_size;
13897 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13900 return descr;
13903 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13904 if it is not possible. */
13906 static dw_loc_descr_ref
13907 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13909 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13910 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13911 else if (dwarf_version >= 3 || !dwarf_strict)
13912 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13913 else
13914 return NULL;
13917 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13918 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13920 static dw_loc_descr_ref
13921 dw_sra_loc_expr (tree decl, rtx loc)
13923 rtx p;
13924 unsigned int padsize = 0;
13925 dw_loc_descr_ref descr, *descr_tail;
13926 unsigned HOST_WIDE_INT decl_size;
13927 rtx varloc;
13928 enum var_init_status initialized;
13930 if (DECL_SIZE (decl) == NULL
13931 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13932 return NULL;
13934 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13935 descr = NULL;
13936 descr_tail = &descr;
13938 for (p = loc; p; p = XEXP (p, 1))
13940 unsigned int bitsize = decl_piece_bitsize (p);
13941 rtx loc_note = *decl_piece_varloc_ptr (p);
13942 dw_loc_descr_ref cur_descr;
13943 dw_loc_descr_ref *tail, last = NULL;
13944 unsigned int opsize = 0;
13946 if (loc_note == NULL_RTX
13947 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13949 padsize += bitsize;
13950 continue;
13952 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13953 varloc = NOTE_VAR_LOCATION (loc_note);
13954 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13955 if (cur_descr == NULL)
13957 padsize += bitsize;
13958 continue;
13961 /* Check that cur_descr either doesn't use
13962 DW_OP_*piece operations, or their sum is equal
13963 to bitsize. Otherwise we can't embed it. */
13964 for (tail = &cur_descr; *tail != NULL;
13965 tail = &(*tail)->dw_loc_next)
13966 if ((*tail)->dw_loc_opc == DW_OP_piece)
13968 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13969 * BITS_PER_UNIT;
13970 last = *tail;
13972 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13974 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13975 last = *tail;
13978 if (last != NULL && opsize != bitsize)
13980 padsize += bitsize;
13981 /* Discard the current piece of the descriptor and release any
13982 addr_table entries it uses. */
13983 remove_loc_list_addr_table_entries (cur_descr);
13984 continue;
13987 /* If there is a hole, add DW_OP_*piece after empty DWARF
13988 expression, which means that those bits are optimized out. */
13989 if (padsize)
13991 if (padsize > decl_size)
13993 remove_loc_list_addr_table_entries (cur_descr);
13994 goto discard_descr;
13996 decl_size -= padsize;
13997 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13998 if (*descr_tail == NULL)
14000 remove_loc_list_addr_table_entries (cur_descr);
14001 goto discard_descr;
14003 descr_tail = &(*descr_tail)->dw_loc_next;
14004 padsize = 0;
14006 *descr_tail = cur_descr;
14007 descr_tail = tail;
14008 if (bitsize > decl_size)
14009 goto discard_descr;
14010 decl_size -= bitsize;
14011 if (last == NULL)
14013 HOST_WIDE_INT offset = 0;
14014 if (GET_CODE (varloc) == VAR_LOCATION
14015 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14017 varloc = PAT_VAR_LOCATION_LOC (varloc);
14018 if (GET_CODE (varloc) == EXPR_LIST)
14019 varloc = XEXP (varloc, 0);
14023 if (GET_CODE (varloc) == CONST
14024 || GET_CODE (varloc) == SIGN_EXTEND
14025 || GET_CODE (varloc) == ZERO_EXTEND)
14026 varloc = XEXP (varloc, 0);
14027 else if (GET_CODE (varloc) == SUBREG)
14028 varloc = SUBREG_REG (varloc);
14029 else
14030 break;
14032 while (1);
14033 /* DW_OP_bit_size offset should be zero for register
14034 or implicit location descriptions and empty location
14035 descriptions, but for memory addresses needs big endian
14036 adjustment. */
14037 if (MEM_P (varloc))
14039 unsigned HOST_WIDE_INT memsize
14040 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14041 if (memsize != bitsize)
14043 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14044 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14045 goto discard_descr;
14046 if (memsize < bitsize)
14047 goto discard_descr;
14048 if (BITS_BIG_ENDIAN)
14049 offset = memsize - bitsize;
14053 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14054 if (*descr_tail == NULL)
14055 goto discard_descr;
14056 descr_tail = &(*descr_tail)->dw_loc_next;
14060 /* If there were any non-empty expressions, add padding till the end of
14061 the decl. */
14062 if (descr != NULL && decl_size != 0)
14064 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14065 if (*descr_tail == NULL)
14066 goto discard_descr;
14068 return descr;
14070 discard_descr:
14071 /* Discard the descriptor and release any addr_table entries it uses. */
14072 remove_loc_list_addr_table_entries (descr);
14073 return NULL;
14076 /* Return the dwarf representation of the location list LOC_LIST of
14077 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14078 function. */
14080 static dw_loc_list_ref
14081 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14083 const char *endname, *secname;
14084 rtx varloc;
14085 enum var_init_status initialized;
14086 struct var_loc_node *node;
14087 dw_loc_descr_ref descr;
14088 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14089 dw_loc_list_ref list = NULL;
14090 dw_loc_list_ref *listp = &list;
14092 /* Now that we know what section we are using for a base,
14093 actually construct the list of locations.
14094 The first location information is what is passed to the
14095 function that creates the location list, and the remaining
14096 locations just get added on to that list.
14097 Note that we only know the start address for a location
14098 (IE location changes), so to build the range, we use
14099 the range [current location start, next location start].
14100 This means we have to special case the last node, and generate
14101 a range of [last location start, end of function label]. */
14103 secname = secname_for_decl (decl);
14105 for (node = loc_list->first; node; node = node->next)
14106 if (GET_CODE (node->loc) == EXPR_LIST
14107 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14109 if (GET_CODE (node->loc) == EXPR_LIST)
14111 /* This requires DW_OP_{,bit_}piece, which is not usable
14112 inside DWARF expressions. */
14113 if (want_address != 2)
14114 continue;
14115 descr = dw_sra_loc_expr (decl, node->loc);
14116 if (descr == NULL)
14117 continue;
14119 else
14121 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14122 varloc = NOTE_VAR_LOCATION (node->loc);
14123 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14125 if (descr)
14127 bool range_across_switch = false;
14128 /* If section switch happens in between node->label
14129 and node->next->label (or end of function) and
14130 we can't emit it as a single entry list,
14131 emit two ranges, first one ending at the end
14132 of first partition and second one starting at the
14133 beginning of second partition. */
14134 if (node == loc_list->last_before_switch
14135 && (node != loc_list->first || loc_list->first->next)
14136 && current_function_decl)
14138 endname = cfun->fde->dw_fde_end;
14139 range_across_switch = true;
14141 /* The variable has a location between NODE->LABEL and
14142 NODE->NEXT->LABEL. */
14143 else if (node->next)
14144 endname = node->next->label;
14145 /* If the variable has a location at the last label
14146 it keeps its location until the end of function. */
14147 else if (!current_function_decl)
14148 endname = text_end_label;
14149 else
14151 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14152 current_function_funcdef_no);
14153 endname = ggc_strdup (label_id);
14156 *listp = new_loc_list (descr, node->label, endname, secname);
14157 if (TREE_CODE (decl) == PARM_DECL
14158 && node == loc_list->first
14159 && NOTE_P (node->loc)
14160 && strcmp (node->label, endname) == 0)
14161 (*listp)->force = true;
14162 listp = &(*listp)->dw_loc_next;
14164 if (range_across_switch)
14166 if (GET_CODE (node->loc) == EXPR_LIST)
14167 descr = dw_sra_loc_expr (decl, node->loc);
14168 else
14170 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14171 varloc = NOTE_VAR_LOCATION (node->loc);
14172 descr = dw_loc_list_1 (decl, varloc, want_address,
14173 initialized);
14175 gcc_assert (descr);
14176 /* The variable has a location between NODE->LABEL and
14177 NODE->NEXT->LABEL. */
14178 if (node->next)
14179 endname = node->next->label;
14180 else
14181 endname = cfun->fde->dw_fde_second_end;
14182 *listp = new_loc_list (descr,
14183 cfun->fde->dw_fde_second_begin,
14184 endname, secname);
14185 listp = &(*listp)->dw_loc_next;
14190 /* Try to avoid the overhead of a location list emitting a location
14191 expression instead, but only if we didn't have more than one
14192 location entry in the first place. If some entries were not
14193 representable, we don't want to pretend a single entry that was
14194 applies to the entire scope in which the variable is
14195 available. */
14196 if (list && loc_list->first->next)
14197 gen_llsym (list);
14199 return list;
14202 /* Return if the loc_list has only single element and thus can be represented
14203 as location description. */
14205 static bool
14206 single_element_loc_list_p (dw_loc_list_ref list)
14208 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14209 return !list->ll_symbol;
14212 /* To each location in list LIST add loc descr REF. */
14214 static void
14215 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14217 dw_loc_descr_ref copy;
14218 add_loc_descr (&list->expr, ref);
14219 list = list->dw_loc_next;
14220 while (list)
14222 copy = ggc_alloc<dw_loc_descr_node> ();
14223 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14224 add_loc_descr (&list->expr, copy);
14225 while (copy->dw_loc_next)
14227 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14228 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14229 copy->dw_loc_next = new_copy;
14230 copy = new_copy;
14232 list = list->dw_loc_next;
14236 /* Given two lists RET and LIST
14237 produce location list that is result of adding expression in LIST
14238 to expression in RET on each position in program.
14239 Might be destructive on both RET and LIST.
14241 TODO: We handle only simple cases of RET or LIST having at most one
14242 element. General case would inolve sorting the lists in program order
14243 and merging them that will need some additional work.
14244 Adding that will improve quality of debug info especially for SRA-ed
14245 structures. */
14247 static void
14248 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14250 if (!list)
14251 return;
14252 if (!*ret)
14254 *ret = list;
14255 return;
14257 if (!list->dw_loc_next)
14259 add_loc_descr_to_each (*ret, list->expr);
14260 return;
14262 if (!(*ret)->dw_loc_next)
14264 add_loc_descr_to_each (list, (*ret)->expr);
14265 *ret = list;
14266 return;
14268 expansion_failed (NULL_TREE, NULL_RTX,
14269 "Don't know how to merge two non-trivial"
14270 " location lists.\n");
14271 *ret = NULL;
14272 return;
14275 /* LOC is constant expression. Try a luck, look it up in constant
14276 pool and return its loc_descr of its address. */
14278 static dw_loc_descr_ref
14279 cst_pool_loc_descr (tree loc)
14281 /* Get an RTL for this, if something has been emitted. */
14282 rtx rtl = lookup_constant_def (loc);
14284 if (!rtl || !MEM_P (rtl))
14286 gcc_assert (!rtl);
14287 return 0;
14289 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14291 /* TODO: We might get more coverage if we was actually delaying expansion
14292 of all expressions till end of compilation when constant pools are fully
14293 populated. */
14294 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14296 expansion_failed (loc, NULL_RTX,
14297 "CST value in contant pool but not marked.");
14298 return 0;
14300 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14301 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14304 /* Return dw_loc_list representing address of addr_expr LOC
14305 by looking for inner INDIRECT_REF expression and turning
14306 it into simple arithmetics.
14308 See loc_list_from_tree for the meaning of CONTEXT. */
14310 static dw_loc_list_ref
14311 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14312 const loc_descr_context *context)
14314 tree obj, offset;
14315 HOST_WIDE_INT bitsize, bitpos, bytepos;
14316 machine_mode mode;
14317 int unsignedp, volatilep = 0;
14318 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14320 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14321 &bitsize, &bitpos, &offset, &mode,
14322 &unsignedp, &volatilep, false);
14323 STRIP_NOPS (obj);
14324 if (bitpos % BITS_PER_UNIT)
14326 expansion_failed (loc, NULL_RTX, "bitfield access");
14327 return 0;
14329 if (!INDIRECT_REF_P (obj))
14331 expansion_failed (obj,
14332 NULL_RTX, "no indirect ref in inner refrence");
14333 return 0;
14335 if (!offset && !bitpos)
14336 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14337 context);
14338 else if (toplev
14339 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14340 && (dwarf_version >= 4 || !dwarf_strict))
14342 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14343 if (!list_ret)
14344 return 0;
14345 if (offset)
14347 /* Variable offset. */
14348 list_ret1 = loc_list_from_tree (offset, 0, context);
14349 if (list_ret1 == 0)
14350 return 0;
14351 add_loc_list (&list_ret, list_ret1);
14352 if (!list_ret)
14353 return 0;
14354 add_loc_descr_to_each (list_ret,
14355 new_loc_descr (DW_OP_plus, 0, 0));
14357 bytepos = bitpos / BITS_PER_UNIT;
14358 if (bytepos > 0)
14359 add_loc_descr_to_each (list_ret,
14360 new_loc_descr (DW_OP_plus_uconst,
14361 bytepos, 0));
14362 else if (bytepos < 0)
14363 loc_list_plus_const (list_ret, bytepos);
14364 add_loc_descr_to_each (list_ret,
14365 new_loc_descr (DW_OP_stack_value, 0, 0));
14367 return list_ret;
14371 /* Helper structure for location descriptions generation. */
14372 struct loc_descr_context
14374 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14375 NULL_TREE if DW_OP_push_object_address in invalid for this location
14376 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14377 tree context_type;
14378 /* The ..._DECL node that should be translated as a
14379 DW_OP_push_object_address operation. */
14380 tree base_decl;
14383 /* Generate Dwarf location list representing LOC.
14384 If WANT_ADDRESS is false, expression computing LOC will be computed
14385 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14386 if WANT_ADDRESS is 2, expression computing address useable in location
14387 will be returned (i.e. DW_OP_reg can be used
14388 to refer to register values).
14390 CONTEXT provides information to customize the location descriptions
14391 generation. Its context_type field specifies what type is implicitly
14392 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14393 will not be generated.
14395 If CONTEXT is NULL, the behavior is the same as if both context_type and
14396 base_decl fields were NULL_TREE. */
14398 static dw_loc_list_ref
14399 loc_list_from_tree (tree loc, int want_address,
14400 const struct loc_descr_context *context)
14402 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14403 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14404 int have_address = 0;
14405 enum dwarf_location_atom op;
14407 /* ??? Most of the time we do not take proper care for sign/zero
14408 extending the values properly. Hopefully this won't be a real
14409 problem... */
14411 if (context != NULL
14412 && context->base_decl == loc
14413 && want_address == 0)
14415 if (dwarf_version >= 3 || !dwarf_strict)
14416 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14417 NULL, NULL, NULL);
14418 else
14419 return NULL;
14422 switch (TREE_CODE (loc))
14424 case ERROR_MARK:
14425 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14426 return 0;
14428 case PLACEHOLDER_EXPR:
14429 /* This case involves extracting fields from an object to determine the
14430 position of other fields. It is supposed to appear only as the first
14431 operand of COMPONENT_REF nodes and to reference precisely the type
14432 that the context allows. */
14433 if (context != NULL
14434 && TREE_TYPE (loc) == context->context_type
14435 && want_address >= 1)
14437 if (dwarf_version >= 3 || !dwarf_strict)
14439 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14440 have_address = 1;
14441 break;
14443 else
14444 return NULL;
14446 else
14447 expansion_failed (loc, NULL_RTX,
14448 "PLACEHOLDER_EXPR for an unexpected type");
14449 break;
14451 case CALL_EXPR:
14452 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14453 /* There are no opcodes for these operations. */
14454 return 0;
14456 case PREINCREMENT_EXPR:
14457 case PREDECREMENT_EXPR:
14458 case POSTINCREMENT_EXPR:
14459 case POSTDECREMENT_EXPR:
14460 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14461 /* There are no opcodes for these operations. */
14462 return 0;
14464 case ADDR_EXPR:
14465 /* If we already want an address, see if there is INDIRECT_REF inside
14466 e.g. for &this->field. */
14467 if (want_address)
14469 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14470 (loc, want_address == 2, context);
14471 if (list_ret)
14472 have_address = 1;
14473 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14474 && (ret = cst_pool_loc_descr (loc)))
14475 have_address = 1;
14477 /* Otherwise, process the argument and look for the address. */
14478 if (!list_ret && !ret)
14479 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14480 else
14482 if (want_address)
14483 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14484 return NULL;
14486 break;
14488 case VAR_DECL:
14489 if (DECL_THREAD_LOCAL_P (loc))
14491 rtx rtl;
14492 enum dwarf_location_atom tls_op;
14493 enum dtprel_bool dtprel = dtprel_false;
14495 if (targetm.have_tls)
14497 /* If this is not defined, we have no way to emit the
14498 data. */
14499 if (!targetm.asm_out.output_dwarf_dtprel)
14500 return 0;
14502 /* The way DW_OP_GNU_push_tls_address is specified, we
14503 can only look up addresses of objects in the current
14504 module. We used DW_OP_addr as first op, but that's
14505 wrong, because DW_OP_addr is relocated by the debug
14506 info consumer, while DW_OP_GNU_push_tls_address
14507 operand shouldn't be. */
14508 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14509 return 0;
14510 dtprel = dtprel_true;
14511 tls_op = DW_OP_GNU_push_tls_address;
14513 else
14515 if (!targetm.emutls.debug_form_tls_address
14516 || !(dwarf_version >= 3 || !dwarf_strict))
14517 return 0;
14518 /* We stuffed the control variable into the DECL_VALUE_EXPR
14519 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14520 no longer appear in gimple code. We used the control
14521 variable in specific so that we could pick it up here. */
14522 loc = DECL_VALUE_EXPR (loc);
14523 tls_op = DW_OP_form_tls_address;
14526 rtl = rtl_for_decl_location (loc);
14527 if (rtl == NULL_RTX)
14528 return 0;
14530 if (!MEM_P (rtl))
14531 return 0;
14532 rtl = XEXP (rtl, 0);
14533 if (! CONSTANT_P (rtl))
14534 return 0;
14536 ret = new_addr_loc_descr (rtl, dtprel);
14537 ret1 = new_loc_descr (tls_op, 0, 0);
14538 add_loc_descr (&ret, ret1);
14540 have_address = 1;
14541 break;
14543 /* FALLTHRU */
14545 case PARM_DECL:
14546 case RESULT_DECL:
14547 if (DECL_HAS_VALUE_EXPR_P (loc))
14548 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14549 want_address, context);
14550 /* FALLTHRU */
14552 case FUNCTION_DECL:
14554 rtx rtl;
14555 var_loc_list *loc_list = lookup_decl_loc (loc);
14557 if (loc_list && loc_list->first)
14559 list_ret = dw_loc_list (loc_list, loc, want_address);
14560 have_address = want_address != 0;
14561 break;
14563 rtl = rtl_for_decl_location (loc);
14564 if (rtl == NULL_RTX)
14566 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14567 return 0;
14569 else if (CONST_INT_P (rtl))
14571 HOST_WIDE_INT val = INTVAL (rtl);
14572 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14573 val &= GET_MODE_MASK (DECL_MODE (loc));
14574 ret = int_loc_descriptor (val);
14576 else if (GET_CODE (rtl) == CONST_STRING)
14578 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14579 return 0;
14581 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14582 ret = new_addr_loc_descr (rtl, dtprel_false);
14583 else
14585 machine_mode mode, mem_mode;
14587 /* Certain constructs can only be represented at top-level. */
14588 if (want_address == 2)
14590 ret = loc_descriptor (rtl, VOIDmode,
14591 VAR_INIT_STATUS_INITIALIZED);
14592 have_address = 1;
14594 else
14596 mode = GET_MODE (rtl);
14597 mem_mode = VOIDmode;
14598 if (MEM_P (rtl))
14600 mem_mode = mode;
14601 mode = get_address_mode (rtl);
14602 rtl = XEXP (rtl, 0);
14603 have_address = 1;
14605 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14606 VAR_INIT_STATUS_INITIALIZED);
14608 if (!ret)
14609 expansion_failed (loc, rtl,
14610 "failed to produce loc descriptor for rtl");
14613 break;
14615 case MEM_REF:
14616 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14618 have_address = 1;
14619 goto do_plus;
14621 /* Fallthru. */
14622 case INDIRECT_REF:
14623 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14624 have_address = 1;
14625 break;
14627 case TARGET_MEM_REF:
14628 case SSA_NAME:
14629 return NULL;
14631 case COMPOUND_EXPR:
14632 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14634 CASE_CONVERT:
14635 case VIEW_CONVERT_EXPR:
14636 case SAVE_EXPR:
14637 case MODIFY_EXPR:
14638 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14640 case COMPONENT_REF:
14641 case BIT_FIELD_REF:
14642 case ARRAY_REF:
14643 case ARRAY_RANGE_REF:
14644 case REALPART_EXPR:
14645 case IMAGPART_EXPR:
14647 tree obj, offset;
14648 HOST_WIDE_INT bitsize, bitpos, bytepos;
14649 machine_mode mode;
14650 int unsignedp, volatilep = 0;
14652 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14653 &unsignedp, &volatilep, false);
14655 gcc_assert (obj != loc);
14657 list_ret = loc_list_from_tree (obj,
14658 want_address == 2
14659 && !bitpos && !offset ? 2 : 1,
14660 context);
14661 /* TODO: We can extract value of the small expression via shifting even
14662 for nonzero bitpos. */
14663 if (list_ret == 0)
14664 return 0;
14665 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14667 expansion_failed (loc, NULL_RTX,
14668 "bitfield access");
14669 return 0;
14672 if (offset != NULL_TREE)
14674 /* Variable offset. */
14675 list_ret1 = loc_list_from_tree (offset, 0, context);
14676 if (list_ret1 == 0)
14677 return 0;
14678 add_loc_list (&list_ret, list_ret1);
14679 if (!list_ret)
14680 return 0;
14681 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14684 bytepos = bitpos / BITS_PER_UNIT;
14685 if (bytepos > 0)
14686 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14687 else if (bytepos < 0)
14688 loc_list_plus_const (list_ret, bytepos);
14690 have_address = 1;
14691 break;
14694 case INTEGER_CST:
14695 if ((want_address || !tree_fits_shwi_p (loc))
14696 && (ret = cst_pool_loc_descr (loc)))
14697 have_address = 1;
14698 else if (want_address == 2
14699 && tree_fits_shwi_p (loc)
14700 && (ret = address_of_int_loc_descriptor
14701 (int_size_in_bytes (TREE_TYPE (loc)),
14702 tree_to_shwi (loc))))
14703 have_address = 1;
14704 else if (tree_fits_shwi_p (loc))
14705 ret = int_loc_descriptor (tree_to_shwi (loc));
14706 else
14708 expansion_failed (loc, NULL_RTX,
14709 "Integer operand is not host integer");
14710 return 0;
14712 break;
14714 case CONSTRUCTOR:
14715 case REAL_CST:
14716 case STRING_CST:
14717 case COMPLEX_CST:
14718 if ((ret = cst_pool_loc_descr (loc)))
14719 have_address = 1;
14720 else
14721 /* We can construct small constants here using int_loc_descriptor. */
14722 expansion_failed (loc, NULL_RTX,
14723 "constructor or constant not in constant pool");
14724 break;
14726 case TRUTH_AND_EXPR:
14727 case TRUTH_ANDIF_EXPR:
14728 case BIT_AND_EXPR:
14729 op = DW_OP_and;
14730 goto do_binop;
14732 case TRUTH_XOR_EXPR:
14733 case BIT_XOR_EXPR:
14734 op = DW_OP_xor;
14735 goto do_binop;
14737 case TRUTH_OR_EXPR:
14738 case TRUTH_ORIF_EXPR:
14739 case BIT_IOR_EXPR:
14740 op = DW_OP_or;
14741 goto do_binop;
14743 case FLOOR_DIV_EXPR:
14744 case CEIL_DIV_EXPR:
14745 case ROUND_DIV_EXPR:
14746 case TRUNC_DIV_EXPR:
14747 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14748 return 0;
14749 op = DW_OP_div;
14750 goto do_binop;
14752 case MINUS_EXPR:
14753 op = DW_OP_minus;
14754 goto do_binop;
14756 case FLOOR_MOD_EXPR:
14757 case CEIL_MOD_EXPR:
14758 case ROUND_MOD_EXPR:
14759 case TRUNC_MOD_EXPR:
14760 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14762 op = DW_OP_mod;
14763 goto do_binop;
14765 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14766 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14767 if (list_ret == 0 || list_ret1 == 0)
14768 return 0;
14770 add_loc_list (&list_ret, list_ret1);
14771 if (list_ret == 0)
14772 return 0;
14773 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14774 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14775 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14776 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14777 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14778 break;
14780 case MULT_EXPR:
14781 op = DW_OP_mul;
14782 goto do_binop;
14784 case LSHIFT_EXPR:
14785 op = DW_OP_shl;
14786 goto do_binop;
14788 case RSHIFT_EXPR:
14789 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14790 goto do_binop;
14792 case POINTER_PLUS_EXPR:
14793 case PLUS_EXPR:
14794 do_plus:
14795 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14797 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14798 if (list_ret == 0)
14799 return 0;
14801 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14802 break;
14805 op = DW_OP_plus;
14806 goto do_binop;
14808 case LE_EXPR:
14809 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14810 return 0;
14812 op = DW_OP_le;
14813 goto do_binop;
14815 case GE_EXPR:
14816 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14817 return 0;
14819 op = DW_OP_ge;
14820 goto do_binop;
14822 case LT_EXPR:
14823 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14824 return 0;
14826 op = DW_OP_lt;
14827 goto do_binop;
14829 case GT_EXPR:
14830 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14831 return 0;
14833 op = DW_OP_gt;
14834 goto do_binop;
14836 case EQ_EXPR:
14837 op = DW_OP_eq;
14838 goto do_binop;
14840 case NE_EXPR:
14841 op = DW_OP_ne;
14842 goto do_binop;
14844 do_binop:
14845 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14846 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14847 if (list_ret == 0 || list_ret1 == 0)
14848 return 0;
14850 add_loc_list (&list_ret, list_ret1);
14851 if (list_ret == 0)
14852 return 0;
14853 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14854 break;
14856 case TRUTH_NOT_EXPR:
14857 case BIT_NOT_EXPR:
14858 op = DW_OP_not;
14859 goto do_unop;
14861 case ABS_EXPR:
14862 op = DW_OP_abs;
14863 goto do_unop;
14865 case NEGATE_EXPR:
14866 op = DW_OP_neg;
14867 goto do_unop;
14869 do_unop:
14870 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14871 if (list_ret == 0)
14872 return 0;
14874 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14875 break;
14877 case MIN_EXPR:
14878 case MAX_EXPR:
14880 const enum tree_code code =
14881 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14883 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14884 build2 (code, integer_type_node,
14885 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14886 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14889 /* ... fall through ... */
14891 case COND_EXPR:
14893 dw_loc_descr_ref lhs
14894 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
14895 dw_loc_list_ref rhs
14896 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
14897 dw_loc_descr_ref bra_node, jump_node, tmp;
14899 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14900 if (list_ret == 0 || lhs == 0 || rhs == 0)
14901 return 0;
14903 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14904 add_loc_descr_to_each (list_ret, bra_node);
14906 add_loc_list (&list_ret, rhs);
14907 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14908 add_loc_descr_to_each (list_ret, jump_node);
14910 add_loc_descr_to_each (list_ret, lhs);
14911 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14912 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14914 /* ??? Need a node to point the skip at. Use a nop. */
14915 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14916 add_loc_descr_to_each (list_ret, tmp);
14917 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14918 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14920 break;
14922 case FIX_TRUNC_EXPR:
14923 return 0;
14925 default:
14926 /* Leave front-end specific codes as simply unknown. This comes
14927 up, for instance, with the C STMT_EXPR. */
14928 if ((unsigned int) TREE_CODE (loc)
14929 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14931 expansion_failed (loc, NULL_RTX,
14932 "language specific tree node");
14933 return 0;
14936 #ifdef ENABLE_CHECKING
14937 /* Otherwise this is a generic code; we should just lists all of
14938 these explicitly. We forgot one. */
14939 gcc_unreachable ();
14940 #else
14941 /* In a release build, we want to degrade gracefully: better to
14942 generate incomplete debugging information than to crash. */
14943 return NULL;
14944 #endif
14947 if (!ret && !list_ret)
14948 return 0;
14950 if (want_address == 2 && !have_address
14951 && (dwarf_version >= 4 || !dwarf_strict))
14953 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14955 expansion_failed (loc, NULL_RTX,
14956 "DWARF address size mismatch");
14957 return 0;
14959 if (ret)
14960 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14961 else
14962 add_loc_descr_to_each (list_ret,
14963 new_loc_descr (DW_OP_stack_value, 0, 0));
14964 have_address = 1;
14966 /* Show if we can't fill the request for an address. */
14967 if (want_address && !have_address)
14969 expansion_failed (loc, NULL_RTX,
14970 "Want address and only have value");
14971 return 0;
14974 gcc_assert (!ret || !list_ret);
14976 /* If we've got an address and don't want one, dereference. */
14977 if (!want_address && have_address)
14979 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14981 if (size > DWARF2_ADDR_SIZE || size == -1)
14983 expansion_failed (loc, NULL_RTX,
14984 "DWARF address size mismatch");
14985 return 0;
14987 else if (size == DWARF2_ADDR_SIZE)
14988 op = DW_OP_deref;
14989 else
14990 op = DW_OP_deref_size;
14992 if (ret)
14993 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14994 else
14995 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14997 if (ret)
14998 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15000 return list_ret;
15003 /* Same as above but return only single location expression. */
15004 static dw_loc_descr_ref
15005 loc_descriptor_from_tree (tree loc, int want_address,
15006 const struct loc_descr_context *context)
15008 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15009 if (!ret)
15010 return NULL;
15011 if (ret->dw_loc_next)
15013 expansion_failed (loc, NULL_RTX,
15014 "Location list where only loc descriptor needed");
15015 return NULL;
15017 return ret->expr;
15020 /* Given a value, round it up to the lowest multiple of `boundary'
15021 which is not less than the value itself. */
15023 static inline HOST_WIDE_INT
15024 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15026 return (((value + boundary - 1) / boundary) * boundary);
15029 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15030 pointer to the declared type for the relevant field variable, or return
15031 `integer_type_node' if the given node turns out to be an
15032 ERROR_MARK node. */
15034 static inline tree
15035 field_type (const_tree decl)
15037 tree type;
15039 if (TREE_CODE (decl) == ERROR_MARK)
15040 return integer_type_node;
15042 type = DECL_BIT_FIELD_TYPE (decl);
15043 if (type == NULL_TREE)
15044 type = TREE_TYPE (decl);
15046 return type;
15049 /* Given a pointer to a tree node, return the alignment in bits for
15050 it, or else return BITS_PER_WORD if the node actually turns out to
15051 be an ERROR_MARK node. */
15053 static inline unsigned
15054 simple_type_align_in_bits (const_tree type)
15056 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15059 static inline unsigned
15060 simple_decl_align_in_bits (const_tree decl)
15062 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15065 /* Return the result of rounding T up to ALIGN. */
15067 static inline offset_int
15068 round_up_to_align (const offset_int &t, unsigned int align)
15070 return wi::udiv_trunc (t + align - 1, align) * align;
15073 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15074 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15075 or return 0 if we are unable to determine what that offset is, either
15076 because the argument turns out to be a pointer to an ERROR_MARK node, or
15077 because the offset is actually variable. (We can't handle the latter case
15078 just yet). */
15080 static HOST_WIDE_INT
15081 field_byte_offset (const_tree decl)
15083 offset_int object_offset_in_bits;
15084 offset_int object_offset_in_bytes;
15085 offset_int bitpos_int;
15087 if (TREE_CODE (decl) == ERROR_MARK)
15088 return 0;
15090 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15092 /* We cannot yet cope with fields whose positions are variable, so
15093 for now, when we see such things, we simply return 0. Someday, we may
15094 be able to handle such cases, but it will be damn difficult. */
15095 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15096 return 0;
15098 bitpos_int = wi::to_offset (bit_position (decl));
15100 #ifdef PCC_BITFIELD_TYPE_MATTERS
15101 if (PCC_BITFIELD_TYPE_MATTERS)
15103 tree type;
15104 tree field_size_tree;
15105 offset_int deepest_bitpos;
15106 offset_int field_size_in_bits;
15107 unsigned int type_align_in_bits;
15108 unsigned int decl_align_in_bits;
15109 offset_int type_size_in_bits;
15111 type = field_type (decl);
15112 type_size_in_bits = offset_int_type_size_in_bits (type);
15113 type_align_in_bits = simple_type_align_in_bits (type);
15115 field_size_tree = DECL_SIZE (decl);
15117 /* The size could be unspecified if there was an error, or for
15118 a flexible array member. */
15119 if (!field_size_tree)
15120 field_size_tree = bitsize_zero_node;
15122 /* If the size of the field is not constant, use the type size. */
15123 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15124 field_size_in_bits = wi::to_offset (field_size_tree);
15125 else
15126 field_size_in_bits = type_size_in_bits;
15128 decl_align_in_bits = simple_decl_align_in_bits (decl);
15130 /* The GCC front-end doesn't make any attempt to keep track of the
15131 starting bit offset (relative to the start of the containing
15132 structure type) of the hypothetical "containing object" for a
15133 bit-field. Thus, when computing the byte offset value for the
15134 start of the "containing object" of a bit-field, we must deduce
15135 this information on our own. This can be rather tricky to do in
15136 some cases. For example, handling the following structure type
15137 definition when compiling for an i386/i486 target (which only
15138 aligns long long's to 32-bit boundaries) can be very tricky:
15140 struct S { int field1; long long field2:31; };
15142 Fortunately, there is a simple rule-of-thumb which can be used
15143 in such cases. When compiling for an i386/i486, GCC will
15144 allocate 8 bytes for the structure shown above. It decides to
15145 do this based upon one simple rule for bit-field allocation.
15146 GCC allocates each "containing object" for each bit-field at
15147 the first (i.e. lowest addressed) legitimate alignment boundary
15148 (based upon the required minimum alignment for the declared
15149 type of the field) which it can possibly use, subject to the
15150 condition that there is still enough available space remaining
15151 in the containing object (when allocated at the selected point)
15152 to fully accommodate all of the bits of the bit-field itself.
15154 This simple rule makes it obvious why GCC allocates 8 bytes for
15155 each object of the structure type shown above. When looking
15156 for a place to allocate the "containing object" for `field2',
15157 the compiler simply tries to allocate a 64-bit "containing
15158 object" at each successive 32-bit boundary (starting at zero)
15159 until it finds a place to allocate that 64- bit field such that
15160 at least 31 contiguous (and previously unallocated) bits remain
15161 within that selected 64 bit field. (As it turns out, for the
15162 example above, the compiler finds it is OK to allocate the
15163 "containing object" 64-bit field at bit-offset zero within the
15164 structure type.)
15166 Here we attempt to work backwards from the limited set of facts
15167 we're given, and we try to deduce from those facts, where GCC
15168 must have believed that the containing object started (within
15169 the structure type). The value we deduce is then used (by the
15170 callers of this routine) to generate DW_AT_location and
15171 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15172 the case of DW_AT_location, regular fields as well). */
15174 /* Figure out the bit-distance from the start of the structure to
15175 the "deepest" bit of the bit-field. */
15176 deepest_bitpos = bitpos_int + field_size_in_bits;
15178 /* This is the tricky part. Use some fancy footwork to deduce
15179 where the lowest addressed bit of the containing object must
15180 be. */
15181 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15183 /* Round up to type_align by default. This works best for
15184 bitfields. */
15185 object_offset_in_bits
15186 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15188 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15190 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15192 /* Round up to decl_align instead. */
15193 object_offset_in_bits
15194 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15197 else
15198 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15199 object_offset_in_bits = bitpos_int;
15201 object_offset_in_bytes
15202 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15203 return object_offset_in_bytes.to_shwi ();
15206 /* The following routines define various Dwarf attributes and any data
15207 associated with them. */
15209 /* Add a location description attribute value to a DIE.
15211 This emits location attributes suitable for whole variables and
15212 whole parameters. Note that the location attributes for struct fields are
15213 generated by the routine `data_member_location_attribute' below. */
15215 static inline void
15216 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15217 dw_loc_list_ref descr)
15219 if (descr == 0)
15220 return;
15221 if (single_element_loc_list_p (descr))
15222 add_AT_loc (die, attr_kind, descr->expr);
15223 else
15224 add_AT_loc_list (die, attr_kind, descr);
15227 /* Add DW_AT_accessibility attribute to DIE if needed. */
15229 static void
15230 add_accessibility_attribute (dw_die_ref die, tree decl)
15232 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15233 children, otherwise the default is DW_ACCESS_public. In DWARF2
15234 the default has always been DW_ACCESS_public. */
15235 if (TREE_PROTECTED (decl))
15236 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15237 else if (TREE_PRIVATE (decl))
15239 if (dwarf_version == 2
15240 || die->die_parent == NULL
15241 || die->die_parent->die_tag != DW_TAG_class_type)
15242 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15244 else if (dwarf_version > 2
15245 && die->die_parent
15246 && die->die_parent->die_tag == DW_TAG_class_type)
15247 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15250 /* Attach the specialized form of location attribute used for data members of
15251 struct and union types. In the special case of a FIELD_DECL node which
15252 represents a bit-field, the "offset" part of this special location
15253 descriptor must indicate the distance in bytes from the lowest-addressed
15254 byte of the containing struct or union type to the lowest-addressed byte of
15255 the "containing object" for the bit-field. (See the `field_byte_offset'
15256 function above).
15258 For any given bit-field, the "containing object" is a hypothetical object
15259 (of some integral or enum type) within which the given bit-field lives. The
15260 type of this hypothetical "containing object" is always the same as the
15261 declared type of the individual bit-field itself (for GCC anyway... the
15262 DWARF spec doesn't actually mandate this). Note that it is the size (in
15263 bytes) of the hypothetical "containing object" which will be given in the
15264 DW_AT_byte_size attribute for this bit-field. (See the
15265 `byte_size_attribute' function below.) It is also used when calculating the
15266 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15267 function below.) */
15269 static void
15270 add_data_member_location_attribute (dw_die_ref die, tree decl)
15272 HOST_WIDE_INT offset;
15273 dw_loc_descr_ref loc_descr = 0;
15275 if (TREE_CODE (decl) == TREE_BINFO)
15277 /* We're working on the TAG_inheritance for a base class. */
15278 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15280 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15281 aren't at a fixed offset from all (sub)objects of the same
15282 type. We need to extract the appropriate offset from our
15283 vtable. The following dwarf expression means
15285 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15287 This is specific to the V3 ABI, of course. */
15289 dw_loc_descr_ref tmp;
15291 /* Make a copy of the object address. */
15292 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15293 add_loc_descr (&loc_descr, tmp);
15295 /* Extract the vtable address. */
15296 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15297 add_loc_descr (&loc_descr, tmp);
15299 /* Calculate the address of the offset. */
15300 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15301 gcc_assert (offset < 0);
15303 tmp = int_loc_descriptor (-offset);
15304 add_loc_descr (&loc_descr, tmp);
15305 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15306 add_loc_descr (&loc_descr, tmp);
15308 /* Extract the offset. */
15309 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15310 add_loc_descr (&loc_descr, tmp);
15312 /* Add it to the object address. */
15313 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15314 add_loc_descr (&loc_descr, tmp);
15316 else
15317 offset = tree_to_shwi (BINFO_OFFSET (decl));
15319 else
15320 offset = field_byte_offset (decl);
15322 if (! loc_descr)
15324 if (dwarf_version > 2)
15326 /* Don't need to output a location expression, just the constant. */
15327 if (offset < 0)
15328 add_AT_int (die, DW_AT_data_member_location, offset);
15329 else
15330 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15331 return;
15333 else
15335 enum dwarf_location_atom op;
15337 /* The DWARF2 standard says that we should assume that the structure
15338 address is already on the stack, so we can specify a structure
15339 field address by using DW_OP_plus_uconst. */
15340 op = DW_OP_plus_uconst;
15341 loc_descr = new_loc_descr (op, offset, 0);
15345 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15348 /* Writes integer values to dw_vec_const array. */
15350 static void
15351 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15353 while (size != 0)
15355 *dest++ = val & 0xff;
15356 val >>= 8;
15357 --size;
15361 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15363 static HOST_WIDE_INT
15364 extract_int (const unsigned char *src, unsigned int size)
15366 HOST_WIDE_INT val = 0;
15368 src += size;
15369 while (size != 0)
15371 val <<= 8;
15372 val |= *--src & 0xff;
15373 --size;
15375 return val;
15378 /* Writes wide_int values to dw_vec_const array. */
15380 static void
15381 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15383 int i;
15385 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15387 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15388 return;
15391 /* We'd have to extend this code to support odd sizes. */
15392 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15394 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15396 if (WORDS_BIG_ENDIAN)
15397 for (i = n - 1; i >= 0; i--)
15399 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15400 dest += sizeof (HOST_WIDE_INT);
15402 else
15403 for (i = 0; i < n; i++)
15405 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15406 dest += sizeof (HOST_WIDE_INT);
15410 /* Writes floating point values to dw_vec_const array. */
15412 static void
15413 insert_float (const_rtx rtl, unsigned char *array)
15415 REAL_VALUE_TYPE rv;
15416 long val[4];
15417 int i;
15419 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15420 real_to_target (val, &rv, GET_MODE (rtl));
15422 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15423 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15425 insert_int (val[i], 4, array);
15426 array += 4;
15430 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15431 does not have a "location" either in memory or in a register. These
15432 things can arise in GNU C when a constant is passed as an actual parameter
15433 to an inlined function. They can also arise in C++ where declared
15434 constants do not necessarily get memory "homes". */
15436 static bool
15437 add_const_value_attribute (dw_die_ref die, rtx rtl)
15439 switch (GET_CODE (rtl))
15441 case CONST_INT:
15443 HOST_WIDE_INT val = INTVAL (rtl);
15445 if (val < 0)
15446 add_AT_int (die, DW_AT_const_value, val);
15447 else
15448 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15450 return true;
15452 case CONST_WIDE_INT:
15453 add_AT_wide (die, DW_AT_const_value,
15454 std::make_pair (rtl, GET_MODE (rtl)));
15455 return true;
15457 case CONST_DOUBLE:
15458 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15459 floating-point constant. A CONST_DOUBLE is used whenever the
15460 constant requires more than one word in order to be adequately
15461 represented. */
15463 machine_mode mode = GET_MODE (rtl);
15465 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15466 add_AT_double (die, DW_AT_const_value,
15467 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15468 else
15470 unsigned int length = GET_MODE_SIZE (mode);
15471 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15473 insert_float (rtl, array);
15474 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15477 return true;
15479 case CONST_VECTOR:
15481 machine_mode mode = GET_MODE (rtl);
15482 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15483 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15484 unsigned char *array
15485 = ggc_vec_alloc<unsigned char> (length * elt_size);
15486 unsigned int i;
15487 unsigned char *p;
15488 machine_mode imode = GET_MODE_INNER (mode);
15490 switch (GET_MODE_CLASS (mode))
15492 case MODE_VECTOR_INT:
15493 for (i = 0, p = array; i < length; i++, p += elt_size)
15495 rtx elt = CONST_VECTOR_ELT (rtl, i);
15496 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15498 break;
15500 case MODE_VECTOR_FLOAT:
15501 for (i = 0, p = array; i < length; i++, p += elt_size)
15503 rtx elt = CONST_VECTOR_ELT (rtl, i);
15504 insert_float (elt, p);
15506 break;
15508 default:
15509 gcc_unreachable ();
15512 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15514 return true;
15516 case CONST_STRING:
15517 if (dwarf_version >= 4 || !dwarf_strict)
15519 dw_loc_descr_ref loc_result;
15520 resolve_one_addr (&rtl);
15521 rtl_addr:
15522 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15523 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15524 add_AT_loc (die, DW_AT_location, loc_result);
15525 vec_safe_push (used_rtx_array, rtl);
15526 return true;
15528 return false;
15530 case CONST:
15531 if (CONSTANT_P (XEXP (rtl, 0)))
15532 return add_const_value_attribute (die, XEXP (rtl, 0));
15533 /* FALLTHROUGH */
15534 case SYMBOL_REF:
15535 if (!const_ok_for_output (rtl))
15536 return false;
15537 case LABEL_REF:
15538 if (dwarf_version >= 4 || !dwarf_strict)
15539 goto rtl_addr;
15540 return false;
15542 case PLUS:
15543 /* In cases where an inlined instance of an inline function is passed
15544 the address of an `auto' variable (which is local to the caller) we
15545 can get a situation where the DECL_RTL of the artificial local
15546 variable (for the inlining) which acts as a stand-in for the
15547 corresponding formal parameter (of the inline function) will look
15548 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15549 exactly a compile-time constant expression, but it isn't the address
15550 of the (artificial) local variable either. Rather, it represents the
15551 *value* which the artificial local variable always has during its
15552 lifetime. We currently have no way to represent such quasi-constant
15553 values in Dwarf, so for now we just punt and generate nothing. */
15554 return false;
15556 case HIGH:
15557 case CONST_FIXED:
15558 return false;
15560 case MEM:
15561 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15562 && MEM_READONLY_P (rtl)
15563 && GET_MODE (rtl) == BLKmode)
15565 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15566 return true;
15568 return false;
15570 default:
15571 /* No other kinds of rtx should be possible here. */
15572 gcc_unreachable ();
15574 return false;
15577 /* Determine whether the evaluation of EXPR references any variables
15578 or functions which aren't otherwise used (and therefore may not be
15579 output). */
15580 static tree
15581 reference_to_unused (tree * tp, int * walk_subtrees,
15582 void * data ATTRIBUTE_UNUSED)
15584 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15585 *walk_subtrees = 0;
15587 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15588 && ! TREE_ASM_WRITTEN (*tp))
15589 return *tp;
15590 /* ??? The C++ FE emits debug information for using decls, so
15591 putting gcc_unreachable here falls over. See PR31899. For now
15592 be conservative. */
15593 else if (!symtab->global_info_ready
15594 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15595 return *tp;
15596 else if (TREE_CODE (*tp) == VAR_DECL)
15598 varpool_node *node = varpool_node::get (*tp);
15599 if (!node || !node->definition)
15600 return *tp;
15602 else if (TREE_CODE (*tp) == FUNCTION_DECL
15603 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15605 /* The call graph machinery must have finished analyzing,
15606 optimizing and gimplifying the CU by now.
15607 So if *TP has no call graph node associated
15608 to it, it means *TP will not be emitted. */
15609 if (!cgraph_node::get (*tp))
15610 return *tp;
15612 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15613 return *tp;
15615 return NULL_TREE;
15618 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15619 for use in a later add_const_value_attribute call. */
15621 static rtx
15622 rtl_for_decl_init (tree init, tree type)
15624 rtx rtl = NULL_RTX;
15626 STRIP_NOPS (init);
15628 /* If a variable is initialized with a string constant without embedded
15629 zeros, build CONST_STRING. */
15630 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15632 tree enttype = TREE_TYPE (type);
15633 tree domain = TYPE_DOMAIN (type);
15634 machine_mode mode = TYPE_MODE (enttype);
15636 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15637 && domain
15638 && integer_zerop (TYPE_MIN_VALUE (domain))
15639 && compare_tree_int (TYPE_MAX_VALUE (domain),
15640 TREE_STRING_LENGTH (init) - 1) == 0
15641 && ((size_t) TREE_STRING_LENGTH (init)
15642 == strlen (TREE_STRING_POINTER (init)) + 1))
15644 rtl = gen_rtx_CONST_STRING (VOIDmode,
15645 ggc_strdup (TREE_STRING_POINTER (init)));
15646 rtl = gen_rtx_MEM (BLKmode, rtl);
15647 MEM_READONLY_P (rtl) = 1;
15650 /* Other aggregates, and complex values, could be represented using
15651 CONCAT: FIXME! */
15652 else if (AGGREGATE_TYPE_P (type)
15653 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15654 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15655 || TREE_CODE (type) == COMPLEX_TYPE)
15657 /* Vectors only work if their mode is supported by the target.
15658 FIXME: generic vectors ought to work too. */
15659 else if (TREE_CODE (type) == VECTOR_TYPE
15660 && !VECTOR_MODE_P (TYPE_MODE (type)))
15662 /* If the initializer is something that we know will expand into an
15663 immediate RTL constant, expand it now. We must be careful not to
15664 reference variables which won't be output. */
15665 else if (initializer_constant_valid_p (init, type)
15666 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15668 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15669 possible. */
15670 if (TREE_CODE (type) == VECTOR_TYPE)
15671 switch (TREE_CODE (init))
15673 case VECTOR_CST:
15674 break;
15675 case CONSTRUCTOR:
15676 if (TREE_CONSTANT (init))
15678 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15679 bool constant_p = true;
15680 tree value;
15681 unsigned HOST_WIDE_INT ix;
15683 /* Even when ctor is constant, it might contain non-*_CST
15684 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15685 belong into VECTOR_CST nodes. */
15686 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15687 if (!CONSTANT_CLASS_P (value))
15689 constant_p = false;
15690 break;
15693 if (constant_p)
15695 init = build_vector_from_ctor (type, elts);
15696 break;
15699 /* FALLTHRU */
15701 default:
15702 return NULL;
15705 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15707 /* If expand_expr returns a MEM, it wasn't immediate. */
15708 gcc_assert (!rtl || !MEM_P (rtl));
15711 return rtl;
15714 /* Generate RTL for the variable DECL to represent its location. */
15716 static rtx
15717 rtl_for_decl_location (tree decl)
15719 rtx rtl;
15721 /* Here we have to decide where we are going to say the parameter "lives"
15722 (as far as the debugger is concerned). We only have a couple of
15723 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15725 DECL_RTL normally indicates where the parameter lives during most of the
15726 activation of the function. If optimization is enabled however, this
15727 could be either NULL or else a pseudo-reg. Both of those cases indicate
15728 that the parameter doesn't really live anywhere (as far as the code
15729 generation parts of GCC are concerned) during most of the function's
15730 activation. That will happen (for example) if the parameter is never
15731 referenced within the function.
15733 We could just generate a location descriptor here for all non-NULL
15734 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15735 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15736 where DECL_RTL is NULL or is a pseudo-reg.
15738 Note however that we can only get away with using DECL_INCOMING_RTL as
15739 a backup substitute for DECL_RTL in certain limited cases. In cases
15740 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15741 we can be sure that the parameter was passed using the same type as it is
15742 declared to have within the function, and that its DECL_INCOMING_RTL
15743 points us to a place where a value of that type is passed.
15745 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15746 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15747 because in these cases DECL_INCOMING_RTL points us to a value of some
15748 type which is *different* from the type of the parameter itself. Thus,
15749 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15750 such cases, the debugger would end up (for example) trying to fetch a
15751 `float' from a place which actually contains the first part of a
15752 `double'. That would lead to really incorrect and confusing
15753 output at debug-time.
15755 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15756 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15757 are a couple of exceptions however. On little-endian machines we can
15758 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15759 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15760 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15761 when (on a little-endian machine) a non-prototyped function has a
15762 parameter declared to be of type `short' or `char'. In such cases,
15763 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15764 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15765 passed `int' value. If the debugger then uses that address to fetch
15766 a `short' or a `char' (on a little-endian machine) the result will be
15767 the correct data, so we allow for such exceptional cases below.
15769 Note that our goal here is to describe the place where the given formal
15770 parameter lives during most of the function's activation (i.e. between the
15771 end of the prologue and the start of the epilogue). We'll do that as best
15772 as we can. Note however that if the given formal parameter is modified
15773 sometime during the execution of the function, then a stack backtrace (at
15774 debug-time) will show the function as having been called with the *new*
15775 value rather than the value which was originally passed in. This happens
15776 rarely enough that it is not a major problem, but it *is* a problem, and
15777 I'd like to fix it.
15779 A future version of dwarf2out.c may generate two additional attributes for
15780 any given DW_TAG_formal_parameter DIE which will describe the "passed
15781 type" and the "passed location" for the given formal parameter in addition
15782 to the attributes we now generate to indicate the "declared type" and the
15783 "active location" for each parameter. This additional set of attributes
15784 could be used by debuggers for stack backtraces. Separately, note that
15785 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15786 This happens (for example) for inlined-instances of inline function formal
15787 parameters which are never referenced. This really shouldn't be
15788 happening. All PARM_DECL nodes should get valid non-NULL
15789 DECL_INCOMING_RTL values. FIXME. */
15791 /* Use DECL_RTL as the "location" unless we find something better. */
15792 rtl = DECL_RTL_IF_SET (decl);
15794 /* When generating abstract instances, ignore everything except
15795 constants, symbols living in memory, and symbols living in
15796 fixed registers. */
15797 if (! reload_completed)
15799 if (rtl
15800 && (CONSTANT_P (rtl)
15801 || (MEM_P (rtl)
15802 && CONSTANT_P (XEXP (rtl, 0)))
15803 || (REG_P (rtl)
15804 && TREE_CODE (decl) == VAR_DECL
15805 && TREE_STATIC (decl))))
15807 rtl = targetm.delegitimize_address (rtl);
15808 return rtl;
15810 rtl = NULL_RTX;
15812 else if (TREE_CODE (decl) == PARM_DECL)
15814 if (rtl == NULL_RTX
15815 || is_pseudo_reg (rtl)
15816 || (MEM_P (rtl)
15817 && is_pseudo_reg (XEXP (rtl, 0))
15818 && DECL_INCOMING_RTL (decl)
15819 && MEM_P (DECL_INCOMING_RTL (decl))
15820 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15822 tree declared_type = TREE_TYPE (decl);
15823 tree passed_type = DECL_ARG_TYPE (decl);
15824 machine_mode dmode = TYPE_MODE (declared_type);
15825 machine_mode pmode = TYPE_MODE (passed_type);
15827 /* This decl represents a formal parameter which was optimized out.
15828 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15829 all cases where (rtl == NULL_RTX) just below. */
15830 if (dmode == pmode)
15831 rtl = DECL_INCOMING_RTL (decl);
15832 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15833 && SCALAR_INT_MODE_P (dmode)
15834 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15835 && DECL_INCOMING_RTL (decl))
15837 rtx inc = DECL_INCOMING_RTL (decl);
15838 if (REG_P (inc))
15839 rtl = inc;
15840 else if (MEM_P (inc))
15842 if (BYTES_BIG_ENDIAN)
15843 rtl = adjust_address_nv (inc, dmode,
15844 GET_MODE_SIZE (pmode)
15845 - GET_MODE_SIZE (dmode));
15846 else
15847 rtl = inc;
15852 /* If the parm was passed in registers, but lives on the stack, then
15853 make a big endian correction if the mode of the type of the
15854 parameter is not the same as the mode of the rtl. */
15855 /* ??? This is the same series of checks that are made in dbxout.c before
15856 we reach the big endian correction code there. It isn't clear if all
15857 of these checks are necessary here, but keeping them all is the safe
15858 thing to do. */
15859 else if (MEM_P (rtl)
15860 && XEXP (rtl, 0) != const0_rtx
15861 && ! CONSTANT_P (XEXP (rtl, 0))
15862 /* Not passed in memory. */
15863 && !MEM_P (DECL_INCOMING_RTL (decl))
15864 /* Not passed by invisible reference. */
15865 && (!REG_P (XEXP (rtl, 0))
15866 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15867 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15868 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15869 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15870 #endif
15872 /* Big endian correction check. */
15873 && BYTES_BIG_ENDIAN
15874 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15875 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15876 < UNITS_PER_WORD))
15878 machine_mode addr_mode = get_address_mode (rtl);
15879 int offset = (UNITS_PER_WORD
15880 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15882 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15883 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15886 else if (TREE_CODE (decl) == VAR_DECL
15887 && rtl
15888 && MEM_P (rtl)
15889 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15890 && BYTES_BIG_ENDIAN)
15892 machine_mode addr_mode = get_address_mode (rtl);
15893 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15894 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15896 /* If a variable is declared "register" yet is smaller than
15897 a register, then if we store the variable to memory, it
15898 looks like we're storing a register-sized value, when in
15899 fact we are not. We need to adjust the offset of the
15900 storage location to reflect the actual value's bytes,
15901 else gdb will not be able to display it. */
15902 if (rsize > dsize)
15903 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15904 plus_constant (addr_mode, XEXP (rtl, 0),
15905 rsize - dsize));
15908 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15909 and will have been substituted directly into all expressions that use it.
15910 C does not have such a concept, but C++ and other languages do. */
15911 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15912 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15914 if (rtl)
15915 rtl = targetm.delegitimize_address (rtl);
15917 /* If we don't look past the constant pool, we risk emitting a
15918 reference to a constant pool entry that isn't referenced from
15919 code, and thus is not emitted. */
15920 if (rtl)
15921 rtl = avoid_constant_pool_reference (rtl);
15923 /* Try harder to get a rtl. If this symbol ends up not being emitted
15924 in the current CU, resolve_addr will remove the expression referencing
15925 it. */
15926 if (rtl == NULL_RTX
15927 && TREE_CODE (decl) == VAR_DECL
15928 && !DECL_EXTERNAL (decl)
15929 && TREE_STATIC (decl)
15930 && DECL_NAME (decl)
15931 && !DECL_HARD_REGISTER (decl)
15932 && DECL_MODE (decl) != VOIDmode)
15934 rtl = make_decl_rtl_for_debug (decl);
15935 if (!MEM_P (rtl)
15936 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15937 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15938 rtl = NULL_RTX;
15941 return rtl;
15944 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15945 returned. If so, the decl for the COMMON block is returned, and the
15946 value is the offset into the common block for the symbol. */
15948 static tree
15949 fortran_common (tree decl, HOST_WIDE_INT *value)
15951 tree val_expr, cvar;
15952 machine_mode mode;
15953 HOST_WIDE_INT bitsize, bitpos;
15954 tree offset;
15955 int unsignedp, volatilep = 0;
15957 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15958 it does not have a value (the offset into the common area), or if it
15959 is thread local (as opposed to global) then it isn't common, and shouldn't
15960 be handled as such. */
15961 if (TREE_CODE (decl) != VAR_DECL
15962 || !TREE_STATIC (decl)
15963 || !DECL_HAS_VALUE_EXPR_P (decl)
15964 || !is_fortran ())
15965 return NULL_TREE;
15967 val_expr = DECL_VALUE_EXPR (decl);
15968 if (TREE_CODE (val_expr) != COMPONENT_REF)
15969 return NULL_TREE;
15971 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15972 &mode, &unsignedp, &volatilep, true);
15974 if (cvar == NULL_TREE
15975 || TREE_CODE (cvar) != VAR_DECL
15976 || DECL_ARTIFICIAL (cvar)
15977 || !TREE_PUBLIC (cvar))
15978 return NULL_TREE;
15980 *value = 0;
15981 if (offset != NULL)
15983 if (!tree_fits_shwi_p (offset))
15984 return NULL_TREE;
15985 *value = tree_to_shwi (offset);
15987 if (bitpos != 0)
15988 *value += bitpos / BITS_PER_UNIT;
15990 return cvar;
15993 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15994 data attribute for a variable or a parameter. We generate the
15995 DW_AT_const_value attribute only in those cases where the given variable
15996 or parameter does not have a true "location" either in memory or in a
15997 register. This can happen (for example) when a constant is passed as an
15998 actual argument in a call to an inline function. (It's possible that
15999 these things can crop up in other ways also.) Note that one type of
16000 constant value which can be passed into an inlined function is a constant
16001 pointer. This can happen for example if an actual argument in an inlined
16002 function call evaluates to a compile-time constant address.
16004 CACHE_P is true if it is worth caching the location list for DECL,
16005 so that future calls can reuse it rather than regenerate it from scratch.
16006 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16007 since we will need to refer to them each time the function is inlined. */
16009 static bool
16010 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16011 enum dwarf_attribute attr)
16013 rtx rtl;
16014 dw_loc_list_ref list;
16015 var_loc_list *loc_list;
16016 cached_dw_loc_list *cache;
16018 if (TREE_CODE (decl) == ERROR_MARK)
16019 return false;
16021 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16022 || TREE_CODE (decl) == RESULT_DECL);
16024 /* Try to get some constant RTL for this decl, and use that as the value of
16025 the location. */
16027 rtl = rtl_for_decl_location (decl);
16028 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16029 && add_const_value_attribute (die, rtl))
16030 return true;
16032 /* See if we have single element location list that is equivalent to
16033 a constant value. That way we are better to use add_const_value_attribute
16034 rather than expanding constant value equivalent. */
16035 loc_list = lookup_decl_loc (decl);
16036 if (loc_list
16037 && loc_list->first
16038 && loc_list->first->next == NULL
16039 && NOTE_P (loc_list->first->loc)
16040 && NOTE_VAR_LOCATION (loc_list->first->loc)
16041 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16043 struct var_loc_node *node;
16045 node = loc_list->first;
16046 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16047 if (GET_CODE (rtl) == EXPR_LIST)
16048 rtl = XEXP (rtl, 0);
16049 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16050 && add_const_value_attribute (die, rtl))
16051 return true;
16053 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16054 list several times. See if we've already cached the contents. */
16055 list = NULL;
16056 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16057 cache_p = false;
16058 if (cache_p)
16060 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16061 if (cache)
16062 list = cache->loc_list;
16064 if (list == NULL)
16066 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16067 NULL);
16068 /* It is usually worth caching this result if the decl is from
16069 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16070 if (cache_p && list && list->dw_loc_next)
16072 cached_dw_loc_list **slot
16073 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16074 DECL_UID (decl),
16075 INSERT);
16076 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16077 cache->decl_id = DECL_UID (decl);
16078 cache->loc_list = list;
16079 *slot = cache;
16082 if (list)
16084 add_AT_location_description (die, attr, list);
16085 return true;
16087 /* None of that worked, so it must not really have a location;
16088 try adding a constant value attribute from the DECL_INITIAL. */
16089 return tree_add_const_value_attribute_for_decl (die, decl);
16092 /* Add VARIABLE and DIE into deferred locations list. */
16094 static void
16095 defer_location (tree variable, dw_die_ref die)
16097 deferred_locations entry;
16098 entry.variable = variable;
16099 entry.die = die;
16100 vec_safe_push (deferred_locations_list, entry);
16103 /* Helper function for tree_add_const_value_attribute. Natively encode
16104 initializer INIT into an array. Return true if successful. */
16106 static bool
16107 native_encode_initializer (tree init, unsigned char *array, int size)
16109 tree type;
16111 if (init == NULL_TREE)
16112 return false;
16114 STRIP_NOPS (init);
16115 switch (TREE_CODE (init))
16117 case STRING_CST:
16118 type = TREE_TYPE (init);
16119 if (TREE_CODE (type) == ARRAY_TYPE)
16121 tree enttype = TREE_TYPE (type);
16122 machine_mode mode = TYPE_MODE (enttype);
16124 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16125 return false;
16126 if (int_size_in_bytes (type) != size)
16127 return false;
16128 if (size > TREE_STRING_LENGTH (init))
16130 memcpy (array, TREE_STRING_POINTER (init),
16131 TREE_STRING_LENGTH (init));
16132 memset (array + TREE_STRING_LENGTH (init),
16133 '\0', size - TREE_STRING_LENGTH (init));
16135 else
16136 memcpy (array, TREE_STRING_POINTER (init), size);
16137 return true;
16139 return false;
16140 case CONSTRUCTOR:
16141 type = TREE_TYPE (init);
16142 if (int_size_in_bytes (type) != size)
16143 return false;
16144 if (TREE_CODE (type) == ARRAY_TYPE)
16146 HOST_WIDE_INT min_index;
16147 unsigned HOST_WIDE_INT cnt;
16148 int curpos = 0, fieldsize;
16149 constructor_elt *ce;
16151 if (TYPE_DOMAIN (type) == NULL_TREE
16152 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16153 return false;
16155 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16156 if (fieldsize <= 0)
16157 return false;
16159 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16160 memset (array, '\0', size);
16161 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16163 tree val = ce->value;
16164 tree index = ce->index;
16165 int pos = curpos;
16166 if (index && TREE_CODE (index) == RANGE_EXPR)
16167 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16168 * fieldsize;
16169 else if (index)
16170 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16172 if (val)
16174 STRIP_NOPS (val);
16175 if (!native_encode_initializer (val, array + pos, fieldsize))
16176 return false;
16178 curpos = pos + fieldsize;
16179 if (index && TREE_CODE (index) == RANGE_EXPR)
16181 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16182 - tree_to_shwi (TREE_OPERAND (index, 0));
16183 while (count-- > 0)
16185 if (val)
16186 memcpy (array + curpos, array + pos, fieldsize);
16187 curpos += fieldsize;
16190 gcc_assert (curpos <= size);
16192 return true;
16194 else if (TREE_CODE (type) == RECORD_TYPE
16195 || TREE_CODE (type) == UNION_TYPE)
16197 tree field = NULL_TREE;
16198 unsigned HOST_WIDE_INT cnt;
16199 constructor_elt *ce;
16201 if (int_size_in_bytes (type) != size)
16202 return false;
16204 if (TREE_CODE (type) == RECORD_TYPE)
16205 field = TYPE_FIELDS (type);
16207 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16209 tree val = ce->value;
16210 int pos, fieldsize;
16212 if (ce->index != 0)
16213 field = ce->index;
16215 if (val)
16216 STRIP_NOPS (val);
16218 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16219 return false;
16221 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16222 && TYPE_DOMAIN (TREE_TYPE (field))
16223 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16224 return false;
16225 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16226 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16227 return false;
16228 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16229 pos = int_byte_position (field);
16230 gcc_assert (pos + fieldsize <= size);
16231 if (val
16232 && !native_encode_initializer (val, array + pos, fieldsize))
16233 return false;
16235 return true;
16237 return false;
16238 case VIEW_CONVERT_EXPR:
16239 case NON_LVALUE_EXPR:
16240 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16241 default:
16242 return native_encode_expr (init, array, size) == size;
16246 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16247 attribute is the const value T. */
16249 static bool
16250 tree_add_const_value_attribute (dw_die_ref die, tree t)
16252 tree init;
16253 tree type = TREE_TYPE (t);
16254 rtx rtl;
16256 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16257 return false;
16259 init = t;
16260 gcc_assert (!DECL_P (init));
16262 rtl = rtl_for_decl_init (init, type);
16263 if (rtl)
16264 return add_const_value_attribute (die, rtl);
16265 /* If the host and target are sane, try harder. */
16266 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16267 && initializer_constant_valid_p (init, type))
16269 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16270 if (size > 0 && (int) size == size)
16272 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16274 if (native_encode_initializer (init, array, size))
16276 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16277 return true;
16279 ggc_free (array);
16282 return false;
16285 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16286 attribute is the const value of T, where T is an integral constant
16287 variable with static storage duration
16288 (so it can't be a PARM_DECL or a RESULT_DECL). */
16290 static bool
16291 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16294 if (!decl
16295 || (TREE_CODE (decl) != VAR_DECL
16296 && TREE_CODE (decl) != CONST_DECL)
16297 || (TREE_CODE (decl) == VAR_DECL
16298 && !TREE_STATIC (decl)))
16299 return false;
16301 if (TREE_READONLY (decl)
16302 && ! TREE_THIS_VOLATILE (decl)
16303 && DECL_INITIAL (decl))
16304 /* OK */;
16305 else
16306 return false;
16308 /* Don't add DW_AT_const_value if abstract origin already has one. */
16309 if (get_AT (var_die, DW_AT_const_value))
16310 return false;
16312 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16315 /* Convert the CFI instructions for the current function into a
16316 location list. This is used for DW_AT_frame_base when we targeting
16317 a dwarf2 consumer that does not support the dwarf3
16318 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16319 expressions. */
16321 static dw_loc_list_ref
16322 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16324 int ix;
16325 dw_fde_ref fde;
16326 dw_loc_list_ref list, *list_tail;
16327 dw_cfi_ref cfi;
16328 dw_cfa_location last_cfa, next_cfa;
16329 const char *start_label, *last_label, *section;
16330 dw_cfa_location remember;
16332 fde = cfun->fde;
16333 gcc_assert (fde != NULL);
16335 section = secname_for_decl (current_function_decl);
16336 list_tail = &list;
16337 list = NULL;
16339 memset (&next_cfa, 0, sizeof (next_cfa));
16340 next_cfa.reg = INVALID_REGNUM;
16341 remember = next_cfa;
16343 start_label = fde->dw_fde_begin;
16345 /* ??? Bald assumption that the CIE opcode list does not contain
16346 advance opcodes. */
16347 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16348 lookup_cfa_1 (cfi, &next_cfa, &remember);
16350 last_cfa = next_cfa;
16351 last_label = start_label;
16353 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16355 /* If the first partition contained no CFI adjustments, the
16356 CIE opcodes apply to the whole first partition. */
16357 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16358 fde->dw_fde_begin, fde->dw_fde_end, section);
16359 list_tail =&(*list_tail)->dw_loc_next;
16360 start_label = last_label = fde->dw_fde_second_begin;
16363 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16365 switch (cfi->dw_cfi_opc)
16367 case DW_CFA_set_loc:
16368 case DW_CFA_advance_loc1:
16369 case DW_CFA_advance_loc2:
16370 case DW_CFA_advance_loc4:
16371 if (!cfa_equal_p (&last_cfa, &next_cfa))
16373 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16374 start_label, last_label, section);
16376 list_tail = &(*list_tail)->dw_loc_next;
16377 last_cfa = next_cfa;
16378 start_label = last_label;
16380 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16381 break;
16383 case DW_CFA_advance_loc:
16384 /* The encoding is complex enough that we should never emit this. */
16385 gcc_unreachable ();
16387 default:
16388 lookup_cfa_1 (cfi, &next_cfa, &remember);
16389 break;
16391 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16393 if (!cfa_equal_p (&last_cfa, &next_cfa))
16395 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16396 start_label, last_label, section);
16398 list_tail = &(*list_tail)->dw_loc_next;
16399 last_cfa = next_cfa;
16400 start_label = last_label;
16402 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16403 start_label, fde->dw_fde_end, section);
16404 list_tail = &(*list_tail)->dw_loc_next;
16405 start_label = last_label = fde->dw_fde_second_begin;
16409 if (!cfa_equal_p (&last_cfa, &next_cfa))
16411 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16412 start_label, last_label, section);
16413 list_tail = &(*list_tail)->dw_loc_next;
16414 start_label = last_label;
16417 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16418 start_label,
16419 fde->dw_fde_second_begin
16420 ? fde->dw_fde_second_end : fde->dw_fde_end,
16421 section);
16423 if (list && list->dw_loc_next)
16424 gen_llsym (list);
16426 return list;
16429 /* Compute a displacement from the "steady-state frame pointer" to the
16430 frame base (often the same as the CFA), and store it in
16431 frame_pointer_fb_offset. OFFSET is added to the displacement
16432 before the latter is negated. */
16434 static void
16435 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16437 rtx reg, elim;
16439 #ifdef FRAME_POINTER_CFA_OFFSET
16440 reg = frame_pointer_rtx;
16441 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16442 #else
16443 reg = arg_pointer_rtx;
16444 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16445 #endif
16447 elim = (ira_use_lra_p
16448 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16449 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16450 if (GET_CODE (elim) == PLUS)
16452 offset += INTVAL (XEXP (elim, 1));
16453 elim = XEXP (elim, 0);
16456 frame_pointer_fb_offset = -offset;
16458 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16459 in which to eliminate. This is because it's stack pointer isn't
16460 directly accessible as a register within the ISA. To work around
16461 this, assume that while we cannot provide a proper value for
16462 frame_pointer_fb_offset, we won't need one either. */
16463 frame_pointer_fb_offset_valid
16464 = ((SUPPORTS_STACK_ALIGNMENT
16465 && (elim == hard_frame_pointer_rtx
16466 || elim == stack_pointer_rtx))
16467 || elim == (frame_pointer_needed
16468 ? hard_frame_pointer_rtx
16469 : stack_pointer_rtx));
16472 /* Generate a DW_AT_name attribute given some string value to be included as
16473 the value of the attribute. */
16475 static void
16476 add_name_attribute (dw_die_ref die, const char *name_string)
16478 if (name_string != NULL && *name_string != 0)
16480 if (demangle_name_func)
16481 name_string = (*demangle_name_func) (name_string);
16483 add_AT_string (die, DW_AT_name, name_string);
16487 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16488 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16489 of TYPE accordingly.
16491 ??? This is a temporary measure until after we're able to generate
16492 regular DWARF for the complex Ada type system. */
16494 static void
16495 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16496 dw_die_ref context_die)
16498 tree dtype;
16499 dw_die_ref dtype_die;
16501 if (!lang_hooks.types.descriptive_type)
16502 return;
16504 dtype = lang_hooks.types.descriptive_type (type);
16505 if (!dtype)
16506 return;
16508 dtype_die = lookup_type_die (dtype);
16509 if (!dtype_die)
16511 gen_type_die (dtype, context_die);
16512 dtype_die = lookup_type_die (dtype);
16513 gcc_assert (dtype_die);
16516 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16519 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16521 static const char *
16522 comp_dir_string (void)
16524 const char *wd;
16525 char *wd1;
16526 static const char *cached_wd = NULL;
16528 if (cached_wd != NULL)
16529 return cached_wd;
16531 wd = get_src_pwd ();
16532 if (wd == NULL)
16533 return NULL;
16535 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16537 int wdlen;
16539 wdlen = strlen (wd);
16540 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16541 strcpy (wd1, wd);
16542 wd1 [wdlen] = DIR_SEPARATOR;
16543 wd1 [wdlen + 1] = 0;
16544 wd = wd1;
16547 cached_wd = remap_debug_filename (wd);
16548 return cached_wd;
16551 /* Generate a DW_AT_comp_dir attribute for DIE. */
16553 static void
16554 add_comp_dir_attribute (dw_die_ref die)
16556 const char * wd = comp_dir_string ();
16557 if (wd != NULL)
16558 add_AT_string (die, DW_AT_comp_dir, wd);
16561 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16562 pointer computation, ...), output a representation for that bound according
16563 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16564 loc_list_from_tree for the meaning of CONTEXT. */
16566 static void
16567 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16568 int forms, const struct loc_descr_context *context)
16570 dw_die_ref ctx, decl_die;
16571 dw_loc_list_ref list;
16573 bool strip_conversions = true;
16575 while (strip_conversions)
16576 switch (TREE_CODE (value))
16578 case ERROR_MARK:
16579 case SAVE_EXPR:
16580 return;
16582 CASE_CONVERT:
16583 case VIEW_CONVERT_EXPR:
16584 value = TREE_OPERAND (value, 0);
16585 break;
16587 default:
16588 strip_conversions = false;
16589 break;
16592 /* If possible and permitted, output the attribute as a constant. */
16593 if ((forms & dw_scalar_form_constant) != 0
16594 && TREE_CODE (value) == INTEGER_CST)
16596 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16598 /* If HOST_WIDE_INT is big enough then represent the bound as
16599 a constant value. We need to choose a form based on
16600 whether the type is signed or unsigned. We cannot just
16601 call add_AT_unsigned if the value itself is positive
16602 (add_AT_unsigned might add the unsigned value encoded as
16603 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16604 bounds type and then sign extend any unsigned values found
16605 for signed types. This is needed only for
16606 DW_AT_{lower,upper}_bound, since for most other attributes,
16607 consumers will treat DW_FORM_data[1248] as unsigned values,
16608 regardless of the underlying type. */
16609 if (prec <= HOST_BITS_PER_WIDE_INT
16610 || tree_fits_uhwi_p (value))
16612 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16613 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16614 else
16615 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16617 else
16618 /* Otherwise represent the bound as an unsigned value with
16619 the precision of its type. The precision and signedness
16620 of the type will be necessary to re-interpret it
16621 unambiguously. */
16622 add_AT_wide (die, attr, value);
16623 return;
16626 /* Otherwise, if it's possible and permitted too, output a reference to
16627 another DIE. */
16628 if ((forms & dw_scalar_form_reference) != 0)
16630 tree decl = NULL_TREE;
16632 /* Some type attributes reference an outer type. For instance, the upper
16633 bound of an array may reference an embedding record (this happens in
16634 Ada). */
16635 if (TREE_CODE (value) == COMPONENT_REF
16636 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16637 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16638 decl = TREE_OPERAND (value, 1);
16640 else if (TREE_CODE (value) == VAR_DECL
16641 || TREE_CODE (value) == PARM_DECL
16642 || TREE_CODE (value) == RESULT_DECL)
16643 decl = value;
16645 if (decl != NULL_TREE)
16647 dw_die_ref decl_die = lookup_decl_die (decl);
16649 /* ??? Can this happen, or should the variable have been bound
16650 first? Probably it can, since I imagine that we try to create
16651 the types of parameters in the order in which they exist in
16652 the list, and won't have created a forward reference to a
16653 later parameter. */
16654 if (decl_die != NULL)
16656 add_AT_die_ref (die, attr, decl_die);
16657 return;
16662 /* Last chance: try to create a stack operation procedure to evaluate the
16663 value. Do nothing if even that is not possible or permitted. */
16664 if ((forms & dw_scalar_form_exprloc) == 0)
16665 return;
16667 list = loc_list_from_tree (value, 2, context);
16668 if (list == NULL || single_element_loc_list_p (list))
16670 /* If this attribute is not a reference nor constant, it is
16671 a DWARF expression rather than location description. For that
16672 loc_list_from_tree (value, 0, &context) is needed. */
16673 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16674 if (list2 && single_element_loc_list_p (list2))
16676 add_AT_loc (die, attr, list2->expr);
16677 return;
16681 /* If that failed to give a single element location list, fall back to
16682 outputting this as a reference... still if permitted. */
16683 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16684 return;
16686 if (current_function_decl == 0)
16687 ctx = comp_unit_die ();
16688 else
16689 ctx = lookup_decl_die (current_function_decl);
16691 decl_die = new_die (DW_TAG_variable, ctx, value);
16692 add_AT_flag (decl_die, DW_AT_artificial, 1);
16693 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16694 add_AT_location_description (decl_die, DW_AT_location, list);
16695 add_AT_die_ref (die, attr, decl_die);
16698 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16699 default. */
16701 static int
16702 lower_bound_default (void)
16704 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16706 case DW_LANG_C:
16707 case DW_LANG_C89:
16708 case DW_LANG_C99:
16709 case DW_LANG_C11:
16710 case DW_LANG_C_plus_plus:
16711 case DW_LANG_C_plus_plus_11:
16712 case DW_LANG_C_plus_plus_14:
16713 case DW_LANG_ObjC:
16714 case DW_LANG_ObjC_plus_plus:
16715 case DW_LANG_Java:
16716 return 0;
16717 case DW_LANG_Fortran77:
16718 case DW_LANG_Fortran90:
16719 case DW_LANG_Fortran95:
16720 return 1;
16721 case DW_LANG_UPC:
16722 case DW_LANG_D:
16723 case DW_LANG_Python:
16724 return dwarf_version >= 4 ? 0 : -1;
16725 case DW_LANG_Ada95:
16726 case DW_LANG_Ada83:
16727 case DW_LANG_Cobol74:
16728 case DW_LANG_Cobol85:
16729 case DW_LANG_Pascal83:
16730 case DW_LANG_Modula2:
16731 case DW_LANG_PLI:
16732 return dwarf_version >= 4 ? 1 : -1;
16733 default:
16734 return -1;
16738 /* Given a tree node describing an array bound (either lower or upper) output
16739 a representation for that bound. */
16741 static void
16742 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16743 tree bound, const struct loc_descr_context *context)
16745 int dflt;
16747 while (1)
16748 switch (TREE_CODE (bound))
16750 /* Strip all conversions. */
16751 CASE_CONVERT:
16752 case VIEW_CONVERT_EXPR:
16753 bound = TREE_OPERAND (bound, 0);
16754 break;
16756 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16757 are even omitted when they are the default. */
16758 case INTEGER_CST:
16759 /* If the value for this bound is the default one, we can even omit the
16760 attribute. */
16761 if (bound_attr == DW_AT_lower_bound
16762 && tree_fits_shwi_p (bound)
16763 && (dflt = lower_bound_default ()) != -1
16764 && tree_to_shwi (bound) == dflt)
16765 return;
16767 /* FALLTHRU */
16769 default:
16770 add_scalar_info (subrange_die, bound_attr, bound,
16771 dw_scalar_form_constant
16772 | dw_scalar_form_exprloc
16773 | dw_scalar_form_reference,
16774 context);
16775 return;
16779 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16780 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16781 Note that the block of subscript information for an array type also
16782 includes information about the element type of the given array type. */
16784 static void
16785 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16787 unsigned dimension_number;
16788 tree lower, upper;
16789 dw_die_ref subrange_die;
16791 for (dimension_number = 0;
16792 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16793 type = TREE_TYPE (type), dimension_number++)
16795 tree domain = TYPE_DOMAIN (type);
16797 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16798 break;
16800 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16801 and (in GNU C only) variable bounds. Handle all three forms
16802 here. */
16803 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16804 if (domain)
16806 /* We have an array type with specified bounds. */
16807 lower = TYPE_MIN_VALUE (domain);
16808 upper = TYPE_MAX_VALUE (domain);
16810 /* Define the index type. */
16811 if (TREE_TYPE (domain))
16813 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16814 TREE_TYPE field. We can't emit debug info for this
16815 because it is an unnamed integral type. */
16816 if (TREE_CODE (domain) == INTEGER_TYPE
16817 && TYPE_NAME (domain) == NULL_TREE
16818 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16819 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16821 else
16822 add_type_attribute (subrange_die, TREE_TYPE (domain),
16823 TYPE_UNQUALIFIED, type_die);
16826 /* ??? If upper is NULL, the array has unspecified length,
16827 but it does have a lower bound. This happens with Fortran
16828 dimension arr(N:*)
16829 Since the debugger is definitely going to need to know N
16830 to produce useful results, go ahead and output the lower
16831 bound solo, and hope the debugger can cope. */
16833 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16834 if (upper)
16835 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16838 /* Otherwise we have an array type with an unspecified length. The
16839 DWARF-2 spec does not say how to handle this; let's just leave out the
16840 bounds. */
16844 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16846 static void
16847 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16849 dw_die_ref decl_die;
16850 HOST_WIDE_INT size;
16852 switch (TREE_CODE (tree_node))
16854 case ERROR_MARK:
16855 size = 0;
16856 break;
16857 case ENUMERAL_TYPE:
16858 case RECORD_TYPE:
16859 case UNION_TYPE:
16860 case QUAL_UNION_TYPE:
16861 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16862 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16864 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16865 return;
16867 size = int_size_in_bytes (tree_node);
16868 break;
16869 case FIELD_DECL:
16870 /* For a data member of a struct or union, the DW_AT_byte_size is
16871 generally given as the number of bytes normally allocated for an
16872 object of the *declared* type of the member itself. This is true
16873 even for bit-fields. */
16874 size = int_size_in_bytes (field_type (tree_node));
16875 break;
16876 default:
16877 gcc_unreachable ();
16880 /* Note that `size' might be -1 when we get to this point. If it is, that
16881 indicates that the byte size of the entity in question is variable. We
16882 have no good way of expressing this fact in Dwarf at the present time,
16883 when location description was not used by the caller code instead. */
16884 if (size >= 0)
16885 add_AT_unsigned (die, DW_AT_byte_size, size);
16888 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16889 which specifies the distance in bits from the highest order bit of the
16890 "containing object" for the bit-field to the highest order bit of the
16891 bit-field itself.
16893 For any given bit-field, the "containing object" is a hypothetical object
16894 (of some integral or enum type) within which the given bit-field lives. The
16895 type of this hypothetical "containing object" is always the same as the
16896 declared type of the individual bit-field itself. The determination of the
16897 exact location of the "containing object" for a bit-field is rather
16898 complicated. It's handled by the `field_byte_offset' function (above).
16900 Note that it is the size (in bytes) of the hypothetical "containing object"
16901 which will be given in the DW_AT_byte_size attribute for this bit-field.
16902 (See `byte_size_attribute' above). */
16904 static inline void
16905 add_bit_offset_attribute (dw_die_ref die, tree decl)
16907 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16908 tree type = DECL_BIT_FIELD_TYPE (decl);
16909 HOST_WIDE_INT bitpos_int;
16910 HOST_WIDE_INT highest_order_object_bit_offset;
16911 HOST_WIDE_INT highest_order_field_bit_offset;
16912 HOST_WIDE_INT bit_offset;
16914 /* Must be a field and a bit field. */
16915 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16917 /* We can't yet handle bit-fields whose offsets are variable, so if we
16918 encounter such things, just return without generating any attribute
16919 whatsoever. Likewise for variable or too large size. */
16920 if (! tree_fits_shwi_p (bit_position (decl))
16921 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16922 return;
16924 bitpos_int = int_bit_position (decl);
16926 /* Note that the bit offset is always the distance (in bits) from the
16927 highest-order bit of the "containing object" to the highest-order bit of
16928 the bit-field itself. Since the "high-order end" of any object or field
16929 is different on big-endian and little-endian machines, the computation
16930 below must take account of these differences. */
16931 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16932 highest_order_field_bit_offset = bitpos_int;
16934 if (! BYTES_BIG_ENDIAN)
16936 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16937 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16940 bit_offset
16941 = (! BYTES_BIG_ENDIAN
16942 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16943 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16945 if (bit_offset < 0)
16946 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16947 else
16948 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16951 /* For a FIELD_DECL node which represents a bit field, output an attribute
16952 which specifies the length in bits of the given field. */
16954 static inline void
16955 add_bit_size_attribute (dw_die_ref die, tree decl)
16957 /* Must be a field and a bit field. */
16958 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16959 && DECL_BIT_FIELD_TYPE (decl));
16961 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16962 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16965 /* If the compiled language is ANSI C, then add a 'prototyped'
16966 attribute, if arg types are given for the parameters of a function. */
16968 static inline void
16969 add_prototyped_attribute (dw_die_ref die, tree func_type)
16971 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16973 case DW_LANG_C:
16974 case DW_LANG_C89:
16975 case DW_LANG_C99:
16976 case DW_LANG_C11:
16977 case DW_LANG_ObjC:
16978 if (prototype_p (func_type))
16979 add_AT_flag (die, DW_AT_prototyped, 1);
16980 break;
16981 default:
16982 break;
16986 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16987 by looking in either the type declaration or object declaration
16988 equate table. */
16990 static inline dw_die_ref
16991 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16993 dw_die_ref origin_die = NULL;
16995 if (TREE_CODE (origin) != FUNCTION_DECL)
16997 /* We may have gotten separated from the block for the inlined
16998 function, if we're in an exception handler or some such; make
16999 sure that the abstract function has been written out.
17001 Doing this for nested functions is wrong, however; functions are
17002 distinct units, and our context might not even be inline. */
17003 tree fn = origin;
17005 if (TYPE_P (fn))
17006 fn = TYPE_STUB_DECL (fn);
17008 fn = decl_function_context (fn);
17009 if (fn)
17010 dwarf2out_abstract_function (fn);
17013 if (DECL_P (origin))
17014 origin_die = lookup_decl_die (origin);
17015 else if (TYPE_P (origin))
17016 origin_die = lookup_type_die (origin);
17018 /* XXX: Functions that are never lowered don't always have correct block
17019 trees (in the case of java, they simply have no block tree, in some other
17020 languages). For these functions, there is nothing we can really do to
17021 output correct debug info for inlined functions in all cases. Rather
17022 than die, we'll just produce deficient debug info now, in that we will
17023 have variables without a proper abstract origin. In the future, when all
17024 functions are lowered, we should re-add a gcc_assert (origin_die)
17025 here. */
17027 if (origin_die)
17028 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17029 return origin_die;
17032 /* We do not currently support the pure_virtual attribute. */
17034 static inline void
17035 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17037 if (DECL_VINDEX (func_decl))
17039 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17041 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17042 add_AT_loc (die, DW_AT_vtable_elem_location,
17043 new_loc_descr (DW_OP_constu,
17044 tree_to_shwi (DECL_VINDEX (func_decl)),
17045 0));
17047 /* GNU extension: Record what type this method came from originally. */
17048 if (debug_info_level > DINFO_LEVEL_TERSE
17049 && DECL_CONTEXT (func_decl))
17050 add_AT_die_ref (die, DW_AT_containing_type,
17051 lookup_type_die (DECL_CONTEXT (func_decl)));
17055 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17056 given decl. This used to be a vendor extension until after DWARF 4
17057 standardized it. */
17059 static void
17060 add_linkage_attr (dw_die_ref die, tree decl)
17062 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17064 /* Mimic what assemble_name_raw does with a leading '*'. */
17065 if (name[0] == '*')
17066 name = &name[1];
17068 if (dwarf_version >= 4)
17069 add_AT_string (die, DW_AT_linkage_name, name);
17070 else
17071 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17074 /* Add source coordinate attributes for the given decl. */
17076 static void
17077 add_src_coords_attributes (dw_die_ref die, tree decl)
17079 expanded_location s;
17081 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17082 return;
17083 s = expand_location (DECL_SOURCE_LOCATION (decl));
17084 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17085 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17088 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17090 static void
17091 add_linkage_name (dw_die_ref die, tree decl)
17093 if (debug_info_level > DINFO_LEVEL_NONE
17094 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17095 && TREE_PUBLIC (decl)
17096 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17097 && die->die_tag != DW_TAG_member)
17099 /* Defer until we have an assembler name set. */
17100 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17102 limbo_die_node *asm_name;
17104 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17105 asm_name->die = die;
17106 asm_name->created_for = decl;
17107 asm_name->next = deferred_asm_name;
17108 deferred_asm_name = asm_name;
17110 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17111 add_linkage_attr (die, decl);
17115 /* Add a DW_AT_name attribute and source coordinate attribute for the
17116 given decl, but only if it actually has a name. */
17118 static void
17119 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17121 tree decl_name;
17123 decl_name = DECL_NAME (decl);
17124 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17126 const char *name = dwarf2_name (decl, 0);
17127 if (name)
17128 add_name_attribute (die, name);
17129 if (! DECL_ARTIFICIAL (decl))
17130 add_src_coords_attributes (die, decl);
17132 add_linkage_name (die, decl);
17135 #ifdef VMS_DEBUGGING_INFO
17136 /* Get the function's name, as described by its RTL. This may be different
17137 from the DECL_NAME name used in the source file. */
17138 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17140 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17141 XEXP (DECL_RTL (decl), 0), false);
17142 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17144 #endif /* VMS_DEBUGGING_INFO */
17147 #ifdef VMS_DEBUGGING_INFO
17148 /* Output the debug main pointer die for VMS */
17150 void
17151 dwarf2out_vms_debug_main_pointer (void)
17153 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17154 dw_die_ref die;
17156 /* Allocate the VMS debug main subprogram die. */
17157 die = ggc_cleared_alloc<die_node> ();
17158 die->die_tag = DW_TAG_subprogram;
17159 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17160 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17161 current_function_funcdef_no);
17162 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17164 /* Make it the first child of comp_unit_die (). */
17165 die->die_parent = comp_unit_die ();
17166 if (comp_unit_die ()->die_child)
17168 die->die_sib = comp_unit_die ()->die_child->die_sib;
17169 comp_unit_die ()->die_child->die_sib = die;
17171 else
17173 die->die_sib = die;
17174 comp_unit_die ()->die_child = die;
17177 #endif /* VMS_DEBUGGING_INFO */
17179 /* Push a new declaration scope. */
17181 static void
17182 push_decl_scope (tree scope)
17184 vec_safe_push (decl_scope_table, scope);
17187 /* Pop a declaration scope. */
17189 static inline void
17190 pop_decl_scope (void)
17192 decl_scope_table->pop ();
17195 /* walk_tree helper function for uses_local_type, below. */
17197 static tree
17198 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17200 if (!TYPE_P (*tp))
17201 *walk_subtrees = 0;
17202 else
17204 tree name = TYPE_NAME (*tp);
17205 if (name && DECL_P (name) && decl_function_context (name))
17206 return *tp;
17208 return NULL_TREE;
17211 /* If TYPE involves a function-local type (including a local typedef to a
17212 non-local type), returns that type; otherwise returns NULL_TREE. */
17214 static tree
17215 uses_local_type (tree type)
17217 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17218 return used;
17221 /* Return the DIE for the scope that immediately contains this type.
17222 Non-named types that do not involve a function-local type get global
17223 scope. Named types nested in namespaces or other types get their
17224 containing scope. All other types (i.e. function-local named types) get
17225 the current active scope. */
17227 static dw_die_ref
17228 scope_die_for (tree t, dw_die_ref context_die)
17230 dw_die_ref scope_die = NULL;
17231 tree containing_scope;
17233 /* Non-types always go in the current scope. */
17234 gcc_assert (TYPE_P (t));
17236 /* Use the scope of the typedef, rather than the scope of the type
17237 it refers to. */
17238 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17239 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17240 else
17241 containing_scope = TYPE_CONTEXT (t);
17243 /* Use the containing namespace if there is one. */
17244 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17246 if (context_die == lookup_decl_die (containing_scope))
17247 /* OK */;
17248 else if (debug_info_level > DINFO_LEVEL_TERSE)
17249 context_die = get_context_die (containing_scope);
17250 else
17251 containing_scope = NULL_TREE;
17254 /* Ignore function type "scopes" from the C frontend. They mean that
17255 a tagged type is local to a parmlist of a function declarator, but
17256 that isn't useful to DWARF. */
17257 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17258 containing_scope = NULL_TREE;
17260 if (SCOPE_FILE_SCOPE_P (containing_scope))
17262 /* If T uses a local type keep it local as well, to avoid references
17263 to function-local DIEs from outside the function. */
17264 if (current_function_decl && uses_local_type (t))
17265 scope_die = context_die;
17266 else
17267 scope_die = comp_unit_die ();
17269 else if (TYPE_P (containing_scope))
17271 /* For types, we can just look up the appropriate DIE. */
17272 if (debug_info_level > DINFO_LEVEL_TERSE)
17273 scope_die = get_context_die (containing_scope);
17274 else
17276 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17277 if (scope_die == NULL)
17278 scope_die = comp_unit_die ();
17281 else
17282 scope_die = context_die;
17284 return scope_die;
17287 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17289 static inline int
17290 local_scope_p (dw_die_ref context_die)
17292 for (; context_die; context_die = context_die->die_parent)
17293 if (context_die->die_tag == DW_TAG_inlined_subroutine
17294 || context_die->die_tag == DW_TAG_subprogram)
17295 return 1;
17297 return 0;
17300 /* Returns nonzero if CONTEXT_DIE is a class. */
17302 static inline int
17303 class_scope_p (dw_die_ref context_die)
17305 return (context_die
17306 && (context_die->die_tag == DW_TAG_structure_type
17307 || context_die->die_tag == DW_TAG_class_type
17308 || context_die->die_tag == DW_TAG_interface_type
17309 || context_die->die_tag == DW_TAG_union_type));
17312 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17313 whether or not to treat a DIE in this context as a declaration. */
17315 static inline int
17316 class_or_namespace_scope_p (dw_die_ref context_die)
17318 return (class_scope_p (context_die)
17319 || (context_die && context_die->die_tag == DW_TAG_namespace));
17322 /* Many forms of DIEs require a "type description" attribute. This
17323 routine locates the proper "type descriptor" die for the type given
17324 by 'type' plus any additional qualifiers given by 'cv_quals', and
17325 adds a DW_AT_type attribute below the given die. */
17327 static void
17328 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17329 dw_die_ref context_die)
17331 enum tree_code code = TREE_CODE (type);
17332 dw_die_ref type_die = NULL;
17334 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17335 or fixed-point type, use the inner type. This is because we have no
17336 support for unnamed types in base_type_die. This can happen if this is
17337 an Ada subrange type. Correct solution is emit a subrange type die. */
17338 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17339 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17340 type = TREE_TYPE (type), code = TREE_CODE (type);
17342 if (code == ERROR_MARK
17343 /* Handle a special case. For functions whose return type is void, we
17344 generate *no* type attribute. (Note that no object may have type
17345 `void', so this only applies to function return types). */
17346 || code == VOID_TYPE)
17347 return;
17349 type_die = modified_type_die (type,
17350 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17351 context_die);
17353 if (type_die != NULL)
17354 add_AT_die_ref (object_die, DW_AT_type, type_die);
17357 /* Given an object die, add the calling convention attribute for the
17358 function call type. */
17359 static void
17360 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17362 enum dwarf_calling_convention value = DW_CC_normal;
17364 value = ((enum dwarf_calling_convention)
17365 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17367 if (is_fortran ()
17368 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17370 /* DWARF 2 doesn't provide a way to identify a program's source-level
17371 entry point. DW_AT_calling_convention attributes are only meant
17372 to describe functions' calling conventions. However, lacking a
17373 better way to signal the Fortran main program, we used this for
17374 a long time, following existing custom. Now, DWARF 4 has
17375 DW_AT_main_subprogram, which we add below, but some tools still
17376 rely on the old way, which we thus keep. */
17377 value = DW_CC_program;
17379 if (dwarf_version >= 4 || !dwarf_strict)
17380 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17383 /* Only add the attribute if the backend requests it, and
17384 is not DW_CC_normal. */
17385 if (value && (value != DW_CC_normal))
17386 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17389 /* Given a tree pointer to a struct, class, union, or enum type node, return
17390 a pointer to the (string) tag name for the given type, or zero if the type
17391 was declared without a tag. */
17393 static const char *
17394 type_tag (const_tree type)
17396 const char *name = 0;
17398 if (TYPE_NAME (type) != 0)
17400 tree t = 0;
17402 /* Find the IDENTIFIER_NODE for the type name. */
17403 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17404 && !TYPE_NAMELESS (type))
17405 t = TYPE_NAME (type);
17407 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17408 a TYPE_DECL node, regardless of whether or not a `typedef' was
17409 involved. */
17410 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17411 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17413 /* We want to be extra verbose. Don't call dwarf_name if
17414 DECL_NAME isn't set. The default hook for decl_printable_name
17415 doesn't like that, and in this context it's correct to return
17416 0, instead of "<anonymous>" or the like. */
17417 if (DECL_NAME (TYPE_NAME (type))
17418 && !DECL_NAMELESS (TYPE_NAME (type)))
17419 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17422 /* Now get the name as a string, or invent one. */
17423 if (!name && t != 0)
17424 name = IDENTIFIER_POINTER (t);
17427 return (name == 0 || *name == '\0') ? 0 : name;
17430 /* Return the type associated with a data member, make a special check
17431 for bit field types. */
17433 static inline tree
17434 member_declared_type (const_tree member)
17436 return (DECL_BIT_FIELD_TYPE (member)
17437 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17440 /* Get the decl's label, as described by its RTL. This may be different
17441 from the DECL_NAME name used in the source file. */
17443 #if 0
17444 static const char *
17445 decl_start_label (tree decl)
17447 rtx x;
17448 const char *fnname;
17450 x = DECL_RTL (decl);
17451 gcc_assert (MEM_P (x));
17453 x = XEXP (x, 0);
17454 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17456 fnname = XSTR (x, 0);
17457 return fnname;
17459 #endif
17461 /* These routines generate the internal representation of the DIE's for
17462 the compilation unit. Debugging information is collected by walking
17463 the declaration trees passed in from dwarf2out_decl(). */
17465 static void
17466 gen_array_type_die (tree type, dw_die_ref context_die)
17468 dw_die_ref scope_die = scope_die_for (type, context_die);
17469 dw_die_ref array_die;
17471 /* GNU compilers represent multidimensional array types as sequences of one
17472 dimensional array types whose element types are themselves array types.
17473 We sometimes squish that down to a single array_type DIE with multiple
17474 subscripts in the Dwarf debugging info. The draft Dwarf specification
17475 say that we are allowed to do this kind of compression in C, because
17476 there is no difference between an array of arrays and a multidimensional
17477 array. We don't do this for Ada to remain as close as possible to the
17478 actual representation, which is especially important against the language
17479 flexibilty wrt arrays of variable size. */
17481 bool collapse_nested_arrays = !is_ada ();
17482 tree element_type;
17484 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17485 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17486 if (TYPE_STRING_FLAG (type)
17487 && TREE_CODE (type) == ARRAY_TYPE
17488 && is_fortran ()
17489 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17491 HOST_WIDE_INT size;
17493 array_die = new_die (DW_TAG_string_type, scope_die, type);
17494 add_name_attribute (array_die, type_tag (type));
17495 equate_type_number_to_die (type, array_die);
17496 size = int_size_in_bytes (type);
17497 if (size >= 0)
17498 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17499 else if (TYPE_DOMAIN (type) != NULL_TREE
17500 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17501 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17503 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17504 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17506 size = int_size_in_bytes (TREE_TYPE (szdecl));
17507 if (loc && size > 0)
17509 add_AT_location_description (array_die, DW_AT_string_length, loc);
17510 if (size != DWARF2_ADDR_SIZE)
17511 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17514 return;
17517 array_die = new_die (DW_TAG_array_type, scope_die, type);
17518 add_name_attribute (array_die, type_tag (type));
17519 equate_type_number_to_die (type, array_die);
17521 if (TREE_CODE (type) == VECTOR_TYPE)
17522 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17524 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17525 if (is_fortran ()
17526 && TREE_CODE (type) == ARRAY_TYPE
17527 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17528 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17529 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17531 #if 0
17532 /* We default the array ordering. SDB will probably do
17533 the right things even if DW_AT_ordering is not present. It's not even
17534 an issue until we start to get into multidimensional arrays anyway. If
17535 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17536 then we'll have to put the DW_AT_ordering attribute back in. (But if
17537 and when we find out that we need to put these in, we will only do so
17538 for multidimensional arrays. */
17539 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17540 #endif
17542 if (TREE_CODE (type) == VECTOR_TYPE)
17544 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17545 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17546 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17547 add_bound_info (subrange_die, DW_AT_upper_bound,
17548 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17550 else
17551 add_subscript_info (array_die, type, collapse_nested_arrays);
17553 /* Add representation of the type of the elements of this array type and
17554 emit the corresponding DIE if we haven't done it already. */
17555 element_type = TREE_TYPE (type);
17556 if (collapse_nested_arrays)
17557 while (TREE_CODE (element_type) == ARRAY_TYPE)
17559 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17560 break;
17561 element_type = TREE_TYPE (element_type);
17564 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17566 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17567 if (TYPE_ARTIFICIAL (type))
17568 add_AT_flag (array_die, DW_AT_artificial, 1);
17570 if (get_AT (array_die, DW_AT_name))
17571 add_pubtype (type, array_die);
17574 /* This routine generates DIE for array with hidden descriptor, details
17575 are filled into *info by a langhook. */
17577 static void
17578 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17579 dw_die_ref context_die)
17581 const dw_die_ref scope_die = scope_die_for (type, context_die);
17582 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17583 const struct loc_descr_context context = { type, info->base_decl };
17584 int dim;
17586 add_name_attribute (array_die, type_tag (type));
17587 equate_type_number_to_die (type, array_die);
17589 if (info->ndimensions > 1)
17590 switch (info->ordering)
17592 case array_descr_ordering_row_major:
17593 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17594 break;
17595 case array_descr_ordering_column_major:
17596 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17597 break;
17598 default:
17599 break;
17602 if (dwarf_version >= 3 || !dwarf_strict)
17604 if (info->data_location)
17605 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17606 dw_scalar_form_exprloc, &context);
17607 if (info->associated)
17608 add_scalar_info (array_die, DW_AT_associated, info->associated,
17609 dw_scalar_form_constant
17610 | dw_scalar_form_exprloc
17611 | dw_scalar_form_reference, &context);
17612 if (info->allocated)
17613 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17614 dw_scalar_form_constant
17615 | dw_scalar_form_exprloc
17616 | dw_scalar_form_reference, &context);
17619 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17621 for (dim = 0; dim < info->ndimensions; dim++)
17623 dw_die_ref subrange_die
17624 = new_die (DW_TAG_subrange_type, array_die, NULL);
17626 if (info->dimen[dim].bounds_type)
17627 add_type_attribute (subrange_die,
17628 info->dimen[dim].bounds_type, 0,
17629 context_die);
17630 if (info->dimen[dim].lower_bound)
17631 add_bound_info (subrange_die, DW_AT_lower_bound,
17632 info->dimen[dim].lower_bound, &context);
17633 if (info->dimen[dim].upper_bound)
17634 add_bound_info (subrange_die, DW_AT_upper_bound,
17635 info->dimen[dim].upper_bound, &context);
17636 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17637 add_scalar_info (subrange_die, DW_AT_byte_stride,
17638 info->dimen[dim].stride,
17639 dw_scalar_form_constant
17640 | dw_scalar_form_exprloc
17641 | dw_scalar_form_reference,
17642 &context);
17645 gen_type_die (info->element_type, context_die);
17646 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17647 context_die);
17649 if (get_AT (array_die, DW_AT_name))
17650 add_pubtype (type, array_die);
17653 #if 0
17654 static void
17655 gen_entry_point_die (tree decl, dw_die_ref context_die)
17657 tree origin = decl_ultimate_origin (decl);
17658 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17660 if (origin != NULL)
17661 add_abstract_origin_attribute (decl_die, origin);
17662 else
17664 add_name_and_src_coords_attributes (decl_die, decl);
17665 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17666 TYPE_UNQUALIFIED, context_die);
17669 if (DECL_ABSTRACT_P (decl))
17670 equate_decl_number_to_die (decl, decl_die);
17671 else
17672 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17674 #endif
17676 /* Walk through the list of incomplete types again, trying once more to
17677 emit full debugging info for them. */
17679 static void
17680 retry_incomplete_types (void)
17682 int i;
17684 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17685 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17686 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17689 /* Determine what tag to use for a record type. */
17691 static enum dwarf_tag
17692 record_type_tag (tree type)
17694 if (! lang_hooks.types.classify_record)
17695 return DW_TAG_structure_type;
17697 switch (lang_hooks.types.classify_record (type))
17699 case RECORD_IS_STRUCT:
17700 return DW_TAG_structure_type;
17702 case RECORD_IS_CLASS:
17703 return DW_TAG_class_type;
17705 case RECORD_IS_INTERFACE:
17706 if (dwarf_version >= 3 || !dwarf_strict)
17707 return DW_TAG_interface_type;
17708 return DW_TAG_structure_type;
17710 default:
17711 gcc_unreachable ();
17715 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17716 include all of the information about the enumeration values also. Each
17717 enumerated type name/value is listed as a child of the enumerated type
17718 DIE. */
17720 static dw_die_ref
17721 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17723 dw_die_ref type_die = lookup_type_die (type);
17725 if (type_die == NULL)
17727 type_die = new_die (DW_TAG_enumeration_type,
17728 scope_die_for (type, context_die), type);
17729 equate_type_number_to_die (type, type_die);
17730 add_name_attribute (type_die, type_tag (type));
17731 if (dwarf_version >= 4 || !dwarf_strict)
17733 if (ENUM_IS_SCOPED (type))
17734 add_AT_flag (type_die, DW_AT_enum_class, 1);
17735 if (ENUM_IS_OPAQUE (type))
17736 add_AT_flag (type_die, DW_AT_declaration, 1);
17739 else if (! TYPE_SIZE (type))
17740 return type_die;
17741 else
17742 remove_AT (type_die, DW_AT_declaration);
17744 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17745 given enum type is incomplete, do not generate the DW_AT_byte_size
17746 attribute or the DW_AT_element_list attribute. */
17747 if (TYPE_SIZE (type))
17749 tree link;
17751 TREE_ASM_WRITTEN (type) = 1;
17752 add_byte_size_attribute (type_die, type);
17753 if (dwarf_version >= 3 || !dwarf_strict)
17755 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17756 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17757 context_die);
17759 if (TYPE_STUB_DECL (type) != NULL_TREE)
17761 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17762 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17765 /* If the first reference to this type was as the return type of an
17766 inline function, then it may not have a parent. Fix this now. */
17767 if (type_die->die_parent == NULL)
17768 add_child_die (scope_die_for (type, context_die), type_die);
17770 for (link = TYPE_VALUES (type);
17771 link != NULL; link = TREE_CHAIN (link))
17773 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17774 tree value = TREE_VALUE (link);
17776 add_name_attribute (enum_die,
17777 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17779 if (TREE_CODE (value) == CONST_DECL)
17780 value = DECL_INITIAL (value);
17782 if (simple_type_size_in_bits (TREE_TYPE (value))
17783 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17785 /* For constant forms created by add_AT_unsigned DWARF
17786 consumers (GDB, elfutils, etc.) always zero extend
17787 the value. Only when the actual value is negative
17788 do we need to use add_AT_int to generate a constant
17789 form that can represent negative values. */
17790 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17791 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17792 add_AT_unsigned (enum_die, DW_AT_const_value,
17793 (unsigned HOST_WIDE_INT) val);
17794 else
17795 add_AT_int (enum_die, DW_AT_const_value, val);
17797 else
17798 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17799 that here. TODO: This should be re-worked to use correct
17800 signed/unsigned double tags for all cases. */
17801 add_AT_wide (enum_die, DW_AT_const_value, value);
17804 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17805 if (TYPE_ARTIFICIAL (type))
17806 add_AT_flag (type_die, DW_AT_artificial, 1);
17808 else
17809 add_AT_flag (type_die, DW_AT_declaration, 1);
17811 add_pubtype (type, type_die);
17813 return type_die;
17816 /* Generate a DIE to represent either a real live formal parameter decl or to
17817 represent just the type of some formal parameter position in some function
17818 type.
17820 Note that this routine is a bit unusual because its argument may be a
17821 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17822 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17823 node. If it's the former then this function is being called to output a
17824 DIE to represent a formal parameter object (or some inlining thereof). If
17825 it's the latter, then this function is only being called to output a
17826 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17827 argument type of some subprogram type.
17828 If EMIT_NAME_P is true, name and source coordinate attributes
17829 are emitted. */
17831 static dw_die_ref
17832 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17833 dw_die_ref context_die)
17835 tree node_or_origin = node ? node : origin;
17836 tree ultimate_origin;
17837 dw_die_ref parm_die
17838 = new_die (DW_TAG_formal_parameter, context_die, node);
17840 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17842 case tcc_declaration:
17843 ultimate_origin = decl_ultimate_origin (node_or_origin);
17844 if (node || ultimate_origin)
17845 origin = ultimate_origin;
17846 if (origin != NULL)
17847 add_abstract_origin_attribute (parm_die, origin);
17848 else if (emit_name_p)
17849 add_name_and_src_coords_attributes (parm_die, node);
17850 if (origin == NULL
17851 || (! DECL_ABSTRACT_P (node_or_origin)
17852 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17853 decl_function_context
17854 (node_or_origin))))
17856 tree type = TREE_TYPE (node_or_origin);
17857 if (decl_by_reference_p (node_or_origin))
17858 add_type_attribute (parm_die, TREE_TYPE (type),
17859 TYPE_UNQUALIFIED, context_die);
17860 else
17861 add_type_attribute (parm_die, type,
17862 decl_quals (node_or_origin),
17863 context_die);
17865 if (origin == NULL && DECL_ARTIFICIAL (node))
17866 add_AT_flag (parm_die, DW_AT_artificial, 1);
17868 if (node && node != origin)
17869 equate_decl_number_to_die (node, parm_die);
17870 if (! DECL_ABSTRACT_P (node_or_origin))
17871 add_location_or_const_value_attribute (parm_die, node_or_origin,
17872 node == NULL, DW_AT_location);
17874 break;
17876 case tcc_type:
17877 /* We were called with some kind of a ..._TYPE node. */
17878 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17879 context_die);
17880 break;
17882 default:
17883 gcc_unreachable ();
17886 return parm_die;
17889 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17890 children DW_TAG_formal_parameter DIEs representing the arguments of the
17891 parameter pack.
17893 PARM_PACK must be a function parameter pack.
17894 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17895 must point to the subsequent arguments of the function PACK_ARG belongs to.
17896 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17897 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17898 following the last one for which a DIE was generated. */
17900 static dw_die_ref
17901 gen_formal_parameter_pack_die (tree parm_pack,
17902 tree pack_arg,
17903 dw_die_ref subr_die,
17904 tree *next_arg)
17906 tree arg;
17907 dw_die_ref parm_pack_die;
17909 gcc_assert (parm_pack
17910 && lang_hooks.function_parameter_pack_p (parm_pack)
17911 && subr_die);
17913 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17914 add_src_coords_attributes (parm_pack_die, parm_pack);
17916 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17918 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17919 parm_pack))
17920 break;
17921 gen_formal_parameter_die (arg, NULL,
17922 false /* Don't emit name attribute. */,
17923 parm_pack_die);
17925 if (next_arg)
17926 *next_arg = arg;
17927 return parm_pack_die;
17930 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17931 at the end of an (ANSI prototyped) formal parameters list. */
17933 static void
17934 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17936 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17939 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17940 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17941 parameters as specified in some function type specification (except for
17942 those which appear as part of a function *definition*). */
17944 static void
17945 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17947 tree link;
17948 tree formal_type = NULL;
17949 tree first_parm_type;
17950 tree arg;
17952 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17954 arg = DECL_ARGUMENTS (function_or_method_type);
17955 function_or_method_type = TREE_TYPE (function_or_method_type);
17957 else
17958 arg = NULL_TREE;
17960 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17962 /* Make our first pass over the list of formal parameter types and output a
17963 DW_TAG_formal_parameter DIE for each one. */
17964 for (link = first_parm_type; link; )
17966 dw_die_ref parm_die;
17968 formal_type = TREE_VALUE (link);
17969 if (formal_type == void_type_node)
17970 break;
17972 /* Output a (nameless) DIE to represent the formal parameter itself. */
17973 if (!POINTER_BOUNDS_TYPE_P (formal_type))
17975 parm_die = gen_formal_parameter_die (formal_type, NULL,
17976 true /* Emit name attribute. */,
17977 context_die);
17978 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17979 && link == first_parm_type)
17981 add_AT_flag (parm_die, DW_AT_artificial, 1);
17982 if (dwarf_version >= 3 || !dwarf_strict)
17983 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17985 else if (arg && DECL_ARTIFICIAL (arg))
17986 add_AT_flag (parm_die, DW_AT_artificial, 1);
17989 link = TREE_CHAIN (link);
17990 if (arg)
17991 arg = DECL_CHAIN (arg);
17994 /* If this function type has an ellipsis, add a
17995 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17996 if (formal_type != void_type_node)
17997 gen_unspecified_parameters_die (function_or_method_type, context_die);
17999 /* Make our second (and final) pass over the list of formal parameter types
18000 and output DIEs to represent those types (as necessary). */
18001 for (link = TYPE_ARG_TYPES (function_or_method_type);
18002 link && TREE_VALUE (link);
18003 link = TREE_CHAIN (link))
18004 gen_type_die (TREE_VALUE (link), context_die);
18007 /* We want to generate the DIE for TYPE so that we can generate the
18008 die for MEMBER, which has been defined; we will need to refer back
18009 to the member declaration nested within TYPE. If we're trying to
18010 generate minimal debug info for TYPE, processing TYPE won't do the
18011 trick; we need to attach the member declaration by hand. */
18013 static void
18014 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18016 gen_type_die (type, context_die);
18018 /* If we're trying to avoid duplicate debug info, we may not have
18019 emitted the member decl for this function. Emit it now. */
18020 if (TYPE_STUB_DECL (type)
18021 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18022 && ! lookup_decl_die (member))
18024 dw_die_ref type_die;
18025 gcc_assert (!decl_ultimate_origin (member));
18027 push_decl_scope (type);
18028 type_die = lookup_type_die_strip_naming_typedef (type);
18029 if (TREE_CODE (member) == FUNCTION_DECL)
18030 gen_subprogram_die (member, type_die);
18031 else if (TREE_CODE (member) == FIELD_DECL)
18033 /* Ignore the nameless fields that are used to skip bits but handle
18034 C++ anonymous unions and structs. */
18035 if (DECL_NAME (member) != NULL_TREE
18036 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18037 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18039 gen_type_die (member_declared_type (member), type_die);
18040 gen_field_die (member, type_die);
18043 else
18044 gen_variable_die (member, NULL_TREE, type_die);
18046 pop_decl_scope ();
18050 /* Forward declare these functions, because they are mutually recursive
18051 with their set_block_* pairing functions. */
18052 static void set_decl_origin_self (tree);
18053 static void set_decl_abstract_flags (tree, int);
18055 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18056 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18057 that it points to the node itself, thus indicating that the node is its
18058 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18059 the given node is NULL, recursively descend the decl/block tree which
18060 it is the root of, and for each other ..._DECL or BLOCK node contained
18061 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18062 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18063 values to point to themselves. */
18065 static void
18066 set_block_origin_self (tree stmt)
18068 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18070 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18073 tree local_decl;
18075 for (local_decl = BLOCK_VARS (stmt);
18076 local_decl != NULL_TREE;
18077 local_decl = DECL_CHAIN (local_decl))
18078 /* Do not recurse on nested functions since the inlining status
18079 of parent and child can be different as per the DWARF spec. */
18080 if (TREE_CODE (local_decl) != FUNCTION_DECL
18081 && !DECL_EXTERNAL (local_decl))
18082 set_decl_origin_self (local_decl);
18086 tree subblock;
18088 for (subblock = BLOCK_SUBBLOCKS (stmt);
18089 subblock != NULL_TREE;
18090 subblock = BLOCK_CHAIN (subblock))
18091 set_block_origin_self (subblock); /* Recurse. */
18096 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18097 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18098 node to so that it points to the node itself, thus indicating that the
18099 node represents its own (abstract) origin. Additionally, if the
18100 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18101 the decl/block tree of which the given node is the root of, and for
18102 each other ..._DECL or BLOCK node contained therein whose
18103 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18104 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18105 point to themselves. */
18107 static void
18108 set_decl_origin_self (tree decl)
18110 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18112 DECL_ABSTRACT_ORIGIN (decl) = decl;
18113 if (TREE_CODE (decl) == FUNCTION_DECL)
18115 tree arg;
18117 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18118 DECL_ABSTRACT_ORIGIN (arg) = arg;
18119 if (DECL_INITIAL (decl) != NULL_TREE
18120 && DECL_INITIAL (decl) != error_mark_node)
18121 set_block_origin_self (DECL_INITIAL (decl));
18126 /* Given a pointer to some BLOCK node, and a boolean value to set the
18127 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
18128 the given block, and for all local decls and all local sub-blocks
18129 (recursively) which are contained therein. */
18131 static void
18132 set_block_abstract_flags (tree stmt, int setting)
18134 tree local_decl;
18135 tree subblock;
18136 unsigned int i;
18138 BLOCK_ABSTRACT (stmt) = setting;
18140 for (local_decl = BLOCK_VARS (stmt);
18141 local_decl != NULL_TREE;
18142 local_decl = DECL_CHAIN (local_decl))
18143 if (! DECL_EXTERNAL (local_decl))
18144 set_decl_abstract_flags (local_decl, setting);
18146 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18148 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18149 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18150 || TREE_CODE (local_decl) == PARM_DECL)
18151 set_decl_abstract_flags (local_decl, setting);
18154 for (subblock = BLOCK_SUBBLOCKS (stmt);
18155 subblock != NULL_TREE;
18156 subblock = BLOCK_CHAIN (subblock))
18157 set_block_abstract_flags (subblock, setting);
18160 /* Given a pointer to some ..._DECL node, and a boolean value to set the
18161 "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
18162 given decl, and (in the case where the decl is a FUNCTION_DECL) also
18163 set the abstract flags for all of the parameters, local vars, local
18164 blocks and sub-blocks (recursively) to the same setting. */
18166 static void
18167 set_decl_abstract_flags (tree decl, int setting)
18169 DECL_ABSTRACT_P (decl) = setting;
18170 if (TREE_CODE (decl) == FUNCTION_DECL)
18172 tree arg;
18174 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18175 DECL_ABSTRACT_P (arg) = setting;
18176 if (DECL_INITIAL (decl) != NULL_TREE
18177 && DECL_INITIAL (decl) != error_mark_node)
18178 set_block_abstract_flags (DECL_INITIAL (decl), setting);
18182 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18183 may later generate inlined and/or out-of-line instances of. */
18185 static void
18186 dwarf2out_abstract_function (tree decl)
18188 dw_die_ref old_die;
18189 tree save_fn;
18190 tree context;
18191 int was_abstract;
18192 hash_table<decl_loc_hasher> *old_decl_loc_table;
18193 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18194 int old_call_site_count, old_tail_call_site_count;
18195 struct call_arg_loc_node *old_call_arg_locations;
18197 /* Make sure we have the actual abstract inline, not a clone. */
18198 decl = DECL_ORIGIN (decl);
18200 old_die = lookup_decl_die (decl);
18201 if (old_die && get_AT (old_die, DW_AT_inline))
18202 /* We've already generated the abstract instance. */
18203 return;
18205 /* We can be called while recursively when seeing block defining inlined subroutine
18206 DIE. Be sure to not clobber the outer location table nor use it or we would
18207 get locations in abstract instantces. */
18208 old_decl_loc_table = decl_loc_table;
18209 decl_loc_table = NULL;
18210 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18211 cached_dw_loc_list_table = NULL;
18212 old_call_arg_locations = call_arg_locations;
18213 call_arg_locations = NULL;
18214 old_call_site_count = call_site_count;
18215 call_site_count = -1;
18216 old_tail_call_site_count = tail_call_site_count;
18217 tail_call_site_count = -1;
18219 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18220 we don't get confused by DECL_ABSTRACT_P. */
18221 if (debug_info_level > DINFO_LEVEL_TERSE)
18223 context = decl_class_context (decl);
18224 if (context)
18225 gen_type_die_for_member
18226 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18229 /* Pretend we've just finished compiling this function. */
18230 save_fn = current_function_decl;
18231 current_function_decl = decl;
18233 was_abstract = DECL_ABSTRACT_P (decl);
18234 set_decl_abstract_flags (decl, 1);
18235 dwarf2out_decl (decl);
18236 if (! was_abstract)
18237 set_decl_abstract_flags (decl, 0);
18239 current_function_decl = save_fn;
18240 decl_loc_table = old_decl_loc_table;
18241 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18242 call_arg_locations = old_call_arg_locations;
18243 call_site_count = old_call_site_count;
18244 tail_call_site_count = old_tail_call_site_count;
18247 /* Helper function of premark_used_types() which gets called through
18248 htab_traverse.
18250 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18251 marked as unused by prune_unused_types. */
18253 bool
18254 premark_used_types_helper (tree const &type, void *)
18256 dw_die_ref die;
18258 die = lookup_type_die (type);
18259 if (die != NULL)
18260 die->die_perennial_p = 1;
18261 return true;
18264 /* Helper function of premark_types_used_by_global_vars which gets called
18265 through htab_traverse.
18267 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18268 marked as unused by prune_unused_types. The DIE of the type is marked
18269 only if the global variable using the type will actually be emitted. */
18272 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18273 void *)
18275 struct types_used_by_vars_entry *entry;
18276 dw_die_ref die;
18278 entry = (struct types_used_by_vars_entry *) *slot;
18279 gcc_assert (entry->type != NULL
18280 && entry->var_decl != NULL);
18281 die = lookup_type_die (entry->type);
18282 if (die)
18284 /* Ask cgraph if the global variable really is to be emitted.
18285 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18286 varpool_node *node = varpool_node::get (entry->var_decl);
18287 if (node && node->definition)
18289 die->die_perennial_p = 1;
18290 /* Keep the parent DIEs as well. */
18291 while ((die = die->die_parent) && die->die_perennial_p == 0)
18292 die->die_perennial_p = 1;
18295 return 1;
18298 /* Mark all members of used_types_hash as perennial. */
18300 static void
18301 premark_used_types (struct function *fun)
18303 if (fun && fun->used_types_hash)
18304 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18307 /* Mark all members of types_used_by_vars_entry as perennial. */
18309 static void
18310 premark_types_used_by_global_vars (void)
18312 if (types_used_by_vars_hash)
18313 types_used_by_vars_hash
18314 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18317 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18318 for CA_LOC call arg loc node. */
18320 static dw_die_ref
18321 gen_call_site_die (tree decl, dw_die_ref subr_die,
18322 struct call_arg_loc_node *ca_loc)
18324 dw_die_ref stmt_die = NULL, die;
18325 tree block = ca_loc->block;
18327 while (block
18328 && block != DECL_INITIAL (decl)
18329 && TREE_CODE (block) == BLOCK)
18331 if (block_map.length () > BLOCK_NUMBER (block))
18332 stmt_die = block_map[BLOCK_NUMBER (block)];
18333 if (stmt_die)
18334 break;
18335 block = BLOCK_SUPERCONTEXT (block);
18337 if (stmt_die == NULL)
18338 stmt_die = subr_die;
18339 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18340 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18341 if (ca_loc->tail_call_p)
18342 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18343 if (ca_loc->symbol_ref)
18345 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18346 if (tdie)
18347 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18348 else
18349 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18351 return die;
18354 /* Generate a DIE to represent a declared function (either file-scope or
18355 block-local). */
18357 static void
18358 gen_subprogram_die (tree decl, dw_die_ref context_die)
18360 tree origin = decl_ultimate_origin (decl);
18361 dw_die_ref subr_die;
18362 tree outer_scope;
18363 dw_die_ref old_die = lookup_decl_die (decl);
18364 int declaration = (current_function_decl != decl
18365 || class_or_namespace_scope_p (context_die));
18367 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18369 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18370 started to generate the abstract instance of an inline, decided to output
18371 its containing class, and proceeded to emit the declaration of the inline
18372 from the member list for the class. If so, DECLARATION takes priority;
18373 we'll get back to the abstract instance when done with the class. */
18375 /* The class-scope declaration DIE must be the primary DIE. */
18376 if (origin && declaration && class_or_namespace_scope_p (context_die))
18378 origin = NULL;
18379 gcc_assert (!old_die);
18382 /* Now that the C++ front end lazily declares artificial member fns, we
18383 might need to retrofit the declaration into its class. */
18384 if (!declaration && !origin && !old_die
18385 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18386 && !class_or_namespace_scope_p (context_die)
18387 && debug_info_level > DINFO_LEVEL_TERSE)
18388 old_die = force_decl_die (decl);
18390 if (origin != NULL)
18392 gcc_assert (!declaration || local_scope_p (context_die));
18394 /* Fixup die_parent for the abstract instance of a nested
18395 inline function. */
18396 if (old_die && old_die->die_parent == NULL)
18397 add_child_die (context_die, old_die);
18399 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18400 add_abstract_origin_attribute (subr_die, origin);
18401 /* This is where the actual code for a cloned function is.
18402 Let's emit linkage name attribute for it. This helps
18403 debuggers to e.g, set breakpoints into
18404 constructors/destructors when the user asks "break
18405 K::K". */
18406 add_linkage_name (subr_die, decl);
18408 else if (old_die)
18410 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18411 struct dwarf_file_data * file_index = lookup_filename (s.file);
18413 if (!get_AT_flag (old_die, DW_AT_declaration)
18414 /* We can have a normal definition following an inline one in the
18415 case of redefinition of GNU C extern inlines.
18416 It seems reasonable to use AT_specification in this case. */
18417 && !get_AT (old_die, DW_AT_inline))
18419 /* Detect and ignore this case, where we are trying to output
18420 something we have already output. */
18421 return;
18424 /* If the definition comes from the same place as the declaration,
18425 maybe use the old DIE. We always want the DIE for this function
18426 that has the *_pc attributes to be under comp_unit_die so the
18427 debugger can find it. We also need to do this for abstract
18428 instances of inlines, since the spec requires the out-of-line copy
18429 to have the same parent. For local class methods, this doesn't
18430 apply; we just use the old DIE. */
18431 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18432 && (DECL_ARTIFICIAL (decl)
18433 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18434 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18435 == (unsigned) s.line))))
18437 subr_die = old_die;
18439 /* Clear out the declaration attribute and the formal parameters.
18440 Do not remove all children, because it is possible that this
18441 declaration die was forced using force_decl_die(). In such
18442 cases die that forced declaration die (e.g. TAG_imported_module)
18443 is one of the children that we do not want to remove. */
18444 remove_AT (subr_die, DW_AT_declaration);
18445 remove_AT (subr_die, DW_AT_object_pointer);
18446 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18448 else
18450 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18451 add_AT_specification (subr_die, old_die);
18452 add_pubname (decl, subr_die);
18453 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18454 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18455 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18456 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18458 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18459 emit the real type on the definition die. */
18460 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18462 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18463 if (die == auto_die || die == decltype_auto_die)
18464 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18465 TYPE_UNQUALIFIED, context_die);
18469 else
18471 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18473 if (TREE_PUBLIC (decl))
18474 add_AT_flag (subr_die, DW_AT_external, 1);
18476 add_name_and_src_coords_attributes (subr_die, decl);
18477 add_pubname (decl, subr_die);
18478 if (debug_info_level > DINFO_LEVEL_TERSE)
18480 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18481 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18482 TYPE_UNQUALIFIED, context_die);
18485 add_pure_or_virtual_attribute (subr_die, decl);
18486 if (DECL_ARTIFICIAL (decl))
18487 add_AT_flag (subr_die, DW_AT_artificial, 1);
18489 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18490 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18492 add_accessibility_attribute (subr_die, decl);
18495 if (declaration)
18497 if (!old_die || !get_AT (old_die, DW_AT_inline))
18499 add_AT_flag (subr_die, DW_AT_declaration, 1);
18501 /* If this is an explicit function declaration then generate
18502 a DW_AT_explicit attribute. */
18503 if (lang_hooks.decls.function_decl_explicit_p (decl)
18504 && (dwarf_version >= 3 || !dwarf_strict))
18505 add_AT_flag (subr_die, DW_AT_explicit, 1);
18507 /* If this is a C++11 deleted special function member then generate
18508 a DW_AT_GNU_deleted attribute. */
18509 if (lang_hooks.decls.function_decl_deleted_p (decl)
18510 && (! dwarf_strict))
18511 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18513 /* The first time we see a member function, it is in the context of
18514 the class to which it belongs. We make sure of this by emitting
18515 the class first. The next time is the definition, which is
18516 handled above. The two may come from the same source text.
18518 Note that force_decl_die() forces function declaration die. It is
18519 later reused to represent definition. */
18520 equate_decl_number_to_die (decl, subr_die);
18523 else if (DECL_ABSTRACT_P (decl))
18525 if (DECL_DECLARED_INLINE_P (decl))
18527 if (cgraph_function_possibly_inlined_p (decl))
18528 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18529 else
18530 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18532 else
18534 if (cgraph_function_possibly_inlined_p (decl))
18535 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18536 else
18537 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18540 if (DECL_DECLARED_INLINE_P (decl)
18541 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18542 add_AT_flag (subr_die, DW_AT_artificial, 1);
18544 equate_decl_number_to_die (decl, subr_die);
18546 else if (!DECL_EXTERNAL (decl))
18548 HOST_WIDE_INT cfa_fb_offset;
18549 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18551 if (!old_die || !get_AT (old_die, DW_AT_inline))
18552 equate_decl_number_to_die (decl, subr_die);
18554 gcc_checking_assert (fun);
18555 if (!flag_reorder_blocks_and_partition)
18557 dw_fde_ref fde = fun->fde;
18558 if (fde->dw_fde_begin)
18560 /* We have already generated the labels. */
18561 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18562 fde->dw_fde_end, false);
18564 else
18566 /* Create start/end labels and add the range. */
18567 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18568 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18569 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18570 current_function_funcdef_no);
18571 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18572 current_function_funcdef_no);
18573 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18574 false);
18577 #if VMS_DEBUGGING_INFO
18578 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18579 Section 2.3 Prologue and Epilogue Attributes:
18580 When a breakpoint is set on entry to a function, it is generally
18581 desirable for execution to be suspended, not on the very first
18582 instruction of the function, but rather at a point after the
18583 function's frame has been set up, after any language defined local
18584 declaration processing has been completed, and before execution of
18585 the first statement of the function begins. Debuggers generally
18586 cannot properly determine where this point is. Similarly for a
18587 breakpoint set on exit from a function. The prologue and epilogue
18588 attributes allow a compiler to communicate the location(s) to use. */
18591 if (fde->dw_fde_vms_end_prologue)
18592 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18593 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18595 if (fde->dw_fde_vms_begin_epilogue)
18596 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18597 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18599 #endif
18602 else
18604 /* Generate pubnames entries for the split function code ranges. */
18605 dw_fde_ref fde = fun->fde;
18607 if (fde->dw_fde_second_begin)
18609 if (dwarf_version >= 3 || !dwarf_strict)
18611 /* We should use ranges for non-contiguous code section
18612 addresses. Use the actual code range for the initial
18613 section, since the HOT/COLD labels might precede an
18614 alignment offset. */
18615 bool range_list_added = false;
18616 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18617 fde->dw_fde_end, &range_list_added,
18618 false);
18619 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18620 fde->dw_fde_second_end,
18621 &range_list_added, false);
18622 if (range_list_added)
18623 add_ranges (NULL);
18625 else
18627 /* There is no real support in DW2 for this .. so we make
18628 a work-around. First, emit the pub name for the segment
18629 containing the function label. Then make and emit a
18630 simplified subprogram DIE for the second segment with the
18631 name pre-fixed by __hot/cold_sect_of_. We use the same
18632 linkage name for the second die so that gdb will find both
18633 sections when given "b foo". */
18634 const char *name = NULL;
18635 tree decl_name = DECL_NAME (decl);
18636 dw_die_ref seg_die;
18638 /* Do the 'primary' section. */
18639 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18640 fde->dw_fde_end, false);
18642 /* Build a minimal DIE for the secondary section. */
18643 seg_die = new_die (DW_TAG_subprogram,
18644 subr_die->die_parent, decl);
18646 if (TREE_PUBLIC (decl))
18647 add_AT_flag (seg_die, DW_AT_external, 1);
18649 if (decl_name != NULL
18650 && IDENTIFIER_POINTER (decl_name) != NULL)
18652 name = dwarf2_name (decl, 1);
18653 if (! DECL_ARTIFICIAL (decl))
18654 add_src_coords_attributes (seg_die, decl);
18656 add_linkage_name (seg_die, decl);
18658 gcc_assert (name != NULL);
18659 add_pure_or_virtual_attribute (seg_die, decl);
18660 if (DECL_ARTIFICIAL (decl))
18661 add_AT_flag (seg_die, DW_AT_artificial, 1);
18663 name = concat ("__second_sect_of_", name, NULL);
18664 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18665 fde->dw_fde_second_end, false);
18666 add_name_attribute (seg_die, name);
18667 if (want_pubnames ())
18668 add_pubname_string (name, seg_die);
18671 else
18672 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18673 false);
18676 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18678 /* We define the "frame base" as the function's CFA. This is more
18679 convenient for several reasons: (1) It's stable across the prologue
18680 and epilogue, which makes it better than just a frame pointer,
18681 (2) With dwarf3, there exists a one-byte encoding that allows us
18682 to reference the .debug_frame data by proxy, but failing that,
18683 (3) We can at least reuse the code inspection and interpretation
18684 code that determines the CFA position at various points in the
18685 function. */
18686 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18688 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18689 add_AT_loc (subr_die, DW_AT_frame_base, op);
18691 else
18693 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18694 if (list->dw_loc_next)
18695 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18696 else
18697 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18700 /* Compute a displacement from the "steady-state frame pointer" to
18701 the CFA. The former is what all stack slots and argument slots
18702 will reference in the rtl; the latter is what we've told the
18703 debugger about. We'll need to adjust all frame_base references
18704 by this displacement. */
18705 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18707 if (fun->static_chain_decl)
18708 add_AT_location_description (subr_die, DW_AT_static_link,
18709 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
18712 /* Generate child dies for template paramaters. */
18713 if (debug_info_level > DINFO_LEVEL_TERSE)
18714 gen_generic_params_dies (decl);
18716 /* Now output descriptions of the arguments for this function. This gets
18717 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18718 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18719 `...' at the end of the formal parameter list. In order to find out if
18720 there was a trailing ellipsis or not, we must instead look at the type
18721 associated with the FUNCTION_DECL. This will be a node of type
18722 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18723 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18724 an ellipsis at the end. */
18726 /* In the case where we are describing a mere function declaration, all we
18727 need to do here (and all we *can* do here) is to describe the *types* of
18728 its formal parameters. */
18729 if (debug_info_level <= DINFO_LEVEL_TERSE)
18731 else if (declaration)
18732 gen_formal_types_die (decl, subr_die);
18733 else
18735 /* Generate DIEs to represent all known formal parameters. */
18736 tree parm = DECL_ARGUMENTS (decl);
18737 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18738 tree generic_decl_parm = generic_decl
18739 ? DECL_ARGUMENTS (generic_decl)
18740 : NULL;
18742 /* Now we want to walk the list of parameters of the function and
18743 emit their relevant DIEs.
18745 We consider the case of DECL being an instance of a generic function
18746 as well as it being a normal function.
18748 If DECL is an instance of a generic function we walk the
18749 parameters of the generic function declaration _and_ the parameters of
18750 DECL itself. This is useful because we want to emit specific DIEs for
18751 function parameter packs and those are declared as part of the
18752 generic function declaration. In that particular case,
18753 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18754 That DIE has children DIEs representing the set of arguments
18755 of the pack. Note that the set of pack arguments can be empty.
18756 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18757 children DIE.
18759 Otherwise, we just consider the parameters of DECL. */
18760 while (generic_decl_parm || parm)
18762 if (generic_decl_parm
18763 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18764 gen_formal_parameter_pack_die (generic_decl_parm,
18765 parm, subr_die,
18766 &parm);
18767 else if (parm && !POINTER_BOUNDS_P (parm))
18769 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18771 if (parm == DECL_ARGUMENTS (decl)
18772 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18773 && parm_die
18774 && (dwarf_version >= 3 || !dwarf_strict))
18775 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18777 parm = DECL_CHAIN (parm);
18779 else if (parm)
18780 parm = DECL_CHAIN (parm);
18782 if (generic_decl_parm)
18783 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18786 /* Decide whether we need an unspecified_parameters DIE at the end.
18787 There are 2 more cases to do this for: 1) the ansi ... declaration -
18788 this is detectable when the end of the arg list is not a
18789 void_type_node 2) an unprototyped function declaration (not a
18790 definition). This just means that we have no info about the
18791 parameters at all. */
18792 if (prototype_p (TREE_TYPE (decl)))
18794 /* This is the prototyped case, check for.... */
18795 if (stdarg_p (TREE_TYPE (decl)))
18796 gen_unspecified_parameters_die (decl, subr_die);
18798 else if (DECL_INITIAL (decl) == NULL_TREE)
18799 gen_unspecified_parameters_die (decl, subr_die);
18802 /* Output Dwarf info for all of the stuff within the body of the function
18803 (if it has one - it may be just a declaration). */
18804 outer_scope = DECL_INITIAL (decl);
18806 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18807 a function. This BLOCK actually represents the outermost binding contour
18808 for the function, i.e. the contour in which the function's formal
18809 parameters and labels get declared. Curiously, it appears that the front
18810 end doesn't actually put the PARM_DECL nodes for the current function onto
18811 the BLOCK_VARS list for this outer scope, but are strung off of the
18812 DECL_ARGUMENTS list for the function instead.
18814 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18815 the LABEL_DECL nodes for the function however, and we output DWARF info
18816 for those in decls_for_scope. Just within the `outer_scope' there will be
18817 a BLOCK node representing the function's outermost pair of curly braces,
18818 and any blocks used for the base and member initializers of a C++
18819 constructor function. */
18820 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18822 int call_site_note_count = 0;
18823 int tail_call_site_note_count = 0;
18825 /* Emit a DW_TAG_variable DIE for a named return value. */
18826 if (DECL_NAME (DECL_RESULT (decl)))
18827 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18829 decls_for_scope (outer_scope, subr_die);
18831 if (call_arg_locations && !dwarf_strict)
18833 struct call_arg_loc_node *ca_loc;
18834 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18836 dw_die_ref die = NULL;
18837 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18838 rtx arg, next_arg;
18840 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18841 arg; arg = next_arg)
18843 dw_loc_descr_ref reg, val;
18844 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18845 dw_die_ref cdie, tdie = NULL;
18847 next_arg = XEXP (arg, 1);
18848 if (REG_P (XEXP (XEXP (arg, 0), 0))
18849 && next_arg
18850 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18851 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18852 && REGNO (XEXP (XEXP (arg, 0), 0))
18853 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18854 next_arg = XEXP (next_arg, 1);
18855 if (mode == VOIDmode)
18857 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18858 if (mode == VOIDmode)
18859 mode = GET_MODE (XEXP (arg, 0));
18861 if (mode == VOIDmode || mode == BLKmode)
18862 continue;
18863 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18865 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18866 tloc = XEXP (XEXP (arg, 0), 1);
18867 continue;
18869 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18870 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18872 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18873 tlocc = XEXP (XEXP (arg, 0), 1);
18874 continue;
18876 reg = NULL;
18877 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18878 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18879 VAR_INIT_STATUS_INITIALIZED);
18880 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18882 rtx mem = XEXP (XEXP (arg, 0), 0);
18883 reg = mem_loc_descriptor (XEXP (mem, 0),
18884 get_address_mode (mem),
18885 GET_MODE (mem),
18886 VAR_INIT_STATUS_INITIALIZED);
18888 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18889 == DEBUG_PARAMETER_REF)
18891 tree tdecl
18892 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18893 tdie = lookup_decl_die (tdecl);
18894 if (tdie == NULL)
18895 continue;
18897 else
18898 continue;
18899 if (reg == NULL
18900 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18901 != DEBUG_PARAMETER_REF)
18902 continue;
18903 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18904 VOIDmode,
18905 VAR_INIT_STATUS_INITIALIZED);
18906 if (val == NULL)
18907 continue;
18908 if (die == NULL)
18909 die = gen_call_site_die (decl, subr_die, ca_loc);
18910 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18911 NULL_TREE);
18912 if (reg != NULL)
18913 add_AT_loc (cdie, DW_AT_location, reg);
18914 else if (tdie != NULL)
18915 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18916 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18917 if (next_arg != XEXP (arg, 1))
18919 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18920 if (mode == VOIDmode)
18921 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18922 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18923 0), 1),
18924 mode, VOIDmode,
18925 VAR_INIT_STATUS_INITIALIZED);
18926 if (val != NULL)
18927 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18930 if (die == NULL
18931 && (ca_loc->symbol_ref || tloc))
18932 die = gen_call_site_die (decl, subr_die, ca_loc);
18933 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18935 dw_loc_descr_ref tval = NULL;
18937 if (tloc != NULL_RTX)
18938 tval = mem_loc_descriptor (tloc,
18939 GET_MODE (tloc) == VOIDmode
18940 ? Pmode : GET_MODE (tloc),
18941 VOIDmode,
18942 VAR_INIT_STATUS_INITIALIZED);
18943 if (tval)
18944 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18945 else if (tlocc != NULL_RTX)
18947 tval = mem_loc_descriptor (tlocc,
18948 GET_MODE (tlocc) == VOIDmode
18949 ? Pmode : GET_MODE (tlocc),
18950 VOIDmode,
18951 VAR_INIT_STATUS_INITIALIZED);
18952 if (tval)
18953 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18954 tval);
18957 if (die != NULL)
18959 call_site_note_count++;
18960 if (ca_loc->tail_call_p)
18961 tail_call_site_note_count++;
18965 call_arg_locations = NULL;
18966 call_arg_loc_last = NULL;
18967 if (tail_call_site_count >= 0
18968 && tail_call_site_count == tail_call_site_note_count
18969 && !dwarf_strict)
18971 if (call_site_count >= 0
18972 && call_site_count == call_site_note_count)
18973 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18974 else
18975 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18977 call_site_count = -1;
18978 tail_call_site_count = -1;
18981 if (subr_die != old_die)
18982 /* Add the calling convention attribute if requested. */
18983 add_calling_convention_attribute (subr_die, decl);
18986 /* Returns a hash value for X (which really is a die_struct). */
18988 hashval_t
18989 block_die_hasher::hash (die_struct *d)
18991 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18994 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18995 as decl_id and die_parent of die_struct Y. */
18997 bool
18998 block_die_hasher::equal (die_struct *x, die_struct *y)
19000 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19003 /* Generate a DIE to represent a declared data object.
19004 Either DECL or ORIGIN must be non-null. */
19006 static void
19007 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19009 HOST_WIDE_INT off = 0;
19010 tree com_decl;
19011 tree decl_or_origin = decl ? decl : origin;
19012 tree ultimate_origin;
19013 dw_die_ref var_die;
19014 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19015 dw_die_ref origin_die;
19016 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19017 || class_or_namespace_scope_p (context_die));
19018 bool specialization_p = false;
19020 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19021 if (decl || ultimate_origin)
19022 origin = ultimate_origin;
19023 com_decl = fortran_common (decl_or_origin, &off);
19025 /* Symbol in common gets emitted as a child of the common block, in the form
19026 of a data member. */
19027 if (com_decl)
19029 dw_die_ref com_die;
19030 dw_loc_list_ref loc;
19031 die_node com_die_arg;
19033 var_die = lookup_decl_die (decl_or_origin);
19034 if (var_die)
19036 if (get_AT (var_die, DW_AT_location) == NULL)
19038 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19039 if (loc)
19041 if (off)
19043 /* Optimize the common case. */
19044 if (single_element_loc_list_p (loc)
19045 && loc->expr->dw_loc_opc == DW_OP_addr
19046 && loc->expr->dw_loc_next == NULL
19047 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19048 == SYMBOL_REF)
19050 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19051 loc->expr->dw_loc_oprnd1.v.val_addr
19052 = plus_constant (GET_MODE (x), x , off);
19054 else
19055 loc_list_plus_const (loc, off);
19057 add_AT_location_description (var_die, DW_AT_location, loc);
19058 remove_AT (var_die, DW_AT_declaration);
19061 return;
19064 if (common_block_die_table == NULL)
19065 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19067 com_die_arg.decl_id = DECL_UID (com_decl);
19068 com_die_arg.die_parent = context_die;
19069 com_die = common_block_die_table->find (&com_die_arg);
19070 loc = loc_list_from_tree (com_decl, 2, NULL);
19071 if (com_die == NULL)
19073 const char *cnam
19074 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19075 die_node **slot;
19077 com_die = new_die (DW_TAG_common_block, context_die, decl);
19078 add_name_and_src_coords_attributes (com_die, com_decl);
19079 if (loc)
19081 add_AT_location_description (com_die, DW_AT_location, loc);
19082 /* Avoid sharing the same loc descriptor between
19083 DW_TAG_common_block and DW_TAG_variable. */
19084 loc = loc_list_from_tree (com_decl, 2, NULL);
19086 else if (DECL_EXTERNAL (decl))
19087 add_AT_flag (com_die, DW_AT_declaration, 1);
19088 if (want_pubnames ())
19089 add_pubname_string (cnam, com_die); /* ??? needed? */
19090 com_die->decl_id = DECL_UID (com_decl);
19091 slot = common_block_die_table->find_slot (com_die, INSERT);
19092 *slot = com_die;
19094 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19096 add_AT_location_description (com_die, DW_AT_location, loc);
19097 loc = loc_list_from_tree (com_decl, 2, NULL);
19098 remove_AT (com_die, DW_AT_declaration);
19100 var_die = new_die (DW_TAG_variable, com_die, decl);
19101 add_name_and_src_coords_attributes (var_die, decl);
19102 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19103 context_die);
19104 add_AT_flag (var_die, DW_AT_external, 1);
19105 if (loc)
19107 if (off)
19109 /* Optimize the common case. */
19110 if (single_element_loc_list_p (loc)
19111 && loc->expr->dw_loc_opc == DW_OP_addr
19112 && loc->expr->dw_loc_next == NULL
19113 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19115 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19116 loc->expr->dw_loc_oprnd1.v.val_addr
19117 = plus_constant (GET_MODE (x), x, off);
19119 else
19120 loc_list_plus_const (loc, off);
19122 add_AT_location_description (var_die, DW_AT_location, loc);
19124 else if (DECL_EXTERNAL (decl))
19125 add_AT_flag (var_die, DW_AT_declaration, 1);
19126 equate_decl_number_to_die (decl, var_die);
19127 return;
19130 /* If the compiler emitted a definition for the DECL declaration
19131 and if we already emitted a DIE for it, don't emit a second
19132 DIE for it again. Allow re-declarations of DECLs that are
19133 inside functions, though. */
19134 if (old_die && declaration && !local_scope_p (context_die))
19135 return;
19137 /* For static data members, the declaration in the class is supposed
19138 to have DW_TAG_member tag; the specification should still be
19139 DW_TAG_variable referencing the DW_TAG_member DIE. */
19140 if (declaration && class_scope_p (context_die))
19141 var_die = new_die (DW_TAG_member, context_die, decl);
19142 else
19143 var_die = new_die (DW_TAG_variable, context_die, decl);
19145 origin_die = NULL;
19146 if (origin != NULL)
19147 origin_die = add_abstract_origin_attribute (var_die, origin);
19149 /* Loop unrolling can create multiple blocks that refer to the same
19150 static variable, so we must test for the DW_AT_declaration flag.
19152 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19153 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19154 sharing them.
19156 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19158 ??? The declare_in_namespace support causes us to get two DIEs for one
19159 variable, both of which are declarations. We want to avoid considering
19160 one to be a specification, so we must test that this DIE is not a
19161 declaration. */
19162 else if (old_die && TREE_STATIC (decl) && ! declaration
19163 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19165 /* This is a definition of a C++ class level static. */
19166 add_AT_specification (var_die, old_die);
19167 specialization_p = true;
19168 if (DECL_NAME (decl))
19170 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19171 struct dwarf_file_data * file_index = lookup_filename (s.file);
19173 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19174 add_AT_file (var_die, DW_AT_decl_file, file_index);
19176 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19177 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19179 if (old_die->die_tag == DW_TAG_member)
19180 add_linkage_name (var_die, decl);
19183 else
19184 add_name_and_src_coords_attributes (var_die, decl);
19186 if ((origin == NULL && !specialization_p)
19187 || (origin != NULL
19188 && !DECL_ABSTRACT_P (decl_or_origin)
19189 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19190 decl_function_context
19191 (decl_or_origin))))
19193 tree type = TREE_TYPE (decl_or_origin);
19195 if (decl_by_reference_p (decl_or_origin))
19196 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19197 context_die);
19198 else
19199 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19200 context_die);
19203 if (origin == NULL && !specialization_p)
19205 if (TREE_PUBLIC (decl))
19206 add_AT_flag (var_die, DW_AT_external, 1);
19208 if (DECL_ARTIFICIAL (decl))
19209 add_AT_flag (var_die, DW_AT_artificial, 1);
19211 add_accessibility_attribute (var_die, decl);
19214 if (declaration)
19215 add_AT_flag (var_die, DW_AT_declaration, 1);
19217 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19218 equate_decl_number_to_die (decl, var_die);
19220 if (! declaration
19221 && (! DECL_ABSTRACT_P (decl_or_origin)
19222 /* Local static vars are shared between all clones/inlines,
19223 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19224 already set. */
19225 || (TREE_CODE (decl_or_origin) == VAR_DECL
19226 && TREE_STATIC (decl_or_origin)
19227 && DECL_RTL_SET_P (decl_or_origin)))
19228 /* When abstract origin already has DW_AT_location attribute, no need
19229 to add it again. */
19230 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19232 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19233 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19234 defer_location (decl_or_origin, var_die);
19235 else
19236 add_location_or_const_value_attribute (var_die, decl_or_origin,
19237 decl == NULL, DW_AT_location);
19238 add_pubname (decl_or_origin, var_die);
19240 else
19241 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19244 /* Generate a DIE to represent a named constant. */
19246 static void
19247 gen_const_die (tree decl, dw_die_ref context_die)
19249 dw_die_ref const_die;
19250 tree type = TREE_TYPE (decl);
19252 const_die = new_die (DW_TAG_constant, context_die, decl);
19253 add_name_and_src_coords_attributes (const_die, decl);
19254 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19255 if (TREE_PUBLIC (decl))
19256 add_AT_flag (const_die, DW_AT_external, 1);
19257 if (DECL_ARTIFICIAL (decl))
19258 add_AT_flag (const_die, DW_AT_artificial, 1);
19259 tree_add_const_value_attribute_for_decl (const_die, decl);
19262 /* Generate a DIE to represent a label identifier. */
19264 static void
19265 gen_label_die (tree decl, dw_die_ref context_die)
19267 tree origin = decl_ultimate_origin (decl);
19268 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19269 rtx insn;
19270 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19272 if (origin != NULL)
19273 add_abstract_origin_attribute (lbl_die, origin);
19274 else
19275 add_name_and_src_coords_attributes (lbl_die, decl);
19277 if (DECL_ABSTRACT_P (decl))
19278 equate_decl_number_to_die (decl, lbl_die);
19279 else
19281 insn = DECL_RTL_IF_SET (decl);
19283 /* Deleted labels are programmer specified labels which have been
19284 eliminated because of various optimizations. We still emit them
19285 here so that it is possible to put breakpoints on them. */
19286 if (insn
19287 && (LABEL_P (insn)
19288 || ((NOTE_P (insn)
19289 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19291 /* When optimization is enabled (via -O) some parts of the compiler
19292 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19293 represent source-level labels which were explicitly declared by
19294 the user. This really shouldn't be happening though, so catch
19295 it if it ever does happen. */
19296 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19298 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19299 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19301 else if (insn
19302 && NOTE_P (insn)
19303 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19304 && CODE_LABEL_NUMBER (insn) != -1)
19306 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19307 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19312 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19313 attributes to the DIE for a block STMT, to describe where the inlined
19314 function was called from. This is similar to add_src_coords_attributes. */
19316 static inline void
19317 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19319 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19321 if (dwarf_version >= 3 || !dwarf_strict)
19323 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19324 add_AT_unsigned (die, DW_AT_call_line, s.line);
19329 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19330 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19332 static inline void
19333 add_high_low_attributes (tree stmt, dw_die_ref die)
19335 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19337 if (BLOCK_FRAGMENT_CHAIN (stmt)
19338 && (dwarf_version >= 3 || !dwarf_strict))
19340 tree chain, superblock = NULL_TREE;
19341 dw_die_ref pdie;
19342 dw_attr_ref attr = NULL;
19344 if (inlined_function_outer_scope_p (stmt))
19346 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19347 BLOCK_NUMBER (stmt));
19348 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19351 /* Optimize duplicate .debug_ranges lists or even tails of
19352 lists. If this BLOCK has same ranges as its supercontext,
19353 lookup DW_AT_ranges attribute in the supercontext (and
19354 recursively so), verify that the ranges_table contains the
19355 right values and use it instead of adding a new .debug_range. */
19356 for (chain = stmt, pdie = die;
19357 BLOCK_SAME_RANGE (chain);
19358 chain = BLOCK_SUPERCONTEXT (chain))
19360 dw_attr_ref new_attr;
19362 pdie = pdie->die_parent;
19363 if (pdie == NULL)
19364 break;
19365 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19366 break;
19367 new_attr = get_AT (pdie, DW_AT_ranges);
19368 if (new_attr == NULL
19369 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19370 break;
19371 attr = new_attr;
19372 superblock = BLOCK_SUPERCONTEXT (chain);
19374 if (attr != NULL
19375 && (ranges_table[attr->dw_attr_val.v.val_offset
19376 / 2 / DWARF2_ADDR_SIZE].num
19377 == BLOCK_NUMBER (superblock))
19378 && BLOCK_FRAGMENT_CHAIN (superblock))
19380 unsigned long off = attr->dw_attr_val.v.val_offset
19381 / 2 / DWARF2_ADDR_SIZE;
19382 unsigned long supercnt = 0, thiscnt = 0;
19383 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19384 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19386 ++supercnt;
19387 gcc_checking_assert (ranges_table[off + supercnt].num
19388 == BLOCK_NUMBER (chain));
19390 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19391 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19392 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19393 ++thiscnt;
19394 gcc_assert (supercnt >= thiscnt);
19395 add_AT_range_list (die, DW_AT_ranges,
19396 ((off + supercnt - thiscnt)
19397 * 2 * DWARF2_ADDR_SIZE),
19398 false);
19399 return;
19402 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19404 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19407 add_ranges (chain);
19408 chain = BLOCK_FRAGMENT_CHAIN (chain);
19410 while (chain);
19411 add_ranges (NULL);
19413 else
19415 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19416 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19417 BLOCK_NUMBER (stmt));
19418 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19419 BLOCK_NUMBER (stmt));
19420 add_AT_low_high_pc (die, label, label_high, false);
19424 /* Generate a DIE for a lexical block. */
19426 static void
19427 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19429 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19431 if (call_arg_locations)
19433 if (block_map.length () <= BLOCK_NUMBER (stmt))
19434 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19435 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19438 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19439 add_high_low_attributes (stmt, stmt_die);
19441 decls_for_scope (stmt, stmt_die);
19444 /* Generate a DIE for an inlined subprogram. */
19446 static void
19447 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19449 tree decl;
19451 /* The instance of function that is effectively being inlined shall not
19452 be abstract. */
19453 gcc_assert (! BLOCK_ABSTRACT (stmt));
19455 decl = block_ultimate_origin (stmt);
19457 /* Emit info for the abstract instance first, if we haven't yet. We
19458 must emit this even if the block is abstract, otherwise when we
19459 emit the block below (or elsewhere), we may end up trying to emit
19460 a die whose origin die hasn't been emitted, and crashing. */
19461 dwarf2out_abstract_function (decl);
19463 if (! BLOCK_ABSTRACT (stmt))
19465 dw_die_ref subr_die
19466 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19468 if (call_arg_locations)
19470 if (block_map.length () <= BLOCK_NUMBER (stmt))
19471 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19472 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19474 add_abstract_origin_attribute (subr_die, decl);
19475 if (TREE_ASM_WRITTEN (stmt))
19476 add_high_low_attributes (stmt, subr_die);
19477 add_call_src_coords_attributes (stmt, subr_die);
19479 decls_for_scope (stmt, subr_die);
19483 /* Generate a DIE for a field in a record, or structure. */
19485 static void
19486 gen_field_die (tree decl, dw_die_ref context_die)
19488 dw_die_ref decl_die;
19490 if (TREE_TYPE (decl) == error_mark_node)
19491 return;
19493 decl_die = new_die (DW_TAG_member, context_die, decl);
19494 add_name_and_src_coords_attributes (decl_die, decl);
19495 add_type_attribute (decl_die, member_declared_type (decl),
19496 decl_quals (decl), context_die);
19498 if (DECL_BIT_FIELD_TYPE (decl))
19500 add_byte_size_attribute (decl_die, decl);
19501 add_bit_size_attribute (decl_die, decl);
19502 add_bit_offset_attribute (decl_die, decl);
19505 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19506 add_data_member_location_attribute (decl_die, decl);
19508 if (DECL_ARTIFICIAL (decl))
19509 add_AT_flag (decl_die, DW_AT_artificial, 1);
19511 add_accessibility_attribute (decl_die, decl);
19513 /* Equate decl number to die, so that we can look up this decl later on. */
19514 equate_decl_number_to_die (decl, decl_die);
19517 #if 0
19518 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19519 Use modified_type_die instead.
19520 We keep this code here just in case these types of DIEs may be needed to
19521 represent certain things in other languages (e.g. Pascal) someday. */
19523 static void
19524 gen_pointer_type_die (tree type, dw_die_ref context_die)
19526 dw_die_ref ptr_die
19527 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19529 equate_type_number_to_die (type, ptr_die);
19530 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19531 context_die);
19532 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19535 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19536 Use modified_type_die instead.
19537 We keep this code here just in case these types of DIEs may be needed to
19538 represent certain things in other languages (e.g. Pascal) someday. */
19540 static void
19541 gen_reference_type_die (tree type, dw_die_ref context_die)
19543 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19545 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19546 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19547 else
19548 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19550 equate_type_number_to_die (type, ref_die);
19551 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19552 context_die);
19553 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19555 #endif
19557 /* Generate a DIE for a pointer to a member type. */
19559 static void
19560 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19562 dw_die_ref ptr_die
19563 = new_die (DW_TAG_ptr_to_member_type,
19564 scope_die_for (type, context_die), type);
19566 equate_type_number_to_die (type, ptr_die);
19567 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19568 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19569 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19570 context_die);
19573 typedef const char *dchar_p; /* For DEF_VEC_P. */
19575 static char *producer_string;
19577 /* Return a heap allocated producer string including command line options
19578 if -grecord-gcc-switches. */
19580 static char *
19581 gen_producer_string (void)
19583 size_t j;
19584 auto_vec<dchar_p> switches;
19585 const char *language_string = lang_hooks.name;
19586 char *producer, *tail;
19587 const char *p;
19588 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19589 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19591 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19592 switch (save_decoded_options[j].opt_index)
19594 case OPT_o:
19595 case OPT_d:
19596 case OPT_dumpbase:
19597 case OPT_dumpdir:
19598 case OPT_auxbase:
19599 case OPT_auxbase_strip:
19600 case OPT_quiet:
19601 case OPT_version:
19602 case OPT_v:
19603 case OPT_w:
19604 case OPT_L:
19605 case OPT_D:
19606 case OPT_I:
19607 case OPT_U:
19608 case OPT_SPECIAL_unknown:
19609 case OPT_SPECIAL_ignore:
19610 case OPT_SPECIAL_program_name:
19611 case OPT_SPECIAL_input_file:
19612 case OPT_grecord_gcc_switches:
19613 case OPT_gno_record_gcc_switches:
19614 case OPT__output_pch_:
19615 case OPT_fdiagnostics_show_location_:
19616 case OPT_fdiagnostics_show_option:
19617 case OPT_fdiagnostics_show_caret:
19618 case OPT_fdiagnostics_color_:
19619 case OPT_fverbose_asm:
19620 case OPT____:
19621 case OPT__sysroot_:
19622 case OPT_nostdinc:
19623 case OPT_nostdinc__:
19624 /* Ignore these. */
19625 continue;
19626 default:
19627 if (cl_options[save_decoded_options[j].opt_index].flags
19628 & CL_NO_DWARF_RECORD)
19629 continue;
19630 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19631 == '-');
19632 switch (save_decoded_options[j].canonical_option[0][1])
19634 case 'M':
19635 case 'i':
19636 case 'W':
19637 continue;
19638 case 'f':
19639 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19640 "dump", 4) == 0)
19641 continue;
19642 break;
19643 default:
19644 break;
19646 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19647 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19648 break;
19651 producer = XNEWVEC (char, plen + 1 + len + 1);
19652 tail = producer;
19653 sprintf (tail, "%s %s", language_string, version_string);
19654 tail += plen;
19656 FOR_EACH_VEC_ELT (switches, j, p)
19658 len = strlen (p);
19659 *tail = ' ';
19660 memcpy (tail + 1, p, len);
19661 tail += len + 1;
19664 *tail = '\0';
19665 return producer;
19668 /* Given a C and/or C++ language/version string return the "highest".
19669 C++ is assumed to be "higher" than C in this case. Used for merging
19670 LTO translation unit languages. */
19671 static const char *
19672 highest_c_language (const char *lang1, const char *lang2)
19674 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19675 return "GNU C++14";
19676 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19677 return "GNU C++11";
19678 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19679 return "GNU C++98";
19681 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19682 return "GNU C11";
19683 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19684 return "GNU C99";
19685 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19686 return "GNU C89";
19688 gcc_unreachable ();
19692 /* Generate the DIE for the compilation unit. */
19694 static dw_die_ref
19695 gen_compile_unit_die (const char *filename)
19697 dw_die_ref die;
19698 const char *language_string = lang_hooks.name;
19699 int language;
19701 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19703 if (filename)
19705 add_name_attribute (die, filename);
19706 /* Don't add cwd for <built-in>. */
19707 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19708 add_comp_dir_attribute (die);
19711 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19713 /* If our producer is LTO try to figure out a common language to use
19714 from the global list of translation units. */
19715 if (strcmp (language_string, "GNU GIMPLE") == 0)
19717 unsigned i;
19718 tree t;
19719 const char *common_lang = NULL;
19721 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19723 if (!TRANSLATION_UNIT_LANGUAGE (t))
19724 continue;
19725 if (!common_lang)
19726 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19727 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19729 else if (strncmp (common_lang, "GNU C", 5) == 0
19730 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19731 /* Mixing C and C++ is ok, use C++ in that case. */
19732 common_lang = highest_c_language (common_lang,
19733 TRANSLATION_UNIT_LANGUAGE (t));
19734 else
19736 /* Fall back to C. */
19737 common_lang = NULL;
19738 break;
19742 if (common_lang)
19743 language_string = common_lang;
19746 language = DW_LANG_C;
19747 if (strncmp (language_string, "GNU C", 5) == 0
19748 && ISDIGIT (language_string[5]))
19750 language = DW_LANG_C89;
19751 if (dwarf_version >= 3 || !dwarf_strict)
19753 if (strcmp (language_string, "GNU C89") != 0)
19754 language = DW_LANG_C99;
19756 if (dwarf_version >= 5 /* || !dwarf_strict */)
19757 if (strcmp (language_string, "GNU C11") == 0)
19758 language = DW_LANG_C11;
19761 else if (strncmp (language_string, "GNU C++", 7) == 0)
19763 language = DW_LANG_C_plus_plus;
19764 if (dwarf_version >= 5 /* || !dwarf_strict */)
19766 if (strcmp (language_string, "GNU C++11") == 0)
19767 language = DW_LANG_C_plus_plus_11;
19768 else if (strcmp (language_string, "GNU C++14") == 0)
19769 language = DW_LANG_C_plus_plus_14;
19772 else if (strcmp (language_string, "GNU F77") == 0)
19773 language = DW_LANG_Fortran77;
19774 else if (strcmp (language_string, "GNU Pascal") == 0)
19775 language = DW_LANG_Pascal83;
19776 else if (dwarf_version >= 3 || !dwarf_strict)
19778 if (strcmp (language_string, "GNU Ada") == 0)
19779 language = DW_LANG_Ada95;
19780 else if (strcmp (language_string, "GNU Fortran") == 0)
19781 language = DW_LANG_Fortran95;
19782 else if (strcmp (language_string, "GNU Java") == 0)
19783 language = DW_LANG_Java;
19784 else if (strcmp (language_string, "GNU Objective-C") == 0)
19785 language = DW_LANG_ObjC;
19786 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19787 language = DW_LANG_ObjC_plus_plus;
19788 else if (dwarf_version >= 5 || !dwarf_strict)
19790 if (strcmp (language_string, "GNU Go") == 0)
19791 language = DW_LANG_Go;
19794 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19795 else if (strcmp (language_string, "GNU Fortran") == 0)
19796 language = DW_LANG_Fortran90;
19798 add_AT_unsigned (die, DW_AT_language, language);
19800 switch (language)
19802 case DW_LANG_Fortran77:
19803 case DW_LANG_Fortran90:
19804 case DW_LANG_Fortran95:
19805 /* Fortran has case insensitive identifiers and the front-end
19806 lowercases everything. */
19807 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19808 break;
19809 default:
19810 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19811 break;
19813 return die;
19816 /* Generate the DIE for a base class. */
19818 static void
19819 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19821 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19823 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19824 add_data_member_location_attribute (die, binfo);
19826 if (BINFO_VIRTUAL_P (binfo))
19827 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19829 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19830 children, otherwise the default is DW_ACCESS_public. In DWARF2
19831 the default has always been DW_ACCESS_private. */
19832 if (access == access_public_node)
19834 if (dwarf_version == 2
19835 || context_die->die_tag == DW_TAG_class_type)
19836 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19838 else if (access == access_protected_node)
19839 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19840 else if (dwarf_version > 2
19841 && context_die->die_tag != DW_TAG_class_type)
19842 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19845 /* Generate a DIE for a class member. */
19847 static void
19848 gen_member_die (tree type, dw_die_ref context_die)
19850 tree member;
19851 tree binfo = TYPE_BINFO (type);
19852 dw_die_ref child;
19854 /* If this is not an incomplete type, output descriptions of each of its
19855 members. Note that as we output the DIEs necessary to represent the
19856 members of this record or union type, we will also be trying to output
19857 DIEs to represent the *types* of those members. However the `type'
19858 function (above) will specifically avoid generating type DIEs for member
19859 types *within* the list of member DIEs for this (containing) type except
19860 for those types (of members) which are explicitly marked as also being
19861 members of this (containing) type themselves. The g++ front- end can
19862 force any given type to be treated as a member of some other (containing)
19863 type by setting the TYPE_CONTEXT of the given (member) type to point to
19864 the TREE node representing the appropriate (containing) type. */
19866 /* First output info about the base classes. */
19867 if (binfo)
19869 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19870 int i;
19871 tree base;
19873 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19874 gen_inheritance_die (base,
19875 (accesses ? (*accesses)[i] : access_public_node),
19876 context_die);
19879 /* Now output info about the data members and type members. */
19880 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19882 /* If we thought we were generating minimal debug info for TYPE
19883 and then changed our minds, some of the member declarations
19884 may have already been defined. Don't define them again, but
19885 do put them in the right order. */
19887 child = lookup_decl_die (member);
19888 if (child)
19889 splice_child_die (context_die, child);
19890 else
19891 gen_decl_die (member, NULL, context_die);
19894 /* Now output info about the function members (if any). */
19895 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19897 /* Don't include clones in the member list. */
19898 if (DECL_ABSTRACT_ORIGIN (member))
19899 continue;
19901 child = lookup_decl_die (member);
19902 if (child)
19903 splice_child_die (context_die, child);
19904 else
19905 gen_decl_die (member, NULL, context_die);
19909 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19910 is set, we pretend that the type was never defined, so we only get the
19911 member DIEs needed by later specification DIEs. */
19913 static void
19914 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19915 enum debug_info_usage usage)
19917 dw_die_ref type_die = lookup_type_die (type);
19918 dw_die_ref scope_die = 0;
19919 int nested = 0;
19920 int complete = (TYPE_SIZE (type)
19921 && (! TYPE_STUB_DECL (type)
19922 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19923 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19924 complete = complete && should_emit_struct_debug (type, usage);
19926 if (type_die && ! complete)
19927 return;
19929 if (TYPE_CONTEXT (type) != NULL_TREE
19930 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19931 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19932 nested = 1;
19934 scope_die = scope_die_for (type, context_die);
19936 /* Generate child dies for template paramaters. */
19937 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19938 schedule_generic_params_dies_gen (type);
19940 if (! type_die || (nested && is_cu_die (scope_die)))
19941 /* First occurrence of type or toplevel definition of nested class. */
19943 dw_die_ref old_die = type_die;
19945 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19946 ? record_type_tag (type) : DW_TAG_union_type,
19947 scope_die, type);
19948 equate_type_number_to_die (type, type_die);
19949 if (old_die)
19950 add_AT_specification (type_die, old_die);
19951 else
19952 add_name_attribute (type_die, type_tag (type));
19954 else
19955 remove_AT (type_die, DW_AT_declaration);
19957 /* If this type has been completed, then give it a byte_size attribute and
19958 then give a list of members. */
19959 if (complete && !ns_decl)
19961 /* Prevent infinite recursion in cases where the type of some member of
19962 this type is expressed in terms of this type itself. */
19963 TREE_ASM_WRITTEN (type) = 1;
19964 add_byte_size_attribute (type_die, type);
19965 if (TYPE_STUB_DECL (type) != NULL_TREE)
19967 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19968 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19971 /* If the first reference to this type was as the return type of an
19972 inline function, then it may not have a parent. Fix this now. */
19973 if (type_die->die_parent == NULL)
19974 add_child_die (scope_die, type_die);
19976 push_decl_scope (type);
19977 gen_member_die (type, type_die);
19978 pop_decl_scope ();
19980 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19981 if (TYPE_ARTIFICIAL (type))
19982 add_AT_flag (type_die, DW_AT_artificial, 1);
19984 /* GNU extension: Record what type our vtable lives in. */
19985 if (TYPE_VFIELD (type))
19987 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19989 gen_type_die (vtype, context_die);
19990 add_AT_die_ref (type_die, DW_AT_containing_type,
19991 lookup_type_die (vtype));
19994 else
19996 add_AT_flag (type_die, DW_AT_declaration, 1);
19998 /* We don't need to do this for function-local types. */
19999 if (TYPE_STUB_DECL (type)
20000 && ! decl_function_context (TYPE_STUB_DECL (type)))
20001 vec_safe_push (incomplete_types, type);
20004 if (get_AT (type_die, DW_AT_name))
20005 add_pubtype (type, type_die);
20008 /* Generate a DIE for a subroutine _type_. */
20010 static void
20011 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20013 tree return_type = TREE_TYPE (type);
20014 dw_die_ref subr_die
20015 = new_die (DW_TAG_subroutine_type,
20016 scope_die_for (type, context_die), type);
20018 equate_type_number_to_die (type, subr_die);
20019 add_prototyped_attribute (subr_die, type);
20020 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20021 gen_formal_types_die (type, subr_die);
20023 if (get_AT (subr_die, DW_AT_name))
20024 add_pubtype (type, subr_die);
20027 /* Generate a DIE for a type definition. */
20029 static void
20030 gen_typedef_die (tree decl, dw_die_ref context_die)
20032 dw_die_ref type_die;
20033 tree origin;
20035 if (TREE_ASM_WRITTEN (decl))
20036 return;
20038 TREE_ASM_WRITTEN (decl) = 1;
20039 type_die = new_die (DW_TAG_typedef, context_die, decl);
20040 origin = decl_ultimate_origin (decl);
20041 if (origin != NULL)
20042 add_abstract_origin_attribute (type_die, origin);
20043 else
20045 tree type;
20047 add_name_and_src_coords_attributes (type_die, decl);
20048 if (DECL_ORIGINAL_TYPE (decl))
20050 type = DECL_ORIGINAL_TYPE (decl);
20052 gcc_assert (type != TREE_TYPE (decl));
20053 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20055 else
20057 type = TREE_TYPE (decl);
20059 if (is_naming_typedef_decl (TYPE_NAME (type)))
20061 /* Here, we are in the case of decl being a typedef naming
20062 an anonymous type, e.g:
20063 typedef struct {...} foo;
20064 In that case TREE_TYPE (decl) is not a typedef variant
20065 type and TYPE_NAME of the anonymous type is set to the
20066 TYPE_DECL of the typedef. This construct is emitted by
20067 the C++ FE.
20069 TYPE is the anonymous struct named by the typedef
20070 DECL. As we need the DW_AT_type attribute of the
20071 DW_TAG_typedef to point to the DIE of TYPE, let's
20072 generate that DIE right away. add_type_attribute
20073 called below will then pick (via lookup_type_die) that
20074 anonymous struct DIE. */
20075 if (!TREE_ASM_WRITTEN (type))
20076 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20078 /* This is a GNU Extension. We are adding a
20079 DW_AT_linkage_name attribute to the DIE of the
20080 anonymous struct TYPE. The value of that attribute
20081 is the name of the typedef decl naming the anonymous
20082 struct. This greatly eases the work of consumers of
20083 this debug info. */
20084 add_linkage_attr (lookup_type_die (type), decl);
20088 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20090 if (is_naming_typedef_decl (decl))
20091 /* We want that all subsequent calls to lookup_type_die with
20092 TYPE in argument yield the DW_TAG_typedef we have just
20093 created. */
20094 equate_type_number_to_die (type, type_die);
20096 add_accessibility_attribute (type_die, decl);
20099 if (DECL_ABSTRACT_P (decl))
20100 equate_decl_number_to_die (decl, type_die);
20102 if (get_AT (type_die, DW_AT_name))
20103 add_pubtype (decl, type_die);
20106 /* Generate a DIE for a struct, class, enum or union type. */
20108 static void
20109 gen_tagged_type_die (tree type,
20110 dw_die_ref context_die,
20111 enum debug_info_usage usage)
20113 int need_pop;
20115 if (type == NULL_TREE
20116 || !is_tagged_type (type))
20117 return;
20119 /* If this is a nested type whose containing class hasn't been written
20120 out yet, writing it out will cover this one, too. This does not apply
20121 to instantiations of member class templates; they need to be added to
20122 the containing class as they are generated. FIXME: This hurts the
20123 idea of combining type decls from multiple TUs, since we can't predict
20124 what set of template instantiations we'll get. */
20125 if (TYPE_CONTEXT (type)
20126 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20127 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20129 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20131 if (TREE_ASM_WRITTEN (type))
20132 return;
20134 /* If that failed, attach ourselves to the stub. */
20135 push_decl_scope (TYPE_CONTEXT (type));
20136 context_die = lookup_type_die (TYPE_CONTEXT (type));
20137 need_pop = 1;
20139 else if (TYPE_CONTEXT (type) != NULL_TREE
20140 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20142 /* If this type is local to a function that hasn't been written
20143 out yet, use a NULL context for now; it will be fixed up in
20144 decls_for_scope. */
20145 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20146 /* A declaration DIE doesn't count; nested types need to go in the
20147 specification. */
20148 if (context_die && is_declaration_die (context_die))
20149 context_die = NULL;
20150 need_pop = 0;
20152 else
20154 context_die = declare_in_namespace (type, context_die);
20155 need_pop = 0;
20158 if (TREE_CODE (type) == ENUMERAL_TYPE)
20160 /* This might have been written out by the call to
20161 declare_in_namespace. */
20162 if (!TREE_ASM_WRITTEN (type))
20163 gen_enumeration_type_die (type, context_die);
20165 else
20166 gen_struct_or_union_type_die (type, context_die, usage);
20168 if (need_pop)
20169 pop_decl_scope ();
20171 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20172 it up if it is ever completed. gen_*_type_die will set it for us
20173 when appropriate. */
20176 /* Generate a type description DIE. */
20178 static void
20179 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20180 enum debug_info_usage usage)
20182 struct array_descr_info info;
20184 if (type == NULL_TREE || type == error_mark_node)
20185 return;
20187 if (TYPE_NAME (type) != NULL_TREE
20188 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20189 && is_redundant_typedef (TYPE_NAME (type))
20190 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20191 /* The DECL of this type is a typedef we don't want to emit debug
20192 info for but we want debug info for its underlying typedef.
20193 This can happen for e.g, the injected-class-name of a C++
20194 type. */
20195 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20197 /* If TYPE is a typedef type variant, let's generate debug info
20198 for the parent typedef which TYPE is a type of. */
20199 if (typedef_variant_p (type))
20201 if (TREE_ASM_WRITTEN (type))
20202 return;
20204 /* Prevent broken recursion; we can't hand off to the same type. */
20205 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20207 /* Give typedefs the right scope. */
20208 context_die = scope_die_for (type, context_die);
20210 TREE_ASM_WRITTEN (type) = 1;
20212 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20213 return;
20216 /* If type is an anonymous tagged type named by a typedef, let's
20217 generate debug info for the typedef. */
20218 if (is_naming_typedef_decl (TYPE_NAME (type)))
20220 /* Use the DIE of the containing namespace as the parent DIE of
20221 the type description DIE we want to generate. */
20222 if (DECL_CONTEXT (TYPE_NAME (type))
20223 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20224 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20226 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20227 return;
20230 /* We are going to output a DIE to represent the unqualified version
20231 of this type (i.e. without any const or volatile qualifiers) so
20232 get the main variant (i.e. the unqualified version) of this type
20233 now. (Vectors are special because the debugging info is in the
20234 cloned type itself). */
20235 if (TREE_CODE (type) != VECTOR_TYPE)
20236 type = type_main_variant (type);
20238 /* If this is an array type with hidden descriptor, handle it first. */
20239 if (!TREE_ASM_WRITTEN (type)
20240 && lang_hooks.types.get_array_descr_info)
20242 memset (&info, 0, sizeof (info));
20243 if (lang_hooks.types.get_array_descr_info (type, &info))
20245 gen_descr_array_type_die (type, &info, context_die);
20246 TREE_ASM_WRITTEN (type) = 1;
20247 return;
20251 if (TREE_ASM_WRITTEN (type))
20252 return;
20254 switch (TREE_CODE (type))
20256 case ERROR_MARK:
20257 break;
20259 case POINTER_TYPE:
20260 case REFERENCE_TYPE:
20261 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20262 ensures that the gen_type_die recursion will terminate even if the
20263 type is recursive. Recursive types are possible in Ada. */
20264 /* ??? We could perhaps do this for all types before the switch
20265 statement. */
20266 TREE_ASM_WRITTEN (type) = 1;
20268 /* For these types, all that is required is that we output a DIE (or a
20269 set of DIEs) to represent the "basis" type. */
20270 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20271 DINFO_USAGE_IND_USE);
20272 break;
20274 case OFFSET_TYPE:
20275 /* This code is used for C++ pointer-to-data-member types.
20276 Output a description of the relevant class type. */
20277 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20278 DINFO_USAGE_IND_USE);
20280 /* Output a description of the type of the object pointed to. */
20281 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20282 DINFO_USAGE_IND_USE);
20284 /* Now output a DIE to represent this pointer-to-data-member type
20285 itself. */
20286 gen_ptr_to_mbr_type_die (type, context_die);
20287 break;
20289 case FUNCTION_TYPE:
20290 /* Force out return type (in case it wasn't forced out already). */
20291 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20292 DINFO_USAGE_DIR_USE);
20293 gen_subroutine_type_die (type, context_die);
20294 break;
20296 case METHOD_TYPE:
20297 /* Force out return type (in case it wasn't forced out already). */
20298 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20299 DINFO_USAGE_DIR_USE);
20300 gen_subroutine_type_die (type, context_die);
20301 break;
20303 case ARRAY_TYPE:
20304 gen_array_type_die (type, context_die);
20305 break;
20307 case VECTOR_TYPE:
20308 gen_array_type_die (type, context_die);
20309 break;
20311 case ENUMERAL_TYPE:
20312 case RECORD_TYPE:
20313 case UNION_TYPE:
20314 case QUAL_UNION_TYPE:
20315 gen_tagged_type_die (type, context_die, usage);
20316 return;
20318 case VOID_TYPE:
20319 case INTEGER_TYPE:
20320 case REAL_TYPE:
20321 case FIXED_POINT_TYPE:
20322 case COMPLEX_TYPE:
20323 case BOOLEAN_TYPE:
20324 case POINTER_BOUNDS_TYPE:
20325 /* No DIEs needed for fundamental types. */
20326 break;
20328 case NULLPTR_TYPE:
20329 case LANG_TYPE:
20330 /* Just use DW_TAG_unspecified_type. */
20332 dw_die_ref type_die = lookup_type_die (type);
20333 if (type_die == NULL)
20335 tree name = TYPE_IDENTIFIER (type);
20336 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20337 type);
20338 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20339 equate_type_number_to_die (type, type_die);
20342 break;
20344 default:
20345 if (is_cxx_auto (type))
20347 tree name = TYPE_IDENTIFIER (type);
20348 dw_die_ref *die = (name == get_identifier ("auto")
20349 ? &auto_die : &decltype_auto_die);
20350 if (!*die)
20352 *die = new_die (DW_TAG_unspecified_type,
20353 comp_unit_die (), NULL_TREE);
20354 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20356 equate_type_number_to_die (type, *die);
20357 break;
20359 gcc_unreachable ();
20362 TREE_ASM_WRITTEN (type) = 1;
20365 static void
20366 gen_type_die (tree type, dw_die_ref context_die)
20368 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20371 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20372 things which are local to the given block. */
20374 static void
20375 gen_block_die (tree stmt, dw_die_ref context_die)
20377 int must_output_die = 0;
20378 bool inlined_func;
20380 /* Ignore blocks that are NULL. */
20381 if (stmt == NULL_TREE)
20382 return;
20384 inlined_func = inlined_function_outer_scope_p (stmt);
20386 /* If the block is one fragment of a non-contiguous block, do not
20387 process the variables, since they will have been done by the
20388 origin block. Do process subblocks. */
20389 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20391 tree sub;
20393 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20394 gen_block_die (sub, context_die);
20396 return;
20399 /* Determine if we need to output any Dwarf DIEs at all to represent this
20400 block. */
20401 if (inlined_func)
20402 /* The outer scopes for inlinings *must* always be represented. We
20403 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20404 must_output_die = 1;
20405 else
20407 /* Determine if this block directly contains any "significant"
20408 local declarations which we will need to output DIEs for. */
20409 if (debug_info_level > DINFO_LEVEL_TERSE)
20410 /* We are not in terse mode so *any* local declaration counts
20411 as being a "significant" one. */
20412 must_output_die = ((BLOCK_VARS (stmt) != NULL
20413 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20414 && (TREE_USED (stmt)
20415 || TREE_ASM_WRITTEN (stmt)
20416 || BLOCK_ABSTRACT (stmt)));
20417 else if ((TREE_USED (stmt)
20418 || TREE_ASM_WRITTEN (stmt)
20419 || BLOCK_ABSTRACT (stmt))
20420 && !dwarf2out_ignore_block (stmt))
20421 must_output_die = 1;
20424 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20425 DIE for any block which contains no significant local declarations at
20426 all. Rather, in such cases we just call `decls_for_scope' so that any
20427 needed Dwarf info for any sub-blocks will get properly generated. Note
20428 that in terse mode, our definition of what constitutes a "significant"
20429 local declaration gets restricted to include only inlined function
20430 instances and local (nested) function definitions. */
20431 if (must_output_die)
20433 if (inlined_func)
20435 /* If STMT block is abstract, that means we have been called
20436 indirectly from dwarf2out_abstract_function.
20437 That function rightfully marks the descendent blocks (of
20438 the abstract function it is dealing with) as being abstract,
20439 precisely to prevent us from emitting any
20440 DW_TAG_inlined_subroutine DIE as a descendent
20441 of an abstract function instance. So in that case, we should
20442 not call gen_inlined_subroutine_die.
20444 Later though, when cgraph asks dwarf2out to emit info
20445 for the concrete instance of the function decl into which
20446 the concrete instance of STMT got inlined, the later will lead
20447 to the generation of a DW_TAG_inlined_subroutine DIE. */
20448 if (! BLOCK_ABSTRACT (stmt))
20449 gen_inlined_subroutine_die (stmt, context_die);
20451 else
20452 gen_lexical_block_die (stmt, context_die);
20454 else
20455 decls_for_scope (stmt, context_die);
20458 /* Process variable DECL (or variable with origin ORIGIN) within
20459 block STMT and add it to CONTEXT_DIE. */
20460 static void
20461 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20463 dw_die_ref die;
20464 tree decl_or_origin = decl ? decl : origin;
20466 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20467 die = lookup_decl_die (decl_or_origin);
20468 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20469 && TYPE_DECL_IS_STUB (decl_or_origin))
20470 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20471 else
20472 die = NULL;
20474 if (die != NULL && die->die_parent == NULL)
20475 add_child_die (context_die, die);
20476 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20477 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20478 stmt, context_die);
20479 else
20480 gen_decl_die (decl, origin, context_die);
20483 /* Generate all of the decls declared within a given scope and (recursively)
20484 all of its sub-blocks. */
20486 static void
20487 decls_for_scope (tree stmt, dw_die_ref context_die)
20489 tree decl;
20490 unsigned int i;
20491 tree subblocks;
20493 /* Ignore NULL blocks. */
20494 if (stmt == NULL_TREE)
20495 return;
20497 /* Output the DIEs to represent all of the data objects and typedefs
20498 declared directly within this block but not within any nested
20499 sub-blocks. Also, nested function and tag DIEs have been
20500 generated with a parent of NULL; fix that up now. We don't
20501 have to do this if we're at -g1. */
20502 if (debug_info_level > DINFO_LEVEL_TERSE)
20504 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20505 process_scope_var (stmt, decl, NULL_TREE, context_die);
20506 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20507 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20508 context_die);
20511 /* Even if we're at -g1, we need to process the subblocks in order to get
20512 inlined call information. */
20514 /* Output the DIEs to represent all sub-blocks (and the items declared
20515 therein) of this block. */
20516 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20517 subblocks != NULL;
20518 subblocks = BLOCK_CHAIN (subblocks))
20519 gen_block_die (subblocks, context_die);
20522 /* Is this a typedef we can avoid emitting? */
20524 static inline int
20525 is_redundant_typedef (const_tree decl)
20527 if (TYPE_DECL_IS_STUB (decl))
20528 return 1;
20530 if (DECL_ARTIFICIAL (decl)
20531 && DECL_CONTEXT (decl)
20532 && is_tagged_type (DECL_CONTEXT (decl))
20533 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20534 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20535 /* Also ignore the artificial member typedef for the class name. */
20536 return 1;
20538 return 0;
20541 /* Return TRUE if TYPE is a typedef that names a type for linkage
20542 purposes. This kind of typedefs is produced by the C++ FE for
20543 constructs like:
20545 typedef struct {...} foo;
20547 In that case, there is no typedef variant type produced for foo.
20548 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20549 struct type. */
20551 static bool
20552 is_naming_typedef_decl (const_tree decl)
20554 if (decl == NULL_TREE
20555 || TREE_CODE (decl) != TYPE_DECL
20556 || !is_tagged_type (TREE_TYPE (decl))
20557 || DECL_IS_BUILTIN (decl)
20558 || is_redundant_typedef (decl)
20559 /* It looks like Ada produces TYPE_DECLs that are very similar
20560 to C++ naming typedefs but that have different
20561 semantics. Let's be specific to c++ for now. */
20562 || !is_cxx ())
20563 return FALSE;
20565 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20566 && TYPE_NAME (TREE_TYPE (decl)) == decl
20567 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20568 != TYPE_NAME (TREE_TYPE (decl))));
20571 /* Returns the DIE for a context. */
20573 static inline dw_die_ref
20574 get_context_die (tree context)
20576 if (context)
20578 /* Find die that represents this context. */
20579 if (TYPE_P (context))
20581 context = TYPE_MAIN_VARIANT (context);
20582 return strip_naming_typedef (context, force_type_die (context));
20584 else
20585 return force_decl_die (context);
20587 return comp_unit_die ();
20590 /* Returns the DIE for decl. A DIE will always be returned. */
20592 static dw_die_ref
20593 force_decl_die (tree decl)
20595 dw_die_ref decl_die;
20596 unsigned saved_external_flag;
20597 tree save_fn = NULL_TREE;
20598 decl_die = lookup_decl_die (decl);
20599 if (!decl_die)
20601 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20603 decl_die = lookup_decl_die (decl);
20604 if (decl_die)
20605 return decl_die;
20607 switch (TREE_CODE (decl))
20609 case FUNCTION_DECL:
20610 /* Clear current_function_decl, so that gen_subprogram_die thinks
20611 that this is a declaration. At this point, we just want to force
20612 declaration die. */
20613 save_fn = current_function_decl;
20614 current_function_decl = NULL_TREE;
20615 gen_subprogram_die (decl, context_die);
20616 current_function_decl = save_fn;
20617 break;
20619 case VAR_DECL:
20620 /* Set external flag to force declaration die. Restore it after
20621 gen_decl_die() call. */
20622 saved_external_flag = DECL_EXTERNAL (decl);
20623 DECL_EXTERNAL (decl) = 1;
20624 gen_decl_die (decl, NULL, context_die);
20625 DECL_EXTERNAL (decl) = saved_external_flag;
20626 break;
20628 case NAMESPACE_DECL:
20629 if (dwarf_version >= 3 || !dwarf_strict)
20630 dwarf2out_decl (decl);
20631 else
20632 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20633 decl_die = comp_unit_die ();
20634 break;
20636 case TRANSLATION_UNIT_DECL:
20637 decl_die = comp_unit_die ();
20638 break;
20640 default:
20641 gcc_unreachable ();
20644 /* We should be able to find the DIE now. */
20645 if (!decl_die)
20646 decl_die = lookup_decl_die (decl);
20647 gcc_assert (decl_die);
20650 return decl_die;
20653 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20654 always returned. */
20656 static dw_die_ref
20657 force_type_die (tree type)
20659 dw_die_ref type_die;
20661 type_die = lookup_type_die (type);
20662 if (!type_die)
20664 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20666 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20667 context_die);
20668 gcc_assert (type_die);
20670 return type_die;
20673 /* Force out any required namespaces to be able to output DECL,
20674 and return the new context_die for it, if it's changed. */
20676 static dw_die_ref
20677 setup_namespace_context (tree thing, dw_die_ref context_die)
20679 tree context = (DECL_P (thing)
20680 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20681 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20682 /* Force out the namespace. */
20683 context_die = force_decl_die (context);
20685 return context_die;
20688 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20689 type) within its namespace, if appropriate.
20691 For compatibility with older debuggers, namespace DIEs only contain
20692 declarations; all definitions are emitted at CU scope. */
20694 static dw_die_ref
20695 declare_in_namespace (tree thing, dw_die_ref context_die)
20697 dw_die_ref ns_context;
20699 if (debug_info_level <= DINFO_LEVEL_TERSE)
20700 return context_die;
20702 /* External declarations in the local scope only need to be emitted
20703 once, not once in the namespace and once in the scope.
20705 This avoids declaring the `extern' below in the
20706 namespace DIE as well as in the innermost scope:
20708 namespace S
20710 int i=5;
20711 int foo()
20713 int i=8;
20714 extern int i;
20715 return i;
20719 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20720 return context_die;
20722 /* If this decl is from an inlined function, then don't try to emit it in its
20723 namespace, as we will get confused. It would have already been emitted
20724 when the abstract instance of the inline function was emitted anyways. */
20725 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20726 return context_die;
20728 ns_context = setup_namespace_context (thing, context_die);
20730 if (ns_context != context_die)
20732 if (is_fortran ())
20733 return ns_context;
20734 if (DECL_P (thing))
20735 gen_decl_die (thing, NULL, ns_context);
20736 else
20737 gen_type_die (thing, ns_context);
20739 return context_die;
20742 /* Generate a DIE for a namespace or namespace alias. */
20744 static void
20745 gen_namespace_die (tree decl, dw_die_ref context_die)
20747 dw_die_ref namespace_die;
20749 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20750 they are an alias of. */
20751 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20753 /* Output a real namespace or module. */
20754 context_die = setup_namespace_context (decl, comp_unit_die ());
20755 namespace_die = new_die (is_fortran ()
20756 ? DW_TAG_module : DW_TAG_namespace,
20757 context_die, decl);
20758 /* For Fortran modules defined in different CU don't add src coords. */
20759 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20761 const char *name = dwarf2_name (decl, 0);
20762 if (name)
20763 add_name_attribute (namespace_die, name);
20765 else
20766 add_name_and_src_coords_attributes (namespace_die, decl);
20767 if (DECL_EXTERNAL (decl))
20768 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20769 equate_decl_number_to_die (decl, namespace_die);
20771 else
20773 /* Output a namespace alias. */
20775 /* Force out the namespace we are an alias of, if necessary. */
20776 dw_die_ref origin_die
20777 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20779 if (DECL_FILE_SCOPE_P (decl)
20780 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20781 context_die = setup_namespace_context (decl, comp_unit_die ());
20782 /* Now create the namespace alias DIE. */
20783 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20784 add_name_and_src_coords_attributes (namespace_die, decl);
20785 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20786 equate_decl_number_to_die (decl, namespace_die);
20788 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20789 if (want_pubnames ())
20790 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20793 /* Generate Dwarf debug information for a decl described by DECL.
20794 The return value is currently only meaningful for PARM_DECLs,
20795 for all other decls it returns NULL. */
20797 static dw_die_ref
20798 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20800 tree decl_or_origin = decl ? decl : origin;
20801 tree class_origin = NULL, ultimate_origin;
20803 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20804 return NULL;
20806 /* Ignore pointer bounds decls. */
20807 if (DECL_P (decl_or_origin)
20808 && TREE_TYPE (decl_or_origin)
20809 && POINTER_BOUNDS_P (decl_or_origin))
20810 return NULL;
20812 switch (TREE_CODE (decl_or_origin))
20814 case ERROR_MARK:
20815 break;
20817 case CONST_DECL:
20818 if (!is_fortran () && !is_ada ())
20820 /* The individual enumerators of an enum type get output when we output
20821 the Dwarf representation of the relevant enum type itself. */
20822 break;
20825 /* Emit its type. */
20826 gen_type_die (TREE_TYPE (decl), context_die);
20828 /* And its containing namespace. */
20829 context_die = declare_in_namespace (decl, context_die);
20831 gen_const_die (decl, context_die);
20832 break;
20834 case FUNCTION_DECL:
20835 /* Don't output any DIEs to represent mere function declarations,
20836 unless they are class members or explicit block externs. */
20837 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20838 && DECL_FILE_SCOPE_P (decl_or_origin)
20839 && (current_function_decl == NULL_TREE
20840 || DECL_ARTIFICIAL (decl_or_origin)))
20841 break;
20843 #if 0
20844 /* FIXME */
20845 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20846 on local redeclarations of global functions. That seems broken. */
20847 if (current_function_decl != decl)
20848 /* This is only a declaration. */;
20849 #endif
20851 /* If we're emitting a clone, emit info for the abstract instance. */
20852 if (origin || DECL_ORIGIN (decl) != decl)
20853 dwarf2out_abstract_function (origin
20854 ? DECL_ORIGIN (origin)
20855 : DECL_ABSTRACT_ORIGIN (decl));
20857 /* If we're emitting an out-of-line copy of an inline function,
20858 emit info for the abstract instance and set up to refer to it. */
20859 else if (cgraph_function_possibly_inlined_p (decl)
20860 && ! DECL_ABSTRACT_P (decl)
20861 && ! class_or_namespace_scope_p (context_die)
20862 /* dwarf2out_abstract_function won't emit a die if this is just
20863 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20864 that case, because that works only if we have a die. */
20865 && DECL_INITIAL (decl) != NULL_TREE)
20867 dwarf2out_abstract_function (decl);
20868 set_decl_origin_self (decl);
20871 /* Otherwise we're emitting the primary DIE for this decl. */
20872 else if (debug_info_level > DINFO_LEVEL_TERSE)
20874 /* Before we describe the FUNCTION_DECL itself, make sure that we
20875 have its containing type. */
20876 if (!origin)
20877 origin = decl_class_context (decl);
20878 if (origin != NULL_TREE)
20879 gen_type_die (origin, context_die);
20881 /* And its return type. */
20882 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20884 /* And its virtual context. */
20885 if (DECL_VINDEX (decl) != NULL_TREE)
20886 gen_type_die (DECL_CONTEXT (decl), context_die);
20888 /* Make sure we have a member DIE for decl. */
20889 if (origin != NULL_TREE)
20890 gen_type_die_for_member (origin, decl, context_die);
20892 /* And its containing namespace. */
20893 context_die = declare_in_namespace (decl, context_die);
20896 /* Now output a DIE to represent the function itself. */
20897 if (decl)
20898 gen_subprogram_die (decl, context_die);
20899 break;
20901 case TYPE_DECL:
20902 /* If we are in terse mode, don't generate any DIEs to represent any
20903 actual typedefs. */
20904 if (debug_info_level <= DINFO_LEVEL_TERSE)
20905 break;
20907 /* In the special case of a TYPE_DECL node representing the declaration
20908 of some type tag, if the given TYPE_DECL is marked as having been
20909 instantiated from some other (original) TYPE_DECL node (e.g. one which
20910 was generated within the original definition of an inline function) we
20911 used to generate a special (abbreviated) DW_TAG_structure_type,
20912 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20913 should be actually referencing those DIEs, as variable DIEs with that
20914 type would be emitted already in the abstract origin, so it was always
20915 removed during unused type prunning. Don't add anything in this
20916 case. */
20917 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20918 break;
20920 if (is_redundant_typedef (decl))
20921 gen_type_die (TREE_TYPE (decl), context_die);
20922 else
20923 /* Output a DIE to represent the typedef itself. */
20924 gen_typedef_die (decl, context_die);
20925 break;
20927 case LABEL_DECL:
20928 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20929 gen_label_die (decl, context_die);
20930 break;
20932 case VAR_DECL:
20933 case RESULT_DECL:
20934 /* If we are in terse mode, don't generate any DIEs to represent any
20935 variable declarations or definitions. */
20936 if (debug_info_level <= DINFO_LEVEL_TERSE)
20937 break;
20939 /* Output any DIEs that are needed to specify the type of this data
20940 object. */
20941 if (decl_by_reference_p (decl_or_origin))
20942 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20943 else
20944 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20946 /* And its containing type. */
20947 class_origin = decl_class_context (decl_or_origin);
20948 if (class_origin != NULL_TREE)
20949 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20951 /* And its containing namespace. */
20952 context_die = declare_in_namespace (decl_or_origin, context_die);
20954 /* Now output the DIE to represent the data object itself. This gets
20955 complicated because of the possibility that the VAR_DECL really
20956 represents an inlined instance of a formal parameter for an inline
20957 function. */
20958 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20959 if (ultimate_origin != NULL_TREE
20960 && TREE_CODE (ultimate_origin) == PARM_DECL)
20961 gen_formal_parameter_die (decl, origin,
20962 true /* Emit name attribute. */,
20963 context_die);
20964 else
20965 gen_variable_die (decl, origin, context_die);
20966 break;
20968 case FIELD_DECL:
20969 /* Ignore the nameless fields that are used to skip bits but handle C++
20970 anonymous unions and structs. */
20971 if (DECL_NAME (decl) != NULL_TREE
20972 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20973 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20975 gen_type_die (member_declared_type (decl), context_die);
20976 gen_field_die (decl, context_die);
20978 break;
20980 case PARM_DECL:
20981 if (DECL_BY_REFERENCE (decl_or_origin))
20982 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20983 else
20984 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20985 return gen_formal_parameter_die (decl, origin,
20986 true /* Emit name attribute. */,
20987 context_die);
20989 case NAMESPACE_DECL:
20990 case IMPORTED_DECL:
20991 if (dwarf_version >= 3 || !dwarf_strict)
20992 gen_namespace_die (decl, context_die);
20993 break;
20995 case NAMELIST_DECL:
20996 gen_namelist_decl (DECL_NAME (decl), context_die,
20997 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20998 break;
21000 default:
21001 /* Probably some frontend-internal decl. Assume we don't care. */
21002 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21003 break;
21006 return NULL;
21009 /* Output debug information for global decl DECL. Called from toplev.c after
21010 compilation proper has finished. */
21012 static void
21013 dwarf2out_global_decl (tree decl)
21015 /* Output DWARF2 information for file-scope tentative data object
21016 declarations, file-scope (extern) function declarations (which
21017 had no corresponding body) and file-scope tagged type declarations
21018 and definitions which have not yet been forced out. */
21019 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21020 && !POINTER_BOUNDS_P (decl))
21021 dwarf2out_decl (decl);
21024 /* Output debug information for type decl DECL. Called from toplev.c
21025 and from language front ends (to record built-in types). */
21026 static void
21027 dwarf2out_type_decl (tree decl, int local)
21029 if (!local)
21030 dwarf2out_decl (decl);
21033 /* Output debug information for imported module or decl DECL.
21034 NAME is non-NULL name in the lexical block if the decl has been renamed.
21035 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21036 that DECL belongs to.
21037 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21038 static void
21039 dwarf2out_imported_module_or_decl_1 (tree decl,
21040 tree name,
21041 tree lexical_block,
21042 dw_die_ref lexical_block_die)
21044 expanded_location xloc;
21045 dw_die_ref imported_die = NULL;
21046 dw_die_ref at_import_die;
21048 if (TREE_CODE (decl) == IMPORTED_DECL)
21050 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21051 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21052 gcc_assert (decl);
21054 else
21055 xloc = expand_location (input_location);
21057 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21059 at_import_die = force_type_die (TREE_TYPE (decl));
21060 /* For namespace N { typedef void T; } using N::T; base_type_die
21061 returns NULL, but DW_TAG_imported_declaration requires
21062 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21063 if (!at_import_die)
21065 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21066 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21067 at_import_die = lookup_type_die (TREE_TYPE (decl));
21068 gcc_assert (at_import_die);
21071 else
21073 at_import_die = lookup_decl_die (decl);
21074 if (!at_import_die)
21076 /* If we're trying to avoid duplicate debug info, we may not have
21077 emitted the member decl for this field. Emit it now. */
21078 if (TREE_CODE (decl) == FIELD_DECL)
21080 tree type = DECL_CONTEXT (decl);
21082 if (TYPE_CONTEXT (type)
21083 && TYPE_P (TYPE_CONTEXT (type))
21084 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21085 DINFO_USAGE_DIR_USE))
21086 return;
21087 gen_type_die_for_member (type, decl,
21088 get_context_die (TYPE_CONTEXT (type)));
21090 if (TREE_CODE (decl) == NAMELIST_DECL)
21091 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21092 get_context_die (DECL_CONTEXT (decl)),
21093 NULL_TREE);
21094 else
21095 at_import_die = force_decl_die (decl);
21099 if (TREE_CODE (decl) == NAMESPACE_DECL)
21101 if (dwarf_version >= 3 || !dwarf_strict)
21102 imported_die = new_die (DW_TAG_imported_module,
21103 lexical_block_die,
21104 lexical_block);
21105 else
21106 return;
21108 else
21109 imported_die = new_die (DW_TAG_imported_declaration,
21110 lexical_block_die,
21111 lexical_block);
21113 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21114 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21115 if (name)
21116 add_AT_string (imported_die, DW_AT_name,
21117 IDENTIFIER_POINTER (name));
21118 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21121 /* Output debug information for imported module or decl DECL.
21122 NAME is non-NULL name in context if the decl has been renamed.
21123 CHILD is true if decl is one of the renamed decls as part of
21124 importing whole module. */
21126 static void
21127 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21128 bool child)
21130 /* dw_die_ref at_import_die; */
21131 dw_die_ref scope_die;
21133 if (debug_info_level <= DINFO_LEVEL_TERSE)
21134 return;
21136 gcc_assert (decl);
21138 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21139 We need decl DIE for reference and scope die. First, get DIE for the decl
21140 itself. */
21142 /* Get the scope die for decl context. Use comp_unit_die for global module
21143 or decl. If die is not found for non globals, force new die. */
21144 if (context
21145 && TYPE_P (context)
21146 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21147 return;
21149 if (!(dwarf_version >= 3 || !dwarf_strict))
21150 return;
21152 scope_die = get_context_die (context);
21154 if (child)
21156 gcc_assert (scope_die->die_child);
21157 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21158 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21159 scope_die = scope_die->die_child;
21162 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21163 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21167 /* Output debug information for namelists. */
21169 static dw_die_ref
21170 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21172 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21173 tree value;
21174 unsigned i;
21176 if (debug_info_level <= DINFO_LEVEL_TERSE)
21177 return NULL;
21179 gcc_assert (scope_die != NULL);
21180 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21181 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21183 /* If there are no item_decls, we have a nondefining namelist, e.g.
21184 with USE association; hence, set DW_AT_declaration. */
21185 if (item_decls == NULL_TREE)
21187 add_AT_flag (nml_die, DW_AT_declaration, 1);
21188 return nml_die;
21191 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21193 nml_item_ref_die = lookup_decl_die (value);
21194 if (!nml_item_ref_die)
21195 nml_item_ref_die = force_decl_die (value);
21197 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21198 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21200 return nml_die;
21204 /* Write the debugging output for DECL. */
21206 static void
21207 dwarf2out_decl (tree decl)
21209 dw_die_ref context_die = comp_unit_die ();
21211 switch (TREE_CODE (decl))
21213 case ERROR_MARK:
21214 return;
21216 case FUNCTION_DECL:
21217 /* What we would really like to do here is to filter out all mere
21218 file-scope declarations of file-scope functions which are never
21219 referenced later within this translation unit (and keep all of ones
21220 that *are* referenced later on) but we aren't clairvoyant, so we have
21221 no idea which functions will be referenced in the future (i.e. later
21222 on within the current translation unit). So here we just ignore all
21223 file-scope function declarations which are not also definitions. If
21224 and when the debugger needs to know something about these functions,
21225 it will have to hunt around and find the DWARF information associated
21226 with the definition of the function.
21228 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21229 nodes represent definitions and which ones represent mere
21230 declarations. We have to check DECL_INITIAL instead. That's because
21231 the C front-end supports some weird semantics for "extern inline"
21232 function definitions. These can get inlined within the current
21233 translation unit (and thus, we need to generate Dwarf info for their
21234 abstract instances so that the Dwarf info for the concrete inlined
21235 instances can have something to refer to) but the compiler never
21236 generates any out-of-lines instances of such things (despite the fact
21237 that they *are* definitions).
21239 The important point is that the C front-end marks these "extern
21240 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21241 them anyway. Note that the C++ front-end also plays some similar games
21242 for inline function definitions appearing within include files which
21243 also contain `#pragma interface' pragmas.
21245 If we are called from dwarf2out_abstract_function output a DIE
21246 anyway. We can end up here this way with early inlining and LTO
21247 where the inlined function is output in a different LTRANS unit
21248 or not at all. */
21249 if (DECL_INITIAL (decl) == NULL_TREE
21250 && ! DECL_ABSTRACT_P (decl))
21251 return;
21253 /* If we're a nested function, initially use a parent of NULL; if we're
21254 a plain function, this will be fixed up in decls_for_scope. If
21255 we're a method, it will be ignored, since we already have a DIE. */
21256 if (decl_function_context (decl)
21257 /* But if we're in terse mode, we don't care about scope. */
21258 && debug_info_level > DINFO_LEVEL_TERSE)
21259 context_die = NULL;
21260 break;
21262 case VAR_DECL:
21263 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21264 declaration and if the declaration was never even referenced from
21265 within this entire compilation unit. We suppress these DIEs in
21266 order to save space in the .debug section (by eliminating entries
21267 which are probably useless). Note that we must not suppress
21268 block-local extern declarations (whether used or not) because that
21269 would screw-up the debugger's name lookup mechanism and cause it to
21270 miss things which really ought to be in scope at a given point. */
21271 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21272 return;
21274 /* For local statics lookup proper context die. */
21275 if (TREE_STATIC (decl)
21276 && DECL_CONTEXT (decl)
21277 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21278 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21280 /* If we are in terse mode, don't generate any DIEs to represent any
21281 variable declarations or definitions. */
21282 if (debug_info_level <= DINFO_LEVEL_TERSE)
21283 return;
21284 break;
21286 case CONST_DECL:
21287 if (debug_info_level <= DINFO_LEVEL_TERSE)
21288 return;
21289 if (!is_fortran () && !is_ada ())
21290 return;
21291 if (TREE_STATIC (decl) && decl_function_context (decl))
21292 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21293 break;
21295 case NAMESPACE_DECL:
21296 case IMPORTED_DECL:
21297 if (debug_info_level <= DINFO_LEVEL_TERSE)
21298 return;
21299 if (lookup_decl_die (decl) != NULL)
21300 return;
21301 break;
21303 case TYPE_DECL:
21304 /* Don't emit stubs for types unless they are needed by other DIEs. */
21305 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21306 return;
21308 /* Don't bother trying to generate any DIEs to represent any of the
21309 normal built-in types for the language we are compiling. */
21310 if (DECL_IS_BUILTIN (decl))
21311 return;
21313 /* If we are in terse mode, don't generate any DIEs for types. */
21314 if (debug_info_level <= DINFO_LEVEL_TERSE)
21315 return;
21317 /* If we're a function-scope tag, initially use a parent of NULL;
21318 this will be fixed up in decls_for_scope. */
21319 if (decl_function_context (decl))
21320 context_die = NULL;
21322 break;
21324 case NAMELIST_DECL:
21325 break;
21327 default:
21328 return;
21331 gen_decl_die (decl, NULL, context_die);
21334 /* Write the debugging output for DECL. */
21336 static void
21337 dwarf2out_function_decl (tree decl)
21339 dwarf2out_decl (decl);
21340 call_arg_locations = NULL;
21341 call_arg_loc_last = NULL;
21342 call_site_count = -1;
21343 tail_call_site_count = -1;
21344 block_map.release ();
21345 decl_loc_table->empty ();
21346 cached_dw_loc_list_table->empty ();
21349 /* Output a marker (i.e. a label) for the beginning of the generated code for
21350 a lexical block. */
21352 static void
21353 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21354 unsigned int blocknum)
21356 switch_to_section (current_function_section ());
21357 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21360 /* Output a marker (i.e. a label) for the end of the generated code for a
21361 lexical block. */
21363 static void
21364 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21366 switch_to_section (current_function_section ());
21367 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21370 /* Returns nonzero if it is appropriate not to emit any debugging
21371 information for BLOCK, because it doesn't contain any instructions.
21373 Don't allow this for blocks with nested functions or local classes
21374 as we would end up with orphans, and in the presence of scheduling
21375 we may end up calling them anyway. */
21377 static bool
21378 dwarf2out_ignore_block (const_tree block)
21380 tree decl;
21381 unsigned int i;
21383 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21384 if (TREE_CODE (decl) == FUNCTION_DECL
21385 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21386 return 0;
21387 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21389 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21390 if (TREE_CODE (decl) == FUNCTION_DECL
21391 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21392 return 0;
21395 return 1;
21398 /* Hash table routines for file_hash. */
21400 bool
21401 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21403 return filename_cmp (p1->filename, p2) == 0;
21406 hashval_t
21407 dwarf_file_hasher::hash (dwarf_file_data *p)
21409 return htab_hash_string (p->filename);
21412 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21413 dwarf2out.c) and return its "index". The index of each (known) filename is
21414 just a unique number which is associated with only that one filename. We
21415 need such numbers for the sake of generating labels (in the .debug_sfnames
21416 section) and references to those files numbers (in the .debug_srcinfo
21417 and.debug_macinfo sections). If the filename given as an argument is not
21418 found in our current list, add it to the list and assign it the next
21419 available unique index number. In order to speed up searches, we remember
21420 the index of the filename was looked up last. This handles the majority of
21421 all searches. */
21423 static struct dwarf_file_data *
21424 lookup_filename (const char *file_name)
21426 struct dwarf_file_data * created;
21428 /* Check to see if the file name that was searched on the previous
21429 call matches this file name. If so, return the index. */
21430 if (file_table_last_lookup
21431 && (file_name == file_table_last_lookup->filename
21432 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21433 return file_table_last_lookup;
21435 /* Didn't match the previous lookup, search the table. */
21436 dwarf_file_data **slot
21437 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21438 INSERT);
21439 if (*slot)
21440 return *slot;
21442 created = ggc_alloc<dwarf_file_data> ();
21443 created->filename = file_name;
21444 created->emitted_number = 0;
21445 *slot = created;
21446 return created;
21449 /* If the assembler will construct the file table, then translate the compiler
21450 internal file table number into the assembler file table number, and emit
21451 a .file directive if we haven't already emitted one yet. The file table
21452 numbers are different because we prune debug info for unused variables and
21453 types, which may include filenames. */
21455 static int
21456 maybe_emit_file (struct dwarf_file_data * fd)
21458 if (! fd->emitted_number)
21460 if (last_emitted_file)
21461 fd->emitted_number = last_emitted_file->emitted_number + 1;
21462 else
21463 fd->emitted_number = 1;
21464 last_emitted_file = fd;
21466 if (DWARF2_ASM_LINE_DEBUG_INFO)
21468 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21469 output_quoted_string (asm_out_file,
21470 remap_debug_filename (fd->filename));
21471 fputc ('\n', asm_out_file);
21475 return fd->emitted_number;
21478 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21479 That generation should happen after function debug info has been
21480 generated. The value of the attribute is the constant value of ARG. */
21482 static void
21483 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21485 die_arg_entry entry;
21487 if (!die || !arg)
21488 return;
21490 if (!tmpl_value_parm_die_table)
21491 vec_alloc (tmpl_value_parm_die_table, 32);
21493 entry.die = die;
21494 entry.arg = arg;
21495 vec_safe_push (tmpl_value_parm_die_table, entry);
21498 /* Return TRUE if T is an instance of generic type, FALSE
21499 otherwise. */
21501 static bool
21502 generic_type_p (tree t)
21504 if (t == NULL_TREE || !TYPE_P (t))
21505 return false;
21506 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21509 /* Schedule the generation of the generic parameter dies for the
21510 instance of generic type T. The proper generation itself is later
21511 done by gen_scheduled_generic_parms_dies. */
21513 static void
21514 schedule_generic_params_dies_gen (tree t)
21516 if (!generic_type_p (t))
21517 return;
21519 if (!generic_type_instances)
21520 vec_alloc (generic_type_instances, 256);
21522 vec_safe_push (generic_type_instances, t);
21525 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21526 by append_entry_to_tmpl_value_parm_die_table. This function must
21527 be called after function DIEs have been generated. */
21529 static void
21530 gen_remaining_tmpl_value_param_die_attribute (void)
21532 if (tmpl_value_parm_die_table)
21534 unsigned i;
21535 die_arg_entry *e;
21537 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21538 tree_add_const_value_attribute (e->die, e->arg);
21542 /* Generate generic parameters DIEs for instances of generic types
21543 that have been previously scheduled by
21544 schedule_generic_params_dies_gen. This function must be called
21545 after all the types of the CU have been laid out. */
21547 static void
21548 gen_scheduled_generic_parms_dies (void)
21550 unsigned i;
21551 tree t;
21553 if (!generic_type_instances)
21554 return;
21556 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21557 if (COMPLETE_TYPE_P (t))
21558 gen_generic_params_dies (t);
21562 /* Replace DW_AT_name for the decl with name. */
21564 static void
21565 dwarf2out_set_name (tree decl, tree name)
21567 dw_die_ref die;
21568 dw_attr_ref attr;
21569 const char *dname;
21571 die = TYPE_SYMTAB_DIE (decl);
21572 if (!die)
21573 return;
21575 dname = dwarf2_name (name, 0);
21576 if (!dname)
21577 return;
21579 attr = get_AT (die, DW_AT_name);
21580 if (attr)
21582 struct indirect_string_node *node;
21584 node = find_AT_string (dname);
21585 /* replace the string. */
21586 attr->dw_attr_val.v.val_str = node;
21589 else
21590 add_name_attribute (die, dname);
21593 /* True if before or during processing of the first function being emitted. */
21594 static bool in_first_function_p = true;
21595 /* True if loc_note during dwarf2out_var_location call might still be
21596 before first real instruction at address equal to .Ltext0. */
21597 static bool maybe_at_text_label_p = true;
21598 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21599 static unsigned int first_loclabel_num_not_at_text_label;
21601 /* Called by the final INSN scan whenever we see a var location. We
21602 use it to drop labels in the right places, and throw the location in
21603 our lookup table. */
21605 static void
21606 dwarf2out_var_location (rtx_insn *loc_note)
21608 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21609 struct var_loc_node *newloc;
21610 rtx_insn *next_real, *next_note;
21611 static const char *last_label;
21612 static const char *last_postcall_label;
21613 static bool last_in_cold_section_p;
21614 static rtx_insn *expected_next_loc_note;
21615 tree decl;
21616 bool var_loc_p;
21618 if (!NOTE_P (loc_note))
21620 if (CALL_P (loc_note))
21622 call_site_count++;
21623 if (SIBLING_CALL_P (loc_note))
21624 tail_call_site_count++;
21626 return;
21629 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21630 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21631 return;
21633 /* Optimize processing a large consecutive sequence of location
21634 notes so we don't spend too much time in next_real_insn. If the
21635 next insn is another location note, remember the next_real_insn
21636 calculation for next time. */
21637 next_real = cached_next_real_insn;
21638 if (next_real)
21640 if (expected_next_loc_note != loc_note)
21641 next_real = NULL;
21644 next_note = NEXT_INSN (loc_note);
21645 if (! next_note
21646 || next_note->deleted ()
21647 || ! NOTE_P (next_note)
21648 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21649 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21650 next_note = NULL;
21652 if (! next_real)
21653 next_real = next_real_insn (loc_note);
21655 if (next_note)
21657 expected_next_loc_note = next_note;
21658 cached_next_real_insn = next_real;
21660 else
21661 cached_next_real_insn = NULL;
21663 /* If there are no instructions which would be affected by this note,
21664 don't do anything. */
21665 if (var_loc_p
21666 && next_real == NULL_RTX
21667 && !NOTE_DURING_CALL_P (loc_note))
21668 return;
21670 if (next_real == NULL_RTX)
21671 next_real = get_last_insn ();
21673 /* If there were any real insns between note we processed last time
21674 and this note (or if it is the first note), clear
21675 last_{,postcall_}label so that they are not reused this time. */
21676 if (last_var_location_insn == NULL_RTX
21677 || last_var_location_insn != next_real
21678 || last_in_cold_section_p != in_cold_section_p)
21680 last_label = NULL;
21681 last_postcall_label = NULL;
21684 if (var_loc_p)
21686 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21687 newloc = add_var_loc_to_decl (decl, loc_note,
21688 NOTE_DURING_CALL_P (loc_note)
21689 ? last_postcall_label : last_label);
21690 if (newloc == NULL)
21691 return;
21693 else
21695 decl = NULL_TREE;
21696 newloc = NULL;
21699 /* If there were no real insns between note we processed last time
21700 and this note, use the label we emitted last time. Otherwise
21701 create a new label and emit it. */
21702 if (last_label == NULL)
21704 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21705 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21706 loclabel_num++;
21707 last_label = ggc_strdup (loclabel);
21708 /* See if loclabel might be equal to .Ltext0. If yes,
21709 bump first_loclabel_num_not_at_text_label. */
21710 if (!have_multiple_function_sections
21711 && in_first_function_p
21712 && maybe_at_text_label_p)
21714 static rtx_insn *last_start;
21715 rtx_insn *insn;
21716 for (insn = loc_note; insn; insn = previous_insn (insn))
21717 if (insn == last_start)
21718 break;
21719 else if (!NONDEBUG_INSN_P (insn))
21720 continue;
21721 else
21723 rtx body = PATTERN (insn);
21724 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21725 continue;
21726 /* Inline asm could occupy zero bytes. */
21727 else if (GET_CODE (body) == ASM_INPUT
21728 || asm_noperands (body) >= 0)
21729 continue;
21730 #ifdef HAVE_attr_length
21731 else if (get_attr_min_length (insn) == 0)
21732 continue;
21733 #endif
21734 else
21736 /* Assume insn has non-zero length. */
21737 maybe_at_text_label_p = false;
21738 break;
21741 if (maybe_at_text_label_p)
21743 last_start = loc_note;
21744 first_loclabel_num_not_at_text_label = loclabel_num;
21749 if (!var_loc_p)
21751 struct call_arg_loc_node *ca_loc
21752 = ggc_cleared_alloc<call_arg_loc_node> ();
21753 rtx_insn *prev = prev_real_insn (loc_note);
21754 rtx x;
21755 ca_loc->call_arg_loc_note = loc_note;
21756 ca_loc->next = NULL;
21757 ca_loc->label = last_label;
21758 gcc_assert (prev
21759 && (CALL_P (prev)
21760 || (NONJUMP_INSN_P (prev)
21761 && GET_CODE (PATTERN (prev)) == SEQUENCE
21762 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21763 if (!CALL_P (prev))
21764 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21765 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21766 x = get_call_rtx_from (PATTERN (prev));
21767 if (x)
21769 x = XEXP (XEXP (x, 0), 0);
21770 if (GET_CODE (x) == SYMBOL_REF
21771 && SYMBOL_REF_DECL (x)
21772 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21773 ca_loc->symbol_ref = x;
21775 ca_loc->block = insn_scope (prev);
21776 if (call_arg_locations)
21777 call_arg_loc_last->next = ca_loc;
21778 else
21779 call_arg_locations = ca_loc;
21780 call_arg_loc_last = ca_loc;
21782 else if (!NOTE_DURING_CALL_P (loc_note))
21783 newloc->label = last_label;
21784 else
21786 if (!last_postcall_label)
21788 sprintf (loclabel, "%s-1", last_label);
21789 last_postcall_label = ggc_strdup (loclabel);
21791 newloc->label = last_postcall_label;
21794 last_var_location_insn = next_real;
21795 last_in_cold_section_p = in_cold_section_p;
21798 /* Note in one location list that text section has changed. */
21801 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21803 var_loc_list *list = *slot;
21804 if (list->first)
21805 list->last_before_switch
21806 = list->last->next ? list->last->next : list->last;
21807 return 1;
21810 /* Note in all location lists that text section has changed. */
21812 static void
21813 var_location_switch_text_section (void)
21815 if (decl_loc_table == NULL)
21816 return;
21818 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21821 /* Create a new line number table. */
21823 static dw_line_info_table *
21824 new_line_info_table (void)
21826 dw_line_info_table *table;
21828 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21829 table->file_num = 1;
21830 table->line_num = 1;
21831 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21833 return table;
21836 /* Lookup the "current" table into which we emit line info, so
21837 that we don't have to do it for every source line. */
21839 static void
21840 set_cur_line_info_table (section *sec)
21842 dw_line_info_table *table;
21844 if (sec == text_section)
21845 table = text_section_line_info;
21846 else if (sec == cold_text_section)
21848 table = cold_text_section_line_info;
21849 if (!table)
21851 cold_text_section_line_info = table = new_line_info_table ();
21852 table->end_label = cold_end_label;
21855 else
21857 const char *end_label;
21859 if (flag_reorder_blocks_and_partition)
21861 if (in_cold_section_p)
21862 end_label = crtl->subsections.cold_section_end_label;
21863 else
21864 end_label = crtl->subsections.hot_section_end_label;
21866 else
21868 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21869 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21870 current_function_funcdef_no);
21871 end_label = ggc_strdup (label);
21874 table = new_line_info_table ();
21875 table->end_label = end_label;
21877 vec_safe_push (separate_line_info, table);
21880 if (DWARF2_ASM_LINE_DEBUG_INFO)
21881 table->is_stmt = (cur_line_info_table
21882 ? cur_line_info_table->is_stmt
21883 : DWARF_LINE_DEFAULT_IS_STMT_START);
21884 cur_line_info_table = table;
21888 /* We need to reset the locations at the beginning of each
21889 function. We can't do this in the end_function hook, because the
21890 declarations that use the locations won't have been output when
21891 that hook is called. Also compute have_multiple_function_sections here. */
21893 static void
21894 dwarf2out_begin_function (tree fun)
21896 section *sec = function_section (fun);
21898 if (sec != text_section)
21899 have_multiple_function_sections = true;
21901 if (flag_reorder_blocks_and_partition && !cold_text_section)
21903 gcc_assert (current_function_decl == fun);
21904 cold_text_section = unlikely_text_section ();
21905 switch_to_section (cold_text_section);
21906 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21907 switch_to_section (sec);
21910 dwarf2out_note_section_used ();
21911 call_site_count = 0;
21912 tail_call_site_count = 0;
21914 set_cur_line_info_table (sec);
21917 /* Helper function of dwarf2out_end_function, called only after emitting
21918 the very first function into assembly. Check if some .debug_loc range
21919 might end with a .LVL* label that could be equal to .Ltext0.
21920 In that case we must force using absolute addresses in .debug_loc ranges,
21921 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21922 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21923 list terminator.
21924 Set have_multiple_function_sections to true in that case and
21925 terminate htab traversal. */
21928 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21930 var_loc_list *entry = *slot;
21931 struct var_loc_node *node;
21933 node = entry->first;
21934 if (node && node->next && node->next->label)
21936 unsigned int i;
21937 const char *label = node->next->label;
21938 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21940 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21942 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21943 if (strcmp (label, loclabel) == 0)
21945 have_multiple_function_sections = true;
21946 return 0;
21950 return 1;
21953 /* Hook called after emitting a function into assembly.
21954 This does something only for the very first function emitted. */
21956 static void
21957 dwarf2out_end_function (unsigned int)
21959 if (in_first_function_p
21960 && !have_multiple_function_sections
21961 && first_loclabel_num_not_at_text_label
21962 && decl_loc_table)
21963 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
21964 in_first_function_p = false;
21965 maybe_at_text_label_p = false;
21968 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21970 static void
21971 push_dw_line_info_entry (dw_line_info_table *table,
21972 enum dw_line_info_opcode opcode, unsigned int val)
21974 dw_line_info_entry e;
21975 e.opcode = opcode;
21976 e.val = val;
21977 vec_safe_push (table->entries, e);
21980 /* Output a label to mark the beginning of a source code line entry
21981 and record information relating to this source line, in
21982 'line_info_table' for later output of the .debug_line section. */
21983 /* ??? The discriminator parameter ought to be unsigned. */
21985 static void
21986 dwarf2out_source_line (unsigned int line, const char *filename,
21987 int discriminator, bool is_stmt)
21989 unsigned int file_num;
21990 dw_line_info_table *table;
21992 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21993 return;
21995 /* The discriminator column was added in dwarf4. Simplify the below
21996 by simply removing it if we're not supposed to output it. */
21997 if (dwarf_version < 4 && dwarf_strict)
21998 discriminator = 0;
22000 table = cur_line_info_table;
22001 file_num = maybe_emit_file (lookup_filename (filename));
22003 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22004 the debugger has used the second (possibly duplicate) line number
22005 at the beginning of the function to mark the end of the prologue.
22006 We could eliminate any other duplicates within the function. For
22007 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22008 that second line number entry. */
22009 /* Recall that this end-of-prologue indication is *not* the same thing
22010 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22011 to which the hook corresponds, follows the last insn that was
22012 emitted by gen_prologue. What we need is to precede the first insn
22013 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22014 insn that corresponds to something the user wrote. These may be
22015 very different locations once scheduling is enabled. */
22017 if (0 && file_num == table->file_num
22018 && line == table->line_num
22019 && discriminator == table->discrim_num
22020 && is_stmt == table->is_stmt)
22021 return;
22023 switch_to_section (current_function_section ());
22025 /* If requested, emit something human-readable. */
22026 if (flag_debug_asm)
22027 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22029 if (DWARF2_ASM_LINE_DEBUG_INFO)
22031 /* Emit the .loc directive understood by GNU as. */
22032 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22033 file_num, line, is_stmt, discriminator */
22034 fputs ("\t.loc ", asm_out_file);
22035 fprint_ul (asm_out_file, file_num);
22036 putc (' ', asm_out_file);
22037 fprint_ul (asm_out_file, line);
22038 putc (' ', asm_out_file);
22039 putc ('0', asm_out_file);
22041 if (is_stmt != table->is_stmt)
22043 fputs (" is_stmt ", asm_out_file);
22044 putc (is_stmt ? '1' : '0', asm_out_file);
22046 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22048 gcc_assert (discriminator > 0);
22049 fputs (" discriminator ", asm_out_file);
22050 fprint_ul (asm_out_file, (unsigned long) discriminator);
22052 putc ('\n', asm_out_file);
22054 else
22056 unsigned int label_num = ++line_info_label_num;
22058 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22060 push_dw_line_info_entry (table, LI_set_address, label_num);
22061 if (file_num != table->file_num)
22062 push_dw_line_info_entry (table, LI_set_file, file_num);
22063 if (discriminator != table->discrim_num)
22064 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22065 if (is_stmt != table->is_stmt)
22066 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22067 push_dw_line_info_entry (table, LI_set_line, line);
22070 table->file_num = file_num;
22071 table->line_num = line;
22072 table->discrim_num = discriminator;
22073 table->is_stmt = is_stmt;
22074 table->in_use = true;
22077 /* Record the beginning of a new source file. */
22079 static void
22080 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22082 if (flag_eliminate_dwarf2_dups)
22084 /* Record the beginning of the file for break_out_includes. */
22085 dw_die_ref bincl_die;
22087 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22088 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22091 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22093 macinfo_entry e;
22094 e.code = DW_MACINFO_start_file;
22095 e.lineno = lineno;
22096 e.info = ggc_strdup (filename);
22097 vec_safe_push (macinfo_table, e);
22101 /* Record the end of a source file. */
22103 static void
22104 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22106 if (flag_eliminate_dwarf2_dups)
22107 /* Record the end of the file for break_out_includes. */
22108 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22110 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22112 macinfo_entry e;
22113 e.code = DW_MACINFO_end_file;
22114 e.lineno = lineno;
22115 e.info = NULL;
22116 vec_safe_push (macinfo_table, e);
22120 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22121 the tail part of the directive line, i.e. the part which is past the
22122 initial whitespace, #, whitespace, directive-name, whitespace part. */
22124 static void
22125 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22126 const char *buffer ATTRIBUTE_UNUSED)
22128 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22130 macinfo_entry e;
22131 /* Insert a dummy first entry to be able to optimize the whole
22132 predefined macro block using DW_MACRO_GNU_transparent_include. */
22133 if (macinfo_table->is_empty () && lineno <= 1)
22135 e.code = 0;
22136 e.lineno = 0;
22137 e.info = NULL;
22138 vec_safe_push (macinfo_table, e);
22140 e.code = DW_MACINFO_define;
22141 e.lineno = lineno;
22142 e.info = ggc_strdup (buffer);
22143 vec_safe_push (macinfo_table, e);
22147 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22148 the tail part of the directive line, i.e. the part which is past the
22149 initial whitespace, #, whitespace, directive-name, whitespace part. */
22151 static void
22152 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22153 const char *buffer ATTRIBUTE_UNUSED)
22155 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22157 macinfo_entry e;
22158 /* Insert a dummy first entry to be able to optimize the whole
22159 predefined macro block using DW_MACRO_GNU_transparent_include. */
22160 if (macinfo_table->is_empty () && lineno <= 1)
22162 e.code = 0;
22163 e.lineno = 0;
22164 e.info = NULL;
22165 vec_safe_push (macinfo_table, e);
22167 e.code = DW_MACINFO_undef;
22168 e.lineno = lineno;
22169 e.info = ggc_strdup (buffer);
22170 vec_safe_push (macinfo_table, e);
22174 /* Helpers to manipulate hash table of CUs. */
22176 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22178 typedef macinfo_entry value_type;
22179 typedef macinfo_entry compare_type;
22180 static inline hashval_t hash (const value_type *);
22181 static inline bool equal (const value_type *, const compare_type *);
22184 inline hashval_t
22185 macinfo_entry_hasher::hash (const value_type *entry)
22187 return htab_hash_string (entry->info);
22190 inline bool
22191 macinfo_entry_hasher::equal (const value_type *entry1,
22192 const compare_type *entry2)
22194 return !strcmp (entry1->info, entry2->info);
22197 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22199 /* Output a single .debug_macinfo entry. */
22201 static void
22202 output_macinfo_op (macinfo_entry *ref)
22204 int file_num;
22205 size_t len;
22206 struct indirect_string_node *node;
22207 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22208 struct dwarf_file_data *fd;
22210 switch (ref->code)
22212 case DW_MACINFO_start_file:
22213 fd = lookup_filename (ref->info);
22214 file_num = maybe_emit_file (fd);
22215 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22216 dw2_asm_output_data_uleb128 (ref->lineno,
22217 "Included from line number %lu",
22218 (unsigned long) ref->lineno);
22219 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22220 break;
22221 case DW_MACINFO_end_file:
22222 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22223 break;
22224 case DW_MACINFO_define:
22225 case DW_MACINFO_undef:
22226 len = strlen (ref->info) + 1;
22227 if (!dwarf_strict
22228 && len > DWARF_OFFSET_SIZE
22229 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22230 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22232 ref->code = ref->code == DW_MACINFO_define
22233 ? DW_MACRO_GNU_define_indirect
22234 : DW_MACRO_GNU_undef_indirect;
22235 output_macinfo_op (ref);
22236 return;
22238 dw2_asm_output_data (1, ref->code,
22239 ref->code == DW_MACINFO_define
22240 ? "Define macro" : "Undefine macro");
22241 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22242 (unsigned long) ref->lineno);
22243 dw2_asm_output_nstring (ref->info, -1, "The macro");
22244 break;
22245 case DW_MACRO_GNU_define_indirect:
22246 case DW_MACRO_GNU_undef_indirect:
22247 node = find_AT_string (ref->info);
22248 gcc_assert (node
22249 && ((node->form == DW_FORM_strp)
22250 || (node->form == DW_FORM_GNU_str_index)));
22251 dw2_asm_output_data (1, ref->code,
22252 ref->code == DW_MACRO_GNU_define_indirect
22253 ? "Define macro indirect"
22254 : "Undefine macro indirect");
22255 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22256 (unsigned long) ref->lineno);
22257 if (node->form == DW_FORM_strp)
22258 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22259 debug_str_section, "The macro: \"%s\"",
22260 ref->info);
22261 else
22262 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22263 ref->info);
22264 break;
22265 case DW_MACRO_GNU_transparent_include:
22266 dw2_asm_output_data (1, ref->code, "Transparent include");
22267 ASM_GENERATE_INTERNAL_LABEL (label,
22268 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22269 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22270 break;
22271 default:
22272 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22273 ASM_COMMENT_START, (unsigned long) ref->code);
22274 break;
22278 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22279 other compilation unit .debug_macinfo sections. IDX is the first
22280 index of a define/undef, return the number of ops that should be
22281 emitted in a comdat .debug_macinfo section and emit
22282 a DW_MACRO_GNU_transparent_include entry referencing it.
22283 If the define/undef entry should be emitted normally, return 0. */
22285 static unsigned
22286 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22287 macinfo_hash_type **macinfo_htab)
22289 macinfo_entry *first, *second, *cur, *inc;
22290 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22291 unsigned char checksum[16];
22292 struct md5_ctx ctx;
22293 char *grp_name, *tail;
22294 const char *base;
22295 unsigned int i, count, encoded_filename_len, linebuf_len;
22296 macinfo_entry **slot;
22298 first = &(*macinfo_table)[idx];
22299 second = &(*macinfo_table)[idx + 1];
22301 /* Optimize only if there are at least two consecutive define/undef ops,
22302 and either all of them are before first DW_MACINFO_start_file
22303 with lineno {0,1} (i.e. predefined macro block), or all of them are
22304 in some included header file. */
22305 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22306 return 0;
22307 if (vec_safe_is_empty (files))
22309 if (first->lineno > 1 || second->lineno > 1)
22310 return 0;
22312 else if (first->lineno == 0)
22313 return 0;
22315 /* Find the last define/undef entry that can be grouped together
22316 with first and at the same time compute md5 checksum of their
22317 codes, linenumbers and strings. */
22318 md5_init_ctx (&ctx);
22319 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22320 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22321 break;
22322 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22323 break;
22324 else
22326 unsigned char code = cur->code;
22327 md5_process_bytes (&code, 1, &ctx);
22328 checksum_uleb128 (cur->lineno, &ctx);
22329 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22331 md5_finish_ctx (&ctx, checksum);
22332 count = i - idx;
22334 /* From the containing include filename (if any) pick up just
22335 usable characters from its basename. */
22336 if (vec_safe_is_empty (files))
22337 base = "";
22338 else
22339 base = lbasename (files->last ().info);
22340 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22341 if (ISIDNUM (base[i]) || base[i] == '.')
22342 encoded_filename_len++;
22343 /* Count . at the end. */
22344 if (encoded_filename_len)
22345 encoded_filename_len++;
22347 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22348 linebuf_len = strlen (linebuf);
22350 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22351 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22352 + 16 * 2 + 1);
22353 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22354 tail = grp_name + 4;
22355 if (encoded_filename_len)
22357 for (i = 0; base[i]; i++)
22358 if (ISIDNUM (base[i]) || base[i] == '.')
22359 *tail++ = base[i];
22360 *tail++ = '.';
22362 memcpy (tail, linebuf, linebuf_len);
22363 tail += linebuf_len;
22364 *tail++ = '.';
22365 for (i = 0; i < 16; i++)
22366 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22368 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22369 in the empty vector entry before the first define/undef. */
22370 inc = &(*macinfo_table)[idx - 1];
22371 inc->code = DW_MACRO_GNU_transparent_include;
22372 inc->lineno = 0;
22373 inc->info = ggc_strdup (grp_name);
22374 if (!*macinfo_htab)
22375 *macinfo_htab = new macinfo_hash_type (10);
22376 /* Avoid emitting duplicates. */
22377 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22378 if (*slot != NULL)
22380 inc->code = 0;
22381 inc->info = NULL;
22382 /* If such an entry has been used before, just emit
22383 a DW_MACRO_GNU_transparent_include op. */
22384 inc = *slot;
22385 output_macinfo_op (inc);
22386 /* And clear all macinfo_entry in the range to avoid emitting them
22387 in the second pass. */
22388 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22390 cur->code = 0;
22391 cur->info = NULL;
22394 else
22396 *slot = inc;
22397 inc->lineno = (*macinfo_htab)->elements ();
22398 output_macinfo_op (inc);
22400 return count;
22403 /* Save any strings needed by the macinfo table in the debug str
22404 table. All strings must be collected into the table by the time
22405 index_string is called. */
22407 static void
22408 save_macinfo_strings (void)
22410 unsigned len;
22411 unsigned i;
22412 macinfo_entry *ref;
22414 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22416 switch (ref->code)
22418 /* Match the logic in output_macinfo_op to decide on
22419 indirect strings. */
22420 case DW_MACINFO_define:
22421 case DW_MACINFO_undef:
22422 len = strlen (ref->info) + 1;
22423 if (!dwarf_strict
22424 && len > DWARF_OFFSET_SIZE
22425 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22426 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22427 set_indirect_string (find_AT_string (ref->info));
22428 break;
22429 case DW_MACRO_GNU_define_indirect:
22430 case DW_MACRO_GNU_undef_indirect:
22431 set_indirect_string (find_AT_string (ref->info));
22432 break;
22433 default:
22434 break;
22439 /* Output macinfo section(s). */
22441 static void
22442 output_macinfo (void)
22444 unsigned i;
22445 unsigned long length = vec_safe_length (macinfo_table);
22446 macinfo_entry *ref;
22447 vec<macinfo_entry, va_gc> *files = NULL;
22448 macinfo_hash_type *macinfo_htab = NULL;
22450 if (! length)
22451 return;
22453 /* output_macinfo* uses these interchangeably. */
22454 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22455 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22456 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22457 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22459 /* For .debug_macro emit the section header. */
22460 if (!dwarf_strict)
22462 dw2_asm_output_data (2, 4, "DWARF macro version number");
22463 if (DWARF_OFFSET_SIZE == 8)
22464 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22465 else
22466 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22467 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22468 (!dwarf_split_debug_info ? debug_line_section_label
22469 : debug_skeleton_line_section_label),
22470 debug_line_section, NULL);
22473 /* In the first loop, it emits the primary .debug_macinfo section
22474 and after each emitted op the macinfo_entry is cleared.
22475 If a longer range of define/undef ops can be optimized using
22476 DW_MACRO_GNU_transparent_include, the
22477 DW_MACRO_GNU_transparent_include op is emitted and kept in
22478 the vector before the first define/undef in the range and the
22479 whole range of define/undef ops is not emitted and kept. */
22480 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22482 switch (ref->code)
22484 case DW_MACINFO_start_file:
22485 vec_safe_push (files, *ref);
22486 break;
22487 case DW_MACINFO_end_file:
22488 if (!vec_safe_is_empty (files))
22489 files->pop ();
22490 break;
22491 case DW_MACINFO_define:
22492 case DW_MACINFO_undef:
22493 if (!dwarf_strict
22494 && HAVE_COMDAT_GROUP
22495 && vec_safe_length (files) != 1
22496 && i > 0
22497 && i + 1 < length
22498 && (*macinfo_table)[i - 1].code == 0)
22500 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22501 if (count)
22503 i += count - 1;
22504 continue;
22507 break;
22508 case 0:
22509 /* A dummy entry may be inserted at the beginning to be able
22510 to optimize the whole block of predefined macros. */
22511 if (i == 0)
22512 continue;
22513 default:
22514 break;
22516 output_macinfo_op (ref);
22517 ref->info = NULL;
22518 ref->code = 0;
22521 if (!macinfo_htab)
22522 return;
22524 delete macinfo_htab;
22525 macinfo_htab = NULL;
22527 /* If any DW_MACRO_GNU_transparent_include were used, on those
22528 DW_MACRO_GNU_transparent_include entries terminate the
22529 current chain and switch to a new comdat .debug_macinfo
22530 section and emit the define/undef entries within it. */
22531 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22532 switch (ref->code)
22534 case 0:
22535 continue;
22536 case DW_MACRO_GNU_transparent_include:
22538 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22539 tree comdat_key = get_identifier (ref->info);
22540 /* Terminate the previous .debug_macinfo section. */
22541 dw2_asm_output_data (1, 0, "End compilation unit");
22542 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22543 SECTION_DEBUG
22544 | SECTION_LINKONCE,
22545 comdat_key);
22546 ASM_GENERATE_INTERNAL_LABEL (label,
22547 DEBUG_MACRO_SECTION_LABEL,
22548 ref->lineno);
22549 ASM_OUTPUT_LABEL (asm_out_file, label);
22550 ref->code = 0;
22551 ref->info = NULL;
22552 dw2_asm_output_data (2, 4, "DWARF macro version number");
22553 if (DWARF_OFFSET_SIZE == 8)
22554 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22555 else
22556 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22558 break;
22559 case DW_MACINFO_define:
22560 case DW_MACINFO_undef:
22561 output_macinfo_op (ref);
22562 ref->code = 0;
22563 ref->info = NULL;
22564 break;
22565 default:
22566 gcc_unreachable ();
22570 /* Set up for Dwarf output at the start of compilation. */
22572 static void
22573 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22575 /* Allocate the file_table. */
22576 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22578 /* Allocate the decl_die_table. */
22579 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22581 /* Allocate the decl_loc_table. */
22582 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22584 /* Allocate the cached_dw_loc_list_table. */
22585 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22587 /* Allocate the initial hunk of the decl_scope_table. */
22588 vec_alloc (decl_scope_table, 256);
22590 /* Allocate the initial hunk of the abbrev_die_table. */
22591 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22592 (ABBREV_DIE_TABLE_INCREMENT);
22593 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22594 /* Zero-th entry is allocated, but unused. */
22595 abbrev_die_table_in_use = 1;
22597 /* Allocate the pubtypes and pubnames vectors. */
22598 vec_alloc (pubname_table, 32);
22599 vec_alloc (pubtype_table, 32);
22601 vec_alloc (incomplete_types, 64);
22603 vec_alloc (used_rtx_array, 32);
22605 if (!dwarf_split_debug_info)
22607 debug_info_section = get_section (DEBUG_INFO_SECTION,
22608 SECTION_DEBUG, NULL);
22609 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22610 SECTION_DEBUG, NULL);
22611 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22612 SECTION_DEBUG, NULL);
22614 else
22616 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22617 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22618 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22619 SECTION_DEBUG | SECTION_EXCLUDE,
22620 NULL);
22621 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22622 SECTION_DEBUG, NULL);
22623 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22624 SECTION_DEBUG, NULL);
22625 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22626 SECTION_DEBUG, NULL);
22627 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22628 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22630 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22631 the main .o, but the skeleton_line goes into the split off dwo. */
22632 debug_skeleton_line_section
22633 = get_section (DEBUG_DWO_LINE_SECTION,
22634 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22635 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22636 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22637 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22638 SECTION_DEBUG | SECTION_EXCLUDE,
22639 NULL);
22640 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22641 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22642 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22643 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22644 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22645 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22647 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22648 SECTION_DEBUG, NULL);
22649 debug_macinfo_section = get_section (dwarf_strict
22650 ? DEBUG_MACINFO_SECTION
22651 : DEBUG_MACRO_SECTION,
22652 DEBUG_MACRO_SECTION_FLAGS, NULL);
22653 debug_line_section = get_section (DEBUG_LINE_SECTION,
22654 SECTION_DEBUG, NULL);
22655 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22656 SECTION_DEBUG, NULL);
22657 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22658 SECTION_DEBUG, NULL);
22659 debug_str_section = get_section (DEBUG_STR_SECTION,
22660 DEBUG_STR_SECTION_FLAGS, NULL);
22661 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22662 SECTION_DEBUG, NULL);
22663 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22664 SECTION_DEBUG, NULL);
22666 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22667 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22668 DEBUG_ABBREV_SECTION_LABEL, 0);
22669 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22670 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22671 COLD_TEXT_SECTION_LABEL, 0);
22672 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22674 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22675 DEBUG_INFO_SECTION_LABEL, 0);
22676 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22677 DEBUG_LINE_SECTION_LABEL, 0);
22678 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22679 DEBUG_RANGES_SECTION_LABEL, 0);
22680 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22681 DEBUG_ADDR_SECTION_LABEL, 0);
22682 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22683 dwarf_strict
22684 ? DEBUG_MACINFO_SECTION_LABEL
22685 : DEBUG_MACRO_SECTION_LABEL, 0);
22686 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22688 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22689 vec_alloc (macinfo_table, 64);
22691 switch_to_section (text_section);
22692 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22694 /* Make sure the line number table for .text always exists. */
22695 text_section_line_info = new_line_info_table ();
22696 text_section_line_info->end_label = text_end_label;
22699 /* Called before compile () starts outputtting functions, variables
22700 and toplevel asms into assembly. */
22702 static void
22703 dwarf2out_assembly_start (void)
22705 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22706 && dwarf2out_do_cfi_asm ()
22707 && (!(flag_unwind_tables || flag_exceptions)
22708 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22709 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22712 /* A helper function for dwarf2out_finish called through
22713 htab_traverse. Assign a string its index. All strings must be
22714 collected into the table by the time index_string is called,
22715 because the indexing code relies on htab_traverse to traverse nodes
22716 in the same order for each run. */
22719 index_string (indirect_string_node **h, unsigned int *index)
22721 indirect_string_node *node = *h;
22723 find_string_form (node);
22724 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22726 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22727 node->index = *index;
22728 *index += 1;
22730 return 1;
22733 /* A helper function for output_indirect_strings called through
22734 htab_traverse. Output the offset to a string and update the
22735 current offset. */
22738 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22740 indirect_string_node *node = *h;
22742 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22744 /* Assert that this node has been assigned an index. */
22745 gcc_assert (node->index != NO_INDEX_ASSIGNED
22746 && node->index != NOT_INDEXED);
22747 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22748 "indexed string 0x%x: %s", node->index, node->str);
22749 *offset += strlen (node->str) + 1;
22751 return 1;
22754 /* A helper function for dwarf2out_finish called through
22755 htab_traverse. Output the indexed string. */
22758 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22760 struct indirect_string_node *node = *h;
22762 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22764 /* Assert that the strings are output in the same order as their
22765 indexes were assigned. */
22766 gcc_assert (*cur_idx == node->index);
22767 assemble_string (node->str, strlen (node->str) + 1);
22768 *cur_idx += 1;
22770 return 1;
22773 /* A helper function for dwarf2out_finish called through
22774 htab_traverse. Emit one queued .debug_str string. */
22777 output_indirect_string (indirect_string_node **h, void *)
22779 struct indirect_string_node *node = *h;
22781 node->form = find_string_form (node);
22782 if (node->form == DW_FORM_strp && node->refcount > 0)
22784 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22785 assemble_string (node->str, strlen (node->str) + 1);
22788 return 1;
22791 /* Output the indexed string table. */
22793 static void
22794 output_indirect_strings (void)
22796 switch_to_section (debug_str_section);
22797 if (!dwarf_split_debug_info)
22798 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22799 else
22801 unsigned int offset = 0;
22802 unsigned int cur_idx = 0;
22804 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22806 switch_to_section (debug_str_offsets_section);
22807 debug_str_hash->traverse_noresize
22808 <unsigned int *, output_index_string_offset> (&offset);
22809 switch_to_section (debug_str_dwo_section);
22810 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22811 (&cur_idx);
22815 /* Callback for htab_traverse to assign an index to an entry in the
22816 table, and to write that entry to the .debug_addr section. */
22819 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22821 addr_table_entry *entry = *slot;
22823 if (entry->refcount == 0)
22825 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22826 || entry->index == NOT_INDEXED);
22827 return 1;
22830 gcc_assert (entry->index == *cur_index);
22831 (*cur_index)++;
22833 switch (entry->kind)
22835 case ate_kind_rtx:
22836 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22837 "0x%x", entry->index);
22838 break;
22839 case ate_kind_rtx_dtprel:
22840 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22841 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22842 DWARF2_ADDR_SIZE,
22843 entry->addr.rtl);
22844 fputc ('\n', asm_out_file);
22845 break;
22846 case ate_kind_label:
22847 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22848 "0x%x", entry->index);
22849 break;
22850 default:
22851 gcc_unreachable ();
22853 return 1;
22856 /* Produce the .debug_addr section. */
22858 static void
22859 output_addr_table (void)
22861 unsigned int index = 0;
22862 if (addr_index_table == NULL || addr_index_table->size () == 0)
22863 return;
22865 switch_to_section (debug_addr_section);
22866 addr_index_table
22867 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22870 #if ENABLE_ASSERT_CHECKING
22871 /* Verify that all marks are clear. */
22873 static void
22874 verify_marks_clear (dw_die_ref die)
22876 dw_die_ref c;
22878 gcc_assert (! die->die_mark);
22879 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22881 #endif /* ENABLE_ASSERT_CHECKING */
22883 /* Clear the marks for a die and its children.
22884 Be cool if the mark isn't set. */
22886 static void
22887 prune_unmark_dies (dw_die_ref die)
22889 dw_die_ref c;
22891 if (die->die_mark)
22892 die->die_mark = 0;
22893 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22896 /* Given DIE that we're marking as used, find any other dies
22897 it references as attributes and mark them as used. */
22899 static void
22900 prune_unused_types_walk_attribs (dw_die_ref die)
22902 dw_attr_ref a;
22903 unsigned ix;
22905 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22907 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22909 /* A reference to another DIE.
22910 Make sure that it will get emitted.
22911 If it was broken out into a comdat group, don't follow it. */
22912 if (! AT_ref (a)->comdat_type_p
22913 || a->dw_attr == DW_AT_specification)
22914 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22916 /* Set the string's refcount to 0 so that prune_unused_types_mark
22917 accounts properly for it. */
22918 if (AT_class (a) == dw_val_class_str)
22919 a->dw_attr_val.v.val_str->refcount = 0;
22923 /* Mark the generic parameters and arguments children DIEs of DIE. */
22925 static void
22926 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22928 dw_die_ref c;
22930 if (die == NULL || die->die_child == NULL)
22931 return;
22932 c = die->die_child;
22935 if (is_template_parameter (c))
22936 prune_unused_types_mark (c, 1);
22937 c = c->die_sib;
22938 } while (c && c != die->die_child);
22941 /* Mark DIE as being used. If DOKIDS is true, then walk down
22942 to DIE's children. */
22944 static void
22945 prune_unused_types_mark (dw_die_ref die, int dokids)
22947 dw_die_ref c;
22949 if (die->die_mark == 0)
22951 /* We haven't done this node yet. Mark it as used. */
22952 die->die_mark = 1;
22953 /* If this is the DIE of a generic type instantiation,
22954 mark the children DIEs that describe its generic parms and
22955 args. */
22956 prune_unused_types_mark_generic_parms_dies (die);
22958 /* We also have to mark its parents as used.
22959 (But we don't want to mark our parent's kids due to this,
22960 unless it is a class.) */
22961 if (die->die_parent)
22962 prune_unused_types_mark (die->die_parent,
22963 class_scope_p (die->die_parent));
22965 /* Mark any referenced nodes. */
22966 prune_unused_types_walk_attribs (die);
22968 /* If this node is a specification,
22969 also mark the definition, if it exists. */
22970 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22971 prune_unused_types_mark (die->die_definition, 1);
22974 if (dokids && die->die_mark != 2)
22976 /* We need to walk the children, but haven't done so yet.
22977 Remember that we've walked the kids. */
22978 die->die_mark = 2;
22980 /* If this is an array type, we need to make sure our
22981 kids get marked, even if they're types. If we're
22982 breaking out types into comdat sections, do this
22983 for all type definitions. */
22984 if (die->die_tag == DW_TAG_array_type
22985 || (use_debug_types
22986 && is_type_die (die) && ! is_declaration_die (die)))
22987 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22988 else
22989 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22993 /* For local classes, look if any static member functions were emitted
22994 and if so, mark them. */
22996 static void
22997 prune_unused_types_walk_local_classes (dw_die_ref die)
22999 dw_die_ref c;
23001 if (die->die_mark == 2)
23002 return;
23004 switch (die->die_tag)
23006 case DW_TAG_structure_type:
23007 case DW_TAG_union_type:
23008 case DW_TAG_class_type:
23009 break;
23011 case DW_TAG_subprogram:
23012 if (!get_AT_flag (die, DW_AT_declaration)
23013 || die->die_definition != NULL)
23014 prune_unused_types_mark (die, 1);
23015 return;
23017 default:
23018 return;
23021 /* Mark children. */
23022 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23025 /* Walk the tree DIE and mark types that we actually use. */
23027 static void
23028 prune_unused_types_walk (dw_die_ref die)
23030 dw_die_ref c;
23032 /* Don't do anything if this node is already marked and
23033 children have been marked as well. */
23034 if (die->die_mark == 2)
23035 return;
23037 switch (die->die_tag)
23039 case DW_TAG_structure_type:
23040 case DW_TAG_union_type:
23041 case DW_TAG_class_type:
23042 if (die->die_perennial_p)
23043 break;
23045 for (c = die->die_parent; c; c = c->die_parent)
23046 if (c->die_tag == DW_TAG_subprogram)
23047 break;
23049 /* Finding used static member functions inside of classes
23050 is needed just for local classes, because for other classes
23051 static member function DIEs with DW_AT_specification
23052 are emitted outside of the DW_TAG_*_type. If we ever change
23053 it, we'd need to call this even for non-local classes. */
23054 if (c)
23055 prune_unused_types_walk_local_classes (die);
23057 /* It's a type node --- don't mark it. */
23058 return;
23060 case DW_TAG_const_type:
23061 case DW_TAG_packed_type:
23062 case DW_TAG_pointer_type:
23063 case DW_TAG_reference_type:
23064 case DW_TAG_rvalue_reference_type:
23065 case DW_TAG_volatile_type:
23066 case DW_TAG_typedef:
23067 case DW_TAG_array_type:
23068 case DW_TAG_interface_type:
23069 case DW_TAG_friend:
23070 case DW_TAG_variant_part:
23071 case DW_TAG_enumeration_type:
23072 case DW_TAG_subroutine_type:
23073 case DW_TAG_string_type:
23074 case DW_TAG_set_type:
23075 case DW_TAG_subrange_type:
23076 case DW_TAG_ptr_to_member_type:
23077 case DW_TAG_file_type:
23078 if (die->die_perennial_p)
23079 break;
23081 /* It's a type node --- don't mark it. */
23082 return;
23084 default:
23085 /* Mark everything else. */
23086 break;
23089 if (die->die_mark == 0)
23091 die->die_mark = 1;
23093 /* Now, mark any dies referenced from here. */
23094 prune_unused_types_walk_attribs (die);
23097 die->die_mark = 2;
23099 /* Mark children. */
23100 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23103 /* Increment the string counts on strings referred to from DIE's
23104 attributes. */
23106 static void
23107 prune_unused_types_update_strings (dw_die_ref die)
23109 dw_attr_ref a;
23110 unsigned ix;
23112 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23113 if (AT_class (a) == dw_val_class_str)
23115 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23116 s->refcount++;
23117 /* Avoid unnecessarily putting strings that are used less than
23118 twice in the hash table. */
23119 if (s->refcount
23120 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23122 indirect_string_node **slot
23123 = debug_str_hash->find_slot_with_hash (s->str,
23124 htab_hash_string (s->str),
23125 INSERT);
23126 gcc_assert (*slot == NULL);
23127 *slot = s;
23132 /* Remove from the tree DIE any dies that aren't marked. */
23134 static void
23135 prune_unused_types_prune (dw_die_ref die)
23137 dw_die_ref c;
23139 gcc_assert (die->die_mark);
23140 prune_unused_types_update_strings (die);
23142 if (! die->die_child)
23143 return;
23145 c = die->die_child;
23146 do {
23147 dw_die_ref prev = c;
23148 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23149 if (c == die->die_child)
23151 /* No marked children between 'prev' and the end of the list. */
23152 if (prev == c)
23153 /* No marked children at all. */
23154 die->die_child = NULL;
23155 else
23157 prev->die_sib = c->die_sib;
23158 die->die_child = prev;
23160 return;
23163 if (c != prev->die_sib)
23164 prev->die_sib = c;
23165 prune_unused_types_prune (c);
23166 } while (c != die->die_child);
23169 /* Remove dies representing declarations that we never use. */
23171 static void
23172 prune_unused_types (void)
23174 unsigned int i;
23175 limbo_die_node *node;
23176 comdat_type_node *ctnode;
23177 pubname_ref pub;
23178 dw_die_ref base_type;
23180 #if ENABLE_ASSERT_CHECKING
23181 /* All the marks should already be clear. */
23182 verify_marks_clear (comp_unit_die ());
23183 for (node = limbo_die_list; node; node = node->next)
23184 verify_marks_clear (node->die);
23185 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23186 verify_marks_clear (ctnode->root_die);
23187 #endif /* ENABLE_ASSERT_CHECKING */
23189 /* Mark types that are used in global variables. */
23190 premark_types_used_by_global_vars ();
23192 /* Set the mark on nodes that are actually used. */
23193 prune_unused_types_walk (comp_unit_die ());
23194 for (node = limbo_die_list; node; node = node->next)
23195 prune_unused_types_walk (node->die);
23196 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23198 prune_unused_types_walk (ctnode->root_die);
23199 prune_unused_types_mark (ctnode->type_die, 1);
23202 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23203 are unusual in that they are pubnames that are the children of pubtypes.
23204 They should only be marked via their parent DW_TAG_enumeration_type die,
23205 not as roots in themselves. */
23206 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23207 if (pub->die->die_tag != DW_TAG_enumerator)
23208 prune_unused_types_mark (pub->die, 1);
23209 for (i = 0; base_types.iterate (i, &base_type); i++)
23210 prune_unused_types_mark (base_type, 1);
23212 if (debug_str_hash)
23213 debug_str_hash->empty ();
23214 if (skeleton_debug_str_hash)
23215 skeleton_debug_str_hash->empty ();
23216 prune_unused_types_prune (comp_unit_die ());
23217 for (node = limbo_die_list; node; node = node->next)
23218 prune_unused_types_prune (node->die);
23219 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23220 prune_unused_types_prune (ctnode->root_die);
23222 /* Leave the marks clear. */
23223 prune_unmark_dies (comp_unit_die ());
23224 for (node = limbo_die_list; node; node = node->next)
23225 prune_unmark_dies (node->die);
23226 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23227 prune_unmark_dies (ctnode->root_die);
23230 /* Set the parameter to true if there are any relative pathnames in
23231 the file table. */
23233 file_table_relative_p (dwarf_file_data **slot, bool *p)
23235 struct dwarf_file_data *d = *slot;
23236 if (!IS_ABSOLUTE_PATH (d->filename))
23238 *p = true;
23239 return 0;
23241 return 1;
23244 /* Helpers to manipulate hash table of comdat type units. */
23246 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23248 typedef comdat_type_node value_type;
23249 typedef comdat_type_node compare_type;
23250 static inline hashval_t hash (const value_type *);
23251 static inline bool equal (const value_type *, const compare_type *);
23254 inline hashval_t
23255 comdat_type_hasher::hash (const value_type *type_node)
23257 hashval_t h;
23258 memcpy (&h, type_node->signature, sizeof (h));
23259 return h;
23262 inline bool
23263 comdat_type_hasher::equal (const value_type *type_node_1,
23264 const compare_type *type_node_2)
23266 return (! memcmp (type_node_1->signature, type_node_2->signature,
23267 DWARF_TYPE_SIGNATURE_SIZE));
23270 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23271 to the location it would have been added, should we know its
23272 DECL_ASSEMBLER_NAME when we added other attributes. This will
23273 probably improve compactness of debug info, removing equivalent
23274 abbrevs, and hide any differences caused by deferring the
23275 computation of the assembler name, triggered by e.g. PCH. */
23277 static inline void
23278 move_linkage_attr (dw_die_ref die)
23280 unsigned ix = vec_safe_length (die->die_attr);
23281 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23283 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23284 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23286 while (--ix > 0)
23288 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23290 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23291 break;
23294 if (ix != vec_safe_length (die->die_attr) - 1)
23296 die->die_attr->pop ();
23297 die->die_attr->quick_insert (ix, linkage);
23301 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23302 referenced from typed stack ops and count how often they are used. */
23304 static void
23305 mark_base_types (dw_loc_descr_ref loc)
23307 dw_die_ref base_type = NULL;
23309 for (; loc; loc = loc->dw_loc_next)
23311 switch (loc->dw_loc_opc)
23313 case DW_OP_GNU_regval_type:
23314 case DW_OP_GNU_deref_type:
23315 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23316 break;
23317 case DW_OP_GNU_convert:
23318 case DW_OP_GNU_reinterpret:
23319 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23320 continue;
23321 /* FALLTHRU */
23322 case DW_OP_GNU_const_type:
23323 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23324 break;
23325 case DW_OP_GNU_entry_value:
23326 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23327 continue;
23328 default:
23329 continue;
23331 gcc_assert (base_type->die_parent == comp_unit_die ());
23332 if (base_type->die_mark)
23333 base_type->die_mark++;
23334 else
23336 base_types.safe_push (base_type);
23337 base_type->die_mark = 1;
23342 /* Comparison function for sorting marked base types. */
23344 static int
23345 base_type_cmp (const void *x, const void *y)
23347 dw_die_ref dx = *(const dw_die_ref *) x;
23348 dw_die_ref dy = *(const dw_die_ref *) y;
23349 unsigned int byte_size1, byte_size2;
23350 unsigned int encoding1, encoding2;
23351 if (dx->die_mark > dy->die_mark)
23352 return -1;
23353 if (dx->die_mark < dy->die_mark)
23354 return 1;
23355 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23356 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23357 if (byte_size1 < byte_size2)
23358 return 1;
23359 if (byte_size1 > byte_size2)
23360 return -1;
23361 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23362 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23363 if (encoding1 < encoding2)
23364 return 1;
23365 if (encoding1 > encoding2)
23366 return -1;
23367 return 0;
23370 /* Move base types marked by mark_base_types as early as possible
23371 in the CU, sorted by decreasing usage count both to make the
23372 uleb128 references as small as possible and to make sure they
23373 will have die_offset already computed by calc_die_sizes when
23374 sizes of typed stack loc ops is computed. */
23376 static void
23377 move_marked_base_types (void)
23379 unsigned int i;
23380 dw_die_ref base_type, die, c;
23382 if (base_types.is_empty ())
23383 return;
23385 /* Sort by decreasing usage count, they will be added again in that
23386 order later on. */
23387 base_types.qsort (base_type_cmp);
23388 die = comp_unit_die ();
23389 c = die->die_child;
23392 dw_die_ref prev = c;
23393 c = c->die_sib;
23394 while (c->die_mark)
23396 remove_child_with_prev (c, prev);
23397 /* As base types got marked, there must be at least
23398 one node other than DW_TAG_base_type. */
23399 gcc_assert (c != c->die_sib);
23400 c = c->die_sib;
23403 while (c != die->die_child);
23404 gcc_assert (die->die_child);
23405 c = die->die_child;
23406 for (i = 0; base_types.iterate (i, &base_type); i++)
23408 base_type->die_mark = 0;
23409 base_type->die_sib = c->die_sib;
23410 c->die_sib = base_type;
23411 c = base_type;
23415 /* Helper function for resolve_addr, attempt to resolve
23416 one CONST_STRING, return true if successful. Similarly verify that
23417 SYMBOL_REFs refer to variables emitted in the current CU. */
23419 static bool
23420 resolve_one_addr (rtx *addr)
23422 rtx rtl = *addr;
23424 if (GET_CODE (rtl) == CONST_STRING)
23426 size_t len = strlen (XSTR (rtl, 0)) + 1;
23427 tree t = build_string (len, XSTR (rtl, 0));
23428 tree tlen = size_int (len - 1);
23429 TREE_TYPE (t)
23430 = build_array_type (char_type_node, build_index_type (tlen));
23431 rtl = lookup_constant_def (t);
23432 if (!rtl || !MEM_P (rtl))
23433 return false;
23434 rtl = XEXP (rtl, 0);
23435 if (GET_CODE (rtl) == SYMBOL_REF
23436 && SYMBOL_REF_DECL (rtl)
23437 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23438 return false;
23439 vec_safe_push (used_rtx_array, rtl);
23440 *addr = rtl;
23441 return true;
23444 if (GET_CODE (rtl) == SYMBOL_REF
23445 && SYMBOL_REF_DECL (rtl))
23447 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23449 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23450 return false;
23452 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23453 return false;
23456 if (GET_CODE (rtl) == CONST)
23458 subrtx_ptr_iterator::array_type array;
23459 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23460 if (!resolve_one_addr (*iter))
23461 return false;
23464 return true;
23467 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23468 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23469 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23471 static rtx
23472 string_cst_pool_decl (tree t)
23474 rtx rtl = output_constant_def (t, 1);
23475 unsigned char *array;
23476 dw_loc_descr_ref l;
23477 tree decl;
23478 size_t len;
23479 dw_die_ref ref;
23481 if (!rtl || !MEM_P (rtl))
23482 return NULL_RTX;
23483 rtl = XEXP (rtl, 0);
23484 if (GET_CODE (rtl) != SYMBOL_REF
23485 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23486 return NULL_RTX;
23488 decl = SYMBOL_REF_DECL (rtl);
23489 if (!lookup_decl_die (decl))
23491 len = TREE_STRING_LENGTH (t);
23492 vec_safe_push (used_rtx_array, rtl);
23493 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23494 array = ggc_vec_alloc<unsigned char> (len);
23495 memcpy (array, TREE_STRING_POINTER (t), len);
23496 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23497 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23498 l->dw_loc_oprnd2.v.val_vec.length = len;
23499 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23500 l->dw_loc_oprnd2.v.val_vec.array = array;
23501 add_AT_loc (ref, DW_AT_location, l);
23502 equate_decl_number_to_die (decl, ref);
23504 return rtl;
23507 /* Helper function of resolve_addr_in_expr. LOC is
23508 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23509 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23510 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23511 with DW_OP_GNU_implicit_pointer if possible
23512 and return true, if unsuccessful, return false. */
23514 static bool
23515 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23517 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23518 HOST_WIDE_INT offset = 0;
23519 dw_die_ref ref = NULL;
23520 tree decl;
23522 if (GET_CODE (rtl) == CONST
23523 && GET_CODE (XEXP (rtl, 0)) == PLUS
23524 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23526 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23527 rtl = XEXP (XEXP (rtl, 0), 0);
23529 if (GET_CODE (rtl) == CONST_STRING)
23531 size_t len = strlen (XSTR (rtl, 0)) + 1;
23532 tree t = build_string (len, XSTR (rtl, 0));
23533 tree tlen = size_int (len - 1);
23535 TREE_TYPE (t)
23536 = build_array_type (char_type_node, build_index_type (tlen));
23537 rtl = string_cst_pool_decl (t);
23538 if (!rtl)
23539 return false;
23541 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23543 decl = SYMBOL_REF_DECL (rtl);
23544 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23546 ref = lookup_decl_die (decl);
23547 if (ref && (get_AT (ref, DW_AT_location)
23548 || get_AT (ref, DW_AT_const_value)))
23550 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23551 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23552 loc->dw_loc_oprnd1.val_entry = NULL;
23553 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23554 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23555 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23556 loc->dw_loc_oprnd2.v.val_int = offset;
23557 return true;
23561 return false;
23564 /* Helper function for resolve_addr, handle one location
23565 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23566 the location list couldn't be resolved. */
23568 static bool
23569 resolve_addr_in_expr (dw_loc_descr_ref loc)
23571 dw_loc_descr_ref keep = NULL;
23572 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23573 switch (loc->dw_loc_opc)
23575 case DW_OP_addr:
23576 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23578 if ((prev == NULL
23579 || prev->dw_loc_opc == DW_OP_piece
23580 || prev->dw_loc_opc == DW_OP_bit_piece)
23581 && loc->dw_loc_next
23582 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23583 && !dwarf_strict
23584 && optimize_one_addr_into_implicit_ptr (loc))
23585 break;
23586 return false;
23588 break;
23589 case DW_OP_GNU_addr_index:
23590 case DW_OP_GNU_const_index:
23591 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23592 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23594 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23595 if (!resolve_one_addr (&rtl))
23596 return false;
23597 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23598 loc->dw_loc_oprnd1.val_entry =
23599 add_addr_table_entry (rtl, ate_kind_rtx);
23601 break;
23602 case DW_OP_const4u:
23603 case DW_OP_const8u:
23604 if (loc->dtprel
23605 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23606 return false;
23607 break;
23608 case DW_OP_plus_uconst:
23609 if (size_of_loc_descr (loc)
23610 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23612 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23614 dw_loc_descr_ref repl
23615 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23616 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23617 add_loc_descr (&repl, loc->dw_loc_next);
23618 *loc = *repl;
23620 break;
23621 case DW_OP_implicit_value:
23622 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23623 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23624 return false;
23625 break;
23626 case DW_OP_GNU_implicit_pointer:
23627 case DW_OP_GNU_parameter_ref:
23628 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23630 dw_die_ref ref
23631 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23632 if (ref == NULL)
23633 return false;
23634 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23635 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23636 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23638 break;
23639 case DW_OP_GNU_const_type:
23640 case DW_OP_GNU_regval_type:
23641 case DW_OP_GNU_deref_type:
23642 case DW_OP_GNU_convert:
23643 case DW_OP_GNU_reinterpret:
23644 while (loc->dw_loc_next
23645 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23647 dw_die_ref base1, base2;
23648 unsigned enc1, enc2, size1, size2;
23649 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23650 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23651 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23652 else if (loc->dw_loc_oprnd1.val_class
23653 == dw_val_class_unsigned_const)
23654 break;
23655 else
23656 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23657 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23658 == dw_val_class_unsigned_const)
23659 break;
23660 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23661 gcc_assert (base1->die_tag == DW_TAG_base_type
23662 && base2->die_tag == DW_TAG_base_type);
23663 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23664 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23665 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23666 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23667 if (size1 == size2
23668 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23669 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23670 && loc != keep)
23671 || enc1 == enc2))
23673 /* Optimize away next DW_OP_GNU_convert after
23674 adjusting LOC's base type die reference. */
23675 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23676 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23677 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23678 else
23679 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23680 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23681 continue;
23683 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23684 point typed stack entry. */
23685 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23686 keep = loc->dw_loc_next;
23687 break;
23689 break;
23690 default:
23691 break;
23693 return true;
23696 /* Helper function of resolve_addr. DIE had DW_AT_location of
23697 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23698 and DW_OP_addr couldn't be resolved. resolve_addr has already
23699 removed the DW_AT_location attribute. This function attempts to
23700 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23701 to it or DW_AT_const_value attribute, if possible. */
23703 static void
23704 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23706 if (TREE_CODE (decl) != VAR_DECL
23707 || lookup_decl_die (decl) != die
23708 || DECL_EXTERNAL (decl)
23709 || !TREE_STATIC (decl)
23710 || DECL_INITIAL (decl) == NULL_TREE
23711 || DECL_P (DECL_INITIAL (decl))
23712 || get_AT (die, DW_AT_const_value))
23713 return;
23715 tree init = DECL_INITIAL (decl);
23716 HOST_WIDE_INT offset = 0;
23717 /* For variables that have been optimized away and thus
23718 don't have a memory location, see if we can emit
23719 DW_AT_const_value instead. */
23720 if (tree_add_const_value_attribute (die, init))
23721 return;
23722 if (dwarf_strict)
23723 return;
23724 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23725 and ADDR_EXPR refers to a decl that has DW_AT_location or
23726 DW_AT_const_value (but isn't addressable, otherwise
23727 resolving the original DW_OP_addr wouldn't fail), see if
23728 we can add DW_OP_GNU_implicit_pointer. */
23729 STRIP_NOPS (init);
23730 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23731 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23733 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23734 init = TREE_OPERAND (init, 0);
23735 STRIP_NOPS (init);
23737 if (TREE_CODE (init) != ADDR_EXPR)
23738 return;
23739 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23740 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23741 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23742 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23743 && TREE_OPERAND (init, 0) != decl))
23745 dw_die_ref ref;
23746 dw_loc_descr_ref l;
23748 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23750 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23751 if (!rtl)
23752 return;
23753 decl = SYMBOL_REF_DECL (rtl);
23755 else
23756 decl = TREE_OPERAND (init, 0);
23757 ref = lookup_decl_die (decl);
23758 if (ref == NULL
23759 || (!get_AT (ref, DW_AT_location)
23760 && !get_AT (ref, DW_AT_const_value)))
23761 return;
23762 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23763 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23764 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23765 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23766 add_AT_loc (die, DW_AT_location, l);
23770 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23771 an address in .rodata section if the string literal is emitted there,
23772 or remove the containing location list or replace DW_AT_const_value
23773 with DW_AT_location and empty location expression, if it isn't found
23774 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23775 to something that has been emitted in the current CU. */
23777 static void
23778 resolve_addr (dw_die_ref die)
23780 dw_die_ref c;
23781 dw_attr_ref a;
23782 dw_loc_list_ref *curr, *start, loc;
23783 unsigned ix;
23785 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23786 switch (AT_class (a))
23788 case dw_val_class_loc_list:
23789 start = curr = AT_loc_list_ptr (a);
23790 loc = *curr;
23791 gcc_assert (loc);
23792 /* The same list can be referenced more than once. See if we have
23793 already recorded the result from a previous pass. */
23794 if (loc->replaced)
23795 *curr = loc->dw_loc_next;
23796 else if (!loc->resolved_addr)
23798 /* As things stand, we do not expect or allow one die to
23799 reference a suffix of another die's location list chain.
23800 References must be identical or completely separate.
23801 There is therefore no need to cache the result of this
23802 pass on any list other than the first; doing so
23803 would lead to unnecessary writes. */
23804 while (*curr)
23806 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23807 if (!resolve_addr_in_expr ((*curr)->expr))
23809 dw_loc_list_ref next = (*curr)->dw_loc_next;
23810 dw_loc_descr_ref l = (*curr)->expr;
23812 if (next && (*curr)->ll_symbol)
23814 gcc_assert (!next->ll_symbol);
23815 next->ll_symbol = (*curr)->ll_symbol;
23817 if (dwarf_split_debug_info)
23818 remove_loc_list_addr_table_entries (l);
23819 *curr = next;
23821 else
23823 mark_base_types ((*curr)->expr);
23824 curr = &(*curr)->dw_loc_next;
23827 if (loc == *start)
23828 loc->resolved_addr = 1;
23829 else
23831 loc->replaced = 1;
23832 loc->dw_loc_next = *start;
23835 if (!*start)
23837 remove_AT (die, a->dw_attr);
23838 ix--;
23840 break;
23841 case dw_val_class_loc:
23843 dw_loc_descr_ref l = AT_loc (a);
23844 /* For -gdwarf-2 don't attempt to optimize
23845 DW_AT_data_member_location containing
23846 DW_OP_plus_uconst - older consumers might
23847 rely on it being that op instead of a more complex,
23848 but shorter, location description. */
23849 if ((dwarf_version > 2
23850 || a->dw_attr != DW_AT_data_member_location
23851 || l == NULL
23852 || l->dw_loc_opc != DW_OP_plus_uconst
23853 || l->dw_loc_next != NULL)
23854 && !resolve_addr_in_expr (l))
23856 if (dwarf_split_debug_info)
23857 remove_loc_list_addr_table_entries (l);
23858 if (l != NULL
23859 && l->dw_loc_next == NULL
23860 && l->dw_loc_opc == DW_OP_addr
23861 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23862 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23863 && a->dw_attr == DW_AT_location)
23865 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23866 remove_AT (die, a->dw_attr);
23867 ix--;
23868 optimize_location_into_implicit_ptr (die, decl);
23869 break;
23871 remove_AT (die, a->dw_attr);
23872 ix--;
23874 else
23875 mark_base_types (l);
23877 break;
23878 case dw_val_class_addr:
23879 if (a->dw_attr == DW_AT_const_value
23880 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23882 if (AT_index (a) != NOT_INDEXED)
23883 remove_addr_table_entry (a->dw_attr_val.val_entry);
23884 remove_AT (die, a->dw_attr);
23885 ix--;
23887 if (die->die_tag == DW_TAG_GNU_call_site
23888 && a->dw_attr == DW_AT_abstract_origin)
23890 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23891 dw_die_ref tdie = lookup_decl_die (tdecl);
23892 if (tdie == NULL
23893 && DECL_EXTERNAL (tdecl)
23894 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23896 force_decl_die (tdecl);
23897 tdie = lookup_decl_die (tdecl);
23899 if (tdie)
23901 a->dw_attr_val.val_class = dw_val_class_die_ref;
23902 a->dw_attr_val.v.val_die_ref.die = tdie;
23903 a->dw_attr_val.v.val_die_ref.external = 0;
23905 else
23907 if (AT_index (a) != NOT_INDEXED)
23908 remove_addr_table_entry (a->dw_attr_val.val_entry);
23909 remove_AT (die, a->dw_attr);
23910 ix--;
23913 break;
23914 default:
23915 break;
23918 FOR_EACH_CHILD (die, c, resolve_addr (c));
23921 /* Helper routines for optimize_location_lists.
23922 This pass tries to share identical local lists in .debug_loc
23923 section. */
23925 /* Iteratively hash operands of LOC opcode into HSTATE. */
23927 static void
23928 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23930 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23931 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23933 switch (loc->dw_loc_opc)
23935 case DW_OP_const4u:
23936 case DW_OP_const8u:
23937 if (loc->dtprel)
23938 goto hash_addr;
23939 /* FALLTHRU */
23940 case DW_OP_const1u:
23941 case DW_OP_const1s:
23942 case DW_OP_const2u:
23943 case DW_OP_const2s:
23944 case DW_OP_const4s:
23945 case DW_OP_const8s:
23946 case DW_OP_constu:
23947 case DW_OP_consts:
23948 case DW_OP_pick:
23949 case DW_OP_plus_uconst:
23950 case DW_OP_breg0:
23951 case DW_OP_breg1:
23952 case DW_OP_breg2:
23953 case DW_OP_breg3:
23954 case DW_OP_breg4:
23955 case DW_OP_breg5:
23956 case DW_OP_breg6:
23957 case DW_OP_breg7:
23958 case DW_OP_breg8:
23959 case DW_OP_breg9:
23960 case DW_OP_breg10:
23961 case DW_OP_breg11:
23962 case DW_OP_breg12:
23963 case DW_OP_breg13:
23964 case DW_OP_breg14:
23965 case DW_OP_breg15:
23966 case DW_OP_breg16:
23967 case DW_OP_breg17:
23968 case DW_OP_breg18:
23969 case DW_OP_breg19:
23970 case DW_OP_breg20:
23971 case DW_OP_breg21:
23972 case DW_OP_breg22:
23973 case DW_OP_breg23:
23974 case DW_OP_breg24:
23975 case DW_OP_breg25:
23976 case DW_OP_breg26:
23977 case DW_OP_breg27:
23978 case DW_OP_breg28:
23979 case DW_OP_breg29:
23980 case DW_OP_breg30:
23981 case DW_OP_breg31:
23982 case DW_OP_regx:
23983 case DW_OP_fbreg:
23984 case DW_OP_piece:
23985 case DW_OP_deref_size:
23986 case DW_OP_xderef_size:
23987 hstate.add_object (val1->v.val_int);
23988 break;
23989 case DW_OP_skip:
23990 case DW_OP_bra:
23992 int offset;
23994 gcc_assert (val1->val_class == dw_val_class_loc);
23995 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23996 hstate.add_object (offset);
23998 break;
23999 case DW_OP_implicit_value:
24000 hstate.add_object (val1->v.val_unsigned);
24001 switch (val2->val_class)
24003 case dw_val_class_const:
24004 hstate.add_object (val2->v.val_int);
24005 break;
24006 case dw_val_class_vec:
24008 unsigned int elt_size = val2->v.val_vec.elt_size;
24009 unsigned int len = val2->v.val_vec.length;
24011 hstate.add_int (elt_size);
24012 hstate.add_int (len);
24013 hstate.add (val2->v.val_vec.array, len * elt_size);
24015 break;
24016 case dw_val_class_const_double:
24017 hstate.add_object (val2->v.val_double.low);
24018 hstate.add_object (val2->v.val_double.high);
24019 break;
24020 case dw_val_class_wide_int:
24021 hstate.add_object (*val2->v.val_wide);
24022 break;
24023 case dw_val_class_addr:
24024 inchash::add_rtx (val2->v.val_addr, hstate);
24025 break;
24026 default:
24027 gcc_unreachable ();
24029 break;
24030 case DW_OP_bregx:
24031 case DW_OP_bit_piece:
24032 hstate.add_object (val1->v.val_int);
24033 hstate.add_object (val2->v.val_int);
24034 break;
24035 case DW_OP_addr:
24036 hash_addr:
24037 if (loc->dtprel)
24039 unsigned char dtprel = 0xd1;
24040 hstate.add_object (dtprel);
24042 inchash::add_rtx (val1->v.val_addr, hstate);
24043 break;
24044 case DW_OP_GNU_addr_index:
24045 case DW_OP_GNU_const_index:
24047 if (loc->dtprel)
24049 unsigned char dtprel = 0xd1;
24050 hstate.add_object (dtprel);
24052 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24054 break;
24055 case DW_OP_GNU_implicit_pointer:
24056 hstate.add_int (val2->v.val_int);
24057 break;
24058 case DW_OP_GNU_entry_value:
24059 hstate.add_object (val1->v.val_loc);
24060 break;
24061 case DW_OP_GNU_regval_type:
24062 case DW_OP_GNU_deref_type:
24064 unsigned int byte_size
24065 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24066 unsigned int encoding
24067 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24068 hstate.add_object (val1->v.val_int);
24069 hstate.add_object (byte_size);
24070 hstate.add_object (encoding);
24072 break;
24073 case DW_OP_GNU_convert:
24074 case DW_OP_GNU_reinterpret:
24075 if (val1->val_class == dw_val_class_unsigned_const)
24077 hstate.add_object (val1->v.val_unsigned);
24078 break;
24080 /* FALLTHRU */
24081 case DW_OP_GNU_const_type:
24083 unsigned int byte_size
24084 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24085 unsigned int encoding
24086 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24087 hstate.add_object (byte_size);
24088 hstate.add_object (encoding);
24089 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24090 break;
24091 hstate.add_object (val2->val_class);
24092 switch (val2->val_class)
24094 case dw_val_class_const:
24095 hstate.add_object (val2->v.val_int);
24096 break;
24097 case dw_val_class_vec:
24099 unsigned int elt_size = val2->v.val_vec.elt_size;
24100 unsigned int len = val2->v.val_vec.length;
24102 hstate.add_object (elt_size);
24103 hstate.add_object (len);
24104 hstate.add (val2->v.val_vec.array, len * elt_size);
24106 break;
24107 case dw_val_class_const_double:
24108 hstate.add_object (val2->v.val_double.low);
24109 hstate.add_object (val2->v.val_double.high);
24110 break;
24111 case dw_val_class_wide_int:
24112 hstate.add_object (*val2->v.val_wide);
24113 break;
24114 default:
24115 gcc_unreachable ();
24118 break;
24120 default:
24121 /* Other codes have no operands. */
24122 break;
24126 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24128 static inline void
24129 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24131 dw_loc_descr_ref l;
24132 bool sizes_computed = false;
24133 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24134 size_of_locs (loc);
24136 for (l = loc; l != NULL; l = l->dw_loc_next)
24138 enum dwarf_location_atom opc = l->dw_loc_opc;
24139 hstate.add_object (opc);
24140 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24142 size_of_locs (loc);
24143 sizes_computed = true;
24145 hash_loc_operands (l, hstate);
24149 /* Compute hash of the whole location list LIST_HEAD. */
24151 static inline void
24152 hash_loc_list (dw_loc_list_ref list_head)
24154 dw_loc_list_ref curr = list_head;
24155 inchash::hash hstate;
24157 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24159 hstate.add (curr->begin, strlen (curr->begin) + 1);
24160 hstate.add (curr->end, strlen (curr->end) + 1);
24161 if (curr->section)
24162 hstate.add (curr->section, strlen (curr->section) + 1);
24163 hash_locs (curr->expr, hstate);
24165 list_head->hash = hstate.end ();
24168 /* Return true if X and Y opcodes have the same operands. */
24170 static inline bool
24171 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24173 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24174 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24175 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24176 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24178 switch (x->dw_loc_opc)
24180 case DW_OP_const4u:
24181 case DW_OP_const8u:
24182 if (x->dtprel)
24183 goto hash_addr;
24184 /* FALLTHRU */
24185 case DW_OP_const1u:
24186 case DW_OP_const1s:
24187 case DW_OP_const2u:
24188 case DW_OP_const2s:
24189 case DW_OP_const4s:
24190 case DW_OP_const8s:
24191 case DW_OP_constu:
24192 case DW_OP_consts:
24193 case DW_OP_pick:
24194 case DW_OP_plus_uconst:
24195 case DW_OP_breg0:
24196 case DW_OP_breg1:
24197 case DW_OP_breg2:
24198 case DW_OP_breg3:
24199 case DW_OP_breg4:
24200 case DW_OP_breg5:
24201 case DW_OP_breg6:
24202 case DW_OP_breg7:
24203 case DW_OP_breg8:
24204 case DW_OP_breg9:
24205 case DW_OP_breg10:
24206 case DW_OP_breg11:
24207 case DW_OP_breg12:
24208 case DW_OP_breg13:
24209 case DW_OP_breg14:
24210 case DW_OP_breg15:
24211 case DW_OP_breg16:
24212 case DW_OP_breg17:
24213 case DW_OP_breg18:
24214 case DW_OP_breg19:
24215 case DW_OP_breg20:
24216 case DW_OP_breg21:
24217 case DW_OP_breg22:
24218 case DW_OP_breg23:
24219 case DW_OP_breg24:
24220 case DW_OP_breg25:
24221 case DW_OP_breg26:
24222 case DW_OP_breg27:
24223 case DW_OP_breg28:
24224 case DW_OP_breg29:
24225 case DW_OP_breg30:
24226 case DW_OP_breg31:
24227 case DW_OP_regx:
24228 case DW_OP_fbreg:
24229 case DW_OP_piece:
24230 case DW_OP_deref_size:
24231 case DW_OP_xderef_size:
24232 return valx1->v.val_int == valy1->v.val_int;
24233 case DW_OP_skip:
24234 case DW_OP_bra:
24235 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24236 can cause irrelevant differences in dw_loc_addr. */
24237 gcc_assert (valx1->val_class == dw_val_class_loc
24238 && valy1->val_class == dw_val_class_loc
24239 && (dwarf_split_debug_info
24240 || x->dw_loc_addr == y->dw_loc_addr));
24241 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24242 case DW_OP_implicit_value:
24243 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24244 || valx2->val_class != valy2->val_class)
24245 return false;
24246 switch (valx2->val_class)
24248 case dw_val_class_const:
24249 return valx2->v.val_int == valy2->v.val_int;
24250 case dw_val_class_vec:
24251 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24252 && valx2->v.val_vec.length == valy2->v.val_vec.length
24253 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24254 valx2->v.val_vec.elt_size
24255 * valx2->v.val_vec.length) == 0;
24256 case dw_val_class_const_double:
24257 return valx2->v.val_double.low == valy2->v.val_double.low
24258 && valx2->v.val_double.high == valy2->v.val_double.high;
24259 case dw_val_class_wide_int:
24260 return *valx2->v.val_wide == *valy2->v.val_wide;
24261 case dw_val_class_addr:
24262 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24263 default:
24264 gcc_unreachable ();
24266 case DW_OP_bregx:
24267 case DW_OP_bit_piece:
24268 return valx1->v.val_int == valy1->v.val_int
24269 && valx2->v.val_int == valy2->v.val_int;
24270 case DW_OP_addr:
24271 hash_addr:
24272 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24273 case DW_OP_GNU_addr_index:
24274 case DW_OP_GNU_const_index:
24276 rtx ax1 = valx1->val_entry->addr.rtl;
24277 rtx ay1 = valy1->val_entry->addr.rtl;
24278 return rtx_equal_p (ax1, ay1);
24280 case DW_OP_GNU_implicit_pointer:
24281 return valx1->val_class == dw_val_class_die_ref
24282 && valx1->val_class == valy1->val_class
24283 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24284 && valx2->v.val_int == valy2->v.val_int;
24285 case DW_OP_GNU_entry_value:
24286 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24287 case DW_OP_GNU_const_type:
24288 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24289 || valx2->val_class != valy2->val_class)
24290 return false;
24291 switch (valx2->val_class)
24293 case dw_val_class_const:
24294 return valx2->v.val_int == valy2->v.val_int;
24295 case dw_val_class_vec:
24296 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24297 && valx2->v.val_vec.length == valy2->v.val_vec.length
24298 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24299 valx2->v.val_vec.elt_size
24300 * valx2->v.val_vec.length) == 0;
24301 case dw_val_class_const_double:
24302 return valx2->v.val_double.low == valy2->v.val_double.low
24303 && valx2->v.val_double.high == valy2->v.val_double.high;
24304 case dw_val_class_wide_int:
24305 return *valx2->v.val_wide == *valy2->v.val_wide;
24306 default:
24307 gcc_unreachable ();
24309 case DW_OP_GNU_regval_type:
24310 case DW_OP_GNU_deref_type:
24311 return valx1->v.val_int == valy1->v.val_int
24312 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24313 case DW_OP_GNU_convert:
24314 case DW_OP_GNU_reinterpret:
24315 if (valx1->val_class != valy1->val_class)
24316 return false;
24317 if (valx1->val_class == dw_val_class_unsigned_const)
24318 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24319 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24320 case DW_OP_GNU_parameter_ref:
24321 return valx1->val_class == dw_val_class_die_ref
24322 && valx1->val_class == valy1->val_class
24323 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24324 default:
24325 /* Other codes have no operands. */
24326 return true;
24330 /* Return true if DWARF location expressions X and Y are the same. */
24332 static inline bool
24333 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24335 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24336 if (x->dw_loc_opc != y->dw_loc_opc
24337 || x->dtprel != y->dtprel
24338 || !compare_loc_operands (x, y))
24339 break;
24340 return x == NULL && y == NULL;
24343 /* Hashtable helpers. */
24345 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24347 typedef dw_loc_list_struct value_type;
24348 typedef dw_loc_list_struct compare_type;
24349 static inline hashval_t hash (const value_type *);
24350 static inline bool equal (const value_type *, const compare_type *);
24353 /* Return precomputed hash of location list X. */
24355 inline hashval_t
24356 loc_list_hasher::hash (const value_type *x)
24358 return x->hash;
24361 /* Return true if location lists A and B are the same. */
24363 inline bool
24364 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24366 if (a == b)
24367 return 1;
24368 if (a->hash != b->hash)
24369 return 0;
24370 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24371 if (strcmp (a->begin, b->begin) != 0
24372 || strcmp (a->end, b->end) != 0
24373 || (a->section == NULL) != (b->section == NULL)
24374 || (a->section && strcmp (a->section, b->section) != 0)
24375 || !compare_locs (a->expr, b->expr))
24376 break;
24377 return a == NULL && b == NULL;
24380 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24383 /* Recursively optimize location lists referenced from DIE
24384 children and share them whenever possible. */
24386 static void
24387 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24389 dw_die_ref c;
24390 dw_attr_ref a;
24391 unsigned ix;
24392 dw_loc_list_struct **slot;
24394 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24395 if (AT_class (a) == dw_val_class_loc_list)
24397 dw_loc_list_ref list = AT_loc_list (a);
24398 /* TODO: perform some optimizations here, before hashing
24399 it and storing into the hash table. */
24400 hash_loc_list (list);
24401 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24402 if (*slot == NULL)
24403 *slot = list;
24404 else
24405 a->dw_attr_val.v.val_loc_list = *slot;
24408 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24412 /* Recursively assign each location list a unique index into the debug_addr
24413 section. */
24415 static void
24416 index_location_lists (dw_die_ref die)
24418 dw_die_ref c;
24419 dw_attr_ref a;
24420 unsigned ix;
24422 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24423 if (AT_class (a) == dw_val_class_loc_list)
24425 dw_loc_list_ref list = AT_loc_list (a);
24426 dw_loc_list_ref curr;
24427 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24429 /* Don't index an entry that has already been indexed
24430 or won't be output. */
24431 if (curr->begin_entry != NULL
24432 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24433 continue;
24435 curr->begin_entry
24436 = add_addr_table_entry (xstrdup (curr->begin),
24437 ate_kind_label);
24441 FOR_EACH_CHILD (die, c, index_location_lists (c));
24444 /* Optimize location lists referenced from DIE
24445 children and share them whenever possible. */
24447 static void
24448 optimize_location_lists (dw_die_ref die)
24450 loc_list_hash_type htab (500);
24451 optimize_location_lists_1 (die, &htab);
24454 /* Output stuff that dwarf requires at the end of every file,
24455 and generate the DWARF-2 debugging info. */
24457 static void
24458 dwarf2out_finish (const char *filename)
24460 limbo_die_node *node, *next_node;
24461 comdat_type_node *ctnode;
24462 unsigned int i;
24463 dw_die_ref main_comp_unit_die;
24465 /* PCH might result in DW_AT_producer string being restored from the
24466 header compilation, so always fill it with empty string initially
24467 and overwrite only here. */
24468 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24469 producer_string = gen_producer_string ();
24470 producer->dw_attr_val.v.val_str->refcount--;
24471 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24473 gen_scheduled_generic_parms_dies ();
24474 gen_remaining_tmpl_value_param_die_attribute ();
24476 /* Add the name for the main input file now. We delayed this from
24477 dwarf2out_init to avoid complications with PCH. */
24478 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24479 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24480 add_comp_dir_attribute (comp_unit_die ());
24481 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24483 bool p = false;
24484 file_table->traverse<bool *, file_table_relative_p> (&p);
24485 if (p)
24486 add_comp_dir_attribute (comp_unit_die ());
24489 if (deferred_locations_list)
24490 for (i = 0; i < deferred_locations_list->length (); i++)
24492 add_location_or_const_value_attribute (
24493 (*deferred_locations_list)[i].die,
24494 (*deferred_locations_list)[i].variable,
24495 false,
24496 DW_AT_location);
24499 /* Traverse the limbo die list, and add parent/child links. The only
24500 dies without parents that should be here are concrete instances of
24501 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24502 For concrete instances, we can get the parent die from the abstract
24503 instance. */
24504 for (node = limbo_die_list; node; node = next_node)
24506 dw_die_ref die = node->die;
24507 next_node = node->next;
24509 if (die->die_parent == NULL)
24511 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24513 if (origin && origin->die_parent)
24514 add_child_die (origin->die_parent, die);
24515 else if (is_cu_die (die))
24517 else if (seen_error ())
24518 /* It's OK to be confused by errors in the input. */
24519 add_child_die (comp_unit_die (), die);
24520 else
24522 /* In certain situations, the lexical block containing a
24523 nested function can be optimized away, which results
24524 in the nested function die being orphaned. Likewise
24525 with the return type of that nested function. Force
24526 this to be a child of the containing function.
24528 It may happen that even the containing function got fully
24529 inlined and optimized out. In that case we are lost and
24530 assign the empty child. This should not be big issue as
24531 the function is likely unreachable too. */
24532 gcc_assert (node->created_for);
24534 if (DECL_P (node->created_for))
24535 origin = get_context_die (DECL_CONTEXT (node->created_for));
24536 else if (TYPE_P (node->created_for))
24537 origin = scope_die_for (node->created_for, comp_unit_die ());
24538 else
24539 origin = comp_unit_die ();
24541 add_child_die (origin, die);
24546 limbo_die_list = NULL;
24548 #if ENABLE_ASSERT_CHECKING
24550 dw_die_ref die = comp_unit_die (), c;
24551 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24553 #endif
24554 resolve_addr (comp_unit_die ());
24555 move_marked_base_types ();
24557 for (node = deferred_asm_name; node; node = node->next)
24559 tree decl = node->created_for;
24560 /* When generating LTO bytecode we can not generate new assembler
24561 names at this point and all important decls got theirs via
24562 free-lang-data. */
24563 if (((!flag_generate_lto && !flag_generate_offload)
24564 || DECL_ASSEMBLER_NAME_SET_P (decl))
24565 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24567 add_linkage_attr (node->die, decl);
24568 move_linkage_attr (node->die);
24572 deferred_asm_name = NULL;
24574 /* Walk through the list of incomplete types again, trying once more to
24575 emit full debugging info for them. */
24576 retry_incomplete_types ();
24578 if (flag_eliminate_unused_debug_types)
24579 prune_unused_types ();
24581 /* Generate separate COMDAT sections for type DIEs. */
24582 if (use_debug_types)
24584 break_out_comdat_types (comp_unit_die ());
24586 /* Each new type_unit DIE was added to the limbo die list when created.
24587 Since these have all been added to comdat_type_list, clear the
24588 limbo die list. */
24589 limbo_die_list = NULL;
24591 /* For each new comdat type unit, copy declarations for incomplete
24592 types to make the new unit self-contained (i.e., no direct
24593 references to the main compile unit). */
24594 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24595 copy_decls_for_unworthy_types (ctnode->root_die);
24596 copy_decls_for_unworthy_types (comp_unit_die ());
24598 /* In the process of copying declarations from one unit to another,
24599 we may have left some declarations behind that are no longer
24600 referenced. Prune them. */
24601 prune_unused_types ();
24604 /* Generate separate CUs for each of the include files we've seen.
24605 They will go into limbo_die_list. */
24606 if (flag_eliminate_dwarf2_dups)
24607 break_out_includes (comp_unit_die ());
24609 /* Traverse the DIE's and add add sibling attributes to those DIE's
24610 that have children. */
24611 add_sibling_attributes (comp_unit_die ());
24612 for (node = limbo_die_list; node; node = node->next)
24613 add_sibling_attributes (node->die);
24614 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24615 add_sibling_attributes (ctnode->root_die);
24617 /* When splitting DWARF info, we put some attributes in the
24618 skeleton compile_unit DIE that remains in the .o, while
24619 most attributes go in the DWO compile_unit_die. */
24620 if (dwarf_split_debug_info)
24621 main_comp_unit_die = gen_compile_unit_die (NULL);
24622 else
24623 main_comp_unit_die = comp_unit_die ();
24625 /* Output a terminator label for the .text section. */
24626 switch_to_section (text_section);
24627 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24628 if (cold_text_section)
24630 switch_to_section (cold_text_section);
24631 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24634 /* We can only use the low/high_pc attributes if all of the code was
24635 in .text. */
24636 if (!have_multiple_function_sections
24637 || (dwarf_version < 3 && dwarf_strict))
24639 /* Don't add if the CU has no associated code. */
24640 if (text_section_used)
24641 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24642 text_end_label, true);
24644 else
24646 unsigned fde_idx;
24647 dw_fde_ref fde;
24648 bool range_list_added = false;
24650 if (text_section_used)
24651 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24652 text_end_label, &range_list_added, true);
24653 if (cold_text_section_used)
24654 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24655 cold_end_label, &range_list_added, true);
24657 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24659 if (DECL_IGNORED_P (fde->decl))
24660 continue;
24661 if (!fde->in_std_section)
24662 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24663 fde->dw_fde_end, &range_list_added,
24664 true);
24665 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24666 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24667 fde->dw_fde_second_end, &range_list_added,
24668 true);
24671 if (range_list_added)
24673 /* We need to give .debug_loc and .debug_ranges an appropriate
24674 "base address". Use zero so that these addresses become
24675 absolute. Historically, we've emitted the unexpected
24676 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24677 Emit both to give time for other tools to adapt. */
24678 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24679 if (! dwarf_strict && dwarf_version < 4)
24680 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24682 add_ranges (NULL);
24686 if (debug_info_level >= DINFO_LEVEL_TERSE)
24687 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24688 debug_line_section_label);
24690 if (have_macinfo)
24691 add_AT_macptr (comp_unit_die (),
24692 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24693 macinfo_section_label);
24695 if (dwarf_split_debug_info)
24697 /* optimize_location_lists calculates the size of the lists,
24698 so index them first, and assign indices to the entries.
24699 Although optimize_location_lists will remove entries from
24700 the table, it only does so for duplicates, and therefore
24701 only reduces ref_counts to 1. */
24702 index_location_lists (comp_unit_die ());
24704 if (addr_index_table != NULL)
24706 unsigned int index = 0;
24707 addr_index_table
24708 ->traverse_noresize<unsigned int *, index_addr_table_entry>
24709 (&index);
24713 if (have_location_lists)
24714 optimize_location_lists (comp_unit_die ());
24716 save_macinfo_strings ();
24718 if (dwarf_split_debug_info)
24720 unsigned int index = 0;
24722 /* Add attributes common to skeleton compile_units and
24723 type_units. Because these attributes include strings, it
24724 must be done before freezing the string table. Top-level
24725 skeleton die attrs are added when the skeleton type unit is
24726 created, so ensure it is created by this point. */
24727 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24728 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24731 /* Output all of the compilation units. We put the main one last so that
24732 the offsets are available to output_pubnames. */
24733 for (node = limbo_die_list; node; node = node->next)
24734 output_comp_unit (node->die, 0);
24736 hash_table<comdat_type_hasher> comdat_type_table (100);
24737 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24739 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24741 /* Don't output duplicate types. */
24742 if (*slot != HTAB_EMPTY_ENTRY)
24743 continue;
24745 /* Add a pointer to the line table for the main compilation unit
24746 so that the debugger can make sense of DW_AT_decl_file
24747 attributes. */
24748 if (debug_info_level >= DINFO_LEVEL_TERSE)
24749 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24750 (!dwarf_split_debug_info
24751 ? debug_line_section_label
24752 : debug_skeleton_line_section_label));
24754 output_comdat_type_unit (ctnode);
24755 *slot = ctnode;
24758 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24759 both the main_cu and all skeleton TUs. Making this call unconditional
24760 would end up either adding a second copy of the AT_pubnames attribute, or
24761 requiring a special case in add_top_level_skeleton_die_attrs. */
24762 if (!dwarf_split_debug_info)
24763 add_AT_pubnames (comp_unit_die ());
24765 if (dwarf_split_debug_info)
24767 int mark;
24768 unsigned char checksum[16];
24769 struct md5_ctx ctx;
24771 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24772 md5_init_ctx (&ctx);
24773 mark = 0;
24774 die_checksum (comp_unit_die (), &ctx, &mark);
24775 unmark_all_dies (comp_unit_die ());
24776 md5_finish_ctx (&ctx, checksum);
24778 /* Use the first 8 bytes of the checksum as the dwo_id,
24779 and add it to both comp-unit DIEs. */
24780 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24781 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24783 /* Add the base offset of the ranges table to the skeleton
24784 comp-unit DIE. */
24785 if (ranges_table_in_use)
24786 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24787 ranges_section_label);
24789 switch_to_section (debug_addr_section);
24790 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24791 output_addr_table ();
24794 /* Output the main compilation unit if non-empty or if .debug_macinfo
24795 or .debug_macro will be emitted. */
24796 output_comp_unit (comp_unit_die (), have_macinfo);
24798 if (dwarf_split_debug_info && info_section_emitted)
24799 output_skeleton_debug_sections (main_comp_unit_die);
24801 /* Output the abbreviation table. */
24802 if (abbrev_die_table_in_use != 1)
24804 switch_to_section (debug_abbrev_section);
24805 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24806 output_abbrev_section ();
24809 /* Output location list section if necessary. */
24810 if (have_location_lists)
24812 /* Output the location lists info. */
24813 switch_to_section (debug_loc_section);
24814 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24815 output_location_lists (comp_unit_die ());
24818 output_pubtables ();
24820 /* Output the address range information if a CU (.debug_info section)
24821 was emitted. We output an empty table even if we had no functions
24822 to put in it. This because the consumer has no way to tell the
24823 difference between an empty table that we omitted and failure to
24824 generate a table that would have contained data. */
24825 if (info_section_emitted)
24827 unsigned long aranges_length = size_of_aranges ();
24829 switch_to_section (debug_aranges_section);
24830 output_aranges (aranges_length);
24833 /* Output ranges section if necessary. */
24834 if (ranges_table_in_use)
24836 switch_to_section (debug_ranges_section);
24837 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24838 output_ranges ();
24841 /* Have to end the macro section. */
24842 if (have_macinfo)
24844 switch_to_section (debug_macinfo_section);
24845 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24846 output_macinfo ();
24847 dw2_asm_output_data (1, 0, "End compilation unit");
24850 /* Output the source line correspondence table. We must do this
24851 even if there is no line information. Otherwise, on an empty
24852 translation unit, we will generate a present, but empty,
24853 .debug_info section. IRIX 6.5 `nm' will then complain when
24854 examining the file. This is done late so that any filenames
24855 used by the debug_info section are marked as 'used'. */
24856 switch_to_section (debug_line_section);
24857 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24858 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24859 output_line_info (false);
24861 if (dwarf_split_debug_info && info_section_emitted)
24863 switch_to_section (debug_skeleton_line_section);
24864 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24865 output_line_info (true);
24868 /* If we emitted any indirect strings, output the string table too. */
24869 if (debug_str_hash || skeleton_debug_str_hash)
24870 output_indirect_strings ();
24873 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24874 within the same process. For use by toplev::finalize. */
24876 void
24877 dwarf2out_c_finalize (void)
24879 last_var_location_insn = NULL;
24880 cached_next_real_insn = NULL;
24881 used_rtx_array = NULL;
24882 incomplete_types = NULL;
24883 decl_scope_table = NULL;
24884 debug_info_section = NULL;
24885 debug_skeleton_info_section = NULL;
24886 debug_abbrev_section = NULL;
24887 debug_skeleton_abbrev_section = NULL;
24888 debug_aranges_section = NULL;
24889 debug_addr_section = NULL;
24890 debug_macinfo_section = NULL;
24891 debug_line_section = NULL;
24892 debug_skeleton_line_section = NULL;
24893 debug_loc_section = NULL;
24894 debug_pubnames_section = NULL;
24895 debug_pubtypes_section = NULL;
24896 debug_str_section = NULL;
24897 debug_str_dwo_section = NULL;
24898 debug_str_offsets_section = NULL;
24899 debug_ranges_section = NULL;
24900 debug_frame_section = NULL;
24901 fde_vec = NULL;
24902 debug_str_hash = NULL;
24903 skeleton_debug_str_hash = NULL;
24904 dw2_string_counter = 0;
24905 have_multiple_function_sections = false;
24906 text_section_used = false;
24907 cold_text_section_used = false;
24908 cold_text_section = NULL;
24909 current_unit_personality = NULL;
24911 deferred_locations_list = NULL;
24913 next_die_offset = 0;
24914 single_comp_unit_die = NULL;
24915 comdat_type_list = NULL;
24916 limbo_die_list = NULL;
24917 deferred_asm_name = NULL;
24918 file_table = NULL;
24919 decl_die_table = NULL;
24920 common_block_die_table = NULL;
24921 decl_loc_table = NULL;
24922 call_arg_locations = NULL;
24923 call_arg_loc_last = NULL;
24924 call_site_count = -1;
24925 tail_call_site_count = -1;
24926 //block_map = NULL;
24927 cached_dw_loc_list_table = NULL;
24928 abbrev_die_table = NULL;
24929 abbrev_die_table_allocated = 0;
24930 abbrev_die_table_in_use = 0;
24931 line_info_label_num = 0;
24932 cur_line_info_table = NULL;
24933 text_section_line_info = NULL;
24934 cold_text_section_line_info = NULL;
24935 separate_line_info = NULL;
24936 info_section_emitted = false;
24937 pubname_table = NULL;
24938 pubtype_table = NULL;
24939 macinfo_table = NULL;
24940 ranges_table = NULL;
24941 ranges_table_allocated = 0;
24942 ranges_table_in_use = 0;
24943 ranges_by_label = 0;
24944 ranges_by_label_allocated = 0;
24945 ranges_by_label_in_use = 0;
24946 have_location_lists = false;
24947 loclabel_num = 0;
24948 poc_label_num = 0;
24949 last_emitted_file = NULL;
24950 label_num = 0;
24951 file_table_last_lookup = NULL;
24952 tmpl_value_parm_die_table = NULL;
24953 generic_type_instances = NULL;
24954 frame_pointer_fb_offset = 0;
24955 frame_pointer_fb_offset_valid = false;
24956 base_types.release ();
24957 XDELETEVEC (producer_string);
24958 producer_string = NULL;
24961 #include "gt-dwarf2out.h"