2014-10-24 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / dwarf2out.c
bloba87f9c0997e7ed52fa0b1229a465047bcde7206b
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "hashtab.h"
68 #include "hash-set.h"
69 #include "vec.h"
70 #include "machmode.h"
71 #include "hard-reg-set.h"
72 #include "input.h"
73 #include "function.h"
74 #include "emit-rtl.h"
75 #include "hash-table.h"
76 #include "version.h"
77 #include "flags.h"
78 #include "regs.h"
79 #include "rtlhash.h"
80 #include "insn-config.h"
81 #include "reload.h"
82 #include "output.h"
83 #include "expr.h"
84 #include "except.h"
85 #include "dwarf2.h"
86 #include "dwarf2out.h"
87 #include "dwarf2asm.h"
88 #include "toplev.h"
89 #include "md5.h"
90 #include "tm_p.h"
91 #include "diagnostic.h"
92 #include "tree-pretty-print.h"
93 #include "debug.h"
94 #include "target.h"
95 #include "common/common-target.h"
96 #include "langhooks.h"
97 #include "cgraph.h"
98 #include "ira.h"
99 #include "lra.h"
100 #include "dumpfile.h"
101 #include "opts.h"
102 #include "tree-dfa.h"
103 #include "gdb/gdb-index.h"
104 #include "rtl-iter.h"
106 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
107 static rtx_insn *last_var_location_insn;
108 static rtx_insn *cached_next_real_insn;
109 static void dwarf2out_decl (tree);
111 #ifdef VMS_DEBUGGING_INFO
112 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
114 /* Define this macro to be a nonzero value if the directory specifications
115 which are output in the debug info should end with a separator. */
116 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
117 /* Define this macro to evaluate to a nonzero value if GCC should refrain
118 from generating indirect strings in DWARF2 debug information, for instance
119 if your target is stuck with an old version of GDB that is unable to
120 process them properly or uses VMS Debug. */
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
122 #else
123 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
124 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
125 #endif
127 /* ??? Poison these here until it can be done generically. They've been
128 totally replaced in this file; make sure it stays that way. */
129 #undef DWARF2_UNWIND_INFO
130 #undef DWARF2_FRAME_INFO
131 #if (GCC_VERSION >= 3000)
132 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
133 #endif
135 /* The size of the target's pointer type. */
136 #ifndef PTR_SIZE
137 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
138 #endif
140 /* Array of RTXes referenced by the debugging information, which therefore
141 must be kept around forever. */
142 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
144 /* A pointer to the base of a list of incomplete types which might be
145 completed at some later time. incomplete_types_list needs to be a
146 vec<tree, va_gc> *because we want to tell the garbage collector about
147 it. */
148 static GTY(()) vec<tree, va_gc> *incomplete_types;
150 /* A pointer to the base of a table of references to declaration
151 scopes. This table is a display which tracks the nesting
152 of declaration scopes at the current scope and containing
153 scopes. This table is used to find the proper place to
154 define type declaration DIE's. */
155 static GTY(()) vec<tree, va_gc> *decl_scope_table;
157 /* Pointers to various DWARF2 sections. */
158 static GTY(()) section *debug_info_section;
159 static GTY(()) section *debug_skeleton_info_section;
160 static GTY(()) section *debug_abbrev_section;
161 static GTY(()) section *debug_skeleton_abbrev_section;
162 static GTY(()) section *debug_aranges_section;
163 static GTY(()) section *debug_addr_section;
164 static GTY(()) section *debug_macinfo_section;
165 static GTY(()) section *debug_line_section;
166 static GTY(()) section *debug_skeleton_line_section;
167 static GTY(()) section *debug_loc_section;
168 static GTY(()) section *debug_pubnames_section;
169 static GTY(()) section *debug_pubtypes_section;
170 static GTY(()) section *debug_str_section;
171 static GTY(()) section *debug_str_dwo_section;
172 static GTY(()) section *debug_str_offsets_section;
173 static GTY(()) section *debug_ranges_section;
174 static GTY(()) section *debug_frame_section;
176 /* Maximum size (in bytes) of an artificially generated label. */
177 #define MAX_ARTIFICIAL_LABEL_BYTES 30
179 /* According to the (draft) DWARF 3 specification, the initial length
180 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
181 bytes are 0xffffffff, followed by the length stored in the next 8
182 bytes.
184 However, the SGI/MIPS ABI uses an initial length which is equal to
185 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
187 #ifndef DWARF_INITIAL_LENGTH_SIZE
188 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
189 #endif
191 /* Round SIZE up to the nearest BOUNDARY. */
192 #define DWARF_ROUND(SIZE,BOUNDARY) \
193 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
195 /* CIE identifier. */
196 #if HOST_BITS_PER_WIDE_INT >= 64
197 #define DWARF_CIE_ID \
198 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
199 #else
200 #define DWARF_CIE_ID DW_CIE_ID
201 #endif
204 /* A vector for a table that contains frame description
205 information for each routine. */
206 #define NOT_INDEXED (-1U)
207 #define NO_INDEX_ASSIGNED (-2U)
209 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
211 struct GTY((for_user)) indirect_string_node {
212 const char *str;
213 unsigned int refcount;
214 enum dwarf_form form;
215 char *label;
216 unsigned int index;
219 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
221 typedef const char *compare_type;
223 static hashval_t hash (indirect_string_node *);
224 static bool equal (indirect_string_node *, const char *);
227 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
229 /* With split_debug_info, both the comp_dir and dwo_name go in the
230 main object file, rather than the dwo, similar to the force_direct
231 parameter elsewhere but with additional complications:
233 1) The string is needed in both the main object file and the dwo.
234 That is, the comp_dir and dwo_name will appear in both places.
236 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
237 DW_FORM_GNU_str_index.
239 3) GCC chooses the form to use late, depending on the size and
240 reference count.
242 Rather than forcing the all debug string handling functions and
243 callers to deal with these complications, simply use a separate,
244 special-cased string table for any attribute that should go in the
245 main object file. This limits the complexity to just the places
246 that need it. */
248 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
250 static GTY(()) int dw2_string_counter;
252 /* True if the compilation unit places functions in more than one section. */
253 static GTY(()) bool have_multiple_function_sections = false;
255 /* Whether the default text and cold text sections have been used at all. */
257 static GTY(()) bool text_section_used = false;
258 static GTY(()) bool cold_text_section_used = false;
260 /* The default cold text section. */
261 static GTY(()) section *cold_text_section;
263 /* The DIE for C++14 'auto' in a function return type. */
264 static GTY(()) dw_die_ref auto_die;
266 /* The DIE for C++14 'decltype(auto)' in a function return type. */
267 static GTY(()) dw_die_ref decltype_auto_die;
269 /* Forward declarations for functions defined in this file. */
271 static char *stripattributes (const char *);
272 static void output_call_frame_info (int);
273 static void dwarf2out_note_section_used (void);
275 /* Personality decl of current unit. Used only when assembler does not support
276 personality CFI. */
277 static GTY(()) rtx current_unit_personality;
279 /* Data and reference forms for relocatable data. */
280 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
281 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
283 #ifndef DEBUG_FRAME_SECTION
284 #define DEBUG_FRAME_SECTION ".debug_frame"
285 #endif
287 #ifndef FUNC_BEGIN_LABEL
288 #define FUNC_BEGIN_LABEL "LFB"
289 #endif
291 #ifndef FUNC_END_LABEL
292 #define FUNC_END_LABEL "LFE"
293 #endif
295 #ifndef PROLOGUE_END_LABEL
296 #define PROLOGUE_END_LABEL "LPE"
297 #endif
299 #ifndef EPILOGUE_BEGIN_LABEL
300 #define EPILOGUE_BEGIN_LABEL "LEB"
301 #endif
303 #ifndef FRAME_BEGIN_LABEL
304 #define FRAME_BEGIN_LABEL "Lframe"
305 #endif
306 #define CIE_AFTER_SIZE_LABEL "LSCIE"
307 #define CIE_END_LABEL "LECIE"
308 #define FDE_LABEL "LSFDE"
309 #define FDE_AFTER_SIZE_LABEL "LASFDE"
310 #define FDE_END_LABEL "LEFDE"
311 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
312 #define LINE_NUMBER_END_LABEL "LELT"
313 #define LN_PROLOG_AS_LABEL "LASLTP"
314 #define LN_PROLOG_END_LABEL "LELTP"
315 #define DIE_LABEL_PREFIX "DW"
317 /* Match the base name of a file to the base name of a compilation unit. */
319 static int
320 matches_main_base (const char *path)
322 /* Cache the last query. */
323 static const char *last_path = NULL;
324 static int last_match = 0;
325 if (path != last_path)
327 const char *base;
328 int length = base_of_path (path, &base);
329 last_path = path;
330 last_match = (length == main_input_baselength
331 && memcmp (base, main_input_basename, length) == 0);
333 return last_match;
336 #ifdef DEBUG_DEBUG_STRUCT
338 static int
339 dump_struct_debug (tree type, enum debug_info_usage usage,
340 enum debug_struct_file criterion, int generic,
341 int matches, int result)
343 /* Find the type name. */
344 tree type_decl = TYPE_STUB_DECL (type);
345 tree t = type_decl;
346 const char *name = 0;
347 if (TREE_CODE (t) == TYPE_DECL)
348 t = DECL_NAME (t);
349 if (t)
350 name = IDENTIFIER_POINTER (t);
352 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
353 criterion,
354 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
355 matches ? "bas" : "hdr",
356 generic ? "gen" : "ord",
357 usage == DINFO_USAGE_DFN ? ";" :
358 usage == DINFO_USAGE_DIR_USE ? "." : "*",
359 result,
360 (void*) type_decl, name);
361 return result;
363 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
364 dump_struct_debug (type, usage, criterion, generic, matches, result)
366 #else
368 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
369 (result)
371 #endif
373 /* Get the number of HOST_WIDE_INTs needed to represent the precision
374 of the number. */
376 static unsigned int
377 get_full_len (const wide_int &op)
379 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
380 / HOST_BITS_PER_WIDE_INT);
383 static bool
384 should_emit_struct_debug (tree type, enum debug_info_usage usage)
386 enum debug_struct_file criterion;
387 tree type_decl;
388 bool generic = lang_hooks.types.generic_p (type);
390 if (generic)
391 criterion = debug_struct_generic[usage];
392 else
393 criterion = debug_struct_ordinary[usage];
395 if (criterion == DINFO_STRUCT_FILE_NONE)
396 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
397 if (criterion == DINFO_STRUCT_FILE_ANY)
398 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
400 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
402 if (type_decl != NULL)
404 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
405 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
407 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
408 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
411 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
414 /* Return a pointer to a copy of the section string name S with all
415 attributes stripped off, and an asterisk prepended (for assemble_name). */
417 static inline char *
418 stripattributes (const char *s)
420 char *stripped = XNEWVEC (char, strlen (s) + 2);
421 char *p = stripped;
423 *p++ = '*';
425 while (*s && *s != ',')
426 *p++ = *s++;
428 *p = '\0';
429 return stripped;
432 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
433 switch to the data section instead, and write out a synthetic start label
434 for collect2 the first time around. */
436 static void
437 switch_to_eh_frame_section (bool back)
439 tree label;
441 #ifdef EH_FRAME_SECTION_NAME
442 if (eh_frame_section == 0)
444 int flags;
446 if (EH_TABLES_CAN_BE_READ_ONLY)
448 int fde_encoding;
449 int per_encoding;
450 int lsda_encoding;
452 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
453 /*global=*/0);
454 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
455 /*global=*/1);
456 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
457 /*global=*/0);
458 flags = ((! flag_pic
459 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
460 && (fde_encoding & 0x70) != DW_EH_PE_aligned
461 && (per_encoding & 0x70) != DW_EH_PE_absptr
462 && (per_encoding & 0x70) != DW_EH_PE_aligned
463 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
464 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
465 ? 0 : SECTION_WRITE);
467 else
468 flags = SECTION_WRITE;
469 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
471 #endif /* EH_FRAME_SECTION_NAME */
473 if (eh_frame_section)
474 switch_to_section (eh_frame_section);
475 else
477 /* We have no special eh_frame section. Put the information in
478 the data section and emit special labels to guide collect2. */
479 switch_to_section (data_section);
481 if (!back)
483 label = get_file_function_name ("F");
484 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
485 targetm.asm_out.globalize_label (asm_out_file,
486 IDENTIFIER_POINTER (label));
487 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
492 /* Switch [BACK] to the eh or debug frame table section, depending on
493 FOR_EH. */
495 static void
496 switch_to_frame_table_section (int for_eh, bool back)
498 if (for_eh)
499 switch_to_eh_frame_section (back);
500 else
502 if (!debug_frame_section)
503 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
504 SECTION_DEBUG, NULL);
505 switch_to_section (debug_frame_section);
509 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
511 enum dw_cfi_oprnd_type
512 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
514 switch (cfi)
516 case DW_CFA_nop:
517 case DW_CFA_GNU_window_save:
518 case DW_CFA_remember_state:
519 case DW_CFA_restore_state:
520 return dw_cfi_oprnd_unused;
522 case DW_CFA_set_loc:
523 case DW_CFA_advance_loc1:
524 case DW_CFA_advance_loc2:
525 case DW_CFA_advance_loc4:
526 case DW_CFA_MIPS_advance_loc8:
527 return dw_cfi_oprnd_addr;
529 case DW_CFA_offset:
530 case DW_CFA_offset_extended:
531 case DW_CFA_def_cfa:
532 case DW_CFA_offset_extended_sf:
533 case DW_CFA_def_cfa_sf:
534 case DW_CFA_restore:
535 case DW_CFA_restore_extended:
536 case DW_CFA_undefined:
537 case DW_CFA_same_value:
538 case DW_CFA_def_cfa_register:
539 case DW_CFA_register:
540 case DW_CFA_expression:
541 return dw_cfi_oprnd_reg_num;
543 case DW_CFA_def_cfa_offset:
544 case DW_CFA_GNU_args_size:
545 case DW_CFA_def_cfa_offset_sf:
546 return dw_cfi_oprnd_offset;
548 case DW_CFA_def_cfa_expression:
549 return dw_cfi_oprnd_loc;
551 default:
552 gcc_unreachable ();
556 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
558 enum dw_cfi_oprnd_type
559 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
561 switch (cfi)
563 case DW_CFA_def_cfa:
564 case DW_CFA_def_cfa_sf:
565 case DW_CFA_offset:
566 case DW_CFA_offset_extended_sf:
567 case DW_CFA_offset_extended:
568 return dw_cfi_oprnd_offset;
570 case DW_CFA_register:
571 return dw_cfi_oprnd_reg_num;
573 case DW_CFA_expression:
574 return dw_cfi_oprnd_loc;
576 default:
577 return dw_cfi_oprnd_unused;
581 /* Output one FDE. */
583 static void
584 output_fde (dw_fde_ref fde, bool for_eh, bool second,
585 char *section_start_label, int fde_encoding, char *augmentation,
586 bool any_lsda_needed, int lsda_encoding)
588 const char *begin, *end;
589 static unsigned int j;
590 char l1[20], l2[20];
592 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
593 /* empty */ 0);
594 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
595 for_eh + j);
596 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
597 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
598 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
599 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
600 " indicating 64-bit DWARF extension");
601 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
602 "FDE Length");
603 ASM_OUTPUT_LABEL (asm_out_file, l1);
605 if (for_eh)
606 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
607 else
608 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
609 debug_frame_section, "FDE CIE offset");
611 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
612 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
614 if (for_eh)
616 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
617 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
618 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
619 "FDE initial location");
620 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
621 end, begin, "FDE address range");
623 else
625 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
626 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
629 if (augmentation[0])
631 if (any_lsda_needed)
633 int size = size_of_encoded_value (lsda_encoding);
635 if (lsda_encoding == DW_EH_PE_aligned)
637 int offset = ( 4 /* Length */
638 + 4 /* CIE offset */
639 + 2 * size_of_encoded_value (fde_encoding)
640 + 1 /* Augmentation size */ );
641 int pad = -offset & (PTR_SIZE - 1);
643 size += pad;
644 gcc_assert (size_of_uleb128 (size) == 1);
647 dw2_asm_output_data_uleb128 (size, "Augmentation size");
649 if (fde->uses_eh_lsda)
651 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
652 fde->funcdef_number);
653 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
654 gen_rtx_SYMBOL_REF (Pmode, l1),
655 false,
656 "Language Specific Data Area");
658 else
660 if (lsda_encoding == DW_EH_PE_aligned)
661 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
662 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
663 "Language Specific Data Area (none)");
666 else
667 dw2_asm_output_data_uleb128 (0, "Augmentation size");
670 /* Loop through the Call Frame Instructions associated with this FDE. */
671 fde->dw_fde_current_label = begin;
673 size_t from, until, i;
675 from = 0;
676 until = vec_safe_length (fde->dw_fde_cfi);
678 if (fde->dw_fde_second_begin == NULL)
680 else if (!second)
681 until = fde->dw_fde_switch_cfi_index;
682 else
683 from = fde->dw_fde_switch_cfi_index;
685 for (i = from; i < until; i++)
686 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
689 /* If we are to emit a ref/link from function bodies to their frame tables,
690 do it now. This is typically performed to make sure that tables
691 associated with functions are dragged with them and not discarded in
692 garbage collecting links. We need to do this on a per function basis to
693 cope with -ffunction-sections. */
695 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
696 /* Switch to the function section, emit the ref to the tables, and
697 switch *back* into the table section. */
698 switch_to_section (function_section (fde->decl));
699 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
700 switch_to_frame_table_section (for_eh, true);
701 #endif
703 /* Pad the FDE out to an address sized boundary. */
704 ASM_OUTPUT_ALIGN (asm_out_file,
705 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
706 ASM_OUTPUT_LABEL (asm_out_file, l2);
708 j += 2;
711 /* Return true if frame description entry FDE is needed for EH. */
713 static bool
714 fde_needed_for_eh_p (dw_fde_ref fde)
716 if (flag_asynchronous_unwind_tables)
717 return true;
719 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
720 return true;
722 if (fde->uses_eh_lsda)
723 return true;
725 /* If exceptions are enabled, we have collected nothrow info. */
726 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
727 return false;
729 return true;
732 /* Output the call frame information used to record information
733 that relates to calculating the frame pointer, and records the
734 location of saved registers. */
736 static void
737 output_call_frame_info (int for_eh)
739 unsigned int i;
740 dw_fde_ref fde;
741 dw_cfi_ref cfi;
742 char l1[20], l2[20], section_start_label[20];
743 bool any_lsda_needed = false;
744 char augmentation[6];
745 int augmentation_size;
746 int fde_encoding = DW_EH_PE_absptr;
747 int per_encoding = DW_EH_PE_absptr;
748 int lsda_encoding = DW_EH_PE_absptr;
749 int return_reg;
750 rtx personality = NULL;
751 int dw_cie_version;
753 /* Don't emit a CIE if there won't be any FDEs. */
754 if (!fde_vec)
755 return;
757 /* Nothing to do if the assembler's doing it all. */
758 if (dwarf2out_do_cfi_asm ())
759 return;
761 /* If we don't have any functions we'll want to unwind out of, don't emit
762 any EH unwind information. If we make FDEs linkonce, we may have to
763 emit an empty label for an FDE that wouldn't otherwise be emitted. We
764 want to avoid having an FDE kept around when the function it refers to
765 is discarded. Example where this matters: a primary function template
766 in C++ requires EH information, an explicit specialization doesn't. */
767 if (for_eh)
769 bool any_eh_needed = false;
771 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
773 if (fde->uses_eh_lsda)
774 any_eh_needed = any_lsda_needed = true;
775 else if (fde_needed_for_eh_p (fde))
776 any_eh_needed = true;
777 else if (TARGET_USES_WEAK_UNWIND_INFO)
778 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
781 if (!any_eh_needed)
782 return;
785 /* We're going to be generating comments, so turn on app. */
786 if (flag_debug_asm)
787 app_enable ();
789 /* Switch to the proper frame section, first time. */
790 switch_to_frame_table_section (for_eh, false);
792 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
793 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
795 /* Output the CIE. */
796 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
797 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
798 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
799 dw2_asm_output_data (4, 0xffffffff,
800 "Initial length escape value indicating 64-bit DWARF extension");
801 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
802 "Length of Common Information Entry");
803 ASM_OUTPUT_LABEL (asm_out_file, l1);
805 /* Now that the CIE pointer is PC-relative for EH,
806 use 0 to identify the CIE. */
807 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
808 (for_eh ? 0 : DWARF_CIE_ID),
809 "CIE Identifier Tag");
811 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
812 use CIE version 1, unless that would produce incorrect results
813 due to overflowing the return register column. */
814 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
815 dw_cie_version = 1;
816 if (return_reg >= 256 || dwarf_version > 2)
817 dw_cie_version = 3;
818 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
820 augmentation[0] = 0;
821 augmentation_size = 0;
823 personality = current_unit_personality;
824 if (for_eh)
826 char *p;
828 /* Augmentation:
829 z Indicates that a uleb128 is present to size the
830 augmentation section.
831 L Indicates the encoding (and thus presence) of
832 an LSDA pointer in the FDE augmentation.
833 R Indicates a non-default pointer encoding for
834 FDE code pointers.
835 P Indicates the presence of an encoding + language
836 personality routine in the CIE augmentation. */
838 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
839 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
840 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
842 p = augmentation + 1;
843 if (personality)
845 *p++ = 'P';
846 augmentation_size += 1 + size_of_encoded_value (per_encoding);
847 assemble_external_libcall (personality);
849 if (any_lsda_needed)
851 *p++ = 'L';
852 augmentation_size += 1;
854 if (fde_encoding != DW_EH_PE_absptr)
856 *p++ = 'R';
857 augmentation_size += 1;
859 if (p > augmentation + 1)
861 augmentation[0] = 'z';
862 *p = '\0';
865 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
866 if (personality && per_encoding == DW_EH_PE_aligned)
868 int offset = ( 4 /* Length */
869 + 4 /* CIE Id */
870 + 1 /* CIE version */
871 + strlen (augmentation) + 1 /* Augmentation */
872 + size_of_uleb128 (1) /* Code alignment */
873 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
874 + 1 /* RA column */
875 + 1 /* Augmentation size */
876 + 1 /* Personality encoding */ );
877 int pad = -offset & (PTR_SIZE - 1);
879 augmentation_size += pad;
881 /* Augmentations should be small, so there's scarce need to
882 iterate for a solution. Die if we exceed one uleb128 byte. */
883 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
887 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
888 if (dw_cie_version >= 4)
890 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
891 dw2_asm_output_data (1, 0, "CIE Segment Size");
893 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
894 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
895 "CIE Data Alignment Factor");
897 if (dw_cie_version == 1)
898 dw2_asm_output_data (1, return_reg, "CIE RA Column");
899 else
900 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
902 if (augmentation[0])
904 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
905 if (personality)
907 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
908 eh_data_format_name (per_encoding));
909 dw2_asm_output_encoded_addr_rtx (per_encoding,
910 personality,
911 true, NULL);
914 if (any_lsda_needed)
915 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
916 eh_data_format_name (lsda_encoding));
918 if (fde_encoding != DW_EH_PE_absptr)
919 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
920 eh_data_format_name (fde_encoding));
923 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
924 output_cfi (cfi, NULL, for_eh);
926 /* Pad the CIE out to an address sized boundary. */
927 ASM_OUTPUT_ALIGN (asm_out_file,
928 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
929 ASM_OUTPUT_LABEL (asm_out_file, l2);
931 /* Loop through all of the FDE's. */
932 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
934 unsigned int k;
936 /* Don't emit EH unwind info for leaf functions that don't need it. */
937 if (for_eh && !fde_needed_for_eh_p (fde))
938 continue;
940 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
941 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
942 augmentation, any_lsda_needed, lsda_encoding);
945 if (for_eh && targetm.terminate_dw2_eh_frame_info)
946 dw2_asm_output_data (4, 0, "End of Table");
948 /* Turn off app to make assembly quicker. */
949 if (flag_debug_asm)
950 app_disable ();
953 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
955 static void
956 dwarf2out_do_cfi_startproc (bool second)
958 int enc;
959 rtx ref;
960 rtx personality = get_personality_function (current_function_decl);
962 fprintf (asm_out_file, "\t.cfi_startproc\n");
964 if (personality)
966 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
967 ref = personality;
969 /* ??? The GAS support isn't entirely consistent. We have to
970 handle indirect support ourselves, but PC-relative is done
971 in the assembler. Further, the assembler can't handle any
972 of the weirder relocation types. */
973 if (enc & DW_EH_PE_indirect)
974 ref = dw2_force_const_mem (ref, true);
976 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
977 output_addr_const (asm_out_file, ref);
978 fputc ('\n', asm_out_file);
981 if (crtl->uses_eh_lsda)
983 char lab[20];
985 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
986 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
987 current_function_funcdef_no);
988 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
989 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
991 if (enc & DW_EH_PE_indirect)
992 ref = dw2_force_const_mem (ref, true);
994 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
995 output_addr_const (asm_out_file, ref);
996 fputc ('\n', asm_out_file);
1000 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1001 this allocation may be done before pass_final. */
1003 dw_fde_ref
1004 dwarf2out_alloc_current_fde (void)
1006 dw_fde_ref fde;
1008 fde = ggc_cleared_alloc<dw_fde_node> ();
1009 fde->decl = current_function_decl;
1010 fde->funcdef_number = current_function_funcdef_no;
1011 fde->fde_index = vec_safe_length (fde_vec);
1012 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1013 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1014 fde->nothrow = crtl->nothrow;
1015 fde->drap_reg = INVALID_REGNUM;
1016 fde->vdrap_reg = INVALID_REGNUM;
1018 /* Record the FDE associated with this function. */
1019 cfun->fde = fde;
1020 vec_safe_push (fde_vec, fde);
1022 return fde;
1025 /* Output a marker (i.e. a label) for the beginning of a function, before
1026 the prologue. */
1028 void
1029 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1030 const char *file ATTRIBUTE_UNUSED)
1032 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1033 char * dup_label;
1034 dw_fde_ref fde;
1035 section *fnsec;
1036 bool do_frame;
1038 current_function_func_begin_label = NULL;
1040 do_frame = dwarf2out_do_frame ();
1042 /* ??? current_function_func_begin_label is also used by except.c for
1043 call-site information. We must emit this label if it might be used. */
1044 if (!do_frame
1045 && (!flag_exceptions
1046 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1047 return;
1049 fnsec = function_section (current_function_decl);
1050 switch_to_section (fnsec);
1051 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1052 current_function_funcdef_no);
1053 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1054 current_function_funcdef_no);
1055 dup_label = xstrdup (label);
1056 current_function_func_begin_label = dup_label;
1058 /* We can elide the fde allocation if we're not emitting debug info. */
1059 if (!do_frame)
1060 return;
1062 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1063 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1064 would include pass_dwarf2_frame. If we've not created the FDE yet,
1065 do so now. */
1066 fde = cfun->fde;
1067 if (fde == NULL)
1068 fde = dwarf2out_alloc_current_fde ();
1070 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1071 fde->dw_fde_begin = dup_label;
1072 fde->dw_fde_current_label = dup_label;
1073 fde->in_std_section = (fnsec == text_section
1074 || (cold_text_section && fnsec == cold_text_section));
1076 /* We only want to output line number information for the genuine dwarf2
1077 prologue case, not the eh frame case. */
1078 #ifdef DWARF2_DEBUGGING_INFO
1079 if (file)
1080 dwarf2out_source_line (line, file, 0, true);
1081 #endif
1083 if (dwarf2out_do_cfi_asm ())
1084 dwarf2out_do_cfi_startproc (false);
1085 else
1087 rtx personality = get_personality_function (current_function_decl);
1088 if (!current_unit_personality)
1089 current_unit_personality = personality;
1091 /* We cannot keep a current personality per function as without CFI
1092 asm, at the point where we emit the CFI data, there is no current
1093 function anymore. */
1094 if (personality && current_unit_personality != personality)
1095 sorry ("multiple EH personalities are supported only with assemblers "
1096 "supporting .cfi_personality directive");
1100 /* Output a marker (i.e. a label) for the end of the generated code
1101 for a function prologue. This gets called *after* the prologue code has
1102 been generated. */
1104 void
1105 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1106 const char *file ATTRIBUTE_UNUSED)
1108 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1110 /* Output a label to mark the endpoint of the code generated for this
1111 function. */
1112 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1113 current_function_funcdef_no);
1114 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1115 current_function_funcdef_no);
1116 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1119 /* Output a marker (i.e. a label) for the beginning of the generated code
1120 for a function epilogue. This gets called *before* the prologue code has
1121 been generated. */
1123 void
1124 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1125 const char *file ATTRIBUTE_UNUSED)
1127 dw_fde_ref fde = cfun->fde;
1128 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1130 if (fde->dw_fde_vms_begin_epilogue)
1131 return;
1133 /* Output a label to mark the endpoint of the code generated for this
1134 function. */
1135 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1136 current_function_funcdef_no);
1137 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1138 current_function_funcdef_no);
1139 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1142 /* Output a marker (i.e. a label) for the absolute end of the generated code
1143 for a function definition. This gets called *after* the epilogue code has
1144 been generated. */
1146 void
1147 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1148 const char *file ATTRIBUTE_UNUSED)
1150 dw_fde_ref fde;
1151 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1153 last_var_location_insn = NULL;
1154 cached_next_real_insn = NULL;
1156 if (dwarf2out_do_cfi_asm ())
1157 fprintf (asm_out_file, "\t.cfi_endproc\n");
1159 /* Output a label to mark the endpoint of the code generated for this
1160 function. */
1161 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1162 current_function_funcdef_no);
1163 ASM_OUTPUT_LABEL (asm_out_file, label);
1164 fde = cfun->fde;
1165 gcc_assert (fde != NULL);
1166 if (fde->dw_fde_second_begin == NULL)
1167 fde->dw_fde_end = xstrdup (label);
1170 void
1171 dwarf2out_frame_finish (void)
1173 /* Output call frame information. */
1174 if (targetm.debug_unwind_info () == UI_DWARF2)
1175 output_call_frame_info (0);
1177 /* Output another copy for the unwinder. */
1178 if ((flag_unwind_tables || flag_exceptions)
1179 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1180 output_call_frame_info (1);
1183 /* Note that the current function section is being used for code. */
1185 static void
1186 dwarf2out_note_section_used (void)
1188 section *sec = current_function_section ();
1189 if (sec == text_section)
1190 text_section_used = true;
1191 else if (sec == cold_text_section)
1192 cold_text_section_used = true;
1195 static void var_location_switch_text_section (void);
1196 static void set_cur_line_info_table (section *);
1198 void
1199 dwarf2out_switch_text_section (void)
1201 section *sect;
1202 dw_fde_ref fde = cfun->fde;
1204 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1206 if (!in_cold_section_p)
1208 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1209 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1210 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1212 else
1214 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1215 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1216 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1218 have_multiple_function_sections = true;
1220 /* There is no need to mark used sections when not debugging. */
1221 if (cold_text_section != NULL)
1222 dwarf2out_note_section_used ();
1224 if (dwarf2out_do_cfi_asm ())
1225 fprintf (asm_out_file, "\t.cfi_endproc\n");
1227 /* Now do the real section switch. */
1228 sect = current_function_section ();
1229 switch_to_section (sect);
1231 fde->second_in_std_section
1232 = (sect == text_section
1233 || (cold_text_section && sect == cold_text_section));
1235 if (dwarf2out_do_cfi_asm ())
1236 dwarf2out_do_cfi_startproc (true);
1238 var_location_switch_text_section ();
1240 if (cold_text_section != NULL)
1241 set_cur_line_info_table (sect);
1244 /* And now, the subset of the debugging information support code necessary
1245 for emitting location expressions. */
1247 /* Data about a single source file. */
1248 struct GTY((for_user)) dwarf_file_data {
1249 const char * filename;
1250 int emitted_number;
1253 typedef struct GTY(()) deferred_locations_struct
1255 tree variable;
1256 dw_die_ref die;
1257 } deferred_locations;
1260 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1263 /* Describe an entry into the .debug_addr section. */
1265 enum ate_kind {
1266 ate_kind_rtx,
1267 ate_kind_rtx_dtprel,
1268 ate_kind_label
1271 typedef struct GTY((for_user)) addr_table_entry_struct {
1272 enum ate_kind kind;
1273 unsigned int refcount;
1274 unsigned int index;
1275 union addr_table_entry_struct_union
1277 rtx GTY ((tag ("0"))) rtl;
1278 char * GTY ((tag ("1"))) label;
1280 GTY ((desc ("%1.kind"))) addr;
1282 addr_table_entry;
1284 /* Location lists are ranges + location descriptions for that range,
1285 so you can track variables that are in different places over
1286 their entire life. */
1287 typedef struct GTY(()) dw_loc_list_struct {
1288 dw_loc_list_ref dw_loc_next;
1289 const char *begin; /* Label and addr_entry for start of range */
1290 addr_table_entry *begin_entry;
1291 const char *end; /* Label for end of range */
1292 char *ll_symbol; /* Label for beginning of location list.
1293 Only on head of list */
1294 const char *section; /* Section this loclist is relative to */
1295 dw_loc_descr_ref expr;
1296 hashval_t hash;
1297 /* True if all addresses in this and subsequent lists are known to be
1298 resolved. */
1299 bool resolved_addr;
1300 /* True if this list has been replaced by dw_loc_next. */
1301 bool replaced;
1302 bool emitted;
1303 /* True if the range should be emitted even if begin and end
1304 are the same. */
1305 bool force;
1306 } dw_loc_list_node;
1308 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1310 /* Convert a DWARF stack opcode into its string name. */
1312 static const char *
1313 dwarf_stack_op_name (unsigned int op)
1315 const char *name = get_DW_OP_name (op);
1317 if (name != NULL)
1318 return name;
1320 return "OP_<unknown>";
1323 /* Return a pointer to a newly allocated location description. Location
1324 descriptions are simple expression terms that can be strung
1325 together to form more complicated location (address) descriptions. */
1327 static inline dw_loc_descr_ref
1328 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1329 unsigned HOST_WIDE_INT oprnd2)
1331 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1333 descr->dw_loc_opc = op;
1334 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1335 descr->dw_loc_oprnd1.val_entry = NULL;
1336 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1337 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1338 descr->dw_loc_oprnd2.val_entry = NULL;
1339 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1341 return descr;
1344 /* Return a pointer to a newly allocated location description for
1345 REG and OFFSET. */
1347 static inline dw_loc_descr_ref
1348 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1350 if (reg <= 31)
1351 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1352 offset, 0);
1353 else
1354 return new_loc_descr (DW_OP_bregx, reg, offset);
1357 /* Add a location description term to a location description expression. */
1359 static inline void
1360 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1362 dw_loc_descr_ref *d;
1364 /* Find the end of the chain. */
1365 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1368 *d = descr;
1371 /* Compare two location operands for exact equality. */
1373 static bool
1374 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1376 if (a->val_class != b->val_class)
1377 return false;
1378 switch (a->val_class)
1380 case dw_val_class_none:
1381 return true;
1382 case dw_val_class_addr:
1383 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1385 case dw_val_class_offset:
1386 case dw_val_class_unsigned_const:
1387 case dw_val_class_const:
1388 case dw_val_class_range_list:
1389 case dw_val_class_lineptr:
1390 case dw_val_class_macptr:
1391 /* These are all HOST_WIDE_INT, signed or unsigned. */
1392 return a->v.val_unsigned == b->v.val_unsigned;
1394 case dw_val_class_loc:
1395 return a->v.val_loc == b->v.val_loc;
1396 case dw_val_class_loc_list:
1397 return a->v.val_loc_list == b->v.val_loc_list;
1398 case dw_val_class_die_ref:
1399 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1400 case dw_val_class_fde_ref:
1401 return a->v.val_fde_index == b->v.val_fde_index;
1402 case dw_val_class_lbl_id:
1403 case dw_val_class_high_pc:
1404 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1405 case dw_val_class_str:
1406 return a->v.val_str == b->v.val_str;
1407 case dw_val_class_flag:
1408 return a->v.val_flag == b->v.val_flag;
1409 case dw_val_class_file:
1410 return a->v.val_file == b->v.val_file;
1411 case dw_val_class_decl_ref:
1412 return a->v.val_decl_ref == b->v.val_decl_ref;
1414 case dw_val_class_const_double:
1415 return (a->v.val_double.high == b->v.val_double.high
1416 && a->v.val_double.low == b->v.val_double.low);
1418 case dw_val_class_wide_int:
1419 return *a->v.val_wide == *b->v.val_wide;
1421 case dw_val_class_vec:
1423 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1424 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1426 return (a_len == b_len
1427 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1430 case dw_val_class_data8:
1431 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1433 case dw_val_class_vms_delta:
1434 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1435 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1437 gcc_unreachable ();
1440 /* Compare two location atoms for exact equality. */
1442 static bool
1443 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1445 if (a->dw_loc_opc != b->dw_loc_opc)
1446 return false;
1448 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1449 address size, but since we always allocate cleared storage it
1450 should be zero for other types of locations. */
1451 if (a->dtprel != b->dtprel)
1452 return false;
1454 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1455 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1458 /* Compare two complete location expressions for exact equality. */
1460 bool
1461 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1463 while (1)
1465 if (a == b)
1466 return true;
1467 if (a == NULL || b == NULL)
1468 return false;
1469 if (!loc_descr_equal_p_1 (a, b))
1470 return false;
1472 a = a->dw_loc_next;
1473 b = b->dw_loc_next;
1478 /* Add a constant OFFSET to a location expression. */
1480 static void
1481 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1483 dw_loc_descr_ref loc;
1484 HOST_WIDE_INT *p;
1486 gcc_assert (*list_head != NULL);
1488 if (!offset)
1489 return;
1491 /* Find the end of the chain. */
1492 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1495 p = NULL;
1496 if (loc->dw_loc_opc == DW_OP_fbreg
1497 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1498 p = &loc->dw_loc_oprnd1.v.val_int;
1499 else if (loc->dw_loc_opc == DW_OP_bregx)
1500 p = &loc->dw_loc_oprnd2.v.val_int;
1502 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1503 offset. Don't optimize if an signed integer overflow would happen. */
1504 if (p != NULL
1505 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1506 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1507 *p += offset;
1509 else if (offset > 0)
1510 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1512 else
1514 loc->dw_loc_next = int_loc_descriptor (-offset);
1515 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1519 /* Add a constant OFFSET to a location list. */
1521 static void
1522 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1524 dw_loc_list_ref d;
1525 for (d = list_head; d != NULL; d = d->dw_loc_next)
1526 loc_descr_plus_const (&d->expr, offset);
1529 #define DWARF_REF_SIZE \
1530 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1532 static unsigned long int get_base_type_offset (dw_die_ref);
1534 /* Return the size of a location descriptor. */
1536 static unsigned long
1537 size_of_loc_descr (dw_loc_descr_ref loc)
1539 unsigned long size = 1;
1541 switch (loc->dw_loc_opc)
1543 case DW_OP_addr:
1544 size += DWARF2_ADDR_SIZE;
1545 break;
1546 case DW_OP_GNU_addr_index:
1547 case DW_OP_GNU_const_index:
1548 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1549 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1550 break;
1551 case DW_OP_const1u:
1552 case DW_OP_const1s:
1553 size += 1;
1554 break;
1555 case DW_OP_const2u:
1556 case DW_OP_const2s:
1557 size += 2;
1558 break;
1559 case DW_OP_const4u:
1560 case DW_OP_const4s:
1561 size += 4;
1562 break;
1563 case DW_OP_const8u:
1564 case DW_OP_const8s:
1565 size += 8;
1566 break;
1567 case DW_OP_constu:
1568 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1569 break;
1570 case DW_OP_consts:
1571 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1572 break;
1573 case DW_OP_pick:
1574 size += 1;
1575 break;
1576 case DW_OP_plus_uconst:
1577 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1578 break;
1579 case DW_OP_skip:
1580 case DW_OP_bra:
1581 size += 2;
1582 break;
1583 case DW_OP_breg0:
1584 case DW_OP_breg1:
1585 case DW_OP_breg2:
1586 case DW_OP_breg3:
1587 case DW_OP_breg4:
1588 case DW_OP_breg5:
1589 case DW_OP_breg6:
1590 case DW_OP_breg7:
1591 case DW_OP_breg8:
1592 case DW_OP_breg9:
1593 case DW_OP_breg10:
1594 case DW_OP_breg11:
1595 case DW_OP_breg12:
1596 case DW_OP_breg13:
1597 case DW_OP_breg14:
1598 case DW_OP_breg15:
1599 case DW_OP_breg16:
1600 case DW_OP_breg17:
1601 case DW_OP_breg18:
1602 case DW_OP_breg19:
1603 case DW_OP_breg20:
1604 case DW_OP_breg21:
1605 case DW_OP_breg22:
1606 case DW_OP_breg23:
1607 case DW_OP_breg24:
1608 case DW_OP_breg25:
1609 case DW_OP_breg26:
1610 case DW_OP_breg27:
1611 case DW_OP_breg28:
1612 case DW_OP_breg29:
1613 case DW_OP_breg30:
1614 case DW_OP_breg31:
1615 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1616 break;
1617 case DW_OP_regx:
1618 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1619 break;
1620 case DW_OP_fbreg:
1621 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1622 break;
1623 case DW_OP_bregx:
1624 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1625 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1626 break;
1627 case DW_OP_piece:
1628 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1629 break;
1630 case DW_OP_bit_piece:
1631 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1632 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1633 break;
1634 case DW_OP_deref_size:
1635 case DW_OP_xderef_size:
1636 size += 1;
1637 break;
1638 case DW_OP_call2:
1639 size += 2;
1640 break;
1641 case DW_OP_call4:
1642 size += 4;
1643 break;
1644 case DW_OP_call_ref:
1645 size += DWARF_REF_SIZE;
1646 break;
1647 case DW_OP_implicit_value:
1648 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1649 + loc->dw_loc_oprnd1.v.val_unsigned;
1650 break;
1651 case DW_OP_GNU_implicit_pointer:
1652 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1653 break;
1654 case DW_OP_GNU_entry_value:
1656 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1657 size += size_of_uleb128 (op_size) + op_size;
1658 break;
1660 case DW_OP_GNU_const_type:
1662 unsigned long o
1663 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1664 size += size_of_uleb128 (o) + 1;
1665 switch (loc->dw_loc_oprnd2.val_class)
1667 case dw_val_class_vec:
1668 size += loc->dw_loc_oprnd2.v.val_vec.length
1669 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1670 break;
1671 case dw_val_class_const:
1672 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1673 break;
1674 case dw_val_class_const_double:
1675 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1676 break;
1677 case dw_val_class_wide_int:
1678 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1679 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1680 break;
1681 default:
1682 gcc_unreachable ();
1684 break;
1686 case DW_OP_GNU_regval_type:
1688 unsigned long o
1689 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1690 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1691 + size_of_uleb128 (o);
1693 break;
1694 case DW_OP_GNU_deref_type:
1696 unsigned long o
1697 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1698 size += 1 + size_of_uleb128 (o);
1700 break;
1701 case DW_OP_GNU_convert:
1702 case DW_OP_GNU_reinterpret:
1703 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1704 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1705 else
1707 unsigned long o
1708 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1709 size += size_of_uleb128 (o);
1711 break;
1712 case DW_OP_GNU_parameter_ref:
1713 size += 4;
1714 break;
1715 default:
1716 break;
1719 return size;
1722 /* Return the size of a series of location descriptors. */
1724 unsigned long
1725 size_of_locs (dw_loc_descr_ref loc)
1727 dw_loc_descr_ref l;
1728 unsigned long size;
1730 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1731 field, to avoid writing to a PCH file. */
1732 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1734 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1735 break;
1736 size += size_of_loc_descr (l);
1738 if (! l)
1739 return size;
1741 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1743 l->dw_loc_addr = size;
1744 size += size_of_loc_descr (l);
1747 return size;
1750 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1751 static void get_ref_die_offset_label (char *, dw_die_ref);
1752 static unsigned long int get_ref_die_offset (dw_die_ref);
1754 /* Output location description stack opcode's operands (if any).
1755 The for_eh_or_skip parameter controls whether register numbers are
1756 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1757 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1758 info). This should be suppressed for the cases that have not been converted
1759 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1761 static void
1762 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1764 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1765 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1767 switch (loc->dw_loc_opc)
1769 #ifdef DWARF2_DEBUGGING_INFO
1770 case DW_OP_const2u:
1771 case DW_OP_const2s:
1772 dw2_asm_output_data (2, val1->v.val_int, NULL);
1773 break;
1774 case DW_OP_const4u:
1775 if (loc->dtprel)
1777 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1778 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1779 val1->v.val_addr);
1780 fputc ('\n', asm_out_file);
1781 break;
1783 /* FALLTHRU */
1784 case DW_OP_const4s:
1785 dw2_asm_output_data (4, val1->v.val_int, NULL);
1786 break;
1787 case DW_OP_const8u:
1788 if (loc->dtprel)
1790 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1791 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1792 val1->v.val_addr);
1793 fputc ('\n', asm_out_file);
1794 break;
1796 /* FALLTHRU */
1797 case DW_OP_const8s:
1798 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1799 dw2_asm_output_data (8, val1->v.val_int, NULL);
1800 break;
1801 case DW_OP_skip:
1802 case DW_OP_bra:
1804 int offset;
1806 gcc_assert (val1->val_class == dw_val_class_loc);
1807 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1809 dw2_asm_output_data (2, offset, NULL);
1811 break;
1812 case DW_OP_implicit_value:
1813 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1814 switch (val2->val_class)
1816 case dw_val_class_const:
1817 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1818 break;
1819 case dw_val_class_vec:
1821 unsigned int elt_size = val2->v.val_vec.elt_size;
1822 unsigned int len = val2->v.val_vec.length;
1823 unsigned int i;
1824 unsigned char *p;
1826 if (elt_size > sizeof (HOST_WIDE_INT))
1828 elt_size /= 2;
1829 len *= 2;
1831 for (i = 0, p = val2->v.val_vec.array;
1832 i < len;
1833 i++, p += elt_size)
1834 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1835 "fp or vector constant word %u", i);
1837 break;
1838 case dw_val_class_const_double:
1840 unsigned HOST_WIDE_INT first, second;
1842 if (WORDS_BIG_ENDIAN)
1844 first = val2->v.val_double.high;
1845 second = val2->v.val_double.low;
1847 else
1849 first = val2->v.val_double.low;
1850 second = val2->v.val_double.high;
1852 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1853 first, NULL);
1854 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1855 second, NULL);
1857 break;
1858 case dw_val_class_wide_int:
1860 int i;
1861 int len = get_full_len (*val2->v.val_wide);
1862 if (WORDS_BIG_ENDIAN)
1863 for (i = len - 1; i >= 0; --i)
1864 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1865 val2->v.val_wide->elt (i), NULL);
1866 else
1867 for (i = 0; i < len; ++i)
1868 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1869 val2->v.val_wide->elt (i), NULL);
1871 break;
1872 case dw_val_class_addr:
1873 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1874 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1875 break;
1876 default:
1877 gcc_unreachable ();
1879 break;
1880 #else
1881 case DW_OP_const2u:
1882 case DW_OP_const2s:
1883 case DW_OP_const4u:
1884 case DW_OP_const4s:
1885 case DW_OP_const8u:
1886 case DW_OP_const8s:
1887 case DW_OP_skip:
1888 case DW_OP_bra:
1889 case DW_OP_implicit_value:
1890 /* We currently don't make any attempt to make sure these are
1891 aligned properly like we do for the main unwind info, so
1892 don't support emitting things larger than a byte if we're
1893 only doing unwinding. */
1894 gcc_unreachable ();
1895 #endif
1896 case DW_OP_const1u:
1897 case DW_OP_const1s:
1898 dw2_asm_output_data (1, val1->v.val_int, NULL);
1899 break;
1900 case DW_OP_constu:
1901 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1902 break;
1903 case DW_OP_consts:
1904 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1905 break;
1906 case DW_OP_pick:
1907 dw2_asm_output_data (1, val1->v.val_int, NULL);
1908 break;
1909 case DW_OP_plus_uconst:
1910 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1911 break;
1912 case DW_OP_breg0:
1913 case DW_OP_breg1:
1914 case DW_OP_breg2:
1915 case DW_OP_breg3:
1916 case DW_OP_breg4:
1917 case DW_OP_breg5:
1918 case DW_OP_breg6:
1919 case DW_OP_breg7:
1920 case DW_OP_breg8:
1921 case DW_OP_breg9:
1922 case DW_OP_breg10:
1923 case DW_OP_breg11:
1924 case DW_OP_breg12:
1925 case DW_OP_breg13:
1926 case DW_OP_breg14:
1927 case DW_OP_breg15:
1928 case DW_OP_breg16:
1929 case DW_OP_breg17:
1930 case DW_OP_breg18:
1931 case DW_OP_breg19:
1932 case DW_OP_breg20:
1933 case DW_OP_breg21:
1934 case DW_OP_breg22:
1935 case DW_OP_breg23:
1936 case DW_OP_breg24:
1937 case DW_OP_breg25:
1938 case DW_OP_breg26:
1939 case DW_OP_breg27:
1940 case DW_OP_breg28:
1941 case DW_OP_breg29:
1942 case DW_OP_breg30:
1943 case DW_OP_breg31:
1944 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1945 break;
1946 case DW_OP_regx:
1948 unsigned r = val1->v.val_unsigned;
1949 if (for_eh_or_skip >= 0)
1950 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1951 gcc_assert (size_of_uleb128 (r)
1952 == size_of_uleb128 (val1->v.val_unsigned));
1953 dw2_asm_output_data_uleb128 (r, NULL);
1955 break;
1956 case DW_OP_fbreg:
1957 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1958 break;
1959 case DW_OP_bregx:
1961 unsigned r = val1->v.val_unsigned;
1962 if (for_eh_or_skip >= 0)
1963 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1964 gcc_assert (size_of_uleb128 (r)
1965 == size_of_uleb128 (val1->v.val_unsigned));
1966 dw2_asm_output_data_uleb128 (r, NULL);
1967 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1969 break;
1970 case DW_OP_piece:
1971 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1972 break;
1973 case DW_OP_bit_piece:
1974 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1975 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1976 break;
1977 case DW_OP_deref_size:
1978 case DW_OP_xderef_size:
1979 dw2_asm_output_data (1, val1->v.val_int, NULL);
1980 break;
1982 case DW_OP_addr:
1983 if (loc->dtprel)
1985 if (targetm.asm_out.output_dwarf_dtprel)
1987 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1988 DWARF2_ADDR_SIZE,
1989 val1->v.val_addr);
1990 fputc ('\n', asm_out_file);
1992 else
1993 gcc_unreachable ();
1995 else
1997 #ifdef DWARF2_DEBUGGING_INFO
1998 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1999 #else
2000 gcc_unreachable ();
2001 #endif
2003 break;
2005 case DW_OP_GNU_addr_index:
2006 case DW_OP_GNU_const_index:
2007 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2008 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2009 "(index into .debug_addr)");
2010 break;
2012 case DW_OP_GNU_implicit_pointer:
2014 char label[MAX_ARTIFICIAL_LABEL_BYTES
2015 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2016 gcc_assert (val1->val_class == dw_val_class_die_ref);
2017 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2018 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2019 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2021 break;
2023 case DW_OP_GNU_entry_value:
2024 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2025 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2026 break;
2028 case DW_OP_GNU_const_type:
2030 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2031 gcc_assert (o);
2032 dw2_asm_output_data_uleb128 (o, NULL);
2033 switch (val2->val_class)
2035 case dw_val_class_const:
2036 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2037 dw2_asm_output_data (1, l, NULL);
2038 dw2_asm_output_data (l, val2->v.val_int, NULL);
2039 break;
2040 case dw_val_class_vec:
2042 unsigned int elt_size = val2->v.val_vec.elt_size;
2043 unsigned int len = val2->v.val_vec.length;
2044 unsigned int i;
2045 unsigned char *p;
2047 l = len * elt_size;
2048 dw2_asm_output_data (1, l, NULL);
2049 if (elt_size > sizeof (HOST_WIDE_INT))
2051 elt_size /= 2;
2052 len *= 2;
2054 for (i = 0, p = val2->v.val_vec.array;
2055 i < len;
2056 i++, p += elt_size)
2057 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2058 "fp or vector constant word %u", i);
2060 break;
2061 case dw_val_class_const_double:
2063 unsigned HOST_WIDE_INT first, second;
2064 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2066 dw2_asm_output_data (1, 2 * l, NULL);
2067 if (WORDS_BIG_ENDIAN)
2069 first = val2->v.val_double.high;
2070 second = val2->v.val_double.low;
2072 else
2074 first = val2->v.val_double.low;
2075 second = val2->v.val_double.high;
2077 dw2_asm_output_data (l, first, NULL);
2078 dw2_asm_output_data (l, second, NULL);
2080 break;
2081 case dw_val_class_wide_int:
2083 int i;
2084 int len = get_full_len (*val2->v.val_wide);
2085 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2087 dw2_asm_output_data (1, len * l, NULL);
2088 if (WORDS_BIG_ENDIAN)
2089 for (i = len - 1; i >= 0; --i)
2090 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2091 else
2092 for (i = 0; i < len; ++i)
2093 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2095 break;
2096 default:
2097 gcc_unreachable ();
2100 break;
2101 case DW_OP_GNU_regval_type:
2103 unsigned r = val1->v.val_unsigned;
2104 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2105 gcc_assert (o);
2106 if (for_eh_or_skip >= 0)
2108 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2109 gcc_assert (size_of_uleb128 (r)
2110 == size_of_uleb128 (val1->v.val_unsigned));
2112 dw2_asm_output_data_uleb128 (r, NULL);
2113 dw2_asm_output_data_uleb128 (o, NULL);
2115 break;
2116 case DW_OP_GNU_deref_type:
2118 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2119 gcc_assert (o);
2120 dw2_asm_output_data (1, val1->v.val_int, NULL);
2121 dw2_asm_output_data_uleb128 (o, NULL);
2123 break;
2124 case DW_OP_GNU_convert:
2125 case DW_OP_GNU_reinterpret:
2126 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2127 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2128 else
2130 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2131 gcc_assert (o);
2132 dw2_asm_output_data_uleb128 (o, NULL);
2134 break;
2136 case DW_OP_GNU_parameter_ref:
2138 unsigned long o;
2139 gcc_assert (val1->val_class == dw_val_class_die_ref);
2140 o = get_ref_die_offset (val1->v.val_die_ref.die);
2141 dw2_asm_output_data (4, o, NULL);
2143 break;
2145 default:
2146 /* Other codes have no operands. */
2147 break;
2151 /* Output a sequence of location operations.
2152 The for_eh_or_skip parameter controls whether register numbers are
2153 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2154 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2155 info). This should be suppressed for the cases that have not been converted
2156 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2158 void
2159 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2161 for (; loc != NULL; loc = loc->dw_loc_next)
2163 enum dwarf_location_atom opc = loc->dw_loc_opc;
2164 /* Output the opcode. */
2165 if (for_eh_or_skip >= 0
2166 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2168 unsigned r = (opc - DW_OP_breg0);
2169 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2170 gcc_assert (r <= 31);
2171 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2173 else if (for_eh_or_skip >= 0
2174 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2176 unsigned r = (opc - DW_OP_reg0);
2177 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2178 gcc_assert (r <= 31);
2179 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2182 dw2_asm_output_data (1, opc,
2183 "%s", dwarf_stack_op_name (opc));
2185 /* Output the operand(s) (if any). */
2186 output_loc_operands (loc, for_eh_or_skip);
2190 /* Output location description stack opcode's operands (if any).
2191 The output is single bytes on a line, suitable for .cfi_escape. */
2193 static void
2194 output_loc_operands_raw (dw_loc_descr_ref loc)
2196 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2197 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2199 switch (loc->dw_loc_opc)
2201 case DW_OP_addr:
2202 case DW_OP_GNU_addr_index:
2203 case DW_OP_GNU_const_index:
2204 case DW_OP_implicit_value:
2205 /* We cannot output addresses in .cfi_escape, only bytes. */
2206 gcc_unreachable ();
2208 case DW_OP_const1u:
2209 case DW_OP_const1s:
2210 case DW_OP_pick:
2211 case DW_OP_deref_size:
2212 case DW_OP_xderef_size:
2213 fputc (',', asm_out_file);
2214 dw2_asm_output_data_raw (1, val1->v.val_int);
2215 break;
2217 case DW_OP_const2u:
2218 case DW_OP_const2s:
2219 fputc (',', asm_out_file);
2220 dw2_asm_output_data_raw (2, val1->v.val_int);
2221 break;
2223 case DW_OP_const4u:
2224 case DW_OP_const4s:
2225 fputc (',', asm_out_file);
2226 dw2_asm_output_data_raw (4, val1->v.val_int);
2227 break;
2229 case DW_OP_const8u:
2230 case DW_OP_const8s:
2231 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2232 fputc (',', asm_out_file);
2233 dw2_asm_output_data_raw (8, val1->v.val_int);
2234 break;
2236 case DW_OP_skip:
2237 case DW_OP_bra:
2239 int offset;
2241 gcc_assert (val1->val_class == dw_val_class_loc);
2242 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2244 fputc (',', asm_out_file);
2245 dw2_asm_output_data_raw (2, offset);
2247 break;
2249 case DW_OP_regx:
2251 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2252 gcc_assert (size_of_uleb128 (r)
2253 == size_of_uleb128 (val1->v.val_unsigned));
2254 fputc (',', asm_out_file);
2255 dw2_asm_output_data_uleb128_raw (r);
2257 break;
2259 case DW_OP_constu:
2260 case DW_OP_plus_uconst:
2261 case DW_OP_piece:
2262 fputc (',', asm_out_file);
2263 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2264 break;
2266 case DW_OP_bit_piece:
2267 fputc (',', asm_out_file);
2268 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2269 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2270 break;
2272 case DW_OP_consts:
2273 case DW_OP_breg0:
2274 case DW_OP_breg1:
2275 case DW_OP_breg2:
2276 case DW_OP_breg3:
2277 case DW_OP_breg4:
2278 case DW_OP_breg5:
2279 case DW_OP_breg6:
2280 case DW_OP_breg7:
2281 case DW_OP_breg8:
2282 case DW_OP_breg9:
2283 case DW_OP_breg10:
2284 case DW_OP_breg11:
2285 case DW_OP_breg12:
2286 case DW_OP_breg13:
2287 case DW_OP_breg14:
2288 case DW_OP_breg15:
2289 case DW_OP_breg16:
2290 case DW_OP_breg17:
2291 case DW_OP_breg18:
2292 case DW_OP_breg19:
2293 case DW_OP_breg20:
2294 case DW_OP_breg21:
2295 case DW_OP_breg22:
2296 case DW_OP_breg23:
2297 case DW_OP_breg24:
2298 case DW_OP_breg25:
2299 case DW_OP_breg26:
2300 case DW_OP_breg27:
2301 case DW_OP_breg28:
2302 case DW_OP_breg29:
2303 case DW_OP_breg30:
2304 case DW_OP_breg31:
2305 case DW_OP_fbreg:
2306 fputc (',', asm_out_file);
2307 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2308 break;
2310 case DW_OP_bregx:
2312 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2313 gcc_assert (size_of_uleb128 (r)
2314 == size_of_uleb128 (val1->v.val_unsigned));
2315 fputc (',', asm_out_file);
2316 dw2_asm_output_data_uleb128_raw (r);
2317 fputc (',', asm_out_file);
2318 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2320 break;
2322 case DW_OP_GNU_implicit_pointer:
2323 case DW_OP_GNU_entry_value:
2324 case DW_OP_GNU_const_type:
2325 case DW_OP_GNU_regval_type:
2326 case DW_OP_GNU_deref_type:
2327 case DW_OP_GNU_convert:
2328 case DW_OP_GNU_reinterpret:
2329 case DW_OP_GNU_parameter_ref:
2330 gcc_unreachable ();
2331 break;
2333 default:
2334 /* Other codes have no operands. */
2335 break;
2339 void
2340 output_loc_sequence_raw (dw_loc_descr_ref loc)
2342 while (1)
2344 enum dwarf_location_atom opc = loc->dw_loc_opc;
2345 /* Output the opcode. */
2346 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2348 unsigned r = (opc - DW_OP_breg0);
2349 r = DWARF2_FRAME_REG_OUT (r, 1);
2350 gcc_assert (r <= 31);
2351 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2353 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2355 unsigned r = (opc - DW_OP_reg0);
2356 r = DWARF2_FRAME_REG_OUT (r, 1);
2357 gcc_assert (r <= 31);
2358 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2360 /* Output the opcode. */
2361 fprintf (asm_out_file, "%#x", opc);
2362 output_loc_operands_raw (loc);
2364 if (!loc->dw_loc_next)
2365 break;
2366 loc = loc->dw_loc_next;
2368 fputc (',', asm_out_file);
2372 /* This function builds a dwarf location descriptor sequence from a
2373 dw_cfa_location, adding the given OFFSET to the result of the
2374 expression. */
2376 struct dw_loc_descr_node *
2377 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2379 struct dw_loc_descr_node *head, *tmp;
2381 offset += cfa->offset;
2383 if (cfa->indirect)
2385 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2386 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2387 head->dw_loc_oprnd1.val_entry = NULL;
2388 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2389 add_loc_descr (&head, tmp);
2390 if (offset != 0)
2392 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2393 add_loc_descr (&head, tmp);
2396 else
2397 head = new_reg_loc_descr (cfa->reg, offset);
2399 return head;
2402 /* This function builds a dwarf location descriptor sequence for
2403 the address at OFFSET from the CFA when stack is aligned to
2404 ALIGNMENT byte. */
2406 struct dw_loc_descr_node *
2407 build_cfa_aligned_loc (dw_cfa_location *cfa,
2408 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2410 struct dw_loc_descr_node *head;
2411 unsigned int dwarf_fp
2412 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2414 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2415 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2417 head = new_reg_loc_descr (dwarf_fp, 0);
2418 add_loc_descr (&head, int_loc_descriptor (alignment));
2419 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2420 loc_descr_plus_const (&head, offset);
2422 else
2423 head = new_reg_loc_descr (dwarf_fp, offset);
2424 return head;
2427 /* And now, the support for symbolic debugging information. */
2429 /* .debug_str support. */
2431 static void dwarf2out_init (const char *);
2432 static void dwarf2out_finish (const char *);
2433 static void dwarf2out_assembly_start (void);
2434 static void dwarf2out_define (unsigned int, const char *);
2435 static void dwarf2out_undef (unsigned int, const char *);
2436 static void dwarf2out_start_source_file (unsigned, const char *);
2437 static void dwarf2out_end_source_file (unsigned);
2438 static void dwarf2out_function_decl (tree);
2439 static void dwarf2out_begin_block (unsigned, unsigned);
2440 static void dwarf2out_end_block (unsigned, unsigned);
2441 static bool dwarf2out_ignore_block (const_tree);
2442 static void dwarf2out_global_decl (tree);
2443 static void dwarf2out_type_decl (tree, int);
2444 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2445 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2446 dw_die_ref);
2447 static void dwarf2out_abstract_function (tree);
2448 static void dwarf2out_var_location (rtx_insn *);
2449 static void dwarf2out_begin_function (tree);
2450 static void dwarf2out_end_function (unsigned int);
2451 static void dwarf2out_set_name (tree, tree);
2453 /* The debug hooks structure. */
2455 const struct gcc_debug_hooks dwarf2_debug_hooks =
2457 dwarf2out_init,
2458 dwarf2out_finish,
2459 dwarf2out_assembly_start,
2460 dwarf2out_define,
2461 dwarf2out_undef,
2462 dwarf2out_start_source_file,
2463 dwarf2out_end_source_file,
2464 dwarf2out_begin_block,
2465 dwarf2out_end_block,
2466 dwarf2out_ignore_block,
2467 dwarf2out_source_line,
2468 dwarf2out_begin_prologue,
2469 #if VMS_DEBUGGING_INFO
2470 dwarf2out_vms_end_prologue,
2471 dwarf2out_vms_begin_epilogue,
2472 #else
2473 debug_nothing_int_charstar,
2474 debug_nothing_int_charstar,
2475 #endif
2476 dwarf2out_end_epilogue,
2477 dwarf2out_begin_function,
2478 dwarf2out_end_function, /* end_function */
2479 dwarf2out_function_decl, /* function_decl */
2480 dwarf2out_global_decl,
2481 dwarf2out_type_decl, /* type_decl */
2482 dwarf2out_imported_module_or_decl,
2483 debug_nothing_tree, /* deferred_inline_function */
2484 /* The DWARF 2 backend tries to reduce debugging bloat by not
2485 emitting the abstract description of inline functions until
2486 something tries to reference them. */
2487 dwarf2out_abstract_function, /* outlining_inline_function */
2488 debug_nothing_rtx_code_label, /* label */
2489 debug_nothing_int, /* handle_pch */
2490 dwarf2out_var_location,
2491 dwarf2out_switch_text_section,
2492 dwarf2out_set_name,
2493 1, /* start_end_main_source_file */
2494 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2497 /* NOTE: In the comments in this file, many references are made to
2498 "Debugging Information Entries". This term is abbreviated as `DIE'
2499 throughout the remainder of this file. */
2501 /* An internal representation of the DWARF output is built, and then
2502 walked to generate the DWARF debugging info. The walk of the internal
2503 representation is done after the entire program has been compiled.
2504 The types below are used to describe the internal representation. */
2506 /* Whether to put type DIEs into their own section .debug_types instead
2507 of making them part of the .debug_info section. Only supported for
2508 Dwarf V4 or higher and the user didn't disable them through
2509 -fno-debug-types-section. It is more efficient to put them in a
2510 separate comdat sections since the linker will then be able to
2511 remove duplicates. But not all tools support .debug_types sections
2512 yet. */
2514 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2516 /* Various DIE's use offsets relative to the beginning of the
2517 .debug_info section to refer to each other. */
2519 typedef long int dw_offset;
2521 /* Define typedefs here to avoid circular dependencies. */
2523 typedef struct dw_attr_struct *dw_attr_ref;
2524 typedef struct dw_line_info_struct *dw_line_info_ref;
2525 typedef struct pubname_struct *pubname_ref;
2526 typedef struct dw_ranges_struct *dw_ranges_ref;
2527 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2528 typedef struct comdat_type_struct *comdat_type_node_ref;
2530 /* The entries in the line_info table more-or-less mirror the opcodes
2531 that are used in the real dwarf line table. Arrays of these entries
2532 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2533 supported. */
2535 enum dw_line_info_opcode {
2536 /* Emit DW_LNE_set_address; the operand is the label index. */
2537 LI_set_address,
2539 /* Emit a row to the matrix with the given line. This may be done
2540 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2541 special opcodes. */
2542 LI_set_line,
2544 /* Emit a DW_LNS_set_file. */
2545 LI_set_file,
2547 /* Emit a DW_LNS_set_column. */
2548 LI_set_column,
2550 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2551 LI_negate_stmt,
2553 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2554 LI_set_prologue_end,
2555 LI_set_epilogue_begin,
2557 /* Emit a DW_LNE_set_discriminator. */
2558 LI_set_discriminator
2561 typedef struct GTY(()) dw_line_info_struct {
2562 enum dw_line_info_opcode opcode;
2563 unsigned int val;
2564 } dw_line_info_entry;
2567 typedef struct GTY(()) dw_line_info_table_struct {
2568 /* The label that marks the end of this section. */
2569 const char *end_label;
2571 /* The values for the last row of the matrix, as collected in the table.
2572 These are used to minimize the changes to the next row. */
2573 unsigned int file_num;
2574 unsigned int line_num;
2575 unsigned int column_num;
2576 int discrim_num;
2577 bool is_stmt;
2578 bool in_use;
2580 vec<dw_line_info_entry, va_gc> *entries;
2581 } dw_line_info_table;
2583 typedef dw_line_info_table *dw_line_info_table_p;
2586 /* Each DIE attribute has a field specifying the attribute kind,
2587 a link to the next attribute in the chain, and an attribute value.
2588 Attributes are typically linked below the DIE they modify. */
2590 typedef struct GTY(()) dw_attr_struct {
2591 enum dwarf_attribute dw_attr;
2592 dw_val_node dw_attr_val;
2594 dw_attr_node;
2597 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2598 The children of each node form a circular list linked by
2599 die_sib. die_child points to the node *before* the "first" child node. */
2601 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2602 union die_symbol_or_type_node
2604 const char * GTY ((tag ("0"))) die_symbol;
2605 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2607 GTY ((desc ("%0.comdat_type_p"))) die_id;
2608 vec<dw_attr_node, va_gc> *die_attr;
2609 dw_die_ref die_parent;
2610 dw_die_ref die_child;
2611 dw_die_ref die_sib;
2612 dw_die_ref die_definition; /* ref from a specification to its definition */
2613 dw_offset die_offset;
2614 unsigned long die_abbrev;
2615 int die_mark;
2616 unsigned int decl_id;
2617 enum dwarf_tag die_tag;
2618 /* Die is used and must not be pruned as unused. */
2619 BOOL_BITFIELD die_perennial_p : 1;
2620 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2621 /* Lots of spare bits. */
2623 die_node;
2625 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2626 #define FOR_EACH_CHILD(die, c, expr) do { \
2627 c = die->die_child; \
2628 if (c) do { \
2629 c = c->die_sib; \
2630 expr; \
2631 } while (c != die->die_child); \
2632 } while (0)
2634 /* The pubname structure */
2636 typedef struct GTY(()) pubname_struct {
2637 dw_die_ref die;
2638 const char *name;
2640 pubname_entry;
2643 struct GTY(()) dw_ranges_struct {
2644 /* If this is positive, it's a block number, otherwise it's a
2645 bitwise-negated index into dw_ranges_by_label. */
2646 int num;
2649 /* A structure to hold a macinfo entry. */
2651 typedef struct GTY(()) macinfo_struct {
2652 unsigned char code;
2653 unsigned HOST_WIDE_INT lineno;
2654 const char *info;
2656 macinfo_entry;
2659 struct GTY(()) dw_ranges_by_label_struct {
2660 const char *begin;
2661 const char *end;
2664 /* The comdat type node structure. */
2665 typedef struct GTY(()) comdat_type_struct
2667 dw_die_ref root_die;
2668 dw_die_ref type_die;
2669 dw_die_ref skeleton_die;
2670 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2671 struct comdat_type_struct *next;
2673 comdat_type_node;
2675 /* The limbo die list structure. */
2676 typedef struct GTY(()) limbo_die_struct {
2677 dw_die_ref die;
2678 tree created_for;
2679 struct limbo_die_struct *next;
2681 limbo_die_node;
2683 typedef struct skeleton_chain_struct
2685 dw_die_ref old_die;
2686 dw_die_ref new_die;
2687 struct skeleton_chain_struct *parent;
2689 skeleton_chain_node;
2691 /* Define a macro which returns nonzero for a TYPE_DECL which was
2692 implicitly generated for a type.
2694 Note that, unlike the C front-end (which generates a NULL named
2695 TYPE_DECL node for each complete tagged type, each array type,
2696 and each function type node created) the C++ front-end generates
2697 a _named_ TYPE_DECL node for each tagged type node created.
2698 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2699 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2700 front-end, but for each type, tagged or not. */
2702 #define TYPE_DECL_IS_STUB(decl) \
2703 (DECL_NAME (decl) == NULL_TREE \
2704 || (DECL_ARTIFICIAL (decl) \
2705 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2706 /* This is necessary for stub decls that \
2707 appear in nested inline functions. */ \
2708 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2709 && (decl_ultimate_origin (decl) \
2710 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2712 /* Information concerning the compilation unit's programming
2713 language, and compiler version. */
2715 /* Fixed size portion of the DWARF compilation unit header. */
2716 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2717 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2719 /* Fixed size portion of the DWARF comdat type unit header. */
2720 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2721 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2722 + DWARF_OFFSET_SIZE)
2724 /* Fixed size portion of public names info. */
2725 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2727 /* Fixed size portion of the address range info. */
2728 #define DWARF_ARANGES_HEADER_SIZE \
2729 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2730 DWARF2_ADDR_SIZE * 2) \
2731 - DWARF_INITIAL_LENGTH_SIZE)
2733 /* Size of padding portion in the address range info. It must be
2734 aligned to twice the pointer size. */
2735 #define DWARF_ARANGES_PAD_SIZE \
2736 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2737 DWARF2_ADDR_SIZE * 2) \
2738 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2740 /* Use assembler line directives if available. */
2741 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2742 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2743 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2744 #else
2745 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2746 #endif
2747 #endif
2749 /* Minimum line offset in a special line info. opcode.
2750 This value was chosen to give a reasonable range of values. */
2751 #define DWARF_LINE_BASE -10
2753 /* First special line opcode - leave room for the standard opcodes. */
2754 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2756 /* Range of line offsets in a special line info. opcode. */
2757 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2759 /* Flag that indicates the initial value of the is_stmt_start flag.
2760 In the present implementation, we do not mark any lines as
2761 the beginning of a source statement, because that information
2762 is not made available by the GCC front-end. */
2763 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2765 /* Maximum number of operations per instruction bundle. */
2766 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2767 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2768 #endif
2770 /* This location is used by calc_die_sizes() to keep track
2771 the offset of each DIE within the .debug_info section. */
2772 static unsigned long next_die_offset;
2774 /* Record the root of the DIE's built for the current compilation unit. */
2775 static GTY(()) dw_die_ref single_comp_unit_die;
2777 /* A list of type DIEs that have been separated into comdat sections. */
2778 static GTY(()) comdat_type_node *comdat_type_list;
2780 /* A list of DIEs with a NULL parent waiting to be relocated. */
2781 static GTY(()) limbo_die_node *limbo_die_list;
2783 /* A list of DIEs for which we may have to generate
2784 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2785 static GTY(()) limbo_die_node *deferred_asm_name;
2787 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2789 typedef const char *compare_type;
2791 static hashval_t hash (dwarf_file_data *);
2792 static bool equal (dwarf_file_data *, const char *);
2795 /* Filenames referenced by this compilation unit. */
2796 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2798 struct decl_die_hasher : ggc_hasher<die_node *>
2800 typedef tree compare_type;
2802 static hashval_t hash (die_node *);
2803 static bool equal (die_node *, tree);
2805 /* A hash table of references to DIE's that describe declarations.
2806 The key is a DECL_UID() which is a unique number identifying each decl. */
2807 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2809 struct block_die_hasher : ggc_hasher<die_struct *>
2811 static hashval_t hash (die_struct *);
2812 static bool equal (die_struct *, die_struct *);
2815 /* A hash table of references to DIE's that describe COMMON blocks.
2816 The key is DECL_UID() ^ die_parent. */
2817 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2819 typedef struct GTY(()) die_arg_entry_struct {
2820 dw_die_ref die;
2821 tree arg;
2822 } die_arg_entry;
2825 /* Node of the variable location list. */
2826 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2827 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2828 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2829 in mode of the EXPR_LIST node and first EXPR_LIST operand
2830 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2831 location or NULL for padding. For larger bitsizes,
2832 mode is 0 and first operand is a CONCAT with bitsize
2833 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2834 NULL as second operand. */
2835 rtx GTY (()) loc;
2836 const char * GTY (()) label;
2837 struct var_loc_node * GTY (()) next;
2840 /* Variable location list. */
2841 struct GTY ((for_user)) var_loc_list_def {
2842 struct var_loc_node * GTY (()) first;
2844 /* Pointer to the last but one or last element of the
2845 chained list. If the list is empty, both first and
2846 last are NULL, if the list contains just one node
2847 or the last node certainly is not redundant, it points
2848 to the last node, otherwise points to the last but one.
2849 Do not mark it for GC because it is marked through the chain. */
2850 struct var_loc_node * GTY ((skip ("%h"))) last;
2852 /* Pointer to the last element before section switch,
2853 if NULL, either sections weren't switched or first
2854 is after section switch. */
2855 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2857 /* DECL_UID of the variable decl. */
2858 unsigned int decl_id;
2860 typedef struct var_loc_list_def var_loc_list;
2862 /* Call argument location list. */
2863 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2864 rtx GTY (()) call_arg_loc_note;
2865 const char * GTY (()) label;
2866 tree GTY (()) block;
2867 bool tail_call_p;
2868 rtx GTY (()) symbol_ref;
2869 struct call_arg_loc_node * GTY (()) next;
2873 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2875 typedef const_tree compare_type;
2877 static hashval_t hash (var_loc_list *);
2878 static bool equal (var_loc_list *, const_tree);
2881 /* Table of decl location linked lists. */
2882 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2884 /* Head and tail of call_arg_loc chain. */
2885 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2886 static struct call_arg_loc_node *call_arg_loc_last;
2888 /* Number of call sites in the current function. */
2889 static int call_site_count = -1;
2890 /* Number of tail call sites in the current function. */
2891 static int tail_call_site_count = -1;
2893 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2894 DIEs. */
2895 static vec<dw_die_ref> block_map;
2897 /* A cached location list. */
2898 struct GTY ((for_user)) cached_dw_loc_list_def {
2899 /* The DECL_UID of the decl that this entry describes. */
2900 unsigned int decl_id;
2902 /* The cached location list. */
2903 dw_loc_list_ref loc_list;
2905 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2907 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2910 typedef const_tree compare_type;
2912 static hashval_t hash (cached_dw_loc_list *);
2913 static bool equal (cached_dw_loc_list *, const_tree);
2916 /* Table of cached location lists. */
2917 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2919 /* A pointer to the base of a list of references to DIE's that
2920 are uniquely identified by their tag, presence/absence of
2921 children DIE's, and list of attribute/value pairs. */
2922 static GTY((length ("abbrev_die_table_allocated")))
2923 dw_die_ref *abbrev_die_table;
2925 /* Number of elements currently allocated for abbrev_die_table. */
2926 static GTY(()) unsigned abbrev_die_table_allocated;
2928 /* Number of elements in type_die_table currently in use. */
2929 static GTY(()) unsigned abbrev_die_table_in_use;
2931 /* Size (in elements) of increments by which we may expand the
2932 abbrev_die_table. */
2933 #define ABBREV_DIE_TABLE_INCREMENT 256
2935 /* A global counter for generating labels for line number data. */
2936 static unsigned int line_info_label_num;
2938 /* The current table to which we should emit line number information
2939 for the current function. This will be set up at the beginning of
2940 assembly for the function. */
2941 static dw_line_info_table *cur_line_info_table;
2943 /* The two default tables of line number info. */
2944 static GTY(()) dw_line_info_table *text_section_line_info;
2945 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2947 /* The set of all non-default tables of line number info. */
2948 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2950 /* A flag to tell pubnames/types export if there is an info section to
2951 refer to. */
2952 static bool info_section_emitted;
2954 /* A pointer to the base of a table that contains a list of publicly
2955 accessible names. */
2956 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2958 /* A pointer to the base of a table that contains a list of publicly
2959 accessible types. */
2960 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2962 /* A pointer to the base of a table that contains a list of macro
2963 defines/undefines (and file start/end markers). */
2964 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2966 /* True if .debug_macinfo or .debug_macros section is going to be
2967 emitted. */
2968 #define have_macinfo \
2969 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2970 && !macinfo_table->is_empty ())
2972 /* Array of dies for which we should generate .debug_ranges info. */
2973 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2975 /* Number of elements currently allocated for ranges_table. */
2976 static GTY(()) unsigned ranges_table_allocated;
2978 /* Number of elements in ranges_table currently in use. */
2979 static GTY(()) unsigned ranges_table_in_use;
2981 /* Array of pairs of labels referenced in ranges_table. */
2982 static GTY ((length ("ranges_by_label_allocated")))
2983 dw_ranges_by_label_ref ranges_by_label;
2985 /* Number of elements currently allocated for ranges_by_label. */
2986 static GTY(()) unsigned ranges_by_label_allocated;
2988 /* Number of elements in ranges_by_label currently in use. */
2989 static GTY(()) unsigned ranges_by_label_in_use;
2991 /* Size (in elements) of increments by which we may expand the
2992 ranges_table. */
2993 #define RANGES_TABLE_INCREMENT 64
2995 /* Whether we have location lists that need outputting */
2996 static GTY(()) bool have_location_lists;
2998 /* Unique label counter. */
2999 static GTY(()) unsigned int loclabel_num;
3001 /* Unique label counter for point-of-call tables. */
3002 static GTY(()) unsigned int poc_label_num;
3004 /* The last file entry emitted by maybe_emit_file(). */
3005 static GTY(()) struct dwarf_file_data * last_emitted_file;
3007 /* Number of internal labels generated by gen_internal_sym(). */
3008 static GTY(()) int label_num;
3010 /* Cached result of previous call to lookup_filename. */
3011 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3013 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3015 /* Instances of generic types for which we need to generate debug
3016 info that describe their generic parameters and arguments. That
3017 generation needs to happen once all types are properly laid out so
3018 we do it at the end of compilation. */
3019 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3021 /* Offset from the "steady-state frame pointer" to the frame base,
3022 within the current function. */
3023 static HOST_WIDE_INT frame_pointer_fb_offset;
3024 static bool frame_pointer_fb_offset_valid;
3026 static vec<dw_die_ref> base_types;
3028 /* Forward declarations for functions defined in this file. */
3030 static int is_pseudo_reg (const_rtx);
3031 static tree type_main_variant (tree);
3032 static int is_tagged_type (const_tree);
3033 static const char *dwarf_tag_name (unsigned);
3034 static const char *dwarf_attr_name (unsigned);
3035 static const char *dwarf_form_name (unsigned);
3036 static tree decl_ultimate_origin (const_tree);
3037 static tree decl_class_context (tree);
3038 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3039 static inline enum dw_val_class AT_class (dw_attr_ref);
3040 static inline unsigned int AT_index (dw_attr_ref);
3041 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3042 static inline unsigned AT_flag (dw_attr_ref);
3043 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3044 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3045 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3046 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3047 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3048 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3049 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3050 unsigned int, unsigned char *);
3051 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3052 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3053 static inline const char *AT_string (dw_attr_ref);
3054 static enum dwarf_form AT_string_form (dw_attr_ref);
3055 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3056 static void add_AT_specification (dw_die_ref, dw_die_ref);
3057 static inline dw_die_ref AT_ref (dw_attr_ref);
3058 static inline int AT_ref_external (dw_attr_ref);
3059 static inline void set_AT_ref_external (dw_attr_ref, int);
3060 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3061 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3062 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3063 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3064 dw_loc_list_ref);
3065 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3066 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3067 static void remove_addr_table_entry (addr_table_entry *);
3068 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3069 static inline rtx AT_addr (dw_attr_ref);
3070 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3071 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3072 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3073 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3074 unsigned HOST_WIDE_INT);
3075 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3076 unsigned long, bool);
3077 static inline const char *AT_lbl (dw_attr_ref);
3078 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3079 static const char *get_AT_low_pc (dw_die_ref);
3080 static const char *get_AT_hi_pc (dw_die_ref);
3081 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3082 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3083 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3084 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3085 static bool is_cxx (void);
3086 static bool is_fortran (void);
3087 static bool is_ada (void);
3088 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3089 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3090 static void add_child_die (dw_die_ref, dw_die_ref);
3091 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3092 static dw_die_ref lookup_type_die (tree);
3093 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3094 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3095 static void equate_type_number_to_die (tree, dw_die_ref);
3096 static dw_die_ref lookup_decl_die (tree);
3097 static var_loc_list *lookup_decl_loc (const_tree);
3098 static void equate_decl_number_to_die (tree, dw_die_ref);
3099 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3100 static void print_spaces (FILE *);
3101 static void print_die (dw_die_ref, FILE *);
3102 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3103 static dw_die_ref pop_compile_unit (dw_die_ref);
3104 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3105 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3106 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3107 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3108 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3109 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3110 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3111 struct md5_ctx *, int *);
3112 struct checksum_attributes;
3113 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3114 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3115 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3116 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3117 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3118 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3119 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3120 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3121 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3122 static void compute_section_prefix (dw_die_ref);
3123 static int is_type_die (dw_die_ref);
3124 static int is_comdat_die (dw_die_ref);
3125 static int is_symbol_die (dw_die_ref);
3126 static inline bool is_template_instantiation (dw_die_ref);
3127 static void assign_symbol_names (dw_die_ref);
3128 static void break_out_includes (dw_die_ref);
3129 static int is_declaration_die (dw_die_ref);
3130 static int should_move_die_to_comdat (dw_die_ref);
3131 static dw_die_ref clone_as_declaration (dw_die_ref);
3132 static dw_die_ref clone_die (dw_die_ref);
3133 static dw_die_ref clone_tree (dw_die_ref);
3134 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3135 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3136 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3137 static dw_die_ref generate_skeleton (dw_die_ref);
3138 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3139 dw_die_ref,
3140 dw_die_ref);
3141 static void break_out_comdat_types (dw_die_ref);
3142 static void copy_decls_for_unworthy_types (dw_die_ref);
3144 static void add_sibling_attributes (dw_die_ref);
3145 static void output_location_lists (dw_die_ref);
3146 static int constant_size (unsigned HOST_WIDE_INT);
3147 static unsigned long size_of_die (dw_die_ref);
3148 static void calc_die_sizes (dw_die_ref);
3149 static void calc_base_type_die_sizes (void);
3150 static void mark_dies (dw_die_ref);
3151 static void unmark_dies (dw_die_ref);
3152 static void unmark_all_dies (dw_die_ref);
3153 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3154 static unsigned long size_of_aranges (void);
3155 static enum dwarf_form value_format (dw_attr_ref);
3156 static void output_value_format (dw_attr_ref);
3157 static void output_abbrev_section (void);
3158 static void output_die_abbrevs (unsigned long, dw_die_ref);
3159 static void output_die_symbol (dw_die_ref);
3160 static void output_die (dw_die_ref);
3161 static void output_compilation_unit_header (void);
3162 static void output_comp_unit (dw_die_ref, int);
3163 static void output_comdat_type_unit (comdat_type_node *);
3164 static const char *dwarf2_name (tree, int);
3165 static void add_pubname (tree, dw_die_ref);
3166 static void add_enumerator_pubname (const char *, dw_die_ref);
3167 static void add_pubname_string (const char *, dw_die_ref);
3168 static void add_pubtype (tree, dw_die_ref);
3169 static void output_pubnames (vec<pubname_entry, va_gc> *);
3170 static void output_aranges (unsigned long);
3171 static unsigned int add_ranges_num (int);
3172 static unsigned int add_ranges (const_tree);
3173 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3174 bool *, bool);
3175 static void output_ranges (void);
3176 static dw_line_info_table *new_line_info_table (void);
3177 static void output_line_info (bool);
3178 static void output_file_names (void);
3179 static dw_die_ref base_type_die (tree);
3180 static int is_base_type (tree);
3181 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3182 static int decl_quals (const_tree);
3183 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3184 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3185 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3186 static int type_is_enum (const_tree);
3187 static unsigned int dbx_reg_number (const_rtx);
3188 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3189 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3190 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3191 enum var_init_status);
3192 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3193 enum var_init_status);
3194 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3195 enum var_init_status);
3196 static int is_based_loc (const_rtx);
3197 static bool resolve_one_addr (rtx *);
3198 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3199 enum var_init_status);
3200 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3201 enum var_init_status);
3202 static dw_loc_list_ref loc_list_from_tree (tree, int);
3203 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3204 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3205 static tree field_type (const_tree);
3206 static unsigned int simple_type_align_in_bits (const_tree);
3207 static unsigned int simple_decl_align_in_bits (const_tree);
3208 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3209 static HOST_WIDE_INT field_byte_offset (const_tree);
3210 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3211 dw_loc_list_ref);
3212 static void add_data_member_location_attribute (dw_die_ref, tree);
3213 static bool add_const_value_attribute (dw_die_ref, rtx);
3214 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3215 static void insert_wide_int (const wide_int &, unsigned char *, int);
3216 static void insert_float (const_rtx, unsigned char *);
3217 static rtx rtl_for_decl_location (tree);
3218 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3219 enum dwarf_attribute);
3220 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3221 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3222 static void add_name_attribute (dw_die_ref, const char *);
3223 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3224 static void add_comp_dir_attribute (dw_die_ref);
3225 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3226 static void add_subscript_info (dw_die_ref, tree, bool);
3227 static void add_byte_size_attribute (dw_die_ref, tree);
3228 static void add_bit_offset_attribute (dw_die_ref, tree);
3229 static void add_bit_size_attribute (dw_die_ref, tree);
3230 static void add_prototyped_attribute (dw_die_ref, tree);
3231 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3232 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3233 static void add_src_coords_attributes (dw_die_ref, tree);
3234 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3235 static void push_decl_scope (tree);
3236 static void pop_decl_scope (void);
3237 static dw_die_ref scope_die_for (tree, dw_die_ref);
3238 static inline int local_scope_p (dw_die_ref);
3239 static inline int class_scope_p (dw_die_ref);
3240 static inline int class_or_namespace_scope_p (dw_die_ref);
3241 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3242 static void add_calling_convention_attribute (dw_die_ref, tree);
3243 static const char *type_tag (const_tree);
3244 static tree member_declared_type (const_tree);
3245 #if 0
3246 static const char *decl_start_label (tree);
3247 #endif
3248 static void gen_array_type_die (tree, dw_die_ref);
3249 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3250 #if 0
3251 static void gen_entry_point_die (tree, dw_die_ref);
3252 #endif
3253 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3254 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3255 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3256 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3257 static void gen_formal_types_die (tree, dw_die_ref);
3258 static void gen_subprogram_die (tree, dw_die_ref);
3259 static void gen_variable_die (tree, tree, dw_die_ref);
3260 static void gen_const_die (tree, dw_die_ref);
3261 static void gen_label_die (tree, dw_die_ref);
3262 static void gen_lexical_block_die (tree, dw_die_ref, int);
3263 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3264 static void gen_field_die (tree, dw_die_ref);
3265 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3266 static dw_die_ref gen_compile_unit_die (const char *);
3267 static void gen_inheritance_die (tree, tree, dw_die_ref);
3268 static void gen_member_die (tree, dw_die_ref);
3269 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3270 enum debug_info_usage);
3271 static void gen_subroutine_type_die (tree, dw_die_ref);
3272 static void gen_typedef_die (tree, dw_die_ref);
3273 static void gen_type_die (tree, dw_die_ref);
3274 static void gen_block_die (tree, dw_die_ref, int);
3275 static void decls_for_scope (tree, dw_die_ref, int);
3276 static inline int is_redundant_typedef (const_tree);
3277 static bool is_naming_typedef_decl (const_tree);
3278 static inline dw_die_ref get_context_die (tree);
3279 static void gen_namespace_die (tree, dw_die_ref);
3280 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3281 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3282 static dw_die_ref force_decl_die (tree);
3283 static dw_die_ref force_type_die (tree);
3284 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3285 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3286 static struct dwarf_file_data * lookup_filename (const char *);
3287 static void retry_incomplete_types (void);
3288 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3289 static void gen_generic_params_dies (tree);
3290 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3291 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3292 static void splice_child_die (dw_die_ref, dw_die_ref);
3293 static int file_info_cmp (const void *, const void *);
3294 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3295 const char *, const char *);
3296 static void output_loc_list (dw_loc_list_ref);
3297 static char *gen_internal_sym (const char *);
3298 static bool want_pubnames (void);
3300 static void prune_unmark_dies (dw_die_ref);
3301 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3302 static void prune_unused_types_mark (dw_die_ref, int);
3303 static void prune_unused_types_walk (dw_die_ref);
3304 static void prune_unused_types_walk_attribs (dw_die_ref);
3305 static void prune_unused_types_prune (dw_die_ref);
3306 static void prune_unused_types (void);
3307 static int maybe_emit_file (struct dwarf_file_data *fd);
3308 static inline const char *AT_vms_delta1 (dw_attr_ref);
3309 static inline const char *AT_vms_delta2 (dw_attr_ref);
3310 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3311 const char *, const char *);
3312 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3313 static void gen_remaining_tmpl_value_param_die_attribute (void);
3314 static bool generic_type_p (tree);
3315 static void schedule_generic_params_dies_gen (tree t);
3316 static void gen_scheduled_generic_parms_dies (void);
3318 static const char *comp_dir_string (void);
3320 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3322 /* enum for tracking thread-local variables whose address is really an offset
3323 relative to the TLS pointer, which will need link-time relocation, but will
3324 not need relocation by the DWARF consumer. */
3326 enum dtprel_bool
3328 dtprel_false = 0,
3329 dtprel_true = 1
3332 /* Return the operator to use for an address of a variable. For dtprel_true, we
3333 use DW_OP_const*. For regular variables, which need both link-time
3334 relocation and consumer-level relocation (e.g., to account for shared objects
3335 loaded at a random address), we use DW_OP_addr*. */
3337 static inline enum dwarf_location_atom
3338 dw_addr_op (enum dtprel_bool dtprel)
3340 if (dtprel == dtprel_true)
3341 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3342 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3343 else
3344 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3347 /* Return a pointer to a newly allocated address location description. If
3348 dwarf_split_debug_info is true, then record the address with the appropriate
3349 relocation. */
3350 static inline dw_loc_descr_ref
3351 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3353 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3355 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3356 ref->dw_loc_oprnd1.v.val_addr = addr;
3357 ref->dtprel = dtprel;
3358 if (dwarf_split_debug_info)
3359 ref->dw_loc_oprnd1.val_entry
3360 = add_addr_table_entry (addr,
3361 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3362 else
3363 ref->dw_loc_oprnd1.val_entry = NULL;
3365 return ref;
3368 /* Section names used to hold DWARF debugging information. */
3370 #ifndef DEBUG_INFO_SECTION
3371 #define DEBUG_INFO_SECTION ".debug_info"
3372 #endif
3373 #ifndef DEBUG_DWO_INFO_SECTION
3374 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3375 #endif
3376 #ifndef DEBUG_ABBREV_SECTION
3377 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3378 #endif
3379 #ifndef DEBUG_DWO_ABBREV_SECTION
3380 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3381 #endif
3382 #ifndef DEBUG_ARANGES_SECTION
3383 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3384 #endif
3385 #ifndef DEBUG_ADDR_SECTION
3386 #define DEBUG_ADDR_SECTION ".debug_addr"
3387 #endif
3388 #ifndef DEBUG_NORM_MACINFO_SECTION
3389 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3390 #endif
3391 #ifndef DEBUG_DWO_MACINFO_SECTION
3392 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3393 #endif
3394 #ifndef DEBUG_MACINFO_SECTION
3395 #define DEBUG_MACINFO_SECTION \
3396 (!dwarf_split_debug_info \
3397 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3398 #endif
3399 #ifndef DEBUG_NORM_MACRO_SECTION
3400 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3401 #endif
3402 #ifndef DEBUG_DWO_MACRO_SECTION
3403 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3404 #endif
3405 #ifndef DEBUG_MACRO_SECTION
3406 #define DEBUG_MACRO_SECTION \
3407 (!dwarf_split_debug_info \
3408 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3409 #endif
3410 #ifndef DEBUG_LINE_SECTION
3411 #define DEBUG_LINE_SECTION ".debug_line"
3412 #endif
3413 #ifndef DEBUG_DWO_LINE_SECTION
3414 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3415 #endif
3416 #ifndef DEBUG_LOC_SECTION
3417 #define DEBUG_LOC_SECTION ".debug_loc"
3418 #endif
3419 #ifndef DEBUG_DWO_LOC_SECTION
3420 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3421 #endif
3422 #ifndef DEBUG_PUBNAMES_SECTION
3423 #define DEBUG_PUBNAMES_SECTION \
3424 ((debug_generate_pub_sections == 2) \
3425 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3426 #endif
3427 #ifndef DEBUG_PUBTYPES_SECTION
3428 #define DEBUG_PUBTYPES_SECTION \
3429 ((debug_generate_pub_sections == 2) \
3430 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3431 #endif
3432 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3433 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3434 #ifndef DEBUG_STR_OFFSETS_SECTION
3435 #define DEBUG_STR_OFFSETS_SECTION \
3436 (!dwarf_split_debug_info \
3437 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3438 #endif
3439 #ifndef DEBUG_STR_DWO_SECTION
3440 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3441 #endif
3442 #ifndef DEBUG_STR_SECTION
3443 #define DEBUG_STR_SECTION ".debug_str"
3444 #endif
3445 #ifndef DEBUG_RANGES_SECTION
3446 #define DEBUG_RANGES_SECTION ".debug_ranges"
3447 #endif
3449 /* Standard ELF section names for compiled code and data. */
3450 #ifndef TEXT_SECTION_NAME
3451 #define TEXT_SECTION_NAME ".text"
3452 #endif
3454 /* Section flags for .debug_macinfo/.debug_macro section. */
3455 #define DEBUG_MACRO_SECTION_FLAGS \
3456 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3458 /* Section flags for .debug_str section. */
3459 #define DEBUG_STR_SECTION_FLAGS \
3460 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3461 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3462 : SECTION_DEBUG)
3464 /* Section flags for .debug_str.dwo section. */
3465 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3467 /* Labels we insert at beginning sections we can reference instead of
3468 the section names themselves. */
3470 #ifndef TEXT_SECTION_LABEL
3471 #define TEXT_SECTION_LABEL "Ltext"
3472 #endif
3473 #ifndef COLD_TEXT_SECTION_LABEL
3474 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3475 #endif
3476 #ifndef DEBUG_LINE_SECTION_LABEL
3477 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3478 #endif
3479 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3480 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3481 #endif
3482 #ifndef DEBUG_INFO_SECTION_LABEL
3483 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3484 #endif
3485 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3486 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3487 #endif
3488 #ifndef DEBUG_ABBREV_SECTION_LABEL
3489 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3490 #endif
3491 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3492 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3493 #endif
3494 #ifndef DEBUG_ADDR_SECTION_LABEL
3495 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3496 #endif
3497 #ifndef DEBUG_LOC_SECTION_LABEL
3498 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3499 #endif
3500 #ifndef DEBUG_RANGES_SECTION_LABEL
3501 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3502 #endif
3503 #ifndef DEBUG_MACINFO_SECTION_LABEL
3504 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3505 #endif
3506 #ifndef DEBUG_MACRO_SECTION_LABEL
3507 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3508 #endif
3509 #define SKELETON_COMP_DIE_ABBREV 1
3510 #define SKELETON_TYPE_DIE_ABBREV 2
3512 /* Definitions of defaults for formats and names of various special
3513 (artificial) labels which may be generated within this file (when the -g
3514 options is used and DWARF2_DEBUGGING_INFO is in effect.
3515 If necessary, these may be overridden from within the tm.h file, but
3516 typically, overriding these defaults is unnecessary. */
3518 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3519 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3520 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3521 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3522 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3523 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3524 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3525 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3526 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3527 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3528 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3529 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3530 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3531 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3533 #ifndef TEXT_END_LABEL
3534 #define TEXT_END_LABEL "Letext"
3535 #endif
3536 #ifndef COLD_END_LABEL
3537 #define COLD_END_LABEL "Letext_cold"
3538 #endif
3539 #ifndef BLOCK_BEGIN_LABEL
3540 #define BLOCK_BEGIN_LABEL "LBB"
3541 #endif
3542 #ifndef BLOCK_END_LABEL
3543 #define BLOCK_END_LABEL "LBE"
3544 #endif
3545 #ifndef LINE_CODE_LABEL
3546 #define LINE_CODE_LABEL "LM"
3547 #endif
3550 /* Return the root of the DIE's built for the current compilation unit. */
3551 static dw_die_ref
3552 comp_unit_die (void)
3554 if (!single_comp_unit_die)
3555 single_comp_unit_die = gen_compile_unit_die (NULL);
3556 return single_comp_unit_die;
3559 /* We allow a language front-end to designate a function that is to be
3560 called to "demangle" any name before it is put into a DIE. */
3562 static const char *(*demangle_name_func) (const char *);
3564 void
3565 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3567 demangle_name_func = func;
3570 /* Test if rtl node points to a pseudo register. */
3572 static inline int
3573 is_pseudo_reg (const_rtx rtl)
3575 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3576 || (GET_CODE (rtl) == SUBREG
3577 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3580 /* Return a reference to a type, with its const and volatile qualifiers
3581 removed. */
3583 static inline tree
3584 type_main_variant (tree type)
3586 type = TYPE_MAIN_VARIANT (type);
3588 /* ??? There really should be only one main variant among any group of
3589 variants of a given type (and all of the MAIN_VARIANT values for all
3590 members of the group should point to that one type) but sometimes the C
3591 front-end messes this up for array types, so we work around that bug
3592 here. */
3593 if (TREE_CODE (type) == ARRAY_TYPE)
3594 while (type != TYPE_MAIN_VARIANT (type))
3595 type = TYPE_MAIN_VARIANT (type);
3597 return type;
3600 /* Return nonzero if the given type node represents a tagged type. */
3602 static inline int
3603 is_tagged_type (const_tree type)
3605 enum tree_code code = TREE_CODE (type);
3607 return (code == RECORD_TYPE || code == UNION_TYPE
3608 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3611 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3613 static void
3614 get_ref_die_offset_label (char *label, dw_die_ref ref)
3616 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3619 /* Return die_offset of a DIE reference to a base type. */
3621 static unsigned long int
3622 get_base_type_offset (dw_die_ref ref)
3624 if (ref->die_offset)
3625 return ref->die_offset;
3626 if (comp_unit_die ()->die_abbrev)
3628 calc_base_type_die_sizes ();
3629 gcc_assert (ref->die_offset);
3631 return ref->die_offset;
3634 /* Return die_offset of a DIE reference other than base type. */
3636 static unsigned long int
3637 get_ref_die_offset (dw_die_ref ref)
3639 gcc_assert (ref->die_offset);
3640 return ref->die_offset;
3643 /* Convert a DIE tag into its string name. */
3645 static const char *
3646 dwarf_tag_name (unsigned int tag)
3648 const char *name = get_DW_TAG_name (tag);
3650 if (name != NULL)
3651 return name;
3653 return "DW_TAG_<unknown>";
3656 /* Convert a DWARF attribute code into its string name. */
3658 static const char *
3659 dwarf_attr_name (unsigned int attr)
3661 const char *name;
3663 switch (attr)
3665 #if VMS_DEBUGGING_INFO
3666 case DW_AT_HP_prologue:
3667 return "DW_AT_HP_prologue";
3668 #else
3669 case DW_AT_MIPS_loop_unroll_factor:
3670 return "DW_AT_MIPS_loop_unroll_factor";
3671 #endif
3673 #if VMS_DEBUGGING_INFO
3674 case DW_AT_HP_epilogue:
3675 return "DW_AT_HP_epilogue";
3676 #else
3677 case DW_AT_MIPS_stride:
3678 return "DW_AT_MIPS_stride";
3679 #endif
3682 name = get_DW_AT_name (attr);
3684 if (name != NULL)
3685 return name;
3687 return "DW_AT_<unknown>";
3690 /* Convert a DWARF value form code into its string name. */
3692 static const char *
3693 dwarf_form_name (unsigned int form)
3695 const char *name = get_DW_FORM_name (form);
3697 if (name != NULL)
3698 return name;
3700 return "DW_FORM_<unknown>";
3703 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3704 instance of an inlined instance of a decl which is local to an inline
3705 function, so we have to trace all of the way back through the origin chain
3706 to find out what sort of node actually served as the original seed for the
3707 given block. */
3709 static tree
3710 decl_ultimate_origin (const_tree decl)
3712 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3713 return NULL_TREE;
3715 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3716 we're trying to output the abstract instance of this function. */
3717 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3718 return NULL_TREE;
3720 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3721 most distant ancestor, this should never happen. */
3722 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3724 return DECL_ABSTRACT_ORIGIN (decl);
3727 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3728 of a virtual function may refer to a base class, so we check the 'this'
3729 parameter. */
3731 static tree
3732 decl_class_context (tree decl)
3734 tree context = NULL_TREE;
3736 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3737 context = DECL_CONTEXT (decl);
3738 else
3739 context = TYPE_MAIN_VARIANT
3740 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3742 if (context && !TYPE_P (context))
3743 context = NULL_TREE;
3745 return context;
3748 /* Add an attribute/value pair to a DIE. */
3750 static inline void
3751 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3753 /* Maybe this should be an assert? */
3754 if (die == NULL)
3755 return;
3757 vec_safe_reserve (die->die_attr, 1);
3758 vec_safe_push (die->die_attr, *attr);
3761 static inline enum dw_val_class
3762 AT_class (dw_attr_ref a)
3764 return a->dw_attr_val.val_class;
3767 /* Return the index for any attribute that will be referenced with a
3768 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3769 are stored in dw_attr_val.v.val_str for reference counting
3770 pruning. */
3772 static inline unsigned int
3773 AT_index (dw_attr_ref a)
3775 if (AT_class (a) == dw_val_class_str)
3776 return a->dw_attr_val.v.val_str->index;
3777 else if (a->dw_attr_val.val_entry != NULL)
3778 return a->dw_attr_val.val_entry->index;
3779 return NOT_INDEXED;
3782 /* Add a flag value attribute to a DIE. */
3784 static inline void
3785 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3787 dw_attr_node attr;
3789 attr.dw_attr = attr_kind;
3790 attr.dw_attr_val.val_class = dw_val_class_flag;
3791 attr.dw_attr_val.val_entry = NULL;
3792 attr.dw_attr_val.v.val_flag = flag;
3793 add_dwarf_attr (die, &attr);
3796 static inline unsigned
3797 AT_flag (dw_attr_ref a)
3799 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3800 return a->dw_attr_val.v.val_flag;
3803 /* Add a signed integer attribute value to a DIE. */
3805 static inline void
3806 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3808 dw_attr_node attr;
3810 attr.dw_attr = attr_kind;
3811 attr.dw_attr_val.val_class = dw_val_class_const;
3812 attr.dw_attr_val.val_entry = NULL;
3813 attr.dw_attr_val.v.val_int = int_val;
3814 add_dwarf_attr (die, &attr);
3817 static inline HOST_WIDE_INT
3818 AT_int (dw_attr_ref a)
3820 gcc_assert (a && AT_class (a) == dw_val_class_const);
3821 return a->dw_attr_val.v.val_int;
3824 /* Add an unsigned integer attribute value to a DIE. */
3826 static inline void
3827 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3828 unsigned HOST_WIDE_INT unsigned_val)
3830 dw_attr_node attr;
3832 attr.dw_attr = attr_kind;
3833 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3834 attr.dw_attr_val.val_entry = NULL;
3835 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3836 add_dwarf_attr (die, &attr);
3839 static inline unsigned HOST_WIDE_INT
3840 AT_unsigned (dw_attr_ref a)
3842 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3843 return a->dw_attr_val.v.val_unsigned;
3846 /* Add an unsigned wide integer attribute value to a DIE. */
3848 static inline void
3849 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3850 const wide_int& w)
3852 dw_attr_node attr;
3854 attr.dw_attr = attr_kind;
3855 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3856 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3857 *attr.dw_attr_val.v.val_wide = w;
3858 add_dwarf_attr (die, &attr);
3861 /* Add an unsigned double integer attribute value to a DIE. */
3863 static inline void
3864 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3865 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3867 dw_attr_node attr;
3869 attr.dw_attr = attr_kind;
3870 attr.dw_attr_val.val_class = dw_val_class_const_double;
3871 attr.dw_attr_val.val_entry = NULL;
3872 attr.dw_attr_val.v.val_double.high = high;
3873 attr.dw_attr_val.v.val_double.low = low;
3874 add_dwarf_attr (die, &attr);
3877 /* Add a floating point attribute value to a DIE and return it. */
3879 static inline void
3880 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3881 unsigned int length, unsigned int elt_size, unsigned char *array)
3883 dw_attr_node attr;
3885 attr.dw_attr = attr_kind;
3886 attr.dw_attr_val.val_class = dw_val_class_vec;
3887 attr.dw_attr_val.val_entry = NULL;
3888 attr.dw_attr_val.v.val_vec.length = length;
3889 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3890 attr.dw_attr_val.v.val_vec.array = array;
3891 add_dwarf_attr (die, &attr);
3894 /* Add an 8-byte data attribute value to a DIE. */
3896 static inline void
3897 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3898 unsigned char data8[8])
3900 dw_attr_node attr;
3902 attr.dw_attr = attr_kind;
3903 attr.dw_attr_val.val_class = dw_val_class_data8;
3904 attr.dw_attr_val.val_entry = NULL;
3905 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3906 add_dwarf_attr (die, &attr);
3909 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3910 dwarf_split_debug_info, address attributes in dies destined for the
3911 final executable have force_direct set to avoid using indexed
3912 references. */
3914 static inline void
3915 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3916 bool force_direct)
3918 dw_attr_node attr;
3919 char * lbl_id;
3921 lbl_id = xstrdup (lbl_low);
3922 attr.dw_attr = DW_AT_low_pc;
3923 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3924 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3925 if (dwarf_split_debug_info && !force_direct)
3926 attr.dw_attr_val.val_entry
3927 = add_addr_table_entry (lbl_id, ate_kind_label);
3928 else
3929 attr.dw_attr_val.val_entry = NULL;
3930 add_dwarf_attr (die, &attr);
3932 attr.dw_attr = DW_AT_high_pc;
3933 if (dwarf_version < 4)
3934 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3935 else
3936 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3937 lbl_id = xstrdup (lbl_high);
3938 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3939 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3940 && dwarf_split_debug_info && !force_direct)
3941 attr.dw_attr_val.val_entry
3942 = add_addr_table_entry (lbl_id, ate_kind_label);
3943 else
3944 attr.dw_attr_val.val_entry = NULL;
3945 add_dwarf_attr (die, &attr);
3948 /* Hash and equality functions for debug_str_hash. */
3950 hashval_t
3951 indirect_string_hasher::hash (indirect_string_node *x)
3953 return htab_hash_string (x->str);
3956 bool
3957 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3959 return strcmp (x1->str, x2) == 0;
3962 /* Add STR to the given string hash table. */
3964 static struct indirect_string_node *
3965 find_AT_string_in_table (const char *str,
3966 hash_table<indirect_string_hasher> *table)
3968 struct indirect_string_node *node;
3970 indirect_string_node **slot
3971 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
3972 if (*slot == NULL)
3974 node = ggc_cleared_alloc<indirect_string_node> ();
3975 node->str = ggc_strdup (str);
3976 *slot = node;
3978 else
3979 node = *slot;
3981 node->refcount++;
3982 return node;
3985 /* Add STR to the indirect string hash table. */
3987 static struct indirect_string_node *
3988 find_AT_string (const char *str)
3990 if (! debug_str_hash)
3991 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
3993 return find_AT_string_in_table (str, debug_str_hash);
3996 /* Add a string attribute value to a DIE. */
3998 static inline void
3999 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4001 dw_attr_node attr;
4002 struct indirect_string_node *node;
4004 node = find_AT_string (str);
4006 attr.dw_attr = attr_kind;
4007 attr.dw_attr_val.val_class = dw_val_class_str;
4008 attr.dw_attr_val.val_entry = NULL;
4009 attr.dw_attr_val.v.val_str = node;
4010 add_dwarf_attr (die, &attr);
4013 static inline const char *
4014 AT_string (dw_attr_ref a)
4016 gcc_assert (a && AT_class (a) == dw_val_class_str);
4017 return a->dw_attr_val.v.val_str->str;
4020 /* Call this function directly to bypass AT_string_form's logic to put
4021 the string inline in the die. */
4023 static void
4024 set_indirect_string (struct indirect_string_node *node)
4026 char label[32];
4027 /* Already indirect is a no op. */
4028 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4030 gcc_assert (node->label);
4031 return;
4033 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4034 ++dw2_string_counter;
4035 node->label = xstrdup (label);
4037 if (!dwarf_split_debug_info)
4039 node->form = DW_FORM_strp;
4040 node->index = NOT_INDEXED;
4042 else
4044 node->form = DW_FORM_GNU_str_index;
4045 node->index = NO_INDEX_ASSIGNED;
4049 /* Find out whether a string should be output inline in DIE
4050 or out-of-line in .debug_str section. */
4052 static enum dwarf_form
4053 find_string_form (struct indirect_string_node *node)
4055 unsigned int len;
4057 if (node->form)
4058 return node->form;
4060 len = strlen (node->str) + 1;
4062 /* If the string is shorter or equal to the size of the reference, it is
4063 always better to put it inline. */
4064 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4065 return node->form = DW_FORM_string;
4067 /* If we cannot expect the linker to merge strings in .debug_str
4068 section, only put it into .debug_str if it is worth even in this
4069 single module. */
4070 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4071 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4072 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4073 return node->form = DW_FORM_string;
4075 set_indirect_string (node);
4077 return node->form;
4080 /* Find out whether the string referenced from the attribute should be
4081 output inline in DIE or out-of-line in .debug_str section. */
4083 static enum dwarf_form
4084 AT_string_form (dw_attr_ref a)
4086 gcc_assert (a && AT_class (a) == dw_val_class_str);
4087 return find_string_form (a->dw_attr_val.v.val_str);
4090 /* Add a DIE reference attribute value to a DIE. */
4092 static inline void
4093 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4095 dw_attr_node attr;
4097 #ifdef ENABLE_CHECKING
4098 gcc_assert (targ_die != NULL);
4099 #else
4100 /* With LTO we can end up trying to reference something we didn't create
4101 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4102 if (targ_die == NULL)
4103 return;
4104 #endif
4106 attr.dw_attr = attr_kind;
4107 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4108 attr.dw_attr_val.val_entry = NULL;
4109 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4110 attr.dw_attr_val.v.val_die_ref.external = 0;
4111 add_dwarf_attr (die, &attr);
4114 /* Change DIE reference REF to point to NEW_DIE instead. */
4116 static inline void
4117 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4119 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4120 ref->dw_attr_val.v.val_die_ref.die = new_die;
4121 ref->dw_attr_val.v.val_die_ref.external = 0;
4124 /* Add an AT_specification attribute to a DIE, and also make the back
4125 pointer from the specification to the definition. */
4127 static inline void
4128 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4130 add_AT_die_ref (die, DW_AT_specification, targ_die);
4131 gcc_assert (!targ_die->die_definition);
4132 targ_die->die_definition = die;
4135 static inline dw_die_ref
4136 AT_ref (dw_attr_ref a)
4138 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4139 return a->dw_attr_val.v.val_die_ref.die;
4142 static inline int
4143 AT_ref_external (dw_attr_ref a)
4145 if (a && AT_class (a) == dw_val_class_die_ref)
4146 return a->dw_attr_val.v.val_die_ref.external;
4148 return 0;
4151 static inline void
4152 set_AT_ref_external (dw_attr_ref a, int i)
4154 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4155 a->dw_attr_val.v.val_die_ref.external = i;
4158 /* Add an FDE reference attribute value to a DIE. */
4160 static inline void
4161 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4163 dw_attr_node attr;
4165 attr.dw_attr = attr_kind;
4166 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4167 attr.dw_attr_val.val_entry = NULL;
4168 attr.dw_attr_val.v.val_fde_index = targ_fde;
4169 add_dwarf_attr (die, &attr);
4172 /* Add a location description attribute value to a DIE. */
4174 static inline void
4175 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4177 dw_attr_node attr;
4179 attr.dw_attr = attr_kind;
4180 attr.dw_attr_val.val_class = dw_val_class_loc;
4181 attr.dw_attr_val.val_entry = NULL;
4182 attr.dw_attr_val.v.val_loc = loc;
4183 add_dwarf_attr (die, &attr);
4186 static inline dw_loc_descr_ref
4187 AT_loc (dw_attr_ref a)
4189 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4190 return a->dw_attr_val.v.val_loc;
4193 static inline void
4194 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4196 dw_attr_node attr;
4198 attr.dw_attr = attr_kind;
4199 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4200 attr.dw_attr_val.val_entry = NULL;
4201 attr.dw_attr_val.v.val_loc_list = loc_list;
4202 add_dwarf_attr (die, &attr);
4203 have_location_lists = true;
4206 static inline dw_loc_list_ref
4207 AT_loc_list (dw_attr_ref a)
4209 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4210 return a->dw_attr_val.v.val_loc_list;
4213 static inline dw_loc_list_ref *
4214 AT_loc_list_ptr (dw_attr_ref a)
4216 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4217 return &a->dw_attr_val.v.val_loc_list;
4220 struct addr_hasher : ggc_hasher<addr_table_entry *>
4222 static hashval_t hash (addr_table_entry *);
4223 static bool equal (addr_table_entry *, addr_table_entry *);
4226 /* Table of entries into the .debug_addr section. */
4228 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4230 /* Hash an address_table_entry. */
4232 hashval_t
4233 addr_hasher::hash (addr_table_entry *a)
4235 inchash::hash hstate;
4236 switch (a->kind)
4238 case ate_kind_rtx:
4239 hstate.add_int (0);
4240 break;
4241 case ate_kind_rtx_dtprel:
4242 hstate.add_int (1);
4243 break;
4244 case ate_kind_label:
4245 return htab_hash_string (a->addr.label);
4246 default:
4247 gcc_unreachable ();
4249 inchash::add_rtx (a->addr.rtl, hstate);
4250 return hstate.end ();
4253 /* Determine equality for two address_table_entries. */
4255 bool
4256 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4258 if (a1->kind != a2->kind)
4259 return 0;
4260 switch (a1->kind)
4262 case ate_kind_rtx:
4263 case ate_kind_rtx_dtprel:
4264 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4265 case ate_kind_label:
4266 return strcmp (a1->addr.label, a2->addr.label) == 0;
4267 default:
4268 gcc_unreachable ();
4272 /* Initialize an addr_table_entry. */
4274 void
4275 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4277 e->kind = kind;
4278 switch (kind)
4280 case ate_kind_rtx:
4281 case ate_kind_rtx_dtprel:
4282 e->addr.rtl = (rtx) addr;
4283 break;
4284 case ate_kind_label:
4285 e->addr.label = (char *) addr;
4286 break;
4288 e->refcount = 0;
4289 e->index = NO_INDEX_ASSIGNED;
4292 /* Add attr to the address table entry to the table. Defer setting an
4293 index until output time. */
4295 static addr_table_entry *
4296 add_addr_table_entry (void *addr, enum ate_kind kind)
4298 addr_table_entry *node;
4299 addr_table_entry finder;
4301 gcc_assert (dwarf_split_debug_info);
4302 if (! addr_index_table)
4303 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4304 init_addr_table_entry (&finder, kind, addr);
4305 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4307 if (*slot == HTAB_EMPTY_ENTRY)
4309 node = ggc_cleared_alloc<addr_table_entry> ();
4310 init_addr_table_entry (node, kind, addr);
4311 *slot = node;
4313 else
4314 node = *slot;
4316 node->refcount++;
4317 return node;
4320 /* Remove an entry from the addr table by decrementing its refcount.
4321 Strictly, decrementing the refcount would be enough, but the
4322 assertion that the entry is actually in the table has found
4323 bugs. */
4325 static void
4326 remove_addr_table_entry (addr_table_entry *entry)
4328 gcc_assert (dwarf_split_debug_info && addr_index_table);
4329 /* After an index is assigned, the table is frozen. */
4330 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4331 entry->refcount--;
4334 /* Given a location list, remove all addresses it refers to from the
4335 address_table. */
4337 static void
4338 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4340 for (; descr; descr = descr->dw_loc_next)
4341 if (descr->dw_loc_oprnd1.val_entry != NULL)
4343 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4344 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4348 /* A helper function for dwarf2out_finish called through
4349 htab_traverse. Assign an addr_table_entry its index. All entries
4350 must be collected into the table when this function is called,
4351 because the indexing code relies on htab_traverse to traverse nodes
4352 in the same order for each run. */
4355 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4357 addr_table_entry *node = *h;
4359 /* Don't index unreferenced nodes. */
4360 if (node->refcount == 0)
4361 return 1;
4363 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4364 node->index = *index;
4365 *index += 1;
4367 return 1;
4370 /* Add an address constant attribute value to a DIE. When using
4371 dwarf_split_debug_info, address attributes in dies destined for the
4372 final executable should be direct references--setting the parameter
4373 force_direct ensures this behavior. */
4375 static inline void
4376 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4377 bool force_direct)
4379 dw_attr_node attr;
4381 attr.dw_attr = attr_kind;
4382 attr.dw_attr_val.val_class = dw_val_class_addr;
4383 attr.dw_attr_val.v.val_addr = addr;
4384 if (dwarf_split_debug_info && !force_direct)
4385 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4386 else
4387 attr.dw_attr_val.val_entry = NULL;
4388 add_dwarf_attr (die, &attr);
4391 /* Get the RTX from to an address DIE attribute. */
4393 static inline rtx
4394 AT_addr (dw_attr_ref a)
4396 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4397 return a->dw_attr_val.v.val_addr;
4400 /* Add a file attribute value to a DIE. */
4402 static inline void
4403 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4404 struct dwarf_file_data *fd)
4406 dw_attr_node attr;
4408 attr.dw_attr = attr_kind;
4409 attr.dw_attr_val.val_class = dw_val_class_file;
4410 attr.dw_attr_val.val_entry = NULL;
4411 attr.dw_attr_val.v.val_file = fd;
4412 add_dwarf_attr (die, &attr);
4415 /* Get the dwarf_file_data from a file DIE attribute. */
4417 static inline struct dwarf_file_data *
4418 AT_file (dw_attr_ref a)
4420 gcc_assert (a && AT_class (a) == dw_val_class_file);
4421 return a->dw_attr_val.v.val_file;
4424 /* Add a vms delta attribute value to a DIE. */
4426 static inline void
4427 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4428 const char *lbl1, const char *lbl2)
4430 dw_attr_node attr;
4432 attr.dw_attr = attr_kind;
4433 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4434 attr.dw_attr_val.val_entry = NULL;
4435 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4436 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4437 add_dwarf_attr (die, &attr);
4440 /* Add a label identifier attribute value to a DIE. */
4442 static inline void
4443 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4444 const char *lbl_id)
4446 dw_attr_node attr;
4448 attr.dw_attr = attr_kind;
4449 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4450 attr.dw_attr_val.val_entry = NULL;
4451 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4452 if (dwarf_split_debug_info)
4453 attr.dw_attr_val.val_entry
4454 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4455 ate_kind_label);
4456 add_dwarf_attr (die, &attr);
4459 /* Add a section offset attribute value to a DIE, an offset into the
4460 debug_line section. */
4462 static inline void
4463 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4464 const char *label)
4466 dw_attr_node attr;
4468 attr.dw_attr = attr_kind;
4469 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4470 attr.dw_attr_val.val_entry = NULL;
4471 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4472 add_dwarf_attr (die, &attr);
4475 /* Add a section offset attribute value to a DIE, an offset into the
4476 debug_macinfo section. */
4478 static inline void
4479 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4480 const char *label)
4482 dw_attr_node attr;
4484 attr.dw_attr = attr_kind;
4485 attr.dw_attr_val.val_class = dw_val_class_macptr;
4486 attr.dw_attr_val.val_entry = NULL;
4487 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4488 add_dwarf_attr (die, &attr);
4491 /* Add an offset attribute value to a DIE. */
4493 static inline void
4494 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4495 unsigned HOST_WIDE_INT offset)
4497 dw_attr_node attr;
4499 attr.dw_attr = attr_kind;
4500 attr.dw_attr_val.val_class = dw_val_class_offset;
4501 attr.dw_attr_val.val_entry = NULL;
4502 attr.dw_attr_val.v.val_offset = offset;
4503 add_dwarf_attr (die, &attr);
4506 /* Add a range_list attribute value to a DIE. When using
4507 dwarf_split_debug_info, address attributes in dies destined for the
4508 final executable should be direct references--setting the parameter
4509 force_direct ensures this behavior. */
4511 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4512 #define RELOCATED_OFFSET (NULL)
4514 static void
4515 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4516 long unsigned int offset, bool force_direct)
4518 dw_attr_node attr;
4520 attr.dw_attr = attr_kind;
4521 attr.dw_attr_val.val_class = dw_val_class_range_list;
4522 /* For the range_list attribute, use val_entry to store whether the
4523 offset should follow split-debug-info or normal semantics. This
4524 value is read in output_range_list_offset. */
4525 if (dwarf_split_debug_info && !force_direct)
4526 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4527 else
4528 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4529 attr.dw_attr_val.v.val_offset = offset;
4530 add_dwarf_attr (die, &attr);
4533 /* Return the start label of a delta attribute. */
4535 static inline const char *
4536 AT_vms_delta1 (dw_attr_ref a)
4538 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4539 return a->dw_attr_val.v.val_vms_delta.lbl1;
4542 /* Return the end label of a delta attribute. */
4544 static inline const char *
4545 AT_vms_delta2 (dw_attr_ref a)
4547 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4548 return a->dw_attr_val.v.val_vms_delta.lbl2;
4551 static inline const char *
4552 AT_lbl (dw_attr_ref a)
4554 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4555 || AT_class (a) == dw_val_class_lineptr
4556 || AT_class (a) == dw_val_class_macptr
4557 || AT_class (a) == dw_val_class_high_pc));
4558 return a->dw_attr_val.v.val_lbl_id;
4561 /* Get the attribute of type attr_kind. */
4563 static dw_attr_ref
4564 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4566 dw_attr_ref a;
4567 unsigned ix;
4568 dw_die_ref spec = NULL;
4570 if (! die)
4571 return NULL;
4573 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4574 if (a->dw_attr == attr_kind)
4575 return a;
4576 else if (a->dw_attr == DW_AT_specification
4577 || a->dw_attr == DW_AT_abstract_origin)
4578 spec = AT_ref (a);
4580 if (spec)
4581 return get_AT (spec, attr_kind);
4583 return NULL;
4586 /* Returns the parent of the declaration of DIE. */
4588 static dw_die_ref
4589 get_die_parent (dw_die_ref die)
4591 dw_die_ref t;
4593 if (!die)
4594 return NULL;
4596 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4597 || (t = get_AT_ref (die, DW_AT_specification)))
4598 die = t;
4600 return die->die_parent;
4603 /* Return the "low pc" attribute value, typically associated with a subprogram
4604 DIE. Return null if the "low pc" attribute is either not present, or if it
4605 cannot be represented as an assembler label identifier. */
4607 static inline const char *
4608 get_AT_low_pc (dw_die_ref die)
4610 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4612 return a ? AT_lbl (a) : NULL;
4615 /* Return the "high pc" attribute value, typically associated with a subprogram
4616 DIE. Return null if the "high pc" attribute is either not present, or if it
4617 cannot be represented as an assembler label identifier. */
4619 static inline const char *
4620 get_AT_hi_pc (dw_die_ref die)
4622 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4624 return a ? AT_lbl (a) : NULL;
4627 /* Return the value of the string attribute designated by ATTR_KIND, or
4628 NULL if it is not present. */
4630 static inline const char *
4631 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4633 dw_attr_ref a = get_AT (die, attr_kind);
4635 return a ? AT_string (a) : NULL;
4638 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4639 if it is not present. */
4641 static inline int
4642 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4644 dw_attr_ref a = get_AT (die, attr_kind);
4646 return a ? AT_flag (a) : 0;
4649 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4650 if it is not present. */
4652 static inline unsigned
4653 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4655 dw_attr_ref a = get_AT (die, attr_kind);
4657 return a ? AT_unsigned (a) : 0;
4660 static inline dw_die_ref
4661 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4663 dw_attr_ref a = get_AT (die, attr_kind);
4665 return a ? AT_ref (a) : NULL;
4668 static inline struct dwarf_file_data *
4669 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4671 dw_attr_ref a = get_AT (die, attr_kind);
4673 return a ? AT_file (a) : NULL;
4676 /* Return TRUE if the language is C++. */
4678 static inline bool
4679 is_cxx (void)
4681 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4683 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4686 /* Return TRUE if the language is Java. */
4688 static inline bool
4689 is_java (void)
4691 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4693 return lang == DW_LANG_Java;
4696 /* Return TRUE if the language is Fortran. */
4698 static inline bool
4699 is_fortran (void)
4701 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4703 return (lang == DW_LANG_Fortran77
4704 || lang == DW_LANG_Fortran90
4705 || lang == DW_LANG_Fortran95);
4708 /* Return TRUE if the language is Ada. */
4710 static inline bool
4711 is_ada (void)
4713 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4715 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4718 /* Remove the specified attribute if present. */
4720 static void
4721 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4723 dw_attr_ref a;
4724 unsigned ix;
4726 if (! die)
4727 return;
4729 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4730 if (a->dw_attr == attr_kind)
4732 if (AT_class (a) == dw_val_class_str)
4733 if (a->dw_attr_val.v.val_str->refcount)
4734 a->dw_attr_val.v.val_str->refcount--;
4736 /* vec::ordered_remove should help reduce the number of abbrevs
4737 that are needed. */
4738 die->die_attr->ordered_remove (ix);
4739 return;
4743 /* Remove CHILD from its parent. PREV must have the property that
4744 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4746 static void
4747 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4749 gcc_assert (child->die_parent == prev->die_parent);
4750 gcc_assert (prev->die_sib == child);
4751 if (prev == child)
4753 gcc_assert (child->die_parent->die_child == child);
4754 prev = NULL;
4756 else
4757 prev->die_sib = child->die_sib;
4758 if (child->die_parent->die_child == child)
4759 child->die_parent->die_child = prev;
4762 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4763 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4765 static void
4766 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4768 dw_die_ref parent = old_child->die_parent;
4770 gcc_assert (parent == prev->die_parent);
4771 gcc_assert (prev->die_sib == old_child);
4773 new_child->die_parent = parent;
4774 if (prev == old_child)
4776 gcc_assert (parent->die_child == old_child);
4777 new_child->die_sib = new_child;
4779 else
4781 prev->die_sib = new_child;
4782 new_child->die_sib = old_child->die_sib;
4784 if (old_child->die_parent->die_child == old_child)
4785 old_child->die_parent->die_child = new_child;
4788 /* Move all children from OLD_PARENT to NEW_PARENT. */
4790 static void
4791 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4793 dw_die_ref c;
4794 new_parent->die_child = old_parent->die_child;
4795 old_parent->die_child = NULL;
4796 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4799 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4800 matches TAG. */
4802 static void
4803 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4805 dw_die_ref c;
4807 c = die->die_child;
4808 if (c) do {
4809 dw_die_ref prev = c;
4810 c = c->die_sib;
4811 while (c->die_tag == tag)
4813 remove_child_with_prev (c, prev);
4814 /* Might have removed every child. */
4815 if (c == c->die_sib)
4816 return;
4817 c = c->die_sib;
4819 } while (c != die->die_child);
4822 /* Add a CHILD_DIE as the last child of DIE. */
4824 static void
4825 add_child_die (dw_die_ref die, dw_die_ref child_die)
4827 /* FIXME this should probably be an assert. */
4828 if (! die || ! child_die)
4829 return;
4830 gcc_assert (die != child_die);
4832 child_die->die_parent = die;
4833 if (die->die_child)
4835 child_die->die_sib = die->die_child->die_sib;
4836 die->die_child->die_sib = child_die;
4838 else
4839 child_die->die_sib = child_die;
4840 die->die_child = child_die;
4843 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4844 is the specification, to the end of PARENT's list of children.
4845 This is done by removing and re-adding it. */
4847 static void
4848 splice_child_die (dw_die_ref parent, dw_die_ref child)
4850 dw_die_ref p;
4852 /* We want the declaration DIE from inside the class, not the
4853 specification DIE at toplevel. */
4854 if (child->die_parent != parent)
4856 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4858 if (tmp)
4859 child = tmp;
4862 gcc_assert (child->die_parent == parent
4863 || (child->die_parent
4864 == get_AT_ref (parent, DW_AT_specification)));
4866 for (p = child->die_parent->die_child; ; p = p->die_sib)
4867 if (p->die_sib == child)
4869 remove_child_with_prev (child, p);
4870 break;
4873 add_child_die (parent, child);
4876 /* Return a pointer to a newly created DIE node. */
4878 static inline dw_die_ref
4879 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4881 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4883 die->die_tag = tag_value;
4885 if (parent_die != NULL)
4886 add_child_die (parent_die, die);
4887 else
4889 limbo_die_node *limbo_node;
4891 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4892 limbo_node->die = die;
4893 limbo_node->created_for = t;
4894 limbo_node->next = limbo_die_list;
4895 limbo_die_list = limbo_node;
4898 return die;
4901 /* Return the DIE associated with the given type specifier. */
4903 static inline dw_die_ref
4904 lookup_type_die (tree type)
4906 return TYPE_SYMTAB_DIE (type);
4909 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4910 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4911 anonymous type instead the one of the naming typedef. */
4913 static inline dw_die_ref
4914 strip_naming_typedef (tree type, dw_die_ref type_die)
4916 if (type
4917 && TREE_CODE (type) == RECORD_TYPE
4918 && type_die
4919 && type_die->die_tag == DW_TAG_typedef
4920 && is_naming_typedef_decl (TYPE_NAME (type)))
4921 type_die = get_AT_ref (type_die, DW_AT_type);
4922 return type_die;
4925 /* Like lookup_type_die, but if type is an anonymous type named by a
4926 typedef[1], return the DIE of the anonymous type instead the one of
4927 the naming typedef. This is because in gen_typedef_die, we did
4928 equate the anonymous struct named by the typedef with the DIE of
4929 the naming typedef. So by default, lookup_type_die on an anonymous
4930 struct yields the DIE of the naming typedef.
4932 [1]: Read the comment of is_naming_typedef_decl to learn about what
4933 a naming typedef is. */
4935 static inline dw_die_ref
4936 lookup_type_die_strip_naming_typedef (tree type)
4938 dw_die_ref die = lookup_type_die (type);
4939 return strip_naming_typedef (type, die);
4942 /* Equate a DIE to a given type specifier. */
4944 static inline void
4945 equate_type_number_to_die (tree type, dw_die_ref type_die)
4947 TYPE_SYMTAB_DIE (type) = type_die;
4950 /* Returns a hash value for X (which really is a die_struct). */
4952 inline hashval_t
4953 decl_die_hasher::hash (die_node *x)
4955 return (hashval_t) x->decl_id;
4958 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4960 inline bool
4961 decl_die_hasher::equal (die_node *x, tree y)
4963 return (x->decl_id == DECL_UID (y));
4966 /* Return the DIE associated with a given declaration. */
4968 static inline dw_die_ref
4969 lookup_decl_die (tree decl)
4971 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
4974 /* Returns a hash value for X (which really is a var_loc_list). */
4976 inline hashval_t
4977 decl_loc_hasher::hash (var_loc_list *x)
4979 return (hashval_t) x->decl_id;
4982 /* Return nonzero if decl_id of var_loc_list X is the same as
4983 UID of decl *Y. */
4985 inline bool
4986 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
4988 return (x->decl_id == DECL_UID (y));
4991 /* Return the var_loc list associated with a given declaration. */
4993 static inline var_loc_list *
4994 lookup_decl_loc (const_tree decl)
4996 if (!decl_loc_table)
4997 return NULL;
4998 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5001 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5003 inline hashval_t
5004 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5006 return (hashval_t) x->decl_id;
5009 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5010 UID of decl *Y. */
5012 inline bool
5013 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5015 return (x->decl_id == DECL_UID (y));
5018 /* Equate a DIE to a particular declaration. */
5020 static void
5021 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5023 unsigned int decl_id = DECL_UID (decl);
5025 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5026 decl_die->decl_id = decl_id;
5029 /* Return how many bits covers PIECE EXPR_LIST. */
5031 static int
5032 decl_piece_bitsize (rtx piece)
5034 int ret = (int) GET_MODE (piece);
5035 if (ret)
5036 return ret;
5037 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5038 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5039 return INTVAL (XEXP (XEXP (piece, 0), 0));
5042 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5044 static rtx *
5045 decl_piece_varloc_ptr (rtx piece)
5047 if ((int) GET_MODE (piece))
5048 return &XEXP (piece, 0);
5049 else
5050 return &XEXP (XEXP (piece, 0), 1);
5053 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5054 Next is the chain of following piece nodes. */
5056 static rtx_expr_list *
5057 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5059 if (bitsize <= (int) MAX_MACHINE_MODE)
5060 return alloc_EXPR_LIST (bitsize, loc_note, next);
5061 else
5062 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5063 GEN_INT (bitsize),
5064 loc_note), next);
5067 /* Return rtx that should be stored into loc field for
5068 LOC_NOTE and BITPOS/BITSIZE. */
5070 static rtx
5071 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5072 HOST_WIDE_INT bitsize)
5074 if (bitsize != -1)
5076 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5077 if (bitpos != 0)
5078 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5080 return loc_note;
5083 /* This function either modifies location piece list *DEST in
5084 place (if SRC and INNER is NULL), or copies location piece list
5085 *SRC to *DEST while modifying it. Location BITPOS is modified
5086 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5087 not copied and if needed some padding around it is added.
5088 When modifying in place, DEST should point to EXPR_LIST where
5089 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5090 to the start of the whole list and INNER points to the EXPR_LIST
5091 where earlier pieces cover PIECE_BITPOS bits. */
5093 static void
5094 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5095 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5096 HOST_WIDE_INT bitsize, rtx loc_note)
5098 int diff;
5099 bool copy = inner != NULL;
5101 if (copy)
5103 /* First copy all nodes preceding the current bitpos. */
5104 while (src != inner)
5106 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5107 decl_piece_bitsize (*src), NULL_RTX);
5108 dest = &XEXP (*dest, 1);
5109 src = &XEXP (*src, 1);
5112 /* Add padding if needed. */
5113 if (bitpos != piece_bitpos)
5115 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5116 copy ? NULL_RTX : *dest);
5117 dest = &XEXP (*dest, 1);
5119 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5121 gcc_assert (!copy);
5122 /* A piece with correct bitpos and bitsize already exist,
5123 just update the location for it and return. */
5124 *decl_piece_varloc_ptr (*dest) = loc_note;
5125 return;
5127 /* Add the piece that changed. */
5128 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5129 dest = &XEXP (*dest, 1);
5130 /* Skip over pieces that overlap it. */
5131 diff = bitpos - piece_bitpos + bitsize;
5132 if (!copy)
5133 src = dest;
5134 while (diff > 0 && *src)
5136 rtx piece = *src;
5137 diff -= decl_piece_bitsize (piece);
5138 if (copy)
5139 src = &XEXP (piece, 1);
5140 else
5142 *src = XEXP (piece, 1);
5143 free_EXPR_LIST_node (piece);
5146 /* Add padding if needed. */
5147 if (diff < 0 && *src)
5149 if (!copy)
5150 dest = src;
5151 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5152 dest = &XEXP (*dest, 1);
5154 if (!copy)
5155 return;
5156 /* Finally copy all nodes following it. */
5157 while (*src)
5159 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5160 decl_piece_bitsize (*src), NULL_RTX);
5161 dest = &XEXP (*dest, 1);
5162 src = &XEXP (*src, 1);
5166 /* Add a variable location node to the linked list for DECL. */
5168 static struct var_loc_node *
5169 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5171 unsigned int decl_id;
5172 var_loc_list *temp;
5173 struct var_loc_node *loc = NULL;
5174 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5176 if (TREE_CODE (decl) == VAR_DECL
5177 && DECL_HAS_DEBUG_EXPR_P (decl))
5179 tree realdecl = DECL_DEBUG_EXPR (decl);
5180 if (handled_component_p (realdecl)
5181 || (TREE_CODE (realdecl) == MEM_REF
5182 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5184 HOST_WIDE_INT maxsize;
5185 tree innerdecl;
5186 innerdecl
5187 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5188 if (!DECL_P (innerdecl)
5189 || DECL_IGNORED_P (innerdecl)
5190 || TREE_STATIC (innerdecl)
5191 || bitsize <= 0
5192 || bitpos + bitsize > 256
5193 || bitsize != maxsize)
5194 return NULL;
5195 decl = innerdecl;
5199 decl_id = DECL_UID (decl);
5200 var_loc_list **slot
5201 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5202 if (*slot == NULL)
5204 temp = ggc_cleared_alloc<var_loc_list> ();
5205 temp->decl_id = decl_id;
5206 *slot = temp;
5208 else
5209 temp = *slot;
5211 /* For PARM_DECLs try to keep around the original incoming value,
5212 even if that means we'll emit a zero-range .debug_loc entry. */
5213 if (temp->last
5214 && temp->first == temp->last
5215 && TREE_CODE (decl) == PARM_DECL
5216 && NOTE_P (temp->first->loc)
5217 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5218 && DECL_INCOMING_RTL (decl)
5219 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5220 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5221 == GET_CODE (DECL_INCOMING_RTL (decl))
5222 && prev_real_insn (temp->first->loc) == NULL_RTX
5223 && (bitsize != -1
5224 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5225 NOTE_VAR_LOCATION_LOC (loc_note))
5226 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5227 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5229 loc = ggc_cleared_alloc<var_loc_node> ();
5230 temp->first->next = loc;
5231 temp->last = loc;
5232 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5234 else if (temp->last)
5236 struct var_loc_node *last = temp->last, *unused = NULL;
5237 rtx *piece_loc = NULL, last_loc_note;
5238 int piece_bitpos = 0;
5239 if (last->next)
5241 last = last->next;
5242 gcc_assert (last->next == NULL);
5244 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5246 piece_loc = &last->loc;
5249 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5250 if (piece_bitpos + cur_bitsize > bitpos)
5251 break;
5252 piece_bitpos += cur_bitsize;
5253 piece_loc = &XEXP (*piece_loc, 1);
5255 while (*piece_loc);
5257 /* TEMP->LAST here is either pointer to the last but one or
5258 last element in the chained list, LAST is pointer to the
5259 last element. */
5260 if (label && strcmp (last->label, label) == 0)
5262 /* For SRA optimized variables if there weren't any real
5263 insns since last note, just modify the last node. */
5264 if (piece_loc != NULL)
5266 adjust_piece_list (piece_loc, NULL, NULL,
5267 bitpos, piece_bitpos, bitsize, loc_note);
5268 return NULL;
5270 /* If the last note doesn't cover any instructions, remove it. */
5271 if (temp->last != last)
5273 temp->last->next = NULL;
5274 unused = last;
5275 last = temp->last;
5276 gcc_assert (strcmp (last->label, label) != 0);
5278 else
5280 gcc_assert (temp->first == temp->last
5281 || (temp->first->next == temp->last
5282 && TREE_CODE (decl) == PARM_DECL));
5283 memset (temp->last, '\0', sizeof (*temp->last));
5284 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5285 return temp->last;
5288 if (bitsize == -1 && NOTE_P (last->loc))
5289 last_loc_note = last->loc;
5290 else if (piece_loc != NULL
5291 && *piece_loc != NULL_RTX
5292 && piece_bitpos == bitpos
5293 && decl_piece_bitsize (*piece_loc) == bitsize)
5294 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5295 else
5296 last_loc_note = NULL_RTX;
5297 /* If the current location is the same as the end of the list,
5298 and either both or neither of the locations is uninitialized,
5299 we have nothing to do. */
5300 if (last_loc_note == NULL_RTX
5301 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5302 NOTE_VAR_LOCATION_LOC (loc_note)))
5303 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5304 != NOTE_VAR_LOCATION_STATUS (loc_note))
5305 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5306 == VAR_INIT_STATUS_UNINITIALIZED)
5307 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5308 == VAR_INIT_STATUS_UNINITIALIZED))))
5310 /* Add LOC to the end of list and update LAST. If the last
5311 element of the list has been removed above, reuse its
5312 memory for the new node, otherwise allocate a new one. */
5313 if (unused)
5315 loc = unused;
5316 memset (loc, '\0', sizeof (*loc));
5318 else
5319 loc = ggc_cleared_alloc<var_loc_node> ();
5320 if (bitsize == -1 || piece_loc == NULL)
5321 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5322 else
5323 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5324 bitpos, piece_bitpos, bitsize, loc_note);
5325 last->next = loc;
5326 /* Ensure TEMP->LAST will point either to the new last but one
5327 element of the chain, or to the last element in it. */
5328 if (last != temp->last)
5329 temp->last = last;
5331 else if (unused)
5332 ggc_free (unused);
5334 else
5336 loc = ggc_cleared_alloc<var_loc_node> ();
5337 temp->first = loc;
5338 temp->last = loc;
5339 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5341 return loc;
5344 /* Keep track of the number of spaces used to indent the
5345 output of the debugging routines that print the structure of
5346 the DIE internal representation. */
5347 static int print_indent;
5349 /* Indent the line the number of spaces given by print_indent. */
5351 static inline void
5352 print_spaces (FILE *outfile)
5354 fprintf (outfile, "%*s", print_indent, "");
5357 /* Print a type signature in hex. */
5359 static inline void
5360 print_signature (FILE *outfile, char *sig)
5362 int i;
5364 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5365 fprintf (outfile, "%02x", sig[i] & 0xff);
5368 /* Print the information associated with a given DIE, and its children.
5369 This routine is a debugging aid only. */
5371 static void
5372 print_die (dw_die_ref die, FILE *outfile)
5374 dw_attr_ref a;
5375 dw_die_ref c;
5376 unsigned ix;
5378 print_spaces (outfile);
5379 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5380 die->die_offset, dwarf_tag_name (die->die_tag),
5381 (void*) die);
5382 print_spaces (outfile);
5383 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5384 fprintf (outfile, " offset: %ld", die->die_offset);
5385 fprintf (outfile, " mark: %d\n", die->die_mark);
5387 if (die->comdat_type_p)
5389 print_spaces (outfile);
5390 fprintf (outfile, " signature: ");
5391 print_signature (outfile, die->die_id.die_type_node->signature);
5392 fprintf (outfile, "\n");
5395 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5397 print_spaces (outfile);
5398 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5400 switch (AT_class (a))
5402 case dw_val_class_addr:
5403 fprintf (outfile, "address");
5404 break;
5405 case dw_val_class_offset:
5406 fprintf (outfile, "offset");
5407 break;
5408 case dw_val_class_loc:
5409 fprintf (outfile, "location descriptor");
5410 break;
5411 case dw_val_class_loc_list:
5412 fprintf (outfile, "location list -> label:%s",
5413 AT_loc_list (a)->ll_symbol);
5414 break;
5415 case dw_val_class_range_list:
5416 fprintf (outfile, "range list");
5417 break;
5418 case dw_val_class_const:
5419 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5420 break;
5421 case dw_val_class_unsigned_const:
5422 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5423 break;
5424 case dw_val_class_const_double:
5425 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5426 HOST_WIDE_INT_PRINT_UNSIGNED")",
5427 a->dw_attr_val.v.val_double.high,
5428 a->dw_attr_val.v.val_double.low);
5429 break;
5430 case dw_val_class_wide_int:
5432 int i = a->dw_attr_val.v.val_wide->get_len ();
5433 fprintf (outfile, "constant (");
5434 gcc_assert (i > 0);
5435 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5436 fprintf (outfile, "0x");
5437 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5438 a->dw_attr_val.v.val_wide->elt (--i));
5439 while (--i >= 0)
5440 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5441 a->dw_attr_val.v.val_wide->elt (i));
5442 fprintf (outfile, ")");
5443 break;
5445 case dw_val_class_vec:
5446 fprintf (outfile, "floating-point or vector constant");
5447 break;
5448 case dw_val_class_flag:
5449 fprintf (outfile, "%u", AT_flag (a));
5450 break;
5451 case dw_val_class_die_ref:
5452 if (AT_ref (a) != NULL)
5454 if (AT_ref (a)->comdat_type_p)
5456 fprintf (outfile, "die -> signature: ");
5457 print_signature (outfile,
5458 AT_ref (a)->die_id.die_type_node->signature);
5460 else if (AT_ref (a)->die_id.die_symbol)
5461 fprintf (outfile, "die -> label: %s",
5462 AT_ref (a)->die_id.die_symbol);
5463 else
5464 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5465 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5467 else
5468 fprintf (outfile, "die -> <null>");
5469 break;
5470 case dw_val_class_vms_delta:
5471 fprintf (outfile, "delta: @slotcount(%s-%s)",
5472 AT_vms_delta2 (a), AT_vms_delta1 (a));
5473 break;
5474 case dw_val_class_lbl_id:
5475 case dw_val_class_lineptr:
5476 case dw_val_class_macptr:
5477 case dw_val_class_high_pc:
5478 fprintf (outfile, "label: %s", AT_lbl (a));
5479 break;
5480 case dw_val_class_str:
5481 if (AT_string (a) != NULL)
5482 fprintf (outfile, "\"%s\"", AT_string (a));
5483 else
5484 fprintf (outfile, "<null>");
5485 break;
5486 case dw_val_class_file:
5487 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5488 AT_file (a)->emitted_number);
5489 break;
5490 case dw_val_class_data8:
5492 int i;
5494 for (i = 0; i < 8; i++)
5495 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5496 break;
5498 default:
5499 break;
5502 fprintf (outfile, "\n");
5505 if (die->die_child != NULL)
5507 print_indent += 4;
5508 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5509 print_indent -= 4;
5511 if (print_indent == 0)
5512 fprintf (outfile, "\n");
5515 /* Print the information collected for a given DIE. */
5517 DEBUG_FUNCTION void
5518 debug_dwarf_die (dw_die_ref die)
5520 print_die (die, stderr);
5523 DEBUG_FUNCTION void
5524 debug (die_struct &ref)
5526 print_die (&ref, stderr);
5529 DEBUG_FUNCTION void
5530 debug (die_struct *ptr)
5532 if (ptr)
5533 debug (*ptr);
5534 else
5535 fprintf (stderr, "<nil>\n");
5539 /* Print all DWARF information collected for the compilation unit.
5540 This routine is a debugging aid only. */
5542 DEBUG_FUNCTION void
5543 debug_dwarf (void)
5545 print_indent = 0;
5546 print_die (comp_unit_die (), stderr);
5549 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5550 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5551 DIE that marks the start of the DIEs for this include file. */
5553 static dw_die_ref
5554 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5556 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5557 dw_die_ref new_unit = gen_compile_unit_die (filename);
5559 new_unit->die_sib = old_unit;
5560 return new_unit;
5563 /* Close an include-file CU and reopen the enclosing one. */
5565 static dw_die_ref
5566 pop_compile_unit (dw_die_ref old_unit)
5568 dw_die_ref new_unit = old_unit->die_sib;
5570 old_unit->die_sib = NULL;
5571 return new_unit;
5574 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5575 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5576 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5578 /* Calculate the checksum of a location expression. */
5580 static inline void
5581 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5583 int tem;
5584 inchash::hash hstate;
5585 hashval_t hash;
5587 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5588 CHECKSUM (tem);
5589 hash_loc_operands (loc, hstate);
5590 hash = hstate.end();
5591 CHECKSUM (hash);
5594 /* Calculate the checksum of an attribute. */
5596 static void
5597 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5599 dw_loc_descr_ref loc;
5600 rtx r;
5602 CHECKSUM (at->dw_attr);
5604 /* We don't care that this was compiled with a different compiler
5605 snapshot; if the output is the same, that's what matters. */
5606 if (at->dw_attr == DW_AT_producer)
5607 return;
5609 switch (AT_class (at))
5611 case dw_val_class_const:
5612 CHECKSUM (at->dw_attr_val.v.val_int);
5613 break;
5614 case dw_val_class_unsigned_const:
5615 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5616 break;
5617 case dw_val_class_const_double:
5618 CHECKSUM (at->dw_attr_val.v.val_double);
5619 break;
5620 case dw_val_class_wide_int:
5621 CHECKSUM (*at->dw_attr_val.v.val_wide);
5622 break;
5623 case dw_val_class_vec:
5624 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5625 (at->dw_attr_val.v.val_vec.length
5626 * at->dw_attr_val.v.val_vec.elt_size));
5627 break;
5628 case dw_val_class_flag:
5629 CHECKSUM (at->dw_attr_val.v.val_flag);
5630 break;
5631 case dw_val_class_str:
5632 CHECKSUM_STRING (AT_string (at));
5633 break;
5635 case dw_val_class_addr:
5636 r = AT_addr (at);
5637 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5638 CHECKSUM_STRING (XSTR (r, 0));
5639 break;
5641 case dw_val_class_offset:
5642 CHECKSUM (at->dw_attr_val.v.val_offset);
5643 break;
5645 case dw_val_class_loc:
5646 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5647 loc_checksum (loc, ctx);
5648 break;
5650 case dw_val_class_die_ref:
5651 die_checksum (AT_ref (at), ctx, mark);
5652 break;
5654 case dw_val_class_fde_ref:
5655 case dw_val_class_vms_delta:
5656 case dw_val_class_lbl_id:
5657 case dw_val_class_lineptr:
5658 case dw_val_class_macptr:
5659 case dw_val_class_high_pc:
5660 break;
5662 case dw_val_class_file:
5663 CHECKSUM_STRING (AT_file (at)->filename);
5664 break;
5666 case dw_val_class_data8:
5667 CHECKSUM (at->dw_attr_val.v.val_data8);
5668 break;
5670 default:
5671 break;
5675 /* Calculate the checksum of a DIE. */
5677 static void
5678 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5680 dw_die_ref c;
5681 dw_attr_ref a;
5682 unsigned ix;
5684 /* To avoid infinite recursion. */
5685 if (die->die_mark)
5687 CHECKSUM (die->die_mark);
5688 return;
5690 die->die_mark = ++(*mark);
5692 CHECKSUM (die->die_tag);
5694 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5695 attr_checksum (a, ctx, mark);
5697 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5700 #undef CHECKSUM
5701 #undef CHECKSUM_BLOCK
5702 #undef CHECKSUM_STRING
5704 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5705 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5706 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5707 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5708 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5709 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5710 #define CHECKSUM_ATTR(FOO) \
5711 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5713 /* Calculate the checksum of a number in signed LEB128 format. */
5715 static void
5716 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5718 unsigned char byte;
5719 bool more;
5721 while (1)
5723 byte = (value & 0x7f);
5724 value >>= 7;
5725 more = !((value == 0 && (byte & 0x40) == 0)
5726 || (value == -1 && (byte & 0x40) != 0));
5727 if (more)
5728 byte |= 0x80;
5729 CHECKSUM (byte);
5730 if (!more)
5731 break;
5735 /* Calculate the checksum of a number in unsigned LEB128 format. */
5737 static void
5738 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5740 while (1)
5742 unsigned char byte = (value & 0x7f);
5743 value >>= 7;
5744 if (value != 0)
5745 /* More bytes to follow. */
5746 byte |= 0x80;
5747 CHECKSUM (byte);
5748 if (value == 0)
5749 break;
5753 /* Checksum the context of the DIE. This adds the names of any
5754 surrounding namespaces or structures to the checksum. */
5756 static void
5757 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5759 const char *name;
5760 dw_die_ref spec;
5761 int tag = die->die_tag;
5763 if (tag != DW_TAG_namespace
5764 && tag != DW_TAG_structure_type
5765 && tag != DW_TAG_class_type)
5766 return;
5768 name = get_AT_string (die, DW_AT_name);
5770 spec = get_AT_ref (die, DW_AT_specification);
5771 if (spec != NULL)
5772 die = spec;
5774 if (die->die_parent != NULL)
5775 checksum_die_context (die->die_parent, ctx);
5777 CHECKSUM_ULEB128 ('C');
5778 CHECKSUM_ULEB128 (tag);
5779 if (name != NULL)
5780 CHECKSUM_STRING (name);
5783 /* Calculate the checksum of a location expression. */
5785 static inline void
5786 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5788 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5789 were emitted as a DW_FORM_sdata instead of a location expression. */
5790 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5792 CHECKSUM_ULEB128 (DW_FORM_sdata);
5793 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5794 return;
5797 /* Otherwise, just checksum the raw location expression. */
5798 while (loc != NULL)
5800 inchash::hash hstate;
5801 hashval_t hash;
5803 CHECKSUM_ULEB128 (loc->dtprel);
5804 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5805 hash_loc_operands (loc, hstate);
5806 hash = hstate.end ();
5807 CHECKSUM (hash);
5808 loc = loc->dw_loc_next;
5812 /* Calculate the checksum of an attribute. */
5814 static void
5815 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5816 struct md5_ctx *ctx, int *mark)
5818 dw_loc_descr_ref loc;
5819 rtx r;
5821 if (AT_class (at) == dw_val_class_die_ref)
5823 dw_die_ref target_die = AT_ref (at);
5825 /* For pointer and reference types, we checksum only the (qualified)
5826 name of the target type (if there is a name). For friend entries,
5827 we checksum only the (qualified) name of the target type or function.
5828 This allows the checksum to remain the same whether the target type
5829 is complete or not. */
5830 if ((at->dw_attr == DW_AT_type
5831 && (tag == DW_TAG_pointer_type
5832 || tag == DW_TAG_reference_type
5833 || tag == DW_TAG_rvalue_reference_type
5834 || tag == DW_TAG_ptr_to_member_type))
5835 || (at->dw_attr == DW_AT_friend
5836 && tag == DW_TAG_friend))
5838 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5840 if (name_attr != NULL)
5842 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5844 if (decl == NULL)
5845 decl = target_die;
5846 CHECKSUM_ULEB128 ('N');
5847 CHECKSUM_ULEB128 (at->dw_attr);
5848 if (decl->die_parent != NULL)
5849 checksum_die_context (decl->die_parent, ctx);
5850 CHECKSUM_ULEB128 ('E');
5851 CHECKSUM_STRING (AT_string (name_attr));
5852 return;
5856 /* For all other references to another DIE, we check to see if the
5857 target DIE has already been visited. If it has, we emit a
5858 backward reference; if not, we descend recursively. */
5859 if (target_die->die_mark > 0)
5861 CHECKSUM_ULEB128 ('R');
5862 CHECKSUM_ULEB128 (at->dw_attr);
5863 CHECKSUM_ULEB128 (target_die->die_mark);
5865 else
5867 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5869 if (decl == NULL)
5870 decl = target_die;
5871 target_die->die_mark = ++(*mark);
5872 CHECKSUM_ULEB128 ('T');
5873 CHECKSUM_ULEB128 (at->dw_attr);
5874 if (decl->die_parent != NULL)
5875 checksum_die_context (decl->die_parent, ctx);
5876 die_checksum_ordered (target_die, ctx, mark);
5878 return;
5881 CHECKSUM_ULEB128 ('A');
5882 CHECKSUM_ULEB128 (at->dw_attr);
5884 switch (AT_class (at))
5886 case dw_val_class_const:
5887 CHECKSUM_ULEB128 (DW_FORM_sdata);
5888 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5889 break;
5891 case dw_val_class_unsigned_const:
5892 CHECKSUM_ULEB128 (DW_FORM_sdata);
5893 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5894 break;
5896 case dw_val_class_const_double:
5897 CHECKSUM_ULEB128 (DW_FORM_block);
5898 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5899 CHECKSUM (at->dw_attr_val.v.val_double);
5900 break;
5902 case dw_val_class_wide_int:
5903 CHECKSUM_ULEB128 (DW_FORM_block);
5904 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5905 CHECKSUM (*at->dw_attr_val.v.val_wide);
5906 break;
5908 case dw_val_class_vec:
5909 CHECKSUM_ULEB128 (DW_FORM_block);
5910 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5911 * at->dw_attr_val.v.val_vec.elt_size);
5912 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5913 (at->dw_attr_val.v.val_vec.length
5914 * at->dw_attr_val.v.val_vec.elt_size));
5915 break;
5917 case dw_val_class_flag:
5918 CHECKSUM_ULEB128 (DW_FORM_flag);
5919 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5920 break;
5922 case dw_val_class_str:
5923 CHECKSUM_ULEB128 (DW_FORM_string);
5924 CHECKSUM_STRING (AT_string (at));
5925 break;
5927 case dw_val_class_addr:
5928 r = AT_addr (at);
5929 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5930 CHECKSUM_ULEB128 (DW_FORM_string);
5931 CHECKSUM_STRING (XSTR (r, 0));
5932 break;
5934 case dw_val_class_offset:
5935 CHECKSUM_ULEB128 (DW_FORM_sdata);
5936 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5937 break;
5939 case dw_val_class_loc:
5940 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5941 loc_checksum_ordered (loc, ctx);
5942 break;
5944 case dw_val_class_fde_ref:
5945 case dw_val_class_lbl_id:
5946 case dw_val_class_lineptr:
5947 case dw_val_class_macptr:
5948 case dw_val_class_high_pc:
5949 break;
5951 case dw_val_class_file:
5952 CHECKSUM_ULEB128 (DW_FORM_string);
5953 CHECKSUM_STRING (AT_file (at)->filename);
5954 break;
5956 case dw_val_class_data8:
5957 CHECKSUM (at->dw_attr_val.v.val_data8);
5958 break;
5960 default:
5961 break;
5965 struct checksum_attributes
5967 dw_attr_ref at_name;
5968 dw_attr_ref at_type;
5969 dw_attr_ref at_friend;
5970 dw_attr_ref at_accessibility;
5971 dw_attr_ref at_address_class;
5972 dw_attr_ref at_allocated;
5973 dw_attr_ref at_artificial;
5974 dw_attr_ref at_associated;
5975 dw_attr_ref at_binary_scale;
5976 dw_attr_ref at_bit_offset;
5977 dw_attr_ref at_bit_size;
5978 dw_attr_ref at_bit_stride;
5979 dw_attr_ref at_byte_size;
5980 dw_attr_ref at_byte_stride;
5981 dw_attr_ref at_const_value;
5982 dw_attr_ref at_containing_type;
5983 dw_attr_ref at_count;
5984 dw_attr_ref at_data_location;
5985 dw_attr_ref at_data_member_location;
5986 dw_attr_ref at_decimal_scale;
5987 dw_attr_ref at_decimal_sign;
5988 dw_attr_ref at_default_value;
5989 dw_attr_ref at_digit_count;
5990 dw_attr_ref at_discr;
5991 dw_attr_ref at_discr_list;
5992 dw_attr_ref at_discr_value;
5993 dw_attr_ref at_encoding;
5994 dw_attr_ref at_endianity;
5995 dw_attr_ref at_explicit;
5996 dw_attr_ref at_is_optional;
5997 dw_attr_ref at_location;
5998 dw_attr_ref at_lower_bound;
5999 dw_attr_ref at_mutable;
6000 dw_attr_ref at_ordering;
6001 dw_attr_ref at_picture_string;
6002 dw_attr_ref at_prototyped;
6003 dw_attr_ref at_small;
6004 dw_attr_ref at_segment;
6005 dw_attr_ref at_string_length;
6006 dw_attr_ref at_threads_scaled;
6007 dw_attr_ref at_upper_bound;
6008 dw_attr_ref at_use_location;
6009 dw_attr_ref at_use_UTF8;
6010 dw_attr_ref at_variable_parameter;
6011 dw_attr_ref at_virtuality;
6012 dw_attr_ref at_visibility;
6013 dw_attr_ref at_vtable_elem_location;
6016 /* Collect the attributes that we will want to use for the checksum. */
6018 static void
6019 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6021 dw_attr_ref a;
6022 unsigned ix;
6024 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6026 switch (a->dw_attr)
6028 case DW_AT_name:
6029 attrs->at_name = a;
6030 break;
6031 case DW_AT_type:
6032 attrs->at_type = a;
6033 break;
6034 case DW_AT_friend:
6035 attrs->at_friend = a;
6036 break;
6037 case DW_AT_accessibility:
6038 attrs->at_accessibility = a;
6039 break;
6040 case DW_AT_address_class:
6041 attrs->at_address_class = a;
6042 break;
6043 case DW_AT_allocated:
6044 attrs->at_allocated = a;
6045 break;
6046 case DW_AT_artificial:
6047 attrs->at_artificial = a;
6048 break;
6049 case DW_AT_associated:
6050 attrs->at_associated = a;
6051 break;
6052 case DW_AT_binary_scale:
6053 attrs->at_binary_scale = a;
6054 break;
6055 case DW_AT_bit_offset:
6056 attrs->at_bit_offset = a;
6057 break;
6058 case DW_AT_bit_size:
6059 attrs->at_bit_size = a;
6060 break;
6061 case DW_AT_bit_stride:
6062 attrs->at_bit_stride = a;
6063 break;
6064 case DW_AT_byte_size:
6065 attrs->at_byte_size = a;
6066 break;
6067 case DW_AT_byte_stride:
6068 attrs->at_byte_stride = a;
6069 break;
6070 case DW_AT_const_value:
6071 attrs->at_const_value = a;
6072 break;
6073 case DW_AT_containing_type:
6074 attrs->at_containing_type = a;
6075 break;
6076 case DW_AT_count:
6077 attrs->at_count = a;
6078 break;
6079 case DW_AT_data_location:
6080 attrs->at_data_location = a;
6081 break;
6082 case DW_AT_data_member_location:
6083 attrs->at_data_member_location = a;
6084 break;
6085 case DW_AT_decimal_scale:
6086 attrs->at_decimal_scale = a;
6087 break;
6088 case DW_AT_decimal_sign:
6089 attrs->at_decimal_sign = a;
6090 break;
6091 case DW_AT_default_value:
6092 attrs->at_default_value = a;
6093 break;
6094 case DW_AT_digit_count:
6095 attrs->at_digit_count = a;
6096 break;
6097 case DW_AT_discr:
6098 attrs->at_discr = a;
6099 break;
6100 case DW_AT_discr_list:
6101 attrs->at_discr_list = a;
6102 break;
6103 case DW_AT_discr_value:
6104 attrs->at_discr_value = a;
6105 break;
6106 case DW_AT_encoding:
6107 attrs->at_encoding = a;
6108 break;
6109 case DW_AT_endianity:
6110 attrs->at_endianity = a;
6111 break;
6112 case DW_AT_explicit:
6113 attrs->at_explicit = a;
6114 break;
6115 case DW_AT_is_optional:
6116 attrs->at_is_optional = a;
6117 break;
6118 case DW_AT_location:
6119 attrs->at_location = a;
6120 break;
6121 case DW_AT_lower_bound:
6122 attrs->at_lower_bound = a;
6123 break;
6124 case DW_AT_mutable:
6125 attrs->at_mutable = a;
6126 break;
6127 case DW_AT_ordering:
6128 attrs->at_ordering = a;
6129 break;
6130 case DW_AT_picture_string:
6131 attrs->at_picture_string = a;
6132 break;
6133 case DW_AT_prototyped:
6134 attrs->at_prototyped = a;
6135 break;
6136 case DW_AT_small:
6137 attrs->at_small = a;
6138 break;
6139 case DW_AT_segment:
6140 attrs->at_segment = a;
6141 break;
6142 case DW_AT_string_length:
6143 attrs->at_string_length = a;
6144 break;
6145 case DW_AT_threads_scaled:
6146 attrs->at_threads_scaled = a;
6147 break;
6148 case DW_AT_upper_bound:
6149 attrs->at_upper_bound = a;
6150 break;
6151 case DW_AT_use_location:
6152 attrs->at_use_location = a;
6153 break;
6154 case DW_AT_use_UTF8:
6155 attrs->at_use_UTF8 = a;
6156 break;
6157 case DW_AT_variable_parameter:
6158 attrs->at_variable_parameter = a;
6159 break;
6160 case DW_AT_virtuality:
6161 attrs->at_virtuality = a;
6162 break;
6163 case DW_AT_visibility:
6164 attrs->at_visibility = a;
6165 break;
6166 case DW_AT_vtable_elem_location:
6167 attrs->at_vtable_elem_location = a;
6168 break;
6169 default:
6170 break;
6175 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6177 static void
6178 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6180 dw_die_ref c;
6181 dw_die_ref decl;
6182 struct checksum_attributes attrs;
6184 CHECKSUM_ULEB128 ('D');
6185 CHECKSUM_ULEB128 (die->die_tag);
6187 memset (&attrs, 0, sizeof (attrs));
6189 decl = get_AT_ref (die, DW_AT_specification);
6190 if (decl != NULL)
6191 collect_checksum_attributes (&attrs, decl);
6192 collect_checksum_attributes (&attrs, die);
6194 CHECKSUM_ATTR (attrs.at_name);
6195 CHECKSUM_ATTR (attrs.at_accessibility);
6196 CHECKSUM_ATTR (attrs.at_address_class);
6197 CHECKSUM_ATTR (attrs.at_allocated);
6198 CHECKSUM_ATTR (attrs.at_artificial);
6199 CHECKSUM_ATTR (attrs.at_associated);
6200 CHECKSUM_ATTR (attrs.at_binary_scale);
6201 CHECKSUM_ATTR (attrs.at_bit_offset);
6202 CHECKSUM_ATTR (attrs.at_bit_size);
6203 CHECKSUM_ATTR (attrs.at_bit_stride);
6204 CHECKSUM_ATTR (attrs.at_byte_size);
6205 CHECKSUM_ATTR (attrs.at_byte_stride);
6206 CHECKSUM_ATTR (attrs.at_const_value);
6207 CHECKSUM_ATTR (attrs.at_containing_type);
6208 CHECKSUM_ATTR (attrs.at_count);
6209 CHECKSUM_ATTR (attrs.at_data_location);
6210 CHECKSUM_ATTR (attrs.at_data_member_location);
6211 CHECKSUM_ATTR (attrs.at_decimal_scale);
6212 CHECKSUM_ATTR (attrs.at_decimal_sign);
6213 CHECKSUM_ATTR (attrs.at_default_value);
6214 CHECKSUM_ATTR (attrs.at_digit_count);
6215 CHECKSUM_ATTR (attrs.at_discr);
6216 CHECKSUM_ATTR (attrs.at_discr_list);
6217 CHECKSUM_ATTR (attrs.at_discr_value);
6218 CHECKSUM_ATTR (attrs.at_encoding);
6219 CHECKSUM_ATTR (attrs.at_endianity);
6220 CHECKSUM_ATTR (attrs.at_explicit);
6221 CHECKSUM_ATTR (attrs.at_is_optional);
6222 CHECKSUM_ATTR (attrs.at_location);
6223 CHECKSUM_ATTR (attrs.at_lower_bound);
6224 CHECKSUM_ATTR (attrs.at_mutable);
6225 CHECKSUM_ATTR (attrs.at_ordering);
6226 CHECKSUM_ATTR (attrs.at_picture_string);
6227 CHECKSUM_ATTR (attrs.at_prototyped);
6228 CHECKSUM_ATTR (attrs.at_small);
6229 CHECKSUM_ATTR (attrs.at_segment);
6230 CHECKSUM_ATTR (attrs.at_string_length);
6231 CHECKSUM_ATTR (attrs.at_threads_scaled);
6232 CHECKSUM_ATTR (attrs.at_upper_bound);
6233 CHECKSUM_ATTR (attrs.at_use_location);
6234 CHECKSUM_ATTR (attrs.at_use_UTF8);
6235 CHECKSUM_ATTR (attrs.at_variable_parameter);
6236 CHECKSUM_ATTR (attrs.at_virtuality);
6237 CHECKSUM_ATTR (attrs.at_visibility);
6238 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6239 CHECKSUM_ATTR (attrs.at_type);
6240 CHECKSUM_ATTR (attrs.at_friend);
6242 /* Checksum the child DIEs. */
6243 c = die->die_child;
6244 if (c) do {
6245 dw_attr_ref name_attr;
6247 c = c->die_sib;
6248 name_attr = get_AT (c, DW_AT_name);
6249 if (is_template_instantiation (c))
6251 /* Ignore instantiations of member type and function templates. */
6253 else if (name_attr != NULL
6254 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6256 /* Use a shallow checksum for named nested types and member
6257 functions. */
6258 CHECKSUM_ULEB128 ('S');
6259 CHECKSUM_ULEB128 (c->die_tag);
6260 CHECKSUM_STRING (AT_string (name_attr));
6262 else
6264 /* Use a deep checksum for other children. */
6265 /* Mark this DIE so it gets processed when unmarking. */
6266 if (c->die_mark == 0)
6267 c->die_mark = -1;
6268 die_checksum_ordered (c, ctx, mark);
6270 } while (c != die->die_child);
6272 CHECKSUM_ULEB128 (0);
6275 /* Add a type name and tag to a hash. */
6276 static void
6277 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6279 CHECKSUM_ULEB128 (tag);
6280 CHECKSUM_STRING (name);
6283 #undef CHECKSUM
6284 #undef CHECKSUM_STRING
6285 #undef CHECKSUM_ATTR
6286 #undef CHECKSUM_LEB128
6287 #undef CHECKSUM_ULEB128
6289 /* Generate the type signature for DIE. This is computed by generating an
6290 MD5 checksum over the DIE's tag, its relevant attributes, and its
6291 children. Attributes that are references to other DIEs are processed
6292 by recursion, using the MARK field to prevent infinite recursion.
6293 If the DIE is nested inside a namespace or another type, we also
6294 need to include that context in the signature. The lower 64 bits
6295 of the resulting MD5 checksum comprise the signature. */
6297 static void
6298 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6300 int mark;
6301 const char *name;
6302 unsigned char checksum[16];
6303 struct md5_ctx ctx;
6304 dw_die_ref decl;
6305 dw_die_ref parent;
6307 name = get_AT_string (die, DW_AT_name);
6308 decl = get_AT_ref (die, DW_AT_specification);
6309 parent = get_die_parent (die);
6311 /* First, compute a signature for just the type name (and its surrounding
6312 context, if any. This is stored in the type unit DIE for link-time
6313 ODR (one-definition rule) checking. */
6315 if (is_cxx () && name != NULL)
6317 md5_init_ctx (&ctx);
6319 /* Checksum the names of surrounding namespaces and structures. */
6320 if (parent != NULL)
6321 checksum_die_context (parent, &ctx);
6323 /* Checksum the current DIE. */
6324 die_odr_checksum (die->die_tag, name, &ctx);
6325 md5_finish_ctx (&ctx, checksum);
6327 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6330 /* Next, compute the complete type signature. */
6332 md5_init_ctx (&ctx);
6333 mark = 1;
6334 die->die_mark = mark;
6336 /* Checksum the names of surrounding namespaces and structures. */
6337 if (parent != NULL)
6338 checksum_die_context (parent, &ctx);
6340 /* Checksum the DIE and its children. */
6341 die_checksum_ordered (die, &ctx, &mark);
6342 unmark_all_dies (die);
6343 md5_finish_ctx (&ctx, checksum);
6345 /* Store the signature in the type node and link the type DIE and the
6346 type node together. */
6347 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6348 DWARF_TYPE_SIGNATURE_SIZE);
6349 die->comdat_type_p = true;
6350 die->die_id.die_type_node = type_node;
6351 type_node->type_die = die;
6353 /* If the DIE is a specification, link its declaration to the type node
6354 as well. */
6355 if (decl != NULL)
6357 decl->comdat_type_p = true;
6358 decl->die_id.die_type_node = type_node;
6362 /* Do the location expressions look same? */
6363 static inline int
6364 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6366 return loc1->dw_loc_opc == loc2->dw_loc_opc
6367 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6368 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6371 /* Do the values look the same? */
6372 static int
6373 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6375 dw_loc_descr_ref loc1, loc2;
6376 rtx r1, r2;
6378 if (v1->val_class != v2->val_class)
6379 return 0;
6381 switch (v1->val_class)
6383 case dw_val_class_const:
6384 return v1->v.val_int == v2->v.val_int;
6385 case dw_val_class_unsigned_const:
6386 return v1->v.val_unsigned == v2->v.val_unsigned;
6387 case dw_val_class_const_double:
6388 return v1->v.val_double.high == v2->v.val_double.high
6389 && v1->v.val_double.low == v2->v.val_double.low;
6390 case dw_val_class_wide_int:
6391 return *v1->v.val_wide == *v2->v.val_wide;
6392 case dw_val_class_vec:
6393 if (v1->v.val_vec.length != v2->v.val_vec.length
6394 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6395 return 0;
6396 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6397 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6398 return 0;
6399 return 1;
6400 case dw_val_class_flag:
6401 return v1->v.val_flag == v2->v.val_flag;
6402 case dw_val_class_str:
6403 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6405 case dw_val_class_addr:
6406 r1 = v1->v.val_addr;
6407 r2 = v2->v.val_addr;
6408 if (GET_CODE (r1) != GET_CODE (r2))
6409 return 0;
6410 return !rtx_equal_p (r1, r2);
6412 case dw_val_class_offset:
6413 return v1->v.val_offset == v2->v.val_offset;
6415 case dw_val_class_loc:
6416 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6417 loc1 && loc2;
6418 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6419 if (!same_loc_p (loc1, loc2, mark))
6420 return 0;
6421 return !loc1 && !loc2;
6423 case dw_val_class_die_ref:
6424 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6426 case dw_val_class_fde_ref:
6427 case dw_val_class_vms_delta:
6428 case dw_val_class_lbl_id:
6429 case dw_val_class_lineptr:
6430 case dw_val_class_macptr:
6431 case dw_val_class_high_pc:
6432 return 1;
6434 case dw_val_class_file:
6435 return v1->v.val_file == v2->v.val_file;
6437 case dw_val_class_data8:
6438 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6440 default:
6441 return 1;
6445 /* Do the attributes look the same? */
6447 static int
6448 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6450 if (at1->dw_attr != at2->dw_attr)
6451 return 0;
6453 /* We don't care that this was compiled with a different compiler
6454 snapshot; if the output is the same, that's what matters. */
6455 if (at1->dw_attr == DW_AT_producer)
6456 return 1;
6458 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6461 /* Do the dies look the same? */
6463 static int
6464 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6466 dw_die_ref c1, c2;
6467 dw_attr_ref a1;
6468 unsigned ix;
6470 /* To avoid infinite recursion. */
6471 if (die1->die_mark)
6472 return die1->die_mark == die2->die_mark;
6473 die1->die_mark = die2->die_mark = ++(*mark);
6475 if (die1->die_tag != die2->die_tag)
6476 return 0;
6478 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6479 return 0;
6481 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6482 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6483 return 0;
6485 c1 = die1->die_child;
6486 c2 = die2->die_child;
6487 if (! c1)
6489 if (c2)
6490 return 0;
6492 else
6493 for (;;)
6495 if (!same_die_p (c1, c2, mark))
6496 return 0;
6497 c1 = c1->die_sib;
6498 c2 = c2->die_sib;
6499 if (c1 == die1->die_child)
6501 if (c2 == die2->die_child)
6502 break;
6503 else
6504 return 0;
6508 return 1;
6511 /* Do the dies look the same? Wrapper around same_die_p. */
6513 static int
6514 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6516 int mark = 0;
6517 int ret = same_die_p (die1, die2, &mark);
6519 unmark_all_dies (die1);
6520 unmark_all_dies (die2);
6522 return ret;
6525 /* The prefix to attach to symbols on DIEs in the current comdat debug
6526 info section. */
6527 static const char *comdat_symbol_id;
6529 /* The index of the current symbol within the current comdat CU. */
6530 static unsigned int comdat_symbol_number;
6532 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6533 children, and set comdat_symbol_id accordingly. */
6535 static void
6536 compute_section_prefix (dw_die_ref unit_die)
6538 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6539 const char *base = die_name ? lbasename (die_name) : "anonymous";
6540 char *name = XALLOCAVEC (char, strlen (base) + 64);
6541 char *p;
6542 int i, mark;
6543 unsigned char checksum[16];
6544 struct md5_ctx ctx;
6546 /* Compute the checksum of the DIE, then append part of it as hex digits to
6547 the name filename of the unit. */
6549 md5_init_ctx (&ctx);
6550 mark = 0;
6551 die_checksum (unit_die, &ctx, &mark);
6552 unmark_all_dies (unit_die);
6553 md5_finish_ctx (&ctx, checksum);
6555 sprintf (name, "%s.", base);
6556 clean_symbol_name (name);
6558 p = name + strlen (name);
6559 for (i = 0; i < 4; i++)
6561 sprintf (p, "%.2x", checksum[i]);
6562 p += 2;
6565 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6566 comdat_symbol_number = 0;
6569 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6571 static int
6572 is_type_die (dw_die_ref die)
6574 switch (die->die_tag)
6576 case DW_TAG_array_type:
6577 case DW_TAG_class_type:
6578 case DW_TAG_interface_type:
6579 case DW_TAG_enumeration_type:
6580 case DW_TAG_pointer_type:
6581 case DW_TAG_reference_type:
6582 case DW_TAG_rvalue_reference_type:
6583 case DW_TAG_string_type:
6584 case DW_TAG_structure_type:
6585 case DW_TAG_subroutine_type:
6586 case DW_TAG_union_type:
6587 case DW_TAG_ptr_to_member_type:
6588 case DW_TAG_set_type:
6589 case DW_TAG_subrange_type:
6590 case DW_TAG_base_type:
6591 case DW_TAG_const_type:
6592 case DW_TAG_file_type:
6593 case DW_TAG_packed_type:
6594 case DW_TAG_volatile_type:
6595 case DW_TAG_typedef:
6596 return 1;
6597 default:
6598 return 0;
6602 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6603 Basically, we want to choose the bits that are likely to be shared between
6604 compilations (types) and leave out the bits that are specific to individual
6605 compilations (functions). */
6607 static int
6608 is_comdat_die (dw_die_ref c)
6610 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6611 we do for stabs. The advantage is a greater likelihood of sharing between
6612 objects that don't include headers in the same order (and therefore would
6613 put the base types in a different comdat). jason 8/28/00 */
6615 if (c->die_tag == DW_TAG_base_type)
6616 return 0;
6618 if (c->die_tag == DW_TAG_pointer_type
6619 || c->die_tag == DW_TAG_reference_type
6620 || c->die_tag == DW_TAG_rvalue_reference_type
6621 || c->die_tag == DW_TAG_const_type
6622 || c->die_tag == DW_TAG_volatile_type)
6624 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6626 return t ? is_comdat_die (t) : 0;
6629 return is_type_die (c);
6632 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6633 compilation unit. */
6635 static int
6636 is_symbol_die (dw_die_ref c)
6638 return (is_type_die (c)
6639 || is_declaration_die (c)
6640 || c->die_tag == DW_TAG_namespace
6641 || c->die_tag == DW_TAG_module);
6644 /* Returns true iff C is a compile-unit DIE. */
6646 static inline bool
6647 is_cu_die (dw_die_ref c)
6649 return c && c->die_tag == DW_TAG_compile_unit;
6652 /* Returns true iff C is a unit DIE of some sort. */
6654 static inline bool
6655 is_unit_die (dw_die_ref c)
6657 return c && (c->die_tag == DW_TAG_compile_unit
6658 || c->die_tag == DW_TAG_partial_unit
6659 || c->die_tag == DW_TAG_type_unit);
6662 /* Returns true iff C is a namespace DIE. */
6664 static inline bool
6665 is_namespace_die (dw_die_ref c)
6667 return c && c->die_tag == DW_TAG_namespace;
6670 /* Returns true iff C is a class or structure DIE. */
6672 static inline bool
6673 is_class_die (dw_die_ref c)
6675 return c && (c->die_tag == DW_TAG_class_type
6676 || c->die_tag == DW_TAG_structure_type);
6679 /* Return non-zero if this DIE is a template parameter. */
6681 static inline bool
6682 is_template_parameter (dw_die_ref die)
6684 switch (die->die_tag)
6686 case DW_TAG_template_type_param:
6687 case DW_TAG_template_value_param:
6688 case DW_TAG_GNU_template_template_param:
6689 case DW_TAG_GNU_template_parameter_pack:
6690 return true;
6691 default:
6692 return false;
6696 /* Return non-zero if this DIE represents a template instantiation. */
6698 static inline bool
6699 is_template_instantiation (dw_die_ref die)
6701 dw_die_ref c;
6703 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6704 return false;
6705 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6706 return false;
6709 static char *
6710 gen_internal_sym (const char *prefix)
6712 char buf[256];
6714 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6715 return xstrdup (buf);
6718 /* Assign symbols to all worthy DIEs under DIE. */
6720 static void
6721 assign_symbol_names (dw_die_ref die)
6723 dw_die_ref c;
6725 if (is_symbol_die (die) && !die->comdat_type_p)
6727 if (comdat_symbol_id)
6729 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6731 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6732 comdat_symbol_id, comdat_symbol_number++);
6733 die->die_id.die_symbol = xstrdup (p);
6735 else
6736 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6739 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6742 struct cu_hash_table_entry
6744 dw_die_ref cu;
6745 unsigned min_comdat_num, max_comdat_num;
6746 struct cu_hash_table_entry *next;
6749 /* Helpers to manipulate hash table of CUs. */
6751 struct cu_hash_table_entry_hasher
6753 typedef cu_hash_table_entry value_type;
6754 typedef die_struct compare_type;
6755 static inline hashval_t hash (const value_type *);
6756 static inline bool equal (const value_type *, const compare_type *);
6757 static inline void remove (value_type *);
6760 inline hashval_t
6761 cu_hash_table_entry_hasher::hash (const value_type *entry)
6763 return htab_hash_string (entry->cu->die_id.die_symbol);
6766 inline bool
6767 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6768 const compare_type *entry2)
6770 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6773 inline void
6774 cu_hash_table_entry_hasher::remove (value_type *entry)
6776 struct cu_hash_table_entry *next;
6778 while (entry)
6780 next = entry->next;
6781 free (entry);
6782 entry = next;
6786 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6788 /* Check whether we have already seen this CU and set up SYM_NUM
6789 accordingly. */
6790 static int
6791 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6793 struct cu_hash_table_entry dummy;
6794 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6796 dummy.max_comdat_num = 0;
6798 slot = htable->find_slot_with_hash (cu,
6799 htab_hash_string (cu->die_id.die_symbol),
6800 INSERT);
6801 entry = *slot;
6803 for (; entry; last = entry, entry = entry->next)
6805 if (same_die_p_wrap (cu, entry->cu))
6806 break;
6809 if (entry)
6811 *sym_num = entry->min_comdat_num;
6812 return 1;
6815 entry = XCNEW (struct cu_hash_table_entry);
6816 entry->cu = cu;
6817 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6818 entry->next = *slot;
6819 *slot = entry;
6821 return 0;
6824 /* Record SYM_NUM to record of CU in HTABLE. */
6825 static void
6826 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6827 unsigned int sym_num)
6829 struct cu_hash_table_entry **slot, *entry;
6831 slot = htable->find_slot_with_hash (cu,
6832 htab_hash_string (cu->die_id.die_symbol),
6833 NO_INSERT);
6834 entry = *slot;
6836 entry->max_comdat_num = sym_num;
6839 /* Traverse the DIE (which is always comp_unit_die), and set up
6840 additional compilation units for each of the include files we see
6841 bracketed by BINCL/EINCL. */
6843 static void
6844 break_out_includes (dw_die_ref die)
6846 dw_die_ref c;
6847 dw_die_ref unit = NULL;
6848 limbo_die_node *node, **pnode;
6850 c = die->die_child;
6851 if (c) do {
6852 dw_die_ref prev = c;
6853 c = c->die_sib;
6854 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6855 || (unit && is_comdat_die (c)))
6857 dw_die_ref next = c->die_sib;
6859 /* This DIE is for a secondary CU; remove it from the main one. */
6860 remove_child_with_prev (c, prev);
6862 if (c->die_tag == DW_TAG_GNU_BINCL)
6863 unit = push_new_compile_unit (unit, c);
6864 else if (c->die_tag == DW_TAG_GNU_EINCL)
6865 unit = pop_compile_unit (unit);
6866 else
6867 add_child_die (unit, c);
6868 c = next;
6869 if (c == die->die_child)
6870 break;
6872 } while (c != die->die_child);
6874 #if 0
6875 /* We can only use this in debugging, since the frontend doesn't check
6876 to make sure that we leave every include file we enter. */
6877 gcc_assert (!unit);
6878 #endif
6880 assign_symbol_names (die);
6881 cu_hash_type cu_hash_table (10);
6882 for (node = limbo_die_list, pnode = &limbo_die_list;
6883 node;
6884 node = node->next)
6886 int is_dupl;
6888 compute_section_prefix (node->die);
6889 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6890 &comdat_symbol_number);
6891 assign_symbol_names (node->die);
6892 if (is_dupl)
6893 *pnode = node->next;
6894 else
6896 pnode = &node->next;
6897 record_comdat_symbol_number (node->die, &cu_hash_table,
6898 comdat_symbol_number);
6903 /* Return non-zero if this DIE is a declaration. */
6905 static int
6906 is_declaration_die (dw_die_ref die)
6908 dw_attr_ref a;
6909 unsigned ix;
6911 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6912 if (a->dw_attr == DW_AT_declaration)
6913 return 1;
6915 return 0;
6918 /* Return non-zero if this DIE is nested inside a subprogram. */
6920 static int
6921 is_nested_in_subprogram (dw_die_ref die)
6923 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6925 if (decl == NULL)
6926 decl = die;
6927 return local_scope_p (decl);
6930 /* Return non-zero if this DIE contains a defining declaration of a
6931 subprogram. */
6933 static int
6934 contains_subprogram_definition (dw_die_ref die)
6936 dw_die_ref c;
6938 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6939 return 1;
6940 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6941 return 0;
6944 /* Return non-zero if this is a type DIE that should be moved to a
6945 COMDAT .debug_types section. */
6947 static int
6948 should_move_die_to_comdat (dw_die_ref die)
6950 switch (die->die_tag)
6952 case DW_TAG_class_type:
6953 case DW_TAG_structure_type:
6954 case DW_TAG_enumeration_type:
6955 case DW_TAG_union_type:
6956 /* Don't move declarations, inlined instances, types nested in a
6957 subprogram, or types that contain subprogram definitions. */
6958 if (is_declaration_die (die)
6959 || get_AT (die, DW_AT_abstract_origin)
6960 || is_nested_in_subprogram (die)
6961 || contains_subprogram_definition (die))
6962 return 0;
6963 return 1;
6964 case DW_TAG_array_type:
6965 case DW_TAG_interface_type:
6966 case DW_TAG_pointer_type:
6967 case DW_TAG_reference_type:
6968 case DW_TAG_rvalue_reference_type:
6969 case DW_TAG_string_type:
6970 case DW_TAG_subroutine_type:
6971 case DW_TAG_ptr_to_member_type:
6972 case DW_TAG_set_type:
6973 case DW_TAG_subrange_type:
6974 case DW_TAG_base_type:
6975 case DW_TAG_const_type:
6976 case DW_TAG_file_type:
6977 case DW_TAG_packed_type:
6978 case DW_TAG_volatile_type:
6979 case DW_TAG_typedef:
6980 default:
6981 return 0;
6985 /* Make a clone of DIE. */
6987 static dw_die_ref
6988 clone_die (dw_die_ref die)
6990 dw_die_ref clone;
6991 dw_attr_ref a;
6992 unsigned ix;
6994 clone = ggc_cleared_alloc<die_node> ();
6995 clone->die_tag = die->die_tag;
6997 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6998 add_dwarf_attr (clone, a);
7000 return clone;
7003 /* Make a clone of the tree rooted at DIE. */
7005 static dw_die_ref
7006 clone_tree (dw_die_ref die)
7008 dw_die_ref c;
7009 dw_die_ref clone = clone_die (die);
7011 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7013 return clone;
7016 /* Make a clone of DIE as a declaration. */
7018 static dw_die_ref
7019 clone_as_declaration (dw_die_ref die)
7021 dw_die_ref clone;
7022 dw_die_ref decl;
7023 dw_attr_ref a;
7024 unsigned ix;
7026 /* If the DIE is already a declaration, just clone it. */
7027 if (is_declaration_die (die))
7028 return clone_die (die);
7030 /* If the DIE is a specification, just clone its declaration DIE. */
7031 decl = get_AT_ref (die, DW_AT_specification);
7032 if (decl != NULL)
7034 clone = clone_die (decl);
7035 if (die->comdat_type_p)
7036 add_AT_die_ref (clone, DW_AT_signature, die);
7037 return clone;
7040 clone = ggc_cleared_alloc<die_node> ();
7041 clone->die_tag = die->die_tag;
7043 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7045 /* We don't want to copy over all attributes.
7046 For example we don't want DW_AT_byte_size because otherwise we will no
7047 longer have a declaration and GDB will treat it as a definition. */
7049 switch (a->dw_attr)
7051 case DW_AT_abstract_origin:
7052 case DW_AT_artificial:
7053 case DW_AT_containing_type:
7054 case DW_AT_external:
7055 case DW_AT_name:
7056 case DW_AT_type:
7057 case DW_AT_virtuality:
7058 case DW_AT_linkage_name:
7059 case DW_AT_MIPS_linkage_name:
7060 add_dwarf_attr (clone, a);
7061 break;
7062 case DW_AT_byte_size:
7063 default:
7064 break;
7068 if (die->comdat_type_p)
7069 add_AT_die_ref (clone, DW_AT_signature, die);
7071 add_AT_flag (clone, DW_AT_declaration, 1);
7072 return clone;
7076 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7078 struct decl_table_entry
7080 dw_die_ref orig;
7081 dw_die_ref copy;
7084 /* Helpers to manipulate hash table of copied declarations. */
7086 /* Hashtable helpers. */
7088 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7090 typedef decl_table_entry value_type;
7091 typedef die_struct compare_type;
7092 static inline hashval_t hash (const value_type *);
7093 static inline bool equal (const value_type *, const compare_type *);
7096 inline hashval_t
7097 decl_table_entry_hasher::hash (const value_type *entry)
7099 return htab_hash_pointer (entry->orig);
7102 inline bool
7103 decl_table_entry_hasher::equal (const value_type *entry1,
7104 const compare_type *entry2)
7106 return entry1->orig == entry2;
7109 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7111 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7112 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7113 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7114 to check if the ancestor has already been copied into UNIT. */
7116 static dw_die_ref
7117 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7118 decl_hash_type *decl_table)
7120 dw_die_ref parent = die->die_parent;
7121 dw_die_ref new_parent = unit;
7122 dw_die_ref copy;
7123 decl_table_entry **slot = NULL;
7124 struct decl_table_entry *entry = NULL;
7126 if (decl_table)
7128 /* Check if the entry has already been copied to UNIT. */
7129 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7130 INSERT);
7131 if (*slot != HTAB_EMPTY_ENTRY)
7133 entry = *slot;
7134 return entry->copy;
7137 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7138 entry = XCNEW (struct decl_table_entry);
7139 entry->orig = die;
7140 entry->copy = NULL;
7141 *slot = entry;
7144 if (parent != NULL)
7146 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7147 if (spec != NULL)
7148 parent = spec;
7149 if (!is_unit_die (parent))
7150 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7153 copy = clone_as_declaration (die);
7154 add_child_die (new_parent, copy);
7156 if (decl_table)
7158 /* Record the pointer to the copy. */
7159 entry->copy = copy;
7162 return copy;
7164 /* Copy the declaration context to the new type unit DIE. This includes
7165 any surrounding namespace or type declarations. If the DIE has an
7166 AT_specification attribute, it also includes attributes and children
7167 attached to the specification, and returns a pointer to the original
7168 parent of the declaration DIE. Returns NULL otherwise. */
7170 static dw_die_ref
7171 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7173 dw_die_ref decl;
7174 dw_die_ref new_decl;
7175 dw_die_ref orig_parent = NULL;
7177 decl = get_AT_ref (die, DW_AT_specification);
7178 if (decl == NULL)
7179 decl = die;
7180 else
7182 unsigned ix;
7183 dw_die_ref c;
7184 dw_attr_ref a;
7186 /* The original DIE will be changed to a declaration, and must
7187 be moved to be a child of the original declaration DIE. */
7188 orig_parent = decl->die_parent;
7190 /* Copy the type node pointer from the new DIE to the original
7191 declaration DIE so we can forward references later. */
7192 decl->comdat_type_p = true;
7193 decl->die_id.die_type_node = die->die_id.die_type_node;
7195 remove_AT (die, DW_AT_specification);
7197 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7199 if (a->dw_attr != DW_AT_name
7200 && a->dw_attr != DW_AT_declaration
7201 && a->dw_attr != DW_AT_external)
7202 add_dwarf_attr (die, a);
7205 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7208 if (decl->die_parent != NULL
7209 && !is_unit_die (decl->die_parent))
7211 new_decl = copy_ancestor_tree (unit, decl, NULL);
7212 if (new_decl != NULL)
7214 remove_AT (new_decl, DW_AT_signature);
7215 add_AT_specification (die, new_decl);
7219 return orig_parent;
7222 /* Generate the skeleton ancestor tree for the given NODE, then clone
7223 the DIE and add the clone into the tree. */
7225 static void
7226 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7228 if (node->new_die != NULL)
7229 return;
7231 node->new_die = clone_as_declaration (node->old_die);
7233 if (node->parent != NULL)
7235 generate_skeleton_ancestor_tree (node->parent);
7236 add_child_die (node->parent->new_die, node->new_die);
7240 /* Generate a skeleton tree of DIEs containing any declarations that are
7241 found in the original tree. We traverse the tree looking for declaration
7242 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7244 static void
7245 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7247 skeleton_chain_node node;
7248 dw_die_ref c;
7249 dw_die_ref first;
7250 dw_die_ref prev = NULL;
7251 dw_die_ref next = NULL;
7253 node.parent = parent;
7255 first = c = parent->old_die->die_child;
7256 if (c)
7257 next = c->die_sib;
7258 if (c) do {
7259 if (prev == NULL || prev->die_sib == c)
7260 prev = c;
7261 c = next;
7262 next = (c == first ? NULL : c->die_sib);
7263 node.old_die = c;
7264 node.new_die = NULL;
7265 if (is_declaration_die (c))
7267 if (is_template_instantiation (c))
7269 /* Instantiated templates do not need to be cloned into the
7270 type unit. Just move the DIE and its children back to
7271 the skeleton tree (in the main CU). */
7272 remove_child_with_prev (c, prev);
7273 add_child_die (parent->new_die, c);
7274 c = prev;
7276 else
7278 /* Clone the existing DIE, move the original to the skeleton
7279 tree (which is in the main CU), and put the clone, with
7280 all the original's children, where the original came from
7281 (which is about to be moved to the type unit). */
7282 dw_die_ref clone = clone_die (c);
7283 move_all_children (c, clone);
7285 /* If the original has a DW_AT_object_pointer attribute,
7286 it would now point to a child DIE just moved to the
7287 cloned tree, so we need to remove that attribute from
7288 the original. */
7289 remove_AT (c, DW_AT_object_pointer);
7291 replace_child (c, clone, prev);
7292 generate_skeleton_ancestor_tree (parent);
7293 add_child_die (parent->new_die, c);
7294 node.new_die = c;
7295 c = clone;
7298 generate_skeleton_bottom_up (&node);
7299 } while (next != NULL);
7302 /* Wrapper function for generate_skeleton_bottom_up. */
7304 static dw_die_ref
7305 generate_skeleton (dw_die_ref die)
7307 skeleton_chain_node node;
7309 node.old_die = die;
7310 node.new_die = NULL;
7311 node.parent = NULL;
7313 /* If this type definition is nested inside another type,
7314 and is not an instantiation of a template, always leave
7315 at least a declaration in its place. */
7316 if (die->die_parent != NULL
7317 && is_type_die (die->die_parent)
7318 && !is_template_instantiation (die))
7319 node.new_die = clone_as_declaration (die);
7321 generate_skeleton_bottom_up (&node);
7322 return node.new_die;
7325 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7326 declaration. The original DIE is moved to a new compile unit so that
7327 existing references to it follow it to the new location. If any of the
7328 original DIE's descendants is a declaration, we need to replace the
7329 original DIE with a skeleton tree and move the declarations back into the
7330 skeleton tree. */
7332 static dw_die_ref
7333 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7334 dw_die_ref prev)
7336 dw_die_ref skeleton, orig_parent;
7338 /* Copy the declaration context to the type unit DIE. If the returned
7339 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7340 that DIE. */
7341 orig_parent = copy_declaration_context (unit, child);
7343 skeleton = generate_skeleton (child);
7344 if (skeleton == NULL)
7345 remove_child_with_prev (child, prev);
7346 else
7348 skeleton->comdat_type_p = true;
7349 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7351 /* If the original DIE was a specification, we need to put
7352 the skeleton under the parent DIE of the declaration.
7353 This leaves the original declaration in the tree, but
7354 it will be pruned later since there are no longer any
7355 references to it. */
7356 if (orig_parent != NULL)
7358 remove_child_with_prev (child, prev);
7359 add_child_die (orig_parent, skeleton);
7361 else
7362 replace_child (child, skeleton, prev);
7365 return skeleton;
7368 /* Traverse the DIE and set up additional .debug_types sections for each
7369 type worthy of being placed in a COMDAT section. */
7371 static void
7372 break_out_comdat_types (dw_die_ref die)
7374 dw_die_ref c;
7375 dw_die_ref first;
7376 dw_die_ref prev = NULL;
7377 dw_die_ref next = NULL;
7378 dw_die_ref unit = NULL;
7380 first = c = die->die_child;
7381 if (c)
7382 next = c->die_sib;
7383 if (c) do {
7384 if (prev == NULL || prev->die_sib == c)
7385 prev = c;
7386 c = next;
7387 next = (c == first ? NULL : c->die_sib);
7388 if (should_move_die_to_comdat (c))
7390 dw_die_ref replacement;
7391 comdat_type_node_ref type_node;
7393 /* Break out nested types into their own type units. */
7394 break_out_comdat_types (c);
7396 /* Create a new type unit DIE as the root for the new tree, and
7397 add it to the list of comdat types. */
7398 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7399 add_AT_unsigned (unit, DW_AT_language,
7400 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7401 type_node = ggc_cleared_alloc<comdat_type_node> ();
7402 type_node->root_die = unit;
7403 type_node->next = comdat_type_list;
7404 comdat_type_list = type_node;
7406 /* Generate the type signature. */
7407 generate_type_signature (c, type_node);
7409 /* Copy the declaration context, attributes, and children of the
7410 declaration into the new type unit DIE, then remove this DIE
7411 from the main CU (or replace it with a skeleton if necessary). */
7412 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7413 type_node->skeleton_die = replacement;
7415 /* Add the DIE to the new compunit. */
7416 add_child_die (unit, c);
7418 if (replacement != NULL)
7419 c = replacement;
7421 else if (c->die_tag == DW_TAG_namespace
7422 || c->die_tag == DW_TAG_class_type
7423 || c->die_tag == DW_TAG_structure_type
7424 || c->die_tag == DW_TAG_union_type)
7426 /* Look for nested types that can be broken out. */
7427 break_out_comdat_types (c);
7429 } while (next != NULL);
7432 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7433 Enter all the cloned children into the hash table decl_table. */
7435 static dw_die_ref
7436 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7438 dw_die_ref c;
7439 dw_die_ref clone;
7440 struct decl_table_entry *entry;
7441 decl_table_entry **slot;
7443 if (die->die_tag == DW_TAG_subprogram)
7444 clone = clone_as_declaration (die);
7445 else
7446 clone = clone_die (die);
7448 slot = decl_table->find_slot_with_hash (die,
7449 htab_hash_pointer (die), INSERT);
7451 /* Assert that DIE isn't in the hash table yet. If it would be there
7452 before, the ancestors would be necessarily there as well, therefore
7453 clone_tree_partial wouldn't be called. */
7454 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7456 entry = XCNEW (struct decl_table_entry);
7457 entry->orig = die;
7458 entry->copy = clone;
7459 *slot = entry;
7461 if (die->die_tag != DW_TAG_subprogram)
7462 FOR_EACH_CHILD (die, c,
7463 add_child_die (clone, clone_tree_partial (c, decl_table)));
7465 return clone;
7468 /* Walk the DIE and its children, looking for references to incomplete
7469 or trivial types that are unmarked (i.e., that are not in the current
7470 type_unit). */
7472 static void
7473 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7475 dw_die_ref c;
7476 dw_attr_ref a;
7477 unsigned ix;
7479 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7481 if (AT_class (a) == dw_val_class_die_ref)
7483 dw_die_ref targ = AT_ref (a);
7484 decl_table_entry **slot;
7485 struct decl_table_entry *entry;
7487 if (targ->die_mark != 0 || targ->comdat_type_p)
7488 continue;
7490 slot = decl_table->find_slot_with_hash (targ,
7491 htab_hash_pointer (targ),
7492 INSERT);
7494 if (*slot != HTAB_EMPTY_ENTRY)
7496 /* TARG has already been copied, so we just need to
7497 modify the reference to point to the copy. */
7498 entry = *slot;
7499 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7501 else
7503 dw_die_ref parent = unit;
7504 dw_die_ref copy = clone_die (targ);
7506 /* Record in DECL_TABLE that TARG has been copied.
7507 Need to do this now, before the recursive call,
7508 because DECL_TABLE may be expanded and SLOT
7509 would no longer be a valid pointer. */
7510 entry = XCNEW (struct decl_table_entry);
7511 entry->orig = targ;
7512 entry->copy = copy;
7513 *slot = entry;
7515 /* If TARG is not a declaration DIE, we need to copy its
7516 children. */
7517 if (!is_declaration_die (targ))
7519 FOR_EACH_CHILD (
7520 targ, c,
7521 add_child_die (copy,
7522 clone_tree_partial (c, decl_table)));
7525 /* Make sure the cloned tree is marked as part of the
7526 type unit. */
7527 mark_dies (copy);
7529 /* If TARG has surrounding context, copy its ancestor tree
7530 into the new type unit. */
7531 if (targ->die_parent != NULL
7532 && !is_unit_die (targ->die_parent))
7533 parent = copy_ancestor_tree (unit, targ->die_parent,
7534 decl_table);
7536 add_child_die (parent, copy);
7537 a->dw_attr_val.v.val_die_ref.die = copy;
7539 /* Make sure the newly-copied DIE is walked. If it was
7540 installed in a previously-added context, it won't
7541 get visited otherwise. */
7542 if (parent != unit)
7544 /* Find the highest point of the newly-added tree,
7545 mark each node along the way, and walk from there. */
7546 parent->die_mark = 1;
7547 while (parent->die_parent
7548 && parent->die_parent->die_mark == 0)
7550 parent = parent->die_parent;
7551 parent->die_mark = 1;
7553 copy_decls_walk (unit, parent, decl_table);
7559 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7562 /* Copy declarations for "unworthy" types into the new comdat section.
7563 Incomplete types, modified types, and certain other types aren't broken
7564 out into comdat sections of their own, so they don't have a signature,
7565 and we need to copy the declaration into the same section so that we
7566 don't have an external reference. */
7568 static void
7569 copy_decls_for_unworthy_types (dw_die_ref unit)
7571 mark_dies (unit);
7572 decl_hash_type decl_table (10);
7573 copy_decls_walk (unit, unit, &decl_table);
7574 unmark_dies (unit);
7577 /* Traverse the DIE and add a sibling attribute if it may have the
7578 effect of speeding up access to siblings. To save some space,
7579 avoid generating sibling attributes for DIE's without children. */
7581 static void
7582 add_sibling_attributes (dw_die_ref die)
7584 dw_die_ref c;
7586 if (! die->die_child)
7587 return;
7589 if (die->die_parent && die != die->die_parent->die_child)
7590 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7592 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7595 /* Output all location lists for the DIE and its children. */
7597 static void
7598 output_location_lists (dw_die_ref die)
7600 dw_die_ref c;
7601 dw_attr_ref a;
7602 unsigned ix;
7604 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7605 if (AT_class (a) == dw_val_class_loc_list)
7606 output_loc_list (AT_loc_list (a));
7608 FOR_EACH_CHILD (die, c, output_location_lists (c));
7611 /* We want to limit the number of external references, because they are
7612 larger than local references: a relocation takes multiple words, and
7613 even a sig8 reference is always eight bytes, whereas a local reference
7614 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7615 So if we encounter multiple external references to the same type DIE, we
7616 make a local typedef stub for it and redirect all references there.
7618 This is the element of the hash table for keeping track of these
7619 references. */
7621 struct external_ref
7623 dw_die_ref type;
7624 dw_die_ref stub;
7625 unsigned n_refs;
7628 /* Hashtable helpers. */
7630 struct external_ref_hasher : typed_free_remove <external_ref>
7632 typedef external_ref value_type;
7633 typedef external_ref compare_type;
7634 static inline hashval_t hash (const value_type *);
7635 static inline bool equal (const value_type *, const compare_type *);
7638 inline hashval_t
7639 external_ref_hasher::hash (const value_type *r)
7641 dw_die_ref die = r->type;
7642 hashval_t h = 0;
7644 /* We can't use the address of the DIE for hashing, because
7645 that will make the order of the stub DIEs non-deterministic. */
7646 if (! die->comdat_type_p)
7647 /* We have a symbol; use it to compute a hash. */
7648 h = htab_hash_string (die->die_id.die_symbol);
7649 else
7651 /* We have a type signature; use a subset of the bits as the hash.
7652 The 8-byte signature is at least as large as hashval_t. */
7653 comdat_type_node_ref type_node = die->die_id.die_type_node;
7654 memcpy (&h, type_node->signature, sizeof (h));
7656 return h;
7659 inline bool
7660 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7662 return r1->type == r2->type;
7665 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7667 /* Return a pointer to the external_ref for references to DIE. */
7669 static struct external_ref *
7670 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7672 struct external_ref ref, *ref_p;
7673 external_ref **slot;
7675 ref.type = die;
7676 slot = map->find_slot (&ref, INSERT);
7677 if (*slot != HTAB_EMPTY_ENTRY)
7678 return *slot;
7680 ref_p = XCNEW (struct external_ref);
7681 ref_p->type = die;
7682 *slot = ref_p;
7683 return ref_p;
7686 /* Subroutine of optimize_external_refs, below.
7688 If we see a type skeleton, record it as our stub. If we see external
7689 references, remember how many we've seen. */
7691 static void
7692 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7694 dw_die_ref c;
7695 dw_attr_ref a;
7696 unsigned ix;
7697 struct external_ref *ref_p;
7699 if (is_type_die (die)
7700 && (c = get_AT_ref (die, DW_AT_signature)))
7702 /* This is a local skeleton; use it for local references. */
7703 ref_p = lookup_external_ref (map, c);
7704 ref_p->stub = die;
7707 /* Scan the DIE references, and remember any that refer to DIEs from
7708 other CUs (i.e. those which are not marked). */
7709 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7710 if (AT_class (a) == dw_val_class_die_ref
7711 && (c = AT_ref (a))->die_mark == 0
7712 && is_type_die (c))
7714 ref_p = lookup_external_ref (map, c);
7715 ref_p->n_refs++;
7718 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7721 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7722 points to an external_ref, DATA is the CU we're processing. If we don't
7723 already have a local stub, and we have multiple refs, build a stub. */
7726 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7728 struct external_ref *ref_p = *slot;
7730 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7732 /* We have multiple references to this type, so build a small stub.
7733 Both of these forms are a bit dodgy from the perspective of the
7734 DWARF standard, since technically they should have names. */
7735 dw_die_ref cu = data;
7736 dw_die_ref type = ref_p->type;
7737 dw_die_ref stub = NULL;
7739 if (type->comdat_type_p)
7741 /* If we refer to this type via sig8, use AT_signature. */
7742 stub = new_die (type->die_tag, cu, NULL_TREE);
7743 add_AT_die_ref (stub, DW_AT_signature, type);
7745 else
7747 /* Otherwise, use a typedef with no name. */
7748 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7749 add_AT_die_ref (stub, DW_AT_type, type);
7752 stub->die_mark++;
7753 ref_p->stub = stub;
7755 return 1;
7758 /* DIE is a unit; look through all the DIE references to see if there are
7759 any external references to types, and if so, create local stubs for
7760 them which will be applied in build_abbrev_table. This is useful because
7761 references to local DIEs are smaller. */
7763 static external_ref_hash_type *
7764 optimize_external_refs (dw_die_ref die)
7766 external_ref_hash_type *map = new external_ref_hash_type (10);
7767 optimize_external_refs_1 (die, map);
7768 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7769 return map;
7772 /* The format of each DIE (and its attribute value pairs) is encoded in an
7773 abbreviation table. This routine builds the abbreviation table and assigns
7774 a unique abbreviation id for each abbreviation entry. The children of each
7775 die are visited recursively. */
7777 static void
7778 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7780 unsigned long abbrev_id;
7781 unsigned int n_alloc;
7782 dw_die_ref c;
7783 dw_attr_ref a;
7784 unsigned ix;
7786 /* Scan the DIE references, and replace any that refer to
7787 DIEs from other CUs (i.e. those which are not marked) with
7788 the local stubs we built in optimize_external_refs. */
7789 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7790 if (AT_class (a) == dw_val_class_die_ref
7791 && (c = AT_ref (a))->die_mark == 0)
7793 struct external_ref *ref_p;
7794 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7796 ref_p = lookup_external_ref (extern_map, c);
7797 if (ref_p->stub && ref_p->stub != die)
7798 change_AT_die_ref (a, ref_p->stub);
7799 else
7800 /* We aren't changing this reference, so mark it external. */
7801 set_AT_ref_external (a, 1);
7804 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7806 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7807 dw_attr_ref die_a, abbrev_a;
7808 unsigned ix;
7809 bool ok = true;
7811 if (abbrev->die_tag != die->die_tag)
7812 continue;
7813 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7814 continue;
7816 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7817 continue;
7819 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7821 abbrev_a = &(*abbrev->die_attr)[ix];
7822 if ((abbrev_a->dw_attr != die_a->dw_attr)
7823 || (value_format (abbrev_a) != value_format (die_a)))
7825 ok = false;
7826 break;
7829 if (ok)
7830 break;
7833 if (abbrev_id >= abbrev_die_table_in_use)
7835 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7837 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7838 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7839 n_alloc);
7841 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7842 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7843 abbrev_die_table_allocated = n_alloc;
7846 ++abbrev_die_table_in_use;
7847 abbrev_die_table[abbrev_id] = die;
7850 die->die_abbrev = abbrev_id;
7851 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7854 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7856 static int
7857 constant_size (unsigned HOST_WIDE_INT value)
7859 int log;
7861 if (value == 0)
7862 log = 0;
7863 else
7864 log = floor_log2 (value);
7866 log = log / 8;
7867 log = 1 << (floor_log2 (log) + 1);
7869 return log;
7872 /* Return the size of a DIE as it is represented in the
7873 .debug_info section. */
7875 static unsigned long
7876 size_of_die (dw_die_ref die)
7878 unsigned long size = 0;
7879 dw_attr_ref a;
7880 unsigned ix;
7881 enum dwarf_form form;
7883 size += size_of_uleb128 (die->die_abbrev);
7884 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7886 switch (AT_class (a))
7888 case dw_val_class_addr:
7889 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7891 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7892 size += size_of_uleb128 (AT_index (a));
7894 else
7895 size += DWARF2_ADDR_SIZE;
7896 break;
7897 case dw_val_class_offset:
7898 size += DWARF_OFFSET_SIZE;
7899 break;
7900 case dw_val_class_loc:
7902 unsigned long lsize = size_of_locs (AT_loc (a));
7904 /* Block length. */
7905 if (dwarf_version >= 4)
7906 size += size_of_uleb128 (lsize);
7907 else
7908 size += constant_size (lsize);
7909 size += lsize;
7911 break;
7912 case dw_val_class_loc_list:
7913 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7915 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7916 size += size_of_uleb128 (AT_index (a));
7918 else
7919 size += DWARF_OFFSET_SIZE;
7920 break;
7921 case dw_val_class_range_list:
7922 size += DWARF_OFFSET_SIZE;
7923 break;
7924 case dw_val_class_const:
7925 size += size_of_sleb128 (AT_int (a));
7926 break;
7927 case dw_val_class_unsigned_const:
7929 int csize = constant_size (AT_unsigned (a));
7930 if (dwarf_version == 3
7931 && a->dw_attr == DW_AT_data_member_location
7932 && csize >= 4)
7933 size += size_of_uleb128 (AT_unsigned (a));
7934 else
7935 size += csize;
7937 break;
7938 case dw_val_class_const_double:
7939 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7940 if (HOST_BITS_PER_WIDE_INT >= 64)
7941 size++; /* block */
7942 break;
7943 case dw_val_class_wide_int:
7944 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7945 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7946 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7947 > 64)
7948 size++; /* block */
7949 break;
7950 case dw_val_class_vec:
7951 size += constant_size (a->dw_attr_val.v.val_vec.length
7952 * a->dw_attr_val.v.val_vec.elt_size)
7953 + a->dw_attr_val.v.val_vec.length
7954 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7955 break;
7956 case dw_val_class_flag:
7957 if (dwarf_version >= 4)
7958 /* Currently all add_AT_flag calls pass in 1 as last argument,
7959 so DW_FORM_flag_present can be used. If that ever changes,
7960 we'll need to use DW_FORM_flag and have some optimization
7961 in build_abbrev_table that will change those to
7962 DW_FORM_flag_present if it is set to 1 in all DIEs using
7963 the same abbrev entry. */
7964 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7965 else
7966 size += 1;
7967 break;
7968 case dw_val_class_die_ref:
7969 if (AT_ref_external (a))
7971 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7972 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7973 is sized by target address length, whereas in DWARF3
7974 it's always sized as an offset. */
7975 if (use_debug_types)
7976 size += DWARF_TYPE_SIGNATURE_SIZE;
7977 else if (dwarf_version == 2)
7978 size += DWARF2_ADDR_SIZE;
7979 else
7980 size += DWARF_OFFSET_SIZE;
7982 else
7983 size += DWARF_OFFSET_SIZE;
7984 break;
7985 case dw_val_class_fde_ref:
7986 size += DWARF_OFFSET_SIZE;
7987 break;
7988 case dw_val_class_lbl_id:
7989 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7991 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7992 size += size_of_uleb128 (AT_index (a));
7994 else
7995 size += DWARF2_ADDR_SIZE;
7996 break;
7997 case dw_val_class_lineptr:
7998 case dw_val_class_macptr:
7999 size += DWARF_OFFSET_SIZE;
8000 break;
8001 case dw_val_class_str:
8002 form = AT_string_form (a);
8003 if (form == DW_FORM_strp)
8004 size += DWARF_OFFSET_SIZE;
8005 else if (form == DW_FORM_GNU_str_index)
8006 size += size_of_uleb128 (AT_index (a));
8007 else
8008 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8009 break;
8010 case dw_val_class_file:
8011 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8012 break;
8013 case dw_val_class_data8:
8014 size += 8;
8015 break;
8016 case dw_val_class_vms_delta:
8017 size += DWARF_OFFSET_SIZE;
8018 break;
8019 case dw_val_class_high_pc:
8020 size += DWARF2_ADDR_SIZE;
8021 break;
8022 default:
8023 gcc_unreachable ();
8027 return size;
8030 /* Size the debugging information associated with a given DIE. Visits the
8031 DIE's children recursively. Updates the global variable next_die_offset, on
8032 each time through. Uses the current value of next_die_offset to update the
8033 die_offset field in each DIE. */
8035 static void
8036 calc_die_sizes (dw_die_ref die)
8038 dw_die_ref c;
8040 gcc_assert (die->die_offset == 0
8041 || (unsigned long int) die->die_offset == next_die_offset);
8042 die->die_offset = next_die_offset;
8043 next_die_offset += size_of_die (die);
8045 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8047 if (die->die_child != NULL)
8048 /* Count the null byte used to terminate sibling lists. */
8049 next_die_offset += 1;
8052 /* Size just the base type children at the start of the CU.
8053 This is needed because build_abbrev needs to size locs
8054 and sizing of type based stack ops needs to know die_offset
8055 values for the base types. */
8057 static void
8058 calc_base_type_die_sizes (void)
8060 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8061 unsigned int i;
8062 dw_die_ref base_type;
8063 #if ENABLE_ASSERT_CHECKING
8064 dw_die_ref prev = comp_unit_die ()->die_child;
8065 #endif
8067 die_offset += size_of_die (comp_unit_die ());
8068 for (i = 0; base_types.iterate (i, &base_type); i++)
8070 #if ENABLE_ASSERT_CHECKING
8071 gcc_assert (base_type->die_offset == 0
8072 && prev->die_sib == base_type
8073 && base_type->die_child == NULL
8074 && base_type->die_abbrev);
8075 prev = base_type;
8076 #endif
8077 base_type->die_offset = die_offset;
8078 die_offset += size_of_die (base_type);
8082 /* Set the marks for a die and its children. We do this so
8083 that we know whether or not a reference needs to use FORM_ref_addr; only
8084 DIEs in the same CU will be marked. We used to clear out the offset
8085 and use that as the flag, but ran into ordering problems. */
8087 static void
8088 mark_dies (dw_die_ref die)
8090 dw_die_ref c;
8092 gcc_assert (!die->die_mark);
8094 die->die_mark = 1;
8095 FOR_EACH_CHILD (die, c, mark_dies (c));
8098 /* Clear the marks for a die and its children. */
8100 static void
8101 unmark_dies (dw_die_ref die)
8103 dw_die_ref c;
8105 if (! use_debug_types)
8106 gcc_assert (die->die_mark);
8108 die->die_mark = 0;
8109 FOR_EACH_CHILD (die, c, unmark_dies (c));
8112 /* Clear the marks for a die, its children and referred dies. */
8114 static void
8115 unmark_all_dies (dw_die_ref die)
8117 dw_die_ref c;
8118 dw_attr_ref a;
8119 unsigned ix;
8121 if (!die->die_mark)
8122 return;
8123 die->die_mark = 0;
8125 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8127 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8128 if (AT_class (a) == dw_val_class_die_ref)
8129 unmark_all_dies (AT_ref (a));
8132 /* Calculate if the entry should appear in the final output file. It may be
8133 from a pruned a type. */
8135 static bool
8136 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8138 /* By limiting gnu pubnames to definitions only, gold can generate a
8139 gdb index without entries for declarations, which don't include
8140 enough information to be useful. */
8141 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8142 return false;
8144 if (table == pubname_table)
8146 /* Enumerator names are part of the pubname table, but the
8147 parent DW_TAG_enumeration_type die may have been pruned.
8148 Don't output them if that is the case. */
8149 if (p->die->die_tag == DW_TAG_enumerator &&
8150 (p->die->die_parent == NULL
8151 || !p->die->die_parent->die_perennial_p))
8152 return false;
8154 /* Everything else in the pubname table is included. */
8155 return true;
8158 /* The pubtypes table shouldn't include types that have been
8159 pruned. */
8160 return (p->die->die_offset != 0
8161 || !flag_eliminate_unused_debug_types);
8164 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8165 generated for the compilation unit. */
8167 static unsigned long
8168 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8170 unsigned long size;
8171 unsigned i;
8172 pubname_ref p;
8173 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8175 size = DWARF_PUBNAMES_HEADER_SIZE;
8176 FOR_EACH_VEC_ELT (*names, i, p)
8177 if (include_pubname_in_output (names, p))
8178 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8180 size += DWARF_OFFSET_SIZE;
8181 return size;
8184 /* Return the size of the information in the .debug_aranges section. */
8186 static unsigned long
8187 size_of_aranges (void)
8189 unsigned long size;
8191 size = DWARF_ARANGES_HEADER_SIZE;
8193 /* Count the address/length pair for this compilation unit. */
8194 if (text_section_used)
8195 size += 2 * DWARF2_ADDR_SIZE;
8196 if (cold_text_section_used)
8197 size += 2 * DWARF2_ADDR_SIZE;
8198 if (have_multiple_function_sections)
8200 unsigned fde_idx;
8201 dw_fde_ref fde;
8203 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8205 if (DECL_IGNORED_P (fde->decl))
8206 continue;
8207 if (!fde->in_std_section)
8208 size += 2 * DWARF2_ADDR_SIZE;
8209 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8210 size += 2 * DWARF2_ADDR_SIZE;
8214 /* Count the two zero words used to terminated the address range table. */
8215 size += 2 * DWARF2_ADDR_SIZE;
8216 return size;
8219 /* Select the encoding of an attribute value. */
8221 static enum dwarf_form
8222 value_format (dw_attr_ref a)
8224 switch (AT_class (a))
8226 case dw_val_class_addr:
8227 /* Only very few attributes allow DW_FORM_addr. */
8228 switch (a->dw_attr)
8230 case DW_AT_low_pc:
8231 case DW_AT_high_pc:
8232 case DW_AT_entry_pc:
8233 case DW_AT_trampoline:
8234 return (AT_index (a) == NOT_INDEXED
8235 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8236 default:
8237 break;
8239 switch (DWARF2_ADDR_SIZE)
8241 case 1:
8242 return DW_FORM_data1;
8243 case 2:
8244 return DW_FORM_data2;
8245 case 4:
8246 return DW_FORM_data4;
8247 case 8:
8248 return DW_FORM_data8;
8249 default:
8250 gcc_unreachable ();
8252 case dw_val_class_range_list:
8253 case dw_val_class_loc_list:
8254 if (dwarf_version >= 4)
8255 return DW_FORM_sec_offset;
8256 /* FALLTHRU */
8257 case dw_val_class_vms_delta:
8258 case dw_val_class_offset:
8259 switch (DWARF_OFFSET_SIZE)
8261 case 4:
8262 return DW_FORM_data4;
8263 case 8:
8264 return DW_FORM_data8;
8265 default:
8266 gcc_unreachable ();
8268 case dw_val_class_loc:
8269 if (dwarf_version >= 4)
8270 return DW_FORM_exprloc;
8271 switch (constant_size (size_of_locs (AT_loc (a))))
8273 case 1:
8274 return DW_FORM_block1;
8275 case 2:
8276 return DW_FORM_block2;
8277 case 4:
8278 return DW_FORM_block4;
8279 default:
8280 gcc_unreachable ();
8282 case dw_val_class_const:
8283 return DW_FORM_sdata;
8284 case dw_val_class_unsigned_const:
8285 switch (constant_size (AT_unsigned (a)))
8287 case 1:
8288 return DW_FORM_data1;
8289 case 2:
8290 return DW_FORM_data2;
8291 case 4:
8292 /* In DWARF3 DW_AT_data_member_location with
8293 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8294 constant, so we need to use DW_FORM_udata if we need
8295 a large constant. */
8296 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8297 return DW_FORM_udata;
8298 return DW_FORM_data4;
8299 case 8:
8300 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8301 return DW_FORM_udata;
8302 return DW_FORM_data8;
8303 default:
8304 gcc_unreachable ();
8306 case dw_val_class_const_double:
8307 switch (HOST_BITS_PER_WIDE_INT)
8309 case 8:
8310 return DW_FORM_data2;
8311 case 16:
8312 return DW_FORM_data4;
8313 case 32:
8314 return DW_FORM_data8;
8315 case 64:
8316 default:
8317 return DW_FORM_block1;
8319 case dw_val_class_wide_int:
8320 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8322 case 8:
8323 return DW_FORM_data1;
8324 case 16:
8325 return DW_FORM_data2;
8326 case 32:
8327 return DW_FORM_data4;
8328 case 64:
8329 return DW_FORM_data8;
8330 default:
8331 return DW_FORM_block1;
8333 case dw_val_class_vec:
8334 switch (constant_size (a->dw_attr_val.v.val_vec.length
8335 * a->dw_attr_val.v.val_vec.elt_size))
8337 case 1:
8338 return DW_FORM_block1;
8339 case 2:
8340 return DW_FORM_block2;
8341 case 4:
8342 return DW_FORM_block4;
8343 default:
8344 gcc_unreachable ();
8346 case dw_val_class_flag:
8347 if (dwarf_version >= 4)
8349 /* Currently all add_AT_flag calls pass in 1 as last argument,
8350 so DW_FORM_flag_present can be used. If that ever changes,
8351 we'll need to use DW_FORM_flag and have some optimization
8352 in build_abbrev_table that will change those to
8353 DW_FORM_flag_present if it is set to 1 in all DIEs using
8354 the same abbrev entry. */
8355 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8356 return DW_FORM_flag_present;
8358 return DW_FORM_flag;
8359 case dw_val_class_die_ref:
8360 if (AT_ref_external (a))
8361 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8362 else
8363 return DW_FORM_ref;
8364 case dw_val_class_fde_ref:
8365 return DW_FORM_data;
8366 case dw_val_class_lbl_id:
8367 return (AT_index (a) == NOT_INDEXED
8368 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8369 case dw_val_class_lineptr:
8370 case dw_val_class_macptr:
8371 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8372 case dw_val_class_str:
8373 return AT_string_form (a);
8374 case dw_val_class_file:
8375 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8377 case 1:
8378 return DW_FORM_data1;
8379 case 2:
8380 return DW_FORM_data2;
8381 case 4:
8382 return DW_FORM_data4;
8383 default:
8384 gcc_unreachable ();
8387 case dw_val_class_data8:
8388 return DW_FORM_data8;
8390 case dw_val_class_high_pc:
8391 switch (DWARF2_ADDR_SIZE)
8393 case 1:
8394 return DW_FORM_data1;
8395 case 2:
8396 return DW_FORM_data2;
8397 case 4:
8398 return DW_FORM_data4;
8399 case 8:
8400 return DW_FORM_data8;
8401 default:
8402 gcc_unreachable ();
8405 default:
8406 gcc_unreachable ();
8410 /* Output the encoding of an attribute value. */
8412 static void
8413 output_value_format (dw_attr_ref a)
8415 enum dwarf_form form = value_format (a);
8417 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8420 /* Given a die and id, produce the appropriate abbreviations. */
8422 static void
8423 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8425 unsigned ix;
8426 dw_attr_ref a_attr;
8428 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8429 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8430 dwarf_tag_name (abbrev->die_tag));
8432 if (abbrev->die_child != NULL)
8433 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8434 else
8435 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8437 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8439 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8440 dwarf_attr_name (a_attr->dw_attr));
8441 output_value_format (a_attr);
8444 dw2_asm_output_data (1, 0, NULL);
8445 dw2_asm_output_data (1, 0, NULL);
8449 /* Output the .debug_abbrev section which defines the DIE abbreviation
8450 table. */
8452 static void
8453 output_abbrev_section (void)
8455 unsigned long abbrev_id;
8457 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8458 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8460 /* Terminate the table. */
8461 dw2_asm_output_data (1, 0, NULL);
8464 /* Output a symbol we can use to refer to this DIE from another CU. */
8466 static inline void
8467 output_die_symbol (dw_die_ref die)
8469 const char *sym = die->die_id.die_symbol;
8471 gcc_assert (!die->comdat_type_p);
8473 if (sym == 0)
8474 return;
8476 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8477 /* We make these global, not weak; if the target doesn't support
8478 .linkonce, it doesn't support combining the sections, so debugging
8479 will break. */
8480 targetm.asm_out.globalize_label (asm_out_file, sym);
8482 ASM_OUTPUT_LABEL (asm_out_file, sym);
8485 /* Return a new location list, given the begin and end range, and the
8486 expression. */
8488 static inline dw_loc_list_ref
8489 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8490 const char *section)
8492 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8494 retlist->begin = begin;
8495 retlist->begin_entry = NULL;
8496 retlist->end = end;
8497 retlist->expr = expr;
8498 retlist->section = section;
8500 return retlist;
8503 /* Generate a new internal symbol for this location list node, if it
8504 hasn't got one yet. */
8506 static inline void
8507 gen_llsym (dw_loc_list_ref list)
8509 gcc_assert (!list->ll_symbol);
8510 list->ll_symbol = gen_internal_sym ("LLST");
8513 /* Output the location list given to us. */
8515 static void
8516 output_loc_list (dw_loc_list_ref list_head)
8518 dw_loc_list_ref curr = list_head;
8520 if (list_head->emitted)
8521 return;
8522 list_head->emitted = true;
8524 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8526 /* Walk the location list, and output each range + expression. */
8527 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8529 unsigned long size;
8530 /* Don't output an entry that starts and ends at the same address. */
8531 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8532 continue;
8533 size = size_of_locs (curr->expr);
8534 /* If the expression is too large, drop it on the floor. We could
8535 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8536 in the expression, but >= 64KB expressions for a single value
8537 in a single range are unlikely very useful. */
8538 if (size > 0xffff)
8539 continue;
8540 if (dwarf_split_debug_info)
8542 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8543 "Location list start/length entry (%s)",
8544 list_head->ll_symbol);
8545 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8546 "Location list range start index (%s)",
8547 curr->begin);
8548 /* The length field is 4 bytes. If we ever need to support
8549 an 8-byte length, we can add a new DW_LLE code or fall back
8550 to DW_LLE_GNU_start_end_entry. */
8551 dw2_asm_output_delta (4, curr->end, curr->begin,
8552 "Location list range length (%s)",
8553 list_head->ll_symbol);
8555 else if (!have_multiple_function_sections)
8557 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8558 "Location list begin address (%s)",
8559 list_head->ll_symbol);
8560 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8561 "Location list end address (%s)",
8562 list_head->ll_symbol);
8564 else
8566 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8567 "Location list begin address (%s)",
8568 list_head->ll_symbol);
8569 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8570 "Location list end address (%s)",
8571 list_head->ll_symbol);
8574 /* Output the block length for this list of location operations. */
8575 gcc_assert (size <= 0xffff);
8576 dw2_asm_output_data (2, size, "%s", "Location expression size");
8578 output_loc_sequence (curr->expr, -1);
8581 if (dwarf_split_debug_info)
8582 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8583 "Location list terminator (%s)",
8584 list_head->ll_symbol);
8585 else
8587 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8588 "Location list terminator begin (%s)",
8589 list_head->ll_symbol);
8590 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8591 "Location list terminator end (%s)",
8592 list_head->ll_symbol);
8596 /* Output a range_list offset into the debug_range section. Emit a
8597 relocated reference if val_entry is NULL, otherwise, emit an
8598 indirect reference. */
8600 static void
8601 output_range_list_offset (dw_attr_ref a)
8603 const char *name = dwarf_attr_name (a->dw_attr);
8605 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8607 char *p = strchr (ranges_section_label, '\0');
8608 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8609 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8610 debug_ranges_section, "%s", name);
8611 *p = '\0';
8613 else
8614 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8615 "%s (offset from %s)", name, ranges_section_label);
8618 /* Output the offset into the debug_loc section. */
8620 static void
8621 output_loc_list_offset (dw_attr_ref a)
8623 char *sym = AT_loc_list (a)->ll_symbol;
8625 gcc_assert (sym);
8626 if (dwarf_split_debug_info)
8627 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8628 "%s", dwarf_attr_name (a->dw_attr));
8629 else
8630 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8631 "%s", dwarf_attr_name (a->dw_attr));
8634 /* Output an attribute's index or value appropriately. */
8636 static void
8637 output_attr_index_or_value (dw_attr_ref a)
8639 const char *name = dwarf_attr_name (a->dw_attr);
8641 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8643 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8644 return;
8646 switch (AT_class (a))
8648 case dw_val_class_addr:
8649 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8650 break;
8651 case dw_val_class_high_pc:
8652 case dw_val_class_lbl_id:
8653 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8654 break;
8655 case dw_val_class_loc_list:
8656 output_loc_list_offset (a);
8657 break;
8658 default:
8659 gcc_unreachable ();
8663 /* Output a type signature. */
8665 static inline void
8666 output_signature (const char *sig, const char *name)
8668 int i;
8670 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8671 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8674 /* Output the DIE and its attributes. Called recursively to generate
8675 the definitions of each child DIE. */
8677 static void
8678 output_die (dw_die_ref die)
8680 dw_attr_ref a;
8681 dw_die_ref c;
8682 unsigned long size;
8683 unsigned ix;
8685 /* If someone in another CU might refer to us, set up a symbol for
8686 them to point to. */
8687 if (! die->comdat_type_p && die->die_id.die_symbol)
8688 output_die_symbol (die);
8690 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8691 (unsigned long)die->die_offset,
8692 dwarf_tag_name (die->die_tag));
8694 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8696 const char *name = dwarf_attr_name (a->dw_attr);
8698 switch (AT_class (a))
8700 case dw_val_class_addr:
8701 output_attr_index_or_value (a);
8702 break;
8704 case dw_val_class_offset:
8705 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8706 "%s", name);
8707 break;
8709 case dw_val_class_range_list:
8710 output_range_list_offset (a);
8711 break;
8713 case dw_val_class_loc:
8714 size = size_of_locs (AT_loc (a));
8716 /* Output the block length for this list of location operations. */
8717 if (dwarf_version >= 4)
8718 dw2_asm_output_data_uleb128 (size, "%s", name);
8719 else
8720 dw2_asm_output_data (constant_size (size), size, "%s", name);
8722 output_loc_sequence (AT_loc (a), -1);
8723 break;
8725 case dw_val_class_const:
8726 /* ??? It would be slightly more efficient to use a scheme like is
8727 used for unsigned constants below, but gdb 4.x does not sign
8728 extend. Gdb 5.x does sign extend. */
8729 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8730 break;
8732 case dw_val_class_unsigned_const:
8734 int csize = constant_size (AT_unsigned (a));
8735 if (dwarf_version == 3
8736 && a->dw_attr == DW_AT_data_member_location
8737 && csize >= 4)
8738 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8739 else
8740 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8742 break;
8744 case dw_val_class_const_double:
8746 unsigned HOST_WIDE_INT first, second;
8748 if (HOST_BITS_PER_WIDE_INT >= 64)
8749 dw2_asm_output_data (1,
8750 HOST_BITS_PER_DOUBLE_INT
8751 / HOST_BITS_PER_CHAR,
8752 NULL);
8754 if (WORDS_BIG_ENDIAN)
8756 first = a->dw_attr_val.v.val_double.high;
8757 second = a->dw_attr_val.v.val_double.low;
8759 else
8761 first = a->dw_attr_val.v.val_double.low;
8762 second = a->dw_attr_val.v.val_double.high;
8765 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8766 first, "%s", name);
8767 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8768 second, NULL);
8770 break;
8772 case dw_val_class_wide_int:
8774 int i;
8775 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8776 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8777 if (len * HOST_BITS_PER_WIDE_INT > 64)
8778 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8779 NULL);
8781 if (WORDS_BIG_ENDIAN)
8782 for (i = len - 1; i >= 0; --i)
8784 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8785 name);
8786 name = NULL;
8788 else
8789 for (i = 0; i < len; ++i)
8791 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8792 name);
8793 name = NULL;
8796 break;
8798 case dw_val_class_vec:
8800 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8801 unsigned int len = a->dw_attr_val.v.val_vec.length;
8802 unsigned int i;
8803 unsigned char *p;
8805 dw2_asm_output_data (constant_size (len * elt_size),
8806 len * elt_size, "%s", name);
8807 if (elt_size > sizeof (HOST_WIDE_INT))
8809 elt_size /= 2;
8810 len *= 2;
8812 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8813 i < len;
8814 i++, p += elt_size)
8815 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8816 "fp or vector constant word %u", i);
8817 break;
8820 case dw_val_class_flag:
8821 if (dwarf_version >= 4)
8823 /* Currently all add_AT_flag calls pass in 1 as last argument,
8824 so DW_FORM_flag_present can be used. If that ever changes,
8825 we'll need to use DW_FORM_flag and have some optimization
8826 in build_abbrev_table that will change those to
8827 DW_FORM_flag_present if it is set to 1 in all DIEs using
8828 the same abbrev entry. */
8829 gcc_assert (AT_flag (a) == 1);
8830 if (flag_debug_asm)
8831 fprintf (asm_out_file, "\t\t\t%s %s\n",
8832 ASM_COMMENT_START, name);
8833 break;
8835 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8836 break;
8838 case dw_val_class_loc_list:
8839 output_attr_index_or_value (a);
8840 break;
8842 case dw_val_class_die_ref:
8843 if (AT_ref_external (a))
8845 if (AT_ref (a)->comdat_type_p)
8847 comdat_type_node_ref type_node =
8848 AT_ref (a)->die_id.die_type_node;
8850 gcc_assert (type_node);
8851 output_signature (type_node->signature, name);
8853 else
8855 const char *sym = AT_ref (a)->die_id.die_symbol;
8856 int size;
8858 gcc_assert (sym);
8859 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8860 length, whereas in DWARF3 it's always sized as an
8861 offset. */
8862 if (dwarf_version == 2)
8863 size = DWARF2_ADDR_SIZE;
8864 else
8865 size = DWARF_OFFSET_SIZE;
8866 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8867 name);
8870 else
8872 gcc_assert (AT_ref (a)->die_offset);
8873 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8874 "%s", name);
8876 break;
8878 case dw_val_class_fde_ref:
8880 char l1[20];
8882 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8883 a->dw_attr_val.v.val_fde_index * 2);
8884 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8885 "%s", name);
8887 break;
8889 case dw_val_class_vms_delta:
8890 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8891 AT_vms_delta2 (a), AT_vms_delta1 (a),
8892 "%s", name);
8893 break;
8895 case dw_val_class_lbl_id:
8896 output_attr_index_or_value (a);
8897 break;
8899 case dw_val_class_lineptr:
8900 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8901 debug_line_section, "%s", name);
8902 break;
8904 case dw_val_class_macptr:
8905 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8906 debug_macinfo_section, "%s", name);
8907 break;
8909 case dw_val_class_str:
8910 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8911 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8912 a->dw_attr_val.v.val_str->label,
8913 debug_str_section,
8914 "%s: \"%s\"", name, AT_string (a));
8915 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8916 dw2_asm_output_data_uleb128 (AT_index (a),
8917 "%s: \"%s\"", name, AT_string (a));
8918 else
8919 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8920 break;
8922 case dw_val_class_file:
8924 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8926 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8927 a->dw_attr_val.v.val_file->filename);
8928 break;
8931 case dw_val_class_data8:
8933 int i;
8935 for (i = 0; i < 8; i++)
8936 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8937 i == 0 ? "%s" : NULL, name);
8938 break;
8941 case dw_val_class_high_pc:
8942 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8943 get_AT_low_pc (die), "DW_AT_high_pc");
8944 break;
8946 default:
8947 gcc_unreachable ();
8951 FOR_EACH_CHILD (die, c, output_die (c));
8953 /* Add null byte to terminate sibling list. */
8954 if (die->die_child != NULL)
8955 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8956 (unsigned long) die->die_offset);
8959 /* Output the compilation unit that appears at the beginning of the
8960 .debug_info section, and precedes the DIE descriptions. */
8962 static void
8963 output_compilation_unit_header (void)
8965 int ver = dwarf_version;
8967 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8968 dw2_asm_output_data (4, 0xffffffff,
8969 "Initial length escape value indicating 64-bit DWARF extension");
8970 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8971 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8972 "Length of Compilation Unit Info");
8973 dw2_asm_output_data (2, ver, "DWARF version number");
8974 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8975 debug_abbrev_section,
8976 "Offset Into Abbrev. Section");
8977 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8980 /* Output the compilation unit DIE and its children. */
8982 static void
8983 output_comp_unit (dw_die_ref die, int output_if_empty)
8985 const char *secname, *oldsym;
8986 char *tmp;
8988 /* Unless we are outputting main CU, we may throw away empty ones. */
8989 if (!output_if_empty && die->die_child == NULL)
8990 return;
8992 /* Even if there are no children of this DIE, we must output the information
8993 about the compilation unit. Otherwise, on an empty translation unit, we
8994 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8995 will then complain when examining the file. First mark all the DIEs in
8996 this CU so we know which get local refs. */
8997 mark_dies (die);
8999 external_ref_hash_type *extern_map = optimize_external_refs (die);
9001 build_abbrev_table (die, extern_map);
9003 delete extern_map;
9005 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9006 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9007 calc_die_sizes (die);
9009 oldsym = die->die_id.die_symbol;
9010 if (oldsym)
9012 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9014 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9015 secname = tmp;
9016 die->die_id.die_symbol = NULL;
9017 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9019 else
9021 switch_to_section (debug_info_section);
9022 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9023 info_section_emitted = true;
9026 /* Output debugging information. */
9027 output_compilation_unit_header ();
9028 output_die (die);
9030 /* Leave the marks on the main CU, so we can check them in
9031 output_pubnames. */
9032 if (oldsym)
9034 unmark_dies (die);
9035 die->die_id.die_symbol = oldsym;
9039 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9040 and .debug_pubtypes. This is configured per-target, but can be
9041 overridden by the -gpubnames or -gno-pubnames options. */
9043 static inline bool
9044 want_pubnames (void)
9046 if (debug_info_level <= DINFO_LEVEL_TERSE)
9047 return false;
9048 if (debug_generate_pub_sections != -1)
9049 return debug_generate_pub_sections;
9050 return targetm.want_debug_pub_sections;
9053 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9055 static void
9056 add_AT_pubnames (dw_die_ref die)
9058 if (want_pubnames ())
9059 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9062 /* Add a string attribute value to a skeleton DIE. */
9064 static inline void
9065 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9066 const char *str)
9068 dw_attr_node attr;
9069 struct indirect_string_node *node;
9071 if (! skeleton_debug_str_hash)
9072 skeleton_debug_str_hash
9073 = hash_table<indirect_string_hasher>::create_ggc (10);
9075 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9076 find_string_form (node);
9077 if (node->form == DW_FORM_GNU_str_index)
9078 node->form = DW_FORM_strp;
9080 attr.dw_attr = attr_kind;
9081 attr.dw_attr_val.val_class = dw_val_class_str;
9082 attr.dw_attr_val.val_entry = NULL;
9083 attr.dw_attr_val.v.val_str = node;
9084 add_dwarf_attr (die, &attr);
9087 /* Helper function to generate top-level dies for skeleton debug_info and
9088 debug_types. */
9090 static void
9091 add_top_level_skeleton_die_attrs (dw_die_ref die)
9093 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9094 const char *comp_dir = comp_dir_string ();
9096 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9097 if (comp_dir != NULL)
9098 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9099 add_AT_pubnames (die);
9100 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9103 /* Output skeleton debug sections that point to the dwo file. */
9105 static void
9106 output_skeleton_debug_sections (dw_die_ref comp_unit)
9108 /* These attributes will be found in the full debug_info section. */
9109 remove_AT (comp_unit, DW_AT_producer);
9110 remove_AT (comp_unit, DW_AT_language);
9112 switch_to_section (debug_skeleton_info_section);
9113 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9115 /* Produce the skeleton compilation-unit header. This one differs enough from
9116 a normal CU header that it's better not to call output_compilation_unit
9117 header. */
9118 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9119 dw2_asm_output_data (4, 0xffffffff,
9120 "Initial length escape value indicating 64-bit DWARF extension");
9122 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9123 DWARF_COMPILE_UNIT_HEADER_SIZE
9124 - DWARF_INITIAL_LENGTH_SIZE
9125 + size_of_die (comp_unit),
9126 "Length of Compilation Unit Info");
9127 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9128 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9129 debug_abbrev_section,
9130 "Offset Into Abbrev. Section");
9131 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9133 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9134 output_die (comp_unit);
9136 /* Build the skeleton debug_abbrev section. */
9137 switch_to_section (debug_skeleton_abbrev_section);
9138 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9140 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9142 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9145 /* Output a comdat type unit DIE and its children. */
9147 static void
9148 output_comdat_type_unit (comdat_type_node *node)
9150 const char *secname;
9151 char *tmp;
9152 int i;
9153 #if defined (OBJECT_FORMAT_ELF)
9154 tree comdat_key;
9155 #endif
9157 /* First mark all the DIEs in this CU so we know which get local refs. */
9158 mark_dies (node->root_die);
9160 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9162 build_abbrev_table (node->root_die, extern_map);
9164 delete extern_map;
9165 extern_map = NULL;
9167 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9168 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9169 calc_die_sizes (node->root_die);
9171 #if defined (OBJECT_FORMAT_ELF)
9172 if (!dwarf_split_debug_info)
9173 secname = ".debug_types";
9174 else
9175 secname = ".debug_types.dwo";
9177 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9178 sprintf (tmp, "wt.");
9179 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9180 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9181 comdat_key = get_identifier (tmp);
9182 targetm.asm_out.named_section (secname,
9183 SECTION_DEBUG | SECTION_LINKONCE,
9184 comdat_key);
9185 #else
9186 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9187 sprintf (tmp, ".gnu.linkonce.wt.");
9188 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9189 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9190 secname = tmp;
9191 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9192 #endif
9194 /* Output debugging information. */
9195 output_compilation_unit_header ();
9196 output_signature (node->signature, "Type Signature");
9197 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9198 "Offset to Type DIE");
9199 output_die (node->root_die);
9201 unmark_dies (node->root_die);
9204 /* Return the DWARF2/3 pubname associated with a decl. */
9206 static const char *
9207 dwarf2_name (tree decl, int scope)
9209 if (DECL_NAMELESS (decl))
9210 return NULL;
9211 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9214 /* Add a new entry to .debug_pubnames if appropriate. */
9216 static void
9217 add_pubname_string (const char *str, dw_die_ref die)
9219 pubname_entry e;
9221 e.die = die;
9222 e.name = xstrdup (str);
9223 vec_safe_push (pubname_table, e);
9226 static void
9227 add_pubname (tree decl, dw_die_ref die)
9229 if (!want_pubnames ())
9230 return;
9232 /* Don't add items to the table when we expect that the consumer will have
9233 just read the enclosing die. For example, if the consumer is looking at a
9234 class_member, it will either be inside the class already, or will have just
9235 looked up the class to find the member. Either way, searching the class is
9236 faster than searching the index. */
9237 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9238 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9240 const char *name = dwarf2_name (decl, 1);
9242 if (name)
9243 add_pubname_string (name, die);
9247 /* Add an enumerator to the pubnames section. */
9249 static void
9250 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9252 pubname_entry e;
9254 gcc_assert (scope_name);
9255 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9256 e.die = die;
9257 vec_safe_push (pubname_table, e);
9260 /* Add a new entry to .debug_pubtypes if appropriate. */
9262 static void
9263 add_pubtype (tree decl, dw_die_ref die)
9265 pubname_entry e;
9267 if (!want_pubnames ())
9268 return;
9270 if ((TREE_PUBLIC (decl)
9271 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9272 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9274 tree scope = NULL;
9275 const char *scope_name = "";
9276 const char *sep = is_cxx () ? "::" : ".";
9277 const char *name;
9279 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9280 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9282 scope_name = lang_hooks.dwarf_name (scope, 1);
9283 if (scope_name != NULL && scope_name[0] != '\0')
9284 scope_name = concat (scope_name, sep, NULL);
9285 else
9286 scope_name = "";
9289 if (TYPE_P (decl))
9290 name = type_tag (decl);
9291 else
9292 name = lang_hooks.dwarf_name (decl, 1);
9294 /* If we don't have a name for the type, there's no point in adding
9295 it to the table. */
9296 if (name != NULL && name[0] != '\0')
9298 e.die = die;
9299 e.name = concat (scope_name, name, NULL);
9300 vec_safe_push (pubtype_table, e);
9303 /* Although it might be more consistent to add the pubinfo for the
9304 enumerators as their dies are created, they should only be added if the
9305 enum type meets the criteria above. So rather than re-check the parent
9306 enum type whenever an enumerator die is created, just output them all
9307 here. This isn't protected by the name conditional because anonymous
9308 enums don't have names. */
9309 if (die->die_tag == DW_TAG_enumeration_type)
9311 dw_die_ref c;
9313 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9318 /* Output a single entry in the pubnames table. */
9320 static void
9321 output_pubname (dw_offset die_offset, pubname_entry *entry)
9323 dw_die_ref die = entry->die;
9324 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9326 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9328 if (debug_generate_pub_sections == 2)
9330 /* This logic follows gdb's method for determining the value of the flag
9331 byte. */
9332 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9333 switch (die->die_tag)
9335 case DW_TAG_typedef:
9336 case DW_TAG_base_type:
9337 case DW_TAG_subrange_type:
9338 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9339 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9340 break;
9341 case DW_TAG_enumerator:
9342 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9343 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9344 if (!is_cxx () && !is_java ())
9345 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9346 break;
9347 case DW_TAG_subprogram:
9348 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9349 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9350 if (!is_ada ())
9351 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9352 break;
9353 case DW_TAG_constant:
9354 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9355 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9356 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9357 break;
9358 case DW_TAG_variable:
9359 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9360 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9361 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9362 break;
9363 case DW_TAG_namespace:
9364 case DW_TAG_imported_declaration:
9365 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9366 break;
9367 case DW_TAG_class_type:
9368 case DW_TAG_interface_type:
9369 case DW_TAG_structure_type:
9370 case DW_TAG_union_type:
9371 case DW_TAG_enumeration_type:
9372 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9373 if (!is_cxx () && !is_java ())
9374 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9375 break;
9376 default:
9377 /* An unusual tag. Leave the flag-byte empty. */
9378 break;
9380 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9381 "GDB-index flags");
9384 dw2_asm_output_nstring (entry->name, -1, "external name");
9388 /* Output the public names table used to speed up access to externally
9389 visible names; or the public types table used to find type definitions. */
9391 static void
9392 output_pubnames (vec<pubname_entry, va_gc> *names)
9394 unsigned i;
9395 unsigned long pubnames_length = size_of_pubnames (names);
9396 pubname_ref pub;
9398 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9399 dw2_asm_output_data (4, 0xffffffff,
9400 "Initial length escape value indicating 64-bit DWARF extension");
9401 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9403 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9404 dw2_asm_output_data (2, 2, "DWARF Version");
9406 if (dwarf_split_debug_info)
9407 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9408 debug_skeleton_info_section,
9409 "Offset of Compilation Unit Info");
9410 else
9411 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9412 debug_info_section,
9413 "Offset of Compilation Unit Info");
9414 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9415 "Compilation Unit Length");
9417 FOR_EACH_VEC_ELT (*names, i, pub)
9419 if (include_pubname_in_output (names, pub))
9421 dw_offset die_offset = pub->die->die_offset;
9423 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9424 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9425 gcc_assert (pub->die->die_mark);
9427 /* If we're putting types in their own .debug_types sections,
9428 the .debug_pubtypes table will still point to the compile
9429 unit (not the type unit), so we want to use the offset of
9430 the skeleton DIE (if there is one). */
9431 if (pub->die->comdat_type_p && names == pubtype_table)
9433 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9435 if (type_node != NULL)
9436 die_offset = (type_node->skeleton_die != NULL
9437 ? type_node->skeleton_die->die_offset
9438 : comp_unit_die ()->die_offset);
9441 output_pubname (die_offset, pub);
9445 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9448 /* Output public names and types tables if necessary. */
9450 static void
9451 output_pubtables (void)
9453 if (!want_pubnames () || !info_section_emitted)
9454 return;
9456 switch_to_section (debug_pubnames_section);
9457 output_pubnames (pubname_table);
9458 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9459 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9460 simply won't look for the section. */
9461 switch_to_section (debug_pubtypes_section);
9462 output_pubnames (pubtype_table);
9466 /* Output the information that goes into the .debug_aranges table.
9467 Namely, define the beginning and ending address range of the
9468 text section generated for this compilation unit. */
9470 static void
9471 output_aranges (unsigned long aranges_length)
9473 unsigned i;
9475 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9476 dw2_asm_output_data (4, 0xffffffff,
9477 "Initial length escape value indicating 64-bit DWARF extension");
9478 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9479 "Length of Address Ranges Info");
9480 /* Version number for aranges is still 2, even in DWARF3. */
9481 dw2_asm_output_data (2, 2, "DWARF Version");
9482 if (dwarf_split_debug_info)
9483 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9484 debug_skeleton_info_section,
9485 "Offset of Compilation Unit Info");
9486 else
9487 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9488 debug_info_section,
9489 "Offset of Compilation Unit Info");
9490 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9491 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9493 /* We need to align to twice the pointer size here. */
9494 if (DWARF_ARANGES_PAD_SIZE)
9496 /* Pad using a 2 byte words so that padding is correct for any
9497 pointer size. */
9498 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9499 2 * DWARF2_ADDR_SIZE);
9500 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9501 dw2_asm_output_data (2, 0, NULL);
9504 /* It is necessary not to output these entries if the sections were
9505 not used; if the sections were not used, the length will be 0 and
9506 the address may end up as 0 if the section is discarded by ld
9507 --gc-sections, leaving an invalid (0, 0) entry that can be
9508 confused with the terminator. */
9509 if (text_section_used)
9511 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9512 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9513 text_section_label, "Length");
9515 if (cold_text_section_used)
9517 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9518 "Address");
9519 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9520 cold_text_section_label, "Length");
9523 if (have_multiple_function_sections)
9525 unsigned fde_idx;
9526 dw_fde_ref fde;
9528 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9530 if (DECL_IGNORED_P (fde->decl))
9531 continue;
9532 if (!fde->in_std_section)
9534 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9535 "Address");
9536 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9537 fde->dw_fde_begin, "Length");
9539 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9541 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9542 "Address");
9543 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9544 fde->dw_fde_second_begin, "Length");
9549 /* Output the terminator words. */
9550 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9551 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9554 /* Add a new entry to .debug_ranges. Return the offset at which it
9555 was placed. */
9557 static unsigned int
9558 add_ranges_num (int num)
9560 unsigned int in_use = ranges_table_in_use;
9562 if (in_use == ranges_table_allocated)
9564 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9565 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9566 ranges_table_allocated);
9567 memset (ranges_table + ranges_table_in_use, 0,
9568 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9571 ranges_table[in_use].num = num;
9572 ranges_table_in_use = in_use + 1;
9574 return in_use * 2 * DWARF2_ADDR_SIZE;
9577 /* Add a new entry to .debug_ranges corresponding to a block, or a
9578 range terminator if BLOCK is NULL. */
9580 static unsigned int
9581 add_ranges (const_tree block)
9583 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9586 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9587 When using dwarf_split_debug_info, address attributes in dies destined
9588 for the final executable should be direct references--setting the
9589 parameter force_direct ensures this behavior. */
9591 static void
9592 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9593 bool *added, bool force_direct)
9595 unsigned int in_use = ranges_by_label_in_use;
9596 unsigned int offset;
9598 if (in_use == ranges_by_label_allocated)
9600 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9601 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9602 ranges_by_label,
9603 ranges_by_label_allocated);
9604 memset (ranges_by_label + ranges_by_label_in_use, 0,
9605 RANGES_TABLE_INCREMENT
9606 * sizeof (struct dw_ranges_by_label_struct));
9609 ranges_by_label[in_use].begin = begin;
9610 ranges_by_label[in_use].end = end;
9611 ranges_by_label_in_use = in_use + 1;
9613 offset = add_ranges_num (-(int)in_use - 1);
9614 if (!*added)
9616 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9617 *added = true;
9621 static void
9622 output_ranges (void)
9624 unsigned i;
9625 static const char *const start_fmt = "Offset %#x";
9626 const char *fmt = start_fmt;
9628 for (i = 0; i < ranges_table_in_use; i++)
9630 int block_num = ranges_table[i].num;
9632 if (block_num > 0)
9634 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9635 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9637 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9638 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9640 /* If all code is in the text section, then the compilation
9641 unit base address defaults to DW_AT_low_pc, which is the
9642 base of the text section. */
9643 if (!have_multiple_function_sections)
9645 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9646 text_section_label,
9647 fmt, i * 2 * DWARF2_ADDR_SIZE);
9648 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9649 text_section_label, NULL);
9652 /* Otherwise, the compilation unit base address is zero,
9653 which allows us to use absolute addresses, and not worry
9654 about whether the target supports cross-section
9655 arithmetic. */
9656 else
9658 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9659 fmt, i * 2 * DWARF2_ADDR_SIZE);
9660 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9663 fmt = NULL;
9666 /* Negative block_num stands for an index into ranges_by_label. */
9667 else if (block_num < 0)
9669 int lab_idx = - block_num - 1;
9671 if (!have_multiple_function_sections)
9673 gcc_unreachable ();
9674 #if 0
9675 /* If we ever use add_ranges_by_labels () for a single
9676 function section, all we have to do is to take out
9677 the #if 0 above. */
9678 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9679 ranges_by_label[lab_idx].begin,
9680 text_section_label,
9681 fmt, i * 2 * DWARF2_ADDR_SIZE);
9682 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9683 ranges_by_label[lab_idx].end,
9684 text_section_label, NULL);
9685 #endif
9687 else
9689 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9690 ranges_by_label[lab_idx].begin,
9691 fmt, i * 2 * DWARF2_ADDR_SIZE);
9692 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9693 ranges_by_label[lab_idx].end,
9694 NULL);
9697 else
9699 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9700 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9701 fmt = start_fmt;
9706 /* Data structure containing information about input files. */
9707 struct file_info
9709 const char *path; /* Complete file name. */
9710 const char *fname; /* File name part. */
9711 int length; /* Length of entire string. */
9712 struct dwarf_file_data * file_idx; /* Index in input file table. */
9713 int dir_idx; /* Index in directory table. */
9716 /* Data structure containing information about directories with source
9717 files. */
9718 struct dir_info
9720 const char *path; /* Path including directory name. */
9721 int length; /* Path length. */
9722 int prefix; /* Index of directory entry which is a prefix. */
9723 int count; /* Number of files in this directory. */
9724 int dir_idx; /* Index of directory used as base. */
9727 /* Callback function for file_info comparison. We sort by looking at
9728 the directories in the path. */
9730 static int
9731 file_info_cmp (const void *p1, const void *p2)
9733 const struct file_info *const s1 = (const struct file_info *) p1;
9734 const struct file_info *const s2 = (const struct file_info *) p2;
9735 const unsigned char *cp1;
9736 const unsigned char *cp2;
9738 /* Take care of file names without directories. We need to make sure that
9739 we return consistent values to qsort since some will get confused if
9740 we return the same value when identical operands are passed in opposite
9741 orders. So if neither has a directory, return 0 and otherwise return
9742 1 or -1 depending on which one has the directory. */
9743 if ((s1->path == s1->fname || s2->path == s2->fname))
9744 return (s2->path == s2->fname) - (s1->path == s1->fname);
9746 cp1 = (const unsigned char *) s1->path;
9747 cp2 = (const unsigned char *) s2->path;
9749 while (1)
9751 ++cp1;
9752 ++cp2;
9753 /* Reached the end of the first path? If so, handle like above. */
9754 if ((cp1 == (const unsigned char *) s1->fname)
9755 || (cp2 == (const unsigned char *) s2->fname))
9756 return ((cp2 == (const unsigned char *) s2->fname)
9757 - (cp1 == (const unsigned char *) s1->fname));
9759 /* Character of current path component the same? */
9760 else if (*cp1 != *cp2)
9761 return *cp1 - *cp2;
9765 struct file_name_acquire_data
9767 struct file_info *files;
9768 int used_files;
9769 int max_files;
9772 /* Traversal function for the hash table. */
9775 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9777 struct dwarf_file_data *d = *slot;
9778 struct file_info *fi;
9779 const char *f;
9781 gcc_assert (fnad->max_files >= d->emitted_number);
9783 if (! d->emitted_number)
9784 return 1;
9786 gcc_assert (fnad->max_files != fnad->used_files);
9788 fi = fnad->files + fnad->used_files++;
9790 /* Skip all leading "./". */
9791 f = d->filename;
9792 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9793 f += 2;
9795 /* Create a new array entry. */
9796 fi->path = f;
9797 fi->length = strlen (f);
9798 fi->file_idx = d;
9800 /* Search for the file name part. */
9801 f = strrchr (f, DIR_SEPARATOR);
9802 #if defined (DIR_SEPARATOR_2)
9804 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9806 if (g != NULL)
9808 if (f == NULL || f < g)
9809 f = g;
9812 #endif
9814 fi->fname = f == NULL ? fi->path : f + 1;
9815 return 1;
9818 /* Output the directory table and the file name table. We try to minimize
9819 the total amount of memory needed. A heuristic is used to avoid large
9820 slowdowns with many input files. */
9822 static void
9823 output_file_names (void)
9825 struct file_name_acquire_data fnad;
9826 int numfiles;
9827 struct file_info *files;
9828 struct dir_info *dirs;
9829 int *saved;
9830 int *savehere;
9831 int *backmap;
9832 int ndirs;
9833 int idx_offset;
9834 int i;
9836 if (!last_emitted_file)
9838 dw2_asm_output_data (1, 0, "End directory table");
9839 dw2_asm_output_data (1, 0, "End file name table");
9840 return;
9843 numfiles = last_emitted_file->emitted_number;
9845 /* Allocate the various arrays we need. */
9846 files = XALLOCAVEC (struct file_info, numfiles);
9847 dirs = XALLOCAVEC (struct dir_info, numfiles);
9849 fnad.files = files;
9850 fnad.used_files = 0;
9851 fnad.max_files = numfiles;
9852 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9853 gcc_assert (fnad.used_files == fnad.max_files);
9855 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9857 /* Find all the different directories used. */
9858 dirs[0].path = files[0].path;
9859 dirs[0].length = files[0].fname - files[0].path;
9860 dirs[0].prefix = -1;
9861 dirs[0].count = 1;
9862 dirs[0].dir_idx = 0;
9863 files[0].dir_idx = 0;
9864 ndirs = 1;
9866 for (i = 1; i < numfiles; i++)
9867 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9868 && memcmp (dirs[ndirs - 1].path, files[i].path,
9869 dirs[ndirs - 1].length) == 0)
9871 /* Same directory as last entry. */
9872 files[i].dir_idx = ndirs - 1;
9873 ++dirs[ndirs - 1].count;
9875 else
9877 int j;
9879 /* This is a new directory. */
9880 dirs[ndirs].path = files[i].path;
9881 dirs[ndirs].length = files[i].fname - files[i].path;
9882 dirs[ndirs].count = 1;
9883 dirs[ndirs].dir_idx = ndirs;
9884 files[i].dir_idx = ndirs;
9886 /* Search for a prefix. */
9887 dirs[ndirs].prefix = -1;
9888 for (j = 0; j < ndirs; j++)
9889 if (dirs[j].length < dirs[ndirs].length
9890 && dirs[j].length > 1
9891 && (dirs[ndirs].prefix == -1
9892 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9893 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9894 dirs[ndirs].prefix = j;
9896 ++ndirs;
9899 /* Now to the actual work. We have to find a subset of the directories which
9900 allow expressing the file name using references to the directory table
9901 with the least amount of characters. We do not do an exhaustive search
9902 where we would have to check out every combination of every single
9903 possible prefix. Instead we use a heuristic which provides nearly optimal
9904 results in most cases and never is much off. */
9905 saved = XALLOCAVEC (int, ndirs);
9906 savehere = XALLOCAVEC (int, ndirs);
9908 memset (saved, '\0', ndirs * sizeof (saved[0]));
9909 for (i = 0; i < ndirs; i++)
9911 int j;
9912 int total;
9914 /* We can always save some space for the current directory. But this
9915 does not mean it will be enough to justify adding the directory. */
9916 savehere[i] = dirs[i].length;
9917 total = (savehere[i] - saved[i]) * dirs[i].count;
9919 for (j = i + 1; j < ndirs; j++)
9921 savehere[j] = 0;
9922 if (saved[j] < dirs[i].length)
9924 /* Determine whether the dirs[i] path is a prefix of the
9925 dirs[j] path. */
9926 int k;
9928 k = dirs[j].prefix;
9929 while (k != -1 && k != (int) i)
9930 k = dirs[k].prefix;
9932 if (k == (int) i)
9934 /* Yes it is. We can possibly save some memory by
9935 writing the filenames in dirs[j] relative to
9936 dirs[i]. */
9937 savehere[j] = dirs[i].length;
9938 total += (savehere[j] - saved[j]) * dirs[j].count;
9943 /* Check whether we can save enough to justify adding the dirs[i]
9944 directory. */
9945 if (total > dirs[i].length + 1)
9947 /* It's worthwhile adding. */
9948 for (j = i; j < ndirs; j++)
9949 if (savehere[j] > 0)
9951 /* Remember how much we saved for this directory so far. */
9952 saved[j] = savehere[j];
9954 /* Remember the prefix directory. */
9955 dirs[j].dir_idx = i;
9960 /* Emit the directory name table. */
9961 idx_offset = dirs[0].length > 0 ? 1 : 0;
9962 for (i = 1 - idx_offset; i < ndirs; i++)
9963 dw2_asm_output_nstring (dirs[i].path,
9964 dirs[i].length
9965 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9966 "Directory Entry: %#x", i + idx_offset);
9968 dw2_asm_output_data (1, 0, "End directory table");
9970 /* We have to emit them in the order of emitted_number since that's
9971 used in the debug info generation. To do this efficiently we
9972 generate a back-mapping of the indices first. */
9973 backmap = XALLOCAVEC (int, numfiles);
9974 for (i = 0; i < numfiles; i++)
9975 backmap[files[i].file_idx->emitted_number - 1] = i;
9977 /* Now write all the file names. */
9978 for (i = 0; i < numfiles; i++)
9980 int file_idx = backmap[i];
9981 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9983 #ifdef VMS_DEBUGGING_INFO
9984 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9986 /* Setting these fields can lead to debugger miscomparisons,
9987 but VMS Debug requires them to be set correctly. */
9989 int ver;
9990 long long cdt;
9991 long siz;
9992 int maxfilelen = strlen (files[file_idx].path)
9993 + dirs[dir_idx].length
9994 + MAX_VMS_VERSION_LEN + 1;
9995 char *filebuf = XALLOCAVEC (char, maxfilelen);
9997 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9998 snprintf (filebuf, maxfilelen, "%s;%d",
9999 files[file_idx].path + dirs[dir_idx].length, ver);
10001 dw2_asm_output_nstring
10002 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10004 /* Include directory index. */
10005 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10007 /* Modification time. */
10008 dw2_asm_output_data_uleb128
10009 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10010 ? cdt : 0,
10011 NULL);
10013 /* File length in bytes. */
10014 dw2_asm_output_data_uleb128
10015 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10016 ? siz : 0,
10017 NULL);
10018 #else
10019 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10020 "File Entry: %#x", (unsigned) i + 1);
10022 /* Include directory index. */
10023 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10025 /* Modification time. */
10026 dw2_asm_output_data_uleb128 (0, NULL);
10028 /* File length in bytes. */
10029 dw2_asm_output_data_uleb128 (0, NULL);
10030 #endif /* VMS_DEBUGGING_INFO */
10033 dw2_asm_output_data (1, 0, "End file name table");
10037 /* Output one line number table into the .debug_line section. */
10039 static void
10040 output_one_line_info_table (dw_line_info_table *table)
10042 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10043 unsigned int current_line = 1;
10044 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10045 dw_line_info_entry *ent;
10046 size_t i;
10048 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10050 switch (ent->opcode)
10052 case LI_set_address:
10053 /* ??? Unfortunately, we have little choice here currently, and
10054 must always use the most general form. GCC does not know the
10055 address delta itself, so we can't use DW_LNS_advance_pc. Many
10056 ports do have length attributes which will give an upper bound
10057 on the address range. We could perhaps use length attributes
10058 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10059 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10061 /* This can handle any delta. This takes
10062 4+DWARF2_ADDR_SIZE bytes. */
10063 dw2_asm_output_data (1, 0, "set address %s", line_label);
10064 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10065 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10066 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10067 break;
10069 case LI_set_line:
10070 if (ent->val == current_line)
10072 /* We still need to start a new row, so output a copy insn. */
10073 dw2_asm_output_data (1, DW_LNS_copy,
10074 "copy line %u", current_line);
10076 else
10078 int line_offset = ent->val - current_line;
10079 int line_delta = line_offset - DWARF_LINE_BASE;
10081 current_line = ent->val;
10082 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10084 /* This can handle deltas from -10 to 234, using the current
10085 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10086 This takes 1 byte. */
10087 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10088 "line %u", current_line);
10090 else
10092 /* This can handle any delta. This takes at least 4 bytes,
10093 depending on the value being encoded. */
10094 dw2_asm_output_data (1, DW_LNS_advance_line,
10095 "advance to line %u", current_line);
10096 dw2_asm_output_data_sleb128 (line_offset, NULL);
10097 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10100 break;
10102 case LI_set_file:
10103 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10104 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10105 break;
10107 case LI_set_column:
10108 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10109 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10110 break;
10112 case LI_negate_stmt:
10113 current_is_stmt = !current_is_stmt;
10114 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10115 "is_stmt %d", current_is_stmt);
10116 break;
10118 case LI_set_prologue_end:
10119 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10120 "set prologue end");
10121 break;
10123 case LI_set_epilogue_begin:
10124 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10125 "set epilogue begin");
10126 break;
10128 case LI_set_discriminator:
10129 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10130 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10131 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10132 dw2_asm_output_data_uleb128 (ent->val, NULL);
10133 break;
10137 /* Emit debug info for the address of the end of the table. */
10138 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10139 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10140 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10141 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10143 dw2_asm_output_data (1, 0, "end sequence");
10144 dw2_asm_output_data_uleb128 (1, NULL);
10145 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10148 /* Output the source line number correspondence information. This
10149 information goes into the .debug_line section. */
10151 static void
10152 output_line_info (bool prologue_only)
10154 char l1[20], l2[20], p1[20], p2[20];
10155 int ver = dwarf_version;
10156 bool saw_one = false;
10157 int opc;
10159 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10160 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10161 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10162 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10164 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10165 dw2_asm_output_data (4, 0xffffffff,
10166 "Initial length escape value indicating 64-bit DWARF extension");
10167 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10168 "Length of Source Line Info");
10169 ASM_OUTPUT_LABEL (asm_out_file, l1);
10171 dw2_asm_output_data (2, ver, "DWARF Version");
10172 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10173 ASM_OUTPUT_LABEL (asm_out_file, p1);
10175 /* Define the architecture-dependent minimum instruction length (in bytes).
10176 In this implementation of DWARF, this field is used for information
10177 purposes only. Since GCC generates assembly language, we have no
10178 a priori knowledge of how many instruction bytes are generated for each
10179 source line, and therefore can use only the DW_LNE_set_address and
10180 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10181 this as '1', which is "correct enough" for all architectures,
10182 and don't let the target override. */
10183 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10185 if (ver >= 4)
10186 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10187 "Maximum Operations Per Instruction");
10188 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10189 "Default is_stmt_start flag");
10190 dw2_asm_output_data (1, DWARF_LINE_BASE,
10191 "Line Base Value (Special Opcodes)");
10192 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10193 "Line Range Value (Special Opcodes)");
10194 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10195 "Special Opcode Base");
10197 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10199 int n_op_args;
10200 switch (opc)
10202 case DW_LNS_advance_pc:
10203 case DW_LNS_advance_line:
10204 case DW_LNS_set_file:
10205 case DW_LNS_set_column:
10206 case DW_LNS_fixed_advance_pc:
10207 case DW_LNS_set_isa:
10208 n_op_args = 1;
10209 break;
10210 default:
10211 n_op_args = 0;
10212 break;
10215 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10216 opc, n_op_args);
10219 /* Write out the information about the files we use. */
10220 output_file_names ();
10221 ASM_OUTPUT_LABEL (asm_out_file, p2);
10222 if (prologue_only)
10224 /* Output the marker for the end of the line number info. */
10225 ASM_OUTPUT_LABEL (asm_out_file, l2);
10226 return;
10229 if (separate_line_info)
10231 dw_line_info_table *table;
10232 size_t i;
10234 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10235 if (table->in_use)
10237 output_one_line_info_table (table);
10238 saw_one = true;
10241 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10243 output_one_line_info_table (cold_text_section_line_info);
10244 saw_one = true;
10247 /* ??? Some Darwin linkers crash on a .debug_line section with no
10248 sequences. Further, merely a DW_LNE_end_sequence entry is not
10249 sufficient -- the address column must also be initialized.
10250 Make sure to output at least one set_address/end_sequence pair,
10251 choosing .text since that section is always present. */
10252 if (text_section_line_info->in_use || !saw_one)
10253 output_one_line_info_table (text_section_line_info);
10255 /* Output the marker for the end of the line number info. */
10256 ASM_OUTPUT_LABEL (asm_out_file, l2);
10259 /* Given a pointer to a tree node for some base type, return a pointer to
10260 a DIE that describes the given type.
10262 This routine must only be called for GCC type nodes that correspond to
10263 Dwarf base (fundamental) types. */
10265 static dw_die_ref
10266 base_type_die (tree type)
10268 dw_die_ref base_type_result;
10269 enum dwarf_type encoding;
10271 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10272 return 0;
10274 /* If this is a subtype that should not be emitted as a subrange type,
10275 use the base type. See subrange_type_for_debug_p. */
10276 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10277 type = TREE_TYPE (type);
10279 switch (TREE_CODE (type))
10281 case INTEGER_TYPE:
10282 if ((dwarf_version >= 4 || !dwarf_strict)
10283 && TYPE_NAME (type)
10284 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10285 && DECL_IS_BUILTIN (TYPE_NAME (type))
10286 && DECL_NAME (TYPE_NAME (type)))
10288 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10289 if (strcmp (name, "char16_t") == 0
10290 || strcmp (name, "char32_t") == 0)
10292 encoding = DW_ATE_UTF;
10293 break;
10296 if (TYPE_STRING_FLAG (type))
10298 if (TYPE_UNSIGNED (type))
10299 encoding = DW_ATE_unsigned_char;
10300 else
10301 encoding = DW_ATE_signed_char;
10303 else if (TYPE_UNSIGNED (type))
10304 encoding = DW_ATE_unsigned;
10305 else
10306 encoding = DW_ATE_signed;
10307 break;
10309 case REAL_TYPE:
10310 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10312 if (dwarf_version >= 3 || !dwarf_strict)
10313 encoding = DW_ATE_decimal_float;
10314 else
10315 encoding = DW_ATE_lo_user;
10317 else
10318 encoding = DW_ATE_float;
10319 break;
10321 case FIXED_POINT_TYPE:
10322 if (!(dwarf_version >= 3 || !dwarf_strict))
10323 encoding = DW_ATE_lo_user;
10324 else if (TYPE_UNSIGNED (type))
10325 encoding = DW_ATE_unsigned_fixed;
10326 else
10327 encoding = DW_ATE_signed_fixed;
10328 break;
10330 /* Dwarf2 doesn't know anything about complex ints, so use
10331 a user defined type for it. */
10332 case COMPLEX_TYPE:
10333 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10334 encoding = DW_ATE_complex_float;
10335 else
10336 encoding = DW_ATE_lo_user;
10337 break;
10339 case BOOLEAN_TYPE:
10340 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10341 encoding = DW_ATE_boolean;
10342 break;
10344 default:
10345 /* No other TREE_CODEs are Dwarf fundamental types. */
10346 gcc_unreachable ();
10349 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10351 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10352 int_size_in_bytes (type));
10353 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10354 add_pubtype (type, base_type_result);
10356 return base_type_result;
10359 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10360 named 'auto' in its type: return true for it, false otherwise. */
10362 static inline bool
10363 is_cxx_auto (tree type)
10365 if (is_cxx ())
10367 tree name = TYPE_IDENTIFIER (type);
10368 if (name == get_identifier ("auto")
10369 || name == get_identifier ("decltype(auto)"))
10370 return true;
10372 return false;
10375 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10376 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10378 static inline int
10379 is_base_type (tree type)
10381 switch (TREE_CODE (type))
10383 case ERROR_MARK:
10384 case VOID_TYPE:
10385 case INTEGER_TYPE:
10386 case REAL_TYPE:
10387 case FIXED_POINT_TYPE:
10388 case COMPLEX_TYPE:
10389 case BOOLEAN_TYPE:
10390 return 1;
10392 case ARRAY_TYPE:
10393 case RECORD_TYPE:
10394 case UNION_TYPE:
10395 case QUAL_UNION_TYPE:
10396 case ENUMERAL_TYPE:
10397 case FUNCTION_TYPE:
10398 case METHOD_TYPE:
10399 case POINTER_TYPE:
10400 case REFERENCE_TYPE:
10401 case NULLPTR_TYPE:
10402 case OFFSET_TYPE:
10403 case LANG_TYPE:
10404 case VECTOR_TYPE:
10405 return 0;
10407 default:
10408 if (is_cxx_auto (type))
10409 return 0;
10410 gcc_unreachable ();
10413 return 0;
10416 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10417 node, return the size in bits for the type if it is a constant, or else
10418 return the alignment for the type if the type's size is not constant, or
10419 else return BITS_PER_WORD if the type actually turns out to be an
10420 ERROR_MARK node. */
10422 static inline unsigned HOST_WIDE_INT
10423 simple_type_size_in_bits (const_tree type)
10425 if (TREE_CODE (type) == ERROR_MARK)
10426 return BITS_PER_WORD;
10427 else if (TYPE_SIZE (type) == NULL_TREE)
10428 return 0;
10429 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10430 return tree_to_uhwi (TYPE_SIZE (type));
10431 else
10432 return TYPE_ALIGN (type);
10435 /* Similarly, but return an offset_int instead of UHWI. */
10437 static inline offset_int
10438 offset_int_type_size_in_bits (const_tree type)
10440 if (TREE_CODE (type) == ERROR_MARK)
10441 return BITS_PER_WORD;
10442 else if (TYPE_SIZE (type) == NULL_TREE)
10443 return 0;
10444 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10445 return wi::to_offset (TYPE_SIZE (type));
10446 else
10447 return TYPE_ALIGN (type);
10450 /* Given a pointer to a tree node for a subrange type, return a pointer
10451 to a DIE that describes the given type. */
10453 static dw_die_ref
10454 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10456 dw_die_ref subrange_die;
10457 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10459 if (context_die == NULL)
10460 context_die = comp_unit_die ();
10462 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10464 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10466 /* The size of the subrange type and its base type do not match,
10467 so we need to generate a size attribute for the subrange type. */
10468 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10471 if (low)
10472 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10473 if (high)
10474 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10476 return subrange_die;
10479 /* Returns the (const and/or volatile) cv_qualifiers associated with
10480 the decl node. This will normally be augmented with the
10481 cv_qualifiers of the underlying type in add_type_attribute. */
10483 static int
10484 decl_quals (const_tree decl)
10486 return ((TREE_READONLY (decl)
10487 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10488 | (TREE_THIS_VOLATILE (decl)
10489 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10492 /* Determine the TYPE whose qualifiers match the largest strict subset
10493 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10494 qualifiers outside QUAL_MASK. */
10496 static int
10497 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10499 tree t;
10500 int best_rank = 0, best_qual = 0, max_rank;
10502 type_quals &= qual_mask;
10503 max_rank = popcount_hwi (type_quals) - 1;
10505 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10506 t = TYPE_NEXT_VARIANT (t))
10508 int q = TYPE_QUALS (t) & qual_mask;
10510 if ((q & type_quals) == q && q != type_quals
10511 && check_base_type (t, type))
10513 int rank = popcount_hwi (q);
10515 if (rank > best_rank)
10517 best_rank = rank;
10518 best_qual = q;
10523 return best_qual;
10526 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10527 entry that chains various modifiers in front of the given type. */
10529 static dw_die_ref
10530 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10532 enum tree_code code = TREE_CODE (type);
10533 dw_die_ref mod_type_die;
10534 dw_die_ref sub_die = NULL;
10535 tree item_type = NULL;
10536 tree qualified_type;
10537 tree name, low, high;
10538 dw_die_ref mod_scope;
10539 /* Only these cv-qualifiers are currently handled. */
10540 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10541 | TYPE_QUAL_RESTRICT);
10543 if (code == ERROR_MARK)
10544 return NULL;
10546 cv_quals &= cv_qual_mask;
10548 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10549 tag modifier (and not an attribute) old consumers won't be able
10550 to handle it. */
10551 if (dwarf_version < 3)
10552 cv_quals &= ~TYPE_QUAL_RESTRICT;
10554 /* See if we already have the appropriately qualified variant of
10555 this type. */
10556 qualified_type = get_qualified_type (type, cv_quals);
10558 if (qualified_type == sizetype
10559 && TYPE_NAME (qualified_type)
10560 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10562 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10564 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10565 && TYPE_PRECISION (t)
10566 == TYPE_PRECISION (qualified_type)
10567 && TYPE_UNSIGNED (t)
10568 == TYPE_UNSIGNED (qualified_type));
10569 qualified_type = t;
10572 /* If we do, then we can just use its DIE, if it exists. */
10573 if (qualified_type)
10575 mod_type_die = lookup_type_die (qualified_type);
10576 if (mod_type_die)
10577 return mod_type_die;
10580 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10582 /* Handle C typedef types. */
10583 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10584 && !DECL_ARTIFICIAL (name))
10586 tree dtype = TREE_TYPE (name);
10588 if (qualified_type == dtype)
10590 /* For a named type, use the typedef. */
10591 gen_type_die (qualified_type, context_die);
10592 return lookup_type_die (qualified_type);
10594 else
10596 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10597 dquals &= cv_qual_mask;
10598 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10599 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10600 /* cv-unqualified version of named type. Just use
10601 the unnamed type to which it refers. */
10602 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10603 cv_quals, context_die);
10604 /* Else cv-qualified version of named type; fall through. */
10608 mod_scope = scope_die_for (type, context_die);
10610 if (cv_quals)
10612 struct qual_info { int q; enum dwarf_tag t; };
10613 static const struct qual_info qual_info[] =
10615 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10616 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10617 { TYPE_QUAL_CONST, DW_TAG_const_type },
10619 int sub_quals;
10620 unsigned i;
10622 /* Determine a lesser qualified type that most closely matches
10623 this one. Then generate DW_TAG_* entries for the remaining
10624 qualifiers. */
10625 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10626 cv_qual_mask);
10627 mod_type_die = modified_type_die (type, sub_quals, context_die);
10629 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10630 if (qual_info[i].q & cv_quals & ~sub_quals)
10632 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10633 if (mod_type_die)
10634 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10635 mod_type_die = d;
10638 else if (code == POINTER_TYPE)
10640 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10641 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10642 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10643 item_type = TREE_TYPE (type);
10644 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10645 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10646 TYPE_ADDR_SPACE (item_type));
10648 else if (code == REFERENCE_TYPE)
10650 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10651 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10652 type);
10653 else
10654 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10655 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10656 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10657 item_type = TREE_TYPE (type);
10658 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10659 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10660 TYPE_ADDR_SPACE (item_type));
10662 else if (code == INTEGER_TYPE
10663 && TREE_TYPE (type) != NULL_TREE
10664 && subrange_type_for_debug_p (type, &low, &high))
10666 mod_type_die = subrange_type_die (type, low, high, context_die);
10667 item_type = TREE_TYPE (type);
10669 else if (is_base_type (type))
10670 mod_type_die = base_type_die (type);
10671 else
10673 gen_type_die (type, context_die);
10675 /* We have to get the type_main_variant here (and pass that to the
10676 `lookup_type_die' routine) because the ..._TYPE node we have
10677 might simply be a *copy* of some original type node (where the
10678 copy was created to help us keep track of typedef names) and
10679 that copy might have a different TYPE_UID from the original
10680 ..._TYPE node. */
10681 if (TREE_CODE (type) != VECTOR_TYPE)
10682 return lookup_type_die (type_main_variant (type));
10683 else
10684 /* Vectors have the debugging information in the type,
10685 not the main variant. */
10686 return lookup_type_die (type);
10689 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10690 don't output a DW_TAG_typedef, since there isn't one in the
10691 user's program; just attach a DW_AT_name to the type.
10692 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10693 if the base type already has the same name. */
10694 if (name
10695 && ((TREE_CODE (name) != TYPE_DECL
10696 && (qualified_type == TYPE_MAIN_VARIANT (type)
10697 || (cv_quals == TYPE_UNQUALIFIED)))
10698 || (TREE_CODE (name) == TYPE_DECL
10699 && TREE_TYPE (name) == qualified_type
10700 && DECL_NAME (name))))
10702 if (TREE_CODE (name) == TYPE_DECL)
10703 /* Could just call add_name_and_src_coords_attributes here,
10704 but since this is a builtin type it doesn't have any
10705 useful source coordinates anyway. */
10706 name = DECL_NAME (name);
10707 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10709 /* This probably indicates a bug. */
10710 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10712 name = TYPE_IDENTIFIER (type);
10713 add_name_attribute (mod_type_die,
10714 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10717 if (qualified_type)
10718 equate_type_number_to_die (qualified_type, mod_type_die);
10720 if (item_type)
10721 /* We must do this after the equate_type_number_to_die call, in case
10722 this is a recursive type. This ensures that the modified_type_die
10723 recursion will terminate even if the type is recursive. Recursive
10724 types are possible in Ada. */
10725 sub_die = modified_type_die (item_type,
10726 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10727 context_die);
10729 if (sub_die != NULL)
10730 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10732 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10733 if (TYPE_ARTIFICIAL (type))
10734 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10736 return mod_type_die;
10739 /* Generate DIEs for the generic parameters of T.
10740 T must be either a generic type or a generic function.
10741 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10743 static void
10744 gen_generic_params_dies (tree t)
10746 tree parms, args;
10747 int parms_num, i;
10748 dw_die_ref die = NULL;
10749 int non_default;
10751 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10752 return;
10754 if (TYPE_P (t))
10755 die = lookup_type_die (t);
10756 else if (DECL_P (t))
10757 die = lookup_decl_die (t);
10759 gcc_assert (die);
10761 parms = lang_hooks.get_innermost_generic_parms (t);
10762 if (!parms)
10763 /* T has no generic parameter. It means T is neither a generic type
10764 or function. End of story. */
10765 return;
10767 parms_num = TREE_VEC_LENGTH (parms);
10768 args = lang_hooks.get_innermost_generic_args (t);
10769 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10770 non_default = int_cst_value (TREE_CHAIN (args));
10771 else
10772 non_default = TREE_VEC_LENGTH (args);
10773 for (i = 0; i < parms_num; i++)
10775 tree parm, arg, arg_pack_elems;
10776 dw_die_ref parm_die;
10778 parm = TREE_VEC_ELT (parms, i);
10779 arg = TREE_VEC_ELT (args, i);
10780 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10781 gcc_assert (parm && TREE_VALUE (parm) && arg);
10783 if (parm && TREE_VALUE (parm) && arg)
10785 /* If PARM represents a template parameter pack,
10786 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10787 by DW_TAG_template_*_parameter DIEs for the argument
10788 pack elements of ARG. Note that ARG would then be
10789 an argument pack. */
10790 if (arg_pack_elems)
10791 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10792 arg_pack_elems,
10793 die);
10794 else
10795 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10796 true /* emit name */, die);
10797 if (i >= non_default)
10798 add_AT_flag (parm_die, DW_AT_default_value, 1);
10803 /* Create and return a DIE for PARM which should be
10804 the representation of a generic type parameter.
10805 For instance, in the C++ front end, PARM would be a template parameter.
10806 ARG is the argument to PARM.
10807 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10808 name of the PARM.
10809 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10810 as a child node. */
10812 static dw_die_ref
10813 generic_parameter_die (tree parm, tree arg,
10814 bool emit_name_p,
10815 dw_die_ref parent_die)
10817 dw_die_ref tmpl_die = NULL;
10818 const char *name = NULL;
10820 if (!parm || !DECL_NAME (parm) || !arg)
10821 return NULL;
10823 /* We support non-type generic parameters and arguments,
10824 type generic parameters and arguments, as well as
10825 generic generic parameters (a.k.a. template template parameters in C++)
10826 and arguments. */
10827 if (TREE_CODE (parm) == PARM_DECL)
10828 /* PARM is a nontype generic parameter */
10829 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10830 else if (TREE_CODE (parm) == TYPE_DECL)
10831 /* PARM is a type generic parameter. */
10832 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10833 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10834 /* PARM is a generic generic parameter.
10835 Its DIE is a GNU extension. It shall have a
10836 DW_AT_name attribute to represent the name of the template template
10837 parameter, and a DW_AT_GNU_template_name attribute to represent the
10838 name of the template template argument. */
10839 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10840 parent_die, parm);
10841 else
10842 gcc_unreachable ();
10844 if (tmpl_die)
10846 tree tmpl_type;
10848 /* If PARM is a generic parameter pack, it means we are
10849 emitting debug info for a template argument pack element.
10850 In other terms, ARG is a template argument pack element.
10851 In that case, we don't emit any DW_AT_name attribute for
10852 the die. */
10853 if (emit_name_p)
10855 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10856 gcc_assert (name);
10857 add_AT_string (tmpl_die, DW_AT_name, name);
10860 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10862 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10863 TMPL_DIE should have a child DW_AT_type attribute that is set
10864 to the type of the argument to PARM, which is ARG.
10865 If PARM is a type generic parameter, TMPL_DIE should have a
10866 child DW_AT_type that is set to ARG. */
10867 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10868 add_type_attribute (tmpl_die, tmpl_type,
10869 (TREE_THIS_VOLATILE (tmpl_type)
10870 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10871 parent_die);
10873 else
10875 /* So TMPL_DIE is a DIE representing a
10876 a generic generic template parameter, a.k.a template template
10877 parameter in C++ and arg is a template. */
10879 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10880 to the name of the argument. */
10881 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10882 if (name)
10883 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10886 if (TREE_CODE (parm) == PARM_DECL)
10887 /* So PARM is a non-type generic parameter.
10888 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10889 attribute of TMPL_DIE which value represents the value
10890 of ARG.
10891 We must be careful here:
10892 The value of ARG might reference some function decls.
10893 We might currently be emitting debug info for a generic
10894 type and types are emitted before function decls, we don't
10895 know if the function decls referenced by ARG will actually be
10896 emitted after cgraph computations.
10897 So must defer the generation of the DW_AT_const_value to
10898 after cgraph is ready. */
10899 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10902 return tmpl_die;
10905 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10906 PARM_PACK must be a template parameter pack. The returned DIE
10907 will be child DIE of PARENT_DIE. */
10909 static dw_die_ref
10910 template_parameter_pack_die (tree parm_pack,
10911 tree parm_pack_args,
10912 dw_die_ref parent_die)
10914 dw_die_ref die;
10915 int j;
10917 gcc_assert (parent_die && parm_pack);
10919 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10920 add_name_and_src_coords_attributes (die, parm_pack);
10921 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10922 generic_parameter_die (parm_pack,
10923 TREE_VEC_ELT (parm_pack_args, j),
10924 false /* Don't emit DW_AT_name */,
10925 die);
10926 return die;
10929 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10930 an enumerated type. */
10932 static inline int
10933 type_is_enum (const_tree type)
10935 return TREE_CODE (type) == ENUMERAL_TYPE;
10938 /* Return the DBX register number described by a given RTL node. */
10940 static unsigned int
10941 dbx_reg_number (const_rtx rtl)
10943 unsigned regno = REGNO (rtl);
10945 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10947 #ifdef LEAF_REG_REMAP
10948 if (crtl->uses_only_leaf_regs)
10950 int leaf_reg = LEAF_REG_REMAP (regno);
10951 if (leaf_reg != -1)
10952 regno = (unsigned) leaf_reg;
10954 #endif
10956 regno = DBX_REGISTER_NUMBER (regno);
10957 gcc_assert (regno != INVALID_REGNUM);
10958 return regno;
10961 /* Optionally add a DW_OP_piece term to a location description expression.
10962 DW_OP_piece is only added if the location description expression already
10963 doesn't end with DW_OP_piece. */
10965 static void
10966 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10968 dw_loc_descr_ref loc;
10970 if (*list_head != NULL)
10972 /* Find the end of the chain. */
10973 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10976 if (loc->dw_loc_opc != DW_OP_piece)
10977 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10981 /* Return a location descriptor that designates a machine register or
10982 zero if there is none. */
10984 static dw_loc_descr_ref
10985 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10987 rtx regs;
10989 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10990 return 0;
10992 /* We only use "frame base" when we're sure we're talking about the
10993 post-prologue local stack frame. We do this by *not* running
10994 register elimination until this point, and recognizing the special
10995 argument pointer and soft frame pointer rtx's.
10996 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10997 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10998 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11000 dw_loc_descr_ref result = NULL;
11002 if (dwarf_version >= 4 || !dwarf_strict)
11004 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11005 initialized);
11006 if (result)
11007 add_loc_descr (&result,
11008 new_loc_descr (DW_OP_stack_value, 0, 0));
11010 return result;
11013 regs = targetm.dwarf_register_span (rtl);
11015 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11016 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11017 else
11019 unsigned int dbx_regnum = dbx_reg_number (rtl);
11020 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11021 return 0;
11022 return one_reg_loc_descriptor (dbx_regnum, initialized);
11026 /* Return a location descriptor that designates a machine register for
11027 a given hard register number. */
11029 static dw_loc_descr_ref
11030 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11032 dw_loc_descr_ref reg_loc_descr;
11034 if (regno <= 31)
11035 reg_loc_descr
11036 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11037 else
11038 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11040 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11041 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11043 return reg_loc_descr;
11046 /* Given an RTL of a register, return a location descriptor that
11047 designates a value that spans more than one register. */
11049 static dw_loc_descr_ref
11050 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11051 enum var_init_status initialized)
11053 int size, i;
11054 dw_loc_descr_ref loc_result = NULL;
11056 /* Simple, contiguous registers. */
11057 if (regs == NULL_RTX)
11059 unsigned reg = REGNO (rtl);
11060 int nregs;
11062 #ifdef LEAF_REG_REMAP
11063 if (crtl->uses_only_leaf_regs)
11065 int leaf_reg = LEAF_REG_REMAP (reg);
11066 if (leaf_reg != -1)
11067 reg = (unsigned) leaf_reg;
11069 #endif
11071 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11072 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11074 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11076 loc_result = NULL;
11077 while (nregs--)
11079 dw_loc_descr_ref t;
11081 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11082 VAR_INIT_STATUS_INITIALIZED);
11083 add_loc_descr (&loc_result, t);
11084 add_loc_descr_op_piece (&loc_result, size);
11085 ++reg;
11087 return loc_result;
11090 /* Now onto stupid register sets in non contiguous locations. */
11092 gcc_assert (GET_CODE (regs) == PARALLEL);
11094 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11095 loc_result = NULL;
11097 for (i = 0; i < XVECLEN (regs, 0); ++i)
11099 dw_loc_descr_ref t;
11101 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11102 VAR_INIT_STATUS_INITIALIZED);
11103 add_loc_descr (&loc_result, t);
11104 add_loc_descr_op_piece (&loc_result, size);
11107 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11108 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11109 return loc_result;
11112 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11114 /* Return a location descriptor that designates a constant i,
11115 as a compound operation from constant (i >> shift), constant shift
11116 and DW_OP_shl. */
11118 static dw_loc_descr_ref
11119 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11121 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11122 add_loc_descr (&ret, int_loc_descriptor (shift));
11123 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11124 return ret;
11127 /* Return a location descriptor that designates a constant. */
11129 static dw_loc_descr_ref
11130 int_loc_descriptor (HOST_WIDE_INT i)
11132 enum dwarf_location_atom op;
11134 /* Pick the smallest representation of a constant, rather than just
11135 defaulting to the LEB encoding. */
11136 if (i >= 0)
11138 int clz = clz_hwi (i);
11139 int ctz = ctz_hwi (i);
11140 if (i <= 31)
11141 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11142 else if (i <= 0xff)
11143 op = DW_OP_const1u;
11144 else if (i <= 0xffff)
11145 op = DW_OP_const2u;
11146 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11147 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11148 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11149 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11150 while DW_OP_const4u is 5 bytes. */
11151 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11152 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11153 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11154 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11155 while DW_OP_const4u is 5 bytes. */
11156 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11157 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11158 op = DW_OP_const4u;
11159 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11160 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11161 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11162 while DW_OP_constu of constant >= 0x100000000 takes at least
11163 6 bytes. */
11164 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11165 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11166 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11167 >= HOST_BITS_PER_WIDE_INT)
11168 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11169 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11170 while DW_OP_constu takes in this case at least 6 bytes. */
11171 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11172 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11173 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11174 && size_of_uleb128 (i) > 6)
11175 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11176 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11177 else
11178 op = DW_OP_constu;
11180 else
11182 if (i >= -0x80)
11183 op = DW_OP_const1s;
11184 else if (i >= -0x8000)
11185 op = DW_OP_const2s;
11186 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11188 if (size_of_int_loc_descriptor (i) < 5)
11190 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11191 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11192 return ret;
11194 op = DW_OP_const4s;
11196 else
11198 if (size_of_int_loc_descriptor (i)
11199 < (unsigned long) 1 + size_of_sleb128 (i))
11201 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11202 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11203 return ret;
11205 op = DW_OP_consts;
11209 return new_loc_descr (op, i, 0);
11212 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11213 without actually allocating it. */
11215 static unsigned long
11216 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11218 return size_of_int_loc_descriptor (i >> shift)
11219 + size_of_int_loc_descriptor (shift)
11220 + 1;
11223 /* Return size_of_locs (int_loc_descriptor (i)) without
11224 actually allocating it. */
11226 static unsigned long
11227 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11229 unsigned long s;
11231 if (i >= 0)
11233 int clz, ctz;
11234 if (i <= 31)
11235 return 1;
11236 else if (i <= 0xff)
11237 return 2;
11238 else if (i <= 0xffff)
11239 return 3;
11240 clz = clz_hwi (i);
11241 ctz = ctz_hwi (i);
11242 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11243 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11244 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11245 - clz - 5);
11246 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11247 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11248 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11249 - clz - 8);
11250 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11251 return 5;
11252 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11253 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11254 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11255 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11256 - clz - 8);
11257 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11258 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11259 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11260 - clz - 16);
11261 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11262 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11263 && s > 6)
11264 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11265 - clz - 32);
11266 else
11267 return 1 + s;
11269 else
11271 if (i >= -0x80)
11272 return 2;
11273 else if (i >= -0x8000)
11274 return 3;
11275 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11277 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11279 s = size_of_int_loc_descriptor (-i) + 1;
11280 if (s < 5)
11281 return s;
11283 return 5;
11285 else
11287 unsigned long r = 1 + size_of_sleb128 (i);
11288 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11290 s = size_of_int_loc_descriptor (-i) + 1;
11291 if (s < r)
11292 return s;
11294 return r;
11299 /* Return loc description representing "address" of integer value.
11300 This can appear only as toplevel expression. */
11302 static dw_loc_descr_ref
11303 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11305 int litsize;
11306 dw_loc_descr_ref loc_result = NULL;
11308 if (!(dwarf_version >= 4 || !dwarf_strict))
11309 return NULL;
11311 litsize = size_of_int_loc_descriptor (i);
11312 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11313 is more compact. For DW_OP_stack_value we need:
11314 litsize + 1 (DW_OP_stack_value)
11315 and for DW_OP_implicit_value:
11316 1 (DW_OP_implicit_value) + 1 (length) + size. */
11317 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11319 loc_result = int_loc_descriptor (i);
11320 add_loc_descr (&loc_result,
11321 new_loc_descr (DW_OP_stack_value, 0, 0));
11322 return loc_result;
11325 loc_result = new_loc_descr (DW_OP_implicit_value,
11326 size, 0);
11327 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11328 loc_result->dw_loc_oprnd2.v.val_int = i;
11329 return loc_result;
11332 /* Return a location descriptor that designates a base+offset location. */
11334 static dw_loc_descr_ref
11335 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11336 enum var_init_status initialized)
11338 unsigned int regno;
11339 dw_loc_descr_ref result;
11340 dw_fde_ref fde = cfun->fde;
11342 /* We only use "frame base" when we're sure we're talking about the
11343 post-prologue local stack frame. We do this by *not* running
11344 register elimination until this point, and recognizing the special
11345 argument pointer and soft frame pointer rtx's. */
11346 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11348 rtx elim = (ira_use_lra_p
11349 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11350 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11352 if (elim != reg)
11354 if (GET_CODE (elim) == PLUS)
11356 offset += INTVAL (XEXP (elim, 1));
11357 elim = XEXP (elim, 0);
11359 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11360 && (elim == hard_frame_pointer_rtx
11361 || elim == stack_pointer_rtx))
11362 || elim == (frame_pointer_needed
11363 ? hard_frame_pointer_rtx
11364 : stack_pointer_rtx));
11366 /* If drap register is used to align stack, use frame
11367 pointer + offset to access stack variables. If stack
11368 is aligned without drap, use stack pointer + offset to
11369 access stack variables. */
11370 if (crtl->stack_realign_tried
11371 && reg == frame_pointer_rtx)
11373 int base_reg
11374 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11375 ? HARD_FRAME_POINTER_REGNUM
11376 : REGNO (elim));
11377 return new_reg_loc_descr (base_reg, offset);
11380 gcc_assert (frame_pointer_fb_offset_valid);
11381 offset += frame_pointer_fb_offset;
11382 return new_loc_descr (DW_OP_fbreg, offset, 0);
11386 regno = REGNO (reg);
11387 #ifdef LEAF_REG_REMAP
11388 if (crtl->uses_only_leaf_regs)
11390 int leaf_reg = LEAF_REG_REMAP (regno);
11391 if (leaf_reg != -1)
11392 regno = (unsigned) leaf_reg;
11394 #endif
11395 regno = DWARF_FRAME_REGNUM (regno);
11397 if (!optimize && fde
11398 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11400 /* Use cfa+offset to represent the location of arguments passed
11401 on the stack when drap is used to align stack.
11402 Only do this when not optimizing, for optimized code var-tracking
11403 is supposed to track where the arguments live and the register
11404 used as vdrap or drap in some spot might be used for something
11405 else in other part of the routine. */
11406 return new_loc_descr (DW_OP_fbreg, offset, 0);
11409 if (regno <= 31)
11410 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11411 offset, 0);
11412 else
11413 result = new_loc_descr (DW_OP_bregx, regno, offset);
11415 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11416 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11418 return result;
11421 /* Return true if this RTL expression describes a base+offset calculation. */
11423 static inline int
11424 is_based_loc (const_rtx rtl)
11426 return (GET_CODE (rtl) == PLUS
11427 && ((REG_P (XEXP (rtl, 0))
11428 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11429 && CONST_INT_P (XEXP (rtl, 1)))));
11432 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11433 failed. */
11435 static dw_loc_descr_ref
11436 tls_mem_loc_descriptor (rtx mem)
11438 tree base;
11439 dw_loc_descr_ref loc_result;
11441 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11442 return NULL;
11444 base = get_base_address (MEM_EXPR (mem));
11445 if (base == NULL
11446 || TREE_CODE (base) != VAR_DECL
11447 || !DECL_THREAD_LOCAL_P (base))
11448 return NULL;
11450 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11451 if (loc_result == NULL)
11452 return NULL;
11454 if (MEM_OFFSET (mem))
11455 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11457 return loc_result;
11460 /* Output debug info about reason why we failed to expand expression as dwarf
11461 expression. */
11463 static void
11464 expansion_failed (tree expr, rtx rtl, char const *reason)
11466 if (dump_file && (dump_flags & TDF_DETAILS))
11468 fprintf (dump_file, "Failed to expand as dwarf: ");
11469 if (expr)
11470 print_generic_expr (dump_file, expr, dump_flags);
11471 if (rtl)
11473 fprintf (dump_file, "\n");
11474 print_rtl (dump_file, rtl);
11476 fprintf (dump_file, "\nReason: %s\n", reason);
11480 /* Helper function for const_ok_for_output. */
11482 static bool
11483 const_ok_for_output_1 (rtx rtl)
11485 if (GET_CODE (rtl) == UNSPEC)
11487 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11488 we can't express it in the debug info. */
11489 #ifdef ENABLE_CHECKING
11490 /* Don't complain about TLS UNSPECs, those are just too hard to
11491 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11492 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11493 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11494 if (XVECLEN (rtl, 0) == 0
11495 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11496 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11497 inform (current_function_decl
11498 ? DECL_SOURCE_LOCATION (current_function_decl)
11499 : UNKNOWN_LOCATION,
11500 #if NUM_UNSPEC_VALUES > 0
11501 "non-delegitimized UNSPEC %s (%d) found in variable location",
11502 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11503 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11504 XINT (rtl, 1));
11505 #else
11506 "non-delegitimized UNSPEC %d found in variable location",
11507 XINT (rtl, 1));
11508 #endif
11509 #endif
11510 expansion_failed (NULL_TREE, rtl,
11511 "UNSPEC hasn't been delegitimized.\n");
11512 return false;
11515 if (targetm.const_not_ok_for_debug_p (rtl))
11517 expansion_failed (NULL_TREE, rtl,
11518 "Expression rejected for debug by the backend.\n");
11519 return false;
11522 /* FIXME: Refer to PR60655. It is possible for simplification
11523 of rtl expressions in var tracking to produce such expressions.
11524 We should really identify / validate expressions
11525 enclosed in CONST that can be handled by assemblers on various
11526 targets and only handle legitimate cases here. */
11527 if (GET_CODE (rtl) != SYMBOL_REF)
11529 if (GET_CODE (rtl) == NOT)
11530 return false;
11531 return true;
11534 if (CONSTANT_POOL_ADDRESS_P (rtl))
11536 bool marked;
11537 get_pool_constant_mark (rtl, &marked);
11538 /* If all references to this pool constant were optimized away,
11539 it was not output and thus we can't represent it. */
11540 if (!marked)
11542 expansion_failed (NULL_TREE, rtl,
11543 "Constant was removed from constant pool.\n");
11544 return false;
11548 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11549 return false;
11551 /* Avoid references to external symbols in debug info, on several targets
11552 the linker might even refuse to link when linking a shared library,
11553 and in many other cases the relocations for .debug_info/.debug_loc are
11554 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11555 to be defined within the same shared library or executable are fine. */
11556 if (SYMBOL_REF_EXTERNAL_P (rtl))
11558 tree decl = SYMBOL_REF_DECL (rtl);
11560 if (decl == NULL || !targetm.binds_local_p (decl))
11562 expansion_failed (NULL_TREE, rtl,
11563 "Symbol not defined in current TU.\n");
11564 return false;
11568 return true;
11571 /* Return true if constant RTL can be emitted in DW_OP_addr or
11572 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11573 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11575 static bool
11576 const_ok_for_output (rtx rtl)
11578 if (GET_CODE (rtl) == SYMBOL_REF)
11579 return const_ok_for_output_1 (rtl);
11581 if (GET_CODE (rtl) == CONST)
11583 subrtx_var_iterator::array_type array;
11584 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11585 if (!const_ok_for_output_1 (*iter))
11586 return false;
11587 return true;
11590 return true;
11593 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11594 if possible, NULL otherwise. */
11596 static dw_die_ref
11597 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11599 dw_die_ref type_die;
11600 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11602 if (type == NULL)
11603 return NULL;
11604 switch (TREE_CODE (type))
11606 case INTEGER_TYPE:
11607 case REAL_TYPE:
11608 break;
11609 default:
11610 return NULL;
11612 type_die = lookup_type_die (type);
11613 if (!type_die)
11614 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11615 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11616 return NULL;
11617 return type_die;
11620 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11621 type matching MODE, or, if MODE is narrower than or as wide as
11622 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11623 possible. */
11625 static dw_loc_descr_ref
11626 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11628 enum machine_mode outer_mode = mode;
11629 dw_die_ref type_die;
11630 dw_loc_descr_ref cvt;
11632 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11634 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11635 return op;
11637 type_die = base_type_for_mode (outer_mode, 1);
11638 if (type_die == NULL)
11639 return NULL;
11640 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11641 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11642 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11643 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11644 add_loc_descr (&op, cvt);
11645 return op;
11648 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11650 static dw_loc_descr_ref
11651 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11652 dw_loc_descr_ref op1)
11654 dw_loc_descr_ref ret = op0;
11655 add_loc_descr (&ret, op1);
11656 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11657 if (STORE_FLAG_VALUE != 1)
11659 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11660 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11662 return ret;
11665 /* Return location descriptor for signed comparison OP RTL. */
11667 static dw_loc_descr_ref
11668 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11669 enum machine_mode mem_mode)
11671 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11672 dw_loc_descr_ref op0, op1;
11673 int shift;
11675 if (op_mode == VOIDmode)
11676 op_mode = GET_MODE (XEXP (rtl, 1));
11677 if (op_mode == VOIDmode)
11678 return NULL;
11680 if (dwarf_strict
11681 && (GET_MODE_CLASS (op_mode) != MODE_INT
11682 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11683 return NULL;
11685 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11686 VAR_INIT_STATUS_INITIALIZED);
11687 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11688 VAR_INIT_STATUS_INITIALIZED);
11690 if (op0 == NULL || op1 == NULL)
11691 return NULL;
11693 if (GET_MODE_CLASS (op_mode) != MODE_INT
11694 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11695 return compare_loc_descriptor (op, op0, op1);
11697 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11699 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11700 dw_loc_descr_ref cvt;
11702 if (type_die == NULL)
11703 return NULL;
11704 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11705 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11706 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11707 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11708 add_loc_descr (&op0, cvt);
11709 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11710 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11711 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11712 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11713 add_loc_descr (&op1, cvt);
11714 return compare_loc_descriptor (op, op0, op1);
11717 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11718 /* For eq/ne, if the operands are known to be zero-extended,
11719 there is no need to do the fancy shifting up. */
11720 if (op == DW_OP_eq || op == DW_OP_ne)
11722 dw_loc_descr_ref last0, last1;
11723 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11725 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11727 /* deref_size zero extends, and for constants we can check
11728 whether they are zero extended or not. */
11729 if (((last0->dw_loc_opc == DW_OP_deref_size
11730 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11731 || (CONST_INT_P (XEXP (rtl, 0))
11732 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11733 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11734 && ((last1->dw_loc_opc == DW_OP_deref_size
11735 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11736 || (CONST_INT_P (XEXP (rtl, 1))
11737 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11738 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11739 return compare_loc_descriptor (op, op0, op1);
11741 /* EQ/NE comparison against constant in narrower type than
11742 DWARF2_ADDR_SIZE can be performed either as
11743 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11744 DW_OP_{eq,ne}
11746 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11747 DW_OP_{eq,ne}. Pick whatever is shorter. */
11748 if (CONST_INT_P (XEXP (rtl, 1))
11749 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11750 && (size_of_int_loc_descriptor (shift) + 1
11751 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11752 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11753 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11754 & GET_MODE_MASK (op_mode))))
11756 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11757 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11758 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11759 & GET_MODE_MASK (op_mode));
11760 return compare_loc_descriptor (op, op0, op1);
11763 add_loc_descr (&op0, int_loc_descriptor (shift));
11764 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11765 if (CONST_INT_P (XEXP (rtl, 1)))
11766 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11767 else
11769 add_loc_descr (&op1, int_loc_descriptor (shift));
11770 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11772 return compare_loc_descriptor (op, op0, op1);
11775 /* Return location descriptor for unsigned comparison OP RTL. */
11777 static dw_loc_descr_ref
11778 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11779 enum machine_mode mem_mode)
11781 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11782 dw_loc_descr_ref op0, op1;
11784 if (op_mode == VOIDmode)
11785 op_mode = GET_MODE (XEXP (rtl, 1));
11786 if (op_mode == VOIDmode)
11787 return NULL;
11788 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11789 return NULL;
11791 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11792 return NULL;
11794 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11795 VAR_INIT_STATUS_INITIALIZED);
11796 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11797 VAR_INIT_STATUS_INITIALIZED);
11799 if (op0 == NULL || op1 == NULL)
11800 return NULL;
11802 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11804 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11805 dw_loc_descr_ref last0, last1;
11806 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11808 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11810 if (CONST_INT_P (XEXP (rtl, 0)))
11811 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11812 /* deref_size zero extends, so no need to mask it again. */
11813 else if (last0->dw_loc_opc != DW_OP_deref_size
11814 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11816 add_loc_descr (&op0, int_loc_descriptor (mask));
11817 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11819 if (CONST_INT_P (XEXP (rtl, 1)))
11820 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11821 /* deref_size zero extends, so no need to mask it again. */
11822 else if (last1->dw_loc_opc != DW_OP_deref_size
11823 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11825 add_loc_descr (&op1, int_loc_descriptor (mask));
11826 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11829 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11831 HOST_WIDE_INT bias = 1;
11832 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11833 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11834 if (CONST_INT_P (XEXP (rtl, 1)))
11835 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11836 + INTVAL (XEXP (rtl, 1)));
11837 else
11838 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11839 bias, 0));
11841 return compare_loc_descriptor (op, op0, op1);
11844 /* Return location descriptor for {U,S}{MIN,MAX}. */
11846 static dw_loc_descr_ref
11847 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11848 enum machine_mode mem_mode)
11850 enum dwarf_location_atom op;
11851 dw_loc_descr_ref op0, op1, ret;
11852 dw_loc_descr_ref bra_node, drop_node;
11854 if (dwarf_strict
11855 && (GET_MODE_CLASS (mode) != MODE_INT
11856 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11857 return NULL;
11859 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11860 VAR_INIT_STATUS_INITIALIZED);
11861 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11862 VAR_INIT_STATUS_INITIALIZED);
11864 if (op0 == NULL || op1 == NULL)
11865 return NULL;
11867 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11868 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11869 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11870 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11872 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11874 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11875 add_loc_descr (&op0, int_loc_descriptor (mask));
11876 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11877 add_loc_descr (&op1, int_loc_descriptor (mask));
11878 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11880 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11882 HOST_WIDE_INT bias = 1;
11883 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11884 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11885 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11888 else if (GET_MODE_CLASS (mode) == MODE_INT
11889 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11891 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11892 add_loc_descr (&op0, int_loc_descriptor (shift));
11893 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11894 add_loc_descr (&op1, int_loc_descriptor (shift));
11895 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11897 else if (GET_MODE_CLASS (mode) == MODE_INT
11898 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11900 dw_die_ref type_die = base_type_for_mode (mode, 0);
11901 dw_loc_descr_ref cvt;
11902 if (type_die == NULL)
11903 return NULL;
11904 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11905 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11906 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11907 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11908 add_loc_descr (&op0, cvt);
11909 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11910 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11911 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11912 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11913 add_loc_descr (&op1, cvt);
11916 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11917 op = DW_OP_lt;
11918 else
11919 op = DW_OP_gt;
11920 ret = op0;
11921 add_loc_descr (&ret, op1);
11922 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11923 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11924 add_loc_descr (&ret, bra_node);
11925 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11926 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11927 add_loc_descr (&ret, drop_node);
11928 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11929 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11930 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11931 && GET_MODE_CLASS (mode) == MODE_INT
11932 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11933 ret = convert_descriptor_to_mode (mode, ret);
11934 return ret;
11937 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11938 but after converting arguments to type_die, afterwards
11939 convert back to unsigned. */
11941 static dw_loc_descr_ref
11942 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11943 enum machine_mode mode, enum machine_mode mem_mode)
11945 dw_loc_descr_ref cvt, op0, op1;
11947 if (type_die == NULL)
11948 return NULL;
11949 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11950 VAR_INIT_STATUS_INITIALIZED);
11951 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11952 VAR_INIT_STATUS_INITIALIZED);
11953 if (op0 == NULL || op1 == NULL)
11954 return NULL;
11955 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11956 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11957 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11958 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11959 add_loc_descr (&op0, cvt);
11960 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11961 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11962 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11963 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11964 add_loc_descr (&op1, cvt);
11965 add_loc_descr (&op0, op1);
11966 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11967 return convert_descriptor_to_mode (mode, op0);
11970 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11971 const0 is DW_OP_lit0 or corresponding typed constant,
11972 const1 is DW_OP_lit1 or corresponding typed constant
11973 and constMSB is constant with just the MSB bit set
11974 for the mode):
11975 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11976 L1: const0 DW_OP_swap
11977 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11978 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11979 L3: DW_OP_drop
11980 L4: DW_OP_nop
11982 CTZ is similar:
11983 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11984 L1: const0 DW_OP_swap
11985 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11986 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11987 L3: DW_OP_drop
11988 L4: DW_OP_nop
11990 FFS is similar:
11991 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11992 L1: const1 DW_OP_swap
11993 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11994 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11995 L3: DW_OP_drop
11996 L4: DW_OP_nop */
11998 static dw_loc_descr_ref
11999 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
12000 enum machine_mode mem_mode)
12002 dw_loc_descr_ref op0, ret, tmp;
12003 HOST_WIDE_INT valv;
12004 dw_loc_descr_ref l1jump, l1label;
12005 dw_loc_descr_ref l2jump, l2label;
12006 dw_loc_descr_ref l3jump, l3label;
12007 dw_loc_descr_ref l4jump, l4label;
12008 rtx msb;
12010 if (GET_MODE_CLASS (mode) != MODE_INT
12011 || GET_MODE (XEXP (rtl, 0)) != mode)
12012 return NULL;
12014 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12015 VAR_INIT_STATUS_INITIALIZED);
12016 if (op0 == NULL)
12017 return NULL;
12018 ret = op0;
12019 if (GET_CODE (rtl) == CLZ)
12021 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12022 valv = GET_MODE_BITSIZE (mode);
12024 else if (GET_CODE (rtl) == FFS)
12025 valv = 0;
12026 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12027 valv = GET_MODE_BITSIZE (mode);
12028 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12029 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12030 add_loc_descr (&ret, l1jump);
12031 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12032 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12033 VAR_INIT_STATUS_INITIALIZED);
12034 if (tmp == NULL)
12035 return NULL;
12036 add_loc_descr (&ret, tmp);
12037 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12038 add_loc_descr (&ret, l4jump);
12039 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12040 ? const1_rtx : const0_rtx,
12041 mode, mem_mode,
12042 VAR_INIT_STATUS_INITIALIZED);
12043 if (l1label == NULL)
12044 return NULL;
12045 add_loc_descr (&ret, l1label);
12046 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12047 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12048 add_loc_descr (&ret, l2label);
12049 if (GET_CODE (rtl) != CLZ)
12050 msb = const1_rtx;
12051 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12052 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12053 << (GET_MODE_BITSIZE (mode) - 1));
12054 else
12055 msb = immed_wide_int_const
12056 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12057 GET_MODE_PRECISION (mode)), mode);
12058 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12059 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12060 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12061 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12062 else
12063 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12064 VAR_INIT_STATUS_INITIALIZED);
12065 if (tmp == NULL)
12066 return NULL;
12067 add_loc_descr (&ret, tmp);
12068 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12069 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12070 add_loc_descr (&ret, l3jump);
12071 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12072 VAR_INIT_STATUS_INITIALIZED);
12073 if (tmp == NULL)
12074 return NULL;
12075 add_loc_descr (&ret, tmp);
12076 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12077 ? DW_OP_shl : DW_OP_shr, 0, 0));
12078 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12079 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12080 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12081 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12082 add_loc_descr (&ret, l2jump);
12083 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12084 add_loc_descr (&ret, l3label);
12085 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12086 add_loc_descr (&ret, l4label);
12087 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12088 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12089 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12090 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12091 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12092 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12093 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12094 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12095 return ret;
12098 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12099 const1 is DW_OP_lit1 or corresponding typed constant):
12100 const0 DW_OP_swap
12101 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12102 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12103 L2: DW_OP_drop
12105 PARITY is similar:
12106 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12107 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12108 L2: DW_OP_drop */
12110 static dw_loc_descr_ref
12111 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
12112 enum machine_mode mem_mode)
12114 dw_loc_descr_ref op0, ret, tmp;
12115 dw_loc_descr_ref l1jump, l1label;
12116 dw_loc_descr_ref l2jump, l2label;
12118 if (GET_MODE_CLASS (mode) != MODE_INT
12119 || GET_MODE (XEXP (rtl, 0)) != mode)
12120 return NULL;
12122 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12123 VAR_INIT_STATUS_INITIALIZED);
12124 if (op0 == NULL)
12125 return NULL;
12126 ret = op0;
12127 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12128 VAR_INIT_STATUS_INITIALIZED);
12129 if (tmp == NULL)
12130 return NULL;
12131 add_loc_descr (&ret, tmp);
12132 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12133 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12134 add_loc_descr (&ret, l1label);
12135 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12136 add_loc_descr (&ret, l2jump);
12137 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12138 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12139 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12140 VAR_INIT_STATUS_INITIALIZED);
12141 if (tmp == NULL)
12142 return NULL;
12143 add_loc_descr (&ret, tmp);
12144 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12145 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12146 ? DW_OP_plus : DW_OP_xor, 0, 0));
12147 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12148 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12149 VAR_INIT_STATUS_INITIALIZED);
12150 add_loc_descr (&ret, tmp);
12151 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12152 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12153 add_loc_descr (&ret, l1jump);
12154 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12155 add_loc_descr (&ret, l2label);
12156 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12157 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12158 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12159 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12160 return ret;
12163 /* BSWAP (constS is initial shift count, either 56 or 24):
12164 constS const0
12165 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12166 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12167 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12168 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12169 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12171 static dw_loc_descr_ref
12172 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
12173 enum machine_mode mem_mode)
12175 dw_loc_descr_ref op0, ret, tmp;
12176 dw_loc_descr_ref l1jump, l1label;
12177 dw_loc_descr_ref l2jump, l2label;
12179 if (GET_MODE_CLASS (mode) != MODE_INT
12180 || BITS_PER_UNIT != 8
12181 || (GET_MODE_BITSIZE (mode) != 32
12182 && GET_MODE_BITSIZE (mode) != 64))
12183 return NULL;
12185 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12186 VAR_INIT_STATUS_INITIALIZED);
12187 if (op0 == NULL)
12188 return NULL;
12190 ret = op0;
12191 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12192 mode, mem_mode,
12193 VAR_INIT_STATUS_INITIALIZED);
12194 if (tmp == NULL)
12195 return NULL;
12196 add_loc_descr (&ret, tmp);
12197 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12198 VAR_INIT_STATUS_INITIALIZED);
12199 if (tmp == NULL)
12200 return NULL;
12201 add_loc_descr (&ret, tmp);
12202 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12203 add_loc_descr (&ret, l1label);
12204 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12205 mode, mem_mode,
12206 VAR_INIT_STATUS_INITIALIZED);
12207 add_loc_descr (&ret, tmp);
12208 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12209 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12210 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12211 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12212 VAR_INIT_STATUS_INITIALIZED);
12213 if (tmp == NULL)
12214 return NULL;
12215 add_loc_descr (&ret, tmp);
12216 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12217 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12218 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12219 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12220 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12221 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12222 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12223 VAR_INIT_STATUS_INITIALIZED);
12224 add_loc_descr (&ret, tmp);
12225 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12226 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12227 add_loc_descr (&ret, l2jump);
12228 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12229 VAR_INIT_STATUS_INITIALIZED);
12230 add_loc_descr (&ret, tmp);
12231 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12232 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12233 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12234 add_loc_descr (&ret, l1jump);
12235 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12236 add_loc_descr (&ret, l2label);
12237 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12238 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12239 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12240 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12241 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12242 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12243 return ret;
12246 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12247 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12248 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12249 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12251 ROTATERT is similar:
12252 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12253 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12254 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12256 static dw_loc_descr_ref
12257 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12258 enum machine_mode mem_mode)
12260 rtx rtlop1 = XEXP (rtl, 1);
12261 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12262 int i;
12264 if (GET_MODE_CLASS (mode) != MODE_INT)
12265 return NULL;
12267 if (GET_MODE (rtlop1) != VOIDmode
12268 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12269 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12270 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12271 VAR_INIT_STATUS_INITIALIZED);
12272 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12273 VAR_INIT_STATUS_INITIALIZED);
12274 if (op0 == NULL || op1 == NULL)
12275 return NULL;
12276 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12277 for (i = 0; i < 2; i++)
12279 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12280 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12281 mode, mem_mode,
12282 VAR_INIT_STATUS_INITIALIZED);
12283 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12284 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12285 ? DW_OP_const4u
12286 : HOST_BITS_PER_WIDE_INT == 64
12287 ? DW_OP_const8u : DW_OP_constu,
12288 GET_MODE_MASK (mode), 0);
12289 else
12290 mask[i] = NULL;
12291 if (mask[i] == NULL)
12292 return NULL;
12293 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12295 ret = op0;
12296 add_loc_descr (&ret, op1);
12297 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12298 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12299 if (GET_CODE (rtl) == ROTATERT)
12301 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12302 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12303 GET_MODE_BITSIZE (mode), 0));
12305 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12306 if (mask[0] != NULL)
12307 add_loc_descr (&ret, mask[0]);
12308 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12309 if (mask[1] != NULL)
12311 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12312 add_loc_descr (&ret, mask[1]);
12313 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12315 if (GET_CODE (rtl) == ROTATE)
12317 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12318 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12319 GET_MODE_BITSIZE (mode), 0));
12321 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12322 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12323 return ret;
12326 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12327 for DEBUG_PARAMETER_REF RTL. */
12329 static dw_loc_descr_ref
12330 parameter_ref_descriptor (rtx rtl)
12332 dw_loc_descr_ref ret;
12333 dw_die_ref ref;
12335 if (dwarf_strict)
12336 return NULL;
12337 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12338 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12339 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12340 if (ref)
12342 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12343 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12344 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12346 else
12348 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12349 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12351 return ret;
12354 /* The following routine converts the RTL for a variable or parameter
12355 (resident in memory) into an equivalent Dwarf representation of a
12356 mechanism for getting the address of that same variable onto the top of a
12357 hypothetical "address evaluation" stack.
12359 When creating memory location descriptors, we are effectively transforming
12360 the RTL for a memory-resident object into its Dwarf postfix expression
12361 equivalent. This routine recursively descends an RTL tree, turning
12362 it into Dwarf postfix code as it goes.
12364 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12366 MEM_MODE is the mode of the memory reference, needed to handle some
12367 autoincrement addressing modes.
12369 Return 0 if we can't represent the location. */
12371 dw_loc_descr_ref
12372 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12373 enum machine_mode mem_mode,
12374 enum var_init_status initialized)
12376 dw_loc_descr_ref mem_loc_result = NULL;
12377 enum dwarf_location_atom op;
12378 dw_loc_descr_ref op0, op1;
12379 rtx inner = NULL_RTX;
12381 if (mode == VOIDmode)
12382 mode = GET_MODE (rtl);
12384 /* Note that for a dynamically sized array, the location we will generate a
12385 description of here will be the lowest numbered location which is
12386 actually within the array. That's *not* necessarily the same as the
12387 zeroth element of the array. */
12389 rtl = targetm.delegitimize_address (rtl);
12391 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12392 return NULL;
12394 switch (GET_CODE (rtl))
12396 case POST_INC:
12397 case POST_DEC:
12398 case POST_MODIFY:
12399 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12401 case SUBREG:
12402 /* The case of a subreg may arise when we have a local (register)
12403 variable or a formal (register) parameter which doesn't quite fill
12404 up an entire register. For now, just assume that it is
12405 legitimate to make the Dwarf info refer to the whole register which
12406 contains the given subreg. */
12407 if (!subreg_lowpart_p (rtl))
12408 break;
12409 inner = SUBREG_REG (rtl);
12410 case TRUNCATE:
12411 if (inner == NULL_RTX)
12412 inner = XEXP (rtl, 0);
12413 if (GET_MODE_CLASS (mode) == MODE_INT
12414 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12415 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12416 #ifdef POINTERS_EXTEND_UNSIGNED
12417 || (mode == Pmode && mem_mode != VOIDmode)
12418 #endif
12420 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12422 mem_loc_result = mem_loc_descriptor (inner,
12423 GET_MODE (inner),
12424 mem_mode, initialized);
12425 break;
12427 if (dwarf_strict)
12428 break;
12429 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12430 break;
12431 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12432 && (GET_MODE_CLASS (mode) != MODE_INT
12433 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12434 break;
12435 else
12437 dw_die_ref type_die;
12438 dw_loc_descr_ref cvt;
12440 mem_loc_result = mem_loc_descriptor (inner,
12441 GET_MODE (inner),
12442 mem_mode, initialized);
12443 if (mem_loc_result == NULL)
12444 break;
12445 type_die = base_type_for_mode (mode,
12446 GET_MODE_CLASS (mode) == MODE_INT);
12447 if (type_die == NULL)
12449 mem_loc_result = NULL;
12450 break;
12452 if (GET_MODE_SIZE (mode)
12453 != GET_MODE_SIZE (GET_MODE (inner)))
12454 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12455 else
12456 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12457 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12458 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12459 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12460 add_loc_descr (&mem_loc_result, cvt);
12462 break;
12464 case REG:
12465 if (GET_MODE_CLASS (mode) != MODE_INT
12466 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12467 && rtl != arg_pointer_rtx
12468 && rtl != frame_pointer_rtx
12469 #ifdef POINTERS_EXTEND_UNSIGNED
12470 && (mode != Pmode || mem_mode == VOIDmode)
12471 #endif
12474 dw_die_ref type_die;
12475 unsigned int dbx_regnum;
12477 if (dwarf_strict)
12478 break;
12479 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12480 break;
12481 type_die = base_type_for_mode (mode,
12482 GET_MODE_CLASS (mode) == MODE_INT);
12483 if (type_die == NULL)
12484 break;
12486 dbx_regnum = dbx_reg_number (rtl);
12487 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12488 break;
12489 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12490 dbx_regnum, 0);
12491 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12492 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12493 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12494 break;
12496 /* Whenever a register number forms a part of the description of the
12497 method for calculating the (dynamic) address of a memory resident
12498 object, DWARF rules require the register number be referred to as
12499 a "base register". This distinction is not based in any way upon
12500 what category of register the hardware believes the given register
12501 belongs to. This is strictly DWARF terminology we're dealing with
12502 here. Note that in cases where the location of a memory-resident
12503 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12504 OP_CONST (0)) the actual DWARF location descriptor that we generate
12505 may just be OP_BASEREG (basereg). This may look deceptively like
12506 the object in question was allocated to a register (rather than in
12507 memory) so DWARF consumers need to be aware of the subtle
12508 distinction between OP_REG and OP_BASEREG. */
12509 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12510 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12511 else if (stack_realign_drap
12512 && crtl->drap_reg
12513 && crtl->args.internal_arg_pointer == rtl
12514 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12516 /* If RTL is internal_arg_pointer, which has been optimized
12517 out, use DRAP instead. */
12518 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12519 VAR_INIT_STATUS_INITIALIZED);
12521 break;
12523 case SIGN_EXTEND:
12524 case ZERO_EXTEND:
12525 if (GET_MODE_CLASS (mode) != MODE_INT)
12526 break;
12527 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12528 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12529 if (op0 == 0)
12530 break;
12531 else if (GET_CODE (rtl) == ZERO_EXTEND
12532 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12533 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12534 < HOST_BITS_PER_WIDE_INT
12535 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12536 to expand zero extend as two shifts instead of
12537 masking. */
12538 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12540 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12541 mem_loc_result = op0;
12542 add_loc_descr (&mem_loc_result,
12543 int_loc_descriptor (GET_MODE_MASK (imode)));
12544 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12546 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12548 int shift = DWARF2_ADDR_SIZE
12549 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12550 shift *= BITS_PER_UNIT;
12551 if (GET_CODE (rtl) == SIGN_EXTEND)
12552 op = DW_OP_shra;
12553 else
12554 op = DW_OP_shr;
12555 mem_loc_result = op0;
12556 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12557 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12558 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12559 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12561 else if (!dwarf_strict)
12563 dw_die_ref type_die1, type_die2;
12564 dw_loc_descr_ref cvt;
12566 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12567 GET_CODE (rtl) == ZERO_EXTEND);
12568 if (type_die1 == NULL)
12569 break;
12570 type_die2 = base_type_for_mode (mode, 1);
12571 if (type_die2 == NULL)
12572 break;
12573 mem_loc_result = op0;
12574 cvt = new_loc_descr (DW_OP_GNU_convert, 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_die1;
12577 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12578 add_loc_descr (&mem_loc_result, cvt);
12579 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12580 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12581 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12582 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12583 add_loc_descr (&mem_loc_result, cvt);
12585 break;
12587 case MEM:
12589 rtx new_rtl = avoid_constant_pool_reference (rtl);
12590 if (new_rtl != rtl)
12592 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12593 initialized);
12594 if (mem_loc_result != NULL)
12595 return mem_loc_result;
12598 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12599 get_address_mode (rtl), mode,
12600 VAR_INIT_STATUS_INITIALIZED);
12601 if (mem_loc_result == NULL)
12602 mem_loc_result = tls_mem_loc_descriptor (rtl);
12603 if (mem_loc_result != NULL)
12605 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12606 || GET_MODE_CLASS (mode) != MODE_INT)
12608 dw_die_ref type_die;
12609 dw_loc_descr_ref deref;
12611 if (dwarf_strict)
12612 return NULL;
12613 type_die
12614 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12615 if (type_die == NULL)
12616 return NULL;
12617 deref = new_loc_descr (DW_OP_GNU_deref_type,
12618 GET_MODE_SIZE (mode), 0);
12619 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12620 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12621 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12622 add_loc_descr (&mem_loc_result, deref);
12624 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12625 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12626 else
12627 add_loc_descr (&mem_loc_result,
12628 new_loc_descr (DW_OP_deref_size,
12629 GET_MODE_SIZE (mode), 0));
12631 break;
12633 case LO_SUM:
12634 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12636 case LABEL_REF:
12637 /* Some ports can transform a symbol ref into a label ref, because
12638 the symbol ref is too far away and has to be dumped into a constant
12639 pool. */
12640 case CONST:
12641 case SYMBOL_REF:
12642 if ((GET_MODE_CLASS (mode) != MODE_INT
12643 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12644 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12645 #ifdef POINTERS_EXTEND_UNSIGNED
12646 && (mode != Pmode || mem_mode == VOIDmode)
12647 #endif
12649 break;
12650 if (GET_CODE (rtl) == SYMBOL_REF
12651 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12653 dw_loc_descr_ref temp;
12655 /* If this is not defined, we have no way to emit the data. */
12656 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12657 break;
12659 temp = new_addr_loc_descr (rtl, dtprel_true);
12661 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12662 add_loc_descr (&mem_loc_result, temp);
12664 break;
12667 if (!const_ok_for_output (rtl))
12668 break;
12670 symref:
12671 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12672 vec_safe_push (used_rtx_array, rtl);
12673 break;
12675 case CONCAT:
12676 case CONCATN:
12677 case VAR_LOCATION:
12678 case DEBUG_IMPLICIT_PTR:
12679 expansion_failed (NULL_TREE, rtl,
12680 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12681 return 0;
12683 case ENTRY_VALUE:
12684 if (dwarf_strict)
12685 return NULL;
12686 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12688 if (GET_MODE_CLASS (mode) != MODE_INT
12689 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12690 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12691 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12692 else
12694 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12695 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12696 return NULL;
12697 op0 = one_reg_loc_descriptor (dbx_regnum,
12698 VAR_INIT_STATUS_INITIALIZED);
12701 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12702 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12704 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12705 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12706 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12707 return NULL;
12709 else
12710 gcc_unreachable ();
12711 if (op0 == NULL)
12712 return NULL;
12713 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12714 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12715 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12716 break;
12718 case DEBUG_PARAMETER_REF:
12719 mem_loc_result = parameter_ref_descriptor (rtl);
12720 break;
12722 case PRE_MODIFY:
12723 /* Extract the PLUS expression nested inside and fall into
12724 PLUS code below. */
12725 rtl = XEXP (rtl, 1);
12726 goto plus;
12728 case PRE_INC:
12729 case PRE_DEC:
12730 /* Turn these into a PLUS expression and fall into the PLUS code
12731 below. */
12732 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12733 gen_int_mode (GET_CODE (rtl) == PRE_INC
12734 ? GET_MODE_UNIT_SIZE (mem_mode)
12735 : -GET_MODE_UNIT_SIZE (mem_mode),
12736 mode));
12738 /* ... fall through ... */
12740 case PLUS:
12741 plus:
12742 if (is_based_loc (rtl)
12743 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12744 || XEXP (rtl, 0) == arg_pointer_rtx
12745 || XEXP (rtl, 0) == frame_pointer_rtx)
12746 && GET_MODE_CLASS (mode) == MODE_INT)
12747 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12748 INTVAL (XEXP (rtl, 1)),
12749 VAR_INIT_STATUS_INITIALIZED);
12750 else
12752 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12753 VAR_INIT_STATUS_INITIALIZED);
12754 if (mem_loc_result == 0)
12755 break;
12757 if (CONST_INT_P (XEXP (rtl, 1))
12758 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12759 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12760 else
12762 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12763 VAR_INIT_STATUS_INITIALIZED);
12764 if (op1 == 0)
12765 return NULL;
12766 add_loc_descr (&mem_loc_result, op1);
12767 add_loc_descr (&mem_loc_result,
12768 new_loc_descr (DW_OP_plus, 0, 0));
12771 break;
12773 /* If a pseudo-reg is optimized away, it is possible for it to
12774 be replaced with a MEM containing a multiply or shift. */
12775 case MINUS:
12776 op = DW_OP_minus;
12777 goto do_binop;
12779 case MULT:
12780 op = DW_OP_mul;
12781 goto do_binop;
12783 case DIV:
12784 if (!dwarf_strict
12785 && GET_MODE_CLASS (mode) == MODE_INT
12786 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12788 mem_loc_result = typed_binop (DW_OP_div, rtl,
12789 base_type_for_mode (mode, 0),
12790 mode, mem_mode);
12791 break;
12793 op = DW_OP_div;
12794 goto do_binop;
12796 case UMOD:
12797 op = DW_OP_mod;
12798 goto do_binop;
12800 case ASHIFT:
12801 op = DW_OP_shl;
12802 goto do_shift;
12804 case ASHIFTRT:
12805 op = DW_OP_shra;
12806 goto do_shift;
12808 case LSHIFTRT:
12809 op = DW_OP_shr;
12810 goto do_shift;
12812 do_shift:
12813 if (GET_MODE_CLASS (mode) != MODE_INT)
12814 break;
12815 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12816 VAR_INIT_STATUS_INITIALIZED);
12818 rtx rtlop1 = XEXP (rtl, 1);
12819 if (GET_MODE (rtlop1) != VOIDmode
12820 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12821 < GET_MODE_BITSIZE (mode))
12822 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12823 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12824 VAR_INIT_STATUS_INITIALIZED);
12827 if (op0 == 0 || op1 == 0)
12828 break;
12830 mem_loc_result = op0;
12831 add_loc_descr (&mem_loc_result, op1);
12832 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12833 break;
12835 case AND:
12836 op = DW_OP_and;
12837 goto do_binop;
12839 case IOR:
12840 op = DW_OP_or;
12841 goto do_binop;
12843 case XOR:
12844 op = DW_OP_xor;
12845 goto do_binop;
12847 do_binop:
12848 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12849 VAR_INIT_STATUS_INITIALIZED);
12850 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12851 VAR_INIT_STATUS_INITIALIZED);
12853 if (op0 == 0 || op1 == 0)
12854 break;
12856 mem_loc_result = op0;
12857 add_loc_descr (&mem_loc_result, op1);
12858 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12859 break;
12861 case MOD:
12862 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12864 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12865 base_type_for_mode (mode, 0),
12866 mode, mem_mode);
12867 break;
12870 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12871 VAR_INIT_STATUS_INITIALIZED);
12872 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12873 VAR_INIT_STATUS_INITIALIZED);
12875 if (op0 == 0 || op1 == 0)
12876 break;
12878 mem_loc_result = op0;
12879 add_loc_descr (&mem_loc_result, op1);
12880 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12881 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12882 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12883 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12884 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12885 break;
12887 case UDIV:
12888 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12890 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12892 op = DW_OP_div;
12893 goto do_binop;
12895 mem_loc_result = typed_binop (DW_OP_div, rtl,
12896 base_type_for_mode (mode, 1),
12897 mode, mem_mode);
12899 break;
12901 case NOT:
12902 op = DW_OP_not;
12903 goto do_unop;
12905 case ABS:
12906 op = DW_OP_abs;
12907 goto do_unop;
12909 case NEG:
12910 op = DW_OP_neg;
12911 goto do_unop;
12913 do_unop:
12914 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12915 VAR_INIT_STATUS_INITIALIZED);
12917 if (op0 == 0)
12918 break;
12920 mem_loc_result = op0;
12921 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12922 break;
12924 case CONST_INT:
12925 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12926 #ifdef POINTERS_EXTEND_UNSIGNED
12927 || (mode == Pmode
12928 && mem_mode != VOIDmode
12929 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12930 #endif
12933 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12934 break;
12936 if (!dwarf_strict
12937 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12938 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12940 dw_die_ref type_die = base_type_for_mode (mode, 1);
12941 enum machine_mode amode;
12942 if (type_die == NULL)
12943 return NULL;
12944 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12945 MODE_INT, 0);
12946 if (INTVAL (rtl) >= 0
12947 && amode != BLKmode
12948 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12949 /* const DW_OP_GNU_convert <XXX> vs.
12950 DW_OP_GNU_const_type <XXX, 1, const>. */
12951 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12952 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12954 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12955 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12956 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12957 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12958 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12959 add_loc_descr (&mem_loc_result, op0);
12960 return mem_loc_result;
12962 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12963 INTVAL (rtl));
12964 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12965 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12966 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12967 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12968 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12969 else
12971 mem_loc_result->dw_loc_oprnd2.val_class
12972 = dw_val_class_const_double;
12973 mem_loc_result->dw_loc_oprnd2.v.val_double
12974 = double_int::from_shwi (INTVAL (rtl));
12977 break;
12979 case CONST_DOUBLE:
12980 if (!dwarf_strict)
12982 dw_die_ref type_die;
12984 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12985 CONST_DOUBLE rtx could represent either a large integer
12986 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
12987 the value is always a floating point constant.
12989 When it is an integer, a CONST_DOUBLE is used whenever
12990 the constant requires 2 HWIs to be adequately represented.
12991 We output CONST_DOUBLEs as blocks. */
12992 if (mode == VOIDmode
12993 || (GET_MODE (rtl) == VOIDmode
12994 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12995 break;
12996 type_die = base_type_for_mode (mode,
12997 GET_MODE_CLASS (mode) == MODE_INT);
12998 if (type_die == NULL)
12999 return NULL;
13000 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13001 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13002 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13003 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13004 #if TARGET_SUPPORTS_WIDE_INT == 0
13005 if (!SCALAR_FLOAT_MODE_P (mode))
13007 mem_loc_result->dw_loc_oprnd2.val_class
13008 = dw_val_class_const_double;
13009 mem_loc_result->dw_loc_oprnd2.v.val_double
13010 = rtx_to_double_int (rtl);
13012 else
13013 #endif
13015 unsigned int length = GET_MODE_SIZE (mode);
13016 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13018 insert_float (rtl, array);
13019 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13020 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13021 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13022 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13025 break;
13027 case CONST_WIDE_INT:
13028 if (!dwarf_strict)
13030 dw_die_ref type_die;
13032 type_die = base_type_for_mode (mode,
13033 GET_MODE_CLASS (mode) == MODE_INT);
13034 if (type_die == NULL)
13035 return NULL;
13036 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13037 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13038 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13039 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13040 mem_loc_result->dw_loc_oprnd2.val_class
13041 = dw_val_class_wide_int;
13042 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13043 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13045 break;
13047 case EQ:
13048 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13049 break;
13051 case GE:
13052 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13053 break;
13055 case GT:
13056 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13057 break;
13059 case LE:
13060 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13061 break;
13063 case LT:
13064 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13065 break;
13067 case NE:
13068 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13069 break;
13071 case GEU:
13072 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13073 break;
13075 case GTU:
13076 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13077 break;
13079 case LEU:
13080 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13081 break;
13083 case LTU:
13084 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13085 break;
13087 case UMIN:
13088 case UMAX:
13089 if (GET_MODE_CLASS (mode) != MODE_INT)
13090 break;
13091 /* FALLTHRU */
13092 case SMIN:
13093 case SMAX:
13094 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13095 break;
13097 case ZERO_EXTRACT:
13098 case SIGN_EXTRACT:
13099 if (CONST_INT_P (XEXP (rtl, 1))
13100 && CONST_INT_P (XEXP (rtl, 2))
13101 && ((unsigned) INTVAL (XEXP (rtl, 1))
13102 + (unsigned) INTVAL (XEXP (rtl, 2))
13103 <= GET_MODE_BITSIZE (mode))
13104 && GET_MODE_CLASS (mode) == MODE_INT
13105 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13106 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13108 int shift, size;
13109 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13110 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13111 if (op0 == 0)
13112 break;
13113 if (GET_CODE (rtl) == SIGN_EXTRACT)
13114 op = DW_OP_shra;
13115 else
13116 op = DW_OP_shr;
13117 mem_loc_result = op0;
13118 size = INTVAL (XEXP (rtl, 1));
13119 shift = INTVAL (XEXP (rtl, 2));
13120 if (BITS_BIG_ENDIAN)
13121 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13122 - shift - size;
13123 if (shift + size != (int) DWARF2_ADDR_SIZE)
13125 add_loc_descr (&mem_loc_result,
13126 int_loc_descriptor (DWARF2_ADDR_SIZE
13127 - shift - size));
13128 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13130 if (size != (int) DWARF2_ADDR_SIZE)
13132 add_loc_descr (&mem_loc_result,
13133 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13134 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13137 break;
13139 case IF_THEN_ELSE:
13141 dw_loc_descr_ref op2, bra_node, drop_node;
13142 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13143 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13144 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13145 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13146 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13147 VAR_INIT_STATUS_INITIALIZED);
13148 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13149 VAR_INIT_STATUS_INITIALIZED);
13150 if (op0 == NULL || op1 == NULL || op2 == NULL)
13151 break;
13153 mem_loc_result = op1;
13154 add_loc_descr (&mem_loc_result, op2);
13155 add_loc_descr (&mem_loc_result, op0);
13156 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13157 add_loc_descr (&mem_loc_result, bra_node);
13158 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13159 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13160 add_loc_descr (&mem_loc_result, drop_node);
13161 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13162 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13164 break;
13166 case FLOAT_EXTEND:
13167 case FLOAT_TRUNCATE:
13168 case FLOAT:
13169 case UNSIGNED_FLOAT:
13170 case FIX:
13171 case UNSIGNED_FIX:
13172 if (!dwarf_strict)
13174 dw_die_ref type_die;
13175 dw_loc_descr_ref cvt;
13177 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13178 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13179 if (op0 == NULL)
13180 break;
13181 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13182 && (GET_CODE (rtl) == FLOAT
13183 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13184 <= DWARF2_ADDR_SIZE))
13186 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13187 GET_CODE (rtl) == UNSIGNED_FLOAT);
13188 if (type_die == NULL)
13189 break;
13190 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13191 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13192 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13193 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13194 add_loc_descr (&op0, cvt);
13196 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13197 if (type_die == NULL)
13198 break;
13199 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13200 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13201 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13202 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13203 add_loc_descr (&op0, cvt);
13204 if (GET_MODE_CLASS (mode) == MODE_INT
13205 && (GET_CODE (rtl) == FIX
13206 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13208 op0 = convert_descriptor_to_mode (mode, op0);
13209 if (op0 == NULL)
13210 break;
13212 mem_loc_result = op0;
13214 break;
13216 case CLZ:
13217 case CTZ:
13218 case FFS:
13219 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13220 break;
13222 case POPCOUNT:
13223 case PARITY:
13224 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13225 break;
13227 case BSWAP:
13228 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13229 break;
13231 case ROTATE:
13232 case ROTATERT:
13233 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13234 break;
13236 case COMPARE:
13237 /* In theory, we could implement the above. */
13238 /* DWARF cannot represent the unsigned compare operations
13239 natively. */
13240 case SS_MULT:
13241 case US_MULT:
13242 case SS_DIV:
13243 case US_DIV:
13244 case SS_PLUS:
13245 case US_PLUS:
13246 case SS_MINUS:
13247 case US_MINUS:
13248 case SS_NEG:
13249 case US_NEG:
13250 case SS_ABS:
13251 case SS_ASHIFT:
13252 case US_ASHIFT:
13253 case SS_TRUNCATE:
13254 case US_TRUNCATE:
13255 case UNORDERED:
13256 case ORDERED:
13257 case UNEQ:
13258 case UNGE:
13259 case UNGT:
13260 case UNLE:
13261 case UNLT:
13262 case LTGT:
13263 case FRACT_CONVERT:
13264 case UNSIGNED_FRACT_CONVERT:
13265 case SAT_FRACT:
13266 case UNSIGNED_SAT_FRACT:
13267 case SQRT:
13268 case ASM_OPERANDS:
13269 case VEC_MERGE:
13270 case VEC_SELECT:
13271 case VEC_CONCAT:
13272 case VEC_DUPLICATE:
13273 case UNSPEC:
13274 case HIGH:
13275 case FMA:
13276 case STRICT_LOW_PART:
13277 case CONST_VECTOR:
13278 case CONST_FIXED:
13279 case CLRSB:
13280 case CLOBBER:
13281 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13282 can't express it in the debug info. This can happen e.g. with some
13283 TLS UNSPECs. */
13284 break;
13286 case CONST_STRING:
13287 resolve_one_addr (&rtl);
13288 goto symref;
13290 default:
13291 #ifdef ENABLE_CHECKING
13292 print_rtl (stderr, rtl);
13293 gcc_unreachable ();
13294 #else
13295 break;
13296 #endif
13299 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13300 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13302 return mem_loc_result;
13305 /* Return a descriptor that describes the concatenation of two locations.
13306 This is typically a complex variable. */
13308 static dw_loc_descr_ref
13309 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13311 dw_loc_descr_ref cc_loc_result = NULL;
13312 dw_loc_descr_ref x0_ref
13313 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13314 dw_loc_descr_ref x1_ref
13315 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13317 if (x0_ref == 0 || x1_ref == 0)
13318 return 0;
13320 cc_loc_result = x0_ref;
13321 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13323 add_loc_descr (&cc_loc_result, x1_ref);
13324 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13326 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13327 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13329 return cc_loc_result;
13332 /* Return a descriptor that describes the concatenation of N
13333 locations. */
13335 static dw_loc_descr_ref
13336 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13338 unsigned int i;
13339 dw_loc_descr_ref cc_loc_result = NULL;
13340 unsigned int n = XVECLEN (concatn, 0);
13342 for (i = 0; i < n; ++i)
13344 dw_loc_descr_ref ref;
13345 rtx x = XVECEXP (concatn, 0, i);
13347 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13348 if (ref == NULL)
13349 return NULL;
13351 add_loc_descr (&cc_loc_result, ref);
13352 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13355 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13356 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13358 return cc_loc_result;
13361 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13362 for DEBUG_IMPLICIT_PTR RTL. */
13364 static dw_loc_descr_ref
13365 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13367 dw_loc_descr_ref ret;
13368 dw_die_ref ref;
13370 if (dwarf_strict)
13371 return NULL;
13372 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13373 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13374 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13375 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13376 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13377 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13378 if (ref)
13380 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13381 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13382 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13384 else
13386 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13387 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13389 return ret;
13392 /* Output a proper Dwarf location descriptor for a variable or parameter
13393 which is either allocated in a register or in a memory location. For a
13394 register, we just generate an OP_REG and the register number. For a
13395 memory location we provide a Dwarf postfix expression describing how to
13396 generate the (dynamic) address of the object onto the address stack.
13398 MODE is mode of the decl if this loc_descriptor is going to be used in
13399 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13400 allowed, VOIDmode otherwise.
13402 If we don't know how to describe it, return 0. */
13404 static dw_loc_descr_ref
13405 loc_descriptor (rtx rtl, enum machine_mode mode,
13406 enum var_init_status initialized)
13408 dw_loc_descr_ref loc_result = NULL;
13410 switch (GET_CODE (rtl))
13412 case SUBREG:
13413 /* The case of a subreg may arise when we have a local (register)
13414 variable or a formal (register) parameter which doesn't quite fill
13415 up an entire register. For now, just assume that it is
13416 legitimate to make the Dwarf info refer to the whole register which
13417 contains the given subreg. */
13418 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13419 loc_result = loc_descriptor (SUBREG_REG (rtl),
13420 GET_MODE (SUBREG_REG (rtl)), initialized);
13421 else
13422 goto do_default;
13423 break;
13425 case REG:
13426 loc_result = reg_loc_descriptor (rtl, initialized);
13427 break;
13429 case MEM:
13430 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13431 GET_MODE (rtl), initialized);
13432 if (loc_result == NULL)
13433 loc_result = tls_mem_loc_descriptor (rtl);
13434 if (loc_result == NULL)
13436 rtx new_rtl = avoid_constant_pool_reference (rtl);
13437 if (new_rtl != rtl)
13438 loc_result = loc_descriptor (new_rtl, mode, initialized);
13440 break;
13442 case CONCAT:
13443 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13444 initialized);
13445 break;
13447 case CONCATN:
13448 loc_result = concatn_loc_descriptor (rtl, initialized);
13449 break;
13451 case VAR_LOCATION:
13452 /* Single part. */
13453 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13455 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13456 if (GET_CODE (loc) == EXPR_LIST)
13457 loc = XEXP (loc, 0);
13458 loc_result = loc_descriptor (loc, mode, initialized);
13459 break;
13462 rtl = XEXP (rtl, 1);
13463 /* FALLTHRU */
13465 case PARALLEL:
13467 rtvec par_elems = XVEC (rtl, 0);
13468 int num_elem = GET_NUM_ELEM (par_elems);
13469 enum machine_mode mode;
13470 int i;
13472 /* Create the first one, so we have something to add to. */
13473 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13474 VOIDmode, initialized);
13475 if (loc_result == NULL)
13476 return NULL;
13477 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13478 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13479 for (i = 1; i < num_elem; i++)
13481 dw_loc_descr_ref temp;
13483 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13484 VOIDmode, initialized);
13485 if (temp == NULL)
13486 return NULL;
13487 add_loc_descr (&loc_result, temp);
13488 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13489 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13492 break;
13494 case CONST_INT:
13495 if (mode != VOIDmode && mode != BLKmode)
13496 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13497 INTVAL (rtl));
13498 break;
13500 case CONST_DOUBLE:
13501 if (mode == VOIDmode)
13502 mode = GET_MODE (rtl);
13504 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13506 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13508 /* Note that a CONST_DOUBLE rtx could represent either an integer
13509 or a floating-point constant. A CONST_DOUBLE is used whenever
13510 the constant requires more than one word in order to be
13511 adequately represented. We output CONST_DOUBLEs as blocks. */
13512 loc_result = new_loc_descr (DW_OP_implicit_value,
13513 GET_MODE_SIZE (mode), 0);
13514 #if TARGET_SUPPORTS_WIDE_INT == 0
13515 if (!SCALAR_FLOAT_MODE_P (mode))
13517 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13518 loc_result->dw_loc_oprnd2.v.val_double
13519 = rtx_to_double_int (rtl);
13521 else
13522 #endif
13524 unsigned int length = GET_MODE_SIZE (mode);
13525 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13527 insert_float (rtl, array);
13528 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13529 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13530 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13531 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13534 break;
13536 case CONST_WIDE_INT:
13537 if (mode == VOIDmode)
13538 mode = GET_MODE (rtl);
13540 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13542 loc_result = new_loc_descr (DW_OP_implicit_value,
13543 GET_MODE_SIZE (mode), 0);
13544 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13545 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13546 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13548 break;
13550 case CONST_VECTOR:
13551 if (mode == VOIDmode)
13552 mode = GET_MODE (rtl);
13554 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13556 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13557 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13558 unsigned char *array
13559 = ggc_vec_alloc<unsigned char> (length * elt_size);
13560 unsigned int i;
13561 unsigned char *p;
13562 enum machine_mode imode = GET_MODE_INNER (mode);
13564 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13565 switch (GET_MODE_CLASS (mode))
13567 case MODE_VECTOR_INT:
13568 for (i = 0, p = array; i < length; i++, p += elt_size)
13570 rtx elt = CONST_VECTOR_ELT (rtl, i);
13571 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13573 break;
13575 case MODE_VECTOR_FLOAT:
13576 for (i = 0, p = array; i < length; i++, p += elt_size)
13578 rtx elt = CONST_VECTOR_ELT (rtl, i);
13579 insert_float (elt, p);
13581 break;
13583 default:
13584 gcc_unreachable ();
13587 loc_result = new_loc_descr (DW_OP_implicit_value,
13588 length * elt_size, 0);
13589 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13590 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13591 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13592 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13594 break;
13596 case CONST:
13597 if (mode == VOIDmode
13598 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13599 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13600 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13602 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13603 break;
13605 /* FALLTHROUGH */
13606 case SYMBOL_REF:
13607 if (!const_ok_for_output (rtl))
13608 break;
13609 case LABEL_REF:
13610 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13611 && (dwarf_version >= 4 || !dwarf_strict))
13613 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13614 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13615 vec_safe_push (used_rtx_array, rtl);
13617 break;
13619 case DEBUG_IMPLICIT_PTR:
13620 loc_result = implicit_ptr_descriptor (rtl, 0);
13621 break;
13623 case PLUS:
13624 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13625 && CONST_INT_P (XEXP (rtl, 1)))
13627 loc_result
13628 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13629 break;
13631 /* FALLTHRU */
13632 do_default:
13633 default:
13634 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13635 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13636 && dwarf_version >= 4)
13637 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13639 /* Value expression. */
13640 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13641 if (loc_result)
13642 add_loc_descr (&loc_result,
13643 new_loc_descr (DW_OP_stack_value, 0, 0));
13645 break;
13648 return loc_result;
13651 /* We need to figure out what section we should use as the base for the
13652 address ranges where a given location is valid.
13653 1. If this particular DECL has a section associated with it, use that.
13654 2. If this function has a section associated with it, use that.
13655 3. Otherwise, use the text section.
13656 XXX: If you split a variable across multiple sections, we won't notice. */
13658 static const char *
13659 secname_for_decl (const_tree decl)
13661 const char *secname;
13663 if (VAR_OR_FUNCTION_DECL_P (decl)
13664 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13665 && DECL_SECTION_NAME (decl))
13666 secname = DECL_SECTION_NAME (decl);
13667 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13668 secname = DECL_SECTION_NAME (current_function_decl);
13669 else if (cfun && in_cold_section_p)
13670 secname = crtl->subsections.cold_section_label;
13671 else
13672 secname = text_section_label;
13674 return secname;
13677 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13679 static bool
13680 decl_by_reference_p (tree decl)
13682 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13683 || TREE_CODE (decl) == VAR_DECL)
13684 && DECL_BY_REFERENCE (decl));
13687 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13688 for VARLOC. */
13690 static dw_loc_descr_ref
13691 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13692 enum var_init_status initialized)
13694 int have_address = 0;
13695 dw_loc_descr_ref descr;
13696 enum machine_mode mode;
13698 if (want_address != 2)
13700 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13701 /* Single part. */
13702 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13704 varloc = PAT_VAR_LOCATION_LOC (varloc);
13705 if (GET_CODE (varloc) == EXPR_LIST)
13706 varloc = XEXP (varloc, 0);
13707 mode = GET_MODE (varloc);
13708 if (MEM_P (varloc))
13710 rtx addr = XEXP (varloc, 0);
13711 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13712 mode, initialized);
13713 if (descr)
13714 have_address = 1;
13715 else
13717 rtx x = avoid_constant_pool_reference (varloc);
13718 if (x != varloc)
13719 descr = mem_loc_descriptor (x, mode, VOIDmode,
13720 initialized);
13723 else
13724 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13726 else
13727 return 0;
13729 else
13731 if (GET_CODE (varloc) == VAR_LOCATION)
13732 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13733 else
13734 mode = DECL_MODE (loc);
13735 descr = loc_descriptor (varloc, mode, initialized);
13736 have_address = 1;
13739 if (!descr)
13740 return 0;
13742 if (want_address == 2 && !have_address
13743 && (dwarf_version >= 4 || !dwarf_strict))
13745 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13747 expansion_failed (loc, NULL_RTX,
13748 "DWARF address size mismatch");
13749 return 0;
13751 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13752 have_address = 1;
13754 /* Show if we can't fill the request for an address. */
13755 if (want_address && !have_address)
13757 expansion_failed (loc, NULL_RTX,
13758 "Want address and only have value");
13759 return 0;
13762 /* If we've got an address and don't want one, dereference. */
13763 if (!want_address && have_address)
13765 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13766 enum dwarf_location_atom op;
13768 if (size > DWARF2_ADDR_SIZE || size == -1)
13770 expansion_failed (loc, NULL_RTX,
13771 "DWARF address size mismatch");
13772 return 0;
13774 else if (size == DWARF2_ADDR_SIZE)
13775 op = DW_OP_deref;
13776 else
13777 op = DW_OP_deref_size;
13779 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13782 return descr;
13785 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13786 if it is not possible. */
13788 static dw_loc_descr_ref
13789 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13791 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13792 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13793 else if (dwarf_version >= 3 || !dwarf_strict)
13794 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13795 else
13796 return NULL;
13799 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13800 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13802 static dw_loc_descr_ref
13803 dw_sra_loc_expr (tree decl, rtx loc)
13805 rtx p;
13806 unsigned int padsize = 0;
13807 dw_loc_descr_ref descr, *descr_tail;
13808 unsigned HOST_WIDE_INT decl_size;
13809 rtx varloc;
13810 enum var_init_status initialized;
13812 if (DECL_SIZE (decl) == NULL
13813 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13814 return NULL;
13816 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13817 descr = NULL;
13818 descr_tail = &descr;
13820 for (p = loc; p; p = XEXP (p, 1))
13822 unsigned int bitsize = decl_piece_bitsize (p);
13823 rtx loc_note = *decl_piece_varloc_ptr (p);
13824 dw_loc_descr_ref cur_descr;
13825 dw_loc_descr_ref *tail, last = NULL;
13826 unsigned int opsize = 0;
13828 if (loc_note == NULL_RTX
13829 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13831 padsize += bitsize;
13832 continue;
13834 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13835 varloc = NOTE_VAR_LOCATION (loc_note);
13836 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13837 if (cur_descr == NULL)
13839 padsize += bitsize;
13840 continue;
13843 /* Check that cur_descr either doesn't use
13844 DW_OP_*piece operations, or their sum is equal
13845 to bitsize. Otherwise we can't embed it. */
13846 for (tail = &cur_descr; *tail != NULL;
13847 tail = &(*tail)->dw_loc_next)
13848 if ((*tail)->dw_loc_opc == DW_OP_piece)
13850 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13851 * BITS_PER_UNIT;
13852 last = *tail;
13854 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13856 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13857 last = *tail;
13860 if (last != NULL && opsize != bitsize)
13862 padsize += bitsize;
13863 /* Discard the current piece of the descriptor and release any
13864 addr_table entries it uses. */
13865 remove_loc_list_addr_table_entries (cur_descr);
13866 continue;
13869 /* If there is a hole, add DW_OP_*piece after empty DWARF
13870 expression, which means that those bits are optimized out. */
13871 if (padsize)
13873 if (padsize > decl_size)
13875 remove_loc_list_addr_table_entries (cur_descr);
13876 goto discard_descr;
13878 decl_size -= padsize;
13879 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13880 if (*descr_tail == NULL)
13882 remove_loc_list_addr_table_entries (cur_descr);
13883 goto discard_descr;
13885 descr_tail = &(*descr_tail)->dw_loc_next;
13886 padsize = 0;
13888 *descr_tail = cur_descr;
13889 descr_tail = tail;
13890 if (bitsize > decl_size)
13891 goto discard_descr;
13892 decl_size -= bitsize;
13893 if (last == NULL)
13895 HOST_WIDE_INT offset = 0;
13896 if (GET_CODE (varloc) == VAR_LOCATION
13897 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13899 varloc = PAT_VAR_LOCATION_LOC (varloc);
13900 if (GET_CODE (varloc) == EXPR_LIST)
13901 varloc = XEXP (varloc, 0);
13905 if (GET_CODE (varloc) == CONST
13906 || GET_CODE (varloc) == SIGN_EXTEND
13907 || GET_CODE (varloc) == ZERO_EXTEND)
13908 varloc = XEXP (varloc, 0);
13909 else if (GET_CODE (varloc) == SUBREG)
13910 varloc = SUBREG_REG (varloc);
13911 else
13912 break;
13914 while (1);
13915 /* DW_OP_bit_size offset should be zero for register
13916 or implicit location descriptions and empty location
13917 descriptions, but for memory addresses needs big endian
13918 adjustment. */
13919 if (MEM_P (varloc))
13921 unsigned HOST_WIDE_INT memsize
13922 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13923 if (memsize != bitsize)
13925 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13926 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13927 goto discard_descr;
13928 if (memsize < bitsize)
13929 goto discard_descr;
13930 if (BITS_BIG_ENDIAN)
13931 offset = memsize - bitsize;
13935 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13936 if (*descr_tail == NULL)
13937 goto discard_descr;
13938 descr_tail = &(*descr_tail)->dw_loc_next;
13942 /* If there were any non-empty expressions, add padding till the end of
13943 the decl. */
13944 if (descr != NULL && decl_size != 0)
13946 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13947 if (*descr_tail == NULL)
13948 goto discard_descr;
13950 return descr;
13952 discard_descr:
13953 /* Discard the descriptor and release any addr_table entries it uses. */
13954 remove_loc_list_addr_table_entries (descr);
13955 return NULL;
13958 /* Return the dwarf representation of the location list LOC_LIST of
13959 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13960 function. */
13962 static dw_loc_list_ref
13963 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13965 const char *endname, *secname;
13966 rtx varloc;
13967 enum var_init_status initialized;
13968 struct var_loc_node *node;
13969 dw_loc_descr_ref descr;
13970 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13971 dw_loc_list_ref list = NULL;
13972 dw_loc_list_ref *listp = &list;
13974 /* Now that we know what section we are using for a base,
13975 actually construct the list of locations.
13976 The first location information is what is passed to the
13977 function that creates the location list, and the remaining
13978 locations just get added on to that list.
13979 Note that we only know the start address for a location
13980 (IE location changes), so to build the range, we use
13981 the range [current location start, next location start].
13982 This means we have to special case the last node, and generate
13983 a range of [last location start, end of function label]. */
13985 secname = secname_for_decl (decl);
13987 for (node = loc_list->first; node; node = node->next)
13988 if (GET_CODE (node->loc) == EXPR_LIST
13989 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13991 if (GET_CODE (node->loc) == EXPR_LIST)
13993 /* This requires DW_OP_{,bit_}piece, which is not usable
13994 inside DWARF expressions. */
13995 if (want_address != 2)
13996 continue;
13997 descr = dw_sra_loc_expr (decl, node->loc);
13998 if (descr == NULL)
13999 continue;
14001 else
14003 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14004 varloc = NOTE_VAR_LOCATION (node->loc);
14005 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14007 if (descr)
14009 bool range_across_switch = false;
14010 /* If section switch happens in between node->label
14011 and node->next->label (or end of function) and
14012 we can't emit it as a single entry list,
14013 emit two ranges, first one ending at the end
14014 of first partition and second one starting at the
14015 beginning of second partition. */
14016 if (node == loc_list->last_before_switch
14017 && (node != loc_list->first || loc_list->first->next)
14018 && current_function_decl)
14020 endname = cfun->fde->dw_fde_end;
14021 range_across_switch = true;
14023 /* The variable has a location between NODE->LABEL and
14024 NODE->NEXT->LABEL. */
14025 else if (node->next)
14026 endname = node->next->label;
14027 /* If the variable has a location at the last label
14028 it keeps its location until the end of function. */
14029 else if (!current_function_decl)
14030 endname = text_end_label;
14031 else
14033 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14034 current_function_funcdef_no);
14035 endname = ggc_strdup (label_id);
14038 *listp = new_loc_list (descr, node->label, endname, secname);
14039 if (TREE_CODE (decl) == PARM_DECL
14040 && node == loc_list->first
14041 && NOTE_P (node->loc)
14042 && strcmp (node->label, endname) == 0)
14043 (*listp)->force = true;
14044 listp = &(*listp)->dw_loc_next;
14046 if (range_across_switch)
14048 if (GET_CODE (node->loc) == EXPR_LIST)
14049 descr = dw_sra_loc_expr (decl, node->loc);
14050 else
14052 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14053 varloc = NOTE_VAR_LOCATION (node->loc);
14054 descr = dw_loc_list_1 (decl, varloc, want_address,
14055 initialized);
14057 gcc_assert (descr);
14058 /* The variable has a location between NODE->LABEL and
14059 NODE->NEXT->LABEL. */
14060 if (node->next)
14061 endname = node->next->label;
14062 else
14063 endname = cfun->fde->dw_fde_second_end;
14064 *listp = new_loc_list (descr,
14065 cfun->fde->dw_fde_second_begin,
14066 endname, secname);
14067 listp = &(*listp)->dw_loc_next;
14072 /* Try to avoid the overhead of a location list emitting a location
14073 expression instead, but only if we didn't have more than one
14074 location entry in the first place. If some entries were not
14075 representable, we don't want to pretend a single entry that was
14076 applies to the entire scope in which the variable is
14077 available. */
14078 if (list && loc_list->first->next)
14079 gen_llsym (list);
14081 return list;
14084 /* Return if the loc_list has only single element and thus can be represented
14085 as location description. */
14087 static bool
14088 single_element_loc_list_p (dw_loc_list_ref list)
14090 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14091 return !list->ll_symbol;
14094 /* To each location in list LIST add loc descr REF. */
14096 static void
14097 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14099 dw_loc_descr_ref copy;
14100 add_loc_descr (&list->expr, ref);
14101 list = list->dw_loc_next;
14102 while (list)
14104 copy = ggc_alloc<dw_loc_descr_node> ();
14105 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14106 add_loc_descr (&list->expr, copy);
14107 while (copy->dw_loc_next)
14109 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14110 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14111 copy->dw_loc_next = new_copy;
14112 copy = new_copy;
14114 list = list->dw_loc_next;
14118 /* Given two lists RET and LIST
14119 produce location list that is result of adding expression in LIST
14120 to expression in RET on each position in program.
14121 Might be destructive on both RET and LIST.
14123 TODO: We handle only simple cases of RET or LIST having at most one
14124 element. General case would inolve sorting the lists in program order
14125 and merging them that will need some additional work.
14126 Adding that will improve quality of debug info especially for SRA-ed
14127 structures. */
14129 static void
14130 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14132 if (!list)
14133 return;
14134 if (!*ret)
14136 *ret = list;
14137 return;
14139 if (!list->dw_loc_next)
14141 add_loc_descr_to_each (*ret, list->expr);
14142 return;
14144 if (!(*ret)->dw_loc_next)
14146 add_loc_descr_to_each (list, (*ret)->expr);
14147 *ret = list;
14148 return;
14150 expansion_failed (NULL_TREE, NULL_RTX,
14151 "Don't know how to merge two non-trivial"
14152 " location lists.\n");
14153 *ret = NULL;
14154 return;
14157 /* LOC is constant expression. Try a luck, look it up in constant
14158 pool and return its loc_descr of its address. */
14160 static dw_loc_descr_ref
14161 cst_pool_loc_descr (tree loc)
14163 /* Get an RTL for this, if something has been emitted. */
14164 rtx rtl = lookup_constant_def (loc);
14166 if (!rtl || !MEM_P (rtl))
14168 gcc_assert (!rtl);
14169 return 0;
14171 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14173 /* TODO: We might get more coverage if we was actually delaying expansion
14174 of all expressions till end of compilation when constant pools are fully
14175 populated. */
14176 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14178 expansion_failed (loc, NULL_RTX,
14179 "CST value in contant pool but not marked.");
14180 return 0;
14182 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14183 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14186 /* Return dw_loc_list representing address of addr_expr LOC
14187 by looking for inner INDIRECT_REF expression and turning
14188 it into simple arithmetics. */
14190 static dw_loc_list_ref
14191 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14193 tree obj, offset;
14194 HOST_WIDE_INT bitsize, bitpos, bytepos;
14195 enum machine_mode mode;
14196 int unsignedp, volatilep = 0;
14197 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14199 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14200 &bitsize, &bitpos, &offset, &mode,
14201 &unsignedp, &volatilep, false);
14202 STRIP_NOPS (obj);
14203 if (bitpos % BITS_PER_UNIT)
14205 expansion_failed (loc, NULL_RTX, "bitfield access");
14206 return 0;
14208 if (!INDIRECT_REF_P (obj))
14210 expansion_failed (obj,
14211 NULL_RTX, "no indirect ref in inner refrence");
14212 return 0;
14214 if (!offset && !bitpos)
14215 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14216 else if (toplev
14217 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14218 && (dwarf_version >= 4 || !dwarf_strict))
14220 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14221 if (!list_ret)
14222 return 0;
14223 if (offset)
14225 /* Variable offset. */
14226 list_ret1 = loc_list_from_tree (offset, 0);
14227 if (list_ret1 == 0)
14228 return 0;
14229 add_loc_list (&list_ret, list_ret1);
14230 if (!list_ret)
14231 return 0;
14232 add_loc_descr_to_each (list_ret,
14233 new_loc_descr (DW_OP_plus, 0, 0));
14235 bytepos = bitpos / BITS_PER_UNIT;
14236 if (bytepos > 0)
14237 add_loc_descr_to_each (list_ret,
14238 new_loc_descr (DW_OP_plus_uconst,
14239 bytepos, 0));
14240 else if (bytepos < 0)
14241 loc_list_plus_const (list_ret, bytepos);
14242 add_loc_descr_to_each (list_ret,
14243 new_loc_descr (DW_OP_stack_value, 0, 0));
14245 return list_ret;
14249 /* Generate Dwarf location list representing LOC.
14250 If WANT_ADDRESS is false, expression computing LOC will be computed
14251 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14252 if WANT_ADDRESS is 2, expression computing address useable in location
14253 will be returned (i.e. DW_OP_reg can be used
14254 to refer to register values). */
14256 static dw_loc_list_ref
14257 loc_list_from_tree (tree loc, int want_address)
14259 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14260 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14261 int have_address = 0;
14262 enum dwarf_location_atom op;
14264 /* ??? Most of the time we do not take proper care for sign/zero
14265 extending the values properly. Hopefully this won't be a real
14266 problem... */
14268 switch (TREE_CODE (loc))
14270 case ERROR_MARK:
14271 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14272 return 0;
14274 case PLACEHOLDER_EXPR:
14275 /* This case involves extracting fields from an object to determine the
14276 position of other fields. We don't try to encode this here. The
14277 only user of this is Ada, which encodes the needed information using
14278 the names of types. */
14279 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14280 return 0;
14282 case CALL_EXPR:
14283 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14284 /* There are no opcodes for these operations. */
14285 return 0;
14287 case PREINCREMENT_EXPR:
14288 case PREDECREMENT_EXPR:
14289 case POSTINCREMENT_EXPR:
14290 case POSTDECREMENT_EXPR:
14291 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14292 /* There are no opcodes for these operations. */
14293 return 0;
14295 case ADDR_EXPR:
14296 /* If we already want an address, see if there is INDIRECT_REF inside
14297 e.g. for &this->field. */
14298 if (want_address)
14300 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14301 (loc, want_address == 2);
14302 if (list_ret)
14303 have_address = 1;
14304 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14305 && (ret = cst_pool_loc_descr (loc)))
14306 have_address = 1;
14308 /* Otherwise, process the argument and look for the address. */
14309 if (!list_ret && !ret)
14310 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14311 else
14313 if (want_address)
14314 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14315 return NULL;
14317 break;
14319 case VAR_DECL:
14320 if (DECL_THREAD_LOCAL_P (loc))
14322 rtx rtl;
14323 enum dwarf_location_atom tls_op;
14324 enum dtprel_bool dtprel = dtprel_false;
14326 if (targetm.have_tls)
14328 /* If this is not defined, we have no way to emit the
14329 data. */
14330 if (!targetm.asm_out.output_dwarf_dtprel)
14331 return 0;
14333 /* The way DW_OP_GNU_push_tls_address is specified, we
14334 can only look up addresses of objects in the current
14335 module. We used DW_OP_addr as first op, but that's
14336 wrong, because DW_OP_addr is relocated by the debug
14337 info consumer, while DW_OP_GNU_push_tls_address
14338 operand shouldn't be. */
14339 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14340 return 0;
14341 dtprel = dtprel_true;
14342 tls_op = DW_OP_GNU_push_tls_address;
14344 else
14346 if (!targetm.emutls.debug_form_tls_address
14347 || !(dwarf_version >= 3 || !dwarf_strict))
14348 return 0;
14349 /* We stuffed the control variable into the DECL_VALUE_EXPR
14350 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14351 no longer appear in gimple code. We used the control
14352 variable in specific so that we could pick it up here. */
14353 loc = DECL_VALUE_EXPR (loc);
14354 tls_op = DW_OP_form_tls_address;
14357 rtl = rtl_for_decl_location (loc);
14358 if (rtl == NULL_RTX)
14359 return 0;
14361 if (!MEM_P (rtl))
14362 return 0;
14363 rtl = XEXP (rtl, 0);
14364 if (! CONSTANT_P (rtl))
14365 return 0;
14367 ret = new_addr_loc_descr (rtl, dtprel);
14368 ret1 = new_loc_descr (tls_op, 0, 0);
14369 add_loc_descr (&ret, ret1);
14371 have_address = 1;
14372 break;
14374 /* FALLTHRU */
14376 case PARM_DECL:
14377 case RESULT_DECL:
14378 if (DECL_HAS_VALUE_EXPR_P (loc))
14379 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14380 want_address);
14381 /* FALLTHRU */
14383 case FUNCTION_DECL:
14385 rtx rtl;
14386 var_loc_list *loc_list = lookup_decl_loc (loc);
14388 if (loc_list && loc_list->first)
14390 list_ret = dw_loc_list (loc_list, loc, want_address);
14391 have_address = want_address != 0;
14392 break;
14394 rtl = rtl_for_decl_location (loc);
14395 if (rtl == NULL_RTX)
14397 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14398 return 0;
14400 else if (CONST_INT_P (rtl))
14402 HOST_WIDE_INT val = INTVAL (rtl);
14403 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14404 val &= GET_MODE_MASK (DECL_MODE (loc));
14405 ret = int_loc_descriptor (val);
14407 else if (GET_CODE (rtl) == CONST_STRING)
14409 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14410 return 0;
14412 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14413 ret = new_addr_loc_descr (rtl, dtprel_false);
14414 else
14416 enum machine_mode mode, mem_mode;
14418 /* Certain constructs can only be represented at top-level. */
14419 if (want_address == 2)
14421 ret = loc_descriptor (rtl, VOIDmode,
14422 VAR_INIT_STATUS_INITIALIZED);
14423 have_address = 1;
14425 else
14427 mode = GET_MODE (rtl);
14428 mem_mode = VOIDmode;
14429 if (MEM_P (rtl))
14431 mem_mode = mode;
14432 mode = get_address_mode (rtl);
14433 rtl = XEXP (rtl, 0);
14434 have_address = 1;
14436 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14437 VAR_INIT_STATUS_INITIALIZED);
14439 if (!ret)
14440 expansion_failed (loc, rtl,
14441 "failed to produce loc descriptor for rtl");
14444 break;
14446 case MEM_REF:
14447 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14449 have_address = 1;
14450 goto do_plus;
14452 /* Fallthru. */
14453 case INDIRECT_REF:
14454 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14455 have_address = 1;
14456 break;
14458 case TARGET_MEM_REF:
14459 case SSA_NAME:
14460 return NULL;
14462 case COMPOUND_EXPR:
14463 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14465 CASE_CONVERT:
14466 case VIEW_CONVERT_EXPR:
14467 case SAVE_EXPR:
14468 case MODIFY_EXPR:
14469 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14471 case COMPONENT_REF:
14472 case BIT_FIELD_REF:
14473 case ARRAY_REF:
14474 case ARRAY_RANGE_REF:
14475 case REALPART_EXPR:
14476 case IMAGPART_EXPR:
14478 tree obj, offset;
14479 HOST_WIDE_INT bitsize, bitpos, bytepos;
14480 enum machine_mode mode;
14481 int unsignedp, volatilep = 0;
14483 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14484 &unsignedp, &volatilep, false);
14486 gcc_assert (obj != loc);
14488 list_ret = loc_list_from_tree (obj,
14489 want_address == 2
14490 && !bitpos && !offset ? 2 : 1);
14491 /* TODO: We can extract value of the small expression via shifting even
14492 for nonzero bitpos. */
14493 if (list_ret == 0)
14494 return 0;
14495 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14497 expansion_failed (loc, NULL_RTX,
14498 "bitfield access");
14499 return 0;
14502 if (offset != NULL_TREE)
14504 /* Variable offset. */
14505 list_ret1 = loc_list_from_tree (offset, 0);
14506 if (list_ret1 == 0)
14507 return 0;
14508 add_loc_list (&list_ret, list_ret1);
14509 if (!list_ret)
14510 return 0;
14511 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14514 bytepos = bitpos / BITS_PER_UNIT;
14515 if (bytepos > 0)
14516 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14517 else if (bytepos < 0)
14518 loc_list_plus_const (list_ret, bytepos);
14520 have_address = 1;
14521 break;
14524 case INTEGER_CST:
14525 if ((want_address || !tree_fits_shwi_p (loc))
14526 && (ret = cst_pool_loc_descr (loc)))
14527 have_address = 1;
14528 else if (want_address == 2
14529 && tree_fits_shwi_p (loc)
14530 && (ret = address_of_int_loc_descriptor
14531 (int_size_in_bytes (TREE_TYPE (loc)),
14532 tree_to_shwi (loc))))
14533 have_address = 1;
14534 else if (tree_fits_shwi_p (loc))
14535 ret = int_loc_descriptor (tree_to_shwi (loc));
14536 else
14538 expansion_failed (loc, NULL_RTX,
14539 "Integer operand is not host integer");
14540 return 0;
14542 break;
14544 case CONSTRUCTOR:
14545 case REAL_CST:
14546 case STRING_CST:
14547 case COMPLEX_CST:
14548 if ((ret = cst_pool_loc_descr (loc)))
14549 have_address = 1;
14550 else
14551 /* We can construct small constants here using int_loc_descriptor. */
14552 expansion_failed (loc, NULL_RTX,
14553 "constructor or constant not in constant pool");
14554 break;
14556 case TRUTH_AND_EXPR:
14557 case TRUTH_ANDIF_EXPR:
14558 case BIT_AND_EXPR:
14559 op = DW_OP_and;
14560 goto do_binop;
14562 case TRUTH_XOR_EXPR:
14563 case BIT_XOR_EXPR:
14564 op = DW_OP_xor;
14565 goto do_binop;
14567 case TRUTH_OR_EXPR:
14568 case TRUTH_ORIF_EXPR:
14569 case BIT_IOR_EXPR:
14570 op = DW_OP_or;
14571 goto do_binop;
14573 case FLOOR_DIV_EXPR:
14574 case CEIL_DIV_EXPR:
14575 case ROUND_DIV_EXPR:
14576 case TRUNC_DIV_EXPR:
14577 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14578 return 0;
14579 op = DW_OP_div;
14580 goto do_binop;
14582 case MINUS_EXPR:
14583 op = DW_OP_minus;
14584 goto do_binop;
14586 case FLOOR_MOD_EXPR:
14587 case CEIL_MOD_EXPR:
14588 case ROUND_MOD_EXPR:
14589 case TRUNC_MOD_EXPR:
14590 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14592 op = DW_OP_mod;
14593 goto do_binop;
14595 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14596 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14597 if (list_ret == 0 || list_ret1 == 0)
14598 return 0;
14600 add_loc_list (&list_ret, list_ret1);
14601 if (list_ret == 0)
14602 return 0;
14603 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14604 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14605 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14606 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14607 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14608 break;
14610 case MULT_EXPR:
14611 op = DW_OP_mul;
14612 goto do_binop;
14614 case LSHIFT_EXPR:
14615 op = DW_OP_shl;
14616 goto do_binop;
14618 case RSHIFT_EXPR:
14619 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14620 goto do_binop;
14622 case POINTER_PLUS_EXPR:
14623 case PLUS_EXPR:
14624 do_plus:
14625 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14627 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14628 if (list_ret == 0)
14629 return 0;
14631 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14632 break;
14635 op = DW_OP_plus;
14636 goto do_binop;
14638 case LE_EXPR:
14639 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14640 return 0;
14642 op = DW_OP_le;
14643 goto do_binop;
14645 case GE_EXPR:
14646 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14647 return 0;
14649 op = DW_OP_ge;
14650 goto do_binop;
14652 case LT_EXPR:
14653 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14654 return 0;
14656 op = DW_OP_lt;
14657 goto do_binop;
14659 case GT_EXPR:
14660 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14661 return 0;
14663 op = DW_OP_gt;
14664 goto do_binop;
14666 case EQ_EXPR:
14667 op = DW_OP_eq;
14668 goto do_binop;
14670 case NE_EXPR:
14671 op = DW_OP_ne;
14672 goto do_binop;
14674 do_binop:
14675 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14676 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14677 if (list_ret == 0 || list_ret1 == 0)
14678 return 0;
14680 add_loc_list (&list_ret, list_ret1);
14681 if (list_ret == 0)
14682 return 0;
14683 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14684 break;
14686 case TRUTH_NOT_EXPR:
14687 case BIT_NOT_EXPR:
14688 op = DW_OP_not;
14689 goto do_unop;
14691 case ABS_EXPR:
14692 op = DW_OP_abs;
14693 goto do_unop;
14695 case NEGATE_EXPR:
14696 op = DW_OP_neg;
14697 goto do_unop;
14699 do_unop:
14700 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14701 if (list_ret == 0)
14702 return 0;
14704 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14705 break;
14707 case MIN_EXPR:
14708 case MAX_EXPR:
14710 const enum tree_code code =
14711 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14713 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14714 build2 (code, integer_type_node,
14715 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14716 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14719 /* ... fall through ... */
14721 case COND_EXPR:
14723 dw_loc_descr_ref lhs
14724 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14725 dw_loc_list_ref rhs
14726 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14727 dw_loc_descr_ref bra_node, jump_node, tmp;
14729 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14730 if (list_ret == 0 || lhs == 0 || rhs == 0)
14731 return 0;
14733 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14734 add_loc_descr_to_each (list_ret, bra_node);
14736 add_loc_list (&list_ret, rhs);
14737 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14738 add_loc_descr_to_each (list_ret, jump_node);
14740 add_loc_descr_to_each (list_ret, lhs);
14741 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14742 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14744 /* ??? Need a node to point the skip at. Use a nop. */
14745 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14746 add_loc_descr_to_each (list_ret, tmp);
14747 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14748 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14750 break;
14752 case FIX_TRUNC_EXPR:
14753 return 0;
14755 default:
14756 /* Leave front-end specific codes as simply unknown. This comes
14757 up, for instance, with the C STMT_EXPR. */
14758 if ((unsigned int) TREE_CODE (loc)
14759 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14761 expansion_failed (loc, NULL_RTX,
14762 "language specific tree node");
14763 return 0;
14766 #ifdef ENABLE_CHECKING
14767 /* Otherwise this is a generic code; we should just lists all of
14768 these explicitly. We forgot one. */
14769 gcc_unreachable ();
14770 #else
14771 /* In a release build, we want to degrade gracefully: better to
14772 generate incomplete debugging information than to crash. */
14773 return NULL;
14774 #endif
14777 if (!ret && !list_ret)
14778 return 0;
14780 if (want_address == 2 && !have_address
14781 && (dwarf_version >= 4 || !dwarf_strict))
14783 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14785 expansion_failed (loc, NULL_RTX,
14786 "DWARF address size mismatch");
14787 return 0;
14789 if (ret)
14790 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14791 else
14792 add_loc_descr_to_each (list_ret,
14793 new_loc_descr (DW_OP_stack_value, 0, 0));
14794 have_address = 1;
14796 /* Show if we can't fill the request for an address. */
14797 if (want_address && !have_address)
14799 expansion_failed (loc, NULL_RTX,
14800 "Want address and only have value");
14801 return 0;
14804 gcc_assert (!ret || !list_ret);
14806 /* If we've got an address and don't want one, dereference. */
14807 if (!want_address && have_address)
14809 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14811 if (size > DWARF2_ADDR_SIZE || size == -1)
14813 expansion_failed (loc, NULL_RTX,
14814 "DWARF address size mismatch");
14815 return 0;
14817 else if (size == DWARF2_ADDR_SIZE)
14818 op = DW_OP_deref;
14819 else
14820 op = DW_OP_deref_size;
14822 if (ret)
14823 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14824 else
14825 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14827 if (ret)
14828 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14830 return list_ret;
14833 /* Same as above but return only single location expression. */
14834 static dw_loc_descr_ref
14835 loc_descriptor_from_tree (tree loc, int want_address)
14837 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14838 if (!ret)
14839 return NULL;
14840 if (ret->dw_loc_next)
14842 expansion_failed (loc, NULL_RTX,
14843 "Location list where only loc descriptor needed");
14844 return NULL;
14846 return ret->expr;
14849 /* Given a value, round it up to the lowest multiple of `boundary'
14850 which is not less than the value itself. */
14852 static inline HOST_WIDE_INT
14853 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14855 return (((value + boundary - 1) / boundary) * boundary);
14858 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14859 pointer to the declared type for the relevant field variable, or return
14860 `integer_type_node' if the given node turns out to be an
14861 ERROR_MARK node. */
14863 static inline tree
14864 field_type (const_tree decl)
14866 tree type;
14868 if (TREE_CODE (decl) == ERROR_MARK)
14869 return integer_type_node;
14871 type = DECL_BIT_FIELD_TYPE (decl);
14872 if (type == NULL_TREE)
14873 type = TREE_TYPE (decl);
14875 return type;
14878 /* Given a pointer to a tree node, return the alignment in bits for
14879 it, or else return BITS_PER_WORD if the node actually turns out to
14880 be an ERROR_MARK node. */
14882 static inline unsigned
14883 simple_type_align_in_bits (const_tree type)
14885 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14888 static inline unsigned
14889 simple_decl_align_in_bits (const_tree decl)
14891 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14894 /* Return the result of rounding T up to ALIGN. */
14896 static inline offset_int
14897 round_up_to_align (const offset_int &t, unsigned int align)
14899 return wi::udiv_trunc (t + align - 1, align) * align;
14902 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14903 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14904 or return 0 if we are unable to determine what that offset is, either
14905 because the argument turns out to be a pointer to an ERROR_MARK node, or
14906 because the offset is actually variable. (We can't handle the latter case
14907 just yet). */
14909 static HOST_WIDE_INT
14910 field_byte_offset (const_tree decl)
14912 offset_int object_offset_in_bits;
14913 offset_int object_offset_in_bytes;
14914 offset_int bitpos_int;
14916 if (TREE_CODE (decl) == ERROR_MARK)
14917 return 0;
14919 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14921 /* We cannot yet cope with fields whose positions are variable, so
14922 for now, when we see such things, we simply return 0. Someday, we may
14923 be able to handle such cases, but it will be damn difficult. */
14924 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14925 return 0;
14927 bitpos_int = wi::to_offset (bit_position (decl));
14929 #ifdef PCC_BITFIELD_TYPE_MATTERS
14930 if (PCC_BITFIELD_TYPE_MATTERS)
14932 tree type;
14933 tree field_size_tree;
14934 offset_int deepest_bitpos;
14935 offset_int field_size_in_bits;
14936 unsigned int type_align_in_bits;
14937 unsigned int decl_align_in_bits;
14938 offset_int type_size_in_bits;
14940 type = field_type (decl);
14941 type_size_in_bits = offset_int_type_size_in_bits (type);
14942 type_align_in_bits = simple_type_align_in_bits (type);
14944 field_size_tree = DECL_SIZE (decl);
14946 /* The size could be unspecified if there was an error, or for
14947 a flexible array member. */
14948 if (!field_size_tree)
14949 field_size_tree = bitsize_zero_node;
14951 /* If the size of the field is not constant, use the type size. */
14952 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14953 field_size_in_bits = wi::to_offset (field_size_tree);
14954 else
14955 field_size_in_bits = type_size_in_bits;
14957 decl_align_in_bits = simple_decl_align_in_bits (decl);
14959 /* The GCC front-end doesn't make any attempt to keep track of the
14960 starting bit offset (relative to the start of the containing
14961 structure type) of the hypothetical "containing object" for a
14962 bit-field. Thus, when computing the byte offset value for the
14963 start of the "containing object" of a bit-field, we must deduce
14964 this information on our own. This can be rather tricky to do in
14965 some cases. For example, handling the following structure type
14966 definition when compiling for an i386/i486 target (which only
14967 aligns long long's to 32-bit boundaries) can be very tricky:
14969 struct S { int field1; long long field2:31; };
14971 Fortunately, there is a simple rule-of-thumb which can be used
14972 in such cases. When compiling for an i386/i486, GCC will
14973 allocate 8 bytes for the structure shown above. It decides to
14974 do this based upon one simple rule for bit-field allocation.
14975 GCC allocates each "containing object" for each bit-field at
14976 the first (i.e. lowest addressed) legitimate alignment boundary
14977 (based upon the required minimum alignment for the declared
14978 type of the field) which it can possibly use, subject to the
14979 condition that there is still enough available space remaining
14980 in the containing object (when allocated at the selected point)
14981 to fully accommodate all of the bits of the bit-field itself.
14983 This simple rule makes it obvious why GCC allocates 8 bytes for
14984 each object of the structure type shown above. When looking
14985 for a place to allocate the "containing object" for `field2',
14986 the compiler simply tries to allocate a 64-bit "containing
14987 object" at each successive 32-bit boundary (starting at zero)
14988 until it finds a place to allocate that 64- bit field such that
14989 at least 31 contiguous (and previously unallocated) bits remain
14990 within that selected 64 bit field. (As it turns out, for the
14991 example above, the compiler finds it is OK to allocate the
14992 "containing object" 64-bit field at bit-offset zero within the
14993 structure type.)
14995 Here we attempt to work backwards from the limited set of facts
14996 we're given, and we try to deduce from those facts, where GCC
14997 must have believed that the containing object started (within
14998 the structure type). The value we deduce is then used (by the
14999 callers of this routine) to generate DW_AT_location and
15000 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15001 the case of DW_AT_location, regular fields as well). */
15003 /* Figure out the bit-distance from the start of the structure to
15004 the "deepest" bit of the bit-field. */
15005 deepest_bitpos = bitpos_int + field_size_in_bits;
15007 /* This is the tricky part. Use some fancy footwork to deduce
15008 where the lowest addressed bit of the containing object must
15009 be. */
15010 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15012 /* Round up to type_align by default. This works best for
15013 bitfields. */
15014 object_offset_in_bits
15015 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15017 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15019 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15021 /* Round up to decl_align instead. */
15022 object_offset_in_bits
15023 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15026 else
15027 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15028 object_offset_in_bits = bitpos_int;
15030 object_offset_in_bytes
15031 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15032 return object_offset_in_bytes.to_shwi ();
15035 /* The following routines define various Dwarf attributes and any data
15036 associated with them. */
15038 /* Add a location description attribute value to a DIE.
15040 This emits location attributes suitable for whole variables and
15041 whole parameters. Note that the location attributes for struct fields are
15042 generated by the routine `data_member_location_attribute' below. */
15044 static inline void
15045 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15046 dw_loc_list_ref descr)
15048 if (descr == 0)
15049 return;
15050 if (single_element_loc_list_p (descr))
15051 add_AT_loc (die, attr_kind, descr->expr);
15052 else
15053 add_AT_loc_list (die, attr_kind, descr);
15056 /* Add DW_AT_accessibility attribute to DIE if needed. */
15058 static void
15059 add_accessibility_attribute (dw_die_ref die, tree decl)
15061 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15062 children, otherwise the default is DW_ACCESS_public. In DWARF2
15063 the default has always been DW_ACCESS_public. */
15064 if (TREE_PROTECTED (decl))
15065 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15066 else if (TREE_PRIVATE (decl))
15068 if (dwarf_version == 2
15069 || die->die_parent == NULL
15070 || die->die_parent->die_tag != DW_TAG_class_type)
15071 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15073 else if (dwarf_version > 2
15074 && die->die_parent
15075 && die->die_parent->die_tag == DW_TAG_class_type)
15076 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15079 /* Attach the specialized form of location attribute used for data members of
15080 struct and union types. In the special case of a FIELD_DECL node which
15081 represents a bit-field, the "offset" part of this special location
15082 descriptor must indicate the distance in bytes from the lowest-addressed
15083 byte of the containing struct or union type to the lowest-addressed byte of
15084 the "containing object" for the bit-field. (See the `field_byte_offset'
15085 function above).
15087 For any given bit-field, the "containing object" is a hypothetical object
15088 (of some integral or enum type) within which the given bit-field lives. The
15089 type of this hypothetical "containing object" is always the same as the
15090 declared type of the individual bit-field itself (for GCC anyway... the
15091 DWARF spec doesn't actually mandate this). Note that it is the size (in
15092 bytes) of the hypothetical "containing object" which will be given in the
15093 DW_AT_byte_size attribute for this bit-field. (See the
15094 `byte_size_attribute' function below.) It is also used when calculating the
15095 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15096 function below.) */
15098 static void
15099 add_data_member_location_attribute (dw_die_ref die, tree decl)
15101 HOST_WIDE_INT offset;
15102 dw_loc_descr_ref loc_descr = 0;
15104 if (TREE_CODE (decl) == TREE_BINFO)
15106 /* We're working on the TAG_inheritance for a base class. */
15107 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15109 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15110 aren't at a fixed offset from all (sub)objects of the same
15111 type. We need to extract the appropriate offset from our
15112 vtable. The following dwarf expression means
15114 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15116 This is specific to the V3 ABI, of course. */
15118 dw_loc_descr_ref tmp;
15120 /* Make a copy of the object address. */
15121 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15122 add_loc_descr (&loc_descr, tmp);
15124 /* Extract the vtable address. */
15125 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15126 add_loc_descr (&loc_descr, tmp);
15128 /* Calculate the address of the offset. */
15129 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15130 gcc_assert (offset < 0);
15132 tmp = int_loc_descriptor (-offset);
15133 add_loc_descr (&loc_descr, tmp);
15134 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15135 add_loc_descr (&loc_descr, tmp);
15137 /* Extract the offset. */
15138 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15139 add_loc_descr (&loc_descr, tmp);
15141 /* Add it to the object address. */
15142 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15143 add_loc_descr (&loc_descr, tmp);
15145 else
15146 offset = tree_to_shwi (BINFO_OFFSET (decl));
15148 else
15149 offset = field_byte_offset (decl);
15151 if (! loc_descr)
15153 if (dwarf_version > 2)
15155 /* Don't need to output a location expression, just the constant. */
15156 if (offset < 0)
15157 add_AT_int (die, DW_AT_data_member_location, offset);
15158 else
15159 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15160 return;
15162 else
15164 enum dwarf_location_atom op;
15166 /* The DWARF2 standard says that we should assume that the structure
15167 address is already on the stack, so we can specify a structure
15168 field address by using DW_OP_plus_uconst. */
15169 op = DW_OP_plus_uconst;
15170 loc_descr = new_loc_descr (op, offset, 0);
15174 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15177 /* Writes integer values to dw_vec_const array. */
15179 static void
15180 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15182 while (size != 0)
15184 *dest++ = val & 0xff;
15185 val >>= 8;
15186 --size;
15190 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15192 static HOST_WIDE_INT
15193 extract_int (const unsigned char *src, unsigned int size)
15195 HOST_WIDE_INT val = 0;
15197 src += size;
15198 while (size != 0)
15200 val <<= 8;
15201 val |= *--src & 0xff;
15202 --size;
15204 return val;
15207 /* Writes wide_int values to dw_vec_const array. */
15209 static void
15210 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15212 int i;
15214 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15216 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15217 return;
15220 /* We'd have to extend this code to support odd sizes. */
15221 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15223 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15225 if (WORDS_BIG_ENDIAN)
15226 for (i = n - 1; i >= 0; i--)
15228 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15229 dest += sizeof (HOST_WIDE_INT);
15231 else
15232 for (i = 0; i < n; i++)
15234 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15235 dest += sizeof (HOST_WIDE_INT);
15239 /* Writes floating point values to dw_vec_const array. */
15241 static void
15242 insert_float (const_rtx rtl, unsigned char *array)
15244 REAL_VALUE_TYPE rv;
15245 long val[4];
15246 int i;
15248 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15249 real_to_target (val, &rv, GET_MODE (rtl));
15251 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15252 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15254 insert_int (val[i], 4, array);
15255 array += 4;
15259 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15260 does not have a "location" either in memory or in a register. These
15261 things can arise in GNU C when a constant is passed as an actual parameter
15262 to an inlined function. They can also arise in C++ where declared
15263 constants do not necessarily get memory "homes". */
15265 static bool
15266 add_const_value_attribute (dw_die_ref die, rtx rtl)
15268 switch (GET_CODE (rtl))
15270 case CONST_INT:
15272 HOST_WIDE_INT val = INTVAL (rtl);
15274 if (val < 0)
15275 add_AT_int (die, DW_AT_const_value, val);
15276 else
15277 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15279 return true;
15281 case CONST_WIDE_INT:
15282 add_AT_wide (die, DW_AT_const_value,
15283 std::make_pair (rtl, GET_MODE (rtl)));
15284 return true;
15286 case CONST_DOUBLE:
15287 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15288 floating-point constant. A CONST_DOUBLE is used whenever the
15289 constant requires more than one word in order to be adequately
15290 represented. */
15292 enum machine_mode mode = GET_MODE (rtl);
15294 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15295 add_AT_double (die, DW_AT_const_value,
15296 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15297 else
15299 unsigned int length = GET_MODE_SIZE (mode);
15300 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15302 insert_float (rtl, array);
15303 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15306 return true;
15308 case CONST_VECTOR:
15310 enum machine_mode mode = GET_MODE (rtl);
15311 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15312 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15313 unsigned char *array
15314 = ggc_vec_alloc<unsigned char> (length * elt_size);
15315 unsigned int i;
15316 unsigned char *p;
15317 enum machine_mode imode = GET_MODE_INNER (mode);
15319 switch (GET_MODE_CLASS (mode))
15321 case MODE_VECTOR_INT:
15322 for (i = 0, p = array; i < length; i++, p += elt_size)
15324 rtx elt = CONST_VECTOR_ELT (rtl, i);
15325 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15327 break;
15329 case MODE_VECTOR_FLOAT:
15330 for (i = 0, p = array; i < length; i++, p += elt_size)
15332 rtx elt = CONST_VECTOR_ELT (rtl, i);
15333 insert_float (elt, p);
15335 break;
15337 default:
15338 gcc_unreachable ();
15341 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15343 return true;
15345 case CONST_STRING:
15346 if (dwarf_version >= 4 || !dwarf_strict)
15348 dw_loc_descr_ref loc_result;
15349 resolve_one_addr (&rtl);
15350 rtl_addr:
15351 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15352 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15353 add_AT_loc (die, DW_AT_location, loc_result);
15354 vec_safe_push (used_rtx_array, rtl);
15355 return true;
15357 return false;
15359 case CONST:
15360 if (CONSTANT_P (XEXP (rtl, 0)))
15361 return add_const_value_attribute (die, XEXP (rtl, 0));
15362 /* FALLTHROUGH */
15363 case SYMBOL_REF:
15364 if (!const_ok_for_output (rtl))
15365 return false;
15366 case LABEL_REF:
15367 if (dwarf_version >= 4 || !dwarf_strict)
15368 goto rtl_addr;
15369 return false;
15371 case PLUS:
15372 /* In cases where an inlined instance of an inline function is passed
15373 the address of an `auto' variable (which is local to the caller) we
15374 can get a situation where the DECL_RTL of the artificial local
15375 variable (for the inlining) which acts as a stand-in for the
15376 corresponding formal parameter (of the inline function) will look
15377 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15378 exactly a compile-time constant expression, but it isn't the address
15379 of the (artificial) local variable either. Rather, it represents the
15380 *value* which the artificial local variable always has during its
15381 lifetime. We currently have no way to represent such quasi-constant
15382 values in Dwarf, so for now we just punt and generate nothing. */
15383 return false;
15385 case HIGH:
15386 case CONST_FIXED:
15387 return false;
15389 case MEM:
15390 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15391 && MEM_READONLY_P (rtl)
15392 && GET_MODE (rtl) == BLKmode)
15394 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15395 return true;
15397 return false;
15399 default:
15400 /* No other kinds of rtx should be possible here. */
15401 gcc_unreachable ();
15403 return false;
15406 /* Determine whether the evaluation of EXPR references any variables
15407 or functions which aren't otherwise used (and therefore may not be
15408 output). */
15409 static tree
15410 reference_to_unused (tree * tp, int * walk_subtrees,
15411 void * data ATTRIBUTE_UNUSED)
15413 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15414 *walk_subtrees = 0;
15416 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15417 && ! TREE_ASM_WRITTEN (*tp))
15418 return *tp;
15419 /* ??? The C++ FE emits debug information for using decls, so
15420 putting gcc_unreachable here falls over. See PR31899. For now
15421 be conservative. */
15422 else if (!symtab->global_info_ready
15423 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15424 return *tp;
15425 else if (TREE_CODE (*tp) == VAR_DECL)
15427 varpool_node *node = varpool_node::get (*tp);
15428 if (!node || !node->definition)
15429 return *tp;
15431 else if (TREE_CODE (*tp) == FUNCTION_DECL
15432 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15434 /* The call graph machinery must have finished analyzing,
15435 optimizing and gimplifying the CU by now.
15436 So if *TP has no call graph node associated
15437 to it, it means *TP will not be emitted. */
15438 if (!cgraph_node::get (*tp))
15439 return *tp;
15441 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15442 return *tp;
15444 return NULL_TREE;
15447 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15448 for use in a later add_const_value_attribute call. */
15450 static rtx
15451 rtl_for_decl_init (tree init, tree type)
15453 rtx rtl = NULL_RTX;
15455 STRIP_NOPS (init);
15457 /* If a variable is initialized with a string constant without embedded
15458 zeros, build CONST_STRING. */
15459 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15461 tree enttype = TREE_TYPE (type);
15462 tree domain = TYPE_DOMAIN (type);
15463 enum machine_mode mode = TYPE_MODE (enttype);
15465 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15466 && domain
15467 && integer_zerop (TYPE_MIN_VALUE (domain))
15468 && compare_tree_int (TYPE_MAX_VALUE (domain),
15469 TREE_STRING_LENGTH (init) - 1) == 0
15470 && ((size_t) TREE_STRING_LENGTH (init)
15471 == strlen (TREE_STRING_POINTER (init)) + 1))
15473 rtl = gen_rtx_CONST_STRING (VOIDmode,
15474 ggc_strdup (TREE_STRING_POINTER (init)));
15475 rtl = gen_rtx_MEM (BLKmode, rtl);
15476 MEM_READONLY_P (rtl) = 1;
15479 /* Other aggregates, and complex values, could be represented using
15480 CONCAT: FIXME! */
15481 else if (AGGREGATE_TYPE_P (type)
15482 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15483 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15484 || TREE_CODE (type) == COMPLEX_TYPE)
15486 /* Vectors only work if their mode is supported by the target.
15487 FIXME: generic vectors ought to work too. */
15488 else if (TREE_CODE (type) == VECTOR_TYPE
15489 && !VECTOR_MODE_P (TYPE_MODE (type)))
15491 /* If the initializer is something that we know will expand into an
15492 immediate RTL constant, expand it now. We must be careful not to
15493 reference variables which won't be output. */
15494 else if (initializer_constant_valid_p (init, type)
15495 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15497 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15498 possible. */
15499 if (TREE_CODE (type) == VECTOR_TYPE)
15500 switch (TREE_CODE (init))
15502 case VECTOR_CST:
15503 break;
15504 case CONSTRUCTOR:
15505 if (TREE_CONSTANT (init))
15507 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15508 bool constant_p = true;
15509 tree value;
15510 unsigned HOST_WIDE_INT ix;
15512 /* Even when ctor is constant, it might contain non-*_CST
15513 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15514 belong into VECTOR_CST nodes. */
15515 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15516 if (!CONSTANT_CLASS_P (value))
15518 constant_p = false;
15519 break;
15522 if (constant_p)
15524 init = build_vector_from_ctor (type, elts);
15525 break;
15528 /* FALLTHRU */
15530 default:
15531 return NULL;
15534 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15536 /* If expand_expr returns a MEM, it wasn't immediate. */
15537 gcc_assert (!rtl || !MEM_P (rtl));
15540 return rtl;
15543 /* Generate RTL for the variable DECL to represent its location. */
15545 static rtx
15546 rtl_for_decl_location (tree decl)
15548 rtx rtl;
15550 /* Here we have to decide where we are going to say the parameter "lives"
15551 (as far as the debugger is concerned). We only have a couple of
15552 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15554 DECL_RTL normally indicates where the parameter lives during most of the
15555 activation of the function. If optimization is enabled however, this
15556 could be either NULL or else a pseudo-reg. Both of those cases indicate
15557 that the parameter doesn't really live anywhere (as far as the code
15558 generation parts of GCC are concerned) during most of the function's
15559 activation. That will happen (for example) if the parameter is never
15560 referenced within the function.
15562 We could just generate a location descriptor here for all non-NULL
15563 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15564 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15565 where DECL_RTL is NULL or is a pseudo-reg.
15567 Note however that we can only get away with using DECL_INCOMING_RTL as
15568 a backup substitute for DECL_RTL in certain limited cases. In cases
15569 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15570 we can be sure that the parameter was passed using the same type as it is
15571 declared to have within the function, and that its DECL_INCOMING_RTL
15572 points us to a place where a value of that type is passed.
15574 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15575 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15576 because in these cases DECL_INCOMING_RTL points us to a value of some
15577 type which is *different* from the type of the parameter itself. Thus,
15578 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15579 such cases, the debugger would end up (for example) trying to fetch a
15580 `float' from a place which actually contains the first part of a
15581 `double'. That would lead to really incorrect and confusing
15582 output at debug-time.
15584 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15585 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15586 are a couple of exceptions however. On little-endian machines we can
15587 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15588 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15589 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15590 when (on a little-endian machine) a non-prototyped function has a
15591 parameter declared to be of type `short' or `char'. In such cases,
15592 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15593 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15594 passed `int' value. If the debugger then uses that address to fetch
15595 a `short' or a `char' (on a little-endian machine) the result will be
15596 the correct data, so we allow for such exceptional cases below.
15598 Note that our goal here is to describe the place where the given formal
15599 parameter lives during most of the function's activation (i.e. between the
15600 end of the prologue and the start of the epilogue). We'll do that as best
15601 as we can. Note however that if the given formal parameter is modified
15602 sometime during the execution of the function, then a stack backtrace (at
15603 debug-time) will show the function as having been called with the *new*
15604 value rather than the value which was originally passed in. This happens
15605 rarely enough that it is not a major problem, but it *is* a problem, and
15606 I'd like to fix it.
15608 A future version of dwarf2out.c may generate two additional attributes for
15609 any given DW_TAG_formal_parameter DIE which will describe the "passed
15610 type" and the "passed location" for the given formal parameter in addition
15611 to the attributes we now generate to indicate the "declared type" and the
15612 "active location" for each parameter. This additional set of attributes
15613 could be used by debuggers for stack backtraces. Separately, note that
15614 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15615 This happens (for example) for inlined-instances of inline function formal
15616 parameters which are never referenced. This really shouldn't be
15617 happening. All PARM_DECL nodes should get valid non-NULL
15618 DECL_INCOMING_RTL values. FIXME. */
15620 /* Use DECL_RTL as the "location" unless we find something better. */
15621 rtl = DECL_RTL_IF_SET (decl);
15623 /* When generating abstract instances, ignore everything except
15624 constants, symbols living in memory, and symbols living in
15625 fixed registers. */
15626 if (! reload_completed)
15628 if (rtl
15629 && (CONSTANT_P (rtl)
15630 || (MEM_P (rtl)
15631 && CONSTANT_P (XEXP (rtl, 0)))
15632 || (REG_P (rtl)
15633 && TREE_CODE (decl) == VAR_DECL
15634 && TREE_STATIC (decl))))
15636 rtl = targetm.delegitimize_address (rtl);
15637 return rtl;
15639 rtl = NULL_RTX;
15641 else if (TREE_CODE (decl) == PARM_DECL)
15643 if (rtl == NULL_RTX
15644 || is_pseudo_reg (rtl)
15645 || (MEM_P (rtl)
15646 && is_pseudo_reg (XEXP (rtl, 0))
15647 && DECL_INCOMING_RTL (decl)
15648 && MEM_P (DECL_INCOMING_RTL (decl))
15649 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15651 tree declared_type = TREE_TYPE (decl);
15652 tree passed_type = DECL_ARG_TYPE (decl);
15653 enum machine_mode dmode = TYPE_MODE (declared_type);
15654 enum machine_mode pmode = TYPE_MODE (passed_type);
15656 /* This decl represents a formal parameter which was optimized out.
15657 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15658 all cases where (rtl == NULL_RTX) just below. */
15659 if (dmode == pmode)
15660 rtl = DECL_INCOMING_RTL (decl);
15661 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15662 && SCALAR_INT_MODE_P (dmode)
15663 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15664 && DECL_INCOMING_RTL (decl))
15666 rtx inc = DECL_INCOMING_RTL (decl);
15667 if (REG_P (inc))
15668 rtl = inc;
15669 else if (MEM_P (inc))
15671 if (BYTES_BIG_ENDIAN)
15672 rtl = adjust_address_nv (inc, dmode,
15673 GET_MODE_SIZE (pmode)
15674 - GET_MODE_SIZE (dmode));
15675 else
15676 rtl = inc;
15681 /* If the parm was passed in registers, but lives on the stack, then
15682 make a big endian correction if the mode of the type of the
15683 parameter is not the same as the mode of the rtl. */
15684 /* ??? This is the same series of checks that are made in dbxout.c before
15685 we reach the big endian correction code there. It isn't clear if all
15686 of these checks are necessary here, but keeping them all is the safe
15687 thing to do. */
15688 else if (MEM_P (rtl)
15689 && XEXP (rtl, 0) != const0_rtx
15690 && ! CONSTANT_P (XEXP (rtl, 0))
15691 /* Not passed in memory. */
15692 && !MEM_P (DECL_INCOMING_RTL (decl))
15693 /* Not passed by invisible reference. */
15694 && (!REG_P (XEXP (rtl, 0))
15695 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15696 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15697 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15698 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15699 #endif
15701 /* Big endian correction check. */
15702 && BYTES_BIG_ENDIAN
15703 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15704 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15705 < UNITS_PER_WORD))
15707 enum machine_mode addr_mode = get_address_mode (rtl);
15708 int offset = (UNITS_PER_WORD
15709 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15711 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15712 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15715 else if (TREE_CODE (decl) == VAR_DECL
15716 && rtl
15717 && MEM_P (rtl)
15718 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15719 && BYTES_BIG_ENDIAN)
15721 enum machine_mode addr_mode = get_address_mode (rtl);
15722 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15723 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15725 /* If a variable is declared "register" yet is smaller than
15726 a register, then if we store the variable to memory, it
15727 looks like we're storing a register-sized value, when in
15728 fact we are not. We need to adjust the offset of the
15729 storage location to reflect the actual value's bytes,
15730 else gdb will not be able to display it. */
15731 if (rsize > dsize)
15732 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15733 plus_constant (addr_mode, XEXP (rtl, 0),
15734 rsize - dsize));
15737 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15738 and will have been substituted directly into all expressions that use it.
15739 C does not have such a concept, but C++ and other languages do. */
15740 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15741 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15743 if (rtl)
15744 rtl = targetm.delegitimize_address (rtl);
15746 /* If we don't look past the constant pool, we risk emitting a
15747 reference to a constant pool entry that isn't referenced from
15748 code, and thus is not emitted. */
15749 if (rtl)
15750 rtl = avoid_constant_pool_reference (rtl);
15752 /* Try harder to get a rtl. If this symbol ends up not being emitted
15753 in the current CU, resolve_addr will remove the expression referencing
15754 it. */
15755 if (rtl == NULL_RTX
15756 && TREE_CODE (decl) == VAR_DECL
15757 && !DECL_EXTERNAL (decl)
15758 && TREE_STATIC (decl)
15759 && DECL_NAME (decl)
15760 && !DECL_HARD_REGISTER (decl)
15761 && DECL_MODE (decl) != VOIDmode)
15763 rtl = make_decl_rtl_for_debug (decl);
15764 if (!MEM_P (rtl)
15765 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15766 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15767 rtl = NULL_RTX;
15770 return rtl;
15773 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15774 returned. If so, the decl for the COMMON block is returned, and the
15775 value is the offset into the common block for the symbol. */
15777 static tree
15778 fortran_common (tree decl, HOST_WIDE_INT *value)
15780 tree val_expr, cvar;
15781 enum machine_mode mode;
15782 HOST_WIDE_INT bitsize, bitpos;
15783 tree offset;
15784 int unsignedp, volatilep = 0;
15786 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15787 it does not have a value (the offset into the common area), or if it
15788 is thread local (as opposed to global) then it isn't common, and shouldn't
15789 be handled as such. */
15790 if (TREE_CODE (decl) != VAR_DECL
15791 || !TREE_STATIC (decl)
15792 || !DECL_HAS_VALUE_EXPR_P (decl)
15793 || !is_fortran ())
15794 return NULL_TREE;
15796 val_expr = DECL_VALUE_EXPR (decl);
15797 if (TREE_CODE (val_expr) != COMPONENT_REF)
15798 return NULL_TREE;
15800 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15801 &mode, &unsignedp, &volatilep, true);
15803 if (cvar == NULL_TREE
15804 || TREE_CODE (cvar) != VAR_DECL
15805 || DECL_ARTIFICIAL (cvar)
15806 || !TREE_PUBLIC (cvar))
15807 return NULL_TREE;
15809 *value = 0;
15810 if (offset != NULL)
15812 if (!tree_fits_shwi_p (offset))
15813 return NULL_TREE;
15814 *value = tree_to_shwi (offset);
15816 if (bitpos != 0)
15817 *value += bitpos / BITS_PER_UNIT;
15819 return cvar;
15822 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15823 data attribute for a variable or a parameter. We generate the
15824 DW_AT_const_value attribute only in those cases where the given variable
15825 or parameter does not have a true "location" either in memory or in a
15826 register. This can happen (for example) when a constant is passed as an
15827 actual argument in a call to an inline function. (It's possible that
15828 these things can crop up in other ways also.) Note that one type of
15829 constant value which can be passed into an inlined function is a constant
15830 pointer. This can happen for example if an actual argument in an inlined
15831 function call evaluates to a compile-time constant address.
15833 CACHE_P is true if it is worth caching the location list for DECL,
15834 so that future calls can reuse it rather than regenerate it from scratch.
15835 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15836 since we will need to refer to them each time the function is inlined. */
15838 static bool
15839 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15840 enum dwarf_attribute attr)
15842 rtx rtl;
15843 dw_loc_list_ref list;
15844 var_loc_list *loc_list;
15845 cached_dw_loc_list *cache;
15847 if (TREE_CODE (decl) == ERROR_MARK)
15848 return false;
15850 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15851 || TREE_CODE (decl) == RESULT_DECL);
15853 /* Try to get some constant RTL for this decl, and use that as the value of
15854 the location. */
15856 rtl = rtl_for_decl_location (decl);
15857 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15858 && add_const_value_attribute (die, rtl))
15859 return true;
15861 /* See if we have single element location list that is equivalent to
15862 a constant value. That way we are better to use add_const_value_attribute
15863 rather than expanding constant value equivalent. */
15864 loc_list = lookup_decl_loc (decl);
15865 if (loc_list
15866 && loc_list->first
15867 && loc_list->first->next == NULL
15868 && NOTE_P (loc_list->first->loc)
15869 && NOTE_VAR_LOCATION (loc_list->first->loc)
15870 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15872 struct var_loc_node *node;
15874 node = loc_list->first;
15875 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15876 if (GET_CODE (rtl) == EXPR_LIST)
15877 rtl = XEXP (rtl, 0);
15878 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15879 && add_const_value_attribute (die, rtl))
15880 return true;
15882 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15883 list several times. See if we've already cached the contents. */
15884 list = NULL;
15885 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15886 cache_p = false;
15887 if (cache_p)
15889 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
15890 if (cache)
15891 list = cache->loc_list;
15893 if (list == NULL)
15895 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15896 /* It is usually worth caching this result if the decl is from
15897 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15898 if (cache_p && list && list->dw_loc_next)
15900 cached_dw_loc_list **slot
15901 = cached_dw_loc_list_table->find_slot_with_hash (decl,
15902 DECL_UID (decl),
15903 INSERT);
15904 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15905 cache->decl_id = DECL_UID (decl);
15906 cache->loc_list = list;
15907 *slot = cache;
15910 if (list)
15912 add_AT_location_description (die, attr, list);
15913 return true;
15915 /* None of that worked, so it must not really have a location;
15916 try adding a constant value attribute from the DECL_INITIAL. */
15917 return tree_add_const_value_attribute_for_decl (die, decl);
15920 /* Add VARIABLE and DIE into deferred locations list. */
15922 static void
15923 defer_location (tree variable, dw_die_ref die)
15925 deferred_locations entry;
15926 entry.variable = variable;
15927 entry.die = die;
15928 vec_safe_push (deferred_locations_list, entry);
15931 /* Helper function for tree_add_const_value_attribute. Natively encode
15932 initializer INIT into an array. Return true if successful. */
15934 static bool
15935 native_encode_initializer (tree init, unsigned char *array, int size)
15937 tree type;
15939 if (init == NULL_TREE)
15940 return false;
15942 STRIP_NOPS (init);
15943 switch (TREE_CODE (init))
15945 case STRING_CST:
15946 type = TREE_TYPE (init);
15947 if (TREE_CODE (type) == ARRAY_TYPE)
15949 tree enttype = TREE_TYPE (type);
15950 enum machine_mode mode = TYPE_MODE (enttype);
15952 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15953 return false;
15954 if (int_size_in_bytes (type) != size)
15955 return false;
15956 if (size > TREE_STRING_LENGTH (init))
15958 memcpy (array, TREE_STRING_POINTER (init),
15959 TREE_STRING_LENGTH (init));
15960 memset (array + TREE_STRING_LENGTH (init),
15961 '\0', size - TREE_STRING_LENGTH (init));
15963 else
15964 memcpy (array, TREE_STRING_POINTER (init), size);
15965 return true;
15967 return false;
15968 case CONSTRUCTOR:
15969 type = TREE_TYPE (init);
15970 if (int_size_in_bytes (type) != size)
15971 return false;
15972 if (TREE_CODE (type) == ARRAY_TYPE)
15974 HOST_WIDE_INT min_index;
15975 unsigned HOST_WIDE_INT cnt;
15976 int curpos = 0, fieldsize;
15977 constructor_elt *ce;
15979 if (TYPE_DOMAIN (type) == NULL_TREE
15980 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15981 return false;
15983 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15984 if (fieldsize <= 0)
15985 return false;
15987 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15988 memset (array, '\0', size);
15989 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15991 tree val = ce->value;
15992 tree index = ce->index;
15993 int pos = curpos;
15994 if (index && TREE_CODE (index) == RANGE_EXPR)
15995 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15996 * fieldsize;
15997 else if (index)
15998 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16000 if (val)
16002 STRIP_NOPS (val);
16003 if (!native_encode_initializer (val, array + pos, fieldsize))
16004 return false;
16006 curpos = pos + fieldsize;
16007 if (index && TREE_CODE (index) == RANGE_EXPR)
16009 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16010 - tree_to_shwi (TREE_OPERAND (index, 0));
16011 while (count-- > 0)
16013 if (val)
16014 memcpy (array + curpos, array + pos, fieldsize);
16015 curpos += fieldsize;
16018 gcc_assert (curpos <= size);
16020 return true;
16022 else if (TREE_CODE (type) == RECORD_TYPE
16023 || TREE_CODE (type) == UNION_TYPE)
16025 tree field = NULL_TREE;
16026 unsigned HOST_WIDE_INT cnt;
16027 constructor_elt *ce;
16029 if (int_size_in_bytes (type) != size)
16030 return false;
16032 if (TREE_CODE (type) == RECORD_TYPE)
16033 field = TYPE_FIELDS (type);
16035 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16037 tree val = ce->value;
16038 int pos, fieldsize;
16040 if (ce->index != 0)
16041 field = ce->index;
16043 if (val)
16044 STRIP_NOPS (val);
16046 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16047 return false;
16049 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16050 && TYPE_DOMAIN (TREE_TYPE (field))
16051 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16052 return false;
16053 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16054 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16055 return false;
16056 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16057 pos = int_byte_position (field);
16058 gcc_assert (pos + fieldsize <= size);
16059 if (val
16060 && !native_encode_initializer (val, array + pos, fieldsize))
16061 return false;
16063 return true;
16065 return false;
16066 case VIEW_CONVERT_EXPR:
16067 case NON_LVALUE_EXPR:
16068 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16069 default:
16070 return native_encode_expr (init, array, size) == size;
16074 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16075 attribute is the const value T. */
16077 static bool
16078 tree_add_const_value_attribute (dw_die_ref die, tree t)
16080 tree init;
16081 tree type = TREE_TYPE (t);
16082 rtx rtl;
16084 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16085 return false;
16087 init = t;
16088 gcc_assert (!DECL_P (init));
16090 rtl = rtl_for_decl_init (init, type);
16091 if (rtl)
16092 return add_const_value_attribute (die, rtl);
16093 /* If the host and target are sane, try harder. */
16094 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16095 && initializer_constant_valid_p (init, type))
16097 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16098 if (size > 0 && (int) size == size)
16100 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16102 if (native_encode_initializer (init, array, size))
16104 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16105 return true;
16107 ggc_free (array);
16110 return false;
16113 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16114 attribute is the const value of T, where T is an integral constant
16115 variable with static storage duration
16116 (so it can't be a PARM_DECL or a RESULT_DECL). */
16118 static bool
16119 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16122 if (!decl
16123 || (TREE_CODE (decl) != VAR_DECL
16124 && TREE_CODE (decl) != CONST_DECL)
16125 || (TREE_CODE (decl) == VAR_DECL
16126 && !TREE_STATIC (decl)))
16127 return false;
16129 if (TREE_READONLY (decl)
16130 && ! TREE_THIS_VOLATILE (decl)
16131 && DECL_INITIAL (decl))
16132 /* OK */;
16133 else
16134 return false;
16136 /* Don't add DW_AT_const_value if abstract origin already has one. */
16137 if (get_AT (var_die, DW_AT_const_value))
16138 return false;
16140 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16143 /* Convert the CFI instructions for the current function into a
16144 location list. This is used for DW_AT_frame_base when we targeting
16145 a dwarf2 consumer that does not support the dwarf3
16146 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16147 expressions. */
16149 static dw_loc_list_ref
16150 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16152 int ix;
16153 dw_fde_ref fde;
16154 dw_loc_list_ref list, *list_tail;
16155 dw_cfi_ref cfi;
16156 dw_cfa_location last_cfa, next_cfa;
16157 const char *start_label, *last_label, *section;
16158 dw_cfa_location remember;
16160 fde = cfun->fde;
16161 gcc_assert (fde != NULL);
16163 section = secname_for_decl (current_function_decl);
16164 list_tail = &list;
16165 list = NULL;
16167 memset (&next_cfa, 0, sizeof (next_cfa));
16168 next_cfa.reg = INVALID_REGNUM;
16169 remember = next_cfa;
16171 start_label = fde->dw_fde_begin;
16173 /* ??? Bald assumption that the CIE opcode list does not contain
16174 advance opcodes. */
16175 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16176 lookup_cfa_1 (cfi, &next_cfa, &remember);
16178 last_cfa = next_cfa;
16179 last_label = start_label;
16181 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16183 /* If the first partition contained no CFI adjustments, the
16184 CIE opcodes apply to the whole first partition. */
16185 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16186 fde->dw_fde_begin, fde->dw_fde_end, section);
16187 list_tail =&(*list_tail)->dw_loc_next;
16188 start_label = last_label = fde->dw_fde_second_begin;
16191 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16193 switch (cfi->dw_cfi_opc)
16195 case DW_CFA_set_loc:
16196 case DW_CFA_advance_loc1:
16197 case DW_CFA_advance_loc2:
16198 case DW_CFA_advance_loc4:
16199 if (!cfa_equal_p (&last_cfa, &next_cfa))
16201 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16202 start_label, last_label, section);
16204 list_tail = &(*list_tail)->dw_loc_next;
16205 last_cfa = next_cfa;
16206 start_label = last_label;
16208 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16209 break;
16211 case DW_CFA_advance_loc:
16212 /* The encoding is complex enough that we should never emit this. */
16213 gcc_unreachable ();
16215 default:
16216 lookup_cfa_1 (cfi, &next_cfa, &remember);
16217 break;
16219 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16221 if (!cfa_equal_p (&last_cfa, &next_cfa))
16223 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16224 start_label, last_label, section);
16226 list_tail = &(*list_tail)->dw_loc_next;
16227 last_cfa = next_cfa;
16228 start_label = last_label;
16230 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16231 start_label, fde->dw_fde_end, section);
16232 list_tail = &(*list_tail)->dw_loc_next;
16233 start_label = last_label = fde->dw_fde_second_begin;
16237 if (!cfa_equal_p (&last_cfa, &next_cfa))
16239 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16240 start_label, last_label, section);
16241 list_tail = &(*list_tail)->dw_loc_next;
16242 start_label = last_label;
16245 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16246 start_label,
16247 fde->dw_fde_second_begin
16248 ? fde->dw_fde_second_end : fde->dw_fde_end,
16249 section);
16251 if (list && list->dw_loc_next)
16252 gen_llsym (list);
16254 return list;
16257 /* Compute a displacement from the "steady-state frame pointer" to the
16258 frame base (often the same as the CFA), and store it in
16259 frame_pointer_fb_offset. OFFSET is added to the displacement
16260 before the latter is negated. */
16262 static void
16263 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16265 rtx reg, elim;
16267 #ifdef FRAME_POINTER_CFA_OFFSET
16268 reg = frame_pointer_rtx;
16269 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16270 #else
16271 reg = arg_pointer_rtx;
16272 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16273 #endif
16275 elim = (ira_use_lra_p
16276 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16277 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16278 if (GET_CODE (elim) == PLUS)
16280 offset += INTVAL (XEXP (elim, 1));
16281 elim = XEXP (elim, 0);
16284 frame_pointer_fb_offset = -offset;
16286 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16287 in which to eliminate. This is because it's stack pointer isn't
16288 directly accessible as a register within the ISA. To work around
16289 this, assume that while we cannot provide a proper value for
16290 frame_pointer_fb_offset, we won't need one either. */
16291 frame_pointer_fb_offset_valid
16292 = ((SUPPORTS_STACK_ALIGNMENT
16293 && (elim == hard_frame_pointer_rtx
16294 || elim == stack_pointer_rtx))
16295 || elim == (frame_pointer_needed
16296 ? hard_frame_pointer_rtx
16297 : stack_pointer_rtx));
16300 /* Generate a DW_AT_name attribute given some string value to be included as
16301 the value of the attribute. */
16303 static void
16304 add_name_attribute (dw_die_ref die, const char *name_string)
16306 if (name_string != NULL && *name_string != 0)
16308 if (demangle_name_func)
16309 name_string = (*demangle_name_func) (name_string);
16311 add_AT_string (die, DW_AT_name, name_string);
16315 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16316 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16317 of TYPE accordingly.
16319 ??? This is a temporary measure until after we're able to generate
16320 regular DWARF for the complex Ada type system. */
16322 static void
16323 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16324 dw_die_ref context_die)
16326 tree dtype;
16327 dw_die_ref dtype_die;
16329 if (!lang_hooks.types.descriptive_type)
16330 return;
16332 dtype = lang_hooks.types.descriptive_type (type);
16333 if (!dtype)
16334 return;
16336 dtype_die = lookup_type_die (dtype);
16337 if (!dtype_die)
16339 gen_type_die (dtype, context_die);
16340 dtype_die = lookup_type_die (dtype);
16341 gcc_assert (dtype_die);
16344 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16347 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16349 static const char *
16350 comp_dir_string (void)
16352 const char *wd;
16353 char *wd1;
16354 static const char *cached_wd = NULL;
16356 if (cached_wd != NULL)
16357 return cached_wd;
16359 wd = get_src_pwd ();
16360 if (wd == NULL)
16361 return NULL;
16363 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16365 int wdlen;
16367 wdlen = strlen (wd);
16368 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16369 strcpy (wd1, wd);
16370 wd1 [wdlen] = DIR_SEPARATOR;
16371 wd1 [wdlen + 1] = 0;
16372 wd = wd1;
16375 cached_wd = remap_debug_filename (wd);
16376 return cached_wd;
16379 /* Generate a DW_AT_comp_dir attribute for DIE. */
16381 static void
16382 add_comp_dir_attribute (dw_die_ref die)
16384 const char * wd = comp_dir_string ();
16385 if (wd != NULL)
16386 add_AT_string (die, DW_AT_comp_dir, wd);
16389 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16390 default. */
16392 static int
16393 lower_bound_default (void)
16395 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16397 case DW_LANG_C:
16398 case DW_LANG_C89:
16399 case DW_LANG_C99:
16400 case DW_LANG_C_plus_plus:
16401 case DW_LANG_ObjC:
16402 case DW_LANG_ObjC_plus_plus:
16403 case DW_LANG_Java:
16404 return 0;
16405 case DW_LANG_Fortran77:
16406 case DW_LANG_Fortran90:
16407 case DW_LANG_Fortran95:
16408 return 1;
16409 case DW_LANG_UPC:
16410 case DW_LANG_D:
16411 case DW_LANG_Python:
16412 return dwarf_version >= 4 ? 0 : -1;
16413 case DW_LANG_Ada95:
16414 case DW_LANG_Ada83:
16415 case DW_LANG_Cobol74:
16416 case DW_LANG_Cobol85:
16417 case DW_LANG_Pascal83:
16418 case DW_LANG_Modula2:
16419 case DW_LANG_PLI:
16420 return dwarf_version >= 4 ? 1 : -1;
16421 default:
16422 return -1;
16426 /* Given a tree node describing an array bound (either lower or upper) output
16427 a representation for that bound. */
16429 static void
16430 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16432 switch (TREE_CODE (bound))
16434 case ERROR_MARK:
16435 return;
16437 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16438 case INTEGER_CST:
16440 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16441 int dflt;
16443 /* Use the default if possible. */
16444 if (bound_attr == DW_AT_lower_bound
16445 && tree_fits_shwi_p (bound)
16446 && (dflt = lower_bound_default ()) != -1
16447 && tree_to_shwi (bound) == dflt)
16450 /* If HOST_WIDE_INT is big enough then represent the bound as
16451 a constant value. We need to choose a form based on
16452 whether the type is signed or unsigned. We cannot just
16453 call add_AT_unsigned if the value itself is positive
16454 (add_AT_unsigned might add the unsigned value encoded as
16455 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16456 bounds type and then sign extend any unsigned values found
16457 for signed types. This is needed only for
16458 DW_AT_{lower,upper}_bound, since for most other attributes,
16459 consumers will treat DW_FORM_data[1248] as unsigned values,
16460 regardless of the underlying type. */
16461 else if (prec <= HOST_BITS_PER_WIDE_INT
16462 || tree_fits_uhwi_p (bound))
16464 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16465 add_AT_unsigned (subrange_die, bound_attr,
16466 TREE_INT_CST_LOW (bound));
16467 else
16468 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16470 else
16471 /* Otherwise represent the bound as an unsigned value with
16472 the precision of its type. The precision and signedness
16473 of the type will be necessary to re-interpret it
16474 unambiguously. */
16475 add_AT_wide (subrange_die, bound_attr, bound);
16477 break;
16479 CASE_CONVERT:
16480 case VIEW_CONVERT_EXPR:
16481 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16482 break;
16484 case SAVE_EXPR:
16485 break;
16487 case VAR_DECL:
16488 case PARM_DECL:
16489 case RESULT_DECL:
16491 dw_die_ref decl_die = lookup_decl_die (bound);
16493 /* ??? Can this happen, or should the variable have been bound
16494 first? Probably it can, since I imagine that we try to create
16495 the types of parameters in the order in which they exist in
16496 the list, and won't have created a forward reference to a
16497 later parameter. */
16498 if (decl_die != NULL)
16500 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16501 break;
16504 /* FALLTHRU */
16506 default:
16508 /* Otherwise try to create a stack operation procedure to
16509 evaluate the value of the array bound. */
16511 dw_die_ref ctx, decl_die;
16512 dw_loc_list_ref list;
16514 list = loc_list_from_tree (bound, 2);
16515 if (list == NULL || single_element_loc_list_p (list))
16517 /* If DW_AT_*bound is not a reference nor constant, it is
16518 a DWARF expression rather than location description.
16519 For that loc_list_from_tree (bound, 0) is needed.
16520 If that fails to give a single element list,
16521 fall back to outputting this as a reference anyway. */
16522 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16523 if (list2 && single_element_loc_list_p (list2))
16525 add_AT_loc (subrange_die, bound_attr, list2->expr);
16526 break;
16529 if (list == NULL)
16530 break;
16532 if (current_function_decl == 0)
16533 ctx = comp_unit_die ();
16534 else
16535 ctx = lookup_decl_die (current_function_decl);
16537 decl_die = new_die (DW_TAG_variable, ctx, bound);
16538 add_AT_flag (decl_die, DW_AT_artificial, 1);
16539 add_type_attribute (decl_die, TREE_TYPE (bound), TYPE_QUAL_CONST, ctx);
16540 add_AT_location_description (decl_die, DW_AT_location, list);
16541 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16542 break;
16547 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16548 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16549 Note that the block of subscript information for an array type also
16550 includes information about the element type of the given array type. */
16552 static void
16553 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16555 unsigned dimension_number;
16556 tree lower, upper;
16557 dw_die_ref subrange_die;
16559 for (dimension_number = 0;
16560 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16561 type = TREE_TYPE (type), dimension_number++)
16563 tree domain = TYPE_DOMAIN (type);
16565 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16566 break;
16568 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16569 and (in GNU C only) variable bounds. Handle all three forms
16570 here. */
16571 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16572 if (domain)
16574 /* We have an array type with specified bounds. */
16575 lower = TYPE_MIN_VALUE (domain);
16576 upper = TYPE_MAX_VALUE (domain);
16578 /* Define the index type. */
16579 if (TREE_TYPE (domain))
16581 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16582 TREE_TYPE field. We can't emit debug info for this
16583 because it is an unnamed integral type. */
16584 if (TREE_CODE (domain) == INTEGER_TYPE
16585 && TYPE_NAME (domain) == NULL_TREE
16586 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16587 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16589 else
16590 add_type_attribute (subrange_die, TREE_TYPE (domain),
16591 TYPE_UNQUALIFIED, type_die);
16594 /* ??? If upper is NULL, the array has unspecified length,
16595 but it does have a lower bound. This happens with Fortran
16596 dimension arr(N:*)
16597 Since the debugger is definitely going to need to know N
16598 to produce useful results, go ahead and output the lower
16599 bound solo, and hope the debugger can cope. */
16601 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16602 if (upper)
16603 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16606 /* Otherwise we have an array type with an unspecified length. The
16607 DWARF-2 spec does not say how to handle this; let's just leave out the
16608 bounds. */
16612 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16614 static void
16615 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16617 dw_die_ref decl_die;
16618 HOST_WIDE_INT size;
16620 switch (TREE_CODE (tree_node))
16622 case ERROR_MARK:
16623 size = 0;
16624 break;
16625 case ENUMERAL_TYPE:
16626 case RECORD_TYPE:
16627 case UNION_TYPE:
16628 case QUAL_UNION_TYPE:
16629 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16630 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16632 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16633 return;
16635 size = int_size_in_bytes (tree_node);
16636 break;
16637 case FIELD_DECL:
16638 /* For a data member of a struct or union, the DW_AT_byte_size is
16639 generally given as the number of bytes normally allocated for an
16640 object of the *declared* type of the member itself. This is true
16641 even for bit-fields. */
16642 size = int_size_in_bytes (field_type (tree_node));
16643 break;
16644 default:
16645 gcc_unreachable ();
16648 /* Note that `size' might be -1 when we get to this point. If it is, that
16649 indicates that the byte size of the entity in question is variable. We
16650 have no good way of expressing this fact in Dwarf at the present time,
16651 when location description was not used by the caller code instead. */
16652 if (size >= 0)
16653 add_AT_unsigned (die, DW_AT_byte_size, size);
16656 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16657 which specifies the distance in bits from the highest order bit of the
16658 "containing object" for the bit-field to the highest order bit of the
16659 bit-field itself.
16661 For any given bit-field, the "containing object" is a hypothetical object
16662 (of some integral or enum type) within which the given bit-field lives. The
16663 type of this hypothetical "containing object" is always the same as the
16664 declared type of the individual bit-field itself. The determination of the
16665 exact location of the "containing object" for a bit-field is rather
16666 complicated. It's handled by the `field_byte_offset' function (above).
16668 Note that it is the size (in bytes) of the hypothetical "containing object"
16669 which will be given in the DW_AT_byte_size attribute for this bit-field.
16670 (See `byte_size_attribute' above). */
16672 static inline void
16673 add_bit_offset_attribute (dw_die_ref die, tree decl)
16675 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16676 tree type = DECL_BIT_FIELD_TYPE (decl);
16677 HOST_WIDE_INT bitpos_int;
16678 HOST_WIDE_INT highest_order_object_bit_offset;
16679 HOST_WIDE_INT highest_order_field_bit_offset;
16680 HOST_WIDE_INT bit_offset;
16682 /* Must be a field and a bit field. */
16683 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16685 /* We can't yet handle bit-fields whose offsets are variable, so if we
16686 encounter such things, just return without generating any attribute
16687 whatsoever. Likewise for variable or too large size. */
16688 if (! tree_fits_shwi_p (bit_position (decl))
16689 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16690 return;
16692 bitpos_int = int_bit_position (decl);
16694 /* Note that the bit offset is always the distance (in bits) from the
16695 highest-order bit of the "containing object" to the highest-order bit of
16696 the bit-field itself. Since the "high-order end" of any object or field
16697 is different on big-endian and little-endian machines, the computation
16698 below must take account of these differences. */
16699 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16700 highest_order_field_bit_offset = bitpos_int;
16702 if (! BYTES_BIG_ENDIAN)
16704 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16705 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16708 bit_offset
16709 = (! BYTES_BIG_ENDIAN
16710 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16711 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16713 if (bit_offset < 0)
16714 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16715 else
16716 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16719 /* For a FIELD_DECL node which represents a bit field, output an attribute
16720 which specifies the length in bits of the given field. */
16722 static inline void
16723 add_bit_size_attribute (dw_die_ref die, tree decl)
16725 /* Must be a field and a bit field. */
16726 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16727 && DECL_BIT_FIELD_TYPE (decl));
16729 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16730 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16733 /* If the compiled language is ANSI C, then add a 'prototyped'
16734 attribute, if arg types are given for the parameters of a function. */
16736 static inline void
16737 add_prototyped_attribute (dw_die_ref die, tree func_type)
16739 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16740 && prototype_p (func_type))
16741 add_AT_flag (die, DW_AT_prototyped, 1);
16744 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16745 by looking in either the type declaration or object declaration
16746 equate table. */
16748 static inline dw_die_ref
16749 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16751 dw_die_ref origin_die = NULL;
16753 if (TREE_CODE (origin) != FUNCTION_DECL)
16755 /* We may have gotten separated from the block for the inlined
16756 function, if we're in an exception handler or some such; make
16757 sure that the abstract function has been written out.
16759 Doing this for nested functions is wrong, however; functions are
16760 distinct units, and our context might not even be inline. */
16761 tree fn = origin;
16763 if (TYPE_P (fn))
16764 fn = TYPE_STUB_DECL (fn);
16766 fn = decl_function_context (fn);
16767 if (fn)
16768 dwarf2out_abstract_function (fn);
16771 if (DECL_P (origin))
16772 origin_die = lookup_decl_die (origin);
16773 else if (TYPE_P (origin))
16774 origin_die = lookup_type_die (origin);
16776 /* XXX: Functions that are never lowered don't always have correct block
16777 trees (in the case of java, they simply have no block tree, in some other
16778 languages). For these functions, there is nothing we can really do to
16779 output correct debug info for inlined functions in all cases. Rather
16780 than die, we'll just produce deficient debug info now, in that we will
16781 have variables without a proper abstract origin. In the future, when all
16782 functions are lowered, we should re-add a gcc_assert (origin_die)
16783 here. */
16785 if (origin_die)
16786 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16787 return origin_die;
16790 /* We do not currently support the pure_virtual attribute. */
16792 static inline void
16793 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16795 if (DECL_VINDEX (func_decl))
16797 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16799 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16800 add_AT_loc (die, DW_AT_vtable_elem_location,
16801 new_loc_descr (DW_OP_constu,
16802 tree_to_shwi (DECL_VINDEX (func_decl)),
16803 0));
16805 /* GNU extension: Record what type this method came from originally. */
16806 if (debug_info_level > DINFO_LEVEL_TERSE
16807 && DECL_CONTEXT (func_decl))
16808 add_AT_die_ref (die, DW_AT_containing_type,
16809 lookup_type_die (DECL_CONTEXT (func_decl)));
16813 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16814 given decl. This used to be a vendor extension until after DWARF 4
16815 standardized it. */
16817 static void
16818 add_linkage_attr (dw_die_ref die, tree decl)
16820 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16822 /* Mimic what assemble_name_raw does with a leading '*'. */
16823 if (name[0] == '*')
16824 name = &name[1];
16826 if (dwarf_version >= 4)
16827 add_AT_string (die, DW_AT_linkage_name, name);
16828 else
16829 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16832 /* Add source coordinate attributes for the given decl. */
16834 static void
16835 add_src_coords_attributes (dw_die_ref die, tree decl)
16837 expanded_location s;
16839 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16840 return;
16841 s = expand_location (DECL_SOURCE_LOCATION (decl));
16842 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16843 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16846 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16848 static void
16849 add_linkage_name (dw_die_ref die, tree decl)
16851 if (debug_info_level > DINFO_LEVEL_NONE
16852 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16853 && TREE_PUBLIC (decl)
16854 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16855 && die->die_tag != DW_TAG_member)
16857 /* Defer until we have an assembler name set. */
16858 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16860 limbo_die_node *asm_name;
16862 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16863 asm_name->die = die;
16864 asm_name->created_for = decl;
16865 asm_name->next = deferred_asm_name;
16866 deferred_asm_name = asm_name;
16868 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16869 add_linkage_attr (die, decl);
16873 /* Add a DW_AT_name attribute and source coordinate attribute for the
16874 given decl, but only if it actually has a name. */
16876 static void
16877 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16879 tree decl_name;
16881 decl_name = DECL_NAME (decl);
16882 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16884 const char *name = dwarf2_name (decl, 0);
16885 if (name)
16886 add_name_attribute (die, name);
16887 if (! DECL_ARTIFICIAL (decl))
16888 add_src_coords_attributes (die, decl);
16890 add_linkage_name (die, decl);
16893 #ifdef VMS_DEBUGGING_INFO
16894 /* Get the function's name, as described by its RTL. This may be different
16895 from the DECL_NAME name used in the source file. */
16896 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16898 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16899 XEXP (DECL_RTL (decl), 0), false);
16900 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16902 #endif /* VMS_DEBUGGING_INFO */
16905 #ifdef VMS_DEBUGGING_INFO
16906 /* Output the debug main pointer die for VMS */
16908 void
16909 dwarf2out_vms_debug_main_pointer (void)
16911 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16912 dw_die_ref die;
16914 /* Allocate the VMS debug main subprogram die. */
16915 die = ggc_cleared_alloc<die_node> ();
16916 die->die_tag = DW_TAG_subprogram;
16917 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16918 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16919 current_function_funcdef_no);
16920 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16922 /* Make it the first child of comp_unit_die (). */
16923 die->die_parent = comp_unit_die ();
16924 if (comp_unit_die ()->die_child)
16926 die->die_sib = comp_unit_die ()->die_child->die_sib;
16927 comp_unit_die ()->die_child->die_sib = die;
16929 else
16931 die->die_sib = die;
16932 comp_unit_die ()->die_child = die;
16935 #endif /* VMS_DEBUGGING_INFO */
16937 /* Push a new declaration scope. */
16939 static void
16940 push_decl_scope (tree scope)
16942 vec_safe_push (decl_scope_table, scope);
16945 /* Pop a declaration scope. */
16947 static inline void
16948 pop_decl_scope (void)
16950 decl_scope_table->pop ();
16953 /* walk_tree helper function for uses_local_type, below. */
16955 static tree
16956 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16958 if (!TYPE_P (*tp))
16959 *walk_subtrees = 0;
16960 else
16962 tree name = TYPE_NAME (*tp);
16963 if (name && DECL_P (name) && decl_function_context (name))
16964 return *tp;
16966 return NULL_TREE;
16969 /* If TYPE involves a function-local type (including a local typedef to a
16970 non-local type), returns that type; otherwise returns NULL_TREE. */
16972 static tree
16973 uses_local_type (tree type)
16975 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16976 return used;
16979 /* Return the DIE for the scope that immediately contains this type.
16980 Non-named types that do not involve a function-local type get global
16981 scope. Named types nested in namespaces or other types get their
16982 containing scope. All other types (i.e. function-local named types) get
16983 the current active scope. */
16985 static dw_die_ref
16986 scope_die_for (tree t, dw_die_ref context_die)
16988 dw_die_ref scope_die = NULL;
16989 tree containing_scope;
16991 /* Non-types always go in the current scope. */
16992 gcc_assert (TYPE_P (t));
16994 /* Use the scope of the typedef, rather than the scope of the type
16995 it refers to. */
16996 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16997 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16998 else
16999 containing_scope = TYPE_CONTEXT (t);
17001 /* Use the containing namespace if there is one. */
17002 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17004 if (context_die == lookup_decl_die (containing_scope))
17005 /* OK */;
17006 else if (debug_info_level > DINFO_LEVEL_TERSE)
17007 context_die = get_context_die (containing_scope);
17008 else
17009 containing_scope = NULL_TREE;
17012 /* Ignore function type "scopes" from the C frontend. They mean that
17013 a tagged type is local to a parmlist of a function declarator, but
17014 that isn't useful to DWARF. */
17015 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17016 containing_scope = NULL_TREE;
17018 if (SCOPE_FILE_SCOPE_P (containing_scope))
17020 /* If T uses a local type keep it local as well, to avoid references
17021 to function-local DIEs from outside the function. */
17022 if (current_function_decl && uses_local_type (t))
17023 scope_die = context_die;
17024 else
17025 scope_die = comp_unit_die ();
17027 else if (TYPE_P (containing_scope))
17029 /* For types, we can just look up the appropriate DIE. */
17030 if (debug_info_level > DINFO_LEVEL_TERSE)
17031 scope_die = get_context_die (containing_scope);
17032 else
17034 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17035 if (scope_die == NULL)
17036 scope_die = comp_unit_die ();
17039 else
17040 scope_die = context_die;
17042 return scope_die;
17045 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17047 static inline int
17048 local_scope_p (dw_die_ref context_die)
17050 for (; context_die; context_die = context_die->die_parent)
17051 if (context_die->die_tag == DW_TAG_inlined_subroutine
17052 || context_die->die_tag == DW_TAG_subprogram)
17053 return 1;
17055 return 0;
17058 /* Returns nonzero if CONTEXT_DIE is a class. */
17060 static inline int
17061 class_scope_p (dw_die_ref context_die)
17063 return (context_die
17064 && (context_die->die_tag == DW_TAG_structure_type
17065 || context_die->die_tag == DW_TAG_class_type
17066 || context_die->die_tag == DW_TAG_interface_type
17067 || context_die->die_tag == DW_TAG_union_type));
17070 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17071 whether or not to treat a DIE in this context as a declaration. */
17073 static inline int
17074 class_or_namespace_scope_p (dw_die_ref context_die)
17076 return (class_scope_p (context_die)
17077 || (context_die && context_die->die_tag == DW_TAG_namespace));
17080 /* Many forms of DIEs require a "type description" attribute. This
17081 routine locates the proper "type descriptor" die for the type given
17082 by 'type' plus any additional qualifiers given by 'cv_quals', and
17083 adds a DW_AT_type attribute below the given die. */
17085 static void
17086 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17087 dw_die_ref context_die)
17089 enum tree_code code = TREE_CODE (type);
17090 dw_die_ref type_die = NULL;
17092 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17093 or fixed-point type, use the inner type. This is because we have no
17094 support for unnamed types in base_type_die. This can happen if this is
17095 an Ada subrange type. Correct solution is emit a subrange type die. */
17096 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17097 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17098 type = TREE_TYPE (type), code = TREE_CODE (type);
17100 if (code == ERROR_MARK
17101 /* Handle a special case. For functions whose return type is void, we
17102 generate *no* type attribute. (Note that no object may have type
17103 `void', so this only applies to function return types). */
17104 || code == VOID_TYPE)
17105 return;
17107 type_die = modified_type_die (type,
17108 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17109 context_die);
17111 if (type_die != NULL)
17112 add_AT_die_ref (object_die, DW_AT_type, type_die);
17115 /* Given an object die, add the calling convention attribute for the
17116 function call type. */
17117 static void
17118 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17120 enum dwarf_calling_convention value = DW_CC_normal;
17122 value = ((enum dwarf_calling_convention)
17123 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17125 if (is_fortran ()
17126 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17128 /* DWARF 2 doesn't provide a way to identify a program's source-level
17129 entry point. DW_AT_calling_convention attributes are only meant
17130 to describe functions' calling conventions. However, lacking a
17131 better way to signal the Fortran main program, we used this for
17132 a long time, following existing custom. Now, DWARF 4 has
17133 DW_AT_main_subprogram, which we add below, but some tools still
17134 rely on the old way, which we thus keep. */
17135 value = DW_CC_program;
17137 if (dwarf_version >= 4 || !dwarf_strict)
17138 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17141 /* Only add the attribute if the backend requests it, and
17142 is not DW_CC_normal. */
17143 if (value && (value != DW_CC_normal))
17144 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17147 /* Given a tree pointer to a struct, class, union, or enum type node, return
17148 a pointer to the (string) tag name for the given type, or zero if the type
17149 was declared without a tag. */
17151 static const char *
17152 type_tag (const_tree type)
17154 const char *name = 0;
17156 if (TYPE_NAME (type) != 0)
17158 tree t = 0;
17160 /* Find the IDENTIFIER_NODE for the type name. */
17161 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17162 && !TYPE_NAMELESS (type))
17163 t = TYPE_NAME (type);
17165 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17166 a TYPE_DECL node, regardless of whether or not a `typedef' was
17167 involved. */
17168 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17169 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17171 /* We want to be extra verbose. Don't call dwarf_name if
17172 DECL_NAME isn't set. The default hook for decl_printable_name
17173 doesn't like that, and in this context it's correct to return
17174 0, instead of "<anonymous>" or the like. */
17175 if (DECL_NAME (TYPE_NAME (type))
17176 && !DECL_NAMELESS (TYPE_NAME (type)))
17177 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17180 /* Now get the name as a string, or invent one. */
17181 if (!name && t != 0)
17182 name = IDENTIFIER_POINTER (t);
17185 return (name == 0 || *name == '\0') ? 0 : name;
17188 /* Return the type associated with a data member, make a special check
17189 for bit field types. */
17191 static inline tree
17192 member_declared_type (const_tree member)
17194 return (DECL_BIT_FIELD_TYPE (member)
17195 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17198 /* Get the decl's label, as described by its RTL. This may be different
17199 from the DECL_NAME name used in the source file. */
17201 #if 0
17202 static const char *
17203 decl_start_label (tree decl)
17205 rtx x;
17206 const char *fnname;
17208 x = DECL_RTL (decl);
17209 gcc_assert (MEM_P (x));
17211 x = XEXP (x, 0);
17212 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17214 fnname = XSTR (x, 0);
17215 return fnname;
17217 #endif
17219 /* These routines generate the internal representation of the DIE's for
17220 the compilation unit. Debugging information is collected by walking
17221 the declaration trees passed in from dwarf2out_decl(). */
17223 static void
17224 gen_array_type_die (tree type, dw_die_ref context_die)
17226 dw_die_ref scope_die = scope_die_for (type, context_die);
17227 dw_die_ref array_die;
17229 /* GNU compilers represent multidimensional array types as sequences of one
17230 dimensional array types whose element types are themselves array types.
17231 We sometimes squish that down to a single array_type DIE with multiple
17232 subscripts in the Dwarf debugging info. The draft Dwarf specification
17233 say that we are allowed to do this kind of compression in C, because
17234 there is no difference between an array of arrays and a multidimensional
17235 array. We don't do this for Ada to remain as close as possible to the
17236 actual representation, which is especially important against the language
17237 flexibilty wrt arrays of variable size. */
17239 bool collapse_nested_arrays = !is_ada ();
17240 tree element_type;
17242 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17243 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17244 if (TYPE_STRING_FLAG (type)
17245 && TREE_CODE (type) == ARRAY_TYPE
17246 && is_fortran ()
17247 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17249 HOST_WIDE_INT size;
17251 array_die = new_die (DW_TAG_string_type, scope_die, type);
17252 add_name_attribute (array_die, type_tag (type));
17253 equate_type_number_to_die (type, array_die);
17254 size = int_size_in_bytes (type);
17255 if (size >= 0)
17256 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17257 else if (TYPE_DOMAIN (type) != NULL_TREE
17258 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17259 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17261 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17262 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17264 size = int_size_in_bytes (TREE_TYPE (szdecl));
17265 if (loc && size > 0)
17267 add_AT_location_description (array_die, DW_AT_string_length, loc);
17268 if (size != DWARF2_ADDR_SIZE)
17269 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17272 return;
17275 array_die = new_die (DW_TAG_array_type, scope_die, type);
17276 add_name_attribute (array_die, type_tag (type));
17277 equate_type_number_to_die (type, array_die);
17279 if (TREE_CODE (type) == VECTOR_TYPE)
17280 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17282 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17283 if (is_fortran ()
17284 && TREE_CODE (type) == ARRAY_TYPE
17285 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17286 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17287 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17289 #if 0
17290 /* We default the array ordering. SDB will probably do
17291 the right things even if DW_AT_ordering is not present. It's not even
17292 an issue until we start to get into multidimensional arrays anyway. If
17293 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17294 then we'll have to put the DW_AT_ordering attribute back in. (But if
17295 and when we find out that we need to put these in, we will only do so
17296 for multidimensional arrays. */
17297 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17298 #endif
17300 if (TREE_CODE (type) == VECTOR_TYPE)
17302 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17303 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17304 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17305 add_bound_info (subrange_die, DW_AT_upper_bound,
17306 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17308 else
17309 add_subscript_info (array_die, type, collapse_nested_arrays);
17311 /* Add representation of the type of the elements of this array type and
17312 emit the corresponding DIE if we haven't done it already. */
17313 element_type = TREE_TYPE (type);
17314 if (collapse_nested_arrays)
17315 while (TREE_CODE (element_type) == ARRAY_TYPE)
17317 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17318 break;
17319 element_type = TREE_TYPE (element_type);
17322 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17324 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17325 if (TYPE_ARTIFICIAL (type))
17326 add_AT_flag (array_die, DW_AT_artificial, 1);
17328 if (get_AT (array_die, DW_AT_name))
17329 add_pubtype (type, array_die);
17332 static dw_loc_descr_ref
17333 descr_info_loc (tree val, tree base_decl)
17335 HOST_WIDE_INT size;
17336 dw_loc_descr_ref loc, loc2;
17337 enum dwarf_location_atom op;
17339 if (val == base_decl)
17340 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17342 switch (TREE_CODE (val))
17344 CASE_CONVERT:
17345 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17346 case VAR_DECL:
17347 return loc_descriptor_from_tree (val, 0);
17348 case INTEGER_CST:
17349 if (tree_fits_shwi_p (val))
17350 return int_loc_descriptor (tree_to_shwi (val));
17351 break;
17352 case INDIRECT_REF:
17353 size = int_size_in_bytes (TREE_TYPE (val));
17354 if (size < 0)
17355 break;
17356 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17357 if (!loc)
17358 break;
17359 if (size == DWARF2_ADDR_SIZE)
17360 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17361 else
17362 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17363 return loc;
17364 case POINTER_PLUS_EXPR:
17365 case PLUS_EXPR:
17366 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17367 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17369 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17370 if (!loc)
17371 break;
17372 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17374 else
17376 op = DW_OP_plus;
17377 do_binop:
17378 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17379 if (!loc)
17380 break;
17381 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17382 if (!loc2)
17383 break;
17384 add_loc_descr (&loc, loc2);
17385 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17387 return loc;
17388 case MINUS_EXPR:
17389 op = DW_OP_minus;
17390 goto do_binop;
17391 case MULT_EXPR:
17392 op = DW_OP_mul;
17393 goto do_binop;
17394 case EQ_EXPR:
17395 op = DW_OP_eq;
17396 goto do_binop;
17397 case NE_EXPR:
17398 op = DW_OP_ne;
17399 goto do_binop;
17400 default:
17401 break;
17403 return NULL;
17406 static void
17407 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17408 tree val, tree base_decl)
17410 dw_loc_descr_ref loc;
17412 if (tree_fits_shwi_p (val))
17414 add_AT_unsigned (die, attr, tree_to_shwi (val));
17415 return;
17418 loc = descr_info_loc (val, base_decl);
17419 if (!loc)
17420 return;
17422 add_AT_loc (die, attr, loc);
17425 /* This routine generates DIE for array with hidden descriptor, details
17426 are filled into *info by a langhook. */
17428 static void
17429 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17430 dw_die_ref context_die)
17432 dw_die_ref scope_die = scope_die_for (type, context_die);
17433 dw_die_ref array_die;
17434 int dim;
17436 array_die = new_die (DW_TAG_array_type, scope_die, type);
17437 add_name_attribute (array_die, type_tag (type));
17438 equate_type_number_to_die (type, array_die);
17440 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17441 if (is_fortran ()
17442 && info->ndimensions >= 2)
17443 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17445 if (info->data_location)
17446 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17447 info->base_decl);
17448 if (info->associated)
17449 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17450 info->base_decl);
17451 if (info->allocated)
17452 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17453 info->base_decl);
17455 for (dim = 0; dim < info->ndimensions; dim++)
17457 dw_die_ref subrange_die
17458 = new_die (DW_TAG_subrange_type, array_die, NULL);
17460 if (info->dimen[dim].lower_bound)
17462 /* If it is the default value, omit it. */
17463 int dflt;
17465 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17466 && (dflt = lower_bound_default ()) != -1
17467 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17469 else
17470 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17471 info->dimen[dim].lower_bound,
17472 info->base_decl);
17474 if (info->dimen[dim].upper_bound)
17475 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17476 info->dimen[dim].upper_bound,
17477 info->base_decl);
17478 if (info->dimen[dim].stride)
17479 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17480 info->dimen[dim].stride,
17481 info->base_decl);
17484 gen_type_die (info->element_type, context_die);
17485 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17486 context_die);
17488 if (get_AT (array_die, DW_AT_name))
17489 add_pubtype (type, array_die);
17492 #if 0
17493 static void
17494 gen_entry_point_die (tree decl, dw_die_ref context_die)
17496 tree origin = decl_ultimate_origin (decl);
17497 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17499 if (origin != NULL)
17500 add_abstract_origin_attribute (decl_die, origin);
17501 else
17503 add_name_and_src_coords_attributes (decl_die, decl);
17504 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17505 TYPE_UNQUALIFIED, context_die);
17508 if (DECL_ABSTRACT_P (decl))
17509 equate_decl_number_to_die (decl, decl_die);
17510 else
17511 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17513 #endif
17515 /* Walk through the list of incomplete types again, trying once more to
17516 emit full debugging info for them. */
17518 static void
17519 retry_incomplete_types (void)
17521 int i;
17523 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17524 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17525 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17528 /* Determine what tag to use for a record type. */
17530 static enum dwarf_tag
17531 record_type_tag (tree type)
17533 if (! lang_hooks.types.classify_record)
17534 return DW_TAG_structure_type;
17536 switch (lang_hooks.types.classify_record (type))
17538 case RECORD_IS_STRUCT:
17539 return DW_TAG_structure_type;
17541 case RECORD_IS_CLASS:
17542 return DW_TAG_class_type;
17544 case RECORD_IS_INTERFACE:
17545 if (dwarf_version >= 3 || !dwarf_strict)
17546 return DW_TAG_interface_type;
17547 return DW_TAG_structure_type;
17549 default:
17550 gcc_unreachable ();
17554 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17555 include all of the information about the enumeration values also. Each
17556 enumerated type name/value is listed as a child of the enumerated type
17557 DIE. */
17559 static dw_die_ref
17560 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17562 dw_die_ref type_die = lookup_type_die (type);
17564 if (type_die == NULL)
17566 type_die = new_die (DW_TAG_enumeration_type,
17567 scope_die_for (type, context_die), type);
17568 equate_type_number_to_die (type, type_die);
17569 add_name_attribute (type_die, type_tag (type));
17570 if (dwarf_version >= 4 || !dwarf_strict)
17572 if (ENUM_IS_SCOPED (type))
17573 add_AT_flag (type_die, DW_AT_enum_class, 1);
17574 if (ENUM_IS_OPAQUE (type))
17575 add_AT_flag (type_die, DW_AT_declaration, 1);
17578 else if (! TYPE_SIZE (type))
17579 return type_die;
17580 else
17581 remove_AT (type_die, DW_AT_declaration);
17583 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17584 given enum type is incomplete, do not generate the DW_AT_byte_size
17585 attribute or the DW_AT_element_list attribute. */
17586 if (TYPE_SIZE (type))
17588 tree link;
17590 TREE_ASM_WRITTEN (type) = 1;
17591 add_byte_size_attribute (type_die, type);
17592 if (dwarf_version >= 3 || !dwarf_strict)
17594 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17595 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17596 context_die);
17598 if (TYPE_STUB_DECL (type) != NULL_TREE)
17600 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17601 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17604 /* If the first reference to this type was as the return type of an
17605 inline function, then it may not have a parent. Fix this now. */
17606 if (type_die->die_parent == NULL)
17607 add_child_die (scope_die_for (type, context_die), type_die);
17609 for (link = TYPE_VALUES (type);
17610 link != NULL; link = TREE_CHAIN (link))
17612 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17613 tree value = TREE_VALUE (link);
17615 add_name_attribute (enum_die,
17616 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17618 if (TREE_CODE (value) == CONST_DECL)
17619 value = DECL_INITIAL (value);
17621 if (simple_type_size_in_bits (TREE_TYPE (value))
17622 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17624 /* For constant forms created by add_AT_unsigned DWARF
17625 consumers (GDB, elfutils, etc.) always zero extend
17626 the value. Only when the actual value is negative
17627 do we need to use add_AT_int to generate a constant
17628 form that can represent negative values. */
17629 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17630 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17631 add_AT_unsigned (enum_die, DW_AT_const_value,
17632 (unsigned HOST_WIDE_INT) val);
17633 else
17634 add_AT_int (enum_die, DW_AT_const_value, val);
17636 else
17637 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17638 that here. TODO: This should be re-worked to use correct
17639 signed/unsigned double tags for all cases. */
17640 add_AT_wide (enum_die, DW_AT_const_value, value);
17643 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17644 if (TYPE_ARTIFICIAL (type))
17645 add_AT_flag (type_die, DW_AT_artificial, 1);
17647 else
17648 add_AT_flag (type_die, DW_AT_declaration, 1);
17650 add_pubtype (type, type_die);
17652 return type_die;
17655 /* Generate a DIE to represent either a real live formal parameter decl or to
17656 represent just the type of some formal parameter position in some function
17657 type.
17659 Note that this routine is a bit unusual because its argument may be a
17660 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17661 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17662 node. If it's the former then this function is being called to output a
17663 DIE to represent a formal parameter object (or some inlining thereof). If
17664 it's the latter, then this function is only being called to output a
17665 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17666 argument type of some subprogram type.
17667 If EMIT_NAME_P is true, name and source coordinate attributes
17668 are emitted. */
17670 static dw_die_ref
17671 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17672 dw_die_ref context_die)
17674 tree node_or_origin = node ? node : origin;
17675 tree ultimate_origin;
17676 dw_die_ref parm_die
17677 = new_die (DW_TAG_formal_parameter, context_die, node);
17679 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17681 case tcc_declaration:
17682 ultimate_origin = decl_ultimate_origin (node_or_origin);
17683 if (node || ultimate_origin)
17684 origin = ultimate_origin;
17685 if (origin != NULL)
17686 add_abstract_origin_attribute (parm_die, origin);
17687 else if (emit_name_p)
17688 add_name_and_src_coords_attributes (parm_die, node);
17689 if (origin == NULL
17690 || (! DECL_ABSTRACT_P (node_or_origin)
17691 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17692 decl_function_context
17693 (node_or_origin))))
17695 tree type = TREE_TYPE (node_or_origin);
17696 if (decl_by_reference_p (node_or_origin))
17697 add_type_attribute (parm_die, TREE_TYPE (type),
17698 TYPE_UNQUALIFIED, context_die);
17699 else
17700 add_type_attribute (parm_die, type,
17701 decl_quals (node_or_origin),
17702 context_die);
17704 if (origin == NULL && DECL_ARTIFICIAL (node))
17705 add_AT_flag (parm_die, DW_AT_artificial, 1);
17707 if (node && node != origin)
17708 equate_decl_number_to_die (node, parm_die);
17709 if (! DECL_ABSTRACT_P (node_or_origin))
17710 add_location_or_const_value_attribute (parm_die, node_or_origin,
17711 node == NULL, DW_AT_location);
17713 break;
17715 case tcc_type:
17716 /* We were called with some kind of a ..._TYPE node. */
17717 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17718 context_die);
17719 break;
17721 default:
17722 gcc_unreachable ();
17725 return parm_die;
17728 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17729 children DW_TAG_formal_parameter DIEs representing the arguments of the
17730 parameter pack.
17732 PARM_PACK must be a function parameter pack.
17733 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17734 must point to the subsequent arguments of the function PACK_ARG belongs to.
17735 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17736 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17737 following the last one for which a DIE was generated. */
17739 static dw_die_ref
17740 gen_formal_parameter_pack_die (tree parm_pack,
17741 tree pack_arg,
17742 dw_die_ref subr_die,
17743 tree *next_arg)
17745 tree arg;
17746 dw_die_ref parm_pack_die;
17748 gcc_assert (parm_pack
17749 && lang_hooks.function_parameter_pack_p (parm_pack)
17750 && subr_die);
17752 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17753 add_src_coords_attributes (parm_pack_die, parm_pack);
17755 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17757 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17758 parm_pack))
17759 break;
17760 gen_formal_parameter_die (arg, NULL,
17761 false /* Don't emit name attribute. */,
17762 parm_pack_die);
17764 if (next_arg)
17765 *next_arg = arg;
17766 return parm_pack_die;
17769 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17770 at the end of an (ANSI prototyped) formal parameters list. */
17772 static void
17773 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17775 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17778 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17779 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17780 parameters as specified in some function type specification (except for
17781 those which appear as part of a function *definition*). */
17783 static void
17784 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17786 tree link;
17787 tree formal_type = NULL;
17788 tree first_parm_type;
17789 tree arg;
17791 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17793 arg = DECL_ARGUMENTS (function_or_method_type);
17794 function_or_method_type = TREE_TYPE (function_or_method_type);
17796 else
17797 arg = NULL_TREE;
17799 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17801 /* Make our first pass over the list of formal parameter types and output a
17802 DW_TAG_formal_parameter DIE for each one. */
17803 for (link = first_parm_type; link; )
17805 dw_die_ref parm_die;
17807 formal_type = TREE_VALUE (link);
17808 if (formal_type == void_type_node)
17809 break;
17811 /* Output a (nameless) DIE to represent the formal parameter itself. */
17812 parm_die = gen_formal_parameter_die (formal_type, NULL,
17813 true /* Emit name attribute. */,
17814 context_die);
17815 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17816 && link == first_parm_type)
17818 add_AT_flag (parm_die, DW_AT_artificial, 1);
17819 if (dwarf_version >= 3 || !dwarf_strict)
17820 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17822 else if (arg && DECL_ARTIFICIAL (arg))
17823 add_AT_flag (parm_die, DW_AT_artificial, 1);
17825 link = TREE_CHAIN (link);
17826 if (arg)
17827 arg = DECL_CHAIN (arg);
17830 /* If this function type has an ellipsis, add a
17831 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17832 if (formal_type != void_type_node)
17833 gen_unspecified_parameters_die (function_or_method_type, context_die);
17835 /* Make our second (and final) pass over the list of formal parameter types
17836 and output DIEs to represent those types (as necessary). */
17837 for (link = TYPE_ARG_TYPES (function_or_method_type);
17838 link && TREE_VALUE (link);
17839 link = TREE_CHAIN (link))
17840 gen_type_die (TREE_VALUE (link), context_die);
17843 /* We want to generate the DIE for TYPE so that we can generate the
17844 die for MEMBER, which has been defined; we will need to refer back
17845 to the member declaration nested within TYPE. If we're trying to
17846 generate minimal debug info for TYPE, processing TYPE won't do the
17847 trick; we need to attach the member declaration by hand. */
17849 static void
17850 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17852 gen_type_die (type, context_die);
17854 /* If we're trying to avoid duplicate debug info, we may not have
17855 emitted the member decl for this function. Emit it now. */
17856 if (TYPE_STUB_DECL (type)
17857 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17858 && ! lookup_decl_die (member))
17860 dw_die_ref type_die;
17861 gcc_assert (!decl_ultimate_origin (member));
17863 push_decl_scope (type);
17864 type_die = lookup_type_die_strip_naming_typedef (type);
17865 if (TREE_CODE (member) == FUNCTION_DECL)
17866 gen_subprogram_die (member, type_die);
17867 else if (TREE_CODE (member) == FIELD_DECL)
17869 /* Ignore the nameless fields that are used to skip bits but handle
17870 C++ anonymous unions and structs. */
17871 if (DECL_NAME (member) != NULL_TREE
17872 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17873 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17875 gen_type_die (member_declared_type (member), type_die);
17876 gen_field_die (member, type_die);
17879 else
17880 gen_variable_die (member, NULL_TREE, type_die);
17882 pop_decl_scope ();
17886 /* Forward declare these functions, because they are mutually recursive
17887 with their set_block_* pairing functions. */
17888 static void set_decl_origin_self (tree);
17889 static void set_decl_abstract_flags (tree, int);
17891 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17892 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17893 that it points to the node itself, thus indicating that the node is its
17894 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17895 the given node is NULL, recursively descend the decl/block tree which
17896 it is the root of, and for each other ..._DECL or BLOCK node contained
17897 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17898 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17899 values to point to themselves. */
17901 static void
17902 set_block_origin_self (tree stmt)
17904 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17906 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17909 tree local_decl;
17911 for (local_decl = BLOCK_VARS (stmt);
17912 local_decl != NULL_TREE;
17913 local_decl = DECL_CHAIN (local_decl))
17914 if (! DECL_EXTERNAL (local_decl))
17915 set_decl_origin_self (local_decl); /* Potential recursion. */
17919 tree subblock;
17921 for (subblock = BLOCK_SUBBLOCKS (stmt);
17922 subblock != NULL_TREE;
17923 subblock = BLOCK_CHAIN (subblock))
17924 set_block_origin_self (subblock); /* Recurse. */
17929 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17930 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17931 node to so that it points to the node itself, thus indicating that the
17932 node represents its own (abstract) origin. Additionally, if the
17933 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17934 the decl/block tree of which the given node is the root of, and for
17935 each other ..._DECL or BLOCK node contained therein whose
17936 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17937 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17938 point to themselves. */
17940 static void
17941 set_decl_origin_self (tree decl)
17943 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17945 DECL_ABSTRACT_ORIGIN (decl) = decl;
17946 if (TREE_CODE (decl) == FUNCTION_DECL)
17948 tree arg;
17950 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17951 DECL_ABSTRACT_ORIGIN (arg) = arg;
17952 if (DECL_INITIAL (decl) != NULL_TREE
17953 && DECL_INITIAL (decl) != error_mark_node)
17954 set_block_origin_self (DECL_INITIAL (decl));
17959 /* Given a pointer to some BLOCK node, and a boolean value to set the
17960 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17961 the given block, and for all local decls and all local sub-blocks
17962 (recursively) which are contained therein. */
17964 static void
17965 set_block_abstract_flags (tree stmt, int setting)
17967 tree local_decl;
17968 tree subblock;
17969 unsigned int i;
17971 BLOCK_ABSTRACT (stmt) = setting;
17973 for (local_decl = BLOCK_VARS (stmt);
17974 local_decl != NULL_TREE;
17975 local_decl = DECL_CHAIN (local_decl))
17976 if (! DECL_EXTERNAL (local_decl))
17977 set_decl_abstract_flags (local_decl, setting);
17979 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17981 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17982 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17983 || TREE_CODE (local_decl) == PARM_DECL)
17984 set_decl_abstract_flags (local_decl, setting);
17987 for (subblock = BLOCK_SUBBLOCKS (stmt);
17988 subblock != NULL_TREE;
17989 subblock = BLOCK_CHAIN (subblock))
17990 set_block_abstract_flags (subblock, setting);
17993 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17994 "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
17995 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17996 set the abstract flags for all of the parameters, local vars, local
17997 blocks and sub-blocks (recursively) to the same setting. */
17999 static void
18000 set_decl_abstract_flags (tree decl, int setting)
18002 DECL_ABSTRACT_P (decl) = setting;
18003 if (TREE_CODE (decl) == FUNCTION_DECL)
18005 tree arg;
18007 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18008 DECL_ABSTRACT_P (arg) = setting;
18009 if (DECL_INITIAL (decl) != NULL_TREE
18010 && DECL_INITIAL (decl) != error_mark_node)
18011 set_block_abstract_flags (DECL_INITIAL (decl), setting);
18015 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18016 may later generate inlined and/or out-of-line instances of. */
18018 static void
18019 dwarf2out_abstract_function (tree decl)
18021 dw_die_ref old_die;
18022 tree save_fn;
18023 tree context;
18024 int was_abstract;
18025 hash_table<decl_loc_hasher> *old_decl_loc_table;
18026 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18027 int old_call_site_count, old_tail_call_site_count;
18028 struct call_arg_loc_node *old_call_arg_locations;
18030 /* Make sure we have the actual abstract inline, not a clone. */
18031 decl = DECL_ORIGIN (decl);
18033 old_die = lookup_decl_die (decl);
18034 if (old_die && get_AT (old_die, DW_AT_inline))
18035 /* We've already generated the abstract instance. */
18036 return;
18038 /* We can be called while recursively when seeing block defining inlined subroutine
18039 DIE. Be sure to not clobber the outer location table nor use it or we would
18040 get locations in abstract instantces. */
18041 old_decl_loc_table = decl_loc_table;
18042 decl_loc_table = NULL;
18043 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18044 cached_dw_loc_list_table = NULL;
18045 old_call_arg_locations = call_arg_locations;
18046 call_arg_locations = NULL;
18047 old_call_site_count = call_site_count;
18048 call_site_count = -1;
18049 old_tail_call_site_count = tail_call_site_count;
18050 tail_call_site_count = -1;
18052 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18053 we don't get confused by DECL_ABSTRACT_P. */
18054 if (debug_info_level > DINFO_LEVEL_TERSE)
18056 context = decl_class_context (decl);
18057 if (context)
18058 gen_type_die_for_member
18059 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18062 /* Pretend we've just finished compiling this function. */
18063 save_fn = current_function_decl;
18064 current_function_decl = decl;
18066 was_abstract = DECL_ABSTRACT_P (decl);
18067 set_decl_abstract_flags (decl, 1);
18068 dwarf2out_decl (decl);
18069 if (! was_abstract)
18070 set_decl_abstract_flags (decl, 0);
18072 current_function_decl = save_fn;
18073 decl_loc_table = old_decl_loc_table;
18074 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18075 call_arg_locations = old_call_arg_locations;
18076 call_site_count = old_call_site_count;
18077 tail_call_site_count = old_tail_call_site_count;
18080 /* Helper function of premark_used_types() which gets called through
18081 htab_traverse.
18083 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18084 marked as unused by prune_unused_types. */
18086 bool
18087 premark_used_types_helper (tree const &type, void *)
18089 dw_die_ref die;
18091 die = lookup_type_die (type);
18092 if (die != NULL)
18093 die->die_perennial_p = 1;
18094 return true;
18097 /* Helper function of premark_types_used_by_global_vars which gets called
18098 through htab_traverse.
18100 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18101 marked as unused by prune_unused_types. The DIE of the type is marked
18102 only if the global variable using the type will actually be emitted. */
18105 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18106 void *)
18108 struct types_used_by_vars_entry *entry;
18109 dw_die_ref die;
18111 entry = (struct types_used_by_vars_entry *) *slot;
18112 gcc_assert (entry->type != NULL
18113 && entry->var_decl != NULL);
18114 die = lookup_type_die (entry->type);
18115 if (die)
18117 /* Ask cgraph if the global variable really is to be emitted.
18118 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18119 varpool_node *node = varpool_node::get (entry->var_decl);
18120 if (node && node->definition)
18122 die->die_perennial_p = 1;
18123 /* Keep the parent DIEs as well. */
18124 while ((die = die->die_parent) && die->die_perennial_p == 0)
18125 die->die_perennial_p = 1;
18128 return 1;
18131 /* Mark all members of used_types_hash as perennial. */
18133 static void
18134 premark_used_types (struct function *fun)
18136 if (fun && fun->used_types_hash)
18137 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18140 /* Mark all members of types_used_by_vars_entry as perennial. */
18142 static void
18143 premark_types_used_by_global_vars (void)
18145 if (types_used_by_vars_hash)
18146 types_used_by_vars_hash
18147 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18150 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18151 for CA_LOC call arg loc node. */
18153 static dw_die_ref
18154 gen_call_site_die (tree decl, dw_die_ref subr_die,
18155 struct call_arg_loc_node *ca_loc)
18157 dw_die_ref stmt_die = NULL, die;
18158 tree block = ca_loc->block;
18160 while (block
18161 && block != DECL_INITIAL (decl)
18162 && TREE_CODE (block) == BLOCK)
18164 if (block_map.length () > BLOCK_NUMBER (block))
18165 stmt_die = block_map[BLOCK_NUMBER (block)];
18166 if (stmt_die)
18167 break;
18168 block = BLOCK_SUPERCONTEXT (block);
18170 if (stmt_die == NULL)
18171 stmt_die = subr_die;
18172 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18173 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18174 if (ca_loc->tail_call_p)
18175 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18176 if (ca_loc->symbol_ref)
18178 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18179 if (tdie)
18180 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18181 else
18182 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18184 return die;
18187 /* Generate a DIE to represent a declared function (either file-scope or
18188 block-local). */
18190 static void
18191 gen_subprogram_die (tree decl, dw_die_ref context_die)
18193 tree origin = decl_ultimate_origin (decl);
18194 dw_die_ref subr_die;
18195 tree outer_scope;
18196 dw_die_ref old_die = lookup_decl_die (decl);
18197 int declaration = (current_function_decl != decl
18198 || class_or_namespace_scope_p (context_die));
18200 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18202 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18203 started to generate the abstract instance of an inline, decided to output
18204 its containing class, and proceeded to emit the declaration of the inline
18205 from the member list for the class. If so, DECLARATION takes priority;
18206 we'll get back to the abstract instance when done with the class. */
18208 /* The class-scope declaration DIE must be the primary DIE. */
18209 if (origin && declaration && class_or_namespace_scope_p (context_die))
18211 origin = NULL;
18212 gcc_assert (!old_die);
18215 /* Now that the C++ front end lazily declares artificial member fns, we
18216 might need to retrofit the declaration into its class. */
18217 if (!declaration && !origin && !old_die
18218 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18219 && !class_or_namespace_scope_p (context_die)
18220 && debug_info_level > DINFO_LEVEL_TERSE)
18221 old_die = force_decl_die (decl);
18223 if (origin != NULL)
18225 gcc_assert (!declaration || local_scope_p (context_die));
18227 /* Fixup die_parent for the abstract instance of a nested
18228 inline function. */
18229 if (old_die && old_die->die_parent == NULL)
18230 add_child_die (context_die, old_die);
18232 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18233 add_abstract_origin_attribute (subr_die, origin);
18234 /* This is where the actual code for a cloned function is.
18235 Let's emit linkage name attribute for it. This helps
18236 debuggers to e.g, set breakpoints into
18237 constructors/destructors when the user asks "break
18238 K::K". */
18239 add_linkage_name (subr_die, decl);
18241 else if (old_die)
18243 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18244 struct dwarf_file_data * file_index = lookup_filename (s.file);
18246 if (!get_AT_flag (old_die, DW_AT_declaration)
18247 /* We can have a normal definition following an inline one in the
18248 case of redefinition of GNU C extern inlines.
18249 It seems reasonable to use AT_specification in this case. */
18250 && !get_AT (old_die, DW_AT_inline))
18252 /* Detect and ignore this case, where we are trying to output
18253 something we have already output. */
18254 return;
18257 /* If the definition comes from the same place as the declaration,
18258 maybe use the old DIE. We always want the DIE for this function
18259 that has the *_pc attributes to be under comp_unit_die so the
18260 debugger can find it. We also need to do this for abstract
18261 instances of inlines, since the spec requires the out-of-line copy
18262 to have the same parent. For local class methods, this doesn't
18263 apply; we just use the old DIE. */
18264 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18265 && (DECL_ARTIFICIAL (decl)
18266 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18267 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18268 == (unsigned) s.line))))
18270 subr_die = old_die;
18272 /* Clear out the declaration attribute and the formal parameters.
18273 Do not remove all children, because it is possible that this
18274 declaration die was forced using force_decl_die(). In such
18275 cases die that forced declaration die (e.g. TAG_imported_module)
18276 is one of the children that we do not want to remove. */
18277 remove_AT (subr_die, DW_AT_declaration);
18278 remove_AT (subr_die, DW_AT_object_pointer);
18279 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18281 else
18283 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18284 add_AT_specification (subr_die, old_die);
18285 add_pubname (decl, subr_die);
18286 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18287 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18288 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18289 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18291 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18292 emit the real type on the definition die. */
18293 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18295 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18296 if (die == auto_die || die == decltype_auto_die)
18297 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18298 TYPE_UNQUALIFIED, context_die);
18302 else
18304 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18306 if (TREE_PUBLIC (decl))
18307 add_AT_flag (subr_die, DW_AT_external, 1);
18309 add_name_and_src_coords_attributes (subr_die, decl);
18310 add_pubname (decl, subr_die);
18311 if (debug_info_level > DINFO_LEVEL_TERSE)
18313 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18314 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18315 TYPE_UNQUALIFIED, context_die);
18318 add_pure_or_virtual_attribute (subr_die, decl);
18319 if (DECL_ARTIFICIAL (decl))
18320 add_AT_flag (subr_die, DW_AT_artificial, 1);
18322 add_accessibility_attribute (subr_die, decl);
18325 if (declaration)
18327 if (!old_die || !get_AT (old_die, DW_AT_inline))
18329 add_AT_flag (subr_die, DW_AT_declaration, 1);
18331 /* If this is an explicit function declaration then generate
18332 a DW_AT_explicit attribute. */
18333 if (lang_hooks.decls.function_decl_explicit_p (decl)
18334 && (dwarf_version >= 3 || !dwarf_strict))
18335 add_AT_flag (subr_die, DW_AT_explicit, 1);
18337 /* If this is a C++11 deleted special function member then generate
18338 a DW_AT_GNU_deleted attribute. */
18339 if (lang_hooks.decls.function_decl_deleted_p (decl)
18340 && (! dwarf_strict))
18341 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18343 /* The first time we see a member function, it is in the context of
18344 the class to which it belongs. We make sure of this by emitting
18345 the class first. The next time is the definition, which is
18346 handled above. The two may come from the same source text.
18348 Note that force_decl_die() forces function declaration die. It is
18349 later reused to represent definition. */
18350 equate_decl_number_to_die (decl, subr_die);
18353 else if (DECL_ABSTRACT_P (decl))
18355 if (DECL_DECLARED_INLINE_P (decl))
18357 if (cgraph_function_possibly_inlined_p (decl))
18358 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18359 else
18360 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18362 else
18364 if (cgraph_function_possibly_inlined_p (decl))
18365 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18366 else
18367 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18370 if (DECL_DECLARED_INLINE_P (decl)
18371 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18372 add_AT_flag (subr_die, DW_AT_artificial, 1);
18374 equate_decl_number_to_die (decl, subr_die);
18376 else if (!DECL_EXTERNAL (decl))
18378 HOST_WIDE_INT cfa_fb_offset;
18379 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18381 if (!old_die || !get_AT (old_die, DW_AT_inline))
18382 equate_decl_number_to_die (decl, subr_die);
18384 gcc_checking_assert (fun);
18385 if (!flag_reorder_blocks_and_partition)
18387 dw_fde_ref fde = fun->fde;
18388 if (fde->dw_fde_begin)
18390 /* We have already generated the labels. */
18391 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18392 fde->dw_fde_end, false);
18394 else
18396 /* Create start/end labels and add the range. */
18397 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18398 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18399 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18400 current_function_funcdef_no);
18401 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18402 current_function_funcdef_no);
18403 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18404 false);
18407 #if VMS_DEBUGGING_INFO
18408 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18409 Section 2.3 Prologue and Epilogue Attributes:
18410 When a breakpoint is set on entry to a function, it is generally
18411 desirable for execution to be suspended, not on the very first
18412 instruction of the function, but rather at a point after the
18413 function's frame has been set up, after any language defined local
18414 declaration processing has been completed, and before execution of
18415 the first statement of the function begins. Debuggers generally
18416 cannot properly determine where this point is. Similarly for a
18417 breakpoint set on exit from a function. The prologue and epilogue
18418 attributes allow a compiler to communicate the location(s) to use. */
18421 if (fde->dw_fde_vms_end_prologue)
18422 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18423 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18425 if (fde->dw_fde_vms_begin_epilogue)
18426 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18427 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18429 #endif
18432 else
18434 /* Generate pubnames entries for the split function code ranges. */
18435 dw_fde_ref fde = fun->fde;
18437 if (fde->dw_fde_second_begin)
18439 if (dwarf_version >= 3 || !dwarf_strict)
18441 /* We should use ranges for non-contiguous code section
18442 addresses. Use the actual code range for the initial
18443 section, since the HOT/COLD labels might precede an
18444 alignment offset. */
18445 bool range_list_added = false;
18446 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18447 fde->dw_fde_end, &range_list_added,
18448 false);
18449 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18450 fde->dw_fde_second_end,
18451 &range_list_added, false);
18452 if (range_list_added)
18453 add_ranges (NULL);
18455 else
18457 /* There is no real support in DW2 for this .. so we make
18458 a work-around. First, emit the pub name for the segment
18459 containing the function label. Then make and emit a
18460 simplified subprogram DIE for the second segment with the
18461 name pre-fixed by __hot/cold_sect_of_. We use the same
18462 linkage name for the second die so that gdb will find both
18463 sections when given "b foo". */
18464 const char *name = NULL;
18465 tree decl_name = DECL_NAME (decl);
18466 dw_die_ref seg_die;
18468 /* Do the 'primary' section. */
18469 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18470 fde->dw_fde_end, false);
18472 /* Build a minimal DIE for the secondary section. */
18473 seg_die = new_die (DW_TAG_subprogram,
18474 subr_die->die_parent, decl);
18476 if (TREE_PUBLIC (decl))
18477 add_AT_flag (seg_die, DW_AT_external, 1);
18479 if (decl_name != NULL
18480 && IDENTIFIER_POINTER (decl_name) != NULL)
18482 name = dwarf2_name (decl, 1);
18483 if (! DECL_ARTIFICIAL (decl))
18484 add_src_coords_attributes (seg_die, decl);
18486 add_linkage_name (seg_die, decl);
18488 gcc_assert (name != NULL);
18489 add_pure_or_virtual_attribute (seg_die, decl);
18490 if (DECL_ARTIFICIAL (decl))
18491 add_AT_flag (seg_die, DW_AT_artificial, 1);
18493 name = concat ("__second_sect_of_", name, NULL);
18494 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18495 fde->dw_fde_second_end, false);
18496 add_name_attribute (seg_die, name);
18497 if (want_pubnames ())
18498 add_pubname_string (name, seg_die);
18501 else
18502 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18503 false);
18506 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18508 /* We define the "frame base" as the function's CFA. This is more
18509 convenient for several reasons: (1) It's stable across the prologue
18510 and epilogue, which makes it better than just a frame pointer,
18511 (2) With dwarf3, there exists a one-byte encoding that allows us
18512 to reference the .debug_frame data by proxy, but failing that,
18513 (3) We can at least reuse the code inspection and interpretation
18514 code that determines the CFA position at various points in the
18515 function. */
18516 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18518 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18519 add_AT_loc (subr_die, DW_AT_frame_base, op);
18521 else
18523 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18524 if (list->dw_loc_next)
18525 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18526 else
18527 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18530 /* Compute a displacement from the "steady-state frame pointer" to
18531 the CFA. The former is what all stack slots and argument slots
18532 will reference in the rtl; the latter is what we've told the
18533 debugger about. We'll need to adjust all frame_base references
18534 by this displacement. */
18535 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18537 if (fun->static_chain_decl)
18538 add_AT_location_description (subr_die, DW_AT_static_link,
18539 loc_list_from_tree (fun->static_chain_decl, 2));
18542 /* Generate child dies for template paramaters. */
18543 if (debug_info_level > DINFO_LEVEL_TERSE)
18544 gen_generic_params_dies (decl);
18546 /* Now output descriptions of the arguments for this function. This gets
18547 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18548 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18549 `...' at the end of the formal parameter list. In order to find out if
18550 there was a trailing ellipsis or not, we must instead look at the type
18551 associated with the FUNCTION_DECL. This will be a node of type
18552 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18553 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18554 an ellipsis at the end. */
18556 /* In the case where we are describing a mere function declaration, all we
18557 need to do here (and all we *can* do here) is to describe the *types* of
18558 its formal parameters. */
18559 if (debug_info_level <= DINFO_LEVEL_TERSE)
18561 else if (declaration)
18562 gen_formal_types_die (decl, subr_die);
18563 else
18565 /* Generate DIEs to represent all known formal parameters. */
18566 tree parm = DECL_ARGUMENTS (decl);
18567 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18568 tree generic_decl_parm = generic_decl
18569 ? DECL_ARGUMENTS (generic_decl)
18570 : NULL;
18572 /* Now we want to walk the list of parameters of the function and
18573 emit their relevant DIEs.
18575 We consider the case of DECL being an instance of a generic function
18576 as well as it being a normal function.
18578 If DECL is an instance of a generic function we walk the
18579 parameters of the generic function declaration _and_ the parameters of
18580 DECL itself. This is useful because we want to emit specific DIEs for
18581 function parameter packs and those are declared as part of the
18582 generic function declaration. In that particular case,
18583 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18584 That DIE has children DIEs representing the set of arguments
18585 of the pack. Note that the set of pack arguments can be empty.
18586 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18587 children DIE.
18589 Otherwise, we just consider the parameters of DECL. */
18590 while (generic_decl_parm || parm)
18592 if (generic_decl_parm
18593 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18594 gen_formal_parameter_pack_die (generic_decl_parm,
18595 parm, subr_die,
18596 &parm);
18597 else if (parm)
18599 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18601 if (parm == DECL_ARGUMENTS (decl)
18602 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18603 && parm_die
18604 && (dwarf_version >= 3 || !dwarf_strict))
18605 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18607 parm = DECL_CHAIN (parm);
18610 if (generic_decl_parm)
18611 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18614 /* Decide whether we need an unspecified_parameters DIE at the end.
18615 There are 2 more cases to do this for: 1) the ansi ... declaration -
18616 this is detectable when the end of the arg list is not a
18617 void_type_node 2) an unprototyped function declaration (not a
18618 definition). This just means that we have no info about the
18619 parameters at all. */
18620 if (prototype_p (TREE_TYPE (decl)))
18622 /* This is the prototyped case, check for.... */
18623 if (stdarg_p (TREE_TYPE (decl)))
18624 gen_unspecified_parameters_die (decl, subr_die);
18626 else if (DECL_INITIAL (decl) == NULL_TREE)
18627 gen_unspecified_parameters_die (decl, subr_die);
18630 /* Output Dwarf info for all of the stuff within the body of the function
18631 (if it has one - it may be just a declaration). */
18632 outer_scope = DECL_INITIAL (decl);
18634 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18635 a function. This BLOCK actually represents the outermost binding contour
18636 for the function, i.e. the contour in which the function's formal
18637 parameters and labels get declared. Curiously, it appears that the front
18638 end doesn't actually put the PARM_DECL nodes for the current function onto
18639 the BLOCK_VARS list for this outer scope, but are strung off of the
18640 DECL_ARGUMENTS list for the function instead.
18642 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18643 the LABEL_DECL nodes for the function however, and we output DWARF info
18644 for those in decls_for_scope. Just within the `outer_scope' there will be
18645 a BLOCK node representing the function's outermost pair of curly braces,
18646 and any blocks used for the base and member initializers of a C++
18647 constructor function. */
18648 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18650 int call_site_note_count = 0;
18651 int tail_call_site_note_count = 0;
18653 /* Emit a DW_TAG_variable DIE for a named return value. */
18654 if (DECL_NAME (DECL_RESULT (decl)))
18655 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18657 decls_for_scope (outer_scope, subr_die, 0);
18659 if (call_arg_locations && !dwarf_strict)
18661 struct call_arg_loc_node *ca_loc;
18662 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18664 dw_die_ref die = NULL;
18665 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18666 rtx arg, next_arg;
18668 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18669 arg; arg = next_arg)
18671 dw_loc_descr_ref reg, val;
18672 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18673 dw_die_ref cdie, tdie = NULL;
18675 next_arg = XEXP (arg, 1);
18676 if (REG_P (XEXP (XEXP (arg, 0), 0))
18677 && next_arg
18678 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18679 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18680 && REGNO (XEXP (XEXP (arg, 0), 0))
18681 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18682 next_arg = XEXP (next_arg, 1);
18683 if (mode == VOIDmode)
18685 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18686 if (mode == VOIDmode)
18687 mode = GET_MODE (XEXP (arg, 0));
18689 if (mode == VOIDmode || mode == BLKmode)
18690 continue;
18691 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18693 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18694 tloc = XEXP (XEXP (arg, 0), 1);
18695 continue;
18697 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18698 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18700 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18701 tlocc = XEXP (XEXP (arg, 0), 1);
18702 continue;
18704 reg = NULL;
18705 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18706 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18707 VAR_INIT_STATUS_INITIALIZED);
18708 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18710 rtx mem = XEXP (XEXP (arg, 0), 0);
18711 reg = mem_loc_descriptor (XEXP (mem, 0),
18712 get_address_mode (mem),
18713 GET_MODE (mem),
18714 VAR_INIT_STATUS_INITIALIZED);
18716 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18717 == DEBUG_PARAMETER_REF)
18719 tree tdecl
18720 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18721 tdie = lookup_decl_die (tdecl);
18722 if (tdie == NULL)
18723 continue;
18725 else
18726 continue;
18727 if (reg == NULL
18728 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18729 != DEBUG_PARAMETER_REF)
18730 continue;
18731 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18732 VOIDmode,
18733 VAR_INIT_STATUS_INITIALIZED);
18734 if (val == NULL)
18735 continue;
18736 if (die == NULL)
18737 die = gen_call_site_die (decl, subr_die, ca_loc);
18738 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18739 NULL_TREE);
18740 if (reg != NULL)
18741 add_AT_loc (cdie, DW_AT_location, reg);
18742 else if (tdie != NULL)
18743 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18744 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18745 if (next_arg != XEXP (arg, 1))
18747 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18748 if (mode == VOIDmode)
18749 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18750 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18751 0), 1),
18752 mode, VOIDmode,
18753 VAR_INIT_STATUS_INITIALIZED);
18754 if (val != NULL)
18755 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18758 if (die == NULL
18759 && (ca_loc->symbol_ref || tloc))
18760 die = gen_call_site_die (decl, subr_die, ca_loc);
18761 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18763 dw_loc_descr_ref tval = NULL;
18765 if (tloc != NULL_RTX)
18766 tval = mem_loc_descriptor (tloc,
18767 GET_MODE (tloc) == VOIDmode
18768 ? Pmode : GET_MODE (tloc),
18769 VOIDmode,
18770 VAR_INIT_STATUS_INITIALIZED);
18771 if (tval)
18772 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18773 else if (tlocc != NULL_RTX)
18775 tval = mem_loc_descriptor (tlocc,
18776 GET_MODE (tlocc) == VOIDmode
18777 ? Pmode : GET_MODE (tlocc),
18778 VOIDmode,
18779 VAR_INIT_STATUS_INITIALIZED);
18780 if (tval)
18781 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18782 tval);
18785 if (die != NULL)
18787 call_site_note_count++;
18788 if (ca_loc->tail_call_p)
18789 tail_call_site_note_count++;
18793 call_arg_locations = NULL;
18794 call_arg_loc_last = NULL;
18795 if (tail_call_site_count >= 0
18796 && tail_call_site_count == tail_call_site_note_count
18797 && !dwarf_strict)
18799 if (call_site_count >= 0
18800 && call_site_count == call_site_note_count)
18801 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18802 else
18803 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18805 call_site_count = -1;
18806 tail_call_site_count = -1;
18809 if (subr_die != old_die)
18810 /* Add the calling convention attribute if requested. */
18811 add_calling_convention_attribute (subr_die, decl);
18814 /* Returns a hash value for X (which really is a die_struct). */
18816 hashval_t
18817 block_die_hasher::hash (die_struct *d)
18819 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18822 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18823 as decl_id and die_parent of die_struct Y. */
18825 bool
18826 block_die_hasher::equal (die_struct *x, die_struct *y)
18828 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
18831 /* Generate a DIE to represent a declared data object.
18832 Either DECL or ORIGIN must be non-null. */
18834 static void
18835 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18837 HOST_WIDE_INT off = 0;
18838 tree com_decl;
18839 tree decl_or_origin = decl ? decl : origin;
18840 tree ultimate_origin;
18841 dw_die_ref var_die;
18842 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18843 dw_die_ref origin_die;
18844 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18845 || class_or_namespace_scope_p (context_die));
18846 bool specialization_p = false;
18848 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18849 if (decl || ultimate_origin)
18850 origin = ultimate_origin;
18851 com_decl = fortran_common (decl_or_origin, &off);
18853 /* Symbol in common gets emitted as a child of the common block, in the form
18854 of a data member. */
18855 if (com_decl)
18857 dw_die_ref com_die;
18858 dw_loc_list_ref loc;
18859 die_node com_die_arg;
18861 var_die = lookup_decl_die (decl_or_origin);
18862 if (var_die)
18864 if (get_AT (var_die, DW_AT_location) == NULL)
18866 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18867 if (loc)
18869 if (off)
18871 /* Optimize the common case. */
18872 if (single_element_loc_list_p (loc)
18873 && loc->expr->dw_loc_opc == DW_OP_addr
18874 && loc->expr->dw_loc_next == NULL
18875 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18876 == SYMBOL_REF)
18878 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18879 loc->expr->dw_loc_oprnd1.v.val_addr
18880 = plus_constant (GET_MODE (x), x , off);
18882 else
18883 loc_list_plus_const (loc, off);
18885 add_AT_location_description (var_die, DW_AT_location, loc);
18886 remove_AT (var_die, DW_AT_declaration);
18889 return;
18892 if (common_block_die_table == NULL)
18893 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
18895 com_die_arg.decl_id = DECL_UID (com_decl);
18896 com_die_arg.die_parent = context_die;
18897 com_die = common_block_die_table->find (&com_die_arg);
18898 loc = loc_list_from_tree (com_decl, 2);
18899 if (com_die == NULL)
18901 const char *cnam
18902 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18903 die_node **slot;
18905 com_die = new_die (DW_TAG_common_block, context_die, decl);
18906 add_name_and_src_coords_attributes (com_die, com_decl);
18907 if (loc)
18909 add_AT_location_description (com_die, DW_AT_location, loc);
18910 /* Avoid sharing the same loc descriptor between
18911 DW_TAG_common_block and DW_TAG_variable. */
18912 loc = loc_list_from_tree (com_decl, 2);
18914 else if (DECL_EXTERNAL (decl))
18915 add_AT_flag (com_die, DW_AT_declaration, 1);
18916 if (want_pubnames ())
18917 add_pubname_string (cnam, com_die); /* ??? needed? */
18918 com_die->decl_id = DECL_UID (com_decl);
18919 slot = common_block_die_table->find_slot (com_die, INSERT);
18920 *slot = com_die;
18922 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18924 add_AT_location_description (com_die, DW_AT_location, loc);
18925 loc = loc_list_from_tree (com_decl, 2);
18926 remove_AT (com_die, DW_AT_declaration);
18928 var_die = new_die (DW_TAG_variable, com_die, decl);
18929 add_name_and_src_coords_attributes (var_die, decl);
18930 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
18931 context_die);
18932 add_AT_flag (var_die, DW_AT_external, 1);
18933 if (loc)
18935 if (off)
18937 /* Optimize the common case. */
18938 if (single_element_loc_list_p (loc)
18939 && loc->expr->dw_loc_opc == DW_OP_addr
18940 && loc->expr->dw_loc_next == NULL
18941 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18943 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18944 loc->expr->dw_loc_oprnd1.v.val_addr
18945 = plus_constant (GET_MODE (x), x, off);
18947 else
18948 loc_list_plus_const (loc, off);
18950 add_AT_location_description (var_die, DW_AT_location, loc);
18952 else if (DECL_EXTERNAL (decl))
18953 add_AT_flag (var_die, DW_AT_declaration, 1);
18954 equate_decl_number_to_die (decl, var_die);
18955 return;
18958 /* If the compiler emitted a definition for the DECL declaration
18959 and if we already emitted a DIE for it, don't emit a second
18960 DIE for it again. Allow re-declarations of DECLs that are
18961 inside functions, though. */
18962 if (old_die && declaration && !local_scope_p (context_die))
18963 return;
18965 /* For static data members, the declaration in the class is supposed
18966 to have DW_TAG_member tag; the specification should still be
18967 DW_TAG_variable referencing the DW_TAG_member DIE. */
18968 if (declaration && class_scope_p (context_die))
18969 var_die = new_die (DW_TAG_member, context_die, decl);
18970 else
18971 var_die = new_die (DW_TAG_variable, context_die, decl);
18973 origin_die = NULL;
18974 if (origin != NULL)
18975 origin_die = add_abstract_origin_attribute (var_die, origin);
18977 /* Loop unrolling can create multiple blocks that refer to the same
18978 static variable, so we must test for the DW_AT_declaration flag.
18980 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18981 copy decls and set the DECL_ABSTRACT_P flag on them instead of
18982 sharing them.
18984 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18986 ??? The declare_in_namespace support causes us to get two DIEs for one
18987 variable, both of which are declarations. We want to avoid considering
18988 one to be a specification, so we must test that this DIE is not a
18989 declaration. */
18990 else if (old_die && TREE_STATIC (decl) && ! declaration
18991 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18993 /* This is a definition of a C++ class level static. */
18994 add_AT_specification (var_die, old_die);
18995 specialization_p = true;
18996 if (DECL_NAME (decl))
18998 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18999 struct dwarf_file_data * file_index = lookup_filename (s.file);
19001 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19002 add_AT_file (var_die, DW_AT_decl_file, file_index);
19004 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19005 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19007 if (old_die->die_tag == DW_TAG_member)
19008 add_linkage_name (var_die, decl);
19011 else
19012 add_name_and_src_coords_attributes (var_die, decl);
19014 if ((origin == NULL && !specialization_p)
19015 || (origin != NULL
19016 && !DECL_ABSTRACT_P (decl_or_origin)
19017 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19018 decl_function_context
19019 (decl_or_origin))))
19021 tree type = TREE_TYPE (decl_or_origin);
19023 if (decl_by_reference_p (decl_or_origin))
19024 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19025 context_die);
19026 else
19027 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19028 context_die);
19031 if (origin == NULL && !specialization_p)
19033 if (TREE_PUBLIC (decl))
19034 add_AT_flag (var_die, DW_AT_external, 1);
19036 if (DECL_ARTIFICIAL (decl))
19037 add_AT_flag (var_die, DW_AT_artificial, 1);
19039 add_accessibility_attribute (var_die, decl);
19042 if (declaration)
19043 add_AT_flag (var_die, DW_AT_declaration, 1);
19045 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19046 equate_decl_number_to_die (decl, var_die);
19048 if (! declaration
19049 && (! DECL_ABSTRACT_P (decl_or_origin)
19050 /* Local static vars are shared between all clones/inlines,
19051 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19052 already set. */
19053 || (TREE_CODE (decl_or_origin) == VAR_DECL
19054 && TREE_STATIC (decl_or_origin)
19055 && DECL_RTL_SET_P (decl_or_origin)))
19056 /* When abstract origin already has DW_AT_location attribute, no need
19057 to add it again. */
19058 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19060 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19061 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19062 defer_location (decl_or_origin, var_die);
19063 else
19064 add_location_or_const_value_attribute (var_die, decl_or_origin,
19065 decl == NULL, DW_AT_location);
19066 add_pubname (decl_or_origin, var_die);
19068 else
19069 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19072 /* Generate a DIE to represent a named constant. */
19074 static void
19075 gen_const_die (tree decl, dw_die_ref context_die)
19077 dw_die_ref const_die;
19078 tree type = TREE_TYPE (decl);
19080 const_die = new_die (DW_TAG_constant, context_die, decl);
19081 add_name_and_src_coords_attributes (const_die, decl);
19082 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19083 if (TREE_PUBLIC (decl))
19084 add_AT_flag (const_die, DW_AT_external, 1);
19085 if (DECL_ARTIFICIAL (decl))
19086 add_AT_flag (const_die, DW_AT_artificial, 1);
19087 tree_add_const_value_attribute_for_decl (const_die, decl);
19090 /* Generate a DIE to represent a label identifier. */
19092 static void
19093 gen_label_die (tree decl, dw_die_ref context_die)
19095 tree origin = decl_ultimate_origin (decl);
19096 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19097 rtx insn;
19098 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19100 if (origin != NULL)
19101 add_abstract_origin_attribute (lbl_die, origin);
19102 else
19103 add_name_and_src_coords_attributes (lbl_die, decl);
19105 if (DECL_ABSTRACT_P (decl))
19106 equate_decl_number_to_die (decl, lbl_die);
19107 else
19109 insn = DECL_RTL_IF_SET (decl);
19111 /* Deleted labels are programmer specified labels which have been
19112 eliminated because of various optimizations. We still emit them
19113 here so that it is possible to put breakpoints on them. */
19114 if (insn
19115 && (LABEL_P (insn)
19116 || ((NOTE_P (insn)
19117 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19119 /* When optimization is enabled (via -O) some parts of the compiler
19120 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19121 represent source-level labels which were explicitly declared by
19122 the user. This really shouldn't be happening though, so catch
19123 it if it ever does happen. */
19124 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19126 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19127 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19129 else if (insn
19130 && NOTE_P (insn)
19131 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19132 && CODE_LABEL_NUMBER (insn) != -1)
19134 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19135 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19140 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19141 attributes to the DIE for a block STMT, to describe where the inlined
19142 function was called from. This is similar to add_src_coords_attributes. */
19144 static inline void
19145 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19147 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19149 if (dwarf_version >= 3 || !dwarf_strict)
19151 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19152 add_AT_unsigned (die, DW_AT_call_line, s.line);
19157 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19158 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19160 static inline void
19161 add_high_low_attributes (tree stmt, dw_die_ref die)
19163 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19165 if (BLOCK_FRAGMENT_CHAIN (stmt)
19166 && (dwarf_version >= 3 || !dwarf_strict))
19168 tree chain, superblock = NULL_TREE;
19169 dw_die_ref pdie;
19170 dw_attr_ref attr = NULL;
19172 if (inlined_function_outer_scope_p (stmt))
19174 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19175 BLOCK_NUMBER (stmt));
19176 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19179 /* Optimize duplicate .debug_ranges lists or even tails of
19180 lists. If this BLOCK has same ranges as its supercontext,
19181 lookup DW_AT_ranges attribute in the supercontext (and
19182 recursively so), verify that the ranges_table contains the
19183 right values and use it instead of adding a new .debug_range. */
19184 for (chain = stmt, pdie = die;
19185 BLOCK_SAME_RANGE (chain);
19186 chain = BLOCK_SUPERCONTEXT (chain))
19188 dw_attr_ref new_attr;
19190 pdie = pdie->die_parent;
19191 if (pdie == NULL)
19192 break;
19193 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19194 break;
19195 new_attr = get_AT (pdie, DW_AT_ranges);
19196 if (new_attr == NULL
19197 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19198 break;
19199 attr = new_attr;
19200 superblock = BLOCK_SUPERCONTEXT (chain);
19202 if (attr != NULL
19203 && (ranges_table[attr->dw_attr_val.v.val_offset
19204 / 2 / DWARF2_ADDR_SIZE].num
19205 == BLOCK_NUMBER (superblock))
19206 && BLOCK_FRAGMENT_CHAIN (superblock))
19208 unsigned long off = attr->dw_attr_val.v.val_offset
19209 / 2 / DWARF2_ADDR_SIZE;
19210 unsigned long supercnt = 0, thiscnt = 0;
19211 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19212 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19214 ++supercnt;
19215 gcc_checking_assert (ranges_table[off + supercnt].num
19216 == BLOCK_NUMBER (chain));
19218 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19219 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19220 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19221 ++thiscnt;
19222 gcc_assert (supercnt >= thiscnt);
19223 add_AT_range_list (die, DW_AT_ranges,
19224 ((off + supercnt - thiscnt)
19225 * 2 * DWARF2_ADDR_SIZE),
19226 false);
19227 return;
19230 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19232 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19235 add_ranges (chain);
19236 chain = BLOCK_FRAGMENT_CHAIN (chain);
19238 while (chain);
19239 add_ranges (NULL);
19241 else
19243 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19244 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19245 BLOCK_NUMBER (stmt));
19246 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19247 BLOCK_NUMBER (stmt));
19248 add_AT_low_high_pc (die, label, label_high, false);
19252 /* Generate a DIE for a lexical block. */
19254 static void
19255 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19257 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19259 if (call_arg_locations)
19261 if (block_map.length () <= BLOCK_NUMBER (stmt))
19262 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19263 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19266 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19267 add_high_low_attributes (stmt, stmt_die);
19269 decls_for_scope (stmt, stmt_die, depth);
19272 /* Generate a DIE for an inlined subprogram. */
19274 static void
19275 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19277 tree decl;
19279 /* The instance of function that is effectively being inlined shall not
19280 be abstract. */
19281 gcc_assert (! BLOCK_ABSTRACT (stmt));
19283 decl = block_ultimate_origin (stmt);
19285 /* Emit info for the abstract instance first, if we haven't yet. We
19286 must emit this even if the block is abstract, otherwise when we
19287 emit the block below (or elsewhere), we may end up trying to emit
19288 a die whose origin die hasn't been emitted, and crashing. */
19289 dwarf2out_abstract_function (decl);
19291 if (! BLOCK_ABSTRACT (stmt))
19293 dw_die_ref subr_die
19294 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19296 if (call_arg_locations)
19298 if (block_map.length () <= BLOCK_NUMBER (stmt))
19299 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19300 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19302 add_abstract_origin_attribute (subr_die, decl);
19303 if (TREE_ASM_WRITTEN (stmt))
19304 add_high_low_attributes (stmt, subr_die);
19305 add_call_src_coords_attributes (stmt, subr_die);
19307 decls_for_scope (stmt, subr_die, depth);
19311 /* Generate a DIE for a field in a record, or structure. */
19313 static void
19314 gen_field_die (tree decl, dw_die_ref context_die)
19316 dw_die_ref decl_die;
19318 if (TREE_TYPE (decl) == error_mark_node)
19319 return;
19321 decl_die = new_die (DW_TAG_member, context_die, decl);
19322 add_name_and_src_coords_attributes (decl_die, decl);
19323 add_type_attribute (decl_die, member_declared_type (decl),
19324 decl_quals (decl), context_die);
19326 if (DECL_BIT_FIELD_TYPE (decl))
19328 add_byte_size_attribute (decl_die, decl);
19329 add_bit_size_attribute (decl_die, decl);
19330 add_bit_offset_attribute (decl_die, decl);
19333 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19334 add_data_member_location_attribute (decl_die, decl);
19336 if (DECL_ARTIFICIAL (decl))
19337 add_AT_flag (decl_die, DW_AT_artificial, 1);
19339 add_accessibility_attribute (decl_die, decl);
19341 /* Equate decl number to die, so that we can look up this decl later on. */
19342 equate_decl_number_to_die (decl, decl_die);
19345 #if 0
19346 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19347 Use modified_type_die instead.
19348 We keep this code here just in case these types of DIEs may be needed to
19349 represent certain things in other languages (e.g. Pascal) someday. */
19351 static void
19352 gen_pointer_type_die (tree type, dw_die_ref context_die)
19354 dw_die_ref ptr_die
19355 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19357 equate_type_number_to_die (type, ptr_die);
19358 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19359 context_die);
19360 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19363 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19364 Use modified_type_die instead.
19365 We keep this code here just in case these types of DIEs may be needed to
19366 represent certain things in other languages (e.g. Pascal) someday. */
19368 static void
19369 gen_reference_type_die (tree type, dw_die_ref context_die)
19371 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19373 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19374 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19375 else
19376 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19378 equate_type_number_to_die (type, ref_die);
19379 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19380 context_die);
19381 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19383 #endif
19385 /* Generate a DIE for a pointer to a member type. */
19387 static void
19388 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19390 dw_die_ref ptr_die
19391 = new_die (DW_TAG_ptr_to_member_type,
19392 scope_die_for (type, context_die), type);
19394 equate_type_number_to_die (type, ptr_die);
19395 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19396 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19397 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19398 context_die);
19401 typedef const char *dchar_p; /* For DEF_VEC_P. */
19403 static char *producer_string;
19405 /* Return a heap allocated producer string including command line options
19406 if -grecord-gcc-switches. */
19408 static char *
19409 gen_producer_string (void)
19411 size_t j;
19412 auto_vec<dchar_p> switches;
19413 const char *language_string = lang_hooks.name;
19414 char *producer, *tail;
19415 const char *p;
19416 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19417 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19419 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19420 switch (save_decoded_options[j].opt_index)
19422 case OPT_o:
19423 case OPT_d:
19424 case OPT_dumpbase:
19425 case OPT_dumpdir:
19426 case OPT_auxbase:
19427 case OPT_auxbase_strip:
19428 case OPT_quiet:
19429 case OPT_version:
19430 case OPT_v:
19431 case OPT_w:
19432 case OPT_L:
19433 case OPT_D:
19434 case OPT_I:
19435 case OPT_U:
19436 case OPT_SPECIAL_unknown:
19437 case OPT_SPECIAL_ignore:
19438 case OPT_SPECIAL_program_name:
19439 case OPT_SPECIAL_input_file:
19440 case OPT_grecord_gcc_switches:
19441 case OPT_gno_record_gcc_switches:
19442 case OPT__output_pch_:
19443 case OPT_fdiagnostics_show_location_:
19444 case OPT_fdiagnostics_show_option:
19445 case OPT_fdiagnostics_show_caret:
19446 case OPT_fdiagnostics_color_:
19447 case OPT_fverbose_asm:
19448 case OPT____:
19449 case OPT__sysroot_:
19450 case OPT_nostdinc:
19451 case OPT_nostdinc__:
19452 /* Ignore these. */
19453 continue;
19454 default:
19455 if (cl_options[save_decoded_options[j].opt_index].flags
19456 & CL_NO_DWARF_RECORD)
19457 continue;
19458 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19459 == '-');
19460 switch (save_decoded_options[j].canonical_option[0][1])
19462 case 'M':
19463 case 'i':
19464 case 'W':
19465 continue;
19466 case 'f':
19467 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19468 "dump", 4) == 0)
19469 continue;
19470 break;
19471 default:
19472 break;
19474 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19475 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19476 break;
19479 producer = XNEWVEC (char, plen + 1 + len + 1);
19480 tail = producer;
19481 sprintf (tail, "%s %s", language_string, version_string);
19482 tail += plen;
19484 FOR_EACH_VEC_ELT (switches, j, p)
19486 len = strlen (p);
19487 *tail = ' ';
19488 memcpy (tail + 1, p, len);
19489 tail += len + 1;
19492 *tail = '\0';
19493 return producer;
19496 /* Generate the DIE for the compilation unit. */
19498 static dw_die_ref
19499 gen_compile_unit_die (const char *filename)
19501 dw_die_ref die;
19502 const char *language_string = lang_hooks.name;
19503 int language;
19505 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19507 if (filename)
19509 add_name_attribute (die, filename);
19510 /* Don't add cwd for <built-in>. */
19511 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19512 add_comp_dir_attribute (die);
19515 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19517 /* If our producer is LTO try to figure out a common language to use
19518 from the global list of translation units. */
19519 if (strcmp (language_string, "GNU GIMPLE") == 0)
19521 unsigned i;
19522 tree t;
19523 const char *common_lang = NULL;
19525 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19527 if (!TRANSLATION_UNIT_LANGUAGE (t))
19528 continue;
19529 if (!common_lang)
19530 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19531 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19533 else if (strncmp (common_lang, "GNU C", 5) == 0
19534 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19535 /* Mixing C and C++ is ok, use C++ in that case. */
19536 common_lang = "GNU C++";
19537 else
19539 /* Fall back to C. */
19540 common_lang = NULL;
19541 break;
19545 if (common_lang)
19546 language_string = common_lang;
19549 language = DW_LANG_C89;
19550 if (strcmp (language_string, "GNU C++") == 0)
19551 language = DW_LANG_C_plus_plus;
19552 else if (strcmp (language_string, "GNU F77") == 0)
19553 language = DW_LANG_Fortran77;
19554 else if (strcmp (language_string, "GNU Pascal") == 0)
19555 language = DW_LANG_Pascal83;
19556 else if (dwarf_version >= 3 || !dwarf_strict)
19558 if (strcmp (language_string, "GNU Ada") == 0)
19559 language = DW_LANG_Ada95;
19560 else if (strcmp (language_string, "GNU Fortran") == 0)
19561 language = DW_LANG_Fortran95;
19562 else if (strcmp (language_string, "GNU Java") == 0)
19563 language = DW_LANG_Java;
19564 else if (strcmp (language_string, "GNU Objective-C") == 0)
19565 language = DW_LANG_ObjC;
19566 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19567 language = DW_LANG_ObjC_plus_plus;
19568 else if (dwarf_version >= 5 || !dwarf_strict)
19570 if (strcmp (language_string, "GNU Go") == 0)
19571 language = DW_LANG_Go;
19574 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19575 else if (strcmp (language_string, "GNU Fortran") == 0)
19576 language = DW_LANG_Fortran90;
19578 add_AT_unsigned (die, DW_AT_language, language);
19580 switch (language)
19582 case DW_LANG_Fortran77:
19583 case DW_LANG_Fortran90:
19584 case DW_LANG_Fortran95:
19585 /* Fortran has case insensitive identifiers and the front-end
19586 lowercases everything. */
19587 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19588 break;
19589 default:
19590 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19591 break;
19593 return die;
19596 /* Generate the DIE for a base class. */
19598 static void
19599 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19601 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19603 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19604 add_data_member_location_attribute (die, binfo);
19606 if (BINFO_VIRTUAL_P (binfo))
19607 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19609 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19610 children, otherwise the default is DW_ACCESS_public. In DWARF2
19611 the default has always been DW_ACCESS_private. */
19612 if (access == access_public_node)
19614 if (dwarf_version == 2
19615 || context_die->die_tag == DW_TAG_class_type)
19616 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19618 else if (access == access_protected_node)
19619 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19620 else if (dwarf_version > 2
19621 && context_die->die_tag != DW_TAG_class_type)
19622 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19625 /* Generate a DIE for a class member. */
19627 static void
19628 gen_member_die (tree type, dw_die_ref context_die)
19630 tree member;
19631 tree binfo = TYPE_BINFO (type);
19632 dw_die_ref child;
19634 /* If this is not an incomplete type, output descriptions of each of its
19635 members. Note that as we output the DIEs necessary to represent the
19636 members of this record or union type, we will also be trying to output
19637 DIEs to represent the *types* of those members. However the `type'
19638 function (above) will specifically avoid generating type DIEs for member
19639 types *within* the list of member DIEs for this (containing) type except
19640 for those types (of members) which are explicitly marked as also being
19641 members of this (containing) type themselves. The g++ front- end can
19642 force any given type to be treated as a member of some other (containing)
19643 type by setting the TYPE_CONTEXT of the given (member) type to point to
19644 the TREE node representing the appropriate (containing) type. */
19646 /* First output info about the base classes. */
19647 if (binfo)
19649 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19650 int i;
19651 tree base;
19653 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19654 gen_inheritance_die (base,
19655 (accesses ? (*accesses)[i] : access_public_node),
19656 context_die);
19659 /* Now output info about the data members and type members. */
19660 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19662 /* If we thought we were generating minimal debug info for TYPE
19663 and then changed our minds, some of the member declarations
19664 may have already been defined. Don't define them again, but
19665 do put them in the right order. */
19667 child = lookup_decl_die (member);
19668 if (child)
19669 splice_child_die (context_die, child);
19670 else
19671 gen_decl_die (member, NULL, context_die);
19674 /* Now output info about the function members (if any). */
19675 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19677 /* Don't include clones in the member list. */
19678 if (DECL_ABSTRACT_ORIGIN (member))
19679 continue;
19681 child = lookup_decl_die (member);
19682 if (child)
19683 splice_child_die (context_die, child);
19684 else
19685 gen_decl_die (member, NULL, context_die);
19689 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19690 is set, we pretend that the type was never defined, so we only get the
19691 member DIEs needed by later specification DIEs. */
19693 static void
19694 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19695 enum debug_info_usage usage)
19697 dw_die_ref type_die = lookup_type_die (type);
19698 dw_die_ref scope_die = 0;
19699 int nested = 0;
19700 int complete = (TYPE_SIZE (type)
19701 && (! TYPE_STUB_DECL (type)
19702 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19703 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19704 complete = complete && should_emit_struct_debug (type, usage);
19706 if (type_die && ! complete)
19707 return;
19709 if (TYPE_CONTEXT (type) != NULL_TREE
19710 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19711 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19712 nested = 1;
19714 scope_die = scope_die_for (type, context_die);
19716 /* Generate child dies for template paramaters. */
19717 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19718 schedule_generic_params_dies_gen (type);
19720 if (! type_die || (nested && is_cu_die (scope_die)))
19721 /* First occurrence of type or toplevel definition of nested class. */
19723 dw_die_ref old_die = type_die;
19725 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19726 ? record_type_tag (type) : DW_TAG_union_type,
19727 scope_die, type);
19728 equate_type_number_to_die (type, type_die);
19729 if (old_die)
19730 add_AT_specification (type_die, old_die);
19731 else
19732 add_name_attribute (type_die, type_tag (type));
19734 else
19735 remove_AT (type_die, DW_AT_declaration);
19737 /* If this type has been completed, then give it a byte_size attribute and
19738 then give a list of members. */
19739 if (complete && !ns_decl)
19741 /* Prevent infinite recursion in cases where the type of some member of
19742 this type is expressed in terms of this type itself. */
19743 TREE_ASM_WRITTEN (type) = 1;
19744 add_byte_size_attribute (type_die, type);
19745 if (TYPE_STUB_DECL (type) != NULL_TREE)
19747 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19748 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19751 /* If the first reference to this type was as the return type of an
19752 inline function, then it may not have a parent. Fix this now. */
19753 if (type_die->die_parent == NULL)
19754 add_child_die (scope_die, type_die);
19756 push_decl_scope (type);
19757 gen_member_die (type, type_die);
19758 pop_decl_scope ();
19760 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19761 if (TYPE_ARTIFICIAL (type))
19762 add_AT_flag (type_die, DW_AT_artificial, 1);
19764 /* GNU extension: Record what type our vtable lives in. */
19765 if (TYPE_VFIELD (type))
19767 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19769 gen_type_die (vtype, context_die);
19770 add_AT_die_ref (type_die, DW_AT_containing_type,
19771 lookup_type_die (vtype));
19774 else
19776 add_AT_flag (type_die, DW_AT_declaration, 1);
19778 /* We don't need to do this for function-local types. */
19779 if (TYPE_STUB_DECL (type)
19780 && ! decl_function_context (TYPE_STUB_DECL (type)))
19781 vec_safe_push (incomplete_types, type);
19784 if (get_AT (type_die, DW_AT_name))
19785 add_pubtype (type, type_die);
19788 /* Generate a DIE for a subroutine _type_. */
19790 static void
19791 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19793 tree return_type = TREE_TYPE (type);
19794 dw_die_ref subr_die
19795 = new_die (DW_TAG_subroutine_type,
19796 scope_die_for (type, context_die), type);
19798 equate_type_number_to_die (type, subr_die);
19799 add_prototyped_attribute (subr_die, type);
19800 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
19801 gen_formal_types_die (type, subr_die);
19803 if (get_AT (subr_die, DW_AT_name))
19804 add_pubtype (type, subr_die);
19807 /* Generate a DIE for a type definition. */
19809 static void
19810 gen_typedef_die (tree decl, dw_die_ref context_die)
19812 dw_die_ref type_die;
19813 tree origin;
19815 if (TREE_ASM_WRITTEN (decl))
19816 return;
19818 TREE_ASM_WRITTEN (decl) = 1;
19819 type_die = new_die (DW_TAG_typedef, context_die, decl);
19820 origin = decl_ultimate_origin (decl);
19821 if (origin != NULL)
19822 add_abstract_origin_attribute (type_die, origin);
19823 else
19825 tree type;
19827 add_name_and_src_coords_attributes (type_die, decl);
19828 if (DECL_ORIGINAL_TYPE (decl))
19830 type = DECL_ORIGINAL_TYPE (decl);
19832 gcc_assert (type != TREE_TYPE (decl));
19833 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19835 else
19837 type = TREE_TYPE (decl);
19839 if (is_naming_typedef_decl (TYPE_NAME (type)))
19841 /* Here, we are in the case of decl being a typedef naming
19842 an anonymous type, e.g:
19843 typedef struct {...} foo;
19844 In that case TREE_TYPE (decl) is not a typedef variant
19845 type and TYPE_NAME of the anonymous type is set to the
19846 TYPE_DECL of the typedef. This construct is emitted by
19847 the C++ FE.
19849 TYPE is the anonymous struct named by the typedef
19850 DECL. As we need the DW_AT_type attribute of the
19851 DW_TAG_typedef to point to the DIE of TYPE, let's
19852 generate that DIE right away. add_type_attribute
19853 called below will then pick (via lookup_type_die) that
19854 anonymous struct DIE. */
19855 if (!TREE_ASM_WRITTEN (type))
19856 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19858 /* This is a GNU Extension. We are adding a
19859 DW_AT_linkage_name attribute to the DIE of the
19860 anonymous struct TYPE. The value of that attribute
19861 is the name of the typedef decl naming the anonymous
19862 struct. This greatly eases the work of consumers of
19863 this debug info. */
19864 add_linkage_attr (lookup_type_die (type), decl);
19868 add_type_attribute (type_die, type, decl_quals (decl), context_die);
19870 if (is_naming_typedef_decl (decl))
19871 /* We want that all subsequent calls to lookup_type_die with
19872 TYPE in argument yield the DW_TAG_typedef we have just
19873 created. */
19874 equate_type_number_to_die (type, type_die);
19876 add_accessibility_attribute (type_die, decl);
19879 if (DECL_ABSTRACT_P (decl))
19880 equate_decl_number_to_die (decl, type_die);
19882 if (get_AT (type_die, DW_AT_name))
19883 add_pubtype (decl, type_die);
19886 /* Generate a DIE for a struct, class, enum or union type. */
19888 static void
19889 gen_tagged_type_die (tree type,
19890 dw_die_ref context_die,
19891 enum debug_info_usage usage)
19893 int need_pop;
19895 if (type == NULL_TREE
19896 || !is_tagged_type (type))
19897 return;
19899 /* If this is a nested type whose containing class hasn't been written
19900 out yet, writing it out will cover this one, too. This does not apply
19901 to instantiations of member class templates; they need to be added to
19902 the containing class as they are generated. FIXME: This hurts the
19903 idea of combining type decls from multiple TUs, since we can't predict
19904 what set of template instantiations we'll get. */
19905 if (TYPE_CONTEXT (type)
19906 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19907 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19909 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19911 if (TREE_ASM_WRITTEN (type))
19912 return;
19914 /* If that failed, attach ourselves to the stub. */
19915 push_decl_scope (TYPE_CONTEXT (type));
19916 context_die = lookup_type_die (TYPE_CONTEXT (type));
19917 need_pop = 1;
19919 else if (TYPE_CONTEXT (type) != NULL_TREE
19920 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19922 /* If this type is local to a function that hasn't been written
19923 out yet, use a NULL context for now; it will be fixed up in
19924 decls_for_scope. */
19925 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19926 /* A declaration DIE doesn't count; nested types need to go in the
19927 specification. */
19928 if (context_die && is_declaration_die (context_die))
19929 context_die = NULL;
19930 need_pop = 0;
19932 else
19934 context_die = declare_in_namespace (type, context_die);
19935 need_pop = 0;
19938 if (TREE_CODE (type) == ENUMERAL_TYPE)
19940 /* This might have been written out by the call to
19941 declare_in_namespace. */
19942 if (!TREE_ASM_WRITTEN (type))
19943 gen_enumeration_type_die (type, context_die);
19945 else
19946 gen_struct_or_union_type_die (type, context_die, usage);
19948 if (need_pop)
19949 pop_decl_scope ();
19951 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19952 it up if it is ever completed. gen_*_type_die will set it for us
19953 when appropriate. */
19956 /* Generate a type description DIE. */
19958 static void
19959 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19960 enum debug_info_usage usage)
19962 struct array_descr_info info;
19964 if (type == NULL_TREE || type == error_mark_node)
19965 return;
19967 if (TYPE_NAME (type) != NULL_TREE
19968 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19969 && is_redundant_typedef (TYPE_NAME (type))
19970 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19971 /* The DECL of this type is a typedef we don't want to emit debug
19972 info for but we want debug info for its underlying typedef.
19973 This can happen for e.g, the injected-class-name of a C++
19974 type. */
19975 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19977 /* If TYPE is a typedef type variant, let's generate debug info
19978 for the parent typedef which TYPE is a type of. */
19979 if (typedef_variant_p (type))
19981 if (TREE_ASM_WRITTEN (type))
19982 return;
19984 /* Prevent broken recursion; we can't hand off to the same type. */
19985 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19987 /* Give typedefs the right scope. */
19988 context_die = scope_die_for (type, context_die);
19990 TREE_ASM_WRITTEN (type) = 1;
19992 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19993 return;
19996 /* If type is an anonymous tagged type named by a typedef, let's
19997 generate debug info for the typedef. */
19998 if (is_naming_typedef_decl (TYPE_NAME (type)))
20000 /* Use the DIE of the containing namespace as the parent DIE of
20001 the type description DIE we want to generate. */
20002 if (DECL_CONTEXT (TYPE_NAME (type))
20003 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20004 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20006 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20007 return;
20010 /* If this is an array type with hidden descriptor, handle it first. */
20011 if (!TREE_ASM_WRITTEN (type)
20012 && lang_hooks.types.get_array_descr_info
20013 && lang_hooks.types.get_array_descr_info (type, &info)
20014 && (dwarf_version >= 3 || !dwarf_strict))
20016 gen_descr_array_type_die (type, &info, context_die);
20017 TREE_ASM_WRITTEN (type) = 1;
20018 return;
20021 /* We are going to output a DIE to represent the unqualified version
20022 of this type (i.e. without any const or volatile qualifiers) so
20023 get the main variant (i.e. the unqualified version) of this type
20024 now. (Vectors are special because the debugging info is in the
20025 cloned type itself). */
20026 if (TREE_CODE (type) != VECTOR_TYPE)
20027 type = type_main_variant (type);
20029 if (TREE_ASM_WRITTEN (type))
20030 return;
20032 switch (TREE_CODE (type))
20034 case ERROR_MARK:
20035 break;
20037 case POINTER_TYPE:
20038 case REFERENCE_TYPE:
20039 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20040 ensures that the gen_type_die recursion will terminate even if the
20041 type is recursive. Recursive types are possible in Ada. */
20042 /* ??? We could perhaps do this for all types before the switch
20043 statement. */
20044 TREE_ASM_WRITTEN (type) = 1;
20046 /* For these types, all that is required is that we output a DIE (or a
20047 set of DIEs) to represent the "basis" type. */
20048 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20049 DINFO_USAGE_IND_USE);
20050 break;
20052 case OFFSET_TYPE:
20053 /* This code is used for C++ pointer-to-data-member types.
20054 Output a description of the relevant class type. */
20055 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20056 DINFO_USAGE_IND_USE);
20058 /* Output a description of the type of the object pointed to. */
20059 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20060 DINFO_USAGE_IND_USE);
20062 /* Now output a DIE to represent this pointer-to-data-member type
20063 itself. */
20064 gen_ptr_to_mbr_type_die (type, context_die);
20065 break;
20067 case FUNCTION_TYPE:
20068 /* Force out return type (in case it wasn't forced out already). */
20069 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20070 DINFO_USAGE_DIR_USE);
20071 gen_subroutine_type_die (type, context_die);
20072 break;
20074 case METHOD_TYPE:
20075 /* Force out return type (in case it wasn't forced out already). */
20076 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20077 DINFO_USAGE_DIR_USE);
20078 gen_subroutine_type_die (type, context_die);
20079 break;
20081 case ARRAY_TYPE:
20082 gen_array_type_die (type, context_die);
20083 break;
20085 case VECTOR_TYPE:
20086 gen_array_type_die (type, context_die);
20087 break;
20089 case ENUMERAL_TYPE:
20090 case RECORD_TYPE:
20091 case UNION_TYPE:
20092 case QUAL_UNION_TYPE:
20093 gen_tagged_type_die (type, context_die, usage);
20094 return;
20096 case VOID_TYPE:
20097 case INTEGER_TYPE:
20098 case REAL_TYPE:
20099 case FIXED_POINT_TYPE:
20100 case COMPLEX_TYPE:
20101 case BOOLEAN_TYPE:
20102 /* No DIEs needed for fundamental types. */
20103 break;
20105 case NULLPTR_TYPE:
20106 case LANG_TYPE:
20107 /* Just use DW_TAG_unspecified_type. */
20109 dw_die_ref type_die = lookup_type_die (type);
20110 if (type_die == NULL)
20112 tree name = TYPE_IDENTIFIER (type);
20113 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20114 type);
20115 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20116 equate_type_number_to_die (type, type_die);
20119 break;
20121 default:
20122 if (is_cxx_auto (type))
20124 tree name = TYPE_IDENTIFIER (type);
20125 dw_die_ref *die = (name == get_identifier ("auto")
20126 ? &auto_die : &decltype_auto_die);
20127 if (!*die)
20129 *die = new_die (DW_TAG_unspecified_type,
20130 comp_unit_die (), NULL_TREE);
20131 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20133 equate_type_number_to_die (type, *die);
20134 break;
20136 gcc_unreachable ();
20139 TREE_ASM_WRITTEN (type) = 1;
20142 static void
20143 gen_type_die (tree type, dw_die_ref context_die)
20145 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20148 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20149 things which are local to the given block. */
20151 static void
20152 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20154 int must_output_die = 0;
20155 bool inlined_func;
20157 /* Ignore blocks that are NULL. */
20158 if (stmt == NULL_TREE)
20159 return;
20161 inlined_func = inlined_function_outer_scope_p (stmt);
20163 /* If the block is one fragment of a non-contiguous block, do not
20164 process the variables, since they will have been done by the
20165 origin block. Do process subblocks. */
20166 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20168 tree sub;
20170 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20171 gen_block_die (sub, context_die, depth + 1);
20173 return;
20176 /* Determine if we need to output any Dwarf DIEs at all to represent this
20177 block. */
20178 if (inlined_func)
20179 /* The outer scopes for inlinings *must* always be represented. We
20180 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20181 must_output_die = 1;
20182 else
20184 /* Determine if this block directly contains any "significant"
20185 local declarations which we will need to output DIEs for. */
20186 if (debug_info_level > DINFO_LEVEL_TERSE)
20187 /* We are not in terse mode so *any* local declaration counts
20188 as being a "significant" one. */
20189 must_output_die = ((BLOCK_VARS (stmt) != NULL
20190 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20191 && (TREE_USED (stmt)
20192 || TREE_ASM_WRITTEN (stmt)
20193 || BLOCK_ABSTRACT (stmt)));
20194 else if ((TREE_USED (stmt)
20195 || TREE_ASM_WRITTEN (stmt)
20196 || BLOCK_ABSTRACT (stmt))
20197 && !dwarf2out_ignore_block (stmt))
20198 must_output_die = 1;
20201 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20202 DIE for any block which contains no significant local declarations at
20203 all. Rather, in such cases we just call `decls_for_scope' so that any
20204 needed Dwarf info for any sub-blocks will get properly generated. Note
20205 that in terse mode, our definition of what constitutes a "significant"
20206 local declaration gets restricted to include only inlined function
20207 instances and local (nested) function definitions. */
20208 if (must_output_die)
20210 if (inlined_func)
20212 /* If STMT block is abstract, that means we have been called
20213 indirectly from dwarf2out_abstract_function.
20214 That function rightfully marks the descendent blocks (of
20215 the abstract function it is dealing with) as being abstract,
20216 precisely to prevent us from emitting any
20217 DW_TAG_inlined_subroutine DIE as a descendent
20218 of an abstract function instance. So in that case, we should
20219 not call gen_inlined_subroutine_die.
20221 Later though, when cgraph asks dwarf2out to emit info
20222 for the concrete instance of the function decl into which
20223 the concrete instance of STMT got inlined, the later will lead
20224 to the generation of a DW_TAG_inlined_subroutine DIE. */
20225 if (! BLOCK_ABSTRACT (stmt))
20226 gen_inlined_subroutine_die (stmt, context_die, depth);
20228 else
20229 gen_lexical_block_die (stmt, context_die, depth);
20231 else
20232 decls_for_scope (stmt, context_die, depth);
20235 /* Process variable DECL (or variable with origin ORIGIN) within
20236 block STMT and add it to CONTEXT_DIE. */
20237 static void
20238 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20240 dw_die_ref die;
20241 tree decl_or_origin = decl ? decl : origin;
20243 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20244 die = lookup_decl_die (decl_or_origin);
20245 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20246 && TYPE_DECL_IS_STUB (decl_or_origin))
20247 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20248 else
20249 die = NULL;
20251 if (die != NULL && die->die_parent == NULL)
20252 add_child_die (context_die, die);
20253 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20254 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20255 stmt, context_die);
20256 else
20257 gen_decl_die (decl, origin, context_die);
20260 /* Generate all of the decls declared within a given scope and (recursively)
20261 all of its sub-blocks. */
20263 static void
20264 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20266 tree decl;
20267 unsigned int i;
20268 tree subblocks;
20270 /* Ignore NULL blocks. */
20271 if (stmt == NULL_TREE)
20272 return;
20274 /* Output the DIEs to represent all of the data objects and typedefs
20275 declared directly within this block but not within any nested
20276 sub-blocks. Also, nested function and tag DIEs have been
20277 generated with a parent of NULL; fix that up now. We don't
20278 have to do this if we're at -g1. */
20279 if (debug_info_level > DINFO_LEVEL_TERSE)
20281 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20282 process_scope_var (stmt, decl, NULL_TREE, context_die);
20283 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20284 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20285 context_die);
20288 /* Even if we're at -g1, we need to process the subblocks in order to get
20289 inlined call information. */
20291 /* Output the DIEs to represent all sub-blocks (and the items declared
20292 therein) of this block. */
20293 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20294 subblocks != NULL;
20295 subblocks = BLOCK_CHAIN (subblocks))
20296 gen_block_die (subblocks, context_die, depth + 1);
20299 /* Is this a typedef we can avoid emitting? */
20301 static inline int
20302 is_redundant_typedef (const_tree decl)
20304 if (TYPE_DECL_IS_STUB (decl))
20305 return 1;
20307 if (DECL_ARTIFICIAL (decl)
20308 && DECL_CONTEXT (decl)
20309 && is_tagged_type (DECL_CONTEXT (decl))
20310 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20311 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20312 /* Also ignore the artificial member typedef for the class name. */
20313 return 1;
20315 return 0;
20318 /* Return TRUE if TYPE is a typedef that names a type for linkage
20319 purposes. This kind of typedefs is produced by the C++ FE for
20320 constructs like:
20322 typedef struct {...} foo;
20324 In that case, there is no typedef variant type produced for foo.
20325 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20326 struct type. */
20328 static bool
20329 is_naming_typedef_decl (const_tree decl)
20331 if (decl == NULL_TREE
20332 || TREE_CODE (decl) != TYPE_DECL
20333 || !is_tagged_type (TREE_TYPE (decl))
20334 || DECL_IS_BUILTIN (decl)
20335 || is_redundant_typedef (decl)
20336 /* It looks like Ada produces TYPE_DECLs that are very similar
20337 to C++ naming typedefs but that have different
20338 semantics. Let's be specific to c++ for now. */
20339 || !is_cxx ())
20340 return FALSE;
20342 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20343 && TYPE_NAME (TREE_TYPE (decl)) == decl
20344 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20345 != TYPE_NAME (TREE_TYPE (decl))));
20348 /* Returns the DIE for a context. */
20350 static inline dw_die_ref
20351 get_context_die (tree context)
20353 if (context)
20355 /* Find die that represents this context. */
20356 if (TYPE_P (context))
20358 context = TYPE_MAIN_VARIANT (context);
20359 return strip_naming_typedef (context, force_type_die (context));
20361 else
20362 return force_decl_die (context);
20364 return comp_unit_die ();
20367 /* Returns the DIE for decl. A DIE will always be returned. */
20369 static dw_die_ref
20370 force_decl_die (tree decl)
20372 dw_die_ref decl_die;
20373 unsigned saved_external_flag;
20374 tree save_fn = NULL_TREE;
20375 decl_die = lookup_decl_die (decl);
20376 if (!decl_die)
20378 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20380 decl_die = lookup_decl_die (decl);
20381 if (decl_die)
20382 return decl_die;
20384 switch (TREE_CODE (decl))
20386 case FUNCTION_DECL:
20387 /* Clear current_function_decl, so that gen_subprogram_die thinks
20388 that this is a declaration. At this point, we just want to force
20389 declaration die. */
20390 save_fn = current_function_decl;
20391 current_function_decl = NULL_TREE;
20392 gen_subprogram_die (decl, context_die);
20393 current_function_decl = save_fn;
20394 break;
20396 case VAR_DECL:
20397 /* Set external flag to force declaration die. Restore it after
20398 gen_decl_die() call. */
20399 saved_external_flag = DECL_EXTERNAL (decl);
20400 DECL_EXTERNAL (decl) = 1;
20401 gen_decl_die (decl, NULL, context_die);
20402 DECL_EXTERNAL (decl) = saved_external_flag;
20403 break;
20405 case NAMESPACE_DECL:
20406 if (dwarf_version >= 3 || !dwarf_strict)
20407 dwarf2out_decl (decl);
20408 else
20409 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20410 decl_die = comp_unit_die ();
20411 break;
20413 case TRANSLATION_UNIT_DECL:
20414 decl_die = comp_unit_die ();
20415 break;
20417 default:
20418 gcc_unreachable ();
20421 /* We should be able to find the DIE now. */
20422 if (!decl_die)
20423 decl_die = lookup_decl_die (decl);
20424 gcc_assert (decl_die);
20427 return decl_die;
20430 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20431 always returned. */
20433 static dw_die_ref
20434 force_type_die (tree type)
20436 dw_die_ref type_die;
20438 type_die = lookup_type_die (type);
20439 if (!type_die)
20441 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20443 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20444 context_die);
20445 gcc_assert (type_die);
20447 return type_die;
20450 /* Force out any required namespaces to be able to output DECL,
20451 and return the new context_die for it, if it's changed. */
20453 static dw_die_ref
20454 setup_namespace_context (tree thing, dw_die_ref context_die)
20456 tree context = (DECL_P (thing)
20457 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20458 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20459 /* Force out the namespace. */
20460 context_die = force_decl_die (context);
20462 return context_die;
20465 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20466 type) within its namespace, if appropriate.
20468 For compatibility with older debuggers, namespace DIEs only contain
20469 declarations; all definitions are emitted at CU scope. */
20471 static dw_die_ref
20472 declare_in_namespace (tree thing, dw_die_ref context_die)
20474 dw_die_ref ns_context;
20476 if (debug_info_level <= DINFO_LEVEL_TERSE)
20477 return context_die;
20479 /* If this decl is from an inlined function, then don't try to emit it in its
20480 namespace, as we will get confused. It would have already been emitted
20481 when the abstract instance of the inline function was emitted anyways. */
20482 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20483 return context_die;
20485 ns_context = setup_namespace_context (thing, context_die);
20487 if (ns_context != context_die)
20489 if (is_fortran ())
20490 return ns_context;
20491 if (DECL_P (thing))
20492 gen_decl_die (thing, NULL, ns_context);
20493 else
20494 gen_type_die (thing, ns_context);
20496 return context_die;
20499 /* Generate a DIE for a namespace or namespace alias. */
20501 static void
20502 gen_namespace_die (tree decl, dw_die_ref context_die)
20504 dw_die_ref namespace_die;
20506 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20507 they are an alias of. */
20508 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20510 /* Output a real namespace or module. */
20511 context_die = setup_namespace_context (decl, comp_unit_die ());
20512 namespace_die = new_die (is_fortran ()
20513 ? DW_TAG_module : DW_TAG_namespace,
20514 context_die, decl);
20515 /* For Fortran modules defined in different CU don't add src coords. */
20516 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20518 const char *name = dwarf2_name (decl, 0);
20519 if (name)
20520 add_name_attribute (namespace_die, name);
20522 else
20523 add_name_and_src_coords_attributes (namespace_die, decl);
20524 if (DECL_EXTERNAL (decl))
20525 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20526 equate_decl_number_to_die (decl, namespace_die);
20528 else
20530 /* Output a namespace alias. */
20532 /* Force out the namespace we are an alias of, if necessary. */
20533 dw_die_ref origin_die
20534 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20536 if (DECL_FILE_SCOPE_P (decl)
20537 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20538 context_die = setup_namespace_context (decl, comp_unit_die ());
20539 /* Now create the namespace alias DIE. */
20540 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20541 add_name_and_src_coords_attributes (namespace_die, decl);
20542 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20543 equate_decl_number_to_die (decl, namespace_die);
20545 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20546 if (want_pubnames ())
20547 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20550 /* Generate Dwarf debug information for a decl described by DECL.
20551 The return value is currently only meaningful for PARM_DECLs,
20552 for all other decls it returns NULL. */
20554 static dw_die_ref
20555 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20557 tree decl_or_origin = decl ? decl : origin;
20558 tree class_origin = NULL, ultimate_origin;
20560 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20561 return NULL;
20563 switch (TREE_CODE (decl_or_origin))
20565 case ERROR_MARK:
20566 break;
20568 case CONST_DECL:
20569 if (!is_fortran () && !is_ada ())
20571 /* The individual enumerators of an enum type get output when we output
20572 the Dwarf representation of the relevant enum type itself. */
20573 break;
20576 /* Emit its type. */
20577 gen_type_die (TREE_TYPE (decl), context_die);
20579 /* And its containing namespace. */
20580 context_die = declare_in_namespace (decl, context_die);
20582 gen_const_die (decl, context_die);
20583 break;
20585 case FUNCTION_DECL:
20586 /* Don't output any DIEs to represent mere function declarations,
20587 unless they are class members or explicit block externs. */
20588 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20589 && DECL_FILE_SCOPE_P (decl_or_origin)
20590 && (current_function_decl == NULL_TREE
20591 || DECL_ARTIFICIAL (decl_or_origin)))
20592 break;
20594 #if 0
20595 /* FIXME */
20596 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20597 on local redeclarations of global functions. That seems broken. */
20598 if (current_function_decl != decl)
20599 /* This is only a declaration. */;
20600 #endif
20602 /* If we're emitting a clone, emit info for the abstract instance. */
20603 if (origin || DECL_ORIGIN (decl) != decl)
20604 dwarf2out_abstract_function (origin
20605 ? DECL_ORIGIN (origin)
20606 : DECL_ABSTRACT_ORIGIN (decl));
20608 /* If we're emitting an out-of-line copy of an inline function,
20609 emit info for the abstract instance and set up to refer to it. */
20610 else if (cgraph_function_possibly_inlined_p (decl)
20611 && ! DECL_ABSTRACT_P (decl)
20612 && ! class_or_namespace_scope_p (context_die)
20613 /* dwarf2out_abstract_function won't emit a die if this is just
20614 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20615 that case, because that works only if we have a die. */
20616 && DECL_INITIAL (decl) != NULL_TREE)
20618 dwarf2out_abstract_function (decl);
20619 set_decl_origin_self (decl);
20622 /* Otherwise we're emitting the primary DIE for this decl. */
20623 else if (debug_info_level > DINFO_LEVEL_TERSE)
20625 /* Before we describe the FUNCTION_DECL itself, make sure that we
20626 have its containing type. */
20627 if (!origin)
20628 origin = decl_class_context (decl);
20629 if (origin != NULL_TREE)
20630 gen_type_die (origin, context_die);
20632 /* And its return type. */
20633 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20635 /* And its virtual context. */
20636 if (DECL_VINDEX (decl) != NULL_TREE)
20637 gen_type_die (DECL_CONTEXT (decl), context_die);
20639 /* Make sure we have a member DIE for decl. */
20640 if (origin != NULL_TREE)
20641 gen_type_die_for_member (origin, decl, context_die);
20643 /* And its containing namespace. */
20644 context_die = declare_in_namespace (decl, context_die);
20647 /* Now output a DIE to represent the function itself. */
20648 if (decl)
20649 gen_subprogram_die (decl, context_die);
20650 break;
20652 case TYPE_DECL:
20653 /* If we are in terse mode, don't generate any DIEs to represent any
20654 actual typedefs. */
20655 if (debug_info_level <= DINFO_LEVEL_TERSE)
20656 break;
20658 /* In the special case of a TYPE_DECL node representing the declaration
20659 of some type tag, if the given TYPE_DECL is marked as having been
20660 instantiated from some other (original) TYPE_DECL node (e.g. one which
20661 was generated within the original definition of an inline function) we
20662 used to generate a special (abbreviated) DW_TAG_structure_type,
20663 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20664 should be actually referencing those DIEs, as variable DIEs with that
20665 type would be emitted already in the abstract origin, so it was always
20666 removed during unused type prunning. Don't add anything in this
20667 case. */
20668 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20669 break;
20671 if (is_redundant_typedef (decl))
20672 gen_type_die (TREE_TYPE (decl), context_die);
20673 else
20674 /* Output a DIE to represent the typedef itself. */
20675 gen_typedef_die (decl, context_die);
20676 break;
20678 case LABEL_DECL:
20679 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20680 gen_label_die (decl, context_die);
20681 break;
20683 case VAR_DECL:
20684 case RESULT_DECL:
20685 /* If we are in terse mode, don't generate any DIEs to represent any
20686 variable declarations or definitions. */
20687 if (debug_info_level <= DINFO_LEVEL_TERSE)
20688 break;
20690 /* Output any DIEs that are needed to specify the type of this data
20691 object. */
20692 if (decl_by_reference_p (decl_or_origin))
20693 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20694 else
20695 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20697 /* And its containing type. */
20698 class_origin = decl_class_context (decl_or_origin);
20699 if (class_origin != NULL_TREE)
20700 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20702 /* And its containing namespace. */
20703 context_die = declare_in_namespace (decl_or_origin, context_die);
20705 /* Now output the DIE to represent the data object itself. This gets
20706 complicated because of the possibility that the VAR_DECL really
20707 represents an inlined instance of a formal parameter for an inline
20708 function. */
20709 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20710 if (ultimate_origin != NULL_TREE
20711 && TREE_CODE (ultimate_origin) == PARM_DECL)
20712 gen_formal_parameter_die (decl, origin,
20713 true /* Emit name attribute. */,
20714 context_die);
20715 else
20716 gen_variable_die (decl, origin, context_die);
20717 break;
20719 case FIELD_DECL:
20720 /* Ignore the nameless fields that are used to skip bits but handle C++
20721 anonymous unions and structs. */
20722 if (DECL_NAME (decl) != NULL_TREE
20723 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20724 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20726 gen_type_die (member_declared_type (decl), context_die);
20727 gen_field_die (decl, context_die);
20729 break;
20731 case PARM_DECL:
20732 if (DECL_BY_REFERENCE (decl_or_origin))
20733 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20734 else
20735 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20736 return gen_formal_parameter_die (decl, origin,
20737 true /* Emit name attribute. */,
20738 context_die);
20740 case NAMESPACE_DECL:
20741 case IMPORTED_DECL:
20742 if (dwarf_version >= 3 || !dwarf_strict)
20743 gen_namespace_die (decl, context_die);
20744 break;
20746 case NAMELIST_DECL:
20747 gen_namelist_decl (DECL_NAME (decl), context_die,
20748 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20749 break;
20751 default:
20752 /* Probably some frontend-internal decl. Assume we don't care. */
20753 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20754 break;
20757 return NULL;
20760 /* Output debug information for global decl DECL. Called from toplev.c after
20761 compilation proper has finished. */
20763 static void
20764 dwarf2out_global_decl (tree decl)
20766 /* Output DWARF2 information for file-scope tentative data object
20767 declarations, file-scope (extern) function declarations (which
20768 had no corresponding body) and file-scope tagged type declarations
20769 and definitions which have not yet been forced out. */
20770 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20771 dwarf2out_decl (decl);
20774 /* Output debug information for type decl DECL. Called from toplev.c
20775 and from language front ends (to record built-in types). */
20776 static void
20777 dwarf2out_type_decl (tree decl, int local)
20779 if (!local)
20780 dwarf2out_decl (decl);
20783 /* Output debug information for imported module or decl DECL.
20784 NAME is non-NULL name in the lexical block if the decl has been renamed.
20785 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20786 that DECL belongs to.
20787 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20788 static void
20789 dwarf2out_imported_module_or_decl_1 (tree decl,
20790 tree name,
20791 tree lexical_block,
20792 dw_die_ref lexical_block_die)
20794 expanded_location xloc;
20795 dw_die_ref imported_die = NULL;
20796 dw_die_ref at_import_die;
20798 if (TREE_CODE (decl) == IMPORTED_DECL)
20800 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20801 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20802 gcc_assert (decl);
20804 else
20805 xloc = expand_location (input_location);
20807 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20809 at_import_die = force_type_die (TREE_TYPE (decl));
20810 /* For namespace N { typedef void T; } using N::T; base_type_die
20811 returns NULL, but DW_TAG_imported_declaration requires
20812 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20813 if (!at_import_die)
20815 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20816 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20817 at_import_die = lookup_type_die (TREE_TYPE (decl));
20818 gcc_assert (at_import_die);
20821 else
20823 at_import_die = lookup_decl_die (decl);
20824 if (!at_import_die)
20826 /* If we're trying to avoid duplicate debug info, we may not have
20827 emitted the member decl for this field. Emit it now. */
20828 if (TREE_CODE (decl) == FIELD_DECL)
20830 tree type = DECL_CONTEXT (decl);
20832 if (TYPE_CONTEXT (type)
20833 && TYPE_P (TYPE_CONTEXT (type))
20834 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20835 DINFO_USAGE_DIR_USE))
20836 return;
20837 gen_type_die_for_member (type, decl,
20838 get_context_die (TYPE_CONTEXT (type)));
20840 if (TREE_CODE (decl) == NAMELIST_DECL)
20841 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20842 get_context_die (DECL_CONTEXT (decl)),
20843 NULL_TREE);
20844 else
20845 at_import_die = force_decl_die (decl);
20849 if (TREE_CODE (decl) == NAMESPACE_DECL)
20851 if (dwarf_version >= 3 || !dwarf_strict)
20852 imported_die = new_die (DW_TAG_imported_module,
20853 lexical_block_die,
20854 lexical_block);
20855 else
20856 return;
20858 else
20859 imported_die = new_die (DW_TAG_imported_declaration,
20860 lexical_block_die,
20861 lexical_block);
20863 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20864 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20865 if (name)
20866 add_AT_string (imported_die, DW_AT_name,
20867 IDENTIFIER_POINTER (name));
20868 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20871 /* Output debug information for imported module or decl DECL.
20872 NAME is non-NULL name in context if the decl has been renamed.
20873 CHILD is true if decl is one of the renamed decls as part of
20874 importing whole module. */
20876 static void
20877 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20878 bool child)
20880 /* dw_die_ref at_import_die; */
20881 dw_die_ref scope_die;
20883 if (debug_info_level <= DINFO_LEVEL_TERSE)
20884 return;
20886 gcc_assert (decl);
20888 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20889 We need decl DIE for reference and scope die. First, get DIE for the decl
20890 itself. */
20892 /* Get the scope die for decl context. Use comp_unit_die for global module
20893 or decl. If die is not found for non globals, force new die. */
20894 if (context
20895 && TYPE_P (context)
20896 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20897 return;
20899 if (!(dwarf_version >= 3 || !dwarf_strict))
20900 return;
20902 scope_die = get_context_die (context);
20904 if (child)
20906 gcc_assert (scope_die->die_child);
20907 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20908 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20909 scope_die = scope_die->die_child;
20912 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20913 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20917 /* Output debug information for namelists. */
20919 static dw_die_ref
20920 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20922 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20923 tree value;
20924 unsigned i;
20926 if (debug_info_level <= DINFO_LEVEL_TERSE)
20927 return NULL;
20929 gcc_assert (scope_die != NULL);
20930 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20931 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20933 /* If there are no item_decls, we have a nondefining namelist, e.g.
20934 with USE association; hence, set DW_AT_declaration. */
20935 if (item_decls == NULL_TREE)
20937 add_AT_flag (nml_die, DW_AT_declaration, 1);
20938 return nml_die;
20941 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20943 nml_item_ref_die = lookup_decl_die (value);
20944 if (!nml_item_ref_die)
20945 nml_item_ref_die = force_decl_die (value);
20947 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20948 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20950 return nml_die;
20954 /* Write the debugging output for DECL. */
20956 static void
20957 dwarf2out_decl (tree decl)
20959 dw_die_ref context_die = comp_unit_die ();
20961 switch (TREE_CODE (decl))
20963 case ERROR_MARK:
20964 return;
20966 case FUNCTION_DECL:
20967 /* What we would really like to do here is to filter out all mere
20968 file-scope declarations of file-scope functions which are never
20969 referenced later within this translation unit (and keep all of ones
20970 that *are* referenced later on) but we aren't clairvoyant, so we have
20971 no idea which functions will be referenced in the future (i.e. later
20972 on within the current translation unit). So here we just ignore all
20973 file-scope function declarations which are not also definitions. If
20974 and when the debugger needs to know something about these functions,
20975 it will have to hunt around and find the DWARF information associated
20976 with the definition of the function.
20978 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20979 nodes represent definitions and which ones represent mere
20980 declarations. We have to check DECL_INITIAL instead. That's because
20981 the C front-end supports some weird semantics for "extern inline"
20982 function definitions. These can get inlined within the current
20983 translation unit (and thus, we need to generate Dwarf info for their
20984 abstract instances so that the Dwarf info for the concrete inlined
20985 instances can have something to refer to) but the compiler never
20986 generates any out-of-lines instances of such things (despite the fact
20987 that they *are* definitions).
20989 The important point is that the C front-end marks these "extern
20990 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20991 them anyway. Note that the C++ front-end also plays some similar games
20992 for inline function definitions appearing within include files which
20993 also contain `#pragma interface' pragmas.
20995 If we are called from dwarf2out_abstract_function output a DIE
20996 anyway. We can end up here this way with early inlining and LTO
20997 where the inlined function is output in a different LTRANS unit
20998 or not at all. */
20999 if (DECL_INITIAL (decl) == NULL_TREE
21000 && ! DECL_ABSTRACT_P (decl))
21001 return;
21003 /* If we're a nested function, initially use a parent of NULL; if we're
21004 a plain function, this will be fixed up in decls_for_scope. If
21005 we're a method, it will be ignored, since we already have a DIE. */
21006 if (decl_function_context (decl)
21007 /* But if we're in terse mode, we don't care about scope. */
21008 && debug_info_level > DINFO_LEVEL_TERSE)
21009 context_die = NULL;
21010 break;
21012 case VAR_DECL:
21013 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21014 declaration and if the declaration was never even referenced from
21015 within this entire compilation unit. We suppress these DIEs in
21016 order to save space in the .debug section (by eliminating entries
21017 which are probably useless). Note that we must not suppress
21018 block-local extern declarations (whether used or not) because that
21019 would screw-up the debugger's name lookup mechanism and cause it to
21020 miss things which really ought to be in scope at a given point. */
21021 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21022 return;
21024 /* For local statics lookup proper context die. */
21025 if (TREE_STATIC (decl)
21026 && DECL_CONTEXT (decl)
21027 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21028 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21030 /* If we are in terse mode, don't generate any DIEs to represent any
21031 variable declarations or definitions. */
21032 if (debug_info_level <= DINFO_LEVEL_TERSE)
21033 return;
21034 break;
21036 case CONST_DECL:
21037 if (debug_info_level <= DINFO_LEVEL_TERSE)
21038 return;
21039 if (!is_fortran () && !is_ada ())
21040 return;
21041 if (TREE_STATIC (decl) && decl_function_context (decl))
21042 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21043 break;
21045 case NAMESPACE_DECL:
21046 case IMPORTED_DECL:
21047 if (debug_info_level <= DINFO_LEVEL_TERSE)
21048 return;
21049 if (lookup_decl_die (decl) != NULL)
21050 return;
21051 break;
21053 case TYPE_DECL:
21054 /* Don't emit stubs for types unless they are needed by other DIEs. */
21055 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21056 return;
21058 /* Don't bother trying to generate any DIEs to represent any of the
21059 normal built-in types for the language we are compiling. */
21060 if (DECL_IS_BUILTIN (decl))
21061 return;
21063 /* If we are in terse mode, don't generate any DIEs for types. */
21064 if (debug_info_level <= DINFO_LEVEL_TERSE)
21065 return;
21067 /* If we're a function-scope tag, initially use a parent of NULL;
21068 this will be fixed up in decls_for_scope. */
21069 if (decl_function_context (decl))
21070 context_die = NULL;
21072 break;
21074 case NAMELIST_DECL:
21075 break;
21077 default:
21078 return;
21081 gen_decl_die (decl, NULL, context_die);
21084 /* Write the debugging output for DECL. */
21086 static void
21087 dwarf2out_function_decl (tree decl)
21089 dwarf2out_decl (decl);
21090 call_arg_locations = NULL;
21091 call_arg_loc_last = NULL;
21092 call_site_count = -1;
21093 tail_call_site_count = -1;
21094 block_map.release ();
21095 decl_loc_table->empty ();
21096 cached_dw_loc_list_table->empty ();
21099 /* Output a marker (i.e. a label) for the beginning of the generated code for
21100 a lexical block. */
21102 static void
21103 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21104 unsigned int blocknum)
21106 switch_to_section (current_function_section ());
21107 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21110 /* Output a marker (i.e. a label) for the end of the generated code for a
21111 lexical block. */
21113 static void
21114 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21116 switch_to_section (current_function_section ());
21117 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21120 /* Returns nonzero if it is appropriate not to emit any debugging
21121 information for BLOCK, because it doesn't contain any instructions.
21123 Don't allow this for blocks with nested functions or local classes
21124 as we would end up with orphans, and in the presence of scheduling
21125 we may end up calling them anyway. */
21127 static bool
21128 dwarf2out_ignore_block (const_tree block)
21130 tree decl;
21131 unsigned int i;
21133 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21134 if (TREE_CODE (decl) == FUNCTION_DECL
21135 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21136 return 0;
21137 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21139 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21140 if (TREE_CODE (decl) == FUNCTION_DECL
21141 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21142 return 0;
21145 return 1;
21148 /* Hash table routines for file_hash. */
21150 bool
21151 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21153 return filename_cmp (p1->filename, p2) == 0;
21156 hashval_t
21157 dwarf_file_hasher::hash (dwarf_file_data *p)
21159 return htab_hash_string (p->filename);
21162 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21163 dwarf2out.c) and return its "index". The index of each (known) filename is
21164 just a unique number which is associated with only that one filename. We
21165 need such numbers for the sake of generating labels (in the .debug_sfnames
21166 section) and references to those files numbers (in the .debug_srcinfo
21167 and.debug_macinfo sections). If the filename given as an argument is not
21168 found in our current list, add it to the list and assign it the next
21169 available unique index number. In order to speed up searches, we remember
21170 the index of the filename was looked up last. This handles the majority of
21171 all searches. */
21173 static struct dwarf_file_data *
21174 lookup_filename (const char *file_name)
21176 struct dwarf_file_data * created;
21178 /* Check to see if the file name that was searched on the previous
21179 call matches this file name. If so, return the index. */
21180 if (file_table_last_lookup
21181 && (file_name == file_table_last_lookup->filename
21182 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21183 return file_table_last_lookup;
21185 /* Didn't match the previous lookup, search the table. */
21186 dwarf_file_data **slot
21187 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21188 INSERT);
21189 if (*slot)
21190 return *slot;
21192 created = ggc_alloc<dwarf_file_data> ();
21193 created->filename = file_name;
21194 created->emitted_number = 0;
21195 *slot = created;
21196 return created;
21199 /* If the assembler will construct the file table, then translate the compiler
21200 internal file table number into the assembler file table number, and emit
21201 a .file directive if we haven't already emitted one yet. The file table
21202 numbers are different because we prune debug info for unused variables and
21203 types, which may include filenames. */
21205 static int
21206 maybe_emit_file (struct dwarf_file_data * fd)
21208 if (! fd->emitted_number)
21210 if (last_emitted_file)
21211 fd->emitted_number = last_emitted_file->emitted_number + 1;
21212 else
21213 fd->emitted_number = 1;
21214 last_emitted_file = fd;
21216 if (DWARF2_ASM_LINE_DEBUG_INFO)
21218 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21219 output_quoted_string (asm_out_file,
21220 remap_debug_filename (fd->filename));
21221 fputc ('\n', asm_out_file);
21225 return fd->emitted_number;
21228 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21229 That generation should happen after function debug info has been
21230 generated. The value of the attribute is the constant value of ARG. */
21232 static void
21233 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21235 die_arg_entry entry;
21237 if (!die || !arg)
21238 return;
21240 if (!tmpl_value_parm_die_table)
21241 vec_alloc (tmpl_value_parm_die_table, 32);
21243 entry.die = die;
21244 entry.arg = arg;
21245 vec_safe_push (tmpl_value_parm_die_table, entry);
21248 /* Return TRUE if T is an instance of generic type, FALSE
21249 otherwise. */
21251 static bool
21252 generic_type_p (tree t)
21254 if (t == NULL_TREE || !TYPE_P (t))
21255 return false;
21256 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21259 /* Schedule the generation of the generic parameter dies for the
21260 instance of generic type T. The proper generation itself is later
21261 done by gen_scheduled_generic_parms_dies. */
21263 static void
21264 schedule_generic_params_dies_gen (tree t)
21266 if (!generic_type_p (t))
21267 return;
21269 if (!generic_type_instances)
21270 vec_alloc (generic_type_instances, 256);
21272 vec_safe_push (generic_type_instances, t);
21275 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21276 by append_entry_to_tmpl_value_parm_die_table. This function must
21277 be called after function DIEs have been generated. */
21279 static void
21280 gen_remaining_tmpl_value_param_die_attribute (void)
21282 if (tmpl_value_parm_die_table)
21284 unsigned i;
21285 die_arg_entry *e;
21287 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21288 tree_add_const_value_attribute (e->die, e->arg);
21292 /* Generate generic parameters DIEs for instances of generic types
21293 that have been previously scheduled by
21294 schedule_generic_params_dies_gen. This function must be called
21295 after all the types of the CU have been laid out. */
21297 static void
21298 gen_scheduled_generic_parms_dies (void)
21300 unsigned i;
21301 tree t;
21303 if (!generic_type_instances)
21304 return;
21306 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21307 if (COMPLETE_TYPE_P (t))
21308 gen_generic_params_dies (t);
21312 /* Replace DW_AT_name for the decl with name. */
21314 static void
21315 dwarf2out_set_name (tree decl, tree name)
21317 dw_die_ref die;
21318 dw_attr_ref attr;
21319 const char *dname;
21321 die = TYPE_SYMTAB_DIE (decl);
21322 if (!die)
21323 return;
21325 dname = dwarf2_name (name, 0);
21326 if (!dname)
21327 return;
21329 attr = get_AT (die, DW_AT_name);
21330 if (attr)
21332 struct indirect_string_node *node;
21334 node = find_AT_string (dname);
21335 /* replace the string. */
21336 attr->dw_attr_val.v.val_str = node;
21339 else
21340 add_name_attribute (die, dname);
21343 /* True if before or during processing of the first function being emitted. */
21344 static bool in_first_function_p = true;
21345 /* True if loc_note during dwarf2out_var_location call might still be
21346 before first real instruction at address equal to .Ltext0. */
21347 static bool maybe_at_text_label_p = true;
21348 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21349 static unsigned int first_loclabel_num_not_at_text_label;
21351 /* Called by the final INSN scan whenever we see a var location. We
21352 use it to drop labels in the right places, and throw the location in
21353 our lookup table. */
21355 static void
21356 dwarf2out_var_location (rtx_insn *loc_note)
21358 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21359 struct var_loc_node *newloc;
21360 rtx_insn *next_real, *next_note;
21361 static const char *last_label;
21362 static const char *last_postcall_label;
21363 static bool last_in_cold_section_p;
21364 static rtx_insn *expected_next_loc_note;
21365 tree decl;
21366 bool var_loc_p;
21368 if (!NOTE_P (loc_note))
21370 if (CALL_P (loc_note))
21372 call_site_count++;
21373 if (SIBLING_CALL_P (loc_note))
21374 tail_call_site_count++;
21376 return;
21379 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21380 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21381 return;
21383 /* Optimize processing a large consecutive sequence of location
21384 notes so we don't spend too much time in next_real_insn. If the
21385 next insn is another location note, remember the next_real_insn
21386 calculation for next time. */
21387 next_real = cached_next_real_insn;
21388 if (next_real)
21390 if (expected_next_loc_note != loc_note)
21391 next_real = NULL;
21394 next_note = NEXT_INSN (loc_note);
21395 if (! next_note
21396 || next_note->deleted ()
21397 || ! NOTE_P (next_note)
21398 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21399 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21400 next_note = NULL;
21402 if (! next_real)
21403 next_real = next_real_insn (loc_note);
21405 if (next_note)
21407 expected_next_loc_note = next_note;
21408 cached_next_real_insn = next_real;
21410 else
21411 cached_next_real_insn = NULL;
21413 /* If there are no instructions which would be affected by this note,
21414 don't do anything. */
21415 if (var_loc_p
21416 && next_real == NULL_RTX
21417 && !NOTE_DURING_CALL_P (loc_note))
21418 return;
21420 if (next_real == NULL_RTX)
21421 next_real = get_last_insn ();
21423 /* If there were any real insns between note we processed last time
21424 and this note (or if it is the first note), clear
21425 last_{,postcall_}label so that they are not reused this time. */
21426 if (last_var_location_insn == NULL_RTX
21427 || last_var_location_insn != next_real
21428 || last_in_cold_section_p != in_cold_section_p)
21430 last_label = NULL;
21431 last_postcall_label = NULL;
21434 if (var_loc_p)
21436 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21437 newloc = add_var_loc_to_decl (decl, loc_note,
21438 NOTE_DURING_CALL_P (loc_note)
21439 ? last_postcall_label : last_label);
21440 if (newloc == NULL)
21441 return;
21443 else
21445 decl = NULL_TREE;
21446 newloc = NULL;
21449 /* If there were no real insns between note we processed last time
21450 and this note, use the label we emitted last time. Otherwise
21451 create a new label and emit it. */
21452 if (last_label == NULL)
21454 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21455 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21456 loclabel_num++;
21457 last_label = ggc_strdup (loclabel);
21458 /* See if loclabel might be equal to .Ltext0. If yes,
21459 bump first_loclabel_num_not_at_text_label. */
21460 if (!have_multiple_function_sections
21461 && in_first_function_p
21462 && maybe_at_text_label_p)
21464 static rtx_insn *last_start;
21465 rtx_insn *insn;
21466 for (insn = loc_note; insn; insn = previous_insn (insn))
21467 if (insn == last_start)
21468 break;
21469 else if (!NONDEBUG_INSN_P (insn))
21470 continue;
21471 else
21473 rtx body = PATTERN (insn);
21474 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21475 continue;
21476 /* Inline asm could occupy zero bytes. */
21477 else if (GET_CODE (body) == ASM_INPUT
21478 || asm_noperands (body) >= 0)
21479 continue;
21480 #ifdef HAVE_attr_length
21481 else if (get_attr_min_length (insn) == 0)
21482 continue;
21483 #endif
21484 else
21486 /* Assume insn has non-zero length. */
21487 maybe_at_text_label_p = false;
21488 break;
21491 if (maybe_at_text_label_p)
21493 last_start = loc_note;
21494 first_loclabel_num_not_at_text_label = loclabel_num;
21499 if (!var_loc_p)
21501 struct call_arg_loc_node *ca_loc
21502 = ggc_cleared_alloc<call_arg_loc_node> ();
21503 rtx_insn *prev = prev_real_insn (loc_note);
21504 rtx x;
21505 ca_loc->call_arg_loc_note = loc_note;
21506 ca_loc->next = NULL;
21507 ca_loc->label = last_label;
21508 gcc_assert (prev
21509 && (CALL_P (prev)
21510 || (NONJUMP_INSN_P (prev)
21511 && GET_CODE (PATTERN (prev)) == SEQUENCE
21512 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21513 if (!CALL_P (prev))
21514 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21515 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21516 x = get_call_rtx_from (PATTERN (prev));
21517 if (x)
21519 x = XEXP (XEXP (x, 0), 0);
21520 if (GET_CODE (x) == SYMBOL_REF
21521 && SYMBOL_REF_DECL (x)
21522 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21523 ca_loc->symbol_ref = x;
21525 ca_loc->block = insn_scope (prev);
21526 if (call_arg_locations)
21527 call_arg_loc_last->next = ca_loc;
21528 else
21529 call_arg_locations = ca_loc;
21530 call_arg_loc_last = ca_loc;
21532 else if (!NOTE_DURING_CALL_P (loc_note))
21533 newloc->label = last_label;
21534 else
21536 if (!last_postcall_label)
21538 sprintf (loclabel, "%s-1", last_label);
21539 last_postcall_label = ggc_strdup (loclabel);
21541 newloc->label = last_postcall_label;
21544 last_var_location_insn = next_real;
21545 last_in_cold_section_p = in_cold_section_p;
21548 /* Note in one location list that text section has changed. */
21551 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21553 var_loc_list *list = *slot;
21554 if (list->first)
21555 list->last_before_switch
21556 = list->last->next ? list->last->next : list->last;
21557 return 1;
21560 /* Note in all location lists that text section has changed. */
21562 static void
21563 var_location_switch_text_section (void)
21565 if (decl_loc_table == NULL)
21566 return;
21568 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21571 /* Create a new line number table. */
21573 static dw_line_info_table *
21574 new_line_info_table (void)
21576 dw_line_info_table *table;
21578 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21579 table->file_num = 1;
21580 table->line_num = 1;
21581 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21583 return table;
21586 /* Lookup the "current" table into which we emit line info, so
21587 that we don't have to do it for every source line. */
21589 static void
21590 set_cur_line_info_table (section *sec)
21592 dw_line_info_table *table;
21594 if (sec == text_section)
21595 table = text_section_line_info;
21596 else if (sec == cold_text_section)
21598 table = cold_text_section_line_info;
21599 if (!table)
21601 cold_text_section_line_info = table = new_line_info_table ();
21602 table->end_label = cold_end_label;
21605 else
21607 const char *end_label;
21609 if (flag_reorder_blocks_and_partition)
21611 if (in_cold_section_p)
21612 end_label = crtl->subsections.cold_section_end_label;
21613 else
21614 end_label = crtl->subsections.hot_section_end_label;
21616 else
21618 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21619 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21620 current_function_funcdef_no);
21621 end_label = ggc_strdup (label);
21624 table = new_line_info_table ();
21625 table->end_label = end_label;
21627 vec_safe_push (separate_line_info, table);
21630 if (DWARF2_ASM_LINE_DEBUG_INFO)
21631 table->is_stmt = (cur_line_info_table
21632 ? cur_line_info_table->is_stmt
21633 : DWARF_LINE_DEFAULT_IS_STMT_START);
21634 cur_line_info_table = table;
21638 /* We need to reset the locations at the beginning of each
21639 function. We can't do this in the end_function hook, because the
21640 declarations that use the locations won't have been output when
21641 that hook is called. Also compute have_multiple_function_sections here. */
21643 static void
21644 dwarf2out_begin_function (tree fun)
21646 section *sec = function_section (fun);
21648 if (sec != text_section)
21649 have_multiple_function_sections = true;
21651 if (flag_reorder_blocks_and_partition && !cold_text_section)
21653 gcc_assert (current_function_decl == fun);
21654 cold_text_section = unlikely_text_section ();
21655 switch_to_section (cold_text_section);
21656 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21657 switch_to_section (sec);
21660 dwarf2out_note_section_used ();
21661 call_site_count = 0;
21662 tail_call_site_count = 0;
21664 set_cur_line_info_table (sec);
21667 /* Helper function of dwarf2out_end_function, called only after emitting
21668 the very first function into assembly. Check if some .debug_loc range
21669 might end with a .LVL* label that could be equal to .Ltext0.
21670 In that case we must force using absolute addresses in .debug_loc ranges,
21671 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21672 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21673 list terminator.
21674 Set have_multiple_function_sections to true in that case and
21675 terminate htab traversal. */
21678 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21680 var_loc_list *entry = *slot;
21681 struct var_loc_node *node;
21683 node = entry->first;
21684 if (node && node->next && node->next->label)
21686 unsigned int i;
21687 const char *label = node->next->label;
21688 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21690 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21692 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21693 if (strcmp (label, loclabel) == 0)
21695 have_multiple_function_sections = true;
21696 return 0;
21700 return 1;
21703 /* Hook called after emitting a function into assembly.
21704 This does something only for the very first function emitted. */
21706 static void
21707 dwarf2out_end_function (unsigned int)
21709 if (in_first_function_p
21710 && !have_multiple_function_sections
21711 && first_loclabel_num_not_at_text_label
21712 && decl_loc_table)
21713 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
21714 in_first_function_p = false;
21715 maybe_at_text_label_p = false;
21718 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21720 static void
21721 push_dw_line_info_entry (dw_line_info_table *table,
21722 enum dw_line_info_opcode opcode, unsigned int val)
21724 dw_line_info_entry e;
21725 e.opcode = opcode;
21726 e.val = val;
21727 vec_safe_push (table->entries, e);
21730 /* Output a label to mark the beginning of a source code line entry
21731 and record information relating to this source line, in
21732 'line_info_table' for later output of the .debug_line section. */
21733 /* ??? The discriminator parameter ought to be unsigned. */
21735 static void
21736 dwarf2out_source_line (unsigned int line, const char *filename,
21737 int discriminator, bool is_stmt)
21739 unsigned int file_num;
21740 dw_line_info_table *table;
21742 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21743 return;
21745 /* The discriminator column was added in dwarf4. Simplify the below
21746 by simply removing it if we're not supposed to output it. */
21747 if (dwarf_version < 4 && dwarf_strict)
21748 discriminator = 0;
21750 table = cur_line_info_table;
21751 file_num = maybe_emit_file (lookup_filename (filename));
21753 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21754 the debugger has used the second (possibly duplicate) line number
21755 at the beginning of the function to mark the end of the prologue.
21756 We could eliminate any other duplicates within the function. For
21757 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21758 that second line number entry. */
21759 /* Recall that this end-of-prologue indication is *not* the same thing
21760 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21761 to which the hook corresponds, follows the last insn that was
21762 emitted by gen_prologue. What we need is to precede the first insn
21763 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21764 insn that corresponds to something the user wrote. These may be
21765 very different locations once scheduling is enabled. */
21767 if (0 && file_num == table->file_num
21768 && line == table->line_num
21769 && discriminator == table->discrim_num
21770 && is_stmt == table->is_stmt)
21771 return;
21773 switch_to_section (current_function_section ());
21775 /* If requested, emit something human-readable. */
21776 if (flag_debug_asm)
21777 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21779 if (DWARF2_ASM_LINE_DEBUG_INFO)
21781 /* Emit the .loc directive understood by GNU as. */
21782 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21783 file_num, line, is_stmt, discriminator */
21784 fputs ("\t.loc ", asm_out_file);
21785 fprint_ul (asm_out_file, file_num);
21786 putc (' ', asm_out_file);
21787 fprint_ul (asm_out_file, line);
21788 putc (' ', asm_out_file);
21789 putc ('0', asm_out_file);
21791 if (is_stmt != table->is_stmt)
21793 fputs (" is_stmt ", asm_out_file);
21794 putc (is_stmt ? '1' : '0', asm_out_file);
21796 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21798 gcc_assert (discriminator > 0);
21799 fputs (" discriminator ", asm_out_file);
21800 fprint_ul (asm_out_file, (unsigned long) discriminator);
21802 putc ('\n', asm_out_file);
21804 else
21806 unsigned int label_num = ++line_info_label_num;
21808 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21810 push_dw_line_info_entry (table, LI_set_address, label_num);
21811 if (file_num != table->file_num)
21812 push_dw_line_info_entry (table, LI_set_file, file_num);
21813 if (discriminator != table->discrim_num)
21814 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21815 if (is_stmt != table->is_stmt)
21816 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21817 push_dw_line_info_entry (table, LI_set_line, line);
21820 table->file_num = file_num;
21821 table->line_num = line;
21822 table->discrim_num = discriminator;
21823 table->is_stmt = is_stmt;
21824 table->in_use = true;
21827 /* Record the beginning of a new source file. */
21829 static void
21830 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21832 if (flag_eliminate_dwarf2_dups)
21834 /* Record the beginning of the file for break_out_includes. */
21835 dw_die_ref bincl_die;
21837 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21838 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21841 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21843 macinfo_entry e;
21844 e.code = DW_MACINFO_start_file;
21845 e.lineno = lineno;
21846 e.info = ggc_strdup (filename);
21847 vec_safe_push (macinfo_table, e);
21851 /* Record the end of a source file. */
21853 static void
21854 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21856 if (flag_eliminate_dwarf2_dups)
21857 /* Record the end of the file for break_out_includes. */
21858 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21860 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21862 macinfo_entry e;
21863 e.code = DW_MACINFO_end_file;
21864 e.lineno = lineno;
21865 e.info = NULL;
21866 vec_safe_push (macinfo_table, e);
21870 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21871 the tail part of the directive line, i.e. the part which is past the
21872 initial whitespace, #, whitespace, directive-name, whitespace part. */
21874 static void
21875 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21876 const char *buffer ATTRIBUTE_UNUSED)
21878 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21880 macinfo_entry e;
21881 /* Insert a dummy first entry to be able to optimize the whole
21882 predefined macro block using DW_MACRO_GNU_transparent_include. */
21883 if (macinfo_table->is_empty () && lineno <= 1)
21885 e.code = 0;
21886 e.lineno = 0;
21887 e.info = NULL;
21888 vec_safe_push (macinfo_table, e);
21890 e.code = DW_MACINFO_define;
21891 e.lineno = lineno;
21892 e.info = ggc_strdup (buffer);
21893 vec_safe_push (macinfo_table, e);
21897 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21898 the tail part of the directive line, i.e. the part which is past the
21899 initial whitespace, #, whitespace, directive-name, whitespace part. */
21901 static void
21902 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21903 const char *buffer ATTRIBUTE_UNUSED)
21905 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21907 macinfo_entry e;
21908 /* Insert a dummy first entry to be able to optimize the whole
21909 predefined macro block using DW_MACRO_GNU_transparent_include. */
21910 if (macinfo_table->is_empty () && lineno <= 1)
21912 e.code = 0;
21913 e.lineno = 0;
21914 e.info = NULL;
21915 vec_safe_push (macinfo_table, e);
21917 e.code = DW_MACINFO_undef;
21918 e.lineno = lineno;
21919 e.info = ggc_strdup (buffer);
21920 vec_safe_push (macinfo_table, e);
21924 /* Helpers to manipulate hash table of CUs. */
21926 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21928 typedef macinfo_entry value_type;
21929 typedef macinfo_entry compare_type;
21930 static inline hashval_t hash (const value_type *);
21931 static inline bool equal (const value_type *, const compare_type *);
21934 inline hashval_t
21935 macinfo_entry_hasher::hash (const value_type *entry)
21937 return htab_hash_string (entry->info);
21940 inline bool
21941 macinfo_entry_hasher::equal (const value_type *entry1,
21942 const compare_type *entry2)
21944 return !strcmp (entry1->info, entry2->info);
21947 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
21949 /* Output a single .debug_macinfo entry. */
21951 static void
21952 output_macinfo_op (macinfo_entry *ref)
21954 int file_num;
21955 size_t len;
21956 struct indirect_string_node *node;
21957 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21958 struct dwarf_file_data *fd;
21960 switch (ref->code)
21962 case DW_MACINFO_start_file:
21963 fd = lookup_filename (ref->info);
21964 file_num = maybe_emit_file (fd);
21965 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21966 dw2_asm_output_data_uleb128 (ref->lineno,
21967 "Included from line number %lu",
21968 (unsigned long) ref->lineno);
21969 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21970 break;
21971 case DW_MACINFO_end_file:
21972 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21973 break;
21974 case DW_MACINFO_define:
21975 case DW_MACINFO_undef:
21976 len = strlen (ref->info) + 1;
21977 if (!dwarf_strict
21978 && len > DWARF_OFFSET_SIZE
21979 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21980 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21982 ref->code = ref->code == DW_MACINFO_define
21983 ? DW_MACRO_GNU_define_indirect
21984 : DW_MACRO_GNU_undef_indirect;
21985 output_macinfo_op (ref);
21986 return;
21988 dw2_asm_output_data (1, ref->code,
21989 ref->code == DW_MACINFO_define
21990 ? "Define macro" : "Undefine macro");
21991 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21992 (unsigned long) ref->lineno);
21993 dw2_asm_output_nstring (ref->info, -1, "The macro");
21994 break;
21995 case DW_MACRO_GNU_define_indirect:
21996 case DW_MACRO_GNU_undef_indirect:
21997 node = find_AT_string (ref->info);
21998 gcc_assert (node
21999 && ((node->form == DW_FORM_strp)
22000 || (node->form == DW_FORM_GNU_str_index)));
22001 dw2_asm_output_data (1, ref->code,
22002 ref->code == DW_MACRO_GNU_define_indirect
22003 ? "Define macro indirect"
22004 : "Undefine macro indirect");
22005 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22006 (unsigned long) ref->lineno);
22007 if (node->form == DW_FORM_strp)
22008 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22009 debug_str_section, "The macro: \"%s\"",
22010 ref->info);
22011 else
22012 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22013 ref->info);
22014 break;
22015 case DW_MACRO_GNU_transparent_include:
22016 dw2_asm_output_data (1, ref->code, "Transparent include");
22017 ASM_GENERATE_INTERNAL_LABEL (label,
22018 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22019 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22020 break;
22021 default:
22022 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22023 ASM_COMMENT_START, (unsigned long) ref->code);
22024 break;
22028 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22029 other compilation unit .debug_macinfo sections. IDX is the first
22030 index of a define/undef, return the number of ops that should be
22031 emitted in a comdat .debug_macinfo section and emit
22032 a DW_MACRO_GNU_transparent_include entry referencing it.
22033 If the define/undef entry should be emitted normally, return 0. */
22035 static unsigned
22036 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22037 macinfo_hash_type **macinfo_htab)
22039 macinfo_entry *first, *second, *cur, *inc;
22040 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22041 unsigned char checksum[16];
22042 struct md5_ctx ctx;
22043 char *grp_name, *tail;
22044 const char *base;
22045 unsigned int i, count, encoded_filename_len, linebuf_len;
22046 macinfo_entry **slot;
22048 first = &(*macinfo_table)[idx];
22049 second = &(*macinfo_table)[idx + 1];
22051 /* Optimize only if there are at least two consecutive define/undef ops,
22052 and either all of them are before first DW_MACINFO_start_file
22053 with lineno {0,1} (i.e. predefined macro block), or all of them are
22054 in some included header file. */
22055 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22056 return 0;
22057 if (vec_safe_is_empty (files))
22059 if (first->lineno > 1 || second->lineno > 1)
22060 return 0;
22062 else if (first->lineno == 0)
22063 return 0;
22065 /* Find the last define/undef entry that can be grouped together
22066 with first and at the same time compute md5 checksum of their
22067 codes, linenumbers and strings. */
22068 md5_init_ctx (&ctx);
22069 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22070 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22071 break;
22072 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22073 break;
22074 else
22076 unsigned char code = cur->code;
22077 md5_process_bytes (&code, 1, &ctx);
22078 checksum_uleb128 (cur->lineno, &ctx);
22079 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22081 md5_finish_ctx (&ctx, checksum);
22082 count = i - idx;
22084 /* From the containing include filename (if any) pick up just
22085 usable characters from its basename. */
22086 if (vec_safe_is_empty (files))
22087 base = "";
22088 else
22089 base = lbasename (files->last ().info);
22090 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22091 if (ISIDNUM (base[i]) || base[i] == '.')
22092 encoded_filename_len++;
22093 /* Count . at the end. */
22094 if (encoded_filename_len)
22095 encoded_filename_len++;
22097 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22098 linebuf_len = strlen (linebuf);
22100 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22101 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22102 + 16 * 2 + 1);
22103 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22104 tail = grp_name + 4;
22105 if (encoded_filename_len)
22107 for (i = 0; base[i]; i++)
22108 if (ISIDNUM (base[i]) || base[i] == '.')
22109 *tail++ = base[i];
22110 *tail++ = '.';
22112 memcpy (tail, linebuf, linebuf_len);
22113 tail += linebuf_len;
22114 *tail++ = '.';
22115 for (i = 0; i < 16; i++)
22116 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22118 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22119 in the empty vector entry before the first define/undef. */
22120 inc = &(*macinfo_table)[idx - 1];
22121 inc->code = DW_MACRO_GNU_transparent_include;
22122 inc->lineno = 0;
22123 inc->info = ggc_strdup (grp_name);
22124 if (!*macinfo_htab)
22125 *macinfo_htab = new macinfo_hash_type (10);
22126 /* Avoid emitting duplicates. */
22127 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22128 if (*slot != NULL)
22130 inc->code = 0;
22131 inc->info = NULL;
22132 /* If such an entry has been used before, just emit
22133 a DW_MACRO_GNU_transparent_include op. */
22134 inc = *slot;
22135 output_macinfo_op (inc);
22136 /* And clear all macinfo_entry in the range to avoid emitting them
22137 in the second pass. */
22138 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22140 cur->code = 0;
22141 cur->info = NULL;
22144 else
22146 *slot = inc;
22147 inc->lineno = (*macinfo_htab)->elements ();
22148 output_macinfo_op (inc);
22150 return count;
22153 /* Save any strings needed by the macinfo table in the debug str
22154 table. All strings must be collected into the table by the time
22155 index_string is called. */
22157 static void
22158 save_macinfo_strings (void)
22160 unsigned len;
22161 unsigned i;
22162 macinfo_entry *ref;
22164 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22166 switch (ref->code)
22168 /* Match the logic in output_macinfo_op to decide on
22169 indirect strings. */
22170 case DW_MACINFO_define:
22171 case DW_MACINFO_undef:
22172 len = strlen (ref->info) + 1;
22173 if (!dwarf_strict
22174 && len > DWARF_OFFSET_SIZE
22175 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22176 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22177 set_indirect_string (find_AT_string (ref->info));
22178 break;
22179 case DW_MACRO_GNU_define_indirect:
22180 case DW_MACRO_GNU_undef_indirect:
22181 set_indirect_string (find_AT_string (ref->info));
22182 break;
22183 default:
22184 break;
22189 /* Output macinfo section(s). */
22191 static void
22192 output_macinfo (void)
22194 unsigned i;
22195 unsigned long length = vec_safe_length (macinfo_table);
22196 macinfo_entry *ref;
22197 vec<macinfo_entry, va_gc> *files = NULL;
22198 macinfo_hash_type *macinfo_htab = NULL;
22200 if (! length)
22201 return;
22203 /* output_macinfo* uses these interchangeably. */
22204 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22205 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22206 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22207 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22209 /* For .debug_macro emit the section header. */
22210 if (!dwarf_strict)
22212 dw2_asm_output_data (2, 4, "DWARF macro version number");
22213 if (DWARF_OFFSET_SIZE == 8)
22214 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22215 else
22216 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22217 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22218 (!dwarf_split_debug_info ? debug_line_section_label
22219 : debug_skeleton_line_section_label),
22220 debug_line_section, NULL);
22223 /* In the first loop, it emits the primary .debug_macinfo section
22224 and after each emitted op the macinfo_entry is cleared.
22225 If a longer range of define/undef ops can be optimized using
22226 DW_MACRO_GNU_transparent_include, the
22227 DW_MACRO_GNU_transparent_include op is emitted and kept in
22228 the vector before the first define/undef in the range and the
22229 whole range of define/undef ops is not emitted and kept. */
22230 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22232 switch (ref->code)
22234 case DW_MACINFO_start_file:
22235 vec_safe_push (files, *ref);
22236 break;
22237 case DW_MACINFO_end_file:
22238 if (!vec_safe_is_empty (files))
22239 files->pop ();
22240 break;
22241 case DW_MACINFO_define:
22242 case DW_MACINFO_undef:
22243 if (!dwarf_strict
22244 && HAVE_COMDAT_GROUP
22245 && vec_safe_length (files) != 1
22246 && i > 0
22247 && i + 1 < length
22248 && (*macinfo_table)[i - 1].code == 0)
22250 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22251 if (count)
22253 i += count - 1;
22254 continue;
22257 break;
22258 case 0:
22259 /* A dummy entry may be inserted at the beginning to be able
22260 to optimize the whole block of predefined macros. */
22261 if (i == 0)
22262 continue;
22263 default:
22264 break;
22266 output_macinfo_op (ref);
22267 ref->info = NULL;
22268 ref->code = 0;
22271 if (!macinfo_htab)
22272 return;
22274 delete macinfo_htab;
22275 macinfo_htab = NULL;
22277 /* If any DW_MACRO_GNU_transparent_include were used, on those
22278 DW_MACRO_GNU_transparent_include entries terminate the
22279 current chain and switch to a new comdat .debug_macinfo
22280 section and emit the define/undef entries within it. */
22281 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22282 switch (ref->code)
22284 case 0:
22285 continue;
22286 case DW_MACRO_GNU_transparent_include:
22288 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22289 tree comdat_key = get_identifier (ref->info);
22290 /* Terminate the previous .debug_macinfo section. */
22291 dw2_asm_output_data (1, 0, "End compilation unit");
22292 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22293 SECTION_DEBUG
22294 | SECTION_LINKONCE,
22295 comdat_key);
22296 ASM_GENERATE_INTERNAL_LABEL (label,
22297 DEBUG_MACRO_SECTION_LABEL,
22298 ref->lineno);
22299 ASM_OUTPUT_LABEL (asm_out_file, label);
22300 ref->code = 0;
22301 ref->info = NULL;
22302 dw2_asm_output_data (2, 4, "DWARF macro version number");
22303 if (DWARF_OFFSET_SIZE == 8)
22304 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22305 else
22306 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22308 break;
22309 case DW_MACINFO_define:
22310 case DW_MACINFO_undef:
22311 output_macinfo_op (ref);
22312 ref->code = 0;
22313 ref->info = NULL;
22314 break;
22315 default:
22316 gcc_unreachable ();
22320 /* Set up for Dwarf output at the start of compilation. */
22322 static void
22323 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22325 /* Allocate the file_table. */
22326 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22328 /* Allocate the decl_die_table. */
22329 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22331 /* Allocate the decl_loc_table. */
22332 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22334 /* Allocate the cached_dw_loc_list_table. */
22335 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22337 /* Allocate the initial hunk of the decl_scope_table. */
22338 vec_alloc (decl_scope_table, 256);
22340 /* Allocate the initial hunk of the abbrev_die_table. */
22341 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22342 (ABBREV_DIE_TABLE_INCREMENT);
22343 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22344 /* Zero-th entry is allocated, but unused. */
22345 abbrev_die_table_in_use = 1;
22347 /* Allocate the pubtypes and pubnames vectors. */
22348 vec_alloc (pubname_table, 32);
22349 vec_alloc (pubtype_table, 32);
22351 vec_alloc (incomplete_types, 64);
22353 vec_alloc (used_rtx_array, 32);
22355 if (!dwarf_split_debug_info)
22357 debug_info_section = get_section (DEBUG_INFO_SECTION,
22358 SECTION_DEBUG, NULL);
22359 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22360 SECTION_DEBUG, NULL);
22361 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22362 SECTION_DEBUG, NULL);
22364 else
22366 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22367 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22368 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22369 SECTION_DEBUG | SECTION_EXCLUDE,
22370 NULL);
22371 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22372 SECTION_DEBUG, NULL);
22373 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22374 SECTION_DEBUG, NULL);
22375 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22376 SECTION_DEBUG, NULL);
22377 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22378 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22380 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22381 the main .o, but the skeleton_line goes into the split off dwo. */
22382 debug_skeleton_line_section
22383 = get_section (DEBUG_DWO_LINE_SECTION,
22384 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22385 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22386 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22387 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22388 SECTION_DEBUG | SECTION_EXCLUDE,
22389 NULL);
22390 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22391 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22392 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22393 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22394 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22395 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22397 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22398 SECTION_DEBUG, NULL);
22399 debug_macinfo_section = get_section (dwarf_strict
22400 ? DEBUG_MACINFO_SECTION
22401 : DEBUG_MACRO_SECTION,
22402 DEBUG_MACRO_SECTION_FLAGS, NULL);
22403 debug_line_section = get_section (DEBUG_LINE_SECTION,
22404 SECTION_DEBUG, NULL);
22405 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22406 SECTION_DEBUG, NULL);
22407 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22408 SECTION_DEBUG, NULL);
22409 debug_str_section = get_section (DEBUG_STR_SECTION,
22410 DEBUG_STR_SECTION_FLAGS, NULL);
22411 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22412 SECTION_DEBUG, NULL);
22413 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22414 SECTION_DEBUG, NULL);
22416 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22417 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22418 DEBUG_ABBREV_SECTION_LABEL, 0);
22419 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22420 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22421 COLD_TEXT_SECTION_LABEL, 0);
22422 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22424 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22425 DEBUG_INFO_SECTION_LABEL, 0);
22426 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22427 DEBUG_LINE_SECTION_LABEL, 0);
22428 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22429 DEBUG_RANGES_SECTION_LABEL, 0);
22430 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22431 DEBUG_ADDR_SECTION_LABEL, 0);
22432 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22433 dwarf_strict
22434 ? DEBUG_MACINFO_SECTION_LABEL
22435 : DEBUG_MACRO_SECTION_LABEL, 0);
22436 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22438 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22439 vec_alloc (macinfo_table, 64);
22441 switch_to_section (text_section);
22442 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22444 /* Make sure the line number table for .text always exists. */
22445 text_section_line_info = new_line_info_table ();
22446 text_section_line_info->end_label = text_end_label;
22449 /* Called before compile () starts outputtting functions, variables
22450 and toplevel asms into assembly. */
22452 static void
22453 dwarf2out_assembly_start (void)
22455 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22456 && dwarf2out_do_cfi_asm ()
22457 && (!(flag_unwind_tables || flag_exceptions)
22458 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22459 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22462 /* A helper function for dwarf2out_finish called through
22463 htab_traverse. Assign a string its index. All strings must be
22464 collected into the table by the time index_string is called,
22465 because the indexing code relies on htab_traverse to traverse nodes
22466 in the same order for each run. */
22469 index_string (indirect_string_node **h, unsigned int *index)
22471 indirect_string_node *node = *h;
22473 find_string_form (node);
22474 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22476 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22477 node->index = *index;
22478 *index += 1;
22480 return 1;
22483 /* A helper function for output_indirect_strings called through
22484 htab_traverse. Output the offset to a string and update the
22485 current offset. */
22488 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22490 indirect_string_node *node = *h;
22492 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22494 /* Assert that this node has been assigned an index. */
22495 gcc_assert (node->index != NO_INDEX_ASSIGNED
22496 && node->index != NOT_INDEXED);
22497 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22498 "indexed string 0x%x: %s", node->index, node->str);
22499 *offset += strlen (node->str) + 1;
22501 return 1;
22504 /* A helper function for dwarf2out_finish called through
22505 htab_traverse. Output the indexed string. */
22508 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22510 struct indirect_string_node *node = *h;
22512 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22514 /* Assert that the strings are output in the same order as their
22515 indexes were assigned. */
22516 gcc_assert (*cur_idx == node->index);
22517 assemble_string (node->str, strlen (node->str) + 1);
22518 *cur_idx += 1;
22520 return 1;
22523 /* A helper function for dwarf2out_finish called through
22524 htab_traverse. Emit one queued .debug_str string. */
22527 output_indirect_string (indirect_string_node **h, void *)
22529 struct indirect_string_node *node = *h;
22531 node->form = find_string_form (node);
22532 if (node->form == DW_FORM_strp && node->refcount > 0)
22534 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22535 assemble_string (node->str, strlen (node->str) + 1);
22538 return 1;
22541 /* Output the indexed string table. */
22543 static void
22544 output_indirect_strings (void)
22546 switch_to_section (debug_str_section);
22547 if (!dwarf_split_debug_info)
22548 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22549 else
22551 unsigned int offset = 0;
22552 unsigned int cur_idx = 0;
22554 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22556 switch_to_section (debug_str_offsets_section);
22557 debug_str_hash->traverse_noresize
22558 <unsigned int *, output_index_string_offset> (&offset);
22559 switch_to_section (debug_str_dwo_section);
22560 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22561 (&cur_idx);
22565 /* Callback for htab_traverse to assign an index to an entry in the
22566 table, and to write that entry to the .debug_addr section. */
22569 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22571 addr_table_entry *entry = *slot;
22573 if (entry->refcount == 0)
22575 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22576 || entry->index == NOT_INDEXED);
22577 return 1;
22580 gcc_assert (entry->index == *cur_index);
22581 (*cur_index)++;
22583 switch (entry->kind)
22585 case ate_kind_rtx:
22586 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22587 "0x%x", entry->index);
22588 break;
22589 case ate_kind_rtx_dtprel:
22590 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22591 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22592 DWARF2_ADDR_SIZE,
22593 entry->addr.rtl);
22594 fputc ('\n', asm_out_file);
22595 break;
22596 case ate_kind_label:
22597 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22598 "0x%x", entry->index);
22599 break;
22600 default:
22601 gcc_unreachable ();
22603 return 1;
22606 /* Produce the .debug_addr section. */
22608 static void
22609 output_addr_table (void)
22611 unsigned int index = 0;
22612 if (addr_index_table == NULL || addr_index_table->size () == 0)
22613 return;
22615 switch_to_section (debug_addr_section);
22616 addr_index_table
22617 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22620 #if ENABLE_ASSERT_CHECKING
22621 /* Verify that all marks are clear. */
22623 static void
22624 verify_marks_clear (dw_die_ref die)
22626 dw_die_ref c;
22628 gcc_assert (! die->die_mark);
22629 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22631 #endif /* ENABLE_ASSERT_CHECKING */
22633 /* Clear the marks for a die and its children.
22634 Be cool if the mark isn't set. */
22636 static void
22637 prune_unmark_dies (dw_die_ref die)
22639 dw_die_ref c;
22641 if (die->die_mark)
22642 die->die_mark = 0;
22643 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22646 /* Given DIE that we're marking as used, find any other dies
22647 it references as attributes and mark them as used. */
22649 static void
22650 prune_unused_types_walk_attribs (dw_die_ref die)
22652 dw_attr_ref a;
22653 unsigned ix;
22655 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22657 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22659 /* A reference to another DIE.
22660 Make sure that it will get emitted.
22661 If it was broken out into a comdat group, don't follow it. */
22662 if (! AT_ref (a)->comdat_type_p
22663 || a->dw_attr == DW_AT_specification)
22664 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22666 /* Set the string's refcount to 0 so that prune_unused_types_mark
22667 accounts properly for it. */
22668 if (AT_class (a) == dw_val_class_str)
22669 a->dw_attr_val.v.val_str->refcount = 0;
22673 /* Mark the generic parameters and arguments children DIEs of DIE. */
22675 static void
22676 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22678 dw_die_ref c;
22680 if (die == NULL || die->die_child == NULL)
22681 return;
22682 c = die->die_child;
22685 if (is_template_parameter (c))
22686 prune_unused_types_mark (c, 1);
22687 c = c->die_sib;
22688 } while (c && c != die->die_child);
22691 /* Mark DIE as being used. If DOKIDS is true, then walk down
22692 to DIE's children. */
22694 static void
22695 prune_unused_types_mark (dw_die_ref die, int dokids)
22697 dw_die_ref c;
22699 if (die->die_mark == 0)
22701 /* We haven't done this node yet. Mark it as used. */
22702 die->die_mark = 1;
22703 /* If this is the DIE of a generic type instantiation,
22704 mark the children DIEs that describe its generic parms and
22705 args. */
22706 prune_unused_types_mark_generic_parms_dies (die);
22708 /* We also have to mark its parents as used.
22709 (But we don't want to mark our parent's kids due to this,
22710 unless it is a class.) */
22711 if (die->die_parent)
22712 prune_unused_types_mark (die->die_parent,
22713 class_scope_p (die->die_parent));
22715 /* Mark any referenced nodes. */
22716 prune_unused_types_walk_attribs (die);
22718 /* If this node is a specification,
22719 also mark the definition, if it exists. */
22720 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22721 prune_unused_types_mark (die->die_definition, 1);
22724 if (dokids && die->die_mark != 2)
22726 /* We need to walk the children, but haven't done so yet.
22727 Remember that we've walked the kids. */
22728 die->die_mark = 2;
22730 /* If this is an array type, we need to make sure our
22731 kids get marked, even if they're types. If we're
22732 breaking out types into comdat sections, do this
22733 for all type definitions. */
22734 if (die->die_tag == DW_TAG_array_type
22735 || (use_debug_types
22736 && is_type_die (die) && ! is_declaration_die (die)))
22737 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22738 else
22739 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22743 /* For local classes, look if any static member functions were emitted
22744 and if so, mark them. */
22746 static void
22747 prune_unused_types_walk_local_classes (dw_die_ref die)
22749 dw_die_ref c;
22751 if (die->die_mark == 2)
22752 return;
22754 switch (die->die_tag)
22756 case DW_TAG_structure_type:
22757 case DW_TAG_union_type:
22758 case DW_TAG_class_type:
22759 break;
22761 case DW_TAG_subprogram:
22762 if (!get_AT_flag (die, DW_AT_declaration)
22763 || die->die_definition != NULL)
22764 prune_unused_types_mark (die, 1);
22765 return;
22767 default:
22768 return;
22771 /* Mark children. */
22772 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22775 /* Walk the tree DIE and mark types that we actually use. */
22777 static void
22778 prune_unused_types_walk (dw_die_ref die)
22780 dw_die_ref c;
22782 /* Don't do anything if this node is already marked and
22783 children have been marked as well. */
22784 if (die->die_mark == 2)
22785 return;
22787 switch (die->die_tag)
22789 case DW_TAG_structure_type:
22790 case DW_TAG_union_type:
22791 case DW_TAG_class_type:
22792 if (die->die_perennial_p)
22793 break;
22795 for (c = die->die_parent; c; c = c->die_parent)
22796 if (c->die_tag == DW_TAG_subprogram)
22797 break;
22799 /* Finding used static member functions inside of classes
22800 is needed just for local classes, because for other classes
22801 static member function DIEs with DW_AT_specification
22802 are emitted outside of the DW_TAG_*_type. If we ever change
22803 it, we'd need to call this even for non-local classes. */
22804 if (c)
22805 prune_unused_types_walk_local_classes (die);
22807 /* It's a type node --- don't mark it. */
22808 return;
22810 case DW_TAG_const_type:
22811 case DW_TAG_packed_type:
22812 case DW_TAG_pointer_type:
22813 case DW_TAG_reference_type:
22814 case DW_TAG_rvalue_reference_type:
22815 case DW_TAG_volatile_type:
22816 case DW_TAG_typedef:
22817 case DW_TAG_array_type:
22818 case DW_TAG_interface_type:
22819 case DW_TAG_friend:
22820 case DW_TAG_variant_part:
22821 case DW_TAG_enumeration_type:
22822 case DW_TAG_subroutine_type:
22823 case DW_TAG_string_type:
22824 case DW_TAG_set_type:
22825 case DW_TAG_subrange_type:
22826 case DW_TAG_ptr_to_member_type:
22827 case DW_TAG_file_type:
22828 if (die->die_perennial_p)
22829 break;
22831 /* It's a type node --- don't mark it. */
22832 return;
22834 default:
22835 /* Mark everything else. */
22836 break;
22839 if (die->die_mark == 0)
22841 die->die_mark = 1;
22843 /* Now, mark any dies referenced from here. */
22844 prune_unused_types_walk_attribs (die);
22847 die->die_mark = 2;
22849 /* Mark children. */
22850 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22853 /* Increment the string counts on strings referred to from DIE's
22854 attributes. */
22856 static void
22857 prune_unused_types_update_strings (dw_die_ref die)
22859 dw_attr_ref a;
22860 unsigned ix;
22862 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22863 if (AT_class (a) == dw_val_class_str)
22865 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22866 s->refcount++;
22867 /* Avoid unnecessarily putting strings that are used less than
22868 twice in the hash table. */
22869 if (s->refcount
22870 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22872 indirect_string_node **slot
22873 = debug_str_hash->find_slot_with_hash (s->str,
22874 htab_hash_string (s->str),
22875 INSERT);
22876 gcc_assert (*slot == NULL);
22877 *slot = s;
22882 /* Remove from the tree DIE any dies that aren't marked. */
22884 static void
22885 prune_unused_types_prune (dw_die_ref die)
22887 dw_die_ref c;
22889 gcc_assert (die->die_mark);
22890 prune_unused_types_update_strings (die);
22892 if (! die->die_child)
22893 return;
22895 c = die->die_child;
22896 do {
22897 dw_die_ref prev = c;
22898 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22899 if (c == die->die_child)
22901 /* No marked children between 'prev' and the end of the list. */
22902 if (prev == c)
22903 /* No marked children at all. */
22904 die->die_child = NULL;
22905 else
22907 prev->die_sib = c->die_sib;
22908 die->die_child = prev;
22910 return;
22913 if (c != prev->die_sib)
22914 prev->die_sib = c;
22915 prune_unused_types_prune (c);
22916 } while (c != die->die_child);
22919 /* Remove dies representing declarations that we never use. */
22921 static void
22922 prune_unused_types (void)
22924 unsigned int i;
22925 limbo_die_node *node;
22926 comdat_type_node *ctnode;
22927 pubname_ref pub;
22928 dw_die_ref base_type;
22930 #if ENABLE_ASSERT_CHECKING
22931 /* All the marks should already be clear. */
22932 verify_marks_clear (comp_unit_die ());
22933 for (node = limbo_die_list; node; node = node->next)
22934 verify_marks_clear (node->die);
22935 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22936 verify_marks_clear (ctnode->root_die);
22937 #endif /* ENABLE_ASSERT_CHECKING */
22939 /* Mark types that are used in global variables. */
22940 premark_types_used_by_global_vars ();
22942 /* Set the mark on nodes that are actually used. */
22943 prune_unused_types_walk (comp_unit_die ());
22944 for (node = limbo_die_list; node; node = node->next)
22945 prune_unused_types_walk (node->die);
22946 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22948 prune_unused_types_walk (ctnode->root_die);
22949 prune_unused_types_mark (ctnode->type_die, 1);
22952 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22953 are unusual in that they are pubnames that are the children of pubtypes.
22954 They should only be marked via their parent DW_TAG_enumeration_type die,
22955 not as roots in themselves. */
22956 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22957 if (pub->die->die_tag != DW_TAG_enumerator)
22958 prune_unused_types_mark (pub->die, 1);
22959 for (i = 0; base_types.iterate (i, &base_type); i++)
22960 prune_unused_types_mark (base_type, 1);
22962 if (debug_str_hash)
22963 debug_str_hash->empty ();
22964 if (skeleton_debug_str_hash)
22965 skeleton_debug_str_hash->empty ();
22966 prune_unused_types_prune (comp_unit_die ());
22967 for (node = limbo_die_list; node; node = node->next)
22968 prune_unused_types_prune (node->die);
22969 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22970 prune_unused_types_prune (ctnode->root_die);
22972 /* Leave the marks clear. */
22973 prune_unmark_dies (comp_unit_die ());
22974 for (node = limbo_die_list; node; node = node->next)
22975 prune_unmark_dies (node->die);
22976 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22977 prune_unmark_dies (ctnode->root_die);
22980 /* Set the parameter to true if there are any relative pathnames in
22981 the file table. */
22983 file_table_relative_p (dwarf_file_data **slot, bool *p)
22985 struct dwarf_file_data *d = *slot;
22986 if (!IS_ABSOLUTE_PATH (d->filename))
22988 *p = true;
22989 return 0;
22991 return 1;
22994 /* Helpers to manipulate hash table of comdat type units. */
22996 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22998 typedef comdat_type_node value_type;
22999 typedef comdat_type_node compare_type;
23000 static inline hashval_t hash (const value_type *);
23001 static inline bool equal (const value_type *, const compare_type *);
23004 inline hashval_t
23005 comdat_type_hasher::hash (const value_type *type_node)
23007 hashval_t h;
23008 memcpy (&h, type_node->signature, sizeof (h));
23009 return h;
23012 inline bool
23013 comdat_type_hasher::equal (const value_type *type_node_1,
23014 const compare_type *type_node_2)
23016 return (! memcmp (type_node_1->signature, type_node_2->signature,
23017 DWARF_TYPE_SIGNATURE_SIZE));
23020 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23021 to the location it would have been added, should we know its
23022 DECL_ASSEMBLER_NAME when we added other attributes. This will
23023 probably improve compactness of debug info, removing equivalent
23024 abbrevs, and hide any differences caused by deferring the
23025 computation of the assembler name, triggered by e.g. PCH. */
23027 static inline void
23028 move_linkage_attr (dw_die_ref die)
23030 unsigned ix = vec_safe_length (die->die_attr);
23031 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23033 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23034 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23036 while (--ix > 0)
23038 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23040 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23041 break;
23044 if (ix != vec_safe_length (die->die_attr) - 1)
23046 die->die_attr->pop ();
23047 die->die_attr->quick_insert (ix, linkage);
23051 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23052 referenced from typed stack ops and count how often they are used. */
23054 static void
23055 mark_base_types (dw_loc_descr_ref loc)
23057 dw_die_ref base_type = NULL;
23059 for (; loc; loc = loc->dw_loc_next)
23061 switch (loc->dw_loc_opc)
23063 case DW_OP_GNU_regval_type:
23064 case DW_OP_GNU_deref_type:
23065 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23066 break;
23067 case DW_OP_GNU_convert:
23068 case DW_OP_GNU_reinterpret:
23069 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23070 continue;
23071 /* FALLTHRU */
23072 case DW_OP_GNU_const_type:
23073 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23074 break;
23075 case DW_OP_GNU_entry_value:
23076 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23077 continue;
23078 default:
23079 continue;
23081 gcc_assert (base_type->die_parent == comp_unit_die ());
23082 if (base_type->die_mark)
23083 base_type->die_mark++;
23084 else
23086 base_types.safe_push (base_type);
23087 base_type->die_mark = 1;
23092 /* Comparison function for sorting marked base types. */
23094 static int
23095 base_type_cmp (const void *x, const void *y)
23097 dw_die_ref dx = *(const dw_die_ref *) x;
23098 dw_die_ref dy = *(const dw_die_ref *) y;
23099 unsigned int byte_size1, byte_size2;
23100 unsigned int encoding1, encoding2;
23101 if (dx->die_mark > dy->die_mark)
23102 return -1;
23103 if (dx->die_mark < dy->die_mark)
23104 return 1;
23105 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23106 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23107 if (byte_size1 < byte_size2)
23108 return 1;
23109 if (byte_size1 > byte_size2)
23110 return -1;
23111 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23112 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23113 if (encoding1 < encoding2)
23114 return 1;
23115 if (encoding1 > encoding2)
23116 return -1;
23117 return 0;
23120 /* Move base types marked by mark_base_types as early as possible
23121 in the CU, sorted by decreasing usage count both to make the
23122 uleb128 references as small as possible and to make sure they
23123 will have die_offset already computed by calc_die_sizes when
23124 sizes of typed stack loc ops is computed. */
23126 static void
23127 move_marked_base_types (void)
23129 unsigned int i;
23130 dw_die_ref base_type, die, c;
23132 if (base_types.is_empty ())
23133 return;
23135 /* Sort by decreasing usage count, they will be added again in that
23136 order later on. */
23137 base_types.qsort (base_type_cmp);
23138 die = comp_unit_die ();
23139 c = die->die_child;
23142 dw_die_ref prev = c;
23143 c = c->die_sib;
23144 while (c->die_mark)
23146 remove_child_with_prev (c, prev);
23147 /* As base types got marked, there must be at least
23148 one node other than DW_TAG_base_type. */
23149 gcc_assert (c != c->die_sib);
23150 c = c->die_sib;
23153 while (c != die->die_child);
23154 gcc_assert (die->die_child);
23155 c = die->die_child;
23156 for (i = 0; base_types.iterate (i, &base_type); i++)
23158 base_type->die_mark = 0;
23159 base_type->die_sib = c->die_sib;
23160 c->die_sib = base_type;
23161 c = base_type;
23165 /* Helper function for resolve_addr, attempt to resolve
23166 one CONST_STRING, return true if successful. Similarly verify that
23167 SYMBOL_REFs refer to variables emitted in the current CU. */
23169 static bool
23170 resolve_one_addr (rtx *addr)
23172 rtx rtl = *addr;
23174 if (GET_CODE (rtl) == CONST_STRING)
23176 size_t len = strlen (XSTR (rtl, 0)) + 1;
23177 tree t = build_string (len, XSTR (rtl, 0));
23178 tree tlen = size_int (len - 1);
23179 TREE_TYPE (t)
23180 = build_array_type (char_type_node, build_index_type (tlen));
23181 rtl = lookup_constant_def (t);
23182 if (!rtl || !MEM_P (rtl))
23183 return false;
23184 rtl = XEXP (rtl, 0);
23185 if (GET_CODE (rtl) == SYMBOL_REF
23186 && SYMBOL_REF_DECL (rtl)
23187 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23188 return false;
23189 vec_safe_push (used_rtx_array, rtl);
23190 *addr = rtl;
23191 return true;
23194 if (GET_CODE (rtl) == SYMBOL_REF
23195 && SYMBOL_REF_DECL (rtl))
23197 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23199 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23200 return false;
23202 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23203 return false;
23206 if (GET_CODE (rtl) == CONST)
23208 subrtx_ptr_iterator::array_type array;
23209 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23210 if (!resolve_one_addr (*iter))
23211 return false;
23214 return true;
23217 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23218 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23219 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23221 static rtx
23222 string_cst_pool_decl (tree t)
23224 rtx rtl = output_constant_def (t, 1);
23225 unsigned char *array;
23226 dw_loc_descr_ref l;
23227 tree decl;
23228 size_t len;
23229 dw_die_ref ref;
23231 if (!rtl || !MEM_P (rtl))
23232 return NULL_RTX;
23233 rtl = XEXP (rtl, 0);
23234 if (GET_CODE (rtl) != SYMBOL_REF
23235 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23236 return NULL_RTX;
23238 decl = SYMBOL_REF_DECL (rtl);
23239 if (!lookup_decl_die (decl))
23241 len = TREE_STRING_LENGTH (t);
23242 vec_safe_push (used_rtx_array, rtl);
23243 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23244 array = ggc_vec_alloc<unsigned char> (len);
23245 memcpy (array, TREE_STRING_POINTER (t), len);
23246 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23247 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23248 l->dw_loc_oprnd2.v.val_vec.length = len;
23249 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23250 l->dw_loc_oprnd2.v.val_vec.array = array;
23251 add_AT_loc (ref, DW_AT_location, l);
23252 equate_decl_number_to_die (decl, ref);
23254 return rtl;
23257 /* Helper function of resolve_addr_in_expr. LOC is
23258 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23259 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23260 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23261 with DW_OP_GNU_implicit_pointer if possible
23262 and return true, if unsuccessful, return false. */
23264 static bool
23265 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23267 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23268 HOST_WIDE_INT offset = 0;
23269 dw_die_ref ref = NULL;
23270 tree decl;
23272 if (GET_CODE (rtl) == CONST
23273 && GET_CODE (XEXP (rtl, 0)) == PLUS
23274 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23276 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23277 rtl = XEXP (XEXP (rtl, 0), 0);
23279 if (GET_CODE (rtl) == CONST_STRING)
23281 size_t len = strlen (XSTR (rtl, 0)) + 1;
23282 tree t = build_string (len, XSTR (rtl, 0));
23283 tree tlen = size_int (len - 1);
23285 TREE_TYPE (t)
23286 = build_array_type (char_type_node, build_index_type (tlen));
23287 rtl = string_cst_pool_decl (t);
23288 if (!rtl)
23289 return false;
23291 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23293 decl = SYMBOL_REF_DECL (rtl);
23294 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23296 ref = lookup_decl_die (decl);
23297 if (ref && (get_AT (ref, DW_AT_location)
23298 || get_AT (ref, DW_AT_const_value)))
23300 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23301 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23302 loc->dw_loc_oprnd1.val_entry = NULL;
23303 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23304 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23305 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23306 loc->dw_loc_oprnd2.v.val_int = offset;
23307 return true;
23311 return false;
23314 /* Helper function for resolve_addr, handle one location
23315 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23316 the location list couldn't be resolved. */
23318 static bool
23319 resolve_addr_in_expr (dw_loc_descr_ref loc)
23321 dw_loc_descr_ref keep = NULL;
23322 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23323 switch (loc->dw_loc_opc)
23325 case DW_OP_addr:
23326 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23328 if ((prev == NULL
23329 || prev->dw_loc_opc == DW_OP_piece
23330 || prev->dw_loc_opc == DW_OP_bit_piece)
23331 && loc->dw_loc_next
23332 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23333 && !dwarf_strict
23334 && optimize_one_addr_into_implicit_ptr (loc))
23335 break;
23336 return false;
23338 break;
23339 case DW_OP_GNU_addr_index:
23340 case DW_OP_GNU_const_index:
23341 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23342 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23344 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23345 if (!resolve_one_addr (&rtl))
23346 return false;
23347 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23348 loc->dw_loc_oprnd1.val_entry =
23349 add_addr_table_entry (rtl, ate_kind_rtx);
23351 break;
23352 case DW_OP_const4u:
23353 case DW_OP_const8u:
23354 if (loc->dtprel
23355 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23356 return false;
23357 break;
23358 case DW_OP_plus_uconst:
23359 if (size_of_loc_descr (loc)
23360 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23362 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23364 dw_loc_descr_ref repl
23365 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23366 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23367 add_loc_descr (&repl, loc->dw_loc_next);
23368 *loc = *repl;
23370 break;
23371 case DW_OP_implicit_value:
23372 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23373 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23374 return false;
23375 break;
23376 case DW_OP_GNU_implicit_pointer:
23377 case DW_OP_GNU_parameter_ref:
23378 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23380 dw_die_ref ref
23381 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23382 if (ref == NULL)
23383 return false;
23384 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23385 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23386 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23388 break;
23389 case DW_OP_GNU_const_type:
23390 case DW_OP_GNU_regval_type:
23391 case DW_OP_GNU_deref_type:
23392 case DW_OP_GNU_convert:
23393 case DW_OP_GNU_reinterpret:
23394 while (loc->dw_loc_next
23395 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23397 dw_die_ref base1, base2;
23398 unsigned enc1, enc2, size1, size2;
23399 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23400 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23401 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23402 else if (loc->dw_loc_oprnd1.val_class
23403 == dw_val_class_unsigned_const)
23404 break;
23405 else
23406 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23407 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23408 == dw_val_class_unsigned_const)
23409 break;
23410 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23411 gcc_assert (base1->die_tag == DW_TAG_base_type
23412 && base2->die_tag == DW_TAG_base_type);
23413 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23414 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23415 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23416 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23417 if (size1 == size2
23418 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23419 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23420 && loc != keep)
23421 || enc1 == enc2))
23423 /* Optimize away next DW_OP_GNU_convert after
23424 adjusting LOC's base type die reference. */
23425 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23426 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23427 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23428 else
23429 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23430 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23431 continue;
23433 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23434 point typed stack entry. */
23435 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23436 keep = loc->dw_loc_next;
23437 break;
23439 break;
23440 default:
23441 break;
23443 return true;
23446 /* Helper function of resolve_addr. DIE had DW_AT_location of
23447 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23448 and DW_OP_addr couldn't be resolved. resolve_addr has already
23449 removed the DW_AT_location attribute. This function attempts to
23450 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23451 to it or DW_AT_const_value attribute, if possible. */
23453 static void
23454 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23456 if (TREE_CODE (decl) != VAR_DECL
23457 || lookup_decl_die (decl) != die
23458 || DECL_EXTERNAL (decl)
23459 || !TREE_STATIC (decl)
23460 || DECL_INITIAL (decl) == NULL_TREE
23461 || DECL_P (DECL_INITIAL (decl))
23462 || get_AT (die, DW_AT_const_value))
23463 return;
23465 tree init = DECL_INITIAL (decl);
23466 HOST_WIDE_INT offset = 0;
23467 /* For variables that have been optimized away and thus
23468 don't have a memory location, see if we can emit
23469 DW_AT_const_value instead. */
23470 if (tree_add_const_value_attribute (die, init))
23471 return;
23472 if (dwarf_strict)
23473 return;
23474 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23475 and ADDR_EXPR refers to a decl that has DW_AT_location or
23476 DW_AT_const_value (but isn't addressable, otherwise
23477 resolving the original DW_OP_addr wouldn't fail), see if
23478 we can add DW_OP_GNU_implicit_pointer. */
23479 STRIP_NOPS (init);
23480 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23481 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23483 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23484 init = TREE_OPERAND (init, 0);
23485 STRIP_NOPS (init);
23487 if (TREE_CODE (init) != ADDR_EXPR)
23488 return;
23489 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23490 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23491 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23492 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23493 && TREE_OPERAND (init, 0) != decl))
23495 dw_die_ref ref;
23496 dw_loc_descr_ref l;
23498 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23500 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23501 if (!rtl)
23502 return;
23503 decl = SYMBOL_REF_DECL (rtl);
23505 else
23506 decl = TREE_OPERAND (init, 0);
23507 ref = lookup_decl_die (decl);
23508 if (ref == NULL
23509 || (!get_AT (ref, DW_AT_location)
23510 && !get_AT (ref, DW_AT_const_value)))
23511 return;
23512 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23513 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23514 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23515 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23516 add_AT_loc (die, DW_AT_location, l);
23520 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23521 an address in .rodata section if the string literal is emitted there,
23522 or remove the containing location list or replace DW_AT_const_value
23523 with DW_AT_location and empty location expression, if it isn't found
23524 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23525 to something that has been emitted in the current CU. */
23527 static void
23528 resolve_addr (dw_die_ref die)
23530 dw_die_ref c;
23531 dw_attr_ref a;
23532 dw_loc_list_ref *curr, *start, loc;
23533 unsigned ix;
23535 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23536 switch (AT_class (a))
23538 case dw_val_class_loc_list:
23539 start = curr = AT_loc_list_ptr (a);
23540 loc = *curr;
23541 gcc_assert (loc);
23542 /* The same list can be referenced more than once. See if we have
23543 already recorded the result from a previous pass. */
23544 if (loc->replaced)
23545 *curr = loc->dw_loc_next;
23546 else if (!loc->resolved_addr)
23548 /* As things stand, we do not expect or allow one die to
23549 reference a suffix of another die's location list chain.
23550 References must be identical or completely separate.
23551 There is therefore no need to cache the result of this
23552 pass on any list other than the first; doing so
23553 would lead to unnecessary writes. */
23554 while (*curr)
23556 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23557 if (!resolve_addr_in_expr ((*curr)->expr))
23559 dw_loc_list_ref next = (*curr)->dw_loc_next;
23560 dw_loc_descr_ref l = (*curr)->expr;
23562 if (next && (*curr)->ll_symbol)
23564 gcc_assert (!next->ll_symbol);
23565 next->ll_symbol = (*curr)->ll_symbol;
23567 if (dwarf_split_debug_info)
23568 remove_loc_list_addr_table_entries (l);
23569 *curr = next;
23571 else
23573 mark_base_types ((*curr)->expr);
23574 curr = &(*curr)->dw_loc_next;
23577 if (loc == *start)
23578 loc->resolved_addr = 1;
23579 else
23581 loc->replaced = 1;
23582 loc->dw_loc_next = *start;
23585 if (!*start)
23587 remove_AT (die, a->dw_attr);
23588 ix--;
23590 break;
23591 case dw_val_class_loc:
23593 dw_loc_descr_ref l = AT_loc (a);
23594 /* For -gdwarf-2 don't attempt to optimize
23595 DW_AT_data_member_location containing
23596 DW_OP_plus_uconst - older consumers might
23597 rely on it being that op instead of a more complex,
23598 but shorter, location description. */
23599 if ((dwarf_version > 2
23600 || a->dw_attr != DW_AT_data_member_location
23601 || l == NULL
23602 || l->dw_loc_opc != DW_OP_plus_uconst
23603 || l->dw_loc_next != NULL)
23604 && !resolve_addr_in_expr (l))
23606 if (dwarf_split_debug_info)
23607 remove_loc_list_addr_table_entries (l);
23608 if (l != NULL
23609 && l->dw_loc_next == NULL
23610 && l->dw_loc_opc == DW_OP_addr
23611 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23612 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23613 && a->dw_attr == DW_AT_location)
23615 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23616 remove_AT (die, a->dw_attr);
23617 ix--;
23618 optimize_location_into_implicit_ptr (die, decl);
23619 break;
23621 remove_AT (die, a->dw_attr);
23622 ix--;
23624 else
23625 mark_base_types (l);
23627 break;
23628 case dw_val_class_addr:
23629 if (a->dw_attr == DW_AT_const_value
23630 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23632 if (AT_index (a) != NOT_INDEXED)
23633 remove_addr_table_entry (a->dw_attr_val.val_entry);
23634 remove_AT (die, a->dw_attr);
23635 ix--;
23637 if (die->die_tag == DW_TAG_GNU_call_site
23638 && a->dw_attr == DW_AT_abstract_origin)
23640 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23641 dw_die_ref tdie = lookup_decl_die (tdecl);
23642 if (tdie == NULL
23643 && DECL_EXTERNAL (tdecl)
23644 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23646 force_decl_die (tdecl);
23647 tdie = lookup_decl_die (tdecl);
23649 if (tdie)
23651 a->dw_attr_val.val_class = dw_val_class_die_ref;
23652 a->dw_attr_val.v.val_die_ref.die = tdie;
23653 a->dw_attr_val.v.val_die_ref.external = 0;
23655 else
23657 if (AT_index (a) != NOT_INDEXED)
23658 remove_addr_table_entry (a->dw_attr_val.val_entry);
23659 remove_AT (die, a->dw_attr);
23660 ix--;
23663 break;
23664 default:
23665 break;
23668 FOR_EACH_CHILD (die, c, resolve_addr (c));
23671 /* Helper routines for optimize_location_lists.
23672 This pass tries to share identical local lists in .debug_loc
23673 section. */
23675 /* Iteratively hash operands of LOC opcode into HSTATE. */
23677 static void
23678 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23680 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23681 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23683 switch (loc->dw_loc_opc)
23685 case DW_OP_const4u:
23686 case DW_OP_const8u:
23687 if (loc->dtprel)
23688 goto hash_addr;
23689 /* FALLTHRU */
23690 case DW_OP_const1u:
23691 case DW_OP_const1s:
23692 case DW_OP_const2u:
23693 case DW_OP_const2s:
23694 case DW_OP_const4s:
23695 case DW_OP_const8s:
23696 case DW_OP_constu:
23697 case DW_OP_consts:
23698 case DW_OP_pick:
23699 case DW_OP_plus_uconst:
23700 case DW_OP_breg0:
23701 case DW_OP_breg1:
23702 case DW_OP_breg2:
23703 case DW_OP_breg3:
23704 case DW_OP_breg4:
23705 case DW_OP_breg5:
23706 case DW_OP_breg6:
23707 case DW_OP_breg7:
23708 case DW_OP_breg8:
23709 case DW_OP_breg9:
23710 case DW_OP_breg10:
23711 case DW_OP_breg11:
23712 case DW_OP_breg12:
23713 case DW_OP_breg13:
23714 case DW_OP_breg14:
23715 case DW_OP_breg15:
23716 case DW_OP_breg16:
23717 case DW_OP_breg17:
23718 case DW_OP_breg18:
23719 case DW_OP_breg19:
23720 case DW_OP_breg20:
23721 case DW_OP_breg21:
23722 case DW_OP_breg22:
23723 case DW_OP_breg23:
23724 case DW_OP_breg24:
23725 case DW_OP_breg25:
23726 case DW_OP_breg26:
23727 case DW_OP_breg27:
23728 case DW_OP_breg28:
23729 case DW_OP_breg29:
23730 case DW_OP_breg30:
23731 case DW_OP_breg31:
23732 case DW_OP_regx:
23733 case DW_OP_fbreg:
23734 case DW_OP_piece:
23735 case DW_OP_deref_size:
23736 case DW_OP_xderef_size:
23737 hstate.add_object (val1->v.val_int);
23738 break;
23739 case DW_OP_skip:
23740 case DW_OP_bra:
23742 int offset;
23744 gcc_assert (val1->val_class == dw_val_class_loc);
23745 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23746 hstate.add_object (offset);
23748 break;
23749 case DW_OP_implicit_value:
23750 hstate.add_object (val1->v.val_unsigned);
23751 switch (val2->val_class)
23753 case dw_val_class_const:
23754 hstate.add_object (val2->v.val_int);
23755 break;
23756 case dw_val_class_vec:
23758 unsigned int elt_size = val2->v.val_vec.elt_size;
23759 unsigned int len = val2->v.val_vec.length;
23761 hstate.add_int (elt_size);
23762 hstate.add_int (len);
23763 hstate.add (val2->v.val_vec.array, len * elt_size);
23765 break;
23766 case dw_val_class_const_double:
23767 hstate.add_object (val2->v.val_double.low);
23768 hstate.add_object (val2->v.val_double.high);
23769 break;
23770 case dw_val_class_wide_int:
23771 hstate.add_object (*val2->v.val_wide);
23772 break;
23773 case dw_val_class_addr:
23774 inchash::add_rtx (val2->v.val_addr, hstate);
23775 break;
23776 default:
23777 gcc_unreachable ();
23779 break;
23780 case DW_OP_bregx:
23781 case DW_OP_bit_piece:
23782 hstate.add_object (val1->v.val_int);
23783 hstate.add_object (val2->v.val_int);
23784 break;
23785 case DW_OP_addr:
23786 hash_addr:
23787 if (loc->dtprel)
23789 unsigned char dtprel = 0xd1;
23790 hstate.add_object (dtprel);
23792 inchash::add_rtx (val1->v.val_addr, hstate);
23793 break;
23794 case DW_OP_GNU_addr_index:
23795 case DW_OP_GNU_const_index:
23797 if (loc->dtprel)
23799 unsigned char dtprel = 0xd1;
23800 hstate.add_object (dtprel);
23802 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
23804 break;
23805 case DW_OP_GNU_implicit_pointer:
23806 hstate.add_int (val2->v.val_int);
23807 break;
23808 case DW_OP_GNU_entry_value:
23809 hstate.add_object (val1->v.val_loc);
23810 break;
23811 case DW_OP_GNU_regval_type:
23812 case DW_OP_GNU_deref_type:
23814 unsigned int byte_size
23815 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23816 unsigned int encoding
23817 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23818 hstate.add_object (val1->v.val_int);
23819 hstate.add_object (byte_size);
23820 hstate.add_object (encoding);
23822 break;
23823 case DW_OP_GNU_convert:
23824 case DW_OP_GNU_reinterpret:
23825 if (val1->val_class == dw_val_class_unsigned_const)
23827 hstate.add_object (val1->v.val_unsigned);
23828 break;
23830 /* FALLTHRU */
23831 case DW_OP_GNU_const_type:
23833 unsigned int byte_size
23834 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23835 unsigned int encoding
23836 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23837 hstate.add_object (byte_size);
23838 hstate.add_object (encoding);
23839 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23840 break;
23841 hstate.add_object (val2->val_class);
23842 switch (val2->val_class)
23844 case dw_val_class_const:
23845 hstate.add_object (val2->v.val_int);
23846 break;
23847 case dw_val_class_vec:
23849 unsigned int elt_size = val2->v.val_vec.elt_size;
23850 unsigned int len = val2->v.val_vec.length;
23852 hstate.add_object (elt_size);
23853 hstate.add_object (len);
23854 hstate.add (val2->v.val_vec.array, len * elt_size);
23856 break;
23857 case dw_val_class_const_double:
23858 hstate.add_object (val2->v.val_double.low);
23859 hstate.add_object (val2->v.val_double.high);
23860 break;
23861 case dw_val_class_wide_int:
23862 hstate.add_object (*val2->v.val_wide);
23863 break;
23864 default:
23865 gcc_unreachable ();
23868 break;
23870 default:
23871 /* Other codes have no operands. */
23872 break;
23876 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
23878 static inline void
23879 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
23881 dw_loc_descr_ref l;
23882 bool sizes_computed = false;
23883 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23884 size_of_locs (loc);
23886 for (l = loc; l != NULL; l = l->dw_loc_next)
23888 enum dwarf_location_atom opc = l->dw_loc_opc;
23889 hstate.add_object (opc);
23890 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23892 size_of_locs (loc);
23893 sizes_computed = true;
23895 hash_loc_operands (l, hstate);
23899 /* Compute hash of the whole location list LIST_HEAD. */
23901 static inline void
23902 hash_loc_list (dw_loc_list_ref list_head)
23904 dw_loc_list_ref curr = list_head;
23905 inchash::hash hstate;
23907 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23909 hstate.add (curr->begin, strlen (curr->begin) + 1);
23910 hstate.add (curr->end, strlen (curr->end) + 1);
23911 if (curr->section)
23912 hstate.add (curr->section, strlen (curr->section) + 1);
23913 hash_locs (curr->expr, hstate);
23915 list_head->hash = hstate.end ();
23918 /* Return true if X and Y opcodes have the same operands. */
23920 static inline bool
23921 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23923 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23924 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23925 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23926 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23928 switch (x->dw_loc_opc)
23930 case DW_OP_const4u:
23931 case DW_OP_const8u:
23932 if (x->dtprel)
23933 goto hash_addr;
23934 /* FALLTHRU */
23935 case DW_OP_const1u:
23936 case DW_OP_const1s:
23937 case DW_OP_const2u:
23938 case DW_OP_const2s:
23939 case DW_OP_const4s:
23940 case DW_OP_const8s:
23941 case DW_OP_constu:
23942 case DW_OP_consts:
23943 case DW_OP_pick:
23944 case DW_OP_plus_uconst:
23945 case DW_OP_breg0:
23946 case DW_OP_breg1:
23947 case DW_OP_breg2:
23948 case DW_OP_breg3:
23949 case DW_OP_breg4:
23950 case DW_OP_breg5:
23951 case DW_OP_breg6:
23952 case DW_OP_breg7:
23953 case DW_OP_breg8:
23954 case DW_OP_breg9:
23955 case DW_OP_breg10:
23956 case DW_OP_breg11:
23957 case DW_OP_breg12:
23958 case DW_OP_breg13:
23959 case DW_OP_breg14:
23960 case DW_OP_breg15:
23961 case DW_OP_breg16:
23962 case DW_OP_breg17:
23963 case DW_OP_breg18:
23964 case DW_OP_breg19:
23965 case DW_OP_breg20:
23966 case DW_OP_breg21:
23967 case DW_OP_breg22:
23968 case DW_OP_breg23:
23969 case DW_OP_breg24:
23970 case DW_OP_breg25:
23971 case DW_OP_breg26:
23972 case DW_OP_breg27:
23973 case DW_OP_breg28:
23974 case DW_OP_breg29:
23975 case DW_OP_breg30:
23976 case DW_OP_breg31:
23977 case DW_OP_regx:
23978 case DW_OP_fbreg:
23979 case DW_OP_piece:
23980 case DW_OP_deref_size:
23981 case DW_OP_xderef_size:
23982 return valx1->v.val_int == valy1->v.val_int;
23983 case DW_OP_skip:
23984 case DW_OP_bra:
23985 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23986 can cause irrelevant differences in dw_loc_addr. */
23987 gcc_assert (valx1->val_class == dw_val_class_loc
23988 && valy1->val_class == dw_val_class_loc
23989 && (dwarf_split_debug_info
23990 || x->dw_loc_addr == y->dw_loc_addr));
23991 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23992 case DW_OP_implicit_value:
23993 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23994 || valx2->val_class != valy2->val_class)
23995 return false;
23996 switch (valx2->val_class)
23998 case dw_val_class_const:
23999 return valx2->v.val_int == valy2->v.val_int;
24000 case dw_val_class_vec:
24001 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24002 && valx2->v.val_vec.length == valy2->v.val_vec.length
24003 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24004 valx2->v.val_vec.elt_size
24005 * valx2->v.val_vec.length) == 0;
24006 case dw_val_class_const_double:
24007 return valx2->v.val_double.low == valy2->v.val_double.low
24008 && valx2->v.val_double.high == valy2->v.val_double.high;
24009 case dw_val_class_wide_int:
24010 return *valx2->v.val_wide == *valy2->v.val_wide;
24011 case dw_val_class_addr:
24012 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24013 default:
24014 gcc_unreachable ();
24016 case DW_OP_bregx:
24017 case DW_OP_bit_piece:
24018 return valx1->v.val_int == valy1->v.val_int
24019 && valx2->v.val_int == valy2->v.val_int;
24020 case DW_OP_addr:
24021 hash_addr:
24022 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24023 case DW_OP_GNU_addr_index:
24024 case DW_OP_GNU_const_index:
24026 rtx ax1 = valx1->val_entry->addr.rtl;
24027 rtx ay1 = valy1->val_entry->addr.rtl;
24028 return rtx_equal_p (ax1, ay1);
24030 case DW_OP_GNU_implicit_pointer:
24031 return valx1->val_class == dw_val_class_die_ref
24032 && valx1->val_class == valy1->val_class
24033 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24034 && valx2->v.val_int == valy2->v.val_int;
24035 case DW_OP_GNU_entry_value:
24036 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24037 case DW_OP_GNU_const_type:
24038 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24039 || valx2->val_class != valy2->val_class)
24040 return false;
24041 switch (valx2->val_class)
24043 case dw_val_class_const:
24044 return valx2->v.val_int == valy2->v.val_int;
24045 case dw_val_class_vec:
24046 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24047 && valx2->v.val_vec.length == valy2->v.val_vec.length
24048 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24049 valx2->v.val_vec.elt_size
24050 * valx2->v.val_vec.length) == 0;
24051 case dw_val_class_const_double:
24052 return valx2->v.val_double.low == valy2->v.val_double.low
24053 && valx2->v.val_double.high == valy2->v.val_double.high;
24054 case dw_val_class_wide_int:
24055 return *valx2->v.val_wide == *valy2->v.val_wide;
24056 default:
24057 gcc_unreachable ();
24059 case DW_OP_GNU_regval_type:
24060 case DW_OP_GNU_deref_type:
24061 return valx1->v.val_int == valy1->v.val_int
24062 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24063 case DW_OP_GNU_convert:
24064 case DW_OP_GNU_reinterpret:
24065 if (valx1->val_class != valy1->val_class)
24066 return false;
24067 if (valx1->val_class == dw_val_class_unsigned_const)
24068 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24069 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24070 case DW_OP_GNU_parameter_ref:
24071 return valx1->val_class == dw_val_class_die_ref
24072 && valx1->val_class == valy1->val_class
24073 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24074 default:
24075 /* Other codes have no operands. */
24076 return true;
24080 /* Return true if DWARF location expressions X and Y are the same. */
24082 static inline bool
24083 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24085 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24086 if (x->dw_loc_opc != y->dw_loc_opc
24087 || x->dtprel != y->dtprel
24088 || !compare_loc_operands (x, y))
24089 break;
24090 return x == NULL && y == NULL;
24093 /* Hashtable helpers. */
24095 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24097 typedef dw_loc_list_struct value_type;
24098 typedef dw_loc_list_struct compare_type;
24099 static inline hashval_t hash (const value_type *);
24100 static inline bool equal (const value_type *, const compare_type *);
24103 /* Return precomputed hash of location list X. */
24105 inline hashval_t
24106 loc_list_hasher::hash (const value_type *x)
24108 return x->hash;
24111 /* Return true if location lists A and B are the same. */
24113 inline bool
24114 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24116 if (a == b)
24117 return 1;
24118 if (a->hash != b->hash)
24119 return 0;
24120 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24121 if (strcmp (a->begin, b->begin) != 0
24122 || strcmp (a->end, b->end) != 0
24123 || (a->section == NULL) != (b->section == NULL)
24124 || (a->section && strcmp (a->section, b->section) != 0)
24125 || !compare_locs (a->expr, b->expr))
24126 break;
24127 return a == NULL && b == NULL;
24130 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24133 /* Recursively optimize location lists referenced from DIE
24134 children and share them whenever possible. */
24136 static void
24137 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24139 dw_die_ref c;
24140 dw_attr_ref a;
24141 unsigned ix;
24142 dw_loc_list_struct **slot;
24144 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24145 if (AT_class (a) == dw_val_class_loc_list)
24147 dw_loc_list_ref list = AT_loc_list (a);
24148 /* TODO: perform some optimizations here, before hashing
24149 it and storing into the hash table. */
24150 hash_loc_list (list);
24151 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24152 if (*slot == NULL)
24153 *slot = list;
24154 else
24155 a->dw_attr_val.v.val_loc_list = *slot;
24158 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24162 /* Recursively assign each location list a unique index into the debug_addr
24163 section. */
24165 static void
24166 index_location_lists (dw_die_ref die)
24168 dw_die_ref c;
24169 dw_attr_ref a;
24170 unsigned ix;
24172 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24173 if (AT_class (a) == dw_val_class_loc_list)
24175 dw_loc_list_ref list = AT_loc_list (a);
24176 dw_loc_list_ref curr;
24177 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24179 /* Don't index an entry that has already been indexed
24180 or won't be output. */
24181 if (curr->begin_entry != NULL
24182 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24183 continue;
24185 curr->begin_entry
24186 = add_addr_table_entry (xstrdup (curr->begin),
24187 ate_kind_label);
24191 FOR_EACH_CHILD (die, c, index_location_lists (c));
24194 /* Optimize location lists referenced from DIE
24195 children and share them whenever possible. */
24197 static void
24198 optimize_location_lists (dw_die_ref die)
24200 loc_list_hash_type htab (500);
24201 optimize_location_lists_1 (die, &htab);
24204 /* Output stuff that dwarf requires at the end of every file,
24205 and generate the DWARF-2 debugging info. */
24207 static void
24208 dwarf2out_finish (const char *filename)
24210 limbo_die_node *node, *next_node;
24211 comdat_type_node *ctnode;
24212 unsigned int i;
24213 dw_die_ref main_comp_unit_die;
24215 /* PCH might result in DW_AT_producer string being restored from the
24216 header compilation, so always fill it with empty string initially
24217 and overwrite only here. */
24218 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24219 producer_string = gen_producer_string ();
24220 producer->dw_attr_val.v.val_str->refcount--;
24221 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24223 gen_scheduled_generic_parms_dies ();
24224 gen_remaining_tmpl_value_param_die_attribute ();
24226 /* Add the name for the main input file now. We delayed this from
24227 dwarf2out_init to avoid complications with PCH. */
24228 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24229 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24230 add_comp_dir_attribute (comp_unit_die ());
24231 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24233 bool p = false;
24234 file_table->traverse<bool *, file_table_relative_p> (&p);
24235 if (p)
24236 add_comp_dir_attribute (comp_unit_die ());
24239 if (deferred_locations_list)
24240 for (i = 0; i < deferred_locations_list->length (); i++)
24242 add_location_or_const_value_attribute (
24243 (*deferred_locations_list)[i].die,
24244 (*deferred_locations_list)[i].variable,
24245 false,
24246 DW_AT_location);
24249 /* Traverse the limbo die list, and add parent/child links. The only
24250 dies without parents that should be here are concrete instances of
24251 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24252 For concrete instances, we can get the parent die from the abstract
24253 instance. */
24254 for (node = limbo_die_list; node; node = next_node)
24256 dw_die_ref die = node->die;
24257 next_node = node->next;
24259 if (die->die_parent == NULL)
24261 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24263 if (origin && origin->die_parent)
24264 add_child_die (origin->die_parent, die);
24265 else if (is_cu_die (die))
24267 else if (seen_error ())
24268 /* It's OK to be confused by errors in the input. */
24269 add_child_die (comp_unit_die (), die);
24270 else
24272 /* In certain situations, the lexical block containing a
24273 nested function can be optimized away, which results
24274 in the nested function die being orphaned. Likewise
24275 with the return type of that nested function. Force
24276 this to be a child of the containing function.
24278 It may happen that even the containing function got fully
24279 inlined and optimized out. In that case we are lost and
24280 assign the empty child. This should not be big issue as
24281 the function is likely unreachable too. */
24282 gcc_assert (node->created_for);
24284 if (DECL_P (node->created_for))
24285 origin = get_context_die (DECL_CONTEXT (node->created_for));
24286 else if (TYPE_P (node->created_for))
24287 origin = scope_die_for (node->created_for, comp_unit_die ());
24288 else
24289 origin = comp_unit_die ();
24291 add_child_die (origin, die);
24296 limbo_die_list = NULL;
24298 #if ENABLE_ASSERT_CHECKING
24300 dw_die_ref die = comp_unit_die (), c;
24301 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24303 #endif
24304 resolve_addr (comp_unit_die ());
24305 move_marked_base_types ();
24307 for (node = deferred_asm_name; node; node = node->next)
24309 tree decl = node->created_for;
24310 /* When generating LTO bytecode we can not generate new assembler
24311 names at this point and all important decls got theirs via
24312 free-lang-data. */
24313 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24314 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24316 add_linkage_attr (node->die, decl);
24317 move_linkage_attr (node->die);
24321 deferred_asm_name = NULL;
24323 /* Walk through the list of incomplete types again, trying once more to
24324 emit full debugging info for them. */
24325 retry_incomplete_types ();
24327 if (flag_eliminate_unused_debug_types)
24328 prune_unused_types ();
24330 /* Generate separate COMDAT sections for type DIEs. */
24331 if (use_debug_types)
24333 break_out_comdat_types (comp_unit_die ());
24335 /* Each new type_unit DIE was added to the limbo die list when created.
24336 Since these have all been added to comdat_type_list, clear the
24337 limbo die list. */
24338 limbo_die_list = NULL;
24340 /* For each new comdat type unit, copy declarations for incomplete
24341 types to make the new unit self-contained (i.e., no direct
24342 references to the main compile unit). */
24343 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24344 copy_decls_for_unworthy_types (ctnode->root_die);
24345 copy_decls_for_unworthy_types (comp_unit_die ());
24347 /* In the process of copying declarations from one unit to another,
24348 we may have left some declarations behind that are no longer
24349 referenced. Prune them. */
24350 prune_unused_types ();
24353 /* Generate separate CUs for each of the include files we've seen.
24354 They will go into limbo_die_list. */
24355 if (flag_eliminate_dwarf2_dups)
24356 break_out_includes (comp_unit_die ());
24358 /* Traverse the DIE's and add add sibling attributes to those DIE's
24359 that have children. */
24360 add_sibling_attributes (comp_unit_die ());
24361 for (node = limbo_die_list; node; node = node->next)
24362 add_sibling_attributes (node->die);
24363 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24364 add_sibling_attributes (ctnode->root_die);
24366 /* When splitting DWARF info, we put some attributes in the
24367 skeleton compile_unit DIE that remains in the .o, while
24368 most attributes go in the DWO compile_unit_die. */
24369 if (dwarf_split_debug_info)
24370 main_comp_unit_die = gen_compile_unit_die (NULL);
24371 else
24372 main_comp_unit_die = comp_unit_die ();
24374 /* Output a terminator label for the .text section. */
24375 switch_to_section (text_section);
24376 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24377 if (cold_text_section)
24379 switch_to_section (cold_text_section);
24380 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24383 /* We can only use the low/high_pc attributes if all of the code was
24384 in .text. */
24385 if (!have_multiple_function_sections
24386 || (dwarf_version < 3 && dwarf_strict))
24388 /* Don't add if the CU has no associated code. */
24389 if (text_section_used)
24390 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24391 text_end_label, true);
24393 else
24395 unsigned fde_idx;
24396 dw_fde_ref fde;
24397 bool range_list_added = false;
24399 if (text_section_used)
24400 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24401 text_end_label, &range_list_added, true);
24402 if (cold_text_section_used)
24403 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24404 cold_end_label, &range_list_added, true);
24406 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24408 if (DECL_IGNORED_P (fde->decl))
24409 continue;
24410 if (!fde->in_std_section)
24411 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24412 fde->dw_fde_end, &range_list_added,
24413 true);
24414 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24415 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24416 fde->dw_fde_second_end, &range_list_added,
24417 true);
24420 if (range_list_added)
24422 /* We need to give .debug_loc and .debug_ranges an appropriate
24423 "base address". Use zero so that these addresses become
24424 absolute. Historically, we've emitted the unexpected
24425 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24426 Emit both to give time for other tools to adapt. */
24427 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24428 if (! dwarf_strict && dwarf_version < 4)
24429 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24431 add_ranges (NULL);
24435 if (debug_info_level >= DINFO_LEVEL_TERSE)
24436 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24437 debug_line_section_label);
24439 if (have_macinfo)
24440 add_AT_macptr (comp_unit_die (),
24441 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24442 macinfo_section_label);
24444 if (dwarf_split_debug_info)
24446 /* optimize_location_lists calculates the size of the lists,
24447 so index them first, and assign indices to the entries.
24448 Although optimize_location_lists will remove entries from
24449 the table, it only does so for duplicates, and therefore
24450 only reduces ref_counts to 1. */
24451 index_location_lists (comp_unit_die ());
24453 if (addr_index_table != NULL)
24455 unsigned int index = 0;
24456 addr_index_table
24457 ->traverse_noresize<unsigned int *, index_addr_table_entry>
24458 (&index);
24462 if (have_location_lists)
24463 optimize_location_lists (comp_unit_die ());
24465 save_macinfo_strings ();
24467 if (dwarf_split_debug_info)
24469 unsigned int index = 0;
24471 /* Add attributes common to skeleton compile_units and
24472 type_units. Because these attributes include strings, it
24473 must be done before freezing the string table. Top-level
24474 skeleton die attrs are added when the skeleton type unit is
24475 created, so ensure it is created by this point. */
24476 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24477 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24480 /* Output all of the compilation units. We put the main one last so that
24481 the offsets are available to output_pubnames. */
24482 for (node = limbo_die_list; node; node = node->next)
24483 output_comp_unit (node->die, 0);
24485 hash_table<comdat_type_hasher> comdat_type_table (100);
24486 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24488 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24490 /* Don't output duplicate types. */
24491 if (*slot != HTAB_EMPTY_ENTRY)
24492 continue;
24494 /* Add a pointer to the line table for the main compilation unit
24495 so that the debugger can make sense of DW_AT_decl_file
24496 attributes. */
24497 if (debug_info_level >= DINFO_LEVEL_TERSE)
24498 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24499 (!dwarf_split_debug_info
24500 ? debug_line_section_label
24501 : debug_skeleton_line_section_label));
24503 output_comdat_type_unit (ctnode);
24504 *slot = ctnode;
24507 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24508 both the main_cu and all skeleton TUs. Making this call unconditional
24509 would end up either adding a second copy of the AT_pubnames attribute, or
24510 requiring a special case in add_top_level_skeleton_die_attrs. */
24511 if (!dwarf_split_debug_info)
24512 add_AT_pubnames (comp_unit_die ());
24514 if (dwarf_split_debug_info)
24516 int mark;
24517 unsigned char checksum[16];
24518 struct md5_ctx ctx;
24520 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24521 md5_init_ctx (&ctx);
24522 mark = 0;
24523 die_checksum (comp_unit_die (), &ctx, &mark);
24524 unmark_all_dies (comp_unit_die ());
24525 md5_finish_ctx (&ctx, checksum);
24527 /* Use the first 8 bytes of the checksum as the dwo_id,
24528 and add it to both comp-unit DIEs. */
24529 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24530 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24532 /* Add the base offset of the ranges table to the skeleton
24533 comp-unit DIE. */
24534 if (ranges_table_in_use)
24535 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24536 ranges_section_label);
24538 switch_to_section (debug_addr_section);
24539 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24540 output_addr_table ();
24543 /* Output the main compilation unit if non-empty or if .debug_macinfo
24544 or .debug_macro will be emitted. */
24545 output_comp_unit (comp_unit_die (), have_macinfo);
24547 if (dwarf_split_debug_info && info_section_emitted)
24548 output_skeleton_debug_sections (main_comp_unit_die);
24550 /* Output the abbreviation table. */
24551 if (abbrev_die_table_in_use != 1)
24553 switch_to_section (debug_abbrev_section);
24554 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24555 output_abbrev_section ();
24558 /* Output location list section if necessary. */
24559 if (have_location_lists)
24561 /* Output the location lists info. */
24562 switch_to_section (debug_loc_section);
24563 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24564 output_location_lists (comp_unit_die ());
24567 output_pubtables ();
24569 /* Output the address range information if a CU (.debug_info section)
24570 was emitted. We output an empty table even if we had no functions
24571 to put in it. This because the consumer has no way to tell the
24572 difference between an empty table that we omitted and failure to
24573 generate a table that would have contained data. */
24574 if (info_section_emitted)
24576 unsigned long aranges_length = size_of_aranges ();
24578 switch_to_section (debug_aranges_section);
24579 output_aranges (aranges_length);
24582 /* Output ranges section if necessary. */
24583 if (ranges_table_in_use)
24585 switch_to_section (debug_ranges_section);
24586 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24587 output_ranges ();
24590 /* Have to end the macro section. */
24591 if (have_macinfo)
24593 switch_to_section (debug_macinfo_section);
24594 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24595 output_macinfo ();
24596 dw2_asm_output_data (1, 0, "End compilation unit");
24599 /* Output the source line correspondence table. We must do this
24600 even if there is no line information. Otherwise, on an empty
24601 translation unit, we will generate a present, but empty,
24602 .debug_info section. IRIX 6.5 `nm' will then complain when
24603 examining the file. This is done late so that any filenames
24604 used by the debug_info section are marked as 'used'. */
24605 switch_to_section (debug_line_section);
24606 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24607 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24608 output_line_info (false);
24610 if (dwarf_split_debug_info && info_section_emitted)
24612 switch_to_section (debug_skeleton_line_section);
24613 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24614 output_line_info (true);
24617 /* If we emitted any indirect strings, output the string table too. */
24618 if (debug_str_hash || skeleton_debug_str_hash)
24619 output_indirect_strings ();
24622 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24623 within the same process. For use by toplev::finalize. */
24625 void
24626 dwarf2out_c_finalize (void)
24628 last_var_location_insn = NULL;
24629 cached_next_real_insn = NULL;
24630 used_rtx_array = NULL;
24631 incomplete_types = NULL;
24632 decl_scope_table = NULL;
24633 debug_info_section = NULL;
24634 debug_skeleton_info_section = NULL;
24635 debug_abbrev_section = NULL;
24636 debug_skeleton_abbrev_section = NULL;
24637 debug_aranges_section = NULL;
24638 debug_addr_section = NULL;
24639 debug_macinfo_section = NULL;
24640 debug_line_section = NULL;
24641 debug_skeleton_line_section = NULL;
24642 debug_loc_section = NULL;
24643 debug_pubnames_section = NULL;
24644 debug_pubtypes_section = NULL;
24645 debug_str_section = NULL;
24646 debug_str_dwo_section = NULL;
24647 debug_str_offsets_section = NULL;
24648 debug_ranges_section = NULL;
24649 debug_frame_section = NULL;
24650 fde_vec = NULL;
24651 debug_str_hash = NULL;
24652 skeleton_debug_str_hash = NULL;
24653 dw2_string_counter = 0;
24654 have_multiple_function_sections = false;
24655 text_section_used = false;
24656 cold_text_section_used = false;
24657 cold_text_section = NULL;
24658 current_unit_personality = NULL;
24660 deferred_locations_list = NULL;
24662 next_die_offset = 0;
24663 single_comp_unit_die = NULL;
24664 comdat_type_list = NULL;
24665 limbo_die_list = NULL;
24666 deferred_asm_name = NULL;
24667 file_table = NULL;
24668 decl_die_table = NULL;
24669 common_block_die_table = NULL;
24670 decl_loc_table = NULL;
24671 call_arg_locations = NULL;
24672 call_arg_loc_last = NULL;
24673 call_site_count = -1;
24674 tail_call_site_count = -1;
24675 //block_map = NULL;
24676 cached_dw_loc_list_table = NULL;
24677 abbrev_die_table = NULL;
24678 abbrev_die_table_allocated = 0;
24679 abbrev_die_table_in_use = 0;
24680 line_info_label_num = 0;
24681 cur_line_info_table = NULL;
24682 text_section_line_info = NULL;
24683 cold_text_section_line_info = NULL;
24684 separate_line_info = NULL;
24685 info_section_emitted = false;
24686 pubname_table = NULL;
24687 pubtype_table = NULL;
24688 macinfo_table = NULL;
24689 ranges_table = NULL;
24690 ranges_table_allocated = 0;
24691 ranges_table_in_use = 0;
24692 ranges_by_label = 0;
24693 ranges_by_label_allocated = 0;
24694 ranges_by_label_in_use = 0;
24695 have_location_lists = false;
24696 loclabel_num = 0;
24697 poc_label_num = 0;
24698 last_emitted_file = NULL;
24699 label_num = 0;
24700 file_table_last_lookup = NULL;
24701 tmpl_value_parm_die_table = NULL;
24702 generic_type_instances = NULL;
24703 frame_pointer_fb_offset = 0;
24704 frame_pointer_fb_offset_valid = false;
24705 base_types.release ();
24708 #include "gt-dwarf2out.h"