Remove trailing whitespace. Add missing dbxout.c hunk.
[official-gcc.git] / gcc / dwarf2out.c
blobd31cab9901220dd301852f38344f4a6788be3d7c
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "function.h"
68 #include "emit-rtl.h"
69 #include "gimple.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "insn-config.h"
75 #include "reload.h"
76 #include "function.h"
77 #include "output.h"
78 #include "expr.h"
79 #include "except.h"
80 #include "dwarf2.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "ggc.h"
85 #include "md5.h"
86 #include "tm_p.h"
87 #include "diagnostic.h"
88 #include "tree-pretty-print.h"
89 #include "debug.h"
90 #include "target.h"
91 #include "common/common-target.h"
92 #include "langhooks.h"
93 #include "hash-table.h"
94 #include "cgraph.h"
95 #include "input.h"
96 #include "ira.h"
97 #include "lra.h"
98 #include "dumpfile.h"
99 #include "opts.h"
100 #include "tree-dfa.h"
101 #include "gdb/gdb-index.h"
103 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
104 static rtx last_var_location_insn;
105 static rtx cached_next_real_insn;
107 #ifdef VMS_DEBUGGING_INFO
108 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
110 /* Define this macro to be a nonzero value if the directory specifications
111 which are output in the debug info should end with a separator. */
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
113 /* Define this macro to evaluate to a nonzero value if GCC should refrain
114 from generating indirect strings in DWARF2 debug information, for instance
115 if your target is stuck with an old version of GDB that is unable to
116 process them properly or uses VMS Debug. */
117 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
118 #else
119 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
120 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
121 #endif
123 /* ??? Poison these here until it can be done generically. They've been
124 totally replaced in this file; make sure it stays that way. */
125 #undef DWARF2_UNWIND_INFO
126 #undef DWARF2_FRAME_INFO
127 #if (GCC_VERSION >= 3000)
128 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
129 #endif
131 /* The size of the target's pointer type. */
132 #ifndef PTR_SIZE
133 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
134 #endif
136 /* Array of RTXes referenced by the debugging information, which therefore
137 must be kept around forever. */
138 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
140 /* A pointer to the base of a list of incomplete types which might be
141 completed at some later time. incomplete_types_list needs to be a
142 vec<tree, va_gc> *because we want to tell the garbage collector about
143 it. */
144 static GTY(()) vec<tree, va_gc> *incomplete_types;
146 /* A pointer to the base of a table of references to declaration
147 scopes. This table is a display which tracks the nesting
148 of declaration scopes at the current scope and containing
149 scopes. This table is used to find the proper place to
150 define type declaration DIE's. */
151 static GTY(()) vec<tree, va_gc> *decl_scope_table;
153 /* Pointers to various DWARF2 sections. */
154 static GTY(()) section *debug_info_section;
155 static GTY(()) section *debug_skeleton_info_section;
156 static GTY(()) section *debug_abbrev_section;
157 static GTY(()) section *debug_skeleton_abbrev_section;
158 static GTY(()) section *debug_aranges_section;
159 static GTY(()) section *debug_addr_section;
160 static GTY(()) section *debug_macinfo_section;
161 static GTY(()) section *debug_line_section;
162 static GTY(()) section *debug_skeleton_line_section;
163 static GTY(()) section *debug_loc_section;
164 static GTY(()) section *debug_pubnames_section;
165 static GTY(()) section *debug_pubtypes_section;
166 static GTY(()) section *debug_str_section;
167 static GTY(()) section *debug_str_dwo_section;
168 static GTY(()) section *debug_str_offsets_section;
169 static GTY(()) section *debug_ranges_section;
170 static GTY(()) section *debug_frame_section;
172 /* Maximum size (in bytes) of an artificially generated label. */
173 #define MAX_ARTIFICIAL_LABEL_BYTES 30
175 /* According to the (draft) DWARF 3 specification, the initial length
176 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
177 bytes are 0xffffffff, followed by the length stored in the next 8
178 bytes.
180 However, the SGI/MIPS ABI uses an initial length which is equal to
181 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
183 #ifndef DWARF_INITIAL_LENGTH_SIZE
184 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
185 #endif
187 /* Round SIZE up to the nearest BOUNDARY. */
188 #define DWARF_ROUND(SIZE,BOUNDARY) \
189 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
191 /* CIE identifier. */
192 #if HOST_BITS_PER_WIDE_INT >= 64
193 #define DWARF_CIE_ID \
194 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
195 #else
196 #define DWARF_CIE_ID DW_CIE_ID
197 #endif
200 /* A vector for a table that contains frame description
201 information for each routine. */
202 #define NOT_INDEXED (-1U)
203 #define NO_INDEX_ASSIGNED (-2U)
205 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
207 struct GTY(()) indirect_string_node {
208 const char *str;
209 unsigned int refcount;
210 enum dwarf_form form;
211 char *label;
212 unsigned int index;
215 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
217 /* With split_debug_info, both the comp_dir and dwo_name go in the
218 main object file, rather than the dwo, similar to the force_direct
219 parameter elsewhere but with additional complications:
221 1) The string is needed in both the main object file and the dwo.
222 That is, the comp_dir and dwo_name will appear in both places.
224 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
225 DW_FORM_GNU_str_index.
227 3) GCC chooses the form to use late, depending on the size and
228 reference count.
230 Rather than forcing the all debug string handling functions and
231 callers to deal with these complications, simply use a separate,
232 special-cased string table for any attribute that should go in the
233 main object file. This limits the complexity to just the places
234 that need it. */
236 static GTY ((param_is (struct indirect_string_node)))
237 htab_t skeleton_debug_str_hash;
239 static GTY(()) int dw2_string_counter;
241 /* True if the compilation unit places functions in more than one section. */
242 static GTY(()) bool have_multiple_function_sections = false;
244 /* Whether the default text and cold text sections have been used at all. */
246 static GTY(()) bool text_section_used = false;
247 static GTY(()) bool cold_text_section_used = false;
249 /* The default cold text section. */
250 static GTY(()) section *cold_text_section;
252 /* Forward declarations for functions defined in this file. */
254 static char *stripattributes (const char *);
255 static void output_call_frame_info (int);
256 static void dwarf2out_note_section_used (void);
258 /* Personality decl of current unit. Used only when assembler does not support
259 personality CFI. */
260 static GTY(()) rtx current_unit_personality;
262 /* Data and reference forms for relocatable data. */
263 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
264 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
266 #ifndef DEBUG_FRAME_SECTION
267 #define DEBUG_FRAME_SECTION ".debug_frame"
268 #endif
270 #ifndef FUNC_BEGIN_LABEL
271 #define FUNC_BEGIN_LABEL "LFB"
272 #endif
274 #ifndef FUNC_END_LABEL
275 #define FUNC_END_LABEL "LFE"
276 #endif
278 #ifndef PROLOGUE_END_LABEL
279 #define PROLOGUE_END_LABEL "LPE"
280 #endif
282 #ifndef EPILOGUE_BEGIN_LABEL
283 #define EPILOGUE_BEGIN_LABEL "LEB"
284 #endif
286 #ifndef FRAME_BEGIN_LABEL
287 #define FRAME_BEGIN_LABEL "Lframe"
288 #endif
289 #define CIE_AFTER_SIZE_LABEL "LSCIE"
290 #define CIE_END_LABEL "LECIE"
291 #define FDE_LABEL "LSFDE"
292 #define FDE_AFTER_SIZE_LABEL "LASFDE"
293 #define FDE_END_LABEL "LEFDE"
294 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
295 #define LINE_NUMBER_END_LABEL "LELT"
296 #define LN_PROLOG_AS_LABEL "LASLTP"
297 #define LN_PROLOG_END_LABEL "LELTP"
298 #define DIE_LABEL_PREFIX "DW"
300 /* Match the base name of a file to the base name of a compilation unit. */
302 static int
303 matches_main_base (const char *path)
305 /* Cache the last query. */
306 static const char *last_path = NULL;
307 static int last_match = 0;
308 if (path != last_path)
310 const char *base;
311 int length = base_of_path (path, &base);
312 last_path = path;
313 last_match = (length == main_input_baselength
314 && memcmp (base, main_input_basename, length) == 0);
316 return last_match;
319 #ifdef DEBUG_DEBUG_STRUCT
321 static int
322 dump_struct_debug (tree type, enum debug_info_usage usage,
323 enum debug_struct_file criterion, int generic,
324 int matches, int result)
326 /* Find the type name. */
327 tree type_decl = TYPE_STUB_DECL (type);
328 tree t = type_decl;
329 const char *name = 0;
330 if (TREE_CODE (t) == TYPE_DECL)
331 t = DECL_NAME (t);
332 if (t)
333 name = IDENTIFIER_POINTER (t);
335 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
336 criterion,
337 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
338 matches ? "bas" : "hdr",
339 generic ? "gen" : "ord",
340 usage == DINFO_USAGE_DFN ? ";" :
341 usage == DINFO_USAGE_DIR_USE ? "." : "*",
342 result,
343 (void*) type_decl, name);
344 return result;
346 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
347 dump_struct_debug (type, usage, criterion, generic, matches, result)
349 #else
351 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
352 (result)
354 #endif
356 /* Get the number of HOST_WIDE_INTs needed to represent the precision
357 of the number. */
359 static unsigned int
360 get_full_len (const wide_int &op)
362 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
363 / HOST_BITS_PER_WIDE_INT);
366 static bool
367 should_emit_struct_debug (tree type, enum debug_info_usage usage)
369 enum debug_struct_file criterion;
370 tree type_decl;
371 bool generic = lang_hooks.types.generic_p (type);
373 if (generic)
374 criterion = debug_struct_generic[usage];
375 else
376 criterion = debug_struct_ordinary[usage];
378 if (criterion == DINFO_STRUCT_FILE_NONE)
379 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
380 if (criterion == DINFO_STRUCT_FILE_ANY)
381 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
383 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
385 if (type_decl != NULL)
387 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
388 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
390 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
391 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
394 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
397 /* Return a pointer to a copy of the section string name S with all
398 attributes stripped off, and an asterisk prepended (for assemble_name). */
400 static inline char *
401 stripattributes (const char *s)
403 char *stripped = XNEWVEC (char, strlen (s) + 2);
404 char *p = stripped;
406 *p++ = '*';
408 while (*s && *s != ',')
409 *p++ = *s++;
411 *p = '\0';
412 return stripped;
415 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
416 switch to the data section instead, and write out a synthetic start label
417 for collect2 the first time around. */
419 static void
420 switch_to_eh_frame_section (bool back)
422 tree label;
424 #ifdef EH_FRAME_SECTION_NAME
425 if (eh_frame_section == 0)
427 int flags;
429 if (EH_TABLES_CAN_BE_READ_ONLY)
431 int fde_encoding;
432 int per_encoding;
433 int lsda_encoding;
435 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
436 /*global=*/0);
437 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
438 /*global=*/1);
439 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
440 /*global=*/0);
441 flags = ((! flag_pic
442 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
443 && (fde_encoding & 0x70) != DW_EH_PE_aligned
444 && (per_encoding & 0x70) != DW_EH_PE_absptr
445 && (per_encoding & 0x70) != DW_EH_PE_aligned
446 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
447 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
448 ? 0 : SECTION_WRITE);
450 else
451 flags = SECTION_WRITE;
452 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
454 #endif /* EH_FRAME_SECTION_NAME */
456 if (eh_frame_section)
457 switch_to_section (eh_frame_section);
458 else
460 /* We have no special eh_frame section. Put the information in
461 the data section and emit special labels to guide collect2. */
462 switch_to_section (data_section);
464 if (!back)
466 label = get_file_function_name ("F");
467 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
468 targetm.asm_out.globalize_label (asm_out_file,
469 IDENTIFIER_POINTER (label));
470 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
475 /* Switch [BACK] to the eh or debug frame table section, depending on
476 FOR_EH. */
478 static void
479 switch_to_frame_table_section (int for_eh, bool back)
481 if (for_eh)
482 switch_to_eh_frame_section (back);
483 else
485 if (!debug_frame_section)
486 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
487 SECTION_DEBUG, NULL);
488 switch_to_section (debug_frame_section);
492 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
494 enum dw_cfi_oprnd_type
495 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
497 switch (cfi)
499 case DW_CFA_nop:
500 case DW_CFA_GNU_window_save:
501 case DW_CFA_remember_state:
502 case DW_CFA_restore_state:
503 return dw_cfi_oprnd_unused;
505 case DW_CFA_set_loc:
506 case DW_CFA_advance_loc1:
507 case DW_CFA_advance_loc2:
508 case DW_CFA_advance_loc4:
509 case DW_CFA_MIPS_advance_loc8:
510 return dw_cfi_oprnd_addr;
512 case DW_CFA_offset:
513 case DW_CFA_offset_extended:
514 case DW_CFA_def_cfa:
515 case DW_CFA_offset_extended_sf:
516 case DW_CFA_def_cfa_sf:
517 case DW_CFA_restore:
518 case DW_CFA_restore_extended:
519 case DW_CFA_undefined:
520 case DW_CFA_same_value:
521 case DW_CFA_def_cfa_register:
522 case DW_CFA_register:
523 case DW_CFA_expression:
524 return dw_cfi_oprnd_reg_num;
526 case DW_CFA_def_cfa_offset:
527 case DW_CFA_GNU_args_size:
528 case DW_CFA_def_cfa_offset_sf:
529 return dw_cfi_oprnd_offset;
531 case DW_CFA_def_cfa_expression:
532 return dw_cfi_oprnd_loc;
534 default:
535 gcc_unreachable ();
539 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
541 enum dw_cfi_oprnd_type
542 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
544 switch (cfi)
546 case DW_CFA_def_cfa:
547 case DW_CFA_def_cfa_sf:
548 case DW_CFA_offset:
549 case DW_CFA_offset_extended_sf:
550 case DW_CFA_offset_extended:
551 return dw_cfi_oprnd_offset;
553 case DW_CFA_register:
554 return dw_cfi_oprnd_reg_num;
556 case DW_CFA_expression:
557 return dw_cfi_oprnd_loc;
559 default:
560 return dw_cfi_oprnd_unused;
564 /* Output one FDE. */
566 static void
567 output_fde (dw_fde_ref fde, bool for_eh, bool second,
568 char *section_start_label, int fde_encoding, char *augmentation,
569 bool any_lsda_needed, int lsda_encoding)
571 const char *begin, *end;
572 static unsigned int j;
573 char l1[20], l2[20];
575 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
576 /* empty */ 0);
577 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
578 for_eh + j);
579 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
580 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
581 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
582 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
583 " indicating 64-bit DWARF extension");
584 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
585 "FDE Length");
586 ASM_OUTPUT_LABEL (asm_out_file, l1);
588 if (for_eh)
589 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
590 else
591 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
592 debug_frame_section, "FDE CIE offset");
594 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
595 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
597 if (for_eh)
599 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
600 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
601 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
602 "FDE initial location");
603 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
604 end, begin, "FDE address range");
606 else
608 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
609 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
612 if (augmentation[0])
614 if (any_lsda_needed)
616 int size = size_of_encoded_value (lsda_encoding);
618 if (lsda_encoding == DW_EH_PE_aligned)
620 int offset = ( 4 /* Length */
621 + 4 /* CIE offset */
622 + 2 * size_of_encoded_value (fde_encoding)
623 + 1 /* Augmentation size */ );
624 int pad = -offset & (PTR_SIZE - 1);
626 size += pad;
627 gcc_assert (size_of_uleb128 (size) == 1);
630 dw2_asm_output_data_uleb128 (size, "Augmentation size");
632 if (fde->uses_eh_lsda)
634 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
635 fde->funcdef_number);
636 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
637 gen_rtx_SYMBOL_REF (Pmode, l1),
638 false,
639 "Language Specific Data Area");
641 else
643 if (lsda_encoding == DW_EH_PE_aligned)
644 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
645 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
646 "Language Specific Data Area (none)");
649 else
650 dw2_asm_output_data_uleb128 (0, "Augmentation size");
653 /* Loop through the Call Frame Instructions associated with this FDE. */
654 fde->dw_fde_current_label = begin;
656 size_t from, until, i;
658 from = 0;
659 until = vec_safe_length (fde->dw_fde_cfi);
661 if (fde->dw_fde_second_begin == NULL)
663 else if (!second)
664 until = fde->dw_fde_switch_cfi_index;
665 else
666 from = fde->dw_fde_switch_cfi_index;
668 for (i = from; i < until; i++)
669 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
672 /* If we are to emit a ref/link from function bodies to their frame tables,
673 do it now. This is typically performed to make sure that tables
674 associated with functions are dragged with them and not discarded in
675 garbage collecting links. We need to do this on a per function basis to
676 cope with -ffunction-sections. */
678 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
679 /* Switch to the function section, emit the ref to the tables, and
680 switch *back* into the table section. */
681 switch_to_section (function_section (fde->decl));
682 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
683 switch_to_frame_table_section (for_eh, true);
684 #endif
686 /* Pad the FDE out to an address sized boundary. */
687 ASM_OUTPUT_ALIGN (asm_out_file,
688 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
689 ASM_OUTPUT_LABEL (asm_out_file, l2);
691 j += 2;
694 /* Return true if frame description entry FDE is needed for EH. */
696 static bool
697 fde_needed_for_eh_p (dw_fde_ref fde)
699 if (flag_asynchronous_unwind_tables)
700 return true;
702 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
703 return true;
705 if (fde->uses_eh_lsda)
706 return true;
708 /* If exceptions are enabled, we have collected nothrow info. */
709 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
710 return false;
712 return true;
715 /* Output the call frame information used to record information
716 that relates to calculating the frame pointer, and records the
717 location of saved registers. */
719 static void
720 output_call_frame_info (int for_eh)
722 unsigned int i;
723 dw_fde_ref fde;
724 dw_cfi_ref cfi;
725 char l1[20], l2[20], section_start_label[20];
726 bool any_lsda_needed = false;
727 char augmentation[6];
728 int augmentation_size;
729 int fde_encoding = DW_EH_PE_absptr;
730 int per_encoding = DW_EH_PE_absptr;
731 int lsda_encoding = DW_EH_PE_absptr;
732 int return_reg;
733 rtx personality = NULL;
734 int dw_cie_version;
736 /* Don't emit a CIE if there won't be any FDEs. */
737 if (!fde_vec)
738 return;
740 /* Nothing to do if the assembler's doing it all. */
741 if (dwarf2out_do_cfi_asm ())
742 return;
744 /* If we don't have any functions we'll want to unwind out of, don't emit
745 any EH unwind information. If we make FDEs linkonce, we may have to
746 emit an empty label for an FDE that wouldn't otherwise be emitted. We
747 want to avoid having an FDE kept around when the function it refers to
748 is discarded. Example where this matters: a primary function template
749 in C++ requires EH information, an explicit specialization doesn't. */
750 if (for_eh)
752 bool any_eh_needed = false;
754 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
756 if (fde->uses_eh_lsda)
757 any_eh_needed = any_lsda_needed = true;
758 else if (fde_needed_for_eh_p (fde))
759 any_eh_needed = true;
760 else if (TARGET_USES_WEAK_UNWIND_INFO)
761 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
764 if (!any_eh_needed)
765 return;
768 /* We're going to be generating comments, so turn on app. */
769 if (flag_debug_asm)
770 app_enable ();
772 /* Switch to the proper frame section, first time. */
773 switch_to_frame_table_section (for_eh, false);
775 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
776 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
778 /* Output the CIE. */
779 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
780 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
781 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
782 dw2_asm_output_data (4, 0xffffffff,
783 "Initial length escape value indicating 64-bit DWARF extension");
784 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
785 "Length of Common Information Entry");
786 ASM_OUTPUT_LABEL (asm_out_file, l1);
788 /* Now that the CIE pointer is PC-relative for EH,
789 use 0 to identify the CIE. */
790 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
791 (for_eh ? 0 : DWARF_CIE_ID),
792 "CIE Identifier Tag");
794 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
795 use CIE version 1, unless that would produce incorrect results
796 due to overflowing the return register column. */
797 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
798 dw_cie_version = 1;
799 if (return_reg >= 256 || dwarf_version > 2)
800 dw_cie_version = 3;
801 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
803 augmentation[0] = 0;
804 augmentation_size = 0;
806 personality = current_unit_personality;
807 if (for_eh)
809 char *p;
811 /* Augmentation:
812 z Indicates that a uleb128 is present to size the
813 augmentation section.
814 L Indicates the encoding (and thus presence) of
815 an LSDA pointer in the FDE augmentation.
816 R Indicates a non-default pointer encoding for
817 FDE code pointers.
818 P Indicates the presence of an encoding + language
819 personality routine in the CIE augmentation. */
821 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
822 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
823 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
825 p = augmentation + 1;
826 if (personality)
828 *p++ = 'P';
829 augmentation_size += 1 + size_of_encoded_value (per_encoding);
830 assemble_external_libcall (personality);
832 if (any_lsda_needed)
834 *p++ = 'L';
835 augmentation_size += 1;
837 if (fde_encoding != DW_EH_PE_absptr)
839 *p++ = 'R';
840 augmentation_size += 1;
842 if (p > augmentation + 1)
844 augmentation[0] = 'z';
845 *p = '\0';
848 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
849 if (personality && per_encoding == DW_EH_PE_aligned)
851 int offset = ( 4 /* Length */
852 + 4 /* CIE Id */
853 + 1 /* CIE version */
854 + strlen (augmentation) + 1 /* Augmentation */
855 + size_of_uleb128 (1) /* Code alignment */
856 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
857 + 1 /* RA column */
858 + 1 /* Augmentation size */
859 + 1 /* Personality encoding */ );
860 int pad = -offset & (PTR_SIZE - 1);
862 augmentation_size += pad;
864 /* Augmentations should be small, so there's scarce need to
865 iterate for a solution. Die if we exceed one uleb128 byte. */
866 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
870 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
871 if (dw_cie_version >= 4)
873 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
874 dw2_asm_output_data (1, 0, "CIE Segment Size");
876 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
877 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
878 "CIE Data Alignment Factor");
880 if (dw_cie_version == 1)
881 dw2_asm_output_data (1, return_reg, "CIE RA Column");
882 else
883 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
885 if (augmentation[0])
887 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
888 if (personality)
890 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
891 eh_data_format_name (per_encoding));
892 dw2_asm_output_encoded_addr_rtx (per_encoding,
893 personality,
894 true, NULL);
897 if (any_lsda_needed)
898 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
899 eh_data_format_name (lsda_encoding));
901 if (fde_encoding != DW_EH_PE_absptr)
902 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
903 eh_data_format_name (fde_encoding));
906 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
907 output_cfi (cfi, NULL, for_eh);
909 /* Pad the CIE out to an address sized boundary. */
910 ASM_OUTPUT_ALIGN (asm_out_file,
911 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
912 ASM_OUTPUT_LABEL (asm_out_file, l2);
914 /* Loop through all of the FDE's. */
915 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
917 unsigned int k;
919 /* Don't emit EH unwind info for leaf functions that don't need it. */
920 if (for_eh && !fde_needed_for_eh_p (fde))
921 continue;
923 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
924 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
925 augmentation, any_lsda_needed, lsda_encoding);
928 if (for_eh && targetm.terminate_dw2_eh_frame_info)
929 dw2_asm_output_data (4, 0, "End of Table");
931 /* Turn off app to make assembly quicker. */
932 if (flag_debug_asm)
933 app_disable ();
936 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
938 static void
939 dwarf2out_do_cfi_startproc (bool second)
941 int enc;
942 rtx ref;
943 rtx personality = get_personality_function (current_function_decl);
945 fprintf (asm_out_file, "\t.cfi_startproc\n");
947 if (personality)
949 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
950 ref = personality;
952 /* ??? The GAS support isn't entirely consistent. We have to
953 handle indirect support ourselves, but PC-relative is done
954 in the assembler. Further, the assembler can't handle any
955 of the weirder relocation types. */
956 if (enc & DW_EH_PE_indirect)
957 ref = dw2_force_const_mem (ref, true);
959 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
960 output_addr_const (asm_out_file, ref);
961 fputc ('\n', asm_out_file);
964 if (crtl->uses_eh_lsda)
966 char lab[20];
968 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
969 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
970 current_function_funcdef_no);
971 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
972 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
974 if (enc & DW_EH_PE_indirect)
975 ref = dw2_force_const_mem (ref, true);
977 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
978 output_addr_const (asm_out_file, ref);
979 fputc ('\n', asm_out_file);
983 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
984 this allocation may be done before pass_final. */
986 dw_fde_ref
987 dwarf2out_alloc_current_fde (void)
989 dw_fde_ref fde;
991 fde = ggc_alloc_cleared_dw_fde_node ();
992 fde->decl = current_function_decl;
993 fde->funcdef_number = current_function_funcdef_no;
994 fde->fde_index = vec_safe_length (fde_vec);
995 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
996 fde->uses_eh_lsda = crtl->uses_eh_lsda;
997 fde->nothrow = crtl->nothrow;
998 fde->drap_reg = INVALID_REGNUM;
999 fde->vdrap_reg = INVALID_REGNUM;
1001 /* Record the FDE associated with this function. */
1002 cfun->fde = fde;
1003 vec_safe_push (fde_vec, fde);
1005 return fde;
1008 /* Output a marker (i.e. a label) for the beginning of a function, before
1009 the prologue. */
1011 void
1012 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1013 const char *file ATTRIBUTE_UNUSED)
1015 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1016 char * dup_label;
1017 dw_fde_ref fde;
1018 section *fnsec;
1019 bool do_frame;
1021 current_function_func_begin_label = NULL;
1023 do_frame = dwarf2out_do_frame ();
1025 /* ??? current_function_func_begin_label is also used by except.c for
1026 call-site information. We must emit this label if it might be used. */
1027 if (!do_frame
1028 && (!flag_exceptions
1029 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1030 return;
1032 fnsec = function_section (current_function_decl);
1033 switch_to_section (fnsec);
1034 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1035 current_function_funcdef_no);
1036 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1037 current_function_funcdef_no);
1038 dup_label = xstrdup (label);
1039 current_function_func_begin_label = dup_label;
1041 /* We can elide the fde allocation if we're not emitting debug info. */
1042 if (!do_frame)
1043 return;
1045 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1046 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1047 would include pass_dwarf2_frame. If we've not created the FDE yet,
1048 do so now. */
1049 fde = cfun->fde;
1050 if (fde == NULL)
1051 fde = dwarf2out_alloc_current_fde ();
1053 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1054 fde->dw_fde_begin = dup_label;
1055 fde->dw_fde_current_label = dup_label;
1056 fde->in_std_section = (fnsec == text_section
1057 || (cold_text_section && fnsec == cold_text_section));
1059 /* We only want to output line number information for the genuine dwarf2
1060 prologue case, not the eh frame case. */
1061 #ifdef DWARF2_DEBUGGING_INFO
1062 if (file)
1063 dwarf2out_source_line (line, file, 0, true);
1064 #endif
1066 if (dwarf2out_do_cfi_asm ())
1067 dwarf2out_do_cfi_startproc (false);
1068 else
1070 rtx personality = get_personality_function (current_function_decl);
1071 if (!current_unit_personality)
1072 current_unit_personality = personality;
1074 /* We cannot keep a current personality per function as without CFI
1075 asm, at the point where we emit the CFI data, there is no current
1076 function anymore. */
1077 if (personality && current_unit_personality != personality)
1078 sorry ("multiple EH personalities are supported only with assemblers "
1079 "supporting .cfi_personality directive");
1083 /* Output a marker (i.e. a label) for the end of the generated code
1084 for a function prologue. This gets called *after* the prologue code has
1085 been generated. */
1087 void
1088 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1089 const char *file ATTRIBUTE_UNUSED)
1091 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1093 /* Output a label to mark the endpoint of the code generated for this
1094 function. */
1095 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1096 current_function_funcdef_no);
1097 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1098 current_function_funcdef_no);
1099 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1102 /* Output a marker (i.e. a label) for the beginning of the generated code
1103 for a function epilogue. This gets called *before* the prologue code has
1104 been generated. */
1106 void
1107 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1108 const char *file ATTRIBUTE_UNUSED)
1110 dw_fde_ref fde = cfun->fde;
1111 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1113 if (fde->dw_fde_vms_begin_epilogue)
1114 return;
1116 /* Output a label to mark the endpoint of the code generated for this
1117 function. */
1118 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1119 current_function_funcdef_no);
1120 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1121 current_function_funcdef_no);
1122 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1125 /* Output a marker (i.e. a label) for the absolute end of the generated code
1126 for a function definition. This gets called *after* the epilogue code has
1127 been generated. */
1129 void
1130 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1131 const char *file ATTRIBUTE_UNUSED)
1133 dw_fde_ref fde;
1134 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1136 last_var_location_insn = NULL_RTX;
1137 cached_next_real_insn = NULL_RTX;
1139 if (dwarf2out_do_cfi_asm ())
1140 fprintf (asm_out_file, "\t.cfi_endproc\n");
1142 /* Output a label to mark the endpoint of the code generated for this
1143 function. */
1144 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1145 current_function_funcdef_no);
1146 ASM_OUTPUT_LABEL (asm_out_file, label);
1147 fde = cfun->fde;
1148 gcc_assert (fde != NULL);
1149 if (fde->dw_fde_second_begin == NULL)
1150 fde->dw_fde_end = xstrdup (label);
1153 void
1154 dwarf2out_frame_finish (void)
1156 /* Output call frame information. */
1157 if (targetm.debug_unwind_info () == UI_DWARF2)
1158 output_call_frame_info (0);
1160 /* Output another copy for the unwinder. */
1161 if ((flag_unwind_tables || flag_exceptions)
1162 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1163 output_call_frame_info (1);
1166 /* Note that the current function section is being used for code. */
1168 static void
1169 dwarf2out_note_section_used (void)
1171 section *sec = current_function_section ();
1172 if (sec == text_section)
1173 text_section_used = true;
1174 else if (sec == cold_text_section)
1175 cold_text_section_used = true;
1178 static void var_location_switch_text_section (void);
1179 static void set_cur_line_info_table (section *);
1181 void
1182 dwarf2out_switch_text_section (void)
1184 section *sect;
1185 dw_fde_ref fde = cfun->fde;
1187 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1189 if (!in_cold_section_p)
1191 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1192 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1193 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1195 else
1197 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1198 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1199 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1201 have_multiple_function_sections = true;
1203 /* There is no need to mark used sections when not debugging. */
1204 if (cold_text_section != NULL)
1205 dwarf2out_note_section_used ();
1207 if (dwarf2out_do_cfi_asm ())
1208 fprintf (asm_out_file, "\t.cfi_endproc\n");
1210 /* Now do the real section switch. */
1211 sect = current_function_section ();
1212 switch_to_section (sect);
1214 fde->second_in_std_section
1215 = (sect == text_section
1216 || (cold_text_section && sect == cold_text_section));
1218 if (dwarf2out_do_cfi_asm ())
1219 dwarf2out_do_cfi_startproc (true);
1221 var_location_switch_text_section ();
1223 if (cold_text_section != NULL)
1224 set_cur_line_info_table (sect);
1227 /* And now, the subset of the debugging information support code necessary
1228 for emitting location expressions. */
1230 /* Data about a single source file. */
1231 struct GTY(()) dwarf_file_data {
1232 const char * filename;
1233 int emitted_number;
1236 typedef struct GTY(()) deferred_locations_struct
1238 tree variable;
1239 dw_die_ref die;
1240 } deferred_locations;
1243 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1246 /* Describe an entry into the .debug_addr section. */
1248 enum ate_kind {
1249 ate_kind_rtx,
1250 ate_kind_rtx_dtprel,
1251 ate_kind_label
1254 typedef struct GTY(()) addr_table_entry_struct {
1255 enum ate_kind kind;
1256 unsigned int refcount;
1257 unsigned int index;
1258 union addr_table_entry_struct_union
1260 rtx GTY ((tag ("0"))) rtl;
1261 char * GTY ((tag ("1"))) label;
1263 GTY ((desc ("%1.kind"))) addr;
1265 addr_table_entry;
1267 /* Location lists are ranges + location descriptions for that range,
1268 so you can track variables that are in different places over
1269 their entire life. */
1270 typedef struct GTY(()) dw_loc_list_struct {
1271 dw_loc_list_ref dw_loc_next;
1272 const char *begin; /* Label and addr_entry for start of range */
1273 addr_table_entry *begin_entry;
1274 const char *end; /* Label for end of range */
1275 char *ll_symbol; /* Label for beginning of location list.
1276 Only on head of list */
1277 const char *section; /* Section this loclist is relative to */
1278 dw_loc_descr_ref expr;
1279 hashval_t hash;
1280 /* True if all addresses in this and subsequent lists are known to be
1281 resolved. */
1282 bool resolved_addr;
1283 /* True if this list has been replaced by dw_loc_next. */
1284 bool replaced;
1285 bool emitted;
1286 /* True if the range should be emitted even if begin and end
1287 are the same. */
1288 bool force;
1289 } dw_loc_list_node;
1291 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1293 /* Convert a DWARF stack opcode into its string name. */
1295 static const char *
1296 dwarf_stack_op_name (unsigned int op)
1298 const char *name = get_DW_OP_name (op);
1300 if (name != NULL)
1301 return name;
1303 return "OP_<unknown>";
1306 /* Return a pointer to a newly allocated location description. Location
1307 descriptions are simple expression terms that can be strung
1308 together to form more complicated location (address) descriptions. */
1310 static inline dw_loc_descr_ref
1311 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1312 unsigned HOST_WIDE_INT oprnd2)
1314 dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1316 descr->dw_loc_opc = op;
1317 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1318 descr->dw_loc_oprnd1.val_entry = NULL;
1319 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1320 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1321 descr->dw_loc_oprnd2.val_entry = NULL;
1322 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1324 return descr;
1327 /* Return a pointer to a newly allocated location description for
1328 REG and OFFSET. */
1330 static inline dw_loc_descr_ref
1331 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1333 if (reg <= 31)
1334 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1335 offset, 0);
1336 else
1337 return new_loc_descr (DW_OP_bregx, reg, offset);
1340 /* Add a location description term to a location description expression. */
1342 static inline void
1343 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1345 dw_loc_descr_ref *d;
1347 /* Find the end of the chain. */
1348 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1351 *d = descr;
1354 /* Compare two location operands for exact equality. */
1356 static bool
1357 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1359 if (a->val_class != b->val_class)
1360 return false;
1361 switch (a->val_class)
1363 case dw_val_class_none:
1364 return true;
1365 case dw_val_class_addr:
1366 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1368 case dw_val_class_offset:
1369 case dw_val_class_unsigned_const:
1370 case dw_val_class_const:
1371 case dw_val_class_range_list:
1372 case dw_val_class_lineptr:
1373 case dw_val_class_macptr:
1374 /* These are all HOST_WIDE_INT, signed or unsigned. */
1375 return a->v.val_unsigned == b->v.val_unsigned;
1377 case dw_val_class_loc:
1378 return a->v.val_loc == b->v.val_loc;
1379 case dw_val_class_loc_list:
1380 return a->v.val_loc_list == b->v.val_loc_list;
1381 case dw_val_class_die_ref:
1382 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1383 case dw_val_class_fde_ref:
1384 return a->v.val_fde_index == b->v.val_fde_index;
1385 case dw_val_class_lbl_id:
1386 case dw_val_class_high_pc:
1387 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1388 case dw_val_class_str:
1389 return a->v.val_str == b->v.val_str;
1390 case dw_val_class_flag:
1391 return a->v.val_flag == b->v.val_flag;
1392 case dw_val_class_file:
1393 return a->v.val_file == b->v.val_file;
1394 case dw_val_class_decl_ref:
1395 return a->v.val_decl_ref == b->v.val_decl_ref;
1397 case dw_val_class_const_double:
1398 return (a->v.val_double.high == b->v.val_double.high
1399 && a->v.val_double.low == b->v.val_double.low);
1401 case dw_val_class_wide_int:
1402 return *a->v.val_wide == *b->v.val_wide;
1404 case dw_val_class_vec:
1406 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1407 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1409 return (a_len == b_len
1410 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1413 case dw_val_class_data8:
1414 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1416 case dw_val_class_vms_delta:
1417 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1418 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1420 gcc_unreachable ();
1423 /* Compare two location atoms for exact equality. */
1425 static bool
1426 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1428 if (a->dw_loc_opc != b->dw_loc_opc)
1429 return false;
1431 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1432 address size, but since we always allocate cleared storage it
1433 should be zero for other types of locations. */
1434 if (a->dtprel != b->dtprel)
1435 return false;
1437 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1438 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1441 /* Compare two complete location expressions for exact equality. */
1443 bool
1444 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1446 while (1)
1448 if (a == b)
1449 return true;
1450 if (a == NULL || b == NULL)
1451 return false;
1452 if (!loc_descr_equal_p_1 (a, b))
1453 return false;
1455 a = a->dw_loc_next;
1456 b = b->dw_loc_next;
1461 /* Add a constant OFFSET to a location expression. */
1463 static void
1464 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1466 dw_loc_descr_ref loc;
1467 HOST_WIDE_INT *p;
1469 gcc_assert (*list_head != NULL);
1471 if (!offset)
1472 return;
1474 /* Find the end of the chain. */
1475 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1478 p = NULL;
1479 if (loc->dw_loc_opc == DW_OP_fbreg
1480 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1481 p = &loc->dw_loc_oprnd1.v.val_int;
1482 else if (loc->dw_loc_opc == DW_OP_bregx)
1483 p = &loc->dw_loc_oprnd2.v.val_int;
1485 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1486 offset. Don't optimize if an signed integer overflow would happen. */
1487 if (p != NULL
1488 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1489 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1490 *p += offset;
1492 else if (offset > 0)
1493 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1495 else
1497 loc->dw_loc_next = int_loc_descriptor (-offset);
1498 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1502 /* Add a constant OFFSET to a location list. */
1504 static void
1505 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1507 dw_loc_list_ref d;
1508 for (d = list_head; d != NULL; d = d->dw_loc_next)
1509 loc_descr_plus_const (&d->expr, offset);
1512 #define DWARF_REF_SIZE \
1513 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1515 static unsigned long int get_base_type_offset (dw_die_ref);
1517 /* Return the size of a location descriptor. */
1519 static unsigned long
1520 size_of_loc_descr (dw_loc_descr_ref loc)
1522 unsigned long size = 1;
1524 switch (loc->dw_loc_opc)
1526 case DW_OP_addr:
1527 size += DWARF2_ADDR_SIZE;
1528 break;
1529 case DW_OP_GNU_addr_index:
1530 case DW_OP_GNU_const_index:
1531 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1532 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1533 break;
1534 case DW_OP_const1u:
1535 case DW_OP_const1s:
1536 size += 1;
1537 break;
1538 case DW_OP_const2u:
1539 case DW_OP_const2s:
1540 size += 2;
1541 break;
1542 case DW_OP_const4u:
1543 case DW_OP_const4s:
1544 size += 4;
1545 break;
1546 case DW_OP_const8u:
1547 case DW_OP_const8s:
1548 size += 8;
1549 break;
1550 case DW_OP_constu:
1551 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1552 break;
1553 case DW_OP_consts:
1554 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1555 break;
1556 case DW_OP_pick:
1557 size += 1;
1558 break;
1559 case DW_OP_plus_uconst:
1560 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1561 break;
1562 case DW_OP_skip:
1563 case DW_OP_bra:
1564 size += 2;
1565 break;
1566 case DW_OP_breg0:
1567 case DW_OP_breg1:
1568 case DW_OP_breg2:
1569 case DW_OP_breg3:
1570 case DW_OP_breg4:
1571 case DW_OP_breg5:
1572 case DW_OP_breg6:
1573 case DW_OP_breg7:
1574 case DW_OP_breg8:
1575 case DW_OP_breg9:
1576 case DW_OP_breg10:
1577 case DW_OP_breg11:
1578 case DW_OP_breg12:
1579 case DW_OP_breg13:
1580 case DW_OP_breg14:
1581 case DW_OP_breg15:
1582 case DW_OP_breg16:
1583 case DW_OP_breg17:
1584 case DW_OP_breg18:
1585 case DW_OP_breg19:
1586 case DW_OP_breg20:
1587 case DW_OP_breg21:
1588 case DW_OP_breg22:
1589 case DW_OP_breg23:
1590 case DW_OP_breg24:
1591 case DW_OP_breg25:
1592 case DW_OP_breg26:
1593 case DW_OP_breg27:
1594 case DW_OP_breg28:
1595 case DW_OP_breg29:
1596 case DW_OP_breg30:
1597 case DW_OP_breg31:
1598 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1599 break;
1600 case DW_OP_regx:
1601 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1602 break;
1603 case DW_OP_fbreg:
1604 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1605 break;
1606 case DW_OP_bregx:
1607 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1608 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1609 break;
1610 case DW_OP_piece:
1611 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1612 break;
1613 case DW_OP_bit_piece:
1614 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1615 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1616 break;
1617 case DW_OP_deref_size:
1618 case DW_OP_xderef_size:
1619 size += 1;
1620 break;
1621 case DW_OP_call2:
1622 size += 2;
1623 break;
1624 case DW_OP_call4:
1625 size += 4;
1626 break;
1627 case DW_OP_call_ref:
1628 size += DWARF_REF_SIZE;
1629 break;
1630 case DW_OP_implicit_value:
1631 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1632 + loc->dw_loc_oprnd1.v.val_unsigned;
1633 break;
1634 case DW_OP_GNU_implicit_pointer:
1635 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1636 break;
1637 case DW_OP_GNU_entry_value:
1639 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1640 size += size_of_uleb128 (op_size) + op_size;
1641 break;
1643 case DW_OP_GNU_const_type:
1645 unsigned long o
1646 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1647 size += size_of_uleb128 (o) + 1;
1648 switch (loc->dw_loc_oprnd2.val_class)
1650 case dw_val_class_vec:
1651 size += loc->dw_loc_oprnd2.v.val_vec.length
1652 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1653 break;
1654 case dw_val_class_const:
1655 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1656 break;
1657 case dw_val_class_const_double:
1658 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1659 break;
1660 case dw_val_class_wide_int:
1661 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1662 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1663 break;
1664 default:
1665 gcc_unreachable ();
1667 break;
1669 case DW_OP_GNU_regval_type:
1671 unsigned long o
1672 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1673 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1674 + size_of_uleb128 (o);
1676 break;
1677 case DW_OP_GNU_deref_type:
1679 unsigned long o
1680 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1681 size += 1 + size_of_uleb128 (o);
1683 break;
1684 case DW_OP_GNU_convert:
1685 case DW_OP_GNU_reinterpret:
1686 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1687 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1688 else
1690 unsigned long o
1691 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1692 size += size_of_uleb128 (o);
1694 break;
1695 case DW_OP_GNU_parameter_ref:
1696 size += 4;
1697 break;
1698 default:
1699 break;
1702 return size;
1705 /* Return the size of a series of location descriptors. */
1707 unsigned long
1708 size_of_locs (dw_loc_descr_ref loc)
1710 dw_loc_descr_ref l;
1711 unsigned long size;
1713 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1714 field, to avoid writing to a PCH file. */
1715 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1717 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1718 break;
1719 size += size_of_loc_descr (l);
1721 if (! l)
1722 return size;
1724 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1726 l->dw_loc_addr = size;
1727 size += size_of_loc_descr (l);
1730 return size;
1733 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1734 static void get_ref_die_offset_label (char *, dw_die_ref);
1735 static unsigned long int get_ref_die_offset (dw_die_ref);
1737 /* Output location description stack opcode's operands (if any).
1738 The for_eh_or_skip parameter controls whether register numbers are
1739 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1740 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1741 info). This should be suppressed for the cases that have not been converted
1742 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1744 static void
1745 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1747 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1748 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1750 switch (loc->dw_loc_opc)
1752 #ifdef DWARF2_DEBUGGING_INFO
1753 case DW_OP_const2u:
1754 case DW_OP_const2s:
1755 dw2_asm_output_data (2, val1->v.val_int, NULL);
1756 break;
1757 case DW_OP_const4u:
1758 if (loc->dtprel)
1760 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1761 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1762 val1->v.val_addr);
1763 fputc ('\n', asm_out_file);
1764 break;
1766 /* FALLTHRU */
1767 case DW_OP_const4s:
1768 dw2_asm_output_data (4, val1->v.val_int, NULL);
1769 break;
1770 case DW_OP_const8u:
1771 if (loc->dtprel)
1773 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1774 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1775 val1->v.val_addr);
1776 fputc ('\n', asm_out_file);
1777 break;
1779 /* FALLTHRU */
1780 case DW_OP_const8s:
1781 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1782 dw2_asm_output_data (8, val1->v.val_int, NULL);
1783 break;
1784 case DW_OP_skip:
1785 case DW_OP_bra:
1787 int offset;
1789 gcc_assert (val1->val_class == dw_val_class_loc);
1790 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1792 dw2_asm_output_data (2, offset, NULL);
1794 break;
1795 case DW_OP_implicit_value:
1796 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1797 switch (val2->val_class)
1799 case dw_val_class_const:
1800 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1801 break;
1802 case dw_val_class_vec:
1804 unsigned int elt_size = val2->v.val_vec.elt_size;
1805 unsigned int len = val2->v.val_vec.length;
1806 unsigned int i;
1807 unsigned char *p;
1809 if (elt_size > sizeof (HOST_WIDE_INT))
1811 elt_size /= 2;
1812 len *= 2;
1814 for (i = 0, p = val2->v.val_vec.array;
1815 i < len;
1816 i++, p += elt_size)
1817 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1818 "fp or vector constant word %u", i);
1820 break;
1821 case dw_val_class_const_double:
1823 unsigned HOST_WIDE_INT first, second;
1825 if (WORDS_BIG_ENDIAN)
1827 first = val2->v.val_double.high;
1828 second = val2->v.val_double.low;
1830 else
1832 first = val2->v.val_double.low;
1833 second = val2->v.val_double.high;
1835 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1836 first, NULL);
1837 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1838 second, NULL);
1840 break;
1841 case dw_val_class_wide_int:
1843 int i;
1844 int len = get_full_len (*val2->v.val_wide);
1845 if (WORDS_BIG_ENDIAN)
1846 for (i = len; i >= 0; --i)
1847 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1848 val2->v.val_wide->elt (i), NULL);
1849 else
1850 for (i = 0; i < len; ++i)
1851 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1852 val2->v.val_wide->elt (i), NULL);
1854 break;
1855 case dw_val_class_addr:
1856 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1857 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1858 break;
1859 default:
1860 gcc_unreachable ();
1862 break;
1863 #else
1864 case DW_OP_const2u:
1865 case DW_OP_const2s:
1866 case DW_OP_const4u:
1867 case DW_OP_const4s:
1868 case DW_OP_const8u:
1869 case DW_OP_const8s:
1870 case DW_OP_skip:
1871 case DW_OP_bra:
1872 case DW_OP_implicit_value:
1873 /* We currently don't make any attempt to make sure these are
1874 aligned properly like we do for the main unwind info, so
1875 don't support emitting things larger than a byte if we're
1876 only doing unwinding. */
1877 gcc_unreachable ();
1878 #endif
1879 case DW_OP_const1u:
1880 case DW_OP_const1s:
1881 dw2_asm_output_data (1, val1->v.val_int, NULL);
1882 break;
1883 case DW_OP_constu:
1884 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1885 break;
1886 case DW_OP_consts:
1887 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1888 break;
1889 case DW_OP_pick:
1890 dw2_asm_output_data (1, val1->v.val_int, NULL);
1891 break;
1892 case DW_OP_plus_uconst:
1893 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1894 break;
1895 case DW_OP_breg0:
1896 case DW_OP_breg1:
1897 case DW_OP_breg2:
1898 case DW_OP_breg3:
1899 case DW_OP_breg4:
1900 case DW_OP_breg5:
1901 case DW_OP_breg6:
1902 case DW_OP_breg7:
1903 case DW_OP_breg8:
1904 case DW_OP_breg9:
1905 case DW_OP_breg10:
1906 case DW_OP_breg11:
1907 case DW_OP_breg12:
1908 case DW_OP_breg13:
1909 case DW_OP_breg14:
1910 case DW_OP_breg15:
1911 case DW_OP_breg16:
1912 case DW_OP_breg17:
1913 case DW_OP_breg18:
1914 case DW_OP_breg19:
1915 case DW_OP_breg20:
1916 case DW_OP_breg21:
1917 case DW_OP_breg22:
1918 case DW_OP_breg23:
1919 case DW_OP_breg24:
1920 case DW_OP_breg25:
1921 case DW_OP_breg26:
1922 case DW_OP_breg27:
1923 case DW_OP_breg28:
1924 case DW_OP_breg29:
1925 case DW_OP_breg30:
1926 case DW_OP_breg31:
1927 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1928 break;
1929 case DW_OP_regx:
1931 unsigned r = val1->v.val_unsigned;
1932 if (for_eh_or_skip >= 0)
1933 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1934 gcc_assert (size_of_uleb128 (r)
1935 == size_of_uleb128 (val1->v.val_unsigned));
1936 dw2_asm_output_data_uleb128 (r, NULL);
1938 break;
1939 case DW_OP_fbreg:
1940 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1941 break;
1942 case DW_OP_bregx:
1944 unsigned r = val1->v.val_unsigned;
1945 if (for_eh_or_skip >= 0)
1946 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1947 gcc_assert (size_of_uleb128 (r)
1948 == size_of_uleb128 (val1->v.val_unsigned));
1949 dw2_asm_output_data_uleb128 (r, NULL);
1950 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1952 break;
1953 case DW_OP_piece:
1954 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1955 break;
1956 case DW_OP_bit_piece:
1957 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1958 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1959 break;
1960 case DW_OP_deref_size:
1961 case DW_OP_xderef_size:
1962 dw2_asm_output_data (1, val1->v.val_int, NULL);
1963 break;
1965 case DW_OP_addr:
1966 if (loc->dtprel)
1968 if (targetm.asm_out.output_dwarf_dtprel)
1970 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1971 DWARF2_ADDR_SIZE,
1972 val1->v.val_addr);
1973 fputc ('\n', asm_out_file);
1975 else
1976 gcc_unreachable ();
1978 else
1980 #ifdef DWARF2_DEBUGGING_INFO
1981 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1982 #else
1983 gcc_unreachable ();
1984 #endif
1986 break;
1988 case DW_OP_GNU_addr_index:
1989 case DW_OP_GNU_const_index:
1990 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1991 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1992 "(index into .debug_addr)");
1993 break;
1995 case DW_OP_GNU_implicit_pointer:
1997 char label[MAX_ARTIFICIAL_LABEL_BYTES
1998 + HOST_BITS_PER_WIDE_INT / 2 + 2];
1999 gcc_assert (val1->val_class == dw_val_class_die_ref);
2000 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2001 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2002 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2004 break;
2006 case DW_OP_GNU_entry_value:
2007 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2008 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2009 break;
2011 case DW_OP_GNU_const_type:
2013 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2014 gcc_assert (o);
2015 dw2_asm_output_data_uleb128 (o, NULL);
2016 switch (val2->val_class)
2018 case dw_val_class_const:
2019 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2020 dw2_asm_output_data (1, l, NULL);
2021 dw2_asm_output_data (l, val2->v.val_int, NULL);
2022 break;
2023 case dw_val_class_vec:
2025 unsigned int elt_size = val2->v.val_vec.elt_size;
2026 unsigned int len = val2->v.val_vec.length;
2027 unsigned int i;
2028 unsigned char *p;
2030 l = len * elt_size;
2031 dw2_asm_output_data (1, l, NULL);
2032 if (elt_size > sizeof (HOST_WIDE_INT))
2034 elt_size /= 2;
2035 len *= 2;
2037 for (i = 0, p = val2->v.val_vec.array;
2038 i < len;
2039 i++, p += elt_size)
2040 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2041 "fp or vector constant word %u", i);
2043 break;
2044 case dw_val_class_const_double:
2046 unsigned HOST_WIDE_INT first, second;
2047 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2049 dw2_asm_output_data (1, 2 * l, NULL);
2050 if (WORDS_BIG_ENDIAN)
2052 first = val2->v.val_double.high;
2053 second = val2->v.val_double.low;
2055 else
2057 first = val2->v.val_double.low;
2058 second = val2->v.val_double.high;
2060 dw2_asm_output_data (l, first, NULL);
2061 dw2_asm_output_data (l, second, NULL);
2063 break;
2064 case dw_val_class_wide_int:
2066 int i;
2067 int len = get_full_len (*val2->v.val_wide);
2068 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2070 dw2_asm_output_data (1, len * l, NULL);
2071 if (WORDS_BIG_ENDIAN)
2072 for (i = len; i >= 0; --i)
2073 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2074 else
2075 for (i = 0; i < len; ++i)
2076 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2078 break;
2079 default:
2080 gcc_unreachable ();
2083 break;
2084 case DW_OP_GNU_regval_type:
2086 unsigned r = val1->v.val_unsigned;
2087 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2088 gcc_assert (o);
2089 if (for_eh_or_skip >= 0)
2091 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2092 gcc_assert (size_of_uleb128 (r)
2093 == size_of_uleb128 (val1->v.val_unsigned));
2095 dw2_asm_output_data_uleb128 (r, NULL);
2096 dw2_asm_output_data_uleb128 (o, NULL);
2098 break;
2099 case DW_OP_GNU_deref_type:
2101 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2102 gcc_assert (o);
2103 dw2_asm_output_data (1, val1->v.val_int, NULL);
2104 dw2_asm_output_data_uleb128 (o, NULL);
2106 break;
2107 case DW_OP_GNU_convert:
2108 case DW_OP_GNU_reinterpret:
2109 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2110 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2111 else
2113 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2114 gcc_assert (o);
2115 dw2_asm_output_data_uleb128 (o, NULL);
2117 break;
2119 case DW_OP_GNU_parameter_ref:
2121 unsigned long o;
2122 gcc_assert (val1->val_class == dw_val_class_die_ref);
2123 o = get_ref_die_offset (val1->v.val_die_ref.die);
2124 dw2_asm_output_data (4, o, NULL);
2126 break;
2128 default:
2129 /* Other codes have no operands. */
2130 break;
2134 /* Output a sequence of location operations.
2135 The for_eh_or_skip parameter controls whether register numbers are
2136 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2137 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2138 info). This should be suppressed for the cases that have not been converted
2139 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2141 void
2142 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2144 for (; loc != NULL; loc = loc->dw_loc_next)
2146 enum dwarf_location_atom opc = loc->dw_loc_opc;
2147 /* Output the opcode. */
2148 if (for_eh_or_skip >= 0
2149 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2151 unsigned r = (opc - DW_OP_breg0);
2152 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2153 gcc_assert (r <= 31);
2154 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2156 else if (for_eh_or_skip >= 0
2157 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2159 unsigned r = (opc - DW_OP_reg0);
2160 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2161 gcc_assert (r <= 31);
2162 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2165 dw2_asm_output_data (1, opc,
2166 "%s", dwarf_stack_op_name (opc));
2168 /* Output the operand(s) (if any). */
2169 output_loc_operands (loc, for_eh_or_skip);
2173 /* Output location description stack opcode's operands (if any).
2174 The output is single bytes on a line, suitable for .cfi_escape. */
2176 static void
2177 output_loc_operands_raw (dw_loc_descr_ref loc)
2179 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2180 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2182 switch (loc->dw_loc_opc)
2184 case DW_OP_addr:
2185 case DW_OP_GNU_addr_index:
2186 case DW_OP_GNU_const_index:
2187 case DW_OP_implicit_value:
2188 /* We cannot output addresses in .cfi_escape, only bytes. */
2189 gcc_unreachable ();
2191 case DW_OP_const1u:
2192 case DW_OP_const1s:
2193 case DW_OP_pick:
2194 case DW_OP_deref_size:
2195 case DW_OP_xderef_size:
2196 fputc (',', asm_out_file);
2197 dw2_asm_output_data_raw (1, val1->v.val_int);
2198 break;
2200 case DW_OP_const2u:
2201 case DW_OP_const2s:
2202 fputc (',', asm_out_file);
2203 dw2_asm_output_data_raw (2, val1->v.val_int);
2204 break;
2206 case DW_OP_const4u:
2207 case DW_OP_const4s:
2208 fputc (',', asm_out_file);
2209 dw2_asm_output_data_raw (4, val1->v.val_int);
2210 break;
2212 case DW_OP_const8u:
2213 case DW_OP_const8s:
2214 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2215 fputc (',', asm_out_file);
2216 dw2_asm_output_data_raw (8, val1->v.val_int);
2217 break;
2219 case DW_OP_skip:
2220 case DW_OP_bra:
2222 int offset;
2224 gcc_assert (val1->val_class == dw_val_class_loc);
2225 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2227 fputc (',', asm_out_file);
2228 dw2_asm_output_data_raw (2, offset);
2230 break;
2232 case DW_OP_regx:
2234 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2235 gcc_assert (size_of_uleb128 (r)
2236 == size_of_uleb128 (val1->v.val_unsigned));
2237 fputc (',', asm_out_file);
2238 dw2_asm_output_data_uleb128_raw (r);
2240 break;
2242 case DW_OP_constu:
2243 case DW_OP_plus_uconst:
2244 case DW_OP_piece:
2245 fputc (',', asm_out_file);
2246 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2247 break;
2249 case DW_OP_bit_piece:
2250 fputc (',', asm_out_file);
2251 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2252 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2253 break;
2255 case DW_OP_consts:
2256 case DW_OP_breg0:
2257 case DW_OP_breg1:
2258 case DW_OP_breg2:
2259 case DW_OP_breg3:
2260 case DW_OP_breg4:
2261 case DW_OP_breg5:
2262 case DW_OP_breg6:
2263 case DW_OP_breg7:
2264 case DW_OP_breg8:
2265 case DW_OP_breg9:
2266 case DW_OP_breg10:
2267 case DW_OP_breg11:
2268 case DW_OP_breg12:
2269 case DW_OP_breg13:
2270 case DW_OP_breg14:
2271 case DW_OP_breg15:
2272 case DW_OP_breg16:
2273 case DW_OP_breg17:
2274 case DW_OP_breg18:
2275 case DW_OP_breg19:
2276 case DW_OP_breg20:
2277 case DW_OP_breg21:
2278 case DW_OP_breg22:
2279 case DW_OP_breg23:
2280 case DW_OP_breg24:
2281 case DW_OP_breg25:
2282 case DW_OP_breg26:
2283 case DW_OP_breg27:
2284 case DW_OP_breg28:
2285 case DW_OP_breg29:
2286 case DW_OP_breg30:
2287 case DW_OP_breg31:
2288 case DW_OP_fbreg:
2289 fputc (',', asm_out_file);
2290 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2291 break;
2293 case DW_OP_bregx:
2295 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2296 gcc_assert (size_of_uleb128 (r)
2297 == size_of_uleb128 (val1->v.val_unsigned));
2298 fputc (',', asm_out_file);
2299 dw2_asm_output_data_uleb128_raw (r);
2300 fputc (',', asm_out_file);
2301 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2303 break;
2305 case DW_OP_GNU_implicit_pointer:
2306 case DW_OP_GNU_entry_value:
2307 case DW_OP_GNU_const_type:
2308 case DW_OP_GNU_regval_type:
2309 case DW_OP_GNU_deref_type:
2310 case DW_OP_GNU_convert:
2311 case DW_OP_GNU_reinterpret:
2312 case DW_OP_GNU_parameter_ref:
2313 gcc_unreachable ();
2314 break;
2316 default:
2317 /* Other codes have no operands. */
2318 break;
2322 void
2323 output_loc_sequence_raw (dw_loc_descr_ref loc)
2325 while (1)
2327 enum dwarf_location_atom opc = loc->dw_loc_opc;
2328 /* Output the opcode. */
2329 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2331 unsigned r = (opc - DW_OP_breg0);
2332 r = DWARF2_FRAME_REG_OUT (r, 1);
2333 gcc_assert (r <= 31);
2334 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2336 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2338 unsigned r = (opc - DW_OP_reg0);
2339 r = DWARF2_FRAME_REG_OUT (r, 1);
2340 gcc_assert (r <= 31);
2341 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2343 /* Output the opcode. */
2344 fprintf (asm_out_file, "%#x", opc);
2345 output_loc_operands_raw (loc);
2347 if (!loc->dw_loc_next)
2348 break;
2349 loc = loc->dw_loc_next;
2351 fputc (',', asm_out_file);
2355 /* This function builds a dwarf location descriptor sequence from a
2356 dw_cfa_location, adding the given OFFSET to the result of the
2357 expression. */
2359 struct dw_loc_descr_struct *
2360 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2362 struct dw_loc_descr_struct *head, *tmp;
2364 offset += cfa->offset;
2366 if (cfa->indirect)
2368 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2369 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2370 head->dw_loc_oprnd1.val_entry = NULL;
2371 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2372 add_loc_descr (&head, tmp);
2373 if (offset != 0)
2375 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2376 add_loc_descr (&head, tmp);
2379 else
2380 head = new_reg_loc_descr (cfa->reg, offset);
2382 return head;
2385 /* This function builds a dwarf location descriptor sequence for
2386 the address at OFFSET from the CFA when stack is aligned to
2387 ALIGNMENT byte. */
2389 struct dw_loc_descr_struct *
2390 build_cfa_aligned_loc (dw_cfa_location *cfa,
2391 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2393 struct dw_loc_descr_struct *head;
2394 unsigned int dwarf_fp
2395 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2397 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2398 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2400 head = new_reg_loc_descr (dwarf_fp, 0);
2401 add_loc_descr (&head, int_loc_descriptor (alignment));
2402 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2403 loc_descr_plus_const (&head, offset);
2405 else
2406 head = new_reg_loc_descr (dwarf_fp, offset);
2407 return head;
2410 /* And now, the support for symbolic debugging information. */
2412 /* .debug_str support. */
2413 static int output_indirect_string (void **, void *);
2415 static void dwarf2out_init (const char *);
2416 static void dwarf2out_finish (const char *);
2417 static void dwarf2out_assembly_start (void);
2418 static void dwarf2out_define (unsigned int, const char *);
2419 static void dwarf2out_undef (unsigned int, const char *);
2420 static void dwarf2out_start_source_file (unsigned, const char *);
2421 static void dwarf2out_end_source_file (unsigned);
2422 static void dwarf2out_function_decl (tree);
2423 static void dwarf2out_begin_block (unsigned, unsigned);
2424 static void dwarf2out_end_block (unsigned, unsigned);
2425 static bool dwarf2out_ignore_block (const_tree);
2426 static void dwarf2out_global_decl (tree);
2427 static void dwarf2out_type_decl (tree, int);
2428 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2429 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2430 dw_die_ref);
2431 static void dwarf2out_abstract_function (tree);
2432 static void dwarf2out_var_location (rtx);
2433 static void dwarf2out_begin_function (tree);
2434 static void dwarf2out_end_function (unsigned int);
2435 static void dwarf2out_set_name (tree, tree);
2437 /* The debug hooks structure. */
2439 const struct gcc_debug_hooks dwarf2_debug_hooks =
2441 dwarf2out_init,
2442 dwarf2out_finish,
2443 dwarf2out_assembly_start,
2444 dwarf2out_define,
2445 dwarf2out_undef,
2446 dwarf2out_start_source_file,
2447 dwarf2out_end_source_file,
2448 dwarf2out_begin_block,
2449 dwarf2out_end_block,
2450 dwarf2out_ignore_block,
2451 dwarf2out_source_line,
2452 dwarf2out_begin_prologue,
2453 #if VMS_DEBUGGING_INFO
2454 dwarf2out_vms_end_prologue,
2455 dwarf2out_vms_begin_epilogue,
2456 #else
2457 debug_nothing_int_charstar,
2458 debug_nothing_int_charstar,
2459 #endif
2460 dwarf2out_end_epilogue,
2461 dwarf2out_begin_function,
2462 dwarf2out_end_function, /* end_function */
2463 dwarf2out_function_decl, /* function_decl */
2464 dwarf2out_global_decl,
2465 dwarf2out_type_decl, /* type_decl */
2466 dwarf2out_imported_module_or_decl,
2467 debug_nothing_tree, /* deferred_inline_function */
2468 /* The DWARF 2 backend tries to reduce debugging bloat by not
2469 emitting the abstract description of inline functions until
2470 something tries to reference them. */
2471 dwarf2out_abstract_function, /* outlining_inline_function */
2472 debug_nothing_rtx, /* label */
2473 debug_nothing_int, /* handle_pch */
2474 dwarf2out_var_location,
2475 dwarf2out_switch_text_section,
2476 dwarf2out_set_name,
2477 1, /* start_end_main_source_file */
2478 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2481 /* NOTE: In the comments in this file, many references are made to
2482 "Debugging Information Entries". This term is abbreviated as `DIE'
2483 throughout the remainder of this file. */
2485 /* An internal representation of the DWARF output is built, and then
2486 walked to generate the DWARF debugging info. The walk of the internal
2487 representation is done after the entire program has been compiled.
2488 The types below are used to describe the internal representation. */
2490 /* Whether to put type DIEs into their own section .debug_types instead
2491 of making them part of the .debug_info section. Only supported for
2492 Dwarf V4 or higher and the user didn't disable them through
2493 -fno-debug-types-section. It is more efficient to put them in a
2494 separate comdat sections since the linker will then be able to
2495 remove duplicates. But not all tools support .debug_types sections
2496 yet. */
2498 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2500 /* Various DIE's use offsets relative to the beginning of the
2501 .debug_info section to refer to each other. */
2503 typedef long int dw_offset;
2505 /* Define typedefs here to avoid circular dependencies. */
2507 typedef struct dw_attr_struct *dw_attr_ref;
2508 typedef struct dw_line_info_struct *dw_line_info_ref;
2509 typedef struct pubname_struct *pubname_ref;
2510 typedef struct dw_ranges_struct *dw_ranges_ref;
2511 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2512 typedef struct comdat_type_struct *comdat_type_node_ref;
2514 /* The entries in the line_info table more-or-less mirror the opcodes
2515 that are used in the real dwarf line table. Arrays of these entries
2516 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2517 supported. */
2519 enum dw_line_info_opcode {
2520 /* Emit DW_LNE_set_address; the operand is the label index. */
2521 LI_set_address,
2523 /* Emit a row to the matrix with the given line. This may be done
2524 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2525 special opcodes. */
2526 LI_set_line,
2528 /* Emit a DW_LNS_set_file. */
2529 LI_set_file,
2531 /* Emit a DW_LNS_set_column. */
2532 LI_set_column,
2534 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2535 LI_negate_stmt,
2537 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2538 LI_set_prologue_end,
2539 LI_set_epilogue_begin,
2541 /* Emit a DW_LNE_set_discriminator. */
2542 LI_set_discriminator
2545 typedef struct GTY(()) dw_line_info_struct {
2546 enum dw_line_info_opcode opcode;
2547 unsigned int val;
2548 } dw_line_info_entry;
2551 typedef struct GTY(()) dw_line_info_table_struct {
2552 /* The label that marks the end of this section. */
2553 const char *end_label;
2555 /* The values for the last row of the matrix, as collected in the table.
2556 These are used to minimize the changes to the next row. */
2557 unsigned int file_num;
2558 unsigned int line_num;
2559 unsigned int column_num;
2560 int discrim_num;
2561 bool is_stmt;
2562 bool in_use;
2564 vec<dw_line_info_entry, va_gc> *entries;
2565 } dw_line_info_table;
2567 typedef dw_line_info_table *dw_line_info_table_p;
2570 /* Each DIE attribute has a field specifying the attribute kind,
2571 a link to the next attribute in the chain, and an attribute value.
2572 Attributes are typically linked below the DIE they modify. */
2574 typedef struct GTY(()) dw_attr_struct {
2575 enum dwarf_attribute dw_attr;
2576 dw_val_node dw_attr_val;
2578 dw_attr_node;
2581 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2582 The children of each node form a circular list linked by
2583 die_sib. die_child points to the node *before* the "first" child node. */
2585 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2586 union die_symbol_or_type_node
2588 const char * GTY ((tag ("0"))) die_symbol;
2589 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2591 GTY ((desc ("%0.comdat_type_p"))) die_id;
2592 vec<dw_attr_node, va_gc> *die_attr;
2593 dw_die_ref die_parent;
2594 dw_die_ref die_child;
2595 dw_die_ref die_sib;
2596 dw_die_ref die_definition; /* ref from a specification to its definition */
2597 dw_offset die_offset;
2598 unsigned long die_abbrev;
2599 int die_mark;
2600 unsigned int decl_id;
2601 enum dwarf_tag die_tag;
2602 /* Die is used and must not be pruned as unused. */
2603 BOOL_BITFIELD die_perennial_p : 1;
2604 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2605 /* Lots of spare bits. */
2607 die_node;
2609 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2610 #define FOR_EACH_CHILD(die, c, expr) do { \
2611 c = die->die_child; \
2612 if (c) do { \
2613 c = c->die_sib; \
2614 expr; \
2615 } while (c != die->die_child); \
2616 } while (0)
2618 /* The pubname structure */
2620 typedef struct GTY(()) pubname_struct {
2621 dw_die_ref die;
2622 const char *name;
2624 pubname_entry;
2627 struct GTY(()) dw_ranges_struct {
2628 /* If this is positive, it's a block number, otherwise it's a
2629 bitwise-negated index into dw_ranges_by_label. */
2630 int num;
2633 /* A structure to hold a macinfo entry. */
2635 typedef struct GTY(()) macinfo_struct {
2636 unsigned char code;
2637 unsigned HOST_WIDE_INT lineno;
2638 const char *info;
2640 macinfo_entry;
2643 struct GTY(()) dw_ranges_by_label_struct {
2644 const char *begin;
2645 const char *end;
2648 /* The comdat type node structure. */
2649 typedef struct GTY(()) comdat_type_struct
2651 dw_die_ref root_die;
2652 dw_die_ref type_die;
2653 dw_die_ref skeleton_die;
2654 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2655 struct comdat_type_struct *next;
2657 comdat_type_node;
2659 /* The limbo die list structure. */
2660 typedef struct GTY(()) limbo_die_struct {
2661 dw_die_ref die;
2662 tree created_for;
2663 struct limbo_die_struct *next;
2665 limbo_die_node;
2667 typedef struct skeleton_chain_struct
2669 dw_die_ref old_die;
2670 dw_die_ref new_die;
2671 struct skeleton_chain_struct *parent;
2673 skeleton_chain_node;
2675 /* Define a macro which returns nonzero for a TYPE_DECL which was
2676 implicitly generated for a type.
2678 Note that, unlike the C front-end (which generates a NULL named
2679 TYPE_DECL node for each complete tagged type, each array type,
2680 and each function type node created) the C++ front-end generates
2681 a _named_ TYPE_DECL node for each tagged type node created.
2682 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2683 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2684 front-end, but for each type, tagged or not. */
2686 #define TYPE_DECL_IS_STUB(decl) \
2687 (DECL_NAME (decl) == NULL_TREE \
2688 || (DECL_ARTIFICIAL (decl) \
2689 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2690 /* This is necessary for stub decls that \
2691 appear in nested inline functions. */ \
2692 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2693 && (decl_ultimate_origin (decl) \
2694 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2696 /* Information concerning the compilation unit's programming
2697 language, and compiler version. */
2699 /* Fixed size portion of the DWARF compilation unit header. */
2700 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2701 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2703 /* Fixed size portion of the DWARF comdat type unit header. */
2704 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2705 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2706 + DWARF_OFFSET_SIZE)
2708 /* Fixed size portion of public names info. */
2709 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2711 /* Fixed size portion of the address range info. */
2712 #define DWARF_ARANGES_HEADER_SIZE \
2713 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2714 DWARF2_ADDR_SIZE * 2) \
2715 - DWARF_INITIAL_LENGTH_SIZE)
2717 /* Size of padding portion in the address range info. It must be
2718 aligned to twice the pointer size. */
2719 #define DWARF_ARANGES_PAD_SIZE \
2720 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2721 DWARF2_ADDR_SIZE * 2) \
2722 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2724 /* Use assembler line directives if available. */
2725 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2726 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2727 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2728 #else
2729 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2730 #endif
2731 #endif
2733 /* Minimum line offset in a special line info. opcode.
2734 This value was chosen to give a reasonable range of values. */
2735 #define DWARF_LINE_BASE -10
2737 /* First special line opcode - leave room for the standard opcodes. */
2738 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2740 /* Range of line offsets in a special line info. opcode. */
2741 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2743 /* Flag that indicates the initial value of the is_stmt_start flag.
2744 In the present implementation, we do not mark any lines as
2745 the beginning of a source statement, because that information
2746 is not made available by the GCC front-end. */
2747 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2749 /* Maximum number of operations per instruction bundle. */
2750 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2751 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2752 #endif
2754 /* This location is used by calc_die_sizes() to keep track
2755 the offset of each DIE within the .debug_info section. */
2756 static unsigned long next_die_offset;
2758 /* Record the root of the DIE's built for the current compilation unit. */
2759 static GTY(()) dw_die_ref single_comp_unit_die;
2761 /* A list of type DIEs that have been separated into comdat sections. */
2762 static GTY(()) comdat_type_node *comdat_type_list;
2764 /* A list of DIEs with a NULL parent waiting to be relocated. */
2765 static GTY(()) limbo_die_node *limbo_die_list;
2767 /* A list of DIEs for which we may have to generate
2768 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2769 static GTY(()) limbo_die_node *deferred_asm_name;
2771 /* Filenames referenced by this compilation unit. */
2772 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2774 /* A hash table of references to DIE's that describe declarations.
2775 The key is a DECL_UID() which is a unique number identifying each decl. */
2776 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2778 /* A hash table of references to DIE's that describe COMMON blocks.
2779 The key is DECL_UID() ^ die_parent. */
2780 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2782 typedef struct GTY(()) die_arg_entry_struct {
2783 dw_die_ref die;
2784 tree arg;
2785 } die_arg_entry;
2788 /* Node of the variable location list. */
2789 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2790 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2791 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2792 in mode of the EXPR_LIST node and first EXPR_LIST operand
2793 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2794 location or NULL for padding. For larger bitsizes,
2795 mode is 0 and first operand is a CONCAT with bitsize
2796 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2797 NULL as second operand. */
2798 rtx GTY (()) loc;
2799 const char * GTY (()) label;
2800 struct var_loc_node * GTY (()) next;
2803 /* Variable location list. */
2804 struct GTY (()) var_loc_list_def {
2805 struct var_loc_node * GTY (()) first;
2807 /* Pointer to the last but one or last element of the
2808 chained list. If the list is empty, both first and
2809 last are NULL, if the list contains just one node
2810 or the last node certainly is not redundant, it points
2811 to the last node, otherwise points to the last but one.
2812 Do not mark it for GC because it is marked through the chain. */
2813 struct var_loc_node * GTY ((skip ("%h"))) last;
2815 /* Pointer to the last element before section switch,
2816 if NULL, either sections weren't switched or first
2817 is after section switch. */
2818 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2820 /* DECL_UID of the variable decl. */
2821 unsigned int decl_id;
2823 typedef struct var_loc_list_def var_loc_list;
2825 /* Call argument location list. */
2826 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2827 rtx GTY (()) call_arg_loc_note;
2828 const char * GTY (()) label;
2829 tree GTY (()) block;
2830 bool tail_call_p;
2831 rtx GTY (()) symbol_ref;
2832 struct call_arg_loc_node * GTY (()) next;
2836 /* Table of decl location linked lists. */
2837 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2839 /* Head and tail of call_arg_loc chain. */
2840 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2841 static struct call_arg_loc_node *call_arg_loc_last;
2843 /* Number of call sites in the current function. */
2844 static int call_site_count = -1;
2845 /* Number of tail call sites in the current function. */
2846 static int tail_call_site_count = -1;
2848 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2849 DIEs. */
2850 static vec<dw_die_ref> block_map;
2852 /* A cached location list. */
2853 struct GTY (()) cached_dw_loc_list_def {
2854 /* The DECL_UID of the decl that this entry describes. */
2855 unsigned int decl_id;
2857 /* The cached location list. */
2858 dw_loc_list_ref loc_list;
2860 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2862 /* Table of cached location lists. */
2863 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2865 /* A pointer to the base of a list of references to DIE's that
2866 are uniquely identified by their tag, presence/absence of
2867 children DIE's, and list of attribute/value pairs. */
2868 static GTY((length ("abbrev_die_table_allocated")))
2869 dw_die_ref *abbrev_die_table;
2871 /* Number of elements currently allocated for abbrev_die_table. */
2872 static GTY(()) unsigned abbrev_die_table_allocated;
2874 /* Number of elements in type_die_table currently in use. */
2875 static GTY(()) unsigned abbrev_die_table_in_use;
2877 /* Size (in elements) of increments by which we may expand the
2878 abbrev_die_table. */
2879 #define ABBREV_DIE_TABLE_INCREMENT 256
2881 /* A global counter for generating labels for line number data. */
2882 static unsigned int line_info_label_num;
2884 /* The current table to which we should emit line number information
2885 for the current function. This will be set up at the beginning of
2886 assembly for the function. */
2887 static dw_line_info_table *cur_line_info_table;
2889 /* The two default tables of line number info. */
2890 static GTY(()) dw_line_info_table *text_section_line_info;
2891 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2893 /* The set of all non-default tables of line number info. */
2894 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2896 /* A flag to tell pubnames/types export if there is an info section to
2897 refer to. */
2898 static bool info_section_emitted;
2900 /* A pointer to the base of a table that contains a list of publicly
2901 accessible names. */
2902 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2904 /* A pointer to the base of a table that contains a list of publicly
2905 accessible types. */
2906 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2908 /* A pointer to the base of a table that contains a list of macro
2909 defines/undefines (and file start/end markers). */
2910 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2912 /* True if .debug_macinfo or .debug_macros section is going to be
2913 emitted. */
2914 #define have_macinfo \
2915 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2916 && !macinfo_table->is_empty ())
2918 /* Array of dies for which we should generate .debug_ranges info. */
2919 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2921 /* Number of elements currently allocated for ranges_table. */
2922 static GTY(()) unsigned ranges_table_allocated;
2924 /* Number of elements in ranges_table currently in use. */
2925 static GTY(()) unsigned ranges_table_in_use;
2927 /* Array of pairs of labels referenced in ranges_table. */
2928 static GTY ((length ("ranges_by_label_allocated")))
2929 dw_ranges_by_label_ref ranges_by_label;
2931 /* Number of elements currently allocated for ranges_by_label. */
2932 static GTY(()) unsigned ranges_by_label_allocated;
2934 /* Number of elements in ranges_by_label currently in use. */
2935 static GTY(()) unsigned ranges_by_label_in_use;
2937 /* Size (in elements) of increments by which we may expand the
2938 ranges_table. */
2939 #define RANGES_TABLE_INCREMENT 64
2941 /* Whether we have location lists that need outputting */
2942 static GTY(()) bool have_location_lists;
2944 /* Unique label counter. */
2945 static GTY(()) unsigned int loclabel_num;
2947 /* Unique label counter for point-of-call tables. */
2948 static GTY(()) unsigned int poc_label_num;
2950 /* Record whether the function being analyzed contains inlined functions. */
2951 static int current_function_has_inlines;
2953 /* The last file entry emitted by maybe_emit_file(). */
2954 static GTY(()) struct dwarf_file_data * last_emitted_file;
2956 /* Number of internal labels generated by gen_internal_sym(). */
2957 static GTY(()) int label_num;
2959 /* Cached result of previous call to lookup_filename. */
2960 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2962 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2964 /* Instances of generic types for which we need to generate debug
2965 info that describe their generic parameters and arguments. That
2966 generation needs to happen once all types are properly laid out so
2967 we do it at the end of compilation. */
2968 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2970 /* Offset from the "steady-state frame pointer" to the frame base,
2971 within the current function. */
2972 static HOST_WIDE_INT frame_pointer_fb_offset;
2973 static bool frame_pointer_fb_offset_valid;
2975 static vec<dw_die_ref> base_types;
2977 /* Forward declarations for functions defined in this file. */
2979 static int is_pseudo_reg (const_rtx);
2980 static tree type_main_variant (tree);
2981 static int is_tagged_type (const_tree);
2982 static const char *dwarf_tag_name (unsigned);
2983 static const char *dwarf_attr_name (unsigned);
2984 static const char *dwarf_form_name (unsigned);
2985 static tree decl_ultimate_origin (const_tree);
2986 static tree decl_class_context (tree);
2987 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2988 static inline enum dw_val_class AT_class (dw_attr_ref);
2989 static inline unsigned int AT_index (dw_attr_ref);
2990 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2991 static inline unsigned AT_flag (dw_attr_ref);
2992 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2993 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2994 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2995 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2996 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2997 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2998 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2999 unsigned int, unsigned char *);
3000 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3001 static hashval_t debug_str_do_hash (const void *);
3002 static int debug_str_eq (const void *, const void *);
3003 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3004 static inline const char *AT_string (dw_attr_ref);
3005 static enum dwarf_form AT_string_form (dw_attr_ref);
3006 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3007 static void add_AT_specification (dw_die_ref, dw_die_ref);
3008 static inline dw_die_ref AT_ref (dw_attr_ref);
3009 static inline int AT_ref_external (dw_attr_ref);
3010 static inline void set_AT_ref_external (dw_attr_ref, int);
3011 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3012 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3013 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3014 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3015 dw_loc_list_ref);
3016 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3017 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3018 static void remove_addr_table_entry (addr_table_entry *);
3019 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3020 static inline rtx AT_addr (dw_attr_ref);
3021 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3022 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3023 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3024 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3025 unsigned HOST_WIDE_INT);
3026 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3027 unsigned long, bool);
3028 static inline const char *AT_lbl (dw_attr_ref);
3029 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3030 static const char *get_AT_low_pc (dw_die_ref);
3031 static const char *get_AT_hi_pc (dw_die_ref);
3032 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3033 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3034 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3035 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3036 static bool is_cxx (void);
3037 static bool is_fortran (void);
3038 static bool is_ada (void);
3039 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3040 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3041 static void add_child_die (dw_die_ref, dw_die_ref);
3042 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3043 static dw_die_ref lookup_type_die (tree);
3044 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3045 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3046 static void equate_type_number_to_die (tree, dw_die_ref);
3047 static hashval_t decl_die_table_hash (const void *);
3048 static int decl_die_table_eq (const void *, const void *);
3049 static dw_die_ref lookup_decl_die (tree);
3050 static hashval_t common_block_die_table_hash (const void *);
3051 static int common_block_die_table_eq (const void *, const void *);
3052 static hashval_t decl_loc_table_hash (const void *);
3053 static int decl_loc_table_eq (const void *, const void *);
3054 static var_loc_list *lookup_decl_loc (const_tree);
3055 static void equate_decl_number_to_die (tree, dw_die_ref);
3056 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3057 static void print_spaces (FILE *);
3058 static void print_die (dw_die_ref, FILE *);
3059 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3060 static dw_die_ref pop_compile_unit (dw_die_ref);
3061 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3062 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3063 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3064 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3065 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3066 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3067 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3068 struct md5_ctx *, int *);
3069 struct checksum_attributes;
3070 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3071 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3072 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3073 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3074 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3075 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3076 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3077 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3078 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3079 static void compute_section_prefix (dw_die_ref);
3080 static int is_type_die (dw_die_ref);
3081 static int is_comdat_die (dw_die_ref);
3082 static int is_symbol_die (dw_die_ref);
3083 static inline bool is_template_instantiation (dw_die_ref);
3084 static void assign_symbol_names (dw_die_ref);
3085 static void break_out_includes (dw_die_ref);
3086 static int is_declaration_die (dw_die_ref);
3087 static int should_move_die_to_comdat (dw_die_ref);
3088 static dw_die_ref clone_as_declaration (dw_die_ref);
3089 static dw_die_ref clone_die (dw_die_ref);
3090 static dw_die_ref clone_tree (dw_die_ref);
3091 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3092 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3093 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3094 static dw_die_ref generate_skeleton (dw_die_ref);
3095 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3096 dw_die_ref,
3097 dw_die_ref);
3098 static void break_out_comdat_types (dw_die_ref);
3099 static void copy_decls_for_unworthy_types (dw_die_ref);
3101 static void add_sibling_attributes (dw_die_ref);
3102 static void output_location_lists (dw_die_ref);
3103 static int constant_size (unsigned HOST_WIDE_INT);
3104 static unsigned long size_of_die (dw_die_ref);
3105 static void calc_die_sizes (dw_die_ref);
3106 static void calc_base_type_die_sizes (void);
3107 static void mark_dies (dw_die_ref);
3108 static void unmark_dies (dw_die_ref);
3109 static void unmark_all_dies (dw_die_ref);
3110 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3111 static unsigned long size_of_aranges (void);
3112 static enum dwarf_form value_format (dw_attr_ref);
3113 static void output_value_format (dw_attr_ref);
3114 static void output_abbrev_section (void);
3115 static void output_die_abbrevs (unsigned long, dw_die_ref);
3116 static void output_die_symbol (dw_die_ref);
3117 static void output_die (dw_die_ref);
3118 static void output_compilation_unit_header (void);
3119 static void output_comp_unit (dw_die_ref, int);
3120 static void output_comdat_type_unit (comdat_type_node *);
3121 static const char *dwarf2_name (tree, int);
3122 static void add_pubname (tree, dw_die_ref);
3123 static void add_enumerator_pubname (const char *, dw_die_ref);
3124 static void add_pubname_string (const char *, dw_die_ref);
3125 static void add_pubtype (tree, dw_die_ref);
3126 static void output_pubnames (vec<pubname_entry, va_gc> *);
3127 static void output_aranges (unsigned long);
3128 static unsigned int add_ranges_num (int);
3129 static unsigned int add_ranges (const_tree);
3130 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3131 bool *, bool);
3132 static void output_ranges (void);
3133 static dw_line_info_table *new_line_info_table (void);
3134 static void output_line_info (bool);
3135 static void output_file_names (void);
3136 static dw_die_ref base_type_die (tree);
3137 static int is_base_type (tree);
3138 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3139 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3140 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3141 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3142 static int type_is_enum (const_tree);
3143 static unsigned int dbx_reg_number (const_rtx);
3144 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3145 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3146 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3147 enum var_init_status);
3148 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3149 enum var_init_status);
3150 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3151 enum var_init_status);
3152 static int is_based_loc (const_rtx);
3153 static int resolve_one_addr (rtx *, void *);
3154 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3155 enum var_init_status);
3156 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3157 enum var_init_status);
3158 static dw_loc_list_ref loc_list_from_tree (tree, int);
3159 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3160 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3161 static tree field_type (const_tree);
3162 static unsigned int simple_type_align_in_bits (const_tree);
3163 static unsigned int simple_decl_align_in_bits (const_tree);
3164 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3165 static HOST_WIDE_INT field_byte_offset (const_tree);
3166 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3167 dw_loc_list_ref);
3168 static void add_data_member_location_attribute (dw_die_ref, tree);
3169 static bool add_const_value_attribute (dw_die_ref, rtx);
3170 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3171 static void insert_wide_int (const wide_int &, unsigned char *, int);
3172 static void insert_float (const_rtx, unsigned char *);
3173 static rtx rtl_for_decl_location (tree);
3174 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3175 enum dwarf_attribute);
3176 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3177 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3178 static void add_name_attribute (dw_die_ref, const char *);
3179 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3180 static void add_comp_dir_attribute (dw_die_ref);
3181 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3182 static void add_subscript_info (dw_die_ref, tree, bool);
3183 static void add_byte_size_attribute (dw_die_ref, tree);
3184 static void add_bit_offset_attribute (dw_die_ref, tree);
3185 static void add_bit_size_attribute (dw_die_ref, tree);
3186 static void add_prototyped_attribute (dw_die_ref, tree);
3187 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3188 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3189 static void add_src_coords_attributes (dw_die_ref, tree);
3190 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3191 static void push_decl_scope (tree);
3192 static void pop_decl_scope (void);
3193 static dw_die_ref scope_die_for (tree, dw_die_ref);
3194 static inline int local_scope_p (dw_die_ref);
3195 static inline int class_scope_p (dw_die_ref);
3196 static inline int class_or_namespace_scope_p (dw_die_ref);
3197 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3198 static void add_calling_convention_attribute (dw_die_ref, tree);
3199 static const char *type_tag (const_tree);
3200 static tree member_declared_type (const_tree);
3201 #if 0
3202 static const char *decl_start_label (tree);
3203 #endif
3204 static void gen_array_type_die (tree, dw_die_ref);
3205 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3206 #if 0
3207 static void gen_entry_point_die (tree, dw_die_ref);
3208 #endif
3209 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3210 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3211 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3212 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3213 static void gen_formal_types_die (tree, dw_die_ref);
3214 static void gen_subprogram_die (tree, dw_die_ref);
3215 static void gen_variable_die (tree, tree, dw_die_ref);
3216 static void gen_const_die (tree, dw_die_ref);
3217 static void gen_label_die (tree, dw_die_ref);
3218 static void gen_lexical_block_die (tree, dw_die_ref, int);
3219 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3220 static void gen_field_die (tree, dw_die_ref);
3221 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3222 static dw_die_ref gen_compile_unit_die (const char *);
3223 static void gen_inheritance_die (tree, tree, dw_die_ref);
3224 static void gen_member_die (tree, dw_die_ref);
3225 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3226 enum debug_info_usage);
3227 static void gen_subroutine_type_die (tree, dw_die_ref);
3228 static void gen_typedef_die (tree, dw_die_ref);
3229 static void gen_type_die (tree, dw_die_ref);
3230 static void gen_block_die (tree, dw_die_ref, int);
3231 static void decls_for_scope (tree, dw_die_ref, int);
3232 static inline int is_redundant_typedef (const_tree);
3233 static bool is_naming_typedef_decl (const_tree);
3234 static inline dw_die_ref get_context_die (tree);
3235 static void gen_namespace_die (tree, dw_die_ref);
3236 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3237 static dw_die_ref force_decl_die (tree);
3238 static dw_die_ref force_type_die (tree);
3239 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3240 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3241 static struct dwarf_file_data * lookup_filename (const char *);
3242 static void retry_incomplete_types (void);
3243 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3244 static void gen_generic_params_dies (tree);
3245 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3246 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3247 static void splice_child_die (dw_die_ref, dw_die_ref);
3248 static int file_info_cmp (const void *, const void *);
3249 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3250 const char *, const char *);
3251 static void output_loc_list (dw_loc_list_ref);
3252 static char *gen_internal_sym (const char *);
3253 static bool want_pubnames (void);
3255 static void prune_unmark_dies (dw_die_ref);
3256 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3257 static void prune_unused_types_mark (dw_die_ref, int);
3258 static void prune_unused_types_walk (dw_die_ref);
3259 static void prune_unused_types_walk_attribs (dw_die_ref);
3260 static void prune_unused_types_prune (dw_die_ref);
3261 static void prune_unused_types (void);
3262 static int maybe_emit_file (struct dwarf_file_data *fd);
3263 static inline const char *AT_vms_delta1 (dw_attr_ref);
3264 static inline const char *AT_vms_delta2 (dw_attr_ref);
3265 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3266 const char *, const char *);
3267 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3268 static void gen_remaining_tmpl_value_param_die_attribute (void);
3269 static bool generic_type_p (tree);
3270 static void schedule_generic_params_dies_gen (tree t);
3271 static void gen_scheduled_generic_parms_dies (void);
3273 static const char *comp_dir_string (void);
3275 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3277 /* enum for tracking thread-local variables whose address is really an offset
3278 relative to the TLS pointer, which will need link-time relocation, but will
3279 not need relocation by the DWARF consumer. */
3281 enum dtprel_bool
3283 dtprel_false = 0,
3284 dtprel_true = 1
3287 /* Return the operator to use for an address of a variable. For dtprel_true, we
3288 use DW_OP_const*. For regular variables, which need both link-time
3289 relocation and consumer-level relocation (e.g., to account for shared objects
3290 loaded at a random address), we use DW_OP_addr*. */
3292 static inline enum dwarf_location_atom
3293 dw_addr_op (enum dtprel_bool dtprel)
3295 if (dtprel == dtprel_true)
3296 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3297 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3298 else
3299 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3302 /* Return a pointer to a newly allocated address location description. If
3303 dwarf_split_debug_info is true, then record the address with the appropriate
3304 relocation. */
3305 static inline dw_loc_descr_ref
3306 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3308 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3310 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3311 ref->dw_loc_oprnd1.v.val_addr = addr;
3312 ref->dtprel = dtprel;
3313 if (dwarf_split_debug_info)
3314 ref->dw_loc_oprnd1.val_entry
3315 = add_addr_table_entry (addr,
3316 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3317 else
3318 ref->dw_loc_oprnd1.val_entry = NULL;
3320 return ref;
3323 /* Section names used to hold DWARF debugging information. */
3325 #ifndef DEBUG_INFO_SECTION
3326 #define DEBUG_INFO_SECTION ".debug_info"
3327 #endif
3328 #ifndef DEBUG_DWO_INFO_SECTION
3329 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3330 #endif
3331 #ifndef DEBUG_ABBREV_SECTION
3332 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3333 #endif
3334 #ifndef DEBUG_DWO_ABBREV_SECTION
3335 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3336 #endif
3337 #ifndef DEBUG_ARANGES_SECTION
3338 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3339 #endif
3340 #ifndef DEBUG_ADDR_SECTION
3341 #define DEBUG_ADDR_SECTION ".debug_addr"
3342 #endif
3343 #ifndef DEBUG_NORM_MACINFO_SECTION
3344 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3345 #endif
3346 #ifndef DEBUG_DWO_MACINFO_SECTION
3347 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3348 #endif
3349 #ifndef DEBUG_MACINFO_SECTION
3350 #define DEBUG_MACINFO_SECTION \
3351 (!dwarf_split_debug_info \
3352 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3353 #endif
3354 #ifndef DEBUG_NORM_MACRO_SECTION
3355 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3356 #endif
3357 #ifndef DEBUG_DWO_MACRO_SECTION
3358 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3359 #endif
3360 #ifndef DEBUG_MACRO_SECTION
3361 #define DEBUG_MACRO_SECTION \
3362 (!dwarf_split_debug_info \
3363 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3364 #endif
3365 #ifndef DEBUG_LINE_SECTION
3366 #define DEBUG_LINE_SECTION ".debug_line"
3367 #endif
3368 #ifndef DEBUG_DWO_LINE_SECTION
3369 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3370 #endif
3371 #ifndef DEBUG_LOC_SECTION
3372 #define DEBUG_LOC_SECTION ".debug_loc"
3373 #endif
3374 #ifndef DEBUG_DWO_LOC_SECTION
3375 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3376 #endif
3377 #ifndef DEBUG_PUBNAMES_SECTION
3378 #define DEBUG_PUBNAMES_SECTION \
3379 ((debug_generate_pub_sections == 2) \
3380 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3381 #endif
3382 #ifndef DEBUG_PUBTYPES_SECTION
3383 #define DEBUG_PUBTYPES_SECTION \
3384 ((debug_generate_pub_sections == 2) \
3385 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3386 #endif
3387 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3388 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3389 #ifndef DEBUG_STR_OFFSETS_SECTION
3390 #define DEBUG_STR_OFFSETS_SECTION \
3391 (!dwarf_split_debug_info \
3392 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3393 #endif
3394 #ifndef DEBUG_STR_DWO_SECTION
3395 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3396 #endif
3397 #ifndef DEBUG_STR_SECTION
3398 #define DEBUG_STR_SECTION ".debug_str"
3399 #endif
3400 #ifndef DEBUG_RANGES_SECTION
3401 #define DEBUG_RANGES_SECTION ".debug_ranges"
3402 #endif
3404 /* Standard ELF section names for compiled code and data. */
3405 #ifndef TEXT_SECTION_NAME
3406 #define TEXT_SECTION_NAME ".text"
3407 #endif
3409 /* Section flags for .debug_macinfo/.debug_macro section. */
3410 #define DEBUG_MACRO_SECTION_FLAGS \
3411 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3413 /* Section flags for .debug_str section. */
3414 #define DEBUG_STR_SECTION_FLAGS \
3415 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3416 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3417 : SECTION_DEBUG)
3419 /* Section flags for .debug_str.dwo section. */
3420 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3422 /* Labels we insert at beginning sections we can reference instead of
3423 the section names themselves. */
3425 #ifndef TEXT_SECTION_LABEL
3426 #define TEXT_SECTION_LABEL "Ltext"
3427 #endif
3428 #ifndef COLD_TEXT_SECTION_LABEL
3429 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3430 #endif
3431 #ifndef DEBUG_LINE_SECTION_LABEL
3432 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3433 #endif
3434 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3435 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3436 #endif
3437 #ifndef DEBUG_INFO_SECTION_LABEL
3438 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3439 #endif
3440 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3441 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3442 #endif
3443 #ifndef DEBUG_ABBREV_SECTION_LABEL
3444 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3445 #endif
3446 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3447 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3448 #endif
3449 #ifndef DEBUG_ADDR_SECTION_LABEL
3450 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3451 #endif
3452 #ifndef DEBUG_LOC_SECTION_LABEL
3453 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3454 #endif
3455 #ifndef DEBUG_RANGES_SECTION_LABEL
3456 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3457 #endif
3458 #ifndef DEBUG_MACINFO_SECTION_LABEL
3459 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3460 #endif
3461 #ifndef DEBUG_MACRO_SECTION_LABEL
3462 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3463 #endif
3464 #define SKELETON_COMP_DIE_ABBREV 1
3465 #define SKELETON_TYPE_DIE_ABBREV 2
3467 /* Definitions of defaults for formats and names of various special
3468 (artificial) labels which may be generated within this file (when the -g
3469 options is used and DWARF2_DEBUGGING_INFO is in effect.
3470 If necessary, these may be overridden from within the tm.h file, but
3471 typically, overriding these defaults is unnecessary. */
3473 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3474 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3475 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3476 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3477 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3478 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3479 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3480 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3481 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3482 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3483 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3484 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3485 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3486 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3488 #ifndef TEXT_END_LABEL
3489 #define TEXT_END_LABEL "Letext"
3490 #endif
3491 #ifndef COLD_END_LABEL
3492 #define COLD_END_LABEL "Letext_cold"
3493 #endif
3494 #ifndef BLOCK_BEGIN_LABEL
3495 #define BLOCK_BEGIN_LABEL "LBB"
3496 #endif
3497 #ifndef BLOCK_END_LABEL
3498 #define BLOCK_END_LABEL "LBE"
3499 #endif
3500 #ifndef LINE_CODE_LABEL
3501 #define LINE_CODE_LABEL "LM"
3502 #endif
3505 /* Return the root of the DIE's built for the current compilation unit. */
3506 static dw_die_ref
3507 comp_unit_die (void)
3509 if (!single_comp_unit_die)
3510 single_comp_unit_die = gen_compile_unit_die (NULL);
3511 return single_comp_unit_die;
3514 /* We allow a language front-end to designate a function that is to be
3515 called to "demangle" any name before it is put into a DIE. */
3517 static const char *(*demangle_name_func) (const char *);
3519 void
3520 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3522 demangle_name_func = func;
3525 /* Test if rtl node points to a pseudo register. */
3527 static inline int
3528 is_pseudo_reg (const_rtx rtl)
3530 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3531 || (GET_CODE (rtl) == SUBREG
3532 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3535 /* Return a reference to a type, with its const and volatile qualifiers
3536 removed. */
3538 static inline tree
3539 type_main_variant (tree type)
3541 type = TYPE_MAIN_VARIANT (type);
3543 /* ??? There really should be only one main variant among any group of
3544 variants of a given type (and all of the MAIN_VARIANT values for all
3545 members of the group should point to that one type) but sometimes the C
3546 front-end messes this up for array types, so we work around that bug
3547 here. */
3548 if (TREE_CODE (type) == ARRAY_TYPE)
3549 while (type != TYPE_MAIN_VARIANT (type))
3550 type = TYPE_MAIN_VARIANT (type);
3552 return type;
3555 /* Return nonzero if the given type node represents a tagged type. */
3557 static inline int
3558 is_tagged_type (const_tree type)
3560 enum tree_code code = TREE_CODE (type);
3562 return (code == RECORD_TYPE || code == UNION_TYPE
3563 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3566 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3568 static void
3569 get_ref_die_offset_label (char *label, dw_die_ref ref)
3571 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3574 /* Return die_offset of a DIE reference to a base type. */
3576 static unsigned long int
3577 get_base_type_offset (dw_die_ref ref)
3579 if (ref->die_offset)
3580 return ref->die_offset;
3581 if (comp_unit_die ()->die_abbrev)
3583 calc_base_type_die_sizes ();
3584 gcc_assert (ref->die_offset);
3586 return ref->die_offset;
3589 /* Return die_offset of a DIE reference other than base type. */
3591 static unsigned long int
3592 get_ref_die_offset (dw_die_ref ref)
3594 gcc_assert (ref->die_offset);
3595 return ref->die_offset;
3598 /* Convert a DIE tag into its string name. */
3600 static const char *
3601 dwarf_tag_name (unsigned int tag)
3603 const char *name = get_DW_TAG_name (tag);
3605 if (name != NULL)
3606 return name;
3608 return "DW_TAG_<unknown>";
3611 /* Convert a DWARF attribute code into its string name. */
3613 static const char *
3614 dwarf_attr_name (unsigned int attr)
3616 const char *name;
3618 switch (attr)
3620 #if VMS_DEBUGGING_INFO
3621 case DW_AT_HP_prologue:
3622 return "DW_AT_HP_prologue";
3623 #else
3624 case DW_AT_MIPS_loop_unroll_factor:
3625 return "DW_AT_MIPS_loop_unroll_factor";
3626 #endif
3628 #if VMS_DEBUGGING_INFO
3629 case DW_AT_HP_epilogue:
3630 return "DW_AT_HP_epilogue";
3631 #else
3632 case DW_AT_MIPS_stride:
3633 return "DW_AT_MIPS_stride";
3634 #endif
3637 name = get_DW_AT_name (attr);
3639 if (name != NULL)
3640 return name;
3642 return "DW_AT_<unknown>";
3645 /* Convert a DWARF value form code into its string name. */
3647 static const char *
3648 dwarf_form_name (unsigned int form)
3650 const char *name = get_DW_FORM_name (form);
3652 if (name != NULL)
3653 return name;
3655 return "DW_FORM_<unknown>";
3658 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3659 instance of an inlined instance of a decl which is local to an inline
3660 function, so we have to trace all of the way back through the origin chain
3661 to find out what sort of node actually served as the original seed for the
3662 given block. */
3664 static tree
3665 decl_ultimate_origin (const_tree decl)
3667 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3668 return NULL_TREE;
3670 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3671 nodes in the function to point to themselves; ignore that if
3672 we're trying to output the abstract instance of this function. */
3673 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3674 return NULL_TREE;
3676 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3677 most distant ancestor, this should never happen. */
3678 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3680 return DECL_ABSTRACT_ORIGIN (decl);
3683 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3684 of a virtual function may refer to a base class, so we check the 'this'
3685 parameter. */
3687 static tree
3688 decl_class_context (tree decl)
3690 tree context = NULL_TREE;
3692 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3693 context = DECL_CONTEXT (decl);
3694 else
3695 context = TYPE_MAIN_VARIANT
3696 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3698 if (context && !TYPE_P (context))
3699 context = NULL_TREE;
3701 return context;
3704 /* Add an attribute/value pair to a DIE. */
3706 static inline void
3707 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3709 /* Maybe this should be an assert? */
3710 if (die == NULL)
3711 return;
3713 vec_safe_reserve (die->die_attr, 1);
3714 vec_safe_push (die->die_attr, *attr);
3717 static inline enum dw_val_class
3718 AT_class (dw_attr_ref a)
3720 return a->dw_attr_val.val_class;
3723 /* Return the index for any attribute that will be referenced with a
3724 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3725 are stored in dw_attr_val.v.val_str for reference counting
3726 pruning. */
3728 static inline unsigned int
3729 AT_index (dw_attr_ref a)
3731 if (AT_class (a) == dw_val_class_str)
3732 return a->dw_attr_val.v.val_str->index;
3733 else if (a->dw_attr_val.val_entry != NULL)
3734 return a->dw_attr_val.val_entry->index;
3735 return NOT_INDEXED;
3738 /* Add a flag value attribute to a DIE. */
3740 static inline void
3741 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3743 dw_attr_node attr;
3745 attr.dw_attr = attr_kind;
3746 attr.dw_attr_val.val_class = dw_val_class_flag;
3747 attr.dw_attr_val.val_entry = NULL;
3748 attr.dw_attr_val.v.val_flag = flag;
3749 add_dwarf_attr (die, &attr);
3752 static inline unsigned
3753 AT_flag (dw_attr_ref a)
3755 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3756 return a->dw_attr_val.v.val_flag;
3759 /* Add a signed integer attribute value to a DIE. */
3761 static inline void
3762 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3764 dw_attr_node attr;
3766 attr.dw_attr = attr_kind;
3767 attr.dw_attr_val.val_class = dw_val_class_const;
3768 attr.dw_attr_val.val_entry = NULL;
3769 attr.dw_attr_val.v.val_int = int_val;
3770 add_dwarf_attr (die, &attr);
3773 static inline HOST_WIDE_INT
3774 AT_int (dw_attr_ref a)
3776 gcc_assert (a && AT_class (a) == dw_val_class_const);
3777 return a->dw_attr_val.v.val_int;
3780 /* Add an unsigned integer attribute value to a DIE. */
3782 static inline void
3783 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3784 unsigned HOST_WIDE_INT unsigned_val)
3786 dw_attr_node attr;
3788 attr.dw_attr = attr_kind;
3789 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3790 attr.dw_attr_val.val_entry = NULL;
3791 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3792 add_dwarf_attr (die, &attr);
3795 static inline unsigned HOST_WIDE_INT
3796 AT_unsigned (dw_attr_ref a)
3798 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3799 return a->dw_attr_val.v.val_unsigned;
3802 /* Add an unsigned wide integer attribute value to a DIE. */
3804 static inline void
3805 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3806 const wide_int& w)
3808 dw_attr_node attr;
3810 attr.dw_attr = attr_kind;
3811 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3812 attr.dw_attr_val.v.val_wide = ggc_alloc_cleared_wide_int ();
3813 *attr.dw_attr_val.v.val_wide = w;
3814 add_dwarf_attr (die, &attr);
3817 /* Add an unsigned double integer attribute value to a DIE. */
3819 static inline void
3820 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3821 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3823 dw_attr_node attr;
3825 attr.dw_attr = attr_kind;
3826 attr.dw_attr_val.val_class = dw_val_class_const_double;
3827 attr.dw_attr_val.val_entry = NULL;
3828 attr.dw_attr_val.v.val_double.high = high;
3829 attr.dw_attr_val.v.val_double.low = low;
3830 add_dwarf_attr (die, &attr);
3833 /* Add a floating point attribute value to a DIE and return it. */
3835 static inline void
3836 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3837 unsigned int length, unsigned int elt_size, unsigned char *array)
3839 dw_attr_node attr;
3841 attr.dw_attr = attr_kind;
3842 attr.dw_attr_val.val_class = dw_val_class_vec;
3843 attr.dw_attr_val.val_entry = NULL;
3844 attr.dw_attr_val.v.val_vec.length = length;
3845 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3846 attr.dw_attr_val.v.val_vec.array = array;
3847 add_dwarf_attr (die, &attr);
3850 /* Add an 8-byte data attribute value to a DIE. */
3852 static inline void
3853 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3854 unsigned char data8[8])
3856 dw_attr_node attr;
3858 attr.dw_attr = attr_kind;
3859 attr.dw_attr_val.val_class = dw_val_class_data8;
3860 attr.dw_attr_val.val_entry = NULL;
3861 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3862 add_dwarf_attr (die, &attr);
3865 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3866 dwarf_split_debug_info, address attributes in dies destined for the
3867 final executable have force_direct set to avoid using indexed
3868 references. */
3870 static inline void
3871 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3872 bool force_direct)
3874 dw_attr_node attr;
3875 char * lbl_id;
3877 lbl_id = xstrdup (lbl_low);
3878 attr.dw_attr = DW_AT_low_pc;
3879 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3880 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3881 if (dwarf_split_debug_info && !force_direct)
3882 attr.dw_attr_val.val_entry
3883 = add_addr_table_entry (lbl_id, ate_kind_label);
3884 else
3885 attr.dw_attr_val.val_entry = NULL;
3886 add_dwarf_attr (die, &attr);
3888 attr.dw_attr = DW_AT_high_pc;
3889 if (dwarf_version < 4)
3890 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3891 else
3892 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3893 lbl_id = xstrdup (lbl_high);
3894 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3895 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3896 && dwarf_split_debug_info && !force_direct)
3897 attr.dw_attr_val.val_entry
3898 = add_addr_table_entry (lbl_id, ate_kind_label);
3899 else
3900 attr.dw_attr_val.val_entry = NULL;
3901 add_dwarf_attr (die, &attr);
3904 /* Hash and equality functions for debug_str_hash. */
3906 static hashval_t
3907 debug_str_do_hash (const void *x)
3909 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3912 static int
3913 debug_str_eq (const void *x1, const void *x2)
3915 return strcmp ((((const struct indirect_string_node *)x1)->str),
3916 (const char *)x2) == 0;
3919 /* Add STR to the given string hash table. */
3921 static struct indirect_string_node *
3922 find_AT_string_in_table (const char *str, htab_t table)
3924 struct indirect_string_node *node;
3925 void **slot;
3927 slot = htab_find_slot_with_hash (table, str,
3928 htab_hash_string (str), INSERT);
3929 if (*slot == NULL)
3931 node = ggc_alloc_cleared_indirect_string_node ();
3932 node->str = ggc_strdup (str);
3933 *slot = node;
3935 else
3936 node = (struct indirect_string_node *) *slot;
3938 node->refcount++;
3939 return node;
3942 /* Add STR to the indirect string hash table. */
3944 static struct indirect_string_node *
3945 find_AT_string (const char *str)
3947 if (! debug_str_hash)
3948 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3949 debug_str_eq, NULL);
3951 return find_AT_string_in_table (str, debug_str_hash);
3954 /* Add a string attribute value to a DIE. */
3956 static inline void
3957 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3959 dw_attr_node attr;
3960 struct indirect_string_node *node;
3962 node = find_AT_string (str);
3964 attr.dw_attr = attr_kind;
3965 attr.dw_attr_val.val_class = dw_val_class_str;
3966 attr.dw_attr_val.val_entry = NULL;
3967 attr.dw_attr_val.v.val_str = node;
3968 add_dwarf_attr (die, &attr);
3971 static inline const char *
3972 AT_string (dw_attr_ref a)
3974 gcc_assert (a && AT_class (a) == dw_val_class_str);
3975 return a->dw_attr_val.v.val_str->str;
3978 /* Call this function directly to bypass AT_string_form's logic to put
3979 the string inline in the die. */
3981 static void
3982 set_indirect_string (struct indirect_string_node *node)
3984 char label[32];
3985 /* Already indirect is a no op. */
3986 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3988 gcc_assert (node->label);
3989 return;
3991 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3992 ++dw2_string_counter;
3993 node->label = xstrdup (label);
3995 if (!dwarf_split_debug_info)
3997 node->form = DW_FORM_strp;
3998 node->index = NOT_INDEXED;
4000 else
4002 node->form = DW_FORM_GNU_str_index;
4003 node->index = NO_INDEX_ASSIGNED;
4007 /* Find out whether a string should be output inline in DIE
4008 or out-of-line in .debug_str section. */
4010 static enum dwarf_form
4011 find_string_form (struct indirect_string_node *node)
4013 unsigned int len;
4015 if (node->form)
4016 return node->form;
4018 len = strlen (node->str) + 1;
4020 /* If the string is shorter or equal to the size of the reference, it is
4021 always better to put it inline. */
4022 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4023 return node->form = DW_FORM_string;
4025 /* If we cannot expect the linker to merge strings in .debug_str
4026 section, only put it into .debug_str if it is worth even in this
4027 single module. */
4028 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4029 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4030 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4031 return node->form = DW_FORM_string;
4033 set_indirect_string (node);
4035 return node->form;
4038 /* Find out whether the string referenced from the attribute should be
4039 output inline in DIE or out-of-line in .debug_str section. */
4041 static enum dwarf_form
4042 AT_string_form (dw_attr_ref a)
4044 gcc_assert (a && AT_class (a) == dw_val_class_str);
4045 return find_string_form (a->dw_attr_val.v.val_str);
4048 /* Add a DIE reference attribute value to a DIE. */
4050 static inline void
4051 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4053 dw_attr_node attr;
4055 #ifdef ENABLE_CHECKING
4056 gcc_assert (targ_die != NULL);
4057 #else
4058 /* With LTO we can end up trying to reference something we didn't create
4059 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4060 if (targ_die == NULL)
4061 return;
4062 #endif
4064 attr.dw_attr = attr_kind;
4065 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4066 attr.dw_attr_val.val_entry = NULL;
4067 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4068 attr.dw_attr_val.v.val_die_ref.external = 0;
4069 add_dwarf_attr (die, &attr);
4072 /* Change DIE reference REF to point to NEW_DIE instead. */
4074 static inline void
4075 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4077 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4078 ref->dw_attr_val.v.val_die_ref.die = new_die;
4079 ref->dw_attr_val.v.val_die_ref.external = 0;
4082 /* Add an AT_specification attribute to a DIE, and also make the back
4083 pointer from the specification to the definition. */
4085 static inline void
4086 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4088 add_AT_die_ref (die, DW_AT_specification, targ_die);
4089 gcc_assert (!targ_die->die_definition);
4090 targ_die->die_definition = die;
4093 static inline dw_die_ref
4094 AT_ref (dw_attr_ref a)
4096 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4097 return a->dw_attr_val.v.val_die_ref.die;
4100 static inline int
4101 AT_ref_external (dw_attr_ref a)
4103 if (a && AT_class (a) == dw_val_class_die_ref)
4104 return a->dw_attr_val.v.val_die_ref.external;
4106 return 0;
4109 static inline void
4110 set_AT_ref_external (dw_attr_ref a, int i)
4112 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4113 a->dw_attr_val.v.val_die_ref.external = i;
4116 /* Add an FDE reference attribute value to a DIE. */
4118 static inline void
4119 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4121 dw_attr_node attr;
4123 attr.dw_attr = attr_kind;
4124 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4125 attr.dw_attr_val.val_entry = NULL;
4126 attr.dw_attr_val.v.val_fde_index = targ_fde;
4127 add_dwarf_attr (die, &attr);
4130 /* Add a location description attribute value to a DIE. */
4132 static inline void
4133 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4135 dw_attr_node attr;
4137 attr.dw_attr = attr_kind;
4138 attr.dw_attr_val.val_class = dw_val_class_loc;
4139 attr.dw_attr_val.val_entry = NULL;
4140 attr.dw_attr_val.v.val_loc = loc;
4141 add_dwarf_attr (die, &attr);
4144 static inline dw_loc_descr_ref
4145 AT_loc (dw_attr_ref a)
4147 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4148 return a->dw_attr_val.v.val_loc;
4151 static inline void
4152 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4154 dw_attr_node attr;
4156 attr.dw_attr = attr_kind;
4157 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4158 attr.dw_attr_val.val_entry = NULL;
4159 attr.dw_attr_val.v.val_loc_list = loc_list;
4160 add_dwarf_attr (die, &attr);
4161 have_location_lists = true;
4164 static inline dw_loc_list_ref
4165 AT_loc_list (dw_attr_ref a)
4167 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4168 return a->dw_attr_val.v.val_loc_list;
4171 static inline dw_loc_list_ref *
4172 AT_loc_list_ptr (dw_attr_ref a)
4174 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4175 return &a->dw_attr_val.v.val_loc_list;
4178 /* Table of entries into the .debug_addr section. */
4180 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4182 /* Hash an address_table_entry. */
4184 static hashval_t
4185 addr_table_entry_do_hash (const void *x)
4187 const addr_table_entry *a = (const addr_table_entry *) x;
4188 switch (a->kind)
4190 case ate_kind_rtx:
4191 return iterative_hash_rtx (a->addr.rtl, 0);
4192 case ate_kind_rtx_dtprel:
4193 return iterative_hash_rtx (a->addr.rtl, 1);
4194 case ate_kind_label:
4195 return htab_hash_string (a->addr.label);
4196 default:
4197 gcc_unreachable ();
4201 /* Determine equality for two address_table_entries. */
4203 static int
4204 addr_table_entry_eq (const void *x1, const void *x2)
4206 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4207 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4209 if (a1->kind != a2->kind)
4210 return 0;
4211 switch (a1->kind)
4213 case ate_kind_rtx:
4214 case ate_kind_rtx_dtprel:
4215 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4216 case ate_kind_label:
4217 return strcmp (a1->addr.label, a2->addr.label) == 0;
4218 default:
4219 gcc_unreachable ();
4223 /* Initialize an addr_table_entry. */
4225 void
4226 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4228 e->kind = kind;
4229 switch (kind)
4231 case ate_kind_rtx:
4232 case ate_kind_rtx_dtprel:
4233 e->addr.rtl = (rtx) addr;
4234 break;
4235 case ate_kind_label:
4236 e->addr.label = (char *) addr;
4237 break;
4239 e->refcount = 0;
4240 e->index = NO_INDEX_ASSIGNED;
4243 /* Add attr to the address table entry to the table. Defer setting an
4244 index until output time. */
4246 static addr_table_entry *
4247 add_addr_table_entry (void *addr, enum ate_kind kind)
4249 addr_table_entry *node;
4250 addr_table_entry finder;
4251 void **slot;
4253 gcc_assert (dwarf_split_debug_info);
4254 if (! addr_index_table)
4255 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4256 addr_table_entry_eq, NULL);
4257 init_addr_table_entry (&finder, kind, addr);
4258 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4260 if (*slot == HTAB_EMPTY_ENTRY)
4262 node = ggc_alloc_cleared_addr_table_entry ();
4263 init_addr_table_entry (node, kind, addr);
4264 *slot = node;
4266 else
4267 node = (addr_table_entry *) *slot;
4269 node->refcount++;
4270 return node;
4273 /* Remove an entry from the addr table by decrementing its refcount.
4274 Strictly, decrementing the refcount would be enough, but the
4275 assertion that the entry is actually in the table has found
4276 bugs. */
4278 static void
4279 remove_addr_table_entry (addr_table_entry *entry)
4281 addr_table_entry *node;
4283 gcc_assert (dwarf_split_debug_info && addr_index_table);
4284 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4285 /* After an index is assigned, the table is frozen. */
4286 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4287 node->refcount--;
4290 /* Given a location list, remove all addresses it refers to from the
4291 address_table. */
4293 static void
4294 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4296 for (; descr; descr = descr->dw_loc_next)
4297 if (descr->dw_loc_oprnd1.val_entry != NULL)
4299 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4300 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4304 /* A helper function for dwarf2out_finish called through
4305 htab_traverse. Assign an addr_table_entry its index. All entries
4306 must be collected into the table when this function is called,
4307 because the indexing code relies on htab_traverse to traverse nodes
4308 in the same order for each run. */
4310 static int
4311 index_addr_table_entry (void **h, void *v)
4313 addr_table_entry *node = (addr_table_entry *) *h;
4314 unsigned int *index = (unsigned int *) v;
4316 /* Don't index unreferenced nodes. */
4317 if (node->refcount == 0)
4318 return 1;
4320 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4321 node->index = *index;
4322 *index += 1;
4324 return 1;
4327 /* Add an address constant attribute value to a DIE. When using
4328 dwarf_split_debug_info, address attributes in dies destined for the
4329 final executable should be direct references--setting the parameter
4330 force_direct ensures this behavior. */
4332 static inline void
4333 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4334 bool force_direct)
4336 dw_attr_node attr;
4338 attr.dw_attr = attr_kind;
4339 attr.dw_attr_val.val_class = dw_val_class_addr;
4340 attr.dw_attr_val.v.val_addr = addr;
4341 if (dwarf_split_debug_info && !force_direct)
4342 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4343 else
4344 attr.dw_attr_val.val_entry = NULL;
4345 add_dwarf_attr (die, &attr);
4348 /* Get the RTX from to an address DIE attribute. */
4350 static inline rtx
4351 AT_addr (dw_attr_ref a)
4353 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4354 return a->dw_attr_val.v.val_addr;
4357 /* Add a file attribute value to a DIE. */
4359 static inline void
4360 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4361 struct dwarf_file_data *fd)
4363 dw_attr_node attr;
4365 attr.dw_attr = attr_kind;
4366 attr.dw_attr_val.val_class = dw_val_class_file;
4367 attr.dw_attr_val.val_entry = NULL;
4368 attr.dw_attr_val.v.val_file = fd;
4369 add_dwarf_attr (die, &attr);
4372 /* Get the dwarf_file_data from a file DIE attribute. */
4374 static inline struct dwarf_file_data *
4375 AT_file (dw_attr_ref a)
4377 gcc_assert (a && AT_class (a) == dw_val_class_file);
4378 return a->dw_attr_val.v.val_file;
4381 /* Add a vms delta attribute value to a DIE. */
4383 static inline void
4384 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4385 const char *lbl1, const char *lbl2)
4387 dw_attr_node attr;
4389 attr.dw_attr = attr_kind;
4390 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4391 attr.dw_attr_val.val_entry = NULL;
4392 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4393 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4394 add_dwarf_attr (die, &attr);
4397 /* Add a label identifier attribute value to a DIE. */
4399 static inline void
4400 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4401 const char *lbl_id)
4403 dw_attr_node attr;
4405 attr.dw_attr = attr_kind;
4406 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4407 attr.dw_attr_val.val_entry = NULL;
4408 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4409 if (dwarf_split_debug_info)
4410 attr.dw_attr_val.val_entry
4411 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4412 ate_kind_label);
4413 add_dwarf_attr (die, &attr);
4416 /* Add a section offset attribute value to a DIE, an offset into the
4417 debug_line section. */
4419 static inline void
4420 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4421 const char *label)
4423 dw_attr_node attr;
4425 attr.dw_attr = attr_kind;
4426 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4427 attr.dw_attr_val.val_entry = NULL;
4428 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4429 add_dwarf_attr (die, &attr);
4432 /* Add a section offset attribute value to a DIE, an offset into the
4433 debug_macinfo section. */
4435 static inline void
4436 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4437 const char *label)
4439 dw_attr_node attr;
4441 attr.dw_attr = attr_kind;
4442 attr.dw_attr_val.val_class = dw_val_class_macptr;
4443 attr.dw_attr_val.val_entry = NULL;
4444 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4445 add_dwarf_attr (die, &attr);
4448 /* Add an offset attribute value to a DIE. */
4450 static inline void
4451 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4452 unsigned HOST_WIDE_INT offset)
4454 dw_attr_node attr;
4456 attr.dw_attr = attr_kind;
4457 attr.dw_attr_val.val_class = dw_val_class_offset;
4458 attr.dw_attr_val.val_entry = NULL;
4459 attr.dw_attr_val.v.val_offset = offset;
4460 add_dwarf_attr (die, &attr);
4463 /* Add a range_list attribute value to a DIE. When using
4464 dwarf_split_debug_info, address attributes in dies destined for the
4465 final executable should be direct references--setting the parameter
4466 force_direct ensures this behavior. */
4468 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4469 #define RELOCATED_OFFSET (NULL)
4471 static void
4472 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4473 long unsigned int offset, bool force_direct)
4475 dw_attr_node attr;
4477 attr.dw_attr = attr_kind;
4478 attr.dw_attr_val.val_class = dw_val_class_range_list;
4479 /* For the range_list attribute, use val_entry to store whether the
4480 offset should follow split-debug-info or normal semantics. This
4481 value is read in output_range_list_offset. */
4482 if (dwarf_split_debug_info && !force_direct)
4483 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4484 else
4485 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4486 attr.dw_attr_val.v.val_offset = offset;
4487 add_dwarf_attr (die, &attr);
4490 /* Return the start label of a delta attribute. */
4492 static inline const char *
4493 AT_vms_delta1 (dw_attr_ref a)
4495 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4496 return a->dw_attr_val.v.val_vms_delta.lbl1;
4499 /* Return the end label of a delta attribute. */
4501 static inline const char *
4502 AT_vms_delta2 (dw_attr_ref a)
4504 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4505 return a->dw_attr_val.v.val_vms_delta.lbl2;
4508 static inline const char *
4509 AT_lbl (dw_attr_ref a)
4511 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4512 || AT_class (a) == dw_val_class_lineptr
4513 || AT_class (a) == dw_val_class_macptr
4514 || AT_class (a) == dw_val_class_high_pc));
4515 return a->dw_attr_val.v.val_lbl_id;
4518 /* Get the attribute of type attr_kind. */
4520 static dw_attr_ref
4521 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4523 dw_attr_ref a;
4524 unsigned ix;
4525 dw_die_ref spec = NULL;
4527 if (! die)
4528 return NULL;
4530 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4531 if (a->dw_attr == attr_kind)
4532 return a;
4533 else if (a->dw_attr == DW_AT_specification
4534 || a->dw_attr == DW_AT_abstract_origin)
4535 spec = AT_ref (a);
4537 if (spec)
4538 return get_AT (spec, attr_kind);
4540 return NULL;
4543 /* Returns the parent of the declaration of DIE. */
4545 static dw_die_ref
4546 get_die_parent (dw_die_ref die)
4548 dw_die_ref t;
4550 if (!die)
4551 return NULL;
4553 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4554 || (t = get_AT_ref (die, DW_AT_specification)))
4555 die = t;
4557 return die->die_parent;
4560 /* Return the "low pc" attribute value, typically associated with a subprogram
4561 DIE. Return null if the "low pc" attribute is either not present, or if it
4562 cannot be represented as an assembler label identifier. */
4564 static inline const char *
4565 get_AT_low_pc (dw_die_ref die)
4567 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4569 return a ? AT_lbl (a) : NULL;
4572 /* Return the "high pc" attribute value, typically associated with a subprogram
4573 DIE. Return null if the "high pc" attribute is either not present, or if it
4574 cannot be represented as an assembler label identifier. */
4576 static inline const char *
4577 get_AT_hi_pc (dw_die_ref die)
4579 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4581 return a ? AT_lbl (a) : NULL;
4584 /* Return the value of the string attribute designated by ATTR_KIND, or
4585 NULL if it is not present. */
4587 static inline const char *
4588 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4590 dw_attr_ref a = get_AT (die, attr_kind);
4592 return a ? AT_string (a) : NULL;
4595 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4596 if it is not present. */
4598 static inline int
4599 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4601 dw_attr_ref a = get_AT (die, attr_kind);
4603 return a ? AT_flag (a) : 0;
4606 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4607 if it is not present. */
4609 static inline unsigned
4610 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4612 dw_attr_ref a = get_AT (die, attr_kind);
4614 return a ? AT_unsigned (a) : 0;
4617 static inline dw_die_ref
4618 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4620 dw_attr_ref a = get_AT (die, attr_kind);
4622 return a ? AT_ref (a) : NULL;
4625 static inline struct dwarf_file_data *
4626 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4628 dw_attr_ref a = get_AT (die, attr_kind);
4630 return a ? AT_file (a) : NULL;
4633 /* Return TRUE if the language is C++. */
4635 static inline bool
4636 is_cxx (void)
4638 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4640 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4643 /* Return TRUE if the language is Java. */
4645 static inline bool
4646 is_java (void)
4648 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4650 return lang == DW_LANG_Java;
4653 /* Return TRUE if the language is Fortran. */
4655 static inline bool
4656 is_fortran (void)
4658 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4660 return (lang == DW_LANG_Fortran77
4661 || lang == DW_LANG_Fortran90
4662 || lang == DW_LANG_Fortran95);
4665 /* Return TRUE if the language is Ada. */
4667 static inline bool
4668 is_ada (void)
4670 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4672 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4675 /* Remove the specified attribute if present. */
4677 static void
4678 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4680 dw_attr_ref a;
4681 unsigned ix;
4683 if (! die)
4684 return;
4686 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4687 if (a->dw_attr == attr_kind)
4689 if (AT_class (a) == dw_val_class_str)
4690 if (a->dw_attr_val.v.val_str->refcount)
4691 a->dw_attr_val.v.val_str->refcount--;
4693 /* vec::ordered_remove should help reduce the number of abbrevs
4694 that are needed. */
4695 die->die_attr->ordered_remove (ix);
4696 return;
4700 /* Remove CHILD from its parent. PREV must have the property that
4701 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4703 static void
4704 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4706 gcc_assert (child->die_parent == prev->die_parent);
4707 gcc_assert (prev->die_sib == child);
4708 if (prev == child)
4710 gcc_assert (child->die_parent->die_child == child);
4711 prev = NULL;
4713 else
4714 prev->die_sib = child->die_sib;
4715 if (child->die_parent->die_child == child)
4716 child->die_parent->die_child = prev;
4719 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4720 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4722 static void
4723 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4725 dw_die_ref parent = old_child->die_parent;
4727 gcc_assert (parent == prev->die_parent);
4728 gcc_assert (prev->die_sib == old_child);
4730 new_child->die_parent = parent;
4731 if (prev == old_child)
4733 gcc_assert (parent->die_child == old_child);
4734 new_child->die_sib = new_child;
4736 else
4738 prev->die_sib = new_child;
4739 new_child->die_sib = old_child->die_sib;
4741 if (old_child->die_parent->die_child == old_child)
4742 old_child->die_parent->die_child = new_child;
4745 /* Move all children from OLD_PARENT to NEW_PARENT. */
4747 static void
4748 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4750 dw_die_ref c;
4751 new_parent->die_child = old_parent->die_child;
4752 old_parent->die_child = NULL;
4753 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4756 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4757 matches TAG. */
4759 static void
4760 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4762 dw_die_ref c;
4764 c = die->die_child;
4765 if (c) do {
4766 dw_die_ref prev = c;
4767 c = c->die_sib;
4768 while (c->die_tag == tag)
4770 remove_child_with_prev (c, prev);
4771 /* Might have removed every child. */
4772 if (c == c->die_sib)
4773 return;
4774 c = c->die_sib;
4776 } while (c != die->die_child);
4779 /* Add a CHILD_DIE as the last child of DIE. */
4781 static void
4782 add_child_die (dw_die_ref die, dw_die_ref child_die)
4784 /* FIXME this should probably be an assert. */
4785 if (! die || ! child_die)
4786 return;
4787 gcc_assert (die != child_die);
4789 child_die->die_parent = die;
4790 if (die->die_child)
4792 child_die->die_sib = die->die_child->die_sib;
4793 die->die_child->die_sib = child_die;
4795 else
4796 child_die->die_sib = child_die;
4797 die->die_child = child_die;
4800 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4801 is the specification, to the end of PARENT's list of children.
4802 This is done by removing and re-adding it. */
4804 static void
4805 splice_child_die (dw_die_ref parent, dw_die_ref child)
4807 dw_die_ref p;
4809 /* We want the declaration DIE from inside the class, not the
4810 specification DIE at toplevel. */
4811 if (child->die_parent != parent)
4813 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4815 if (tmp)
4816 child = tmp;
4819 gcc_assert (child->die_parent == parent
4820 || (child->die_parent
4821 == get_AT_ref (parent, DW_AT_specification)));
4823 for (p = child->die_parent->die_child; ; p = p->die_sib)
4824 if (p->die_sib == child)
4826 remove_child_with_prev (child, p);
4827 break;
4830 add_child_die (parent, child);
4833 /* Return a pointer to a newly created DIE node. */
4835 static inline dw_die_ref
4836 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4838 dw_die_ref die = ggc_alloc_cleared_die_node ();
4840 die->die_tag = tag_value;
4842 if (parent_die != NULL)
4843 add_child_die (parent_die, die);
4844 else
4846 limbo_die_node *limbo_node;
4848 limbo_node = ggc_alloc_cleared_limbo_die_node ();
4849 limbo_node->die = die;
4850 limbo_node->created_for = t;
4851 limbo_node->next = limbo_die_list;
4852 limbo_die_list = limbo_node;
4855 return die;
4858 /* Return the DIE associated with the given type specifier. */
4860 static inline dw_die_ref
4861 lookup_type_die (tree type)
4863 return TYPE_SYMTAB_DIE (type);
4866 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4867 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4868 anonymous type instead the one of the naming typedef. */
4870 static inline dw_die_ref
4871 strip_naming_typedef (tree type, dw_die_ref type_die)
4873 if (type
4874 && TREE_CODE (type) == RECORD_TYPE
4875 && type_die
4876 && type_die->die_tag == DW_TAG_typedef
4877 && is_naming_typedef_decl (TYPE_NAME (type)))
4878 type_die = get_AT_ref (type_die, DW_AT_type);
4879 return type_die;
4882 /* Like lookup_type_die, but if type is an anonymous type named by a
4883 typedef[1], return the DIE of the anonymous type instead the one of
4884 the naming typedef. This is because in gen_typedef_die, we did
4885 equate the anonymous struct named by the typedef with the DIE of
4886 the naming typedef. So by default, lookup_type_die on an anonymous
4887 struct yields the DIE of the naming typedef.
4889 [1]: Read the comment of is_naming_typedef_decl to learn about what
4890 a naming typedef is. */
4892 static inline dw_die_ref
4893 lookup_type_die_strip_naming_typedef (tree type)
4895 dw_die_ref die = lookup_type_die (type);
4896 return strip_naming_typedef (type, die);
4899 /* Equate a DIE to a given type specifier. */
4901 static inline void
4902 equate_type_number_to_die (tree type, dw_die_ref type_die)
4904 TYPE_SYMTAB_DIE (type) = type_die;
4907 /* Returns a hash value for X (which really is a die_struct). */
4909 static hashval_t
4910 decl_die_table_hash (const void *x)
4912 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4915 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4917 static int
4918 decl_die_table_eq (const void *x, const void *y)
4920 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4923 /* Return the DIE associated with a given declaration. */
4925 static inline dw_die_ref
4926 lookup_decl_die (tree decl)
4928 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4931 /* Returns a hash value for X (which really is a var_loc_list). */
4933 static hashval_t
4934 decl_loc_table_hash (const void *x)
4936 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4939 /* Return nonzero if decl_id of var_loc_list X is the same as
4940 UID of decl *Y. */
4942 static int
4943 decl_loc_table_eq (const void *x, const void *y)
4945 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4948 /* Return the var_loc list associated with a given declaration. */
4950 static inline var_loc_list *
4951 lookup_decl_loc (const_tree decl)
4953 if (!decl_loc_table)
4954 return NULL;
4955 return (var_loc_list *)
4956 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4959 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4961 static hashval_t
4962 cached_dw_loc_list_table_hash (const void *x)
4964 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4967 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4968 UID of decl *Y. */
4970 static int
4971 cached_dw_loc_list_table_eq (const void *x, const void *y)
4973 return (((const cached_dw_loc_list *) x)->decl_id
4974 == DECL_UID ((const_tree) y));
4977 /* Equate a DIE to a particular declaration. */
4979 static void
4980 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4982 unsigned int decl_id = DECL_UID (decl);
4983 void **slot;
4985 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4986 *slot = decl_die;
4987 decl_die->decl_id = decl_id;
4990 /* Return how many bits covers PIECE EXPR_LIST. */
4992 static int
4993 decl_piece_bitsize (rtx piece)
4995 int ret = (int) GET_MODE (piece);
4996 if (ret)
4997 return ret;
4998 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4999 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5000 return INTVAL (XEXP (XEXP (piece, 0), 0));
5003 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5005 static rtx *
5006 decl_piece_varloc_ptr (rtx piece)
5008 if ((int) GET_MODE (piece))
5009 return &XEXP (piece, 0);
5010 else
5011 return &XEXP (XEXP (piece, 0), 1);
5014 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5015 Next is the chain of following piece nodes. */
5017 static rtx
5018 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5020 if (bitsize <= (int) MAX_MACHINE_MODE)
5021 return alloc_EXPR_LIST (bitsize, loc_note, next);
5022 else
5023 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5024 GEN_INT (bitsize),
5025 loc_note), next);
5028 /* Return rtx that should be stored into loc field for
5029 LOC_NOTE and BITPOS/BITSIZE. */
5031 static rtx
5032 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5033 HOST_WIDE_INT bitsize)
5035 if (bitsize != -1)
5037 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5038 if (bitpos != 0)
5039 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5041 return loc_note;
5044 /* This function either modifies location piece list *DEST in
5045 place (if SRC and INNER is NULL), or copies location piece list
5046 *SRC to *DEST while modifying it. Location BITPOS is modified
5047 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5048 not copied and if needed some padding around it is added.
5049 When modifying in place, DEST should point to EXPR_LIST where
5050 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5051 to the start of the whole list and INNER points to the EXPR_LIST
5052 where earlier pieces cover PIECE_BITPOS bits. */
5054 static void
5055 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5056 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5057 HOST_WIDE_INT bitsize, rtx loc_note)
5059 int diff;
5060 bool copy = inner != NULL;
5062 if (copy)
5064 /* First copy all nodes preceding the current bitpos. */
5065 while (src != inner)
5067 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5068 decl_piece_bitsize (*src), NULL_RTX);
5069 dest = &XEXP (*dest, 1);
5070 src = &XEXP (*src, 1);
5073 /* Add padding if needed. */
5074 if (bitpos != piece_bitpos)
5076 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5077 copy ? NULL_RTX : *dest);
5078 dest = &XEXP (*dest, 1);
5080 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5082 gcc_assert (!copy);
5083 /* A piece with correct bitpos and bitsize already exist,
5084 just update the location for it and return. */
5085 *decl_piece_varloc_ptr (*dest) = loc_note;
5086 return;
5088 /* Add the piece that changed. */
5089 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5090 dest = &XEXP (*dest, 1);
5091 /* Skip over pieces that overlap it. */
5092 diff = bitpos - piece_bitpos + bitsize;
5093 if (!copy)
5094 src = dest;
5095 while (diff > 0 && *src)
5097 rtx piece = *src;
5098 diff -= decl_piece_bitsize (piece);
5099 if (copy)
5100 src = &XEXP (piece, 1);
5101 else
5103 *src = XEXP (piece, 1);
5104 free_EXPR_LIST_node (piece);
5107 /* Add padding if needed. */
5108 if (diff < 0 && *src)
5110 if (!copy)
5111 dest = src;
5112 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5113 dest = &XEXP (*dest, 1);
5115 if (!copy)
5116 return;
5117 /* Finally copy all nodes following it. */
5118 while (*src)
5120 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5121 decl_piece_bitsize (*src), NULL_RTX);
5122 dest = &XEXP (*dest, 1);
5123 src = &XEXP (*src, 1);
5127 /* Add a variable location node to the linked list for DECL. */
5129 static struct var_loc_node *
5130 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5132 unsigned int decl_id;
5133 var_loc_list *temp;
5134 void **slot;
5135 struct var_loc_node *loc = NULL;
5136 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5138 if (TREE_CODE (decl) == VAR_DECL
5139 && DECL_HAS_DEBUG_EXPR_P (decl))
5141 tree realdecl = DECL_DEBUG_EXPR (decl);
5142 if (handled_component_p (realdecl)
5143 || (TREE_CODE (realdecl) == MEM_REF
5144 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5146 HOST_WIDE_INT maxsize;
5147 tree innerdecl;
5148 innerdecl
5149 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5150 if (!DECL_P (innerdecl)
5151 || DECL_IGNORED_P (innerdecl)
5152 || TREE_STATIC (innerdecl)
5153 || bitsize <= 0
5154 || bitpos + bitsize > 256
5155 || bitsize != maxsize)
5156 return NULL;
5157 decl = innerdecl;
5161 decl_id = DECL_UID (decl);
5162 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5163 if (*slot == NULL)
5165 temp = ggc_alloc_cleared_var_loc_list ();
5166 temp->decl_id = decl_id;
5167 *slot = temp;
5169 else
5170 temp = (var_loc_list *) *slot;
5172 /* For PARM_DECLs try to keep around the original incoming value,
5173 even if that means we'll emit a zero-range .debug_loc entry. */
5174 if (temp->last
5175 && temp->first == temp->last
5176 && TREE_CODE (decl) == PARM_DECL
5177 && NOTE_P (temp->first->loc)
5178 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5179 && DECL_INCOMING_RTL (decl)
5180 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5181 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5182 == GET_CODE (DECL_INCOMING_RTL (decl))
5183 && prev_real_insn (temp->first->loc) == NULL_RTX
5184 && (bitsize != -1
5185 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5186 NOTE_VAR_LOCATION_LOC (loc_note))
5187 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5188 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5190 loc = ggc_alloc_cleared_var_loc_node ();
5191 temp->first->next = loc;
5192 temp->last = loc;
5193 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5195 else if (temp->last)
5197 struct var_loc_node *last = temp->last, *unused = NULL;
5198 rtx *piece_loc = NULL, last_loc_note;
5199 int piece_bitpos = 0;
5200 if (last->next)
5202 last = last->next;
5203 gcc_assert (last->next == NULL);
5205 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5207 piece_loc = &last->loc;
5210 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5211 if (piece_bitpos + cur_bitsize > bitpos)
5212 break;
5213 piece_bitpos += cur_bitsize;
5214 piece_loc = &XEXP (*piece_loc, 1);
5216 while (*piece_loc);
5218 /* TEMP->LAST here is either pointer to the last but one or
5219 last element in the chained list, LAST is pointer to the
5220 last element. */
5221 if (label && strcmp (last->label, label) == 0)
5223 /* For SRA optimized variables if there weren't any real
5224 insns since last note, just modify the last node. */
5225 if (piece_loc != NULL)
5227 adjust_piece_list (piece_loc, NULL, NULL,
5228 bitpos, piece_bitpos, bitsize, loc_note);
5229 return NULL;
5231 /* If the last note doesn't cover any instructions, remove it. */
5232 if (temp->last != last)
5234 temp->last->next = NULL;
5235 unused = last;
5236 last = temp->last;
5237 gcc_assert (strcmp (last->label, label) != 0);
5239 else
5241 gcc_assert (temp->first == temp->last
5242 || (temp->first->next == temp->last
5243 && TREE_CODE (decl) == PARM_DECL));
5244 memset (temp->last, '\0', sizeof (*temp->last));
5245 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5246 return temp->last;
5249 if (bitsize == -1 && NOTE_P (last->loc))
5250 last_loc_note = last->loc;
5251 else if (piece_loc != NULL
5252 && *piece_loc != NULL_RTX
5253 && piece_bitpos == bitpos
5254 && decl_piece_bitsize (*piece_loc) == bitsize)
5255 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5256 else
5257 last_loc_note = NULL_RTX;
5258 /* If the current location is the same as the end of the list,
5259 and either both or neither of the locations is uninitialized,
5260 we have nothing to do. */
5261 if (last_loc_note == NULL_RTX
5262 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5263 NOTE_VAR_LOCATION_LOC (loc_note)))
5264 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5265 != NOTE_VAR_LOCATION_STATUS (loc_note))
5266 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5267 == VAR_INIT_STATUS_UNINITIALIZED)
5268 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5269 == VAR_INIT_STATUS_UNINITIALIZED))))
5271 /* Add LOC to the end of list and update LAST. If the last
5272 element of the list has been removed above, reuse its
5273 memory for the new node, otherwise allocate a new one. */
5274 if (unused)
5276 loc = unused;
5277 memset (loc, '\0', sizeof (*loc));
5279 else
5280 loc = ggc_alloc_cleared_var_loc_node ();
5281 if (bitsize == -1 || piece_loc == NULL)
5282 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5283 else
5284 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5285 bitpos, piece_bitpos, bitsize, loc_note);
5286 last->next = loc;
5287 /* Ensure TEMP->LAST will point either to the new last but one
5288 element of the chain, or to the last element in it. */
5289 if (last != temp->last)
5290 temp->last = last;
5292 else if (unused)
5293 ggc_free (unused);
5295 else
5297 loc = ggc_alloc_cleared_var_loc_node ();
5298 temp->first = loc;
5299 temp->last = loc;
5300 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5302 return loc;
5305 /* Keep track of the number of spaces used to indent the
5306 output of the debugging routines that print the structure of
5307 the DIE internal representation. */
5308 static int print_indent;
5310 /* Indent the line the number of spaces given by print_indent. */
5312 static inline void
5313 print_spaces (FILE *outfile)
5315 fprintf (outfile, "%*s", print_indent, "");
5318 /* Print a type signature in hex. */
5320 static inline void
5321 print_signature (FILE *outfile, char *sig)
5323 int i;
5325 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5326 fprintf (outfile, "%02x", sig[i] & 0xff);
5329 /* Print the information associated with a given DIE, and its children.
5330 This routine is a debugging aid only. */
5332 static void
5333 print_die (dw_die_ref die, FILE *outfile)
5335 dw_attr_ref a;
5336 dw_die_ref c;
5337 unsigned ix;
5339 print_spaces (outfile);
5340 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5341 die->die_offset, dwarf_tag_name (die->die_tag),
5342 (void*) die);
5343 print_spaces (outfile);
5344 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5345 fprintf (outfile, " offset: %ld", die->die_offset);
5346 fprintf (outfile, " mark: %d\n", die->die_mark);
5348 if (die->comdat_type_p)
5350 print_spaces (outfile);
5351 fprintf (outfile, " signature: ");
5352 print_signature (outfile, die->die_id.die_type_node->signature);
5353 fprintf (outfile, "\n");
5356 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5358 print_spaces (outfile);
5359 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5361 switch (AT_class (a))
5363 case dw_val_class_addr:
5364 fprintf (outfile, "address");
5365 break;
5366 case dw_val_class_offset:
5367 fprintf (outfile, "offset");
5368 break;
5369 case dw_val_class_loc:
5370 fprintf (outfile, "location descriptor");
5371 break;
5372 case dw_val_class_loc_list:
5373 fprintf (outfile, "location list -> label:%s",
5374 AT_loc_list (a)->ll_symbol);
5375 break;
5376 case dw_val_class_range_list:
5377 fprintf (outfile, "range list");
5378 break;
5379 case dw_val_class_const:
5380 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5381 break;
5382 case dw_val_class_unsigned_const:
5383 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5384 break;
5385 case dw_val_class_const_double:
5386 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5387 HOST_WIDE_INT_PRINT_UNSIGNED")",
5388 a->dw_attr_val.v.val_double.high,
5389 a->dw_attr_val.v.val_double.low);
5390 break;
5391 case dw_val_class_wide_int:
5393 int i = a->dw_attr_val.v.val_wide->get_len ();
5394 fprintf (outfile, "constant (");
5395 gcc_assert (i > 0);
5396 if (a->dw_attr_val.v.val_wide->elt (i) == 0)
5397 fprintf (outfile, "0x");
5398 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5399 a->dw_attr_val.v.val_wide->elt (--i));
5400 while (-- i >= 0)
5401 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5402 a->dw_attr_val.v.val_wide->elt (i));
5403 fprintf (outfile, ")");
5404 break;
5406 case dw_val_class_vec:
5407 fprintf (outfile, "floating-point or vector constant");
5408 break;
5409 case dw_val_class_flag:
5410 fprintf (outfile, "%u", AT_flag (a));
5411 break;
5412 case dw_val_class_die_ref:
5413 if (AT_ref (a) != NULL)
5415 if (AT_ref (a)->comdat_type_p)
5417 fprintf (outfile, "die -> signature: ");
5418 print_signature (outfile,
5419 AT_ref (a)->die_id.die_type_node->signature);
5421 else if (AT_ref (a)->die_id.die_symbol)
5422 fprintf (outfile, "die -> label: %s",
5423 AT_ref (a)->die_id.die_symbol);
5424 else
5425 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5426 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5428 else
5429 fprintf (outfile, "die -> <null>");
5430 break;
5431 case dw_val_class_vms_delta:
5432 fprintf (outfile, "delta: @slotcount(%s-%s)",
5433 AT_vms_delta2 (a), AT_vms_delta1 (a));
5434 break;
5435 case dw_val_class_lbl_id:
5436 case dw_val_class_lineptr:
5437 case dw_val_class_macptr:
5438 case dw_val_class_high_pc:
5439 fprintf (outfile, "label: %s", AT_lbl (a));
5440 break;
5441 case dw_val_class_str:
5442 if (AT_string (a) != NULL)
5443 fprintf (outfile, "\"%s\"", AT_string (a));
5444 else
5445 fprintf (outfile, "<null>");
5446 break;
5447 case dw_val_class_file:
5448 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5449 AT_file (a)->emitted_number);
5450 break;
5451 case dw_val_class_data8:
5453 int i;
5455 for (i = 0; i < 8; i++)
5456 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5457 break;
5459 default:
5460 break;
5463 fprintf (outfile, "\n");
5466 if (die->die_child != NULL)
5468 print_indent += 4;
5469 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5470 print_indent -= 4;
5472 if (print_indent == 0)
5473 fprintf (outfile, "\n");
5476 /* Print the information collected for a given DIE. */
5478 DEBUG_FUNCTION void
5479 debug_dwarf_die (dw_die_ref die)
5481 print_die (die, stderr);
5484 DEBUG_FUNCTION void
5485 debug (die_struct &ref)
5487 print_die (&ref, stderr);
5490 DEBUG_FUNCTION void
5491 debug (die_struct *ptr)
5493 if (ptr)
5494 debug (*ptr);
5495 else
5496 fprintf (stderr, "<nil>\n");
5500 /* Print all DWARF information collected for the compilation unit.
5501 This routine is a debugging aid only. */
5503 DEBUG_FUNCTION void
5504 debug_dwarf (void)
5506 print_indent = 0;
5507 print_die (comp_unit_die (), stderr);
5510 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5511 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5512 DIE that marks the start of the DIEs for this include file. */
5514 static dw_die_ref
5515 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5517 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5518 dw_die_ref new_unit = gen_compile_unit_die (filename);
5520 new_unit->die_sib = old_unit;
5521 return new_unit;
5524 /* Close an include-file CU and reopen the enclosing one. */
5526 static dw_die_ref
5527 pop_compile_unit (dw_die_ref old_unit)
5529 dw_die_ref new_unit = old_unit->die_sib;
5531 old_unit->die_sib = NULL;
5532 return new_unit;
5535 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5536 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5537 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5539 /* Calculate the checksum of a location expression. */
5541 static inline void
5542 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5544 int tem;
5545 hashval_t hash = 0;
5547 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5548 CHECKSUM (tem);
5549 hash = hash_loc_operands (loc, hash);
5550 CHECKSUM (hash);
5553 /* Calculate the checksum of an attribute. */
5555 static void
5556 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5558 dw_loc_descr_ref loc;
5559 rtx r;
5561 CHECKSUM (at->dw_attr);
5563 /* We don't care that this was compiled with a different compiler
5564 snapshot; if the output is the same, that's what matters. */
5565 if (at->dw_attr == DW_AT_producer)
5566 return;
5568 switch (AT_class (at))
5570 case dw_val_class_const:
5571 CHECKSUM (at->dw_attr_val.v.val_int);
5572 break;
5573 case dw_val_class_unsigned_const:
5574 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5575 break;
5576 case dw_val_class_const_double:
5577 CHECKSUM (at->dw_attr_val.v.val_double);
5578 break;
5579 case dw_val_class_wide_int:
5580 CHECKSUM (*at->dw_attr_val.v.val_wide);
5581 break;
5582 case dw_val_class_vec:
5583 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5584 (at->dw_attr_val.v.val_vec.length
5585 * at->dw_attr_val.v.val_vec.elt_size));
5586 break;
5587 case dw_val_class_flag:
5588 CHECKSUM (at->dw_attr_val.v.val_flag);
5589 break;
5590 case dw_val_class_str:
5591 CHECKSUM_STRING (AT_string (at));
5592 break;
5594 case dw_val_class_addr:
5595 r = AT_addr (at);
5596 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5597 CHECKSUM_STRING (XSTR (r, 0));
5598 break;
5600 case dw_val_class_offset:
5601 CHECKSUM (at->dw_attr_val.v.val_offset);
5602 break;
5604 case dw_val_class_loc:
5605 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5606 loc_checksum (loc, ctx);
5607 break;
5609 case dw_val_class_die_ref:
5610 die_checksum (AT_ref (at), ctx, mark);
5611 break;
5613 case dw_val_class_fde_ref:
5614 case dw_val_class_vms_delta:
5615 case dw_val_class_lbl_id:
5616 case dw_val_class_lineptr:
5617 case dw_val_class_macptr:
5618 case dw_val_class_high_pc:
5619 break;
5621 case dw_val_class_file:
5622 CHECKSUM_STRING (AT_file (at)->filename);
5623 break;
5625 case dw_val_class_data8:
5626 CHECKSUM (at->dw_attr_val.v.val_data8);
5627 break;
5629 default:
5630 break;
5634 /* Calculate the checksum of a DIE. */
5636 static void
5637 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5639 dw_die_ref c;
5640 dw_attr_ref a;
5641 unsigned ix;
5643 /* To avoid infinite recursion. */
5644 if (die->die_mark)
5646 CHECKSUM (die->die_mark);
5647 return;
5649 die->die_mark = ++(*mark);
5651 CHECKSUM (die->die_tag);
5653 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5654 attr_checksum (a, ctx, mark);
5656 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5659 #undef CHECKSUM
5660 #undef CHECKSUM_BLOCK
5661 #undef CHECKSUM_STRING
5663 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5664 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5665 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5666 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5667 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5668 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5669 #define CHECKSUM_ATTR(FOO) \
5670 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5672 /* Calculate the checksum of a number in signed LEB128 format. */
5674 static void
5675 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5677 unsigned char byte;
5678 bool more;
5680 while (1)
5682 byte = (value & 0x7f);
5683 value >>= 7;
5684 more = !((value == 0 && (byte & 0x40) == 0)
5685 || (value == -1 && (byte & 0x40) != 0));
5686 if (more)
5687 byte |= 0x80;
5688 CHECKSUM (byte);
5689 if (!more)
5690 break;
5694 /* Calculate the checksum of a number in unsigned LEB128 format. */
5696 static void
5697 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5699 while (1)
5701 unsigned char byte = (value & 0x7f);
5702 value >>= 7;
5703 if (value != 0)
5704 /* More bytes to follow. */
5705 byte |= 0x80;
5706 CHECKSUM (byte);
5707 if (value == 0)
5708 break;
5712 /* Checksum the context of the DIE. This adds the names of any
5713 surrounding namespaces or structures to the checksum. */
5715 static void
5716 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5718 const char *name;
5719 dw_die_ref spec;
5720 int tag = die->die_tag;
5722 if (tag != DW_TAG_namespace
5723 && tag != DW_TAG_structure_type
5724 && tag != DW_TAG_class_type)
5725 return;
5727 name = get_AT_string (die, DW_AT_name);
5729 spec = get_AT_ref (die, DW_AT_specification);
5730 if (spec != NULL)
5731 die = spec;
5733 if (die->die_parent != NULL)
5734 checksum_die_context (die->die_parent, ctx);
5736 CHECKSUM_ULEB128 ('C');
5737 CHECKSUM_ULEB128 (tag);
5738 if (name != NULL)
5739 CHECKSUM_STRING (name);
5742 /* Calculate the checksum of a location expression. */
5744 static inline void
5745 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5747 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5748 were emitted as a DW_FORM_sdata instead of a location expression. */
5749 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5751 CHECKSUM_ULEB128 (DW_FORM_sdata);
5752 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5753 return;
5756 /* Otherwise, just checksum the raw location expression. */
5757 while (loc != NULL)
5759 hashval_t hash = 0;
5761 CHECKSUM_ULEB128 (loc->dtprel);
5762 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5763 hash = hash_loc_operands (loc, hash);
5764 CHECKSUM (hash);
5765 loc = loc->dw_loc_next;
5769 /* Calculate the checksum of an attribute. */
5771 static void
5772 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5773 struct md5_ctx *ctx, int *mark)
5775 dw_loc_descr_ref loc;
5776 rtx r;
5778 if (AT_class (at) == dw_val_class_die_ref)
5780 dw_die_ref target_die = AT_ref (at);
5782 /* For pointer and reference types, we checksum only the (qualified)
5783 name of the target type (if there is a name). For friend entries,
5784 we checksum only the (qualified) name of the target type or function.
5785 This allows the checksum to remain the same whether the target type
5786 is complete or not. */
5787 if ((at->dw_attr == DW_AT_type
5788 && (tag == DW_TAG_pointer_type
5789 || tag == DW_TAG_reference_type
5790 || tag == DW_TAG_rvalue_reference_type
5791 || tag == DW_TAG_ptr_to_member_type))
5792 || (at->dw_attr == DW_AT_friend
5793 && tag == DW_TAG_friend))
5795 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5797 if (name_attr != NULL)
5799 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5801 if (decl == NULL)
5802 decl = target_die;
5803 CHECKSUM_ULEB128 ('N');
5804 CHECKSUM_ULEB128 (at->dw_attr);
5805 if (decl->die_parent != NULL)
5806 checksum_die_context (decl->die_parent, ctx);
5807 CHECKSUM_ULEB128 ('E');
5808 CHECKSUM_STRING (AT_string (name_attr));
5809 return;
5813 /* For all other references to another DIE, we check to see if the
5814 target DIE has already been visited. If it has, we emit a
5815 backward reference; if not, we descend recursively. */
5816 if (target_die->die_mark > 0)
5818 CHECKSUM_ULEB128 ('R');
5819 CHECKSUM_ULEB128 (at->dw_attr);
5820 CHECKSUM_ULEB128 (target_die->die_mark);
5822 else
5824 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5826 if (decl == NULL)
5827 decl = target_die;
5828 target_die->die_mark = ++(*mark);
5829 CHECKSUM_ULEB128 ('T');
5830 CHECKSUM_ULEB128 (at->dw_attr);
5831 if (decl->die_parent != NULL)
5832 checksum_die_context (decl->die_parent, ctx);
5833 die_checksum_ordered (target_die, ctx, mark);
5835 return;
5838 CHECKSUM_ULEB128 ('A');
5839 CHECKSUM_ULEB128 (at->dw_attr);
5841 switch (AT_class (at))
5843 case dw_val_class_const:
5844 CHECKSUM_ULEB128 (DW_FORM_sdata);
5845 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5846 break;
5848 case dw_val_class_unsigned_const:
5849 CHECKSUM_ULEB128 (DW_FORM_sdata);
5850 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5851 break;
5853 case dw_val_class_const_double:
5854 CHECKSUM_ULEB128 (DW_FORM_block);
5855 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5856 CHECKSUM (at->dw_attr_val.v.val_double);
5857 break;
5859 case dw_val_class_wide_int:
5860 CHECKSUM_ULEB128 (DW_FORM_block);
5861 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5862 CHECKSUM (*at->dw_attr_val.v.val_wide);
5863 break;
5865 case dw_val_class_vec:
5866 CHECKSUM_ULEB128 (DW_FORM_block);
5867 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5868 * at->dw_attr_val.v.val_vec.elt_size);
5869 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5870 (at->dw_attr_val.v.val_vec.length
5871 * at->dw_attr_val.v.val_vec.elt_size));
5872 break;
5874 case dw_val_class_flag:
5875 CHECKSUM_ULEB128 (DW_FORM_flag);
5876 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5877 break;
5879 case dw_val_class_str:
5880 CHECKSUM_ULEB128 (DW_FORM_string);
5881 CHECKSUM_STRING (AT_string (at));
5882 break;
5884 case dw_val_class_addr:
5885 r = AT_addr (at);
5886 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5887 CHECKSUM_ULEB128 (DW_FORM_string);
5888 CHECKSUM_STRING (XSTR (r, 0));
5889 break;
5891 case dw_val_class_offset:
5892 CHECKSUM_ULEB128 (DW_FORM_sdata);
5893 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5894 break;
5896 case dw_val_class_loc:
5897 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5898 loc_checksum_ordered (loc, ctx);
5899 break;
5901 case dw_val_class_fde_ref:
5902 case dw_val_class_lbl_id:
5903 case dw_val_class_lineptr:
5904 case dw_val_class_macptr:
5905 case dw_val_class_high_pc:
5906 break;
5908 case dw_val_class_file:
5909 CHECKSUM_ULEB128 (DW_FORM_string);
5910 CHECKSUM_STRING (AT_file (at)->filename);
5911 break;
5913 case dw_val_class_data8:
5914 CHECKSUM (at->dw_attr_val.v.val_data8);
5915 break;
5917 default:
5918 break;
5922 struct checksum_attributes
5924 dw_attr_ref at_name;
5925 dw_attr_ref at_type;
5926 dw_attr_ref at_friend;
5927 dw_attr_ref at_accessibility;
5928 dw_attr_ref at_address_class;
5929 dw_attr_ref at_allocated;
5930 dw_attr_ref at_artificial;
5931 dw_attr_ref at_associated;
5932 dw_attr_ref at_binary_scale;
5933 dw_attr_ref at_bit_offset;
5934 dw_attr_ref at_bit_size;
5935 dw_attr_ref at_bit_stride;
5936 dw_attr_ref at_byte_size;
5937 dw_attr_ref at_byte_stride;
5938 dw_attr_ref at_const_value;
5939 dw_attr_ref at_containing_type;
5940 dw_attr_ref at_count;
5941 dw_attr_ref at_data_location;
5942 dw_attr_ref at_data_member_location;
5943 dw_attr_ref at_decimal_scale;
5944 dw_attr_ref at_decimal_sign;
5945 dw_attr_ref at_default_value;
5946 dw_attr_ref at_digit_count;
5947 dw_attr_ref at_discr;
5948 dw_attr_ref at_discr_list;
5949 dw_attr_ref at_discr_value;
5950 dw_attr_ref at_encoding;
5951 dw_attr_ref at_endianity;
5952 dw_attr_ref at_explicit;
5953 dw_attr_ref at_is_optional;
5954 dw_attr_ref at_location;
5955 dw_attr_ref at_lower_bound;
5956 dw_attr_ref at_mutable;
5957 dw_attr_ref at_ordering;
5958 dw_attr_ref at_picture_string;
5959 dw_attr_ref at_prototyped;
5960 dw_attr_ref at_small;
5961 dw_attr_ref at_segment;
5962 dw_attr_ref at_string_length;
5963 dw_attr_ref at_threads_scaled;
5964 dw_attr_ref at_upper_bound;
5965 dw_attr_ref at_use_location;
5966 dw_attr_ref at_use_UTF8;
5967 dw_attr_ref at_variable_parameter;
5968 dw_attr_ref at_virtuality;
5969 dw_attr_ref at_visibility;
5970 dw_attr_ref at_vtable_elem_location;
5973 /* Collect the attributes that we will want to use for the checksum. */
5975 static void
5976 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5978 dw_attr_ref a;
5979 unsigned ix;
5981 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5983 switch (a->dw_attr)
5985 case DW_AT_name:
5986 attrs->at_name = a;
5987 break;
5988 case DW_AT_type:
5989 attrs->at_type = a;
5990 break;
5991 case DW_AT_friend:
5992 attrs->at_friend = a;
5993 break;
5994 case DW_AT_accessibility:
5995 attrs->at_accessibility = a;
5996 break;
5997 case DW_AT_address_class:
5998 attrs->at_address_class = a;
5999 break;
6000 case DW_AT_allocated:
6001 attrs->at_allocated = a;
6002 break;
6003 case DW_AT_artificial:
6004 attrs->at_artificial = a;
6005 break;
6006 case DW_AT_associated:
6007 attrs->at_associated = a;
6008 break;
6009 case DW_AT_binary_scale:
6010 attrs->at_binary_scale = a;
6011 break;
6012 case DW_AT_bit_offset:
6013 attrs->at_bit_offset = a;
6014 break;
6015 case DW_AT_bit_size:
6016 attrs->at_bit_size = a;
6017 break;
6018 case DW_AT_bit_stride:
6019 attrs->at_bit_stride = a;
6020 break;
6021 case DW_AT_byte_size:
6022 attrs->at_byte_size = a;
6023 break;
6024 case DW_AT_byte_stride:
6025 attrs->at_byte_stride = a;
6026 break;
6027 case DW_AT_const_value:
6028 attrs->at_const_value = a;
6029 break;
6030 case DW_AT_containing_type:
6031 attrs->at_containing_type = a;
6032 break;
6033 case DW_AT_count:
6034 attrs->at_count = a;
6035 break;
6036 case DW_AT_data_location:
6037 attrs->at_data_location = a;
6038 break;
6039 case DW_AT_data_member_location:
6040 attrs->at_data_member_location = a;
6041 break;
6042 case DW_AT_decimal_scale:
6043 attrs->at_decimal_scale = a;
6044 break;
6045 case DW_AT_decimal_sign:
6046 attrs->at_decimal_sign = a;
6047 break;
6048 case DW_AT_default_value:
6049 attrs->at_default_value = a;
6050 break;
6051 case DW_AT_digit_count:
6052 attrs->at_digit_count = a;
6053 break;
6054 case DW_AT_discr:
6055 attrs->at_discr = a;
6056 break;
6057 case DW_AT_discr_list:
6058 attrs->at_discr_list = a;
6059 break;
6060 case DW_AT_discr_value:
6061 attrs->at_discr_value = a;
6062 break;
6063 case DW_AT_encoding:
6064 attrs->at_encoding = a;
6065 break;
6066 case DW_AT_endianity:
6067 attrs->at_endianity = a;
6068 break;
6069 case DW_AT_explicit:
6070 attrs->at_explicit = a;
6071 break;
6072 case DW_AT_is_optional:
6073 attrs->at_is_optional = a;
6074 break;
6075 case DW_AT_location:
6076 attrs->at_location = a;
6077 break;
6078 case DW_AT_lower_bound:
6079 attrs->at_lower_bound = a;
6080 break;
6081 case DW_AT_mutable:
6082 attrs->at_mutable = a;
6083 break;
6084 case DW_AT_ordering:
6085 attrs->at_ordering = a;
6086 break;
6087 case DW_AT_picture_string:
6088 attrs->at_picture_string = a;
6089 break;
6090 case DW_AT_prototyped:
6091 attrs->at_prototyped = a;
6092 break;
6093 case DW_AT_small:
6094 attrs->at_small = a;
6095 break;
6096 case DW_AT_segment:
6097 attrs->at_segment = a;
6098 break;
6099 case DW_AT_string_length:
6100 attrs->at_string_length = a;
6101 break;
6102 case DW_AT_threads_scaled:
6103 attrs->at_threads_scaled = a;
6104 break;
6105 case DW_AT_upper_bound:
6106 attrs->at_upper_bound = a;
6107 break;
6108 case DW_AT_use_location:
6109 attrs->at_use_location = a;
6110 break;
6111 case DW_AT_use_UTF8:
6112 attrs->at_use_UTF8 = a;
6113 break;
6114 case DW_AT_variable_parameter:
6115 attrs->at_variable_parameter = a;
6116 break;
6117 case DW_AT_virtuality:
6118 attrs->at_virtuality = a;
6119 break;
6120 case DW_AT_visibility:
6121 attrs->at_visibility = a;
6122 break;
6123 case DW_AT_vtable_elem_location:
6124 attrs->at_vtable_elem_location = a;
6125 break;
6126 default:
6127 break;
6132 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6134 static void
6135 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6137 dw_die_ref c;
6138 dw_die_ref decl;
6139 struct checksum_attributes attrs;
6141 CHECKSUM_ULEB128 ('D');
6142 CHECKSUM_ULEB128 (die->die_tag);
6144 memset (&attrs, 0, sizeof (attrs));
6146 decl = get_AT_ref (die, DW_AT_specification);
6147 if (decl != NULL)
6148 collect_checksum_attributes (&attrs, decl);
6149 collect_checksum_attributes (&attrs, die);
6151 CHECKSUM_ATTR (attrs.at_name);
6152 CHECKSUM_ATTR (attrs.at_accessibility);
6153 CHECKSUM_ATTR (attrs.at_address_class);
6154 CHECKSUM_ATTR (attrs.at_allocated);
6155 CHECKSUM_ATTR (attrs.at_artificial);
6156 CHECKSUM_ATTR (attrs.at_associated);
6157 CHECKSUM_ATTR (attrs.at_binary_scale);
6158 CHECKSUM_ATTR (attrs.at_bit_offset);
6159 CHECKSUM_ATTR (attrs.at_bit_size);
6160 CHECKSUM_ATTR (attrs.at_bit_stride);
6161 CHECKSUM_ATTR (attrs.at_byte_size);
6162 CHECKSUM_ATTR (attrs.at_byte_stride);
6163 CHECKSUM_ATTR (attrs.at_const_value);
6164 CHECKSUM_ATTR (attrs.at_containing_type);
6165 CHECKSUM_ATTR (attrs.at_count);
6166 CHECKSUM_ATTR (attrs.at_data_location);
6167 CHECKSUM_ATTR (attrs.at_data_member_location);
6168 CHECKSUM_ATTR (attrs.at_decimal_scale);
6169 CHECKSUM_ATTR (attrs.at_decimal_sign);
6170 CHECKSUM_ATTR (attrs.at_default_value);
6171 CHECKSUM_ATTR (attrs.at_digit_count);
6172 CHECKSUM_ATTR (attrs.at_discr);
6173 CHECKSUM_ATTR (attrs.at_discr_list);
6174 CHECKSUM_ATTR (attrs.at_discr_value);
6175 CHECKSUM_ATTR (attrs.at_encoding);
6176 CHECKSUM_ATTR (attrs.at_endianity);
6177 CHECKSUM_ATTR (attrs.at_explicit);
6178 CHECKSUM_ATTR (attrs.at_is_optional);
6179 CHECKSUM_ATTR (attrs.at_location);
6180 CHECKSUM_ATTR (attrs.at_lower_bound);
6181 CHECKSUM_ATTR (attrs.at_mutable);
6182 CHECKSUM_ATTR (attrs.at_ordering);
6183 CHECKSUM_ATTR (attrs.at_picture_string);
6184 CHECKSUM_ATTR (attrs.at_prototyped);
6185 CHECKSUM_ATTR (attrs.at_small);
6186 CHECKSUM_ATTR (attrs.at_segment);
6187 CHECKSUM_ATTR (attrs.at_string_length);
6188 CHECKSUM_ATTR (attrs.at_threads_scaled);
6189 CHECKSUM_ATTR (attrs.at_upper_bound);
6190 CHECKSUM_ATTR (attrs.at_use_location);
6191 CHECKSUM_ATTR (attrs.at_use_UTF8);
6192 CHECKSUM_ATTR (attrs.at_variable_parameter);
6193 CHECKSUM_ATTR (attrs.at_virtuality);
6194 CHECKSUM_ATTR (attrs.at_visibility);
6195 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6196 CHECKSUM_ATTR (attrs.at_type);
6197 CHECKSUM_ATTR (attrs.at_friend);
6199 /* Checksum the child DIEs. */
6200 c = die->die_child;
6201 if (c) do {
6202 dw_attr_ref name_attr;
6204 c = c->die_sib;
6205 name_attr = get_AT (c, DW_AT_name);
6206 if (is_template_instantiation (c))
6208 /* Ignore instantiations of member type and function templates. */
6210 else if (name_attr != NULL
6211 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6213 /* Use a shallow checksum for named nested types and member
6214 functions. */
6215 CHECKSUM_ULEB128 ('S');
6216 CHECKSUM_ULEB128 (c->die_tag);
6217 CHECKSUM_STRING (AT_string (name_attr));
6219 else
6221 /* Use a deep checksum for other children. */
6222 /* Mark this DIE so it gets processed when unmarking. */
6223 if (c->die_mark == 0)
6224 c->die_mark = -1;
6225 die_checksum_ordered (c, ctx, mark);
6227 } while (c != die->die_child);
6229 CHECKSUM_ULEB128 (0);
6232 /* Add a type name and tag to a hash. */
6233 static void
6234 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6236 CHECKSUM_ULEB128 (tag);
6237 CHECKSUM_STRING (name);
6240 #undef CHECKSUM
6241 #undef CHECKSUM_STRING
6242 #undef CHECKSUM_ATTR
6243 #undef CHECKSUM_LEB128
6244 #undef CHECKSUM_ULEB128
6246 /* Generate the type signature for DIE. This is computed by generating an
6247 MD5 checksum over the DIE's tag, its relevant attributes, and its
6248 children. Attributes that are references to other DIEs are processed
6249 by recursion, using the MARK field to prevent infinite recursion.
6250 If the DIE is nested inside a namespace or another type, we also
6251 need to include that context in the signature. The lower 64 bits
6252 of the resulting MD5 checksum comprise the signature. */
6254 static void
6255 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6257 int mark;
6258 const char *name;
6259 unsigned char checksum[16];
6260 struct md5_ctx ctx;
6261 dw_die_ref decl;
6262 dw_die_ref parent;
6264 name = get_AT_string (die, DW_AT_name);
6265 decl = get_AT_ref (die, DW_AT_specification);
6266 parent = get_die_parent (die);
6268 /* First, compute a signature for just the type name (and its surrounding
6269 context, if any. This is stored in the type unit DIE for link-time
6270 ODR (one-definition rule) checking. */
6272 if (is_cxx () && name != NULL)
6274 md5_init_ctx (&ctx);
6276 /* Checksum the names of surrounding namespaces and structures. */
6277 if (parent != NULL)
6278 checksum_die_context (parent, &ctx);
6280 /* Checksum the current DIE. */
6281 die_odr_checksum (die->die_tag, name, &ctx);
6282 md5_finish_ctx (&ctx, checksum);
6284 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6287 /* Next, compute the complete type signature. */
6289 md5_init_ctx (&ctx);
6290 mark = 1;
6291 die->die_mark = mark;
6293 /* Checksum the names of surrounding namespaces and structures. */
6294 if (parent != NULL)
6295 checksum_die_context (parent, &ctx);
6297 /* Checksum the DIE and its children. */
6298 die_checksum_ordered (die, &ctx, &mark);
6299 unmark_all_dies (die);
6300 md5_finish_ctx (&ctx, checksum);
6302 /* Store the signature in the type node and link the type DIE and the
6303 type node together. */
6304 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6305 DWARF_TYPE_SIGNATURE_SIZE);
6306 die->comdat_type_p = true;
6307 die->die_id.die_type_node = type_node;
6308 type_node->type_die = die;
6310 /* If the DIE is a specification, link its declaration to the type node
6311 as well. */
6312 if (decl != NULL)
6314 decl->comdat_type_p = true;
6315 decl->die_id.die_type_node = type_node;
6319 /* Do the location expressions look same? */
6320 static inline int
6321 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6323 return loc1->dw_loc_opc == loc2->dw_loc_opc
6324 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6325 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6328 /* Do the values look the same? */
6329 static int
6330 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6332 dw_loc_descr_ref loc1, loc2;
6333 rtx r1, r2;
6335 if (v1->val_class != v2->val_class)
6336 return 0;
6338 switch (v1->val_class)
6340 case dw_val_class_const:
6341 return v1->v.val_int == v2->v.val_int;
6342 case dw_val_class_unsigned_const:
6343 return v1->v.val_unsigned == v2->v.val_unsigned;
6344 case dw_val_class_const_double:
6345 return v1->v.val_double.high == v2->v.val_double.high
6346 && v1->v.val_double.low == v2->v.val_double.low;
6347 case dw_val_class_wide_int:
6348 return *v1->v.val_wide == *v2->v.val_wide;
6349 case dw_val_class_vec:
6350 if (v1->v.val_vec.length != v2->v.val_vec.length
6351 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6352 return 0;
6353 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6354 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6355 return 0;
6356 return 1;
6357 case dw_val_class_flag:
6358 return v1->v.val_flag == v2->v.val_flag;
6359 case dw_val_class_str:
6360 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6362 case dw_val_class_addr:
6363 r1 = v1->v.val_addr;
6364 r2 = v2->v.val_addr;
6365 if (GET_CODE (r1) != GET_CODE (r2))
6366 return 0;
6367 return !rtx_equal_p (r1, r2);
6369 case dw_val_class_offset:
6370 return v1->v.val_offset == v2->v.val_offset;
6372 case dw_val_class_loc:
6373 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6374 loc1 && loc2;
6375 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6376 if (!same_loc_p (loc1, loc2, mark))
6377 return 0;
6378 return !loc1 && !loc2;
6380 case dw_val_class_die_ref:
6381 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6383 case dw_val_class_fde_ref:
6384 case dw_val_class_vms_delta:
6385 case dw_val_class_lbl_id:
6386 case dw_val_class_lineptr:
6387 case dw_val_class_macptr:
6388 case dw_val_class_high_pc:
6389 return 1;
6391 case dw_val_class_file:
6392 return v1->v.val_file == v2->v.val_file;
6394 case dw_val_class_data8:
6395 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6397 default:
6398 return 1;
6402 /* Do the attributes look the same? */
6404 static int
6405 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6407 if (at1->dw_attr != at2->dw_attr)
6408 return 0;
6410 /* We don't care that this was compiled with a different compiler
6411 snapshot; if the output is the same, that's what matters. */
6412 if (at1->dw_attr == DW_AT_producer)
6413 return 1;
6415 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6418 /* Do the dies look the same? */
6420 static int
6421 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6423 dw_die_ref c1, c2;
6424 dw_attr_ref a1;
6425 unsigned ix;
6427 /* To avoid infinite recursion. */
6428 if (die1->die_mark)
6429 return die1->die_mark == die2->die_mark;
6430 die1->die_mark = die2->die_mark = ++(*mark);
6432 if (die1->die_tag != die2->die_tag)
6433 return 0;
6435 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6436 return 0;
6438 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6439 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6440 return 0;
6442 c1 = die1->die_child;
6443 c2 = die2->die_child;
6444 if (! c1)
6446 if (c2)
6447 return 0;
6449 else
6450 for (;;)
6452 if (!same_die_p (c1, c2, mark))
6453 return 0;
6454 c1 = c1->die_sib;
6455 c2 = c2->die_sib;
6456 if (c1 == die1->die_child)
6458 if (c2 == die2->die_child)
6459 break;
6460 else
6461 return 0;
6465 return 1;
6468 /* Do the dies look the same? Wrapper around same_die_p. */
6470 static int
6471 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6473 int mark = 0;
6474 int ret = same_die_p (die1, die2, &mark);
6476 unmark_all_dies (die1);
6477 unmark_all_dies (die2);
6479 return ret;
6482 /* The prefix to attach to symbols on DIEs in the current comdat debug
6483 info section. */
6484 static const char *comdat_symbol_id;
6486 /* The index of the current symbol within the current comdat CU. */
6487 static unsigned int comdat_symbol_number;
6489 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6490 children, and set comdat_symbol_id accordingly. */
6492 static void
6493 compute_section_prefix (dw_die_ref unit_die)
6495 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6496 const char *base = die_name ? lbasename (die_name) : "anonymous";
6497 char *name = XALLOCAVEC (char, strlen (base) + 64);
6498 char *p;
6499 int i, mark;
6500 unsigned char checksum[16];
6501 struct md5_ctx ctx;
6503 /* Compute the checksum of the DIE, then append part of it as hex digits to
6504 the name filename of the unit. */
6506 md5_init_ctx (&ctx);
6507 mark = 0;
6508 die_checksum (unit_die, &ctx, &mark);
6509 unmark_all_dies (unit_die);
6510 md5_finish_ctx (&ctx, checksum);
6512 sprintf (name, "%s.", base);
6513 clean_symbol_name (name);
6515 p = name + strlen (name);
6516 for (i = 0; i < 4; i++)
6518 sprintf (p, "%.2x", checksum[i]);
6519 p += 2;
6522 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6523 comdat_symbol_number = 0;
6526 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6528 static int
6529 is_type_die (dw_die_ref die)
6531 switch (die->die_tag)
6533 case DW_TAG_array_type:
6534 case DW_TAG_class_type:
6535 case DW_TAG_interface_type:
6536 case DW_TAG_enumeration_type:
6537 case DW_TAG_pointer_type:
6538 case DW_TAG_reference_type:
6539 case DW_TAG_rvalue_reference_type:
6540 case DW_TAG_string_type:
6541 case DW_TAG_structure_type:
6542 case DW_TAG_subroutine_type:
6543 case DW_TAG_union_type:
6544 case DW_TAG_ptr_to_member_type:
6545 case DW_TAG_set_type:
6546 case DW_TAG_subrange_type:
6547 case DW_TAG_base_type:
6548 case DW_TAG_const_type:
6549 case DW_TAG_file_type:
6550 case DW_TAG_packed_type:
6551 case DW_TAG_volatile_type:
6552 case DW_TAG_typedef:
6553 return 1;
6554 default:
6555 return 0;
6559 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6560 Basically, we want to choose the bits that are likely to be shared between
6561 compilations (types) and leave out the bits that are specific to individual
6562 compilations (functions). */
6564 static int
6565 is_comdat_die (dw_die_ref c)
6567 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6568 we do for stabs. The advantage is a greater likelihood of sharing between
6569 objects that don't include headers in the same order (and therefore would
6570 put the base types in a different comdat). jason 8/28/00 */
6572 if (c->die_tag == DW_TAG_base_type)
6573 return 0;
6575 if (c->die_tag == DW_TAG_pointer_type
6576 || c->die_tag == DW_TAG_reference_type
6577 || c->die_tag == DW_TAG_rvalue_reference_type
6578 || c->die_tag == DW_TAG_const_type
6579 || c->die_tag == DW_TAG_volatile_type)
6581 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6583 return t ? is_comdat_die (t) : 0;
6586 return is_type_die (c);
6589 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6590 compilation unit. */
6592 static int
6593 is_symbol_die (dw_die_ref c)
6595 return (is_type_die (c)
6596 || is_declaration_die (c)
6597 || c->die_tag == DW_TAG_namespace
6598 || c->die_tag == DW_TAG_module);
6601 /* Returns true iff C is a compile-unit DIE. */
6603 static inline bool
6604 is_cu_die (dw_die_ref c)
6606 return c && c->die_tag == DW_TAG_compile_unit;
6609 /* Returns true iff C is a unit DIE of some sort. */
6611 static inline bool
6612 is_unit_die (dw_die_ref c)
6614 return c && (c->die_tag == DW_TAG_compile_unit
6615 || c->die_tag == DW_TAG_partial_unit
6616 || c->die_tag == DW_TAG_type_unit);
6619 /* Returns true iff C is a namespace DIE. */
6621 static inline bool
6622 is_namespace_die (dw_die_ref c)
6624 return c && c->die_tag == DW_TAG_namespace;
6627 /* Returns true iff C is a class or structure DIE. */
6629 static inline bool
6630 is_class_die (dw_die_ref c)
6632 return c && (c->die_tag == DW_TAG_class_type
6633 || c->die_tag == DW_TAG_structure_type);
6636 /* Return non-zero if this DIE is a template parameter. */
6638 static inline bool
6639 is_template_parameter (dw_die_ref die)
6641 switch (die->die_tag)
6643 case DW_TAG_template_type_param:
6644 case DW_TAG_template_value_param:
6645 case DW_TAG_GNU_template_template_param:
6646 case DW_TAG_GNU_template_parameter_pack:
6647 return true;
6648 default:
6649 return false;
6653 /* Return non-zero if this DIE represents a template instantiation. */
6655 static inline bool
6656 is_template_instantiation (dw_die_ref die)
6658 dw_die_ref c;
6660 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6661 return false;
6662 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6663 return false;
6666 static char *
6667 gen_internal_sym (const char *prefix)
6669 char buf[256];
6671 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6672 return xstrdup (buf);
6675 /* Assign symbols to all worthy DIEs under DIE. */
6677 static void
6678 assign_symbol_names (dw_die_ref die)
6680 dw_die_ref c;
6682 if (is_symbol_die (die) && !die->comdat_type_p)
6684 if (comdat_symbol_id)
6686 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6688 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6689 comdat_symbol_id, comdat_symbol_number++);
6690 die->die_id.die_symbol = xstrdup (p);
6692 else
6693 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6696 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6699 struct cu_hash_table_entry
6701 dw_die_ref cu;
6702 unsigned min_comdat_num, max_comdat_num;
6703 struct cu_hash_table_entry *next;
6706 /* Helpers to manipulate hash table of CUs. */
6708 struct cu_hash_table_entry_hasher
6710 typedef cu_hash_table_entry value_type;
6711 typedef die_struct compare_type;
6712 static inline hashval_t hash (const value_type *);
6713 static inline bool equal (const value_type *, const compare_type *);
6714 static inline void remove (value_type *);
6717 inline hashval_t
6718 cu_hash_table_entry_hasher::hash (const value_type *entry)
6720 return htab_hash_string (entry->cu->die_id.die_symbol);
6723 inline bool
6724 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6725 const compare_type *entry2)
6727 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6730 inline void
6731 cu_hash_table_entry_hasher::remove (value_type *entry)
6733 struct cu_hash_table_entry *next;
6735 while (entry)
6737 next = entry->next;
6738 free (entry);
6739 entry = next;
6743 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6745 /* Check whether we have already seen this CU and set up SYM_NUM
6746 accordingly. */
6747 static int
6748 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6750 struct cu_hash_table_entry dummy;
6751 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6753 dummy.max_comdat_num = 0;
6755 slot = htable.find_slot_with_hash (cu,
6756 htab_hash_string (cu->die_id.die_symbol),
6757 INSERT);
6758 entry = *slot;
6760 for (; entry; last = entry, entry = entry->next)
6762 if (same_die_p_wrap (cu, entry->cu))
6763 break;
6766 if (entry)
6768 *sym_num = entry->min_comdat_num;
6769 return 1;
6772 entry = XCNEW (struct cu_hash_table_entry);
6773 entry->cu = cu;
6774 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6775 entry->next = *slot;
6776 *slot = entry;
6778 return 0;
6781 /* Record SYM_NUM to record of CU in HTABLE. */
6782 static void
6783 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6784 unsigned int sym_num)
6786 struct cu_hash_table_entry **slot, *entry;
6788 slot = htable.find_slot_with_hash (cu,
6789 htab_hash_string (cu->die_id.die_symbol),
6790 NO_INSERT);
6791 entry = *slot;
6793 entry->max_comdat_num = sym_num;
6796 /* Traverse the DIE (which is always comp_unit_die), and set up
6797 additional compilation units for each of the include files we see
6798 bracketed by BINCL/EINCL. */
6800 static void
6801 break_out_includes (dw_die_ref die)
6803 dw_die_ref c;
6804 dw_die_ref unit = NULL;
6805 limbo_die_node *node, **pnode;
6806 cu_hash_type cu_hash_table;
6808 c = die->die_child;
6809 if (c) do {
6810 dw_die_ref prev = c;
6811 c = c->die_sib;
6812 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6813 || (unit && is_comdat_die (c)))
6815 dw_die_ref next = c->die_sib;
6817 /* This DIE is for a secondary CU; remove it from the main one. */
6818 remove_child_with_prev (c, prev);
6820 if (c->die_tag == DW_TAG_GNU_BINCL)
6821 unit = push_new_compile_unit (unit, c);
6822 else if (c->die_tag == DW_TAG_GNU_EINCL)
6823 unit = pop_compile_unit (unit);
6824 else
6825 add_child_die (unit, c);
6826 c = next;
6827 if (c == die->die_child)
6828 break;
6830 } while (c != die->die_child);
6832 #if 0
6833 /* We can only use this in debugging, since the frontend doesn't check
6834 to make sure that we leave every include file we enter. */
6835 gcc_assert (!unit);
6836 #endif
6838 assign_symbol_names (die);
6839 cu_hash_table.create (10);
6840 for (node = limbo_die_list, pnode = &limbo_die_list;
6841 node;
6842 node = node->next)
6844 int is_dupl;
6846 compute_section_prefix (node->die);
6847 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6848 &comdat_symbol_number);
6849 assign_symbol_names (node->die);
6850 if (is_dupl)
6851 *pnode = node->next;
6852 else
6854 pnode = &node->next;
6855 record_comdat_symbol_number (node->die, cu_hash_table,
6856 comdat_symbol_number);
6859 cu_hash_table.dispose ();
6862 /* Return non-zero if this DIE is a declaration. */
6864 static int
6865 is_declaration_die (dw_die_ref die)
6867 dw_attr_ref a;
6868 unsigned ix;
6870 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6871 if (a->dw_attr == DW_AT_declaration)
6872 return 1;
6874 return 0;
6877 /* Return non-zero if this DIE is nested inside a subprogram. */
6879 static int
6880 is_nested_in_subprogram (dw_die_ref die)
6882 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6884 if (decl == NULL)
6885 decl = die;
6886 return local_scope_p (decl);
6889 /* Return non-zero if this DIE contains a defining declaration of a
6890 subprogram. */
6892 static int
6893 contains_subprogram_definition (dw_die_ref die)
6895 dw_die_ref c;
6897 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6898 return 1;
6899 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6900 return 0;
6903 /* Return non-zero if this is a type DIE that should be moved to a
6904 COMDAT .debug_types section. */
6906 static int
6907 should_move_die_to_comdat (dw_die_ref die)
6909 switch (die->die_tag)
6911 case DW_TAG_class_type:
6912 case DW_TAG_structure_type:
6913 case DW_TAG_enumeration_type:
6914 case DW_TAG_union_type:
6915 /* Don't move declarations, inlined instances, or types nested in a
6916 subprogram. */
6917 if (is_declaration_die (die)
6918 || get_AT (die, DW_AT_abstract_origin)
6919 || is_nested_in_subprogram (die))
6920 return 0;
6921 /* A type definition should never contain a subprogram definition. */
6922 gcc_assert (!contains_subprogram_definition (die));
6923 return 1;
6924 case DW_TAG_array_type:
6925 case DW_TAG_interface_type:
6926 case DW_TAG_pointer_type:
6927 case DW_TAG_reference_type:
6928 case DW_TAG_rvalue_reference_type:
6929 case DW_TAG_string_type:
6930 case DW_TAG_subroutine_type:
6931 case DW_TAG_ptr_to_member_type:
6932 case DW_TAG_set_type:
6933 case DW_TAG_subrange_type:
6934 case DW_TAG_base_type:
6935 case DW_TAG_const_type:
6936 case DW_TAG_file_type:
6937 case DW_TAG_packed_type:
6938 case DW_TAG_volatile_type:
6939 case DW_TAG_typedef:
6940 default:
6941 return 0;
6945 /* Make a clone of DIE. */
6947 static dw_die_ref
6948 clone_die (dw_die_ref die)
6950 dw_die_ref clone;
6951 dw_attr_ref a;
6952 unsigned ix;
6954 clone = ggc_alloc_cleared_die_node ();
6955 clone->die_tag = die->die_tag;
6957 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6958 add_dwarf_attr (clone, a);
6960 return clone;
6963 /* Make a clone of the tree rooted at DIE. */
6965 static dw_die_ref
6966 clone_tree (dw_die_ref die)
6968 dw_die_ref c;
6969 dw_die_ref clone = clone_die (die);
6971 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6973 return clone;
6976 /* Make a clone of DIE as a declaration. */
6978 static dw_die_ref
6979 clone_as_declaration (dw_die_ref die)
6981 dw_die_ref clone;
6982 dw_die_ref decl;
6983 dw_attr_ref a;
6984 unsigned ix;
6986 /* If the DIE is already a declaration, just clone it. */
6987 if (is_declaration_die (die))
6988 return clone_die (die);
6990 /* If the DIE is a specification, just clone its declaration DIE. */
6991 decl = get_AT_ref (die, DW_AT_specification);
6992 if (decl != NULL)
6994 clone = clone_die (decl);
6995 if (die->comdat_type_p)
6996 add_AT_die_ref (clone, DW_AT_signature, die);
6997 return clone;
7000 clone = ggc_alloc_cleared_die_node ();
7001 clone->die_tag = die->die_tag;
7003 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7005 /* We don't want to copy over all attributes.
7006 For example we don't want DW_AT_byte_size because otherwise we will no
7007 longer have a declaration and GDB will treat it as a definition. */
7009 switch (a->dw_attr)
7011 case DW_AT_artificial:
7012 case DW_AT_containing_type:
7013 case DW_AT_external:
7014 case DW_AT_name:
7015 case DW_AT_type:
7016 case DW_AT_virtuality:
7017 case DW_AT_linkage_name:
7018 case DW_AT_MIPS_linkage_name:
7019 add_dwarf_attr (clone, a);
7020 break;
7021 case DW_AT_byte_size:
7022 default:
7023 break;
7027 if (die->comdat_type_p)
7028 add_AT_die_ref (clone, DW_AT_signature, die);
7030 add_AT_flag (clone, DW_AT_declaration, 1);
7031 return clone;
7035 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7037 struct decl_table_entry
7039 dw_die_ref orig;
7040 dw_die_ref copy;
7043 /* Helpers to manipulate hash table of copied declarations. */
7045 /* Hashtable helpers. */
7047 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7049 typedef decl_table_entry value_type;
7050 typedef die_struct compare_type;
7051 static inline hashval_t hash (const value_type *);
7052 static inline bool equal (const value_type *, const compare_type *);
7055 inline hashval_t
7056 decl_table_entry_hasher::hash (const value_type *entry)
7058 return htab_hash_pointer (entry->orig);
7061 inline bool
7062 decl_table_entry_hasher::equal (const value_type *entry1,
7063 const compare_type *entry2)
7065 return entry1->orig == entry2;
7068 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
7070 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7071 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7072 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7073 to check if the ancestor has already been copied into UNIT. */
7075 static dw_die_ref
7076 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7078 dw_die_ref parent = die->die_parent;
7079 dw_die_ref new_parent = unit;
7080 dw_die_ref copy;
7081 decl_table_entry **slot = NULL;
7082 struct decl_table_entry *entry = NULL;
7084 if (decl_table.is_created ())
7086 /* Check if the entry has already been copied to UNIT. */
7087 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
7088 INSERT);
7089 if (*slot != HTAB_EMPTY_ENTRY)
7091 entry = *slot;
7092 return entry->copy;
7095 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7096 entry = XCNEW (struct decl_table_entry);
7097 entry->orig = die;
7098 entry->copy = NULL;
7099 *slot = entry;
7102 if (parent != NULL)
7104 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7105 if (spec != NULL)
7106 parent = spec;
7107 if (!is_unit_die (parent))
7108 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7111 copy = clone_as_declaration (die);
7112 add_child_die (new_parent, copy);
7114 if (decl_table.is_created ())
7116 /* Record the pointer to the copy. */
7117 entry->copy = copy;
7120 return copy;
7122 /* Copy the declaration context to the new type unit DIE. This includes
7123 any surrounding namespace or type declarations. If the DIE has an
7124 AT_specification attribute, it also includes attributes and children
7125 attached to the specification, and returns a pointer to the original
7126 parent of the declaration DIE. Returns NULL otherwise. */
7128 static dw_die_ref
7129 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7131 dw_die_ref decl;
7132 dw_die_ref new_decl;
7133 dw_die_ref orig_parent = NULL;
7135 decl = get_AT_ref (die, DW_AT_specification);
7136 if (decl == NULL)
7137 decl = die;
7138 else
7140 unsigned ix;
7141 dw_die_ref c;
7142 dw_attr_ref a;
7144 /* The original DIE will be changed to a declaration, and must
7145 be moved to be a child of the original declaration DIE. */
7146 orig_parent = decl->die_parent;
7148 /* Copy the type node pointer from the new DIE to the original
7149 declaration DIE so we can forward references later. */
7150 decl->comdat_type_p = true;
7151 decl->die_id.die_type_node = die->die_id.die_type_node;
7153 remove_AT (die, DW_AT_specification);
7155 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7157 if (a->dw_attr != DW_AT_name
7158 && a->dw_attr != DW_AT_declaration
7159 && a->dw_attr != DW_AT_external)
7160 add_dwarf_attr (die, a);
7163 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7166 if (decl->die_parent != NULL
7167 && !is_unit_die (decl->die_parent))
7169 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7170 if (new_decl != NULL)
7172 remove_AT (new_decl, DW_AT_signature);
7173 add_AT_specification (die, new_decl);
7177 return orig_parent;
7180 /* Generate the skeleton ancestor tree for the given NODE, then clone
7181 the DIE and add the clone into the tree. */
7183 static void
7184 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7186 if (node->new_die != NULL)
7187 return;
7189 node->new_die = clone_as_declaration (node->old_die);
7191 if (node->parent != NULL)
7193 generate_skeleton_ancestor_tree (node->parent);
7194 add_child_die (node->parent->new_die, node->new_die);
7198 /* Generate a skeleton tree of DIEs containing any declarations that are
7199 found in the original tree. We traverse the tree looking for declaration
7200 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7202 static void
7203 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7205 skeleton_chain_node node;
7206 dw_die_ref c;
7207 dw_die_ref first;
7208 dw_die_ref prev = NULL;
7209 dw_die_ref next = NULL;
7211 node.parent = parent;
7213 first = c = parent->old_die->die_child;
7214 if (c)
7215 next = c->die_sib;
7216 if (c) do {
7217 if (prev == NULL || prev->die_sib == c)
7218 prev = c;
7219 c = next;
7220 next = (c == first ? NULL : c->die_sib);
7221 node.old_die = c;
7222 node.new_die = NULL;
7223 if (is_declaration_die (c))
7225 if (is_template_instantiation (c))
7227 /* Instantiated templates do not need to be cloned into the
7228 type unit. Just move the DIE and its children back to
7229 the skeleton tree (in the main CU). */
7230 remove_child_with_prev (c, prev);
7231 add_child_die (parent->new_die, c);
7232 c = prev;
7234 else
7236 /* Clone the existing DIE, move the original to the skeleton
7237 tree (which is in the main CU), and put the clone, with
7238 all the original's children, where the original came from
7239 (which is about to be moved to the type unit). */
7240 dw_die_ref clone = clone_die (c);
7241 move_all_children (c, clone);
7243 replace_child (c, clone, prev);
7244 generate_skeleton_ancestor_tree (parent);
7245 add_child_die (parent->new_die, c);
7246 node.new_die = c;
7247 c = clone;
7250 generate_skeleton_bottom_up (&node);
7251 } while (next != NULL);
7254 /* Wrapper function for generate_skeleton_bottom_up. */
7256 static dw_die_ref
7257 generate_skeleton (dw_die_ref die)
7259 skeleton_chain_node node;
7261 node.old_die = die;
7262 node.new_die = NULL;
7263 node.parent = NULL;
7265 /* If this type definition is nested inside another type,
7266 and is not an instantiation of a template, always leave
7267 at least a declaration in its place. */
7268 if (die->die_parent != NULL
7269 && is_type_die (die->die_parent)
7270 && !is_template_instantiation (die))
7271 node.new_die = clone_as_declaration (die);
7273 generate_skeleton_bottom_up (&node);
7274 return node.new_die;
7277 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7278 declaration. The original DIE is moved to a new compile unit so that
7279 existing references to it follow it to the new location. If any of the
7280 original DIE's descendants is a declaration, we need to replace the
7281 original DIE with a skeleton tree and move the declarations back into the
7282 skeleton tree. */
7284 static dw_die_ref
7285 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7286 dw_die_ref prev)
7288 dw_die_ref skeleton, orig_parent;
7290 /* Copy the declaration context to the type unit DIE. If the returned
7291 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7292 that DIE. */
7293 orig_parent = copy_declaration_context (unit, child);
7295 skeleton = generate_skeleton (child);
7296 if (skeleton == NULL)
7297 remove_child_with_prev (child, prev);
7298 else
7300 skeleton->comdat_type_p = true;
7301 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7303 /* If the original DIE was a specification, we need to put
7304 the skeleton under the parent DIE of the declaration.
7305 This leaves the original declaration in the tree, but
7306 it will be pruned later since there are no longer any
7307 references to it. */
7308 if (orig_parent != NULL)
7310 remove_child_with_prev (child, prev);
7311 add_child_die (orig_parent, skeleton);
7313 else
7314 replace_child (child, skeleton, prev);
7317 return skeleton;
7320 /* Traverse the DIE and set up additional .debug_types sections for each
7321 type worthy of being placed in a COMDAT section. */
7323 static void
7324 break_out_comdat_types (dw_die_ref die)
7326 dw_die_ref c;
7327 dw_die_ref first;
7328 dw_die_ref prev = NULL;
7329 dw_die_ref next = NULL;
7330 dw_die_ref unit = NULL;
7332 first = c = die->die_child;
7333 if (c)
7334 next = c->die_sib;
7335 if (c) do {
7336 if (prev == NULL || prev->die_sib == c)
7337 prev = c;
7338 c = next;
7339 next = (c == first ? NULL : c->die_sib);
7340 if (should_move_die_to_comdat (c))
7342 dw_die_ref replacement;
7343 comdat_type_node_ref type_node;
7345 /* Break out nested types into their own type units. */
7346 break_out_comdat_types (c);
7348 /* Create a new type unit DIE as the root for the new tree, and
7349 add it to the list of comdat types. */
7350 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7351 add_AT_unsigned (unit, DW_AT_language,
7352 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7353 type_node = ggc_alloc_cleared_comdat_type_node ();
7354 type_node->root_die = unit;
7355 type_node->next = comdat_type_list;
7356 comdat_type_list = type_node;
7358 /* Generate the type signature. */
7359 generate_type_signature (c, type_node);
7361 /* Copy the declaration context, attributes, and children of the
7362 declaration into the new type unit DIE, then remove this DIE
7363 from the main CU (or replace it with a skeleton if necessary). */
7364 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7365 type_node->skeleton_die = replacement;
7367 /* Add the DIE to the new compunit. */
7368 add_child_die (unit, c);
7370 if (replacement != NULL)
7371 c = replacement;
7373 else if (c->die_tag == DW_TAG_namespace
7374 || c->die_tag == DW_TAG_class_type
7375 || c->die_tag == DW_TAG_structure_type
7376 || c->die_tag == DW_TAG_union_type)
7378 /* Look for nested types that can be broken out. */
7379 break_out_comdat_types (c);
7381 } while (next != NULL);
7384 /* Like clone_tree, but additionally enter all the children into
7385 the hash table decl_table. */
7387 static dw_die_ref
7388 clone_tree_hash (dw_die_ref die, decl_hash_type decl_table)
7390 dw_die_ref c;
7391 dw_die_ref clone = clone_die (die);
7392 struct decl_table_entry *entry;
7393 decl_table_entry **slot = decl_table.find_slot_with_hash (die,
7394 htab_hash_pointer (die), INSERT);
7395 /* Assert that DIE isn't in the hash table yet. If it would be there
7396 before, the ancestors would be necessarily there as well, therefore
7397 clone_tree_hash wouldn't be called. */
7398 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7399 entry = XCNEW (struct decl_table_entry);
7400 entry->orig = die;
7401 entry->copy = clone;
7402 *slot = entry;
7404 FOR_EACH_CHILD (die, c,
7405 add_child_die (clone, clone_tree_hash (c, decl_table)));
7407 return clone;
7410 /* Walk the DIE and its children, looking for references to incomplete
7411 or trivial types that are unmarked (i.e., that are not in the current
7412 type_unit). */
7414 static void
7415 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7417 dw_die_ref c;
7418 dw_attr_ref a;
7419 unsigned ix;
7421 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7423 if (AT_class (a) == dw_val_class_die_ref)
7425 dw_die_ref targ = AT_ref (a);
7426 decl_table_entry **slot;
7427 struct decl_table_entry *entry;
7429 if (targ->die_mark != 0 || targ->comdat_type_p)
7430 continue;
7432 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7433 INSERT);
7435 if (*slot != HTAB_EMPTY_ENTRY)
7437 /* TARG has already been copied, so we just need to
7438 modify the reference to point to the copy. */
7439 entry = *slot;
7440 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7442 else
7444 dw_die_ref parent = unit;
7445 dw_die_ref copy = clone_die (targ);
7447 /* Record in DECL_TABLE that TARG has been copied.
7448 Need to do this now, before the recursive call,
7449 because DECL_TABLE may be expanded and SLOT
7450 would no longer be a valid pointer. */
7451 entry = XCNEW (struct decl_table_entry);
7452 entry->orig = targ;
7453 entry->copy = copy;
7454 *slot = entry;
7456 FOR_EACH_CHILD (targ, c,
7457 add_child_die (copy,
7458 clone_tree_hash (c, decl_table)));
7460 /* Make sure the cloned tree is marked as part of the
7461 type unit. */
7462 mark_dies (copy);
7464 /* If TARG has surrounding context, copy its ancestor tree
7465 into the new type unit. */
7466 if (targ->die_parent != NULL
7467 && !is_unit_die (targ->die_parent))
7468 parent = copy_ancestor_tree (unit, targ->die_parent,
7469 decl_table);
7471 add_child_die (parent, copy);
7472 a->dw_attr_val.v.val_die_ref.die = copy;
7474 /* Make sure the newly-copied DIE is walked. If it was
7475 installed in a previously-added context, it won't
7476 get visited otherwise. */
7477 if (parent != unit)
7479 /* Find the highest point of the newly-added tree,
7480 mark each node along the way, and walk from there. */
7481 parent->die_mark = 1;
7482 while (parent->die_parent
7483 && parent->die_parent->die_mark == 0)
7485 parent = parent->die_parent;
7486 parent->die_mark = 1;
7488 copy_decls_walk (unit, parent, decl_table);
7494 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7497 /* Copy declarations for "unworthy" types into the new comdat section.
7498 Incomplete types, modified types, and certain other types aren't broken
7499 out into comdat sections of their own, so they don't have a signature,
7500 and we need to copy the declaration into the same section so that we
7501 don't have an external reference. */
7503 static void
7504 copy_decls_for_unworthy_types (dw_die_ref unit)
7506 decl_hash_type decl_table;
7508 mark_dies (unit);
7509 decl_table.create (10);
7510 copy_decls_walk (unit, unit, decl_table);
7511 decl_table.dispose ();
7512 unmark_dies (unit);
7515 /* Traverse the DIE and add a sibling attribute if it may have the
7516 effect of speeding up access to siblings. To save some space,
7517 avoid generating sibling attributes for DIE's without children. */
7519 static void
7520 add_sibling_attributes (dw_die_ref die)
7522 dw_die_ref c;
7524 if (! die->die_child)
7525 return;
7527 if (die->die_parent && die != die->die_parent->die_child)
7528 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7530 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7533 /* Output all location lists for the DIE and its children. */
7535 static void
7536 output_location_lists (dw_die_ref die)
7538 dw_die_ref c;
7539 dw_attr_ref a;
7540 unsigned ix;
7542 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7543 if (AT_class (a) == dw_val_class_loc_list)
7544 output_loc_list (AT_loc_list (a));
7546 FOR_EACH_CHILD (die, c, output_location_lists (c));
7549 /* We want to limit the number of external references, because they are
7550 larger than local references: a relocation takes multiple words, and
7551 even a sig8 reference is always eight bytes, whereas a local reference
7552 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7553 So if we encounter multiple external references to the same type DIE, we
7554 make a local typedef stub for it and redirect all references there.
7556 This is the element of the hash table for keeping track of these
7557 references. */
7559 struct external_ref
7561 dw_die_ref type;
7562 dw_die_ref stub;
7563 unsigned n_refs;
7566 /* Hashtable helpers. */
7568 struct external_ref_hasher : typed_free_remove <external_ref>
7570 typedef external_ref value_type;
7571 typedef external_ref compare_type;
7572 static inline hashval_t hash (const value_type *);
7573 static inline bool equal (const value_type *, const compare_type *);
7576 inline hashval_t
7577 external_ref_hasher::hash (const value_type *r)
7579 dw_die_ref die = r->type;
7580 hashval_t h = 0;
7582 /* We can't use the address of the DIE for hashing, because
7583 that will make the order of the stub DIEs non-deterministic. */
7584 if (! die->comdat_type_p)
7585 /* We have a symbol; use it to compute a hash. */
7586 h = htab_hash_string (die->die_id.die_symbol);
7587 else
7589 /* We have a type signature; use a subset of the bits as the hash.
7590 The 8-byte signature is at least as large as hashval_t. */
7591 comdat_type_node_ref type_node = die->die_id.die_type_node;
7592 memcpy (&h, type_node->signature, sizeof (h));
7594 return h;
7597 inline bool
7598 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7600 return r1->type == r2->type;
7603 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7605 /* Return a pointer to the external_ref for references to DIE. */
7607 static struct external_ref *
7608 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7610 struct external_ref ref, *ref_p;
7611 external_ref **slot;
7613 ref.type = die;
7614 slot = map.find_slot (&ref, INSERT);
7615 if (*slot != HTAB_EMPTY_ENTRY)
7616 return *slot;
7618 ref_p = XCNEW (struct external_ref);
7619 ref_p->type = die;
7620 *slot = ref_p;
7621 return ref_p;
7624 /* Subroutine of optimize_external_refs, below.
7626 If we see a type skeleton, record it as our stub. If we see external
7627 references, remember how many we've seen. */
7629 static void
7630 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7632 dw_die_ref c;
7633 dw_attr_ref a;
7634 unsigned ix;
7635 struct external_ref *ref_p;
7637 if (is_type_die (die)
7638 && (c = get_AT_ref (die, DW_AT_signature)))
7640 /* This is a local skeleton; use it for local references. */
7641 ref_p = lookup_external_ref (map, c);
7642 ref_p->stub = die;
7645 /* Scan the DIE references, and remember any that refer to DIEs from
7646 other CUs (i.e. those which are not marked). */
7647 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7648 if (AT_class (a) == dw_val_class_die_ref
7649 && (c = AT_ref (a))->die_mark == 0
7650 && is_type_die (c))
7652 ref_p = lookup_external_ref (map, c);
7653 ref_p->n_refs++;
7656 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7659 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7660 points to an external_ref, DATA is the CU we're processing. If we don't
7661 already have a local stub, and we have multiple refs, build a stub. */
7664 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7666 struct external_ref *ref_p = *slot;
7668 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7670 /* We have multiple references to this type, so build a small stub.
7671 Both of these forms are a bit dodgy from the perspective of the
7672 DWARF standard, since technically they should have names. */
7673 dw_die_ref cu = data;
7674 dw_die_ref type = ref_p->type;
7675 dw_die_ref stub = NULL;
7677 if (type->comdat_type_p)
7679 /* If we refer to this type via sig8, use AT_signature. */
7680 stub = new_die (type->die_tag, cu, NULL_TREE);
7681 add_AT_die_ref (stub, DW_AT_signature, type);
7683 else
7685 /* Otherwise, use a typedef with no name. */
7686 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7687 add_AT_die_ref (stub, DW_AT_type, type);
7690 stub->die_mark++;
7691 ref_p->stub = stub;
7693 return 1;
7696 /* DIE is a unit; look through all the DIE references to see if there are
7697 any external references to types, and if so, create local stubs for
7698 them which will be applied in build_abbrev_table. This is useful because
7699 references to local DIEs are smaller. */
7701 static external_ref_hash_type
7702 optimize_external_refs (dw_die_ref die)
7704 external_ref_hash_type map;
7705 map.create (10);
7706 optimize_external_refs_1 (die, map);
7707 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7708 return map;
7711 /* The format of each DIE (and its attribute value pairs) is encoded in an
7712 abbreviation table. This routine builds the abbreviation table and assigns
7713 a unique abbreviation id for each abbreviation entry. The children of each
7714 die are visited recursively. */
7716 static void
7717 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7719 unsigned long abbrev_id;
7720 unsigned int n_alloc;
7721 dw_die_ref c;
7722 dw_attr_ref a;
7723 unsigned ix;
7725 /* Scan the DIE references, and replace any that refer to
7726 DIEs from other CUs (i.e. those which are not marked) with
7727 the local stubs we built in optimize_external_refs. */
7728 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7729 if (AT_class (a) == dw_val_class_die_ref
7730 && (c = AT_ref (a))->die_mark == 0)
7732 struct external_ref *ref_p;
7733 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7735 ref_p = lookup_external_ref (extern_map, c);
7736 if (ref_p->stub && ref_p->stub != die)
7737 change_AT_die_ref (a, ref_p->stub);
7738 else
7739 /* We aren't changing this reference, so mark it external. */
7740 set_AT_ref_external (a, 1);
7743 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7745 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7746 dw_attr_ref die_a, abbrev_a;
7747 unsigned ix;
7748 bool ok = true;
7750 if (abbrev->die_tag != die->die_tag)
7751 continue;
7752 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7753 continue;
7755 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7756 continue;
7758 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7760 abbrev_a = &(*abbrev->die_attr)[ix];
7761 if ((abbrev_a->dw_attr != die_a->dw_attr)
7762 || (value_format (abbrev_a) != value_format (die_a)))
7764 ok = false;
7765 break;
7768 if (ok)
7769 break;
7772 if (abbrev_id >= abbrev_die_table_in_use)
7774 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7776 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7777 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7778 n_alloc);
7780 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7781 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7782 abbrev_die_table_allocated = n_alloc;
7785 ++abbrev_die_table_in_use;
7786 abbrev_die_table[abbrev_id] = die;
7789 die->die_abbrev = abbrev_id;
7790 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7793 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7795 static int
7796 constant_size (unsigned HOST_WIDE_INT value)
7798 int log;
7800 if (value == 0)
7801 log = 0;
7802 else
7803 log = floor_log2 (value);
7805 log = log / 8;
7806 log = 1 << (floor_log2 (log) + 1);
7808 return log;
7811 /* Return the size of a DIE as it is represented in the
7812 .debug_info section. */
7814 static unsigned long
7815 size_of_die (dw_die_ref die)
7817 unsigned long size = 0;
7818 dw_attr_ref a;
7819 unsigned ix;
7820 enum dwarf_form form;
7822 size += size_of_uleb128 (die->die_abbrev);
7823 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7825 switch (AT_class (a))
7827 case dw_val_class_addr:
7828 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7830 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7831 size += size_of_uleb128 (AT_index (a));
7833 else
7834 size += DWARF2_ADDR_SIZE;
7835 break;
7836 case dw_val_class_offset:
7837 size += DWARF_OFFSET_SIZE;
7838 break;
7839 case dw_val_class_loc:
7841 unsigned long lsize = size_of_locs (AT_loc (a));
7843 /* Block length. */
7844 if (dwarf_version >= 4)
7845 size += size_of_uleb128 (lsize);
7846 else
7847 size += constant_size (lsize);
7848 size += lsize;
7850 break;
7851 case dw_val_class_loc_list:
7852 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7854 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7855 size += size_of_uleb128 (AT_index (a));
7857 else
7858 size += DWARF_OFFSET_SIZE;
7859 break;
7860 case dw_val_class_range_list:
7861 size += DWARF_OFFSET_SIZE;
7862 break;
7863 case dw_val_class_const:
7864 size += size_of_sleb128 (AT_int (a));
7865 break;
7866 case dw_val_class_unsigned_const:
7868 int csize = constant_size (AT_unsigned (a));
7869 if (dwarf_version == 3
7870 && a->dw_attr == DW_AT_data_member_location
7871 && csize >= 4)
7872 size += size_of_uleb128 (AT_unsigned (a));
7873 else
7874 size += csize;
7876 break;
7877 case dw_val_class_const_double:
7878 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7879 if (HOST_BITS_PER_WIDE_INT >= 64)
7880 size++; /* block */
7881 break;
7882 case dw_val_class_wide_int:
7883 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7884 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7885 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7886 > 64)
7887 size++; /* block */
7888 break;
7889 case dw_val_class_vec:
7890 size += constant_size (a->dw_attr_val.v.val_vec.length
7891 * a->dw_attr_val.v.val_vec.elt_size)
7892 + a->dw_attr_val.v.val_vec.length
7893 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7894 break;
7895 case dw_val_class_flag:
7896 if (dwarf_version >= 4)
7897 /* Currently all add_AT_flag calls pass in 1 as last argument,
7898 so DW_FORM_flag_present can be used. If that ever changes,
7899 we'll need to use DW_FORM_flag and have some optimization
7900 in build_abbrev_table that will change those to
7901 DW_FORM_flag_present if it is set to 1 in all DIEs using
7902 the same abbrev entry. */
7903 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7904 else
7905 size += 1;
7906 break;
7907 case dw_val_class_die_ref:
7908 if (AT_ref_external (a))
7910 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7911 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7912 is sized by target address length, whereas in DWARF3
7913 it's always sized as an offset. */
7914 if (use_debug_types)
7915 size += DWARF_TYPE_SIGNATURE_SIZE;
7916 else if (dwarf_version == 2)
7917 size += DWARF2_ADDR_SIZE;
7918 else
7919 size += DWARF_OFFSET_SIZE;
7921 else
7922 size += DWARF_OFFSET_SIZE;
7923 break;
7924 case dw_val_class_fde_ref:
7925 size += DWARF_OFFSET_SIZE;
7926 break;
7927 case dw_val_class_lbl_id:
7928 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7930 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7931 size += size_of_uleb128 (AT_index (a));
7933 else
7934 size += DWARF2_ADDR_SIZE;
7935 break;
7936 case dw_val_class_lineptr:
7937 case dw_val_class_macptr:
7938 size += DWARF_OFFSET_SIZE;
7939 break;
7940 case dw_val_class_str:
7941 form = AT_string_form (a);
7942 if (form == DW_FORM_strp)
7943 size += DWARF_OFFSET_SIZE;
7944 else if (form == DW_FORM_GNU_str_index)
7945 size += size_of_uleb128 (AT_index (a));
7946 else
7947 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7948 break;
7949 case dw_val_class_file:
7950 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7951 break;
7952 case dw_val_class_data8:
7953 size += 8;
7954 break;
7955 case dw_val_class_vms_delta:
7956 size += DWARF_OFFSET_SIZE;
7957 break;
7958 case dw_val_class_high_pc:
7959 size += DWARF2_ADDR_SIZE;
7960 break;
7961 default:
7962 gcc_unreachable ();
7966 return size;
7969 /* Size the debugging information associated with a given DIE. Visits the
7970 DIE's children recursively. Updates the global variable next_die_offset, on
7971 each time through. Uses the current value of next_die_offset to update the
7972 die_offset field in each DIE. */
7974 static void
7975 calc_die_sizes (dw_die_ref die)
7977 dw_die_ref c;
7979 gcc_assert (die->die_offset == 0
7980 || (unsigned long int) die->die_offset == next_die_offset);
7981 die->die_offset = next_die_offset;
7982 next_die_offset += size_of_die (die);
7984 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7986 if (die->die_child != NULL)
7987 /* Count the null byte used to terminate sibling lists. */
7988 next_die_offset += 1;
7991 /* Size just the base type children at the start of the CU.
7992 This is needed because build_abbrev needs to size locs
7993 and sizing of type based stack ops needs to know die_offset
7994 values for the base types. */
7996 static void
7997 calc_base_type_die_sizes (void)
7999 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8000 unsigned int i;
8001 dw_die_ref base_type;
8002 #if ENABLE_ASSERT_CHECKING
8003 dw_die_ref prev = comp_unit_die ()->die_child;
8004 #endif
8006 die_offset += size_of_die (comp_unit_die ());
8007 for (i = 0; base_types.iterate (i, &base_type); i++)
8009 #if ENABLE_ASSERT_CHECKING
8010 gcc_assert (base_type->die_offset == 0
8011 && prev->die_sib == base_type
8012 && base_type->die_child == NULL
8013 && base_type->die_abbrev);
8014 prev = base_type;
8015 #endif
8016 base_type->die_offset = die_offset;
8017 die_offset += size_of_die (base_type);
8021 /* Set the marks for a die and its children. We do this so
8022 that we know whether or not a reference needs to use FORM_ref_addr; only
8023 DIEs in the same CU will be marked. We used to clear out the offset
8024 and use that as the flag, but ran into ordering problems. */
8026 static void
8027 mark_dies (dw_die_ref die)
8029 dw_die_ref c;
8031 gcc_assert (!die->die_mark);
8033 die->die_mark = 1;
8034 FOR_EACH_CHILD (die, c, mark_dies (c));
8037 /* Clear the marks for a die and its children. */
8039 static void
8040 unmark_dies (dw_die_ref die)
8042 dw_die_ref c;
8044 if (! use_debug_types)
8045 gcc_assert (die->die_mark);
8047 die->die_mark = 0;
8048 FOR_EACH_CHILD (die, c, unmark_dies (c));
8051 /* Clear the marks for a die, its children and referred dies. */
8053 static void
8054 unmark_all_dies (dw_die_ref die)
8056 dw_die_ref c;
8057 dw_attr_ref a;
8058 unsigned ix;
8060 if (!die->die_mark)
8061 return;
8062 die->die_mark = 0;
8064 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8066 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8067 if (AT_class (a) == dw_val_class_die_ref)
8068 unmark_all_dies (AT_ref (a));
8071 /* Calculate if the entry should appear in the final output file. It may be
8072 from a pruned a type. */
8074 static bool
8075 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8077 /* By limiting gnu pubnames to definitions only, gold can generate a
8078 gdb index without entries for declarations, which don't include
8079 enough information to be useful. */
8080 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8081 return false;
8083 if (table == pubname_table)
8085 /* Enumerator names are part of the pubname table, but the
8086 parent DW_TAG_enumeration_type die may have been pruned.
8087 Don't output them if that is the case. */
8088 if (p->die->die_tag == DW_TAG_enumerator &&
8089 (p->die->die_parent == NULL
8090 || !p->die->die_parent->die_perennial_p))
8091 return false;
8093 /* Everything else in the pubname table is included. */
8094 return true;
8097 /* The pubtypes table shouldn't include types that have been
8098 pruned. */
8099 return (p->die->die_offset != 0
8100 || !flag_eliminate_unused_debug_types);
8103 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8104 generated for the compilation unit. */
8106 static unsigned long
8107 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8109 unsigned long size;
8110 unsigned i;
8111 pubname_ref p;
8112 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8114 size = DWARF_PUBNAMES_HEADER_SIZE;
8115 FOR_EACH_VEC_ELT (*names, i, p)
8116 if (include_pubname_in_output (names, p))
8117 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8119 size += DWARF_OFFSET_SIZE;
8120 return size;
8123 /* Return the size of the information in the .debug_aranges section. */
8125 static unsigned long
8126 size_of_aranges (void)
8128 unsigned long size;
8130 size = DWARF_ARANGES_HEADER_SIZE;
8132 /* Count the address/length pair for this compilation unit. */
8133 if (text_section_used)
8134 size += 2 * DWARF2_ADDR_SIZE;
8135 if (cold_text_section_used)
8136 size += 2 * DWARF2_ADDR_SIZE;
8137 if (have_multiple_function_sections)
8139 unsigned fde_idx;
8140 dw_fde_ref fde;
8142 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8144 if (DECL_IGNORED_P (fde->decl))
8145 continue;
8146 if (!fde->in_std_section)
8147 size += 2 * DWARF2_ADDR_SIZE;
8148 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8149 size += 2 * DWARF2_ADDR_SIZE;
8153 /* Count the two zero words used to terminated the address range table. */
8154 size += 2 * DWARF2_ADDR_SIZE;
8155 return size;
8158 /* Select the encoding of an attribute value. */
8160 static enum dwarf_form
8161 value_format (dw_attr_ref a)
8163 switch (AT_class (a))
8165 case dw_val_class_addr:
8166 /* Only very few attributes allow DW_FORM_addr. */
8167 switch (a->dw_attr)
8169 case DW_AT_low_pc:
8170 case DW_AT_high_pc:
8171 case DW_AT_entry_pc:
8172 case DW_AT_trampoline:
8173 return (AT_index (a) == NOT_INDEXED
8174 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8175 default:
8176 break;
8178 switch (DWARF2_ADDR_SIZE)
8180 case 1:
8181 return DW_FORM_data1;
8182 case 2:
8183 return DW_FORM_data2;
8184 case 4:
8185 return DW_FORM_data4;
8186 case 8:
8187 return DW_FORM_data8;
8188 default:
8189 gcc_unreachable ();
8191 case dw_val_class_range_list:
8192 case dw_val_class_loc_list:
8193 if (dwarf_version >= 4)
8194 return DW_FORM_sec_offset;
8195 /* FALLTHRU */
8196 case dw_val_class_vms_delta:
8197 case dw_val_class_offset:
8198 switch (DWARF_OFFSET_SIZE)
8200 case 4:
8201 return DW_FORM_data4;
8202 case 8:
8203 return DW_FORM_data8;
8204 default:
8205 gcc_unreachable ();
8207 case dw_val_class_loc:
8208 if (dwarf_version >= 4)
8209 return DW_FORM_exprloc;
8210 switch (constant_size (size_of_locs (AT_loc (a))))
8212 case 1:
8213 return DW_FORM_block1;
8214 case 2:
8215 return DW_FORM_block2;
8216 case 4:
8217 return DW_FORM_block4;
8218 default:
8219 gcc_unreachable ();
8221 case dw_val_class_const:
8222 return DW_FORM_sdata;
8223 case dw_val_class_unsigned_const:
8224 switch (constant_size (AT_unsigned (a)))
8226 case 1:
8227 return DW_FORM_data1;
8228 case 2:
8229 return DW_FORM_data2;
8230 case 4:
8231 /* In DWARF3 DW_AT_data_member_location with
8232 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8233 constant, so we need to use DW_FORM_udata if we need
8234 a large constant. */
8235 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8236 return DW_FORM_udata;
8237 return DW_FORM_data4;
8238 case 8:
8239 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8240 return DW_FORM_udata;
8241 return DW_FORM_data8;
8242 default:
8243 gcc_unreachable ();
8245 case dw_val_class_const_double:
8246 switch (HOST_BITS_PER_WIDE_INT)
8248 case 8:
8249 return DW_FORM_data2;
8250 case 16:
8251 return DW_FORM_data4;
8252 case 32:
8253 return DW_FORM_data8;
8254 case 64:
8255 default:
8256 return DW_FORM_block1;
8258 case dw_val_class_wide_int:
8259 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8261 case 8:
8262 return DW_FORM_data1;
8263 case 16:
8264 return DW_FORM_data2;
8265 case 32:
8266 return DW_FORM_data4;
8267 case 64:
8268 return DW_FORM_data8;
8269 default:
8270 return DW_FORM_block1;
8272 case dw_val_class_vec:
8273 switch (constant_size (a->dw_attr_val.v.val_vec.length
8274 * a->dw_attr_val.v.val_vec.elt_size))
8276 case 1:
8277 return DW_FORM_block1;
8278 case 2:
8279 return DW_FORM_block2;
8280 case 4:
8281 return DW_FORM_block4;
8282 default:
8283 gcc_unreachable ();
8285 case dw_val_class_flag:
8286 if (dwarf_version >= 4)
8288 /* Currently all add_AT_flag calls pass in 1 as last argument,
8289 so DW_FORM_flag_present can be used. If that ever changes,
8290 we'll need to use DW_FORM_flag and have some optimization
8291 in build_abbrev_table that will change those to
8292 DW_FORM_flag_present if it is set to 1 in all DIEs using
8293 the same abbrev entry. */
8294 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8295 return DW_FORM_flag_present;
8297 return DW_FORM_flag;
8298 case dw_val_class_die_ref:
8299 if (AT_ref_external (a))
8300 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8301 else
8302 return DW_FORM_ref;
8303 case dw_val_class_fde_ref:
8304 return DW_FORM_data;
8305 case dw_val_class_lbl_id:
8306 return (AT_index (a) == NOT_INDEXED
8307 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8308 case dw_val_class_lineptr:
8309 case dw_val_class_macptr:
8310 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8311 case dw_val_class_str:
8312 return AT_string_form (a);
8313 case dw_val_class_file:
8314 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8316 case 1:
8317 return DW_FORM_data1;
8318 case 2:
8319 return DW_FORM_data2;
8320 case 4:
8321 return DW_FORM_data4;
8322 default:
8323 gcc_unreachable ();
8326 case dw_val_class_data8:
8327 return DW_FORM_data8;
8329 case dw_val_class_high_pc:
8330 switch (DWARF2_ADDR_SIZE)
8332 case 1:
8333 return DW_FORM_data1;
8334 case 2:
8335 return DW_FORM_data2;
8336 case 4:
8337 return DW_FORM_data4;
8338 case 8:
8339 return DW_FORM_data8;
8340 default:
8341 gcc_unreachable ();
8344 default:
8345 gcc_unreachable ();
8349 /* Output the encoding of an attribute value. */
8351 static void
8352 output_value_format (dw_attr_ref a)
8354 enum dwarf_form form = value_format (a);
8356 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8359 /* Given a die and id, produce the appropriate abbreviations. */
8361 static void
8362 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8364 unsigned ix;
8365 dw_attr_ref a_attr;
8367 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8368 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8369 dwarf_tag_name (abbrev->die_tag));
8371 if (abbrev->die_child != NULL)
8372 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8373 else
8374 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8376 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8378 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8379 dwarf_attr_name (a_attr->dw_attr));
8380 output_value_format (a_attr);
8383 dw2_asm_output_data (1, 0, NULL);
8384 dw2_asm_output_data (1, 0, NULL);
8388 /* Output the .debug_abbrev section which defines the DIE abbreviation
8389 table. */
8391 static void
8392 output_abbrev_section (void)
8394 unsigned long abbrev_id;
8396 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8397 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8399 /* Terminate the table. */
8400 dw2_asm_output_data (1, 0, NULL);
8403 /* Output a symbol we can use to refer to this DIE from another CU. */
8405 static inline void
8406 output_die_symbol (dw_die_ref die)
8408 const char *sym = die->die_id.die_symbol;
8410 gcc_assert (!die->comdat_type_p);
8412 if (sym == 0)
8413 return;
8415 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8416 /* We make these global, not weak; if the target doesn't support
8417 .linkonce, it doesn't support combining the sections, so debugging
8418 will break. */
8419 targetm.asm_out.globalize_label (asm_out_file, sym);
8421 ASM_OUTPUT_LABEL (asm_out_file, sym);
8424 /* Return a new location list, given the begin and end range, and the
8425 expression. */
8427 static inline dw_loc_list_ref
8428 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8429 const char *section)
8431 dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8433 retlist->begin = begin;
8434 retlist->begin_entry = NULL;
8435 retlist->end = end;
8436 retlist->expr = expr;
8437 retlist->section = section;
8439 return retlist;
8442 /* Generate a new internal symbol for this location list node, if it
8443 hasn't got one yet. */
8445 static inline void
8446 gen_llsym (dw_loc_list_ref list)
8448 gcc_assert (!list->ll_symbol);
8449 list->ll_symbol = gen_internal_sym ("LLST");
8452 /* Output the location list given to us. */
8454 static void
8455 output_loc_list (dw_loc_list_ref list_head)
8457 dw_loc_list_ref curr = list_head;
8459 if (list_head->emitted)
8460 return;
8461 list_head->emitted = true;
8463 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8465 /* Walk the location list, and output each range + expression. */
8466 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8468 unsigned long size;
8469 /* Don't output an entry that starts and ends at the same address. */
8470 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8471 continue;
8472 size = size_of_locs (curr->expr);
8473 /* If the expression is too large, drop it on the floor. We could
8474 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8475 in the expression, but >= 64KB expressions for a single value
8476 in a single range are unlikely very useful. */
8477 if (size > 0xffff)
8478 continue;
8479 if (dwarf_split_debug_info)
8481 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8482 "Location list start/length entry (%s)",
8483 list_head->ll_symbol);
8484 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8485 "Location list range start index (%s)",
8486 curr->begin);
8487 /* The length field is 4 bytes. If we ever need to support
8488 an 8-byte length, we can add a new DW_LLE code or fall back
8489 to DW_LLE_GNU_start_end_entry. */
8490 dw2_asm_output_delta (4, curr->end, curr->begin,
8491 "Location list range length (%s)",
8492 list_head->ll_symbol);
8494 else if (!have_multiple_function_sections)
8496 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8497 "Location list begin address (%s)",
8498 list_head->ll_symbol);
8499 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8500 "Location list end address (%s)",
8501 list_head->ll_symbol);
8503 else
8505 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8506 "Location list begin address (%s)",
8507 list_head->ll_symbol);
8508 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8509 "Location list end address (%s)",
8510 list_head->ll_symbol);
8513 /* Output the block length for this list of location operations. */
8514 gcc_assert (size <= 0xffff);
8515 dw2_asm_output_data (2, size, "%s", "Location expression size");
8517 output_loc_sequence (curr->expr, -1);
8520 if (dwarf_split_debug_info)
8521 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8522 "Location list terminator (%s)",
8523 list_head->ll_symbol);
8524 else
8526 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8527 "Location list terminator begin (%s)",
8528 list_head->ll_symbol);
8529 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8530 "Location list terminator end (%s)",
8531 list_head->ll_symbol);
8535 /* Output a range_list offset into the debug_range section. Emit a
8536 relocated reference if val_entry is NULL, otherwise, emit an
8537 indirect reference. */
8539 static void
8540 output_range_list_offset (dw_attr_ref a)
8542 const char *name = dwarf_attr_name (a->dw_attr);
8544 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8546 char *p = strchr (ranges_section_label, '\0');
8547 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8548 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8549 debug_ranges_section, "%s", name);
8550 *p = '\0';
8552 else
8553 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8554 "%s (offset from %s)", name, ranges_section_label);
8557 /* Output the offset into the debug_loc section. */
8559 static void
8560 output_loc_list_offset (dw_attr_ref a)
8562 char *sym = AT_loc_list (a)->ll_symbol;
8564 gcc_assert (sym);
8565 if (dwarf_split_debug_info)
8566 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8567 "%s", dwarf_attr_name (a->dw_attr));
8568 else
8569 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8570 "%s", dwarf_attr_name (a->dw_attr));
8573 /* Output an attribute's index or value appropriately. */
8575 static void
8576 output_attr_index_or_value (dw_attr_ref a)
8578 const char *name = dwarf_attr_name (a->dw_attr);
8580 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8582 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8583 return;
8585 switch (AT_class (a))
8587 case dw_val_class_addr:
8588 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8589 break;
8590 case dw_val_class_high_pc:
8591 case dw_val_class_lbl_id:
8592 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8593 break;
8594 case dw_val_class_loc_list:
8595 output_loc_list_offset (a);
8596 break;
8597 default:
8598 gcc_unreachable ();
8602 /* Output a type signature. */
8604 static inline void
8605 output_signature (const char *sig, const char *name)
8607 int i;
8609 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8610 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8613 /* Output the DIE and its attributes. Called recursively to generate
8614 the definitions of each child DIE. */
8616 static void
8617 output_die (dw_die_ref die)
8619 dw_attr_ref a;
8620 dw_die_ref c;
8621 unsigned long size;
8622 unsigned ix;
8624 /* If someone in another CU might refer to us, set up a symbol for
8625 them to point to. */
8626 if (! die->comdat_type_p && die->die_id.die_symbol)
8627 output_die_symbol (die);
8629 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8630 (unsigned long)die->die_offset,
8631 dwarf_tag_name (die->die_tag));
8633 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8635 const char *name = dwarf_attr_name (a->dw_attr);
8637 switch (AT_class (a))
8639 case dw_val_class_addr:
8640 output_attr_index_or_value (a);
8641 break;
8643 case dw_val_class_offset:
8644 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8645 "%s", name);
8646 break;
8648 case dw_val_class_range_list:
8649 output_range_list_offset (a);
8650 break;
8652 case dw_val_class_loc:
8653 size = size_of_locs (AT_loc (a));
8655 /* Output the block length for this list of location operations. */
8656 if (dwarf_version >= 4)
8657 dw2_asm_output_data_uleb128 (size, "%s", name);
8658 else
8659 dw2_asm_output_data (constant_size (size), size, "%s", name);
8661 output_loc_sequence (AT_loc (a), -1);
8662 break;
8664 case dw_val_class_const:
8665 /* ??? It would be slightly more efficient to use a scheme like is
8666 used for unsigned constants below, but gdb 4.x does not sign
8667 extend. Gdb 5.x does sign extend. */
8668 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8669 break;
8671 case dw_val_class_unsigned_const:
8673 int csize = constant_size (AT_unsigned (a));
8674 if (dwarf_version == 3
8675 && a->dw_attr == DW_AT_data_member_location
8676 && csize >= 4)
8677 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8678 else
8679 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8681 break;
8683 case dw_val_class_const_double:
8685 unsigned HOST_WIDE_INT first, second;
8687 if (HOST_BITS_PER_WIDE_INT >= 64)
8688 dw2_asm_output_data (1,
8689 HOST_BITS_PER_DOUBLE_INT
8690 / HOST_BITS_PER_CHAR,
8691 NULL);
8693 if (WORDS_BIG_ENDIAN)
8695 first = a->dw_attr_val.v.val_double.high;
8696 second = a->dw_attr_val.v.val_double.low;
8698 else
8700 first = a->dw_attr_val.v.val_double.low;
8701 second = a->dw_attr_val.v.val_double.high;
8704 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8705 first, "%s", name);
8706 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8707 second, NULL);
8709 break;
8711 case dw_val_class_wide_int:
8713 int i;
8714 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8715 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8716 if (len * HOST_BITS_PER_WIDE_INT > 64)
8717 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8718 NULL);
8720 if (WORDS_BIG_ENDIAN)
8721 for (i = len; i >= 0; --i)
8723 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8724 name);
8725 name = NULL;
8727 else
8728 for (i = 0; i < len; ++i)
8730 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8731 name);
8732 name = NULL;
8735 break;
8737 case dw_val_class_vec:
8739 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8740 unsigned int len = a->dw_attr_val.v.val_vec.length;
8741 unsigned int i;
8742 unsigned char *p;
8744 dw2_asm_output_data (constant_size (len * elt_size),
8745 len * elt_size, "%s", name);
8746 if (elt_size > sizeof (HOST_WIDE_INT))
8748 elt_size /= 2;
8749 len *= 2;
8751 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8752 i < len;
8753 i++, p += elt_size)
8754 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8755 "fp or vector constant word %u", i);
8756 break;
8759 case dw_val_class_flag:
8760 if (dwarf_version >= 4)
8762 /* Currently all add_AT_flag calls pass in 1 as last argument,
8763 so DW_FORM_flag_present can be used. If that ever changes,
8764 we'll need to use DW_FORM_flag and have some optimization
8765 in build_abbrev_table that will change those to
8766 DW_FORM_flag_present if it is set to 1 in all DIEs using
8767 the same abbrev entry. */
8768 gcc_assert (AT_flag (a) == 1);
8769 if (flag_debug_asm)
8770 fprintf (asm_out_file, "\t\t\t%s %s\n",
8771 ASM_COMMENT_START, name);
8772 break;
8774 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8775 break;
8777 case dw_val_class_loc_list:
8778 output_attr_index_or_value (a);
8779 break;
8781 case dw_val_class_die_ref:
8782 if (AT_ref_external (a))
8784 if (AT_ref (a)->comdat_type_p)
8786 comdat_type_node_ref type_node =
8787 AT_ref (a)->die_id.die_type_node;
8789 gcc_assert (type_node);
8790 output_signature (type_node->signature, name);
8792 else
8794 const char *sym = AT_ref (a)->die_id.die_symbol;
8795 int size;
8797 gcc_assert (sym);
8798 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8799 length, whereas in DWARF3 it's always sized as an
8800 offset. */
8801 if (dwarf_version == 2)
8802 size = DWARF2_ADDR_SIZE;
8803 else
8804 size = DWARF_OFFSET_SIZE;
8805 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8806 name);
8809 else
8811 gcc_assert (AT_ref (a)->die_offset);
8812 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8813 "%s", name);
8815 break;
8817 case dw_val_class_fde_ref:
8819 char l1[20];
8821 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8822 a->dw_attr_val.v.val_fde_index * 2);
8823 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8824 "%s", name);
8826 break;
8828 case dw_val_class_vms_delta:
8829 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8830 AT_vms_delta2 (a), AT_vms_delta1 (a),
8831 "%s", name);
8832 break;
8834 case dw_val_class_lbl_id:
8835 output_attr_index_or_value (a);
8836 break;
8838 case dw_val_class_lineptr:
8839 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8840 debug_line_section, "%s", name);
8841 break;
8843 case dw_val_class_macptr:
8844 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8845 debug_macinfo_section, "%s", name);
8846 break;
8848 case dw_val_class_str:
8849 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8850 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8851 a->dw_attr_val.v.val_str->label,
8852 debug_str_section,
8853 "%s: \"%s\"", name, AT_string (a));
8854 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8855 dw2_asm_output_data_uleb128 (AT_index (a),
8856 "%s: \"%s\"", name, AT_string (a));
8857 else
8858 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8859 break;
8861 case dw_val_class_file:
8863 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8865 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8866 a->dw_attr_val.v.val_file->filename);
8867 break;
8870 case dw_val_class_data8:
8872 int i;
8874 for (i = 0; i < 8; i++)
8875 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8876 i == 0 ? "%s" : NULL, name);
8877 break;
8880 case dw_val_class_high_pc:
8881 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8882 get_AT_low_pc (die), "DW_AT_high_pc");
8883 break;
8885 default:
8886 gcc_unreachable ();
8890 FOR_EACH_CHILD (die, c, output_die (c));
8892 /* Add null byte to terminate sibling list. */
8893 if (die->die_child != NULL)
8894 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8895 (unsigned long) die->die_offset);
8898 /* Output the compilation unit that appears at the beginning of the
8899 .debug_info section, and precedes the DIE descriptions. */
8901 static void
8902 output_compilation_unit_header (void)
8904 int ver = dwarf_version;
8906 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8907 dw2_asm_output_data (4, 0xffffffff,
8908 "Initial length escape value indicating 64-bit DWARF extension");
8909 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8910 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8911 "Length of Compilation Unit Info");
8912 dw2_asm_output_data (2, ver, "DWARF version number");
8913 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8914 debug_abbrev_section,
8915 "Offset Into Abbrev. Section");
8916 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8919 /* Output the compilation unit DIE and its children. */
8921 static void
8922 output_comp_unit (dw_die_ref die, int output_if_empty)
8924 const char *secname, *oldsym;
8925 char *tmp;
8926 external_ref_hash_type extern_map;
8928 /* Unless we are outputting main CU, we may throw away empty ones. */
8929 if (!output_if_empty && die->die_child == NULL)
8930 return;
8932 /* Even if there are no children of this DIE, we must output the information
8933 about the compilation unit. Otherwise, on an empty translation unit, we
8934 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8935 will then complain when examining the file. First mark all the DIEs in
8936 this CU so we know which get local refs. */
8937 mark_dies (die);
8939 extern_map = optimize_external_refs (die);
8941 build_abbrev_table (die, extern_map);
8943 extern_map.dispose ();
8945 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8946 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8947 calc_die_sizes (die);
8949 oldsym = die->die_id.die_symbol;
8950 if (oldsym)
8952 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8954 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8955 secname = tmp;
8956 die->die_id.die_symbol = NULL;
8957 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8959 else
8961 switch_to_section (debug_info_section);
8962 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8963 info_section_emitted = true;
8966 /* Output debugging information. */
8967 output_compilation_unit_header ();
8968 output_die (die);
8970 /* Leave the marks on the main CU, so we can check them in
8971 output_pubnames. */
8972 if (oldsym)
8974 unmark_dies (die);
8975 die->die_id.die_symbol = oldsym;
8979 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8980 and .debug_pubtypes. This is configured per-target, but can be
8981 overridden by the -gpubnames or -gno-pubnames options. */
8983 static inline bool
8984 want_pubnames (void)
8986 if (debug_generate_pub_sections != -1)
8987 return debug_generate_pub_sections;
8988 return targetm.want_debug_pub_sections;
8991 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
8993 static void
8994 add_AT_pubnames (dw_die_ref die)
8996 if (want_pubnames ())
8997 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9000 /* Add a string attribute value to a skeleton DIE. */
9002 static inline void
9003 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9004 const char *str)
9006 dw_attr_node attr;
9007 struct indirect_string_node *node;
9009 if (! skeleton_debug_str_hash)
9010 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
9011 debug_str_eq, NULL);
9013 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9014 find_string_form (node);
9015 if (node->form == DW_FORM_GNU_str_index)
9016 node->form = DW_FORM_strp;
9018 attr.dw_attr = attr_kind;
9019 attr.dw_attr_val.val_class = dw_val_class_str;
9020 attr.dw_attr_val.val_entry = NULL;
9021 attr.dw_attr_val.v.val_str = node;
9022 add_dwarf_attr (die, &attr);
9025 /* Helper function to generate top-level dies for skeleton debug_info and
9026 debug_types. */
9028 static void
9029 add_top_level_skeleton_die_attrs (dw_die_ref die)
9031 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9032 const char *comp_dir = comp_dir_string ();
9034 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9035 if (comp_dir != NULL)
9036 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9037 add_AT_pubnames (die);
9038 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9041 /* Return the single type-unit die for skeleton type units. */
9043 static dw_die_ref
9044 get_skeleton_type_unit (void)
9046 /* For dwarf_split_debug_sections with use_type info, all type units in the
9047 skeleton sections have identical dies (but different headers). This
9048 single die will be output many times. */
9050 static dw_die_ref skeleton_type_unit = NULL;
9052 if (skeleton_type_unit == NULL)
9054 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
9055 add_top_level_skeleton_die_attrs (skeleton_type_unit);
9056 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
9058 return skeleton_type_unit;
9061 /* Output skeleton debug sections that point to the dwo file. */
9063 static void
9064 output_skeleton_debug_sections (dw_die_ref comp_unit)
9066 /* These attributes will be found in the full debug_info section. */
9067 remove_AT (comp_unit, DW_AT_producer);
9068 remove_AT (comp_unit, DW_AT_language);
9070 switch_to_section (debug_skeleton_info_section);
9071 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9073 /* Produce the skeleton compilation-unit header. This one differs enough from
9074 a normal CU header that it's better not to call output_compilation_unit
9075 header. */
9076 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9077 dw2_asm_output_data (4, 0xffffffff,
9078 "Initial length escape value indicating 64-bit DWARF extension");
9080 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9081 DWARF_COMPILE_UNIT_HEADER_SIZE
9082 - DWARF_INITIAL_LENGTH_SIZE
9083 + size_of_die (comp_unit),
9084 "Length of Compilation Unit Info");
9085 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9086 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9087 debug_abbrev_section,
9088 "Offset Into Abbrev. Section");
9089 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9091 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9092 output_die (comp_unit);
9094 /* Build the skeleton debug_abbrev section. */
9095 switch_to_section (debug_skeleton_abbrev_section);
9096 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9098 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9099 if (use_debug_types)
9100 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
9102 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9105 /* Output a comdat type unit DIE and its children. */
9107 static void
9108 output_comdat_type_unit (comdat_type_node *node)
9110 const char *secname;
9111 char *tmp;
9112 int i;
9113 #if defined (OBJECT_FORMAT_ELF)
9114 tree comdat_key;
9115 #endif
9116 external_ref_hash_type extern_map;
9118 /* First mark all the DIEs in this CU so we know which get local refs. */
9119 mark_dies (node->root_die);
9121 extern_map = optimize_external_refs (node->root_die);
9123 build_abbrev_table (node->root_die, extern_map);
9125 extern_map.dispose ();
9127 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9128 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9129 calc_die_sizes (node->root_die);
9131 #if defined (OBJECT_FORMAT_ELF)
9132 if (!dwarf_split_debug_info)
9133 secname = ".debug_types";
9134 else
9135 secname = ".debug_types.dwo";
9137 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9138 sprintf (tmp, "wt.");
9139 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9140 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9141 comdat_key = get_identifier (tmp);
9142 targetm.asm_out.named_section (secname,
9143 SECTION_DEBUG | SECTION_LINKONCE,
9144 comdat_key);
9145 #else
9146 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9147 sprintf (tmp, ".gnu.linkonce.wt.");
9148 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9149 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9150 secname = tmp;
9151 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9152 #endif
9154 /* Output debugging information. */
9155 output_compilation_unit_header ();
9156 output_signature (node->signature, "Type Signature");
9157 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9158 "Offset to Type DIE");
9159 output_die (node->root_die);
9161 unmark_dies (node->root_die);
9163 #if defined (OBJECT_FORMAT_ELF)
9164 if (dwarf_split_debug_info)
9166 /* Produce the skeleton type-unit header. */
9167 const char *secname = ".debug_types";
9169 targetm.asm_out.named_section (secname,
9170 SECTION_DEBUG | SECTION_LINKONCE,
9171 comdat_key);
9172 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9173 dw2_asm_output_data (4, 0xffffffff,
9174 "Initial length escape value indicating 64-bit DWARF extension");
9176 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9177 DWARF_COMPILE_UNIT_HEADER_SIZE
9178 - DWARF_INITIAL_LENGTH_SIZE
9179 + size_of_die (get_skeleton_type_unit ())
9180 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9181 "Length of Type Unit Info");
9182 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9183 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9184 debug_skeleton_abbrev_section_label,
9185 debug_abbrev_section,
9186 "Offset Into Abbrev. Section");
9187 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9188 output_signature (node->signature, "Type Signature");
9189 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9191 output_die (get_skeleton_type_unit ());
9193 #endif
9196 /* Return the DWARF2/3 pubname associated with a decl. */
9198 static const char *
9199 dwarf2_name (tree decl, int scope)
9201 if (DECL_NAMELESS (decl))
9202 return NULL;
9203 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9206 /* Add a new entry to .debug_pubnames if appropriate. */
9208 static void
9209 add_pubname_string (const char *str, dw_die_ref die)
9211 pubname_entry e;
9213 e.die = die;
9214 e.name = xstrdup (str);
9215 vec_safe_push (pubname_table, e);
9218 static void
9219 add_pubname (tree decl, dw_die_ref die)
9221 if (!want_pubnames ())
9222 return;
9224 /* Don't add items to the table when we expect that the consumer will have
9225 just read the enclosing die. For example, if the consumer is looking at a
9226 class_member, it will either be inside the class already, or will have just
9227 looked up the class to find the member. Either way, searching the class is
9228 faster than searching the index. */
9229 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9230 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9232 const char *name = dwarf2_name (decl, 1);
9234 if (name)
9235 add_pubname_string (name, die);
9239 /* Add an enumerator to the pubnames section. */
9241 static void
9242 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9244 pubname_entry e;
9246 gcc_assert (scope_name);
9247 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9248 e.die = die;
9249 vec_safe_push (pubname_table, e);
9252 /* Add a new entry to .debug_pubtypes if appropriate. */
9254 static void
9255 add_pubtype (tree decl, dw_die_ref die)
9257 pubname_entry e;
9259 if (!want_pubnames ())
9260 return;
9262 if ((TREE_PUBLIC (decl)
9263 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9264 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9266 tree scope = NULL;
9267 const char *scope_name = "";
9268 const char *sep = is_cxx () ? "::" : ".";
9269 const char *name;
9271 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9272 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9274 scope_name = lang_hooks.dwarf_name (scope, 1);
9275 if (scope_name != NULL && scope_name[0] != '\0')
9276 scope_name = concat (scope_name, sep, NULL);
9277 else
9278 scope_name = "";
9281 if (TYPE_P (decl))
9282 name = type_tag (decl);
9283 else
9284 name = lang_hooks.dwarf_name (decl, 1);
9286 /* If we don't have a name for the type, there's no point in adding
9287 it to the table. */
9288 if (name != NULL && name[0] != '\0')
9290 e.die = die;
9291 e.name = concat (scope_name, name, NULL);
9292 vec_safe_push (pubtype_table, e);
9295 /* Although it might be more consistent to add the pubinfo for the
9296 enumerators as their dies are created, they should only be added if the
9297 enum type meets the criteria above. So rather than re-check the parent
9298 enum type whenever an enumerator die is created, just output them all
9299 here. This isn't protected by the name conditional because anonymous
9300 enums don't have names. */
9301 if (die->die_tag == DW_TAG_enumeration_type)
9303 dw_die_ref c;
9305 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9310 /* Output a single entry in the pubnames table. */
9312 static void
9313 output_pubname (dw_offset die_offset, pubname_entry *entry)
9315 dw_die_ref die = entry->die;
9316 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9318 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9320 if (debug_generate_pub_sections == 2)
9322 /* This logic follows gdb's method for determining the value of the flag
9323 byte. */
9324 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9325 switch (die->die_tag)
9327 case DW_TAG_typedef:
9328 case DW_TAG_base_type:
9329 case DW_TAG_subrange_type:
9330 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9331 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9332 break;
9333 case DW_TAG_enumerator:
9334 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9335 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9336 if (!is_cxx () && !is_java ())
9337 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9338 break;
9339 case DW_TAG_subprogram:
9340 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9341 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9342 if (!is_ada ())
9343 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9344 break;
9345 case DW_TAG_constant:
9346 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9347 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9348 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9349 break;
9350 case DW_TAG_variable:
9351 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9352 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9353 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9354 break;
9355 case DW_TAG_namespace:
9356 case DW_TAG_imported_declaration:
9357 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9358 break;
9359 case DW_TAG_class_type:
9360 case DW_TAG_interface_type:
9361 case DW_TAG_structure_type:
9362 case DW_TAG_union_type:
9363 case DW_TAG_enumeration_type:
9364 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9365 if (!is_cxx () && !is_java ())
9366 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9367 break;
9368 default:
9369 /* An unusual tag. Leave the flag-byte empty. */
9370 break;
9372 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9373 "GDB-index flags");
9376 dw2_asm_output_nstring (entry->name, -1, "external name");
9380 /* Output the public names table used to speed up access to externally
9381 visible names; or the public types table used to find type definitions. */
9383 static void
9384 output_pubnames (vec<pubname_entry, va_gc> *names)
9386 unsigned i;
9387 unsigned long pubnames_length = size_of_pubnames (names);
9388 pubname_ref pub;
9390 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9391 dw2_asm_output_data (4, 0xffffffff,
9392 "Initial length escape value indicating 64-bit DWARF extension");
9393 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9395 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9396 dw2_asm_output_data (2, 2, "DWARF Version");
9398 if (dwarf_split_debug_info)
9399 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9400 debug_skeleton_info_section,
9401 "Offset of Compilation Unit Info");
9402 else
9403 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9404 debug_info_section,
9405 "Offset of Compilation Unit Info");
9406 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9407 "Compilation Unit Length");
9409 FOR_EACH_VEC_ELT (*names, i, pub)
9411 if (include_pubname_in_output (names, pub))
9413 dw_offset die_offset = pub->die->die_offset;
9415 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9416 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9417 gcc_assert (pub->die->die_mark);
9419 /* If we're putting types in their own .debug_types sections,
9420 the .debug_pubtypes table will still point to the compile
9421 unit (not the type unit), so we want to use the offset of
9422 the skeleton DIE (if there is one). */
9423 if (pub->die->comdat_type_p && names == pubtype_table)
9425 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9427 if (type_node != NULL)
9428 die_offset = (type_node->skeleton_die != NULL
9429 ? type_node->skeleton_die->die_offset
9430 : 0);
9433 output_pubname (die_offset, pub);
9437 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9440 /* Output public names and types tables if necessary. */
9442 static void
9443 output_pubtables (void)
9445 if (!want_pubnames () || !info_section_emitted)
9446 return;
9448 switch_to_section (debug_pubnames_section);
9449 output_pubnames (pubname_table);
9450 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9451 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9452 simply won't look for the section. */
9453 switch_to_section (debug_pubtypes_section);
9454 output_pubnames (pubtype_table);
9458 /* Output the information that goes into the .debug_aranges table.
9459 Namely, define the beginning and ending address range of the
9460 text section generated for this compilation unit. */
9462 static void
9463 output_aranges (unsigned long aranges_length)
9465 unsigned i;
9467 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9468 dw2_asm_output_data (4, 0xffffffff,
9469 "Initial length escape value indicating 64-bit DWARF extension");
9470 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9471 "Length of Address Ranges Info");
9472 /* Version number for aranges is still 2, even in DWARF3. */
9473 dw2_asm_output_data (2, 2, "DWARF Version");
9474 if (dwarf_split_debug_info)
9475 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9476 debug_skeleton_info_section,
9477 "Offset of Compilation Unit Info");
9478 else
9479 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9480 debug_info_section,
9481 "Offset of Compilation Unit Info");
9482 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9483 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9485 /* We need to align to twice the pointer size here. */
9486 if (DWARF_ARANGES_PAD_SIZE)
9488 /* Pad using a 2 byte words so that padding is correct for any
9489 pointer size. */
9490 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9491 2 * DWARF2_ADDR_SIZE);
9492 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9493 dw2_asm_output_data (2, 0, NULL);
9496 /* It is necessary not to output these entries if the sections were
9497 not used; if the sections were not used, the length will be 0 and
9498 the address may end up as 0 if the section is discarded by ld
9499 --gc-sections, leaving an invalid (0, 0) entry that can be
9500 confused with the terminator. */
9501 if (text_section_used)
9503 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9504 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9505 text_section_label, "Length");
9507 if (cold_text_section_used)
9509 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9510 "Address");
9511 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9512 cold_text_section_label, "Length");
9515 if (have_multiple_function_sections)
9517 unsigned fde_idx;
9518 dw_fde_ref fde;
9520 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9522 if (DECL_IGNORED_P (fde->decl))
9523 continue;
9524 if (!fde->in_std_section)
9526 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9527 "Address");
9528 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9529 fde->dw_fde_begin, "Length");
9531 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9533 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9534 "Address");
9535 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9536 fde->dw_fde_second_begin, "Length");
9541 /* Output the terminator words. */
9542 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9543 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9546 /* Add a new entry to .debug_ranges. Return the offset at which it
9547 was placed. */
9549 static unsigned int
9550 add_ranges_num (int num)
9552 unsigned int in_use = ranges_table_in_use;
9554 if (in_use == ranges_table_allocated)
9556 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9557 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9558 ranges_table_allocated);
9559 memset (ranges_table + ranges_table_in_use, 0,
9560 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9563 ranges_table[in_use].num = num;
9564 ranges_table_in_use = in_use + 1;
9566 return in_use * 2 * DWARF2_ADDR_SIZE;
9569 /* Add a new entry to .debug_ranges corresponding to a block, or a
9570 range terminator if BLOCK is NULL. */
9572 static unsigned int
9573 add_ranges (const_tree block)
9575 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9578 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9579 When using dwarf_split_debug_info, address attributes in dies destined
9580 for the final executable should be direct references--setting the
9581 parameter force_direct ensures this behavior. */
9583 static void
9584 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9585 bool *added, bool force_direct)
9587 unsigned int in_use = ranges_by_label_in_use;
9588 unsigned int offset;
9590 if (in_use == ranges_by_label_allocated)
9592 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9593 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9594 ranges_by_label,
9595 ranges_by_label_allocated);
9596 memset (ranges_by_label + ranges_by_label_in_use, 0,
9597 RANGES_TABLE_INCREMENT
9598 * sizeof (struct dw_ranges_by_label_struct));
9601 ranges_by_label[in_use].begin = begin;
9602 ranges_by_label[in_use].end = end;
9603 ranges_by_label_in_use = in_use + 1;
9605 offset = add_ranges_num (-(int)in_use - 1);
9606 if (!*added)
9608 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9609 *added = true;
9613 static void
9614 output_ranges (void)
9616 unsigned i;
9617 static const char *const start_fmt = "Offset %#x";
9618 const char *fmt = start_fmt;
9620 for (i = 0; i < ranges_table_in_use; i++)
9622 int block_num = ranges_table[i].num;
9624 if (block_num > 0)
9626 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9627 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9629 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9630 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9632 /* If all code is in the text section, then the compilation
9633 unit base address defaults to DW_AT_low_pc, which is the
9634 base of the text section. */
9635 if (!have_multiple_function_sections)
9637 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9638 text_section_label,
9639 fmt, i * 2 * DWARF2_ADDR_SIZE);
9640 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9641 text_section_label, NULL);
9644 /* Otherwise, the compilation unit base address is zero,
9645 which allows us to use absolute addresses, and not worry
9646 about whether the target supports cross-section
9647 arithmetic. */
9648 else
9650 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9651 fmt, i * 2 * DWARF2_ADDR_SIZE);
9652 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9655 fmt = NULL;
9658 /* Negative block_num stands for an index into ranges_by_label. */
9659 else if (block_num < 0)
9661 int lab_idx = - block_num - 1;
9663 if (!have_multiple_function_sections)
9665 gcc_unreachable ();
9666 #if 0
9667 /* If we ever use add_ranges_by_labels () for a single
9668 function section, all we have to do is to take out
9669 the #if 0 above. */
9670 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9671 ranges_by_label[lab_idx].begin,
9672 text_section_label,
9673 fmt, i * 2 * DWARF2_ADDR_SIZE);
9674 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9675 ranges_by_label[lab_idx].end,
9676 text_section_label, NULL);
9677 #endif
9679 else
9681 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9682 ranges_by_label[lab_idx].begin,
9683 fmt, i * 2 * DWARF2_ADDR_SIZE);
9684 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9685 ranges_by_label[lab_idx].end,
9686 NULL);
9689 else
9691 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9692 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9693 fmt = start_fmt;
9698 /* Data structure containing information about input files. */
9699 struct file_info
9701 const char *path; /* Complete file name. */
9702 const char *fname; /* File name part. */
9703 int length; /* Length of entire string. */
9704 struct dwarf_file_data * file_idx; /* Index in input file table. */
9705 int dir_idx; /* Index in directory table. */
9708 /* Data structure containing information about directories with source
9709 files. */
9710 struct dir_info
9712 const char *path; /* Path including directory name. */
9713 int length; /* Path length. */
9714 int prefix; /* Index of directory entry which is a prefix. */
9715 int count; /* Number of files in this directory. */
9716 int dir_idx; /* Index of directory used as base. */
9719 /* Callback function for file_info comparison. We sort by looking at
9720 the directories in the path. */
9722 static int
9723 file_info_cmp (const void *p1, const void *p2)
9725 const struct file_info *const s1 = (const struct file_info *) p1;
9726 const struct file_info *const s2 = (const struct file_info *) p2;
9727 const unsigned char *cp1;
9728 const unsigned char *cp2;
9730 /* Take care of file names without directories. We need to make sure that
9731 we return consistent values to qsort since some will get confused if
9732 we return the same value when identical operands are passed in opposite
9733 orders. So if neither has a directory, return 0 and otherwise return
9734 1 or -1 depending on which one has the directory. */
9735 if ((s1->path == s1->fname || s2->path == s2->fname))
9736 return (s2->path == s2->fname) - (s1->path == s1->fname);
9738 cp1 = (const unsigned char *) s1->path;
9739 cp2 = (const unsigned char *) s2->path;
9741 while (1)
9743 ++cp1;
9744 ++cp2;
9745 /* Reached the end of the first path? If so, handle like above. */
9746 if ((cp1 == (const unsigned char *) s1->fname)
9747 || (cp2 == (const unsigned char *) s2->fname))
9748 return ((cp2 == (const unsigned char *) s2->fname)
9749 - (cp1 == (const unsigned char *) s1->fname));
9751 /* Character of current path component the same? */
9752 else if (*cp1 != *cp2)
9753 return *cp1 - *cp2;
9757 struct file_name_acquire_data
9759 struct file_info *files;
9760 int used_files;
9761 int max_files;
9764 /* Traversal function for the hash table. */
9766 static int
9767 file_name_acquire (void ** slot, void *data)
9769 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9770 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9771 struct file_info *fi;
9772 const char *f;
9774 gcc_assert (fnad->max_files >= d->emitted_number);
9776 if (! d->emitted_number)
9777 return 1;
9779 gcc_assert (fnad->max_files != fnad->used_files);
9781 fi = fnad->files + fnad->used_files++;
9783 /* Skip all leading "./". */
9784 f = d->filename;
9785 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9786 f += 2;
9788 /* Create a new array entry. */
9789 fi->path = f;
9790 fi->length = strlen (f);
9791 fi->file_idx = d;
9793 /* Search for the file name part. */
9794 f = strrchr (f, DIR_SEPARATOR);
9795 #if defined (DIR_SEPARATOR_2)
9797 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9799 if (g != NULL)
9801 if (f == NULL || f < g)
9802 f = g;
9805 #endif
9807 fi->fname = f == NULL ? fi->path : f + 1;
9808 return 1;
9811 /* Output the directory table and the file name table. We try to minimize
9812 the total amount of memory needed. A heuristic is used to avoid large
9813 slowdowns with many input files. */
9815 static void
9816 output_file_names (void)
9818 struct file_name_acquire_data fnad;
9819 int numfiles;
9820 struct file_info *files;
9821 struct dir_info *dirs;
9822 int *saved;
9823 int *savehere;
9824 int *backmap;
9825 int ndirs;
9826 int idx_offset;
9827 int i;
9829 if (!last_emitted_file)
9831 dw2_asm_output_data (1, 0, "End directory table");
9832 dw2_asm_output_data (1, 0, "End file name table");
9833 return;
9836 numfiles = last_emitted_file->emitted_number;
9838 /* Allocate the various arrays we need. */
9839 files = XALLOCAVEC (struct file_info, numfiles);
9840 dirs = XALLOCAVEC (struct dir_info, numfiles);
9842 fnad.files = files;
9843 fnad.used_files = 0;
9844 fnad.max_files = numfiles;
9845 htab_traverse (file_table, file_name_acquire, &fnad);
9846 gcc_assert (fnad.used_files == fnad.max_files);
9848 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9850 /* Find all the different directories used. */
9851 dirs[0].path = files[0].path;
9852 dirs[0].length = files[0].fname - files[0].path;
9853 dirs[0].prefix = -1;
9854 dirs[0].count = 1;
9855 dirs[0].dir_idx = 0;
9856 files[0].dir_idx = 0;
9857 ndirs = 1;
9859 for (i = 1; i < numfiles; i++)
9860 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9861 && memcmp (dirs[ndirs - 1].path, files[i].path,
9862 dirs[ndirs - 1].length) == 0)
9864 /* Same directory as last entry. */
9865 files[i].dir_idx = ndirs - 1;
9866 ++dirs[ndirs - 1].count;
9868 else
9870 int j;
9872 /* This is a new directory. */
9873 dirs[ndirs].path = files[i].path;
9874 dirs[ndirs].length = files[i].fname - files[i].path;
9875 dirs[ndirs].count = 1;
9876 dirs[ndirs].dir_idx = ndirs;
9877 files[i].dir_idx = ndirs;
9879 /* Search for a prefix. */
9880 dirs[ndirs].prefix = -1;
9881 for (j = 0; j < ndirs; j++)
9882 if (dirs[j].length < dirs[ndirs].length
9883 && dirs[j].length > 1
9884 && (dirs[ndirs].prefix == -1
9885 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9886 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9887 dirs[ndirs].prefix = j;
9889 ++ndirs;
9892 /* Now to the actual work. We have to find a subset of the directories which
9893 allow expressing the file name using references to the directory table
9894 with the least amount of characters. We do not do an exhaustive search
9895 where we would have to check out every combination of every single
9896 possible prefix. Instead we use a heuristic which provides nearly optimal
9897 results in most cases and never is much off. */
9898 saved = XALLOCAVEC (int, ndirs);
9899 savehere = XALLOCAVEC (int, ndirs);
9901 memset (saved, '\0', ndirs * sizeof (saved[0]));
9902 for (i = 0; i < ndirs; i++)
9904 int j;
9905 int total;
9907 /* We can always save some space for the current directory. But this
9908 does not mean it will be enough to justify adding the directory. */
9909 savehere[i] = dirs[i].length;
9910 total = (savehere[i] - saved[i]) * dirs[i].count;
9912 for (j = i + 1; j < ndirs; j++)
9914 savehere[j] = 0;
9915 if (saved[j] < dirs[i].length)
9917 /* Determine whether the dirs[i] path is a prefix of the
9918 dirs[j] path. */
9919 int k;
9921 k = dirs[j].prefix;
9922 while (k != -1 && k != (int) i)
9923 k = dirs[k].prefix;
9925 if (k == (int) i)
9927 /* Yes it is. We can possibly save some memory by
9928 writing the filenames in dirs[j] relative to
9929 dirs[i]. */
9930 savehere[j] = dirs[i].length;
9931 total += (savehere[j] - saved[j]) * dirs[j].count;
9936 /* Check whether we can save enough to justify adding the dirs[i]
9937 directory. */
9938 if (total > dirs[i].length + 1)
9940 /* It's worthwhile adding. */
9941 for (j = i; j < ndirs; j++)
9942 if (savehere[j] > 0)
9944 /* Remember how much we saved for this directory so far. */
9945 saved[j] = savehere[j];
9947 /* Remember the prefix directory. */
9948 dirs[j].dir_idx = i;
9953 /* Emit the directory name table. */
9954 idx_offset = dirs[0].length > 0 ? 1 : 0;
9955 for (i = 1 - idx_offset; i < ndirs; i++)
9956 dw2_asm_output_nstring (dirs[i].path,
9957 dirs[i].length
9958 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9959 "Directory Entry: %#x", i + idx_offset);
9961 dw2_asm_output_data (1, 0, "End directory table");
9963 /* We have to emit them in the order of emitted_number since that's
9964 used in the debug info generation. To do this efficiently we
9965 generate a back-mapping of the indices first. */
9966 backmap = XALLOCAVEC (int, numfiles);
9967 for (i = 0; i < numfiles; i++)
9968 backmap[files[i].file_idx->emitted_number - 1] = i;
9970 /* Now write all the file names. */
9971 for (i = 0; i < numfiles; i++)
9973 int file_idx = backmap[i];
9974 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9976 #ifdef VMS_DEBUGGING_INFO
9977 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9979 /* Setting these fields can lead to debugger miscomparisons,
9980 but VMS Debug requires them to be set correctly. */
9982 int ver;
9983 long long cdt;
9984 long siz;
9985 int maxfilelen = strlen (files[file_idx].path)
9986 + dirs[dir_idx].length
9987 + MAX_VMS_VERSION_LEN + 1;
9988 char *filebuf = XALLOCAVEC (char, maxfilelen);
9990 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9991 snprintf (filebuf, maxfilelen, "%s;%d",
9992 files[file_idx].path + dirs[dir_idx].length, ver);
9994 dw2_asm_output_nstring
9995 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9997 /* Include directory index. */
9998 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10000 /* Modification time. */
10001 dw2_asm_output_data_uleb128
10002 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10003 ? cdt : 0,
10004 NULL);
10006 /* File length in bytes. */
10007 dw2_asm_output_data_uleb128
10008 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10009 ? siz : 0,
10010 NULL);
10011 #else
10012 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10013 "File Entry: %#x", (unsigned) i + 1);
10015 /* Include directory index. */
10016 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10018 /* Modification time. */
10019 dw2_asm_output_data_uleb128 (0, NULL);
10021 /* File length in bytes. */
10022 dw2_asm_output_data_uleb128 (0, NULL);
10023 #endif /* VMS_DEBUGGING_INFO */
10026 dw2_asm_output_data (1, 0, "End file name table");
10030 /* Output one line number table into the .debug_line section. */
10032 static void
10033 output_one_line_info_table (dw_line_info_table *table)
10035 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10036 unsigned int current_line = 1;
10037 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10038 dw_line_info_entry *ent;
10039 size_t i;
10041 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10043 switch (ent->opcode)
10045 case LI_set_address:
10046 /* ??? Unfortunately, we have little choice here currently, and
10047 must always use the most general form. GCC does not know the
10048 address delta itself, so we can't use DW_LNS_advance_pc. Many
10049 ports do have length attributes which will give an upper bound
10050 on the address range. We could perhaps use length attributes
10051 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10052 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10054 /* This can handle any delta. This takes
10055 4+DWARF2_ADDR_SIZE bytes. */
10056 dw2_asm_output_data (1, 0, "set address %s", line_label);
10057 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10058 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10059 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10060 break;
10062 case LI_set_line:
10063 if (ent->val == current_line)
10065 /* We still need to start a new row, so output a copy insn. */
10066 dw2_asm_output_data (1, DW_LNS_copy,
10067 "copy line %u", current_line);
10069 else
10071 int line_offset = ent->val - current_line;
10072 int line_delta = line_offset - DWARF_LINE_BASE;
10074 current_line = ent->val;
10075 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10077 /* This can handle deltas from -10 to 234, using the current
10078 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10079 This takes 1 byte. */
10080 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10081 "line %u", current_line);
10083 else
10085 /* This can handle any delta. This takes at least 4 bytes,
10086 depending on the value being encoded. */
10087 dw2_asm_output_data (1, DW_LNS_advance_line,
10088 "advance to line %u", current_line);
10089 dw2_asm_output_data_sleb128 (line_offset, NULL);
10090 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10093 break;
10095 case LI_set_file:
10096 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10097 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10098 break;
10100 case LI_set_column:
10101 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10102 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10103 break;
10105 case LI_negate_stmt:
10106 current_is_stmt = !current_is_stmt;
10107 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10108 "is_stmt %d", current_is_stmt);
10109 break;
10111 case LI_set_prologue_end:
10112 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10113 "set prologue end");
10114 break;
10116 case LI_set_epilogue_begin:
10117 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10118 "set epilogue begin");
10119 break;
10121 case LI_set_discriminator:
10122 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10123 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10124 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10125 dw2_asm_output_data_uleb128 (ent->val, NULL);
10126 break;
10130 /* Emit debug info for the address of the end of the table. */
10131 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10132 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10133 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10134 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10136 dw2_asm_output_data (1, 0, "end sequence");
10137 dw2_asm_output_data_uleb128 (1, NULL);
10138 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10141 /* Output the source line number correspondence information. This
10142 information goes into the .debug_line section. */
10144 static void
10145 output_line_info (bool prologue_only)
10147 char l1[20], l2[20], p1[20], p2[20];
10148 int ver = dwarf_version;
10149 bool saw_one = false;
10150 int opc;
10152 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10153 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10154 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10155 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10157 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10158 dw2_asm_output_data (4, 0xffffffff,
10159 "Initial length escape value indicating 64-bit DWARF extension");
10160 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10161 "Length of Source Line Info");
10162 ASM_OUTPUT_LABEL (asm_out_file, l1);
10164 dw2_asm_output_data (2, ver, "DWARF Version");
10165 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10166 ASM_OUTPUT_LABEL (asm_out_file, p1);
10168 /* Define the architecture-dependent minimum instruction length (in bytes).
10169 In this implementation of DWARF, this field is used for information
10170 purposes only. Since GCC generates assembly language, we have no
10171 a priori knowledge of how many instruction bytes are generated for each
10172 source line, and therefore can use only the DW_LNE_set_address and
10173 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10174 this as '1', which is "correct enough" for all architectures,
10175 and don't let the target override. */
10176 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10178 if (ver >= 4)
10179 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10180 "Maximum Operations Per Instruction");
10181 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10182 "Default is_stmt_start flag");
10183 dw2_asm_output_data (1, DWARF_LINE_BASE,
10184 "Line Base Value (Special Opcodes)");
10185 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10186 "Line Range Value (Special Opcodes)");
10187 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10188 "Special Opcode Base");
10190 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10192 int n_op_args;
10193 switch (opc)
10195 case DW_LNS_advance_pc:
10196 case DW_LNS_advance_line:
10197 case DW_LNS_set_file:
10198 case DW_LNS_set_column:
10199 case DW_LNS_fixed_advance_pc:
10200 case DW_LNS_set_isa:
10201 n_op_args = 1;
10202 break;
10203 default:
10204 n_op_args = 0;
10205 break;
10208 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10209 opc, n_op_args);
10212 /* Write out the information about the files we use. */
10213 output_file_names ();
10214 ASM_OUTPUT_LABEL (asm_out_file, p2);
10215 if (prologue_only)
10217 /* Output the marker for the end of the line number info. */
10218 ASM_OUTPUT_LABEL (asm_out_file, l2);
10219 return;
10222 if (separate_line_info)
10224 dw_line_info_table *table;
10225 size_t i;
10227 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10228 if (table->in_use)
10230 output_one_line_info_table (table);
10231 saw_one = true;
10234 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10236 output_one_line_info_table (cold_text_section_line_info);
10237 saw_one = true;
10240 /* ??? Some Darwin linkers crash on a .debug_line section with no
10241 sequences. Further, merely a DW_LNE_end_sequence entry is not
10242 sufficient -- the address column must also be initialized.
10243 Make sure to output at least one set_address/end_sequence pair,
10244 choosing .text since that section is always present. */
10245 if (text_section_line_info->in_use || !saw_one)
10246 output_one_line_info_table (text_section_line_info);
10248 /* Output the marker for the end of the line number info. */
10249 ASM_OUTPUT_LABEL (asm_out_file, l2);
10252 /* Given a pointer to a tree node for some base type, return a pointer to
10253 a DIE that describes the given type.
10255 This routine must only be called for GCC type nodes that correspond to
10256 Dwarf base (fundamental) types. */
10258 static dw_die_ref
10259 base_type_die (tree type)
10261 dw_die_ref base_type_result;
10262 enum dwarf_type encoding;
10264 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10265 return 0;
10267 /* If this is a subtype that should not be emitted as a subrange type,
10268 use the base type. See subrange_type_for_debug_p. */
10269 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10270 type = TREE_TYPE (type);
10272 switch (TREE_CODE (type))
10274 case INTEGER_TYPE:
10275 if ((dwarf_version >= 4 || !dwarf_strict)
10276 && TYPE_NAME (type)
10277 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10278 && DECL_IS_BUILTIN (TYPE_NAME (type))
10279 && DECL_NAME (TYPE_NAME (type)))
10281 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10282 if (strcmp (name, "char16_t") == 0
10283 || strcmp (name, "char32_t") == 0)
10285 encoding = DW_ATE_UTF;
10286 break;
10289 if (TYPE_STRING_FLAG (type))
10291 if (TYPE_UNSIGNED (type))
10292 encoding = DW_ATE_unsigned_char;
10293 else
10294 encoding = DW_ATE_signed_char;
10296 else if (TYPE_UNSIGNED (type))
10297 encoding = DW_ATE_unsigned;
10298 else
10299 encoding = DW_ATE_signed;
10300 break;
10302 case REAL_TYPE:
10303 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10305 if (dwarf_version >= 3 || !dwarf_strict)
10306 encoding = DW_ATE_decimal_float;
10307 else
10308 encoding = DW_ATE_lo_user;
10310 else
10311 encoding = DW_ATE_float;
10312 break;
10314 case FIXED_POINT_TYPE:
10315 if (!(dwarf_version >= 3 || !dwarf_strict))
10316 encoding = DW_ATE_lo_user;
10317 else if (TYPE_UNSIGNED (type))
10318 encoding = DW_ATE_unsigned_fixed;
10319 else
10320 encoding = DW_ATE_signed_fixed;
10321 break;
10323 /* Dwarf2 doesn't know anything about complex ints, so use
10324 a user defined type for it. */
10325 case COMPLEX_TYPE:
10326 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10327 encoding = DW_ATE_complex_float;
10328 else
10329 encoding = DW_ATE_lo_user;
10330 break;
10332 case BOOLEAN_TYPE:
10333 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10334 encoding = DW_ATE_boolean;
10335 break;
10337 default:
10338 /* No other TREE_CODEs are Dwarf fundamental types. */
10339 gcc_unreachable ();
10342 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10344 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10345 int_size_in_bytes (type));
10346 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10347 add_pubtype (type, base_type_result);
10349 return base_type_result;
10352 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10353 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10355 static inline int
10356 is_base_type (tree type)
10358 switch (TREE_CODE (type))
10360 case ERROR_MARK:
10361 case VOID_TYPE:
10362 case INTEGER_TYPE:
10363 case REAL_TYPE:
10364 case FIXED_POINT_TYPE:
10365 case COMPLEX_TYPE:
10366 case BOOLEAN_TYPE:
10367 return 1;
10369 case ARRAY_TYPE:
10370 case RECORD_TYPE:
10371 case UNION_TYPE:
10372 case QUAL_UNION_TYPE:
10373 case ENUMERAL_TYPE:
10374 case FUNCTION_TYPE:
10375 case METHOD_TYPE:
10376 case POINTER_TYPE:
10377 case REFERENCE_TYPE:
10378 case NULLPTR_TYPE:
10379 case OFFSET_TYPE:
10380 case LANG_TYPE:
10381 case VECTOR_TYPE:
10382 return 0;
10384 default:
10385 gcc_unreachable ();
10388 return 0;
10391 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10392 node, return the size in bits for the type if it is a constant, or else
10393 return the alignment for the type if the type's size is not constant, or
10394 else return BITS_PER_WORD if the type actually turns out to be an
10395 ERROR_MARK node. */
10397 static inline unsigned HOST_WIDE_INT
10398 simple_type_size_in_bits (const_tree type)
10400 if (TREE_CODE (type) == ERROR_MARK)
10401 return BITS_PER_WORD;
10402 else if (TYPE_SIZE (type) == NULL_TREE)
10403 return 0;
10404 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10405 return tree_to_uhwi (TYPE_SIZE (type));
10406 else
10407 return TYPE_ALIGN (type);
10410 /* Similarly, but return an offset_int instead of UHWI. */
10412 static inline offset_int
10413 offset_int_type_size_in_bits (const_tree type)
10415 if (TREE_CODE (type) == ERROR_MARK)
10416 return BITS_PER_WORD;
10417 else if (TYPE_SIZE (type) == NULL_TREE)
10418 return 0;
10419 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10420 return wi::to_offset (TYPE_SIZE (type));
10421 else
10422 return TYPE_ALIGN (type);
10425 /* Given a pointer to a tree node for a subrange type, return a pointer
10426 to a DIE that describes the given type. */
10428 static dw_die_ref
10429 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10431 dw_die_ref subrange_die;
10432 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10434 if (context_die == NULL)
10435 context_die = comp_unit_die ();
10437 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10439 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10441 /* The size of the subrange type and its base type do not match,
10442 so we need to generate a size attribute for the subrange type. */
10443 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10446 if (low)
10447 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10448 if (high)
10449 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10451 return subrange_die;
10454 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10455 entry that chains various modifiers in front of the given type. */
10457 static dw_die_ref
10458 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10459 dw_die_ref context_die)
10461 enum tree_code code = TREE_CODE (type);
10462 dw_die_ref mod_type_die;
10463 dw_die_ref sub_die = NULL;
10464 tree item_type = NULL;
10465 tree qualified_type;
10466 tree name, low, high;
10467 dw_die_ref mod_scope;
10469 if (code == ERROR_MARK)
10470 return NULL;
10472 /* See if we already have the appropriately qualified variant of
10473 this type. */
10474 qualified_type
10475 = get_qualified_type (type,
10476 ((is_const_type ? TYPE_QUAL_CONST : 0)
10477 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10479 if (qualified_type == sizetype
10480 && TYPE_NAME (qualified_type)
10481 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10483 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10485 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10486 && TYPE_PRECISION (t)
10487 == TYPE_PRECISION (qualified_type)
10488 && TYPE_UNSIGNED (t)
10489 == TYPE_UNSIGNED (qualified_type));
10490 qualified_type = t;
10493 /* If we do, then we can just use its DIE, if it exists. */
10494 if (qualified_type)
10496 mod_type_die = lookup_type_die (qualified_type);
10497 if (mod_type_die)
10498 return mod_type_die;
10501 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10503 /* Handle C typedef types. */
10504 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10505 && !DECL_ARTIFICIAL (name))
10507 tree dtype = TREE_TYPE (name);
10509 if (qualified_type == dtype)
10511 /* For a named type, use the typedef. */
10512 gen_type_die (qualified_type, context_die);
10513 return lookup_type_die (qualified_type);
10515 else if (is_const_type < TYPE_READONLY (dtype)
10516 || is_volatile_type < TYPE_VOLATILE (dtype)
10517 || (is_const_type <= TYPE_READONLY (dtype)
10518 && is_volatile_type <= TYPE_VOLATILE (dtype)
10519 && DECL_ORIGINAL_TYPE (name) != type))
10520 /* cv-unqualified version of named type. Just use the unnamed
10521 type to which it refers. */
10522 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10523 is_const_type, is_volatile_type,
10524 context_die);
10525 /* Else cv-qualified version of named type; fall through. */
10528 mod_scope = scope_die_for (type, context_die);
10530 if (is_const_type
10531 /* If both is_const_type and is_volatile_type, prefer the path
10532 which leads to a qualified type. */
10533 && (!is_volatile_type
10534 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10535 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10537 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10538 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10540 else if (is_volatile_type)
10542 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10543 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10545 else if (code == POINTER_TYPE)
10547 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10548 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10549 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10550 item_type = TREE_TYPE (type);
10551 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10552 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10553 TYPE_ADDR_SPACE (item_type));
10555 else if (code == REFERENCE_TYPE)
10557 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10558 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10559 type);
10560 else
10561 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10562 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10563 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10564 item_type = TREE_TYPE (type);
10565 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10566 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10567 TYPE_ADDR_SPACE (item_type));
10569 else if (code == INTEGER_TYPE
10570 && TREE_TYPE (type) != NULL_TREE
10571 && subrange_type_for_debug_p (type, &low, &high))
10573 mod_type_die = subrange_type_die (type, low, high, context_die);
10574 item_type = TREE_TYPE (type);
10576 else if (is_base_type (type))
10577 mod_type_die = base_type_die (type);
10578 else
10580 gen_type_die (type, context_die);
10582 /* We have to get the type_main_variant here (and pass that to the
10583 `lookup_type_die' routine) because the ..._TYPE node we have
10584 might simply be a *copy* of some original type node (where the
10585 copy was created to help us keep track of typedef names) and
10586 that copy might have a different TYPE_UID from the original
10587 ..._TYPE node. */
10588 if (TREE_CODE (type) != VECTOR_TYPE)
10589 return lookup_type_die (type_main_variant (type));
10590 else
10591 /* Vectors have the debugging information in the type,
10592 not the main variant. */
10593 return lookup_type_die (type);
10596 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10597 don't output a DW_TAG_typedef, since there isn't one in the
10598 user's program; just attach a DW_AT_name to the type.
10599 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10600 if the base type already has the same name. */
10601 if (name
10602 && ((TREE_CODE (name) != TYPE_DECL
10603 && (qualified_type == TYPE_MAIN_VARIANT (type)
10604 || (!is_const_type && !is_volatile_type)))
10605 || (TREE_CODE (name) == TYPE_DECL
10606 && TREE_TYPE (name) == qualified_type
10607 && DECL_NAME (name))))
10609 if (TREE_CODE (name) == TYPE_DECL)
10610 /* Could just call add_name_and_src_coords_attributes here,
10611 but since this is a builtin type it doesn't have any
10612 useful source coordinates anyway. */
10613 name = DECL_NAME (name);
10614 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10616 /* This probably indicates a bug. */
10617 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10619 name = TYPE_NAME (type);
10620 if (name
10621 && TREE_CODE (name) == TYPE_DECL)
10622 name = DECL_NAME (name);
10623 add_name_attribute (mod_type_die,
10624 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10627 if (qualified_type)
10628 equate_type_number_to_die (qualified_type, mod_type_die);
10630 if (item_type)
10631 /* We must do this after the equate_type_number_to_die call, in case
10632 this is a recursive type. This ensures that the modified_type_die
10633 recursion will terminate even if the type is recursive. Recursive
10634 types are possible in Ada. */
10635 sub_die = modified_type_die (item_type,
10636 TYPE_READONLY (item_type),
10637 TYPE_VOLATILE (item_type),
10638 context_die);
10640 if (sub_die != NULL)
10641 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10643 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10644 if (TYPE_ARTIFICIAL (type))
10645 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10647 return mod_type_die;
10650 /* Generate DIEs for the generic parameters of T.
10651 T must be either a generic type or a generic function.
10652 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10654 static void
10655 gen_generic_params_dies (tree t)
10657 tree parms, args;
10658 int parms_num, i;
10659 dw_die_ref die = NULL;
10660 int non_default;
10662 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10663 return;
10665 if (TYPE_P (t))
10666 die = lookup_type_die (t);
10667 else if (DECL_P (t))
10668 die = lookup_decl_die (t);
10670 gcc_assert (die);
10672 parms = lang_hooks.get_innermost_generic_parms (t);
10673 if (!parms)
10674 /* T has no generic parameter. It means T is neither a generic type
10675 or function. End of story. */
10676 return;
10678 parms_num = TREE_VEC_LENGTH (parms);
10679 args = lang_hooks.get_innermost_generic_args (t);
10680 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10681 non_default = int_cst_value (TREE_CHAIN (args));
10682 else
10683 non_default = TREE_VEC_LENGTH (args);
10684 for (i = 0; i < parms_num; i++)
10686 tree parm, arg, arg_pack_elems;
10687 dw_die_ref parm_die;
10689 parm = TREE_VEC_ELT (parms, i);
10690 arg = TREE_VEC_ELT (args, i);
10691 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10692 gcc_assert (parm && TREE_VALUE (parm) && arg);
10694 if (parm && TREE_VALUE (parm) && arg)
10696 /* If PARM represents a template parameter pack,
10697 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10698 by DW_TAG_template_*_parameter DIEs for the argument
10699 pack elements of ARG. Note that ARG would then be
10700 an argument pack. */
10701 if (arg_pack_elems)
10702 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10703 arg_pack_elems,
10704 die);
10705 else
10706 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10707 true /* emit name */, die);
10708 if (i >= non_default)
10709 add_AT_flag (parm_die, DW_AT_default_value, 1);
10714 /* Create and return a DIE for PARM which should be
10715 the representation of a generic type parameter.
10716 For instance, in the C++ front end, PARM would be a template parameter.
10717 ARG is the argument to PARM.
10718 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10719 name of the PARM.
10720 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10721 as a child node. */
10723 static dw_die_ref
10724 generic_parameter_die (tree parm, tree arg,
10725 bool emit_name_p,
10726 dw_die_ref parent_die)
10728 dw_die_ref tmpl_die = NULL;
10729 const char *name = NULL;
10731 if (!parm || !DECL_NAME (parm) || !arg)
10732 return NULL;
10734 /* We support non-type generic parameters and arguments,
10735 type generic parameters and arguments, as well as
10736 generic generic parameters (a.k.a. template template parameters in C++)
10737 and arguments. */
10738 if (TREE_CODE (parm) == PARM_DECL)
10739 /* PARM is a nontype generic parameter */
10740 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10741 else if (TREE_CODE (parm) == TYPE_DECL)
10742 /* PARM is a type generic parameter. */
10743 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10744 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10745 /* PARM is a generic generic parameter.
10746 Its DIE is a GNU extension. It shall have a
10747 DW_AT_name attribute to represent the name of the template template
10748 parameter, and a DW_AT_GNU_template_name attribute to represent the
10749 name of the template template argument. */
10750 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10751 parent_die, parm);
10752 else
10753 gcc_unreachable ();
10755 if (tmpl_die)
10757 tree tmpl_type;
10759 /* If PARM is a generic parameter pack, it means we are
10760 emitting debug info for a template argument pack element.
10761 In other terms, ARG is a template argument pack element.
10762 In that case, we don't emit any DW_AT_name attribute for
10763 the die. */
10764 if (emit_name_p)
10766 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10767 gcc_assert (name);
10768 add_AT_string (tmpl_die, DW_AT_name, name);
10771 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10773 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10774 TMPL_DIE should have a child DW_AT_type attribute that is set
10775 to the type of the argument to PARM, which is ARG.
10776 If PARM is a type generic parameter, TMPL_DIE should have a
10777 child DW_AT_type that is set to ARG. */
10778 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10779 add_type_attribute (tmpl_die, tmpl_type, 0,
10780 TREE_THIS_VOLATILE (tmpl_type),
10781 parent_die);
10783 else
10785 /* So TMPL_DIE is a DIE representing a
10786 a generic generic template parameter, a.k.a template template
10787 parameter in C++ and arg is a template. */
10789 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10790 to the name of the argument. */
10791 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10792 if (name)
10793 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10796 if (TREE_CODE (parm) == PARM_DECL)
10797 /* So PARM is a non-type generic parameter.
10798 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10799 attribute of TMPL_DIE which value represents the value
10800 of ARG.
10801 We must be careful here:
10802 The value of ARG might reference some function decls.
10803 We might currently be emitting debug info for a generic
10804 type and types are emitted before function decls, we don't
10805 know if the function decls referenced by ARG will actually be
10806 emitted after cgraph computations.
10807 So must defer the generation of the DW_AT_const_value to
10808 after cgraph is ready. */
10809 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10812 return tmpl_die;
10815 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10816 PARM_PACK must be a template parameter pack. The returned DIE
10817 will be child DIE of PARENT_DIE. */
10819 static dw_die_ref
10820 template_parameter_pack_die (tree parm_pack,
10821 tree parm_pack_args,
10822 dw_die_ref parent_die)
10824 dw_die_ref die;
10825 int j;
10827 gcc_assert (parent_die && parm_pack);
10829 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10830 add_name_and_src_coords_attributes (die, parm_pack);
10831 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10832 generic_parameter_die (parm_pack,
10833 TREE_VEC_ELT (parm_pack_args, j),
10834 false /* Don't emit DW_AT_name */,
10835 die);
10836 return die;
10839 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10840 an enumerated type. */
10842 static inline int
10843 type_is_enum (const_tree type)
10845 return TREE_CODE (type) == ENUMERAL_TYPE;
10848 /* Return the DBX register number described by a given RTL node. */
10850 static unsigned int
10851 dbx_reg_number (const_rtx rtl)
10853 unsigned regno = REGNO (rtl);
10855 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10857 #ifdef LEAF_REG_REMAP
10858 if (crtl->uses_only_leaf_regs)
10860 int leaf_reg = LEAF_REG_REMAP (regno);
10861 if (leaf_reg != -1)
10862 regno = (unsigned) leaf_reg;
10864 #endif
10866 regno = DBX_REGISTER_NUMBER (regno);
10867 gcc_assert (regno != INVALID_REGNUM);
10868 return regno;
10871 /* Optionally add a DW_OP_piece term to a location description expression.
10872 DW_OP_piece is only added if the location description expression already
10873 doesn't end with DW_OP_piece. */
10875 static void
10876 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10878 dw_loc_descr_ref loc;
10880 if (*list_head != NULL)
10882 /* Find the end of the chain. */
10883 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10886 if (loc->dw_loc_opc != DW_OP_piece)
10887 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10891 /* Return a location descriptor that designates a machine register or
10892 zero if there is none. */
10894 static dw_loc_descr_ref
10895 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10897 rtx regs;
10899 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10900 return 0;
10902 /* We only use "frame base" when we're sure we're talking about the
10903 post-prologue local stack frame. We do this by *not* running
10904 register elimination until this point, and recognizing the special
10905 argument pointer and soft frame pointer rtx's.
10906 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10907 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10908 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10910 dw_loc_descr_ref result = NULL;
10912 if (dwarf_version >= 4 || !dwarf_strict)
10914 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10915 initialized);
10916 if (result)
10917 add_loc_descr (&result,
10918 new_loc_descr (DW_OP_stack_value, 0, 0));
10920 return result;
10923 regs = targetm.dwarf_register_span (rtl);
10925 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10926 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10927 else
10929 unsigned int dbx_regnum = dbx_reg_number (rtl);
10930 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10931 return 0;
10932 return one_reg_loc_descriptor (dbx_regnum, initialized);
10936 /* Return a location descriptor that designates a machine register for
10937 a given hard register number. */
10939 static dw_loc_descr_ref
10940 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10942 dw_loc_descr_ref reg_loc_descr;
10944 if (regno <= 31)
10945 reg_loc_descr
10946 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10947 else
10948 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10950 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10951 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10953 return reg_loc_descr;
10956 /* Given an RTL of a register, return a location descriptor that
10957 designates a value that spans more than one register. */
10959 static dw_loc_descr_ref
10960 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10961 enum var_init_status initialized)
10963 int size, i;
10964 dw_loc_descr_ref loc_result = NULL;
10966 /* Simple, contiguous registers. */
10967 if (regs == NULL_RTX)
10969 unsigned reg = REGNO (rtl);
10970 int nregs;
10972 #ifdef LEAF_REG_REMAP
10973 if (crtl->uses_only_leaf_regs)
10975 int leaf_reg = LEAF_REG_REMAP (reg);
10976 if (leaf_reg != -1)
10977 reg = (unsigned) leaf_reg;
10979 #endif
10981 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10982 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10984 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10986 loc_result = NULL;
10987 while (nregs--)
10989 dw_loc_descr_ref t;
10991 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10992 VAR_INIT_STATUS_INITIALIZED);
10993 add_loc_descr (&loc_result, t);
10994 add_loc_descr_op_piece (&loc_result, size);
10995 ++reg;
10997 return loc_result;
11000 /* Now onto stupid register sets in non contiguous locations. */
11002 gcc_assert (GET_CODE (regs) == PARALLEL);
11004 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11005 loc_result = NULL;
11007 for (i = 0; i < XVECLEN (regs, 0); ++i)
11009 dw_loc_descr_ref t;
11011 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11012 VAR_INIT_STATUS_INITIALIZED);
11013 add_loc_descr (&loc_result, t);
11014 add_loc_descr_op_piece (&loc_result, size);
11017 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11018 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11019 return loc_result;
11022 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11024 /* Return a location descriptor that designates a constant i,
11025 as a compound operation from constant (i >> shift), constant shift
11026 and DW_OP_shl. */
11028 static dw_loc_descr_ref
11029 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11031 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11032 add_loc_descr (&ret, int_loc_descriptor (shift));
11033 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11034 return ret;
11037 /* Return a location descriptor that designates a constant. */
11039 static dw_loc_descr_ref
11040 int_loc_descriptor (HOST_WIDE_INT i)
11042 enum dwarf_location_atom op;
11044 /* Pick the smallest representation of a constant, rather than just
11045 defaulting to the LEB encoding. */
11046 if (i >= 0)
11048 int clz = clz_hwi (i);
11049 int ctz = ctz_hwi (i);
11050 if (i <= 31)
11051 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11052 else if (i <= 0xff)
11053 op = DW_OP_const1u;
11054 else if (i <= 0xffff)
11055 op = DW_OP_const2u;
11056 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11057 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11058 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11059 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11060 while DW_OP_const4u is 5 bytes. */
11061 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11062 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11063 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11064 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11065 while DW_OP_const4u is 5 bytes. */
11066 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11067 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11068 op = DW_OP_const4u;
11069 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11070 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11071 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11072 while DW_OP_constu of constant >= 0x100000000 takes at least
11073 6 bytes. */
11074 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11075 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11076 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11077 >= HOST_BITS_PER_WIDE_INT)
11078 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11079 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11080 while DW_OP_constu takes in this case at least 6 bytes. */
11081 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11082 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11083 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11084 && size_of_uleb128 (i) > 6)
11085 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11086 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11087 else
11088 op = DW_OP_constu;
11090 else
11092 if (i >= -0x80)
11093 op = DW_OP_const1s;
11094 else if (i >= -0x8000)
11095 op = DW_OP_const2s;
11096 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11098 if (size_of_int_loc_descriptor (i) < 5)
11100 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11101 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11102 return ret;
11104 op = DW_OP_const4s;
11106 else
11108 if (size_of_int_loc_descriptor (i)
11109 < (unsigned long) 1 + size_of_sleb128 (i))
11111 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11112 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11113 return ret;
11115 op = DW_OP_consts;
11119 return new_loc_descr (op, i, 0);
11122 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11123 without actually allocating it. */
11125 static unsigned long
11126 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11128 return size_of_int_loc_descriptor (i >> shift)
11129 + size_of_int_loc_descriptor (shift)
11130 + 1;
11133 /* Return size_of_locs (int_loc_descriptor (i)) without
11134 actually allocating it. */
11136 static unsigned long
11137 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11139 unsigned long s;
11141 if (i >= 0)
11143 int clz, ctz;
11144 if (i <= 31)
11145 return 1;
11146 else if (i <= 0xff)
11147 return 2;
11148 else if (i <= 0xffff)
11149 return 3;
11150 clz = clz_hwi (i);
11151 ctz = ctz_hwi (i);
11152 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11153 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11154 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11155 - clz - 5);
11156 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11157 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11158 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11159 - clz - 8);
11160 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11161 return 5;
11162 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11163 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11164 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11165 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11166 - clz - 8);
11167 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11168 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11169 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11170 - clz - 16);
11171 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11172 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11173 && s > 6)
11174 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11175 - clz - 32);
11176 else
11177 return 1 + s;
11179 else
11181 if (i >= -0x80)
11182 return 2;
11183 else if (i >= -0x8000)
11184 return 3;
11185 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11187 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11189 s = size_of_int_loc_descriptor (-i) + 1;
11190 if (s < 5)
11191 return s;
11193 return 5;
11195 else
11197 unsigned long r = 1 + size_of_sleb128 (i);
11198 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11200 s = size_of_int_loc_descriptor (-i) + 1;
11201 if (s < r)
11202 return s;
11204 return r;
11209 /* Return loc description representing "address" of integer value.
11210 This can appear only as toplevel expression. */
11212 static dw_loc_descr_ref
11213 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11215 int litsize;
11216 dw_loc_descr_ref loc_result = NULL;
11218 if (!(dwarf_version >= 4 || !dwarf_strict))
11219 return NULL;
11221 litsize = size_of_int_loc_descriptor (i);
11222 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11223 is more compact. For DW_OP_stack_value we need:
11224 litsize + 1 (DW_OP_stack_value)
11225 and for DW_OP_implicit_value:
11226 1 (DW_OP_implicit_value) + 1 (length) + size. */
11227 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11229 loc_result = int_loc_descriptor (i);
11230 add_loc_descr (&loc_result,
11231 new_loc_descr (DW_OP_stack_value, 0, 0));
11232 return loc_result;
11235 loc_result = new_loc_descr (DW_OP_implicit_value,
11236 size, 0);
11237 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11238 loc_result->dw_loc_oprnd2.v.val_int = i;
11239 return loc_result;
11242 /* Return a location descriptor that designates a base+offset location. */
11244 static dw_loc_descr_ref
11245 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11246 enum var_init_status initialized)
11248 unsigned int regno;
11249 dw_loc_descr_ref result;
11250 dw_fde_ref fde = cfun->fde;
11252 /* We only use "frame base" when we're sure we're talking about the
11253 post-prologue local stack frame. We do this by *not* running
11254 register elimination until this point, and recognizing the special
11255 argument pointer and soft frame pointer rtx's. */
11256 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11258 rtx elim = (ira_use_lra_p
11259 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11260 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11262 if (elim != reg)
11264 if (GET_CODE (elim) == PLUS)
11266 offset += INTVAL (XEXP (elim, 1));
11267 elim = XEXP (elim, 0);
11269 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11270 && (elim == hard_frame_pointer_rtx
11271 || elim == stack_pointer_rtx))
11272 || elim == (frame_pointer_needed
11273 ? hard_frame_pointer_rtx
11274 : stack_pointer_rtx));
11276 /* If drap register is used to align stack, use frame
11277 pointer + offset to access stack variables. If stack
11278 is aligned without drap, use stack pointer + offset to
11279 access stack variables. */
11280 if (crtl->stack_realign_tried
11281 && reg == frame_pointer_rtx)
11283 int base_reg
11284 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11285 ? HARD_FRAME_POINTER_REGNUM
11286 : REGNO (elim));
11287 return new_reg_loc_descr (base_reg, offset);
11290 gcc_assert (frame_pointer_fb_offset_valid);
11291 offset += frame_pointer_fb_offset;
11292 return new_loc_descr (DW_OP_fbreg, offset, 0);
11296 regno = REGNO (reg);
11297 #ifdef LEAF_REG_REMAP
11298 if (crtl->uses_only_leaf_regs)
11300 int leaf_reg = LEAF_REG_REMAP (regno);
11301 if (leaf_reg != -1)
11302 regno = (unsigned) leaf_reg;
11304 #endif
11305 regno = DWARF_FRAME_REGNUM (regno);
11307 if (!optimize && fde
11308 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11310 /* Use cfa+offset to represent the location of arguments passed
11311 on the stack when drap is used to align stack.
11312 Only do this when not optimizing, for optimized code var-tracking
11313 is supposed to track where the arguments live and the register
11314 used as vdrap or drap in some spot might be used for something
11315 else in other part of the routine. */
11316 return new_loc_descr (DW_OP_fbreg, offset, 0);
11319 if (regno <= 31)
11320 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11321 offset, 0);
11322 else
11323 result = new_loc_descr (DW_OP_bregx, regno, offset);
11325 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11326 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11328 return result;
11331 /* Return true if this RTL expression describes a base+offset calculation. */
11333 static inline int
11334 is_based_loc (const_rtx rtl)
11336 return (GET_CODE (rtl) == PLUS
11337 && ((REG_P (XEXP (rtl, 0))
11338 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11339 && CONST_INT_P (XEXP (rtl, 1)))));
11342 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11343 failed. */
11345 static dw_loc_descr_ref
11346 tls_mem_loc_descriptor (rtx mem)
11348 tree base;
11349 dw_loc_descr_ref loc_result;
11351 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11352 return NULL;
11354 base = get_base_address (MEM_EXPR (mem));
11355 if (base == NULL
11356 || TREE_CODE (base) != VAR_DECL
11357 || !DECL_THREAD_LOCAL_P (base))
11358 return NULL;
11360 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11361 if (loc_result == NULL)
11362 return NULL;
11364 if (MEM_OFFSET (mem))
11365 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11367 return loc_result;
11370 /* Output debug info about reason why we failed to expand expression as dwarf
11371 expression. */
11373 static void
11374 expansion_failed (tree expr, rtx rtl, char const *reason)
11376 if (dump_file && (dump_flags & TDF_DETAILS))
11378 fprintf (dump_file, "Failed to expand as dwarf: ");
11379 if (expr)
11380 print_generic_expr (dump_file, expr, dump_flags);
11381 if (rtl)
11383 fprintf (dump_file, "\n");
11384 print_rtl (dump_file, rtl);
11386 fprintf (dump_file, "\nReason: %s\n", reason);
11390 /* Helper function for const_ok_for_output, called either directly
11391 or via for_each_rtx. */
11393 static int
11394 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11396 rtx rtl = *rtlp;
11398 if (GET_CODE (rtl) == UNSPEC)
11400 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11401 we can't express it in the debug info. */
11402 #ifdef ENABLE_CHECKING
11403 /* Don't complain about TLS UNSPECs, those are just too hard to
11404 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11405 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11406 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11407 if (XVECLEN (rtl, 0) == 0
11408 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11409 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11410 inform (current_function_decl
11411 ? DECL_SOURCE_LOCATION (current_function_decl)
11412 : UNKNOWN_LOCATION,
11413 #if NUM_UNSPEC_VALUES > 0
11414 "non-delegitimized UNSPEC %s (%d) found in variable location",
11415 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11416 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11417 XINT (rtl, 1));
11418 #else
11419 "non-delegitimized UNSPEC %d found in variable location",
11420 XINT (rtl, 1));
11421 #endif
11422 #endif
11423 expansion_failed (NULL_TREE, rtl,
11424 "UNSPEC hasn't been delegitimized.\n");
11425 return 1;
11428 if (targetm.const_not_ok_for_debug_p (rtl))
11430 expansion_failed (NULL_TREE, rtl,
11431 "Expression rejected for debug by the backend.\n");
11432 return 1;
11435 if (GET_CODE (rtl) != SYMBOL_REF)
11436 return 0;
11438 if (CONSTANT_POOL_ADDRESS_P (rtl))
11440 bool marked;
11441 get_pool_constant_mark (rtl, &marked);
11442 /* If all references to this pool constant were optimized away,
11443 it was not output and thus we can't represent it. */
11444 if (!marked)
11446 expansion_failed (NULL_TREE, rtl,
11447 "Constant was removed from constant pool.\n");
11448 return 1;
11452 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11453 return 1;
11455 /* Avoid references to external symbols in debug info, on several targets
11456 the linker might even refuse to link when linking a shared library,
11457 and in many other cases the relocations for .debug_info/.debug_loc are
11458 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11459 to be defined within the same shared library or executable are fine. */
11460 if (SYMBOL_REF_EXTERNAL_P (rtl))
11462 tree decl = SYMBOL_REF_DECL (rtl);
11464 if (decl == NULL || !targetm.binds_local_p (decl))
11466 expansion_failed (NULL_TREE, rtl,
11467 "Symbol not defined in current TU.\n");
11468 return 1;
11472 return 0;
11475 /* Return true if constant RTL can be emitted in DW_OP_addr or
11476 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11477 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11479 static bool
11480 const_ok_for_output (rtx rtl)
11482 if (GET_CODE (rtl) == SYMBOL_REF)
11483 return const_ok_for_output_1 (&rtl, NULL) == 0;
11485 if (GET_CODE (rtl) == CONST)
11486 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11488 return true;
11491 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11492 if possible, NULL otherwise. */
11494 static dw_die_ref
11495 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11497 dw_die_ref type_die;
11498 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11500 if (type == NULL)
11501 return NULL;
11502 switch (TREE_CODE (type))
11504 case INTEGER_TYPE:
11505 case REAL_TYPE:
11506 break;
11507 default:
11508 return NULL;
11510 type_die = lookup_type_die (type);
11511 if (!type_die)
11512 type_die = modified_type_die (type, false, false, comp_unit_die ());
11513 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11514 return NULL;
11515 return type_die;
11518 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11519 type matching MODE, or, if MODE is narrower than or as wide as
11520 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11521 possible. */
11523 static dw_loc_descr_ref
11524 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11526 enum machine_mode outer_mode = mode;
11527 dw_die_ref type_die;
11528 dw_loc_descr_ref cvt;
11530 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11532 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11533 return op;
11535 type_die = base_type_for_mode (outer_mode, 1);
11536 if (type_die == NULL)
11537 return NULL;
11538 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11539 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11540 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11541 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11542 add_loc_descr (&op, cvt);
11543 return op;
11546 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11548 static dw_loc_descr_ref
11549 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11550 dw_loc_descr_ref op1)
11552 dw_loc_descr_ref ret = op0;
11553 add_loc_descr (&ret, op1);
11554 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11555 if (STORE_FLAG_VALUE != 1)
11557 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11558 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11560 return ret;
11563 /* Return location descriptor for signed comparison OP RTL. */
11565 static dw_loc_descr_ref
11566 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11567 enum machine_mode mem_mode)
11569 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11570 dw_loc_descr_ref op0, op1;
11571 int shift;
11573 if (op_mode == VOIDmode)
11574 op_mode = GET_MODE (XEXP (rtl, 1));
11575 if (op_mode == VOIDmode)
11576 return NULL;
11578 if (dwarf_strict
11579 && (GET_MODE_CLASS (op_mode) != MODE_INT
11580 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11581 return NULL;
11583 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11584 VAR_INIT_STATUS_INITIALIZED);
11585 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11586 VAR_INIT_STATUS_INITIALIZED);
11588 if (op0 == NULL || op1 == NULL)
11589 return NULL;
11591 if (GET_MODE_CLASS (op_mode) != MODE_INT
11592 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11593 return compare_loc_descriptor (op, op0, op1);
11595 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11597 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11598 dw_loc_descr_ref cvt;
11600 if (type_die == NULL)
11601 return NULL;
11602 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11603 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11604 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11605 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11606 add_loc_descr (&op0, cvt);
11607 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11608 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11609 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11610 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11611 add_loc_descr (&op1, cvt);
11612 return compare_loc_descriptor (op, op0, op1);
11615 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11616 /* For eq/ne, if the operands are known to be zero-extended,
11617 there is no need to do the fancy shifting up. */
11618 if (op == DW_OP_eq || op == DW_OP_ne)
11620 dw_loc_descr_ref last0, last1;
11621 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11623 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11625 /* deref_size zero extends, and for constants we can check
11626 whether they are zero extended or not. */
11627 if (((last0->dw_loc_opc == DW_OP_deref_size
11628 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11629 || (CONST_INT_P (XEXP (rtl, 0))
11630 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11631 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11632 && ((last1->dw_loc_opc == DW_OP_deref_size
11633 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11634 || (CONST_INT_P (XEXP (rtl, 1))
11635 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11636 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11637 return compare_loc_descriptor (op, op0, op1);
11639 /* EQ/NE comparison against constant in narrower type than
11640 DWARF2_ADDR_SIZE can be performed either as
11641 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11642 DW_OP_{eq,ne}
11644 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11645 DW_OP_{eq,ne}. Pick whatever is shorter. */
11646 if (CONST_INT_P (XEXP (rtl, 1))
11647 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11648 && (size_of_int_loc_descriptor (shift) + 1
11649 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11650 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11651 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11652 & GET_MODE_MASK (op_mode))))
11654 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11655 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11656 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11657 & GET_MODE_MASK (op_mode));
11658 return compare_loc_descriptor (op, op0, op1);
11661 add_loc_descr (&op0, int_loc_descriptor (shift));
11662 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11663 if (CONST_INT_P (XEXP (rtl, 1)))
11664 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11665 else
11667 add_loc_descr (&op1, int_loc_descriptor (shift));
11668 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11670 return compare_loc_descriptor (op, op0, op1);
11673 /* Return location descriptor for unsigned comparison OP RTL. */
11675 static dw_loc_descr_ref
11676 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11677 enum machine_mode mem_mode)
11679 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11680 dw_loc_descr_ref op0, op1;
11682 if (op_mode == VOIDmode)
11683 op_mode = GET_MODE (XEXP (rtl, 1));
11684 if (op_mode == VOIDmode)
11685 return NULL;
11686 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11687 return NULL;
11689 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11690 return NULL;
11692 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11693 VAR_INIT_STATUS_INITIALIZED);
11694 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11695 VAR_INIT_STATUS_INITIALIZED);
11697 if (op0 == NULL || op1 == NULL)
11698 return NULL;
11700 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11702 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11703 dw_loc_descr_ref last0, last1;
11704 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11706 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11708 if (CONST_INT_P (XEXP (rtl, 0)))
11709 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11710 /* deref_size zero extends, so no need to mask it again. */
11711 else if (last0->dw_loc_opc != DW_OP_deref_size
11712 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11714 add_loc_descr (&op0, int_loc_descriptor (mask));
11715 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11717 if (CONST_INT_P (XEXP (rtl, 1)))
11718 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11719 /* deref_size zero extends, so no need to mask it again. */
11720 else if (last1->dw_loc_opc != DW_OP_deref_size
11721 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11723 add_loc_descr (&op1, int_loc_descriptor (mask));
11724 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11727 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11729 HOST_WIDE_INT bias = 1;
11730 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11731 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11732 if (CONST_INT_P (XEXP (rtl, 1)))
11733 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11734 + INTVAL (XEXP (rtl, 1)));
11735 else
11736 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11737 bias, 0));
11739 return compare_loc_descriptor (op, op0, op1);
11742 /* Return location descriptor for {U,S}{MIN,MAX}. */
11744 static dw_loc_descr_ref
11745 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11746 enum machine_mode mem_mode)
11748 enum dwarf_location_atom op;
11749 dw_loc_descr_ref op0, op1, ret;
11750 dw_loc_descr_ref bra_node, drop_node;
11752 if (dwarf_strict
11753 && (GET_MODE_CLASS (mode) != MODE_INT
11754 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11755 return NULL;
11757 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11758 VAR_INIT_STATUS_INITIALIZED);
11759 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11760 VAR_INIT_STATUS_INITIALIZED);
11762 if (op0 == NULL || op1 == NULL)
11763 return NULL;
11765 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11766 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11767 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11768 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11770 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11772 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11773 add_loc_descr (&op0, int_loc_descriptor (mask));
11774 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11775 add_loc_descr (&op1, int_loc_descriptor (mask));
11776 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11778 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11780 HOST_WIDE_INT bias = 1;
11781 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11782 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11783 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11786 else if (GET_MODE_CLASS (mode) == MODE_INT
11787 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11789 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11790 add_loc_descr (&op0, int_loc_descriptor (shift));
11791 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11792 add_loc_descr (&op1, int_loc_descriptor (shift));
11793 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11795 else if (GET_MODE_CLASS (mode) == MODE_INT
11796 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11798 dw_die_ref type_die = base_type_for_mode (mode, 0);
11799 dw_loc_descr_ref cvt;
11800 if (type_die == NULL)
11801 return NULL;
11802 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11803 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11804 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11805 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11806 add_loc_descr (&op0, cvt);
11807 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11808 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11809 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11810 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11811 add_loc_descr (&op1, cvt);
11814 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11815 op = DW_OP_lt;
11816 else
11817 op = DW_OP_gt;
11818 ret = op0;
11819 add_loc_descr (&ret, op1);
11820 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11821 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11822 add_loc_descr (&ret, bra_node);
11823 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11824 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11825 add_loc_descr (&ret, drop_node);
11826 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11827 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11828 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11829 && GET_MODE_CLASS (mode) == MODE_INT
11830 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11831 ret = convert_descriptor_to_mode (mode, ret);
11832 return ret;
11835 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11836 but after converting arguments to type_die, afterwards
11837 convert back to unsigned. */
11839 static dw_loc_descr_ref
11840 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11841 enum machine_mode mode, enum machine_mode mem_mode)
11843 dw_loc_descr_ref cvt, op0, op1;
11845 if (type_die == NULL)
11846 return NULL;
11847 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11848 VAR_INIT_STATUS_INITIALIZED);
11849 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11850 VAR_INIT_STATUS_INITIALIZED);
11851 if (op0 == NULL || op1 == NULL)
11852 return NULL;
11853 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11854 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11855 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11856 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11857 add_loc_descr (&op0, cvt);
11858 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11859 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11860 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11861 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11862 add_loc_descr (&op1, cvt);
11863 add_loc_descr (&op0, op1);
11864 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11865 return convert_descriptor_to_mode (mode, op0);
11868 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11869 const0 is DW_OP_lit0 or corresponding typed constant,
11870 const1 is DW_OP_lit1 or corresponding typed constant
11871 and constMSB is constant with just the MSB bit set
11872 for the mode):
11873 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11874 L1: const0 DW_OP_swap
11875 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11876 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11877 L3: DW_OP_drop
11878 L4: DW_OP_nop
11880 CTZ is similar:
11881 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11882 L1: const0 DW_OP_swap
11883 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11884 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11885 L3: DW_OP_drop
11886 L4: DW_OP_nop
11888 FFS is similar:
11889 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11890 L1: const1 DW_OP_swap
11891 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11892 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11893 L3: DW_OP_drop
11894 L4: DW_OP_nop */
11896 static dw_loc_descr_ref
11897 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11898 enum machine_mode mem_mode)
11900 dw_loc_descr_ref op0, ret, tmp;
11901 HOST_WIDE_INT valv;
11902 dw_loc_descr_ref l1jump, l1label;
11903 dw_loc_descr_ref l2jump, l2label;
11904 dw_loc_descr_ref l3jump, l3label;
11905 dw_loc_descr_ref l4jump, l4label;
11906 rtx msb;
11908 if (GET_MODE_CLASS (mode) != MODE_INT
11909 || GET_MODE (XEXP (rtl, 0)) != mode)
11910 return NULL;
11912 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11913 VAR_INIT_STATUS_INITIALIZED);
11914 if (op0 == NULL)
11915 return NULL;
11916 ret = op0;
11917 if (GET_CODE (rtl) == CLZ)
11919 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11920 valv = GET_MODE_BITSIZE (mode);
11922 else if (GET_CODE (rtl) == FFS)
11923 valv = 0;
11924 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11925 valv = GET_MODE_BITSIZE (mode);
11926 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11927 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11928 add_loc_descr (&ret, l1jump);
11929 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11930 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11931 VAR_INIT_STATUS_INITIALIZED);
11932 if (tmp == NULL)
11933 return NULL;
11934 add_loc_descr (&ret, tmp);
11935 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11936 add_loc_descr (&ret, l4jump);
11937 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11938 ? const1_rtx : const0_rtx,
11939 mode, mem_mode,
11940 VAR_INIT_STATUS_INITIALIZED);
11941 if (l1label == NULL)
11942 return NULL;
11943 add_loc_descr (&ret, l1label);
11944 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11945 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11946 add_loc_descr (&ret, l2label);
11947 if (GET_CODE (rtl) != CLZ)
11948 msb = const1_rtx;
11949 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11950 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11951 << (GET_MODE_BITSIZE (mode) - 1));
11952 else
11953 msb = immed_wide_int_const
11954 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
11955 GET_MODE_PRECISION (mode)), mode);
11956 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11957 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11958 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11959 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11960 else
11961 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11962 VAR_INIT_STATUS_INITIALIZED);
11963 if (tmp == NULL)
11964 return NULL;
11965 add_loc_descr (&ret, tmp);
11966 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11967 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11968 add_loc_descr (&ret, l3jump);
11969 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11970 VAR_INIT_STATUS_INITIALIZED);
11971 if (tmp == NULL)
11972 return NULL;
11973 add_loc_descr (&ret, tmp);
11974 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11975 ? DW_OP_shl : DW_OP_shr, 0, 0));
11976 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11977 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11978 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11979 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11980 add_loc_descr (&ret, l2jump);
11981 l3label = new_loc_descr (DW_OP_drop, 0, 0);
11982 add_loc_descr (&ret, l3label);
11983 l4label = new_loc_descr (DW_OP_nop, 0, 0);
11984 add_loc_descr (&ret, l4label);
11985 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11986 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11987 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11988 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11989 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11990 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11991 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11992 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11993 return ret;
11996 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11997 const1 is DW_OP_lit1 or corresponding typed constant):
11998 const0 DW_OP_swap
11999 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12000 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12001 L2: DW_OP_drop
12003 PARITY is similar:
12004 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12005 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12006 L2: DW_OP_drop */
12008 static dw_loc_descr_ref
12009 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
12010 enum machine_mode mem_mode)
12012 dw_loc_descr_ref op0, ret, tmp;
12013 dw_loc_descr_ref l1jump, l1label;
12014 dw_loc_descr_ref l2jump, l2label;
12016 if (GET_MODE_CLASS (mode) != MODE_INT
12017 || GET_MODE (XEXP (rtl, 0)) != mode)
12018 return NULL;
12020 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12021 VAR_INIT_STATUS_INITIALIZED);
12022 if (op0 == NULL)
12023 return NULL;
12024 ret = op0;
12025 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12026 VAR_INIT_STATUS_INITIALIZED);
12027 if (tmp == NULL)
12028 return NULL;
12029 add_loc_descr (&ret, tmp);
12030 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12031 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12032 add_loc_descr (&ret, l1label);
12033 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12034 add_loc_descr (&ret, l2jump);
12035 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12036 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12037 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12038 VAR_INIT_STATUS_INITIALIZED);
12039 if (tmp == NULL)
12040 return NULL;
12041 add_loc_descr (&ret, tmp);
12042 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12043 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12044 ? DW_OP_plus : DW_OP_xor, 0, 0));
12045 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12046 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12047 VAR_INIT_STATUS_INITIALIZED);
12048 add_loc_descr (&ret, tmp);
12049 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12050 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12051 add_loc_descr (&ret, l1jump);
12052 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12053 add_loc_descr (&ret, l2label);
12054 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12055 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12056 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12057 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12058 return ret;
12061 /* BSWAP (constS is initial shift count, either 56 or 24):
12062 constS const0
12063 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12064 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12065 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12066 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12067 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12069 static dw_loc_descr_ref
12070 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
12071 enum machine_mode mem_mode)
12073 dw_loc_descr_ref op0, ret, tmp;
12074 dw_loc_descr_ref l1jump, l1label;
12075 dw_loc_descr_ref l2jump, l2label;
12077 if (GET_MODE_CLASS (mode) != MODE_INT
12078 || BITS_PER_UNIT != 8
12079 || (GET_MODE_BITSIZE (mode) != 32
12080 && GET_MODE_BITSIZE (mode) != 64))
12081 return NULL;
12083 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12084 VAR_INIT_STATUS_INITIALIZED);
12085 if (op0 == NULL)
12086 return NULL;
12088 ret = op0;
12089 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12090 mode, mem_mode,
12091 VAR_INIT_STATUS_INITIALIZED);
12092 if (tmp == NULL)
12093 return NULL;
12094 add_loc_descr (&ret, tmp);
12095 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12096 VAR_INIT_STATUS_INITIALIZED);
12097 if (tmp == NULL)
12098 return NULL;
12099 add_loc_descr (&ret, tmp);
12100 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12101 add_loc_descr (&ret, l1label);
12102 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12103 mode, mem_mode,
12104 VAR_INIT_STATUS_INITIALIZED);
12105 add_loc_descr (&ret, tmp);
12106 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12107 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12108 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12109 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12110 VAR_INIT_STATUS_INITIALIZED);
12111 if (tmp == NULL)
12112 return NULL;
12113 add_loc_descr (&ret, tmp);
12114 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12115 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12116 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12117 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12118 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12119 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12120 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12121 VAR_INIT_STATUS_INITIALIZED);
12122 add_loc_descr (&ret, tmp);
12123 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12124 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12125 add_loc_descr (&ret, l2jump);
12126 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12127 VAR_INIT_STATUS_INITIALIZED);
12128 add_loc_descr (&ret, tmp);
12129 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12130 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12131 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12132 add_loc_descr (&ret, l1jump);
12133 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12134 add_loc_descr (&ret, l2label);
12135 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12136 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12137 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12138 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12139 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12140 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12141 return ret;
12144 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12145 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12146 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12147 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12149 ROTATERT is similar:
12150 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12151 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12152 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12154 static dw_loc_descr_ref
12155 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12156 enum machine_mode mem_mode)
12158 rtx rtlop1 = XEXP (rtl, 1);
12159 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12160 int i;
12162 if (GET_MODE_CLASS (mode) != MODE_INT)
12163 return NULL;
12165 if (GET_MODE (rtlop1) != VOIDmode
12166 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12167 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12168 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12169 VAR_INIT_STATUS_INITIALIZED);
12170 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12171 VAR_INIT_STATUS_INITIALIZED);
12172 if (op0 == NULL || op1 == NULL)
12173 return NULL;
12174 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12175 for (i = 0; i < 2; i++)
12177 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12178 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12179 mode, mem_mode,
12180 VAR_INIT_STATUS_INITIALIZED);
12181 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12182 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12183 ? DW_OP_const4u
12184 : HOST_BITS_PER_WIDE_INT == 64
12185 ? DW_OP_const8u : DW_OP_constu,
12186 GET_MODE_MASK (mode), 0);
12187 else
12188 mask[i] = NULL;
12189 if (mask[i] == NULL)
12190 return NULL;
12191 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12193 ret = op0;
12194 add_loc_descr (&ret, op1);
12195 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12196 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12197 if (GET_CODE (rtl) == ROTATERT)
12199 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12200 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12201 GET_MODE_BITSIZE (mode), 0));
12203 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12204 if (mask[0] != NULL)
12205 add_loc_descr (&ret, mask[0]);
12206 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12207 if (mask[1] != NULL)
12209 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12210 add_loc_descr (&ret, mask[1]);
12211 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12213 if (GET_CODE (rtl) == ROTATE)
12215 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12216 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12217 GET_MODE_BITSIZE (mode), 0));
12219 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12220 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12221 return ret;
12224 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12225 for DEBUG_PARAMETER_REF RTL. */
12227 static dw_loc_descr_ref
12228 parameter_ref_descriptor (rtx rtl)
12230 dw_loc_descr_ref ret;
12231 dw_die_ref ref;
12233 if (dwarf_strict)
12234 return NULL;
12235 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12236 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12237 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12238 if (ref)
12240 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12241 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12242 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12244 else
12246 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12247 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12249 return ret;
12252 /* The following routine converts the RTL for a variable or parameter
12253 (resident in memory) into an equivalent Dwarf representation of a
12254 mechanism for getting the address of that same variable onto the top of a
12255 hypothetical "address evaluation" stack.
12257 When creating memory location descriptors, we are effectively transforming
12258 the RTL for a memory-resident object into its Dwarf postfix expression
12259 equivalent. This routine recursively descends an RTL tree, turning
12260 it into Dwarf postfix code as it goes.
12262 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12264 MEM_MODE is the mode of the memory reference, needed to handle some
12265 autoincrement addressing modes.
12267 Return 0 if we can't represent the location. */
12269 dw_loc_descr_ref
12270 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12271 enum machine_mode mem_mode,
12272 enum var_init_status initialized)
12274 dw_loc_descr_ref mem_loc_result = NULL;
12275 enum dwarf_location_atom op;
12276 dw_loc_descr_ref op0, op1;
12277 rtx inner = NULL_RTX;
12279 if (mode == VOIDmode)
12280 mode = GET_MODE (rtl);
12282 /* Note that for a dynamically sized array, the location we will generate a
12283 description of here will be the lowest numbered location which is
12284 actually within the array. That's *not* necessarily the same as the
12285 zeroth element of the array. */
12287 rtl = targetm.delegitimize_address (rtl);
12289 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12290 return NULL;
12292 switch (GET_CODE (rtl))
12294 case POST_INC:
12295 case POST_DEC:
12296 case POST_MODIFY:
12297 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12299 case SUBREG:
12300 /* The case of a subreg may arise when we have a local (register)
12301 variable or a formal (register) parameter which doesn't quite fill
12302 up an entire register. For now, just assume that it is
12303 legitimate to make the Dwarf info refer to the whole register which
12304 contains the given subreg. */
12305 if (!subreg_lowpart_p (rtl))
12306 break;
12307 inner = SUBREG_REG (rtl);
12308 case TRUNCATE:
12309 if (inner == NULL_RTX)
12310 inner = XEXP (rtl, 0);
12311 if (GET_MODE_CLASS (mode) == MODE_INT
12312 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12313 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12314 #ifdef POINTERS_EXTEND_UNSIGNED
12315 || (mode == Pmode && mem_mode != VOIDmode)
12316 #endif
12318 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12320 mem_loc_result = mem_loc_descriptor (inner,
12321 GET_MODE (inner),
12322 mem_mode, initialized);
12323 break;
12325 if (dwarf_strict)
12326 break;
12327 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12328 break;
12329 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12330 && (GET_MODE_CLASS (mode) != MODE_INT
12331 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12332 break;
12333 else
12335 dw_die_ref type_die;
12336 dw_loc_descr_ref cvt;
12338 mem_loc_result = mem_loc_descriptor (inner,
12339 GET_MODE (inner),
12340 mem_mode, initialized);
12341 if (mem_loc_result == NULL)
12342 break;
12343 type_die = base_type_for_mode (mode,
12344 GET_MODE_CLASS (mode) == MODE_INT);
12345 if (type_die == NULL)
12347 mem_loc_result = NULL;
12348 break;
12350 if (GET_MODE_SIZE (mode)
12351 != GET_MODE_SIZE (GET_MODE (inner)))
12352 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12353 else
12354 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12355 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12356 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12357 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12358 add_loc_descr (&mem_loc_result, cvt);
12360 break;
12362 case REG:
12363 if (GET_MODE_CLASS (mode) != MODE_INT
12364 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12365 && rtl != arg_pointer_rtx
12366 && rtl != frame_pointer_rtx
12367 #ifdef POINTERS_EXTEND_UNSIGNED
12368 && (mode != Pmode || mem_mode == VOIDmode)
12369 #endif
12372 dw_die_ref type_die;
12373 unsigned int dbx_regnum;
12375 if (dwarf_strict)
12376 break;
12377 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12378 break;
12379 type_die = base_type_for_mode (mode,
12380 GET_MODE_CLASS (mode) == MODE_INT);
12381 if (type_die == NULL)
12382 break;
12384 dbx_regnum = dbx_reg_number (rtl);
12385 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12386 break;
12387 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12388 dbx_regnum, 0);
12389 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12390 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12391 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12392 break;
12394 /* Whenever a register number forms a part of the description of the
12395 method for calculating the (dynamic) address of a memory resident
12396 object, DWARF rules require the register number be referred to as
12397 a "base register". This distinction is not based in any way upon
12398 what category of register the hardware believes the given register
12399 belongs to. This is strictly DWARF terminology we're dealing with
12400 here. Note that in cases where the location of a memory-resident
12401 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12402 OP_CONST (0)) the actual DWARF location descriptor that we generate
12403 may just be OP_BASEREG (basereg). This may look deceptively like
12404 the object in question was allocated to a register (rather than in
12405 memory) so DWARF consumers need to be aware of the subtle
12406 distinction between OP_REG and OP_BASEREG. */
12407 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12408 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12409 else if (stack_realign_drap
12410 && crtl->drap_reg
12411 && crtl->args.internal_arg_pointer == rtl
12412 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12414 /* If RTL is internal_arg_pointer, which has been optimized
12415 out, use DRAP instead. */
12416 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12417 VAR_INIT_STATUS_INITIALIZED);
12419 break;
12421 case SIGN_EXTEND:
12422 case ZERO_EXTEND:
12423 if (GET_MODE_CLASS (mode) != MODE_INT)
12424 break;
12425 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12426 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12427 if (op0 == 0)
12428 break;
12429 else if (GET_CODE (rtl) == ZERO_EXTEND
12430 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12431 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12432 < HOST_BITS_PER_WIDE_INT
12433 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12434 to expand zero extend as two shifts instead of
12435 masking. */
12436 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12438 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12439 mem_loc_result = op0;
12440 add_loc_descr (&mem_loc_result,
12441 int_loc_descriptor (GET_MODE_MASK (imode)));
12442 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12444 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12446 int shift = DWARF2_ADDR_SIZE
12447 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12448 shift *= BITS_PER_UNIT;
12449 if (GET_CODE (rtl) == SIGN_EXTEND)
12450 op = DW_OP_shra;
12451 else
12452 op = DW_OP_shr;
12453 mem_loc_result = op0;
12454 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12455 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12456 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12457 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12459 else if (!dwarf_strict)
12461 dw_die_ref type_die1, type_die2;
12462 dw_loc_descr_ref cvt;
12464 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12465 GET_CODE (rtl) == ZERO_EXTEND);
12466 if (type_die1 == NULL)
12467 break;
12468 type_die2 = base_type_for_mode (mode, 1);
12469 if (type_die2 == NULL)
12470 break;
12471 mem_loc_result = op0;
12472 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12473 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12474 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12475 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12476 add_loc_descr (&mem_loc_result, cvt);
12477 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12478 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12479 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12480 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12481 add_loc_descr (&mem_loc_result, cvt);
12483 break;
12485 case MEM:
12487 rtx new_rtl = avoid_constant_pool_reference (rtl);
12488 if (new_rtl != rtl)
12490 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12491 initialized);
12492 if (mem_loc_result != NULL)
12493 return mem_loc_result;
12496 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12497 get_address_mode (rtl), mode,
12498 VAR_INIT_STATUS_INITIALIZED);
12499 if (mem_loc_result == NULL)
12500 mem_loc_result = tls_mem_loc_descriptor (rtl);
12501 if (mem_loc_result != NULL)
12503 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12504 || GET_MODE_CLASS (mode) != MODE_INT)
12506 dw_die_ref type_die;
12507 dw_loc_descr_ref deref;
12509 if (dwarf_strict)
12510 return NULL;
12511 type_die
12512 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12513 if (type_die == NULL)
12514 return NULL;
12515 deref = new_loc_descr (DW_OP_GNU_deref_type,
12516 GET_MODE_SIZE (mode), 0);
12517 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12518 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12519 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12520 add_loc_descr (&mem_loc_result, deref);
12522 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12523 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12524 else
12525 add_loc_descr (&mem_loc_result,
12526 new_loc_descr (DW_OP_deref_size,
12527 GET_MODE_SIZE (mode), 0));
12529 break;
12531 case LO_SUM:
12532 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12534 case LABEL_REF:
12535 /* Some ports can transform a symbol ref into a label ref, because
12536 the symbol ref is too far away and has to be dumped into a constant
12537 pool. */
12538 case CONST:
12539 case SYMBOL_REF:
12540 if (GET_MODE_CLASS (mode) != MODE_INT
12541 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12542 #ifdef POINTERS_EXTEND_UNSIGNED
12543 && (mode != Pmode || mem_mode == VOIDmode)
12544 #endif
12546 break;
12547 if (GET_CODE (rtl) == SYMBOL_REF
12548 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12550 dw_loc_descr_ref temp;
12552 /* If this is not defined, we have no way to emit the data. */
12553 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12554 break;
12556 temp = new_addr_loc_descr (rtl, dtprel_true);
12558 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12559 add_loc_descr (&mem_loc_result, temp);
12561 break;
12564 if (!const_ok_for_output (rtl))
12565 break;
12567 symref:
12568 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12569 vec_safe_push (used_rtx_array, rtl);
12570 break;
12572 case CONCAT:
12573 case CONCATN:
12574 case VAR_LOCATION:
12575 case DEBUG_IMPLICIT_PTR:
12576 expansion_failed (NULL_TREE, rtl,
12577 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12578 return 0;
12580 case ENTRY_VALUE:
12581 if (dwarf_strict)
12582 return NULL;
12583 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12585 if (GET_MODE_CLASS (mode) != MODE_INT
12586 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12587 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12588 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12589 else
12591 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12592 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12593 return NULL;
12594 op0 = one_reg_loc_descriptor (dbx_regnum,
12595 VAR_INIT_STATUS_INITIALIZED);
12598 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12599 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12601 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12602 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12603 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12604 return NULL;
12606 else
12607 gcc_unreachable ();
12608 if (op0 == NULL)
12609 return NULL;
12610 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12611 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12612 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12613 break;
12615 case DEBUG_PARAMETER_REF:
12616 mem_loc_result = parameter_ref_descriptor (rtl);
12617 break;
12619 case PRE_MODIFY:
12620 /* Extract the PLUS expression nested inside and fall into
12621 PLUS code below. */
12622 rtl = XEXP (rtl, 1);
12623 goto plus;
12625 case PRE_INC:
12626 case PRE_DEC:
12627 /* Turn these into a PLUS expression and fall into the PLUS code
12628 below. */
12629 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12630 gen_int_mode (GET_CODE (rtl) == PRE_INC
12631 ? GET_MODE_UNIT_SIZE (mem_mode)
12632 : -GET_MODE_UNIT_SIZE (mem_mode),
12633 mode));
12635 /* ... fall through ... */
12637 case PLUS:
12638 plus:
12639 if (is_based_loc (rtl)
12640 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12641 || XEXP (rtl, 0) == arg_pointer_rtx
12642 || XEXP (rtl, 0) == frame_pointer_rtx)
12643 && GET_MODE_CLASS (mode) == MODE_INT)
12644 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12645 INTVAL (XEXP (rtl, 1)),
12646 VAR_INIT_STATUS_INITIALIZED);
12647 else
12649 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12650 VAR_INIT_STATUS_INITIALIZED);
12651 if (mem_loc_result == 0)
12652 break;
12654 if (CONST_INT_P (XEXP (rtl, 1))
12655 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12656 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12657 else
12659 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12660 VAR_INIT_STATUS_INITIALIZED);
12661 if (op1 == 0)
12662 break;
12663 add_loc_descr (&mem_loc_result, op1);
12664 add_loc_descr (&mem_loc_result,
12665 new_loc_descr (DW_OP_plus, 0, 0));
12668 break;
12670 /* If a pseudo-reg is optimized away, it is possible for it to
12671 be replaced with a MEM containing a multiply or shift. */
12672 case MINUS:
12673 op = DW_OP_minus;
12674 goto do_binop;
12676 case MULT:
12677 op = DW_OP_mul;
12678 goto do_binop;
12680 case DIV:
12681 if (!dwarf_strict
12682 && GET_MODE_CLASS (mode) == MODE_INT
12683 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12685 mem_loc_result = typed_binop (DW_OP_div, rtl,
12686 base_type_for_mode (mode, 0),
12687 mode, mem_mode);
12688 break;
12690 op = DW_OP_div;
12691 goto do_binop;
12693 case UMOD:
12694 op = DW_OP_mod;
12695 goto do_binop;
12697 case ASHIFT:
12698 op = DW_OP_shl;
12699 goto do_shift;
12701 case ASHIFTRT:
12702 op = DW_OP_shra;
12703 goto do_shift;
12705 case LSHIFTRT:
12706 op = DW_OP_shr;
12707 goto do_shift;
12709 do_shift:
12710 if (GET_MODE_CLASS (mode) != MODE_INT)
12711 break;
12712 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12713 VAR_INIT_STATUS_INITIALIZED);
12715 rtx rtlop1 = XEXP (rtl, 1);
12716 if (GET_MODE (rtlop1) != VOIDmode
12717 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12718 < GET_MODE_BITSIZE (mode))
12719 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12720 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12721 VAR_INIT_STATUS_INITIALIZED);
12724 if (op0 == 0 || op1 == 0)
12725 break;
12727 mem_loc_result = op0;
12728 add_loc_descr (&mem_loc_result, op1);
12729 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12730 break;
12732 case AND:
12733 op = DW_OP_and;
12734 goto do_binop;
12736 case IOR:
12737 op = DW_OP_or;
12738 goto do_binop;
12740 case XOR:
12741 op = DW_OP_xor;
12742 goto do_binop;
12744 do_binop:
12745 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12746 VAR_INIT_STATUS_INITIALIZED);
12747 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12748 VAR_INIT_STATUS_INITIALIZED);
12750 if (op0 == 0 || op1 == 0)
12751 break;
12753 mem_loc_result = op0;
12754 add_loc_descr (&mem_loc_result, op1);
12755 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12756 break;
12758 case MOD:
12759 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12761 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12762 base_type_for_mode (mode, 0),
12763 mode, mem_mode);
12764 break;
12767 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12768 VAR_INIT_STATUS_INITIALIZED);
12769 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12770 VAR_INIT_STATUS_INITIALIZED);
12772 if (op0 == 0 || op1 == 0)
12773 break;
12775 mem_loc_result = op0;
12776 add_loc_descr (&mem_loc_result, op1);
12777 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12778 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12779 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12780 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12781 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12782 break;
12784 case UDIV:
12785 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12787 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12789 op = DW_OP_div;
12790 goto do_binop;
12792 mem_loc_result = typed_binop (DW_OP_div, rtl,
12793 base_type_for_mode (mode, 1),
12794 mode, mem_mode);
12796 break;
12798 case NOT:
12799 op = DW_OP_not;
12800 goto do_unop;
12802 case ABS:
12803 op = DW_OP_abs;
12804 goto do_unop;
12806 case NEG:
12807 op = DW_OP_neg;
12808 goto do_unop;
12810 do_unop:
12811 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12812 VAR_INIT_STATUS_INITIALIZED);
12814 if (op0 == 0)
12815 break;
12817 mem_loc_result = op0;
12818 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12819 break;
12821 case CONST_INT:
12822 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12823 #ifdef POINTERS_EXTEND_UNSIGNED
12824 || (mode == Pmode
12825 && mem_mode != VOIDmode
12826 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12827 #endif
12830 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12831 break;
12833 if (!dwarf_strict
12834 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12835 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12837 dw_die_ref type_die = base_type_for_mode (mode, 1);
12838 enum machine_mode amode;
12839 if (type_die == NULL)
12840 return NULL;
12841 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12842 MODE_INT, 0);
12843 if (INTVAL (rtl) >= 0
12844 && amode != BLKmode
12845 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12846 /* const DW_OP_GNU_convert <XXX> vs.
12847 DW_OP_GNU_const_type <XXX, 1, const>. */
12848 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12849 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12851 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12852 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12853 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12854 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12855 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12856 add_loc_descr (&mem_loc_result, op0);
12857 return mem_loc_result;
12859 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12860 INTVAL (rtl));
12861 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12862 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12863 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12864 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12865 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12866 else
12868 mem_loc_result->dw_loc_oprnd2.val_class
12869 = dw_val_class_const_double;
12870 mem_loc_result->dw_loc_oprnd2.v.val_double
12871 = double_int::from_shwi (INTVAL (rtl));
12874 break;
12876 case CONST_DOUBLE:
12877 if (!dwarf_strict)
12879 dw_die_ref type_die;
12881 /* Note that a CONST_DOUBLE rtx could represent either an integer
12882 or a floating-point constant. A CONST_DOUBLE is used whenever
12883 the constant requires more than one word in order to be
12884 adequately represented. We output CONST_DOUBLEs as blocks. */
12885 if (mode == VOIDmode
12886 || (GET_MODE (rtl) == VOIDmode
12887 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12888 break;
12889 type_die = base_type_for_mode (mode,
12890 GET_MODE_CLASS (mode) == MODE_INT);
12891 if (type_die == NULL)
12892 return NULL;
12893 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12894 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12895 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12896 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12897 #if TARGET_SUPPORTS_WIDE_INT == 0
12898 if (!SCALAR_FLOAT_MODE_P (mode))
12900 mem_loc_result->dw_loc_oprnd2.val_class
12901 = dw_val_class_const_double;
12902 mem_loc_result->dw_loc_oprnd2.v.val_double
12903 = rtx_to_double_int (rtl);
12905 else
12906 #endif
12908 unsigned int length = GET_MODE_SIZE (mode);
12909 unsigned char *array
12910 = (unsigned char*) ggc_alloc_atomic (length);
12912 insert_float (rtl, array);
12913 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12914 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12915 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12916 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12919 break;
12921 case CONST_WIDE_INT:
12922 if (!dwarf_strict)
12924 dw_die_ref type_die;
12926 type_die = base_type_for_mode (mode,
12927 GET_MODE_CLASS (mode) == MODE_INT);
12928 if (type_die == NULL)
12929 return NULL;
12930 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12931 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12932 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12933 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12934 mem_loc_result->dw_loc_oprnd2.val_class
12935 = dw_val_class_wide_int;
12936 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc_cleared_wide_int ();
12937 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
12939 break;
12941 case EQ:
12942 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12943 break;
12945 case GE:
12946 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12947 break;
12949 case GT:
12950 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12951 break;
12953 case LE:
12954 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12955 break;
12957 case LT:
12958 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12959 break;
12961 case NE:
12962 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12963 break;
12965 case GEU:
12966 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12967 break;
12969 case GTU:
12970 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12971 break;
12973 case LEU:
12974 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12975 break;
12977 case LTU:
12978 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12979 break;
12981 case UMIN:
12982 case UMAX:
12983 if (GET_MODE_CLASS (mode) != MODE_INT)
12984 break;
12985 /* FALLTHRU */
12986 case SMIN:
12987 case SMAX:
12988 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12989 break;
12991 case ZERO_EXTRACT:
12992 case SIGN_EXTRACT:
12993 if (CONST_INT_P (XEXP (rtl, 1))
12994 && CONST_INT_P (XEXP (rtl, 2))
12995 && ((unsigned) INTVAL (XEXP (rtl, 1))
12996 + (unsigned) INTVAL (XEXP (rtl, 2))
12997 <= GET_MODE_BITSIZE (mode))
12998 && GET_MODE_CLASS (mode) == MODE_INT
12999 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13000 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13002 int shift, size;
13003 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13004 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13005 if (op0 == 0)
13006 break;
13007 if (GET_CODE (rtl) == SIGN_EXTRACT)
13008 op = DW_OP_shra;
13009 else
13010 op = DW_OP_shr;
13011 mem_loc_result = op0;
13012 size = INTVAL (XEXP (rtl, 1));
13013 shift = INTVAL (XEXP (rtl, 2));
13014 if (BITS_BIG_ENDIAN)
13015 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13016 - shift - size;
13017 if (shift + size != (int) DWARF2_ADDR_SIZE)
13019 add_loc_descr (&mem_loc_result,
13020 int_loc_descriptor (DWARF2_ADDR_SIZE
13021 - shift - size));
13022 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13024 if (size != (int) DWARF2_ADDR_SIZE)
13026 add_loc_descr (&mem_loc_result,
13027 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13028 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13031 break;
13033 case IF_THEN_ELSE:
13035 dw_loc_descr_ref op2, bra_node, drop_node;
13036 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13037 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13038 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13039 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13040 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13041 VAR_INIT_STATUS_INITIALIZED);
13042 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13043 VAR_INIT_STATUS_INITIALIZED);
13044 if (op0 == NULL || op1 == NULL || op2 == NULL)
13045 break;
13047 mem_loc_result = op1;
13048 add_loc_descr (&mem_loc_result, op2);
13049 add_loc_descr (&mem_loc_result, op0);
13050 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13051 add_loc_descr (&mem_loc_result, bra_node);
13052 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13053 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13054 add_loc_descr (&mem_loc_result, drop_node);
13055 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13056 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13058 break;
13060 case FLOAT_EXTEND:
13061 case FLOAT_TRUNCATE:
13062 case FLOAT:
13063 case UNSIGNED_FLOAT:
13064 case FIX:
13065 case UNSIGNED_FIX:
13066 if (!dwarf_strict)
13068 dw_die_ref type_die;
13069 dw_loc_descr_ref cvt;
13071 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13072 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13073 if (op0 == NULL)
13074 break;
13075 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13076 && (GET_CODE (rtl) == FLOAT
13077 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13078 <= DWARF2_ADDR_SIZE))
13080 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13081 GET_CODE (rtl) == UNSIGNED_FLOAT);
13082 if (type_die == NULL)
13083 break;
13084 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13085 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13086 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13087 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13088 add_loc_descr (&op0, cvt);
13090 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13091 if (type_die == NULL)
13092 break;
13093 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13094 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13095 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13096 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13097 add_loc_descr (&op0, cvt);
13098 if (GET_MODE_CLASS (mode) == MODE_INT
13099 && (GET_CODE (rtl) == FIX
13100 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13102 op0 = convert_descriptor_to_mode (mode, op0);
13103 if (op0 == NULL)
13104 break;
13106 mem_loc_result = op0;
13108 break;
13110 case CLZ:
13111 case CTZ:
13112 case FFS:
13113 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13114 break;
13116 case POPCOUNT:
13117 case PARITY:
13118 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13119 break;
13121 case BSWAP:
13122 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13123 break;
13125 case ROTATE:
13126 case ROTATERT:
13127 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13128 break;
13130 case COMPARE:
13131 /* In theory, we could implement the above. */
13132 /* DWARF cannot represent the unsigned compare operations
13133 natively. */
13134 case SS_MULT:
13135 case US_MULT:
13136 case SS_DIV:
13137 case US_DIV:
13138 case SS_PLUS:
13139 case US_PLUS:
13140 case SS_MINUS:
13141 case US_MINUS:
13142 case SS_NEG:
13143 case US_NEG:
13144 case SS_ABS:
13145 case SS_ASHIFT:
13146 case US_ASHIFT:
13147 case SS_TRUNCATE:
13148 case US_TRUNCATE:
13149 case UNORDERED:
13150 case ORDERED:
13151 case UNEQ:
13152 case UNGE:
13153 case UNGT:
13154 case UNLE:
13155 case UNLT:
13156 case LTGT:
13157 case FRACT_CONVERT:
13158 case UNSIGNED_FRACT_CONVERT:
13159 case SAT_FRACT:
13160 case UNSIGNED_SAT_FRACT:
13161 case SQRT:
13162 case ASM_OPERANDS:
13163 case VEC_MERGE:
13164 case VEC_SELECT:
13165 case VEC_CONCAT:
13166 case VEC_DUPLICATE:
13167 case UNSPEC:
13168 case HIGH:
13169 case FMA:
13170 case STRICT_LOW_PART:
13171 case CONST_VECTOR:
13172 case CONST_FIXED:
13173 case CLRSB:
13174 case CLOBBER:
13175 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13176 can't express it in the debug info. This can happen e.g. with some
13177 TLS UNSPECs. */
13178 break;
13180 case CONST_STRING:
13181 resolve_one_addr (&rtl, NULL);
13182 goto symref;
13184 default:
13185 #ifdef ENABLE_CHECKING
13186 print_rtl (stderr, rtl);
13187 gcc_unreachable ();
13188 #else
13189 break;
13190 #endif
13193 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13194 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13196 return mem_loc_result;
13199 /* Return a descriptor that describes the concatenation of two locations.
13200 This is typically a complex variable. */
13202 static dw_loc_descr_ref
13203 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13205 dw_loc_descr_ref cc_loc_result = NULL;
13206 dw_loc_descr_ref x0_ref
13207 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13208 dw_loc_descr_ref x1_ref
13209 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13211 if (x0_ref == 0 || x1_ref == 0)
13212 return 0;
13214 cc_loc_result = x0_ref;
13215 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13217 add_loc_descr (&cc_loc_result, x1_ref);
13218 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13220 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13221 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13223 return cc_loc_result;
13226 /* Return a descriptor that describes the concatenation of N
13227 locations. */
13229 static dw_loc_descr_ref
13230 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13232 unsigned int i;
13233 dw_loc_descr_ref cc_loc_result = NULL;
13234 unsigned int n = XVECLEN (concatn, 0);
13236 for (i = 0; i < n; ++i)
13238 dw_loc_descr_ref ref;
13239 rtx x = XVECEXP (concatn, 0, i);
13241 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13242 if (ref == NULL)
13243 return NULL;
13245 add_loc_descr (&cc_loc_result, ref);
13246 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13249 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13250 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13252 return cc_loc_result;
13255 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13256 for DEBUG_IMPLICIT_PTR RTL. */
13258 static dw_loc_descr_ref
13259 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13261 dw_loc_descr_ref ret;
13262 dw_die_ref ref;
13264 if (dwarf_strict)
13265 return NULL;
13266 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13267 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13268 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13269 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13270 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13271 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13272 if (ref)
13274 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13275 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13276 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13278 else
13280 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13281 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13283 return ret;
13286 /* Output a proper Dwarf location descriptor for a variable or parameter
13287 which is either allocated in a register or in a memory location. For a
13288 register, we just generate an OP_REG and the register number. For a
13289 memory location we provide a Dwarf postfix expression describing how to
13290 generate the (dynamic) address of the object onto the address stack.
13292 MODE is mode of the decl if this loc_descriptor is going to be used in
13293 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13294 allowed, VOIDmode otherwise.
13296 If we don't know how to describe it, return 0. */
13298 static dw_loc_descr_ref
13299 loc_descriptor (rtx rtl, enum machine_mode mode,
13300 enum var_init_status initialized)
13302 dw_loc_descr_ref loc_result = NULL;
13304 switch (GET_CODE (rtl))
13306 case SUBREG:
13307 /* The case of a subreg may arise when we have a local (register)
13308 variable or a formal (register) parameter which doesn't quite fill
13309 up an entire register. For now, just assume that it is
13310 legitimate to make the Dwarf info refer to the whole register which
13311 contains the given subreg. */
13312 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13313 loc_result = loc_descriptor (SUBREG_REG (rtl),
13314 GET_MODE (SUBREG_REG (rtl)), initialized);
13315 else
13316 goto do_default;
13317 break;
13319 case REG:
13320 loc_result = reg_loc_descriptor (rtl, initialized);
13321 break;
13323 case MEM:
13324 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13325 GET_MODE (rtl), initialized);
13326 if (loc_result == NULL)
13327 loc_result = tls_mem_loc_descriptor (rtl);
13328 if (loc_result == NULL)
13330 rtx new_rtl = avoid_constant_pool_reference (rtl);
13331 if (new_rtl != rtl)
13332 loc_result = loc_descriptor (new_rtl, mode, initialized);
13334 break;
13336 case CONCAT:
13337 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13338 initialized);
13339 break;
13341 case CONCATN:
13342 loc_result = concatn_loc_descriptor (rtl, initialized);
13343 break;
13345 case VAR_LOCATION:
13346 /* Single part. */
13347 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13349 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13350 if (GET_CODE (loc) == EXPR_LIST)
13351 loc = XEXP (loc, 0);
13352 loc_result = loc_descriptor (loc, mode, initialized);
13353 break;
13356 rtl = XEXP (rtl, 1);
13357 /* FALLTHRU */
13359 case PARALLEL:
13361 rtvec par_elems = XVEC (rtl, 0);
13362 int num_elem = GET_NUM_ELEM (par_elems);
13363 enum machine_mode mode;
13364 int i;
13366 /* Create the first one, so we have something to add to. */
13367 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13368 VOIDmode, initialized);
13369 if (loc_result == NULL)
13370 return NULL;
13371 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13372 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13373 for (i = 1; i < num_elem; i++)
13375 dw_loc_descr_ref temp;
13377 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13378 VOIDmode, initialized);
13379 if (temp == NULL)
13380 return NULL;
13381 add_loc_descr (&loc_result, temp);
13382 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13383 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13386 break;
13388 case CONST_INT:
13389 if (mode != VOIDmode && mode != BLKmode)
13390 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13391 INTVAL (rtl));
13392 break;
13394 case CONST_DOUBLE:
13395 if (mode == VOIDmode)
13396 mode = GET_MODE (rtl);
13398 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13400 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13402 /* Note that a CONST_DOUBLE rtx could represent either an integer
13403 or a floating-point constant. A CONST_DOUBLE is used whenever
13404 the constant requires more than one word in order to be
13405 adequately represented. We output CONST_DOUBLEs as blocks. */
13406 loc_result = new_loc_descr (DW_OP_implicit_value,
13407 GET_MODE_SIZE (mode), 0);
13408 #if TARGET_SUPPORTS_WIDE_INT == 0
13409 if (!SCALAR_FLOAT_MODE_P (mode))
13411 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13412 loc_result->dw_loc_oprnd2.v.val_double
13413 = rtx_to_double_int (rtl);
13415 else
13416 #endif
13418 unsigned int length = GET_MODE_SIZE (mode);
13419 unsigned char *array
13420 = (unsigned char*) ggc_alloc_atomic (length);
13422 insert_float (rtl, array);
13423 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13424 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13425 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13426 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13429 break;
13431 case CONST_WIDE_INT:
13432 if (mode == VOIDmode)
13433 mode = GET_MODE (rtl);
13435 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13437 loc_result = new_loc_descr (DW_OP_implicit_value,
13438 GET_MODE_SIZE (mode), 0);
13439 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13440 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc_cleared_wide_int ();
13441 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13443 break;
13445 case CONST_VECTOR:
13446 if (mode == VOIDmode)
13447 mode = GET_MODE (rtl);
13449 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13451 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13452 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13453 unsigned char *array = (unsigned char *)
13454 ggc_alloc_atomic (length * elt_size);
13455 unsigned int i;
13456 unsigned char *p;
13457 enum machine_mode imode = GET_MODE_INNER (mode);
13459 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13460 switch (GET_MODE_CLASS (mode))
13462 case MODE_VECTOR_INT:
13463 for (i = 0, p = array; i < length; i++, p += elt_size)
13465 rtx elt = CONST_VECTOR_ELT (rtl, i);
13466 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13468 break;
13470 case MODE_VECTOR_FLOAT:
13471 for (i = 0, p = array; i < length; i++, p += elt_size)
13473 rtx elt = CONST_VECTOR_ELT (rtl, i);
13474 insert_float (elt, p);
13476 break;
13478 default:
13479 gcc_unreachable ();
13482 loc_result = new_loc_descr (DW_OP_implicit_value,
13483 length * elt_size, 0);
13484 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13485 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13486 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13487 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13489 break;
13491 case CONST:
13492 if (mode == VOIDmode
13493 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13494 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13495 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13497 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13498 break;
13500 /* FALLTHROUGH */
13501 case SYMBOL_REF:
13502 if (!const_ok_for_output (rtl))
13503 break;
13504 case LABEL_REF:
13505 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13506 && (dwarf_version >= 4 || !dwarf_strict))
13508 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13509 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13510 vec_safe_push (used_rtx_array, rtl);
13512 break;
13514 case DEBUG_IMPLICIT_PTR:
13515 loc_result = implicit_ptr_descriptor (rtl, 0);
13516 break;
13518 case PLUS:
13519 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13520 && CONST_INT_P (XEXP (rtl, 1)))
13522 loc_result
13523 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13524 break;
13526 /* FALLTHRU */
13527 do_default:
13528 default:
13529 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13530 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13531 && dwarf_version >= 4)
13532 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13534 /* Value expression. */
13535 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13536 if (loc_result)
13537 add_loc_descr (&loc_result,
13538 new_loc_descr (DW_OP_stack_value, 0, 0));
13540 break;
13543 return loc_result;
13546 /* We need to figure out what section we should use as the base for the
13547 address ranges where a given location is valid.
13548 1. If this particular DECL has a section associated with it, use that.
13549 2. If this function has a section associated with it, use that.
13550 3. Otherwise, use the text section.
13551 XXX: If you split a variable across multiple sections, we won't notice. */
13553 static const char *
13554 secname_for_decl (const_tree decl)
13556 const char *secname;
13558 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13560 tree sectree = DECL_SECTION_NAME (decl);
13561 secname = TREE_STRING_POINTER (sectree);
13563 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13565 tree sectree = DECL_SECTION_NAME (current_function_decl);
13566 secname = TREE_STRING_POINTER (sectree);
13568 else if (cfun && in_cold_section_p)
13569 secname = crtl->subsections.cold_section_label;
13570 else
13571 secname = text_section_label;
13573 return secname;
13576 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13578 static bool
13579 decl_by_reference_p (tree decl)
13581 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13582 || TREE_CODE (decl) == VAR_DECL)
13583 && DECL_BY_REFERENCE (decl));
13586 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13587 for VARLOC. */
13589 static dw_loc_descr_ref
13590 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13591 enum var_init_status initialized)
13593 int have_address = 0;
13594 dw_loc_descr_ref descr;
13595 enum machine_mode mode;
13597 if (want_address != 2)
13599 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13600 /* Single part. */
13601 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13603 varloc = PAT_VAR_LOCATION_LOC (varloc);
13604 if (GET_CODE (varloc) == EXPR_LIST)
13605 varloc = XEXP (varloc, 0);
13606 mode = GET_MODE (varloc);
13607 if (MEM_P (varloc))
13609 rtx addr = XEXP (varloc, 0);
13610 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13611 mode, initialized);
13612 if (descr)
13613 have_address = 1;
13614 else
13616 rtx x = avoid_constant_pool_reference (varloc);
13617 if (x != varloc)
13618 descr = mem_loc_descriptor (x, mode, VOIDmode,
13619 initialized);
13622 else
13623 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13625 else
13626 return 0;
13628 else
13630 if (GET_CODE (varloc) == VAR_LOCATION)
13631 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13632 else
13633 mode = DECL_MODE (loc);
13634 descr = loc_descriptor (varloc, mode, initialized);
13635 have_address = 1;
13638 if (!descr)
13639 return 0;
13641 if (want_address == 2 && !have_address
13642 && (dwarf_version >= 4 || !dwarf_strict))
13644 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13646 expansion_failed (loc, NULL_RTX,
13647 "DWARF address size mismatch");
13648 return 0;
13650 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13651 have_address = 1;
13653 /* Show if we can't fill the request for an address. */
13654 if (want_address && !have_address)
13656 expansion_failed (loc, NULL_RTX,
13657 "Want address and only have value");
13658 return 0;
13661 /* If we've got an address and don't want one, dereference. */
13662 if (!want_address && have_address)
13664 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13665 enum dwarf_location_atom op;
13667 if (size > DWARF2_ADDR_SIZE || size == -1)
13669 expansion_failed (loc, NULL_RTX,
13670 "DWARF address size mismatch");
13671 return 0;
13673 else if (size == DWARF2_ADDR_SIZE)
13674 op = DW_OP_deref;
13675 else
13676 op = DW_OP_deref_size;
13678 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13681 return descr;
13684 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13685 if it is not possible. */
13687 static dw_loc_descr_ref
13688 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13690 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13691 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13692 else if (dwarf_version >= 3 || !dwarf_strict)
13693 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13694 else
13695 return NULL;
13698 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13699 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13701 static dw_loc_descr_ref
13702 dw_sra_loc_expr (tree decl, rtx loc)
13704 rtx p;
13705 unsigned int padsize = 0;
13706 dw_loc_descr_ref descr, *descr_tail;
13707 unsigned HOST_WIDE_INT decl_size;
13708 rtx varloc;
13709 enum var_init_status initialized;
13711 if (DECL_SIZE (decl) == NULL
13712 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13713 return NULL;
13715 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13716 descr = NULL;
13717 descr_tail = &descr;
13719 for (p = loc; p; p = XEXP (p, 1))
13721 unsigned int bitsize = decl_piece_bitsize (p);
13722 rtx loc_note = *decl_piece_varloc_ptr (p);
13723 dw_loc_descr_ref cur_descr;
13724 dw_loc_descr_ref *tail, last = NULL;
13725 unsigned int opsize = 0;
13727 if (loc_note == NULL_RTX
13728 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13730 padsize += bitsize;
13731 continue;
13733 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13734 varloc = NOTE_VAR_LOCATION (loc_note);
13735 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13736 if (cur_descr == NULL)
13738 padsize += bitsize;
13739 continue;
13742 /* Check that cur_descr either doesn't use
13743 DW_OP_*piece operations, or their sum is equal
13744 to bitsize. Otherwise we can't embed it. */
13745 for (tail = &cur_descr; *tail != NULL;
13746 tail = &(*tail)->dw_loc_next)
13747 if ((*tail)->dw_loc_opc == DW_OP_piece)
13749 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13750 * BITS_PER_UNIT;
13751 last = *tail;
13753 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13755 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13756 last = *tail;
13759 if (last != NULL && opsize != bitsize)
13761 padsize += bitsize;
13762 /* Discard the current piece of the descriptor and release any
13763 addr_table entries it uses. */
13764 remove_loc_list_addr_table_entries (cur_descr);
13765 continue;
13768 /* If there is a hole, add DW_OP_*piece after empty DWARF
13769 expression, which means that those bits are optimized out. */
13770 if (padsize)
13772 if (padsize > decl_size)
13774 remove_loc_list_addr_table_entries (cur_descr);
13775 goto discard_descr;
13777 decl_size -= padsize;
13778 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13779 if (*descr_tail == NULL)
13781 remove_loc_list_addr_table_entries (cur_descr);
13782 goto discard_descr;
13784 descr_tail = &(*descr_tail)->dw_loc_next;
13785 padsize = 0;
13787 *descr_tail = cur_descr;
13788 descr_tail = tail;
13789 if (bitsize > decl_size)
13790 goto discard_descr;
13791 decl_size -= bitsize;
13792 if (last == NULL)
13794 HOST_WIDE_INT offset = 0;
13795 if (GET_CODE (varloc) == VAR_LOCATION
13796 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13798 varloc = PAT_VAR_LOCATION_LOC (varloc);
13799 if (GET_CODE (varloc) == EXPR_LIST)
13800 varloc = XEXP (varloc, 0);
13804 if (GET_CODE (varloc) == CONST
13805 || GET_CODE (varloc) == SIGN_EXTEND
13806 || GET_CODE (varloc) == ZERO_EXTEND)
13807 varloc = XEXP (varloc, 0);
13808 else if (GET_CODE (varloc) == SUBREG)
13809 varloc = SUBREG_REG (varloc);
13810 else
13811 break;
13813 while (1);
13814 /* DW_OP_bit_size offset should be zero for register
13815 or implicit location descriptions and empty location
13816 descriptions, but for memory addresses needs big endian
13817 adjustment. */
13818 if (MEM_P (varloc))
13820 unsigned HOST_WIDE_INT memsize
13821 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13822 if (memsize != bitsize)
13824 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13825 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13826 goto discard_descr;
13827 if (memsize < bitsize)
13828 goto discard_descr;
13829 if (BITS_BIG_ENDIAN)
13830 offset = memsize - bitsize;
13834 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13835 if (*descr_tail == NULL)
13836 goto discard_descr;
13837 descr_tail = &(*descr_tail)->dw_loc_next;
13841 /* If there were any non-empty expressions, add padding till the end of
13842 the decl. */
13843 if (descr != NULL && decl_size != 0)
13845 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13846 if (*descr_tail == NULL)
13847 goto discard_descr;
13849 return descr;
13851 discard_descr:
13852 /* Discard the descriptor and release any addr_table entries it uses. */
13853 remove_loc_list_addr_table_entries (descr);
13854 return NULL;
13857 /* Return the dwarf representation of the location list LOC_LIST of
13858 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13859 function. */
13861 static dw_loc_list_ref
13862 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13864 const char *endname, *secname;
13865 rtx varloc;
13866 enum var_init_status initialized;
13867 struct var_loc_node *node;
13868 dw_loc_descr_ref descr;
13869 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13870 dw_loc_list_ref list = NULL;
13871 dw_loc_list_ref *listp = &list;
13873 /* Now that we know what section we are using for a base,
13874 actually construct the list of locations.
13875 The first location information is what is passed to the
13876 function that creates the location list, and the remaining
13877 locations just get added on to that list.
13878 Note that we only know the start address for a location
13879 (IE location changes), so to build the range, we use
13880 the range [current location start, next location start].
13881 This means we have to special case the last node, and generate
13882 a range of [last location start, end of function label]. */
13884 secname = secname_for_decl (decl);
13886 for (node = loc_list->first; node; node = node->next)
13887 if (GET_CODE (node->loc) == EXPR_LIST
13888 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13890 if (GET_CODE (node->loc) == EXPR_LIST)
13892 /* This requires DW_OP_{,bit_}piece, which is not usable
13893 inside DWARF expressions. */
13894 if (want_address != 2)
13895 continue;
13896 descr = dw_sra_loc_expr (decl, node->loc);
13897 if (descr == NULL)
13898 continue;
13900 else
13902 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13903 varloc = NOTE_VAR_LOCATION (node->loc);
13904 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13906 if (descr)
13908 bool range_across_switch = false;
13909 /* If section switch happens in between node->label
13910 and node->next->label (or end of function) and
13911 we can't emit it as a single entry list,
13912 emit two ranges, first one ending at the end
13913 of first partition and second one starting at the
13914 beginning of second partition. */
13915 if (node == loc_list->last_before_switch
13916 && (node != loc_list->first || loc_list->first->next)
13917 && current_function_decl)
13919 endname = cfun->fde->dw_fde_end;
13920 range_across_switch = true;
13922 /* The variable has a location between NODE->LABEL and
13923 NODE->NEXT->LABEL. */
13924 else if (node->next)
13925 endname = node->next->label;
13926 /* If the variable has a location at the last label
13927 it keeps its location until the end of function. */
13928 else if (!current_function_decl)
13929 endname = text_end_label;
13930 else
13932 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13933 current_function_funcdef_no);
13934 endname = ggc_strdup (label_id);
13937 *listp = new_loc_list (descr, node->label, endname, secname);
13938 if (TREE_CODE (decl) == PARM_DECL
13939 && node == loc_list->first
13940 && NOTE_P (node->loc)
13941 && strcmp (node->label, endname) == 0)
13942 (*listp)->force = true;
13943 listp = &(*listp)->dw_loc_next;
13945 if (range_across_switch)
13947 if (GET_CODE (node->loc) == EXPR_LIST)
13948 descr = dw_sra_loc_expr (decl, node->loc);
13949 else
13951 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13952 varloc = NOTE_VAR_LOCATION (node->loc);
13953 descr = dw_loc_list_1 (decl, varloc, want_address,
13954 initialized);
13956 gcc_assert (descr);
13957 /* The variable has a location between NODE->LABEL and
13958 NODE->NEXT->LABEL. */
13959 if (node->next)
13960 endname = node->next->label;
13961 else
13962 endname = cfun->fde->dw_fde_second_end;
13963 *listp = new_loc_list (descr,
13964 cfun->fde->dw_fde_second_begin,
13965 endname, secname);
13966 listp = &(*listp)->dw_loc_next;
13971 /* Try to avoid the overhead of a location list emitting a location
13972 expression instead, but only if we didn't have more than one
13973 location entry in the first place. If some entries were not
13974 representable, we don't want to pretend a single entry that was
13975 applies to the entire scope in which the variable is
13976 available. */
13977 if (list && loc_list->first->next)
13978 gen_llsym (list);
13980 return list;
13983 /* Return if the loc_list has only single element and thus can be represented
13984 as location description. */
13986 static bool
13987 single_element_loc_list_p (dw_loc_list_ref list)
13989 gcc_assert (!list->dw_loc_next || list->ll_symbol);
13990 return !list->ll_symbol;
13993 /* To each location in list LIST add loc descr REF. */
13995 static void
13996 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13998 dw_loc_descr_ref copy;
13999 add_loc_descr (&list->expr, ref);
14000 list = list->dw_loc_next;
14001 while (list)
14003 copy = ggc_alloc_dw_loc_descr_node ();
14004 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14005 add_loc_descr (&list->expr, copy);
14006 while (copy->dw_loc_next)
14008 dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
14009 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14010 copy->dw_loc_next = new_copy;
14011 copy = new_copy;
14013 list = list->dw_loc_next;
14017 /* Given two lists RET and LIST
14018 produce location list that is result of adding expression in LIST
14019 to expression in RET on each position in program.
14020 Might be destructive on both RET and LIST.
14022 TODO: We handle only simple cases of RET or LIST having at most one
14023 element. General case would inolve sorting the lists in program order
14024 and merging them that will need some additional work.
14025 Adding that will improve quality of debug info especially for SRA-ed
14026 structures. */
14028 static void
14029 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14031 if (!list)
14032 return;
14033 if (!*ret)
14035 *ret = list;
14036 return;
14038 if (!list->dw_loc_next)
14040 add_loc_descr_to_each (*ret, list->expr);
14041 return;
14043 if (!(*ret)->dw_loc_next)
14045 add_loc_descr_to_each (list, (*ret)->expr);
14046 *ret = list;
14047 return;
14049 expansion_failed (NULL_TREE, NULL_RTX,
14050 "Don't know how to merge two non-trivial"
14051 " location lists.\n");
14052 *ret = NULL;
14053 return;
14056 /* LOC is constant expression. Try a luck, look it up in constant
14057 pool and return its loc_descr of its address. */
14059 static dw_loc_descr_ref
14060 cst_pool_loc_descr (tree loc)
14062 /* Get an RTL for this, if something has been emitted. */
14063 rtx rtl = lookup_constant_def (loc);
14065 if (!rtl || !MEM_P (rtl))
14067 gcc_assert (!rtl);
14068 return 0;
14070 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14072 /* TODO: We might get more coverage if we was actually delaying expansion
14073 of all expressions till end of compilation when constant pools are fully
14074 populated. */
14075 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14077 expansion_failed (loc, NULL_RTX,
14078 "CST value in contant pool but not marked.");
14079 return 0;
14081 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14082 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14085 /* Return dw_loc_list representing address of addr_expr LOC
14086 by looking for inner INDIRECT_REF expression and turning
14087 it into simple arithmetics. */
14089 static dw_loc_list_ref
14090 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14092 tree obj, offset;
14093 HOST_WIDE_INT bitsize, bitpos, bytepos;
14094 enum machine_mode mode;
14095 int unsignedp, volatilep = 0;
14096 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14098 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14099 &bitsize, &bitpos, &offset, &mode,
14100 &unsignedp, &volatilep, false);
14101 STRIP_NOPS (obj);
14102 if (bitpos % BITS_PER_UNIT)
14104 expansion_failed (loc, NULL_RTX, "bitfield access");
14105 return 0;
14107 if (!INDIRECT_REF_P (obj))
14109 expansion_failed (obj,
14110 NULL_RTX, "no indirect ref in inner refrence");
14111 return 0;
14113 if (!offset && !bitpos)
14114 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14115 else if (toplev
14116 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14117 && (dwarf_version >= 4 || !dwarf_strict))
14119 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14120 if (!list_ret)
14121 return 0;
14122 if (offset)
14124 /* Variable offset. */
14125 list_ret1 = loc_list_from_tree (offset, 0);
14126 if (list_ret1 == 0)
14127 return 0;
14128 add_loc_list (&list_ret, list_ret1);
14129 if (!list_ret)
14130 return 0;
14131 add_loc_descr_to_each (list_ret,
14132 new_loc_descr (DW_OP_plus, 0, 0));
14134 bytepos = bitpos / BITS_PER_UNIT;
14135 if (bytepos > 0)
14136 add_loc_descr_to_each (list_ret,
14137 new_loc_descr (DW_OP_plus_uconst,
14138 bytepos, 0));
14139 else if (bytepos < 0)
14140 loc_list_plus_const (list_ret, bytepos);
14141 add_loc_descr_to_each (list_ret,
14142 new_loc_descr (DW_OP_stack_value, 0, 0));
14144 return list_ret;
14148 /* Generate Dwarf location list representing LOC.
14149 If WANT_ADDRESS is false, expression computing LOC will be computed
14150 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14151 if WANT_ADDRESS is 2, expression computing address useable in location
14152 will be returned (i.e. DW_OP_reg can be used
14153 to refer to register values). */
14155 static dw_loc_list_ref
14156 loc_list_from_tree (tree loc, int want_address)
14158 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14159 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14160 int have_address = 0;
14161 enum dwarf_location_atom op;
14163 /* ??? Most of the time we do not take proper care for sign/zero
14164 extending the values properly. Hopefully this won't be a real
14165 problem... */
14167 switch (TREE_CODE (loc))
14169 case ERROR_MARK:
14170 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14171 return 0;
14173 case PLACEHOLDER_EXPR:
14174 /* This case involves extracting fields from an object to determine the
14175 position of other fields. We don't try to encode this here. The
14176 only user of this is Ada, which encodes the needed information using
14177 the names of types. */
14178 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14179 return 0;
14181 case CALL_EXPR:
14182 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14183 /* There are no opcodes for these operations. */
14184 return 0;
14186 case PREINCREMENT_EXPR:
14187 case PREDECREMENT_EXPR:
14188 case POSTINCREMENT_EXPR:
14189 case POSTDECREMENT_EXPR:
14190 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14191 /* There are no opcodes for these operations. */
14192 return 0;
14194 case ADDR_EXPR:
14195 /* If we already want an address, see if there is INDIRECT_REF inside
14196 e.g. for &this->field. */
14197 if (want_address)
14199 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14200 (loc, want_address == 2);
14201 if (list_ret)
14202 have_address = 1;
14203 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14204 && (ret = cst_pool_loc_descr (loc)))
14205 have_address = 1;
14207 /* Otherwise, process the argument and look for the address. */
14208 if (!list_ret && !ret)
14209 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14210 else
14212 if (want_address)
14213 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14214 return NULL;
14216 break;
14218 case VAR_DECL:
14219 if (DECL_THREAD_LOCAL_P (loc))
14221 rtx rtl;
14222 enum dwarf_location_atom tls_op;
14223 enum dtprel_bool dtprel = dtprel_false;
14225 if (targetm.have_tls)
14227 /* If this is not defined, we have no way to emit the
14228 data. */
14229 if (!targetm.asm_out.output_dwarf_dtprel)
14230 return 0;
14232 /* The way DW_OP_GNU_push_tls_address is specified, we
14233 can only look up addresses of objects in the current
14234 module. We used DW_OP_addr as first op, but that's
14235 wrong, because DW_OP_addr is relocated by the debug
14236 info consumer, while DW_OP_GNU_push_tls_address
14237 operand shouldn't be. */
14238 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14239 return 0;
14240 dtprel = dtprel_true;
14241 tls_op = DW_OP_GNU_push_tls_address;
14243 else
14245 if (!targetm.emutls.debug_form_tls_address
14246 || !(dwarf_version >= 3 || !dwarf_strict))
14247 return 0;
14248 /* We stuffed the control variable into the DECL_VALUE_EXPR
14249 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14250 no longer appear in gimple code. We used the control
14251 variable in specific so that we could pick it up here. */
14252 loc = DECL_VALUE_EXPR (loc);
14253 tls_op = DW_OP_form_tls_address;
14256 rtl = rtl_for_decl_location (loc);
14257 if (rtl == NULL_RTX)
14258 return 0;
14260 if (!MEM_P (rtl))
14261 return 0;
14262 rtl = XEXP (rtl, 0);
14263 if (! CONSTANT_P (rtl))
14264 return 0;
14266 ret = new_addr_loc_descr (rtl, dtprel);
14267 ret1 = new_loc_descr (tls_op, 0, 0);
14268 add_loc_descr (&ret, ret1);
14270 have_address = 1;
14271 break;
14273 /* FALLTHRU */
14275 case PARM_DECL:
14276 case RESULT_DECL:
14277 if (DECL_HAS_VALUE_EXPR_P (loc))
14278 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14279 want_address);
14280 /* FALLTHRU */
14282 case FUNCTION_DECL:
14284 rtx rtl;
14285 var_loc_list *loc_list = lookup_decl_loc (loc);
14287 if (loc_list && loc_list->first)
14289 list_ret = dw_loc_list (loc_list, loc, want_address);
14290 have_address = want_address != 0;
14291 break;
14293 rtl = rtl_for_decl_location (loc);
14294 if (rtl == NULL_RTX)
14296 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14297 return 0;
14299 else if (CONST_INT_P (rtl))
14301 HOST_WIDE_INT val = INTVAL (rtl);
14302 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14303 val &= GET_MODE_MASK (DECL_MODE (loc));
14304 ret = int_loc_descriptor (val);
14306 else if (GET_CODE (rtl) == CONST_STRING)
14308 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14309 return 0;
14311 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14312 ret = new_addr_loc_descr (rtl, dtprel_false);
14313 else
14315 enum machine_mode mode, mem_mode;
14317 /* Certain constructs can only be represented at top-level. */
14318 if (want_address == 2)
14320 ret = loc_descriptor (rtl, VOIDmode,
14321 VAR_INIT_STATUS_INITIALIZED);
14322 have_address = 1;
14324 else
14326 mode = GET_MODE (rtl);
14327 mem_mode = VOIDmode;
14328 if (MEM_P (rtl))
14330 mem_mode = mode;
14331 mode = get_address_mode (rtl);
14332 rtl = XEXP (rtl, 0);
14333 have_address = 1;
14335 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14336 VAR_INIT_STATUS_INITIALIZED);
14338 if (!ret)
14339 expansion_failed (loc, rtl,
14340 "failed to produce loc descriptor for rtl");
14343 break;
14345 case MEM_REF:
14346 /* ??? FIXME. */
14347 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14348 return 0;
14349 /* Fallthru. */
14350 case INDIRECT_REF:
14351 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14352 have_address = 1;
14353 break;
14355 case COMPOUND_EXPR:
14356 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14358 CASE_CONVERT:
14359 case VIEW_CONVERT_EXPR:
14360 case SAVE_EXPR:
14361 case MODIFY_EXPR:
14362 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14364 case COMPONENT_REF:
14365 case BIT_FIELD_REF:
14366 case ARRAY_REF:
14367 case ARRAY_RANGE_REF:
14368 case REALPART_EXPR:
14369 case IMAGPART_EXPR:
14371 tree obj, offset;
14372 HOST_WIDE_INT bitsize, bitpos, bytepos;
14373 enum machine_mode mode;
14374 int unsignedp, volatilep = 0;
14376 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14377 &unsignedp, &volatilep, false);
14379 gcc_assert (obj != loc);
14381 list_ret = loc_list_from_tree (obj,
14382 want_address == 2
14383 && !bitpos && !offset ? 2 : 1);
14384 /* TODO: We can extract value of the small expression via shifting even
14385 for nonzero bitpos. */
14386 if (list_ret == 0)
14387 return 0;
14388 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14390 expansion_failed (loc, NULL_RTX,
14391 "bitfield access");
14392 return 0;
14395 if (offset != NULL_TREE)
14397 /* Variable offset. */
14398 list_ret1 = loc_list_from_tree (offset, 0);
14399 if (list_ret1 == 0)
14400 return 0;
14401 add_loc_list (&list_ret, list_ret1);
14402 if (!list_ret)
14403 return 0;
14404 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14407 bytepos = bitpos / BITS_PER_UNIT;
14408 if (bytepos > 0)
14409 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14410 else if (bytepos < 0)
14411 loc_list_plus_const (list_ret, bytepos);
14413 have_address = 1;
14414 break;
14417 case INTEGER_CST:
14418 if ((want_address || !tree_fits_shwi_p (loc))
14419 && (ret = cst_pool_loc_descr (loc)))
14420 have_address = 1;
14421 else if (want_address == 2
14422 && tree_fits_shwi_p (loc)
14423 && (ret = address_of_int_loc_descriptor
14424 (int_size_in_bytes (TREE_TYPE (loc)),
14425 tree_to_shwi (loc))))
14426 have_address = 1;
14427 else if (tree_fits_shwi_p (loc))
14428 ret = int_loc_descriptor (tree_to_shwi (loc));
14429 else
14431 expansion_failed (loc, NULL_RTX,
14432 "Integer operand is not host integer");
14433 return 0;
14435 break;
14437 case CONSTRUCTOR:
14438 case REAL_CST:
14439 case STRING_CST:
14440 case COMPLEX_CST:
14441 if ((ret = cst_pool_loc_descr (loc)))
14442 have_address = 1;
14443 else
14444 /* We can construct small constants here using int_loc_descriptor. */
14445 expansion_failed (loc, NULL_RTX,
14446 "constructor or constant not in constant pool");
14447 break;
14449 case TRUTH_AND_EXPR:
14450 case TRUTH_ANDIF_EXPR:
14451 case BIT_AND_EXPR:
14452 op = DW_OP_and;
14453 goto do_binop;
14455 case TRUTH_XOR_EXPR:
14456 case BIT_XOR_EXPR:
14457 op = DW_OP_xor;
14458 goto do_binop;
14460 case TRUTH_OR_EXPR:
14461 case TRUTH_ORIF_EXPR:
14462 case BIT_IOR_EXPR:
14463 op = DW_OP_or;
14464 goto do_binop;
14466 case FLOOR_DIV_EXPR:
14467 case CEIL_DIV_EXPR:
14468 case ROUND_DIV_EXPR:
14469 case TRUNC_DIV_EXPR:
14470 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14471 return 0;
14472 op = DW_OP_div;
14473 goto do_binop;
14475 case MINUS_EXPR:
14476 op = DW_OP_minus;
14477 goto do_binop;
14479 case FLOOR_MOD_EXPR:
14480 case CEIL_MOD_EXPR:
14481 case ROUND_MOD_EXPR:
14482 case TRUNC_MOD_EXPR:
14483 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14485 op = DW_OP_mod;
14486 goto do_binop;
14488 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14489 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14490 if (list_ret == 0 || list_ret1 == 0)
14491 return 0;
14493 add_loc_list (&list_ret, list_ret1);
14494 if (list_ret == 0)
14495 return 0;
14496 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14497 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14498 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14499 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14500 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14501 break;
14503 case MULT_EXPR:
14504 op = DW_OP_mul;
14505 goto do_binop;
14507 case LSHIFT_EXPR:
14508 op = DW_OP_shl;
14509 goto do_binop;
14511 case RSHIFT_EXPR:
14512 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14513 goto do_binop;
14515 case POINTER_PLUS_EXPR:
14516 case PLUS_EXPR:
14517 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14519 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14520 if (list_ret == 0)
14521 return 0;
14523 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14524 break;
14527 op = DW_OP_plus;
14528 goto do_binop;
14530 case LE_EXPR:
14531 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14532 return 0;
14534 op = DW_OP_le;
14535 goto do_binop;
14537 case GE_EXPR:
14538 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14539 return 0;
14541 op = DW_OP_ge;
14542 goto do_binop;
14544 case LT_EXPR:
14545 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14546 return 0;
14548 op = DW_OP_lt;
14549 goto do_binop;
14551 case GT_EXPR:
14552 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14553 return 0;
14555 op = DW_OP_gt;
14556 goto do_binop;
14558 case EQ_EXPR:
14559 op = DW_OP_eq;
14560 goto do_binop;
14562 case NE_EXPR:
14563 op = DW_OP_ne;
14564 goto do_binop;
14566 do_binop:
14567 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14568 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14569 if (list_ret == 0 || list_ret1 == 0)
14570 return 0;
14572 add_loc_list (&list_ret, list_ret1);
14573 if (list_ret == 0)
14574 return 0;
14575 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14576 break;
14578 case TRUTH_NOT_EXPR:
14579 case BIT_NOT_EXPR:
14580 op = DW_OP_not;
14581 goto do_unop;
14583 case ABS_EXPR:
14584 op = DW_OP_abs;
14585 goto do_unop;
14587 case NEGATE_EXPR:
14588 op = DW_OP_neg;
14589 goto do_unop;
14591 do_unop:
14592 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14593 if (list_ret == 0)
14594 return 0;
14596 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14597 break;
14599 case MIN_EXPR:
14600 case MAX_EXPR:
14602 const enum tree_code code =
14603 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14605 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14606 build2 (code, integer_type_node,
14607 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14608 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14611 /* ... fall through ... */
14613 case COND_EXPR:
14615 dw_loc_descr_ref lhs
14616 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14617 dw_loc_list_ref rhs
14618 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14619 dw_loc_descr_ref bra_node, jump_node, tmp;
14621 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14622 if (list_ret == 0 || lhs == 0 || rhs == 0)
14623 return 0;
14625 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14626 add_loc_descr_to_each (list_ret, bra_node);
14628 add_loc_list (&list_ret, rhs);
14629 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14630 add_loc_descr_to_each (list_ret, jump_node);
14632 add_loc_descr_to_each (list_ret, lhs);
14633 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14634 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14636 /* ??? Need a node to point the skip at. Use a nop. */
14637 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14638 add_loc_descr_to_each (list_ret, tmp);
14639 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14640 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14642 break;
14644 case FIX_TRUNC_EXPR:
14645 return 0;
14647 default:
14648 /* Leave front-end specific codes as simply unknown. This comes
14649 up, for instance, with the C STMT_EXPR. */
14650 if ((unsigned int) TREE_CODE (loc)
14651 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14653 expansion_failed (loc, NULL_RTX,
14654 "language specific tree node");
14655 return 0;
14658 #ifdef ENABLE_CHECKING
14659 /* Otherwise this is a generic code; we should just lists all of
14660 these explicitly. We forgot one. */
14661 gcc_unreachable ();
14662 #else
14663 /* In a release build, we want to degrade gracefully: better to
14664 generate incomplete debugging information than to crash. */
14665 return NULL;
14666 #endif
14669 if (!ret && !list_ret)
14670 return 0;
14672 if (want_address == 2 && !have_address
14673 && (dwarf_version >= 4 || !dwarf_strict))
14675 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14677 expansion_failed (loc, NULL_RTX,
14678 "DWARF address size mismatch");
14679 return 0;
14681 if (ret)
14682 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14683 else
14684 add_loc_descr_to_each (list_ret,
14685 new_loc_descr (DW_OP_stack_value, 0, 0));
14686 have_address = 1;
14688 /* Show if we can't fill the request for an address. */
14689 if (want_address && !have_address)
14691 expansion_failed (loc, NULL_RTX,
14692 "Want address and only have value");
14693 return 0;
14696 gcc_assert (!ret || !list_ret);
14698 /* If we've got an address and don't want one, dereference. */
14699 if (!want_address && have_address)
14701 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14703 if (size > DWARF2_ADDR_SIZE || size == -1)
14705 expansion_failed (loc, NULL_RTX,
14706 "DWARF address size mismatch");
14707 return 0;
14709 else if (size == DWARF2_ADDR_SIZE)
14710 op = DW_OP_deref;
14711 else
14712 op = DW_OP_deref_size;
14714 if (ret)
14715 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14716 else
14717 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14719 if (ret)
14720 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14722 return list_ret;
14725 /* Same as above but return only single location expression. */
14726 static dw_loc_descr_ref
14727 loc_descriptor_from_tree (tree loc, int want_address)
14729 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14730 if (!ret)
14731 return NULL;
14732 if (ret->dw_loc_next)
14734 expansion_failed (loc, NULL_RTX,
14735 "Location list where only loc descriptor needed");
14736 return NULL;
14738 return ret->expr;
14741 /* Given a value, round it up to the lowest multiple of `boundary'
14742 which is not less than the value itself. */
14744 static inline HOST_WIDE_INT
14745 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14747 return (((value + boundary - 1) / boundary) * boundary);
14750 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14751 pointer to the declared type for the relevant field variable, or return
14752 `integer_type_node' if the given node turns out to be an
14753 ERROR_MARK node. */
14755 static inline tree
14756 field_type (const_tree decl)
14758 tree type;
14760 if (TREE_CODE (decl) == ERROR_MARK)
14761 return integer_type_node;
14763 type = DECL_BIT_FIELD_TYPE (decl);
14764 if (type == NULL_TREE)
14765 type = TREE_TYPE (decl);
14767 return type;
14770 /* Given a pointer to a tree node, return the alignment in bits for
14771 it, or else return BITS_PER_WORD if the node actually turns out to
14772 be an ERROR_MARK node. */
14774 static inline unsigned
14775 simple_type_align_in_bits (const_tree type)
14777 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14780 static inline unsigned
14781 simple_decl_align_in_bits (const_tree decl)
14783 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14786 /* Return the result of rounding T up to ALIGN. */
14788 static inline offset_int
14789 round_up_to_align (offset_int t, unsigned int align)
14791 t += align - 1;
14792 t = wi::udiv_trunc (t, align);
14793 t *= align;
14794 return t;
14797 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14798 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14799 or return 0 if we are unable to determine what that offset is, either
14800 because the argument turns out to be a pointer to an ERROR_MARK node, or
14801 because the offset is actually variable. (We can't handle the latter case
14802 just yet). */
14804 static HOST_WIDE_INT
14805 field_byte_offset (const_tree decl)
14807 offset_int object_offset_in_bits;
14808 offset_int object_offset_in_bytes;
14809 offset_int bitpos_int;
14811 if (TREE_CODE (decl) == ERROR_MARK)
14812 return 0;
14814 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14816 /* We cannot yet cope with fields whose positions are variable, so
14817 for now, when we see such things, we simply return 0. Someday, we may
14818 be able to handle such cases, but it will be damn difficult. */
14819 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14820 return 0;
14822 bitpos_int = wi::to_offset (bit_position (decl));
14824 #ifdef PCC_BITFIELD_TYPE_MATTERS
14825 if (PCC_BITFIELD_TYPE_MATTERS)
14827 tree type;
14828 tree field_size_tree;
14829 offset_int deepest_bitpos;
14830 offset_int field_size_in_bits;
14831 unsigned int type_align_in_bits;
14832 unsigned int decl_align_in_bits;
14833 offset_int type_size_in_bits;
14835 type = field_type (decl);
14836 type_size_in_bits = offset_int_type_size_in_bits (type);
14837 type_align_in_bits = simple_type_align_in_bits (type);
14839 field_size_tree = DECL_SIZE (decl);
14841 /* The size could be unspecified if there was an error, or for
14842 a flexible array member. */
14843 if (!field_size_tree)
14844 field_size_tree = bitsize_zero_node;
14846 /* If the size of the field is not constant, use the type size. */
14847 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14848 field_size_in_bits = wi::to_offset (field_size_tree);
14849 else
14850 field_size_in_bits = type_size_in_bits;
14852 decl_align_in_bits = simple_decl_align_in_bits (decl);
14854 /* The GCC front-end doesn't make any attempt to keep track of the
14855 starting bit offset (relative to the start of the containing
14856 structure type) of the hypothetical "containing object" for a
14857 bit-field. Thus, when computing the byte offset value for the
14858 start of the "containing object" of a bit-field, we must deduce
14859 this information on our own. This can be rather tricky to do in
14860 some cases. For example, handling the following structure type
14861 definition when compiling for an i386/i486 target (which only
14862 aligns long long's to 32-bit boundaries) can be very tricky:
14864 struct S { int field1; long long field2:31; };
14866 Fortunately, there is a simple rule-of-thumb which can be used
14867 in such cases. When compiling for an i386/i486, GCC will
14868 allocate 8 bytes for the structure shown above. It decides to
14869 do this based upon one simple rule for bit-field allocation.
14870 GCC allocates each "containing object" for each bit-field at
14871 the first (i.e. lowest addressed) legitimate alignment boundary
14872 (based upon the required minimum alignment for the declared
14873 type of the field) which it can possibly use, subject to the
14874 condition that there is still enough available space remaining
14875 in the containing object (when allocated at the selected point)
14876 to fully accommodate all of the bits of the bit-field itself.
14878 This simple rule makes it obvious why GCC allocates 8 bytes for
14879 each object of the structure type shown above. When looking
14880 for a place to allocate the "containing object" for `field2',
14881 the compiler simply tries to allocate a 64-bit "containing
14882 object" at each successive 32-bit boundary (starting at zero)
14883 until it finds a place to allocate that 64- bit field such that
14884 at least 31 contiguous (and previously unallocated) bits remain
14885 within that selected 64 bit field. (As it turns out, for the
14886 example above, the compiler finds it is OK to allocate the
14887 "containing object" 64-bit field at bit-offset zero within the
14888 structure type.)
14890 Here we attempt to work backwards from the limited set of facts
14891 we're given, and we try to deduce from those facts, where GCC
14892 must have believed that the containing object started (within
14893 the structure type). The value we deduce is then used (by the
14894 callers of this routine) to generate DW_AT_location and
14895 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14896 the case of DW_AT_location, regular fields as well). */
14898 /* Figure out the bit-distance from the start of the structure to
14899 the "deepest" bit of the bit-field. */
14900 deepest_bitpos = bitpos_int + field_size_in_bits;
14902 /* This is the tricky part. Use some fancy footwork to deduce
14903 where the lowest addressed bit of the containing object must
14904 be. */
14905 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14907 /* Round up to type_align by default. This works best for
14908 bitfields. */
14909 object_offset_in_bits
14910 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14912 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
14914 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14916 /* Round up to decl_align instead. */
14917 object_offset_in_bits
14918 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14921 else
14922 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14923 object_offset_in_bits = bitpos_int;
14925 object_offset_in_bytes
14926 = wi::udiv_trunc (object_offset_in_bits, BITS_PER_UNIT);
14927 return object_offset_in_bytes.to_shwi ();
14930 /* The following routines define various Dwarf attributes and any data
14931 associated with them. */
14933 /* Add a location description attribute value to a DIE.
14935 This emits location attributes suitable for whole variables and
14936 whole parameters. Note that the location attributes for struct fields are
14937 generated by the routine `data_member_location_attribute' below. */
14939 static inline void
14940 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14941 dw_loc_list_ref descr)
14943 if (descr == 0)
14944 return;
14945 if (single_element_loc_list_p (descr))
14946 add_AT_loc (die, attr_kind, descr->expr);
14947 else
14948 add_AT_loc_list (die, attr_kind, descr);
14951 /* Add DW_AT_accessibility attribute to DIE if needed. */
14953 static void
14954 add_accessibility_attribute (dw_die_ref die, tree decl)
14956 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14957 children, otherwise the default is DW_ACCESS_public. In DWARF2
14958 the default has always been DW_ACCESS_public. */
14959 if (TREE_PROTECTED (decl))
14960 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14961 else if (TREE_PRIVATE (decl))
14963 if (dwarf_version == 2
14964 || die->die_parent == NULL
14965 || die->die_parent->die_tag != DW_TAG_class_type)
14966 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14968 else if (dwarf_version > 2
14969 && die->die_parent
14970 && die->die_parent->die_tag == DW_TAG_class_type)
14971 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14974 /* Attach the specialized form of location attribute used for data members of
14975 struct and union types. In the special case of a FIELD_DECL node which
14976 represents a bit-field, the "offset" part of this special location
14977 descriptor must indicate the distance in bytes from the lowest-addressed
14978 byte of the containing struct or union type to the lowest-addressed byte of
14979 the "containing object" for the bit-field. (See the `field_byte_offset'
14980 function above).
14982 For any given bit-field, the "containing object" is a hypothetical object
14983 (of some integral or enum type) within which the given bit-field lives. The
14984 type of this hypothetical "containing object" is always the same as the
14985 declared type of the individual bit-field itself (for GCC anyway... the
14986 DWARF spec doesn't actually mandate this). Note that it is the size (in
14987 bytes) of the hypothetical "containing object" which will be given in the
14988 DW_AT_byte_size attribute for this bit-field. (See the
14989 `byte_size_attribute' function below.) It is also used when calculating the
14990 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
14991 function below.) */
14993 static void
14994 add_data_member_location_attribute (dw_die_ref die, tree decl)
14996 HOST_WIDE_INT offset;
14997 dw_loc_descr_ref loc_descr = 0;
14999 if (TREE_CODE (decl) == TREE_BINFO)
15001 /* We're working on the TAG_inheritance for a base class. */
15002 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15004 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15005 aren't at a fixed offset from all (sub)objects of the same
15006 type. We need to extract the appropriate offset from our
15007 vtable. The following dwarf expression means
15009 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15011 This is specific to the V3 ABI, of course. */
15013 dw_loc_descr_ref tmp;
15015 /* Make a copy of the object address. */
15016 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15017 add_loc_descr (&loc_descr, tmp);
15019 /* Extract the vtable address. */
15020 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15021 add_loc_descr (&loc_descr, tmp);
15023 /* Calculate the address of the offset. */
15024 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15025 gcc_assert (offset < 0);
15027 tmp = int_loc_descriptor (-offset);
15028 add_loc_descr (&loc_descr, tmp);
15029 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15030 add_loc_descr (&loc_descr, tmp);
15032 /* Extract the offset. */
15033 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15034 add_loc_descr (&loc_descr, tmp);
15036 /* Add it to the object address. */
15037 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15038 add_loc_descr (&loc_descr, tmp);
15040 else
15041 offset = tree_to_shwi (BINFO_OFFSET (decl));
15043 else
15044 offset = field_byte_offset (decl);
15046 if (! loc_descr)
15048 if (dwarf_version > 2)
15050 /* Don't need to output a location expression, just the constant. */
15051 if (offset < 0)
15052 add_AT_int (die, DW_AT_data_member_location, offset);
15053 else
15054 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15055 return;
15057 else
15059 enum dwarf_location_atom op;
15061 /* The DWARF2 standard says that we should assume that the structure
15062 address is already on the stack, so we can specify a structure
15063 field address by using DW_OP_plus_uconst. */
15064 op = DW_OP_plus_uconst;
15065 loc_descr = new_loc_descr (op, offset, 0);
15069 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15072 /* Writes integer values to dw_vec_const array. */
15074 static void
15075 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15077 while (size != 0)
15079 *dest++ = val & 0xff;
15080 val >>= 8;
15081 --size;
15085 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15087 static HOST_WIDE_INT
15088 extract_int (const unsigned char *src, unsigned int size)
15090 HOST_WIDE_INT val = 0;
15092 src += size;
15093 while (size != 0)
15095 val <<= 8;
15096 val |= *--src & 0xff;
15097 --size;
15099 return val;
15102 /* Writes wide_int values to dw_vec_const array. */
15104 static void
15105 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15107 int i;
15109 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15111 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15112 return;
15115 /* We'd have to extend this code to support odd sizes. */
15116 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT) == 0);
15118 int n = elt_size / (HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT);
15120 if (WORDS_BIG_ENDIAN)
15121 for (i = n - 1; i >= 0; i--)
15123 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15124 dest += sizeof (HOST_WIDE_INT);
15126 else
15127 for (i = 0; i < n; i++)
15129 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15130 dest += sizeof (HOST_WIDE_INT);
15134 /* Writes floating point values to dw_vec_const array. */
15136 static void
15137 insert_float (const_rtx rtl, unsigned char *array)
15139 REAL_VALUE_TYPE rv;
15140 long val[4];
15141 int i;
15143 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15144 real_to_target (val, &rv, GET_MODE (rtl));
15146 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15147 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15149 insert_int (val[i], 4, array);
15150 array += 4;
15154 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15155 does not have a "location" either in memory or in a register. These
15156 things can arise in GNU C when a constant is passed as an actual parameter
15157 to an inlined function. They can also arise in C++ where declared
15158 constants do not necessarily get memory "homes". */
15160 static bool
15161 add_const_value_attribute (dw_die_ref die, rtx rtl)
15163 switch (GET_CODE (rtl))
15165 case CONST_INT:
15167 HOST_WIDE_INT val = INTVAL (rtl);
15169 if (val < 0)
15170 add_AT_int (die, DW_AT_const_value, val);
15171 else
15172 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15174 return true;
15176 case CONST_WIDE_INT:
15177 add_AT_wide (die, DW_AT_const_value,
15178 std::make_pair (rtl, GET_MODE (rtl)));
15179 return true;
15181 case CONST_DOUBLE:
15182 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15183 floating-point constant. A CONST_DOUBLE is used whenever the
15184 constant requires more than one word in order to be adequately
15185 represented. */
15187 enum machine_mode mode = GET_MODE (rtl);
15189 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15190 add_AT_double (die, DW_AT_const_value,
15191 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15192 else
15194 unsigned int length = GET_MODE_SIZE (mode);
15195 unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
15197 insert_float (rtl, array);
15198 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15201 return true;
15203 case CONST_VECTOR:
15205 enum machine_mode mode = GET_MODE (rtl);
15206 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15207 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15208 unsigned char *array = (unsigned char *) ggc_alloc_atomic
15209 (length * elt_size);
15210 unsigned int i;
15211 unsigned char *p;
15212 enum machine_mode imode = GET_MODE_INNER (mode);
15214 switch (GET_MODE_CLASS (mode))
15216 case MODE_VECTOR_INT:
15217 for (i = 0, p = array; i < length; i++, p += elt_size)
15219 rtx elt = CONST_VECTOR_ELT (rtl, i);
15220 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15222 break;
15224 case MODE_VECTOR_FLOAT:
15225 for (i = 0, p = array; i < length; i++, p += elt_size)
15227 rtx elt = CONST_VECTOR_ELT (rtl, i);
15228 insert_float (elt, p);
15230 break;
15232 default:
15233 gcc_unreachable ();
15236 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15238 return true;
15240 case CONST_STRING:
15241 if (dwarf_version >= 4 || !dwarf_strict)
15243 dw_loc_descr_ref loc_result;
15244 resolve_one_addr (&rtl, NULL);
15245 rtl_addr:
15246 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15247 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15248 add_AT_loc (die, DW_AT_location, loc_result);
15249 vec_safe_push (used_rtx_array, rtl);
15250 return true;
15252 return false;
15254 case CONST:
15255 if (CONSTANT_P (XEXP (rtl, 0)))
15256 return add_const_value_attribute (die, XEXP (rtl, 0));
15257 /* FALLTHROUGH */
15258 case SYMBOL_REF:
15259 if (!const_ok_for_output (rtl))
15260 return false;
15261 case LABEL_REF:
15262 if (dwarf_version >= 4 || !dwarf_strict)
15263 goto rtl_addr;
15264 return false;
15266 case PLUS:
15267 /* In cases where an inlined instance of an inline function is passed
15268 the address of an `auto' variable (which is local to the caller) we
15269 can get a situation where the DECL_RTL of the artificial local
15270 variable (for the inlining) which acts as a stand-in for the
15271 corresponding formal parameter (of the inline function) will look
15272 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15273 exactly a compile-time constant expression, but it isn't the address
15274 of the (artificial) local variable either. Rather, it represents the
15275 *value* which the artificial local variable always has during its
15276 lifetime. We currently have no way to represent such quasi-constant
15277 values in Dwarf, so for now we just punt and generate nothing. */
15278 return false;
15280 case HIGH:
15281 case CONST_FIXED:
15282 return false;
15284 case MEM:
15285 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15286 && MEM_READONLY_P (rtl)
15287 && GET_MODE (rtl) == BLKmode)
15289 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15290 return true;
15292 return false;
15294 default:
15295 /* No other kinds of rtx should be possible here. */
15296 gcc_unreachable ();
15298 return false;
15301 /* Determine whether the evaluation of EXPR references any variables
15302 or functions which aren't otherwise used (and therefore may not be
15303 output). */
15304 static tree
15305 reference_to_unused (tree * tp, int * walk_subtrees,
15306 void * data ATTRIBUTE_UNUSED)
15308 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15309 *walk_subtrees = 0;
15311 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15312 && ! TREE_ASM_WRITTEN (*tp))
15313 return *tp;
15314 /* ??? The C++ FE emits debug information for using decls, so
15315 putting gcc_unreachable here falls over. See PR31899. For now
15316 be conservative. */
15317 else if (!cgraph_global_info_ready
15318 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15319 return *tp;
15320 else if (TREE_CODE (*tp) == VAR_DECL)
15322 struct varpool_node *node = varpool_get_node (*tp);
15323 if (!node || !node->definition)
15324 return *tp;
15326 else if (TREE_CODE (*tp) == FUNCTION_DECL
15327 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15329 /* The call graph machinery must have finished analyzing,
15330 optimizing and gimplifying the CU by now.
15331 So if *TP has no call graph node associated
15332 to it, it means *TP will not be emitted. */
15333 if (!cgraph_get_node (*tp))
15334 return *tp;
15336 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15337 return *tp;
15339 return NULL_TREE;
15342 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15343 for use in a later add_const_value_attribute call. */
15345 static rtx
15346 rtl_for_decl_init (tree init, tree type)
15348 rtx rtl = NULL_RTX;
15350 STRIP_NOPS (init);
15352 /* If a variable is initialized with a string constant without embedded
15353 zeros, build CONST_STRING. */
15354 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15356 tree enttype = TREE_TYPE (type);
15357 tree domain = TYPE_DOMAIN (type);
15358 enum machine_mode mode = TYPE_MODE (enttype);
15360 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15361 && domain
15362 && integer_zerop (TYPE_MIN_VALUE (domain))
15363 && compare_tree_int (TYPE_MAX_VALUE (domain),
15364 TREE_STRING_LENGTH (init) - 1) == 0
15365 && ((size_t) TREE_STRING_LENGTH (init)
15366 == strlen (TREE_STRING_POINTER (init)) + 1))
15368 rtl = gen_rtx_CONST_STRING (VOIDmode,
15369 ggc_strdup (TREE_STRING_POINTER (init)));
15370 rtl = gen_rtx_MEM (BLKmode, rtl);
15371 MEM_READONLY_P (rtl) = 1;
15374 /* Other aggregates, and complex values, could be represented using
15375 CONCAT: FIXME! */
15376 else if (AGGREGATE_TYPE_P (type)
15377 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15378 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15379 || TREE_CODE (type) == COMPLEX_TYPE)
15381 /* Vectors only work if their mode is supported by the target.
15382 FIXME: generic vectors ought to work too. */
15383 else if (TREE_CODE (type) == VECTOR_TYPE
15384 && !VECTOR_MODE_P (TYPE_MODE (type)))
15386 /* If the initializer is something that we know will expand into an
15387 immediate RTL constant, expand it now. We must be careful not to
15388 reference variables which won't be output. */
15389 else if (initializer_constant_valid_p (init, type)
15390 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15392 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15393 possible. */
15394 if (TREE_CODE (type) == VECTOR_TYPE)
15395 switch (TREE_CODE (init))
15397 case VECTOR_CST:
15398 break;
15399 case CONSTRUCTOR:
15400 if (TREE_CONSTANT (init))
15402 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15403 bool constant_p = true;
15404 tree value;
15405 unsigned HOST_WIDE_INT ix;
15407 /* Even when ctor is constant, it might contain non-*_CST
15408 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15409 belong into VECTOR_CST nodes. */
15410 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15411 if (!CONSTANT_CLASS_P (value))
15413 constant_p = false;
15414 break;
15417 if (constant_p)
15419 init = build_vector_from_ctor (type, elts);
15420 break;
15423 /* FALLTHRU */
15425 default:
15426 return NULL;
15429 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15431 /* If expand_expr returns a MEM, it wasn't immediate. */
15432 gcc_assert (!rtl || !MEM_P (rtl));
15435 return rtl;
15438 /* Generate RTL for the variable DECL to represent its location. */
15440 static rtx
15441 rtl_for_decl_location (tree decl)
15443 rtx rtl;
15445 /* Here we have to decide where we are going to say the parameter "lives"
15446 (as far as the debugger is concerned). We only have a couple of
15447 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15449 DECL_RTL normally indicates where the parameter lives during most of the
15450 activation of the function. If optimization is enabled however, this
15451 could be either NULL or else a pseudo-reg. Both of those cases indicate
15452 that the parameter doesn't really live anywhere (as far as the code
15453 generation parts of GCC are concerned) during most of the function's
15454 activation. That will happen (for example) if the parameter is never
15455 referenced within the function.
15457 We could just generate a location descriptor here for all non-NULL
15458 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15459 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15460 where DECL_RTL is NULL or is a pseudo-reg.
15462 Note however that we can only get away with using DECL_INCOMING_RTL as
15463 a backup substitute for DECL_RTL in certain limited cases. In cases
15464 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15465 we can be sure that the parameter was passed using the same type as it is
15466 declared to have within the function, and that its DECL_INCOMING_RTL
15467 points us to a place where a value of that type is passed.
15469 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15470 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15471 because in these cases DECL_INCOMING_RTL points us to a value of some
15472 type which is *different* from the type of the parameter itself. Thus,
15473 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15474 such cases, the debugger would end up (for example) trying to fetch a
15475 `float' from a place which actually contains the first part of a
15476 `double'. That would lead to really incorrect and confusing
15477 output at debug-time.
15479 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15480 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15481 are a couple of exceptions however. On little-endian machines we can
15482 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15483 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15484 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15485 when (on a little-endian machine) a non-prototyped function has a
15486 parameter declared to be of type `short' or `char'. In such cases,
15487 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15488 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15489 passed `int' value. If the debugger then uses that address to fetch
15490 a `short' or a `char' (on a little-endian machine) the result will be
15491 the correct data, so we allow for such exceptional cases below.
15493 Note that our goal here is to describe the place where the given formal
15494 parameter lives during most of the function's activation (i.e. between the
15495 end of the prologue and the start of the epilogue). We'll do that as best
15496 as we can. Note however that if the given formal parameter is modified
15497 sometime during the execution of the function, then a stack backtrace (at
15498 debug-time) will show the function as having been called with the *new*
15499 value rather than the value which was originally passed in. This happens
15500 rarely enough that it is not a major problem, but it *is* a problem, and
15501 I'd like to fix it.
15503 A future version of dwarf2out.c may generate two additional attributes for
15504 any given DW_TAG_formal_parameter DIE which will describe the "passed
15505 type" and the "passed location" for the given formal parameter in addition
15506 to the attributes we now generate to indicate the "declared type" and the
15507 "active location" for each parameter. This additional set of attributes
15508 could be used by debuggers for stack backtraces. Separately, note that
15509 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15510 This happens (for example) for inlined-instances of inline function formal
15511 parameters which are never referenced. This really shouldn't be
15512 happening. All PARM_DECL nodes should get valid non-NULL
15513 DECL_INCOMING_RTL values. FIXME. */
15515 /* Use DECL_RTL as the "location" unless we find something better. */
15516 rtl = DECL_RTL_IF_SET (decl);
15518 /* When generating abstract instances, ignore everything except
15519 constants, symbols living in memory, and symbols living in
15520 fixed registers. */
15521 if (! reload_completed)
15523 if (rtl
15524 && (CONSTANT_P (rtl)
15525 || (MEM_P (rtl)
15526 && CONSTANT_P (XEXP (rtl, 0)))
15527 || (REG_P (rtl)
15528 && TREE_CODE (decl) == VAR_DECL
15529 && TREE_STATIC (decl))))
15531 rtl = targetm.delegitimize_address (rtl);
15532 return rtl;
15534 rtl = NULL_RTX;
15536 else if (TREE_CODE (decl) == PARM_DECL)
15538 if (rtl == NULL_RTX
15539 || is_pseudo_reg (rtl)
15540 || (MEM_P (rtl)
15541 && is_pseudo_reg (XEXP (rtl, 0))
15542 && DECL_INCOMING_RTL (decl)
15543 && MEM_P (DECL_INCOMING_RTL (decl))
15544 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15546 tree declared_type = TREE_TYPE (decl);
15547 tree passed_type = DECL_ARG_TYPE (decl);
15548 enum machine_mode dmode = TYPE_MODE (declared_type);
15549 enum machine_mode pmode = TYPE_MODE (passed_type);
15551 /* This decl represents a formal parameter which was optimized out.
15552 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15553 all cases where (rtl == NULL_RTX) just below. */
15554 if (dmode == pmode)
15555 rtl = DECL_INCOMING_RTL (decl);
15556 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15557 && SCALAR_INT_MODE_P (dmode)
15558 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15559 && DECL_INCOMING_RTL (decl))
15561 rtx inc = DECL_INCOMING_RTL (decl);
15562 if (REG_P (inc))
15563 rtl = inc;
15564 else if (MEM_P (inc))
15566 if (BYTES_BIG_ENDIAN)
15567 rtl = adjust_address_nv (inc, dmode,
15568 GET_MODE_SIZE (pmode)
15569 - GET_MODE_SIZE (dmode));
15570 else
15571 rtl = inc;
15576 /* If the parm was passed in registers, but lives on the stack, then
15577 make a big endian correction if the mode of the type of the
15578 parameter is not the same as the mode of the rtl. */
15579 /* ??? This is the same series of checks that are made in dbxout.c before
15580 we reach the big endian correction code there. It isn't clear if all
15581 of these checks are necessary here, but keeping them all is the safe
15582 thing to do. */
15583 else if (MEM_P (rtl)
15584 && XEXP (rtl, 0) != const0_rtx
15585 && ! CONSTANT_P (XEXP (rtl, 0))
15586 /* Not passed in memory. */
15587 && !MEM_P (DECL_INCOMING_RTL (decl))
15588 /* Not passed by invisible reference. */
15589 && (!REG_P (XEXP (rtl, 0))
15590 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15591 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15592 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15593 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15594 #endif
15596 /* Big endian correction check. */
15597 && BYTES_BIG_ENDIAN
15598 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15599 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15600 < UNITS_PER_WORD))
15602 enum machine_mode addr_mode = get_address_mode (rtl);
15603 int offset = (UNITS_PER_WORD
15604 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15606 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15607 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15610 else if (TREE_CODE (decl) == VAR_DECL
15611 && rtl
15612 && MEM_P (rtl)
15613 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15614 && BYTES_BIG_ENDIAN)
15616 enum machine_mode addr_mode = get_address_mode (rtl);
15617 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15618 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15620 /* If a variable is declared "register" yet is smaller than
15621 a register, then if we store the variable to memory, it
15622 looks like we're storing a register-sized value, when in
15623 fact we are not. We need to adjust the offset of the
15624 storage location to reflect the actual value's bytes,
15625 else gdb will not be able to display it. */
15626 if (rsize > dsize)
15627 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15628 plus_constant (addr_mode, XEXP (rtl, 0),
15629 rsize - dsize));
15632 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15633 and will have been substituted directly into all expressions that use it.
15634 C does not have such a concept, but C++ and other languages do. */
15635 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15636 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15638 if (rtl)
15639 rtl = targetm.delegitimize_address (rtl);
15641 /* If we don't look past the constant pool, we risk emitting a
15642 reference to a constant pool entry that isn't referenced from
15643 code, and thus is not emitted. */
15644 if (rtl)
15645 rtl = avoid_constant_pool_reference (rtl);
15647 /* Try harder to get a rtl. If this symbol ends up not being emitted
15648 in the current CU, resolve_addr will remove the expression referencing
15649 it. */
15650 if (rtl == NULL_RTX
15651 && TREE_CODE (decl) == VAR_DECL
15652 && !DECL_EXTERNAL (decl)
15653 && TREE_STATIC (decl)
15654 && DECL_NAME (decl)
15655 && !DECL_HARD_REGISTER (decl)
15656 && DECL_MODE (decl) != VOIDmode)
15658 rtl = make_decl_rtl_for_debug (decl);
15659 if (!MEM_P (rtl)
15660 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15661 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15662 rtl = NULL_RTX;
15665 return rtl;
15668 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15669 returned. If so, the decl for the COMMON block is returned, and the
15670 value is the offset into the common block for the symbol. */
15672 static tree
15673 fortran_common (tree decl, HOST_WIDE_INT *value)
15675 tree val_expr, cvar;
15676 enum machine_mode mode;
15677 HOST_WIDE_INT bitsize, bitpos;
15678 tree offset;
15679 int unsignedp, volatilep = 0;
15681 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15682 it does not have a value (the offset into the common area), or if it
15683 is thread local (as opposed to global) then it isn't common, and shouldn't
15684 be handled as such. */
15685 if (TREE_CODE (decl) != VAR_DECL
15686 || !TREE_STATIC (decl)
15687 || !DECL_HAS_VALUE_EXPR_P (decl)
15688 || !is_fortran ())
15689 return NULL_TREE;
15691 val_expr = DECL_VALUE_EXPR (decl);
15692 if (TREE_CODE (val_expr) != COMPONENT_REF)
15693 return NULL_TREE;
15695 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15696 &mode, &unsignedp, &volatilep, true);
15698 if (cvar == NULL_TREE
15699 || TREE_CODE (cvar) != VAR_DECL
15700 || DECL_ARTIFICIAL (cvar)
15701 || !TREE_PUBLIC (cvar))
15702 return NULL_TREE;
15704 *value = 0;
15705 if (offset != NULL)
15707 if (!tree_fits_shwi_p (offset))
15708 return NULL_TREE;
15709 *value = tree_to_shwi (offset);
15711 if (bitpos != 0)
15712 *value += bitpos / BITS_PER_UNIT;
15714 return cvar;
15717 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15718 data attribute for a variable or a parameter. We generate the
15719 DW_AT_const_value attribute only in those cases where the given variable
15720 or parameter does not have a true "location" either in memory or in a
15721 register. This can happen (for example) when a constant is passed as an
15722 actual argument in a call to an inline function. (It's possible that
15723 these things can crop up in other ways also.) Note that one type of
15724 constant value which can be passed into an inlined function is a constant
15725 pointer. This can happen for example if an actual argument in an inlined
15726 function call evaluates to a compile-time constant address.
15728 CACHE_P is true if it is worth caching the location list for DECL,
15729 so that future calls can reuse it rather than regenerate it from scratch.
15730 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15731 since we will need to refer to them each time the function is inlined. */
15733 static bool
15734 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15735 enum dwarf_attribute attr)
15737 rtx rtl;
15738 dw_loc_list_ref list;
15739 var_loc_list *loc_list;
15740 cached_dw_loc_list *cache;
15741 void **slot;
15743 if (TREE_CODE (decl) == ERROR_MARK)
15744 return false;
15746 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15747 || TREE_CODE (decl) == RESULT_DECL);
15749 /* Try to get some constant RTL for this decl, and use that as the value of
15750 the location. */
15752 rtl = rtl_for_decl_location (decl);
15753 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15754 && add_const_value_attribute (die, rtl))
15755 return true;
15757 /* See if we have single element location list that is equivalent to
15758 a constant value. That way we are better to use add_const_value_attribute
15759 rather than expanding constant value equivalent. */
15760 loc_list = lookup_decl_loc (decl);
15761 if (loc_list
15762 && loc_list->first
15763 && loc_list->first->next == NULL
15764 && NOTE_P (loc_list->first->loc)
15765 && NOTE_VAR_LOCATION (loc_list->first->loc)
15766 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15768 struct var_loc_node *node;
15770 node = loc_list->first;
15771 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15772 if (GET_CODE (rtl) == EXPR_LIST)
15773 rtl = XEXP (rtl, 0);
15774 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15775 && add_const_value_attribute (die, rtl))
15776 return true;
15778 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15779 list several times. See if we've already cached the contents. */
15780 list = NULL;
15781 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15782 cache_p = false;
15783 if (cache_p)
15785 cache = (cached_dw_loc_list *)
15786 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15787 if (cache)
15788 list = cache->loc_list;
15790 if (list == NULL)
15792 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15793 /* It is usually worth caching this result if the decl is from
15794 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15795 if (cache_p && list && list->dw_loc_next)
15797 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15798 DECL_UID (decl), INSERT);
15799 cache = ggc_alloc_cleared_cached_dw_loc_list ();
15800 cache->decl_id = DECL_UID (decl);
15801 cache->loc_list = list;
15802 *slot = cache;
15805 if (list)
15807 add_AT_location_description (die, attr, list);
15808 return true;
15810 /* None of that worked, so it must not really have a location;
15811 try adding a constant value attribute from the DECL_INITIAL. */
15812 return tree_add_const_value_attribute_for_decl (die, decl);
15815 /* Add VARIABLE and DIE into deferred locations list. */
15817 static void
15818 defer_location (tree variable, dw_die_ref die)
15820 deferred_locations entry;
15821 entry.variable = variable;
15822 entry.die = die;
15823 vec_safe_push (deferred_locations_list, entry);
15826 /* Helper function for tree_add_const_value_attribute. Natively encode
15827 initializer INIT into an array. Return true if successful. */
15829 static bool
15830 native_encode_initializer (tree init, unsigned char *array, int size)
15832 tree type;
15834 if (init == NULL_TREE)
15835 return false;
15837 STRIP_NOPS (init);
15838 switch (TREE_CODE (init))
15840 case STRING_CST:
15841 type = TREE_TYPE (init);
15842 if (TREE_CODE (type) == ARRAY_TYPE)
15844 tree enttype = TREE_TYPE (type);
15845 enum machine_mode mode = TYPE_MODE (enttype);
15847 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15848 return false;
15849 if (int_size_in_bytes (type) != size)
15850 return false;
15851 if (size > TREE_STRING_LENGTH (init))
15853 memcpy (array, TREE_STRING_POINTER (init),
15854 TREE_STRING_LENGTH (init));
15855 memset (array + TREE_STRING_LENGTH (init),
15856 '\0', size - TREE_STRING_LENGTH (init));
15858 else
15859 memcpy (array, TREE_STRING_POINTER (init), size);
15860 return true;
15862 return false;
15863 case CONSTRUCTOR:
15864 type = TREE_TYPE (init);
15865 if (int_size_in_bytes (type) != size)
15866 return false;
15867 if (TREE_CODE (type) == ARRAY_TYPE)
15869 HOST_WIDE_INT min_index;
15870 unsigned HOST_WIDE_INT cnt;
15871 int curpos = 0, fieldsize;
15872 constructor_elt *ce;
15874 if (TYPE_DOMAIN (type) == NULL_TREE
15875 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15876 return false;
15878 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15879 if (fieldsize <= 0)
15880 return false;
15882 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15883 memset (array, '\0', size);
15884 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15886 tree val = ce->value;
15887 tree index = ce->index;
15888 int pos = curpos;
15889 if (index && TREE_CODE (index) == RANGE_EXPR)
15890 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15891 * fieldsize;
15892 else if (index)
15893 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15895 if (val)
15897 STRIP_NOPS (val);
15898 if (!native_encode_initializer (val, array + pos, fieldsize))
15899 return false;
15901 curpos = pos + fieldsize;
15902 if (index && TREE_CODE (index) == RANGE_EXPR)
15904 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15905 - tree_to_shwi (TREE_OPERAND (index, 0));
15906 while (count-- > 0)
15908 if (val)
15909 memcpy (array + curpos, array + pos, fieldsize);
15910 curpos += fieldsize;
15913 gcc_assert (curpos <= size);
15915 return true;
15917 else if (TREE_CODE (type) == RECORD_TYPE
15918 || TREE_CODE (type) == UNION_TYPE)
15920 tree field = NULL_TREE;
15921 unsigned HOST_WIDE_INT cnt;
15922 constructor_elt *ce;
15924 if (int_size_in_bytes (type) != size)
15925 return false;
15927 if (TREE_CODE (type) == RECORD_TYPE)
15928 field = TYPE_FIELDS (type);
15930 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15932 tree val = ce->value;
15933 int pos, fieldsize;
15935 if (ce->index != 0)
15936 field = ce->index;
15938 if (val)
15939 STRIP_NOPS (val);
15941 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15942 return false;
15944 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15945 && TYPE_DOMAIN (TREE_TYPE (field))
15946 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15947 return false;
15948 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15949 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15950 return false;
15951 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15952 pos = int_byte_position (field);
15953 gcc_assert (pos + fieldsize <= size);
15954 if (val
15955 && !native_encode_initializer (val, array + pos, fieldsize))
15956 return false;
15958 return true;
15960 return false;
15961 case VIEW_CONVERT_EXPR:
15962 case NON_LVALUE_EXPR:
15963 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15964 default:
15965 return native_encode_expr (init, array, size) == size;
15969 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15970 attribute is the const value T. */
15972 static bool
15973 tree_add_const_value_attribute (dw_die_ref die, tree t)
15975 tree init;
15976 tree type = TREE_TYPE (t);
15977 rtx rtl;
15979 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15980 return false;
15982 init = t;
15983 gcc_assert (!DECL_P (init));
15985 rtl = rtl_for_decl_init (init, type);
15986 if (rtl)
15987 return add_const_value_attribute (die, rtl);
15988 /* If the host and target are sane, try harder. */
15989 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15990 && initializer_constant_valid_p (init, type))
15992 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15993 if (size > 0 && (int) size == size)
15995 unsigned char *array = (unsigned char *)
15996 ggc_alloc_cleared_atomic (size);
15998 if (native_encode_initializer (init, array, size))
16000 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16001 return true;
16003 ggc_free (array);
16006 return false;
16009 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16010 attribute is the const value of T, where T is an integral constant
16011 variable with static storage duration
16012 (so it can't be a PARM_DECL or a RESULT_DECL). */
16014 static bool
16015 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16018 if (!decl
16019 || (TREE_CODE (decl) != VAR_DECL
16020 && TREE_CODE (decl) != CONST_DECL)
16021 || (TREE_CODE (decl) == VAR_DECL
16022 && !TREE_STATIC (decl)))
16023 return false;
16025 if (TREE_READONLY (decl)
16026 && ! TREE_THIS_VOLATILE (decl)
16027 && DECL_INITIAL (decl))
16028 /* OK */;
16029 else
16030 return false;
16032 /* Don't add DW_AT_const_value if abstract origin already has one. */
16033 if (get_AT (var_die, DW_AT_const_value))
16034 return false;
16036 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16039 /* Convert the CFI instructions for the current function into a
16040 location list. This is used for DW_AT_frame_base when we targeting
16041 a dwarf2 consumer that does not support the dwarf3
16042 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16043 expressions. */
16045 static dw_loc_list_ref
16046 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16048 int ix;
16049 dw_fde_ref fde;
16050 dw_loc_list_ref list, *list_tail;
16051 dw_cfi_ref cfi;
16052 dw_cfa_location last_cfa, next_cfa;
16053 const char *start_label, *last_label, *section;
16054 dw_cfa_location remember;
16056 fde = cfun->fde;
16057 gcc_assert (fde != NULL);
16059 section = secname_for_decl (current_function_decl);
16060 list_tail = &list;
16061 list = NULL;
16063 memset (&next_cfa, 0, sizeof (next_cfa));
16064 next_cfa.reg = INVALID_REGNUM;
16065 remember = next_cfa;
16067 start_label = fde->dw_fde_begin;
16069 /* ??? Bald assumption that the CIE opcode list does not contain
16070 advance opcodes. */
16071 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16072 lookup_cfa_1 (cfi, &next_cfa, &remember);
16074 last_cfa = next_cfa;
16075 last_label = start_label;
16077 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16079 /* If the first partition contained no CFI adjustments, the
16080 CIE opcodes apply to the whole first partition. */
16081 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16082 fde->dw_fde_begin, fde->dw_fde_end, section);
16083 list_tail =&(*list_tail)->dw_loc_next;
16084 start_label = last_label = fde->dw_fde_second_begin;
16087 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16089 switch (cfi->dw_cfi_opc)
16091 case DW_CFA_set_loc:
16092 case DW_CFA_advance_loc1:
16093 case DW_CFA_advance_loc2:
16094 case DW_CFA_advance_loc4:
16095 if (!cfa_equal_p (&last_cfa, &next_cfa))
16097 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16098 start_label, last_label, section);
16100 list_tail = &(*list_tail)->dw_loc_next;
16101 last_cfa = next_cfa;
16102 start_label = last_label;
16104 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16105 break;
16107 case DW_CFA_advance_loc:
16108 /* The encoding is complex enough that we should never emit this. */
16109 gcc_unreachable ();
16111 default:
16112 lookup_cfa_1 (cfi, &next_cfa, &remember);
16113 break;
16115 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16117 if (!cfa_equal_p (&last_cfa, &next_cfa))
16119 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16120 start_label, last_label, section);
16122 list_tail = &(*list_tail)->dw_loc_next;
16123 last_cfa = next_cfa;
16124 start_label = last_label;
16126 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16127 start_label, fde->dw_fde_end, section);
16128 list_tail = &(*list_tail)->dw_loc_next;
16129 start_label = last_label = fde->dw_fde_second_begin;
16133 if (!cfa_equal_p (&last_cfa, &next_cfa))
16135 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16136 start_label, last_label, section);
16137 list_tail = &(*list_tail)->dw_loc_next;
16138 start_label = last_label;
16141 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16142 start_label,
16143 fde->dw_fde_second_begin
16144 ? fde->dw_fde_second_end : fde->dw_fde_end,
16145 section);
16147 if (list && list->dw_loc_next)
16148 gen_llsym (list);
16150 return list;
16153 /* Compute a displacement from the "steady-state frame pointer" to the
16154 frame base (often the same as the CFA), and store it in
16155 frame_pointer_fb_offset. OFFSET is added to the displacement
16156 before the latter is negated. */
16158 static void
16159 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16161 rtx reg, elim;
16163 #ifdef FRAME_POINTER_CFA_OFFSET
16164 reg = frame_pointer_rtx;
16165 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16166 #else
16167 reg = arg_pointer_rtx;
16168 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16169 #endif
16171 elim = (ira_use_lra_p
16172 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16173 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16174 if (GET_CODE (elim) == PLUS)
16176 offset += INTVAL (XEXP (elim, 1));
16177 elim = XEXP (elim, 0);
16180 frame_pointer_fb_offset = -offset;
16182 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16183 in which to eliminate. This is because it's stack pointer isn't
16184 directly accessible as a register within the ISA. To work around
16185 this, assume that while we cannot provide a proper value for
16186 frame_pointer_fb_offset, we won't need one either. */
16187 frame_pointer_fb_offset_valid
16188 = ((SUPPORTS_STACK_ALIGNMENT
16189 && (elim == hard_frame_pointer_rtx
16190 || elim == stack_pointer_rtx))
16191 || elim == (frame_pointer_needed
16192 ? hard_frame_pointer_rtx
16193 : stack_pointer_rtx));
16196 /* Generate a DW_AT_name attribute given some string value to be included as
16197 the value of the attribute. */
16199 static void
16200 add_name_attribute (dw_die_ref die, const char *name_string)
16202 if (name_string != NULL && *name_string != 0)
16204 if (demangle_name_func)
16205 name_string = (*demangle_name_func) (name_string);
16207 add_AT_string (die, DW_AT_name, name_string);
16211 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16212 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16213 of TYPE accordingly.
16215 ??? This is a temporary measure until after we're able to generate
16216 regular DWARF for the complex Ada type system. */
16218 static void
16219 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16220 dw_die_ref context_die)
16222 tree dtype;
16223 dw_die_ref dtype_die;
16225 if (!lang_hooks.types.descriptive_type)
16226 return;
16228 dtype = lang_hooks.types.descriptive_type (type);
16229 if (!dtype)
16230 return;
16232 dtype_die = lookup_type_die (dtype);
16233 if (!dtype_die)
16235 gen_type_die (dtype, context_die);
16236 dtype_die = lookup_type_die (dtype);
16237 gcc_assert (dtype_die);
16240 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16243 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16245 static const char *
16246 comp_dir_string (void)
16248 const char *wd;
16249 char *wd1;
16250 static const char *cached_wd = NULL;
16252 if (cached_wd != NULL)
16253 return cached_wd;
16255 wd = get_src_pwd ();
16256 if (wd == NULL)
16257 return NULL;
16259 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16261 int wdlen;
16263 wdlen = strlen (wd);
16264 wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
16265 strcpy (wd1, wd);
16266 wd1 [wdlen] = DIR_SEPARATOR;
16267 wd1 [wdlen + 1] = 0;
16268 wd = wd1;
16271 cached_wd = remap_debug_filename (wd);
16272 return cached_wd;
16275 /* Generate a DW_AT_comp_dir attribute for DIE. */
16277 static void
16278 add_comp_dir_attribute (dw_die_ref die)
16280 const char * wd = comp_dir_string ();
16281 if (wd != NULL)
16282 add_AT_string (die, DW_AT_comp_dir, wd);
16285 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16286 default. */
16288 static int
16289 lower_bound_default (void)
16291 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16293 case DW_LANG_C:
16294 case DW_LANG_C89:
16295 case DW_LANG_C99:
16296 case DW_LANG_C_plus_plus:
16297 case DW_LANG_ObjC:
16298 case DW_LANG_ObjC_plus_plus:
16299 case DW_LANG_Java:
16300 return 0;
16301 case DW_LANG_Fortran77:
16302 case DW_LANG_Fortran90:
16303 case DW_LANG_Fortran95:
16304 return 1;
16305 case DW_LANG_UPC:
16306 case DW_LANG_D:
16307 case DW_LANG_Python:
16308 return dwarf_version >= 4 ? 0 : -1;
16309 case DW_LANG_Ada95:
16310 case DW_LANG_Ada83:
16311 case DW_LANG_Cobol74:
16312 case DW_LANG_Cobol85:
16313 case DW_LANG_Pascal83:
16314 case DW_LANG_Modula2:
16315 case DW_LANG_PLI:
16316 return dwarf_version >= 4 ? 1 : -1;
16317 default:
16318 return -1;
16322 /* Given a tree node describing an array bound (either lower or upper) output
16323 a representation for that bound. */
16325 static void
16326 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16328 switch (TREE_CODE (bound))
16330 case ERROR_MARK:
16331 return;
16333 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16334 case INTEGER_CST:
16336 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16337 int dflt;
16339 /* Use the default if possible. */
16340 if (bound_attr == DW_AT_lower_bound
16341 && tree_fits_shwi_p (bound)
16342 && (dflt = lower_bound_default ()) != -1
16343 && tree_to_shwi (bound) == dflt)
16346 /* Otherwise represent the bound as an unsigned value with the
16347 precision of its type. The precision and signedness of the
16348 type will be necessary to re-interpret it unambiguously. */
16349 else if (prec < HOST_BITS_PER_WIDE_INT)
16351 unsigned HOST_WIDE_INT mask
16352 = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
16353 add_AT_unsigned (subrange_die, bound_attr,
16354 TREE_INT_CST_LOW (bound) & mask);
16356 else if (prec == HOST_BITS_PER_WIDE_INT || tree_fits_uhwi_p (bound))
16357 add_AT_unsigned (subrange_die, bound_attr,
16358 TREE_INT_CST_LOW (bound));
16359 else
16360 add_AT_wide (subrange_die, bound_attr, bound);
16362 break;
16364 CASE_CONVERT:
16365 case VIEW_CONVERT_EXPR:
16366 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16367 break;
16369 case SAVE_EXPR:
16370 break;
16372 case VAR_DECL:
16373 case PARM_DECL:
16374 case RESULT_DECL:
16376 dw_die_ref decl_die = lookup_decl_die (bound);
16378 /* ??? Can this happen, or should the variable have been bound
16379 first? Probably it can, since I imagine that we try to create
16380 the types of parameters in the order in which they exist in
16381 the list, and won't have created a forward reference to a
16382 later parameter. */
16383 if (decl_die != NULL)
16385 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16386 break;
16389 /* FALLTHRU */
16391 default:
16393 /* Otherwise try to create a stack operation procedure to
16394 evaluate the value of the array bound. */
16396 dw_die_ref ctx, decl_die;
16397 dw_loc_list_ref list;
16399 list = loc_list_from_tree (bound, 2);
16400 if (list == NULL || single_element_loc_list_p (list))
16402 /* If DW_AT_*bound is not a reference nor constant, it is
16403 a DWARF expression rather than location description.
16404 For that loc_list_from_tree (bound, 0) is needed.
16405 If that fails to give a single element list,
16406 fall back to outputting this as a reference anyway. */
16407 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16408 if (list2 && single_element_loc_list_p (list2))
16410 add_AT_loc (subrange_die, bound_attr, list2->expr);
16411 break;
16414 if (list == NULL)
16415 break;
16417 if (current_function_decl == 0)
16418 ctx = comp_unit_die ();
16419 else
16420 ctx = lookup_decl_die (current_function_decl);
16422 decl_die = new_die (DW_TAG_variable, ctx, bound);
16423 add_AT_flag (decl_die, DW_AT_artificial, 1);
16424 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16425 add_AT_location_description (decl_die, DW_AT_location, list);
16426 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16427 break;
16432 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16433 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16434 Note that the block of subscript information for an array type also
16435 includes information about the element type of the given array type. */
16437 static void
16438 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16440 unsigned dimension_number;
16441 tree lower, upper;
16442 dw_die_ref subrange_die;
16444 for (dimension_number = 0;
16445 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16446 type = TREE_TYPE (type), dimension_number++)
16448 tree domain = TYPE_DOMAIN (type);
16450 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16451 break;
16453 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16454 and (in GNU C only) variable bounds. Handle all three forms
16455 here. */
16456 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16457 if (domain)
16459 /* We have an array type with specified bounds. */
16460 lower = TYPE_MIN_VALUE (domain);
16461 upper = TYPE_MAX_VALUE (domain);
16463 /* Define the index type. */
16464 if (TREE_TYPE (domain))
16466 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16467 TREE_TYPE field. We can't emit debug info for this
16468 because it is an unnamed integral type. */
16469 if (TREE_CODE (domain) == INTEGER_TYPE
16470 && TYPE_NAME (domain) == NULL_TREE
16471 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16472 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16474 else
16475 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16476 type_die);
16479 /* ??? If upper is NULL, the array has unspecified length,
16480 but it does have a lower bound. This happens with Fortran
16481 dimension arr(N:*)
16482 Since the debugger is definitely going to need to know N
16483 to produce useful results, go ahead and output the lower
16484 bound solo, and hope the debugger can cope. */
16486 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16487 if (upper)
16488 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16491 /* Otherwise we have an array type with an unspecified length. The
16492 DWARF-2 spec does not say how to handle this; let's just leave out the
16493 bounds. */
16497 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16499 static void
16500 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16502 dw_die_ref decl_die;
16503 HOST_WIDE_INT size;
16505 switch (TREE_CODE (tree_node))
16507 case ERROR_MARK:
16508 size = 0;
16509 break;
16510 case ENUMERAL_TYPE:
16511 case RECORD_TYPE:
16512 case UNION_TYPE:
16513 case QUAL_UNION_TYPE:
16514 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16515 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16517 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16518 return;
16520 size = int_size_in_bytes (tree_node);
16521 break;
16522 case FIELD_DECL:
16523 /* For a data member of a struct or union, the DW_AT_byte_size is
16524 generally given as the number of bytes normally allocated for an
16525 object of the *declared* type of the member itself. This is true
16526 even for bit-fields. */
16527 size = int_size_in_bytes (field_type (tree_node));
16528 break;
16529 default:
16530 gcc_unreachable ();
16533 /* Note that `size' might be -1 when we get to this point. If it is, that
16534 indicates that the byte size of the entity in question is variable. We
16535 have no good way of expressing this fact in Dwarf at the present time,
16536 when location description was not used by the caller code instead. */
16537 if (size >= 0)
16538 add_AT_unsigned (die, DW_AT_byte_size, size);
16541 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16542 which specifies the distance in bits from the highest order bit of the
16543 "containing object" for the bit-field to the highest order bit of the
16544 bit-field itself.
16546 For any given bit-field, the "containing object" is a hypothetical object
16547 (of some integral or enum type) within which the given bit-field lives. The
16548 type of this hypothetical "containing object" is always the same as the
16549 declared type of the individual bit-field itself. The determination of the
16550 exact location of the "containing object" for a bit-field is rather
16551 complicated. It's handled by the `field_byte_offset' function (above).
16553 Note that it is the size (in bytes) of the hypothetical "containing object"
16554 which will be given in the DW_AT_byte_size attribute for this bit-field.
16555 (See `byte_size_attribute' above). */
16557 static inline void
16558 add_bit_offset_attribute (dw_die_ref die, tree decl)
16560 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16561 tree type = DECL_BIT_FIELD_TYPE (decl);
16562 HOST_WIDE_INT bitpos_int;
16563 HOST_WIDE_INT highest_order_object_bit_offset;
16564 HOST_WIDE_INT highest_order_field_bit_offset;
16565 HOST_WIDE_INT bit_offset;
16567 /* Must be a field and a bit field. */
16568 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16570 /* We can't yet handle bit-fields whose offsets are variable, so if we
16571 encounter such things, just return without generating any attribute
16572 whatsoever. Likewise for variable or too large size. */
16573 if (! tree_fits_shwi_p (bit_position (decl))
16574 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16575 return;
16577 bitpos_int = int_bit_position (decl);
16579 /* Note that the bit offset is always the distance (in bits) from the
16580 highest-order bit of the "containing object" to the highest-order bit of
16581 the bit-field itself. Since the "high-order end" of any object or field
16582 is different on big-endian and little-endian machines, the computation
16583 below must take account of these differences. */
16584 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16585 highest_order_field_bit_offset = bitpos_int;
16587 if (! BYTES_BIG_ENDIAN)
16589 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16590 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16593 bit_offset
16594 = (! BYTES_BIG_ENDIAN
16595 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16596 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16598 if (bit_offset < 0)
16599 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16600 else
16601 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16604 /* For a FIELD_DECL node which represents a bit field, output an attribute
16605 which specifies the length in bits of the given field. */
16607 static inline void
16608 add_bit_size_attribute (dw_die_ref die, tree decl)
16610 /* Must be a field and a bit field. */
16611 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16612 && DECL_BIT_FIELD_TYPE (decl));
16614 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16615 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16618 /* If the compiled language is ANSI C, then add a 'prototyped'
16619 attribute, if arg types are given for the parameters of a function. */
16621 static inline void
16622 add_prototyped_attribute (dw_die_ref die, tree func_type)
16624 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16625 && prototype_p (func_type))
16626 add_AT_flag (die, DW_AT_prototyped, 1);
16629 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16630 by looking in either the type declaration or object declaration
16631 equate table. */
16633 static inline dw_die_ref
16634 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16636 dw_die_ref origin_die = NULL;
16638 if (TREE_CODE (origin) != FUNCTION_DECL)
16640 /* We may have gotten separated from the block for the inlined
16641 function, if we're in an exception handler or some such; make
16642 sure that the abstract function has been written out.
16644 Doing this for nested functions is wrong, however; functions are
16645 distinct units, and our context might not even be inline. */
16646 tree fn = origin;
16648 if (TYPE_P (fn))
16649 fn = TYPE_STUB_DECL (fn);
16651 fn = decl_function_context (fn);
16652 if (fn)
16653 dwarf2out_abstract_function (fn);
16656 if (DECL_P (origin))
16657 origin_die = lookup_decl_die (origin);
16658 else if (TYPE_P (origin))
16659 origin_die = lookup_type_die (origin);
16661 /* XXX: Functions that are never lowered don't always have correct block
16662 trees (in the case of java, they simply have no block tree, in some other
16663 languages). For these functions, there is nothing we can really do to
16664 output correct debug info for inlined functions in all cases. Rather
16665 than die, we'll just produce deficient debug info now, in that we will
16666 have variables without a proper abstract origin. In the future, when all
16667 functions are lowered, we should re-add a gcc_assert (origin_die)
16668 here. */
16670 if (origin_die)
16671 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16672 return origin_die;
16675 /* We do not currently support the pure_virtual attribute. */
16677 static inline void
16678 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16680 if (DECL_VINDEX (func_decl))
16682 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16684 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16685 add_AT_loc (die, DW_AT_vtable_elem_location,
16686 new_loc_descr (DW_OP_constu,
16687 tree_to_shwi (DECL_VINDEX (func_decl)),
16688 0));
16690 /* GNU extension: Record what type this method came from originally. */
16691 if (debug_info_level > DINFO_LEVEL_TERSE
16692 && DECL_CONTEXT (func_decl))
16693 add_AT_die_ref (die, DW_AT_containing_type,
16694 lookup_type_die (DECL_CONTEXT (func_decl)));
16698 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16699 given decl. This used to be a vendor extension until after DWARF 4
16700 standardized it. */
16702 static void
16703 add_linkage_attr (dw_die_ref die, tree decl)
16705 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16707 /* Mimic what assemble_name_raw does with a leading '*'. */
16708 if (name[0] == '*')
16709 name = &name[1];
16711 if (dwarf_version >= 4)
16712 add_AT_string (die, DW_AT_linkage_name, name);
16713 else
16714 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16717 /* Add source coordinate attributes for the given decl. */
16719 static void
16720 add_src_coords_attributes (dw_die_ref die, tree decl)
16722 expanded_location s;
16724 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16725 return;
16726 s = expand_location (DECL_SOURCE_LOCATION (decl));
16727 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16728 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16731 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16733 static void
16734 add_linkage_name (dw_die_ref die, tree decl)
16736 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16737 && TREE_PUBLIC (decl)
16738 && !DECL_ABSTRACT (decl)
16739 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16740 && die->die_tag != DW_TAG_member)
16742 /* Defer until we have an assembler name set. */
16743 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16745 limbo_die_node *asm_name;
16747 asm_name = ggc_alloc_cleared_limbo_die_node ();
16748 asm_name->die = die;
16749 asm_name->created_for = decl;
16750 asm_name->next = deferred_asm_name;
16751 deferred_asm_name = asm_name;
16753 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16754 add_linkage_attr (die, decl);
16758 /* Add a DW_AT_name attribute and source coordinate attribute for the
16759 given decl, but only if it actually has a name. */
16761 static void
16762 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16764 tree decl_name;
16766 decl_name = DECL_NAME (decl);
16767 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16769 const char *name = dwarf2_name (decl, 0);
16770 if (name)
16771 add_name_attribute (die, name);
16772 if (! DECL_ARTIFICIAL (decl))
16773 add_src_coords_attributes (die, decl);
16775 add_linkage_name (die, decl);
16778 #ifdef VMS_DEBUGGING_INFO
16779 /* Get the function's name, as described by its RTL. This may be different
16780 from the DECL_NAME name used in the source file. */
16781 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16783 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16784 XEXP (DECL_RTL (decl), 0), false);
16785 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16787 #endif /* VMS_DEBUGGING_INFO */
16790 #ifdef VMS_DEBUGGING_INFO
16791 /* Output the debug main pointer die for VMS */
16793 void
16794 dwarf2out_vms_debug_main_pointer (void)
16796 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16797 dw_die_ref die;
16799 /* Allocate the VMS debug main subprogram die. */
16800 die = ggc_alloc_cleared_die_node ();
16801 die->die_tag = DW_TAG_subprogram;
16802 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16803 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16804 current_function_funcdef_no);
16805 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16807 /* Make it the first child of comp_unit_die (). */
16808 die->die_parent = comp_unit_die ();
16809 if (comp_unit_die ()->die_child)
16811 die->die_sib = comp_unit_die ()->die_child->die_sib;
16812 comp_unit_die ()->die_child->die_sib = die;
16814 else
16816 die->die_sib = die;
16817 comp_unit_die ()->die_child = die;
16820 #endif /* VMS_DEBUGGING_INFO */
16822 /* Push a new declaration scope. */
16824 static void
16825 push_decl_scope (tree scope)
16827 vec_safe_push (decl_scope_table, scope);
16830 /* Pop a declaration scope. */
16832 static inline void
16833 pop_decl_scope (void)
16835 decl_scope_table->pop ();
16838 /* walk_tree helper function for uses_local_type, below. */
16840 static tree
16841 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16843 if (!TYPE_P (*tp))
16844 *walk_subtrees = 0;
16845 else
16847 tree name = TYPE_NAME (*tp);
16848 if (name && DECL_P (name) && decl_function_context (name))
16849 return *tp;
16851 return NULL_TREE;
16854 /* If TYPE involves a function-local type (including a local typedef to a
16855 non-local type), returns that type; otherwise returns NULL_TREE. */
16857 static tree
16858 uses_local_type (tree type)
16860 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16861 return used;
16864 /* Return the DIE for the scope that immediately contains this type.
16865 Non-named types that do not involve a function-local type get global
16866 scope. Named types nested in namespaces or other types get their
16867 containing scope. All other types (i.e. function-local named types) get
16868 the current active scope. */
16870 static dw_die_ref
16871 scope_die_for (tree t, dw_die_ref context_die)
16873 dw_die_ref scope_die = NULL;
16874 tree containing_scope;
16876 /* Non-types always go in the current scope. */
16877 gcc_assert (TYPE_P (t));
16879 /* Use the scope of the typedef, rather than the scope of the type
16880 it refers to. */
16881 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16882 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16883 else
16884 containing_scope = TYPE_CONTEXT (t);
16886 /* Use the containing namespace if there is one. */
16887 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16889 if (context_die == lookup_decl_die (containing_scope))
16890 /* OK */;
16891 else if (debug_info_level > DINFO_LEVEL_TERSE)
16892 context_die = get_context_die (containing_scope);
16893 else
16894 containing_scope = NULL_TREE;
16897 /* Ignore function type "scopes" from the C frontend. They mean that
16898 a tagged type is local to a parmlist of a function declarator, but
16899 that isn't useful to DWARF. */
16900 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16901 containing_scope = NULL_TREE;
16903 if (SCOPE_FILE_SCOPE_P (containing_scope))
16905 /* If T uses a local type keep it local as well, to avoid references
16906 to function-local DIEs from outside the function. */
16907 if (current_function_decl && uses_local_type (t))
16908 scope_die = context_die;
16909 else
16910 scope_die = comp_unit_die ();
16912 else if (TYPE_P (containing_scope))
16914 /* For types, we can just look up the appropriate DIE. */
16915 if (debug_info_level > DINFO_LEVEL_TERSE)
16916 scope_die = get_context_die (containing_scope);
16917 else
16919 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16920 if (scope_die == NULL)
16921 scope_die = comp_unit_die ();
16924 else
16925 scope_die = context_die;
16927 return scope_die;
16930 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16932 static inline int
16933 local_scope_p (dw_die_ref context_die)
16935 for (; context_die; context_die = context_die->die_parent)
16936 if (context_die->die_tag == DW_TAG_inlined_subroutine
16937 || context_die->die_tag == DW_TAG_subprogram)
16938 return 1;
16940 return 0;
16943 /* Returns nonzero if CONTEXT_DIE is a class. */
16945 static inline int
16946 class_scope_p (dw_die_ref context_die)
16948 return (context_die
16949 && (context_die->die_tag == DW_TAG_structure_type
16950 || context_die->die_tag == DW_TAG_class_type
16951 || context_die->die_tag == DW_TAG_interface_type
16952 || context_die->die_tag == DW_TAG_union_type));
16955 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16956 whether or not to treat a DIE in this context as a declaration. */
16958 static inline int
16959 class_or_namespace_scope_p (dw_die_ref context_die)
16961 return (class_scope_p (context_die)
16962 || (context_die && context_die->die_tag == DW_TAG_namespace));
16965 /* Many forms of DIEs require a "type description" attribute. This
16966 routine locates the proper "type descriptor" die for the type given
16967 by 'type', and adds a DW_AT_type attribute below the given die. */
16969 static void
16970 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16971 int decl_volatile, dw_die_ref context_die)
16973 enum tree_code code = TREE_CODE (type);
16974 dw_die_ref type_die = NULL;
16976 /* ??? If this type is an unnamed subrange type of an integral, floating-point
16977 or fixed-point type, use the inner type. This is because we have no
16978 support for unnamed types in base_type_die. This can happen if this is
16979 an Ada subrange type. Correct solution is emit a subrange type die. */
16980 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16981 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16982 type = TREE_TYPE (type), code = TREE_CODE (type);
16984 if (code == ERROR_MARK
16985 /* Handle a special case. For functions whose return type is void, we
16986 generate *no* type attribute. (Note that no object may have type
16987 `void', so this only applies to function return types). */
16988 || code == VOID_TYPE)
16989 return;
16991 type_die = modified_type_die (type,
16992 decl_const || TYPE_READONLY (type),
16993 decl_volatile || TYPE_VOLATILE (type),
16994 context_die);
16996 if (type_die != NULL)
16997 add_AT_die_ref (object_die, DW_AT_type, type_die);
17000 /* Given an object die, add the calling convention attribute for the
17001 function call type. */
17002 static void
17003 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17005 enum dwarf_calling_convention value = DW_CC_normal;
17007 value = ((enum dwarf_calling_convention)
17008 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17010 if (is_fortran ()
17011 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17013 /* DWARF 2 doesn't provide a way to identify a program's source-level
17014 entry point. DW_AT_calling_convention attributes are only meant
17015 to describe functions' calling conventions. However, lacking a
17016 better way to signal the Fortran main program, we used this for
17017 a long time, following existing custom. Now, DWARF 4 has
17018 DW_AT_main_subprogram, which we add below, but some tools still
17019 rely on the old way, which we thus keep. */
17020 value = DW_CC_program;
17022 if (dwarf_version >= 4 || !dwarf_strict)
17023 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17026 /* Only add the attribute if the backend requests it, and
17027 is not DW_CC_normal. */
17028 if (value && (value != DW_CC_normal))
17029 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17032 /* Given a tree pointer to a struct, class, union, or enum type node, return
17033 a pointer to the (string) tag name for the given type, or zero if the type
17034 was declared without a tag. */
17036 static const char *
17037 type_tag (const_tree type)
17039 const char *name = 0;
17041 if (TYPE_NAME (type) != 0)
17043 tree t = 0;
17045 /* Find the IDENTIFIER_NODE for the type name. */
17046 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17047 && !TYPE_NAMELESS (type))
17048 t = TYPE_NAME (type);
17050 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17051 a TYPE_DECL node, regardless of whether or not a `typedef' was
17052 involved. */
17053 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17054 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17056 /* We want to be extra verbose. Don't call dwarf_name if
17057 DECL_NAME isn't set. The default hook for decl_printable_name
17058 doesn't like that, and in this context it's correct to return
17059 0, instead of "<anonymous>" or the like. */
17060 if (DECL_NAME (TYPE_NAME (type))
17061 && !DECL_NAMELESS (TYPE_NAME (type)))
17062 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17065 /* Now get the name as a string, or invent one. */
17066 if (!name && t != 0)
17067 name = IDENTIFIER_POINTER (t);
17070 return (name == 0 || *name == '\0') ? 0 : name;
17073 /* Return the type associated with a data member, make a special check
17074 for bit field types. */
17076 static inline tree
17077 member_declared_type (const_tree member)
17079 return (DECL_BIT_FIELD_TYPE (member)
17080 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17083 /* Get the decl's label, as described by its RTL. This may be different
17084 from the DECL_NAME name used in the source file. */
17086 #if 0
17087 static const char *
17088 decl_start_label (tree decl)
17090 rtx x;
17091 const char *fnname;
17093 x = DECL_RTL (decl);
17094 gcc_assert (MEM_P (x));
17096 x = XEXP (x, 0);
17097 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17099 fnname = XSTR (x, 0);
17100 return fnname;
17102 #endif
17104 /* These routines generate the internal representation of the DIE's for
17105 the compilation unit. Debugging information is collected by walking
17106 the declaration trees passed in from dwarf2out_decl(). */
17108 static void
17109 gen_array_type_die (tree type, dw_die_ref context_die)
17111 dw_die_ref scope_die = scope_die_for (type, context_die);
17112 dw_die_ref array_die;
17114 /* GNU compilers represent multidimensional array types as sequences of one
17115 dimensional array types whose element types are themselves array types.
17116 We sometimes squish that down to a single array_type DIE with multiple
17117 subscripts in the Dwarf debugging info. The draft Dwarf specification
17118 say that we are allowed to do this kind of compression in C, because
17119 there is no difference between an array of arrays and a multidimensional
17120 array. We don't do this for Ada to remain as close as possible to the
17121 actual representation, which is especially important against the language
17122 flexibilty wrt arrays of variable size. */
17124 bool collapse_nested_arrays = !is_ada ();
17125 tree element_type;
17127 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17128 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17129 if (TYPE_STRING_FLAG (type)
17130 && TREE_CODE (type) == ARRAY_TYPE
17131 && is_fortran ()
17132 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17134 HOST_WIDE_INT size;
17136 array_die = new_die (DW_TAG_string_type, scope_die, type);
17137 add_name_attribute (array_die, type_tag (type));
17138 equate_type_number_to_die (type, array_die);
17139 size = int_size_in_bytes (type);
17140 if (size >= 0)
17141 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17142 else if (TYPE_DOMAIN (type) != NULL_TREE
17143 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17144 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17146 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17147 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17149 size = int_size_in_bytes (TREE_TYPE (szdecl));
17150 if (loc && size > 0)
17152 add_AT_location_description (array_die, DW_AT_string_length, loc);
17153 if (size != DWARF2_ADDR_SIZE)
17154 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17157 return;
17160 array_die = new_die (DW_TAG_array_type, scope_die, type);
17161 add_name_attribute (array_die, type_tag (type));
17162 equate_type_number_to_die (type, array_die);
17164 if (TREE_CODE (type) == VECTOR_TYPE)
17165 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17167 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17168 if (is_fortran ()
17169 && TREE_CODE (type) == ARRAY_TYPE
17170 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17171 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17172 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17174 #if 0
17175 /* We default the array ordering. SDB will probably do
17176 the right things even if DW_AT_ordering is not present. It's not even
17177 an issue until we start to get into multidimensional arrays anyway. If
17178 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17179 then we'll have to put the DW_AT_ordering attribute back in. (But if
17180 and when we find out that we need to put these in, we will only do so
17181 for multidimensional arrays. */
17182 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17183 #endif
17185 if (TREE_CODE (type) == VECTOR_TYPE)
17187 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17188 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17189 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17190 add_bound_info (subrange_die, DW_AT_upper_bound,
17191 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17193 else
17194 add_subscript_info (array_die, type, collapse_nested_arrays);
17196 /* Add representation of the type of the elements of this array type and
17197 emit the corresponding DIE if we haven't done it already. */
17198 element_type = TREE_TYPE (type);
17199 if (collapse_nested_arrays)
17200 while (TREE_CODE (element_type) == ARRAY_TYPE)
17202 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17203 break;
17204 element_type = TREE_TYPE (element_type);
17207 add_type_attribute (array_die, element_type, 0, 0, context_die);
17209 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17210 if (TYPE_ARTIFICIAL (type))
17211 add_AT_flag (array_die, DW_AT_artificial, 1);
17213 if (get_AT (array_die, DW_AT_name))
17214 add_pubtype (type, array_die);
17217 static dw_loc_descr_ref
17218 descr_info_loc (tree val, tree base_decl)
17220 HOST_WIDE_INT size;
17221 dw_loc_descr_ref loc, loc2;
17222 enum dwarf_location_atom op;
17224 if (val == base_decl)
17225 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17227 switch (TREE_CODE (val))
17229 CASE_CONVERT:
17230 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17231 case VAR_DECL:
17232 return loc_descriptor_from_tree (val, 0);
17233 case INTEGER_CST:
17234 if (tree_fits_shwi_p (val))
17235 return int_loc_descriptor (tree_to_shwi (val));
17236 break;
17237 case INDIRECT_REF:
17238 size = int_size_in_bytes (TREE_TYPE (val));
17239 if (size < 0)
17240 break;
17241 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17242 if (!loc)
17243 break;
17244 if (size == DWARF2_ADDR_SIZE)
17245 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17246 else
17247 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17248 return loc;
17249 case POINTER_PLUS_EXPR:
17250 case PLUS_EXPR:
17251 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17252 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17254 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17255 if (!loc)
17256 break;
17257 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17259 else
17261 op = DW_OP_plus;
17262 do_binop:
17263 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17264 if (!loc)
17265 break;
17266 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17267 if (!loc2)
17268 break;
17269 add_loc_descr (&loc, loc2);
17270 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17272 return loc;
17273 case MINUS_EXPR:
17274 op = DW_OP_minus;
17275 goto do_binop;
17276 case MULT_EXPR:
17277 op = DW_OP_mul;
17278 goto do_binop;
17279 case EQ_EXPR:
17280 op = DW_OP_eq;
17281 goto do_binop;
17282 case NE_EXPR:
17283 op = DW_OP_ne;
17284 goto do_binop;
17285 default:
17286 break;
17288 return NULL;
17291 static void
17292 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17293 tree val, tree base_decl)
17295 dw_loc_descr_ref loc;
17297 if (tree_fits_shwi_p (val))
17299 add_AT_unsigned (die, attr, tree_to_shwi (val));
17300 return;
17303 loc = descr_info_loc (val, base_decl);
17304 if (!loc)
17305 return;
17307 add_AT_loc (die, attr, loc);
17310 /* This routine generates DIE for array with hidden descriptor, details
17311 are filled into *info by a langhook. */
17313 static void
17314 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17315 dw_die_ref context_die)
17317 dw_die_ref scope_die = scope_die_for (type, context_die);
17318 dw_die_ref array_die;
17319 int dim;
17321 array_die = new_die (DW_TAG_array_type, scope_die, type);
17322 add_name_attribute (array_die, type_tag (type));
17323 equate_type_number_to_die (type, array_die);
17325 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17326 if (is_fortran ()
17327 && info->ndimensions >= 2)
17328 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17330 if (info->data_location)
17331 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17332 info->base_decl);
17333 if (info->associated)
17334 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17335 info->base_decl);
17336 if (info->allocated)
17337 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17338 info->base_decl);
17340 for (dim = 0; dim < info->ndimensions; dim++)
17342 dw_die_ref subrange_die
17343 = new_die (DW_TAG_subrange_type, array_die, NULL);
17345 if (info->dimen[dim].lower_bound)
17347 /* If it is the default value, omit it. */
17348 int dflt;
17350 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17351 && (dflt = lower_bound_default ()) != -1
17352 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17354 else
17355 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17356 info->dimen[dim].lower_bound,
17357 info->base_decl);
17359 if (info->dimen[dim].upper_bound)
17360 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17361 info->dimen[dim].upper_bound,
17362 info->base_decl);
17363 if (info->dimen[dim].stride)
17364 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17365 info->dimen[dim].stride,
17366 info->base_decl);
17369 gen_type_die (info->element_type, context_die);
17370 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17372 if (get_AT (array_die, DW_AT_name))
17373 add_pubtype (type, array_die);
17376 #if 0
17377 static void
17378 gen_entry_point_die (tree decl, dw_die_ref context_die)
17380 tree origin = decl_ultimate_origin (decl);
17381 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17383 if (origin != NULL)
17384 add_abstract_origin_attribute (decl_die, origin);
17385 else
17387 add_name_and_src_coords_attributes (decl_die, decl);
17388 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17389 0, 0, context_die);
17392 if (DECL_ABSTRACT (decl))
17393 equate_decl_number_to_die (decl, decl_die);
17394 else
17395 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17397 #endif
17399 /* Walk through the list of incomplete types again, trying once more to
17400 emit full debugging info for them. */
17402 static void
17403 retry_incomplete_types (void)
17405 int i;
17407 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17408 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17409 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17412 /* Determine what tag to use for a record type. */
17414 static enum dwarf_tag
17415 record_type_tag (tree type)
17417 if (! lang_hooks.types.classify_record)
17418 return DW_TAG_structure_type;
17420 switch (lang_hooks.types.classify_record (type))
17422 case RECORD_IS_STRUCT:
17423 return DW_TAG_structure_type;
17425 case RECORD_IS_CLASS:
17426 return DW_TAG_class_type;
17428 case RECORD_IS_INTERFACE:
17429 if (dwarf_version >= 3 || !dwarf_strict)
17430 return DW_TAG_interface_type;
17431 return DW_TAG_structure_type;
17433 default:
17434 gcc_unreachable ();
17438 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17439 include all of the information about the enumeration values also. Each
17440 enumerated type name/value is listed as a child of the enumerated type
17441 DIE. */
17443 static dw_die_ref
17444 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17446 dw_die_ref type_die = lookup_type_die (type);
17448 if (type_die == NULL)
17450 type_die = new_die (DW_TAG_enumeration_type,
17451 scope_die_for (type, context_die), type);
17452 equate_type_number_to_die (type, type_die);
17453 add_name_attribute (type_die, type_tag (type));
17454 if (dwarf_version >= 4 || !dwarf_strict)
17456 if (ENUM_IS_SCOPED (type))
17457 add_AT_flag (type_die, DW_AT_enum_class, 1);
17458 if (ENUM_IS_OPAQUE (type))
17459 add_AT_flag (type_die, DW_AT_declaration, 1);
17462 else if (! TYPE_SIZE (type))
17463 return type_die;
17464 else
17465 remove_AT (type_die, DW_AT_declaration);
17467 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17468 given enum type is incomplete, do not generate the DW_AT_byte_size
17469 attribute or the DW_AT_element_list attribute. */
17470 if (TYPE_SIZE (type))
17472 tree link;
17474 TREE_ASM_WRITTEN (type) = 1;
17475 add_byte_size_attribute (type_die, type);
17476 if (TYPE_STUB_DECL (type) != NULL_TREE)
17478 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17479 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17482 /* If the first reference to this type was as the return type of an
17483 inline function, then it may not have a parent. Fix this now. */
17484 if (type_die->die_parent == NULL)
17485 add_child_die (scope_die_for (type, context_die), type_die);
17487 for (link = TYPE_VALUES (type);
17488 link != NULL; link = TREE_CHAIN (link))
17490 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17491 tree value = TREE_VALUE (link);
17493 add_name_attribute (enum_die,
17494 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17496 if (TREE_CODE (value) == CONST_DECL)
17497 value = DECL_INITIAL (value);
17499 if (simple_type_size_in_bits (TREE_TYPE (value))
17500 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17501 /* DWARF2 does not provide a way of indicating whether or
17502 not enumeration constants are signed or unsigned. GDB
17503 always assumes the values are signed, so we output all
17504 values as if they were signed. That means that
17505 enumeration constants with very large unsigned values
17506 will appear to have negative values in the debugger.
17508 TODO: the above comment is wrong, DWARF2 does provide
17509 DW_FORM_sdata/DW_FORM_udata to represent signed/unsigned data.
17510 This should be re-worked to use correct signed/unsigned
17511 int/double tags for all cases, instead of always treating as
17512 signed. */
17513 add_AT_int (enum_die, DW_AT_const_value, TREE_INT_CST_LOW (value));
17514 else
17515 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17516 that here. */
17517 add_AT_wide (enum_die, DW_AT_const_value, value);
17520 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17521 if (TYPE_ARTIFICIAL (type))
17522 add_AT_flag (type_die, DW_AT_artificial, 1);
17524 else
17525 add_AT_flag (type_die, DW_AT_declaration, 1);
17527 add_pubtype (type, type_die);
17529 return type_die;
17532 /* Generate a DIE to represent either a real live formal parameter decl or to
17533 represent just the type of some formal parameter position in some function
17534 type.
17536 Note that this routine is a bit unusual because its argument may be a
17537 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17538 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17539 node. If it's the former then this function is being called to output a
17540 DIE to represent a formal parameter object (or some inlining thereof). If
17541 it's the latter, then this function is only being called to output a
17542 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17543 argument type of some subprogram type.
17544 If EMIT_NAME_P is true, name and source coordinate attributes
17545 are emitted. */
17547 static dw_die_ref
17548 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17549 dw_die_ref context_die)
17551 tree node_or_origin = node ? node : origin;
17552 tree ultimate_origin;
17553 dw_die_ref parm_die
17554 = new_die (DW_TAG_formal_parameter, context_die, node);
17556 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17558 case tcc_declaration:
17559 ultimate_origin = decl_ultimate_origin (node_or_origin);
17560 if (node || ultimate_origin)
17561 origin = ultimate_origin;
17562 if (origin != NULL)
17563 add_abstract_origin_attribute (parm_die, origin);
17564 else if (emit_name_p)
17565 add_name_and_src_coords_attributes (parm_die, node);
17566 if (origin == NULL
17567 || (! DECL_ABSTRACT (node_or_origin)
17568 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17569 decl_function_context
17570 (node_or_origin))))
17572 tree type = TREE_TYPE (node_or_origin);
17573 if (decl_by_reference_p (node_or_origin))
17574 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17575 context_die);
17576 else
17577 add_type_attribute (parm_die, type,
17578 TREE_READONLY (node_or_origin),
17579 TREE_THIS_VOLATILE (node_or_origin),
17580 context_die);
17582 if (origin == NULL && DECL_ARTIFICIAL (node))
17583 add_AT_flag (parm_die, DW_AT_artificial, 1);
17585 if (node && node != origin)
17586 equate_decl_number_to_die (node, parm_die);
17587 if (! DECL_ABSTRACT (node_or_origin))
17588 add_location_or_const_value_attribute (parm_die, node_or_origin,
17589 node == NULL, DW_AT_location);
17591 break;
17593 case tcc_type:
17594 /* We were called with some kind of a ..._TYPE node. */
17595 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17596 break;
17598 default:
17599 gcc_unreachable ();
17602 return parm_die;
17605 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17606 children DW_TAG_formal_parameter DIEs representing the arguments of the
17607 parameter pack.
17609 PARM_PACK must be a function parameter pack.
17610 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17611 must point to the subsequent arguments of the function PACK_ARG belongs to.
17612 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17613 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17614 following the last one for which a DIE was generated. */
17616 static dw_die_ref
17617 gen_formal_parameter_pack_die (tree parm_pack,
17618 tree pack_arg,
17619 dw_die_ref subr_die,
17620 tree *next_arg)
17622 tree arg;
17623 dw_die_ref parm_pack_die;
17625 gcc_assert (parm_pack
17626 && lang_hooks.function_parameter_pack_p (parm_pack)
17627 && subr_die);
17629 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17630 add_src_coords_attributes (parm_pack_die, parm_pack);
17632 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17634 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17635 parm_pack))
17636 break;
17637 gen_formal_parameter_die (arg, NULL,
17638 false /* Don't emit name attribute. */,
17639 parm_pack_die);
17641 if (next_arg)
17642 *next_arg = arg;
17643 return parm_pack_die;
17646 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17647 at the end of an (ANSI prototyped) formal parameters list. */
17649 static void
17650 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17652 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17655 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17656 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17657 parameters as specified in some function type specification (except for
17658 those which appear as part of a function *definition*). */
17660 static void
17661 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17663 tree link;
17664 tree formal_type = NULL;
17665 tree first_parm_type;
17666 tree arg;
17668 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17670 arg = DECL_ARGUMENTS (function_or_method_type);
17671 function_or_method_type = TREE_TYPE (function_or_method_type);
17673 else
17674 arg = NULL_TREE;
17676 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17678 /* Make our first pass over the list of formal parameter types and output a
17679 DW_TAG_formal_parameter DIE for each one. */
17680 for (link = first_parm_type; link; )
17682 dw_die_ref parm_die;
17684 formal_type = TREE_VALUE (link);
17685 if (formal_type == void_type_node)
17686 break;
17688 /* Output a (nameless) DIE to represent the formal parameter itself. */
17689 parm_die = gen_formal_parameter_die (formal_type, NULL,
17690 true /* Emit name attribute. */,
17691 context_die);
17692 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17693 && link == first_parm_type)
17695 add_AT_flag (parm_die, DW_AT_artificial, 1);
17696 if (dwarf_version >= 3 || !dwarf_strict)
17697 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17699 else if (arg && DECL_ARTIFICIAL (arg))
17700 add_AT_flag (parm_die, DW_AT_artificial, 1);
17702 link = TREE_CHAIN (link);
17703 if (arg)
17704 arg = DECL_CHAIN (arg);
17707 /* If this function type has an ellipsis, add a
17708 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17709 if (formal_type != void_type_node)
17710 gen_unspecified_parameters_die (function_or_method_type, context_die);
17712 /* Make our second (and final) pass over the list of formal parameter types
17713 and output DIEs to represent those types (as necessary). */
17714 for (link = TYPE_ARG_TYPES (function_or_method_type);
17715 link && TREE_VALUE (link);
17716 link = TREE_CHAIN (link))
17717 gen_type_die (TREE_VALUE (link), context_die);
17720 /* We want to generate the DIE for TYPE so that we can generate the
17721 die for MEMBER, which has been defined; we will need to refer back
17722 to the member declaration nested within TYPE. If we're trying to
17723 generate minimal debug info for TYPE, processing TYPE won't do the
17724 trick; we need to attach the member declaration by hand. */
17726 static void
17727 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17729 gen_type_die (type, context_die);
17731 /* If we're trying to avoid duplicate debug info, we may not have
17732 emitted the member decl for this function. Emit it now. */
17733 if (TYPE_STUB_DECL (type)
17734 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17735 && ! lookup_decl_die (member))
17737 dw_die_ref type_die;
17738 gcc_assert (!decl_ultimate_origin (member));
17740 push_decl_scope (type);
17741 type_die = lookup_type_die_strip_naming_typedef (type);
17742 if (TREE_CODE (member) == FUNCTION_DECL)
17743 gen_subprogram_die (member, type_die);
17744 else if (TREE_CODE (member) == FIELD_DECL)
17746 /* Ignore the nameless fields that are used to skip bits but handle
17747 C++ anonymous unions and structs. */
17748 if (DECL_NAME (member) != NULL_TREE
17749 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17750 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17752 gen_type_die (member_declared_type (member), type_die);
17753 gen_field_die (member, type_die);
17756 else
17757 gen_variable_die (member, NULL_TREE, type_die);
17759 pop_decl_scope ();
17763 /* Forward declare these functions, because they are mutually recursive
17764 with their set_block_* pairing functions. */
17765 static void set_decl_origin_self (tree);
17766 static void set_decl_abstract_flags (tree, int);
17768 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17769 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17770 that it points to the node itself, thus indicating that the node is its
17771 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17772 the given node is NULL, recursively descend the decl/block tree which
17773 it is the root of, and for each other ..._DECL or BLOCK node contained
17774 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17775 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17776 values to point to themselves. */
17778 static void
17779 set_block_origin_self (tree stmt)
17781 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17783 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17786 tree local_decl;
17788 for (local_decl = BLOCK_VARS (stmt);
17789 local_decl != NULL_TREE;
17790 local_decl = DECL_CHAIN (local_decl))
17791 if (! DECL_EXTERNAL (local_decl))
17792 set_decl_origin_self (local_decl); /* Potential recursion. */
17796 tree subblock;
17798 for (subblock = BLOCK_SUBBLOCKS (stmt);
17799 subblock != NULL_TREE;
17800 subblock = BLOCK_CHAIN (subblock))
17801 set_block_origin_self (subblock); /* Recurse. */
17806 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17807 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17808 node to so that it points to the node itself, thus indicating that the
17809 node represents its own (abstract) origin. Additionally, if the
17810 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17811 the decl/block tree of which the given node is the root of, and for
17812 each other ..._DECL or BLOCK node contained therein whose
17813 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17814 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17815 point to themselves. */
17817 static void
17818 set_decl_origin_self (tree decl)
17820 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17822 DECL_ABSTRACT_ORIGIN (decl) = decl;
17823 if (TREE_CODE (decl) == FUNCTION_DECL)
17825 tree arg;
17827 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17828 DECL_ABSTRACT_ORIGIN (arg) = arg;
17829 if (DECL_INITIAL (decl) != NULL_TREE
17830 && DECL_INITIAL (decl) != error_mark_node)
17831 set_block_origin_self (DECL_INITIAL (decl));
17836 /* Given a pointer to some BLOCK node, and a boolean value to set the
17837 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17838 the given block, and for all local decls and all local sub-blocks
17839 (recursively) which are contained therein. */
17841 static void
17842 set_block_abstract_flags (tree stmt, int setting)
17844 tree local_decl;
17845 tree subblock;
17846 unsigned int i;
17848 BLOCK_ABSTRACT (stmt) = setting;
17850 for (local_decl = BLOCK_VARS (stmt);
17851 local_decl != NULL_TREE;
17852 local_decl = DECL_CHAIN (local_decl))
17853 if (! DECL_EXTERNAL (local_decl))
17854 set_decl_abstract_flags (local_decl, setting);
17856 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17858 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17859 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17860 || TREE_CODE (local_decl) == PARM_DECL)
17861 set_decl_abstract_flags (local_decl, setting);
17864 for (subblock = BLOCK_SUBBLOCKS (stmt);
17865 subblock != NULL_TREE;
17866 subblock = BLOCK_CHAIN (subblock))
17867 set_block_abstract_flags (subblock, setting);
17870 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17871 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17872 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17873 set the abstract flags for all of the parameters, local vars, local
17874 blocks and sub-blocks (recursively) to the same setting. */
17876 static void
17877 set_decl_abstract_flags (tree decl, int setting)
17879 DECL_ABSTRACT (decl) = setting;
17880 if (TREE_CODE (decl) == FUNCTION_DECL)
17882 tree arg;
17884 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17885 DECL_ABSTRACT (arg) = setting;
17886 if (DECL_INITIAL (decl) != NULL_TREE
17887 && DECL_INITIAL (decl) != error_mark_node)
17888 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17892 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17893 may later generate inlined and/or out-of-line instances of. */
17895 static void
17896 dwarf2out_abstract_function (tree decl)
17898 dw_die_ref old_die;
17899 tree save_fn;
17900 tree context;
17901 int was_abstract;
17902 htab_t old_decl_loc_table;
17903 htab_t old_cached_dw_loc_list_table;
17904 int old_call_site_count, old_tail_call_site_count;
17905 struct call_arg_loc_node *old_call_arg_locations;
17907 /* Make sure we have the actual abstract inline, not a clone. */
17908 decl = DECL_ORIGIN (decl);
17910 old_die = lookup_decl_die (decl);
17911 if (old_die && get_AT (old_die, DW_AT_inline))
17912 /* We've already generated the abstract instance. */
17913 return;
17915 /* We can be called while recursively when seeing block defining inlined subroutine
17916 DIE. Be sure to not clobber the outer location table nor use it or we would
17917 get locations in abstract instantces. */
17918 old_decl_loc_table = decl_loc_table;
17919 decl_loc_table = NULL;
17920 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17921 cached_dw_loc_list_table = NULL;
17922 old_call_arg_locations = call_arg_locations;
17923 call_arg_locations = NULL;
17924 old_call_site_count = call_site_count;
17925 call_site_count = -1;
17926 old_tail_call_site_count = tail_call_site_count;
17927 tail_call_site_count = -1;
17929 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17930 we don't get confused by DECL_ABSTRACT. */
17931 if (debug_info_level > DINFO_LEVEL_TERSE)
17933 context = decl_class_context (decl);
17934 if (context)
17935 gen_type_die_for_member
17936 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17939 /* Pretend we've just finished compiling this function. */
17940 save_fn = current_function_decl;
17941 current_function_decl = decl;
17943 was_abstract = DECL_ABSTRACT (decl);
17944 set_decl_abstract_flags (decl, 1);
17945 dwarf2out_decl (decl);
17946 if (! was_abstract)
17947 set_decl_abstract_flags (decl, 0);
17949 current_function_decl = save_fn;
17950 decl_loc_table = old_decl_loc_table;
17951 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17952 call_arg_locations = old_call_arg_locations;
17953 call_site_count = old_call_site_count;
17954 tail_call_site_count = old_tail_call_site_count;
17957 /* Helper function of premark_used_types() which gets called through
17958 htab_traverse.
17960 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17961 marked as unused by prune_unused_types. */
17963 static int
17964 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17966 tree type;
17967 dw_die_ref die;
17969 type = (tree) *slot;
17970 die = lookup_type_die (type);
17971 if (die != NULL)
17972 die->die_perennial_p = 1;
17973 return 1;
17976 /* Helper function of premark_types_used_by_global_vars which gets called
17977 through htab_traverse.
17979 Marks the DIE of a given type in *SLOT as perennial, so it never gets
17980 marked as unused by prune_unused_types. The DIE of the type is marked
17981 only if the global variable using the type will actually be emitted. */
17983 static int
17984 premark_types_used_by_global_vars_helper (void **slot,
17985 void *data ATTRIBUTE_UNUSED)
17987 struct types_used_by_vars_entry *entry;
17988 dw_die_ref die;
17990 entry = (struct types_used_by_vars_entry *) *slot;
17991 gcc_assert (entry->type != NULL
17992 && entry->var_decl != NULL);
17993 die = lookup_type_die (entry->type);
17994 if (die)
17996 /* Ask cgraph if the global variable really is to be emitted.
17997 If yes, then we'll keep the DIE of ENTRY->TYPE. */
17998 struct varpool_node *node = varpool_get_node (entry->var_decl);
17999 if (node && node->definition)
18001 die->die_perennial_p = 1;
18002 /* Keep the parent DIEs as well. */
18003 while ((die = die->die_parent) && die->die_perennial_p == 0)
18004 die->die_perennial_p = 1;
18007 return 1;
18010 /* Mark all members of used_types_hash as perennial. */
18012 static void
18013 premark_used_types (struct function *fun)
18015 if (fun && fun->used_types_hash)
18016 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
18019 /* Mark all members of types_used_by_vars_entry as perennial. */
18021 static void
18022 premark_types_used_by_global_vars (void)
18024 if (types_used_by_vars_hash)
18025 htab_traverse (types_used_by_vars_hash,
18026 premark_types_used_by_global_vars_helper, NULL);
18029 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18030 for CA_LOC call arg loc node. */
18032 static dw_die_ref
18033 gen_call_site_die (tree decl, dw_die_ref subr_die,
18034 struct call_arg_loc_node *ca_loc)
18036 dw_die_ref stmt_die = NULL, die;
18037 tree block = ca_loc->block;
18039 while (block
18040 && block != DECL_INITIAL (decl)
18041 && TREE_CODE (block) == BLOCK)
18043 if (block_map.length () > BLOCK_NUMBER (block))
18044 stmt_die = block_map[BLOCK_NUMBER (block)];
18045 if (stmt_die)
18046 break;
18047 block = BLOCK_SUPERCONTEXT (block);
18049 if (stmt_die == NULL)
18050 stmt_die = subr_die;
18051 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18052 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18053 if (ca_loc->tail_call_p)
18054 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18055 if (ca_loc->symbol_ref)
18057 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18058 if (tdie)
18059 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18060 else
18061 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18063 return die;
18066 /* Generate a DIE to represent a declared function (either file-scope or
18067 block-local). */
18069 static void
18070 gen_subprogram_die (tree decl, dw_die_ref context_die)
18072 tree origin = decl_ultimate_origin (decl);
18073 dw_die_ref subr_die;
18074 tree outer_scope;
18075 dw_die_ref old_die = lookup_decl_die (decl);
18076 int declaration = (current_function_decl != decl
18077 || class_or_namespace_scope_p (context_die));
18079 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18081 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18082 started to generate the abstract instance of an inline, decided to output
18083 its containing class, and proceeded to emit the declaration of the inline
18084 from the member list for the class. If so, DECLARATION takes priority;
18085 we'll get back to the abstract instance when done with the class. */
18087 /* The class-scope declaration DIE must be the primary DIE. */
18088 if (origin && declaration && class_or_namespace_scope_p (context_die))
18090 origin = NULL;
18091 gcc_assert (!old_die);
18094 /* Now that the C++ front end lazily declares artificial member fns, we
18095 might need to retrofit the declaration into its class. */
18096 if (!declaration && !origin && !old_die
18097 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18098 && !class_or_namespace_scope_p (context_die)
18099 && debug_info_level > DINFO_LEVEL_TERSE)
18100 old_die = force_decl_die (decl);
18102 if (origin != NULL)
18104 gcc_assert (!declaration || local_scope_p (context_die));
18106 /* Fixup die_parent for the abstract instance of a nested
18107 inline function. */
18108 if (old_die && old_die->die_parent == NULL)
18109 add_child_die (context_die, old_die);
18111 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18112 add_abstract_origin_attribute (subr_die, origin);
18113 /* This is where the actual code for a cloned function is.
18114 Let's emit linkage name attribute for it. This helps
18115 debuggers to e.g, set breakpoints into
18116 constructors/destructors when the user asks "break
18117 K::K". */
18118 add_linkage_name (subr_die, decl);
18120 else if (old_die)
18122 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18123 struct dwarf_file_data * file_index = lookup_filename (s.file);
18125 if (!get_AT_flag (old_die, DW_AT_declaration)
18126 /* We can have a normal definition following an inline one in the
18127 case of redefinition of GNU C extern inlines.
18128 It seems reasonable to use AT_specification in this case. */
18129 && !get_AT (old_die, DW_AT_inline))
18131 /* Detect and ignore this case, where we are trying to output
18132 something we have already output. */
18133 return;
18136 /* If the definition comes from the same place as the declaration,
18137 maybe use the old DIE. We always want the DIE for this function
18138 that has the *_pc attributes to be under comp_unit_die so the
18139 debugger can find it. We also need to do this for abstract
18140 instances of inlines, since the spec requires the out-of-line copy
18141 to have the same parent. For local class methods, this doesn't
18142 apply; we just use the old DIE. */
18143 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18144 && (DECL_ARTIFICIAL (decl)
18145 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18146 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18147 == (unsigned) s.line))))
18149 subr_die = old_die;
18151 /* Clear out the declaration attribute and the formal parameters.
18152 Do not remove all children, because it is possible that this
18153 declaration die was forced using force_decl_die(). In such
18154 cases die that forced declaration die (e.g. TAG_imported_module)
18155 is one of the children that we do not want to remove. */
18156 remove_AT (subr_die, DW_AT_declaration);
18157 remove_AT (subr_die, DW_AT_object_pointer);
18158 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18160 else
18162 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18163 add_AT_specification (subr_die, old_die);
18164 add_pubname (decl, subr_die);
18165 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18166 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18167 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18168 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18171 else
18173 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18175 if (TREE_PUBLIC (decl))
18176 add_AT_flag (subr_die, DW_AT_external, 1);
18178 add_name_and_src_coords_attributes (subr_die, decl);
18179 add_pubname (decl, subr_die);
18180 if (debug_info_level > DINFO_LEVEL_TERSE)
18182 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18183 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18184 0, 0, context_die);
18187 add_pure_or_virtual_attribute (subr_die, decl);
18188 if (DECL_ARTIFICIAL (decl))
18189 add_AT_flag (subr_die, DW_AT_artificial, 1);
18191 add_accessibility_attribute (subr_die, decl);
18194 if (declaration)
18196 if (!old_die || !get_AT (old_die, DW_AT_inline))
18198 add_AT_flag (subr_die, DW_AT_declaration, 1);
18200 /* If this is an explicit function declaration then generate
18201 a DW_AT_explicit attribute. */
18202 if (lang_hooks.decls.function_decl_explicit_p (decl)
18203 && (dwarf_version >= 3 || !dwarf_strict))
18204 add_AT_flag (subr_die, DW_AT_explicit, 1);
18206 /* The first time we see a member function, it is in the context of
18207 the class to which it belongs. We make sure of this by emitting
18208 the class first. The next time is the definition, which is
18209 handled above. The two may come from the same source text.
18211 Note that force_decl_die() forces function declaration die. It is
18212 later reused to represent definition. */
18213 equate_decl_number_to_die (decl, subr_die);
18216 else if (DECL_ABSTRACT (decl))
18218 if (DECL_DECLARED_INLINE_P (decl))
18220 if (cgraph_function_possibly_inlined_p (decl))
18221 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18222 else
18223 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18225 else
18227 if (cgraph_function_possibly_inlined_p (decl))
18228 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18229 else
18230 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18233 if (DECL_DECLARED_INLINE_P (decl)
18234 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18235 add_AT_flag (subr_die, DW_AT_artificial, 1);
18237 equate_decl_number_to_die (decl, subr_die);
18239 else if (!DECL_EXTERNAL (decl))
18241 HOST_WIDE_INT cfa_fb_offset;
18242 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18244 if (!old_die || !get_AT (old_die, DW_AT_inline))
18245 equate_decl_number_to_die (decl, subr_die);
18247 gcc_checking_assert (fun);
18248 if (!flag_reorder_blocks_and_partition)
18250 dw_fde_ref fde = fun->fde;
18251 if (fde->dw_fde_begin)
18253 /* We have already generated the labels. */
18254 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18255 fde->dw_fde_end, false);
18257 else
18259 /* Create start/end labels and add the range. */
18260 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18261 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18262 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18263 current_function_funcdef_no);
18264 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18265 current_function_funcdef_no);
18266 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18267 false);
18270 #if VMS_DEBUGGING_INFO
18271 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18272 Section 2.3 Prologue and Epilogue Attributes:
18273 When a breakpoint is set on entry to a function, it is generally
18274 desirable for execution to be suspended, not on the very first
18275 instruction of the function, but rather at a point after the
18276 function's frame has been set up, after any language defined local
18277 declaration processing has been completed, and before execution of
18278 the first statement of the function begins. Debuggers generally
18279 cannot properly determine where this point is. Similarly for a
18280 breakpoint set on exit from a function. The prologue and epilogue
18281 attributes allow a compiler to communicate the location(s) to use. */
18284 if (fde->dw_fde_vms_end_prologue)
18285 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18286 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18288 if (fde->dw_fde_vms_begin_epilogue)
18289 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18290 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18292 #endif
18295 else
18297 /* Generate pubnames entries for the split function code ranges. */
18298 dw_fde_ref fde = fun->fde;
18300 if (fde->dw_fde_second_begin)
18302 if (dwarf_version >= 3 || !dwarf_strict)
18304 /* We should use ranges for non-contiguous code section
18305 addresses. Use the actual code range for the initial
18306 section, since the HOT/COLD labels might precede an
18307 alignment offset. */
18308 bool range_list_added = false;
18309 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18310 fde->dw_fde_end, &range_list_added,
18311 false);
18312 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18313 fde->dw_fde_second_end,
18314 &range_list_added, false);
18315 if (range_list_added)
18316 add_ranges (NULL);
18318 else
18320 /* There is no real support in DW2 for this .. so we make
18321 a work-around. First, emit the pub name for the segment
18322 containing the function label. Then make and emit a
18323 simplified subprogram DIE for the second segment with the
18324 name pre-fixed by __hot/cold_sect_of_. We use the same
18325 linkage name for the second die so that gdb will find both
18326 sections when given "b foo". */
18327 const char *name = NULL;
18328 tree decl_name = DECL_NAME (decl);
18329 dw_die_ref seg_die;
18331 /* Do the 'primary' section. */
18332 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18333 fde->dw_fde_end, false);
18335 /* Build a minimal DIE for the secondary section. */
18336 seg_die = new_die (DW_TAG_subprogram,
18337 subr_die->die_parent, decl);
18339 if (TREE_PUBLIC (decl))
18340 add_AT_flag (seg_die, DW_AT_external, 1);
18342 if (decl_name != NULL
18343 && IDENTIFIER_POINTER (decl_name) != NULL)
18345 name = dwarf2_name (decl, 1);
18346 if (! DECL_ARTIFICIAL (decl))
18347 add_src_coords_attributes (seg_die, decl);
18349 add_linkage_name (seg_die, decl);
18351 gcc_assert (name != NULL);
18352 add_pure_or_virtual_attribute (seg_die, decl);
18353 if (DECL_ARTIFICIAL (decl))
18354 add_AT_flag (seg_die, DW_AT_artificial, 1);
18356 name = concat ("__second_sect_of_", name, NULL);
18357 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18358 fde->dw_fde_second_end, false);
18359 add_name_attribute (seg_die, name);
18360 if (want_pubnames ())
18361 add_pubname_string (name, seg_die);
18364 else
18365 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18366 false);
18369 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18371 /* We define the "frame base" as the function's CFA. This is more
18372 convenient for several reasons: (1) It's stable across the prologue
18373 and epilogue, which makes it better than just a frame pointer,
18374 (2) With dwarf3, there exists a one-byte encoding that allows us
18375 to reference the .debug_frame data by proxy, but failing that,
18376 (3) We can at least reuse the code inspection and interpretation
18377 code that determines the CFA position at various points in the
18378 function. */
18379 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18381 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18382 add_AT_loc (subr_die, DW_AT_frame_base, op);
18384 else
18386 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18387 if (list->dw_loc_next)
18388 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18389 else
18390 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18393 /* Compute a displacement from the "steady-state frame pointer" to
18394 the CFA. The former is what all stack slots and argument slots
18395 will reference in the rtl; the latter is what we've told the
18396 debugger about. We'll need to adjust all frame_base references
18397 by this displacement. */
18398 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18400 if (fun->static_chain_decl)
18401 add_AT_location_description (subr_die, DW_AT_static_link,
18402 loc_list_from_tree (fun->static_chain_decl, 2));
18405 /* Generate child dies for template paramaters. */
18406 if (debug_info_level > DINFO_LEVEL_TERSE)
18407 gen_generic_params_dies (decl);
18409 /* Now output descriptions of the arguments for this function. This gets
18410 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18411 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18412 `...' at the end of the formal parameter list. In order to find out if
18413 there was a trailing ellipsis or not, we must instead look at the type
18414 associated with the FUNCTION_DECL. This will be a node of type
18415 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18416 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18417 an ellipsis at the end. */
18419 /* In the case where we are describing a mere function declaration, all we
18420 need to do here (and all we *can* do here) is to describe the *types* of
18421 its formal parameters. */
18422 if (debug_info_level <= DINFO_LEVEL_TERSE)
18424 else if (declaration)
18425 gen_formal_types_die (decl, subr_die);
18426 else
18428 /* Generate DIEs to represent all known formal parameters. */
18429 tree parm = DECL_ARGUMENTS (decl);
18430 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18431 tree generic_decl_parm = generic_decl
18432 ? DECL_ARGUMENTS (generic_decl)
18433 : NULL;
18435 /* Now we want to walk the list of parameters of the function and
18436 emit their relevant DIEs.
18438 We consider the case of DECL being an instance of a generic function
18439 as well as it being a normal function.
18441 If DECL is an instance of a generic function we walk the
18442 parameters of the generic function declaration _and_ the parameters of
18443 DECL itself. This is useful because we want to emit specific DIEs for
18444 function parameter packs and those are declared as part of the
18445 generic function declaration. In that particular case,
18446 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18447 That DIE has children DIEs representing the set of arguments
18448 of the pack. Note that the set of pack arguments can be empty.
18449 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18450 children DIE.
18452 Otherwise, we just consider the parameters of DECL. */
18453 while (generic_decl_parm || parm)
18455 if (generic_decl_parm
18456 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18457 gen_formal_parameter_pack_die (generic_decl_parm,
18458 parm, subr_die,
18459 &parm);
18460 else if (parm)
18462 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18464 if (parm == DECL_ARGUMENTS (decl)
18465 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18466 && parm_die
18467 && (dwarf_version >= 3 || !dwarf_strict))
18468 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18470 parm = DECL_CHAIN (parm);
18473 if (generic_decl_parm)
18474 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18477 /* Decide whether we need an unspecified_parameters DIE at the end.
18478 There are 2 more cases to do this for: 1) the ansi ... declaration -
18479 this is detectable when the end of the arg list is not a
18480 void_type_node 2) an unprototyped function declaration (not a
18481 definition). This just means that we have no info about the
18482 parameters at all. */
18483 if (prototype_p (TREE_TYPE (decl)))
18485 /* This is the prototyped case, check for.... */
18486 if (stdarg_p (TREE_TYPE (decl)))
18487 gen_unspecified_parameters_die (decl, subr_die);
18489 else if (DECL_INITIAL (decl) == NULL_TREE)
18490 gen_unspecified_parameters_die (decl, subr_die);
18493 /* Output Dwarf info for all of the stuff within the body of the function
18494 (if it has one - it may be just a declaration). */
18495 outer_scope = DECL_INITIAL (decl);
18497 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18498 a function. This BLOCK actually represents the outermost binding contour
18499 for the function, i.e. the contour in which the function's formal
18500 parameters and labels get declared. Curiously, it appears that the front
18501 end doesn't actually put the PARM_DECL nodes for the current function onto
18502 the BLOCK_VARS list for this outer scope, but are strung off of the
18503 DECL_ARGUMENTS list for the function instead.
18505 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18506 the LABEL_DECL nodes for the function however, and we output DWARF info
18507 for those in decls_for_scope. Just within the `outer_scope' there will be
18508 a BLOCK node representing the function's outermost pair of curly braces,
18509 and any blocks used for the base and member initializers of a C++
18510 constructor function. */
18511 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18513 int call_site_note_count = 0;
18514 int tail_call_site_note_count = 0;
18516 /* Emit a DW_TAG_variable DIE for a named return value. */
18517 if (DECL_NAME (DECL_RESULT (decl)))
18518 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18520 current_function_has_inlines = 0;
18521 decls_for_scope (outer_scope, subr_die, 0);
18523 if (call_arg_locations && !dwarf_strict)
18525 struct call_arg_loc_node *ca_loc;
18526 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18528 dw_die_ref die = NULL;
18529 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18530 rtx arg, next_arg;
18532 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18533 arg; arg = next_arg)
18535 dw_loc_descr_ref reg, val;
18536 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18537 dw_die_ref cdie, tdie = NULL;
18539 next_arg = XEXP (arg, 1);
18540 if (REG_P (XEXP (XEXP (arg, 0), 0))
18541 && next_arg
18542 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18543 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18544 && REGNO (XEXP (XEXP (arg, 0), 0))
18545 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18546 next_arg = XEXP (next_arg, 1);
18547 if (mode == VOIDmode)
18549 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18550 if (mode == VOIDmode)
18551 mode = GET_MODE (XEXP (arg, 0));
18553 if (mode == VOIDmode || mode == BLKmode)
18554 continue;
18555 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18557 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18558 tloc = XEXP (XEXP (arg, 0), 1);
18559 continue;
18561 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18562 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18564 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18565 tlocc = XEXP (XEXP (arg, 0), 1);
18566 continue;
18568 reg = NULL;
18569 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18570 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18571 VAR_INIT_STATUS_INITIALIZED);
18572 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18574 rtx mem = XEXP (XEXP (arg, 0), 0);
18575 reg = mem_loc_descriptor (XEXP (mem, 0),
18576 get_address_mode (mem),
18577 GET_MODE (mem),
18578 VAR_INIT_STATUS_INITIALIZED);
18580 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18581 == DEBUG_PARAMETER_REF)
18583 tree tdecl
18584 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18585 tdie = lookup_decl_die (tdecl);
18586 if (tdie == NULL)
18587 continue;
18589 else
18590 continue;
18591 if (reg == NULL
18592 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18593 != DEBUG_PARAMETER_REF)
18594 continue;
18595 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18596 VOIDmode,
18597 VAR_INIT_STATUS_INITIALIZED);
18598 if (val == NULL)
18599 continue;
18600 if (die == NULL)
18601 die = gen_call_site_die (decl, subr_die, ca_loc);
18602 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18603 NULL_TREE);
18604 if (reg != NULL)
18605 add_AT_loc (cdie, DW_AT_location, reg);
18606 else if (tdie != NULL)
18607 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18608 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18609 if (next_arg != XEXP (arg, 1))
18611 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18612 if (mode == VOIDmode)
18613 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18614 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18615 0), 1),
18616 mode, VOIDmode,
18617 VAR_INIT_STATUS_INITIALIZED);
18618 if (val != NULL)
18619 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18622 if (die == NULL
18623 && (ca_loc->symbol_ref || tloc))
18624 die = gen_call_site_die (decl, subr_die, ca_loc);
18625 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18627 dw_loc_descr_ref tval = NULL;
18629 if (tloc != NULL_RTX)
18630 tval = mem_loc_descriptor (tloc,
18631 GET_MODE (tloc) == VOIDmode
18632 ? Pmode : GET_MODE (tloc),
18633 VOIDmode,
18634 VAR_INIT_STATUS_INITIALIZED);
18635 if (tval)
18636 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18637 else if (tlocc != NULL_RTX)
18639 tval = mem_loc_descriptor (tlocc,
18640 GET_MODE (tlocc) == VOIDmode
18641 ? Pmode : GET_MODE (tlocc),
18642 VOIDmode,
18643 VAR_INIT_STATUS_INITIALIZED);
18644 if (tval)
18645 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18646 tval);
18649 if (die != NULL)
18651 call_site_note_count++;
18652 if (ca_loc->tail_call_p)
18653 tail_call_site_note_count++;
18657 call_arg_locations = NULL;
18658 call_arg_loc_last = NULL;
18659 if (tail_call_site_count >= 0
18660 && tail_call_site_count == tail_call_site_note_count
18661 && !dwarf_strict)
18663 if (call_site_count >= 0
18664 && call_site_count == call_site_note_count)
18665 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18666 else
18667 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18669 call_site_count = -1;
18670 tail_call_site_count = -1;
18672 /* Add the calling convention attribute if requested. */
18673 add_calling_convention_attribute (subr_die, decl);
18677 /* Returns a hash value for X (which really is a die_struct). */
18679 static hashval_t
18680 common_block_die_table_hash (const void *x)
18682 const_dw_die_ref d = (const_dw_die_ref) x;
18683 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18686 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18687 as decl_id and die_parent of die_struct Y. */
18689 static int
18690 common_block_die_table_eq (const void *x, const void *y)
18692 const_dw_die_ref d = (const_dw_die_ref) x;
18693 const_dw_die_ref e = (const_dw_die_ref) y;
18694 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18697 /* Generate a DIE to represent a declared data object.
18698 Either DECL or ORIGIN must be non-null. */
18700 static void
18701 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18703 HOST_WIDE_INT off = 0;
18704 tree com_decl;
18705 tree decl_or_origin = decl ? decl : origin;
18706 tree ultimate_origin;
18707 dw_die_ref var_die;
18708 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18709 dw_die_ref origin_die;
18710 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18711 || class_or_namespace_scope_p (context_die));
18712 bool specialization_p = false;
18714 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18715 if (decl || ultimate_origin)
18716 origin = ultimate_origin;
18717 com_decl = fortran_common (decl_or_origin, &off);
18719 /* Symbol in common gets emitted as a child of the common block, in the form
18720 of a data member. */
18721 if (com_decl)
18723 dw_die_ref com_die;
18724 dw_loc_list_ref loc;
18725 die_node com_die_arg;
18727 var_die = lookup_decl_die (decl_or_origin);
18728 if (var_die)
18730 if (get_AT (var_die, DW_AT_location) == NULL)
18732 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18733 if (loc)
18735 if (off)
18737 /* Optimize the common case. */
18738 if (single_element_loc_list_p (loc)
18739 && loc->expr->dw_loc_opc == DW_OP_addr
18740 && loc->expr->dw_loc_next == NULL
18741 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18742 == SYMBOL_REF)
18744 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18745 loc->expr->dw_loc_oprnd1.v.val_addr
18746 = plus_constant (GET_MODE (x), x , off);
18748 else
18749 loc_list_plus_const (loc, off);
18751 add_AT_location_description (var_die, DW_AT_location, loc);
18752 remove_AT (var_die, DW_AT_declaration);
18755 return;
18758 if (common_block_die_table == NULL)
18759 common_block_die_table
18760 = htab_create_ggc (10, common_block_die_table_hash,
18761 common_block_die_table_eq, NULL);
18763 com_die_arg.decl_id = DECL_UID (com_decl);
18764 com_die_arg.die_parent = context_die;
18765 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18766 loc = loc_list_from_tree (com_decl, 2);
18767 if (com_die == NULL)
18769 const char *cnam
18770 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18771 void **slot;
18773 com_die = new_die (DW_TAG_common_block, context_die, decl);
18774 add_name_and_src_coords_attributes (com_die, com_decl);
18775 if (loc)
18777 add_AT_location_description (com_die, DW_AT_location, loc);
18778 /* Avoid sharing the same loc descriptor between
18779 DW_TAG_common_block and DW_TAG_variable. */
18780 loc = loc_list_from_tree (com_decl, 2);
18782 else if (DECL_EXTERNAL (decl))
18783 add_AT_flag (com_die, DW_AT_declaration, 1);
18784 if (want_pubnames ())
18785 add_pubname_string (cnam, com_die); /* ??? needed? */
18786 com_die->decl_id = DECL_UID (com_decl);
18787 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18788 *slot = (void *) com_die;
18790 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18792 add_AT_location_description (com_die, DW_AT_location, loc);
18793 loc = loc_list_from_tree (com_decl, 2);
18794 remove_AT (com_die, DW_AT_declaration);
18796 var_die = new_die (DW_TAG_variable, com_die, decl);
18797 add_name_and_src_coords_attributes (var_die, decl);
18798 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18799 TREE_THIS_VOLATILE (decl), context_die);
18800 add_AT_flag (var_die, DW_AT_external, 1);
18801 if (loc)
18803 if (off)
18805 /* Optimize the common case. */
18806 if (single_element_loc_list_p (loc)
18807 && loc->expr->dw_loc_opc == DW_OP_addr
18808 && loc->expr->dw_loc_next == NULL
18809 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18811 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18812 loc->expr->dw_loc_oprnd1.v.val_addr
18813 = plus_constant (GET_MODE (x), x, off);
18815 else
18816 loc_list_plus_const (loc, off);
18818 add_AT_location_description (var_die, DW_AT_location, loc);
18820 else if (DECL_EXTERNAL (decl))
18821 add_AT_flag (var_die, DW_AT_declaration, 1);
18822 equate_decl_number_to_die (decl, var_die);
18823 return;
18826 /* If the compiler emitted a definition for the DECL declaration
18827 and if we already emitted a DIE for it, don't emit a second
18828 DIE for it again. Allow re-declarations of DECLs that are
18829 inside functions, though. */
18830 if (old_die && declaration && !local_scope_p (context_die))
18831 return;
18833 /* For static data members, the declaration in the class is supposed
18834 to have DW_TAG_member tag; the specification should still be
18835 DW_TAG_variable referencing the DW_TAG_member DIE. */
18836 if (declaration && class_scope_p (context_die))
18837 var_die = new_die (DW_TAG_member, context_die, decl);
18838 else
18839 var_die = new_die (DW_TAG_variable, context_die, decl);
18841 origin_die = NULL;
18842 if (origin != NULL)
18843 origin_die = add_abstract_origin_attribute (var_die, origin);
18845 /* Loop unrolling can create multiple blocks that refer to the same
18846 static variable, so we must test for the DW_AT_declaration flag.
18848 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18849 copy decls and set the DECL_ABSTRACT flag on them instead of
18850 sharing them.
18852 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18854 ??? The declare_in_namespace support causes us to get two DIEs for one
18855 variable, both of which are declarations. We want to avoid considering
18856 one to be a specification, so we must test that this DIE is not a
18857 declaration. */
18858 else if (old_die && TREE_STATIC (decl) && ! declaration
18859 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18861 /* This is a definition of a C++ class level static. */
18862 add_AT_specification (var_die, old_die);
18863 specialization_p = true;
18864 if (DECL_NAME (decl))
18866 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18867 struct dwarf_file_data * file_index = lookup_filename (s.file);
18869 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18870 add_AT_file (var_die, DW_AT_decl_file, file_index);
18872 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18873 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18875 if (old_die->die_tag == DW_TAG_member)
18876 add_linkage_name (var_die, decl);
18879 else
18880 add_name_and_src_coords_attributes (var_die, decl);
18882 if ((origin == NULL && !specialization_p)
18883 || (origin != NULL
18884 && !DECL_ABSTRACT (decl_or_origin)
18885 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18886 decl_function_context
18887 (decl_or_origin))))
18889 tree type = TREE_TYPE (decl_or_origin);
18891 if (decl_by_reference_p (decl_or_origin))
18892 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18893 else
18894 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18895 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18898 if (origin == NULL && !specialization_p)
18900 if (TREE_PUBLIC (decl))
18901 add_AT_flag (var_die, DW_AT_external, 1);
18903 if (DECL_ARTIFICIAL (decl))
18904 add_AT_flag (var_die, DW_AT_artificial, 1);
18906 add_accessibility_attribute (var_die, decl);
18909 if (declaration)
18910 add_AT_flag (var_die, DW_AT_declaration, 1);
18912 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18913 equate_decl_number_to_die (decl, var_die);
18915 if (! declaration
18916 && (! DECL_ABSTRACT (decl_or_origin)
18917 /* Local static vars are shared between all clones/inlines,
18918 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18919 already set. */
18920 || (TREE_CODE (decl_or_origin) == VAR_DECL
18921 && TREE_STATIC (decl_or_origin)
18922 && DECL_RTL_SET_P (decl_or_origin)))
18923 /* When abstract origin already has DW_AT_location attribute, no need
18924 to add it again. */
18925 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18927 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18928 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18929 defer_location (decl_or_origin, var_die);
18930 else
18931 add_location_or_const_value_attribute (var_die, decl_or_origin,
18932 decl == NULL, DW_AT_location);
18933 add_pubname (decl_or_origin, var_die);
18935 else
18936 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18939 /* Generate a DIE to represent a named constant. */
18941 static void
18942 gen_const_die (tree decl, dw_die_ref context_die)
18944 dw_die_ref const_die;
18945 tree type = TREE_TYPE (decl);
18947 const_die = new_die (DW_TAG_constant, context_die, decl);
18948 add_name_and_src_coords_attributes (const_die, decl);
18949 add_type_attribute (const_die, type, 1, 0, context_die);
18950 if (TREE_PUBLIC (decl))
18951 add_AT_flag (const_die, DW_AT_external, 1);
18952 if (DECL_ARTIFICIAL (decl))
18953 add_AT_flag (const_die, DW_AT_artificial, 1);
18954 tree_add_const_value_attribute_for_decl (const_die, decl);
18957 /* Generate a DIE to represent a label identifier. */
18959 static void
18960 gen_label_die (tree decl, dw_die_ref context_die)
18962 tree origin = decl_ultimate_origin (decl);
18963 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18964 rtx insn;
18965 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18967 if (origin != NULL)
18968 add_abstract_origin_attribute (lbl_die, origin);
18969 else
18970 add_name_and_src_coords_attributes (lbl_die, decl);
18972 if (DECL_ABSTRACT (decl))
18973 equate_decl_number_to_die (decl, lbl_die);
18974 else
18976 insn = DECL_RTL_IF_SET (decl);
18978 /* Deleted labels are programmer specified labels which have been
18979 eliminated because of various optimizations. We still emit them
18980 here so that it is possible to put breakpoints on them. */
18981 if (insn
18982 && (LABEL_P (insn)
18983 || ((NOTE_P (insn)
18984 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18986 /* When optimization is enabled (via -O) some parts of the compiler
18987 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18988 represent source-level labels which were explicitly declared by
18989 the user. This really shouldn't be happening though, so catch
18990 it if it ever does happen. */
18991 gcc_assert (!INSN_DELETED_P (insn));
18993 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18994 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18996 else if (insn
18997 && NOTE_P (insn)
18998 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18999 && CODE_LABEL_NUMBER (insn) != -1)
19001 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19002 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19007 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19008 attributes to the DIE for a block STMT, to describe where the inlined
19009 function was called from. This is similar to add_src_coords_attributes. */
19011 static inline void
19012 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19014 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19016 if (dwarf_version >= 3 || !dwarf_strict)
19018 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19019 add_AT_unsigned (die, DW_AT_call_line, s.line);
19024 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19025 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19027 static inline void
19028 add_high_low_attributes (tree stmt, dw_die_ref die)
19030 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19032 if (BLOCK_FRAGMENT_CHAIN (stmt)
19033 && (dwarf_version >= 3 || !dwarf_strict))
19035 tree chain, superblock = NULL_TREE;
19036 dw_die_ref pdie;
19037 dw_attr_ref attr = NULL;
19039 if (inlined_function_outer_scope_p (stmt))
19041 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19042 BLOCK_NUMBER (stmt));
19043 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19046 /* Optimize duplicate .debug_ranges lists or even tails of
19047 lists. If this BLOCK has same ranges as its supercontext,
19048 lookup DW_AT_ranges attribute in the supercontext (and
19049 recursively so), verify that the ranges_table contains the
19050 right values and use it instead of adding a new .debug_range. */
19051 for (chain = stmt, pdie = die;
19052 BLOCK_SAME_RANGE (chain);
19053 chain = BLOCK_SUPERCONTEXT (chain))
19055 dw_attr_ref new_attr;
19057 pdie = pdie->die_parent;
19058 if (pdie == NULL)
19059 break;
19060 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19061 break;
19062 new_attr = get_AT (pdie, DW_AT_ranges);
19063 if (new_attr == NULL
19064 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19065 break;
19066 attr = new_attr;
19067 superblock = BLOCK_SUPERCONTEXT (chain);
19069 if (attr != NULL
19070 && (ranges_table[attr->dw_attr_val.v.val_offset
19071 / 2 / DWARF2_ADDR_SIZE].num
19072 == BLOCK_NUMBER (superblock))
19073 && BLOCK_FRAGMENT_CHAIN (superblock))
19075 unsigned long off = attr->dw_attr_val.v.val_offset
19076 / 2 / DWARF2_ADDR_SIZE;
19077 unsigned long supercnt = 0, thiscnt = 0;
19078 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19079 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19081 ++supercnt;
19082 gcc_checking_assert (ranges_table[off + supercnt].num
19083 == BLOCK_NUMBER (chain));
19085 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19086 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19087 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19088 ++thiscnt;
19089 gcc_assert (supercnt >= thiscnt);
19090 add_AT_range_list (die, DW_AT_ranges,
19091 ((off + supercnt - thiscnt)
19092 * 2 * DWARF2_ADDR_SIZE),
19093 false);
19094 return;
19097 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19099 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19102 add_ranges (chain);
19103 chain = BLOCK_FRAGMENT_CHAIN (chain);
19105 while (chain);
19106 add_ranges (NULL);
19108 else
19110 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19111 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19112 BLOCK_NUMBER (stmt));
19113 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19114 BLOCK_NUMBER (stmt));
19115 add_AT_low_high_pc (die, label, label_high, false);
19119 /* Generate a DIE for a lexical block. */
19121 static void
19122 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19124 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19126 if (call_arg_locations)
19128 if (block_map.length () <= BLOCK_NUMBER (stmt))
19129 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19130 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19133 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19134 add_high_low_attributes (stmt, stmt_die);
19136 decls_for_scope (stmt, stmt_die, depth);
19139 /* Generate a DIE for an inlined subprogram. */
19141 static void
19142 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19144 tree decl;
19146 /* The instance of function that is effectively being inlined shall not
19147 be abstract. */
19148 gcc_assert (! BLOCK_ABSTRACT (stmt));
19150 decl = block_ultimate_origin (stmt);
19152 /* Emit info for the abstract instance first, if we haven't yet. We
19153 must emit this even if the block is abstract, otherwise when we
19154 emit the block below (or elsewhere), we may end up trying to emit
19155 a die whose origin die hasn't been emitted, and crashing. */
19156 dwarf2out_abstract_function (decl);
19158 if (! BLOCK_ABSTRACT (stmt))
19160 dw_die_ref subr_die
19161 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19163 if (call_arg_locations)
19165 if (block_map.length () <= BLOCK_NUMBER (stmt))
19166 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19167 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19169 add_abstract_origin_attribute (subr_die, decl);
19170 if (TREE_ASM_WRITTEN (stmt))
19171 add_high_low_attributes (stmt, subr_die);
19172 add_call_src_coords_attributes (stmt, subr_die);
19174 decls_for_scope (stmt, subr_die, depth);
19175 current_function_has_inlines = 1;
19179 /* Generate a DIE for a field in a record, or structure. */
19181 static void
19182 gen_field_die (tree decl, dw_die_ref context_die)
19184 dw_die_ref decl_die;
19186 if (TREE_TYPE (decl) == error_mark_node)
19187 return;
19189 decl_die = new_die (DW_TAG_member, context_die, decl);
19190 add_name_and_src_coords_attributes (decl_die, decl);
19191 add_type_attribute (decl_die, member_declared_type (decl),
19192 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19193 context_die);
19195 if (DECL_BIT_FIELD_TYPE (decl))
19197 add_byte_size_attribute (decl_die, decl);
19198 add_bit_size_attribute (decl_die, decl);
19199 add_bit_offset_attribute (decl_die, decl);
19202 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19203 add_data_member_location_attribute (decl_die, decl);
19205 if (DECL_ARTIFICIAL (decl))
19206 add_AT_flag (decl_die, DW_AT_artificial, 1);
19208 add_accessibility_attribute (decl_die, decl);
19210 /* Equate decl number to die, so that we can look up this decl later on. */
19211 equate_decl_number_to_die (decl, decl_die);
19214 #if 0
19215 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19216 Use modified_type_die instead.
19217 We keep this code here just in case these types of DIEs may be needed to
19218 represent certain things in other languages (e.g. Pascal) someday. */
19220 static void
19221 gen_pointer_type_die (tree type, dw_die_ref context_die)
19223 dw_die_ref ptr_die
19224 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19226 equate_type_number_to_die (type, ptr_die);
19227 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19228 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19231 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19232 Use modified_type_die instead.
19233 We keep this code here just in case these types of DIEs may be needed to
19234 represent certain things in other languages (e.g. Pascal) someday. */
19236 static void
19237 gen_reference_type_die (tree type, dw_die_ref context_die)
19239 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19241 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19242 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19243 else
19244 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19246 equate_type_number_to_die (type, ref_die);
19247 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19248 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19250 #endif
19252 /* Generate a DIE for a pointer to a member type. */
19254 static void
19255 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19257 dw_die_ref ptr_die
19258 = new_die (DW_TAG_ptr_to_member_type,
19259 scope_die_for (type, context_die), type);
19261 equate_type_number_to_die (type, ptr_die);
19262 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19263 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19264 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19267 typedef const char *dchar_p; /* For DEF_VEC_P. */
19269 static char *producer_string;
19271 /* Return a heap allocated producer string including command line options
19272 if -grecord-gcc-switches. */
19274 static char *
19275 gen_producer_string (void)
19277 size_t j;
19278 vec<dchar_p> switches = vNULL;
19279 const char *language_string = lang_hooks.name;
19280 char *producer, *tail;
19281 const char *p;
19282 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19283 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19285 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19286 switch (save_decoded_options[j].opt_index)
19288 case OPT_o:
19289 case OPT_d:
19290 case OPT_dumpbase:
19291 case OPT_dumpdir:
19292 case OPT_auxbase:
19293 case OPT_auxbase_strip:
19294 case OPT_quiet:
19295 case OPT_version:
19296 case OPT_v:
19297 case OPT_w:
19298 case OPT_L:
19299 case OPT_D:
19300 case OPT_I:
19301 case OPT_U:
19302 case OPT_SPECIAL_unknown:
19303 case OPT_SPECIAL_ignore:
19304 case OPT_SPECIAL_program_name:
19305 case OPT_SPECIAL_input_file:
19306 case OPT_grecord_gcc_switches:
19307 case OPT_gno_record_gcc_switches:
19308 case OPT__output_pch_:
19309 case OPT_fdiagnostics_show_location_:
19310 case OPT_fdiagnostics_show_option:
19311 case OPT_fdiagnostics_show_caret:
19312 case OPT_fdiagnostics_color_:
19313 case OPT_fverbose_asm:
19314 case OPT____:
19315 case OPT__sysroot_:
19316 case OPT_nostdinc:
19317 case OPT_nostdinc__:
19318 /* Ignore these. */
19319 continue;
19320 default:
19321 if (cl_options[save_decoded_options[j].opt_index].flags
19322 & CL_NO_DWARF_RECORD)
19323 continue;
19324 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19325 == '-');
19326 switch (save_decoded_options[j].canonical_option[0][1])
19328 case 'M':
19329 case 'i':
19330 case 'W':
19331 continue;
19332 case 'f':
19333 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19334 "dump", 4) == 0)
19335 continue;
19336 break;
19337 default:
19338 break;
19340 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19341 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19342 break;
19345 producer = XNEWVEC (char, plen + 1 + len + 1);
19346 tail = producer;
19347 sprintf (tail, "%s %s", language_string, version_string);
19348 tail += plen;
19350 FOR_EACH_VEC_ELT (switches, j, p)
19352 len = strlen (p);
19353 *tail = ' ';
19354 memcpy (tail + 1, p, len);
19355 tail += len + 1;
19358 *tail = '\0';
19359 switches.release ();
19360 return producer;
19363 /* Generate the DIE for the compilation unit. */
19365 static dw_die_ref
19366 gen_compile_unit_die (const char *filename)
19368 dw_die_ref die;
19369 const char *language_string = lang_hooks.name;
19370 int language;
19372 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19374 if (filename)
19376 add_name_attribute (die, filename);
19377 /* Don't add cwd for <built-in>. */
19378 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19379 add_comp_dir_attribute (die);
19382 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19384 /* If our producer is LTO try to figure out a common language to use
19385 from the global list of translation units. */
19386 if (strcmp (language_string, "GNU GIMPLE") == 0)
19388 unsigned i;
19389 tree t;
19390 const char *common_lang = NULL;
19392 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19394 if (!TRANSLATION_UNIT_LANGUAGE (t))
19395 continue;
19396 if (!common_lang)
19397 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19398 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19400 else if (strncmp (common_lang, "GNU C", 5) == 0
19401 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19402 /* Mixing C and C++ is ok, use C++ in that case. */
19403 common_lang = "GNU C++";
19404 else
19406 /* Fall back to C. */
19407 common_lang = NULL;
19408 break;
19412 if (common_lang)
19413 language_string = common_lang;
19416 language = DW_LANG_C89;
19417 if (strcmp (language_string, "GNU C++") == 0)
19418 language = DW_LANG_C_plus_plus;
19419 else if (strcmp (language_string, "GNU F77") == 0)
19420 language = DW_LANG_Fortran77;
19421 else if (strcmp (language_string, "GNU Pascal") == 0)
19422 language = DW_LANG_Pascal83;
19423 else if (dwarf_version >= 3 || !dwarf_strict)
19425 if (strcmp (language_string, "GNU Ada") == 0)
19426 language = DW_LANG_Ada95;
19427 else if (strcmp (language_string, "GNU Fortran") == 0)
19428 language = DW_LANG_Fortran95;
19429 else if (strcmp (language_string, "GNU Java") == 0)
19430 language = DW_LANG_Java;
19431 else if (strcmp (language_string, "GNU Objective-C") == 0)
19432 language = DW_LANG_ObjC;
19433 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19434 language = DW_LANG_ObjC_plus_plus;
19435 else if (dwarf_version >= 5 || !dwarf_strict)
19437 if (strcmp (language_string, "GNU Go") == 0)
19438 language = DW_LANG_Go;
19441 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19442 else if (strcmp (language_string, "GNU Fortran") == 0)
19443 language = DW_LANG_Fortran90;
19445 add_AT_unsigned (die, DW_AT_language, language);
19447 switch (language)
19449 case DW_LANG_Fortran77:
19450 case DW_LANG_Fortran90:
19451 case DW_LANG_Fortran95:
19452 /* Fortran has case insensitive identifiers and the front-end
19453 lowercases everything. */
19454 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19455 break;
19456 default:
19457 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19458 break;
19460 return die;
19463 /* Generate the DIE for a base class. */
19465 static void
19466 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19468 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19470 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19471 add_data_member_location_attribute (die, binfo);
19473 if (BINFO_VIRTUAL_P (binfo))
19474 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19476 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19477 children, otherwise the default is DW_ACCESS_public. In DWARF2
19478 the default has always been DW_ACCESS_private. */
19479 if (access == access_public_node)
19481 if (dwarf_version == 2
19482 || context_die->die_tag == DW_TAG_class_type)
19483 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19485 else if (access == access_protected_node)
19486 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19487 else if (dwarf_version > 2
19488 && context_die->die_tag != DW_TAG_class_type)
19489 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19492 /* Generate a DIE for a class member. */
19494 static void
19495 gen_member_die (tree type, dw_die_ref context_die)
19497 tree member;
19498 tree binfo = TYPE_BINFO (type);
19499 dw_die_ref child;
19501 /* If this is not an incomplete type, output descriptions of each of its
19502 members. Note that as we output the DIEs necessary to represent the
19503 members of this record or union type, we will also be trying to output
19504 DIEs to represent the *types* of those members. However the `type'
19505 function (above) will specifically avoid generating type DIEs for member
19506 types *within* the list of member DIEs for this (containing) type except
19507 for those types (of members) which are explicitly marked as also being
19508 members of this (containing) type themselves. The g++ front- end can
19509 force any given type to be treated as a member of some other (containing)
19510 type by setting the TYPE_CONTEXT of the given (member) type to point to
19511 the TREE node representing the appropriate (containing) type. */
19513 /* First output info about the base classes. */
19514 if (binfo)
19516 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19517 int i;
19518 tree base;
19520 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19521 gen_inheritance_die (base,
19522 (accesses ? (*accesses)[i] : access_public_node),
19523 context_die);
19526 /* Now output info about the data members and type members. */
19527 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19529 /* If we thought we were generating minimal debug info for TYPE
19530 and then changed our minds, some of the member declarations
19531 may have already been defined. Don't define them again, but
19532 do put them in the right order. */
19534 child = lookup_decl_die (member);
19535 if (child)
19536 splice_child_die (context_die, child);
19537 else
19538 gen_decl_die (member, NULL, context_die);
19541 /* Now output info about the function members (if any). */
19542 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19544 /* Don't include clones in the member list. */
19545 if (DECL_ABSTRACT_ORIGIN (member))
19546 continue;
19548 child = lookup_decl_die (member);
19549 if (child)
19550 splice_child_die (context_die, child);
19551 else
19552 gen_decl_die (member, NULL, context_die);
19556 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19557 is set, we pretend that the type was never defined, so we only get the
19558 member DIEs needed by later specification DIEs. */
19560 static void
19561 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19562 enum debug_info_usage usage)
19564 dw_die_ref type_die = lookup_type_die (type);
19565 dw_die_ref scope_die = 0;
19566 int nested = 0;
19567 int complete = (TYPE_SIZE (type)
19568 && (! TYPE_STUB_DECL (type)
19569 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19570 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19571 complete = complete && should_emit_struct_debug (type, usage);
19573 if (type_die && ! complete)
19574 return;
19576 if (TYPE_CONTEXT (type) != NULL_TREE
19577 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19578 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19579 nested = 1;
19581 scope_die = scope_die_for (type, context_die);
19583 /* Generate child dies for template paramaters. */
19584 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19585 schedule_generic_params_dies_gen (type);
19587 if (! type_die || (nested && is_cu_die (scope_die)))
19588 /* First occurrence of type or toplevel definition of nested class. */
19590 dw_die_ref old_die = type_die;
19592 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19593 ? record_type_tag (type) : DW_TAG_union_type,
19594 scope_die, type);
19595 equate_type_number_to_die (type, type_die);
19596 if (old_die)
19597 add_AT_specification (type_die, old_die);
19598 else
19599 add_name_attribute (type_die, type_tag (type));
19601 else
19602 remove_AT (type_die, DW_AT_declaration);
19604 /* If this type has been completed, then give it a byte_size attribute and
19605 then give a list of members. */
19606 if (complete && !ns_decl)
19608 /* Prevent infinite recursion in cases where the type of some member of
19609 this type is expressed in terms of this type itself. */
19610 TREE_ASM_WRITTEN (type) = 1;
19611 add_byte_size_attribute (type_die, type);
19612 if (TYPE_STUB_DECL (type) != NULL_TREE)
19614 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19615 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19618 /* If the first reference to this type was as the return type of an
19619 inline function, then it may not have a parent. Fix this now. */
19620 if (type_die->die_parent == NULL)
19621 add_child_die (scope_die, type_die);
19623 push_decl_scope (type);
19624 gen_member_die (type, type_die);
19625 pop_decl_scope ();
19627 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19628 if (TYPE_ARTIFICIAL (type))
19629 add_AT_flag (type_die, DW_AT_artificial, 1);
19631 /* GNU extension: Record what type our vtable lives in. */
19632 if (TYPE_VFIELD (type))
19634 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19636 gen_type_die (vtype, context_die);
19637 add_AT_die_ref (type_die, DW_AT_containing_type,
19638 lookup_type_die (vtype));
19641 else
19643 add_AT_flag (type_die, DW_AT_declaration, 1);
19645 /* We don't need to do this for function-local types. */
19646 if (TYPE_STUB_DECL (type)
19647 && ! decl_function_context (TYPE_STUB_DECL (type)))
19648 vec_safe_push (incomplete_types, type);
19651 if (get_AT (type_die, DW_AT_name))
19652 add_pubtype (type, type_die);
19655 /* Generate a DIE for a subroutine _type_. */
19657 static void
19658 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19660 tree return_type = TREE_TYPE (type);
19661 dw_die_ref subr_die
19662 = new_die (DW_TAG_subroutine_type,
19663 scope_die_for (type, context_die), type);
19665 equate_type_number_to_die (type, subr_die);
19666 add_prototyped_attribute (subr_die, type);
19667 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19668 gen_formal_types_die (type, subr_die);
19670 if (get_AT (subr_die, DW_AT_name))
19671 add_pubtype (type, subr_die);
19674 /* Generate a DIE for a type definition. */
19676 static void
19677 gen_typedef_die (tree decl, dw_die_ref context_die)
19679 dw_die_ref type_die;
19680 tree origin;
19682 if (TREE_ASM_WRITTEN (decl))
19683 return;
19685 TREE_ASM_WRITTEN (decl) = 1;
19686 type_die = new_die (DW_TAG_typedef, context_die, decl);
19687 origin = decl_ultimate_origin (decl);
19688 if (origin != NULL)
19689 add_abstract_origin_attribute (type_die, origin);
19690 else
19692 tree type;
19694 add_name_and_src_coords_attributes (type_die, decl);
19695 if (DECL_ORIGINAL_TYPE (decl))
19697 type = DECL_ORIGINAL_TYPE (decl);
19699 gcc_assert (type != TREE_TYPE (decl));
19700 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19702 else
19704 type = TREE_TYPE (decl);
19706 if (is_naming_typedef_decl (TYPE_NAME (type)))
19708 /* Here, we are in the case of decl being a typedef naming
19709 an anonymous type, e.g:
19710 typedef struct {...} foo;
19711 In that case TREE_TYPE (decl) is not a typedef variant
19712 type and TYPE_NAME of the anonymous type is set to the
19713 TYPE_DECL of the typedef. This construct is emitted by
19714 the C++ FE.
19716 TYPE is the anonymous struct named by the typedef
19717 DECL. As we need the DW_AT_type attribute of the
19718 DW_TAG_typedef to point to the DIE of TYPE, let's
19719 generate that DIE right away. add_type_attribute
19720 called below will then pick (via lookup_type_die) that
19721 anonymous struct DIE. */
19722 if (!TREE_ASM_WRITTEN (type))
19723 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19725 /* This is a GNU Extension. We are adding a
19726 DW_AT_linkage_name attribute to the DIE of the
19727 anonymous struct TYPE. The value of that attribute
19728 is the name of the typedef decl naming the anonymous
19729 struct. This greatly eases the work of consumers of
19730 this debug info. */
19731 add_linkage_attr (lookup_type_die (type), decl);
19735 add_type_attribute (type_die, type, TREE_READONLY (decl),
19736 TREE_THIS_VOLATILE (decl), context_die);
19738 if (is_naming_typedef_decl (decl))
19739 /* We want that all subsequent calls to lookup_type_die with
19740 TYPE in argument yield the DW_TAG_typedef we have just
19741 created. */
19742 equate_type_number_to_die (type, type_die);
19744 add_accessibility_attribute (type_die, decl);
19747 if (DECL_ABSTRACT (decl))
19748 equate_decl_number_to_die (decl, type_die);
19750 if (get_AT (type_die, DW_AT_name))
19751 add_pubtype (decl, type_die);
19754 /* Generate a DIE for a struct, class, enum or union type. */
19756 static void
19757 gen_tagged_type_die (tree type,
19758 dw_die_ref context_die,
19759 enum debug_info_usage usage)
19761 int need_pop;
19763 if (type == NULL_TREE
19764 || !is_tagged_type (type))
19765 return;
19767 /* If this is a nested type whose containing class hasn't been written
19768 out yet, writing it out will cover this one, too. This does not apply
19769 to instantiations of member class templates; they need to be added to
19770 the containing class as they are generated. FIXME: This hurts the
19771 idea of combining type decls from multiple TUs, since we can't predict
19772 what set of template instantiations we'll get. */
19773 if (TYPE_CONTEXT (type)
19774 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19775 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19777 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19779 if (TREE_ASM_WRITTEN (type))
19780 return;
19782 /* If that failed, attach ourselves to the stub. */
19783 push_decl_scope (TYPE_CONTEXT (type));
19784 context_die = lookup_type_die (TYPE_CONTEXT (type));
19785 need_pop = 1;
19787 else if (TYPE_CONTEXT (type) != NULL_TREE
19788 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19790 /* If this type is local to a function that hasn't been written
19791 out yet, use a NULL context for now; it will be fixed up in
19792 decls_for_scope. */
19793 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19794 /* A declaration DIE doesn't count; nested types need to go in the
19795 specification. */
19796 if (context_die && is_declaration_die (context_die))
19797 context_die = NULL;
19798 need_pop = 0;
19800 else
19802 context_die = declare_in_namespace (type, context_die);
19803 need_pop = 0;
19806 if (TREE_CODE (type) == ENUMERAL_TYPE)
19808 /* This might have been written out by the call to
19809 declare_in_namespace. */
19810 if (!TREE_ASM_WRITTEN (type))
19811 gen_enumeration_type_die (type, context_die);
19813 else
19814 gen_struct_or_union_type_die (type, context_die, usage);
19816 if (need_pop)
19817 pop_decl_scope ();
19819 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19820 it up if it is ever completed. gen_*_type_die will set it for us
19821 when appropriate. */
19824 /* Generate a type description DIE. */
19826 static void
19827 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19828 enum debug_info_usage usage)
19830 struct array_descr_info info;
19832 if (type == NULL_TREE || type == error_mark_node)
19833 return;
19835 if (TYPE_NAME (type) != NULL_TREE
19836 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19837 && is_redundant_typedef (TYPE_NAME (type))
19838 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19839 /* The DECL of this type is a typedef we don't want to emit debug
19840 info for but we want debug info for its underlying typedef.
19841 This can happen for e.g, the injected-class-name of a C++
19842 type. */
19843 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19845 /* If TYPE is a typedef type variant, let's generate debug info
19846 for the parent typedef which TYPE is a type of. */
19847 if (typedef_variant_p (type))
19849 if (TREE_ASM_WRITTEN (type))
19850 return;
19852 /* Prevent broken recursion; we can't hand off to the same type. */
19853 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19855 /* Give typedefs the right scope. */
19856 context_die = scope_die_for (type, context_die);
19858 TREE_ASM_WRITTEN (type) = 1;
19860 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19861 return;
19864 /* If type is an anonymous tagged type named by a typedef, let's
19865 generate debug info for the typedef. */
19866 if (is_naming_typedef_decl (TYPE_NAME (type)))
19868 /* Use the DIE of the containing namespace as the parent DIE of
19869 the type description DIE we want to generate. */
19870 if (DECL_CONTEXT (TYPE_NAME (type))
19871 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19872 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19874 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19875 return;
19878 /* If this is an array type with hidden descriptor, handle it first. */
19879 if (!TREE_ASM_WRITTEN (type)
19880 && lang_hooks.types.get_array_descr_info
19881 && lang_hooks.types.get_array_descr_info (type, &info)
19882 && (dwarf_version >= 3 || !dwarf_strict))
19884 gen_descr_array_type_die (type, &info, context_die);
19885 TREE_ASM_WRITTEN (type) = 1;
19886 return;
19889 /* We are going to output a DIE to represent the unqualified version
19890 of this type (i.e. without any const or volatile qualifiers) so
19891 get the main variant (i.e. the unqualified version) of this type
19892 now. (Vectors are special because the debugging info is in the
19893 cloned type itself). */
19894 if (TREE_CODE (type) != VECTOR_TYPE)
19895 type = type_main_variant (type);
19897 if (TREE_ASM_WRITTEN (type))
19898 return;
19900 switch (TREE_CODE (type))
19902 case ERROR_MARK:
19903 break;
19905 case POINTER_TYPE:
19906 case REFERENCE_TYPE:
19907 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19908 ensures that the gen_type_die recursion will terminate even if the
19909 type is recursive. Recursive types are possible in Ada. */
19910 /* ??? We could perhaps do this for all types before the switch
19911 statement. */
19912 TREE_ASM_WRITTEN (type) = 1;
19914 /* For these types, all that is required is that we output a DIE (or a
19915 set of DIEs) to represent the "basis" type. */
19916 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19917 DINFO_USAGE_IND_USE);
19918 break;
19920 case OFFSET_TYPE:
19921 /* This code is used for C++ pointer-to-data-member types.
19922 Output a description of the relevant class type. */
19923 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19924 DINFO_USAGE_IND_USE);
19926 /* Output a description of the type of the object pointed to. */
19927 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19928 DINFO_USAGE_IND_USE);
19930 /* Now output a DIE to represent this pointer-to-data-member type
19931 itself. */
19932 gen_ptr_to_mbr_type_die (type, context_die);
19933 break;
19935 case FUNCTION_TYPE:
19936 /* Force out return type (in case it wasn't forced out already). */
19937 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19938 DINFO_USAGE_DIR_USE);
19939 gen_subroutine_type_die (type, context_die);
19940 break;
19942 case METHOD_TYPE:
19943 /* Force out return type (in case it wasn't forced out already). */
19944 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19945 DINFO_USAGE_DIR_USE);
19946 gen_subroutine_type_die (type, context_die);
19947 break;
19949 case ARRAY_TYPE:
19950 gen_array_type_die (type, context_die);
19951 break;
19953 case VECTOR_TYPE:
19954 gen_array_type_die (type, context_die);
19955 break;
19957 case ENUMERAL_TYPE:
19958 case RECORD_TYPE:
19959 case UNION_TYPE:
19960 case QUAL_UNION_TYPE:
19961 gen_tagged_type_die (type, context_die, usage);
19962 return;
19964 case VOID_TYPE:
19965 case INTEGER_TYPE:
19966 case REAL_TYPE:
19967 case FIXED_POINT_TYPE:
19968 case COMPLEX_TYPE:
19969 case BOOLEAN_TYPE:
19970 /* No DIEs needed for fundamental types. */
19971 break;
19973 case NULLPTR_TYPE:
19974 case LANG_TYPE:
19975 /* Just use DW_TAG_unspecified_type. */
19977 dw_die_ref type_die = lookup_type_die (type);
19978 if (type_die == NULL)
19980 tree name = TYPE_NAME (type);
19981 if (TREE_CODE (name) == TYPE_DECL)
19982 name = DECL_NAME (name);
19983 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19984 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19985 equate_type_number_to_die (type, type_die);
19988 break;
19990 default:
19991 gcc_unreachable ();
19994 TREE_ASM_WRITTEN (type) = 1;
19997 static void
19998 gen_type_die (tree type, dw_die_ref context_die)
20000 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20003 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20004 things which are local to the given block. */
20006 static void
20007 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20009 int must_output_die = 0;
20010 bool inlined_func;
20012 /* Ignore blocks that are NULL. */
20013 if (stmt == NULL_TREE)
20014 return;
20016 inlined_func = inlined_function_outer_scope_p (stmt);
20018 /* If the block is one fragment of a non-contiguous block, do not
20019 process the variables, since they will have been done by the
20020 origin block. Do process subblocks. */
20021 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20023 tree sub;
20025 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20026 gen_block_die (sub, context_die, depth + 1);
20028 return;
20031 /* Determine if we need to output any Dwarf DIEs at all to represent this
20032 block. */
20033 if (inlined_func)
20034 /* The outer scopes for inlinings *must* always be represented. We
20035 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20036 must_output_die = 1;
20037 else
20039 /* Determine if this block directly contains any "significant"
20040 local declarations which we will need to output DIEs for. */
20041 if (debug_info_level > DINFO_LEVEL_TERSE)
20042 /* We are not in terse mode so *any* local declaration counts
20043 as being a "significant" one. */
20044 must_output_die = ((BLOCK_VARS (stmt) != NULL
20045 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20046 && (TREE_USED (stmt)
20047 || TREE_ASM_WRITTEN (stmt)
20048 || BLOCK_ABSTRACT (stmt)));
20049 else if ((TREE_USED (stmt)
20050 || TREE_ASM_WRITTEN (stmt)
20051 || BLOCK_ABSTRACT (stmt))
20052 && !dwarf2out_ignore_block (stmt))
20053 must_output_die = 1;
20056 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20057 DIE for any block which contains no significant local declarations at
20058 all. Rather, in such cases we just call `decls_for_scope' so that any
20059 needed Dwarf info for any sub-blocks will get properly generated. Note
20060 that in terse mode, our definition of what constitutes a "significant"
20061 local declaration gets restricted to include only inlined function
20062 instances and local (nested) function definitions. */
20063 if (must_output_die)
20065 if (inlined_func)
20067 /* If STMT block is abstract, that means we have been called
20068 indirectly from dwarf2out_abstract_function.
20069 That function rightfully marks the descendent blocks (of
20070 the abstract function it is dealing with) as being abstract,
20071 precisely to prevent us from emitting any
20072 DW_TAG_inlined_subroutine DIE as a descendent
20073 of an abstract function instance. So in that case, we should
20074 not call gen_inlined_subroutine_die.
20076 Later though, when cgraph asks dwarf2out to emit info
20077 for the concrete instance of the function decl into which
20078 the concrete instance of STMT got inlined, the later will lead
20079 to the generation of a DW_TAG_inlined_subroutine DIE. */
20080 if (! BLOCK_ABSTRACT (stmt))
20081 gen_inlined_subroutine_die (stmt, context_die, depth);
20083 else
20084 gen_lexical_block_die (stmt, context_die, depth);
20086 else
20087 decls_for_scope (stmt, context_die, depth);
20090 /* Process variable DECL (or variable with origin ORIGIN) within
20091 block STMT and add it to CONTEXT_DIE. */
20092 static void
20093 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20095 dw_die_ref die;
20096 tree decl_or_origin = decl ? decl : origin;
20098 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20099 die = lookup_decl_die (decl_or_origin);
20100 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20101 && TYPE_DECL_IS_STUB (decl_or_origin))
20102 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20103 else
20104 die = NULL;
20106 if (die != NULL && die->die_parent == NULL)
20107 add_child_die (context_die, die);
20108 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20109 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20110 stmt, context_die);
20111 else
20112 gen_decl_die (decl, origin, context_die);
20115 /* Generate all of the decls declared within a given scope and (recursively)
20116 all of its sub-blocks. */
20118 static void
20119 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20121 tree decl;
20122 unsigned int i;
20123 tree subblocks;
20125 /* Ignore NULL blocks. */
20126 if (stmt == NULL_TREE)
20127 return;
20129 /* Output the DIEs to represent all of the data objects and typedefs
20130 declared directly within this block but not within any nested
20131 sub-blocks. Also, nested function and tag DIEs have been
20132 generated with a parent of NULL; fix that up now. */
20133 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20134 process_scope_var (stmt, decl, NULL_TREE, context_die);
20135 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20136 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20137 context_die);
20139 /* If we're at -g1, we're not interested in subblocks. */
20140 if (debug_info_level <= DINFO_LEVEL_TERSE)
20141 return;
20143 /* Output the DIEs to represent all sub-blocks (and the items declared
20144 therein) of this block. */
20145 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20146 subblocks != NULL;
20147 subblocks = BLOCK_CHAIN (subblocks))
20148 gen_block_die (subblocks, context_die, depth + 1);
20151 /* Is this a typedef we can avoid emitting? */
20153 static inline int
20154 is_redundant_typedef (const_tree decl)
20156 if (TYPE_DECL_IS_STUB (decl))
20157 return 1;
20159 if (DECL_ARTIFICIAL (decl)
20160 && DECL_CONTEXT (decl)
20161 && is_tagged_type (DECL_CONTEXT (decl))
20162 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20163 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20164 /* Also ignore the artificial member typedef for the class name. */
20165 return 1;
20167 return 0;
20170 /* Return TRUE if TYPE is a typedef that names a type for linkage
20171 purposes. This kind of typedefs is produced by the C++ FE for
20172 constructs like:
20174 typedef struct {...} foo;
20176 In that case, there is no typedef variant type produced for foo.
20177 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20178 struct type. */
20180 static bool
20181 is_naming_typedef_decl (const_tree decl)
20183 if (decl == NULL_TREE
20184 || TREE_CODE (decl) != TYPE_DECL
20185 || !is_tagged_type (TREE_TYPE (decl))
20186 || DECL_IS_BUILTIN (decl)
20187 || is_redundant_typedef (decl)
20188 /* It looks like Ada produces TYPE_DECLs that are very similar
20189 to C++ naming typedefs but that have different
20190 semantics. Let's be specific to c++ for now. */
20191 || !is_cxx ())
20192 return FALSE;
20194 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20195 && TYPE_NAME (TREE_TYPE (decl)) == decl
20196 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20197 != TYPE_NAME (TREE_TYPE (decl))));
20200 /* Returns the DIE for a context. */
20202 static inline dw_die_ref
20203 get_context_die (tree context)
20205 if (context)
20207 /* Find die that represents this context. */
20208 if (TYPE_P (context))
20210 context = TYPE_MAIN_VARIANT (context);
20211 return strip_naming_typedef (context, force_type_die (context));
20213 else
20214 return force_decl_die (context);
20216 return comp_unit_die ();
20219 /* Returns the DIE for decl. A DIE will always be returned. */
20221 static dw_die_ref
20222 force_decl_die (tree decl)
20224 dw_die_ref decl_die;
20225 unsigned saved_external_flag;
20226 tree save_fn = NULL_TREE;
20227 decl_die = lookup_decl_die (decl);
20228 if (!decl_die)
20230 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20232 decl_die = lookup_decl_die (decl);
20233 if (decl_die)
20234 return decl_die;
20236 switch (TREE_CODE (decl))
20238 case FUNCTION_DECL:
20239 /* Clear current_function_decl, so that gen_subprogram_die thinks
20240 that this is a declaration. At this point, we just want to force
20241 declaration die. */
20242 save_fn = current_function_decl;
20243 current_function_decl = NULL_TREE;
20244 gen_subprogram_die (decl, context_die);
20245 current_function_decl = save_fn;
20246 break;
20248 case VAR_DECL:
20249 /* Set external flag to force declaration die. Restore it after
20250 gen_decl_die() call. */
20251 saved_external_flag = DECL_EXTERNAL (decl);
20252 DECL_EXTERNAL (decl) = 1;
20253 gen_decl_die (decl, NULL, context_die);
20254 DECL_EXTERNAL (decl) = saved_external_flag;
20255 break;
20257 case NAMESPACE_DECL:
20258 if (dwarf_version >= 3 || !dwarf_strict)
20259 dwarf2out_decl (decl);
20260 else
20261 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20262 decl_die = comp_unit_die ();
20263 break;
20265 case TRANSLATION_UNIT_DECL:
20266 decl_die = comp_unit_die ();
20267 break;
20269 default:
20270 gcc_unreachable ();
20273 /* We should be able to find the DIE now. */
20274 if (!decl_die)
20275 decl_die = lookup_decl_die (decl);
20276 gcc_assert (decl_die);
20279 return decl_die;
20282 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20283 always returned. */
20285 static dw_die_ref
20286 force_type_die (tree type)
20288 dw_die_ref type_die;
20290 type_die = lookup_type_die (type);
20291 if (!type_die)
20293 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20295 type_die = modified_type_die (type, TYPE_READONLY (type),
20296 TYPE_VOLATILE (type), context_die);
20297 gcc_assert (type_die);
20299 return type_die;
20302 /* Force out any required namespaces to be able to output DECL,
20303 and return the new context_die for it, if it's changed. */
20305 static dw_die_ref
20306 setup_namespace_context (tree thing, dw_die_ref context_die)
20308 tree context = (DECL_P (thing)
20309 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20310 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20311 /* Force out the namespace. */
20312 context_die = force_decl_die (context);
20314 return context_die;
20317 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20318 type) within its namespace, if appropriate.
20320 For compatibility with older debuggers, namespace DIEs only contain
20321 declarations; all definitions are emitted at CU scope. */
20323 static dw_die_ref
20324 declare_in_namespace (tree thing, dw_die_ref context_die)
20326 dw_die_ref ns_context;
20328 if (debug_info_level <= DINFO_LEVEL_TERSE)
20329 return context_die;
20331 /* If this decl is from an inlined function, then don't try to emit it in its
20332 namespace, as we will get confused. It would have already been emitted
20333 when the abstract instance of the inline function was emitted anyways. */
20334 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20335 return context_die;
20337 ns_context = setup_namespace_context (thing, context_die);
20339 if (ns_context != context_die)
20341 if (is_fortran ())
20342 return ns_context;
20343 if (DECL_P (thing))
20344 gen_decl_die (thing, NULL, ns_context);
20345 else
20346 gen_type_die (thing, ns_context);
20348 return context_die;
20351 /* Generate a DIE for a namespace or namespace alias. */
20353 static void
20354 gen_namespace_die (tree decl, dw_die_ref context_die)
20356 dw_die_ref namespace_die;
20358 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20359 they are an alias of. */
20360 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20362 /* Output a real namespace or module. */
20363 context_die = setup_namespace_context (decl, comp_unit_die ());
20364 namespace_die = new_die (is_fortran ()
20365 ? DW_TAG_module : DW_TAG_namespace,
20366 context_die, decl);
20367 /* For Fortran modules defined in different CU don't add src coords. */
20368 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20370 const char *name = dwarf2_name (decl, 0);
20371 if (name)
20372 add_name_attribute (namespace_die, name);
20374 else
20375 add_name_and_src_coords_attributes (namespace_die, decl);
20376 if (DECL_EXTERNAL (decl))
20377 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20378 equate_decl_number_to_die (decl, namespace_die);
20380 else
20382 /* Output a namespace alias. */
20384 /* Force out the namespace we are an alias of, if necessary. */
20385 dw_die_ref origin_die
20386 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20388 if (DECL_FILE_SCOPE_P (decl)
20389 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20390 context_die = setup_namespace_context (decl, comp_unit_die ());
20391 /* Now create the namespace alias DIE. */
20392 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20393 add_name_and_src_coords_attributes (namespace_die, decl);
20394 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20395 equate_decl_number_to_die (decl, namespace_die);
20397 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20398 if (want_pubnames ())
20399 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20402 /* Generate Dwarf debug information for a decl described by DECL.
20403 The return value is currently only meaningful for PARM_DECLs,
20404 for all other decls it returns NULL. */
20406 static dw_die_ref
20407 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20409 tree decl_or_origin = decl ? decl : origin;
20410 tree class_origin = NULL, ultimate_origin;
20412 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20413 return NULL;
20415 switch (TREE_CODE (decl_or_origin))
20417 case ERROR_MARK:
20418 break;
20420 case CONST_DECL:
20421 if (!is_fortran () && !is_ada ())
20423 /* The individual enumerators of an enum type get output when we output
20424 the Dwarf representation of the relevant enum type itself. */
20425 break;
20428 /* Emit its type. */
20429 gen_type_die (TREE_TYPE (decl), context_die);
20431 /* And its containing namespace. */
20432 context_die = declare_in_namespace (decl, context_die);
20434 gen_const_die (decl, context_die);
20435 break;
20437 case FUNCTION_DECL:
20438 /* Don't output any DIEs to represent mere function declarations,
20439 unless they are class members or explicit block externs. */
20440 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20441 && DECL_FILE_SCOPE_P (decl_or_origin)
20442 && (current_function_decl == NULL_TREE
20443 || DECL_ARTIFICIAL (decl_or_origin)))
20444 break;
20446 #if 0
20447 /* FIXME */
20448 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20449 on local redeclarations of global functions. That seems broken. */
20450 if (current_function_decl != decl)
20451 /* This is only a declaration. */;
20452 #endif
20454 /* If we're emitting a clone, emit info for the abstract instance. */
20455 if (origin || DECL_ORIGIN (decl) != decl)
20456 dwarf2out_abstract_function (origin
20457 ? DECL_ORIGIN (origin)
20458 : DECL_ABSTRACT_ORIGIN (decl));
20460 /* If we're emitting an out-of-line copy of an inline function,
20461 emit info for the abstract instance and set up to refer to it. */
20462 else if (cgraph_function_possibly_inlined_p (decl)
20463 && ! DECL_ABSTRACT (decl)
20464 && ! class_or_namespace_scope_p (context_die)
20465 /* dwarf2out_abstract_function won't emit a die if this is just
20466 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20467 that case, because that works only if we have a die. */
20468 && DECL_INITIAL (decl) != NULL_TREE)
20470 dwarf2out_abstract_function (decl);
20471 set_decl_origin_self (decl);
20474 /* Otherwise we're emitting the primary DIE for this decl. */
20475 else if (debug_info_level > DINFO_LEVEL_TERSE)
20477 /* Before we describe the FUNCTION_DECL itself, make sure that we
20478 have its containing type. */
20479 if (!origin)
20480 origin = decl_class_context (decl);
20481 if (origin != NULL_TREE)
20482 gen_type_die (origin, context_die);
20484 /* And its return type. */
20485 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20487 /* And its virtual context. */
20488 if (DECL_VINDEX (decl) != NULL_TREE)
20489 gen_type_die (DECL_CONTEXT (decl), context_die);
20491 /* Make sure we have a member DIE for decl. */
20492 if (origin != NULL_TREE)
20493 gen_type_die_for_member (origin, decl, context_die);
20495 /* And its containing namespace. */
20496 context_die = declare_in_namespace (decl, context_die);
20499 /* Now output a DIE to represent the function itself. */
20500 if (decl)
20501 gen_subprogram_die (decl, context_die);
20502 break;
20504 case TYPE_DECL:
20505 /* If we are in terse mode, don't generate any DIEs to represent any
20506 actual typedefs. */
20507 if (debug_info_level <= DINFO_LEVEL_TERSE)
20508 break;
20510 /* In the special case of a TYPE_DECL node representing the declaration
20511 of some type tag, if the given TYPE_DECL is marked as having been
20512 instantiated from some other (original) TYPE_DECL node (e.g. one which
20513 was generated within the original definition of an inline function) we
20514 used to generate a special (abbreviated) DW_TAG_structure_type,
20515 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20516 should be actually referencing those DIEs, as variable DIEs with that
20517 type would be emitted already in the abstract origin, so it was always
20518 removed during unused type prunning. Don't add anything in this
20519 case. */
20520 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20521 break;
20523 if (is_redundant_typedef (decl))
20524 gen_type_die (TREE_TYPE (decl), context_die);
20525 else
20526 /* Output a DIE to represent the typedef itself. */
20527 gen_typedef_die (decl, context_die);
20528 break;
20530 case LABEL_DECL:
20531 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20532 gen_label_die (decl, context_die);
20533 break;
20535 case VAR_DECL:
20536 case RESULT_DECL:
20537 /* If we are in terse mode, don't generate any DIEs to represent any
20538 variable declarations or definitions. */
20539 if (debug_info_level <= DINFO_LEVEL_TERSE)
20540 break;
20542 /* Output any DIEs that are needed to specify the type of this data
20543 object. */
20544 if (decl_by_reference_p (decl_or_origin))
20545 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20546 else
20547 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20549 /* And its containing type. */
20550 class_origin = decl_class_context (decl_or_origin);
20551 if (class_origin != NULL_TREE)
20552 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20554 /* And its containing namespace. */
20555 context_die = declare_in_namespace (decl_or_origin, context_die);
20557 /* Now output the DIE to represent the data object itself. This gets
20558 complicated because of the possibility that the VAR_DECL really
20559 represents an inlined instance of a formal parameter for an inline
20560 function. */
20561 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20562 if (ultimate_origin != NULL_TREE
20563 && TREE_CODE (ultimate_origin) == PARM_DECL)
20564 gen_formal_parameter_die (decl, origin,
20565 true /* Emit name attribute. */,
20566 context_die);
20567 else
20568 gen_variable_die (decl, origin, context_die);
20569 break;
20571 case FIELD_DECL:
20572 /* Ignore the nameless fields that are used to skip bits but handle C++
20573 anonymous unions and structs. */
20574 if (DECL_NAME (decl) != NULL_TREE
20575 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20576 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20578 gen_type_die (member_declared_type (decl), context_die);
20579 gen_field_die (decl, context_die);
20581 break;
20583 case PARM_DECL:
20584 if (DECL_BY_REFERENCE (decl_or_origin))
20585 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20586 else
20587 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20588 return gen_formal_parameter_die (decl, origin,
20589 true /* Emit name attribute. */,
20590 context_die);
20592 case NAMESPACE_DECL:
20593 case IMPORTED_DECL:
20594 if (dwarf_version >= 3 || !dwarf_strict)
20595 gen_namespace_die (decl, context_die);
20596 break;
20598 default:
20599 /* Probably some frontend-internal decl. Assume we don't care. */
20600 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20601 break;
20604 return NULL;
20607 /* Output debug information for global decl DECL. Called from toplev.c after
20608 compilation proper has finished. */
20610 static void
20611 dwarf2out_global_decl (tree decl)
20613 /* Output DWARF2 information for file-scope tentative data object
20614 declarations, file-scope (extern) function declarations (which
20615 had no corresponding body) and file-scope tagged type declarations
20616 and definitions which have not yet been forced out. */
20617 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20618 dwarf2out_decl (decl);
20621 /* Output debug information for type decl DECL. Called from toplev.c
20622 and from language front ends (to record built-in types). */
20623 static void
20624 dwarf2out_type_decl (tree decl, int local)
20626 if (!local)
20627 dwarf2out_decl (decl);
20630 /* Output debug information for imported module or decl DECL.
20631 NAME is non-NULL name in the lexical block if the decl has been renamed.
20632 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20633 that DECL belongs to.
20634 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20635 static void
20636 dwarf2out_imported_module_or_decl_1 (tree decl,
20637 tree name,
20638 tree lexical_block,
20639 dw_die_ref lexical_block_die)
20641 expanded_location xloc;
20642 dw_die_ref imported_die = NULL;
20643 dw_die_ref at_import_die;
20645 if (TREE_CODE (decl) == IMPORTED_DECL)
20647 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20648 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20649 gcc_assert (decl);
20651 else
20652 xloc = expand_location (input_location);
20654 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20656 at_import_die = force_type_die (TREE_TYPE (decl));
20657 /* For namespace N { typedef void T; } using N::T; base_type_die
20658 returns NULL, but DW_TAG_imported_declaration requires
20659 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20660 if (!at_import_die)
20662 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20663 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20664 at_import_die = lookup_type_die (TREE_TYPE (decl));
20665 gcc_assert (at_import_die);
20668 else
20670 at_import_die = lookup_decl_die (decl);
20671 if (!at_import_die)
20673 /* If we're trying to avoid duplicate debug info, we may not have
20674 emitted the member decl for this field. Emit it now. */
20675 if (TREE_CODE (decl) == FIELD_DECL)
20677 tree type = DECL_CONTEXT (decl);
20679 if (TYPE_CONTEXT (type)
20680 && TYPE_P (TYPE_CONTEXT (type))
20681 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20682 DINFO_USAGE_DIR_USE))
20683 return;
20684 gen_type_die_for_member (type, decl,
20685 get_context_die (TYPE_CONTEXT (type)));
20687 at_import_die = force_decl_die (decl);
20691 if (TREE_CODE (decl) == NAMESPACE_DECL)
20693 if (dwarf_version >= 3 || !dwarf_strict)
20694 imported_die = new_die (DW_TAG_imported_module,
20695 lexical_block_die,
20696 lexical_block);
20697 else
20698 return;
20700 else
20701 imported_die = new_die (DW_TAG_imported_declaration,
20702 lexical_block_die,
20703 lexical_block);
20705 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20706 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20707 if (name)
20708 add_AT_string (imported_die, DW_AT_name,
20709 IDENTIFIER_POINTER (name));
20710 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20713 /* Output debug information for imported module or decl DECL.
20714 NAME is non-NULL name in context if the decl has been renamed.
20715 CHILD is true if decl is one of the renamed decls as part of
20716 importing whole module. */
20718 static void
20719 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20720 bool child)
20722 /* dw_die_ref at_import_die; */
20723 dw_die_ref scope_die;
20725 if (debug_info_level <= DINFO_LEVEL_TERSE)
20726 return;
20728 gcc_assert (decl);
20730 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20731 We need decl DIE for reference and scope die. First, get DIE for the decl
20732 itself. */
20734 /* Get the scope die for decl context. Use comp_unit_die for global module
20735 or decl. If die is not found for non globals, force new die. */
20736 if (context
20737 && TYPE_P (context)
20738 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20739 return;
20741 if (!(dwarf_version >= 3 || !dwarf_strict))
20742 return;
20744 scope_die = get_context_die (context);
20746 if (child)
20748 gcc_assert (scope_die->die_child);
20749 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20750 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20751 scope_die = scope_die->die_child;
20754 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20755 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20759 /* Write the debugging output for DECL. */
20761 void
20762 dwarf2out_decl (tree decl)
20764 dw_die_ref context_die = comp_unit_die ();
20766 switch (TREE_CODE (decl))
20768 case ERROR_MARK:
20769 return;
20771 case FUNCTION_DECL:
20772 /* What we would really like to do here is to filter out all mere
20773 file-scope declarations of file-scope functions which are never
20774 referenced later within this translation unit (and keep all of ones
20775 that *are* referenced later on) but we aren't clairvoyant, so we have
20776 no idea which functions will be referenced in the future (i.e. later
20777 on within the current translation unit). So here we just ignore all
20778 file-scope function declarations which are not also definitions. If
20779 and when the debugger needs to know something about these functions,
20780 it will have to hunt around and find the DWARF information associated
20781 with the definition of the function.
20783 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20784 nodes represent definitions and which ones represent mere
20785 declarations. We have to check DECL_INITIAL instead. That's because
20786 the C front-end supports some weird semantics for "extern inline"
20787 function definitions. These can get inlined within the current
20788 translation unit (and thus, we need to generate Dwarf info for their
20789 abstract instances so that the Dwarf info for the concrete inlined
20790 instances can have something to refer to) but the compiler never
20791 generates any out-of-lines instances of such things (despite the fact
20792 that they *are* definitions).
20794 The important point is that the C front-end marks these "extern
20795 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20796 them anyway. Note that the C++ front-end also plays some similar games
20797 for inline function definitions appearing within include files which
20798 also contain `#pragma interface' pragmas.
20800 If we are called from dwarf2out_abstract_function output a DIE
20801 anyway. We can end up here this way with early inlining and LTO
20802 where the inlined function is output in a different LTRANS unit
20803 or not at all. */
20804 if (DECL_INITIAL (decl) == NULL_TREE
20805 && ! DECL_ABSTRACT (decl))
20806 return;
20808 /* If we're a nested function, initially use a parent of NULL; if we're
20809 a plain function, this will be fixed up in decls_for_scope. If
20810 we're a method, it will be ignored, since we already have a DIE. */
20811 if (decl_function_context (decl)
20812 /* But if we're in terse mode, we don't care about scope. */
20813 && debug_info_level > DINFO_LEVEL_TERSE)
20814 context_die = NULL;
20815 break;
20817 case VAR_DECL:
20818 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20819 declaration and if the declaration was never even referenced from
20820 within this entire compilation unit. We suppress these DIEs in
20821 order to save space in the .debug section (by eliminating entries
20822 which are probably useless). Note that we must not suppress
20823 block-local extern declarations (whether used or not) because that
20824 would screw-up the debugger's name lookup mechanism and cause it to
20825 miss things which really ought to be in scope at a given point. */
20826 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20827 return;
20829 /* For local statics lookup proper context die. */
20830 if (TREE_STATIC (decl)
20831 && DECL_CONTEXT (decl)
20832 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20833 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20835 /* If we are in terse mode, don't generate any DIEs to represent any
20836 variable declarations or definitions. */
20837 if (debug_info_level <= DINFO_LEVEL_TERSE)
20838 return;
20839 break;
20841 case CONST_DECL:
20842 if (debug_info_level <= DINFO_LEVEL_TERSE)
20843 return;
20844 if (!is_fortran () && !is_ada ())
20845 return;
20846 if (TREE_STATIC (decl) && decl_function_context (decl))
20847 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20848 break;
20850 case NAMESPACE_DECL:
20851 case IMPORTED_DECL:
20852 if (debug_info_level <= DINFO_LEVEL_TERSE)
20853 return;
20854 if (lookup_decl_die (decl) != NULL)
20855 return;
20856 break;
20858 case TYPE_DECL:
20859 /* Don't emit stubs for types unless they are needed by other DIEs. */
20860 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20861 return;
20863 /* Don't bother trying to generate any DIEs to represent any of the
20864 normal built-in types for the language we are compiling. */
20865 if (DECL_IS_BUILTIN (decl))
20866 return;
20868 /* If we are in terse mode, don't generate any DIEs for types. */
20869 if (debug_info_level <= DINFO_LEVEL_TERSE)
20870 return;
20872 /* If we're a function-scope tag, initially use a parent of NULL;
20873 this will be fixed up in decls_for_scope. */
20874 if (decl_function_context (decl))
20875 context_die = NULL;
20877 break;
20879 default:
20880 return;
20883 gen_decl_die (decl, NULL, context_die);
20886 /* Write the debugging output for DECL. */
20888 static void
20889 dwarf2out_function_decl (tree decl)
20891 dwarf2out_decl (decl);
20892 call_arg_locations = NULL;
20893 call_arg_loc_last = NULL;
20894 call_site_count = -1;
20895 tail_call_site_count = -1;
20896 block_map.release ();
20897 htab_empty (decl_loc_table);
20898 htab_empty (cached_dw_loc_list_table);
20901 /* Output a marker (i.e. a label) for the beginning of the generated code for
20902 a lexical block. */
20904 static void
20905 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20906 unsigned int blocknum)
20908 switch_to_section (current_function_section ());
20909 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20912 /* Output a marker (i.e. a label) for the end of the generated code for a
20913 lexical block. */
20915 static void
20916 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20918 switch_to_section (current_function_section ());
20919 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20922 /* Returns nonzero if it is appropriate not to emit any debugging
20923 information for BLOCK, because it doesn't contain any instructions.
20925 Don't allow this for blocks with nested functions or local classes
20926 as we would end up with orphans, and in the presence of scheduling
20927 we may end up calling them anyway. */
20929 static bool
20930 dwarf2out_ignore_block (const_tree block)
20932 tree decl;
20933 unsigned int i;
20935 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20936 if (TREE_CODE (decl) == FUNCTION_DECL
20937 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20938 return 0;
20939 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20941 decl = BLOCK_NONLOCALIZED_VAR (block, i);
20942 if (TREE_CODE (decl) == FUNCTION_DECL
20943 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20944 return 0;
20947 return 1;
20950 /* Hash table routines for file_hash. */
20952 static int
20953 file_table_eq (const void *p1_p, const void *p2_p)
20955 const struct dwarf_file_data *const p1 =
20956 (const struct dwarf_file_data *) p1_p;
20957 const char *const p2 = (const char *) p2_p;
20958 return filename_cmp (p1->filename, p2) == 0;
20961 static hashval_t
20962 file_table_hash (const void *p_p)
20964 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20965 return htab_hash_string (p->filename);
20968 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20969 dwarf2out.c) and return its "index". The index of each (known) filename is
20970 just a unique number which is associated with only that one filename. We
20971 need such numbers for the sake of generating labels (in the .debug_sfnames
20972 section) and references to those files numbers (in the .debug_srcinfo
20973 and.debug_macinfo sections). If the filename given as an argument is not
20974 found in our current list, add it to the list and assign it the next
20975 available unique index number. In order to speed up searches, we remember
20976 the index of the filename was looked up last. This handles the majority of
20977 all searches. */
20979 static struct dwarf_file_data *
20980 lookup_filename (const char *file_name)
20982 void ** slot;
20983 struct dwarf_file_data * created;
20985 /* Check to see if the file name that was searched on the previous
20986 call matches this file name. If so, return the index. */
20987 if (file_table_last_lookup
20988 && (file_name == file_table_last_lookup->filename
20989 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20990 return file_table_last_lookup;
20992 /* Didn't match the previous lookup, search the table. */
20993 slot = htab_find_slot_with_hash (file_table, file_name,
20994 htab_hash_string (file_name), INSERT);
20995 if (*slot)
20996 return (struct dwarf_file_data *) *slot;
20998 created = ggc_alloc_dwarf_file_data ();
20999 created->filename = file_name;
21000 created->emitted_number = 0;
21001 *slot = created;
21002 return created;
21005 /* If the assembler will construct the file table, then translate the compiler
21006 internal file table number into the assembler file table number, and emit
21007 a .file directive if we haven't already emitted one yet. The file table
21008 numbers are different because we prune debug info for unused variables and
21009 types, which may include filenames. */
21011 static int
21012 maybe_emit_file (struct dwarf_file_data * fd)
21014 if (! fd->emitted_number)
21016 if (last_emitted_file)
21017 fd->emitted_number = last_emitted_file->emitted_number + 1;
21018 else
21019 fd->emitted_number = 1;
21020 last_emitted_file = fd;
21022 if (DWARF2_ASM_LINE_DEBUG_INFO)
21024 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21025 output_quoted_string (asm_out_file,
21026 remap_debug_filename (fd->filename));
21027 fputc ('\n', asm_out_file);
21031 return fd->emitted_number;
21034 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21035 That generation should happen after function debug info has been
21036 generated. The value of the attribute is the constant value of ARG. */
21038 static void
21039 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21041 die_arg_entry entry;
21043 if (!die || !arg)
21044 return;
21046 if (!tmpl_value_parm_die_table)
21047 vec_alloc (tmpl_value_parm_die_table, 32);
21049 entry.die = die;
21050 entry.arg = arg;
21051 vec_safe_push (tmpl_value_parm_die_table, entry);
21054 /* Return TRUE if T is an instance of generic type, FALSE
21055 otherwise. */
21057 static bool
21058 generic_type_p (tree t)
21060 if (t == NULL_TREE || !TYPE_P (t))
21061 return false;
21062 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21065 /* Schedule the generation of the generic parameter dies for the
21066 instance of generic type T. The proper generation itself is later
21067 done by gen_scheduled_generic_parms_dies. */
21069 static void
21070 schedule_generic_params_dies_gen (tree t)
21072 if (!generic_type_p (t))
21073 return;
21075 if (!generic_type_instances)
21076 vec_alloc (generic_type_instances, 256);
21078 vec_safe_push (generic_type_instances, t);
21081 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21082 by append_entry_to_tmpl_value_parm_die_table. This function must
21083 be called after function DIEs have been generated. */
21085 static void
21086 gen_remaining_tmpl_value_param_die_attribute (void)
21088 if (tmpl_value_parm_die_table)
21090 unsigned i;
21091 die_arg_entry *e;
21093 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21094 tree_add_const_value_attribute (e->die, e->arg);
21098 /* Generate generic parameters DIEs for instances of generic types
21099 that have been previously scheduled by
21100 schedule_generic_params_dies_gen. This function must be called
21101 after all the types of the CU have been laid out. */
21103 static void
21104 gen_scheduled_generic_parms_dies (void)
21106 unsigned i;
21107 tree t;
21109 if (!generic_type_instances)
21110 return;
21112 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21113 if (COMPLETE_TYPE_P (t))
21114 gen_generic_params_dies (t);
21118 /* Replace DW_AT_name for the decl with name. */
21120 static void
21121 dwarf2out_set_name (tree decl, tree name)
21123 dw_die_ref die;
21124 dw_attr_ref attr;
21125 const char *dname;
21127 die = TYPE_SYMTAB_DIE (decl);
21128 if (!die)
21129 return;
21131 dname = dwarf2_name (name, 0);
21132 if (!dname)
21133 return;
21135 attr = get_AT (die, DW_AT_name);
21136 if (attr)
21138 struct indirect_string_node *node;
21140 node = find_AT_string (dname);
21141 /* replace the string. */
21142 attr->dw_attr_val.v.val_str = node;
21145 else
21146 add_name_attribute (die, dname);
21149 /* True if before or during processing of the first function being emitted. */
21150 static bool in_first_function_p = true;
21151 /* True if loc_note during dwarf2out_var_location call might still be
21152 before first real instruction at address equal to .Ltext0. */
21153 static bool maybe_at_text_label_p = true;
21154 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21155 static unsigned int first_loclabel_num_not_at_text_label;
21157 /* Called by the final INSN scan whenever we see a var location. We
21158 use it to drop labels in the right places, and throw the location in
21159 our lookup table. */
21161 static void
21162 dwarf2out_var_location (rtx loc_note)
21164 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21165 struct var_loc_node *newloc;
21166 rtx next_real, next_note;
21167 static const char *last_label;
21168 static const char *last_postcall_label;
21169 static bool last_in_cold_section_p;
21170 static rtx expected_next_loc_note;
21171 tree decl;
21172 bool var_loc_p;
21174 if (!NOTE_P (loc_note))
21176 if (CALL_P (loc_note))
21178 call_site_count++;
21179 if (SIBLING_CALL_P (loc_note))
21180 tail_call_site_count++;
21182 return;
21185 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21186 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21187 return;
21189 /* Optimize processing a large consecutive sequence of location
21190 notes so we don't spend too much time in next_real_insn. If the
21191 next insn is another location note, remember the next_real_insn
21192 calculation for next time. */
21193 next_real = cached_next_real_insn;
21194 if (next_real)
21196 if (expected_next_loc_note != loc_note)
21197 next_real = NULL_RTX;
21200 next_note = NEXT_INSN (loc_note);
21201 if (! next_note
21202 || INSN_DELETED_P (next_note)
21203 || ! NOTE_P (next_note)
21204 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21205 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21206 next_note = NULL_RTX;
21208 if (! next_real)
21209 next_real = next_real_insn (loc_note);
21211 if (next_note)
21213 expected_next_loc_note = next_note;
21214 cached_next_real_insn = next_real;
21216 else
21217 cached_next_real_insn = NULL_RTX;
21219 /* If there are no instructions which would be affected by this note,
21220 don't do anything. */
21221 if (var_loc_p
21222 && next_real == NULL_RTX
21223 && !NOTE_DURING_CALL_P (loc_note))
21224 return;
21226 if (next_real == NULL_RTX)
21227 next_real = get_last_insn ();
21229 /* If there were any real insns between note we processed last time
21230 and this note (or if it is the first note), clear
21231 last_{,postcall_}label so that they are not reused this time. */
21232 if (last_var_location_insn == NULL_RTX
21233 || last_var_location_insn != next_real
21234 || last_in_cold_section_p != in_cold_section_p)
21236 last_label = NULL;
21237 last_postcall_label = NULL;
21240 if (var_loc_p)
21242 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21243 newloc = add_var_loc_to_decl (decl, loc_note,
21244 NOTE_DURING_CALL_P (loc_note)
21245 ? last_postcall_label : last_label);
21246 if (newloc == NULL)
21247 return;
21249 else
21251 decl = NULL_TREE;
21252 newloc = NULL;
21255 /* If there were no real insns between note we processed last time
21256 and this note, use the label we emitted last time. Otherwise
21257 create a new label and emit it. */
21258 if (last_label == NULL)
21260 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21261 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21262 loclabel_num++;
21263 last_label = ggc_strdup (loclabel);
21264 /* See if loclabel might be equal to .Ltext0. If yes,
21265 bump first_loclabel_num_not_at_text_label. */
21266 if (!have_multiple_function_sections
21267 && in_first_function_p
21268 && maybe_at_text_label_p)
21270 static rtx last_start;
21271 rtx insn;
21272 for (insn = loc_note; insn; insn = previous_insn (insn))
21273 if (insn == last_start)
21274 break;
21275 else if (!NONDEBUG_INSN_P (insn))
21276 continue;
21277 else
21279 rtx body = PATTERN (insn);
21280 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21281 continue;
21282 /* Inline asm could occupy zero bytes. */
21283 else if (GET_CODE (body) == ASM_INPUT
21284 || asm_noperands (body) >= 0)
21285 continue;
21286 #ifdef HAVE_attr_length
21287 else if (get_attr_min_length (insn) == 0)
21288 continue;
21289 #endif
21290 else
21292 /* Assume insn has non-zero length. */
21293 maybe_at_text_label_p = false;
21294 break;
21297 if (maybe_at_text_label_p)
21299 last_start = loc_note;
21300 first_loclabel_num_not_at_text_label = loclabel_num;
21305 if (!var_loc_p)
21307 struct call_arg_loc_node *ca_loc
21308 = ggc_alloc_cleared_call_arg_loc_node ();
21309 rtx prev = prev_real_insn (loc_note), x;
21310 ca_loc->call_arg_loc_note = loc_note;
21311 ca_loc->next = NULL;
21312 ca_loc->label = last_label;
21313 gcc_assert (prev
21314 && (CALL_P (prev)
21315 || (NONJUMP_INSN_P (prev)
21316 && GET_CODE (PATTERN (prev)) == SEQUENCE
21317 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21318 if (!CALL_P (prev))
21319 prev = XVECEXP (PATTERN (prev), 0, 0);
21320 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21321 x = get_call_rtx_from (PATTERN (prev));
21322 if (x)
21324 x = XEXP (XEXP (x, 0), 0);
21325 if (GET_CODE (x) == SYMBOL_REF
21326 && SYMBOL_REF_DECL (x)
21327 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21328 ca_loc->symbol_ref = x;
21330 ca_loc->block = insn_scope (prev);
21331 if (call_arg_locations)
21332 call_arg_loc_last->next = ca_loc;
21333 else
21334 call_arg_locations = ca_loc;
21335 call_arg_loc_last = ca_loc;
21337 else if (!NOTE_DURING_CALL_P (loc_note))
21338 newloc->label = last_label;
21339 else
21341 if (!last_postcall_label)
21343 sprintf (loclabel, "%s-1", last_label);
21344 last_postcall_label = ggc_strdup (loclabel);
21346 newloc->label = last_postcall_label;
21349 last_var_location_insn = next_real;
21350 last_in_cold_section_p = in_cold_section_p;
21353 /* Note in one location list that text section has changed. */
21355 static int
21356 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21358 var_loc_list *list = (var_loc_list *) *slot;
21359 if (list->first)
21360 list->last_before_switch
21361 = list->last->next ? list->last->next : list->last;
21362 return 1;
21365 /* Note in all location lists that text section has changed. */
21367 static void
21368 var_location_switch_text_section (void)
21370 if (decl_loc_table == NULL)
21371 return;
21373 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21376 /* Create a new line number table. */
21378 static dw_line_info_table *
21379 new_line_info_table (void)
21381 dw_line_info_table *table;
21383 table = ggc_alloc_cleared_dw_line_info_table_struct ();
21384 table->file_num = 1;
21385 table->line_num = 1;
21386 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21388 return table;
21391 /* Lookup the "current" table into which we emit line info, so
21392 that we don't have to do it for every source line. */
21394 static void
21395 set_cur_line_info_table (section *sec)
21397 dw_line_info_table *table;
21399 if (sec == text_section)
21400 table = text_section_line_info;
21401 else if (sec == cold_text_section)
21403 table = cold_text_section_line_info;
21404 if (!table)
21406 cold_text_section_line_info = table = new_line_info_table ();
21407 table->end_label = cold_end_label;
21410 else
21412 const char *end_label;
21414 if (flag_reorder_blocks_and_partition)
21416 if (in_cold_section_p)
21417 end_label = crtl->subsections.cold_section_end_label;
21418 else
21419 end_label = crtl->subsections.hot_section_end_label;
21421 else
21423 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21424 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21425 current_function_funcdef_no);
21426 end_label = ggc_strdup (label);
21429 table = new_line_info_table ();
21430 table->end_label = end_label;
21432 vec_safe_push (separate_line_info, table);
21435 if (DWARF2_ASM_LINE_DEBUG_INFO)
21436 table->is_stmt = (cur_line_info_table
21437 ? cur_line_info_table->is_stmt
21438 : DWARF_LINE_DEFAULT_IS_STMT_START);
21439 cur_line_info_table = table;
21443 /* We need to reset the locations at the beginning of each
21444 function. We can't do this in the end_function hook, because the
21445 declarations that use the locations won't have been output when
21446 that hook is called. Also compute have_multiple_function_sections here. */
21448 static void
21449 dwarf2out_begin_function (tree fun)
21451 section *sec = function_section (fun);
21453 if (sec != text_section)
21454 have_multiple_function_sections = true;
21456 if (flag_reorder_blocks_and_partition && !cold_text_section)
21458 gcc_assert (current_function_decl == fun);
21459 cold_text_section = unlikely_text_section ();
21460 switch_to_section (cold_text_section);
21461 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21462 switch_to_section (sec);
21465 dwarf2out_note_section_used ();
21466 call_site_count = 0;
21467 tail_call_site_count = 0;
21469 set_cur_line_info_table (sec);
21472 /* Helper function of dwarf2out_end_function, called only after emitting
21473 the very first function into assembly. Check if some .debug_loc range
21474 might end with a .LVL* label that could be equal to .Ltext0.
21475 In that case we must force using absolute addresses in .debug_loc ranges,
21476 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21477 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21478 list terminator.
21479 Set have_multiple_function_sections to true in that case and
21480 terminate htab traversal. */
21482 static int
21483 find_empty_loc_ranges_at_text_label (void **slot, void *)
21485 var_loc_list *entry;
21486 struct var_loc_node *node;
21488 entry = (var_loc_list *) *slot;
21489 node = entry->first;
21490 if (node && node->next && node->next->label)
21492 unsigned int i;
21493 const char *label = node->next->label;
21494 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21496 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21498 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21499 if (strcmp (label, loclabel) == 0)
21501 have_multiple_function_sections = true;
21502 return 0;
21506 return 1;
21509 /* Hook called after emitting a function into assembly.
21510 This does something only for the very first function emitted. */
21512 static void
21513 dwarf2out_end_function (unsigned int)
21515 if (in_first_function_p
21516 && !have_multiple_function_sections
21517 && first_loclabel_num_not_at_text_label
21518 && decl_loc_table)
21519 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21520 NULL);
21521 in_first_function_p = false;
21522 maybe_at_text_label_p = false;
21525 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21527 static void
21528 push_dw_line_info_entry (dw_line_info_table *table,
21529 enum dw_line_info_opcode opcode, unsigned int val)
21531 dw_line_info_entry e;
21532 e.opcode = opcode;
21533 e.val = val;
21534 vec_safe_push (table->entries, e);
21537 /* Output a label to mark the beginning of a source code line entry
21538 and record information relating to this source line, in
21539 'line_info_table' for later output of the .debug_line section. */
21540 /* ??? The discriminator parameter ought to be unsigned. */
21542 static void
21543 dwarf2out_source_line (unsigned int line, const char *filename,
21544 int discriminator, bool is_stmt)
21546 unsigned int file_num;
21547 dw_line_info_table *table;
21549 if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21550 return;
21552 /* The discriminator column was added in dwarf4. Simplify the below
21553 by simply removing it if we're not supposed to output it. */
21554 if (dwarf_version < 4 && dwarf_strict)
21555 discriminator = 0;
21557 table = cur_line_info_table;
21558 file_num = maybe_emit_file (lookup_filename (filename));
21560 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21561 the debugger has used the second (possibly duplicate) line number
21562 at the beginning of the function to mark the end of the prologue.
21563 We could eliminate any other duplicates within the function. For
21564 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21565 that second line number entry. */
21566 /* Recall that this end-of-prologue indication is *not* the same thing
21567 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21568 to which the hook corresponds, follows the last insn that was
21569 emitted by gen_prologue. What we need is to precede the first insn
21570 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21571 insn that corresponds to something the user wrote. These may be
21572 very different locations once scheduling is enabled. */
21574 if (0 && file_num == table->file_num
21575 && line == table->line_num
21576 && discriminator == table->discrim_num
21577 && is_stmt == table->is_stmt)
21578 return;
21580 switch_to_section (current_function_section ());
21582 /* If requested, emit something human-readable. */
21583 if (flag_debug_asm)
21584 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21586 if (DWARF2_ASM_LINE_DEBUG_INFO)
21588 /* Emit the .loc directive understood by GNU as. */
21589 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21590 file_num, line, is_stmt, discriminator */
21591 fputs ("\t.loc ", asm_out_file);
21592 fprint_ul (asm_out_file, file_num);
21593 putc (' ', asm_out_file);
21594 fprint_ul (asm_out_file, line);
21595 putc (' ', asm_out_file);
21596 putc ('0', asm_out_file);
21598 if (is_stmt != table->is_stmt)
21600 fputs (" is_stmt ", asm_out_file);
21601 putc (is_stmt ? '1' : '0', asm_out_file);
21603 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21605 gcc_assert (discriminator > 0);
21606 fputs (" discriminator ", asm_out_file);
21607 fprint_ul (asm_out_file, (unsigned long) discriminator);
21609 putc ('\n', asm_out_file);
21611 else
21613 unsigned int label_num = ++line_info_label_num;
21615 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21617 push_dw_line_info_entry (table, LI_set_address, label_num);
21618 if (file_num != table->file_num)
21619 push_dw_line_info_entry (table, LI_set_file, file_num);
21620 if (discriminator != table->discrim_num)
21621 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21622 if (is_stmt != table->is_stmt)
21623 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21624 push_dw_line_info_entry (table, LI_set_line, line);
21627 table->file_num = file_num;
21628 table->line_num = line;
21629 table->discrim_num = discriminator;
21630 table->is_stmt = is_stmt;
21631 table->in_use = true;
21634 /* Record the beginning of a new source file. */
21636 static void
21637 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21639 if (flag_eliminate_dwarf2_dups)
21641 /* Record the beginning of the file for break_out_includes. */
21642 dw_die_ref bincl_die;
21644 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21645 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21648 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21650 macinfo_entry e;
21651 e.code = DW_MACINFO_start_file;
21652 e.lineno = lineno;
21653 e.info = ggc_strdup (filename);
21654 vec_safe_push (macinfo_table, e);
21658 /* Record the end of a source file. */
21660 static void
21661 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21663 if (flag_eliminate_dwarf2_dups)
21664 /* Record the end of the file for break_out_includes. */
21665 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21667 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21669 macinfo_entry e;
21670 e.code = DW_MACINFO_end_file;
21671 e.lineno = lineno;
21672 e.info = NULL;
21673 vec_safe_push (macinfo_table, e);
21677 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21678 the tail part of the directive line, i.e. the part which is past the
21679 initial whitespace, #, whitespace, directive-name, whitespace part. */
21681 static void
21682 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21683 const char *buffer ATTRIBUTE_UNUSED)
21685 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21687 macinfo_entry e;
21688 /* Insert a dummy first entry to be able to optimize the whole
21689 predefined macro block using DW_MACRO_GNU_transparent_include. */
21690 if (macinfo_table->is_empty () && lineno <= 1)
21692 e.code = 0;
21693 e.lineno = 0;
21694 e.info = NULL;
21695 vec_safe_push (macinfo_table, e);
21697 e.code = DW_MACINFO_define;
21698 e.lineno = lineno;
21699 e.info = ggc_strdup (buffer);
21700 vec_safe_push (macinfo_table, e);
21704 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21705 the tail part of the directive line, i.e. the part which is past the
21706 initial whitespace, #, whitespace, directive-name, whitespace part. */
21708 static void
21709 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21710 const char *buffer ATTRIBUTE_UNUSED)
21712 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21714 macinfo_entry e;
21715 /* Insert a dummy first entry to be able to optimize the whole
21716 predefined macro block using DW_MACRO_GNU_transparent_include. */
21717 if (macinfo_table->is_empty () && lineno <= 1)
21719 e.code = 0;
21720 e.lineno = 0;
21721 e.info = NULL;
21722 vec_safe_push (macinfo_table, e);
21724 e.code = DW_MACINFO_undef;
21725 e.lineno = lineno;
21726 e.info = ggc_strdup (buffer);
21727 vec_safe_push (macinfo_table, e);
21731 /* Helpers to manipulate hash table of CUs. */
21733 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21735 typedef macinfo_entry value_type;
21736 typedef macinfo_entry compare_type;
21737 static inline hashval_t hash (const value_type *);
21738 static inline bool equal (const value_type *, const compare_type *);
21741 inline hashval_t
21742 macinfo_entry_hasher::hash (const value_type *entry)
21744 return htab_hash_string (entry->info);
21747 inline bool
21748 macinfo_entry_hasher::equal (const value_type *entry1,
21749 const compare_type *entry2)
21751 return !strcmp (entry1->info, entry2->info);
21754 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21756 /* Output a single .debug_macinfo entry. */
21758 static void
21759 output_macinfo_op (macinfo_entry *ref)
21761 int file_num;
21762 size_t len;
21763 struct indirect_string_node *node;
21764 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21765 struct dwarf_file_data *fd;
21767 switch (ref->code)
21769 case DW_MACINFO_start_file:
21770 fd = lookup_filename (ref->info);
21771 file_num = maybe_emit_file (fd);
21772 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21773 dw2_asm_output_data_uleb128 (ref->lineno,
21774 "Included from line number %lu",
21775 (unsigned long) ref->lineno);
21776 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21777 break;
21778 case DW_MACINFO_end_file:
21779 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21780 break;
21781 case DW_MACINFO_define:
21782 case DW_MACINFO_undef:
21783 len = strlen (ref->info) + 1;
21784 if (!dwarf_strict
21785 && len > DWARF_OFFSET_SIZE
21786 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21787 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21789 ref->code = ref->code == DW_MACINFO_define
21790 ? DW_MACRO_GNU_define_indirect
21791 : DW_MACRO_GNU_undef_indirect;
21792 output_macinfo_op (ref);
21793 return;
21795 dw2_asm_output_data (1, ref->code,
21796 ref->code == DW_MACINFO_define
21797 ? "Define macro" : "Undefine macro");
21798 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21799 (unsigned long) ref->lineno);
21800 dw2_asm_output_nstring (ref->info, -1, "The macro");
21801 break;
21802 case DW_MACRO_GNU_define_indirect:
21803 case DW_MACRO_GNU_undef_indirect:
21804 node = find_AT_string (ref->info);
21805 gcc_assert (node
21806 && ((node->form == DW_FORM_strp)
21807 || (node->form == DW_FORM_GNU_str_index)));
21808 dw2_asm_output_data (1, ref->code,
21809 ref->code == DW_MACRO_GNU_define_indirect
21810 ? "Define macro indirect"
21811 : "Undefine macro indirect");
21812 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21813 (unsigned long) ref->lineno);
21814 if (node->form == DW_FORM_strp)
21815 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21816 debug_str_section, "The macro: \"%s\"",
21817 ref->info);
21818 else
21819 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21820 ref->info);
21821 break;
21822 case DW_MACRO_GNU_transparent_include:
21823 dw2_asm_output_data (1, ref->code, "Transparent include");
21824 ASM_GENERATE_INTERNAL_LABEL (label,
21825 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21826 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21827 break;
21828 default:
21829 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21830 ASM_COMMENT_START, (unsigned long) ref->code);
21831 break;
21835 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21836 other compilation unit .debug_macinfo sections. IDX is the first
21837 index of a define/undef, return the number of ops that should be
21838 emitted in a comdat .debug_macinfo section and emit
21839 a DW_MACRO_GNU_transparent_include entry referencing it.
21840 If the define/undef entry should be emitted normally, return 0. */
21842 static unsigned
21843 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21844 macinfo_hash_type *macinfo_htab)
21846 macinfo_entry *first, *second, *cur, *inc;
21847 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21848 unsigned char checksum[16];
21849 struct md5_ctx ctx;
21850 char *grp_name, *tail;
21851 const char *base;
21852 unsigned int i, count, encoded_filename_len, linebuf_len;
21853 macinfo_entry **slot;
21855 first = &(*macinfo_table)[idx];
21856 second = &(*macinfo_table)[idx + 1];
21858 /* Optimize only if there are at least two consecutive define/undef ops,
21859 and either all of them are before first DW_MACINFO_start_file
21860 with lineno {0,1} (i.e. predefined macro block), or all of them are
21861 in some included header file. */
21862 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21863 return 0;
21864 if (vec_safe_is_empty (files))
21866 if (first->lineno > 1 || second->lineno > 1)
21867 return 0;
21869 else if (first->lineno == 0)
21870 return 0;
21872 /* Find the last define/undef entry that can be grouped together
21873 with first and at the same time compute md5 checksum of their
21874 codes, linenumbers and strings. */
21875 md5_init_ctx (&ctx);
21876 for (i = idx; macinfo_table->iterate (i, &cur); i++)
21877 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21878 break;
21879 else if (vec_safe_is_empty (files) && cur->lineno > 1)
21880 break;
21881 else
21883 unsigned char code = cur->code;
21884 md5_process_bytes (&code, 1, &ctx);
21885 checksum_uleb128 (cur->lineno, &ctx);
21886 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21888 md5_finish_ctx (&ctx, checksum);
21889 count = i - idx;
21891 /* From the containing include filename (if any) pick up just
21892 usable characters from its basename. */
21893 if (vec_safe_is_empty (files))
21894 base = "";
21895 else
21896 base = lbasename (files->last ().info);
21897 for (encoded_filename_len = 0, i = 0; base[i]; i++)
21898 if (ISIDNUM (base[i]) || base[i] == '.')
21899 encoded_filename_len++;
21900 /* Count . at the end. */
21901 if (encoded_filename_len)
21902 encoded_filename_len++;
21904 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21905 linebuf_len = strlen (linebuf);
21907 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
21908 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21909 + 16 * 2 + 1);
21910 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21911 tail = grp_name + 4;
21912 if (encoded_filename_len)
21914 for (i = 0; base[i]; i++)
21915 if (ISIDNUM (base[i]) || base[i] == '.')
21916 *tail++ = base[i];
21917 *tail++ = '.';
21919 memcpy (tail, linebuf, linebuf_len);
21920 tail += linebuf_len;
21921 *tail++ = '.';
21922 for (i = 0; i < 16; i++)
21923 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21925 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21926 in the empty vector entry before the first define/undef. */
21927 inc = &(*macinfo_table)[idx - 1];
21928 inc->code = DW_MACRO_GNU_transparent_include;
21929 inc->lineno = 0;
21930 inc->info = ggc_strdup (grp_name);
21931 if (!macinfo_htab->is_created ())
21932 macinfo_htab->create (10);
21933 /* Avoid emitting duplicates. */
21934 slot = macinfo_htab->find_slot (inc, INSERT);
21935 if (*slot != NULL)
21937 inc->code = 0;
21938 inc->info = NULL;
21939 /* If such an entry has been used before, just emit
21940 a DW_MACRO_GNU_transparent_include op. */
21941 inc = *slot;
21942 output_macinfo_op (inc);
21943 /* And clear all macinfo_entry in the range to avoid emitting them
21944 in the second pass. */
21945 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21947 cur->code = 0;
21948 cur->info = NULL;
21951 else
21953 *slot = inc;
21954 inc->lineno = macinfo_htab->elements ();
21955 output_macinfo_op (inc);
21957 return count;
21960 /* Save any strings needed by the macinfo table in the debug str
21961 table. All strings must be collected into the table by the time
21962 index_string is called. */
21964 static void
21965 save_macinfo_strings (void)
21967 unsigned len;
21968 unsigned i;
21969 macinfo_entry *ref;
21971 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21973 switch (ref->code)
21975 /* Match the logic in output_macinfo_op to decide on
21976 indirect strings. */
21977 case DW_MACINFO_define:
21978 case DW_MACINFO_undef:
21979 len = strlen (ref->info) + 1;
21980 if (!dwarf_strict
21981 && len > DWARF_OFFSET_SIZE
21982 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21983 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21984 set_indirect_string (find_AT_string (ref->info));
21985 break;
21986 case DW_MACRO_GNU_define_indirect:
21987 case DW_MACRO_GNU_undef_indirect:
21988 set_indirect_string (find_AT_string (ref->info));
21989 break;
21990 default:
21991 break;
21996 /* Output macinfo section(s). */
21998 static void
21999 output_macinfo (void)
22001 unsigned i;
22002 unsigned long length = vec_safe_length (macinfo_table);
22003 macinfo_entry *ref;
22004 vec<macinfo_entry, va_gc> *files = NULL;
22005 macinfo_hash_type macinfo_htab;
22007 if (! length)
22008 return;
22010 /* output_macinfo* uses these interchangeably. */
22011 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22012 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22013 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22014 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22016 /* For .debug_macro emit the section header. */
22017 if (!dwarf_strict)
22019 dw2_asm_output_data (2, 4, "DWARF macro version number");
22020 if (DWARF_OFFSET_SIZE == 8)
22021 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22022 else
22023 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22024 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22025 (!dwarf_split_debug_info ? debug_line_section_label
22026 : debug_skeleton_line_section_label),
22027 debug_line_section, NULL);
22030 /* In the first loop, it emits the primary .debug_macinfo section
22031 and after each emitted op the macinfo_entry is cleared.
22032 If a longer range of define/undef ops can be optimized using
22033 DW_MACRO_GNU_transparent_include, the
22034 DW_MACRO_GNU_transparent_include op is emitted and kept in
22035 the vector before the first define/undef in the range and the
22036 whole range of define/undef ops is not emitted and kept. */
22037 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22039 switch (ref->code)
22041 case DW_MACINFO_start_file:
22042 vec_safe_push (files, *ref);
22043 break;
22044 case DW_MACINFO_end_file:
22045 if (!vec_safe_is_empty (files))
22046 files->pop ();
22047 break;
22048 case DW_MACINFO_define:
22049 case DW_MACINFO_undef:
22050 if (!dwarf_strict
22051 && HAVE_COMDAT_GROUP
22052 && vec_safe_length (files) != 1
22053 && i > 0
22054 && i + 1 < length
22055 && (*macinfo_table)[i - 1].code == 0)
22057 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22058 if (count)
22060 i += count - 1;
22061 continue;
22064 break;
22065 case 0:
22066 /* A dummy entry may be inserted at the beginning to be able
22067 to optimize the whole block of predefined macros. */
22068 if (i == 0)
22069 continue;
22070 default:
22071 break;
22073 output_macinfo_op (ref);
22074 ref->info = NULL;
22075 ref->code = 0;
22078 if (!macinfo_htab.is_created ())
22079 return;
22081 macinfo_htab.dispose ();
22083 /* If any DW_MACRO_GNU_transparent_include were used, on those
22084 DW_MACRO_GNU_transparent_include entries terminate the
22085 current chain and switch to a new comdat .debug_macinfo
22086 section and emit the define/undef entries within it. */
22087 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22088 switch (ref->code)
22090 case 0:
22091 continue;
22092 case DW_MACRO_GNU_transparent_include:
22094 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22095 tree comdat_key = get_identifier (ref->info);
22096 /* Terminate the previous .debug_macinfo section. */
22097 dw2_asm_output_data (1, 0, "End compilation unit");
22098 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22099 SECTION_DEBUG
22100 | SECTION_LINKONCE,
22101 comdat_key);
22102 ASM_GENERATE_INTERNAL_LABEL (label,
22103 DEBUG_MACRO_SECTION_LABEL,
22104 ref->lineno);
22105 ASM_OUTPUT_LABEL (asm_out_file, label);
22106 ref->code = 0;
22107 ref->info = NULL;
22108 dw2_asm_output_data (2, 4, "DWARF macro version number");
22109 if (DWARF_OFFSET_SIZE == 8)
22110 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22111 else
22112 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22114 break;
22115 case DW_MACINFO_define:
22116 case DW_MACINFO_undef:
22117 output_macinfo_op (ref);
22118 ref->code = 0;
22119 ref->info = NULL;
22120 break;
22121 default:
22122 gcc_unreachable ();
22126 /* Set up for Dwarf output at the start of compilation. */
22128 static void
22129 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22131 /* Allocate the file_table. */
22132 file_table = htab_create_ggc (50, file_table_hash,
22133 file_table_eq, NULL);
22135 /* Allocate the decl_die_table. */
22136 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22137 decl_die_table_eq, NULL);
22139 /* Allocate the decl_loc_table. */
22140 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22141 decl_loc_table_eq, NULL);
22143 /* Allocate the cached_dw_loc_list_table. */
22144 cached_dw_loc_list_table
22145 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22146 cached_dw_loc_list_table_eq, NULL);
22148 /* Allocate the initial hunk of the decl_scope_table. */
22149 vec_alloc (decl_scope_table, 256);
22151 /* Allocate the initial hunk of the abbrev_die_table. */
22152 abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22153 (ABBREV_DIE_TABLE_INCREMENT);
22154 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22155 /* Zero-th entry is allocated, but unused. */
22156 abbrev_die_table_in_use = 1;
22158 /* Allocate the pubtypes and pubnames vectors. */
22159 vec_alloc (pubname_table, 32);
22160 vec_alloc (pubtype_table, 32);
22162 vec_alloc (incomplete_types, 64);
22164 vec_alloc (used_rtx_array, 32);
22166 if (!dwarf_split_debug_info)
22168 debug_info_section = get_section (DEBUG_INFO_SECTION,
22169 SECTION_DEBUG, NULL);
22170 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22171 SECTION_DEBUG, NULL);
22172 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22173 SECTION_DEBUG, NULL);
22175 else
22177 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22178 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22179 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22180 SECTION_DEBUG | SECTION_EXCLUDE,
22181 NULL);
22182 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22183 SECTION_DEBUG, NULL);
22184 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22185 SECTION_DEBUG, NULL);
22186 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22187 SECTION_DEBUG, NULL);
22188 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22189 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22191 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22192 the main .o, but the skeleton_line goes into the split off dwo. */
22193 debug_skeleton_line_section
22194 = get_section (DEBUG_DWO_LINE_SECTION,
22195 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22196 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22197 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22198 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22199 SECTION_DEBUG | SECTION_EXCLUDE,
22200 NULL);
22201 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22202 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22203 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22204 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22205 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22206 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22208 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22209 SECTION_DEBUG, NULL);
22210 debug_macinfo_section = get_section (dwarf_strict
22211 ? DEBUG_MACINFO_SECTION
22212 : DEBUG_MACRO_SECTION,
22213 DEBUG_MACRO_SECTION_FLAGS, NULL);
22214 debug_line_section = get_section (DEBUG_LINE_SECTION,
22215 SECTION_DEBUG, NULL);
22216 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22217 SECTION_DEBUG, NULL);
22218 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22219 SECTION_DEBUG, NULL);
22220 debug_str_section = get_section (DEBUG_STR_SECTION,
22221 DEBUG_STR_SECTION_FLAGS, NULL);
22222 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22223 SECTION_DEBUG, NULL);
22224 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22225 SECTION_DEBUG, NULL);
22227 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22228 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22229 DEBUG_ABBREV_SECTION_LABEL, 0);
22230 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22231 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22232 COLD_TEXT_SECTION_LABEL, 0);
22233 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22235 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22236 DEBUG_INFO_SECTION_LABEL, 0);
22237 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22238 DEBUG_LINE_SECTION_LABEL, 0);
22239 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22240 DEBUG_RANGES_SECTION_LABEL, 0);
22241 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22242 DEBUG_ADDR_SECTION_LABEL, 0);
22243 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22244 dwarf_strict
22245 ? DEBUG_MACINFO_SECTION_LABEL
22246 : DEBUG_MACRO_SECTION_LABEL, 0);
22247 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22249 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22250 vec_alloc (macinfo_table, 64);
22252 switch_to_section (text_section);
22253 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22255 /* Make sure the line number table for .text always exists. */
22256 text_section_line_info = new_line_info_table ();
22257 text_section_line_info->end_label = text_end_label;
22260 /* Called before compile () starts outputtting functions, variables
22261 and toplevel asms into assembly. */
22263 static void
22264 dwarf2out_assembly_start (void)
22266 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22267 && dwarf2out_do_cfi_asm ()
22268 && (!(flag_unwind_tables || flag_exceptions)
22269 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22270 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22273 /* A helper function for dwarf2out_finish called through
22274 htab_traverse. Assign a string its index. All strings must be
22275 collected into the table by the time index_string is called,
22276 because the indexing code relies on htab_traverse to traverse nodes
22277 in the same order for each run. */
22279 static int
22280 index_string (void **h, void *v)
22282 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22283 unsigned int *index = (unsigned int *) v;
22285 find_string_form (node);
22286 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22288 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22289 node->index = *index;
22290 *index += 1;
22292 return 1;
22295 /* A helper function for output_indirect_strings called through
22296 htab_traverse. Output the offset to a string and update the
22297 current offset. */
22299 static int
22300 output_index_string_offset (void **h, void *v)
22302 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22303 unsigned int *offset = (unsigned int *) v;
22305 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22307 /* Assert that this node has been assigned an index. */
22308 gcc_assert (node->index != NO_INDEX_ASSIGNED
22309 && node->index != NOT_INDEXED);
22310 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22311 "indexed string 0x%x: %s", node->index, node->str);
22312 *offset += strlen (node->str) + 1;
22314 return 1;
22317 /* A helper function for dwarf2out_finish called through
22318 htab_traverse. Output the indexed string. */
22320 static int
22321 output_index_string (void **h, void *v)
22323 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22324 unsigned int *cur_idx = (unsigned int *) v;
22326 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22328 /* Assert that the strings are output in the same order as their
22329 indexes were assigned. */
22330 gcc_assert (*cur_idx == node->index);
22331 assemble_string (node->str, strlen (node->str) + 1);
22332 *cur_idx += 1;
22334 return 1;
22337 /* A helper function for dwarf2out_finish called through
22338 htab_traverse. Emit one queued .debug_str string. */
22340 static int
22341 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22343 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22345 node->form = find_string_form (node);
22346 if (node->form == DW_FORM_strp && node->refcount > 0)
22348 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22349 assemble_string (node->str, strlen (node->str) + 1);
22352 return 1;
22355 /* Output the indexed string table. */
22357 static void
22358 output_indirect_strings (void)
22360 switch_to_section (debug_str_section);
22361 if (!dwarf_split_debug_info)
22362 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22363 else
22365 unsigned int offset = 0;
22366 unsigned int cur_idx = 0;
22368 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22370 switch_to_section (debug_str_offsets_section);
22371 htab_traverse_noresize (debug_str_hash,
22372 output_index_string_offset,
22373 &offset);
22374 switch_to_section (debug_str_dwo_section);
22375 htab_traverse_noresize (debug_str_hash,
22376 output_index_string,
22377 &cur_idx);
22381 /* Callback for htab_traverse to assign an index to an entry in the
22382 table, and to write that entry to the .debug_addr section. */
22384 static int
22385 output_addr_table_entry (void **slot, void *data)
22387 addr_table_entry *entry = (addr_table_entry *) *slot;
22388 unsigned int *cur_index = (unsigned int *)data;
22390 if (entry->refcount == 0)
22392 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22393 || entry->index == NOT_INDEXED);
22394 return 1;
22397 gcc_assert (entry->index == *cur_index);
22398 (*cur_index)++;
22400 switch (entry->kind)
22402 case ate_kind_rtx:
22403 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22404 "0x%x", entry->index);
22405 break;
22406 case ate_kind_rtx_dtprel:
22407 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22408 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22409 DWARF2_ADDR_SIZE,
22410 entry->addr.rtl);
22411 fputc ('\n', asm_out_file);
22412 break;
22413 case ate_kind_label:
22414 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22415 "0x%x", entry->index);
22416 break;
22417 default:
22418 gcc_unreachable ();
22420 return 1;
22423 /* Produce the .debug_addr section. */
22425 static void
22426 output_addr_table (void)
22428 unsigned int index = 0;
22429 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22430 return;
22432 switch_to_section (debug_addr_section);
22433 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22436 #if ENABLE_ASSERT_CHECKING
22437 /* Verify that all marks are clear. */
22439 static void
22440 verify_marks_clear (dw_die_ref die)
22442 dw_die_ref c;
22444 gcc_assert (! die->die_mark);
22445 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22447 #endif /* ENABLE_ASSERT_CHECKING */
22449 /* Clear the marks for a die and its children.
22450 Be cool if the mark isn't set. */
22452 static void
22453 prune_unmark_dies (dw_die_ref die)
22455 dw_die_ref c;
22457 if (die->die_mark)
22458 die->die_mark = 0;
22459 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22462 /* Given DIE that we're marking as used, find any other dies
22463 it references as attributes and mark them as used. */
22465 static void
22466 prune_unused_types_walk_attribs (dw_die_ref die)
22468 dw_attr_ref a;
22469 unsigned ix;
22471 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22473 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22475 /* A reference to another DIE.
22476 Make sure that it will get emitted.
22477 If it was broken out into a comdat group, don't follow it. */
22478 if (! AT_ref (a)->comdat_type_p
22479 || a->dw_attr == DW_AT_specification)
22480 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22482 /* Set the string's refcount to 0 so that prune_unused_types_mark
22483 accounts properly for it. */
22484 if (AT_class (a) == dw_val_class_str)
22485 a->dw_attr_val.v.val_str->refcount = 0;
22489 /* Mark the generic parameters and arguments children DIEs of DIE. */
22491 static void
22492 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22494 dw_die_ref c;
22496 if (die == NULL || die->die_child == NULL)
22497 return;
22498 c = die->die_child;
22501 if (is_template_parameter (c))
22502 prune_unused_types_mark (c, 1);
22503 c = c->die_sib;
22504 } while (c && c != die->die_child);
22507 /* Mark DIE as being used. If DOKIDS is true, then walk down
22508 to DIE's children. */
22510 static void
22511 prune_unused_types_mark (dw_die_ref die, int dokids)
22513 dw_die_ref c;
22515 if (die->die_mark == 0)
22517 /* We haven't done this node yet. Mark it as used. */
22518 die->die_mark = 1;
22519 /* If this is the DIE of a generic type instantiation,
22520 mark the children DIEs that describe its generic parms and
22521 args. */
22522 prune_unused_types_mark_generic_parms_dies (die);
22524 /* We also have to mark its parents as used.
22525 (But we don't want to mark our parent's kids due to this,
22526 unless it is a class.) */
22527 if (die->die_parent)
22528 prune_unused_types_mark (die->die_parent,
22529 class_scope_p (die->die_parent));
22531 /* Mark any referenced nodes. */
22532 prune_unused_types_walk_attribs (die);
22534 /* If this node is a specification,
22535 also mark the definition, if it exists. */
22536 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22537 prune_unused_types_mark (die->die_definition, 1);
22540 if (dokids && die->die_mark != 2)
22542 /* We need to walk the children, but haven't done so yet.
22543 Remember that we've walked the kids. */
22544 die->die_mark = 2;
22546 /* If this is an array type, we need to make sure our
22547 kids get marked, even if they're types. If we're
22548 breaking out types into comdat sections, do this
22549 for all type definitions. */
22550 if (die->die_tag == DW_TAG_array_type
22551 || (use_debug_types
22552 && is_type_die (die) && ! is_declaration_die (die)))
22553 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22554 else
22555 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22559 /* For local classes, look if any static member functions were emitted
22560 and if so, mark them. */
22562 static void
22563 prune_unused_types_walk_local_classes (dw_die_ref die)
22565 dw_die_ref c;
22567 if (die->die_mark == 2)
22568 return;
22570 switch (die->die_tag)
22572 case DW_TAG_structure_type:
22573 case DW_TAG_union_type:
22574 case DW_TAG_class_type:
22575 break;
22577 case DW_TAG_subprogram:
22578 if (!get_AT_flag (die, DW_AT_declaration)
22579 || die->die_definition != NULL)
22580 prune_unused_types_mark (die, 1);
22581 return;
22583 default:
22584 return;
22587 /* Mark children. */
22588 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22591 /* Walk the tree DIE and mark types that we actually use. */
22593 static void
22594 prune_unused_types_walk (dw_die_ref die)
22596 dw_die_ref c;
22598 /* Don't do anything if this node is already marked and
22599 children have been marked as well. */
22600 if (die->die_mark == 2)
22601 return;
22603 switch (die->die_tag)
22605 case DW_TAG_structure_type:
22606 case DW_TAG_union_type:
22607 case DW_TAG_class_type:
22608 if (die->die_perennial_p)
22609 break;
22611 for (c = die->die_parent; c; c = c->die_parent)
22612 if (c->die_tag == DW_TAG_subprogram)
22613 break;
22615 /* Finding used static member functions inside of classes
22616 is needed just for local classes, because for other classes
22617 static member function DIEs with DW_AT_specification
22618 are emitted outside of the DW_TAG_*_type. If we ever change
22619 it, we'd need to call this even for non-local classes. */
22620 if (c)
22621 prune_unused_types_walk_local_classes (die);
22623 /* It's a type node --- don't mark it. */
22624 return;
22626 case DW_TAG_const_type:
22627 case DW_TAG_packed_type:
22628 case DW_TAG_pointer_type:
22629 case DW_TAG_reference_type:
22630 case DW_TAG_rvalue_reference_type:
22631 case DW_TAG_volatile_type:
22632 case DW_TAG_typedef:
22633 case DW_TAG_array_type:
22634 case DW_TAG_interface_type:
22635 case DW_TAG_friend:
22636 case DW_TAG_variant_part:
22637 case DW_TAG_enumeration_type:
22638 case DW_TAG_subroutine_type:
22639 case DW_TAG_string_type:
22640 case DW_TAG_set_type:
22641 case DW_TAG_subrange_type:
22642 case DW_TAG_ptr_to_member_type:
22643 case DW_TAG_file_type:
22644 if (die->die_perennial_p)
22645 break;
22647 /* It's a type node --- don't mark it. */
22648 return;
22650 default:
22651 /* Mark everything else. */
22652 break;
22655 if (die->die_mark == 0)
22657 die->die_mark = 1;
22659 /* Now, mark any dies referenced from here. */
22660 prune_unused_types_walk_attribs (die);
22663 die->die_mark = 2;
22665 /* Mark children. */
22666 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22669 /* Increment the string counts on strings referred to from DIE's
22670 attributes. */
22672 static void
22673 prune_unused_types_update_strings (dw_die_ref die)
22675 dw_attr_ref a;
22676 unsigned ix;
22678 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22679 if (AT_class (a) == dw_val_class_str)
22681 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22682 s->refcount++;
22683 /* Avoid unnecessarily putting strings that are used less than
22684 twice in the hash table. */
22685 if (s->refcount
22686 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22688 void ** slot;
22689 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22690 htab_hash_string (s->str),
22691 INSERT);
22692 gcc_assert (*slot == NULL);
22693 *slot = s;
22698 /* Remove from the tree DIE any dies that aren't marked. */
22700 static void
22701 prune_unused_types_prune (dw_die_ref die)
22703 dw_die_ref c;
22705 gcc_assert (die->die_mark);
22706 prune_unused_types_update_strings (die);
22708 if (! die->die_child)
22709 return;
22711 c = die->die_child;
22712 do {
22713 dw_die_ref prev = c;
22714 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22715 if (c == die->die_child)
22717 /* No marked children between 'prev' and the end of the list. */
22718 if (prev == c)
22719 /* No marked children at all. */
22720 die->die_child = NULL;
22721 else
22723 prev->die_sib = c->die_sib;
22724 die->die_child = prev;
22726 return;
22729 if (c != prev->die_sib)
22730 prev->die_sib = c;
22731 prune_unused_types_prune (c);
22732 } while (c != die->die_child);
22735 /* Remove dies representing declarations that we never use. */
22737 static void
22738 prune_unused_types (void)
22740 unsigned int i;
22741 limbo_die_node *node;
22742 comdat_type_node *ctnode;
22743 pubname_ref pub;
22744 dw_die_ref base_type;
22746 #if ENABLE_ASSERT_CHECKING
22747 /* All the marks should already be clear. */
22748 verify_marks_clear (comp_unit_die ());
22749 for (node = limbo_die_list; node; node = node->next)
22750 verify_marks_clear (node->die);
22751 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22752 verify_marks_clear (ctnode->root_die);
22753 #endif /* ENABLE_ASSERT_CHECKING */
22755 /* Mark types that are used in global variables. */
22756 premark_types_used_by_global_vars ();
22758 /* Set the mark on nodes that are actually used. */
22759 prune_unused_types_walk (comp_unit_die ());
22760 for (node = limbo_die_list; node; node = node->next)
22761 prune_unused_types_walk (node->die);
22762 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22764 prune_unused_types_walk (ctnode->root_die);
22765 prune_unused_types_mark (ctnode->type_die, 1);
22768 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22769 are unusual in that they are pubnames that are the children of pubtypes.
22770 They should only be marked via their parent DW_TAG_enumeration_type die,
22771 not as roots in themselves. */
22772 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22773 if (pub->die->die_tag != DW_TAG_enumerator)
22774 prune_unused_types_mark (pub->die, 1);
22775 for (i = 0; base_types.iterate (i, &base_type); i++)
22776 prune_unused_types_mark (base_type, 1);
22778 if (debug_str_hash)
22779 htab_empty (debug_str_hash);
22780 if (skeleton_debug_str_hash)
22781 htab_empty (skeleton_debug_str_hash);
22782 prune_unused_types_prune (comp_unit_die ());
22783 for (node = limbo_die_list; node; node = node->next)
22784 prune_unused_types_prune (node->die);
22785 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22786 prune_unused_types_prune (ctnode->root_die);
22788 /* Leave the marks clear. */
22789 prune_unmark_dies (comp_unit_die ());
22790 for (node = limbo_die_list; node; node = node->next)
22791 prune_unmark_dies (node->die);
22792 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22793 prune_unmark_dies (ctnode->root_die);
22796 /* Set the parameter to true if there are any relative pathnames in
22797 the file table. */
22798 static int
22799 file_table_relative_p (void ** slot, void *param)
22801 bool *p = (bool *) param;
22802 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22803 if (!IS_ABSOLUTE_PATH (d->filename))
22805 *p = true;
22806 return 0;
22808 return 1;
22811 /* Helpers to manipulate hash table of comdat type units. */
22813 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22815 typedef comdat_type_node value_type;
22816 typedef comdat_type_node compare_type;
22817 static inline hashval_t hash (const value_type *);
22818 static inline bool equal (const value_type *, const compare_type *);
22821 inline hashval_t
22822 comdat_type_hasher::hash (const value_type *type_node)
22824 hashval_t h;
22825 memcpy (&h, type_node->signature, sizeof (h));
22826 return h;
22829 inline bool
22830 comdat_type_hasher::equal (const value_type *type_node_1,
22831 const compare_type *type_node_2)
22833 return (! memcmp (type_node_1->signature, type_node_2->signature,
22834 DWARF_TYPE_SIGNATURE_SIZE));
22837 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22838 to the location it would have been added, should we know its
22839 DECL_ASSEMBLER_NAME when we added other attributes. This will
22840 probably improve compactness of debug info, removing equivalent
22841 abbrevs, and hide any differences caused by deferring the
22842 computation of the assembler name, triggered by e.g. PCH. */
22844 static inline void
22845 move_linkage_attr (dw_die_ref die)
22847 unsigned ix = vec_safe_length (die->die_attr);
22848 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22850 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22851 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22853 while (--ix > 0)
22855 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22857 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22858 break;
22861 if (ix != vec_safe_length (die->die_attr) - 1)
22863 die->die_attr->pop ();
22864 die->die_attr->quick_insert (ix, linkage);
22868 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22869 referenced from typed stack ops and count how often they are used. */
22871 static void
22872 mark_base_types (dw_loc_descr_ref loc)
22874 dw_die_ref base_type = NULL;
22876 for (; loc; loc = loc->dw_loc_next)
22878 switch (loc->dw_loc_opc)
22880 case DW_OP_GNU_regval_type:
22881 case DW_OP_GNU_deref_type:
22882 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22883 break;
22884 case DW_OP_GNU_convert:
22885 case DW_OP_GNU_reinterpret:
22886 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22887 continue;
22888 /* FALLTHRU */
22889 case DW_OP_GNU_const_type:
22890 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22891 break;
22892 case DW_OP_GNU_entry_value:
22893 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22894 continue;
22895 default:
22896 continue;
22898 gcc_assert (base_type->die_parent == comp_unit_die ());
22899 if (base_type->die_mark)
22900 base_type->die_mark++;
22901 else
22903 base_types.safe_push (base_type);
22904 base_type->die_mark = 1;
22909 /* Comparison function for sorting marked base types. */
22911 static int
22912 base_type_cmp (const void *x, const void *y)
22914 dw_die_ref dx = *(const dw_die_ref *) x;
22915 dw_die_ref dy = *(const dw_die_ref *) y;
22916 unsigned int byte_size1, byte_size2;
22917 unsigned int encoding1, encoding2;
22918 if (dx->die_mark > dy->die_mark)
22919 return -1;
22920 if (dx->die_mark < dy->die_mark)
22921 return 1;
22922 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22923 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22924 if (byte_size1 < byte_size2)
22925 return 1;
22926 if (byte_size1 > byte_size2)
22927 return -1;
22928 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22929 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22930 if (encoding1 < encoding2)
22931 return 1;
22932 if (encoding1 > encoding2)
22933 return -1;
22934 return 0;
22937 /* Move base types marked by mark_base_types as early as possible
22938 in the CU, sorted by decreasing usage count both to make the
22939 uleb128 references as small as possible and to make sure they
22940 will have die_offset already computed by calc_die_sizes when
22941 sizes of typed stack loc ops is computed. */
22943 static void
22944 move_marked_base_types (void)
22946 unsigned int i;
22947 dw_die_ref base_type, die, c;
22949 if (base_types.is_empty ())
22950 return;
22952 /* Sort by decreasing usage count, they will be added again in that
22953 order later on. */
22954 base_types.qsort (base_type_cmp);
22955 die = comp_unit_die ();
22956 c = die->die_child;
22959 dw_die_ref prev = c;
22960 c = c->die_sib;
22961 while (c->die_mark)
22963 remove_child_with_prev (c, prev);
22964 /* As base types got marked, there must be at least
22965 one node other than DW_TAG_base_type. */
22966 gcc_assert (c != c->die_sib);
22967 c = c->die_sib;
22970 while (c != die->die_child);
22971 gcc_assert (die->die_child);
22972 c = die->die_child;
22973 for (i = 0; base_types.iterate (i, &base_type); i++)
22975 base_type->die_mark = 0;
22976 base_type->die_sib = c->die_sib;
22977 c->die_sib = base_type;
22978 c = base_type;
22982 /* Helper function for resolve_addr, attempt to resolve
22983 one CONST_STRING, return non-zero if not successful. Similarly verify that
22984 SYMBOL_REFs refer to variables emitted in the current CU. */
22986 static int
22987 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22989 rtx rtl = *addr;
22991 if (GET_CODE (rtl) == CONST_STRING)
22993 size_t len = strlen (XSTR (rtl, 0)) + 1;
22994 tree t = build_string (len, XSTR (rtl, 0));
22995 tree tlen = size_int (len - 1);
22996 TREE_TYPE (t)
22997 = build_array_type (char_type_node, build_index_type (tlen));
22998 rtl = lookup_constant_def (t);
22999 if (!rtl || !MEM_P (rtl))
23000 return 1;
23001 rtl = XEXP (rtl, 0);
23002 if (GET_CODE (rtl) == SYMBOL_REF
23003 && SYMBOL_REF_DECL (rtl)
23004 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23005 return 1;
23006 vec_safe_push (used_rtx_array, rtl);
23007 *addr = rtl;
23008 return 0;
23011 if (GET_CODE (rtl) == SYMBOL_REF
23012 && SYMBOL_REF_DECL (rtl))
23014 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23016 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23017 return 1;
23019 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23020 return 1;
23023 if (GET_CODE (rtl) == CONST
23024 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
23025 return 1;
23027 return 0;
23030 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23031 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23032 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23034 static rtx
23035 string_cst_pool_decl (tree t)
23037 rtx rtl = output_constant_def (t, 1);
23038 unsigned char *array;
23039 dw_loc_descr_ref l;
23040 tree decl;
23041 size_t len;
23042 dw_die_ref ref;
23044 if (!rtl || !MEM_P (rtl))
23045 return NULL_RTX;
23046 rtl = XEXP (rtl, 0);
23047 if (GET_CODE (rtl) != SYMBOL_REF
23048 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23049 return NULL_RTX;
23051 decl = SYMBOL_REF_DECL (rtl);
23052 if (!lookup_decl_die (decl))
23054 len = TREE_STRING_LENGTH (t);
23055 vec_safe_push (used_rtx_array, rtl);
23056 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23057 array = (unsigned char *) ggc_alloc_atomic (len);
23058 memcpy (array, TREE_STRING_POINTER (t), len);
23059 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23060 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23061 l->dw_loc_oprnd2.v.val_vec.length = len;
23062 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23063 l->dw_loc_oprnd2.v.val_vec.array = array;
23064 add_AT_loc (ref, DW_AT_location, l);
23065 equate_decl_number_to_die (decl, ref);
23067 return rtl;
23070 /* Helper function of resolve_addr_in_expr. LOC is
23071 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23072 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23073 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23074 with DW_OP_GNU_implicit_pointer if possible
23075 and return true, if unsuccessful, return false. */
23077 static bool
23078 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23080 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23081 HOST_WIDE_INT offset = 0;
23082 dw_die_ref ref = NULL;
23083 tree decl;
23085 if (GET_CODE (rtl) == CONST
23086 && GET_CODE (XEXP (rtl, 0)) == PLUS
23087 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23089 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23090 rtl = XEXP (XEXP (rtl, 0), 0);
23092 if (GET_CODE (rtl) == CONST_STRING)
23094 size_t len = strlen (XSTR (rtl, 0)) + 1;
23095 tree t = build_string (len, XSTR (rtl, 0));
23096 tree tlen = size_int (len - 1);
23098 TREE_TYPE (t)
23099 = build_array_type (char_type_node, build_index_type (tlen));
23100 rtl = string_cst_pool_decl (t);
23101 if (!rtl)
23102 return false;
23104 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23106 decl = SYMBOL_REF_DECL (rtl);
23107 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23109 ref = lookup_decl_die (decl);
23110 if (ref && (get_AT (ref, DW_AT_location)
23111 || get_AT (ref, DW_AT_const_value)))
23113 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23114 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23115 loc->dw_loc_oprnd1.val_entry = NULL;
23116 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23117 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23118 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23119 loc->dw_loc_oprnd2.v.val_int = offset;
23120 return true;
23124 return false;
23127 /* Helper function for resolve_addr, handle one location
23128 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23129 the location list couldn't be resolved. */
23131 static bool
23132 resolve_addr_in_expr (dw_loc_descr_ref loc)
23134 dw_loc_descr_ref keep = NULL;
23135 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23136 switch (loc->dw_loc_opc)
23138 case DW_OP_addr:
23139 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23141 if ((prev == NULL
23142 || prev->dw_loc_opc == DW_OP_piece
23143 || prev->dw_loc_opc == DW_OP_bit_piece)
23144 && loc->dw_loc_next
23145 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23146 && !dwarf_strict
23147 && optimize_one_addr_into_implicit_ptr (loc))
23148 break;
23149 return false;
23151 break;
23152 case DW_OP_GNU_addr_index:
23153 case DW_OP_GNU_const_index:
23154 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
23155 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23156 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
23157 NULL))
23158 return false;
23159 break;
23160 case DW_OP_const4u:
23161 case DW_OP_const8u:
23162 if (loc->dtprel
23163 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23164 return false;
23165 break;
23166 case DW_OP_plus_uconst:
23167 if (size_of_loc_descr (loc)
23168 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23170 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23172 dw_loc_descr_ref repl
23173 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23174 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23175 add_loc_descr (&repl, loc->dw_loc_next);
23176 *loc = *repl;
23178 break;
23179 case DW_OP_implicit_value:
23180 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23181 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23182 return false;
23183 break;
23184 case DW_OP_GNU_implicit_pointer:
23185 case DW_OP_GNU_parameter_ref:
23186 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23188 dw_die_ref ref
23189 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23190 if (ref == NULL)
23191 return false;
23192 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23193 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23194 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23196 break;
23197 case DW_OP_GNU_const_type:
23198 case DW_OP_GNU_regval_type:
23199 case DW_OP_GNU_deref_type:
23200 case DW_OP_GNU_convert:
23201 case DW_OP_GNU_reinterpret:
23202 while (loc->dw_loc_next
23203 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23205 dw_die_ref base1, base2;
23206 unsigned enc1, enc2, size1, size2;
23207 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23208 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23209 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23210 else if (loc->dw_loc_oprnd1.val_class
23211 == dw_val_class_unsigned_const)
23212 break;
23213 else
23214 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23215 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23216 == dw_val_class_unsigned_const)
23217 break;
23218 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23219 gcc_assert (base1->die_tag == DW_TAG_base_type
23220 && base2->die_tag == DW_TAG_base_type);
23221 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23222 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23223 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23224 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23225 if (size1 == size2
23226 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23227 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23228 && loc != keep)
23229 || enc1 == enc2))
23231 /* Optimize away next DW_OP_GNU_convert after
23232 adjusting LOC's base type die reference. */
23233 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23234 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23235 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23236 else
23237 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23238 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23239 continue;
23241 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23242 point typed stack entry. */
23243 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23244 keep = loc->dw_loc_next;
23245 break;
23247 break;
23248 default:
23249 break;
23251 return true;
23254 /* Helper function of resolve_addr. DIE had DW_AT_location of
23255 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23256 and DW_OP_addr couldn't be resolved. resolve_addr has already
23257 removed the DW_AT_location attribute. This function attempts to
23258 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23259 to it or DW_AT_const_value attribute, if possible. */
23261 static void
23262 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23264 if (TREE_CODE (decl) != VAR_DECL
23265 || lookup_decl_die (decl) != die
23266 || DECL_EXTERNAL (decl)
23267 || !TREE_STATIC (decl)
23268 || DECL_INITIAL (decl) == NULL_TREE
23269 || DECL_P (DECL_INITIAL (decl))
23270 || get_AT (die, DW_AT_const_value))
23271 return;
23273 tree init = DECL_INITIAL (decl);
23274 HOST_WIDE_INT offset = 0;
23275 /* For variables that have been optimized away and thus
23276 don't have a memory location, see if we can emit
23277 DW_AT_const_value instead. */
23278 if (tree_add_const_value_attribute (die, init))
23279 return;
23280 if (dwarf_strict)
23281 return;
23282 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23283 and ADDR_EXPR refers to a decl that has DW_AT_location or
23284 DW_AT_const_value (but isn't addressable, otherwise
23285 resolving the original DW_OP_addr wouldn't fail), see if
23286 we can add DW_OP_GNU_implicit_pointer. */
23287 STRIP_NOPS (init);
23288 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23289 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23291 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23292 init = TREE_OPERAND (init, 0);
23293 STRIP_NOPS (init);
23295 if (TREE_CODE (init) != ADDR_EXPR)
23296 return;
23297 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23298 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23299 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23300 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23301 && TREE_OPERAND (init, 0) != decl))
23303 dw_die_ref ref;
23304 dw_loc_descr_ref l;
23306 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23308 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23309 if (!rtl)
23310 return;
23311 decl = SYMBOL_REF_DECL (rtl);
23313 else
23314 decl = TREE_OPERAND (init, 0);
23315 ref = lookup_decl_die (decl);
23316 if (ref == NULL
23317 || (!get_AT (ref, DW_AT_location)
23318 && !get_AT (ref, DW_AT_const_value)))
23319 return;
23320 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23321 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23322 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23323 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23324 add_AT_loc (die, DW_AT_location, l);
23328 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23329 an address in .rodata section if the string literal is emitted there,
23330 or remove the containing location list or replace DW_AT_const_value
23331 with DW_AT_location and empty location expression, if it isn't found
23332 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23333 to something that has been emitted in the current CU. */
23335 static void
23336 resolve_addr (dw_die_ref die)
23338 dw_die_ref c;
23339 dw_attr_ref a;
23340 dw_loc_list_ref *curr, *start, loc;
23341 unsigned ix;
23343 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23344 switch (AT_class (a))
23346 case dw_val_class_loc_list:
23347 start = curr = AT_loc_list_ptr (a);
23348 loc = *curr;
23349 gcc_assert (loc);
23350 /* The same list can be referenced more than once. See if we have
23351 already recorded the result from a previous pass. */
23352 if (loc->replaced)
23353 *curr = loc->dw_loc_next;
23354 else if (!loc->resolved_addr)
23356 /* As things stand, we do not expect or allow one die to
23357 reference a suffix of another die's location list chain.
23358 References must be identical or completely separate.
23359 There is therefore no need to cache the result of this
23360 pass on any list other than the first; doing so
23361 would lead to unnecessary writes. */
23362 while (*curr)
23364 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23365 if (!resolve_addr_in_expr ((*curr)->expr))
23367 dw_loc_list_ref next = (*curr)->dw_loc_next;
23368 dw_loc_descr_ref l = (*curr)->expr;
23370 if (next && (*curr)->ll_symbol)
23372 gcc_assert (!next->ll_symbol);
23373 next->ll_symbol = (*curr)->ll_symbol;
23375 if (dwarf_split_debug_info)
23376 remove_loc_list_addr_table_entries (l);
23377 *curr = next;
23379 else
23381 mark_base_types ((*curr)->expr);
23382 curr = &(*curr)->dw_loc_next;
23385 if (loc == *start)
23386 loc->resolved_addr = 1;
23387 else
23389 loc->replaced = 1;
23390 loc->dw_loc_next = *start;
23393 if (!*start)
23395 remove_AT (die, a->dw_attr);
23396 ix--;
23398 break;
23399 case dw_val_class_loc:
23401 dw_loc_descr_ref l = AT_loc (a);
23402 /* For -gdwarf-2 don't attempt to optimize
23403 DW_AT_data_member_location containing
23404 DW_OP_plus_uconst - older consumers might
23405 rely on it being that op instead of a more complex,
23406 but shorter, location description. */
23407 if ((dwarf_version > 2
23408 || a->dw_attr != DW_AT_data_member_location
23409 || l == NULL
23410 || l->dw_loc_opc != DW_OP_plus_uconst
23411 || l->dw_loc_next != NULL)
23412 && !resolve_addr_in_expr (l))
23414 if (dwarf_split_debug_info)
23415 remove_loc_list_addr_table_entries (l);
23416 if (l != NULL
23417 && l->dw_loc_next == NULL
23418 && l->dw_loc_opc == DW_OP_addr
23419 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23420 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23421 && a->dw_attr == DW_AT_location)
23423 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23424 remove_AT (die, a->dw_attr);
23425 ix--;
23426 optimize_location_into_implicit_ptr (die, decl);
23427 break;
23429 remove_AT (die, a->dw_attr);
23430 ix--;
23432 else
23433 mark_base_types (l);
23435 break;
23436 case dw_val_class_addr:
23437 if (a->dw_attr == DW_AT_const_value
23438 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23440 if (AT_index (a) != NOT_INDEXED)
23441 remove_addr_table_entry (a->dw_attr_val.val_entry);
23442 remove_AT (die, a->dw_attr);
23443 ix--;
23445 if (die->die_tag == DW_TAG_GNU_call_site
23446 && a->dw_attr == DW_AT_abstract_origin)
23448 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23449 dw_die_ref tdie = lookup_decl_die (tdecl);
23450 if (tdie == NULL
23451 && DECL_EXTERNAL (tdecl)
23452 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23454 force_decl_die (tdecl);
23455 tdie = lookup_decl_die (tdecl);
23457 if (tdie)
23459 a->dw_attr_val.val_class = dw_val_class_die_ref;
23460 a->dw_attr_val.v.val_die_ref.die = tdie;
23461 a->dw_attr_val.v.val_die_ref.external = 0;
23463 else
23465 if (AT_index (a) != NOT_INDEXED)
23466 remove_addr_table_entry (a->dw_attr_val.val_entry);
23467 remove_AT (die, a->dw_attr);
23468 ix--;
23471 break;
23472 default:
23473 break;
23476 FOR_EACH_CHILD (die, c, resolve_addr (c));
23479 /* Helper routines for optimize_location_lists.
23480 This pass tries to share identical local lists in .debug_loc
23481 section. */
23483 /* Iteratively hash operands of LOC opcode. */
23485 static hashval_t
23486 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23488 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23489 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23491 switch (loc->dw_loc_opc)
23493 case DW_OP_const4u:
23494 case DW_OP_const8u:
23495 if (loc->dtprel)
23496 goto hash_addr;
23497 /* FALLTHRU */
23498 case DW_OP_const1u:
23499 case DW_OP_const1s:
23500 case DW_OP_const2u:
23501 case DW_OP_const2s:
23502 case DW_OP_const4s:
23503 case DW_OP_const8s:
23504 case DW_OP_constu:
23505 case DW_OP_consts:
23506 case DW_OP_pick:
23507 case DW_OP_plus_uconst:
23508 case DW_OP_breg0:
23509 case DW_OP_breg1:
23510 case DW_OP_breg2:
23511 case DW_OP_breg3:
23512 case DW_OP_breg4:
23513 case DW_OP_breg5:
23514 case DW_OP_breg6:
23515 case DW_OP_breg7:
23516 case DW_OP_breg8:
23517 case DW_OP_breg9:
23518 case DW_OP_breg10:
23519 case DW_OP_breg11:
23520 case DW_OP_breg12:
23521 case DW_OP_breg13:
23522 case DW_OP_breg14:
23523 case DW_OP_breg15:
23524 case DW_OP_breg16:
23525 case DW_OP_breg17:
23526 case DW_OP_breg18:
23527 case DW_OP_breg19:
23528 case DW_OP_breg20:
23529 case DW_OP_breg21:
23530 case DW_OP_breg22:
23531 case DW_OP_breg23:
23532 case DW_OP_breg24:
23533 case DW_OP_breg25:
23534 case DW_OP_breg26:
23535 case DW_OP_breg27:
23536 case DW_OP_breg28:
23537 case DW_OP_breg29:
23538 case DW_OP_breg30:
23539 case DW_OP_breg31:
23540 case DW_OP_regx:
23541 case DW_OP_fbreg:
23542 case DW_OP_piece:
23543 case DW_OP_deref_size:
23544 case DW_OP_xderef_size:
23545 hash = iterative_hash_object (val1->v.val_int, hash);
23546 break;
23547 case DW_OP_skip:
23548 case DW_OP_bra:
23550 int offset;
23552 gcc_assert (val1->val_class == dw_val_class_loc);
23553 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23554 hash = iterative_hash_object (offset, hash);
23556 break;
23557 case DW_OP_implicit_value:
23558 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23559 switch (val2->val_class)
23561 case dw_val_class_const:
23562 hash = iterative_hash_object (val2->v.val_int, hash);
23563 break;
23564 case dw_val_class_vec:
23566 unsigned int elt_size = val2->v.val_vec.elt_size;
23567 unsigned int len = val2->v.val_vec.length;
23569 hash = iterative_hash_object (elt_size, hash);
23570 hash = iterative_hash_object (len, hash);
23571 hash = iterative_hash (val2->v.val_vec.array,
23572 len * elt_size, hash);
23574 break;
23575 case dw_val_class_const_double:
23576 hash = iterative_hash_object (val2->v.val_double.low, hash);
23577 hash = iterative_hash_object (val2->v.val_double.high, hash);
23578 break;
23579 case dw_val_class_wide_int:
23580 hash = iterative_hash_object (*val2->v.val_wide, hash);
23581 break;
23582 case dw_val_class_addr:
23583 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23584 break;
23585 default:
23586 gcc_unreachable ();
23588 break;
23589 case DW_OP_bregx:
23590 case DW_OP_bit_piece:
23591 hash = iterative_hash_object (val1->v.val_int, hash);
23592 hash = iterative_hash_object (val2->v.val_int, hash);
23593 break;
23594 case DW_OP_addr:
23595 hash_addr:
23596 if (loc->dtprel)
23598 unsigned char dtprel = 0xd1;
23599 hash = iterative_hash_object (dtprel, hash);
23601 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23602 break;
23603 case DW_OP_GNU_addr_index:
23604 case DW_OP_GNU_const_index:
23606 if (loc->dtprel)
23608 unsigned char dtprel = 0xd1;
23609 hash = iterative_hash_object (dtprel, hash);
23611 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23613 break;
23614 case DW_OP_GNU_implicit_pointer:
23615 hash = iterative_hash_object (val2->v.val_int, hash);
23616 break;
23617 case DW_OP_GNU_entry_value:
23618 hash = hash_loc_operands (val1->v.val_loc, hash);
23619 break;
23620 case DW_OP_GNU_regval_type:
23621 case DW_OP_GNU_deref_type:
23623 unsigned int byte_size
23624 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23625 unsigned int encoding
23626 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23627 hash = iterative_hash_object (val1->v.val_int, hash);
23628 hash = iterative_hash_object (byte_size, hash);
23629 hash = iterative_hash_object (encoding, hash);
23631 break;
23632 case DW_OP_GNU_convert:
23633 case DW_OP_GNU_reinterpret:
23634 if (val1->val_class == dw_val_class_unsigned_const)
23636 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23637 break;
23639 /* FALLTHRU */
23640 case DW_OP_GNU_const_type:
23642 unsigned int byte_size
23643 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23644 unsigned int encoding
23645 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23646 hash = iterative_hash_object (byte_size, hash);
23647 hash = iterative_hash_object (encoding, hash);
23648 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23649 break;
23650 hash = iterative_hash_object (val2->val_class, hash);
23651 switch (val2->val_class)
23653 case dw_val_class_const:
23654 hash = iterative_hash_object (val2->v.val_int, hash);
23655 break;
23656 case dw_val_class_vec:
23658 unsigned int elt_size = val2->v.val_vec.elt_size;
23659 unsigned int len = val2->v.val_vec.length;
23661 hash = iterative_hash_object (elt_size, hash);
23662 hash = iterative_hash_object (len, hash);
23663 hash = iterative_hash (val2->v.val_vec.array,
23664 len * elt_size, hash);
23666 break;
23667 case dw_val_class_const_double:
23668 hash = iterative_hash_object (val2->v.val_double.low, hash);
23669 hash = iterative_hash_object (val2->v.val_double.high, hash);
23670 break;
23671 case dw_val_class_wide_int:
23672 hash = iterative_hash_object (*val2->v.val_wide, hash);
23673 break;
23674 default:
23675 gcc_unreachable ();
23678 break;
23680 default:
23681 /* Other codes have no operands. */
23682 break;
23684 return hash;
23687 /* Iteratively hash the whole DWARF location expression LOC. */
23689 static inline hashval_t
23690 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23692 dw_loc_descr_ref l;
23693 bool sizes_computed = false;
23694 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23695 size_of_locs (loc);
23697 for (l = loc; l != NULL; l = l->dw_loc_next)
23699 enum dwarf_location_atom opc = l->dw_loc_opc;
23700 hash = iterative_hash_object (opc, hash);
23701 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23703 size_of_locs (loc);
23704 sizes_computed = true;
23706 hash = hash_loc_operands (l, hash);
23708 return hash;
23711 /* Compute hash of the whole location list LIST_HEAD. */
23713 static inline void
23714 hash_loc_list (dw_loc_list_ref list_head)
23716 dw_loc_list_ref curr = list_head;
23717 hashval_t hash = 0;
23719 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23721 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23722 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23723 if (curr->section)
23724 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23725 hash);
23726 hash = hash_locs (curr->expr, hash);
23728 list_head->hash = hash;
23731 /* Return true if X and Y opcodes have the same operands. */
23733 static inline bool
23734 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23736 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23737 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23738 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23739 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23741 switch (x->dw_loc_opc)
23743 case DW_OP_const4u:
23744 case DW_OP_const8u:
23745 if (x->dtprel)
23746 goto hash_addr;
23747 /* FALLTHRU */
23748 case DW_OP_const1u:
23749 case DW_OP_const1s:
23750 case DW_OP_const2u:
23751 case DW_OP_const2s:
23752 case DW_OP_const4s:
23753 case DW_OP_const8s:
23754 case DW_OP_constu:
23755 case DW_OP_consts:
23756 case DW_OP_pick:
23757 case DW_OP_plus_uconst:
23758 case DW_OP_breg0:
23759 case DW_OP_breg1:
23760 case DW_OP_breg2:
23761 case DW_OP_breg3:
23762 case DW_OP_breg4:
23763 case DW_OP_breg5:
23764 case DW_OP_breg6:
23765 case DW_OP_breg7:
23766 case DW_OP_breg8:
23767 case DW_OP_breg9:
23768 case DW_OP_breg10:
23769 case DW_OP_breg11:
23770 case DW_OP_breg12:
23771 case DW_OP_breg13:
23772 case DW_OP_breg14:
23773 case DW_OP_breg15:
23774 case DW_OP_breg16:
23775 case DW_OP_breg17:
23776 case DW_OP_breg18:
23777 case DW_OP_breg19:
23778 case DW_OP_breg20:
23779 case DW_OP_breg21:
23780 case DW_OP_breg22:
23781 case DW_OP_breg23:
23782 case DW_OP_breg24:
23783 case DW_OP_breg25:
23784 case DW_OP_breg26:
23785 case DW_OP_breg27:
23786 case DW_OP_breg28:
23787 case DW_OP_breg29:
23788 case DW_OP_breg30:
23789 case DW_OP_breg31:
23790 case DW_OP_regx:
23791 case DW_OP_fbreg:
23792 case DW_OP_piece:
23793 case DW_OP_deref_size:
23794 case DW_OP_xderef_size:
23795 return valx1->v.val_int == valy1->v.val_int;
23796 case DW_OP_skip:
23797 case DW_OP_bra:
23798 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23799 can cause irrelevant differences in dw_loc_addr. */
23800 gcc_assert (valx1->val_class == dw_val_class_loc
23801 && valy1->val_class == dw_val_class_loc
23802 && (dwarf_split_debug_info
23803 || x->dw_loc_addr == y->dw_loc_addr));
23804 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23805 case DW_OP_implicit_value:
23806 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23807 || valx2->val_class != valy2->val_class)
23808 return false;
23809 switch (valx2->val_class)
23811 case dw_val_class_const:
23812 return valx2->v.val_int == valy2->v.val_int;
23813 case dw_val_class_vec:
23814 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23815 && valx2->v.val_vec.length == valy2->v.val_vec.length
23816 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23817 valx2->v.val_vec.elt_size
23818 * valx2->v.val_vec.length) == 0;
23819 case dw_val_class_const_double:
23820 return valx2->v.val_double.low == valy2->v.val_double.low
23821 && valx2->v.val_double.high == valy2->v.val_double.high;
23822 case dw_val_class_wide_int:
23823 return *valx2->v.val_wide == *valy2->v.val_wide;
23824 case dw_val_class_addr:
23825 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23826 default:
23827 gcc_unreachable ();
23829 case DW_OP_bregx:
23830 case DW_OP_bit_piece:
23831 return valx1->v.val_int == valy1->v.val_int
23832 && valx2->v.val_int == valy2->v.val_int;
23833 case DW_OP_addr:
23834 hash_addr:
23835 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23836 case DW_OP_GNU_addr_index:
23837 case DW_OP_GNU_const_index:
23839 rtx ax1 = valx1->val_entry->addr.rtl;
23840 rtx ay1 = valy1->val_entry->addr.rtl;
23841 return rtx_equal_p (ax1, ay1);
23843 case DW_OP_GNU_implicit_pointer:
23844 return valx1->val_class == dw_val_class_die_ref
23845 && valx1->val_class == valy1->val_class
23846 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23847 && valx2->v.val_int == valy2->v.val_int;
23848 case DW_OP_GNU_entry_value:
23849 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23850 case DW_OP_GNU_const_type:
23851 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23852 || valx2->val_class != valy2->val_class)
23853 return false;
23854 switch (valx2->val_class)
23856 case dw_val_class_const:
23857 return valx2->v.val_int == valy2->v.val_int;
23858 case dw_val_class_vec:
23859 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23860 && valx2->v.val_vec.length == valy2->v.val_vec.length
23861 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23862 valx2->v.val_vec.elt_size
23863 * valx2->v.val_vec.length) == 0;
23864 case dw_val_class_const_double:
23865 return valx2->v.val_double.low == valy2->v.val_double.low
23866 && valx2->v.val_double.high == valy2->v.val_double.high;
23867 case dw_val_class_wide_int:
23868 return *valx2->v.val_wide == *valy2->v.val_wide;
23869 default:
23870 gcc_unreachable ();
23872 case DW_OP_GNU_regval_type:
23873 case DW_OP_GNU_deref_type:
23874 return valx1->v.val_int == valy1->v.val_int
23875 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23876 case DW_OP_GNU_convert:
23877 case DW_OP_GNU_reinterpret:
23878 if (valx1->val_class != valy1->val_class)
23879 return false;
23880 if (valx1->val_class == dw_val_class_unsigned_const)
23881 return valx1->v.val_unsigned == valy1->v.val_unsigned;
23882 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23883 case DW_OP_GNU_parameter_ref:
23884 return valx1->val_class == dw_val_class_die_ref
23885 && valx1->val_class == valy1->val_class
23886 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23887 default:
23888 /* Other codes have no operands. */
23889 return true;
23893 /* Return true if DWARF location expressions X and Y are the same. */
23895 static inline bool
23896 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23898 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23899 if (x->dw_loc_opc != y->dw_loc_opc
23900 || x->dtprel != y->dtprel
23901 || !compare_loc_operands (x, y))
23902 break;
23903 return x == NULL && y == NULL;
23906 /* Hashtable helpers. */
23908 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
23910 typedef dw_loc_list_struct value_type;
23911 typedef dw_loc_list_struct compare_type;
23912 static inline hashval_t hash (const value_type *);
23913 static inline bool equal (const value_type *, const compare_type *);
23916 /* Return precomputed hash of location list X. */
23918 inline hashval_t
23919 loc_list_hasher::hash (const value_type *x)
23921 return x->hash;
23924 /* Return true if location lists A and B are the same. */
23926 inline bool
23927 loc_list_hasher::equal (const value_type *a, const compare_type *b)
23929 if (a == b)
23930 return 1;
23931 if (a->hash != b->hash)
23932 return 0;
23933 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23934 if (strcmp (a->begin, b->begin) != 0
23935 || strcmp (a->end, b->end) != 0
23936 || (a->section == NULL) != (b->section == NULL)
23937 || (a->section && strcmp (a->section, b->section) != 0)
23938 || !compare_locs (a->expr, b->expr))
23939 break;
23940 return a == NULL && b == NULL;
23943 typedef hash_table <loc_list_hasher> loc_list_hash_type;
23946 /* Recursively optimize location lists referenced from DIE
23947 children and share them whenever possible. */
23949 static void
23950 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
23952 dw_die_ref c;
23953 dw_attr_ref a;
23954 unsigned ix;
23955 dw_loc_list_struct **slot;
23957 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23958 if (AT_class (a) == dw_val_class_loc_list)
23960 dw_loc_list_ref list = AT_loc_list (a);
23961 /* TODO: perform some optimizations here, before hashing
23962 it and storing into the hash table. */
23963 hash_loc_list (list);
23964 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
23965 if (*slot == NULL)
23966 *slot = list;
23967 else
23968 a->dw_attr_val.v.val_loc_list = *slot;
23971 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23975 /* Recursively assign each location list a unique index into the debug_addr
23976 section. */
23978 static void
23979 index_location_lists (dw_die_ref die)
23981 dw_die_ref c;
23982 dw_attr_ref a;
23983 unsigned ix;
23985 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23986 if (AT_class (a) == dw_val_class_loc_list)
23988 dw_loc_list_ref list = AT_loc_list (a);
23989 dw_loc_list_ref curr;
23990 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23992 /* Don't index an entry that has already been indexed
23993 or won't be output. */
23994 if (curr->begin_entry != NULL
23995 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23996 continue;
23998 curr->begin_entry
23999 = add_addr_table_entry (xstrdup (curr->begin),
24000 ate_kind_label);
24004 FOR_EACH_CHILD (die, c, index_location_lists (c));
24007 /* Optimize location lists referenced from DIE
24008 children and share them whenever possible. */
24010 static void
24011 optimize_location_lists (dw_die_ref die)
24013 loc_list_hash_type htab;
24014 htab.create (500);
24015 optimize_location_lists_1 (die, htab);
24016 htab.dispose ();
24019 /* Output stuff that dwarf requires at the end of every file,
24020 and generate the DWARF-2 debugging info. */
24022 static void
24023 dwarf2out_finish (const char *filename)
24025 limbo_die_node *node, *next_node;
24026 comdat_type_node *ctnode;
24027 hash_table <comdat_type_hasher> comdat_type_table;
24028 unsigned int i;
24029 dw_die_ref main_comp_unit_die;
24031 /* PCH might result in DW_AT_producer string being restored from the
24032 header compilation, so always fill it with empty string initially
24033 and overwrite only here. */
24034 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24035 producer_string = gen_producer_string ();
24036 producer->dw_attr_val.v.val_str->refcount--;
24037 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24039 gen_scheduled_generic_parms_dies ();
24040 gen_remaining_tmpl_value_param_die_attribute ();
24042 /* Add the name for the main input file now. We delayed this from
24043 dwarf2out_init to avoid complications with PCH. */
24044 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24045 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24046 add_comp_dir_attribute (comp_unit_die ());
24047 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24049 bool p = false;
24050 htab_traverse (file_table, file_table_relative_p, &p);
24051 if (p)
24052 add_comp_dir_attribute (comp_unit_die ());
24055 if (deferred_locations_list)
24056 for (i = 0; i < deferred_locations_list->length (); i++)
24058 add_location_or_const_value_attribute (
24059 (*deferred_locations_list)[i].die,
24060 (*deferred_locations_list)[i].variable,
24061 false,
24062 DW_AT_location);
24065 /* Traverse the limbo die list, and add parent/child links. The only
24066 dies without parents that should be here are concrete instances of
24067 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24068 For concrete instances, we can get the parent die from the abstract
24069 instance. */
24070 for (node = limbo_die_list; node; node = next_node)
24072 dw_die_ref die = node->die;
24073 next_node = node->next;
24075 if (die->die_parent == NULL)
24077 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24079 if (origin && origin->die_parent)
24080 add_child_die (origin->die_parent, die);
24081 else if (is_cu_die (die))
24083 else if (seen_error ())
24084 /* It's OK to be confused by errors in the input. */
24085 add_child_die (comp_unit_die (), die);
24086 else
24088 /* In certain situations, the lexical block containing a
24089 nested function can be optimized away, which results
24090 in the nested function die being orphaned. Likewise
24091 with the return type of that nested function. Force
24092 this to be a child of the containing function.
24094 It may happen that even the containing function got fully
24095 inlined and optimized out. In that case we are lost and
24096 assign the empty child. This should not be big issue as
24097 the function is likely unreachable too. */
24098 gcc_assert (node->created_for);
24100 if (DECL_P (node->created_for))
24101 origin = get_context_die (DECL_CONTEXT (node->created_for));
24102 else if (TYPE_P (node->created_for))
24103 origin = scope_die_for (node->created_for, comp_unit_die ());
24104 else
24105 origin = comp_unit_die ();
24107 add_child_die (origin, die);
24112 limbo_die_list = NULL;
24114 #if ENABLE_ASSERT_CHECKING
24116 dw_die_ref die = comp_unit_die (), c;
24117 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24119 #endif
24120 resolve_addr (comp_unit_die ());
24121 move_marked_base_types ();
24123 for (node = deferred_asm_name; node; node = node->next)
24125 tree decl = node->created_for;
24126 /* When generating LTO bytecode we can not generate new assembler
24127 names at this point and all important decls got theirs via
24128 free-lang-data. */
24129 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24130 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24132 add_linkage_attr (node->die, decl);
24133 move_linkage_attr (node->die);
24137 deferred_asm_name = NULL;
24139 /* Walk through the list of incomplete types again, trying once more to
24140 emit full debugging info for them. */
24141 retry_incomplete_types ();
24143 if (flag_eliminate_unused_debug_types)
24144 prune_unused_types ();
24146 /* Generate separate COMDAT sections for type DIEs. */
24147 if (use_debug_types)
24149 break_out_comdat_types (comp_unit_die ());
24151 /* Each new type_unit DIE was added to the limbo die list when created.
24152 Since these have all been added to comdat_type_list, clear the
24153 limbo die list. */
24154 limbo_die_list = NULL;
24156 /* For each new comdat type unit, copy declarations for incomplete
24157 types to make the new unit self-contained (i.e., no direct
24158 references to the main compile unit). */
24159 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24160 copy_decls_for_unworthy_types (ctnode->root_die);
24161 copy_decls_for_unworthy_types (comp_unit_die ());
24163 /* In the process of copying declarations from one unit to another,
24164 we may have left some declarations behind that are no longer
24165 referenced. Prune them. */
24166 prune_unused_types ();
24169 /* Generate separate CUs for each of the include files we've seen.
24170 They will go into limbo_die_list. */
24171 if (flag_eliminate_dwarf2_dups)
24172 break_out_includes (comp_unit_die ());
24174 /* Traverse the DIE's and add add sibling attributes to those DIE's
24175 that have children. */
24176 add_sibling_attributes (comp_unit_die ());
24177 for (node = limbo_die_list; node; node = node->next)
24178 add_sibling_attributes (node->die);
24179 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24180 add_sibling_attributes (ctnode->root_die);
24182 /* When splitting DWARF info, we put some attributes in the
24183 skeleton compile_unit DIE that remains in the .o, while
24184 most attributes go in the DWO compile_unit_die. */
24185 if (dwarf_split_debug_info)
24186 main_comp_unit_die = gen_compile_unit_die (NULL);
24187 else
24188 main_comp_unit_die = comp_unit_die ();
24190 /* Output a terminator label for the .text section. */
24191 switch_to_section (text_section);
24192 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24193 if (cold_text_section)
24195 switch_to_section (cold_text_section);
24196 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24199 /* We can only use the low/high_pc attributes if all of the code was
24200 in .text. */
24201 if (!have_multiple_function_sections
24202 || (dwarf_version < 3 && dwarf_strict))
24204 /* Don't add if the CU has no associated code. */
24205 if (text_section_used)
24206 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24207 text_end_label, true);
24209 else
24211 unsigned fde_idx;
24212 dw_fde_ref fde;
24213 bool range_list_added = false;
24215 if (text_section_used)
24216 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24217 text_end_label, &range_list_added, true);
24218 if (cold_text_section_used)
24219 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24220 cold_end_label, &range_list_added, true);
24222 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24224 if (DECL_IGNORED_P (fde->decl))
24225 continue;
24226 if (!fde->in_std_section)
24227 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24228 fde->dw_fde_end, &range_list_added,
24229 true);
24230 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24231 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24232 fde->dw_fde_second_end, &range_list_added,
24233 true);
24236 if (range_list_added)
24238 /* We need to give .debug_loc and .debug_ranges an appropriate
24239 "base address". Use zero so that these addresses become
24240 absolute. Historically, we've emitted the unexpected
24241 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24242 Emit both to give time for other tools to adapt. */
24243 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24244 if (! dwarf_strict && dwarf_version < 4)
24245 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24247 add_ranges (NULL);
24251 if (debug_info_level >= DINFO_LEVEL_NORMAL)
24252 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24253 debug_line_section_label);
24255 if (have_macinfo)
24256 add_AT_macptr (comp_unit_die (),
24257 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24258 macinfo_section_label);
24260 if (dwarf_split_debug_info && addr_index_table != NULL)
24262 /* optimize_location_lists calculates the size of the lists,
24263 so index them first, and assign indices to the entries.
24264 Although optimize_location_lists will remove entries from
24265 the table, it only does so for duplicates, and therefore
24266 only reduces ref_counts to 1. */
24267 unsigned int index = 0;
24268 index_location_lists (comp_unit_die ());
24269 htab_traverse_noresize (addr_index_table,
24270 index_addr_table_entry, &index);
24272 if (have_location_lists)
24273 optimize_location_lists (comp_unit_die ());
24275 save_macinfo_strings ();
24277 if (dwarf_split_debug_info)
24279 unsigned int index = 0;
24281 /* Add attributes common to skeleton compile_units and
24282 type_units. Because these attributes include strings, it
24283 must be done before freezing the string table. Top-level
24284 skeleton die attrs are added when the skeleton type unit is
24285 created, so ensure it is created by this point. */
24286 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24287 (void) get_skeleton_type_unit ();
24288 htab_traverse_noresize (debug_str_hash, index_string, &index);
24291 /* Output all of the compilation units. We put the main one last so that
24292 the offsets are available to output_pubnames. */
24293 for (node = limbo_die_list; node; node = node->next)
24294 output_comp_unit (node->die, 0);
24296 comdat_type_table.create (100);
24297 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24299 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24301 /* Don't output duplicate types. */
24302 if (*slot != HTAB_EMPTY_ENTRY)
24303 continue;
24305 /* Add a pointer to the line table for the main compilation unit
24306 so that the debugger can make sense of DW_AT_decl_file
24307 attributes. */
24308 if (debug_info_level >= DINFO_LEVEL_NORMAL)
24309 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24310 (!dwarf_split_debug_info
24311 ? debug_line_section_label
24312 : debug_skeleton_line_section_label));
24314 output_comdat_type_unit (ctnode);
24315 *slot = ctnode;
24317 comdat_type_table.dispose ();
24319 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24320 both the main_cu and all skeleton TUs. Making this call unconditional
24321 would end up either adding a second copy of the AT_pubnames attribute, or
24322 requiring a special case in add_top_level_skeleton_die_attrs. */
24323 if (!dwarf_split_debug_info)
24324 add_AT_pubnames (comp_unit_die ());
24326 if (dwarf_split_debug_info)
24328 int mark;
24329 unsigned char checksum[16];
24330 struct md5_ctx ctx;
24332 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24333 md5_init_ctx (&ctx);
24334 mark = 0;
24335 die_checksum (comp_unit_die (), &ctx, &mark);
24336 unmark_all_dies (comp_unit_die ());
24337 md5_finish_ctx (&ctx, checksum);
24339 /* Use the first 8 bytes of the checksum as the dwo_id,
24340 and add it to both comp-unit DIEs. */
24341 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24342 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24344 /* Add the base offset of the ranges table to the skeleton
24345 comp-unit DIE. */
24346 if (ranges_table_in_use)
24347 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24348 ranges_section_label);
24350 switch_to_section (debug_addr_section);
24351 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24352 output_addr_table ();
24355 /* Output the main compilation unit if non-empty or if .debug_macinfo
24356 or .debug_macro will be emitted. */
24357 output_comp_unit (comp_unit_die (), have_macinfo);
24359 if (dwarf_split_debug_info && info_section_emitted)
24360 output_skeleton_debug_sections (main_comp_unit_die);
24362 /* Output the abbreviation table. */
24363 if (abbrev_die_table_in_use != 1)
24365 switch_to_section (debug_abbrev_section);
24366 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24367 output_abbrev_section ();
24370 /* Output location list section if necessary. */
24371 if (have_location_lists)
24373 /* Output the location lists info. */
24374 switch_to_section (debug_loc_section);
24375 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24376 output_location_lists (comp_unit_die ());
24379 output_pubtables ();
24381 /* Output the address range information if a CU (.debug_info section)
24382 was emitted. We output an empty table even if we had no functions
24383 to put in it. This because the consumer has no way to tell the
24384 difference between an empty table that we omitted and failure to
24385 generate a table that would have contained data. */
24386 if (info_section_emitted)
24388 unsigned long aranges_length = size_of_aranges ();
24390 switch_to_section (debug_aranges_section);
24391 output_aranges (aranges_length);
24394 /* Output ranges section if necessary. */
24395 if (ranges_table_in_use)
24397 switch_to_section (debug_ranges_section);
24398 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24399 output_ranges ();
24402 /* Have to end the macro section. */
24403 if (have_macinfo)
24405 switch_to_section (debug_macinfo_section);
24406 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24407 output_macinfo ();
24408 dw2_asm_output_data (1, 0, "End compilation unit");
24411 /* Output the source line correspondence table. We must do this
24412 even if there is no line information. Otherwise, on an empty
24413 translation unit, we will generate a present, but empty,
24414 .debug_info section. IRIX 6.5 `nm' will then complain when
24415 examining the file. This is done late so that any filenames
24416 used by the debug_info section are marked as 'used'. */
24417 switch_to_section (debug_line_section);
24418 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24419 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24420 output_line_info (false);
24422 if (dwarf_split_debug_info && info_section_emitted)
24424 switch_to_section (debug_skeleton_line_section);
24425 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24426 output_line_info (true);
24429 /* If we emitted any indirect strings, output the string table too. */
24430 if (debug_str_hash || skeleton_debug_str_hash)
24431 output_indirect_strings ();
24434 #include "gt-dwarf2out.h"