* lib/ubsan-dg.exp (check_effective_target_fsanitize_undefined):
[official-gcc.git] / gcc / dwarf2out.c
blob601be8532b2384a022da4b8e9b01c64af404ad4b
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "hashtab.h"
68 #include "hash-set.h"
69 #include "vec.h"
70 #include "machmode.h"
71 #include "hard-reg-set.h"
72 #include "input.h"
73 #include "function.h"
74 #include "emit-rtl.h"
75 #include "hash-table.h"
76 #include "version.h"
77 #include "flags.h"
78 #include "regs.h"
79 #include "rtlhash.h"
80 #include "insn-config.h"
81 #include "reload.h"
82 #include "output.h"
83 #include "expr.h"
84 #include "except.h"
85 #include "dwarf2.h"
86 #include "dwarf2out.h"
87 #include "dwarf2asm.h"
88 #include "toplev.h"
89 #include "md5.h"
90 #include "tm_p.h"
91 #include "diagnostic.h"
92 #include "tree-pretty-print.h"
93 #include "debug.h"
94 #include "target.h"
95 #include "common/common-target.h"
96 #include "langhooks.h"
97 #include "hash-map.h"
98 #include "is-a.h"
99 #include "plugin-api.h"
100 #include "ipa-ref.h"
101 #include "cgraph.h"
102 #include "ira.h"
103 #include "lra.h"
104 #include "dumpfile.h"
105 #include "opts.h"
106 #include "tree-dfa.h"
107 #include "gdb/gdb-index.h"
108 #include "rtl-iter.h"
110 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
111 static rtx_insn *last_var_location_insn;
112 static rtx_insn *cached_next_real_insn;
113 static void dwarf2out_decl (tree);
115 #ifdef VMS_DEBUGGING_INFO
116 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
118 /* Define this macro to be a nonzero value if the directory specifications
119 which are output in the debug info should end with a separator. */
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
121 /* Define this macro to evaluate to a nonzero value if GCC should refrain
122 from generating indirect strings in DWARF2 debug information, for instance
123 if your target is stuck with an old version of GDB that is unable to
124 process them properly or uses VMS Debug. */
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
126 #else
127 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
128 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
129 #endif
131 /* ??? Poison these here until it can be done generically. They've been
132 totally replaced in this file; make sure it stays that way. */
133 #undef DWARF2_UNWIND_INFO
134 #undef DWARF2_FRAME_INFO
135 #if (GCC_VERSION >= 3000)
136 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
137 #endif
139 /* The size of the target's pointer type. */
140 #ifndef PTR_SIZE
141 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
142 #endif
144 /* Array of RTXes referenced by the debugging information, which therefore
145 must be kept around forever. */
146 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
148 /* A pointer to the base of a list of incomplete types which might be
149 completed at some later time. incomplete_types_list needs to be a
150 vec<tree, va_gc> *because we want to tell the garbage collector about
151 it. */
152 static GTY(()) vec<tree, va_gc> *incomplete_types;
154 /* A pointer to the base of a table of references to declaration
155 scopes. This table is a display which tracks the nesting
156 of declaration scopes at the current scope and containing
157 scopes. This table is used to find the proper place to
158 define type declaration DIE's. */
159 static GTY(()) vec<tree, va_gc> *decl_scope_table;
161 /* Pointers to various DWARF2 sections. */
162 static GTY(()) section *debug_info_section;
163 static GTY(()) section *debug_skeleton_info_section;
164 static GTY(()) section *debug_abbrev_section;
165 static GTY(()) section *debug_skeleton_abbrev_section;
166 static GTY(()) section *debug_aranges_section;
167 static GTY(()) section *debug_addr_section;
168 static GTY(()) section *debug_macinfo_section;
169 static GTY(()) section *debug_line_section;
170 static GTY(()) section *debug_skeleton_line_section;
171 static GTY(()) section *debug_loc_section;
172 static GTY(()) section *debug_pubnames_section;
173 static GTY(()) section *debug_pubtypes_section;
174 static GTY(()) section *debug_str_section;
175 static GTY(()) section *debug_str_dwo_section;
176 static GTY(()) section *debug_str_offsets_section;
177 static GTY(()) section *debug_ranges_section;
178 static GTY(()) section *debug_frame_section;
180 /* Maximum size (in bytes) of an artificially generated label. */
181 #define MAX_ARTIFICIAL_LABEL_BYTES 30
183 /* According to the (draft) DWARF 3 specification, the initial length
184 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
185 bytes are 0xffffffff, followed by the length stored in the next 8
186 bytes.
188 However, the SGI/MIPS ABI uses an initial length which is equal to
189 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
191 #ifndef DWARF_INITIAL_LENGTH_SIZE
192 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
193 #endif
195 /* Round SIZE up to the nearest BOUNDARY. */
196 #define DWARF_ROUND(SIZE,BOUNDARY) \
197 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
199 /* CIE identifier. */
200 #if HOST_BITS_PER_WIDE_INT >= 64
201 #define DWARF_CIE_ID \
202 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
203 #else
204 #define DWARF_CIE_ID DW_CIE_ID
205 #endif
208 /* A vector for a table that contains frame description
209 information for each routine. */
210 #define NOT_INDEXED (-1U)
211 #define NO_INDEX_ASSIGNED (-2U)
213 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
215 struct GTY((for_user)) indirect_string_node {
216 const char *str;
217 unsigned int refcount;
218 enum dwarf_form form;
219 char *label;
220 unsigned int index;
223 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
225 typedef const char *compare_type;
227 static hashval_t hash (indirect_string_node *);
228 static bool equal (indirect_string_node *, const char *);
231 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
233 /* With split_debug_info, both the comp_dir and dwo_name go in the
234 main object file, rather than the dwo, similar to the force_direct
235 parameter elsewhere but with additional complications:
237 1) The string is needed in both the main object file and the dwo.
238 That is, the comp_dir and dwo_name will appear in both places.
240 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
241 DW_FORM_GNU_str_index.
243 3) GCC chooses the form to use late, depending on the size and
244 reference count.
246 Rather than forcing the all debug string handling functions and
247 callers to deal with these complications, simply use a separate,
248 special-cased string table for any attribute that should go in the
249 main object file. This limits the complexity to just the places
250 that need it. */
252 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
254 static GTY(()) int dw2_string_counter;
256 /* True if the compilation unit places functions in more than one section. */
257 static GTY(()) bool have_multiple_function_sections = false;
259 /* Whether the default text and cold text sections have been used at all. */
261 static GTY(()) bool text_section_used = false;
262 static GTY(()) bool cold_text_section_used = false;
264 /* The default cold text section. */
265 static GTY(()) section *cold_text_section;
267 /* The DIE for C++14 'auto' in a function return type. */
268 static GTY(()) dw_die_ref auto_die;
270 /* The DIE for C++14 'decltype(auto)' in a function return type. */
271 static GTY(()) dw_die_ref decltype_auto_die;
273 /* Forward declarations for functions defined in this file. */
275 static char *stripattributes (const char *);
276 static void output_call_frame_info (int);
277 static void dwarf2out_note_section_used (void);
279 /* Personality decl of current unit. Used only when assembler does not support
280 personality CFI. */
281 static GTY(()) rtx current_unit_personality;
283 /* Data and reference forms for relocatable data. */
284 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
285 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
287 #ifndef DEBUG_FRAME_SECTION
288 #define DEBUG_FRAME_SECTION ".debug_frame"
289 #endif
291 #ifndef FUNC_BEGIN_LABEL
292 #define FUNC_BEGIN_LABEL "LFB"
293 #endif
295 #ifndef FUNC_END_LABEL
296 #define FUNC_END_LABEL "LFE"
297 #endif
299 #ifndef PROLOGUE_END_LABEL
300 #define PROLOGUE_END_LABEL "LPE"
301 #endif
303 #ifndef EPILOGUE_BEGIN_LABEL
304 #define EPILOGUE_BEGIN_LABEL "LEB"
305 #endif
307 #ifndef FRAME_BEGIN_LABEL
308 #define FRAME_BEGIN_LABEL "Lframe"
309 #endif
310 #define CIE_AFTER_SIZE_LABEL "LSCIE"
311 #define CIE_END_LABEL "LECIE"
312 #define FDE_LABEL "LSFDE"
313 #define FDE_AFTER_SIZE_LABEL "LASFDE"
314 #define FDE_END_LABEL "LEFDE"
315 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
316 #define LINE_NUMBER_END_LABEL "LELT"
317 #define LN_PROLOG_AS_LABEL "LASLTP"
318 #define LN_PROLOG_END_LABEL "LELTP"
319 #define DIE_LABEL_PREFIX "DW"
321 /* Match the base name of a file to the base name of a compilation unit. */
323 static int
324 matches_main_base (const char *path)
326 /* Cache the last query. */
327 static const char *last_path = NULL;
328 static int last_match = 0;
329 if (path != last_path)
331 const char *base;
332 int length = base_of_path (path, &base);
333 last_path = path;
334 last_match = (length == main_input_baselength
335 && memcmp (base, main_input_basename, length) == 0);
337 return last_match;
340 #ifdef DEBUG_DEBUG_STRUCT
342 static int
343 dump_struct_debug (tree type, enum debug_info_usage usage,
344 enum debug_struct_file criterion, int generic,
345 int matches, int result)
347 /* Find the type name. */
348 tree type_decl = TYPE_STUB_DECL (type);
349 tree t = type_decl;
350 const char *name = 0;
351 if (TREE_CODE (t) == TYPE_DECL)
352 t = DECL_NAME (t);
353 if (t)
354 name = IDENTIFIER_POINTER (t);
356 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
357 criterion,
358 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
359 matches ? "bas" : "hdr",
360 generic ? "gen" : "ord",
361 usage == DINFO_USAGE_DFN ? ";" :
362 usage == DINFO_USAGE_DIR_USE ? "." : "*",
363 result,
364 (void*) type_decl, name);
365 return result;
367 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
368 dump_struct_debug (type, usage, criterion, generic, matches, result)
370 #else
372 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
373 (result)
375 #endif
377 /* Get the number of HOST_WIDE_INTs needed to represent the precision
378 of the number. */
380 static unsigned int
381 get_full_len (const wide_int &op)
383 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
384 / HOST_BITS_PER_WIDE_INT);
387 static bool
388 should_emit_struct_debug (tree type, enum debug_info_usage usage)
390 enum debug_struct_file criterion;
391 tree type_decl;
392 bool generic = lang_hooks.types.generic_p (type);
394 if (generic)
395 criterion = debug_struct_generic[usage];
396 else
397 criterion = debug_struct_ordinary[usage];
399 if (criterion == DINFO_STRUCT_FILE_NONE)
400 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
401 if (criterion == DINFO_STRUCT_FILE_ANY)
402 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
404 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
406 if (type_decl != NULL)
408 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
409 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
411 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
412 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
415 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
418 /* Return a pointer to a copy of the section string name S with all
419 attributes stripped off, and an asterisk prepended (for assemble_name). */
421 static inline char *
422 stripattributes (const char *s)
424 char *stripped = XNEWVEC (char, strlen (s) + 2);
425 char *p = stripped;
427 *p++ = '*';
429 while (*s && *s != ',')
430 *p++ = *s++;
432 *p = '\0';
433 return stripped;
436 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
437 switch to the data section instead, and write out a synthetic start label
438 for collect2 the first time around. */
440 static void
441 switch_to_eh_frame_section (bool back)
443 tree label;
445 #ifdef EH_FRAME_SECTION_NAME
446 if (eh_frame_section == 0)
448 int flags;
450 if (EH_TABLES_CAN_BE_READ_ONLY)
452 int fde_encoding;
453 int per_encoding;
454 int lsda_encoding;
456 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
457 /*global=*/0);
458 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
459 /*global=*/1);
460 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
461 /*global=*/0);
462 flags = ((! flag_pic
463 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
464 && (fde_encoding & 0x70) != DW_EH_PE_aligned
465 && (per_encoding & 0x70) != DW_EH_PE_absptr
466 && (per_encoding & 0x70) != DW_EH_PE_aligned
467 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
468 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
469 ? 0 : SECTION_WRITE);
471 else
472 flags = SECTION_WRITE;
473 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
475 #endif /* EH_FRAME_SECTION_NAME */
477 if (eh_frame_section)
478 switch_to_section (eh_frame_section);
479 else
481 /* We have no special eh_frame section. Put the information in
482 the data section and emit special labels to guide collect2. */
483 switch_to_section (data_section);
485 if (!back)
487 label = get_file_function_name ("F");
488 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
489 targetm.asm_out.globalize_label (asm_out_file,
490 IDENTIFIER_POINTER (label));
491 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
496 /* Switch [BACK] to the eh or debug frame table section, depending on
497 FOR_EH. */
499 static void
500 switch_to_frame_table_section (int for_eh, bool back)
502 if (for_eh)
503 switch_to_eh_frame_section (back);
504 else
506 if (!debug_frame_section)
507 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
508 SECTION_DEBUG, NULL);
509 switch_to_section (debug_frame_section);
513 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
515 enum dw_cfi_oprnd_type
516 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
518 switch (cfi)
520 case DW_CFA_nop:
521 case DW_CFA_GNU_window_save:
522 case DW_CFA_remember_state:
523 case DW_CFA_restore_state:
524 return dw_cfi_oprnd_unused;
526 case DW_CFA_set_loc:
527 case DW_CFA_advance_loc1:
528 case DW_CFA_advance_loc2:
529 case DW_CFA_advance_loc4:
530 case DW_CFA_MIPS_advance_loc8:
531 return dw_cfi_oprnd_addr;
533 case DW_CFA_offset:
534 case DW_CFA_offset_extended:
535 case DW_CFA_def_cfa:
536 case DW_CFA_offset_extended_sf:
537 case DW_CFA_def_cfa_sf:
538 case DW_CFA_restore:
539 case DW_CFA_restore_extended:
540 case DW_CFA_undefined:
541 case DW_CFA_same_value:
542 case DW_CFA_def_cfa_register:
543 case DW_CFA_register:
544 case DW_CFA_expression:
545 return dw_cfi_oprnd_reg_num;
547 case DW_CFA_def_cfa_offset:
548 case DW_CFA_GNU_args_size:
549 case DW_CFA_def_cfa_offset_sf:
550 return dw_cfi_oprnd_offset;
552 case DW_CFA_def_cfa_expression:
553 return dw_cfi_oprnd_loc;
555 default:
556 gcc_unreachable ();
560 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
562 enum dw_cfi_oprnd_type
563 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
565 switch (cfi)
567 case DW_CFA_def_cfa:
568 case DW_CFA_def_cfa_sf:
569 case DW_CFA_offset:
570 case DW_CFA_offset_extended_sf:
571 case DW_CFA_offset_extended:
572 return dw_cfi_oprnd_offset;
574 case DW_CFA_register:
575 return dw_cfi_oprnd_reg_num;
577 case DW_CFA_expression:
578 return dw_cfi_oprnd_loc;
580 default:
581 return dw_cfi_oprnd_unused;
585 /* Output one FDE. */
587 static void
588 output_fde (dw_fde_ref fde, bool for_eh, bool second,
589 char *section_start_label, int fde_encoding, char *augmentation,
590 bool any_lsda_needed, int lsda_encoding)
592 const char *begin, *end;
593 static unsigned int j;
594 char l1[20], l2[20];
596 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
597 /* empty */ 0);
598 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
599 for_eh + j);
600 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
601 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
602 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
603 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
604 " indicating 64-bit DWARF extension");
605 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
606 "FDE Length");
607 ASM_OUTPUT_LABEL (asm_out_file, l1);
609 if (for_eh)
610 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
611 else
612 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
613 debug_frame_section, "FDE CIE offset");
615 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
616 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
618 if (for_eh)
620 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
621 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
622 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
623 "FDE initial location");
624 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
625 end, begin, "FDE address range");
627 else
629 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
630 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
633 if (augmentation[0])
635 if (any_lsda_needed)
637 int size = size_of_encoded_value (lsda_encoding);
639 if (lsda_encoding == DW_EH_PE_aligned)
641 int offset = ( 4 /* Length */
642 + 4 /* CIE offset */
643 + 2 * size_of_encoded_value (fde_encoding)
644 + 1 /* Augmentation size */ );
645 int pad = -offset & (PTR_SIZE - 1);
647 size += pad;
648 gcc_assert (size_of_uleb128 (size) == 1);
651 dw2_asm_output_data_uleb128 (size, "Augmentation size");
653 if (fde->uses_eh_lsda)
655 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
656 fde->funcdef_number);
657 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
658 gen_rtx_SYMBOL_REF (Pmode, l1),
659 false,
660 "Language Specific Data Area");
662 else
664 if (lsda_encoding == DW_EH_PE_aligned)
665 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
666 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
667 "Language Specific Data Area (none)");
670 else
671 dw2_asm_output_data_uleb128 (0, "Augmentation size");
674 /* Loop through the Call Frame Instructions associated with this FDE. */
675 fde->dw_fde_current_label = begin;
677 size_t from, until, i;
679 from = 0;
680 until = vec_safe_length (fde->dw_fde_cfi);
682 if (fde->dw_fde_second_begin == NULL)
684 else if (!second)
685 until = fde->dw_fde_switch_cfi_index;
686 else
687 from = fde->dw_fde_switch_cfi_index;
689 for (i = from; i < until; i++)
690 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
693 /* If we are to emit a ref/link from function bodies to their frame tables,
694 do it now. This is typically performed to make sure that tables
695 associated with functions are dragged with them and not discarded in
696 garbage collecting links. We need to do this on a per function basis to
697 cope with -ffunction-sections. */
699 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
700 /* Switch to the function section, emit the ref to the tables, and
701 switch *back* into the table section. */
702 switch_to_section (function_section (fde->decl));
703 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
704 switch_to_frame_table_section (for_eh, true);
705 #endif
707 /* Pad the FDE out to an address sized boundary. */
708 ASM_OUTPUT_ALIGN (asm_out_file,
709 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
710 ASM_OUTPUT_LABEL (asm_out_file, l2);
712 j += 2;
715 /* Return true if frame description entry FDE is needed for EH. */
717 static bool
718 fde_needed_for_eh_p (dw_fde_ref fde)
720 if (flag_asynchronous_unwind_tables)
721 return true;
723 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
724 return true;
726 if (fde->uses_eh_lsda)
727 return true;
729 /* If exceptions are enabled, we have collected nothrow info. */
730 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
731 return false;
733 return true;
736 /* Output the call frame information used to record information
737 that relates to calculating the frame pointer, and records the
738 location of saved registers. */
740 static void
741 output_call_frame_info (int for_eh)
743 unsigned int i;
744 dw_fde_ref fde;
745 dw_cfi_ref cfi;
746 char l1[20], l2[20], section_start_label[20];
747 bool any_lsda_needed = false;
748 char augmentation[6];
749 int augmentation_size;
750 int fde_encoding = DW_EH_PE_absptr;
751 int per_encoding = DW_EH_PE_absptr;
752 int lsda_encoding = DW_EH_PE_absptr;
753 int return_reg;
754 rtx personality = NULL;
755 int dw_cie_version;
757 /* Don't emit a CIE if there won't be any FDEs. */
758 if (!fde_vec)
759 return;
761 /* Nothing to do if the assembler's doing it all. */
762 if (dwarf2out_do_cfi_asm ())
763 return;
765 /* If we don't have any functions we'll want to unwind out of, don't emit
766 any EH unwind information. If we make FDEs linkonce, we may have to
767 emit an empty label for an FDE that wouldn't otherwise be emitted. We
768 want to avoid having an FDE kept around when the function it refers to
769 is discarded. Example where this matters: a primary function template
770 in C++ requires EH information, an explicit specialization doesn't. */
771 if (for_eh)
773 bool any_eh_needed = false;
775 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
777 if (fde->uses_eh_lsda)
778 any_eh_needed = any_lsda_needed = true;
779 else if (fde_needed_for_eh_p (fde))
780 any_eh_needed = true;
781 else if (TARGET_USES_WEAK_UNWIND_INFO)
782 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
785 if (!any_eh_needed)
786 return;
789 /* We're going to be generating comments, so turn on app. */
790 if (flag_debug_asm)
791 app_enable ();
793 /* Switch to the proper frame section, first time. */
794 switch_to_frame_table_section (for_eh, false);
796 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
797 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
799 /* Output the CIE. */
800 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
801 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
802 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
803 dw2_asm_output_data (4, 0xffffffff,
804 "Initial length escape value indicating 64-bit DWARF extension");
805 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
806 "Length of Common Information Entry");
807 ASM_OUTPUT_LABEL (asm_out_file, l1);
809 /* Now that the CIE pointer is PC-relative for EH,
810 use 0 to identify the CIE. */
811 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812 (for_eh ? 0 : DWARF_CIE_ID),
813 "CIE Identifier Tag");
815 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816 use CIE version 1, unless that would produce incorrect results
817 due to overflowing the return register column. */
818 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819 dw_cie_version = 1;
820 if (return_reg >= 256 || dwarf_version > 2)
821 dw_cie_version = 3;
822 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
824 augmentation[0] = 0;
825 augmentation_size = 0;
827 personality = current_unit_personality;
828 if (for_eh)
830 char *p;
832 /* Augmentation:
833 z Indicates that a uleb128 is present to size the
834 augmentation section.
835 L Indicates the encoding (and thus presence) of
836 an LSDA pointer in the FDE augmentation.
837 R Indicates a non-default pointer encoding for
838 FDE code pointers.
839 P Indicates the presence of an encoding + language
840 personality routine in the CIE augmentation. */
842 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
846 p = augmentation + 1;
847 if (personality)
849 *p++ = 'P';
850 augmentation_size += 1 + size_of_encoded_value (per_encoding);
851 assemble_external_libcall (personality);
853 if (any_lsda_needed)
855 *p++ = 'L';
856 augmentation_size += 1;
858 if (fde_encoding != DW_EH_PE_absptr)
860 *p++ = 'R';
861 augmentation_size += 1;
863 if (p > augmentation + 1)
865 augmentation[0] = 'z';
866 *p = '\0';
869 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
870 if (personality && per_encoding == DW_EH_PE_aligned)
872 int offset = ( 4 /* Length */
873 + 4 /* CIE Id */
874 + 1 /* CIE version */
875 + strlen (augmentation) + 1 /* Augmentation */
876 + size_of_uleb128 (1) /* Code alignment */
877 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878 + 1 /* RA column */
879 + 1 /* Augmentation size */
880 + 1 /* Personality encoding */ );
881 int pad = -offset & (PTR_SIZE - 1);
883 augmentation_size += pad;
885 /* Augmentations should be small, so there's scarce need to
886 iterate for a solution. Die if we exceed one uleb128 byte. */
887 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
891 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892 if (dw_cie_version >= 4)
894 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895 dw2_asm_output_data (1, 0, "CIE Segment Size");
897 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899 "CIE Data Alignment Factor");
901 if (dw_cie_version == 1)
902 dw2_asm_output_data (1, return_reg, "CIE RA Column");
903 else
904 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
906 if (augmentation[0])
908 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909 if (personality)
911 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912 eh_data_format_name (per_encoding));
913 dw2_asm_output_encoded_addr_rtx (per_encoding,
914 personality,
915 true, NULL);
918 if (any_lsda_needed)
919 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920 eh_data_format_name (lsda_encoding));
922 if (fde_encoding != DW_EH_PE_absptr)
923 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924 eh_data_format_name (fde_encoding));
927 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928 output_cfi (cfi, NULL, for_eh);
930 /* Pad the CIE out to an address sized boundary. */
931 ASM_OUTPUT_ALIGN (asm_out_file,
932 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933 ASM_OUTPUT_LABEL (asm_out_file, l2);
935 /* Loop through all of the FDE's. */
936 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
938 unsigned int k;
940 /* Don't emit EH unwind info for leaf functions that don't need it. */
941 if (for_eh && !fde_needed_for_eh_p (fde))
942 continue;
944 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946 augmentation, any_lsda_needed, lsda_encoding);
949 if (for_eh && targetm.terminate_dw2_eh_frame_info)
950 dw2_asm_output_data (4, 0, "End of Table");
952 /* Turn off app to make assembly quicker. */
953 if (flag_debug_asm)
954 app_disable ();
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
962 int enc;
963 rtx ref;
964 rtx personality = get_personality_function (current_function_decl);
966 fprintf (asm_out_file, "\t.cfi_startproc\n");
968 if (personality)
970 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971 ref = personality;
973 /* ??? The GAS support isn't entirely consistent. We have to
974 handle indirect support ourselves, but PC-relative is done
975 in the assembler. Further, the assembler can't handle any
976 of the weirder relocation types. */
977 if (enc & DW_EH_PE_indirect)
978 ref = dw2_force_const_mem (ref, true);
980 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981 output_addr_const (asm_out_file, ref);
982 fputc ('\n', asm_out_file);
985 if (crtl->uses_eh_lsda)
987 char lab[20];
989 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991 current_function_funcdef_no);
992 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
995 if (enc & DW_EH_PE_indirect)
996 ref = dw2_force_const_mem (ref, true);
998 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999 output_addr_const (asm_out_file, ref);
1000 fputc ('\n', asm_out_file);
1004 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1005 this allocation may be done before pass_final. */
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1010 dw_fde_ref fde;
1012 fde = ggc_cleared_alloc<dw_fde_node> ();
1013 fde->decl = current_function_decl;
1014 fde->funcdef_number = current_function_funcdef_no;
1015 fde->fde_index = vec_safe_length (fde_vec);
1016 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018 fde->nothrow = crtl->nothrow;
1019 fde->drap_reg = INVALID_REGNUM;
1020 fde->vdrap_reg = INVALID_REGNUM;
1022 /* Record the FDE associated with this function. */
1023 cfun->fde = fde;
1024 vec_safe_push (fde_vec, fde);
1026 return fde;
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030 the prologue. */
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034 const char *file ATTRIBUTE_UNUSED)
1036 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1037 char * dup_label;
1038 dw_fde_ref fde;
1039 section *fnsec;
1040 bool do_frame;
1042 current_function_func_begin_label = NULL;
1044 do_frame = dwarf2out_do_frame ();
1046 /* ??? current_function_func_begin_label is also used by except.c for
1047 call-site information. We must emit this label if it might be used. */
1048 if (!do_frame
1049 && (!flag_exceptions
1050 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1051 return;
1053 fnsec = function_section (current_function_decl);
1054 switch_to_section (fnsec);
1055 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1056 current_function_funcdef_no);
1057 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1058 current_function_funcdef_no);
1059 dup_label = xstrdup (label);
1060 current_function_func_begin_label = dup_label;
1062 /* We can elide the fde allocation if we're not emitting debug info. */
1063 if (!do_frame)
1064 return;
1066 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1067 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1068 would include pass_dwarf2_frame. If we've not created the FDE yet,
1069 do so now. */
1070 fde = cfun->fde;
1071 if (fde == NULL)
1072 fde = dwarf2out_alloc_current_fde ();
1074 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1075 fde->dw_fde_begin = dup_label;
1076 fde->dw_fde_current_label = dup_label;
1077 fde->in_std_section = (fnsec == text_section
1078 || (cold_text_section && fnsec == cold_text_section));
1080 /* We only want to output line number information for the genuine dwarf2
1081 prologue case, not the eh frame case. */
1082 #ifdef DWARF2_DEBUGGING_INFO
1083 if (file)
1084 dwarf2out_source_line (line, file, 0, true);
1085 #endif
1087 if (dwarf2out_do_cfi_asm ())
1088 dwarf2out_do_cfi_startproc (false);
1089 else
1091 rtx personality = get_personality_function (current_function_decl);
1092 if (!current_unit_personality)
1093 current_unit_personality = personality;
1095 /* We cannot keep a current personality per function as without CFI
1096 asm, at the point where we emit the CFI data, there is no current
1097 function anymore. */
1098 if (personality && current_unit_personality != personality)
1099 sorry ("multiple EH personalities are supported only with assemblers "
1100 "supporting .cfi_personality directive");
1104 /* Output a marker (i.e. a label) for the end of the generated code
1105 for a function prologue. This gets called *after* the prologue code has
1106 been generated. */
1108 void
1109 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1110 const char *file ATTRIBUTE_UNUSED)
1112 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1114 /* Output a label to mark the endpoint of the code generated for this
1115 function. */
1116 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1117 current_function_funcdef_no);
1118 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1119 current_function_funcdef_no);
1120 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1123 /* Output a marker (i.e. a label) for the beginning of the generated code
1124 for a function epilogue. This gets called *before* the prologue code has
1125 been generated. */
1127 void
1128 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1129 const char *file ATTRIBUTE_UNUSED)
1131 dw_fde_ref fde = cfun->fde;
1132 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1134 if (fde->dw_fde_vms_begin_epilogue)
1135 return;
1137 /* Output a label to mark the endpoint of the code generated for this
1138 function. */
1139 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1140 current_function_funcdef_no);
1141 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1142 current_function_funcdef_no);
1143 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1146 /* Output a marker (i.e. a label) for the absolute end of the generated code
1147 for a function definition. This gets called *after* the epilogue code has
1148 been generated. */
1150 void
1151 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1152 const char *file ATTRIBUTE_UNUSED)
1154 dw_fde_ref fde;
1155 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1157 last_var_location_insn = NULL;
1158 cached_next_real_insn = NULL;
1160 if (dwarf2out_do_cfi_asm ())
1161 fprintf (asm_out_file, "\t.cfi_endproc\n");
1163 /* Output a label to mark the endpoint of the code generated for this
1164 function. */
1165 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1166 current_function_funcdef_no);
1167 ASM_OUTPUT_LABEL (asm_out_file, label);
1168 fde = cfun->fde;
1169 gcc_assert (fde != NULL);
1170 if (fde->dw_fde_second_begin == NULL)
1171 fde->dw_fde_end = xstrdup (label);
1174 void
1175 dwarf2out_frame_finish (void)
1177 /* Output call frame information. */
1178 if (targetm.debug_unwind_info () == UI_DWARF2)
1179 output_call_frame_info (0);
1181 /* Output another copy for the unwinder. */
1182 if ((flag_unwind_tables || flag_exceptions)
1183 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1184 output_call_frame_info (1);
1187 /* Note that the current function section is being used for code. */
1189 static void
1190 dwarf2out_note_section_used (void)
1192 section *sec = current_function_section ();
1193 if (sec == text_section)
1194 text_section_used = true;
1195 else if (sec == cold_text_section)
1196 cold_text_section_used = true;
1199 static void var_location_switch_text_section (void);
1200 static void set_cur_line_info_table (section *);
1202 void
1203 dwarf2out_switch_text_section (void)
1205 section *sect;
1206 dw_fde_ref fde = cfun->fde;
1208 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1210 if (!in_cold_section_p)
1212 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1216 else
1218 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1219 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1220 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1222 have_multiple_function_sections = true;
1224 /* There is no need to mark used sections when not debugging. */
1225 if (cold_text_section != NULL)
1226 dwarf2out_note_section_used ();
1228 if (dwarf2out_do_cfi_asm ())
1229 fprintf (asm_out_file, "\t.cfi_endproc\n");
1231 /* Now do the real section switch. */
1232 sect = current_function_section ();
1233 switch_to_section (sect);
1235 fde->second_in_std_section
1236 = (sect == text_section
1237 || (cold_text_section && sect == cold_text_section));
1239 if (dwarf2out_do_cfi_asm ())
1240 dwarf2out_do_cfi_startproc (true);
1242 var_location_switch_text_section ();
1244 if (cold_text_section != NULL)
1245 set_cur_line_info_table (sect);
1248 /* And now, the subset of the debugging information support code necessary
1249 for emitting location expressions. */
1251 /* Data about a single source file. */
1252 struct GTY((for_user)) dwarf_file_data {
1253 const char * filename;
1254 int emitted_number;
1257 typedef struct GTY(()) deferred_locations_struct
1259 tree variable;
1260 dw_die_ref die;
1261 } deferred_locations;
1264 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1267 /* Describe an entry into the .debug_addr section. */
1269 enum ate_kind {
1270 ate_kind_rtx,
1271 ate_kind_rtx_dtprel,
1272 ate_kind_label
1275 typedef struct GTY((for_user)) addr_table_entry_struct {
1276 enum ate_kind kind;
1277 unsigned int refcount;
1278 unsigned int index;
1279 union addr_table_entry_struct_union
1281 rtx GTY ((tag ("0"))) rtl;
1282 char * GTY ((tag ("1"))) label;
1284 GTY ((desc ("%1.kind"))) addr;
1286 addr_table_entry;
1288 /* Location lists are ranges + location descriptions for that range,
1289 so you can track variables that are in different places over
1290 their entire life. */
1291 typedef struct GTY(()) dw_loc_list_struct {
1292 dw_loc_list_ref dw_loc_next;
1293 const char *begin; /* Label and addr_entry for start of range */
1294 addr_table_entry *begin_entry;
1295 const char *end; /* Label for end of range */
1296 char *ll_symbol; /* Label for beginning of location list.
1297 Only on head of list */
1298 const char *section; /* Section this loclist is relative to */
1299 dw_loc_descr_ref expr;
1300 hashval_t hash;
1301 /* True if all addresses in this and subsequent lists are known to be
1302 resolved. */
1303 bool resolved_addr;
1304 /* True if this list has been replaced by dw_loc_next. */
1305 bool replaced;
1306 bool emitted;
1307 /* True if the range should be emitted even if begin and end
1308 are the same. */
1309 bool force;
1310 } dw_loc_list_node;
1312 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1314 /* Convert a DWARF stack opcode into its string name. */
1316 static const char *
1317 dwarf_stack_op_name (unsigned int op)
1319 const char *name = get_DW_OP_name (op);
1321 if (name != NULL)
1322 return name;
1324 return "OP_<unknown>";
1327 /* Return a pointer to a newly allocated location description. Location
1328 descriptions are simple expression terms that can be strung
1329 together to form more complicated location (address) descriptions. */
1331 static inline dw_loc_descr_ref
1332 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1333 unsigned HOST_WIDE_INT oprnd2)
1335 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1337 descr->dw_loc_opc = op;
1338 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1339 descr->dw_loc_oprnd1.val_entry = NULL;
1340 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1341 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1342 descr->dw_loc_oprnd2.val_entry = NULL;
1343 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1345 return descr;
1348 /* Return a pointer to a newly allocated location description for
1349 REG and OFFSET. */
1351 static inline dw_loc_descr_ref
1352 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1354 if (reg <= 31)
1355 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1356 offset, 0);
1357 else
1358 return new_loc_descr (DW_OP_bregx, reg, offset);
1361 /* Add a location description term to a location description expression. */
1363 static inline void
1364 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1366 dw_loc_descr_ref *d;
1368 /* Find the end of the chain. */
1369 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1372 *d = descr;
1375 /* Compare two location operands for exact equality. */
1377 static bool
1378 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1380 if (a->val_class != b->val_class)
1381 return false;
1382 switch (a->val_class)
1384 case dw_val_class_none:
1385 return true;
1386 case dw_val_class_addr:
1387 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1389 case dw_val_class_offset:
1390 case dw_val_class_unsigned_const:
1391 case dw_val_class_const:
1392 case dw_val_class_range_list:
1393 case dw_val_class_lineptr:
1394 case dw_val_class_macptr:
1395 /* These are all HOST_WIDE_INT, signed or unsigned. */
1396 return a->v.val_unsigned == b->v.val_unsigned;
1398 case dw_val_class_loc:
1399 return a->v.val_loc == b->v.val_loc;
1400 case dw_val_class_loc_list:
1401 return a->v.val_loc_list == b->v.val_loc_list;
1402 case dw_val_class_die_ref:
1403 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1404 case dw_val_class_fde_ref:
1405 return a->v.val_fde_index == b->v.val_fde_index;
1406 case dw_val_class_lbl_id:
1407 case dw_val_class_high_pc:
1408 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1409 case dw_val_class_str:
1410 return a->v.val_str == b->v.val_str;
1411 case dw_val_class_flag:
1412 return a->v.val_flag == b->v.val_flag;
1413 case dw_val_class_file:
1414 return a->v.val_file == b->v.val_file;
1415 case dw_val_class_decl_ref:
1416 return a->v.val_decl_ref == b->v.val_decl_ref;
1418 case dw_val_class_const_double:
1419 return (a->v.val_double.high == b->v.val_double.high
1420 && a->v.val_double.low == b->v.val_double.low);
1422 case dw_val_class_wide_int:
1423 return *a->v.val_wide == *b->v.val_wide;
1425 case dw_val_class_vec:
1427 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1428 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1430 return (a_len == b_len
1431 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1434 case dw_val_class_data8:
1435 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1437 case dw_val_class_vms_delta:
1438 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1439 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1441 gcc_unreachable ();
1444 /* Compare two location atoms for exact equality. */
1446 static bool
1447 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1449 if (a->dw_loc_opc != b->dw_loc_opc)
1450 return false;
1452 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1453 address size, but since we always allocate cleared storage it
1454 should be zero for other types of locations. */
1455 if (a->dtprel != b->dtprel)
1456 return false;
1458 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1459 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1462 /* Compare two complete location expressions for exact equality. */
1464 bool
1465 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1467 while (1)
1469 if (a == b)
1470 return true;
1471 if (a == NULL || b == NULL)
1472 return false;
1473 if (!loc_descr_equal_p_1 (a, b))
1474 return false;
1476 a = a->dw_loc_next;
1477 b = b->dw_loc_next;
1482 /* Add a constant OFFSET to a location expression. */
1484 static void
1485 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1487 dw_loc_descr_ref loc;
1488 HOST_WIDE_INT *p;
1490 gcc_assert (*list_head != NULL);
1492 if (!offset)
1493 return;
1495 /* Find the end of the chain. */
1496 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1499 p = NULL;
1500 if (loc->dw_loc_opc == DW_OP_fbreg
1501 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1502 p = &loc->dw_loc_oprnd1.v.val_int;
1503 else if (loc->dw_loc_opc == DW_OP_bregx)
1504 p = &loc->dw_loc_oprnd2.v.val_int;
1506 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1507 offset. Don't optimize if an signed integer overflow would happen. */
1508 if (p != NULL
1509 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1510 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1511 *p += offset;
1513 else if (offset > 0)
1514 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1516 else
1518 loc->dw_loc_next = int_loc_descriptor (-offset);
1519 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1523 /* Add a constant OFFSET to a location list. */
1525 static void
1526 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1528 dw_loc_list_ref d;
1529 for (d = list_head; d != NULL; d = d->dw_loc_next)
1530 loc_descr_plus_const (&d->expr, offset);
1533 #define DWARF_REF_SIZE \
1534 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1536 static unsigned long int get_base_type_offset (dw_die_ref);
1538 /* Return the size of a location descriptor. */
1540 static unsigned long
1541 size_of_loc_descr (dw_loc_descr_ref loc)
1543 unsigned long size = 1;
1545 switch (loc->dw_loc_opc)
1547 case DW_OP_addr:
1548 size += DWARF2_ADDR_SIZE;
1549 break;
1550 case DW_OP_GNU_addr_index:
1551 case DW_OP_GNU_const_index:
1552 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1553 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1554 break;
1555 case DW_OP_const1u:
1556 case DW_OP_const1s:
1557 size += 1;
1558 break;
1559 case DW_OP_const2u:
1560 case DW_OP_const2s:
1561 size += 2;
1562 break;
1563 case DW_OP_const4u:
1564 case DW_OP_const4s:
1565 size += 4;
1566 break;
1567 case DW_OP_const8u:
1568 case DW_OP_const8s:
1569 size += 8;
1570 break;
1571 case DW_OP_constu:
1572 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1573 break;
1574 case DW_OP_consts:
1575 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1576 break;
1577 case DW_OP_pick:
1578 size += 1;
1579 break;
1580 case DW_OP_plus_uconst:
1581 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1582 break;
1583 case DW_OP_skip:
1584 case DW_OP_bra:
1585 size += 2;
1586 break;
1587 case DW_OP_breg0:
1588 case DW_OP_breg1:
1589 case DW_OP_breg2:
1590 case DW_OP_breg3:
1591 case DW_OP_breg4:
1592 case DW_OP_breg5:
1593 case DW_OP_breg6:
1594 case DW_OP_breg7:
1595 case DW_OP_breg8:
1596 case DW_OP_breg9:
1597 case DW_OP_breg10:
1598 case DW_OP_breg11:
1599 case DW_OP_breg12:
1600 case DW_OP_breg13:
1601 case DW_OP_breg14:
1602 case DW_OP_breg15:
1603 case DW_OP_breg16:
1604 case DW_OP_breg17:
1605 case DW_OP_breg18:
1606 case DW_OP_breg19:
1607 case DW_OP_breg20:
1608 case DW_OP_breg21:
1609 case DW_OP_breg22:
1610 case DW_OP_breg23:
1611 case DW_OP_breg24:
1612 case DW_OP_breg25:
1613 case DW_OP_breg26:
1614 case DW_OP_breg27:
1615 case DW_OP_breg28:
1616 case DW_OP_breg29:
1617 case DW_OP_breg30:
1618 case DW_OP_breg31:
1619 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1620 break;
1621 case DW_OP_regx:
1622 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1623 break;
1624 case DW_OP_fbreg:
1625 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1626 break;
1627 case DW_OP_bregx:
1628 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1629 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1630 break;
1631 case DW_OP_piece:
1632 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1633 break;
1634 case DW_OP_bit_piece:
1635 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1636 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1637 break;
1638 case DW_OP_deref_size:
1639 case DW_OP_xderef_size:
1640 size += 1;
1641 break;
1642 case DW_OP_call2:
1643 size += 2;
1644 break;
1645 case DW_OP_call4:
1646 size += 4;
1647 break;
1648 case DW_OP_call_ref:
1649 size += DWARF_REF_SIZE;
1650 break;
1651 case DW_OP_implicit_value:
1652 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1653 + loc->dw_loc_oprnd1.v.val_unsigned;
1654 break;
1655 case DW_OP_GNU_implicit_pointer:
1656 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1657 break;
1658 case DW_OP_GNU_entry_value:
1660 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1661 size += size_of_uleb128 (op_size) + op_size;
1662 break;
1664 case DW_OP_GNU_const_type:
1666 unsigned long o
1667 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1668 size += size_of_uleb128 (o) + 1;
1669 switch (loc->dw_loc_oprnd2.val_class)
1671 case dw_val_class_vec:
1672 size += loc->dw_loc_oprnd2.v.val_vec.length
1673 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1674 break;
1675 case dw_val_class_const:
1676 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1677 break;
1678 case dw_val_class_const_double:
1679 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1680 break;
1681 case dw_val_class_wide_int:
1682 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1683 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1684 break;
1685 default:
1686 gcc_unreachable ();
1688 break;
1690 case DW_OP_GNU_regval_type:
1692 unsigned long o
1693 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1694 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1695 + size_of_uleb128 (o);
1697 break;
1698 case DW_OP_GNU_deref_type:
1700 unsigned long o
1701 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1702 size += 1 + size_of_uleb128 (o);
1704 break;
1705 case DW_OP_GNU_convert:
1706 case DW_OP_GNU_reinterpret:
1707 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1708 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1709 else
1711 unsigned long o
1712 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1713 size += size_of_uleb128 (o);
1715 break;
1716 case DW_OP_GNU_parameter_ref:
1717 size += 4;
1718 break;
1719 default:
1720 break;
1723 return size;
1726 /* Return the size of a series of location descriptors. */
1728 unsigned long
1729 size_of_locs (dw_loc_descr_ref loc)
1731 dw_loc_descr_ref l;
1732 unsigned long size;
1734 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1735 field, to avoid writing to a PCH file. */
1736 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1738 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1739 break;
1740 size += size_of_loc_descr (l);
1742 if (! l)
1743 return size;
1745 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1747 l->dw_loc_addr = size;
1748 size += size_of_loc_descr (l);
1751 return size;
1754 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1755 static void get_ref_die_offset_label (char *, dw_die_ref);
1756 static unsigned long int get_ref_die_offset (dw_die_ref);
1758 /* Output location description stack opcode's operands (if any).
1759 The for_eh_or_skip parameter controls whether register numbers are
1760 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1761 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1762 info). This should be suppressed for the cases that have not been converted
1763 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1765 static void
1766 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1768 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1769 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1771 switch (loc->dw_loc_opc)
1773 #ifdef DWARF2_DEBUGGING_INFO
1774 case DW_OP_const2u:
1775 case DW_OP_const2s:
1776 dw2_asm_output_data (2, val1->v.val_int, NULL);
1777 break;
1778 case DW_OP_const4u:
1779 if (loc->dtprel)
1781 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1782 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1783 val1->v.val_addr);
1784 fputc ('\n', asm_out_file);
1785 break;
1787 /* FALLTHRU */
1788 case DW_OP_const4s:
1789 dw2_asm_output_data (4, val1->v.val_int, NULL);
1790 break;
1791 case DW_OP_const8u:
1792 if (loc->dtprel)
1794 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1795 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1796 val1->v.val_addr);
1797 fputc ('\n', asm_out_file);
1798 break;
1800 /* FALLTHRU */
1801 case DW_OP_const8s:
1802 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1803 dw2_asm_output_data (8, val1->v.val_int, NULL);
1804 break;
1805 case DW_OP_skip:
1806 case DW_OP_bra:
1808 int offset;
1810 gcc_assert (val1->val_class == dw_val_class_loc);
1811 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1813 dw2_asm_output_data (2, offset, NULL);
1815 break;
1816 case DW_OP_implicit_value:
1817 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1818 switch (val2->val_class)
1820 case dw_val_class_const:
1821 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1822 break;
1823 case dw_val_class_vec:
1825 unsigned int elt_size = val2->v.val_vec.elt_size;
1826 unsigned int len = val2->v.val_vec.length;
1827 unsigned int i;
1828 unsigned char *p;
1830 if (elt_size > sizeof (HOST_WIDE_INT))
1832 elt_size /= 2;
1833 len *= 2;
1835 for (i = 0, p = val2->v.val_vec.array;
1836 i < len;
1837 i++, p += elt_size)
1838 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1839 "fp or vector constant word %u", i);
1841 break;
1842 case dw_val_class_const_double:
1844 unsigned HOST_WIDE_INT first, second;
1846 if (WORDS_BIG_ENDIAN)
1848 first = val2->v.val_double.high;
1849 second = val2->v.val_double.low;
1851 else
1853 first = val2->v.val_double.low;
1854 second = val2->v.val_double.high;
1856 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1857 first, NULL);
1858 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1859 second, NULL);
1861 break;
1862 case dw_val_class_wide_int:
1864 int i;
1865 int len = get_full_len (*val2->v.val_wide);
1866 if (WORDS_BIG_ENDIAN)
1867 for (i = len - 1; i >= 0; --i)
1868 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1869 val2->v.val_wide->elt (i), NULL);
1870 else
1871 for (i = 0; i < len; ++i)
1872 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1873 val2->v.val_wide->elt (i), NULL);
1875 break;
1876 case dw_val_class_addr:
1877 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1878 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1879 break;
1880 default:
1881 gcc_unreachable ();
1883 break;
1884 #else
1885 case DW_OP_const2u:
1886 case DW_OP_const2s:
1887 case DW_OP_const4u:
1888 case DW_OP_const4s:
1889 case DW_OP_const8u:
1890 case DW_OP_const8s:
1891 case DW_OP_skip:
1892 case DW_OP_bra:
1893 case DW_OP_implicit_value:
1894 /* We currently don't make any attempt to make sure these are
1895 aligned properly like we do for the main unwind info, so
1896 don't support emitting things larger than a byte if we're
1897 only doing unwinding. */
1898 gcc_unreachable ();
1899 #endif
1900 case DW_OP_const1u:
1901 case DW_OP_const1s:
1902 dw2_asm_output_data (1, val1->v.val_int, NULL);
1903 break;
1904 case DW_OP_constu:
1905 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1906 break;
1907 case DW_OP_consts:
1908 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1909 break;
1910 case DW_OP_pick:
1911 dw2_asm_output_data (1, val1->v.val_int, NULL);
1912 break;
1913 case DW_OP_plus_uconst:
1914 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1915 break;
1916 case DW_OP_breg0:
1917 case DW_OP_breg1:
1918 case DW_OP_breg2:
1919 case DW_OP_breg3:
1920 case DW_OP_breg4:
1921 case DW_OP_breg5:
1922 case DW_OP_breg6:
1923 case DW_OP_breg7:
1924 case DW_OP_breg8:
1925 case DW_OP_breg9:
1926 case DW_OP_breg10:
1927 case DW_OP_breg11:
1928 case DW_OP_breg12:
1929 case DW_OP_breg13:
1930 case DW_OP_breg14:
1931 case DW_OP_breg15:
1932 case DW_OP_breg16:
1933 case DW_OP_breg17:
1934 case DW_OP_breg18:
1935 case DW_OP_breg19:
1936 case DW_OP_breg20:
1937 case DW_OP_breg21:
1938 case DW_OP_breg22:
1939 case DW_OP_breg23:
1940 case DW_OP_breg24:
1941 case DW_OP_breg25:
1942 case DW_OP_breg26:
1943 case DW_OP_breg27:
1944 case DW_OP_breg28:
1945 case DW_OP_breg29:
1946 case DW_OP_breg30:
1947 case DW_OP_breg31:
1948 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1949 break;
1950 case DW_OP_regx:
1952 unsigned r = val1->v.val_unsigned;
1953 if (for_eh_or_skip >= 0)
1954 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1955 gcc_assert (size_of_uleb128 (r)
1956 == size_of_uleb128 (val1->v.val_unsigned));
1957 dw2_asm_output_data_uleb128 (r, NULL);
1959 break;
1960 case DW_OP_fbreg:
1961 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1962 break;
1963 case DW_OP_bregx:
1965 unsigned r = val1->v.val_unsigned;
1966 if (for_eh_or_skip >= 0)
1967 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1968 gcc_assert (size_of_uleb128 (r)
1969 == size_of_uleb128 (val1->v.val_unsigned));
1970 dw2_asm_output_data_uleb128 (r, NULL);
1971 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1973 break;
1974 case DW_OP_piece:
1975 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1976 break;
1977 case DW_OP_bit_piece:
1978 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1979 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1980 break;
1981 case DW_OP_deref_size:
1982 case DW_OP_xderef_size:
1983 dw2_asm_output_data (1, val1->v.val_int, NULL);
1984 break;
1986 case DW_OP_addr:
1987 if (loc->dtprel)
1989 if (targetm.asm_out.output_dwarf_dtprel)
1991 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1992 DWARF2_ADDR_SIZE,
1993 val1->v.val_addr);
1994 fputc ('\n', asm_out_file);
1996 else
1997 gcc_unreachable ();
1999 else
2001 #ifdef DWARF2_DEBUGGING_INFO
2002 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2003 #else
2004 gcc_unreachable ();
2005 #endif
2007 break;
2009 case DW_OP_GNU_addr_index:
2010 case DW_OP_GNU_const_index:
2011 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2012 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2013 "(index into .debug_addr)");
2014 break;
2016 case DW_OP_GNU_implicit_pointer:
2018 char label[MAX_ARTIFICIAL_LABEL_BYTES
2019 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2020 gcc_assert (val1->val_class == dw_val_class_die_ref);
2021 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2022 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2023 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2025 break;
2027 case DW_OP_GNU_entry_value:
2028 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2029 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2030 break;
2032 case DW_OP_GNU_const_type:
2034 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2035 gcc_assert (o);
2036 dw2_asm_output_data_uleb128 (o, NULL);
2037 switch (val2->val_class)
2039 case dw_val_class_const:
2040 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2041 dw2_asm_output_data (1, l, NULL);
2042 dw2_asm_output_data (l, val2->v.val_int, NULL);
2043 break;
2044 case dw_val_class_vec:
2046 unsigned int elt_size = val2->v.val_vec.elt_size;
2047 unsigned int len = val2->v.val_vec.length;
2048 unsigned int i;
2049 unsigned char *p;
2051 l = len * elt_size;
2052 dw2_asm_output_data (1, l, NULL);
2053 if (elt_size > sizeof (HOST_WIDE_INT))
2055 elt_size /= 2;
2056 len *= 2;
2058 for (i = 0, p = val2->v.val_vec.array;
2059 i < len;
2060 i++, p += elt_size)
2061 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2062 "fp or vector constant word %u", i);
2064 break;
2065 case dw_val_class_const_double:
2067 unsigned HOST_WIDE_INT first, second;
2068 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2070 dw2_asm_output_data (1, 2 * l, NULL);
2071 if (WORDS_BIG_ENDIAN)
2073 first = val2->v.val_double.high;
2074 second = val2->v.val_double.low;
2076 else
2078 first = val2->v.val_double.low;
2079 second = val2->v.val_double.high;
2081 dw2_asm_output_data (l, first, NULL);
2082 dw2_asm_output_data (l, second, NULL);
2084 break;
2085 case dw_val_class_wide_int:
2087 int i;
2088 int len = get_full_len (*val2->v.val_wide);
2089 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2091 dw2_asm_output_data (1, len * l, NULL);
2092 if (WORDS_BIG_ENDIAN)
2093 for (i = len - 1; i >= 0; --i)
2094 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2095 else
2096 for (i = 0; i < len; ++i)
2097 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2099 break;
2100 default:
2101 gcc_unreachable ();
2104 break;
2105 case DW_OP_GNU_regval_type:
2107 unsigned r = val1->v.val_unsigned;
2108 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2109 gcc_assert (o);
2110 if (for_eh_or_skip >= 0)
2112 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2113 gcc_assert (size_of_uleb128 (r)
2114 == size_of_uleb128 (val1->v.val_unsigned));
2116 dw2_asm_output_data_uleb128 (r, NULL);
2117 dw2_asm_output_data_uleb128 (o, NULL);
2119 break;
2120 case DW_OP_GNU_deref_type:
2122 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2123 gcc_assert (o);
2124 dw2_asm_output_data (1, val1->v.val_int, NULL);
2125 dw2_asm_output_data_uleb128 (o, NULL);
2127 break;
2128 case DW_OP_GNU_convert:
2129 case DW_OP_GNU_reinterpret:
2130 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2131 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2132 else
2134 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2135 gcc_assert (o);
2136 dw2_asm_output_data_uleb128 (o, NULL);
2138 break;
2140 case DW_OP_GNU_parameter_ref:
2142 unsigned long o;
2143 gcc_assert (val1->val_class == dw_val_class_die_ref);
2144 o = get_ref_die_offset (val1->v.val_die_ref.die);
2145 dw2_asm_output_data (4, o, NULL);
2147 break;
2149 default:
2150 /* Other codes have no operands. */
2151 break;
2155 /* Output a sequence of location operations.
2156 The for_eh_or_skip parameter controls whether register numbers are
2157 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2158 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2159 info). This should be suppressed for the cases that have not been converted
2160 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2162 void
2163 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2165 for (; loc != NULL; loc = loc->dw_loc_next)
2167 enum dwarf_location_atom opc = loc->dw_loc_opc;
2168 /* Output the opcode. */
2169 if (for_eh_or_skip >= 0
2170 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2172 unsigned r = (opc - DW_OP_breg0);
2173 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2174 gcc_assert (r <= 31);
2175 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2177 else if (for_eh_or_skip >= 0
2178 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2180 unsigned r = (opc - DW_OP_reg0);
2181 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2182 gcc_assert (r <= 31);
2183 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2186 dw2_asm_output_data (1, opc,
2187 "%s", dwarf_stack_op_name (opc));
2189 /* Output the operand(s) (if any). */
2190 output_loc_operands (loc, for_eh_or_skip);
2194 /* Output location description stack opcode's operands (if any).
2195 The output is single bytes on a line, suitable for .cfi_escape. */
2197 static void
2198 output_loc_operands_raw (dw_loc_descr_ref loc)
2200 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2201 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2203 switch (loc->dw_loc_opc)
2205 case DW_OP_addr:
2206 case DW_OP_GNU_addr_index:
2207 case DW_OP_GNU_const_index:
2208 case DW_OP_implicit_value:
2209 /* We cannot output addresses in .cfi_escape, only bytes. */
2210 gcc_unreachable ();
2212 case DW_OP_const1u:
2213 case DW_OP_const1s:
2214 case DW_OP_pick:
2215 case DW_OP_deref_size:
2216 case DW_OP_xderef_size:
2217 fputc (',', asm_out_file);
2218 dw2_asm_output_data_raw (1, val1->v.val_int);
2219 break;
2221 case DW_OP_const2u:
2222 case DW_OP_const2s:
2223 fputc (',', asm_out_file);
2224 dw2_asm_output_data_raw (2, val1->v.val_int);
2225 break;
2227 case DW_OP_const4u:
2228 case DW_OP_const4s:
2229 fputc (',', asm_out_file);
2230 dw2_asm_output_data_raw (4, val1->v.val_int);
2231 break;
2233 case DW_OP_const8u:
2234 case DW_OP_const8s:
2235 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2236 fputc (',', asm_out_file);
2237 dw2_asm_output_data_raw (8, val1->v.val_int);
2238 break;
2240 case DW_OP_skip:
2241 case DW_OP_bra:
2243 int offset;
2245 gcc_assert (val1->val_class == dw_val_class_loc);
2246 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2248 fputc (',', asm_out_file);
2249 dw2_asm_output_data_raw (2, offset);
2251 break;
2253 case DW_OP_regx:
2255 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2256 gcc_assert (size_of_uleb128 (r)
2257 == size_of_uleb128 (val1->v.val_unsigned));
2258 fputc (',', asm_out_file);
2259 dw2_asm_output_data_uleb128_raw (r);
2261 break;
2263 case DW_OP_constu:
2264 case DW_OP_plus_uconst:
2265 case DW_OP_piece:
2266 fputc (',', asm_out_file);
2267 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2268 break;
2270 case DW_OP_bit_piece:
2271 fputc (',', asm_out_file);
2272 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2273 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2274 break;
2276 case DW_OP_consts:
2277 case DW_OP_breg0:
2278 case DW_OP_breg1:
2279 case DW_OP_breg2:
2280 case DW_OP_breg3:
2281 case DW_OP_breg4:
2282 case DW_OP_breg5:
2283 case DW_OP_breg6:
2284 case DW_OP_breg7:
2285 case DW_OP_breg8:
2286 case DW_OP_breg9:
2287 case DW_OP_breg10:
2288 case DW_OP_breg11:
2289 case DW_OP_breg12:
2290 case DW_OP_breg13:
2291 case DW_OP_breg14:
2292 case DW_OP_breg15:
2293 case DW_OP_breg16:
2294 case DW_OP_breg17:
2295 case DW_OP_breg18:
2296 case DW_OP_breg19:
2297 case DW_OP_breg20:
2298 case DW_OP_breg21:
2299 case DW_OP_breg22:
2300 case DW_OP_breg23:
2301 case DW_OP_breg24:
2302 case DW_OP_breg25:
2303 case DW_OP_breg26:
2304 case DW_OP_breg27:
2305 case DW_OP_breg28:
2306 case DW_OP_breg29:
2307 case DW_OP_breg30:
2308 case DW_OP_breg31:
2309 case DW_OP_fbreg:
2310 fputc (',', asm_out_file);
2311 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2312 break;
2314 case DW_OP_bregx:
2316 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2317 gcc_assert (size_of_uleb128 (r)
2318 == size_of_uleb128 (val1->v.val_unsigned));
2319 fputc (',', asm_out_file);
2320 dw2_asm_output_data_uleb128_raw (r);
2321 fputc (',', asm_out_file);
2322 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2324 break;
2326 case DW_OP_GNU_implicit_pointer:
2327 case DW_OP_GNU_entry_value:
2328 case DW_OP_GNU_const_type:
2329 case DW_OP_GNU_regval_type:
2330 case DW_OP_GNU_deref_type:
2331 case DW_OP_GNU_convert:
2332 case DW_OP_GNU_reinterpret:
2333 case DW_OP_GNU_parameter_ref:
2334 gcc_unreachable ();
2335 break;
2337 default:
2338 /* Other codes have no operands. */
2339 break;
2343 void
2344 output_loc_sequence_raw (dw_loc_descr_ref loc)
2346 while (1)
2348 enum dwarf_location_atom opc = loc->dw_loc_opc;
2349 /* Output the opcode. */
2350 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2352 unsigned r = (opc - DW_OP_breg0);
2353 r = DWARF2_FRAME_REG_OUT (r, 1);
2354 gcc_assert (r <= 31);
2355 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2357 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2359 unsigned r = (opc - DW_OP_reg0);
2360 r = DWARF2_FRAME_REG_OUT (r, 1);
2361 gcc_assert (r <= 31);
2362 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2364 /* Output the opcode. */
2365 fprintf (asm_out_file, "%#x", opc);
2366 output_loc_operands_raw (loc);
2368 if (!loc->dw_loc_next)
2369 break;
2370 loc = loc->dw_loc_next;
2372 fputc (',', asm_out_file);
2376 /* This function builds a dwarf location descriptor sequence from a
2377 dw_cfa_location, adding the given OFFSET to the result of the
2378 expression. */
2380 struct dw_loc_descr_node *
2381 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2383 struct dw_loc_descr_node *head, *tmp;
2385 offset += cfa->offset;
2387 if (cfa->indirect)
2389 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2390 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2391 head->dw_loc_oprnd1.val_entry = NULL;
2392 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2393 add_loc_descr (&head, tmp);
2394 if (offset != 0)
2396 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2397 add_loc_descr (&head, tmp);
2400 else
2401 head = new_reg_loc_descr (cfa->reg, offset);
2403 return head;
2406 /* This function builds a dwarf location descriptor sequence for
2407 the address at OFFSET from the CFA when stack is aligned to
2408 ALIGNMENT byte. */
2410 struct dw_loc_descr_node *
2411 build_cfa_aligned_loc (dw_cfa_location *cfa,
2412 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2414 struct dw_loc_descr_node *head;
2415 unsigned int dwarf_fp
2416 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2418 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2419 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2421 head = new_reg_loc_descr (dwarf_fp, 0);
2422 add_loc_descr (&head, int_loc_descriptor (alignment));
2423 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2424 loc_descr_plus_const (&head, offset);
2426 else
2427 head = new_reg_loc_descr (dwarf_fp, offset);
2428 return head;
2431 /* And now, the support for symbolic debugging information. */
2433 /* .debug_str support. */
2435 static void dwarf2out_init (const char *);
2436 static void dwarf2out_finish (const char *);
2437 static void dwarf2out_assembly_start (void);
2438 static void dwarf2out_define (unsigned int, const char *);
2439 static void dwarf2out_undef (unsigned int, const char *);
2440 static void dwarf2out_start_source_file (unsigned, const char *);
2441 static void dwarf2out_end_source_file (unsigned);
2442 static void dwarf2out_function_decl (tree);
2443 static void dwarf2out_begin_block (unsigned, unsigned);
2444 static void dwarf2out_end_block (unsigned, unsigned);
2445 static bool dwarf2out_ignore_block (const_tree);
2446 static void dwarf2out_global_decl (tree);
2447 static void dwarf2out_type_decl (tree, int);
2448 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2449 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2450 dw_die_ref);
2451 static void dwarf2out_abstract_function (tree);
2452 static void dwarf2out_var_location (rtx_insn *);
2453 static void dwarf2out_begin_function (tree);
2454 static void dwarf2out_end_function (unsigned int);
2455 static void dwarf2out_set_name (tree, tree);
2457 /* The debug hooks structure. */
2459 const struct gcc_debug_hooks dwarf2_debug_hooks =
2461 dwarf2out_init,
2462 dwarf2out_finish,
2463 dwarf2out_assembly_start,
2464 dwarf2out_define,
2465 dwarf2out_undef,
2466 dwarf2out_start_source_file,
2467 dwarf2out_end_source_file,
2468 dwarf2out_begin_block,
2469 dwarf2out_end_block,
2470 dwarf2out_ignore_block,
2471 dwarf2out_source_line,
2472 dwarf2out_begin_prologue,
2473 #if VMS_DEBUGGING_INFO
2474 dwarf2out_vms_end_prologue,
2475 dwarf2out_vms_begin_epilogue,
2476 #else
2477 debug_nothing_int_charstar,
2478 debug_nothing_int_charstar,
2479 #endif
2480 dwarf2out_end_epilogue,
2481 dwarf2out_begin_function,
2482 dwarf2out_end_function, /* end_function */
2483 dwarf2out_function_decl, /* function_decl */
2484 dwarf2out_global_decl,
2485 dwarf2out_type_decl, /* type_decl */
2486 dwarf2out_imported_module_or_decl,
2487 debug_nothing_tree, /* deferred_inline_function */
2488 /* The DWARF 2 backend tries to reduce debugging bloat by not
2489 emitting the abstract description of inline functions until
2490 something tries to reference them. */
2491 dwarf2out_abstract_function, /* outlining_inline_function */
2492 debug_nothing_rtx_code_label, /* label */
2493 debug_nothing_int, /* handle_pch */
2494 dwarf2out_var_location,
2495 dwarf2out_switch_text_section,
2496 dwarf2out_set_name,
2497 1, /* start_end_main_source_file */
2498 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2501 /* NOTE: In the comments in this file, many references are made to
2502 "Debugging Information Entries". This term is abbreviated as `DIE'
2503 throughout the remainder of this file. */
2505 /* An internal representation of the DWARF output is built, and then
2506 walked to generate the DWARF debugging info. The walk of the internal
2507 representation is done after the entire program has been compiled.
2508 The types below are used to describe the internal representation. */
2510 /* Whether to put type DIEs into their own section .debug_types instead
2511 of making them part of the .debug_info section. Only supported for
2512 Dwarf V4 or higher and the user didn't disable them through
2513 -fno-debug-types-section. It is more efficient to put them in a
2514 separate comdat sections since the linker will then be able to
2515 remove duplicates. But not all tools support .debug_types sections
2516 yet. */
2518 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2520 /* Various DIE's use offsets relative to the beginning of the
2521 .debug_info section to refer to each other. */
2523 typedef long int dw_offset;
2525 /* Define typedefs here to avoid circular dependencies. */
2527 typedef struct dw_attr_struct *dw_attr_ref;
2528 typedef struct dw_line_info_struct *dw_line_info_ref;
2529 typedef struct pubname_struct *pubname_ref;
2530 typedef struct dw_ranges_struct *dw_ranges_ref;
2531 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2532 typedef struct comdat_type_struct *comdat_type_node_ref;
2534 /* The entries in the line_info table more-or-less mirror the opcodes
2535 that are used in the real dwarf line table. Arrays of these entries
2536 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2537 supported. */
2539 enum dw_line_info_opcode {
2540 /* Emit DW_LNE_set_address; the operand is the label index. */
2541 LI_set_address,
2543 /* Emit a row to the matrix with the given line. This may be done
2544 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2545 special opcodes. */
2546 LI_set_line,
2548 /* Emit a DW_LNS_set_file. */
2549 LI_set_file,
2551 /* Emit a DW_LNS_set_column. */
2552 LI_set_column,
2554 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2555 LI_negate_stmt,
2557 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2558 LI_set_prologue_end,
2559 LI_set_epilogue_begin,
2561 /* Emit a DW_LNE_set_discriminator. */
2562 LI_set_discriminator
2565 typedef struct GTY(()) dw_line_info_struct {
2566 enum dw_line_info_opcode opcode;
2567 unsigned int val;
2568 } dw_line_info_entry;
2571 typedef struct GTY(()) dw_line_info_table_struct {
2572 /* The label that marks the end of this section. */
2573 const char *end_label;
2575 /* The values for the last row of the matrix, as collected in the table.
2576 These are used to minimize the changes to the next row. */
2577 unsigned int file_num;
2578 unsigned int line_num;
2579 unsigned int column_num;
2580 int discrim_num;
2581 bool is_stmt;
2582 bool in_use;
2584 vec<dw_line_info_entry, va_gc> *entries;
2585 } dw_line_info_table;
2587 typedef dw_line_info_table *dw_line_info_table_p;
2590 /* Each DIE attribute has a field specifying the attribute kind,
2591 a link to the next attribute in the chain, and an attribute value.
2592 Attributes are typically linked below the DIE they modify. */
2594 typedef struct GTY(()) dw_attr_struct {
2595 enum dwarf_attribute dw_attr;
2596 dw_val_node dw_attr_val;
2598 dw_attr_node;
2601 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2602 The children of each node form a circular list linked by
2603 die_sib. die_child points to the node *before* the "first" child node. */
2605 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2606 union die_symbol_or_type_node
2608 const char * GTY ((tag ("0"))) die_symbol;
2609 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2611 GTY ((desc ("%0.comdat_type_p"))) die_id;
2612 vec<dw_attr_node, va_gc> *die_attr;
2613 dw_die_ref die_parent;
2614 dw_die_ref die_child;
2615 dw_die_ref die_sib;
2616 dw_die_ref die_definition; /* ref from a specification to its definition */
2617 dw_offset die_offset;
2618 unsigned long die_abbrev;
2619 int die_mark;
2620 unsigned int decl_id;
2621 enum dwarf_tag die_tag;
2622 /* Die is used and must not be pruned as unused. */
2623 BOOL_BITFIELD die_perennial_p : 1;
2624 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2625 /* Lots of spare bits. */
2627 die_node;
2629 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2630 #define FOR_EACH_CHILD(die, c, expr) do { \
2631 c = die->die_child; \
2632 if (c) do { \
2633 c = c->die_sib; \
2634 expr; \
2635 } while (c != die->die_child); \
2636 } while (0)
2638 /* The pubname structure */
2640 typedef struct GTY(()) pubname_struct {
2641 dw_die_ref die;
2642 const char *name;
2644 pubname_entry;
2647 struct GTY(()) dw_ranges_struct {
2648 /* If this is positive, it's a block number, otherwise it's a
2649 bitwise-negated index into dw_ranges_by_label. */
2650 int num;
2653 /* A structure to hold a macinfo entry. */
2655 typedef struct GTY(()) macinfo_struct {
2656 unsigned char code;
2657 unsigned HOST_WIDE_INT lineno;
2658 const char *info;
2660 macinfo_entry;
2663 struct GTY(()) dw_ranges_by_label_struct {
2664 const char *begin;
2665 const char *end;
2668 /* The comdat type node structure. */
2669 typedef struct GTY(()) comdat_type_struct
2671 dw_die_ref root_die;
2672 dw_die_ref type_die;
2673 dw_die_ref skeleton_die;
2674 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2675 struct comdat_type_struct *next;
2677 comdat_type_node;
2679 /* The limbo die list structure. */
2680 typedef struct GTY(()) limbo_die_struct {
2681 dw_die_ref die;
2682 tree created_for;
2683 struct limbo_die_struct *next;
2685 limbo_die_node;
2687 typedef struct skeleton_chain_struct
2689 dw_die_ref old_die;
2690 dw_die_ref new_die;
2691 struct skeleton_chain_struct *parent;
2693 skeleton_chain_node;
2695 /* Define a macro which returns nonzero for a TYPE_DECL which was
2696 implicitly generated for a type.
2698 Note that, unlike the C front-end (which generates a NULL named
2699 TYPE_DECL node for each complete tagged type, each array type,
2700 and each function type node created) the C++ front-end generates
2701 a _named_ TYPE_DECL node for each tagged type node created.
2702 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2703 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2704 front-end, but for each type, tagged or not. */
2706 #define TYPE_DECL_IS_STUB(decl) \
2707 (DECL_NAME (decl) == NULL_TREE \
2708 || (DECL_ARTIFICIAL (decl) \
2709 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2710 /* This is necessary for stub decls that \
2711 appear in nested inline functions. */ \
2712 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2713 && (decl_ultimate_origin (decl) \
2714 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2716 /* Information concerning the compilation unit's programming
2717 language, and compiler version. */
2719 /* Fixed size portion of the DWARF compilation unit header. */
2720 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2721 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2723 /* Fixed size portion of the DWARF comdat type unit header. */
2724 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2725 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2726 + DWARF_OFFSET_SIZE)
2728 /* Fixed size portion of public names info. */
2729 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2731 /* Fixed size portion of the address range info. */
2732 #define DWARF_ARANGES_HEADER_SIZE \
2733 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2734 DWARF2_ADDR_SIZE * 2) \
2735 - DWARF_INITIAL_LENGTH_SIZE)
2737 /* Size of padding portion in the address range info. It must be
2738 aligned to twice the pointer size. */
2739 #define DWARF_ARANGES_PAD_SIZE \
2740 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2741 DWARF2_ADDR_SIZE * 2) \
2742 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2744 /* Use assembler line directives if available. */
2745 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2746 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2747 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2748 #else
2749 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2750 #endif
2751 #endif
2753 /* Minimum line offset in a special line info. opcode.
2754 This value was chosen to give a reasonable range of values. */
2755 #define DWARF_LINE_BASE -10
2757 /* First special line opcode - leave room for the standard opcodes. */
2758 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2760 /* Range of line offsets in a special line info. opcode. */
2761 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2763 /* Flag that indicates the initial value of the is_stmt_start flag.
2764 In the present implementation, we do not mark any lines as
2765 the beginning of a source statement, because that information
2766 is not made available by the GCC front-end. */
2767 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2769 /* Maximum number of operations per instruction bundle. */
2770 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2771 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2772 #endif
2774 /* This location is used by calc_die_sizes() to keep track
2775 the offset of each DIE within the .debug_info section. */
2776 static unsigned long next_die_offset;
2778 /* Record the root of the DIE's built for the current compilation unit. */
2779 static GTY(()) dw_die_ref single_comp_unit_die;
2781 /* A list of type DIEs that have been separated into comdat sections. */
2782 static GTY(()) comdat_type_node *comdat_type_list;
2784 /* A list of DIEs with a NULL parent waiting to be relocated. */
2785 static GTY(()) limbo_die_node *limbo_die_list;
2787 /* A list of DIEs for which we may have to generate
2788 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2789 static GTY(()) limbo_die_node *deferred_asm_name;
2791 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2793 typedef const char *compare_type;
2795 static hashval_t hash (dwarf_file_data *);
2796 static bool equal (dwarf_file_data *, const char *);
2799 /* Filenames referenced by this compilation unit. */
2800 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2802 struct decl_die_hasher : ggc_hasher<die_node *>
2804 typedef tree compare_type;
2806 static hashval_t hash (die_node *);
2807 static bool equal (die_node *, tree);
2809 /* A hash table of references to DIE's that describe declarations.
2810 The key is a DECL_UID() which is a unique number identifying each decl. */
2811 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2813 struct block_die_hasher : ggc_hasher<die_struct *>
2815 static hashval_t hash (die_struct *);
2816 static bool equal (die_struct *, die_struct *);
2819 /* A hash table of references to DIE's that describe COMMON blocks.
2820 The key is DECL_UID() ^ die_parent. */
2821 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2823 typedef struct GTY(()) die_arg_entry_struct {
2824 dw_die_ref die;
2825 tree arg;
2826 } die_arg_entry;
2829 /* Node of the variable location list. */
2830 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2831 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2832 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2833 in mode of the EXPR_LIST node and first EXPR_LIST operand
2834 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2835 location or NULL for padding. For larger bitsizes,
2836 mode is 0 and first operand is a CONCAT with bitsize
2837 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2838 NULL as second operand. */
2839 rtx GTY (()) loc;
2840 const char * GTY (()) label;
2841 struct var_loc_node * GTY (()) next;
2844 /* Variable location list. */
2845 struct GTY ((for_user)) var_loc_list_def {
2846 struct var_loc_node * GTY (()) first;
2848 /* Pointer to the last but one or last element of the
2849 chained list. If the list is empty, both first and
2850 last are NULL, if the list contains just one node
2851 or the last node certainly is not redundant, it points
2852 to the last node, otherwise points to the last but one.
2853 Do not mark it for GC because it is marked through the chain. */
2854 struct var_loc_node * GTY ((skip ("%h"))) last;
2856 /* Pointer to the last element before section switch,
2857 if NULL, either sections weren't switched or first
2858 is after section switch. */
2859 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2861 /* DECL_UID of the variable decl. */
2862 unsigned int decl_id;
2864 typedef struct var_loc_list_def var_loc_list;
2866 /* Call argument location list. */
2867 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2868 rtx GTY (()) call_arg_loc_note;
2869 const char * GTY (()) label;
2870 tree GTY (()) block;
2871 bool tail_call_p;
2872 rtx GTY (()) symbol_ref;
2873 struct call_arg_loc_node * GTY (()) next;
2877 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2879 typedef const_tree compare_type;
2881 static hashval_t hash (var_loc_list *);
2882 static bool equal (var_loc_list *, const_tree);
2885 /* Table of decl location linked lists. */
2886 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2888 /* Head and tail of call_arg_loc chain. */
2889 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2890 static struct call_arg_loc_node *call_arg_loc_last;
2892 /* Number of call sites in the current function. */
2893 static int call_site_count = -1;
2894 /* Number of tail call sites in the current function. */
2895 static int tail_call_site_count = -1;
2897 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2898 DIEs. */
2899 static vec<dw_die_ref> block_map;
2901 /* A cached location list. */
2902 struct GTY ((for_user)) cached_dw_loc_list_def {
2903 /* The DECL_UID of the decl that this entry describes. */
2904 unsigned int decl_id;
2906 /* The cached location list. */
2907 dw_loc_list_ref loc_list;
2909 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2911 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2914 typedef const_tree compare_type;
2916 static hashval_t hash (cached_dw_loc_list *);
2917 static bool equal (cached_dw_loc_list *, const_tree);
2920 /* Table of cached location lists. */
2921 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2923 /* A pointer to the base of a list of references to DIE's that
2924 are uniquely identified by their tag, presence/absence of
2925 children DIE's, and list of attribute/value pairs. */
2926 static GTY((length ("abbrev_die_table_allocated")))
2927 dw_die_ref *abbrev_die_table;
2929 /* Number of elements currently allocated for abbrev_die_table. */
2930 static GTY(()) unsigned abbrev_die_table_allocated;
2932 /* Number of elements in type_die_table currently in use. */
2933 static GTY(()) unsigned abbrev_die_table_in_use;
2935 /* Size (in elements) of increments by which we may expand the
2936 abbrev_die_table. */
2937 #define ABBREV_DIE_TABLE_INCREMENT 256
2939 /* A global counter for generating labels for line number data. */
2940 static unsigned int line_info_label_num;
2942 /* The current table to which we should emit line number information
2943 for the current function. This will be set up at the beginning of
2944 assembly for the function. */
2945 static dw_line_info_table *cur_line_info_table;
2947 /* The two default tables of line number info. */
2948 static GTY(()) dw_line_info_table *text_section_line_info;
2949 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2951 /* The set of all non-default tables of line number info. */
2952 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2954 /* A flag to tell pubnames/types export if there is an info section to
2955 refer to. */
2956 static bool info_section_emitted;
2958 /* A pointer to the base of a table that contains a list of publicly
2959 accessible names. */
2960 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2962 /* A pointer to the base of a table that contains a list of publicly
2963 accessible types. */
2964 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2966 /* A pointer to the base of a table that contains a list of macro
2967 defines/undefines (and file start/end markers). */
2968 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2970 /* True if .debug_macinfo or .debug_macros section is going to be
2971 emitted. */
2972 #define have_macinfo \
2973 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2974 && !macinfo_table->is_empty ())
2976 /* Array of dies for which we should generate .debug_ranges info. */
2977 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2979 /* Number of elements currently allocated for ranges_table. */
2980 static GTY(()) unsigned ranges_table_allocated;
2982 /* Number of elements in ranges_table currently in use. */
2983 static GTY(()) unsigned ranges_table_in_use;
2985 /* Array of pairs of labels referenced in ranges_table. */
2986 static GTY ((length ("ranges_by_label_allocated")))
2987 dw_ranges_by_label_ref ranges_by_label;
2989 /* Number of elements currently allocated for ranges_by_label. */
2990 static GTY(()) unsigned ranges_by_label_allocated;
2992 /* Number of elements in ranges_by_label currently in use. */
2993 static GTY(()) unsigned ranges_by_label_in_use;
2995 /* Size (in elements) of increments by which we may expand the
2996 ranges_table. */
2997 #define RANGES_TABLE_INCREMENT 64
2999 /* Whether we have location lists that need outputting */
3000 static GTY(()) bool have_location_lists;
3002 /* Unique label counter. */
3003 static GTY(()) unsigned int loclabel_num;
3005 /* Unique label counter for point-of-call tables. */
3006 static GTY(()) unsigned int poc_label_num;
3008 /* The last file entry emitted by maybe_emit_file(). */
3009 static GTY(()) struct dwarf_file_data * last_emitted_file;
3011 /* Number of internal labels generated by gen_internal_sym(). */
3012 static GTY(()) int label_num;
3014 /* Cached result of previous call to lookup_filename. */
3015 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3017 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3019 /* Instances of generic types for which we need to generate debug
3020 info that describe their generic parameters and arguments. That
3021 generation needs to happen once all types are properly laid out so
3022 we do it at the end of compilation. */
3023 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3025 /* Offset from the "steady-state frame pointer" to the frame base,
3026 within the current function. */
3027 static HOST_WIDE_INT frame_pointer_fb_offset;
3028 static bool frame_pointer_fb_offset_valid;
3030 static vec<dw_die_ref> base_types;
3032 /* Flags to represent a set of attribute classes for attributes that represent
3033 a scalar value (bounds, pointers, ...). */
3034 enum dw_scalar_form
3036 dw_scalar_form_constant = 0x01,
3037 dw_scalar_form_exprloc = 0x02,
3038 dw_scalar_form_reference = 0x04
3041 /* Forward declarations for functions defined in this file. */
3043 static int is_pseudo_reg (const_rtx);
3044 static tree type_main_variant (tree);
3045 static int is_tagged_type (const_tree);
3046 static const char *dwarf_tag_name (unsigned);
3047 static const char *dwarf_attr_name (unsigned);
3048 static const char *dwarf_form_name (unsigned);
3049 static tree decl_ultimate_origin (const_tree);
3050 static tree decl_class_context (tree);
3051 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3052 static inline enum dw_val_class AT_class (dw_attr_ref);
3053 static inline unsigned int AT_index (dw_attr_ref);
3054 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3055 static inline unsigned AT_flag (dw_attr_ref);
3056 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3057 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3058 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3059 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3060 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3061 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3062 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3063 unsigned int, unsigned char *);
3064 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3065 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3066 static inline const char *AT_string (dw_attr_ref);
3067 static enum dwarf_form AT_string_form (dw_attr_ref);
3068 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3069 static void add_AT_specification (dw_die_ref, dw_die_ref);
3070 static inline dw_die_ref AT_ref (dw_attr_ref);
3071 static inline int AT_ref_external (dw_attr_ref);
3072 static inline void set_AT_ref_external (dw_attr_ref, int);
3073 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3074 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3075 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3076 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3077 dw_loc_list_ref);
3078 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3079 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3080 static void remove_addr_table_entry (addr_table_entry *);
3081 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3082 static inline rtx AT_addr (dw_attr_ref);
3083 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3084 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3085 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3086 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3087 unsigned HOST_WIDE_INT);
3088 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3089 unsigned long, bool);
3090 static inline const char *AT_lbl (dw_attr_ref);
3091 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3092 static const char *get_AT_low_pc (dw_die_ref);
3093 static const char *get_AT_hi_pc (dw_die_ref);
3094 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3095 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3096 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3097 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3098 static bool is_cxx (void);
3099 static bool is_fortran (void);
3100 static bool is_ada (void);
3101 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3102 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3103 static void add_child_die (dw_die_ref, dw_die_ref);
3104 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3105 static dw_die_ref lookup_type_die (tree);
3106 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3107 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3108 static void equate_type_number_to_die (tree, dw_die_ref);
3109 static dw_die_ref lookup_decl_die (tree);
3110 static var_loc_list *lookup_decl_loc (const_tree);
3111 static void equate_decl_number_to_die (tree, dw_die_ref);
3112 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3113 static void print_spaces (FILE *);
3114 static void print_die (dw_die_ref, FILE *);
3115 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3116 static dw_die_ref pop_compile_unit (dw_die_ref);
3117 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3118 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3119 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3120 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3121 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3122 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3123 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3124 struct md5_ctx *, int *);
3125 struct checksum_attributes;
3126 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3127 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3128 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3129 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3130 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3131 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3132 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3133 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3134 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3135 static void compute_section_prefix (dw_die_ref);
3136 static int is_type_die (dw_die_ref);
3137 static int is_comdat_die (dw_die_ref);
3138 static int is_symbol_die (dw_die_ref);
3139 static inline bool is_template_instantiation (dw_die_ref);
3140 static void assign_symbol_names (dw_die_ref);
3141 static void break_out_includes (dw_die_ref);
3142 static int is_declaration_die (dw_die_ref);
3143 static int should_move_die_to_comdat (dw_die_ref);
3144 static dw_die_ref clone_as_declaration (dw_die_ref);
3145 static dw_die_ref clone_die (dw_die_ref);
3146 static dw_die_ref clone_tree (dw_die_ref);
3147 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3148 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3149 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3150 static dw_die_ref generate_skeleton (dw_die_ref);
3151 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3152 dw_die_ref,
3153 dw_die_ref);
3154 static void break_out_comdat_types (dw_die_ref);
3155 static void copy_decls_for_unworthy_types (dw_die_ref);
3157 static void add_sibling_attributes (dw_die_ref);
3158 static void output_location_lists (dw_die_ref);
3159 static int constant_size (unsigned HOST_WIDE_INT);
3160 static unsigned long size_of_die (dw_die_ref);
3161 static void calc_die_sizes (dw_die_ref);
3162 static void calc_base_type_die_sizes (void);
3163 static void mark_dies (dw_die_ref);
3164 static void unmark_dies (dw_die_ref);
3165 static void unmark_all_dies (dw_die_ref);
3166 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3167 static unsigned long size_of_aranges (void);
3168 static enum dwarf_form value_format (dw_attr_ref);
3169 static void output_value_format (dw_attr_ref);
3170 static void output_abbrev_section (void);
3171 static void output_die_abbrevs (unsigned long, dw_die_ref);
3172 static void output_die_symbol (dw_die_ref);
3173 static void output_die (dw_die_ref);
3174 static void output_compilation_unit_header (void);
3175 static void output_comp_unit (dw_die_ref, int);
3176 static void output_comdat_type_unit (comdat_type_node *);
3177 static const char *dwarf2_name (tree, int);
3178 static void add_pubname (tree, dw_die_ref);
3179 static void add_enumerator_pubname (const char *, dw_die_ref);
3180 static void add_pubname_string (const char *, dw_die_ref);
3181 static void add_pubtype (tree, dw_die_ref);
3182 static void output_pubnames (vec<pubname_entry, va_gc> *);
3183 static void output_aranges (unsigned long);
3184 static unsigned int add_ranges_num (int);
3185 static unsigned int add_ranges (const_tree);
3186 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3187 bool *, bool);
3188 static void output_ranges (void);
3189 static dw_line_info_table *new_line_info_table (void);
3190 static void output_line_info (bool);
3191 static void output_file_names (void);
3192 static dw_die_ref base_type_die (tree);
3193 static int is_base_type (tree);
3194 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3195 static int decl_quals (const_tree);
3196 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3197 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3198 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3199 static int type_is_enum (const_tree);
3200 static unsigned int dbx_reg_number (const_rtx);
3201 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3202 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3203 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3204 enum var_init_status);
3205 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3206 enum var_init_status);
3207 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3208 enum var_init_status);
3209 static int is_based_loc (const_rtx);
3210 static bool resolve_one_addr (rtx *);
3211 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3212 enum var_init_status);
3213 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3214 enum var_init_status);
3215 struct loc_descr_context;
3216 static dw_loc_list_ref loc_list_from_tree (tree, int,
3217 const struct loc_descr_context *);
3218 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3219 const struct loc_descr_context *);
3220 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3221 static tree field_type (const_tree);
3222 static unsigned int simple_type_align_in_bits (const_tree);
3223 static unsigned int simple_decl_align_in_bits (const_tree);
3224 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3225 static HOST_WIDE_INT field_byte_offset (const_tree);
3226 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3227 dw_loc_list_ref);
3228 static void add_data_member_location_attribute (dw_die_ref, tree);
3229 static bool add_const_value_attribute (dw_die_ref, rtx);
3230 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3231 static void insert_wide_int (const wide_int &, unsigned char *, int);
3232 static void insert_float (const_rtx, unsigned char *);
3233 static rtx rtl_for_decl_location (tree);
3234 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3235 enum dwarf_attribute);
3236 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3237 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3238 static void add_name_attribute (dw_die_ref, const char *);
3239 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3240 static void add_comp_dir_attribute (dw_die_ref);
3241 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3242 const struct loc_descr_context *);
3243 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3244 const struct loc_descr_context *);
3245 static void add_subscript_info (dw_die_ref, tree, bool);
3246 static void add_byte_size_attribute (dw_die_ref, tree);
3247 static void add_bit_offset_attribute (dw_die_ref, tree);
3248 static void add_bit_size_attribute (dw_die_ref, tree);
3249 static void add_prototyped_attribute (dw_die_ref, tree);
3250 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3251 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3252 static void add_src_coords_attributes (dw_die_ref, tree);
3253 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3254 static void push_decl_scope (tree);
3255 static void pop_decl_scope (void);
3256 static dw_die_ref scope_die_for (tree, dw_die_ref);
3257 static inline int local_scope_p (dw_die_ref);
3258 static inline int class_scope_p (dw_die_ref);
3259 static inline int class_or_namespace_scope_p (dw_die_ref);
3260 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3261 static void add_calling_convention_attribute (dw_die_ref, tree);
3262 static const char *type_tag (const_tree);
3263 static tree member_declared_type (const_tree);
3264 #if 0
3265 static const char *decl_start_label (tree);
3266 #endif
3267 static void gen_array_type_die (tree, dw_die_ref);
3268 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3269 #if 0
3270 static void gen_entry_point_die (tree, dw_die_ref);
3271 #endif
3272 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3273 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3274 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3275 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3276 static void gen_formal_types_die (tree, dw_die_ref);
3277 static void gen_subprogram_die (tree, dw_die_ref);
3278 static void gen_variable_die (tree, tree, dw_die_ref);
3279 static void gen_const_die (tree, dw_die_ref);
3280 static void gen_label_die (tree, dw_die_ref);
3281 static void gen_lexical_block_die (tree, dw_die_ref);
3282 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3283 static void gen_field_die (tree, dw_die_ref);
3284 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3285 static dw_die_ref gen_compile_unit_die (const char *);
3286 static void gen_inheritance_die (tree, tree, dw_die_ref);
3287 static void gen_member_die (tree, dw_die_ref);
3288 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3289 enum debug_info_usage);
3290 static void gen_subroutine_type_die (tree, dw_die_ref);
3291 static void gen_typedef_die (tree, dw_die_ref);
3292 static void gen_type_die (tree, dw_die_ref);
3293 static void gen_block_die (tree, dw_die_ref);
3294 static void decls_for_scope (tree, dw_die_ref);
3295 static inline int is_redundant_typedef (const_tree);
3296 static bool is_naming_typedef_decl (const_tree);
3297 static inline dw_die_ref get_context_die (tree);
3298 static void gen_namespace_die (tree, dw_die_ref);
3299 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3300 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3301 static dw_die_ref force_decl_die (tree);
3302 static dw_die_ref force_type_die (tree);
3303 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3304 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3305 static struct dwarf_file_data * lookup_filename (const char *);
3306 static void retry_incomplete_types (void);
3307 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3308 static void gen_generic_params_dies (tree);
3309 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3310 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3311 static void splice_child_die (dw_die_ref, dw_die_ref);
3312 static int file_info_cmp (const void *, const void *);
3313 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3314 const char *, const char *);
3315 static void output_loc_list (dw_loc_list_ref);
3316 static char *gen_internal_sym (const char *);
3317 static bool want_pubnames (void);
3319 static void prune_unmark_dies (dw_die_ref);
3320 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3321 static void prune_unused_types_mark (dw_die_ref, int);
3322 static void prune_unused_types_walk (dw_die_ref);
3323 static void prune_unused_types_walk_attribs (dw_die_ref);
3324 static void prune_unused_types_prune (dw_die_ref);
3325 static void prune_unused_types (void);
3326 static int maybe_emit_file (struct dwarf_file_data *fd);
3327 static inline const char *AT_vms_delta1 (dw_attr_ref);
3328 static inline const char *AT_vms_delta2 (dw_attr_ref);
3329 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3330 const char *, const char *);
3331 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3332 static void gen_remaining_tmpl_value_param_die_attribute (void);
3333 static bool generic_type_p (tree);
3334 static void schedule_generic_params_dies_gen (tree t);
3335 static void gen_scheduled_generic_parms_dies (void);
3337 static const char *comp_dir_string (void);
3339 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3341 /* enum for tracking thread-local variables whose address is really an offset
3342 relative to the TLS pointer, which will need link-time relocation, but will
3343 not need relocation by the DWARF consumer. */
3345 enum dtprel_bool
3347 dtprel_false = 0,
3348 dtprel_true = 1
3351 /* Return the operator to use for an address of a variable. For dtprel_true, we
3352 use DW_OP_const*. For regular variables, which need both link-time
3353 relocation and consumer-level relocation (e.g., to account for shared objects
3354 loaded at a random address), we use DW_OP_addr*. */
3356 static inline enum dwarf_location_atom
3357 dw_addr_op (enum dtprel_bool dtprel)
3359 if (dtprel == dtprel_true)
3360 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3361 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3362 else
3363 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3366 /* Return a pointer to a newly allocated address location description. If
3367 dwarf_split_debug_info is true, then record the address with the appropriate
3368 relocation. */
3369 static inline dw_loc_descr_ref
3370 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3372 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3374 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3375 ref->dw_loc_oprnd1.v.val_addr = addr;
3376 ref->dtprel = dtprel;
3377 if (dwarf_split_debug_info)
3378 ref->dw_loc_oprnd1.val_entry
3379 = add_addr_table_entry (addr,
3380 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3381 else
3382 ref->dw_loc_oprnd1.val_entry = NULL;
3384 return ref;
3387 /* Section names used to hold DWARF debugging information. */
3389 #ifndef DEBUG_INFO_SECTION
3390 #define DEBUG_INFO_SECTION ".debug_info"
3391 #endif
3392 #ifndef DEBUG_DWO_INFO_SECTION
3393 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3394 #endif
3395 #ifndef DEBUG_ABBREV_SECTION
3396 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3397 #endif
3398 #ifndef DEBUG_DWO_ABBREV_SECTION
3399 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3400 #endif
3401 #ifndef DEBUG_ARANGES_SECTION
3402 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3403 #endif
3404 #ifndef DEBUG_ADDR_SECTION
3405 #define DEBUG_ADDR_SECTION ".debug_addr"
3406 #endif
3407 #ifndef DEBUG_NORM_MACINFO_SECTION
3408 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3409 #endif
3410 #ifndef DEBUG_DWO_MACINFO_SECTION
3411 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3412 #endif
3413 #ifndef DEBUG_MACINFO_SECTION
3414 #define DEBUG_MACINFO_SECTION \
3415 (!dwarf_split_debug_info \
3416 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3417 #endif
3418 #ifndef DEBUG_NORM_MACRO_SECTION
3419 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3420 #endif
3421 #ifndef DEBUG_DWO_MACRO_SECTION
3422 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3423 #endif
3424 #ifndef DEBUG_MACRO_SECTION
3425 #define DEBUG_MACRO_SECTION \
3426 (!dwarf_split_debug_info \
3427 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3428 #endif
3429 #ifndef DEBUG_LINE_SECTION
3430 #define DEBUG_LINE_SECTION ".debug_line"
3431 #endif
3432 #ifndef DEBUG_DWO_LINE_SECTION
3433 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3434 #endif
3435 #ifndef DEBUG_LOC_SECTION
3436 #define DEBUG_LOC_SECTION ".debug_loc"
3437 #endif
3438 #ifndef DEBUG_DWO_LOC_SECTION
3439 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3440 #endif
3441 #ifndef DEBUG_PUBNAMES_SECTION
3442 #define DEBUG_PUBNAMES_SECTION \
3443 ((debug_generate_pub_sections == 2) \
3444 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3445 #endif
3446 #ifndef DEBUG_PUBTYPES_SECTION
3447 #define DEBUG_PUBTYPES_SECTION \
3448 ((debug_generate_pub_sections == 2) \
3449 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3450 #endif
3451 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3452 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3453 #ifndef DEBUG_STR_OFFSETS_SECTION
3454 #define DEBUG_STR_OFFSETS_SECTION \
3455 (!dwarf_split_debug_info \
3456 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3457 #endif
3458 #ifndef DEBUG_STR_DWO_SECTION
3459 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3460 #endif
3461 #ifndef DEBUG_STR_SECTION
3462 #define DEBUG_STR_SECTION ".debug_str"
3463 #endif
3464 #ifndef DEBUG_RANGES_SECTION
3465 #define DEBUG_RANGES_SECTION ".debug_ranges"
3466 #endif
3468 /* Standard ELF section names for compiled code and data. */
3469 #ifndef TEXT_SECTION_NAME
3470 #define TEXT_SECTION_NAME ".text"
3471 #endif
3473 /* Section flags for .debug_macinfo/.debug_macro section. */
3474 #define DEBUG_MACRO_SECTION_FLAGS \
3475 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3477 /* Section flags for .debug_str section. */
3478 #define DEBUG_STR_SECTION_FLAGS \
3479 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3480 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3481 : SECTION_DEBUG)
3483 /* Section flags for .debug_str.dwo section. */
3484 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3486 /* Labels we insert at beginning sections we can reference instead of
3487 the section names themselves. */
3489 #ifndef TEXT_SECTION_LABEL
3490 #define TEXT_SECTION_LABEL "Ltext"
3491 #endif
3492 #ifndef COLD_TEXT_SECTION_LABEL
3493 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3494 #endif
3495 #ifndef DEBUG_LINE_SECTION_LABEL
3496 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3497 #endif
3498 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3499 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3500 #endif
3501 #ifndef DEBUG_INFO_SECTION_LABEL
3502 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3503 #endif
3504 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3505 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3506 #endif
3507 #ifndef DEBUG_ABBREV_SECTION_LABEL
3508 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3509 #endif
3510 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3511 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3512 #endif
3513 #ifndef DEBUG_ADDR_SECTION_LABEL
3514 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3515 #endif
3516 #ifndef DEBUG_LOC_SECTION_LABEL
3517 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3518 #endif
3519 #ifndef DEBUG_RANGES_SECTION_LABEL
3520 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3521 #endif
3522 #ifndef DEBUG_MACINFO_SECTION_LABEL
3523 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3524 #endif
3525 #ifndef DEBUG_MACRO_SECTION_LABEL
3526 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3527 #endif
3528 #define SKELETON_COMP_DIE_ABBREV 1
3529 #define SKELETON_TYPE_DIE_ABBREV 2
3531 /* Definitions of defaults for formats and names of various special
3532 (artificial) labels which may be generated within this file (when the -g
3533 options is used and DWARF2_DEBUGGING_INFO is in effect.
3534 If necessary, these may be overridden from within the tm.h file, but
3535 typically, overriding these defaults is unnecessary. */
3537 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3538 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3539 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3540 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3541 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3542 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3543 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3544 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3545 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3546 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3547 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3548 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3549 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3550 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3552 #ifndef TEXT_END_LABEL
3553 #define TEXT_END_LABEL "Letext"
3554 #endif
3555 #ifndef COLD_END_LABEL
3556 #define COLD_END_LABEL "Letext_cold"
3557 #endif
3558 #ifndef BLOCK_BEGIN_LABEL
3559 #define BLOCK_BEGIN_LABEL "LBB"
3560 #endif
3561 #ifndef BLOCK_END_LABEL
3562 #define BLOCK_END_LABEL "LBE"
3563 #endif
3564 #ifndef LINE_CODE_LABEL
3565 #define LINE_CODE_LABEL "LM"
3566 #endif
3569 /* Return the root of the DIE's built for the current compilation unit. */
3570 static dw_die_ref
3571 comp_unit_die (void)
3573 if (!single_comp_unit_die)
3574 single_comp_unit_die = gen_compile_unit_die (NULL);
3575 return single_comp_unit_die;
3578 /* We allow a language front-end to designate a function that is to be
3579 called to "demangle" any name before it is put into a DIE. */
3581 static const char *(*demangle_name_func) (const char *);
3583 void
3584 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3586 demangle_name_func = func;
3589 /* Test if rtl node points to a pseudo register. */
3591 static inline int
3592 is_pseudo_reg (const_rtx rtl)
3594 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3595 || (GET_CODE (rtl) == SUBREG
3596 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3599 /* Return a reference to a type, with its const and volatile qualifiers
3600 removed. */
3602 static inline tree
3603 type_main_variant (tree type)
3605 type = TYPE_MAIN_VARIANT (type);
3607 /* ??? There really should be only one main variant among any group of
3608 variants of a given type (and all of the MAIN_VARIANT values for all
3609 members of the group should point to that one type) but sometimes the C
3610 front-end messes this up for array types, so we work around that bug
3611 here. */
3612 if (TREE_CODE (type) == ARRAY_TYPE)
3613 while (type != TYPE_MAIN_VARIANT (type))
3614 type = TYPE_MAIN_VARIANT (type);
3616 return type;
3619 /* Return nonzero if the given type node represents a tagged type. */
3621 static inline int
3622 is_tagged_type (const_tree type)
3624 enum tree_code code = TREE_CODE (type);
3626 return (code == RECORD_TYPE || code == UNION_TYPE
3627 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3630 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3632 static void
3633 get_ref_die_offset_label (char *label, dw_die_ref ref)
3635 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3638 /* Return die_offset of a DIE reference to a base type. */
3640 static unsigned long int
3641 get_base_type_offset (dw_die_ref ref)
3643 if (ref->die_offset)
3644 return ref->die_offset;
3645 if (comp_unit_die ()->die_abbrev)
3647 calc_base_type_die_sizes ();
3648 gcc_assert (ref->die_offset);
3650 return ref->die_offset;
3653 /* Return die_offset of a DIE reference other than base type. */
3655 static unsigned long int
3656 get_ref_die_offset (dw_die_ref ref)
3658 gcc_assert (ref->die_offset);
3659 return ref->die_offset;
3662 /* Convert a DIE tag into its string name. */
3664 static const char *
3665 dwarf_tag_name (unsigned int tag)
3667 const char *name = get_DW_TAG_name (tag);
3669 if (name != NULL)
3670 return name;
3672 return "DW_TAG_<unknown>";
3675 /* Convert a DWARF attribute code into its string name. */
3677 static const char *
3678 dwarf_attr_name (unsigned int attr)
3680 const char *name;
3682 switch (attr)
3684 #if VMS_DEBUGGING_INFO
3685 case DW_AT_HP_prologue:
3686 return "DW_AT_HP_prologue";
3687 #else
3688 case DW_AT_MIPS_loop_unroll_factor:
3689 return "DW_AT_MIPS_loop_unroll_factor";
3690 #endif
3692 #if VMS_DEBUGGING_INFO
3693 case DW_AT_HP_epilogue:
3694 return "DW_AT_HP_epilogue";
3695 #else
3696 case DW_AT_MIPS_stride:
3697 return "DW_AT_MIPS_stride";
3698 #endif
3701 name = get_DW_AT_name (attr);
3703 if (name != NULL)
3704 return name;
3706 return "DW_AT_<unknown>";
3709 /* Convert a DWARF value form code into its string name. */
3711 static const char *
3712 dwarf_form_name (unsigned int form)
3714 const char *name = get_DW_FORM_name (form);
3716 if (name != NULL)
3717 return name;
3719 return "DW_FORM_<unknown>";
3722 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3723 instance of an inlined instance of a decl which is local to an inline
3724 function, so we have to trace all of the way back through the origin chain
3725 to find out what sort of node actually served as the original seed for the
3726 given block. */
3728 static tree
3729 decl_ultimate_origin (const_tree decl)
3731 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3732 return NULL_TREE;
3734 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3735 we're trying to output the abstract instance of this function. */
3736 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3737 return NULL_TREE;
3739 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3740 most distant ancestor, this should never happen. */
3741 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3743 return DECL_ABSTRACT_ORIGIN (decl);
3746 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3747 of a virtual function may refer to a base class, so we check the 'this'
3748 parameter. */
3750 static tree
3751 decl_class_context (tree decl)
3753 tree context = NULL_TREE;
3755 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3756 context = DECL_CONTEXT (decl);
3757 else
3758 context = TYPE_MAIN_VARIANT
3759 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3761 if (context && !TYPE_P (context))
3762 context = NULL_TREE;
3764 return context;
3767 /* Add an attribute/value pair to a DIE. */
3769 static inline void
3770 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3772 /* Maybe this should be an assert? */
3773 if (die == NULL)
3774 return;
3776 vec_safe_reserve (die->die_attr, 1);
3777 vec_safe_push (die->die_attr, *attr);
3780 static inline enum dw_val_class
3781 AT_class (dw_attr_ref a)
3783 return a->dw_attr_val.val_class;
3786 /* Return the index for any attribute that will be referenced with a
3787 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3788 are stored in dw_attr_val.v.val_str for reference counting
3789 pruning. */
3791 static inline unsigned int
3792 AT_index (dw_attr_ref a)
3794 if (AT_class (a) == dw_val_class_str)
3795 return a->dw_attr_val.v.val_str->index;
3796 else if (a->dw_attr_val.val_entry != NULL)
3797 return a->dw_attr_val.val_entry->index;
3798 return NOT_INDEXED;
3801 /* Add a flag value attribute to a DIE. */
3803 static inline void
3804 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3806 dw_attr_node attr;
3808 attr.dw_attr = attr_kind;
3809 attr.dw_attr_val.val_class = dw_val_class_flag;
3810 attr.dw_attr_val.val_entry = NULL;
3811 attr.dw_attr_val.v.val_flag = flag;
3812 add_dwarf_attr (die, &attr);
3815 static inline unsigned
3816 AT_flag (dw_attr_ref a)
3818 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3819 return a->dw_attr_val.v.val_flag;
3822 /* Add a signed integer attribute value to a DIE. */
3824 static inline void
3825 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3827 dw_attr_node attr;
3829 attr.dw_attr = attr_kind;
3830 attr.dw_attr_val.val_class = dw_val_class_const;
3831 attr.dw_attr_val.val_entry = NULL;
3832 attr.dw_attr_val.v.val_int = int_val;
3833 add_dwarf_attr (die, &attr);
3836 static inline HOST_WIDE_INT
3837 AT_int (dw_attr_ref a)
3839 gcc_assert (a && AT_class (a) == dw_val_class_const);
3840 return a->dw_attr_val.v.val_int;
3843 /* Add an unsigned integer attribute value to a DIE. */
3845 static inline void
3846 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3847 unsigned HOST_WIDE_INT unsigned_val)
3849 dw_attr_node attr;
3851 attr.dw_attr = attr_kind;
3852 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3853 attr.dw_attr_val.val_entry = NULL;
3854 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3855 add_dwarf_attr (die, &attr);
3858 static inline unsigned HOST_WIDE_INT
3859 AT_unsigned (dw_attr_ref a)
3861 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3862 return a->dw_attr_val.v.val_unsigned;
3865 /* Add an unsigned wide integer attribute value to a DIE. */
3867 static inline void
3868 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3869 const wide_int& w)
3871 dw_attr_node attr;
3873 attr.dw_attr = attr_kind;
3874 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3875 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3876 *attr.dw_attr_val.v.val_wide = w;
3877 add_dwarf_attr (die, &attr);
3880 /* Add an unsigned double integer attribute value to a DIE. */
3882 static inline void
3883 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3884 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3886 dw_attr_node attr;
3888 attr.dw_attr = attr_kind;
3889 attr.dw_attr_val.val_class = dw_val_class_const_double;
3890 attr.dw_attr_val.val_entry = NULL;
3891 attr.dw_attr_val.v.val_double.high = high;
3892 attr.dw_attr_val.v.val_double.low = low;
3893 add_dwarf_attr (die, &attr);
3896 /* Add a floating point attribute value to a DIE and return it. */
3898 static inline void
3899 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3900 unsigned int length, unsigned int elt_size, unsigned char *array)
3902 dw_attr_node attr;
3904 attr.dw_attr = attr_kind;
3905 attr.dw_attr_val.val_class = dw_val_class_vec;
3906 attr.dw_attr_val.val_entry = NULL;
3907 attr.dw_attr_val.v.val_vec.length = length;
3908 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3909 attr.dw_attr_val.v.val_vec.array = array;
3910 add_dwarf_attr (die, &attr);
3913 /* Add an 8-byte data attribute value to a DIE. */
3915 static inline void
3916 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3917 unsigned char data8[8])
3919 dw_attr_node attr;
3921 attr.dw_attr = attr_kind;
3922 attr.dw_attr_val.val_class = dw_val_class_data8;
3923 attr.dw_attr_val.val_entry = NULL;
3924 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3925 add_dwarf_attr (die, &attr);
3928 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3929 dwarf_split_debug_info, address attributes in dies destined for the
3930 final executable have force_direct set to avoid using indexed
3931 references. */
3933 static inline void
3934 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3935 bool force_direct)
3937 dw_attr_node attr;
3938 char * lbl_id;
3940 lbl_id = xstrdup (lbl_low);
3941 attr.dw_attr = DW_AT_low_pc;
3942 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3943 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3944 if (dwarf_split_debug_info && !force_direct)
3945 attr.dw_attr_val.val_entry
3946 = add_addr_table_entry (lbl_id, ate_kind_label);
3947 else
3948 attr.dw_attr_val.val_entry = NULL;
3949 add_dwarf_attr (die, &attr);
3951 attr.dw_attr = DW_AT_high_pc;
3952 if (dwarf_version < 4)
3953 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3954 else
3955 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3956 lbl_id = xstrdup (lbl_high);
3957 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3958 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3959 && dwarf_split_debug_info && !force_direct)
3960 attr.dw_attr_val.val_entry
3961 = add_addr_table_entry (lbl_id, ate_kind_label);
3962 else
3963 attr.dw_attr_val.val_entry = NULL;
3964 add_dwarf_attr (die, &attr);
3967 /* Hash and equality functions for debug_str_hash. */
3969 hashval_t
3970 indirect_string_hasher::hash (indirect_string_node *x)
3972 return htab_hash_string (x->str);
3975 bool
3976 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3978 return strcmp (x1->str, x2) == 0;
3981 /* Add STR to the given string hash table. */
3983 static struct indirect_string_node *
3984 find_AT_string_in_table (const char *str,
3985 hash_table<indirect_string_hasher> *table)
3987 struct indirect_string_node *node;
3989 indirect_string_node **slot
3990 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
3991 if (*slot == NULL)
3993 node = ggc_cleared_alloc<indirect_string_node> ();
3994 node->str = ggc_strdup (str);
3995 *slot = node;
3997 else
3998 node = *slot;
4000 node->refcount++;
4001 return node;
4004 /* Add STR to the indirect string hash table. */
4006 static struct indirect_string_node *
4007 find_AT_string (const char *str)
4009 if (! debug_str_hash)
4010 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4012 return find_AT_string_in_table (str, debug_str_hash);
4015 /* Add a string attribute value to a DIE. */
4017 static inline void
4018 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4020 dw_attr_node attr;
4021 struct indirect_string_node *node;
4023 node = find_AT_string (str);
4025 attr.dw_attr = attr_kind;
4026 attr.dw_attr_val.val_class = dw_val_class_str;
4027 attr.dw_attr_val.val_entry = NULL;
4028 attr.dw_attr_val.v.val_str = node;
4029 add_dwarf_attr (die, &attr);
4032 static inline const char *
4033 AT_string (dw_attr_ref a)
4035 gcc_assert (a && AT_class (a) == dw_val_class_str);
4036 return a->dw_attr_val.v.val_str->str;
4039 /* Call this function directly to bypass AT_string_form's logic to put
4040 the string inline in the die. */
4042 static void
4043 set_indirect_string (struct indirect_string_node *node)
4045 char label[32];
4046 /* Already indirect is a no op. */
4047 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4049 gcc_assert (node->label);
4050 return;
4052 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4053 ++dw2_string_counter;
4054 node->label = xstrdup (label);
4056 if (!dwarf_split_debug_info)
4058 node->form = DW_FORM_strp;
4059 node->index = NOT_INDEXED;
4061 else
4063 node->form = DW_FORM_GNU_str_index;
4064 node->index = NO_INDEX_ASSIGNED;
4068 /* Find out whether a string should be output inline in DIE
4069 or out-of-line in .debug_str section. */
4071 static enum dwarf_form
4072 find_string_form (struct indirect_string_node *node)
4074 unsigned int len;
4076 if (node->form)
4077 return node->form;
4079 len = strlen (node->str) + 1;
4081 /* If the string is shorter or equal to the size of the reference, it is
4082 always better to put it inline. */
4083 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4084 return node->form = DW_FORM_string;
4086 /* If we cannot expect the linker to merge strings in .debug_str
4087 section, only put it into .debug_str if it is worth even in this
4088 single module. */
4089 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4090 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4091 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4092 return node->form = DW_FORM_string;
4094 set_indirect_string (node);
4096 return node->form;
4099 /* Find out whether the string referenced from the attribute should be
4100 output inline in DIE or out-of-line in .debug_str section. */
4102 static enum dwarf_form
4103 AT_string_form (dw_attr_ref a)
4105 gcc_assert (a && AT_class (a) == dw_val_class_str);
4106 return find_string_form (a->dw_attr_val.v.val_str);
4109 /* Add a DIE reference attribute value to a DIE. */
4111 static inline void
4112 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4114 dw_attr_node attr;
4116 #ifdef ENABLE_CHECKING
4117 gcc_assert (targ_die != NULL);
4118 #else
4119 /* With LTO we can end up trying to reference something we didn't create
4120 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4121 if (targ_die == NULL)
4122 return;
4123 #endif
4125 attr.dw_attr = attr_kind;
4126 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4127 attr.dw_attr_val.val_entry = NULL;
4128 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4129 attr.dw_attr_val.v.val_die_ref.external = 0;
4130 add_dwarf_attr (die, &attr);
4133 /* Change DIE reference REF to point to NEW_DIE instead. */
4135 static inline void
4136 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4138 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4139 ref->dw_attr_val.v.val_die_ref.die = new_die;
4140 ref->dw_attr_val.v.val_die_ref.external = 0;
4143 /* Add an AT_specification attribute to a DIE, and also make the back
4144 pointer from the specification to the definition. */
4146 static inline void
4147 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4149 add_AT_die_ref (die, DW_AT_specification, targ_die);
4150 gcc_assert (!targ_die->die_definition);
4151 targ_die->die_definition = die;
4154 static inline dw_die_ref
4155 AT_ref (dw_attr_ref a)
4157 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4158 return a->dw_attr_val.v.val_die_ref.die;
4161 static inline int
4162 AT_ref_external (dw_attr_ref a)
4164 if (a && AT_class (a) == dw_val_class_die_ref)
4165 return a->dw_attr_val.v.val_die_ref.external;
4167 return 0;
4170 static inline void
4171 set_AT_ref_external (dw_attr_ref a, int i)
4173 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4174 a->dw_attr_val.v.val_die_ref.external = i;
4177 /* Add an FDE reference attribute value to a DIE. */
4179 static inline void
4180 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4182 dw_attr_node attr;
4184 attr.dw_attr = attr_kind;
4185 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4186 attr.dw_attr_val.val_entry = NULL;
4187 attr.dw_attr_val.v.val_fde_index = targ_fde;
4188 add_dwarf_attr (die, &attr);
4191 /* Add a location description attribute value to a DIE. */
4193 static inline void
4194 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4196 dw_attr_node attr;
4198 attr.dw_attr = attr_kind;
4199 attr.dw_attr_val.val_class = dw_val_class_loc;
4200 attr.dw_attr_val.val_entry = NULL;
4201 attr.dw_attr_val.v.val_loc = loc;
4202 add_dwarf_attr (die, &attr);
4205 static inline dw_loc_descr_ref
4206 AT_loc (dw_attr_ref a)
4208 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4209 return a->dw_attr_val.v.val_loc;
4212 static inline void
4213 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4215 dw_attr_node attr;
4217 attr.dw_attr = attr_kind;
4218 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4219 attr.dw_attr_val.val_entry = NULL;
4220 attr.dw_attr_val.v.val_loc_list = loc_list;
4221 add_dwarf_attr (die, &attr);
4222 have_location_lists = true;
4225 static inline dw_loc_list_ref
4226 AT_loc_list (dw_attr_ref a)
4228 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4229 return a->dw_attr_val.v.val_loc_list;
4232 static inline dw_loc_list_ref *
4233 AT_loc_list_ptr (dw_attr_ref a)
4235 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4236 return &a->dw_attr_val.v.val_loc_list;
4239 struct addr_hasher : ggc_hasher<addr_table_entry *>
4241 static hashval_t hash (addr_table_entry *);
4242 static bool equal (addr_table_entry *, addr_table_entry *);
4245 /* Table of entries into the .debug_addr section. */
4247 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4249 /* Hash an address_table_entry. */
4251 hashval_t
4252 addr_hasher::hash (addr_table_entry *a)
4254 inchash::hash hstate;
4255 switch (a->kind)
4257 case ate_kind_rtx:
4258 hstate.add_int (0);
4259 break;
4260 case ate_kind_rtx_dtprel:
4261 hstate.add_int (1);
4262 break;
4263 case ate_kind_label:
4264 return htab_hash_string (a->addr.label);
4265 default:
4266 gcc_unreachable ();
4268 inchash::add_rtx (a->addr.rtl, hstate);
4269 return hstate.end ();
4272 /* Determine equality for two address_table_entries. */
4274 bool
4275 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4277 if (a1->kind != a2->kind)
4278 return 0;
4279 switch (a1->kind)
4281 case ate_kind_rtx:
4282 case ate_kind_rtx_dtprel:
4283 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4284 case ate_kind_label:
4285 return strcmp (a1->addr.label, a2->addr.label) == 0;
4286 default:
4287 gcc_unreachable ();
4291 /* Initialize an addr_table_entry. */
4293 void
4294 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4296 e->kind = kind;
4297 switch (kind)
4299 case ate_kind_rtx:
4300 case ate_kind_rtx_dtprel:
4301 e->addr.rtl = (rtx) addr;
4302 break;
4303 case ate_kind_label:
4304 e->addr.label = (char *) addr;
4305 break;
4307 e->refcount = 0;
4308 e->index = NO_INDEX_ASSIGNED;
4311 /* Add attr to the address table entry to the table. Defer setting an
4312 index until output time. */
4314 static addr_table_entry *
4315 add_addr_table_entry (void *addr, enum ate_kind kind)
4317 addr_table_entry *node;
4318 addr_table_entry finder;
4320 gcc_assert (dwarf_split_debug_info);
4321 if (! addr_index_table)
4322 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4323 init_addr_table_entry (&finder, kind, addr);
4324 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4326 if (*slot == HTAB_EMPTY_ENTRY)
4328 node = ggc_cleared_alloc<addr_table_entry> ();
4329 init_addr_table_entry (node, kind, addr);
4330 *slot = node;
4332 else
4333 node = *slot;
4335 node->refcount++;
4336 return node;
4339 /* Remove an entry from the addr table by decrementing its refcount.
4340 Strictly, decrementing the refcount would be enough, but the
4341 assertion that the entry is actually in the table has found
4342 bugs. */
4344 static void
4345 remove_addr_table_entry (addr_table_entry *entry)
4347 gcc_assert (dwarf_split_debug_info && addr_index_table);
4348 /* After an index is assigned, the table is frozen. */
4349 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4350 entry->refcount--;
4353 /* Given a location list, remove all addresses it refers to from the
4354 address_table. */
4356 static void
4357 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4359 for (; descr; descr = descr->dw_loc_next)
4360 if (descr->dw_loc_oprnd1.val_entry != NULL)
4362 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4363 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4367 /* A helper function for dwarf2out_finish called through
4368 htab_traverse. Assign an addr_table_entry its index. All entries
4369 must be collected into the table when this function is called,
4370 because the indexing code relies on htab_traverse to traverse nodes
4371 in the same order for each run. */
4374 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4376 addr_table_entry *node = *h;
4378 /* Don't index unreferenced nodes. */
4379 if (node->refcount == 0)
4380 return 1;
4382 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4383 node->index = *index;
4384 *index += 1;
4386 return 1;
4389 /* Add an address constant attribute value to a DIE. When using
4390 dwarf_split_debug_info, address attributes in dies destined for the
4391 final executable should be direct references--setting the parameter
4392 force_direct ensures this behavior. */
4394 static inline void
4395 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4396 bool force_direct)
4398 dw_attr_node attr;
4400 attr.dw_attr = attr_kind;
4401 attr.dw_attr_val.val_class = dw_val_class_addr;
4402 attr.dw_attr_val.v.val_addr = addr;
4403 if (dwarf_split_debug_info && !force_direct)
4404 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4405 else
4406 attr.dw_attr_val.val_entry = NULL;
4407 add_dwarf_attr (die, &attr);
4410 /* Get the RTX from to an address DIE attribute. */
4412 static inline rtx
4413 AT_addr (dw_attr_ref a)
4415 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4416 return a->dw_attr_val.v.val_addr;
4419 /* Add a file attribute value to a DIE. */
4421 static inline void
4422 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4423 struct dwarf_file_data *fd)
4425 dw_attr_node attr;
4427 attr.dw_attr = attr_kind;
4428 attr.dw_attr_val.val_class = dw_val_class_file;
4429 attr.dw_attr_val.val_entry = NULL;
4430 attr.dw_attr_val.v.val_file = fd;
4431 add_dwarf_attr (die, &attr);
4434 /* Get the dwarf_file_data from a file DIE attribute. */
4436 static inline struct dwarf_file_data *
4437 AT_file (dw_attr_ref a)
4439 gcc_assert (a && AT_class (a) == dw_val_class_file);
4440 return a->dw_attr_val.v.val_file;
4443 /* Add a vms delta attribute value to a DIE. */
4445 static inline void
4446 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4447 const char *lbl1, const char *lbl2)
4449 dw_attr_node attr;
4451 attr.dw_attr = attr_kind;
4452 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4453 attr.dw_attr_val.val_entry = NULL;
4454 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4455 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4456 add_dwarf_attr (die, &attr);
4459 /* Add a label identifier attribute value to a DIE. */
4461 static inline void
4462 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4463 const char *lbl_id)
4465 dw_attr_node attr;
4467 attr.dw_attr = attr_kind;
4468 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4469 attr.dw_attr_val.val_entry = NULL;
4470 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4471 if (dwarf_split_debug_info)
4472 attr.dw_attr_val.val_entry
4473 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4474 ate_kind_label);
4475 add_dwarf_attr (die, &attr);
4478 /* Add a section offset attribute value to a DIE, an offset into the
4479 debug_line section. */
4481 static inline void
4482 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4483 const char *label)
4485 dw_attr_node attr;
4487 attr.dw_attr = attr_kind;
4488 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4489 attr.dw_attr_val.val_entry = NULL;
4490 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4491 add_dwarf_attr (die, &attr);
4494 /* Add a section offset attribute value to a DIE, an offset into the
4495 debug_macinfo section. */
4497 static inline void
4498 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4499 const char *label)
4501 dw_attr_node attr;
4503 attr.dw_attr = attr_kind;
4504 attr.dw_attr_val.val_class = dw_val_class_macptr;
4505 attr.dw_attr_val.val_entry = NULL;
4506 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4507 add_dwarf_attr (die, &attr);
4510 /* Add an offset attribute value to a DIE. */
4512 static inline void
4513 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4514 unsigned HOST_WIDE_INT offset)
4516 dw_attr_node attr;
4518 attr.dw_attr = attr_kind;
4519 attr.dw_attr_val.val_class = dw_val_class_offset;
4520 attr.dw_attr_val.val_entry = NULL;
4521 attr.dw_attr_val.v.val_offset = offset;
4522 add_dwarf_attr (die, &attr);
4525 /* Add a range_list attribute value to a DIE. When using
4526 dwarf_split_debug_info, address attributes in dies destined for the
4527 final executable should be direct references--setting the parameter
4528 force_direct ensures this behavior. */
4530 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4531 #define RELOCATED_OFFSET (NULL)
4533 static void
4534 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4535 long unsigned int offset, bool force_direct)
4537 dw_attr_node attr;
4539 attr.dw_attr = attr_kind;
4540 attr.dw_attr_val.val_class = dw_val_class_range_list;
4541 /* For the range_list attribute, use val_entry to store whether the
4542 offset should follow split-debug-info or normal semantics. This
4543 value is read in output_range_list_offset. */
4544 if (dwarf_split_debug_info && !force_direct)
4545 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4546 else
4547 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4548 attr.dw_attr_val.v.val_offset = offset;
4549 add_dwarf_attr (die, &attr);
4552 /* Return the start label of a delta attribute. */
4554 static inline const char *
4555 AT_vms_delta1 (dw_attr_ref a)
4557 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4558 return a->dw_attr_val.v.val_vms_delta.lbl1;
4561 /* Return the end label of a delta attribute. */
4563 static inline const char *
4564 AT_vms_delta2 (dw_attr_ref a)
4566 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4567 return a->dw_attr_val.v.val_vms_delta.lbl2;
4570 static inline const char *
4571 AT_lbl (dw_attr_ref a)
4573 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4574 || AT_class (a) == dw_val_class_lineptr
4575 || AT_class (a) == dw_val_class_macptr
4576 || AT_class (a) == dw_val_class_high_pc));
4577 return a->dw_attr_val.v.val_lbl_id;
4580 /* Get the attribute of type attr_kind. */
4582 static dw_attr_ref
4583 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4585 dw_attr_ref a;
4586 unsigned ix;
4587 dw_die_ref spec = NULL;
4589 if (! die)
4590 return NULL;
4592 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4593 if (a->dw_attr == attr_kind)
4594 return a;
4595 else if (a->dw_attr == DW_AT_specification
4596 || a->dw_attr == DW_AT_abstract_origin)
4597 spec = AT_ref (a);
4599 if (spec)
4600 return get_AT (spec, attr_kind);
4602 return NULL;
4605 /* Returns the parent of the declaration of DIE. */
4607 static dw_die_ref
4608 get_die_parent (dw_die_ref die)
4610 dw_die_ref t;
4612 if (!die)
4613 return NULL;
4615 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4616 || (t = get_AT_ref (die, DW_AT_specification)))
4617 die = t;
4619 return die->die_parent;
4622 /* Return the "low pc" attribute value, typically associated with a subprogram
4623 DIE. Return null if the "low pc" attribute is either not present, or if it
4624 cannot be represented as an assembler label identifier. */
4626 static inline const char *
4627 get_AT_low_pc (dw_die_ref die)
4629 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4631 return a ? AT_lbl (a) : NULL;
4634 /* Return the "high pc" attribute value, typically associated with a subprogram
4635 DIE. Return null if the "high pc" attribute is either not present, or if it
4636 cannot be represented as an assembler label identifier. */
4638 static inline const char *
4639 get_AT_hi_pc (dw_die_ref die)
4641 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4643 return a ? AT_lbl (a) : NULL;
4646 /* Return the value of the string attribute designated by ATTR_KIND, or
4647 NULL if it is not present. */
4649 static inline const char *
4650 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4652 dw_attr_ref a = get_AT (die, attr_kind);
4654 return a ? AT_string (a) : NULL;
4657 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4658 if it is not present. */
4660 static inline int
4661 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4663 dw_attr_ref a = get_AT (die, attr_kind);
4665 return a ? AT_flag (a) : 0;
4668 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4669 if it is not present. */
4671 static inline unsigned
4672 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4674 dw_attr_ref a = get_AT (die, attr_kind);
4676 return a ? AT_unsigned (a) : 0;
4679 static inline dw_die_ref
4680 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4682 dw_attr_ref a = get_AT (die, attr_kind);
4684 return a ? AT_ref (a) : NULL;
4687 static inline struct dwarf_file_data *
4688 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4690 dw_attr_ref a = get_AT (die, attr_kind);
4692 return a ? AT_file (a) : NULL;
4695 /* Return TRUE if the language is C++. */
4697 static inline bool
4698 is_cxx (void)
4700 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4702 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4703 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4706 /* Return TRUE if the language is Java. */
4708 static inline bool
4709 is_java (void)
4711 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4713 return lang == DW_LANG_Java;
4716 /* Return TRUE if the language is Fortran. */
4718 static inline bool
4719 is_fortran (void)
4721 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4723 return (lang == DW_LANG_Fortran77
4724 || lang == DW_LANG_Fortran90
4725 || lang == DW_LANG_Fortran95);
4728 /* Return TRUE if the language is Ada. */
4730 static inline bool
4731 is_ada (void)
4733 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4735 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4738 /* Remove the specified attribute if present. */
4740 static void
4741 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4743 dw_attr_ref a;
4744 unsigned ix;
4746 if (! die)
4747 return;
4749 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4750 if (a->dw_attr == attr_kind)
4752 if (AT_class (a) == dw_val_class_str)
4753 if (a->dw_attr_val.v.val_str->refcount)
4754 a->dw_attr_val.v.val_str->refcount--;
4756 /* vec::ordered_remove should help reduce the number of abbrevs
4757 that are needed. */
4758 die->die_attr->ordered_remove (ix);
4759 return;
4763 /* Remove CHILD from its parent. PREV must have the property that
4764 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4766 static void
4767 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4769 gcc_assert (child->die_parent == prev->die_parent);
4770 gcc_assert (prev->die_sib == child);
4771 if (prev == child)
4773 gcc_assert (child->die_parent->die_child == child);
4774 prev = NULL;
4776 else
4777 prev->die_sib = child->die_sib;
4778 if (child->die_parent->die_child == child)
4779 child->die_parent->die_child = prev;
4782 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4783 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4785 static void
4786 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4788 dw_die_ref parent = old_child->die_parent;
4790 gcc_assert (parent == prev->die_parent);
4791 gcc_assert (prev->die_sib == old_child);
4793 new_child->die_parent = parent;
4794 if (prev == old_child)
4796 gcc_assert (parent->die_child == old_child);
4797 new_child->die_sib = new_child;
4799 else
4801 prev->die_sib = new_child;
4802 new_child->die_sib = old_child->die_sib;
4804 if (old_child->die_parent->die_child == old_child)
4805 old_child->die_parent->die_child = new_child;
4808 /* Move all children from OLD_PARENT to NEW_PARENT. */
4810 static void
4811 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4813 dw_die_ref c;
4814 new_parent->die_child = old_parent->die_child;
4815 old_parent->die_child = NULL;
4816 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4819 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4820 matches TAG. */
4822 static void
4823 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4825 dw_die_ref c;
4827 c = die->die_child;
4828 if (c) do {
4829 dw_die_ref prev = c;
4830 c = c->die_sib;
4831 while (c->die_tag == tag)
4833 remove_child_with_prev (c, prev);
4834 /* Might have removed every child. */
4835 if (c == c->die_sib)
4836 return;
4837 c = c->die_sib;
4839 } while (c != die->die_child);
4842 /* Add a CHILD_DIE as the last child of DIE. */
4844 static void
4845 add_child_die (dw_die_ref die, dw_die_ref child_die)
4847 /* FIXME this should probably be an assert. */
4848 if (! die || ! child_die)
4849 return;
4850 gcc_assert (die != child_die);
4852 child_die->die_parent = die;
4853 if (die->die_child)
4855 child_die->die_sib = die->die_child->die_sib;
4856 die->die_child->die_sib = child_die;
4858 else
4859 child_die->die_sib = child_die;
4860 die->die_child = child_die;
4863 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4864 is the specification, to the end of PARENT's list of children.
4865 This is done by removing and re-adding it. */
4867 static void
4868 splice_child_die (dw_die_ref parent, dw_die_ref child)
4870 dw_die_ref p;
4872 /* We want the declaration DIE from inside the class, not the
4873 specification DIE at toplevel. */
4874 if (child->die_parent != parent)
4876 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4878 if (tmp)
4879 child = tmp;
4882 gcc_assert (child->die_parent == parent
4883 || (child->die_parent
4884 == get_AT_ref (parent, DW_AT_specification)));
4886 for (p = child->die_parent->die_child; ; p = p->die_sib)
4887 if (p->die_sib == child)
4889 remove_child_with_prev (child, p);
4890 break;
4893 add_child_die (parent, child);
4896 /* Return a pointer to a newly created DIE node. */
4898 static inline dw_die_ref
4899 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4901 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4903 die->die_tag = tag_value;
4905 if (parent_die != NULL)
4906 add_child_die (parent_die, die);
4907 else
4909 limbo_die_node *limbo_node;
4911 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4912 limbo_node->die = die;
4913 limbo_node->created_for = t;
4914 limbo_node->next = limbo_die_list;
4915 limbo_die_list = limbo_node;
4918 return die;
4921 /* Return the DIE associated with the given type specifier. */
4923 static inline dw_die_ref
4924 lookup_type_die (tree type)
4926 return TYPE_SYMTAB_DIE (type);
4929 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4930 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4931 anonymous type instead the one of the naming typedef. */
4933 static inline dw_die_ref
4934 strip_naming_typedef (tree type, dw_die_ref type_die)
4936 if (type
4937 && TREE_CODE (type) == RECORD_TYPE
4938 && type_die
4939 && type_die->die_tag == DW_TAG_typedef
4940 && is_naming_typedef_decl (TYPE_NAME (type)))
4941 type_die = get_AT_ref (type_die, DW_AT_type);
4942 return type_die;
4945 /* Like lookup_type_die, but if type is an anonymous type named by a
4946 typedef[1], return the DIE of the anonymous type instead the one of
4947 the naming typedef. This is because in gen_typedef_die, we did
4948 equate the anonymous struct named by the typedef with the DIE of
4949 the naming typedef. So by default, lookup_type_die on an anonymous
4950 struct yields the DIE of the naming typedef.
4952 [1]: Read the comment of is_naming_typedef_decl to learn about what
4953 a naming typedef is. */
4955 static inline dw_die_ref
4956 lookup_type_die_strip_naming_typedef (tree type)
4958 dw_die_ref die = lookup_type_die (type);
4959 return strip_naming_typedef (type, die);
4962 /* Equate a DIE to a given type specifier. */
4964 static inline void
4965 equate_type_number_to_die (tree type, dw_die_ref type_die)
4967 TYPE_SYMTAB_DIE (type) = type_die;
4970 /* Returns a hash value for X (which really is a die_struct). */
4972 inline hashval_t
4973 decl_die_hasher::hash (die_node *x)
4975 return (hashval_t) x->decl_id;
4978 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4980 inline bool
4981 decl_die_hasher::equal (die_node *x, tree y)
4983 return (x->decl_id == DECL_UID (y));
4986 /* Return the DIE associated with a given declaration. */
4988 static inline dw_die_ref
4989 lookup_decl_die (tree decl)
4991 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
4994 /* Returns a hash value for X (which really is a var_loc_list). */
4996 inline hashval_t
4997 decl_loc_hasher::hash (var_loc_list *x)
4999 return (hashval_t) x->decl_id;
5002 /* Return nonzero if decl_id of var_loc_list X is the same as
5003 UID of decl *Y. */
5005 inline bool
5006 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5008 return (x->decl_id == DECL_UID (y));
5011 /* Return the var_loc list associated with a given declaration. */
5013 static inline var_loc_list *
5014 lookup_decl_loc (const_tree decl)
5016 if (!decl_loc_table)
5017 return NULL;
5018 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5021 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5023 inline hashval_t
5024 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5026 return (hashval_t) x->decl_id;
5029 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5030 UID of decl *Y. */
5032 inline bool
5033 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5035 return (x->decl_id == DECL_UID (y));
5038 /* Equate a DIE to a particular declaration. */
5040 static void
5041 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5043 unsigned int decl_id = DECL_UID (decl);
5045 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5046 decl_die->decl_id = decl_id;
5049 /* Return how many bits covers PIECE EXPR_LIST. */
5051 static int
5052 decl_piece_bitsize (rtx piece)
5054 int ret = (int) GET_MODE (piece);
5055 if (ret)
5056 return ret;
5057 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5058 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5059 return INTVAL (XEXP (XEXP (piece, 0), 0));
5062 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5064 static rtx *
5065 decl_piece_varloc_ptr (rtx piece)
5067 if ((int) GET_MODE (piece))
5068 return &XEXP (piece, 0);
5069 else
5070 return &XEXP (XEXP (piece, 0), 1);
5073 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5074 Next is the chain of following piece nodes. */
5076 static rtx_expr_list *
5077 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5079 if (bitsize <= (int) MAX_MACHINE_MODE)
5080 return alloc_EXPR_LIST (bitsize, loc_note, next);
5081 else
5082 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5083 GEN_INT (bitsize),
5084 loc_note), next);
5087 /* Return rtx that should be stored into loc field for
5088 LOC_NOTE and BITPOS/BITSIZE. */
5090 static rtx
5091 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5092 HOST_WIDE_INT bitsize)
5094 if (bitsize != -1)
5096 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5097 if (bitpos != 0)
5098 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5100 return loc_note;
5103 /* This function either modifies location piece list *DEST in
5104 place (if SRC and INNER is NULL), or copies location piece list
5105 *SRC to *DEST while modifying it. Location BITPOS is modified
5106 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5107 not copied and if needed some padding around it is added.
5108 When modifying in place, DEST should point to EXPR_LIST where
5109 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5110 to the start of the whole list and INNER points to the EXPR_LIST
5111 where earlier pieces cover PIECE_BITPOS bits. */
5113 static void
5114 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5115 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5116 HOST_WIDE_INT bitsize, rtx loc_note)
5118 int diff;
5119 bool copy = inner != NULL;
5121 if (copy)
5123 /* First copy all nodes preceding the current bitpos. */
5124 while (src != inner)
5126 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5127 decl_piece_bitsize (*src), NULL_RTX);
5128 dest = &XEXP (*dest, 1);
5129 src = &XEXP (*src, 1);
5132 /* Add padding if needed. */
5133 if (bitpos != piece_bitpos)
5135 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5136 copy ? NULL_RTX : *dest);
5137 dest = &XEXP (*dest, 1);
5139 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5141 gcc_assert (!copy);
5142 /* A piece with correct bitpos and bitsize already exist,
5143 just update the location for it and return. */
5144 *decl_piece_varloc_ptr (*dest) = loc_note;
5145 return;
5147 /* Add the piece that changed. */
5148 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5149 dest = &XEXP (*dest, 1);
5150 /* Skip over pieces that overlap it. */
5151 diff = bitpos - piece_bitpos + bitsize;
5152 if (!copy)
5153 src = dest;
5154 while (diff > 0 && *src)
5156 rtx piece = *src;
5157 diff -= decl_piece_bitsize (piece);
5158 if (copy)
5159 src = &XEXP (piece, 1);
5160 else
5162 *src = XEXP (piece, 1);
5163 free_EXPR_LIST_node (piece);
5166 /* Add padding if needed. */
5167 if (diff < 0 && *src)
5169 if (!copy)
5170 dest = src;
5171 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5172 dest = &XEXP (*dest, 1);
5174 if (!copy)
5175 return;
5176 /* Finally copy all nodes following it. */
5177 while (*src)
5179 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5180 decl_piece_bitsize (*src), NULL_RTX);
5181 dest = &XEXP (*dest, 1);
5182 src = &XEXP (*src, 1);
5186 /* Add a variable location node to the linked list for DECL. */
5188 static struct var_loc_node *
5189 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5191 unsigned int decl_id;
5192 var_loc_list *temp;
5193 struct var_loc_node *loc = NULL;
5194 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5196 if (TREE_CODE (decl) == VAR_DECL
5197 && DECL_HAS_DEBUG_EXPR_P (decl))
5199 tree realdecl = DECL_DEBUG_EXPR (decl);
5200 if (handled_component_p (realdecl)
5201 || (TREE_CODE (realdecl) == MEM_REF
5202 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5204 HOST_WIDE_INT maxsize;
5205 tree innerdecl;
5206 innerdecl
5207 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5208 if (!DECL_P (innerdecl)
5209 || DECL_IGNORED_P (innerdecl)
5210 || TREE_STATIC (innerdecl)
5211 || bitsize <= 0
5212 || bitpos + bitsize > 256
5213 || bitsize != maxsize)
5214 return NULL;
5215 decl = innerdecl;
5219 decl_id = DECL_UID (decl);
5220 var_loc_list **slot
5221 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5222 if (*slot == NULL)
5224 temp = ggc_cleared_alloc<var_loc_list> ();
5225 temp->decl_id = decl_id;
5226 *slot = temp;
5228 else
5229 temp = *slot;
5231 /* For PARM_DECLs try to keep around the original incoming value,
5232 even if that means we'll emit a zero-range .debug_loc entry. */
5233 if (temp->last
5234 && temp->first == temp->last
5235 && TREE_CODE (decl) == PARM_DECL
5236 && NOTE_P (temp->first->loc)
5237 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5238 && DECL_INCOMING_RTL (decl)
5239 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5240 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5241 == GET_CODE (DECL_INCOMING_RTL (decl))
5242 && prev_real_insn (temp->first->loc) == NULL_RTX
5243 && (bitsize != -1
5244 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5245 NOTE_VAR_LOCATION_LOC (loc_note))
5246 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5247 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5249 loc = ggc_cleared_alloc<var_loc_node> ();
5250 temp->first->next = loc;
5251 temp->last = loc;
5252 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5254 else if (temp->last)
5256 struct var_loc_node *last = temp->last, *unused = NULL;
5257 rtx *piece_loc = NULL, last_loc_note;
5258 int piece_bitpos = 0;
5259 if (last->next)
5261 last = last->next;
5262 gcc_assert (last->next == NULL);
5264 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5266 piece_loc = &last->loc;
5269 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5270 if (piece_bitpos + cur_bitsize > bitpos)
5271 break;
5272 piece_bitpos += cur_bitsize;
5273 piece_loc = &XEXP (*piece_loc, 1);
5275 while (*piece_loc);
5277 /* TEMP->LAST here is either pointer to the last but one or
5278 last element in the chained list, LAST is pointer to the
5279 last element. */
5280 if (label && strcmp (last->label, label) == 0)
5282 /* For SRA optimized variables if there weren't any real
5283 insns since last note, just modify the last node. */
5284 if (piece_loc != NULL)
5286 adjust_piece_list (piece_loc, NULL, NULL,
5287 bitpos, piece_bitpos, bitsize, loc_note);
5288 return NULL;
5290 /* If the last note doesn't cover any instructions, remove it. */
5291 if (temp->last != last)
5293 temp->last->next = NULL;
5294 unused = last;
5295 last = temp->last;
5296 gcc_assert (strcmp (last->label, label) != 0);
5298 else
5300 gcc_assert (temp->first == temp->last
5301 || (temp->first->next == temp->last
5302 && TREE_CODE (decl) == PARM_DECL));
5303 memset (temp->last, '\0', sizeof (*temp->last));
5304 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5305 return temp->last;
5308 if (bitsize == -1 && NOTE_P (last->loc))
5309 last_loc_note = last->loc;
5310 else if (piece_loc != NULL
5311 && *piece_loc != NULL_RTX
5312 && piece_bitpos == bitpos
5313 && decl_piece_bitsize (*piece_loc) == bitsize)
5314 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5315 else
5316 last_loc_note = NULL_RTX;
5317 /* If the current location is the same as the end of the list,
5318 and either both or neither of the locations is uninitialized,
5319 we have nothing to do. */
5320 if (last_loc_note == NULL_RTX
5321 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5322 NOTE_VAR_LOCATION_LOC (loc_note)))
5323 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5324 != NOTE_VAR_LOCATION_STATUS (loc_note))
5325 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5326 == VAR_INIT_STATUS_UNINITIALIZED)
5327 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5328 == VAR_INIT_STATUS_UNINITIALIZED))))
5330 /* Add LOC to the end of list and update LAST. If the last
5331 element of the list has been removed above, reuse its
5332 memory for the new node, otherwise allocate a new one. */
5333 if (unused)
5335 loc = unused;
5336 memset (loc, '\0', sizeof (*loc));
5338 else
5339 loc = ggc_cleared_alloc<var_loc_node> ();
5340 if (bitsize == -1 || piece_loc == NULL)
5341 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5342 else
5343 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5344 bitpos, piece_bitpos, bitsize, loc_note);
5345 last->next = loc;
5346 /* Ensure TEMP->LAST will point either to the new last but one
5347 element of the chain, or to the last element in it. */
5348 if (last != temp->last)
5349 temp->last = last;
5351 else if (unused)
5352 ggc_free (unused);
5354 else
5356 loc = ggc_cleared_alloc<var_loc_node> ();
5357 temp->first = loc;
5358 temp->last = loc;
5359 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5361 return loc;
5364 /* Keep track of the number of spaces used to indent the
5365 output of the debugging routines that print the structure of
5366 the DIE internal representation. */
5367 static int print_indent;
5369 /* Indent the line the number of spaces given by print_indent. */
5371 static inline void
5372 print_spaces (FILE *outfile)
5374 fprintf (outfile, "%*s", print_indent, "");
5377 /* Print a type signature in hex. */
5379 static inline void
5380 print_signature (FILE *outfile, char *sig)
5382 int i;
5384 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5385 fprintf (outfile, "%02x", sig[i] & 0xff);
5388 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5390 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5391 RECURSE, output location descriptor operations. */
5393 static void
5394 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5396 switch (val->val_class)
5398 case dw_val_class_addr:
5399 fprintf (outfile, "address");
5400 break;
5401 case dw_val_class_offset:
5402 fprintf (outfile, "offset");
5403 break;
5404 case dw_val_class_loc:
5405 fprintf (outfile, "location descriptor");
5406 if (val->v.val_loc == NULL)
5407 fprintf (outfile, " -> <null>\n");
5408 else if (recurse)
5410 fprintf (outfile, ":\n");
5411 print_indent += 4;
5412 print_loc_descr (val->v.val_loc, outfile);
5413 print_indent -= 4;
5415 else
5416 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5417 break;
5418 case dw_val_class_loc_list:
5419 fprintf (outfile, "location list -> label:%s",
5420 val->v.val_loc_list->ll_symbol);
5421 break;
5422 case dw_val_class_range_list:
5423 fprintf (outfile, "range list");
5424 break;
5425 case dw_val_class_const:
5426 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5427 break;
5428 case dw_val_class_unsigned_const:
5429 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5430 break;
5431 case dw_val_class_const_double:
5432 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5433 HOST_WIDE_INT_PRINT_UNSIGNED")",
5434 val->v.val_double.high,
5435 val->v.val_double.low);
5436 break;
5437 case dw_val_class_wide_int:
5439 int i = val->v.val_wide->get_len ();
5440 fprintf (outfile, "constant (");
5441 gcc_assert (i > 0);
5442 if (val->v.val_wide->elt (i - 1) == 0)
5443 fprintf (outfile, "0x");
5444 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5445 val->v.val_wide->elt (--i));
5446 while (--i >= 0)
5447 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5448 val->v.val_wide->elt (i));
5449 fprintf (outfile, ")");
5450 break;
5452 case dw_val_class_vec:
5453 fprintf (outfile, "floating-point or vector constant");
5454 break;
5455 case dw_val_class_flag:
5456 fprintf (outfile, "%u", val->v.val_flag);
5457 break;
5458 case dw_val_class_die_ref:
5459 if (val->v.val_die_ref.die != NULL)
5461 dw_die_ref die = val->v.val_die_ref.die;
5463 if (die->comdat_type_p)
5465 fprintf (outfile, "die -> signature: ");
5466 print_signature (outfile,
5467 die->die_id.die_type_node->signature);
5469 else if (die->die_id.die_symbol)
5470 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5471 else
5472 fprintf (outfile, "die -> %ld", die->die_offset);
5473 fprintf (outfile, " (%p)", (void *) die);
5475 else
5476 fprintf (outfile, "die -> <null>");
5477 break;
5478 case dw_val_class_vms_delta:
5479 fprintf (outfile, "delta: @slotcount(%s-%s)",
5480 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5481 break;
5482 case dw_val_class_lbl_id:
5483 case dw_val_class_lineptr:
5484 case dw_val_class_macptr:
5485 case dw_val_class_high_pc:
5486 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5487 break;
5488 case dw_val_class_str:
5489 if (val->v.val_str->str != NULL)
5490 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5491 else
5492 fprintf (outfile, "<null>");
5493 break;
5494 case dw_val_class_file:
5495 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5496 val->v.val_file->emitted_number);
5497 break;
5498 case dw_val_class_data8:
5500 int i;
5502 for (i = 0; i < 8; i++)
5503 fprintf (outfile, "%02x", val->v.val_data8[i]);
5504 break;
5506 default:
5507 break;
5511 /* Likewise, for a DIE attribute. */
5513 static void
5514 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5516 print_dw_val (&a->dw_attr_val, recurse, outfile);
5520 /* Print the list of operands in the LOC location description to OUTFILE. This
5521 routine is a debugging aid only. */
5523 static void
5524 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5526 dw_loc_descr_ref l = loc;
5528 if (loc == NULL)
5530 print_spaces (outfile);
5531 fprintf (outfile, "<null>\n");
5532 return;
5535 for (l = loc; l != NULL; l = l->dw_loc_next)
5537 print_spaces (outfile);
5538 fprintf (outfile, "(%p) %s",
5539 (void *) l,
5540 dwarf_stack_op_name (l->dw_loc_opc));
5541 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5543 fprintf (outfile, " ");
5544 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5546 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5548 fprintf (outfile, ", ");
5549 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5551 fprintf (outfile, "\n");
5555 /* Print the information associated with a given DIE, and its children.
5556 This routine is a debugging aid only. */
5558 static void
5559 print_die (dw_die_ref die, FILE *outfile)
5561 dw_attr_ref a;
5562 dw_die_ref c;
5563 unsigned ix;
5565 print_spaces (outfile);
5566 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5567 die->die_offset, dwarf_tag_name (die->die_tag),
5568 (void*) die);
5569 print_spaces (outfile);
5570 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5571 fprintf (outfile, " offset: %ld", die->die_offset);
5572 fprintf (outfile, " mark: %d\n", die->die_mark);
5574 if (die->comdat_type_p)
5576 print_spaces (outfile);
5577 fprintf (outfile, " signature: ");
5578 print_signature (outfile, die->die_id.die_type_node->signature);
5579 fprintf (outfile, "\n");
5582 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5584 print_spaces (outfile);
5585 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5587 print_attribute (a, true, outfile);
5588 fprintf (outfile, "\n");
5591 if (die->die_child != NULL)
5593 print_indent += 4;
5594 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5595 print_indent -= 4;
5597 if (print_indent == 0)
5598 fprintf (outfile, "\n");
5601 /* Print the list of operations in the LOC location description. */
5603 DEBUG_FUNCTION void
5604 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5606 print_loc_descr (loc, stderr);
5609 /* Print the information collected for a given DIE. */
5611 DEBUG_FUNCTION void
5612 debug_dwarf_die (dw_die_ref die)
5614 print_die (die, stderr);
5617 DEBUG_FUNCTION void
5618 debug (die_struct &ref)
5620 print_die (&ref, stderr);
5623 DEBUG_FUNCTION void
5624 debug (die_struct *ptr)
5626 if (ptr)
5627 debug (*ptr);
5628 else
5629 fprintf (stderr, "<nil>\n");
5633 /* Print all DWARF information collected for the compilation unit.
5634 This routine is a debugging aid only. */
5636 DEBUG_FUNCTION void
5637 debug_dwarf (void)
5639 print_indent = 0;
5640 print_die (comp_unit_die (), stderr);
5643 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5644 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5645 DIE that marks the start of the DIEs for this include file. */
5647 static dw_die_ref
5648 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5650 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5651 dw_die_ref new_unit = gen_compile_unit_die (filename);
5653 new_unit->die_sib = old_unit;
5654 return new_unit;
5657 /* Close an include-file CU and reopen the enclosing one. */
5659 static dw_die_ref
5660 pop_compile_unit (dw_die_ref old_unit)
5662 dw_die_ref new_unit = old_unit->die_sib;
5664 old_unit->die_sib = NULL;
5665 return new_unit;
5668 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5669 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5670 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5672 /* Calculate the checksum of a location expression. */
5674 static inline void
5675 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5677 int tem;
5678 inchash::hash hstate;
5679 hashval_t hash;
5681 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5682 CHECKSUM (tem);
5683 hash_loc_operands (loc, hstate);
5684 hash = hstate.end();
5685 CHECKSUM (hash);
5688 /* Calculate the checksum of an attribute. */
5690 static void
5691 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5693 dw_loc_descr_ref loc;
5694 rtx r;
5696 CHECKSUM (at->dw_attr);
5698 /* We don't care that this was compiled with a different compiler
5699 snapshot; if the output is the same, that's what matters. */
5700 if (at->dw_attr == DW_AT_producer)
5701 return;
5703 switch (AT_class (at))
5705 case dw_val_class_const:
5706 CHECKSUM (at->dw_attr_val.v.val_int);
5707 break;
5708 case dw_val_class_unsigned_const:
5709 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5710 break;
5711 case dw_val_class_const_double:
5712 CHECKSUM (at->dw_attr_val.v.val_double);
5713 break;
5714 case dw_val_class_wide_int:
5715 CHECKSUM (*at->dw_attr_val.v.val_wide);
5716 break;
5717 case dw_val_class_vec:
5718 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5719 (at->dw_attr_val.v.val_vec.length
5720 * at->dw_attr_val.v.val_vec.elt_size));
5721 break;
5722 case dw_val_class_flag:
5723 CHECKSUM (at->dw_attr_val.v.val_flag);
5724 break;
5725 case dw_val_class_str:
5726 CHECKSUM_STRING (AT_string (at));
5727 break;
5729 case dw_val_class_addr:
5730 r = AT_addr (at);
5731 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5732 CHECKSUM_STRING (XSTR (r, 0));
5733 break;
5735 case dw_val_class_offset:
5736 CHECKSUM (at->dw_attr_val.v.val_offset);
5737 break;
5739 case dw_val_class_loc:
5740 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5741 loc_checksum (loc, ctx);
5742 break;
5744 case dw_val_class_die_ref:
5745 die_checksum (AT_ref (at), ctx, mark);
5746 break;
5748 case dw_val_class_fde_ref:
5749 case dw_val_class_vms_delta:
5750 case dw_val_class_lbl_id:
5751 case dw_val_class_lineptr:
5752 case dw_val_class_macptr:
5753 case dw_val_class_high_pc:
5754 break;
5756 case dw_val_class_file:
5757 CHECKSUM_STRING (AT_file (at)->filename);
5758 break;
5760 case dw_val_class_data8:
5761 CHECKSUM (at->dw_attr_val.v.val_data8);
5762 break;
5764 default:
5765 break;
5769 /* Calculate the checksum of a DIE. */
5771 static void
5772 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5774 dw_die_ref c;
5775 dw_attr_ref a;
5776 unsigned ix;
5778 /* To avoid infinite recursion. */
5779 if (die->die_mark)
5781 CHECKSUM (die->die_mark);
5782 return;
5784 die->die_mark = ++(*mark);
5786 CHECKSUM (die->die_tag);
5788 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5789 attr_checksum (a, ctx, mark);
5791 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5794 #undef CHECKSUM
5795 #undef CHECKSUM_BLOCK
5796 #undef CHECKSUM_STRING
5798 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5799 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5800 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5801 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5802 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5803 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5804 #define CHECKSUM_ATTR(FOO) \
5805 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5807 /* Calculate the checksum of a number in signed LEB128 format. */
5809 static void
5810 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5812 unsigned char byte;
5813 bool more;
5815 while (1)
5817 byte = (value & 0x7f);
5818 value >>= 7;
5819 more = !((value == 0 && (byte & 0x40) == 0)
5820 || (value == -1 && (byte & 0x40) != 0));
5821 if (more)
5822 byte |= 0x80;
5823 CHECKSUM (byte);
5824 if (!more)
5825 break;
5829 /* Calculate the checksum of a number in unsigned LEB128 format. */
5831 static void
5832 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5834 while (1)
5836 unsigned char byte = (value & 0x7f);
5837 value >>= 7;
5838 if (value != 0)
5839 /* More bytes to follow. */
5840 byte |= 0x80;
5841 CHECKSUM (byte);
5842 if (value == 0)
5843 break;
5847 /* Checksum the context of the DIE. This adds the names of any
5848 surrounding namespaces or structures to the checksum. */
5850 static void
5851 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5853 const char *name;
5854 dw_die_ref spec;
5855 int tag = die->die_tag;
5857 if (tag != DW_TAG_namespace
5858 && tag != DW_TAG_structure_type
5859 && tag != DW_TAG_class_type)
5860 return;
5862 name = get_AT_string (die, DW_AT_name);
5864 spec = get_AT_ref (die, DW_AT_specification);
5865 if (spec != NULL)
5866 die = spec;
5868 if (die->die_parent != NULL)
5869 checksum_die_context (die->die_parent, ctx);
5871 CHECKSUM_ULEB128 ('C');
5872 CHECKSUM_ULEB128 (tag);
5873 if (name != NULL)
5874 CHECKSUM_STRING (name);
5877 /* Calculate the checksum of a location expression. */
5879 static inline void
5880 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5882 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5883 were emitted as a DW_FORM_sdata instead of a location expression. */
5884 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5886 CHECKSUM_ULEB128 (DW_FORM_sdata);
5887 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5888 return;
5891 /* Otherwise, just checksum the raw location expression. */
5892 while (loc != NULL)
5894 inchash::hash hstate;
5895 hashval_t hash;
5897 CHECKSUM_ULEB128 (loc->dtprel);
5898 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5899 hash_loc_operands (loc, hstate);
5900 hash = hstate.end ();
5901 CHECKSUM (hash);
5902 loc = loc->dw_loc_next;
5906 /* Calculate the checksum of an attribute. */
5908 static void
5909 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5910 struct md5_ctx *ctx, int *mark)
5912 dw_loc_descr_ref loc;
5913 rtx r;
5915 if (AT_class (at) == dw_val_class_die_ref)
5917 dw_die_ref target_die = AT_ref (at);
5919 /* For pointer and reference types, we checksum only the (qualified)
5920 name of the target type (if there is a name). For friend entries,
5921 we checksum only the (qualified) name of the target type or function.
5922 This allows the checksum to remain the same whether the target type
5923 is complete or not. */
5924 if ((at->dw_attr == DW_AT_type
5925 && (tag == DW_TAG_pointer_type
5926 || tag == DW_TAG_reference_type
5927 || tag == DW_TAG_rvalue_reference_type
5928 || tag == DW_TAG_ptr_to_member_type))
5929 || (at->dw_attr == DW_AT_friend
5930 && tag == DW_TAG_friend))
5932 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5934 if (name_attr != NULL)
5936 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5938 if (decl == NULL)
5939 decl = target_die;
5940 CHECKSUM_ULEB128 ('N');
5941 CHECKSUM_ULEB128 (at->dw_attr);
5942 if (decl->die_parent != NULL)
5943 checksum_die_context (decl->die_parent, ctx);
5944 CHECKSUM_ULEB128 ('E');
5945 CHECKSUM_STRING (AT_string (name_attr));
5946 return;
5950 /* For all other references to another DIE, we check to see if the
5951 target DIE has already been visited. If it has, we emit a
5952 backward reference; if not, we descend recursively. */
5953 if (target_die->die_mark > 0)
5955 CHECKSUM_ULEB128 ('R');
5956 CHECKSUM_ULEB128 (at->dw_attr);
5957 CHECKSUM_ULEB128 (target_die->die_mark);
5959 else
5961 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5963 if (decl == NULL)
5964 decl = target_die;
5965 target_die->die_mark = ++(*mark);
5966 CHECKSUM_ULEB128 ('T');
5967 CHECKSUM_ULEB128 (at->dw_attr);
5968 if (decl->die_parent != NULL)
5969 checksum_die_context (decl->die_parent, ctx);
5970 die_checksum_ordered (target_die, ctx, mark);
5972 return;
5975 CHECKSUM_ULEB128 ('A');
5976 CHECKSUM_ULEB128 (at->dw_attr);
5978 switch (AT_class (at))
5980 case dw_val_class_const:
5981 CHECKSUM_ULEB128 (DW_FORM_sdata);
5982 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5983 break;
5985 case dw_val_class_unsigned_const:
5986 CHECKSUM_ULEB128 (DW_FORM_sdata);
5987 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5988 break;
5990 case dw_val_class_const_double:
5991 CHECKSUM_ULEB128 (DW_FORM_block);
5992 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5993 CHECKSUM (at->dw_attr_val.v.val_double);
5994 break;
5996 case dw_val_class_wide_int:
5997 CHECKSUM_ULEB128 (DW_FORM_block);
5998 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5999 CHECKSUM (*at->dw_attr_val.v.val_wide);
6000 break;
6002 case dw_val_class_vec:
6003 CHECKSUM_ULEB128 (DW_FORM_block);
6004 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6005 * at->dw_attr_val.v.val_vec.elt_size);
6006 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6007 (at->dw_attr_val.v.val_vec.length
6008 * at->dw_attr_val.v.val_vec.elt_size));
6009 break;
6011 case dw_val_class_flag:
6012 CHECKSUM_ULEB128 (DW_FORM_flag);
6013 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6014 break;
6016 case dw_val_class_str:
6017 CHECKSUM_ULEB128 (DW_FORM_string);
6018 CHECKSUM_STRING (AT_string (at));
6019 break;
6021 case dw_val_class_addr:
6022 r = AT_addr (at);
6023 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6024 CHECKSUM_ULEB128 (DW_FORM_string);
6025 CHECKSUM_STRING (XSTR (r, 0));
6026 break;
6028 case dw_val_class_offset:
6029 CHECKSUM_ULEB128 (DW_FORM_sdata);
6030 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6031 break;
6033 case dw_val_class_loc:
6034 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6035 loc_checksum_ordered (loc, ctx);
6036 break;
6038 case dw_val_class_fde_ref:
6039 case dw_val_class_lbl_id:
6040 case dw_val_class_lineptr:
6041 case dw_val_class_macptr:
6042 case dw_val_class_high_pc:
6043 break;
6045 case dw_val_class_file:
6046 CHECKSUM_ULEB128 (DW_FORM_string);
6047 CHECKSUM_STRING (AT_file (at)->filename);
6048 break;
6050 case dw_val_class_data8:
6051 CHECKSUM (at->dw_attr_val.v.val_data8);
6052 break;
6054 default:
6055 break;
6059 struct checksum_attributes
6061 dw_attr_ref at_name;
6062 dw_attr_ref at_type;
6063 dw_attr_ref at_friend;
6064 dw_attr_ref at_accessibility;
6065 dw_attr_ref at_address_class;
6066 dw_attr_ref at_allocated;
6067 dw_attr_ref at_artificial;
6068 dw_attr_ref at_associated;
6069 dw_attr_ref at_binary_scale;
6070 dw_attr_ref at_bit_offset;
6071 dw_attr_ref at_bit_size;
6072 dw_attr_ref at_bit_stride;
6073 dw_attr_ref at_byte_size;
6074 dw_attr_ref at_byte_stride;
6075 dw_attr_ref at_const_value;
6076 dw_attr_ref at_containing_type;
6077 dw_attr_ref at_count;
6078 dw_attr_ref at_data_location;
6079 dw_attr_ref at_data_member_location;
6080 dw_attr_ref at_decimal_scale;
6081 dw_attr_ref at_decimal_sign;
6082 dw_attr_ref at_default_value;
6083 dw_attr_ref at_digit_count;
6084 dw_attr_ref at_discr;
6085 dw_attr_ref at_discr_list;
6086 dw_attr_ref at_discr_value;
6087 dw_attr_ref at_encoding;
6088 dw_attr_ref at_endianity;
6089 dw_attr_ref at_explicit;
6090 dw_attr_ref at_is_optional;
6091 dw_attr_ref at_location;
6092 dw_attr_ref at_lower_bound;
6093 dw_attr_ref at_mutable;
6094 dw_attr_ref at_ordering;
6095 dw_attr_ref at_picture_string;
6096 dw_attr_ref at_prototyped;
6097 dw_attr_ref at_small;
6098 dw_attr_ref at_segment;
6099 dw_attr_ref at_string_length;
6100 dw_attr_ref at_threads_scaled;
6101 dw_attr_ref at_upper_bound;
6102 dw_attr_ref at_use_location;
6103 dw_attr_ref at_use_UTF8;
6104 dw_attr_ref at_variable_parameter;
6105 dw_attr_ref at_virtuality;
6106 dw_attr_ref at_visibility;
6107 dw_attr_ref at_vtable_elem_location;
6110 /* Collect the attributes that we will want to use for the checksum. */
6112 static void
6113 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6115 dw_attr_ref a;
6116 unsigned ix;
6118 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6120 switch (a->dw_attr)
6122 case DW_AT_name:
6123 attrs->at_name = a;
6124 break;
6125 case DW_AT_type:
6126 attrs->at_type = a;
6127 break;
6128 case DW_AT_friend:
6129 attrs->at_friend = a;
6130 break;
6131 case DW_AT_accessibility:
6132 attrs->at_accessibility = a;
6133 break;
6134 case DW_AT_address_class:
6135 attrs->at_address_class = a;
6136 break;
6137 case DW_AT_allocated:
6138 attrs->at_allocated = a;
6139 break;
6140 case DW_AT_artificial:
6141 attrs->at_artificial = a;
6142 break;
6143 case DW_AT_associated:
6144 attrs->at_associated = a;
6145 break;
6146 case DW_AT_binary_scale:
6147 attrs->at_binary_scale = a;
6148 break;
6149 case DW_AT_bit_offset:
6150 attrs->at_bit_offset = a;
6151 break;
6152 case DW_AT_bit_size:
6153 attrs->at_bit_size = a;
6154 break;
6155 case DW_AT_bit_stride:
6156 attrs->at_bit_stride = a;
6157 break;
6158 case DW_AT_byte_size:
6159 attrs->at_byte_size = a;
6160 break;
6161 case DW_AT_byte_stride:
6162 attrs->at_byte_stride = a;
6163 break;
6164 case DW_AT_const_value:
6165 attrs->at_const_value = a;
6166 break;
6167 case DW_AT_containing_type:
6168 attrs->at_containing_type = a;
6169 break;
6170 case DW_AT_count:
6171 attrs->at_count = a;
6172 break;
6173 case DW_AT_data_location:
6174 attrs->at_data_location = a;
6175 break;
6176 case DW_AT_data_member_location:
6177 attrs->at_data_member_location = a;
6178 break;
6179 case DW_AT_decimal_scale:
6180 attrs->at_decimal_scale = a;
6181 break;
6182 case DW_AT_decimal_sign:
6183 attrs->at_decimal_sign = a;
6184 break;
6185 case DW_AT_default_value:
6186 attrs->at_default_value = a;
6187 break;
6188 case DW_AT_digit_count:
6189 attrs->at_digit_count = a;
6190 break;
6191 case DW_AT_discr:
6192 attrs->at_discr = a;
6193 break;
6194 case DW_AT_discr_list:
6195 attrs->at_discr_list = a;
6196 break;
6197 case DW_AT_discr_value:
6198 attrs->at_discr_value = a;
6199 break;
6200 case DW_AT_encoding:
6201 attrs->at_encoding = a;
6202 break;
6203 case DW_AT_endianity:
6204 attrs->at_endianity = a;
6205 break;
6206 case DW_AT_explicit:
6207 attrs->at_explicit = a;
6208 break;
6209 case DW_AT_is_optional:
6210 attrs->at_is_optional = a;
6211 break;
6212 case DW_AT_location:
6213 attrs->at_location = a;
6214 break;
6215 case DW_AT_lower_bound:
6216 attrs->at_lower_bound = a;
6217 break;
6218 case DW_AT_mutable:
6219 attrs->at_mutable = a;
6220 break;
6221 case DW_AT_ordering:
6222 attrs->at_ordering = a;
6223 break;
6224 case DW_AT_picture_string:
6225 attrs->at_picture_string = a;
6226 break;
6227 case DW_AT_prototyped:
6228 attrs->at_prototyped = a;
6229 break;
6230 case DW_AT_small:
6231 attrs->at_small = a;
6232 break;
6233 case DW_AT_segment:
6234 attrs->at_segment = a;
6235 break;
6236 case DW_AT_string_length:
6237 attrs->at_string_length = a;
6238 break;
6239 case DW_AT_threads_scaled:
6240 attrs->at_threads_scaled = a;
6241 break;
6242 case DW_AT_upper_bound:
6243 attrs->at_upper_bound = a;
6244 break;
6245 case DW_AT_use_location:
6246 attrs->at_use_location = a;
6247 break;
6248 case DW_AT_use_UTF8:
6249 attrs->at_use_UTF8 = a;
6250 break;
6251 case DW_AT_variable_parameter:
6252 attrs->at_variable_parameter = a;
6253 break;
6254 case DW_AT_virtuality:
6255 attrs->at_virtuality = a;
6256 break;
6257 case DW_AT_visibility:
6258 attrs->at_visibility = a;
6259 break;
6260 case DW_AT_vtable_elem_location:
6261 attrs->at_vtable_elem_location = a;
6262 break;
6263 default:
6264 break;
6269 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6271 static void
6272 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6274 dw_die_ref c;
6275 dw_die_ref decl;
6276 struct checksum_attributes attrs;
6278 CHECKSUM_ULEB128 ('D');
6279 CHECKSUM_ULEB128 (die->die_tag);
6281 memset (&attrs, 0, sizeof (attrs));
6283 decl = get_AT_ref (die, DW_AT_specification);
6284 if (decl != NULL)
6285 collect_checksum_attributes (&attrs, decl);
6286 collect_checksum_attributes (&attrs, die);
6288 CHECKSUM_ATTR (attrs.at_name);
6289 CHECKSUM_ATTR (attrs.at_accessibility);
6290 CHECKSUM_ATTR (attrs.at_address_class);
6291 CHECKSUM_ATTR (attrs.at_allocated);
6292 CHECKSUM_ATTR (attrs.at_artificial);
6293 CHECKSUM_ATTR (attrs.at_associated);
6294 CHECKSUM_ATTR (attrs.at_binary_scale);
6295 CHECKSUM_ATTR (attrs.at_bit_offset);
6296 CHECKSUM_ATTR (attrs.at_bit_size);
6297 CHECKSUM_ATTR (attrs.at_bit_stride);
6298 CHECKSUM_ATTR (attrs.at_byte_size);
6299 CHECKSUM_ATTR (attrs.at_byte_stride);
6300 CHECKSUM_ATTR (attrs.at_const_value);
6301 CHECKSUM_ATTR (attrs.at_containing_type);
6302 CHECKSUM_ATTR (attrs.at_count);
6303 CHECKSUM_ATTR (attrs.at_data_location);
6304 CHECKSUM_ATTR (attrs.at_data_member_location);
6305 CHECKSUM_ATTR (attrs.at_decimal_scale);
6306 CHECKSUM_ATTR (attrs.at_decimal_sign);
6307 CHECKSUM_ATTR (attrs.at_default_value);
6308 CHECKSUM_ATTR (attrs.at_digit_count);
6309 CHECKSUM_ATTR (attrs.at_discr);
6310 CHECKSUM_ATTR (attrs.at_discr_list);
6311 CHECKSUM_ATTR (attrs.at_discr_value);
6312 CHECKSUM_ATTR (attrs.at_encoding);
6313 CHECKSUM_ATTR (attrs.at_endianity);
6314 CHECKSUM_ATTR (attrs.at_explicit);
6315 CHECKSUM_ATTR (attrs.at_is_optional);
6316 CHECKSUM_ATTR (attrs.at_location);
6317 CHECKSUM_ATTR (attrs.at_lower_bound);
6318 CHECKSUM_ATTR (attrs.at_mutable);
6319 CHECKSUM_ATTR (attrs.at_ordering);
6320 CHECKSUM_ATTR (attrs.at_picture_string);
6321 CHECKSUM_ATTR (attrs.at_prototyped);
6322 CHECKSUM_ATTR (attrs.at_small);
6323 CHECKSUM_ATTR (attrs.at_segment);
6324 CHECKSUM_ATTR (attrs.at_string_length);
6325 CHECKSUM_ATTR (attrs.at_threads_scaled);
6326 CHECKSUM_ATTR (attrs.at_upper_bound);
6327 CHECKSUM_ATTR (attrs.at_use_location);
6328 CHECKSUM_ATTR (attrs.at_use_UTF8);
6329 CHECKSUM_ATTR (attrs.at_variable_parameter);
6330 CHECKSUM_ATTR (attrs.at_virtuality);
6331 CHECKSUM_ATTR (attrs.at_visibility);
6332 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6333 CHECKSUM_ATTR (attrs.at_type);
6334 CHECKSUM_ATTR (attrs.at_friend);
6336 /* Checksum the child DIEs. */
6337 c = die->die_child;
6338 if (c) do {
6339 dw_attr_ref name_attr;
6341 c = c->die_sib;
6342 name_attr = get_AT (c, DW_AT_name);
6343 if (is_template_instantiation (c))
6345 /* Ignore instantiations of member type and function templates. */
6347 else if (name_attr != NULL
6348 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6350 /* Use a shallow checksum for named nested types and member
6351 functions. */
6352 CHECKSUM_ULEB128 ('S');
6353 CHECKSUM_ULEB128 (c->die_tag);
6354 CHECKSUM_STRING (AT_string (name_attr));
6356 else
6358 /* Use a deep checksum for other children. */
6359 /* Mark this DIE so it gets processed when unmarking. */
6360 if (c->die_mark == 0)
6361 c->die_mark = -1;
6362 die_checksum_ordered (c, ctx, mark);
6364 } while (c != die->die_child);
6366 CHECKSUM_ULEB128 (0);
6369 /* Add a type name and tag to a hash. */
6370 static void
6371 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6373 CHECKSUM_ULEB128 (tag);
6374 CHECKSUM_STRING (name);
6377 #undef CHECKSUM
6378 #undef CHECKSUM_STRING
6379 #undef CHECKSUM_ATTR
6380 #undef CHECKSUM_LEB128
6381 #undef CHECKSUM_ULEB128
6383 /* Generate the type signature for DIE. This is computed by generating an
6384 MD5 checksum over the DIE's tag, its relevant attributes, and its
6385 children. Attributes that are references to other DIEs are processed
6386 by recursion, using the MARK field to prevent infinite recursion.
6387 If the DIE is nested inside a namespace or another type, we also
6388 need to include that context in the signature. The lower 64 bits
6389 of the resulting MD5 checksum comprise the signature. */
6391 static void
6392 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6394 int mark;
6395 const char *name;
6396 unsigned char checksum[16];
6397 struct md5_ctx ctx;
6398 dw_die_ref decl;
6399 dw_die_ref parent;
6401 name = get_AT_string (die, DW_AT_name);
6402 decl = get_AT_ref (die, DW_AT_specification);
6403 parent = get_die_parent (die);
6405 /* First, compute a signature for just the type name (and its surrounding
6406 context, if any. This is stored in the type unit DIE for link-time
6407 ODR (one-definition rule) checking. */
6409 if (is_cxx () && name != NULL)
6411 md5_init_ctx (&ctx);
6413 /* Checksum the names of surrounding namespaces and structures. */
6414 if (parent != NULL)
6415 checksum_die_context (parent, &ctx);
6417 /* Checksum the current DIE. */
6418 die_odr_checksum (die->die_tag, name, &ctx);
6419 md5_finish_ctx (&ctx, checksum);
6421 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6424 /* Next, compute the complete type signature. */
6426 md5_init_ctx (&ctx);
6427 mark = 1;
6428 die->die_mark = mark;
6430 /* Checksum the names of surrounding namespaces and structures. */
6431 if (parent != NULL)
6432 checksum_die_context (parent, &ctx);
6434 /* Checksum the DIE and its children. */
6435 die_checksum_ordered (die, &ctx, &mark);
6436 unmark_all_dies (die);
6437 md5_finish_ctx (&ctx, checksum);
6439 /* Store the signature in the type node and link the type DIE and the
6440 type node together. */
6441 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6442 DWARF_TYPE_SIGNATURE_SIZE);
6443 die->comdat_type_p = true;
6444 die->die_id.die_type_node = type_node;
6445 type_node->type_die = die;
6447 /* If the DIE is a specification, link its declaration to the type node
6448 as well. */
6449 if (decl != NULL)
6451 decl->comdat_type_p = true;
6452 decl->die_id.die_type_node = type_node;
6456 /* Do the location expressions look same? */
6457 static inline int
6458 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6460 return loc1->dw_loc_opc == loc2->dw_loc_opc
6461 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6462 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6465 /* Do the values look the same? */
6466 static int
6467 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6469 dw_loc_descr_ref loc1, loc2;
6470 rtx r1, r2;
6472 if (v1->val_class != v2->val_class)
6473 return 0;
6475 switch (v1->val_class)
6477 case dw_val_class_const:
6478 return v1->v.val_int == v2->v.val_int;
6479 case dw_val_class_unsigned_const:
6480 return v1->v.val_unsigned == v2->v.val_unsigned;
6481 case dw_val_class_const_double:
6482 return v1->v.val_double.high == v2->v.val_double.high
6483 && v1->v.val_double.low == v2->v.val_double.low;
6484 case dw_val_class_wide_int:
6485 return *v1->v.val_wide == *v2->v.val_wide;
6486 case dw_val_class_vec:
6487 if (v1->v.val_vec.length != v2->v.val_vec.length
6488 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6489 return 0;
6490 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6491 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6492 return 0;
6493 return 1;
6494 case dw_val_class_flag:
6495 return v1->v.val_flag == v2->v.val_flag;
6496 case dw_val_class_str:
6497 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6499 case dw_val_class_addr:
6500 r1 = v1->v.val_addr;
6501 r2 = v2->v.val_addr;
6502 if (GET_CODE (r1) != GET_CODE (r2))
6503 return 0;
6504 return !rtx_equal_p (r1, r2);
6506 case dw_val_class_offset:
6507 return v1->v.val_offset == v2->v.val_offset;
6509 case dw_val_class_loc:
6510 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6511 loc1 && loc2;
6512 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6513 if (!same_loc_p (loc1, loc2, mark))
6514 return 0;
6515 return !loc1 && !loc2;
6517 case dw_val_class_die_ref:
6518 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6520 case dw_val_class_fde_ref:
6521 case dw_val_class_vms_delta:
6522 case dw_val_class_lbl_id:
6523 case dw_val_class_lineptr:
6524 case dw_val_class_macptr:
6525 case dw_val_class_high_pc:
6526 return 1;
6528 case dw_val_class_file:
6529 return v1->v.val_file == v2->v.val_file;
6531 case dw_val_class_data8:
6532 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6534 default:
6535 return 1;
6539 /* Do the attributes look the same? */
6541 static int
6542 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6544 if (at1->dw_attr != at2->dw_attr)
6545 return 0;
6547 /* We don't care that this was compiled with a different compiler
6548 snapshot; if the output is the same, that's what matters. */
6549 if (at1->dw_attr == DW_AT_producer)
6550 return 1;
6552 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6555 /* Do the dies look the same? */
6557 static int
6558 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6560 dw_die_ref c1, c2;
6561 dw_attr_ref a1;
6562 unsigned ix;
6564 /* To avoid infinite recursion. */
6565 if (die1->die_mark)
6566 return die1->die_mark == die2->die_mark;
6567 die1->die_mark = die2->die_mark = ++(*mark);
6569 if (die1->die_tag != die2->die_tag)
6570 return 0;
6572 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6573 return 0;
6575 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6576 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6577 return 0;
6579 c1 = die1->die_child;
6580 c2 = die2->die_child;
6581 if (! c1)
6583 if (c2)
6584 return 0;
6586 else
6587 for (;;)
6589 if (!same_die_p (c1, c2, mark))
6590 return 0;
6591 c1 = c1->die_sib;
6592 c2 = c2->die_sib;
6593 if (c1 == die1->die_child)
6595 if (c2 == die2->die_child)
6596 break;
6597 else
6598 return 0;
6602 return 1;
6605 /* Do the dies look the same? Wrapper around same_die_p. */
6607 static int
6608 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6610 int mark = 0;
6611 int ret = same_die_p (die1, die2, &mark);
6613 unmark_all_dies (die1);
6614 unmark_all_dies (die2);
6616 return ret;
6619 /* The prefix to attach to symbols on DIEs in the current comdat debug
6620 info section. */
6621 static const char *comdat_symbol_id;
6623 /* The index of the current symbol within the current comdat CU. */
6624 static unsigned int comdat_symbol_number;
6626 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6627 children, and set comdat_symbol_id accordingly. */
6629 static void
6630 compute_section_prefix (dw_die_ref unit_die)
6632 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6633 const char *base = die_name ? lbasename (die_name) : "anonymous";
6634 char *name = XALLOCAVEC (char, strlen (base) + 64);
6635 char *p;
6636 int i, mark;
6637 unsigned char checksum[16];
6638 struct md5_ctx ctx;
6640 /* Compute the checksum of the DIE, then append part of it as hex digits to
6641 the name filename of the unit. */
6643 md5_init_ctx (&ctx);
6644 mark = 0;
6645 die_checksum (unit_die, &ctx, &mark);
6646 unmark_all_dies (unit_die);
6647 md5_finish_ctx (&ctx, checksum);
6649 sprintf (name, "%s.", base);
6650 clean_symbol_name (name);
6652 p = name + strlen (name);
6653 for (i = 0; i < 4; i++)
6655 sprintf (p, "%.2x", checksum[i]);
6656 p += 2;
6659 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6660 comdat_symbol_number = 0;
6663 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6665 static int
6666 is_type_die (dw_die_ref die)
6668 switch (die->die_tag)
6670 case DW_TAG_array_type:
6671 case DW_TAG_class_type:
6672 case DW_TAG_interface_type:
6673 case DW_TAG_enumeration_type:
6674 case DW_TAG_pointer_type:
6675 case DW_TAG_reference_type:
6676 case DW_TAG_rvalue_reference_type:
6677 case DW_TAG_string_type:
6678 case DW_TAG_structure_type:
6679 case DW_TAG_subroutine_type:
6680 case DW_TAG_union_type:
6681 case DW_TAG_ptr_to_member_type:
6682 case DW_TAG_set_type:
6683 case DW_TAG_subrange_type:
6684 case DW_TAG_base_type:
6685 case DW_TAG_const_type:
6686 case DW_TAG_file_type:
6687 case DW_TAG_packed_type:
6688 case DW_TAG_volatile_type:
6689 case DW_TAG_typedef:
6690 return 1;
6691 default:
6692 return 0;
6696 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6697 Basically, we want to choose the bits that are likely to be shared between
6698 compilations (types) and leave out the bits that are specific to individual
6699 compilations (functions). */
6701 static int
6702 is_comdat_die (dw_die_ref c)
6704 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6705 we do for stabs. The advantage is a greater likelihood of sharing between
6706 objects that don't include headers in the same order (and therefore would
6707 put the base types in a different comdat). jason 8/28/00 */
6709 if (c->die_tag == DW_TAG_base_type)
6710 return 0;
6712 if (c->die_tag == DW_TAG_pointer_type
6713 || c->die_tag == DW_TAG_reference_type
6714 || c->die_tag == DW_TAG_rvalue_reference_type
6715 || c->die_tag == DW_TAG_const_type
6716 || c->die_tag == DW_TAG_volatile_type)
6718 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6720 return t ? is_comdat_die (t) : 0;
6723 return is_type_die (c);
6726 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6727 compilation unit. */
6729 static int
6730 is_symbol_die (dw_die_ref c)
6732 return (is_type_die (c)
6733 || is_declaration_die (c)
6734 || c->die_tag == DW_TAG_namespace
6735 || c->die_tag == DW_TAG_module);
6738 /* Returns true iff C is a compile-unit DIE. */
6740 static inline bool
6741 is_cu_die (dw_die_ref c)
6743 return c && c->die_tag == DW_TAG_compile_unit;
6746 /* Returns true iff C is a unit DIE of some sort. */
6748 static inline bool
6749 is_unit_die (dw_die_ref c)
6751 return c && (c->die_tag == DW_TAG_compile_unit
6752 || c->die_tag == DW_TAG_partial_unit
6753 || c->die_tag == DW_TAG_type_unit);
6756 /* Returns true iff C is a namespace DIE. */
6758 static inline bool
6759 is_namespace_die (dw_die_ref c)
6761 return c && c->die_tag == DW_TAG_namespace;
6764 /* Returns true iff C is a class or structure DIE. */
6766 static inline bool
6767 is_class_die (dw_die_ref c)
6769 return c && (c->die_tag == DW_TAG_class_type
6770 || c->die_tag == DW_TAG_structure_type);
6773 /* Return non-zero if this DIE is a template parameter. */
6775 static inline bool
6776 is_template_parameter (dw_die_ref die)
6778 switch (die->die_tag)
6780 case DW_TAG_template_type_param:
6781 case DW_TAG_template_value_param:
6782 case DW_TAG_GNU_template_template_param:
6783 case DW_TAG_GNU_template_parameter_pack:
6784 return true;
6785 default:
6786 return false;
6790 /* Return non-zero if this DIE represents a template instantiation. */
6792 static inline bool
6793 is_template_instantiation (dw_die_ref die)
6795 dw_die_ref c;
6797 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6798 return false;
6799 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6800 return false;
6803 static char *
6804 gen_internal_sym (const char *prefix)
6806 char buf[256];
6808 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6809 return xstrdup (buf);
6812 /* Assign symbols to all worthy DIEs under DIE. */
6814 static void
6815 assign_symbol_names (dw_die_ref die)
6817 dw_die_ref c;
6819 if (is_symbol_die (die) && !die->comdat_type_p)
6821 if (comdat_symbol_id)
6823 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6825 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6826 comdat_symbol_id, comdat_symbol_number++);
6827 die->die_id.die_symbol = xstrdup (p);
6829 else
6830 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6833 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6836 struct cu_hash_table_entry
6838 dw_die_ref cu;
6839 unsigned min_comdat_num, max_comdat_num;
6840 struct cu_hash_table_entry *next;
6843 /* Helpers to manipulate hash table of CUs. */
6845 struct cu_hash_table_entry_hasher
6847 typedef cu_hash_table_entry value_type;
6848 typedef die_struct compare_type;
6849 static inline hashval_t hash (const value_type *);
6850 static inline bool equal (const value_type *, const compare_type *);
6851 static inline void remove (value_type *);
6854 inline hashval_t
6855 cu_hash_table_entry_hasher::hash (const value_type *entry)
6857 return htab_hash_string (entry->cu->die_id.die_symbol);
6860 inline bool
6861 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6862 const compare_type *entry2)
6864 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6867 inline void
6868 cu_hash_table_entry_hasher::remove (value_type *entry)
6870 struct cu_hash_table_entry *next;
6872 while (entry)
6874 next = entry->next;
6875 free (entry);
6876 entry = next;
6880 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6882 /* Check whether we have already seen this CU and set up SYM_NUM
6883 accordingly. */
6884 static int
6885 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6887 struct cu_hash_table_entry dummy;
6888 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6890 dummy.max_comdat_num = 0;
6892 slot = htable->find_slot_with_hash (cu,
6893 htab_hash_string (cu->die_id.die_symbol),
6894 INSERT);
6895 entry = *slot;
6897 for (; entry; last = entry, entry = entry->next)
6899 if (same_die_p_wrap (cu, entry->cu))
6900 break;
6903 if (entry)
6905 *sym_num = entry->min_comdat_num;
6906 return 1;
6909 entry = XCNEW (struct cu_hash_table_entry);
6910 entry->cu = cu;
6911 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6912 entry->next = *slot;
6913 *slot = entry;
6915 return 0;
6918 /* Record SYM_NUM to record of CU in HTABLE. */
6919 static void
6920 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6921 unsigned int sym_num)
6923 struct cu_hash_table_entry **slot, *entry;
6925 slot = htable->find_slot_with_hash (cu,
6926 htab_hash_string (cu->die_id.die_symbol),
6927 NO_INSERT);
6928 entry = *slot;
6930 entry->max_comdat_num = sym_num;
6933 /* Traverse the DIE (which is always comp_unit_die), and set up
6934 additional compilation units for each of the include files we see
6935 bracketed by BINCL/EINCL. */
6937 static void
6938 break_out_includes (dw_die_ref die)
6940 dw_die_ref c;
6941 dw_die_ref unit = NULL;
6942 limbo_die_node *node, **pnode;
6944 c = die->die_child;
6945 if (c) do {
6946 dw_die_ref prev = c;
6947 c = c->die_sib;
6948 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6949 || (unit && is_comdat_die (c)))
6951 dw_die_ref next = c->die_sib;
6953 /* This DIE is for a secondary CU; remove it from the main one. */
6954 remove_child_with_prev (c, prev);
6956 if (c->die_tag == DW_TAG_GNU_BINCL)
6957 unit = push_new_compile_unit (unit, c);
6958 else if (c->die_tag == DW_TAG_GNU_EINCL)
6959 unit = pop_compile_unit (unit);
6960 else
6961 add_child_die (unit, c);
6962 c = next;
6963 if (c == die->die_child)
6964 break;
6966 } while (c != die->die_child);
6968 #if 0
6969 /* We can only use this in debugging, since the frontend doesn't check
6970 to make sure that we leave every include file we enter. */
6971 gcc_assert (!unit);
6972 #endif
6974 assign_symbol_names (die);
6975 cu_hash_type cu_hash_table (10);
6976 for (node = limbo_die_list, pnode = &limbo_die_list;
6977 node;
6978 node = node->next)
6980 int is_dupl;
6982 compute_section_prefix (node->die);
6983 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6984 &comdat_symbol_number);
6985 assign_symbol_names (node->die);
6986 if (is_dupl)
6987 *pnode = node->next;
6988 else
6990 pnode = &node->next;
6991 record_comdat_symbol_number (node->die, &cu_hash_table,
6992 comdat_symbol_number);
6997 /* Return non-zero if this DIE is a declaration. */
6999 static int
7000 is_declaration_die (dw_die_ref die)
7002 dw_attr_ref a;
7003 unsigned ix;
7005 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7006 if (a->dw_attr == DW_AT_declaration)
7007 return 1;
7009 return 0;
7012 /* Return non-zero if this DIE is nested inside a subprogram. */
7014 static int
7015 is_nested_in_subprogram (dw_die_ref die)
7017 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7019 if (decl == NULL)
7020 decl = die;
7021 return local_scope_p (decl);
7024 /* Return non-zero if this DIE contains a defining declaration of a
7025 subprogram. */
7027 static int
7028 contains_subprogram_definition (dw_die_ref die)
7030 dw_die_ref c;
7032 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7033 return 1;
7034 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7035 return 0;
7038 /* Return non-zero if this is a type DIE that should be moved to a
7039 COMDAT .debug_types section. */
7041 static int
7042 should_move_die_to_comdat (dw_die_ref die)
7044 switch (die->die_tag)
7046 case DW_TAG_class_type:
7047 case DW_TAG_structure_type:
7048 case DW_TAG_enumeration_type:
7049 case DW_TAG_union_type:
7050 /* Don't move declarations, inlined instances, types nested in a
7051 subprogram, or types that contain subprogram definitions. */
7052 if (is_declaration_die (die)
7053 || get_AT (die, DW_AT_abstract_origin)
7054 || is_nested_in_subprogram (die)
7055 || contains_subprogram_definition (die))
7056 return 0;
7057 return 1;
7058 case DW_TAG_array_type:
7059 case DW_TAG_interface_type:
7060 case DW_TAG_pointer_type:
7061 case DW_TAG_reference_type:
7062 case DW_TAG_rvalue_reference_type:
7063 case DW_TAG_string_type:
7064 case DW_TAG_subroutine_type:
7065 case DW_TAG_ptr_to_member_type:
7066 case DW_TAG_set_type:
7067 case DW_TAG_subrange_type:
7068 case DW_TAG_base_type:
7069 case DW_TAG_const_type:
7070 case DW_TAG_file_type:
7071 case DW_TAG_packed_type:
7072 case DW_TAG_volatile_type:
7073 case DW_TAG_typedef:
7074 default:
7075 return 0;
7079 /* Make a clone of DIE. */
7081 static dw_die_ref
7082 clone_die (dw_die_ref die)
7084 dw_die_ref clone;
7085 dw_attr_ref a;
7086 unsigned ix;
7088 clone = ggc_cleared_alloc<die_node> ();
7089 clone->die_tag = die->die_tag;
7091 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7092 add_dwarf_attr (clone, a);
7094 return clone;
7097 /* Make a clone of the tree rooted at DIE. */
7099 static dw_die_ref
7100 clone_tree (dw_die_ref die)
7102 dw_die_ref c;
7103 dw_die_ref clone = clone_die (die);
7105 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7107 return clone;
7110 /* Make a clone of DIE as a declaration. */
7112 static dw_die_ref
7113 clone_as_declaration (dw_die_ref die)
7115 dw_die_ref clone;
7116 dw_die_ref decl;
7117 dw_attr_ref a;
7118 unsigned ix;
7120 /* If the DIE is already a declaration, just clone it. */
7121 if (is_declaration_die (die))
7122 return clone_die (die);
7124 /* If the DIE is a specification, just clone its declaration DIE. */
7125 decl = get_AT_ref (die, DW_AT_specification);
7126 if (decl != NULL)
7128 clone = clone_die (decl);
7129 if (die->comdat_type_p)
7130 add_AT_die_ref (clone, DW_AT_signature, die);
7131 return clone;
7134 clone = ggc_cleared_alloc<die_node> ();
7135 clone->die_tag = die->die_tag;
7137 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7139 /* We don't want to copy over all attributes.
7140 For example we don't want DW_AT_byte_size because otherwise we will no
7141 longer have a declaration and GDB will treat it as a definition. */
7143 switch (a->dw_attr)
7145 case DW_AT_abstract_origin:
7146 case DW_AT_artificial:
7147 case DW_AT_containing_type:
7148 case DW_AT_external:
7149 case DW_AT_name:
7150 case DW_AT_type:
7151 case DW_AT_virtuality:
7152 case DW_AT_linkage_name:
7153 case DW_AT_MIPS_linkage_name:
7154 add_dwarf_attr (clone, a);
7155 break;
7156 case DW_AT_byte_size:
7157 default:
7158 break;
7162 if (die->comdat_type_p)
7163 add_AT_die_ref (clone, DW_AT_signature, die);
7165 add_AT_flag (clone, DW_AT_declaration, 1);
7166 return clone;
7170 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7172 struct decl_table_entry
7174 dw_die_ref orig;
7175 dw_die_ref copy;
7178 /* Helpers to manipulate hash table of copied declarations. */
7180 /* Hashtable helpers. */
7182 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7184 typedef decl_table_entry value_type;
7185 typedef die_struct compare_type;
7186 static inline hashval_t hash (const value_type *);
7187 static inline bool equal (const value_type *, const compare_type *);
7190 inline hashval_t
7191 decl_table_entry_hasher::hash (const value_type *entry)
7193 return htab_hash_pointer (entry->orig);
7196 inline bool
7197 decl_table_entry_hasher::equal (const value_type *entry1,
7198 const compare_type *entry2)
7200 return entry1->orig == entry2;
7203 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7205 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7206 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7207 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7208 to check if the ancestor has already been copied into UNIT. */
7210 static dw_die_ref
7211 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7212 decl_hash_type *decl_table)
7214 dw_die_ref parent = die->die_parent;
7215 dw_die_ref new_parent = unit;
7216 dw_die_ref copy;
7217 decl_table_entry **slot = NULL;
7218 struct decl_table_entry *entry = NULL;
7220 if (decl_table)
7222 /* Check if the entry has already been copied to UNIT. */
7223 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7224 INSERT);
7225 if (*slot != HTAB_EMPTY_ENTRY)
7227 entry = *slot;
7228 return entry->copy;
7231 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7232 entry = XCNEW (struct decl_table_entry);
7233 entry->orig = die;
7234 entry->copy = NULL;
7235 *slot = entry;
7238 if (parent != NULL)
7240 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7241 if (spec != NULL)
7242 parent = spec;
7243 if (!is_unit_die (parent))
7244 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7247 copy = clone_as_declaration (die);
7248 add_child_die (new_parent, copy);
7250 if (decl_table)
7252 /* Record the pointer to the copy. */
7253 entry->copy = copy;
7256 return copy;
7258 /* Copy the declaration context to the new type unit DIE. This includes
7259 any surrounding namespace or type declarations. If the DIE has an
7260 AT_specification attribute, it also includes attributes and children
7261 attached to the specification, and returns a pointer to the original
7262 parent of the declaration DIE. Returns NULL otherwise. */
7264 static dw_die_ref
7265 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7267 dw_die_ref decl;
7268 dw_die_ref new_decl;
7269 dw_die_ref orig_parent = NULL;
7271 decl = get_AT_ref (die, DW_AT_specification);
7272 if (decl == NULL)
7273 decl = die;
7274 else
7276 unsigned ix;
7277 dw_die_ref c;
7278 dw_attr_ref a;
7280 /* The original DIE will be changed to a declaration, and must
7281 be moved to be a child of the original declaration DIE. */
7282 orig_parent = decl->die_parent;
7284 /* Copy the type node pointer from the new DIE to the original
7285 declaration DIE so we can forward references later. */
7286 decl->comdat_type_p = true;
7287 decl->die_id.die_type_node = die->die_id.die_type_node;
7289 remove_AT (die, DW_AT_specification);
7291 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7293 if (a->dw_attr != DW_AT_name
7294 && a->dw_attr != DW_AT_declaration
7295 && a->dw_attr != DW_AT_external)
7296 add_dwarf_attr (die, a);
7299 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7302 if (decl->die_parent != NULL
7303 && !is_unit_die (decl->die_parent))
7305 new_decl = copy_ancestor_tree (unit, decl, NULL);
7306 if (new_decl != NULL)
7308 remove_AT (new_decl, DW_AT_signature);
7309 add_AT_specification (die, new_decl);
7313 return orig_parent;
7316 /* Generate the skeleton ancestor tree for the given NODE, then clone
7317 the DIE and add the clone into the tree. */
7319 static void
7320 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7322 if (node->new_die != NULL)
7323 return;
7325 node->new_die = clone_as_declaration (node->old_die);
7327 if (node->parent != NULL)
7329 generate_skeleton_ancestor_tree (node->parent);
7330 add_child_die (node->parent->new_die, node->new_die);
7334 /* Generate a skeleton tree of DIEs containing any declarations that are
7335 found in the original tree. We traverse the tree looking for declaration
7336 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7338 static void
7339 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7341 skeleton_chain_node node;
7342 dw_die_ref c;
7343 dw_die_ref first;
7344 dw_die_ref prev = NULL;
7345 dw_die_ref next = NULL;
7347 node.parent = parent;
7349 first = c = parent->old_die->die_child;
7350 if (c)
7351 next = c->die_sib;
7352 if (c) do {
7353 if (prev == NULL || prev->die_sib == c)
7354 prev = c;
7355 c = next;
7356 next = (c == first ? NULL : c->die_sib);
7357 node.old_die = c;
7358 node.new_die = NULL;
7359 if (is_declaration_die (c))
7361 if (is_template_instantiation (c))
7363 /* Instantiated templates do not need to be cloned into the
7364 type unit. Just move the DIE and its children back to
7365 the skeleton tree (in the main CU). */
7366 remove_child_with_prev (c, prev);
7367 add_child_die (parent->new_die, c);
7368 c = prev;
7370 else
7372 /* Clone the existing DIE, move the original to the skeleton
7373 tree (which is in the main CU), and put the clone, with
7374 all the original's children, where the original came from
7375 (which is about to be moved to the type unit). */
7376 dw_die_ref clone = clone_die (c);
7377 move_all_children (c, clone);
7379 /* If the original has a DW_AT_object_pointer attribute,
7380 it would now point to a child DIE just moved to the
7381 cloned tree, so we need to remove that attribute from
7382 the original. */
7383 remove_AT (c, DW_AT_object_pointer);
7385 replace_child (c, clone, prev);
7386 generate_skeleton_ancestor_tree (parent);
7387 add_child_die (parent->new_die, c);
7388 node.new_die = c;
7389 c = clone;
7392 generate_skeleton_bottom_up (&node);
7393 } while (next != NULL);
7396 /* Wrapper function for generate_skeleton_bottom_up. */
7398 static dw_die_ref
7399 generate_skeleton (dw_die_ref die)
7401 skeleton_chain_node node;
7403 node.old_die = die;
7404 node.new_die = NULL;
7405 node.parent = NULL;
7407 /* If this type definition is nested inside another type,
7408 and is not an instantiation of a template, always leave
7409 at least a declaration in its place. */
7410 if (die->die_parent != NULL
7411 && is_type_die (die->die_parent)
7412 && !is_template_instantiation (die))
7413 node.new_die = clone_as_declaration (die);
7415 generate_skeleton_bottom_up (&node);
7416 return node.new_die;
7419 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7420 declaration. The original DIE is moved to a new compile unit so that
7421 existing references to it follow it to the new location. If any of the
7422 original DIE's descendants is a declaration, we need to replace the
7423 original DIE with a skeleton tree and move the declarations back into the
7424 skeleton tree. */
7426 static dw_die_ref
7427 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7428 dw_die_ref prev)
7430 dw_die_ref skeleton, orig_parent;
7432 /* Copy the declaration context to the type unit DIE. If the returned
7433 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7434 that DIE. */
7435 orig_parent = copy_declaration_context (unit, child);
7437 skeleton = generate_skeleton (child);
7438 if (skeleton == NULL)
7439 remove_child_with_prev (child, prev);
7440 else
7442 skeleton->comdat_type_p = true;
7443 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7445 /* If the original DIE was a specification, we need to put
7446 the skeleton under the parent DIE of the declaration.
7447 This leaves the original declaration in the tree, but
7448 it will be pruned later since there are no longer any
7449 references to it. */
7450 if (orig_parent != NULL)
7452 remove_child_with_prev (child, prev);
7453 add_child_die (orig_parent, skeleton);
7455 else
7456 replace_child (child, skeleton, prev);
7459 return skeleton;
7462 /* Traverse the DIE and set up additional .debug_types sections for each
7463 type worthy of being placed in a COMDAT section. */
7465 static void
7466 break_out_comdat_types (dw_die_ref die)
7468 dw_die_ref c;
7469 dw_die_ref first;
7470 dw_die_ref prev = NULL;
7471 dw_die_ref next = NULL;
7472 dw_die_ref unit = NULL;
7474 first = c = die->die_child;
7475 if (c)
7476 next = c->die_sib;
7477 if (c) do {
7478 if (prev == NULL || prev->die_sib == c)
7479 prev = c;
7480 c = next;
7481 next = (c == first ? NULL : c->die_sib);
7482 if (should_move_die_to_comdat (c))
7484 dw_die_ref replacement;
7485 comdat_type_node_ref type_node;
7487 /* Break out nested types into their own type units. */
7488 break_out_comdat_types (c);
7490 /* Create a new type unit DIE as the root for the new tree, and
7491 add it to the list of comdat types. */
7492 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7493 add_AT_unsigned (unit, DW_AT_language,
7494 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7495 type_node = ggc_cleared_alloc<comdat_type_node> ();
7496 type_node->root_die = unit;
7497 type_node->next = comdat_type_list;
7498 comdat_type_list = type_node;
7500 /* Generate the type signature. */
7501 generate_type_signature (c, type_node);
7503 /* Copy the declaration context, attributes, and children of the
7504 declaration into the new type unit DIE, then remove this DIE
7505 from the main CU (or replace it with a skeleton if necessary). */
7506 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7507 type_node->skeleton_die = replacement;
7509 /* Add the DIE to the new compunit. */
7510 add_child_die (unit, c);
7512 if (replacement != NULL)
7513 c = replacement;
7515 else if (c->die_tag == DW_TAG_namespace
7516 || c->die_tag == DW_TAG_class_type
7517 || c->die_tag == DW_TAG_structure_type
7518 || c->die_tag == DW_TAG_union_type)
7520 /* Look for nested types that can be broken out. */
7521 break_out_comdat_types (c);
7523 } while (next != NULL);
7526 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7527 Enter all the cloned children into the hash table decl_table. */
7529 static dw_die_ref
7530 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7532 dw_die_ref c;
7533 dw_die_ref clone;
7534 struct decl_table_entry *entry;
7535 decl_table_entry **slot;
7537 if (die->die_tag == DW_TAG_subprogram)
7538 clone = clone_as_declaration (die);
7539 else
7540 clone = clone_die (die);
7542 slot = decl_table->find_slot_with_hash (die,
7543 htab_hash_pointer (die), INSERT);
7545 /* Assert that DIE isn't in the hash table yet. If it would be there
7546 before, the ancestors would be necessarily there as well, therefore
7547 clone_tree_partial wouldn't be called. */
7548 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7550 entry = XCNEW (struct decl_table_entry);
7551 entry->orig = die;
7552 entry->copy = clone;
7553 *slot = entry;
7555 if (die->die_tag != DW_TAG_subprogram)
7556 FOR_EACH_CHILD (die, c,
7557 add_child_die (clone, clone_tree_partial (c, decl_table)));
7559 return clone;
7562 /* Walk the DIE and its children, looking for references to incomplete
7563 or trivial types that are unmarked (i.e., that are not in the current
7564 type_unit). */
7566 static void
7567 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7569 dw_die_ref c;
7570 dw_attr_ref a;
7571 unsigned ix;
7573 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7575 if (AT_class (a) == dw_val_class_die_ref)
7577 dw_die_ref targ = AT_ref (a);
7578 decl_table_entry **slot;
7579 struct decl_table_entry *entry;
7581 if (targ->die_mark != 0 || targ->comdat_type_p)
7582 continue;
7584 slot = decl_table->find_slot_with_hash (targ,
7585 htab_hash_pointer (targ),
7586 INSERT);
7588 if (*slot != HTAB_EMPTY_ENTRY)
7590 /* TARG has already been copied, so we just need to
7591 modify the reference to point to the copy. */
7592 entry = *slot;
7593 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7595 else
7597 dw_die_ref parent = unit;
7598 dw_die_ref copy = clone_die (targ);
7600 /* Record in DECL_TABLE that TARG has been copied.
7601 Need to do this now, before the recursive call,
7602 because DECL_TABLE may be expanded and SLOT
7603 would no longer be a valid pointer. */
7604 entry = XCNEW (struct decl_table_entry);
7605 entry->orig = targ;
7606 entry->copy = copy;
7607 *slot = entry;
7609 /* If TARG is not a declaration DIE, we need to copy its
7610 children. */
7611 if (!is_declaration_die (targ))
7613 FOR_EACH_CHILD (
7614 targ, c,
7615 add_child_die (copy,
7616 clone_tree_partial (c, decl_table)));
7619 /* Make sure the cloned tree is marked as part of the
7620 type unit. */
7621 mark_dies (copy);
7623 /* If TARG has surrounding context, copy its ancestor tree
7624 into the new type unit. */
7625 if (targ->die_parent != NULL
7626 && !is_unit_die (targ->die_parent))
7627 parent = copy_ancestor_tree (unit, targ->die_parent,
7628 decl_table);
7630 add_child_die (parent, copy);
7631 a->dw_attr_val.v.val_die_ref.die = copy;
7633 /* Make sure the newly-copied DIE is walked. If it was
7634 installed in a previously-added context, it won't
7635 get visited otherwise. */
7636 if (parent != unit)
7638 /* Find the highest point of the newly-added tree,
7639 mark each node along the way, and walk from there. */
7640 parent->die_mark = 1;
7641 while (parent->die_parent
7642 && parent->die_parent->die_mark == 0)
7644 parent = parent->die_parent;
7645 parent->die_mark = 1;
7647 copy_decls_walk (unit, parent, decl_table);
7653 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7656 /* Copy declarations for "unworthy" types into the new comdat section.
7657 Incomplete types, modified types, and certain other types aren't broken
7658 out into comdat sections of their own, so they don't have a signature,
7659 and we need to copy the declaration into the same section so that we
7660 don't have an external reference. */
7662 static void
7663 copy_decls_for_unworthy_types (dw_die_ref unit)
7665 mark_dies (unit);
7666 decl_hash_type decl_table (10);
7667 copy_decls_walk (unit, unit, &decl_table);
7668 unmark_dies (unit);
7671 /* Traverse the DIE and add a sibling attribute if it may have the
7672 effect of speeding up access to siblings. To save some space,
7673 avoid generating sibling attributes for DIE's without children. */
7675 static void
7676 add_sibling_attributes (dw_die_ref die)
7678 dw_die_ref c;
7680 if (! die->die_child)
7681 return;
7683 if (die->die_parent && die != die->die_parent->die_child)
7684 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7686 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7689 /* Output all location lists for the DIE and its children. */
7691 static void
7692 output_location_lists (dw_die_ref die)
7694 dw_die_ref c;
7695 dw_attr_ref a;
7696 unsigned ix;
7698 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7699 if (AT_class (a) == dw_val_class_loc_list)
7700 output_loc_list (AT_loc_list (a));
7702 FOR_EACH_CHILD (die, c, output_location_lists (c));
7705 /* We want to limit the number of external references, because they are
7706 larger than local references: a relocation takes multiple words, and
7707 even a sig8 reference is always eight bytes, whereas a local reference
7708 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7709 So if we encounter multiple external references to the same type DIE, we
7710 make a local typedef stub for it and redirect all references there.
7712 This is the element of the hash table for keeping track of these
7713 references. */
7715 struct external_ref
7717 dw_die_ref type;
7718 dw_die_ref stub;
7719 unsigned n_refs;
7722 /* Hashtable helpers. */
7724 struct external_ref_hasher : typed_free_remove <external_ref>
7726 typedef external_ref value_type;
7727 typedef external_ref compare_type;
7728 static inline hashval_t hash (const value_type *);
7729 static inline bool equal (const value_type *, const compare_type *);
7732 inline hashval_t
7733 external_ref_hasher::hash (const value_type *r)
7735 dw_die_ref die = r->type;
7736 hashval_t h = 0;
7738 /* We can't use the address of the DIE for hashing, because
7739 that will make the order of the stub DIEs non-deterministic. */
7740 if (! die->comdat_type_p)
7741 /* We have a symbol; use it to compute a hash. */
7742 h = htab_hash_string (die->die_id.die_symbol);
7743 else
7745 /* We have a type signature; use a subset of the bits as the hash.
7746 The 8-byte signature is at least as large as hashval_t. */
7747 comdat_type_node_ref type_node = die->die_id.die_type_node;
7748 memcpy (&h, type_node->signature, sizeof (h));
7750 return h;
7753 inline bool
7754 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7756 return r1->type == r2->type;
7759 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7761 /* Return a pointer to the external_ref for references to DIE. */
7763 static struct external_ref *
7764 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7766 struct external_ref ref, *ref_p;
7767 external_ref **slot;
7769 ref.type = die;
7770 slot = map->find_slot (&ref, INSERT);
7771 if (*slot != HTAB_EMPTY_ENTRY)
7772 return *slot;
7774 ref_p = XCNEW (struct external_ref);
7775 ref_p->type = die;
7776 *slot = ref_p;
7777 return ref_p;
7780 /* Subroutine of optimize_external_refs, below.
7782 If we see a type skeleton, record it as our stub. If we see external
7783 references, remember how many we've seen. */
7785 static void
7786 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7788 dw_die_ref c;
7789 dw_attr_ref a;
7790 unsigned ix;
7791 struct external_ref *ref_p;
7793 if (is_type_die (die)
7794 && (c = get_AT_ref (die, DW_AT_signature)))
7796 /* This is a local skeleton; use it for local references. */
7797 ref_p = lookup_external_ref (map, c);
7798 ref_p->stub = die;
7801 /* Scan the DIE references, and remember any that refer to DIEs from
7802 other CUs (i.e. those which are not marked). */
7803 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7804 if (AT_class (a) == dw_val_class_die_ref
7805 && (c = AT_ref (a))->die_mark == 0
7806 && is_type_die (c))
7808 ref_p = lookup_external_ref (map, c);
7809 ref_p->n_refs++;
7812 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7815 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7816 points to an external_ref, DATA is the CU we're processing. If we don't
7817 already have a local stub, and we have multiple refs, build a stub. */
7820 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7822 struct external_ref *ref_p = *slot;
7824 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7826 /* We have multiple references to this type, so build a small stub.
7827 Both of these forms are a bit dodgy from the perspective of the
7828 DWARF standard, since technically they should have names. */
7829 dw_die_ref cu = data;
7830 dw_die_ref type = ref_p->type;
7831 dw_die_ref stub = NULL;
7833 if (type->comdat_type_p)
7835 /* If we refer to this type via sig8, use AT_signature. */
7836 stub = new_die (type->die_tag, cu, NULL_TREE);
7837 add_AT_die_ref (stub, DW_AT_signature, type);
7839 else
7841 /* Otherwise, use a typedef with no name. */
7842 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7843 add_AT_die_ref (stub, DW_AT_type, type);
7846 stub->die_mark++;
7847 ref_p->stub = stub;
7849 return 1;
7852 /* DIE is a unit; look through all the DIE references to see if there are
7853 any external references to types, and if so, create local stubs for
7854 them which will be applied in build_abbrev_table. This is useful because
7855 references to local DIEs are smaller. */
7857 static external_ref_hash_type *
7858 optimize_external_refs (dw_die_ref die)
7860 external_ref_hash_type *map = new external_ref_hash_type (10);
7861 optimize_external_refs_1 (die, map);
7862 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7863 return map;
7866 /* The format of each DIE (and its attribute value pairs) is encoded in an
7867 abbreviation table. This routine builds the abbreviation table and assigns
7868 a unique abbreviation id for each abbreviation entry. The children of each
7869 die are visited recursively. */
7871 static void
7872 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7874 unsigned long abbrev_id;
7875 unsigned int n_alloc;
7876 dw_die_ref c;
7877 dw_attr_ref a;
7878 unsigned ix;
7880 /* Scan the DIE references, and replace any that refer to
7881 DIEs from other CUs (i.e. those which are not marked) with
7882 the local stubs we built in optimize_external_refs. */
7883 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7884 if (AT_class (a) == dw_val_class_die_ref
7885 && (c = AT_ref (a))->die_mark == 0)
7887 struct external_ref *ref_p;
7888 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7890 ref_p = lookup_external_ref (extern_map, c);
7891 if (ref_p->stub && ref_p->stub != die)
7892 change_AT_die_ref (a, ref_p->stub);
7893 else
7894 /* We aren't changing this reference, so mark it external. */
7895 set_AT_ref_external (a, 1);
7898 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7900 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7901 dw_attr_ref die_a, abbrev_a;
7902 unsigned ix;
7903 bool ok = true;
7905 if (abbrev->die_tag != die->die_tag)
7906 continue;
7907 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7908 continue;
7910 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7911 continue;
7913 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7915 abbrev_a = &(*abbrev->die_attr)[ix];
7916 if ((abbrev_a->dw_attr != die_a->dw_attr)
7917 || (value_format (abbrev_a) != value_format (die_a)))
7919 ok = false;
7920 break;
7923 if (ok)
7924 break;
7927 if (abbrev_id >= abbrev_die_table_in_use)
7929 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7931 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7932 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7933 n_alloc);
7935 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7936 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7937 abbrev_die_table_allocated = n_alloc;
7940 ++abbrev_die_table_in_use;
7941 abbrev_die_table[abbrev_id] = die;
7944 die->die_abbrev = abbrev_id;
7945 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7948 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7950 static int
7951 constant_size (unsigned HOST_WIDE_INT value)
7953 int log;
7955 if (value == 0)
7956 log = 0;
7957 else
7958 log = floor_log2 (value);
7960 log = log / 8;
7961 log = 1 << (floor_log2 (log) + 1);
7963 return log;
7966 /* Return the size of a DIE as it is represented in the
7967 .debug_info section. */
7969 static unsigned long
7970 size_of_die (dw_die_ref die)
7972 unsigned long size = 0;
7973 dw_attr_ref a;
7974 unsigned ix;
7975 enum dwarf_form form;
7977 size += size_of_uleb128 (die->die_abbrev);
7978 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7980 switch (AT_class (a))
7982 case dw_val_class_addr:
7983 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7985 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7986 size += size_of_uleb128 (AT_index (a));
7988 else
7989 size += DWARF2_ADDR_SIZE;
7990 break;
7991 case dw_val_class_offset:
7992 size += DWARF_OFFSET_SIZE;
7993 break;
7994 case dw_val_class_loc:
7996 unsigned long lsize = size_of_locs (AT_loc (a));
7998 /* Block length. */
7999 if (dwarf_version >= 4)
8000 size += size_of_uleb128 (lsize);
8001 else
8002 size += constant_size (lsize);
8003 size += lsize;
8005 break;
8006 case dw_val_class_loc_list:
8007 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8009 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8010 size += size_of_uleb128 (AT_index (a));
8012 else
8013 size += DWARF_OFFSET_SIZE;
8014 break;
8015 case dw_val_class_range_list:
8016 size += DWARF_OFFSET_SIZE;
8017 break;
8018 case dw_val_class_const:
8019 size += size_of_sleb128 (AT_int (a));
8020 break;
8021 case dw_val_class_unsigned_const:
8023 int csize = constant_size (AT_unsigned (a));
8024 if (dwarf_version == 3
8025 && a->dw_attr == DW_AT_data_member_location
8026 && csize >= 4)
8027 size += size_of_uleb128 (AT_unsigned (a));
8028 else
8029 size += csize;
8031 break;
8032 case dw_val_class_const_double:
8033 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8034 if (HOST_BITS_PER_WIDE_INT >= 64)
8035 size++; /* block */
8036 break;
8037 case dw_val_class_wide_int:
8038 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8039 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8040 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8041 > 64)
8042 size++; /* block */
8043 break;
8044 case dw_val_class_vec:
8045 size += constant_size (a->dw_attr_val.v.val_vec.length
8046 * a->dw_attr_val.v.val_vec.elt_size)
8047 + a->dw_attr_val.v.val_vec.length
8048 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8049 break;
8050 case dw_val_class_flag:
8051 if (dwarf_version >= 4)
8052 /* Currently all add_AT_flag calls pass in 1 as last argument,
8053 so DW_FORM_flag_present can be used. If that ever changes,
8054 we'll need to use DW_FORM_flag and have some optimization
8055 in build_abbrev_table that will change those to
8056 DW_FORM_flag_present if it is set to 1 in all DIEs using
8057 the same abbrev entry. */
8058 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8059 else
8060 size += 1;
8061 break;
8062 case dw_val_class_die_ref:
8063 if (AT_ref_external (a))
8065 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8066 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8067 is sized by target address length, whereas in DWARF3
8068 it's always sized as an offset. */
8069 if (use_debug_types)
8070 size += DWARF_TYPE_SIGNATURE_SIZE;
8071 else if (dwarf_version == 2)
8072 size += DWARF2_ADDR_SIZE;
8073 else
8074 size += DWARF_OFFSET_SIZE;
8076 else
8077 size += DWARF_OFFSET_SIZE;
8078 break;
8079 case dw_val_class_fde_ref:
8080 size += DWARF_OFFSET_SIZE;
8081 break;
8082 case dw_val_class_lbl_id:
8083 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8085 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8086 size += size_of_uleb128 (AT_index (a));
8088 else
8089 size += DWARF2_ADDR_SIZE;
8090 break;
8091 case dw_val_class_lineptr:
8092 case dw_val_class_macptr:
8093 size += DWARF_OFFSET_SIZE;
8094 break;
8095 case dw_val_class_str:
8096 form = AT_string_form (a);
8097 if (form == DW_FORM_strp)
8098 size += DWARF_OFFSET_SIZE;
8099 else if (form == DW_FORM_GNU_str_index)
8100 size += size_of_uleb128 (AT_index (a));
8101 else
8102 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8103 break;
8104 case dw_val_class_file:
8105 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8106 break;
8107 case dw_val_class_data8:
8108 size += 8;
8109 break;
8110 case dw_val_class_vms_delta:
8111 size += DWARF_OFFSET_SIZE;
8112 break;
8113 case dw_val_class_high_pc:
8114 size += DWARF2_ADDR_SIZE;
8115 break;
8116 default:
8117 gcc_unreachable ();
8121 return size;
8124 /* Size the debugging information associated with a given DIE. Visits the
8125 DIE's children recursively. Updates the global variable next_die_offset, on
8126 each time through. Uses the current value of next_die_offset to update the
8127 die_offset field in each DIE. */
8129 static void
8130 calc_die_sizes (dw_die_ref die)
8132 dw_die_ref c;
8134 gcc_assert (die->die_offset == 0
8135 || (unsigned long int) die->die_offset == next_die_offset);
8136 die->die_offset = next_die_offset;
8137 next_die_offset += size_of_die (die);
8139 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8141 if (die->die_child != NULL)
8142 /* Count the null byte used to terminate sibling lists. */
8143 next_die_offset += 1;
8146 /* Size just the base type children at the start of the CU.
8147 This is needed because build_abbrev needs to size locs
8148 and sizing of type based stack ops needs to know die_offset
8149 values for the base types. */
8151 static void
8152 calc_base_type_die_sizes (void)
8154 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8155 unsigned int i;
8156 dw_die_ref base_type;
8157 #if ENABLE_ASSERT_CHECKING
8158 dw_die_ref prev = comp_unit_die ()->die_child;
8159 #endif
8161 die_offset += size_of_die (comp_unit_die ());
8162 for (i = 0; base_types.iterate (i, &base_type); i++)
8164 #if ENABLE_ASSERT_CHECKING
8165 gcc_assert (base_type->die_offset == 0
8166 && prev->die_sib == base_type
8167 && base_type->die_child == NULL
8168 && base_type->die_abbrev);
8169 prev = base_type;
8170 #endif
8171 base_type->die_offset = die_offset;
8172 die_offset += size_of_die (base_type);
8176 /* Set the marks for a die and its children. We do this so
8177 that we know whether or not a reference needs to use FORM_ref_addr; only
8178 DIEs in the same CU will be marked. We used to clear out the offset
8179 and use that as the flag, but ran into ordering problems. */
8181 static void
8182 mark_dies (dw_die_ref die)
8184 dw_die_ref c;
8186 gcc_assert (!die->die_mark);
8188 die->die_mark = 1;
8189 FOR_EACH_CHILD (die, c, mark_dies (c));
8192 /* Clear the marks for a die and its children. */
8194 static void
8195 unmark_dies (dw_die_ref die)
8197 dw_die_ref c;
8199 if (! use_debug_types)
8200 gcc_assert (die->die_mark);
8202 die->die_mark = 0;
8203 FOR_EACH_CHILD (die, c, unmark_dies (c));
8206 /* Clear the marks for a die, its children and referred dies. */
8208 static void
8209 unmark_all_dies (dw_die_ref die)
8211 dw_die_ref c;
8212 dw_attr_ref a;
8213 unsigned ix;
8215 if (!die->die_mark)
8216 return;
8217 die->die_mark = 0;
8219 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8221 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8222 if (AT_class (a) == dw_val_class_die_ref)
8223 unmark_all_dies (AT_ref (a));
8226 /* Calculate if the entry should appear in the final output file. It may be
8227 from a pruned a type. */
8229 static bool
8230 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8232 /* By limiting gnu pubnames to definitions only, gold can generate a
8233 gdb index without entries for declarations, which don't include
8234 enough information to be useful. */
8235 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8236 return false;
8238 if (table == pubname_table)
8240 /* Enumerator names are part of the pubname table, but the
8241 parent DW_TAG_enumeration_type die may have been pruned.
8242 Don't output them if that is the case. */
8243 if (p->die->die_tag == DW_TAG_enumerator &&
8244 (p->die->die_parent == NULL
8245 || !p->die->die_parent->die_perennial_p))
8246 return false;
8248 /* Everything else in the pubname table is included. */
8249 return true;
8252 /* The pubtypes table shouldn't include types that have been
8253 pruned. */
8254 return (p->die->die_offset != 0
8255 || !flag_eliminate_unused_debug_types);
8258 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8259 generated for the compilation unit. */
8261 static unsigned long
8262 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8264 unsigned long size;
8265 unsigned i;
8266 pubname_ref p;
8267 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8269 size = DWARF_PUBNAMES_HEADER_SIZE;
8270 FOR_EACH_VEC_ELT (*names, i, p)
8271 if (include_pubname_in_output (names, p))
8272 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8274 size += DWARF_OFFSET_SIZE;
8275 return size;
8278 /* Return the size of the information in the .debug_aranges section. */
8280 static unsigned long
8281 size_of_aranges (void)
8283 unsigned long size;
8285 size = DWARF_ARANGES_HEADER_SIZE;
8287 /* Count the address/length pair for this compilation unit. */
8288 if (text_section_used)
8289 size += 2 * DWARF2_ADDR_SIZE;
8290 if (cold_text_section_used)
8291 size += 2 * DWARF2_ADDR_SIZE;
8292 if (have_multiple_function_sections)
8294 unsigned fde_idx;
8295 dw_fde_ref fde;
8297 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8299 if (DECL_IGNORED_P (fde->decl))
8300 continue;
8301 if (!fde->in_std_section)
8302 size += 2 * DWARF2_ADDR_SIZE;
8303 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8304 size += 2 * DWARF2_ADDR_SIZE;
8308 /* Count the two zero words used to terminated the address range table. */
8309 size += 2 * DWARF2_ADDR_SIZE;
8310 return size;
8313 /* Select the encoding of an attribute value. */
8315 static enum dwarf_form
8316 value_format (dw_attr_ref a)
8318 switch (AT_class (a))
8320 case dw_val_class_addr:
8321 /* Only very few attributes allow DW_FORM_addr. */
8322 switch (a->dw_attr)
8324 case DW_AT_low_pc:
8325 case DW_AT_high_pc:
8326 case DW_AT_entry_pc:
8327 case DW_AT_trampoline:
8328 return (AT_index (a) == NOT_INDEXED
8329 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8330 default:
8331 break;
8333 switch (DWARF2_ADDR_SIZE)
8335 case 1:
8336 return DW_FORM_data1;
8337 case 2:
8338 return DW_FORM_data2;
8339 case 4:
8340 return DW_FORM_data4;
8341 case 8:
8342 return DW_FORM_data8;
8343 default:
8344 gcc_unreachable ();
8346 case dw_val_class_range_list:
8347 case dw_val_class_loc_list:
8348 if (dwarf_version >= 4)
8349 return DW_FORM_sec_offset;
8350 /* FALLTHRU */
8351 case dw_val_class_vms_delta:
8352 case dw_val_class_offset:
8353 switch (DWARF_OFFSET_SIZE)
8355 case 4:
8356 return DW_FORM_data4;
8357 case 8:
8358 return DW_FORM_data8;
8359 default:
8360 gcc_unreachable ();
8362 case dw_val_class_loc:
8363 if (dwarf_version >= 4)
8364 return DW_FORM_exprloc;
8365 switch (constant_size (size_of_locs (AT_loc (a))))
8367 case 1:
8368 return DW_FORM_block1;
8369 case 2:
8370 return DW_FORM_block2;
8371 case 4:
8372 return DW_FORM_block4;
8373 default:
8374 gcc_unreachable ();
8376 case dw_val_class_const:
8377 return DW_FORM_sdata;
8378 case dw_val_class_unsigned_const:
8379 switch (constant_size (AT_unsigned (a)))
8381 case 1:
8382 return DW_FORM_data1;
8383 case 2:
8384 return DW_FORM_data2;
8385 case 4:
8386 /* In DWARF3 DW_AT_data_member_location with
8387 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8388 constant, so we need to use DW_FORM_udata if we need
8389 a large constant. */
8390 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8391 return DW_FORM_udata;
8392 return DW_FORM_data4;
8393 case 8:
8394 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8395 return DW_FORM_udata;
8396 return DW_FORM_data8;
8397 default:
8398 gcc_unreachable ();
8400 case dw_val_class_const_double:
8401 switch (HOST_BITS_PER_WIDE_INT)
8403 case 8:
8404 return DW_FORM_data2;
8405 case 16:
8406 return DW_FORM_data4;
8407 case 32:
8408 return DW_FORM_data8;
8409 case 64:
8410 default:
8411 return DW_FORM_block1;
8413 case dw_val_class_wide_int:
8414 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8416 case 8:
8417 return DW_FORM_data1;
8418 case 16:
8419 return DW_FORM_data2;
8420 case 32:
8421 return DW_FORM_data4;
8422 case 64:
8423 return DW_FORM_data8;
8424 default:
8425 return DW_FORM_block1;
8427 case dw_val_class_vec:
8428 switch (constant_size (a->dw_attr_val.v.val_vec.length
8429 * a->dw_attr_val.v.val_vec.elt_size))
8431 case 1:
8432 return DW_FORM_block1;
8433 case 2:
8434 return DW_FORM_block2;
8435 case 4:
8436 return DW_FORM_block4;
8437 default:
8438 gcc_unreachable ();
8440 case dw_val_class_flag:
8441 if (dwarf_version >= 4)
8443 /* Currently all add_AT_flag calls pass in 1 as last argument,
8444 so DW_FORM_flag_present can be used. If that ever changes,
8445 we'll need to use DW_FORM_flag and have some optimization
8446 in build_abbrev_table that will change those to
8447 DW_FORM_flag_present if it is set to 1 in all DIEs using
8448 the same abbrev entry. */
8449 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8450 return DW_FORM_flag_present;
8452 return DW_FORM_flag;
8453 case dw_val_class_die_ref:
8454 if (AT_ref_external (a))
8455 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8456 else
8457 return DW_FORM_ref;
8458 case dw_val_class_fde_ref:
8459 return DW_FORM_data;
8460 case dw_val_class_lbl_id:
8461 return (AT_index (a) == NOT_INDEXED
8462 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8463 case dw_val_class_lineptr:
8464 case dw_val_class_macptr:
8465 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8466 case dw_val_class_str:
8467 return AT_string_form (a);
8468 case dw_val_class_file:
8469 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8471 case 1:
8472 return DW_FORM_data1;
8473 case 2:
8474 return DW_FORM_data2;
8475 case 4:
8476 return DW_FORM_data4;
8477 default:
8478 gcc_unreachable ();
8481 case dw_val_class_data8:
8482 return DW_FORM_data8;
8484 case dw_val_class_high_pc:
8485 switch (DWARF2_ADDR_SIZE)
8487 case 1:
8488 return DW_FORM_data1;
8489 case 2:
8490 return DW_FORM_data2;
8491 case 4:
8492 return DW_FORM_data4;
8493 case 8:
8494 return DW_FORM_data8;
8495 default:
8496 gcc_unreachable ();
8499 default:
8500 gcc_unreachable ();
8504 /* Output the encoding of an attribute value. */
8506 static void
8507 output_value_format (dw_attr_ref a)
8509 enum dwarf_form form = value_format (a);
8511 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8514 /* Given a die and id, produce the appropriate abbreviations. */
8516 static void
8517 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8519 unsigned ix;
8520 dw_attr_ref a_attr;
8522 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8523 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8524 dwarf_tag_name (abbrev->die_tag));
8526 if (abbrev->die_child != NULL)
8527 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8528 else
8529 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8531 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8533 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8534 dwarf_attr_name (a_attr->dw_attr));
8535 output_value_format (a_attr);
8538 dw2_asm_output_data (1, 0, NULL);
8539 dw2_asm_output_data (1, 0, NULL);
8543 /* Output the .debug_abbrev section which defines the DIE abbreviation
8544 table. */
8546 static void
8547 output_abbrev_section (void)
8549 unsigned long abbrev_id;
8551 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8552 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8554 /* Terminate the table. */
8555 dw2_asm_output_data (1, 0, NULL);
8558 /* Output a symbol we can use to refer to this DIE from another CU. */
8560 static inline void
8561 output_die_symbol (dw_die_ref die)
8563 const char *sym = die->die_id.die_symbol;
8565 gcc_assert (!die->comdat_type_p);
8567 if (sym == 0)
8568 return;
8570 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8571 /* We make these global, not weak; if the target doesn't support
8572 .linkonce, it doesn't support combining the sections, so debugging
8573 will break. */
8574 targetm.asm_out.globalize_label (asm_out_file, sym);
8576 ASM_OUTPUT_LABEL (asm_out_file, sym);
8579 /* Return a new location list, given the begin and end range, and the
8580 expression. */
8582 static inline dw_loc_list_ref
8583 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8584 const char *section)
8586 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8588 retlist->begin = begin;
8589 retlist->begin_entry = NULL;
8590 retlist->end = end;
8591 retlist->expr = expr;
8592 retlist->section = section;
8594 return retlist;
8597 /* Generate a new internal symbol for this location list node, if it
8598 hasn't got one yet. */
8600 static inline void
8601 gen_llsym (dw_loc_list_ref list)
8603 gcc_assert (!list->ll_symbol);
8604 list->ll_symbol = gen_internal_sym ("LLST");
8607 /* Output the location list given to us. */
8609 static void
8610 output_loc_list (dw_loc_list_ref list_head)
8612 dw_loc_list_ref curr = list_head;
8614 if (list_head->emitted)
8615 return;
8616 list_head->emitted = true;
8618 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8620 /* Walk the location list, and output each range + expression. */
8621 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8623 unsigned long size;
8624 /* Don't output an entry that starts and ends at the same address. */
8625 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8626 continue;
8627 size = size_of_locs (curr->expr);
8628 /* If the expression is too large, drop it on the floor. We could
8629 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8630 in the expression, but >= 64KB expressions for a single value
8631 in a single range are unlikely very useful. */
8632 if (size > 0xffff)
8633 continue;
8634 if (dwarf_split_debug_info)
8636 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8637 "Location list start/length entry (%s)",
8638 list_head->ll_symbol);
8639 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8640 "Location list range start index (%s)",
8641 curr->begin);
8642 /* The length field is 4 bytes. If we ever need to support
8643 an 8-byte length, we can add a new DW_LLE code or fall back
8644 to DW_LLE_GNU_start_end_entry. */
8645 dw2_asm_output_delta (4, curr->end, curr->begin,
8646 "Location list range length (%s)",
8647 list_head->ll_symbol);
8649 else if (!have_multiple_function_sections)
8651 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8652 "Location list begin address (%s)",
8653 list_head->ll_symbol);
8654 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8655 "Location list end address (%s)",
8656 list_head->ll_symbol);
8658 else
8660 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8661 "Location list begin address (%s)",
8662 list_head->ll_symbol);
8663 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8664 "Location list end address (%s)",
8665 list_head->ll_symbol);
8668 /* Output the block length for this list of location operations. */
8669 gcc_assert (size <= 0xffff);
8670 dw2_asm_output_data (2, size, "%s", "Location expression size");
8672 output_loc_sequence (curr->expr, -1);
8675 if (dwarf_split_debug_info)
8676 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8677 "Location list terminator (%s)",
8678 list_head->ll_symbol);
8679 else
8681 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8682 "Location list terminator begin (%s)",
8683 list_head->ll_symbol);
8684 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8685 "Location list terminator end (%s)",
8686 list_head->ll_symbol);
8690 /* Output a range_list offset into the debug_range section. Emit a
8691 relocated reference if val_entry is NULL, otherwise, emit an
8692 indirect reference. */
8694 static void
8695 output_range_list_offset (dw_attr_ref a)
8697 const char *name = dwarf_attr_name (a->dw_attr);
8699 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8701 char *p = strchr (ranges_section_label, '\0');
8702 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8703 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8704 debug_ranges_section, "%s", name);
8705 *p = '\0';
8707 else
8708 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8709 "%s (offset from %s)", name, ranges_section_label);
8712 /* Output the offset into the debug_loc section. */
8714 static void
8715 output_loc_list_offset (dw_attr_ref a)
8717 char *sym = AT_loc_list (a)->ll_symbol;
8719 gcc_assert (sym);
8720 if (dwarf_split_debug_info)
8721 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8722 "%s", dwarf_attr_name (a->dw_attr));
8723 else
8724 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8725 "%s", dwarf_attr_name (a->dw_attr));
8728 /* Output an attribute's index or value appropriately. */
8730 static void
8731 output_attr_index_or_value (dw_attr_ref a)
8733 const char *name = dwarf_attr_name (a->dw_attr);
8735 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8737 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8738 return;
8740 switch (AT_class (a))
8742 case dw_val_class_addr:
8743 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8744 break;
8745 case dw_val_class_high_pc:
8746 case dw_val_class_lbl_id:
8747 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8748 break;
8749 case dw_val_class_loc_list:
8750 output_loc_list_offset (a);
8751 break;
8752 default:
8753 gcc_unreachable ();
8757 /* Output a type signature. */
8759 static inline void
8760 output_signature (const char *sig, const char *name)
8762 int i;
8764 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8765 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8768 /* Output the DIE and its attributes. Called recursively to generate
8769 the definitions of each child DIE. */
8771 static void
8772 output_die (dw_die_ref die)
8774 dw_attr_ref a;
8775 dw_die_ref c;
8776 unsigned long size;
8777 unsigned ix;
8779 /* If someone in another CU might refer to us, set up a symbol for
8780 them to point to. */
8781 if (! die->comdat_type_p && die->die_id.die_symbol)
8782 output_die_symbol (die);
8784 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8785 (unsigned long)die->die_offset,
8786 dwarf_tag_name (die->die_tag));
8788 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8790 const char *name = dwarf_attr_name (a->dw_attr);
8792 switch (AT_class (a))
8794 case dw_val_class_addr:
8795 output_attr_index_or_value (a);
8796 break;
8798 case dw_val_class_offset:
8799 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8800 "%s", name);
8801 break;
8803 case dw_val_class_range_list:
8804 output_range_list_offset (a);
8805 break;
8807 case dw_val_class_loc:
8808 size = size_of_locs (AT_loc (a));
8810 /* Output the block length for this list of location operations. */
8811 if (dwarf_version >= 4)
8812 dw2_asm_output_data_uleb128 (size, "%s", name);
8813 else
8814 dw2_asm_output_data (constant_size (size), size, "%s", name);
8816 output_loc_sequence (AT_loc (a), -1);
8817 break;
8819 case dw_val_class_const:
8820 /* ??? It would be slightly more efficient to use a scheme like is
8821 used for unsigned constants below, but gdb 4.x does not sign
8822 extend. Gdb 5.x does sign extend. */
8823 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8824 break;
8826 case dw_val_class_unsigned_const:
8828 int csize = constant_size (AT_unsigned (a));
8829 if (dwarf_version == 3
8830 && a->dw_attr == DW_AT_data_member_location
8831 && csize >= 4)
8832 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8833 else
8834 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8836 break;
8838 case dw_val_class_const_double:
8840 unsigned HOST_WIDE_INT first, second;
8842 if (HOST_BITS_PER_WIDE_INT >= 64)
8843 dw2_asm_output_data (1,
8844 HOST_BITS_PER_DOUBLE_INT
8845 / HOST_BITS_PER_CHAR,
8846 NULL);
8848 if (WORDS_BIG_ENDIAN)
8850 first = a->dw_attr_val.v.val_double.high;
8851 second = a->dw_attr_val.v.val_double.low;
8853 else
8855 first = a->dw_attr_val.v.val_double.low;
8856 second = a->dw_attr_val.v.val_double.high;
8859 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8860 first, "%s", name);
8861 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8862 second, NULL);
8864 break;
8866 case dw_val_class_wide_int:
8868 int i;
8869 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8870 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8871 if (len * HOST_BITS_PER_WIDE_INT > 64)
8872 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8873 NULL);
8875 if (WORDS_BIG_ENDIAN)
8876 for (i = len - 1; i >= 0; --i)
8878 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8879 name);
8880 name = NULL;
8882 else
8883 for (i = 0; i < len; ++i)
8885 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8886 name);
8887 name = NULL;
8890 break;
8892 case dw_val_class_vec:
8894 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8895 unsigned int len = a->dw_attr_val.v.val_vec.length;
8896 unsigned int i;
8897 unsigned char *p;
8899 dw2_asm_output_data (constant_size (len * elt_size),
8900 len * elt_size, "%s", name);
8901 if (elt_size > sizeof (HOST_WIDE_INT))
8903 elt_size /= 2;
8904 len *= 2;
8906 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8907 i < len;
8908 i++, p += elt_size)
8909 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8910 "fp or vector constant word %u", i);
8911 break;
8914 case dw_val_class_flag:
8915 if (dwarf_version >= 4)
8917 /* Currently all add_AT_flag calls pass in 1 as last argument,
8918 so DW_FORM_flag_present can be used. If that ever changes,
8919 we'll need to use DW_FORM_flag and have some optimization
8920 in build_abbrev_table that will change those to
8921 DW_FORM_flag_present if it is set to 1 in all DIEs using
8922 the same abbrev entry. */
8923 gcc_assert (AT_flag (a) == 1);
8924 if (flag_debug_asm)
8925 fprintf (asm_out_file, "\t\t\t%s %s\n",
8926 ASM_COMMENT_START, name);
8927 break;
8929 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8930 break;
8932 case dw_val_class_loc_list:
8933 output_attr_index_or_value (a);
8934 break;
8936 case dw_val_class_die_ref:
8937 if (AT_ref_external (a))
8939 if (AT_ref (a)->comdat_type_p)
8941 comdat_type_node_ref type_node =
8942 AT_ref (a)->die_id.die_type_node;
8944 gcc_assert (type_node);
8945 output_signature (type_node->signature, name);
8947 else
8949 const char *sym = AT_ref (a)->die_id.die_symbol;
8950 int size;
8952 gcc_assert (sym);
8953 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8954 length, whereas in DWARF3 it's always sized as an
8955 offset. */
8956 if (dwarf_version == 2)
8957 size = DWARF2_ADDR_SIZE;
8958 else
8959 size = DWARF_OFFSET_SIZE;
8960 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8961 name);
8964 else
8966 gcc_assert (AT_ref (a)->die_offset);
8967 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8968 "%s", name);
8970 break;
8972 case dw_val_class_fde_ref:
8974 char l1[20];
8976 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8977 a->dw_attr_val.v.val_fde_index * 2);
8978 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8979 "%s", name);
8981 break;
8983 case dw_val_class_vms_delta:
8984 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8985 AT_vms_delta2 (a), AT_vms_delta1 (a),
8986 "%s", name);
8987 break;
8989 case dw_val_class_lbl_id:
8990 output_attr_index_or_value (a);
8991 break;
8993 case dw_val_class_lineptr:
8994 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8995 debug_line_section, "%s", name);
8996 break;
8998 case dw_val_class_macptr:
8999 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9000 debug_macinfo_section, "%s", name);
9001 break;
9003 case dw_val_class_str:
9004 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9005 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9006 a->dw_attr_val.v.val_str->label,
9007 debug_str_section,
9008 "%s: \"%s\"", name, AT_string (a));
9009 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9010 dw2_asm_output_data_uleb128 (AT_index (a),
9011 "%s: \"%s\"", name, AT_string (a));
9012 else
9013 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9014 break;
9016 case dw_val_class_file:
9018 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9020 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9021 a->dw_attr_val.v.val_file->filename);
9022 break;
9025 case dw_val_class_data8:
9027 int i;
9029 for (i = 0; i < 8; i++)
9030 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9031 i == 0 ? "%s" : NULL, name);
9032 break;
9035 case dw_val_class_high_pc:
9036 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9037 get_AT_low_pc (die), "DW_AT_high_pc");
9038 break;
9040 default:
9041 gcc_unreachable ();
9045 FOR_EACH_CHILD (die, c, output_die (c));
9047 /* Add null byte to terminate sibling list. */
9048 if (die->die_child != NULL)
9049 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9050 (unsigned long) die->die_offset);
9053 /* Output the compilation unit that appears at the beginning of the
9054 .debug_info section, and precedes the DIE descriptions. */
9056 static void
9057 output_compilation_unit_header (void)
9059 /* We don't support actual DWARFv5 units yet, we just use some
9060 DWARFv5 draft DIE tags in DWARFv4 format. */
9061 int ver = dwarf_version < 5 ? dwarf_version : 4;
9063 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9064 dw2_asm_output_data (4, 0xffffffff,
9065 "Initial length escape value indicating 64-bit DWARF extension");
9066 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9067 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9068 "Length of Compilation Unit Info");
9069 dw2_asm_output_data (2, ver, "DWARF version number");
9070 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9071 debug_abbrev_section,
9072 "Offset Into Abbrev. Section");
9073 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9076 /* Output the compilation unit DIE and its children. */
9078 static void
9079 output_comp_unit (dw_die_ref die, int output_if_empty)
9081 const char *secname, *oldsym;
9082 char *tmp;
9084 /* Unless we are outputting main CU, we may throw away empty ones. */
9085 if (!output_if_empty && die->die_child == NULL)
9086 return;
9088 /* Even if there are no children of this DIE, we must output the information
9089 about the compilation unit. Otherwise, on an empty translation unit, we
9090 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9091 will then complain when examining the file. First mark all the DIEs in
9092 this CU so we know which get local refs. */
9093 mark_dies (die);
9095 external_ref_hash_type *extern_map = optimize_external_refs (die);
9097 build_abbrev_table (die, extern_map);
9099 delete extern_map;
9101 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9102 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9103 calc_die_sizes (die);
9105 oldsym = die->die_id.die_symbol;
9106 if (oldsym)
9108 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9110 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9111 secname = tmp;
9112 die->die_id.die_symbol = NULL;
9113 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9115 else
9117 switch_to_section (debug_info_section);
9118 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9119 info_section_emitted = true;
9122 /* Output debugging information. */
9123 output_compilation_unit_header ();
9124 output_die (die);
9126 /* Leave the marks on the main CU, so we can check them in
9127 output_pubnames. */
9128 if (oldsym)
9130 unmark_dies (die);
9131 die->die_id.die_symbol = oldsym;
9135 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9136 and .debug_pubtypes. This is configured per-target, but can be
9137 overridden by the -gpubnames or -gno-pubnames options. */
9139 static inline bool
9140 want_pubnames (void)
9142 if (debug_info_level <= DINFO_LEVEL_TERSE)
9143 return false;
9144 if (debug_generate_pub_sections != -1)
9145 return debug_generate_pub_sections;
9146 return targetm.want_debug_pub_sections;
9149 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9151 static void
9152 add_AT_pubnames (dw_die_ref die)
9154 if (want_pubnames ())
9155 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9158 /* Add a string attribute value to a skeleton DIE. */
9160 static inline void
9161 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9162 const char *str)
9164 dw_attr_node attr;
9165 struct indirect_string_node *node;
9167 if (! skeleton_debug_str_hash)
9168 skeleton_debug_str_hash
9169 = hash_table<indirect_string_hasher>::create_ggc (10);
9171 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9172 find_string_form (node);
9173 if (node->form == DW_FORM_GNU_str_index)
9174 node->form = DW_FORM_strp;
9176 attr.dw_attr = attr_kind;
9177 attr.dw_attr_val.val_class = dw_val_class_str;
9178 attr.dw_attr_val.val_entry = NULL;
9179 attr.dw_attr_val.v.val_str = node;
9180 add_dwarf_attr (die, &attr);
9183 /* Helper function to generate top-level dies for skeleton debug_info and
9184 debug_types. */
9186 static void
9187 add_top_level_skeleton_die_attrs (dw_die_ref die)
9189 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9190 const char *comp_dir = comp_dir_string ();
9192 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9193 if (comp_dir != NULL)
9194 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9195 add_AT_pubnames (die);
9196 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9199 /* Output skeleton debug sections that point to the dwo file. */
9201 static void
9202 output_skeleton_debug_sections (dw_die_ref comp_unit)
9204 /* We don't support actual DWARFv5 units yet, we just use some
9205 DWARFv5 draft DIE tags in DWARFv4 format. */
9206 int ver = dwarf_version < 5 ? dwarf_version : 4;
9208 /* These attributes will be found in the full debug_info section. */
9209 remove_AT (comp_unit, DW_AT_producer);
9210 remove_AT (comp_unit, DW_AT_language);
9212 switch_to_section (debug_skeleton_info_section);
9213 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9215 /* Produce the skeleton compilation-unit header. This one differs enough from
9216 a normal CU header that it's better not to call output_compilation_unit
9217 header. */
9218 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9219 dw2_asm_output_data (4, 0xffffffff,
9220 "Initial length escape value indicating 64-bit DWARF extension");
9222 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9223 DWARF_COMPILE_UNIT_HEADER_SIZE
9224 - DWARF_INITIAL_LENGTH_SIZE
9225 + size_of_die (comp_unit),
9226 "Length of Compilation Unit Info");
9227 dw2_asm_output_data (2, ver, "DWARF version number");
9228 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9229 debug_abbrev_section,
9230 "Offset Into Abbrev. Section");
9231 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9233 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9234 output_die (comp_unit);
9236 /* Build the skeleton debug_abbrev section. */
9237 switch_to_section (debug_skeleton_abbrev_section);
9238 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9240 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9242 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9245 /* Output a comdat type unit DIE and its children. */
9247 static void
9248 output_comdat_type_unit (comdat_type_node *node)
9250 const char *secname;
9251 char *tmp;
9252 int i;
9253 #if defined (OBJECT_FORMAT_ELF)
9254 tree comdat_key;
9255 #endif
9257 /* First mark all the DIEs in this CU so we know which get local refs. */
9258 mark_dies (node->root_die);
9260 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9262 build_abbrev_table (node->root_die, extern_map);
9264 delete extern_map;
9265 extern_map = NULL;
9267 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9268 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9269 calc_die_sizes (node->root_die);
9271 #if defined (OBJECT_FORMAT_ELF)
9272 if (!dwarf_split_debug_info)
9273 secname = ".debug_types";
9274 else
9275 secname = ".debug_types.dwo";
9277 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9278 sprintf (tmp, "wt.");
9279 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9280 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9281 comdat_key = get_identifier (tmp);
9282 targetm.asm_out.named_section (secname,
9283 SECTION_DEBUG | SECTION_LINKONCE,
9284 comdat_key);
9285 #else
9286 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9287 sprintf (tmp, ".gnu.linkonce.wt.");
9288 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9289 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9290 secname = tmp;
9291 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9292 #endif
9294 /* Output debugging information. */
9295 output_compilation_unit_header ();
9296 output_signature (node->signature, "Type Signature");
9297 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9298 "Offset to Type DIE");
9299 output_die (node->root_die);
9301 unmark_dies (node->root_die);
9304 /* Return the DWARF2/3 pubname associated with a decl. */
9306 static const char *
9307 dwarf2_name (tree decl, int scope)
9309 if (DECL_NAMELESS (decl))
9310 return NULL;
9311 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9314 /* Add a new entry to .debug_pubnames if appropriate. */
9316 static void
9317 add_pubname_string (const char *str, dw_die_ref die)
9319 pubname_entry e;
9321 e.die = die;
9322 e.name = xstrdup (str);
9323 vec_safe_push (pubname_table, e);
9326 static void
9327 add_pubname (tree decl, dw_die_ref die)
9329 if (!want_pubnames ())
9330 return;
9332 /* Don't add items to the table when we expect that the consumer will have
9333 just read the enclosing die. For example, if the consumer is looking at a
9334 class_member, it will either be inside the class already, or will have just
9335 looked up the class to find the member. Either way, searching the class is
9336 faster than searching the index. */
9337 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9338 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9340 const char *name = dwarf2_name (decl, 1);
9342 if (name)
9343 add_pubname_string (name, die);
9347 /* Add an enumerator to the pubnames section. */
9349 static void
9350 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9352 pubname_entry e;
9354 gcc_assert (scope_name);
9355 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9356 e.die = die;
9357 vec_safe_push (pubname_table, e);
9360 /* Add a new entry to .debug_pubtypes if appropriate. */
9362 static void
9363 add_pubtype (tree decl, dw_die_ref die)
9365 pubname_entry e;
9367 if (!want_pubnames ())
9368 return;
9370 if ((TREE_PUBLIC (decl)
9371 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9372 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9374 tree scope = NULL;
9375 const char *scope_name = "";
9376 const char *sep = is_cxx () ? "::" : ".";
9377 const char *name;
9379 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9380 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9382 scope_name = lang_hooks.dwarf_name (scope, 1);
9383 if (scope_name != NULL && scope_name[0] != '\0')
9384 scope_name = concat (scope_name, sep, NULL);
9385 else
9386 scope_name = "";
9389 if (TYPE_P (decl))
9390 name = type_tag (decl);
9391 else
9392 name = lang_hooks.dwarf_name (decl, 1);
9394 /* If we don't have a name for the type, there's no point in adding
9395 it to the table. */
9396 if (name != NULL && name[0] != '\0')
9398 e.die = die;
9399 e.name = concat (scope_name, name, NULL);
9400 vec_safe_push (pubtype_table, e);
9403 /* Although it might be more consistent to add the pubinfo for the
9404 enumerators as their dies are created, they should only be added if the
9405 enum type meets the criteria above. So rather than re-check the parent
9406 enum type whenever an enumerator die is created, just output them all
9407 here. This isn't protected by the name conditional because anonymous
9408 enums don't have names. */
9409 if (die->die_tag == DW_TAG_enumeration_type)
9411 dw_die_ref c;
9413 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9418 /* Output a single entry in the pubnames table. */
9420 static void
9421 output_pubname (dw_offset die_offset, pubname_entry *entry)
9423 dw_die_ref die = entry->die;
9424 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9426 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9428 if (debug_generate_pub_sections == 2)
9430 /* This logic follows gdb's method for determining the value of the flag
9431 byte. */
9432 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9433 switch (die->die_tag)
9435 case DW_TAG_typedef:
9436 case DW_TAG_base_type:
9437 case DW_TAG_subrange_type:
9438 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9439 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9440 break;
9441 case DW_TAG_enumerator:
9442 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9443 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9444 if (!is_cxx () && !is_java ())
9445 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9446 break;
9447 case DW_TAG_subprogram:
9448 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9449 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9450 if (!is_ada ())
9451 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9452 break;
9453 case DW_TAG_constant:
9454 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9455 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9456 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9457 break;
9458 case DW_TAG_variable:
9459 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9460 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9461 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9462 break;
9463 case DW_TAG_namespace:
9464 case DW_TAG_imported_declaration:
9465 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9466 break;
9467 case DW_TAG_class_type:
9468 case DW_TAG_interface_type:
9469 case DW_TAG_structure_type:
9470 case DW_TAG_union_type:
9471 case DW_TAG_enumeration_type:
9472 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9473 if (!is_cxx () && !is_java ())
9474 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9475 break;
9476 default:
9477 /* An unusual tag. Leave the flag-byte empty. */
9478 break;
9480 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9481 "GDB-index flags");
9484 dw2_asm_output_nstring (entry->name, -1, "external name");
9488 /* Output the public names table used to speed up access to externally
9489 visible names; or the public types table used to find type definitions. */
9491 static void
9492 output_pubnames (vec<pubname_entry, va_gc> *names)
9494 unsigned i;
9495 unsigned long pubnames_length = size_of_pubnames (names);
9496 pubname_ref pub;
9498 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9499 dw2_asm_output_data (4, 0xffffffff,
9500 "Initial length escape value indicating 64-bit DWARF extension");
9501 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9503 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9504 dw2_asm_output_data (2, 2, "DWARF Version");
9506 if (dwarf_split_debug_info)
9507 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9508 debug_skeleton_info_section,
9509 "Offset of Compilation Unit Info");
9510 else
9511 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9512 debug_info_section,
9513 "Offset of Compilation Unit Info");
9514 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9515 "Compilation Unit Length");
9517 FOR_EACH_VEC_ELT (*names, i, pub)
9519 if (include_pubname_in_output (names, pub))
9521 dw_offset die_offset = pub->die->die_offset;
9523 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9524 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9525 gcc_assert (pub->die->die_mark);
9527 /* If we're putting types in their own .debug_types sections,
9528 the .debug_pubtypes table will still point to the compile
9529 unit (not the type unit), so we want to use the offset of
9530 the skeleton DIE (if there is one). */
9531 if (pub->die->comdat_type_p && names == pubtype_table)
9533 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9535 if (type_node != NULL)
9536 die_offset = (type_node->skeleton_die != NULL
9537 ? type_node->skeleton_die->die_offset
9538 : comp_unit_die ()->die_offset);
9541 output_pubname (die_offset, pub);
9545 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9548 /* Output public names and types tables if necessary. */
9550 static void
9551 output_pubtables (void)
9553 if (!want_pubnames () || !info_section_emitted)
9554 return;
9556 switch_to_section (debug_pubnames_section);
9557 output_pubnames (pubname_table);
9558 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9559 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9560 simply won't look for the section. */
9561 switch_to_section (debug_pubtypes_section);
9562 output_pubnames (pubtype_table);
9566 /* Output the information that goes into the .debug_aranges table.
9567 Namely, define the beginning and ending address range of the
9568 text section generated for this compilation unit. */
9570 static void
9571 output_aranges (unsigned long aranges_length)
9573 unsigned i;
9575 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9576 dw2_asm_output_data (4, 0xffffffff,
9577 "Initial length escape value indicating 64-bit DWARF extension");
9578 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9579 "Length of Address Ranges Info");
9580 /* Version number for aranges is still 2, even up to DWARF5. */
9581 dw2_asm_output_data (2, 2, "DWARF Version");
9582 if (dwarf_split_debug_info)
9583 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9584 debug_skeleton_info_section,
9585 "Offset of Compilation Unit Info");
9586 else
9587 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9588 debug_info_section,
9589 "Offset of Compilation Unit Info");
9590 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9591 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9593 /* We need to align to twice the pointer size here. */
9594 if (DWARF_ARANGES_PAD_SIZE)
9596 /* Pad using a 2 byte words so that padding is correct for any
9597 pointer size. */
9598 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9599 2 * DWARF2_ADDR_SIZE);
9600 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9601 dw2_asm_output_data (2, 0, NULL);
9604 /* It is necessary not to output these entries if the sections were
9605 not used; if the sections were not used, the length will be 0 and
9606 the address may end up as 0 if the section is discarded by ld
9607 --gc-sections, leaving an invalid (0, 0) entry that can be
9608 confused with the terminator. */
9609 if (text_section_used)
9611 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9612 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9613 text_section_label, "Length");
9615 if (cold_text_section_used)
9617 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9618 "Address");
9619 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9620 cold_text_section_label, "Length");
9623 if (have_multiple_function_sections)
9625 unsigned fde_idx;
9626 dw_fde_ref fde;
9628 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9630 if (DECL_IGNORED_P (fde->decl))
9631 continue;
9632 if (!fde->in_std_section)
9634 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9635 "Address");
9636 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9637 fde->dw_fde_begin, "Length");
9639 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9641 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9642 "Address");
9643 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9644 fde->dw_fde_second_begin, "Length");
9649 /* Output the terminator words. */
9650 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9651 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9654 /* Add a new entry to .debug_ranges. Return the offset at which it
9655 was placed. */
9657 static unsigned int
9658 add_ranges_num (int num)
9660 unsigned int in_use = ranges_table_in_use;
9662 if (in_use == ranges_table_allocated)
9664 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9665 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9666 ranges_table_allocated);
9667 memset (ranges_table + ranges_table_in_use, 0,
9668 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9671 ranges_table[in_use].num = num;
9672 ranges_table_in_use = in_use + 1;
9674 return in_use * 2 * DWARF2_ADDR_SIZE;
9677 /* Add a new entry to .debug_ranges corresponding to a block, or a
9678 range terminator if BLOCK is NULL. */
9680 static unsigned int
9681 add_ranges (const_tree block)
9683 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9686 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9687 When using dwarf_split_debug_info, address attributes in dies destined
9688 for the final executable should be direct references--setting the
9689 parameter force_direct ensures this behavior. */
9691 static void
9692 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9693 bool *added, bool force_direct)
9695 unsigned int in_use = ranges_by_label_in_use;
9696 unsigned int offset;
9698 if (in_use == ranges_by_label_allocated)
9700 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9701 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9702 ranges_by_label,
9703 ranges_by_label_allocated);
9704 memset (ranges_by_label + ranges_by_label_in_use, 0,
9705 RANGES_TABLE_INCREMENT
9706 * sizeof (struct dw_ranges_by_label_struct));
9709 ranges_by_label[in_use].begin = begin;
9710 ranges_by_label[in_use].end = end;
9711 ranges_by_label_in_use = in_use + 1;
9713 offset = add_ranges_num (-(int)in_use - 1);
9714 if (!*added)
9716 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9717 *added = true;
9721 static void
9722 output_ranges (void)
9724 unsigned i;
9725 static const char *const start_fmt = "Offset %#x";
9726 const char *fmt = start_fmt;
9728 for (i = 0; i < ranges_table_in_use; i++)
9730 int block_num = ranges_table[i].num;
9732 if (block_num > 0)
9734 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9735 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9737 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9738 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9740 /* If all code is in the text section, then the compilation
9741 unit base address defaults to DW_AT_low_pc, which is the
9742 base of the text section. */
9743 if (!have_multiple_function_sections)
9745 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9746 text_section_label,
9747 fmt, i * 2 * DWARF2_ADDR_SIZE);
9748 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9749 text_section_label, NULL);
9752 /* Otherwise, the compilation unit base address is zero,
9753 which allows us to use absolute addresses, and not worry
9754 about whether the target supports cross-section
9755 arithmetic. */
9756 else
9758 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9759 fmt, i * 2 * DWARF2_ADDR_SIZE);
9760 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9763 fmt = NULL;
9766 /* Negative block_num stands for an index into ranges_by_label. */
9767 else if (block_num < 0)
9769 int lab_idx = - block_num - 1;
9771 if (!have_multiple_function_sections)
9773 gcc_unreachable ();
9774 #if 0
9775 /* If we ever use add_ranges_by_labels () for a single
9776 function section, all we have to do is to take out
9777 the #if 0 above. */
9778 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9779 ranges_by_label[lab_idx].begin,
9780 text_section_label,
9781 fmt, i * 2 * DWARF2_ADDR_SIZE);
9782 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9783 ranges_by_label[lab_idx].end,
9784 text_section_label, NULL);
9785 #endif
9787 else
9789 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9790 ranges_by_label[lab_idx].begin,
9791 fmt, i * 2 * DWARF2_ADDR_SIZE);
9792 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9793 ranges_by_label[lab_idx].end,
9794 NULL);
9797 else
9799 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9800 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9801 fmt = start_fmt;
9806 /* Data structure containing information about input files. */
9807 struct file_info
9809 const char *path; /* Complete file name. */
9810 const char *fname; /* File name part. */
9811 int length; /* Length of entire string. */
9812 struct dwarf_file_data * file_idx; /* Index in input file table. */
9813 int dir_idx; /* Index in directory table. */
9816 /* Data structure containing information about directories with source
9817 files. */
9818 struct dir_info
9820 const char *path; /* Path including directory name. */
9821 int length; /* Path length. */
9822 int prefix; /* Index of directory entry which is a prefix. */
9823 int count; /* Number of files in this directory. */
9824 int dir_idx; /* Index of directory used as base. */
9827 /* Callback function for file_info comparison. We sort by looking at
9828 the directories in the path. */
9830 static int
9831 file_info_cmp (const void *p1, const void *p2)
9833 const struct file_info *const s1 = (const struct file_info *) p1;
9834 const struct file_info *const s2 = (const struct file_info *) p2;
9835 const unsigned char *cp1;
9836 const unsigned char *cp2;
9838 /* Take care of file names without directories. We need to make sure that
9839 we return consistent values to qsort since some will get confused if
9840 we return the same value when identical operands are passed in opposite
9841 orders. So if neither has a directory, return 0 and otherwise return
9842 1 or -1 depending on which one has the directory. */
9843 if ((s1->path == s1->fname || s2->path == s2->fname))
9844 return (s2->path == s2->fname) - (s1->path == s1->fname);
9846 cp1 = (const unsigned char *) s1->path;
9847 cp2 = (const unsigned char *) s2->path;
9849 while (1)
9851 ++cp1;
9852 ++cp2;
9853 /* Reached the end of the first path? If so, handle like above. */
9854 if ((cp1 == (const unsigned char *) s1->fname)
9855 || (cp2 == (const unsigned char *) s2->fname))
9856 return ((cp2 == (const unsigned char *) s2->fname)
9857 - (cp1 == (const unsigned char *) s1->fname));
9859 /* Character of current path component the same? */
9860 else if (*cp1 != *cp2)
9861 return *cp1 - *cp2;
9865 struct file_name_acquire_data
9867 struct file_info *files;
9868 int used_files;
9869 int max_files;
9872 /* Traversal function for the hash table. */
9875 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9877 struct dwarf_file_data *d = *slot;
9878 struct file_info *fi;
9879 const char *f;
9881 gcc_assert (fnad->max_files >= d->emitted_number);
9883 if (! d->emitted_number)
9884 return 1;
9886 gcc_assert (fnad->max_files != fnad->used_files);
9888 fi = fnad->files + fnad->used_files++;
9890 /* Skip all leading "./". */
9891 f = d->filename;
9892 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9893 f += 2;
9895 /* Create a new array entry. */
9896 fi->path = f;
9897 fi->length = strlen (f);
9898 fi->file_idx = d;
9900 /* Search for the file name part. */
9901 f = strrchr (f, DIR_SEPARATOR);
9902 #if defined (DIR_SEPARATOR_2)
9904 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9906 if (g != NULL)
9908 if (f == NULL || f < g)
9909 f = g;
9912 #endif
9914 fi->fname = f == NULL ? fi->path : f + 1;
9915 return 1;
9918 /* Output the directory table and the file name table. We try to minimize
9919 the total amount of memory needed. A heuristic is used to avoid large
9920 slowdowns with many input files. */
9922 static void
9923 output_file_names (void)
9925 struct file_name_acquire_data fnad;
9926 int numfiles;
9927 struct file_info *files;
9928 struct dir_info *dirs;
9929 int *saved;
9930 int *savehere;
9931 int *backmap;
9932 int ndirs;
9933 int idx_offset;
9934 int i;
9936 if (!last_emitted_file)
9938 dw2_asm_output_data (1, 0, "End directory table");
9939 dw2_asm_output_data (1, 0, "End file name table");
9940 return;
9943 numfiles = last_emitted_file->emitted_number;
9945 /* Allocate the various arrays we need. */
9946 files = XALLOCAVEC (struct file_info, numfiles);
9947 dirs = XALLOCAVEC (struct dir_info, numfiles);
9949 fnad.files = files;
9950 fnad.used_files = 0;
9951 fnad.max_files = numfiles;
9952 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9953 gcc_assert (fnad.used_files == fnad.max_files);
9955 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9957 /* Find all the different directories used. */
9958 dirs[0].path = files[0].path;
9959 dirs[0].length = files[0].fname - files[0].path;
9960 dirs[0].prefix = -1;
9961 dirs[0].count = 1;
9962 dirs[0].dir_idx = 0;
9963 files[0].dir_idx = 0;
9964 ndirs = 1;
9966 for (i = 1; i < numfiles; i++)
9967 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9968 && memcmp (dirs[ndirs - 1].path, files[i].path,
9969 dirs[ndirs - 1].length) == 0)
9971 /* Same directory as last entry. */
9972 files[i].dir_idx = ndirs - 1;
9973 ++dirs[ndirs - 1].count;
9975 else
9977 int j;
9979 /* This is a new directory. */
9980 dirs[ndirs].path = files[i].path;
9981 dirs[ndirs].length = files[i].fname - files[i].path;
9982 dirs[ndirs].count = 1;
9983 dirs[ndirs].dir_idx = ndirs;
9984 files[i].dir_idx = ndirs;
9986 /* Search for a prefix. */
9987 dirs[ndirs].prefix = -1;
9988 for (j = 0; j < ndirs; j++)
9989 if (dirs[j].length < dirs[ndirs].length
9990 && dirs[j].length > 1
9991 && (dirs[ndirs].prefix == -1
9992 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9993 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9994 dirs[ndirs].prefix = j;
9996 ++ndirs;
9999 /* Now to the actual work. We have to find a subset of the directories which
10000 allow expressing the file name using references to the directory table
10001 with the least amount of characters. We do not do an exhaustive search
10002 where we would have to check out every combination of every single
10003 possible prefix. Instead we use a heuristic which provides nearly optimal
10004 results in most cases and never is much off. */
10005 saved = XALLOCAVEC (int, ndirs);
10006 savehere = XALLOCAVEC (int, ndirs);
10008 memset (saved, '\0', ndirs * sizeof (saved[0]));
10009 for (i = 0; i < ndirs; i++)
10011 int j;
10012 int total;
10014 /* We can always save some space for the current directory. But this
10015 does not mean it will be enough to justify adding the directory. */
10016 savehere[i] = dirs[i].length;
10017 total = (savehere[i] - saved[i]) * dirs[i].count;
10019 for (j = i + 1; j < ndirs; j++)
10021 savehere[j] = 0;
10022 if (saved[j] < dirs[i].length)
10024 /* Determine whether the dirs[i] path is a prefix of the
10025 dirs[j] path. */
10026 int k;
10028 k = dirs[j].prefix;
10029 while (k != -1 && k != (int) i)
10030 k = dirs[k].prefix;
10032 if (k == (int) i)
10034 /* Yes it is. We can possibly save some memory by
10035 writing the filenames in dirs[j] relative to
10036 dirs[i]. */
10037 savehere[j] = dirs[i].length;
10038 total += (savehere[j] - saved[j]) * dirs[j].count;
10043 /* Check whether we can save enough to justify adding the dirs[i]
10044 directory. */
10045 if (total > dirs[i].length + 1)
10047 /* It's worthwhile adding. */
10048 for (j = i; j < ndirs; j++)
10049 if (savehere[j] > 0)
10051 /* Remember how much we saved for this directory so far. */
10052 saved[j] = savehere[j];
10054 /* Remember the prefix directory. */
10055 dirs[j].dir_idx = i;
10060 /* Emit the directory name table. */
10061 idx_offset = dirs[0].length > 0 ? 1 : 0;
10062 for (i = 1 - idx_offset; i < ndirs; i++)
10063 dw2_asm_output_nstring (dirs[i].path,
10064 dirs[i].length
10065 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10066 "Directory Entry: %#x", i + idx_offset);
10068 dw2_asm_output_data (1, 0, "End directory table");
10070 /* We have to emit them in the order of emitted_number since that's
10071 used in the debug info generation. To do this efficiently we
10072 generate a back-mapping of the indices first. */
10073 backmap = XALLOCAVEC (int, numfiles);
10074 for (i = 0; i < numfiles; i++)
10075 backmap[files[i].file_idx->emitted_number - 1] = i;
10077 /* Now write all the file names. */
10078 for (i = 0; i < numfiles; i++)
10080 int file_idx = backmap[i];
10081 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10083 #ifdef VMS_DEBUGGING_INFO
10084 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10086 /* Setting these fields can lead to debugger miscomparisons,
10087 but VMS Debug requires them to be set correctly. */
10089 int ver;
10090 long long cdt;
10091 long siz;
10092 int maxfilelen = strlen (files[file_idx].path)
10093 + dirs[dir_idx].length
10094 + MAX_VMS_VERSION_LEN + 1;
10095 char *filebuf = XALLOCAVEC (char, maxfilelen);
10097 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10098 snprintf (filebuf, maxfilelen, "%s;%d",
10099 files[file_idx].path + dirs[dir_idx].length, ver);
10101 dw2_asm_output_nstring
10102 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10104 /* Include directory index. */
10105 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10107 /* Modification time. */
10108 dw2_asm_output_data_uleb128
10109 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10110 ? cdt : 0,
10111 NULL);
10113 /* File length in bytes. */
10114 dw2_asm_output_data_uleb128
10115 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10116 ? siz : 0,
10117 NULL);
10118 #else
10119 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10120 "File Entry: %#x", (unsigned) i + 1);
10122 /* Include directory index. */
10123 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10125 /* Modification time. */
10126 dw2_asm_output_data_uleb128 (0, NULL);
10128 /* File length in bytes. */
10129 dw2_asm_output_data_uleb128 (0, NULL);
10130 #endif /* VMS_DEBUGGING_INFO */
10133 dw2_asm_output_data (1, 0, "End file name table");
10137 /* Output one line number table into the .debug_line section. */
10139 static void
10140 output_one_line_info_table (dw_line_info_table *table)
10142 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10143 unsigned int current_line = 1;
10144 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10145 dw_line_info_entry *ent;
10146 size_t i;
10148 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10150 switch (ent->opcode)
10152 case LI_set_address:
10153 /* ??? Unfortunately, we have little choice here currently, and
10154 must always use the most general form. GCC does not know the
10155 address delta itself, so we can't use DW_LNS_advance_pc. Many
10156 ports do have length attributes which will give an upper bound
10157 on the address range. We could perhaps use length attributes
10158 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10159 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10161 /* This can handle any delta. This takes
10162 4+DWARF2_ADDR_SIZE bytes. */
10163 dw2_asm_output_data (1, 0, "set address %s", line_label);
10164 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10165 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10166 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10167 break;
10169 case LI_set_line:
10170 if (ent->val == current_line)
10172 /* We still need to start a new row, so output a copy insn. */
10173 dw2_asm_output_data (1, DW_LNS_copy,
10174 "copy line %u", current_line);
10176 else
10178 int line_offset = ent->val - current_line;
10179 int line_delta = line_offset - DWARF_LINE_BASE;
10181 current_line = ent->val;
10182 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10184 /* This can handle deltas from -10 to 234, using the current
10185 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10186 This takes 1 byte. */
10187 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10188 "line %u", current_line);
10190 else
10192 /* This can handle any delta. This takes at least 4 bytes,
10193 depending on the value being encoded. */
10194 dw2_asm_output_data (1, DW_LNS_advance_line,
10195 "advance to line %u", current_line);
10196 dw2_asm_output_data_sleb128 (line_offset, NULL);
10197 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10200 break;
10202 case LI_set_file:
10203 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10204 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10205 break;
10207 case LI_set_column:
10208 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10209 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10210 break;
10212 case LI_negate_stmt:
10213 current_is_stmt = !current_is_stmt;
10214 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10215 "is_stmt %d", current_is_stmt);
10216 break;
10218 case LI_set_prologue_end:
10219 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10220 "set prologue end");
10221 break;
10223 case LI_set_epilogue_begin:
10224 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10225 "set epilogue begin");
10226 break;
10228 case LI_set_discriminator:
10229 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10230 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10231 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10232 dw2_asm_output_data_uleb128 (ent->val, NULL);
10233 break;
10237 /* Emit debug info for the address of the end of the table. */
10238 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10239 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10240 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10241 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10243 dw2_asm_output_data (1, 0, "end sequence");
10244 dw2_asm_output_data_uleb128 (1, NULL);
10245 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10248 /* Output the source line number correspondence information. This
10249 information goes into the .debug_line section. */
10251 static void
10252 output_line_info (bool prologue_only)
10254 char l1[20], l2[20], p1[20], p2[20];
10255 /* We don't support DWARFv5 line tables yet. */
10256 int ver = dwarf_version < 5 ? dwarf_version : 4;
10257 bool saw_one = false;
10258 int opc;
10260 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10261 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10262 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10263 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10265 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10266 dw2_asm_output_data (4, 0xffffffff,
10267 "Initial length escape value indicating 64-bit DWARF extension");
10268 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10269 "Length of Source Line Info");
10270 ASM_OUTPUT_LABEL (asm_out_file, l1);
10272 dw2_asm_output_data (2, ver, "DWARF Version");
10273 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10274 ASM_OUTPUT_LABEL (asm_out_file, p1);
10276 /* Define the architecture-dependent minimum instruction length (in bytes).
10277 In this implementation of DWARF, this field is used for information
10278 purposes only. Since GCC generates assembly language, we have no
10279 a priori knowledge of how many instruction bytes are generated for each
10280 source line, and therefore can use only the DW_LNE_set_address and
10281 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10282 this as '1', which is "correct enough" for all architectures,
10283 and don't let the target override. */
10284 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10286 if (ver >= 4)
10287 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10288 "Maximum Operations Per Instruction");
10289 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10290 "Default is_stmt_start flag");
10291 dw2_asm_output_data (1, DWARF_LINE_BASE,
10292 "Line Base Value (Special Opcodes)");
10293 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10294 "Line Range Value (Special Opcodes)");
10295 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10296 "Special Opcode Base");
10298 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10300 int n_op_args;
10301 switch (opc)
10303 case DW_LNS_advance_pc:
10304 case DW_LNS_advance_line:
10305 case DW_LNS_set_file:
10306 case DW_LNS_set_column:
10307 case DW_LNS_fixed_advance_pc:
10308 case DW_LNS_set_isa:
10309 n_op_args = 1;
10310 break;
10311 default:
10312 n_op_args = 0;
10313 break;
10316 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10317 opc, n_op_args);
10320 /* Write out the information about the files we use. */
10321 output_file_names ();
10322 ASM_OUTPUT_LABEL (asm_out_file, p2);
10323 if (prologue_only)
10325 /* Output the marker for the end of the line number info. */
10326 ASM_OUTPUT_LABEL (asm_out_file, l2);
10327 return;
10330 if (separate_line_info)
10332 dw_line_info_table *table;
10333 size_t i;
10335 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10336 if (table->in_use)
10338 output_one_line_info_table (table);
10339 saw_one = true;
10342 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10344 output_one_line_info_table (cold_text_section_line_info);
10345 saw_one = true;
10348 /* ??? Some Darwin linkers crash on a .debug_line section with no
10349 sequences. Further, merely a DW_LNE_end_sequence entry is not
10350 sufficient -- the address column must also be initialized.
10351 Make sure to output at least one set_address/end_sequence pair,
10352 choosing .text since that section is always present. */
10353 if (text_section_line_info->in_use || !saw_one)
10354 output_one_line_info_table (text_section_line_info);
10356 /* Output the marker for the end of the line number info. */
10357 ASM_OUTPUT_LABEL (asm_out_file, l2);
10360 /* Given a pointer to a tree node for some base type, return a pointer to
10361 a DIE that describes the given type.
10363 This routine must only be called for GCC type nodes that correspond to
10364 Dwarf base (fundamental) types. */
10366 static dw_die_ref
10367 base_type_die (tree type)
10369 dw_die_ref base_type_result;
10370 enum dwarf_type encoding;
10372 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10373 return 0;
10375 /* If this is a subtype that should not be emitted as a subrange type,
10376 use the base type. See subrange_type_for_debug_p. */
10377 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10378 type = TREE_TYPE (type);
10380 switch (TREE_CODE (type))
10382 case INTEGER_TYPE:
10383 if ((dwarf_version >= 4 || !dwarf_strict)
10384 && TYPE_NAME (type)
10385 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10386 && DECL_IS_BUILTIN (TYPE_NAME (type))
10387 && DECL_NAME (TYPE_NAME (type)))
10389 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10390 if (strcmp (name, "char16_t") == 0
10391 || strcmp (name, "char32_t") == 0)
10393 encoding = DW_ATE_UTF;
10394 break;
10397 if (TYPE_STRING_FLAG (type))
10399 if (TYPE_UNSIGNED (type))
10400 encoding = DW_ATE_unsigned_char;
10401 else
10402 encoding = DW_ATE_signed_char;
10404 else if (TYPE_UNSIGNED (type))
10405 encoding = DW_ATE_unsigned;
10406 else
10407 encoding = DW_ATE_signed;
10408 break;
10410 case REAL_TYPE:
10411 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10413 if (dwarf_version >= 3 || !dwarf_strict)
10414 encoding = DW_ATE_decimal_float;
10415 else
10416 encoding = DW_ATE_lo_user;
10418 else
10419 encoding = DW_ATE_float;
10420 break;
10422 case FIXED_POINT_TYPE:
10423 if (!(dwarf_version >= 3 || !dwarf_strict))
10424 encoding = DW_ATE_lo_user;
10425 else if (TYPE_UNSIGNED (type))
10426 encoding = DW_ATE_unsigned_fixed;
10427 else
10428 encoding = DW_ATE_signed_fixed;
10429 break;
10431 /* Dwarf2 doesn't know anything about complex ints, so use
10432 a user defined type for it. */
10433 case COMPLEX_TYPE:
10434 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10435 encoding = DW_ATE_complex_float;
10436 else
10437 encoding = DW_ATE_lo_user;
10438 break;
10440 case BOOLEAN_TYPE:
10441 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10442 encoding = DW_ATE_boolean;
10443 break;
10445 default:
10446 /* No other TREE_CODEs are Dwarf fundamental types. */
10447 gcc_unreachable ();
10450 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10452 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10453 int_size_in_bytes (type));
10454 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10455 add_pubtype (type, base_type_result);
10457 return base_type_result;
10460 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10461 named 'auto' in its type: return true for it, false otherwise. */
10463 static inline bool
10464 is_cxx_auto (tree type)
10466 if (is_cxx ())
10468 tree name = TYPE_IDENTIFIER (type);
10469 if (name == get_identifier ("auto")
10470 || name == get_identifier ("decltype(auto)"))
10471 return true;
10473 return false;
10476 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10477 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10479 static inline int
10480 is_base_type (tree type)
10482 switch (TREE_CODE (type))
10484 case ERROR_MARK:
10485 case VOID_TYPE:
10486 case INTEGER_TYPE:
10487 case REAL_TYPE:
10488 case FIXED_POINT_TYPE:
10489 case COMPLEX_TYPE:
10490 case BOOLEAN_TYPE:
10491 case POINTER_BOUNDS_TYPE:
10492 return 1;
10494 case ARRAY_TYPE:
10495 case RECORD_TYPE:
10496 case UNION_TYPE:
10497 case QUAL_UNION_TYPE:
10498 case ENUMERAL_TYPE:
10499 case FUNCTION_TYPE:
10500 case METHOD_TYPE:
10501 case POINTER_TYPE:
10502 case REFERENCE_TYPE:
10503 case NULLPTR_TYPE:
10504 case OFFSET_TYPE:
10505 case LANG_TYPE:
10506 case VECTOR_TYPE:
10507 return 0;
10509 default:
10510 if (is_cxx_auto (type))
10511 return 0;
10512 gcc_unreachable ();
10515 return 0;
10518 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10519 node, return the size in bits for the type if it is a constant, or else
10520 return the alignment for the type if the type's size is not constant, or
10521 else return BITS_PER_WORD if the type actually turns out to be an
10522 ERROR_MARK node. */
10524 static inline unsigned HOST_WIDE_INT
10525 simple_type_size_in_bits (const_tree type)
10527 if (TREE_CODE (type) == ERROR_MARK)
10528 return BITS_PER_WORD;
10529 else if (TYPE_SIZE (type) == NULL_TREE)
10530 return 0;
10531 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10532 return tree_to_uhwi (TYPE_SIZE (type));
10533 else
10534 return TYPE_ALIGN (type);
10537 /* Similarly, but return an offset_int instead of UHWI. */
10539 static inline offset_int
10540 offset_int_type_size_in_bits (const_tree type)
10542 if (TREE_CODE (type) == ERROR_MARK)
10543 return BITS_PER_WORD;
10544 else if (TYPE_SIZE (type) == NULL_TREE)
10545 return 0;
10546 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10547 return wi::to_offset (TYPE_SIZE (type));
10548 else
10549 return TYPE_ALIGN (type);
10552 /* Given a pointer to a tree node for a subrange type, return a pointer
10553 to a DIE that describes the given type. */
10555 static dw_die_ref
10556 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10558 dw_die_ref subrange_die;
10559 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10561 if (context_die == NULL)
10562 context_die = comp_unit_die ();
10564 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10566 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10568 /* The size of the subrange type and its base type do not match,
10569 so we need to generate a size attribute for the subrange type. */
10570 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10573 if (low)
10574 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10575 if (high)
10576 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10578 return subrange_die;
10581 /* Returns the (const and/or volatile) cv_qualifiers associated with
10582 the decl node. This will normally be augmented with the
10583 cv_qualifiers of the underlying type in add_type_attribute. */
10585 static int
10586 decl_quals (const_tree decl)
10588 return ((TREE_READONLY (decl)
10589 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10590 | (TREE_THIS_VOLATILE (decl)
10591 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10594 /* Determine the TYPE whose qualifiers match the largest strict subset
10595 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10596 qualifiers outside QUAL_MASK. */
10598 static int
10599 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10601 tree t;
10602 int best_rank = 0, best_qual = 0, max_rank;
10604 type_quals &= qual_mask;
10605 max_rank = popcount_hwi (type_quals) - 1;
10607 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10608 t = TYPE_NEXT_VARIANT (t))
10610 int q = TYPE_QUALS (t) & qual_mask;
10612 if ((q & type_quals) == q && q != type_quals
10613 && check_base_type (t, type))
10615 int rank = popcount_hwi (q);
10617 if (rank > best_rank)
10619 best_rank = rank;
10620 best_qual = q;
10625 return best_qual;
10628 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10629 entry that chains various modifiers in front of the given type. */
10631 static dw_die_ref
10632 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10634 enum tree_code code = TREE_CODE (type);
10635 dw_die_ref mod_type_die;
10636 dw_die_ref sub_die = NULL;
10637 tree item_type = NULL;
10638 tree qualified_type;
10639 tree name, low, high;
10640 dw_die_ref mod_scope;
10641 /* Only these cv-qualifiers are currently handled. */
10642 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10643 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10645 if (code == ERROR_MARK)
10646 return NULL;
10648 cv_quals &= cv_qual_mask;
10650 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10651 tag modifier (and not an attribute) old consumers won't be able
10652 to handle it. */
10653 if (dwarf_version < 3)
10654 cv_quals &= ~TYPE_QUAL_RESTRICT;
10656 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10657 if (dwarf_version < 5)
10658 cv_quals &= ~TYPE_QUAL_ATOMIC;
10660 /* See if we already have the appropriately qualified variant of
10661 this type. */
10662 qualified_type = get_qualified_type (type, cv_quals);
10664 if (qualified_type == sizetype
10665 && TYPE_NAME (qualified_type)
10666 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10668 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10670 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10671 && TYPE_PRECISION (t)
10672 == TYPE_PRECISION (qualified_type)
10673 && TYPE_UNSIGNED (t)
10674 == TYPE_UNSIGNED (qualified_type));
10675 qualified_type = t;
10678 /* If we do, then we can just use its DIE, if it exists. */
10679 if (qualified_type)
10681 mod_type_die = lookup_type_die (qualified_type);
10682 if (mod_type_die)
10683 return mod_type_die;
10686 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10688 /* Handle C typedef types. */
10689 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10690 && !DECL_ARTIFICIAL (name))
10692 tree dtype = TREE_TYPE (name);
10694 if (qualified_type == dtype)
10696 /* For a named type, use the typedef. */
10697 gen_type_die (qualified_type, context_die);
10698 return lookup_type_die (qualified_type);
10700 else
10702 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10703 dquals &= cv_qual_mask;
10704 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10705 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10706 /* cv-unqualified version of named type. Just use
10707 the unnamed type to which it refers. */
10708 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10709 cv_quals, context_die);
10710 /* Else cv-qualified version of named type; fall through. */
10714 mod_scope = scope_die_for (type, context_die);
10716 if (cv_quals)
10718 struct qual_info { int q; enum dwarf_tag t; };
10719 static const struct qual_info qual_info[] =
10721 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10722 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10723 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10724 { TYPE_QUAL_CONST, DW_TAG_const_type },
10726 int sub_quals;
10727 unsigned i;
10729 /* Determine a lesser qualified type that most closely matches
10730 this one. Then generate DW_TAG_* entries for the remaining
10731 qualifiers. */
10732 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10733 cv_qual_mask);
10734 mod_type_die = modified_type_die (type, sub_quals, context_die);
10736 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10737 if (qual_info[i].q & cv_quals & ~sub_quals)
10739 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10740 if (mod_type_die)
10741 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10742 mod_type_die = d;
10745 else if (code == POINTER_TYPE)
10747 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10748 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10749 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10750 item_type = TREE_TYPE (type);
10751 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10752 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10753 TYPE_ADDR_SPACE (item_type));
10755 else if (code == REFERENCE_TYPE)
10757 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10758 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10759 type);
10760 else
10761 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10762 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10763 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10764 item_type = TREE_TYPE (type);
10765 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10766 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10767 TYPE_ADDR_SPACE (item_type));
10769 else if (code == INTEGER_TYPE
10770 && TREE_TYPE (type) != NULL_TREE
10771 && subrange_type_for_debug_p (type, &low, &high))
10773 mod_type_die = subrange_type_die (type, low, high, context_die);
10774 item_type = TREE_TYPE (type);
10776 else if (is_base_type (type))
10777 mod_type_die = base_type_die (type);
10778 else
10780 gen_type_die (type, context_die);
10782 /* We have to get the type_main_variant here (and pass that to the
10783 `lookup_type_die' routine) because the ..._TYPE node we have
10784 might simply be a *copy* of some original type node (where the
10785 copy was created to help us keep track of typedef names) and
10786 that copy might have a different TYPE_UID from the original
10787 ..._TYPE node. */
10788 if (TREE_CODE (type) != VECTOR_TYPE)
10789 return lookup_type_die (type_main_variant (type));
10790 else
10791 /* Vectors have the debugging information in the type,
10792 not the main variant. */
10793 return lookup_type_die (type);
10796 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10797 don't output a DW_TAG_typedef, since there isn't one in the
10798 user's program; just attach a DW_AT_name to the type.
10799 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10800 if the base type already has the same name. */
10801 if (name
10802 && ((TREE_CODE (name) != TYPE_DECL
10803 && (qualified_type == TYPE_MAIN_VARIANT (type)
10804 || (cv_quals == TYPE_UNQUALIFIED)))
10805 || (TREE_CODE (name) == TYPE_DECL
10806 && TREE_TYPE (name) == qualified_type
10807 && DECL_NAME (name))))
10809 if (TREE_CODE (name) == TYPE_DECL)
10810 /* Could just call add_name_and_src_coords_attributes here,
10811 but since this is a builtin type it doesn't have any
10812 useful source coordinates anyway. */
10813 name = DECL_NAME (name);
10814 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10816 /* This probably indicates a bug. */
10817 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10819 name = TYPE_IDENTIFIER (type);
10820 add_name_attribute (mod_type_die,
10821 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10824 if (qualified_type)
10825 equate_type_number_to_die (qualified_type, mod_type_die);
10827 if (item_type)
10828 /* We must do this after the equate_type_number_to_die call, in case
10829 this is a recursive type. This ensures that the modified_type_die
10830 recursion will terminate even if the type is recursive. Recursive
10831 types are possible in Ada. */
10832 sub_die = modified_type_die (item_type,
10833 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10834 context_die);
10836 if (sub_die != NULL)
10837 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10839 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10840 if (TYPE_ARTIFICIAL (type))
10841 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10843 return mod_type_die;
10846 /* Generate DIEs for the generic parameters of T.
10847 T must be either a generic type or a generic function.
10848 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10850 static void
10851 gen_generic_params_dies (tree t)
10853 tree parms, args;
10854 int parms_num, i;
10855 dw_die_ref die = NULL;
10856 int non_default;
10858 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10859 return;
10861 if (TYPE_P (t))
10862 die = lookup_type_die (t);
10863 else if (DECL_P (t))
10864 die = lookup_decl_die (t);
10866 gcc_assert (die);
10868 parms = lang_hooks.get_innermost_generic_parms (t);
10869 if (!parms)
10870 /* T has no generic parameter. It means T is neither a generic type
10871 or function. End of story. */
10872 return;
10874 parms_num = TREE_VEC_LENGTH (parms);
10875 args = lang_hooks.get_innermost_generic_args (t);
10876 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10877 non_default = int_cst_value (TREE_CHAIN (args));
10878 else
10879 non_default = TREE_VEC_LENGTH (args);
10880 for (i = 0; i < parms_num; i++)
10882 tree parm, arg, arg_pack_elems;
10883 dw_die_ref parm_die;
10885 parm = TREE_VEC_ELT (parms, i);
10886 arg = TREE_VEC_ELT (args, i);
10887 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10888 gcc_assert (parm && TREE_VALUE (parm) && arg);
10890 if (parm && TREE_VALUE (parm) && arg)
10892 /* If PARM represents a template parameter pack,
10893 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10894 by DW_TAG_template_*_parameter DIEs for the argument
10895 pack elements of ARG. Note that ARG would then be
10896 an argument pack. */
10897 if (arg_pack_elems)
10898 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10899 arg_pack_elems,
10900 die);
10901 else
10902 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10903 true /* emit name */, die);
10904 if (i >= non_default)
10905 add_AT_flag (parm_die, DW_AT_default_value, 1);
10910 /* Create and return a DIE for PARM which should be
10911 the representation of a generic type parameter.
10912 For instance, in the C++ front end, PARM would be a template parameter.
10913 ARG is the argument to PARM.
10914 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10915 name of the PARM.
10916 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10917 as a child node. */
10919 static dw_die_ref
10920 generic_parameter_die (tree parm, tree arg,
10921 bool emit_name_p,
10922 dw_die_ref parent_die)
10924 dw_die_ref tmpl_die = NULL;
10925 const char *name = NULL;
10927 if (!parm || !DECL_NAME (parm) || !arg)
10928 return NULL;
10930 /* We support non-type generic parameters and arguments,
10931 type generic parameters and arguments, as well as
10932 generic generic parameters (a.k.a. template template parameters in C++)
10933 and arguments. */
10934 if (TREE_CODE (parm) == PARM_DECL)
10935 /* PARM is a nontype generic parameter */
10936 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10937 else if (TREE_CODE (parm) == TYPE_DECL)
10938 /* PARM is a type generic parameter. */
10939 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10940 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10941 /* PARM is a generic generic parameter.
10942 Its DIE is a GNU extension. It shall have a
10943 DW_AT_name attribute to represent the name of the template template
10944 parameter, and a DW_AT_GNU_template_name attribute to represent the
10945 name of the template template argument. */
10946 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10947 parent_die, parm);
10948 else
10949 gcc_unreachable ();
10951 if (tmpl_die)
10953 tree tmpl_type;
10955 /* If PARM is a generic parameter pack, it means we are
10956 emitting debug info for a template argument pack element.
10957 In other terms, ARG is a template argument pack element.
10958 In that case, we don't emit any DW_AT_name attribute for
10959 the die. */
10960 if (emit_name_p)
10962 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10963 gcc_assert (name);
10964 add_AT_string (tmpl_die, DW_AT_name, name);
10967 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10969 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10970 TMPL_DIE should have a child DW_AT_type attribute that is set
10971 to the type of the argument to PARM, which is ARG.
10972 If PARM is a type generic parameter, TMPL_DIE should have a
10973 child DW_AT_type that is set to ARG. */
10974 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10975 add_type_attribute (tmpl_die, tmpl_type,
10976 (TREE_THIS_VOLATILE (tmpl_type)
10977 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10978 parent_die);
10980 else
10982 /* So TMPL_DIE is a DIE representing a
10983 a generic generic template parameter, a.k.a template template
10984 parameter in C++ and arg is a template. */
10986 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10987 to the name of the argument. */
10988 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10989 if (name)
10990 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10993 if (TREE_CODE (parm) == PARM_DECL)
10994 /* So PARM is a non-type generic parameter.
10995 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10996 attribute of TMPL_DIE which value represents the value
10997 of ARG.
10998 We must be careful here:
10999 The value of ARG might reference some function decls.
11000 We might currently be emitting debug info for a generic
11001 type and types are emitted before function decls, we don't
11002 know if the function decls referenced by ARG will actually be
11003 emitted after cgraph computations.
11004 So must defer the generation of the DW_AT_const_value to
11005 after cgraph is ready. */
11006 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11009 return tmpl_die;
11012 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11013 PARM_PACK must be a template parameter pack. The returned DIE
11014 will be child DIE of PARENT_DIE. */
11016 static dw_die_ref
11017 template_parameter_pack_die (tree parm_pack,
11018 tree parm_pack_args,
11019 dw_die_ref parent_die)
11021 dw_die_ref die;
11022 int j;
11024 gcc_assert (parent_die && parm_pack);
11026 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11027 add_name_and_src_coords_attributes (die, parm_pack);
11028 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11029 generic_parameter_die (parm_pack,
11030 TREE_VEC_ELT (parm_pack_args, j),
11031 false /* Don't emit DW_AT_name */,
11032 die);
11033 return die;
11036 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11037 an enumerated type. */
11039 static inline int
11040 type_is_enum (const_tree type)
11042 return TREE_CODE (type) == ENUMERAL_TYPE;
11045 /* Return the DBX register number described by a given RTL node. */
11047 static unsigned int
11048 dbx_reg_number (const_rtx rtl)
11050 unsigned regno = REGNO (rtl);
11052 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11054 #ifdef LEAF_REG_REMAP
11055 if (crtl->uses_only_leaf_regs)
11057 int leaf_reg = LEAF_REG_REMAP (regno);
11058 if (leaf_reg != -1)
11059 regno = (unsigned) leaf_reg;
11061 #endif
11063 regno = DBX_REGISTER_NUMBER (regno);
11064 gcc_assert (regno != INVALID_REGNUM);
11065 return regno;
11068 /* Optionally add a DW_OP_piece term to a location description expression.
11069 DW_OP_piece is only added if the location description expression already
11070 doesn't end with DW_OP_piece. */
11072 static void
11073 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11075 dw_loc_descr_ref loc;
11077 if (*list_head != NULL)
11079 /* Find the end of the chain. */
11080 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11083 if (loc->dw_loc_opc != DW_OP_piece)
11084 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11088 /* Return a location descriptor that designates a machine register or
11089 zero if there is none. */
11091 static dw_loc_descr_ref
11092 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11094 rtx regs;
11096 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11097 return 0;
11099 /* We only use "frame base" when we're sure we're talking about the
11100 post-prologue local stack frame. We do this by *not* running
11101 register elimination until this point, and recognizing the special
11102 argument pointer and soft frame pointer rtx's.
11103 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11104 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11105 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11107 dw_loc_descr_ref result = NULL;
11109 if (dwarf_version >= 4 || !dwarf_strict)
11111 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11112 initialized);
11113 if (result)
11114 add_loc_descr (&result,
11115 new_loc_descr (DW_OP_stack_value, 0, 0));
11117 return result;
11120 regs = targetm.dwarf_register_span (rtl);
11122 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11123 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11124 else
11126 unsigned int dbx_regnum = dbx_reg_number (rtl);
11127 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11128 return 0;
11129 return one_reg_loc_descriptor (dbx_regnum, initialized);
11133 /* Return a location descriptor that designates a machine register for
11134 a given hard register number. */
11136 static dw_loc_descr_ref
11137 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11139 dw_loc_descr_ref reg_loc_descr;
11141 if (regno <= 31)
11142 reg_loc_descr
11143 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11144 else
11145 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11147 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11148 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11150 return reg_loc_descr;
11153 /* Given an RTL of a register, return a location descriptor that
11154 designates a value that spans more than one register. */
11156 static dw_loc_descr_ref
11157 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11158 enum var_init_status initialized)
11160 int size, i;
11161 dw_loc_descr_ref loc_result = NULL;
11163 /* Simple, contiguous registers. */
11164 if (regs == NULL_RTX)
11166 unsigned reg = REGNO (rtl);
11167 int nregs;
11169 #ifdef LEAF_REG_REMAP
11170 if (crtl->uses_only_leaf_regs)
11172 int leaf_reg = LEAF_REG_REMAP (reg);
11173 if (leaf_reg != -1)
11174 reg = (unsigned) leaf_reg;
11176 #endif
11178 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11179 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11181 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11183 loc_result = NULL;
11184 while (nregs--)
11186 dw_loc_descr_ref t;
11188 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11189 VAR_INIT_STATUS_INITIALIZED);
11190 add_loc_descr (&loc_result, t);
11191 add_loc_descr_op_piece (&loc_result, size);
11192 ++reg;
11194 return loc_result;
11197 /* Now onto stupid register sets in non contiguous locations. */
11199 gcc_assert (GET_CODE (regs) == PARALLEL);
11201 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11202 loc_result = NULL;
11204 for (i = 0; i < XVECLEN (regs, 0); ++i)
11206 dw_loc_descr_ref t;
11208 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11209 VAR_INIT_STATUS_INITIALIZED);
11210 add_loc_descr (&loc_result, t);
11211 add_loc_descr_op_piece (&loc_result, size);
11214 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11215 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11216 return loc_result;
11219 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11221 /* Return a location descriptor that designates a constant i,
11222 as a compound operation from constant (i >> shift), constant shift
11223 and DW_OP_shl. */
11225 static dw_loc_descr_ref
11226 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11228 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11229 add_loc_descr (&ret, int_loc_descriptor (shift));
11230 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11231 return ret;
11234 /* Return a location descriptor that designates a constant. */
11236 static dw_loc_descr_ref
11237 int_loc_descriptor (HOST_WIDE_INT i)
11239 enum dwarf_location_atom op;
11241 /* Pick the smallest representation of a constant, rather than just
11242 defaulting to the LEB encoding. */
11243 if (i >= 0)
11245 int clz = clz_hwi (i);
11246 int ctz = ctz_hwi (i);
11247 if (i <= 31)
11248 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11249 else if (i <= 0xff)
11250 op = DW_OP_const1u;
11251 else if (i <= 0xffff)
11252 op = DW_OP_const2u;
11253 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11254 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11255 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11256 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11257 while DW_OP_const4u is 5 bytes. */
11258 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11259 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11260 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11261 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11262 while DW_OP_const4u is 5 bytes. */
11263 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11264 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11265 op = DW_OP_const4u;
11266 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11267 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11268 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11269 while DW_OP_constu of constant >= 0x100000000 takes at least
11270 6 bytes. */
11271 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11272 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11273 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11274 >= HOST_BITS_PER_WIDE_INT)
11275 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11276 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11277 while DW_OP_constu takes in this case at least 6 bytes. */
11278 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11279 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11280 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11281 && size_of_uleb128 (i) > 6)
11282 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11283 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11284 else
11285 op = DW_OP_constu;
11287 else
11289 if (i >= -0x80)
11290 op = DW_OP_const1s;
11291 else if (i >= -0x8000)
11292 op = DW_OP_const2s;
11293 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11295 if (size_of_int_loc_descriptor (i) < 5)
11297 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11298 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11299 return ret;
11301 op = DW_OP_const4s;
11303 else
11305 if (size_of_int_loc_descriptor (i)
11306 < (unsigned long) 1 + size_of_sleb128 (i))
11308 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11309 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11310 return ret;
11312 op = DW_OP_consts;
11316 return new_loc_descr (op, i, 0);
11319 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11320 without actually allocating it. */
11322 static unsigned long
11323 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11325 return size_of_int_loc_descriptor (i >> shift)
11326 + size_of_int_loc_descriptor (shift)
11327 + 1;
11330 /* Return size_of_locs (int_loc_descriptor (i)) without
11331 actually allocating it. */
11333 static unsigned long
11334 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11336 unsigned long s;
11338 if (i >= 0)
11340 int clz, ctz;
11341 if (i <= 31)
11342 return 1;
11343 else if (i <= 0xff)
11344 return 2;
11345 else if (i <= 0xffff)
11346 return 3;
11347 clz = clz_hwi (i);
11348 ctz = ctz_hwi (i);
11349 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11350 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11351 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11352 - clz - 5);
11353 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11354 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11355 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11356 - clz - 8);
11357 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11358 return 5;
11359 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11360 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11361 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11362 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11363 - clz - 8);
11364 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11365 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11366 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11367 - clz - 16);
11368 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11369 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11370 && s > 6)
11371 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11372 - clz - 32);
11373 else
11374 return 1 + s;
11376 else
11378 if (i >= -0x80)
11379 return 2;
11380 else if (i >= -0x8000)
11381 return 3;
11382 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11384 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11386 s = size_of_int_loc_descriptor (-i) + 1;
11387 if (s < 5)
11388 return s;
11390 return 5;
11392 else
11394 unsigned long r = 1 + size_of_sleb128 (i);
11395 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11397 s = size_of_int_loc_descriptor (-i) + 1;
11398 if (s < r)
11399 return s;
11401 return r;
11406 /* Return loc description representing "address" of integer value.
11407 This can appear only as toplevel expression. */
11409 static dw_loc_descr_ref
11410 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11412 int litsize;
11413 dw_loc_descr_ref loc_result = NULL;
11415 if (!(dwarf_version >= 4 || !dwarf_strict))
11416 return NULL;
11418 litsize = size_of_int_loc_descriptor (i);
11419 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11420 is more compact. For DW_OP_stack_value we need:
11421 litsize + 1 (DW_OP_stack_value)
11422 and for DW_OP_implicit_value:
11423 1 (DW_OP_implicit_value) + 1 (length) + size. */
11424 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11426 loc_result = int_loc_descriptor (i);
11427 add_loc_descr (&loc_result,
11428 new_loc_descr (DW_OP_stack_value, 0, 0));
11429 return loc_result;
11432 loc_result = new_loc_descr (DW_OP_implicit_value,
11433 size, 0);
11434 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11435 loc_result->dw_loc_oprnd2.v.val_int = i;
11436 return loc_result;
11439 /* Return a location descriptor that designates a base+offset location. */
11441 static dw_loc_descr_ref
11442 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11443 enum var_init_status initialized)
11445 unsigned int regno;
11446 dw_loc_descr_ref result;
11447 dw_fde_ref fde = cfun->fde;
11449 /* We only use "frame base" when we're sure we're talking about the
11450 post-prologue local stack frame. We do this by *not* running
11451 register elimination until this point, and recognizing the special
11452 argument pointer and soft frame pointer rtx's. */
11453 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11455 rtx elim = (ira_use_lra_p
11456 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11457 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11459 if (elim != reg)
11461 if (GET_CODE (elim) == PLUS)
11463 offset += INTVAL (XEXP (elim, 1));
11464 elim = XEXP (elim, 0);
11466 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11467 && (elim == hard_frame_pointer_rtx
11468 || elim == stack_pointer_rtx))
11469 || elim == (frame_pointer_needed
11470 ? hard_frame_pointer_rtx
11471 : stack_pointer_rtx));
11473 /* If drap register is used to align stack, use frame
11474 pointer + offset to access stack variables. If stack
11475 is aligned without drap, use stack pointer + offset to
11476 access stack variables. */
11477 if (crtl->stack_realign_tried
11478 && reg == frame_pointer_rtx)
11480 int base_reg
11481 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11482 ? HARD_FRAME_POINTER_REGNUM
11483 : REGNO (elim));
11484 return new_reg_loc_descr (base_reg, offset);
11487 gcc_assert (frame_pointer_fb_offset_valid);
11488 offset += frame_pointer_fb_offset;
11489 return new_loc_descr (DW_OP_fbreg, offset, 0);
11493 regno = REGNO (reg);
11494 #ifdef LEAF_REG_REMAP
11495 if (crtl->uses_only_leaf_regs)
11497 int leaf_reg = LEAF_REG_REMAP (regno);
11498 if (leaf_reg != -1)
11499 regno = (unsigned) leaf_reg;
11501 #endif
11502 regno = DWARF_FRAME_REGNUM (regno);
11504 if (!optimize && fde
11505 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11507 /* Use cfa+offset to represent the location of arguments passed
11508 on the stack when drap is used to align stack.
11509 Only do this when not optimizing, for optimized code var-tracking
11510 is supposed to track where the arguments live and the register
11511 used as vdrap or drap in some spot might be used for something
11512 else in other part of the routine. */
11513 return new_loc_descr (DW_OP_fbreg, offset, 0);
11516 if (regno <= 31)
11517 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11518 offset, 0);
11519 else
11520 result = new_loc_descr (DW_OP_bregx, regno, offset);
11522 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11523 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11525 return result;
11528 /* Return true if this RTL expression describes a base+offset calculation. */
11530 static inline int
11531 is_based_loc (const_rtx rtl)
11533 return (GET_CODE (rtl) == PLUS
11534 && ((REG_P (XEXP (rtl, 0))
11535 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11536 && CONST_INT_P (XEXP (rtl, 1)))));
11539 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11540 failed. */
11542 static dw_loc_descr_ref
11543 tls_mem_loc_descriptor (rtx mem)
11545 tree base;
11546 dw_loc_descr_ref loc_result;
11548 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11549 return NULL;
11551 base = get_base_address (MEM_EXPR (mem));
11552 if (base == NULL
11553 || TREE_CODE (base) != VAR_DECL
11554 || !DECL_THREAD_LOCAL_P (base))
11555 return NULL;
11557 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11558 if (loc_result == NULL)
11559 return NULL;
11561 if (MEM_OFFSET (mem))
11562 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11564 return loc_result;
11567 /* Output debug info about reason why we failed to expand expression as dwarf
11568 expression. */
11570 static void
11571 expansion_failed (tree expr, rtx rtl, char const *reason)
11573 if (dump_file && (dump_flags & TDF_DETAILS))
11575 fprintf (dump_file, "Failed to expand as dwarf: ");
11576 if (expr)
11577 print_generic_expr (dump_file, expr, dump_flags);
11578 if (rtl)
11580 fprintf (dump_file, "\n");
11581 print_rtl (dump_file, rtl);
11583 fprintf (dump_file, "\nReason: %s\n", reason);
11587 /* Helper function for const_ok_for_output. */
11589 static bool
11590 const_ok_for_output_1 (rtx rtl)
11592 if (GET_CODE (rtl) == UNSPEC)
11594 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11595 we can't express it in the debug info. */
11596 #ifdef ENABLE_CHECKING
11597 /* Don't complain about TLS UNSPECs, those are just too hard to
11598 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11599 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11600 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11601 if (XVECLEN (rtl, 0) == 0
11602 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11603 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11604 inform (current_function_decl
11605 ? DECL_SOURCE_LOCATION (current_function_decl)
11606 : UNKNOWN_LOCATION,
11607 #if NUM_UNSPEC_VALUES > 0
11608 "non-delegitimized UNSPEC %s (%d) found in variable location",
11609 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11610 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11611 XINT (rtl, 1));
11612 #else
11613 "non-delegitimized UNSPEC %d found in variable location",
11614 XINT (rtl, 1));
11615 #endif
11616 #endif
11617 expansion_failed (NULL_TREE, rtl,
11618 "UNSPEC hasn't been delegitimized.\n");
11619 return false;
11622 if (targetm.const_not_ok_for_debug_p (rtl))
11624 expansion_failed (NULL_TREE, rtl,
11625 "Expression rejected for debug by the backend.\n");
11626 return false;
11629 /* FIXME: Refer to PR60655. It is possible for simplification
11630 of rtl expressions in var tracking to produce such expressions.
11631 We should really identify / validate expressions
11632 enclosed in CONST that can be handled by assemblers on various
11633 targets and only handle legitimate cases here. */
11634 if (GET_CODE (rtl) != SYMBOL_REF)
11636 if (GET_CODE (rtl) == NOT)
11637 return false;
11638 return true;
11641 if (CONSTANT_POOL_ADDRESS_P (rtl))
11643 bool marked;
11644 get_pool_constant_mark (rtl, &marked);
11645 /* If all references to this pool constant were optimized away,
11646 it was not output and thus we can't represent it. */
11647 if (!marked)
11649 expansion_failed (NULL_TREE, rtl,
11650 "Constant was removed from constant pool.\n");
11651 return false;
11655 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11656 return false;
11658 /* Avoid references to external symbols in debug info, on several targets
11659 the linker might even refuse to link when linking a shared library,
11660 and in many other cases the relocations for .debug_info/.debug_loc are
11661 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11662 to be defined within the same shared library or executable are fine. */
11663 if (SYMBOL_REF_EXTERNAL_P (rtl))
11665 tree decl = SYMBOL_REF_DECL (rtl);
11667 if (decl == NULL || !targetm.binds_local_p (decl))
11669 expansion_failed (NULL_TREE, rtl,
11670 "Symbol not defined in current TU.\n");
11671 return false;
11675 return true;
11678 /* Return true if constant RTL can be emitted in DW_OP_addr or
11679 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11680 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11682 static bool
11683 const_ok_for_output (rtx rtl)
11685 if (GET_CODE (rtl) == SYMBOL_REF)
11686 return const_ok_for_output_1 (rtl);
11688 if (GET_CODE (rtl) == CONST)
11690 subrtx_var_iterator::array_type array;
11691 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11692 if (!const_ok_for_output_1 (*iter))
11693 return false;
11694 return true;
11697 return true;
11700 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11701 if possible, NULL otherwise. */
11703 static dw_die_ref
11704 base_type_for_mode (machine_mode mode, bool unsignedp)
11706 dw_die_ref type_die;
11707 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11709 if (type == NULL)
11710 return NULL;
11711 switch (TREE_CODE (type))
11713 case INTEGER_TYPE:
11714 case REAL_TYPE:
11715 break;
11716 default:
11717 return NULL;
11719 type_die = lookup_type_die (type);
11720 if (!type_die)
11721 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11722 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11723 return NULL;
11724 return type_die;
11727 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11728 type matching MODE, or, if MODE is narrower than or as wide as
11729 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11730 possible. */
11732 static dw_loc_descr_ref
11733 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11735 machine_mode outer_mode = mode;
11736 dw_die_ref type_die;
11737 dw_loc_descr_ref cvt;
11739 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11741 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11742 return op;
11744 type_die = base_type_for_mode (outer_mode, 1);
11745 if (type_die == NULL)
11746 return NULL;
11747 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11748 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11749 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11750 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11751 add_loc_descr (&op, cvt);
11752 return op;
11755 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11757 static dw_loc_descr_ref
11758 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11759 dw_loc_descr_ref op1)
11761 dw_loc_descr_ref ret = op0;
11762 add_loc_descr (&ret, op1);
11763 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11764 if (STORE_FLAG_VALUE != 1)
11766 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11767 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11769 return ret;
11772 /* Return location descriptor for signed comparison OP RTL. */
11774 static dw_loc_descr_ref
11775 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11776 machine_mode mem_mode)
11778 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11779 dw_loc_descr_ref op0, op1;
11780 int shift;
11782 if (op_mode == VOIDmode)
11783 op_mode = GET_MODE (XEXP (rtl, 1));
11784 if (op_mode == VOIDmode)
11785 return NULL;
11787 if (dwarf_strict
11788 && (GET_MODE_CLASS (op_mode) != MODE_INT
11789 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11790 return NULL;
11792 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11793 VAR_INIT_STATUS_INITIALIZED);
11794 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11795 VAR_INIT_STATUS_INITIALIZED);
11797 if (op0 == NULL || op1 == NULL)
11798 return NULL;
11800 if (GET_MODE_CLASS (op_mode) != MODE_INT
11801 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11802 return compare_loc_descriptor (op, op0, op1);
11804 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11806 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11807 dw_loc_descr_ref cvt;
11809 if (type_die == NULL)
11810 return NULL;
11811 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11812 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11813 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11814 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11815 add_loc_descr (&op0, cvt);
11816 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11817 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11818 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11819 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11820 add_loc_descr (&op1, cvt);
11821 return compare_loc_descriptor (op, op0, op1);
11824 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11825 /* For eq/ne, if the operands are known to be zero-extended,
11826 there is no need to do the fancy shifting up. */
11827 if (op == DW_OP_eq || op == DW_OP_ne)
11829 dw_loc_descr_ref last0, last1;
11830 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11832 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11834 /* deref_size zero extends, and for constants we can check
11835 whether they are zero extended or not. */
11836 if (((last0->dw_loc_opc == DW_OP_deref_size
11837 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11838 || (CONST_INT_P (XEXP (rtl, 0))
11839 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11840 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11841 && ((last1->dw_loc_opc == DW_OP_deref_size
11842 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11843 || (CONST_INT_P (XEXP (rtl, 1))
11844 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11845 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11846 return compare_loc_descriptor (op, op0, op1);
11848 /* EQ/NE comparison against constant in narrower type than
11849 DWARF2_ADDR_SIZE can be performed either as
11850 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11851 DW_OP_{eq,ne}
11853 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11854 DW_OP_{eq,ne}. Pick whatever is shorter. */
11855 if (CONST_INT_P (XEXP (rtl, 1))
11856 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11857 && (size_of_int_loc_descriptor (shift) + 1
11858 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11859 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11860 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11861 & GET_MODE_MASK (op_mode))))
11863 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11864 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11865 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11866 & GET_MODE_MASK (op_mode));
11867 return compare_loc_descriptor (op, op0, op1);
11870 add_loc_descr (&op0, int_loc_descriptor (shift));
11871 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11872 if (CONST_INT_P (XEXP (rtl, 1)))
11873 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11874 else
11876 add_loc_descr (&op1, int_loc_descriptor (shift));
11877 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11879 return compare_loc_descriptor (op, op0, op1);
11882 /* Return location descriptor for unsigned comparison OP RTL. */
11884 static dw_loc_descr_ref
11885 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11886 machine_mode mem_mode)
11888 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11889 dw_loc_descr_ref op0, op1;
11891 if (op_mode == VOIDmode)
11892 op_mode = GET_MODE (XEXP (rtl, 1));
11893 if (op_mode == VOIDmode)
11894 return NULL;
11895 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11896 return NULL;
11898 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11899 return NULL;
11901 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11902 VAR_INIT_STATUS_INITIALIZED);
11903 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11904 VAR_INIT_STATUS_INITIALIZED);
11906 if (op0 == NULL || op1 == NULL)
11907 return NULL;
11909 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11911 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11912 dw_loc_descr_ref last0, last1;
11913 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11915 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11917 if (CONST_INT_P (XEXP (rtl, 0)))
11918 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11919 /* deref_size zero extends, so no need to mask it again. */
11920 else if (last0->dw_loc_opc != DW_OP_deref_size
11921 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11923 add_loc_descr (&op0, int_loc_descriptor (mask));
11924 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11926 if (CONST_INT_P (XEXP (rtl, 1)))
11927 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11928 /* deref_size zero extends, so no need to mask it again. */
11929 else if (last1->dw_loc_opc != DW_OP_deref_size
11930 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11932 add_loc_descr (&op1, int_loc_descriptor (mask));
11933 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11936 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11938 HOST_WIDE_INT bias = 1;
11939 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11940 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11941 if (CONST_INT_P (XEXP (rtl, 1)))
11942 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11943 + INTVAL (XEXP (rtl, 1)));
11944 else
11945 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11946 bias, 0));
11948 return compare_loc_descriptor (op, op0, op1);
11951 /* Return location descriptor for {U,S}{MIN,MAX}. */
11953 static dw_loc_descr_ref
11954 minmax_loc_descriptor (rtx rtl, machine_mode mode,
11955 machine_mode mem_mode)
11957 enum dwarf_location_atom op;
11958 dw_loc_descr_ref op0, op1, ret;
11959 dw_loc_descr_ref bra_node, drop_node;
11961 if (dwarf_strict
11962 && (GET_MODE_CLASS (mode) != MODE_INT
11963 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11964 return NULL;
11966 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11967 VAR_INIT_STATUS_INITIALIZED);
11968 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11969 VAR_INIT_STATUS_INITIALIZED);
11971 if (op0 == NULL || op1 == NULL)
11972 return NULL;
11974 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11975 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11976 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11977 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11979 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11981 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11982 add_loc_descr (&op0, int_loc_descriptor (mask));
11983 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11984 add_loc_descr (&op1, int_loc_descriptor (mask));
11985 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11987 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11989 HOST_WIDE_INT bias = 1;
11990 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11991 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11992 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11995 else if (GET_MODE_CLASS (mode) == MODE_INT
11996 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11998 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11999 add_loc_descr (&op0, int_loc_descriptor (shift));
12000 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12001 add_loc_descr (&op1, int_loc_descriptor (shift));
12002 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12004 else if (GET_MODE_CLASS (mode) == MODE_INT
12005 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12007 dw_die_ref type_die = base_type_for_mode (mode, 0);
12008 dw_loc_descr_ref cvt;
12009 if (type_die == NULL)
12010 return NULL;
12011 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12012 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12013 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12014 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12015 add_loc_descr (&op0, cvt);
12016 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12017 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12018 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12019 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12020 add_loc_descr (&op1, cvt);
12023 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12024 op = DW_OP_lt;
12025 else
12026 op = DW_OP_gt;
12027 ret = op0;
12028 add_loc_descr (&ret, op1);
12029 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12030 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12031 add_loc_descr (&ret, bra_node);
12032 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12033 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12034 add_loc_descr (&ret, drop_node);
12035 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12036 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12037 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12038 && GET_MODE_CLASS (mode) == MODE_INT
12039 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12040 ret = convert_descriptor_to_mode (mode, ret);
12041 return ret;
12044 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12045 but after converting arguments to type_die, afterwards
12046 convert back to unsigned. */
12048 static dw_loc_descr_ref
12049 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12050 machine_mode mode, machine_mode mem_mode)
12052 dw_loc_descr_ref cvt, op0, op1;
12054 if (type_die == NULL)
12055 return NULL;
12056 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12057 VAR_INIT_STATUS_INITIALIZED);
12058 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12059 VAR_INIT_STATUS_INITIALIZED);
12060 if (op0 == NULL || op1 == NULL)
12061 return NULL;
12062 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12063 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12064 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12065 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12066 add_loc_descr (&op0, cvt);
12067 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12068 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12069 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12070 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12071 add_loc_descr (&op1, cvt);
12072 add_loc_descr (&op0, op1);
12073 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12074 return convert_descriptor_to_mode (mode, op0);
12077 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12078 const0 is DW_OP_lit0 or corresponding typed constant,
12079 const1 is DW_OP_lit1 or corresponding typed constant
12080 and constMSB is constant with just the MSB bit set
12081 for the mode):
12082 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12083 L1: const0 DW_OP_swap
12084 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12085 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12086 L3: DW_OP_drop
12087 L4: DW_OP_nop
12089 CTZ is similar:
12090 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12091 L1: const0 DW_OP_swap
12092 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12093 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12094 L3: DW_OP_drop
12095 L4: DW_OP_nop
12097 FFS is similar:
12098 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12099 L1: const1 DW_OP_swap
12100 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12101 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12102 L3: DW_OP_drop
12103 L4: DW_OP_nop */
12105 static dw_loc_descr_ref
12106 clz_loc_descriptor (rtx rtl, machine_mode mode,
12107 machine_mode mem_mode)
12109 dw_loc_descr_ref op0, ret, tmp;
12110 HOST_WIDE_INT valv;
12111 dw_loc_descr_ref l1jump, l1label;
12112 dw_loc_descr_ref l2jump, l2label;
12113 dw_loc_descr_ref l3jump, l3label;
12114 dw_loc_descr_ref l4jump, l4label;
12115 rtx msb;
12117 if (GET_MODE_CLASS (mode) != MODE_INT
12118 || GET_MODE (XEXP (rtl, 0)) != mode)
12119 return NULL;
12121 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12122 VAR_INIT_STATUS_INITIALIZED);
12123 if (op0 == NULL)
12124 return NULL;
12125 ret = op0;
12126 if (GET_CODE (rtl) == CLZ)
12128 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12129 valv = GET_MODE_BITSIZE (mode);
12131 else if (GET_CODE (rtl) == FFS)
12132 valv = 0;
12133 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12134 valv = GET_MODE_BITSIZE (mode);
12135 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12136 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12137 add_loc_descr (&ret, l1jump);
12138 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12139 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12140 VAR_INIT_STATUS_INITIALIZED);
12141 if (tmp == NULL)
12142 return NULL;
12143 add_loc_descr (&ret, tmp);
12144 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12145 add_loc_descr (&ret, l4jump);
12146 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12147 ? const1_rtx : const0_rtx,
12148 mode, mem_mode,
12149 VAR_INIT_STATUS_INITIALIZED);
12150 if (l1label == NULL)
12151 return NULL;
12152 add_loc_descr (&ret, l1label);
12153 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12154 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12155 add_loc_descr (&ret, l2label);
12156 if (GET_CODE (rtl) != CLZ)
12157 msb = const1_rtx;
12158 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12159 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12160 << (GET_MODE_BITSIZE (mode) - 1));
12161 else
12162 msb = immed_wide_int_const
12163 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12164 GET_MODE_PRECISION (mode)), mode);
12165 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12166 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12167 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12168 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12169 else
12170 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12171 VAR_INIT_STATUS_INITIALIZED);
12172 if (tmp == NULL)
12173 return NULL;
12174 add_loc_descr (&ret, tmp);
12175 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12176 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12177 add_loc_descr (&ret, l3jump);
12178 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12179 VAR_INIT_STATUS_INITIALIZED);
12180 if (tmp == NULL)
12181 return NULL;
12182 add_loc_descr (&ret, tmp);
12183 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12184 ? DW_OP_shl : DW_OP_shr, 0, 0));
12185 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12186 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12187 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12188 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12189 add_loc_descr (&ret, l2jump);
12190 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12191 add_loc_descr (&ret, l3label);
12192 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12193 add_loc_descr (&ret, l4label);
12194 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12195 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12196 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12197 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12198 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12199 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12200 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12201 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12202 return ret;
12205 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12206 const1 is DW_OP_lit1 or corresponding typed constant):
12207 const0 DW_OP_swap
12208 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12209 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12210 L2: DW_OP_drop
12212 PARITY is similar:
12213 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12214 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12215 L2: DW_OP_drop */
12217 static dw_loc_descr_ref
12218 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12219 machine_mode mem_mode)
12221 dw_loc_descr_ref op0, ret, tmp;
12222 dw_loc_descr_ref l1jump, l1label;
12223 dw_loc_descr_ref l2jump, l2label;
12225 if (GET_MODE_CLASS (mode) != MODE_INT
12226 || GET_MODE (XEXP (rtl, 0)) != mode)
12227 return NULL;
12229 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12230 VAR_INIT_STATUS_INITIALIZED);
12231 if (op0 == NULL)
12232 return NULL;
12233 ret = op0;
12234 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12235 VAR_INIT_STATUS_INITIALIZED);
12236 if (tmp == NULL)
12237 return NULL;
12238 add_loc_descr (&ret, tmp);
12239 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12240 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12241 add_loc_descr (&ret, l1label);
12242 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12243 add_loc_descr (&ret, l2jump);
12244 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12245 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12246 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12247 VAR_INIT_STATUS_INITIALIZED);
12248 if (tmp == NULL)
12249 return NULL;
12250 add_loc_descr (&ret, tmp);
12251 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12252 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12253 ? DW_OP_plus : DW_OP_xor, 0, 0));
12254 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12255 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12256 VAR_INIT_STATUS_INITIALIZED);
12257 add_loc_descr (&ret, tmp);
12258 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12259 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12260 add_loc_descr (&ret, l1jump);
12261 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12262 add_loc_descr (&ret, l2label);
12263 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12264 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12265 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12266 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12267 return ret;
12270 /* BSWAP (constS is initial shift count, either 56 or 24):
12271 constS const0
12272 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12273 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12274 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12275 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12276 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12278 static dw_loc_descr_ref
12279 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12280 machine_mode mem_mode)
12282 dw_loc_descr_ref op0, ret, tmp;
12283 dw_loc_descr_ref l1jump, l1label;
12284 dw_loc_descr_ref l2jump, l2label;
12286 if (GET_MODE_CLASS (mode) != MODE_INT
12287 || BITS_PER_UNIT != 8
12288 || (GET_MODE_BITSIZE (mode) != 32
12289 && GET_MODE_BITSIZE (mode) != 64))
12290 return NULL;
12292 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12293 VAR_INIT_STATUS_INITIALIZED);
12294 if (op0 == NULL)
12295 return NULL;
12297 ret = op0;
12298 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12299 mode, mem_mode,
12300 VAR_INIT_STATUS_INITIALIZED);
12301 if (tmp == NULL)
12302 return NULL;
12303 add_loc_descr (&ret, tmp);
12304 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12305 VAR_INIT_STATUS_INITIALIZED);
12306 if (tmp == NULL)
12307 return NULL;
12308 add_loc_descr (&ret, tmp);
12309 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12310 add_loc_descr (&ret, l1label);
12311 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12312 mode, mem_mode,
12313 VAR_INIT_STATUS_INITIALIZED);
12314 add_loc_descr (&ret, tmp);
12315 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12316 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12317 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12318 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12319 VAR_INIT_STATUS_INITIALIZED);
12320 if (tmp == NULL)
12321 return NULL;
12322 add_loc_descr (&ret, tmp);
12323 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12324 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12325 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12326 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12327 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12328 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12329 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12330 VAR_INIT_STATUS_INITIALIZED);
12331 add_loc_descr (&ret, tmp);
12332 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12333 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12334 add_loc_descr (&ret, l2jump);
12335 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12336 VAR_INIT_STATUS_INITIALIZED);
12337 add_loc_descr (&ret, tmp);
12338 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12339 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12340 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12341 add_loc_descr (&ret, l1jump);
12342 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12343 add_loc_descr (&ret, l2label);
12344 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12345 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12346 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12347 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12348 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12349 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12350 return ret;
12353 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12354 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12355 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12356 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12358 ROTATERT is similar:
12359 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12360 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12361 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12363 static dw_loc_descr_ref
12364 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12365 machine_mode mem_mode)
12367 rtx rtlop1 = XEXP (rtl, 1);
12368 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12369 int i;
12371 if (GET_MODE_CLASS (mode) != MODE_INT)
12372 return NULL;
12374 if (GET_MODE (rtlop1) != VOIDmode
12375 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12376 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12377 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12378 VAR_INIT_STATUS_INITIALIZED);
12379 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12380 VAR_INIT_STATUS_INITIALIZED);
12381 if (op0 == NULL || op1 == NULL)
12382 return NULL;
12383 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12384 for (i = 0; i < 2; i++)
12386 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12387 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12388 mode, mem_mode,
12389 VAR_INIT_STATUS_INITIALIZED);
12390 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12391 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12392 ? DW_OP_const4u
12393 : HOST_BITS_PER_WIDE_INT == 64
12394 ? DW_OP_const8u : DW_OP_constu,
12395 GET_MODE_MASK (mode), 0);
12396 else
12397 mask[i] = NULL;
12398 if (mask[i] == NULL)
12399 return NULL;
12400 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12402 ret = op0;
12403 add_loc_descr (&ret, op1);
12404 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12405 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12406 if (GET_CODE (rtl) == ROTATERT)
12408 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12409 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12410 GET_MODE_BITSIZE (mode), 0));
12412 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12413 if (mask[0] != NULL)
12414 add_loc_descr (&ret, mask[0]);
12415 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12416 if (mask[1] != NULL)
12418 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12419 add_loc_descr (&ret, mask[1]);
12420 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12422 if (GET_CODE (rtl) == ROTATE)
12424 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12425 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12426 GET_MODE_BITSIZE (mode), 0));
12428 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12429 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12430 return ret;
12433 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12434 for DEBUG_PARAMETER_REF RTL. */
12436 static dw_loc_descr_ref
12437 parameter_ref_descriptor (rtx rtl)
12439 dw_loc_descr_ref ret;
12440 dw_die_ref ref;
12442 if (dwarf_strict)
12443 return NULL;
12444 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12445 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12446 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12447 if (ref)
12449 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12450 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12451 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12453 else
12455 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12456 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12458 return ret;
12461 /* The following routine converts the RTL for a variable or parameter
12462 (resident in memory) into an equivalent Dwarf representation of a
12463 mechanism for getting the address of that same variable onto the top of a
12464 hypothetical "address evaluation" stack.
12466 When creating memory location descriptors, we are effectively transforming
12467 the RTL for a memory-resident object into its Dwarf postfix expression
12468 equivalent. This routine recursively descends an RTL tree, turning
12469 it into Dwarf postfix code as it goes.
12471 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12473 MEM_MODE is the mode of the memory reference, needed to handle some
12474 autoincrement addressing modes.
12476 Return 0 if we can't represent the location. */
12478 dw_loc_descr_ref
12479 mem_loc_descriptor (rtx rtl, machine_mode mode,
12480 machine_mode mem_mode,
12481 enum var_init_status initialized)
12483 dw_loc_descr_ref mem_loc_result = NULL;
12484 enum dwarf_location_atom op;
12485 dw_loc_descr_ref op0, op1;
12486 rtx inner = NULL_RTX;
12488 if (mode == VOIDmode)
12489 mode = GET_MODE (rtl);
12491 /* Note that for a dynamically sized array, the location we will generate a
12492 description of here will be the lowest numbered location which is
12493 actually within the array. That's *not* necessarily the same as the
12494 zeroth element of the array. */
12496 rtl = targetm.delegitimize_address (rtl);
12498 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12499 return NULL;
12501 switch (GET_CODE (rtl))
12503 case POST_INC:
12504 case POST_DEC:
12505 case POST_MODIFY:
12506 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12508 case SUBREG:
12509 /* The case of a subreg may arise when we have a local (register)
12510 variable or a formal (register) parameter which doesn't quite fill
12511 up an entire register. For now, just assume that it is
12512 legitimate to make the Dwarf info refer to the whole register which
12513 contains the given subreg. */
12514 if (!subreg_lowpart_p (rtl))
12515 break;
12516 inner = SUBREG_REG (rtl);
12517 case TRUNCATE:
12518 if (inner == NULL_RTX)
12519 inner = XEXP (rtl, 0);
12520 if (GET_MODE_CLASS (mode) == MODE_INT
12521 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12522 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12523 #ifdef POINTERS_EXTEND_UNSIGNED
12524 || (mode == Pmode && mem_mode != VOIDmode)
12525 #endif
12527 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12529 mem_loc_result = mem_loc_descriptor (inner,
12530 GET_MODE (inner),
12531 mem_mode, initialized);
12532 break;
12534 if (dwarf_strict)
12535 break;
12536 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12537 break;
12538 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12539 && (GET_MODE_CLASS (mode) != MODE_INT
12540 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12541 break;
12542 else
12544 dw_die_ref type_die;
12545 dw_loc_descr_ref cvt;
12547 mem_loc_result = mem_loc_descriptor (inner,
12548 GET_MODE (inner),
12549 mem_mode, initialized);
12550 if (mem_loc_result == NULL)
12551 break;
12552 type_die = base_type_for_mode (mode,
12553 GET_MODE_CLASS (mode) == MODE_INT);
12554 if (type_die == NULL)
12556 mem_loc_result = NULL;
12557 break;
12559 if (GET_MODE_SIZE (mode)
12560 != GET_MODE_SIZE (GET_MODE (inner)))
12561 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12562 else
12563 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12564 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12565 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12566 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12567 add_loc_descr (&mem_loc_result, cvt);
12569 break;
12571 case REG:
12572 if (GET_MODE_CLASS (mode) != MODE_INT
12573 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12574 && rtl != arg_pointer_rtx
12575 && rtl != frame_pointer_rtx
12576 #ifdef POINTERS_EXTEND_UNSIGNED
12577 && (mode != Pmode || mem_mode == VOIDmode)
12578 #endif
12581 dw_die_ref type_die;
12582 unsigned int dbx_regnum;
12584 if (dwarf_strict)
12585 break;
12586 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12587 break;
12588 type_die = base_type_for_mode (mode,
12589 GET_MODE_CLASS (mode) == MODE_INT);
12590 if (type_die == NULL)
12591 break;
12593 dbx_regnum = dbx_reg_number (rtl);
12594 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12595 break;
12596 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12597 dbx_regnum, 0);
12598 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12599 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12600 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12601 break;
12603 /* Whenever a register number forms a part of the description of the
12604 method for calculating the (dynamic) address of a memory resident
12605 object, DWARF rules require the register number be referred to as
12606 a "base register". This distinction is not based in any way upon
12607 what category of register the hardware believes the given register
12608 belongs to. This is strictly DWARF terminology we're dealing with
12609 here. Note that in cases where the location of a memory-resident
12610 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12611 OP_CONST (0)) the actual DWARF location descriptor that we generate
12612 may just be OP_BASEREG (basereg). This may look deceptively like
12613 the object in question was allocated to a register (rather than in
12614 memory) so DWARF consumers need to be aware of the subtle
12615 distinction between OP_REG and OP_BASEREG. */
12616 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12617 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12618 else if (stack_realign_drap
12619 && crtl->drap_reg
12620 && crtl->args.internal_arg_pointer == rtl
12621 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12623 /* If RTL is internal_arg_pointer, which has been optimized
12624 out, use DRAP instead. */
12625 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12626 VAR_INIT_STATUS_INITIALIZED);
12628 break;
12630 case SIGN_EXTEND:
12631 case ZERO_EXTEND:
12632 if (GET_MODE_CLASS (mode) != MODE_INT)
12633 break;
12634 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12635 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12636 if (op0 == 0)
12637 break;
12638 else if (GET_CODE (rtl) == ZERO_EXTEND
12639 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12640 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12641 < HOST_BITS_PER_WIDE_INT
12642 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12643 to expand zero extend as two shifts instead of
12644 masking. */
12645 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12647 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12648 mem_loc_result = op0;
12649 add_loc_descr (&mem_loc_result,
12650 int_loc_descriptor (GET_MODE_MASK (imode)));
12651 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12653 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12655 int shift = DWARF2_ADDR_SIZE
12656 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12657 shift *= BITS_PER_UNIT;
12658 if (GET_CODE (rtl) == SIGN_EXTEND)
12659 op = DW_OP_shra;
12660 else
12661 op = DW_OP_shr;
12662 mem_loc_result = op0;
12663 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12664 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12665 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12666 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12668 else if (!dwarf_strict)
12670 dw_die_ref type_die1, type_die2;
12671 dw_loc_descr_ref cvt;
12673 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12674 GET_CODE (rtl) == ZERO_EXTEND);
12675 if (type_die1 == NULL)
12676 break;
12677 type_die2 = base_type_for_mode (mode, 1);
12678 if (type_die2 == NULL)
12679 break;
12680 mem_loc_result = op0;
12681 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12682 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12683 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12684 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12685 add_loc_descr (&mem_loc_result, cvt);
12686 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12687 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12688 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12689 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12690 add_loc_descr (&mem_loc_result, cvt);
12692 break;
12694 case MEM:
12696 rtx new_rtl = avoid_constant_pool_reference (rtl);
12697 if (new_rtl != rtl)
12699 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12700 initialized);
12701 if (mem_loc_result != NULL)
12702 return mem_loc_result;
12705 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12706 get_address_mode (rtl), mode,
12707 VAR_INIT_STATUS_INITIALIZED);
12708 if (mem_loc_result == NULL)
12709 mem_loc_result = tls_mem_loc_descriptor (rtl);
12710 if (mem_loc_result != NULL)
12712 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12713 || GET_MODE_CLASS (mode) != MODE_INT)
12715 dw_die_ref type_die;
12716 dw_loc_descr_ref deref;
12718 if (dwarf_strict)
12719 return NULL;
12720 type_die
12721 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12722 if (type_die == NULL)
12723 return NULL;
12724 deref = new_loc_descr (DW_OP_GNU_deref_type,
12725 GET_MODE_SIZE (mode), 0);
12726 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12727 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12728 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12729 add_loc_descr (&mem_loc_result, deref);
12731 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12732 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12733 else
12734 add_loc_descr (&mem_loc_result,
12735 new_loc_descr (DW_OP_deref_size,
12736 GET_MODE_SIZE (mode), 0));
12738 break;
12740 case LO_SUM:
12741 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12743 case LABEL_REF:
12744 /* Some ports can transform a symbol ref into a label ref, because
12745 the symbol ref is too far away and has to be dumped into a constant
12746 pool. */
12747 case CONST:
12748 case SYMBOL_REF:
12749 if ((GET_MODE_CLASS (mode) != MODE_INT
12750 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12751 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12752 #ifdef POINTERS_EXTEND_UNSIGNED
12753 && (mode != Pmode || mem_mode == VOIDmode)
12754 #endif
12756 break;
12757 if (GET_CODE (rtl) == SYMBOL_REF
12758 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12760 dw_loc_descr_ref temp;
12762 /* If this is not defined, we have no way to emit the data. */
12763 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12764 break;
12766 temp = new_addr_loc_descr (rtl, dtprel_true);
12768 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12769 add_loc_descr (&mem_loc_result, temp);
12771 break;
12774 if (!const_ok_for_output (rtl))
12775 break;
12777 symref:
12778 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12779 vec_safe_push (used_rtx_array, rtl);
12780 break;
12782 case CONCAT:
12783 case CONCATN:
12784 case VAR_LOCATION:
12785 case DEBUG_IMPLICIT_PTR:
12786 expansion_failed (NULL_TREE, rtl,
12787 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12788 return 0;
12790 case ENTRY_VALUE:
12791 if (dwarf_strict)
12792 return NULL;
12793 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12795 if (GET_MODE_CLASS (mode) != MODE_INT
12796 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12797 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12798 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12799 else
12801 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12802 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12803 return NULL;
12804 op0 = one_reg_loc_descriptor (dbx_regnum,
12805 VAR_INIT_STATUS_INITIALIZED);
12808 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12809 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12811 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12812 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12813 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12814 return NULL;
12816 else
12817 gcc_unreachable ();
12818 if (op0 == NULL)
12819 return NULL;
12820 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12821 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12822 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12823 break;
12825 case DEBUG_PARAMETER_REF:
12826 mem_loc_result = parameter_ref_descriptor (rtl);
12827 break;
12829 case PRE_MODIFY:
12830 /* Extract the PLUS expression nested inside and fall into
12831 PLUS code below. */
12832 rtl = XEXP (rtl, 1);
12833 goto plus;
12835 case PRE_INC:
12836 case PRE_DEC:
12837 /* Turn these into a PLUS expression and fall into the PLUS code
12838 below. */
12839 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12840 gen_int_mode (GET_CODE (rtl) == PRE_INC
12841 ? GET_MODE_UNIT_SIZE (mem_mode)
12842 : -GET_MODE_UNIT_SIZE (mem_mode),
12843 mode));
12845 /* ... fall through ... */
12847 case PLUS:
12848 plus:
12849 if (is_based_loc (rtl)
12850 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12851 || XEXP (rtl, 0) == arg_pointer_rtx
12852 || XEXP (rtl, 0) == frame_pointer_rtx)
12853 && GET_MODE_CLASS (mode) == MODE_INT)
12854 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12855 INTVAL (XEXP (rtl, 1)),
12856 VAR_INIT_STATUS_INITIALIZED);
12857 else
12859 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12860 VAR_INIT_STATUS_INITIALIZED);
12861 if (mem_loc_result == 0)
12862 break;
12864 if (CONST_INT_P (XEXP (rtl, 1))
12865 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12866 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12867 else
12869 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12870 VAR_INIT_STATUS_INITIALIZED);
12871 if (op1 == 0)
12872 return NULL;
12873 add_loc_descr (&mem_loc_result, op1);
12874 add_loc_descr (&mem_loc_result,
12875 new_loc_descr (DW_OP_plus, 0, 0));
12878 break;
12880 /* If a pseudo-reg is optimized away, it is possible for it to
12881 be replaced with a MEM containing a multiply or shift. */
12882 case MINUS:
12883 op = DW_OP_minus;
12884 goto do_binop;
12886 case MULT:
12887 op = DW_OP_mul;
12888 goto do_binop;
12890 case DIV:
12891 if (!dwarf_strict
12892 && GET_MODE_CLASS (mode) == MODE_INT
12893 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12895 mem_loc_result = typed_binop (DW_OP_div, rtl,
12896 base_type_for_mode (mode, 0),
12897 mode, mem_mode);
12898 break;
12900 op = DW_OP_div;
12901 goto do_binop;
12903 case UMOD:
12904 op = DW_OP_mod;
12905 goto do_binop;
12907 case ASHIFT:
12908 op = DW_OP_shl;
12909 goto do_shift;
12911 case ASHIFTRT:
12912 op = DW_OP_shra;
12913 goto do_shift;
12915 case LSHIFTRT:
12916 op = DW_OP_shr;
12917 goto do_shift;
12919 do_shift:
12920 if (GET_MODE_CLASS (mode) != MODE_INT)
12921 break;
12922 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12923 VAR_INIT_STATUS_INITIALIZED);
12925 rtx rtlop1 = XEXP (rtl, 1);
12926 if (GET_MODE (rtlop1) != VOIDmode
12927 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12928 < GET_MODE_BITSIZE (mode))
12929 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12930 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12931 VAR_INIT_STATUS_INITIALIZED);
12934 if (op0 == 0 || op1 == 0)
12935 break;
12937 mem_loc_result = op0;
12938 add_loc_descr (&mem_loc_result, op1);
12939 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12940 break;
12942 case AND:
12943 op = DW_OP_and;
12944 goto do_binop;
12946 case IOR:
12947 op = DW_OP_or;
12948 goto do_binop;
12950 case XOR:
12951 op = DW_OP_xor;
12952 goto do_binop;
12954 do_binop:
12955 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12956 VAR_INIT_STATUS_INITIALIZED);
12957 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12958 VAR_INIT_STATUS_INITIALIZED);
12960 if (op0 == 0 || op1 == 0)
12961 break;
12963 mem_loc_result = op0;
12964 add_loc_descr (&mem_loc_result, op1);
12965 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12966 break;
12968 case MOD:
12969 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12971 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12972 base_type_for_mode (mode, 0),
12973 mode, mem_mode);
12974 break;
12977 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12978 VAR_INIT_STATUS_INITIALIZED);
12979 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12980 VAR_INIT_STATUS_INITIALIZED);
12982 if (op0 == 0 || op1 == 0)
12983 break;
12985 mem_loc_result = op0;
12986 add_loc_descr (&mem_loc_result, op1);
12987 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12988 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12989 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12990 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12991 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12992 break;
12994 case UDIV:
12995 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12997 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12999 op = DW_OP_div;
13000 goto do_binop;
13002 mem_loc_result = typed_binop (DW_OP_div, rtl,
13003 base_type_for_mode (mode, 1),
13004 mode, mem_mode);
13006 break;
13008 case NOT:
13009 op = DW_OP_not;
13010 goto do_unop;
13012 case ABS:
13013 op = DW_OP_abs;
13014 goto do_unop;
13016 case NEG:
13017 op = DW_OP_neg;
13018 goto do_unop;
13020 do_unop:
13021 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13022 VAR_INIT_STATUS_INITIALIZED);
13024 if (op0 == 0)
13025 break;
13027 mem_loc_result = op0;
13028 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13029 break;
13031 case CONST_INT:
13032 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13033 #ifdef POINTERS_EXTEND_UNSIGNED
13034 || (mode == Pmode
13035 && mem_mode != VOIDmode
13036 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13037 #endif
13040 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13041 break;
13043 if (!dwarf_strict
13044 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13045 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13047 dw_die_ref type_die = base_type_for_mode (mode, 1);
13048 machine_mode amode;
13049 if (type_die == NULL)
13050 return NULL;
13051 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13052 MODE_INT, 0);
13053 if (INTVAL (rtl) >= 0
13054 && amode != BLKmode
13055 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13056 /* const DW_OP_GNU_convert <XXX> vs.
13057 DW_OP_GNU_const_type <XXX, 1, const>. */
13058 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13059 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13061 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13062 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13063 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13064 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13065 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13066 add_loc_descr (&mem_loc_result, op0);
13067 return mem_loc_result;
13069 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13070 INTVAL (rtl));
13071 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13072 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13073 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13074 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13075 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13076 else
13078 mem_loc_result->dw_loc_oprnd2.val_class
13079 = dw_val_class_const_double;
13080 mem_loc_result->dw_loc_oprnd2.v.val_double
13081 = double_int::from_shwi (INTVAL (rtl));
13084 break;
13086 case CONST_DOUBLE:
13087 if (!dwarf_strict)
13089 dw_die_ref type_die;
13091 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13092 CONST_DOUBLE rtx could represent either a large integer
13093 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13094 the value is always a floating point constant.
13096 When it is an integer, a CONST_DOUBLE is used whenever
13097 the constant requires 2 HWIs to be adequately represented.
13098 We output CONST_DOUBLEs as blocks. */
13099 if (mode == VOIDmode
13100 || (GET_MODE (rtl) == VOIDmode
13101 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13102 break;
13103 type_die = base_type_for_mode (mode,
13104 GET_MODE_CLASS (mode) == MODE_INT);
13105 if (type_die == NULL)
13106 return NULL;
13107 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13108 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13109 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13110 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13111 #if TARGET_SUPPORTS_WIDE_INT == 0
13112 if (!SCALAR_FLOAT_MODE_P (mode))
13114 mem_loc_result->dw_loc_oprnd2.val_class
13115 = dw_val_class_const_double;
13116 mem_loc_result->dw_loc_oprnd2.v.val_double
13117 = rtx_to_double_int (rtl);
13119 else
13120 #endif
13122 unsigned int length = GET_MODE_SIZE (mode);
13123 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13125 insert_float (rtl, array);
13126 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13127 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13128 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13129 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13132 break;
13134 case CONST_WIDE_INT:
13135 if (!dwarf_strict)
13137 dw_die_ref type_die;
13139 type_die = base_type_for_mode (mode,
13140 GET_MODE_CLASS (mode) == MODE_INT);
13141 if (type_die == NULL)
13142 return NULL;
13143 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13144 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13145 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13146 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13147 mem_loc_result->dw_loc_oprnd2.val_class
13148 = dw_val_class_wide_int;
13149 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13150 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13152 break;
13154 case EQ:
13155 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13156 break;
13158 case GE:
13159 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13160 break;
13162 case GT:
13163 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13164 break;
13166 case LE:
13167 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13168 break;
13170 case LT:
13171 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13172 break;
13174 case NE:
13175 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13176 break;
13178 case GEU:
13179 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13180 break;
13182 case GTU:
13183 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13184 break;
13186 case LEU:
13187 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13188 break;
13190 case LTU:
13191 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13192 break;
13194 case UMIN:
13195 case UMAX:
13196 if (GET_MODE_CLASS (mode) != MODE_INT)
13197 break;
13198 /* FALLTHRU */
13199 case SMIN:
13200 case SMAX:
13201 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13202 break;
13204 case ZERO_EXTRACT:
13205 case SIGN_EXTRACT:
13206 if (CONST_INT_P (XEXP (rtl, 1))
13207 && CONST_INT_P (XEXP (rtl, 2))
13208 && ((unsigned) INTVAL (XEXP (rtl, 1))
13209 + (unsigned) INTVAL (XEXP (rtl, 2))
13210 <= GET_MODE_BITSIZE (mode))
13211 && GET_MODE_CLASS (mode) == MODE_INT
13212 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13213 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13215 int shift, size;
13216 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13217 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13218 if (op0 == 0)
13219 break;
13220 if (GET_CODE (rtl) == SIGN_EXTRACT)
13221 op = DW_OP_shra;
13222 else
13223 op = DW_OP_shr;
13224 mem_loc_result = op0;
13225 size = INTVAL (XEXP (rtl, 1));
13226 shift = INTVAL (XEXP (rtl, 2));
13227 if (BITS_BIG_ENDIAN)
13228 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13229 - shift - size;
13230 if (shift + size != (int) DWARF2_ADDR_SIZE)
13232 add_loc_descr (&mem_loc_result,
13233 int_loc_descriptor (DWARF2_ADDR_SIZE
13234 - shift - size));
13235 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13237 if (size != (int) DWARF2_ADDR_SIZE)
13239 add_loc_descr (&mem_loc_result,
13240 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13241 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13244 break;
13246 case IF_THEN_ELSE:
13248 dw_loc_descr_ref op2, bra_node, drop_node;
13249 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13250 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13251 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13252 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13253 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13254 VAR_INIT_STATUS_INITIALIZED);
13255 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13256 VAR_INIT_STATUS_INITIALIZED);
13257 if (op0 == NULL || op1 == NULL || op2 == NULL)
13258 break;
13260 mem_loc_result = op1;
13261 add_loc_descr (&mem_loc_result, op2);
13262 add_loc_descr (&mem_loc_result, op0);
13263 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13264 add_loc_descr (&mem_loc_result, bra_node);
13265 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13266 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13267 add_loc_descr (&mem_loc_result, drop_node);
13268 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13269 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13271 break;
13273 case FLOAT_EXTEND:
13274 case FLOAT_TRUNCATE:
13275 case FLOAT:
13276 case UNSIGNED_FLOAT:
13277 case FIX:
13278 case UNSIGNED_FIX:
13279 if (!dwarf_strict)
13281 dw_die_ref type_die;
13282 dw_loc_descr_ref cvt;
13284 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13285 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13286 if (op0 == NULL)
13287 break;
13288 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13289 && (GET_CODE (rtl) == FLOAT
13290 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13291 <= DWARF2_ADDR_SIZE))
13293 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13294 GET_CODE (rtl) == UNSIGNED_FLOAT);
13295 if (type_die == NULL)
13296 break;
13297 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13298 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13299 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13300 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13301 add_loc_descr (&op0, cvt);
13303 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13304 if (type_die == NULL)
13305 break;
13306 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13307 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13308 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13309 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13310 add_loc_descr (&op0, cvt);
13311 if (GET_MODE_CLASS (mode) == MODE_INT
13312 && (GET_CODE (rtl) == FIX
13313 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13315 op0 = convert_descriptor_to_mode (mode, op0);
13316 if (op0 == NULL)
13317 break;
13319 mem_loc_result = op0;
13321 break;
13323 case CLZ:
13324 case CTZ:
13325 case FFS:
13326 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13327 break;
13329 case POPCOUNT:
13330 case PARITY:
13331 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13332 break;
13334 case BSWAP:
13335 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13336 break;
13338 case ROTATE:
13339 case ROTATERT:
13340 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13341 break;
13343 case COMPARE:
13344 /* In theory, we could implement the above. */
13345 /* DWARF cannot represent the unsigned compare operations
13346 natively. */
13347 case SS_MULT:
13348 case US_MULT:
13349 case SS_DIV:
13350 case US_DIV:
13351 case SS_PLUS:
13352 case US_PLUS:
13353 case SS_MINUS:
13354 case US_MINUS:
13355 case SS_NEG:
13356 case US_NEG:
13357 case SS_ABS:
13358 case SS_ASHIFT:
13359 case US_ASHIFT:
13360 case SS_TRUNCATE:
13361 case US_TRUNCATE:
13362 case UNORDERED:
13363 case ORDERED:
13364 case UNEQ:
13365 case UNGE:
13366 case UNGT:
13367 case UNLE:
13368 case UNLT:
13369 case LTGT:
13370 case FRACT_CONVERT:
13371 case UNSIGNED_FRACT_CONVERT:
13372 case SAT_FRACT:
13373 case UNSIGNED_SAT_FRACT:
13374 case SQRT:
13375 case ASM_OPERANDS:
13376 case VEC_MERGE:
13377 case VEC_SELECT:
13378 case VEC_CONCAT:
13379 case VEC_DUPLICATE:
13380 case UNSPEC:
13381 case HIGH:
13382 case FMA:
13383 case STRICT_LOW_PART:
13384 case CONST_VECTOR:
13385 case CONST_FIXED:
13386 case CLRSB:
13387 case CLOBBER:
13388 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13389 can't express it in the debug info. This can happen e.g. with some
13390 TLS UNSPECs. */
13391 break;
13393 case CONST_STRING:
13394 resolve_one_addr (&rtl);
13395 goto symref;
13397 default:
13398 #ifdef ENABLE_CHECKING
13399 print_rtl (stderr, rtl);
13400 gcc_unreachable ();
13401 #else
13402 break;
13403 #endif
13406 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13407 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13409 return mem_loc_result;
13412 /* Return a descriptor that describes the concatenation of two locations.
13413 This is typically a complex variable. */
13415 static dw_loc_descr_ref
13416 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13418 dw_loc_descr_ref cc_loc_result = NULL;
13419 dw_loc_descr_ref x0_ref
13420 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13421 dw_loc_descr_ref x1_ref
13422 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13424 if (x0_ref == 0 || x1_ref == 0)
13425 return 0;
13427 cc_loc_result = x0_ref;
13428 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13430 add_loc_descr (&cc_loc_result, x1_ref);
13431 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13433 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13434 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13436 return cc_loc_result;
13439 /* Return a descriptor that describes the concatenation of N
13440 locations. */
13442 static dw_loc_descr_ref
13443 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13445 unsigned int i;
13446 dw_loc_descr_ref cc_loc_result = NULL;
13447 unsigned int n = XVECLEN (concatn, 0);
13449 for (i = 0; i < n; ++i)
13451 dw_loc_descr_ref ref;
13452 rtx x = XVECEXP (concatn, 0, i);
13454 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13455 if (ref == NULL)
13456 return NULL;
13458 add_loc_descr (&cc_loc_result, ref);
13459 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13462 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13463 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13465 return cc_loc_result;
13468 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13469 for DEBUG_IMPLICIT_PTR RTL. */
13471 static dw_loc_descr_ref
13472 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13474 dw_loc_descr_ref ret;
13475 dw_die_ref ref;
13477 if (dwarf_strict)
13478 return NULL;
13479 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13480 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13481 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13482 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13483 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13484 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13485 if (ref)
13487 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13488 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13489 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13491 else
13493 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13494 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13496 return ret;
13499 /* Output a proper Dwarf location descriptor for a variable or parameter
13500 which is either allocated in a register or in a memory location. For a
13501 register, we just generate an OP_REG and the register number. For a
13502 memory location we provide a Dwarf postfix expression describing how to
13503 generate the (dynamic) address of the object onto the address stack.
13505 MODE is mode of the decl if this loc_descriptor is going to be used in
13506 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13507 allowed, VOIDmode otherwise.
13509 If we don't know how to describe it, return 0. */
13511 static dw_loc_descr_ref
13512 loc_descriptor (rtx rtl, machine_mode mode,
13513 enum var_init_status initialized)
13515 dw_loc_descr_ref loc_result = NULL;
13517 switch (GET_CODE (rtl))
13519 case SUBREG:
13520 /* The case of a subreg may arise when we have a local (register)
13521 variable or a formal (register) parameter which doesn't quite fill
13522 up an entire register. For now, just assume that it is
13523 legitimate to make the Dwarf info refer to the whole register which
13524 contains the given subreg. */
13525 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13526 loc_result = loc_descriptor (SUBREG_REG (rtl),
13527 GET_MODE (SUBREG_REG (rtl)), initialized);
13528 else
13529 goto do_default;
13530 break;
13532 case REG:
13533 loc_result = reg_loc_descriptor (rtl, initialized);
13534 break;
13536 case MEM:
13537 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13538 GET_MODE (rtl), initialized);
13539 if (loc_result == NULL)
13540 loc_result = tls_mem_loc_descriptor (rtl);
13541 if (loc_result == NULL)
13543 rtx new_rtl = avoid_constant_pool_reference (rtl);
13544 if (new_rtl != rtl)
13545 loc_result = loc_descriptor (new_rtl, mode, initialized);
13547 break;
13549 case CONCAT:
13550 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13551 initialized);
13552 break;
13554 case CONCATN:
13555 loc_result = concatn_loc_descriptor (rtl, initialized);
13556 break;
13558 case VAR_LOCATION:
13559 /* Single part. */
13560 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13562 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13563 if (GET_CODE (loc) == EXPR_LIST)
13564 loc = XEXP (loc, 0);
13565 loc_result = loc_descriptor (loc, mode, initialized);
13566 break;
13569 rtl = XEXP (rtl, 1);
13570 /* FALLTHRU */
13572 case PARALLEL:
13574 rtvec par_elems = XVEC (rtl, 0);
13575 int num_elem = GET_NUM_ELEM (par_elems);
13576 machine_mode mode;
13577 int i;
13579 /* Create the first one, so we have something to add to. */
13580 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13581 VOIDmode, initialized);
13582 if (loc_result == NULL)
13583 return NULL;
13584 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13585 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13586 for (i = 1; i < num_elem; i++)
13588 dw_loc_descr_ref temp;
13590 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13591 VOIDmode, initialized);
13592 if (temp == NULL)
13593 return NULL;
13594 add_loc_descr (&loc_result, temp);
13595 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13596 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13599 break;
13601 case CONST_INT:
13602 if (mode != VOIDmode && mode != BLKmode)
13603 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13604 INTVAL (rtl));
13605 break;
13607 case CONST_DOUBLE:
13608 if (mode == VOIDmode)
13609 mode = GET_MODE (rtl);
13611 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13613 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13615 /* Note that a CONST_DOUBLE rtx could represent either an integer
13616 or a floating-point constant. A CONST_DOUBLE is used whenever
13617 the constant requires more than one word in order to be
13618 adequately represented. We output CONST_DOUBLEs as blocks. */
13619 loc_result = new_loc_descr (DW_OP_implicit_value,
13620 GET_MODE_SIZE (mode), 0);
13621 #if TARGET_SUPPORTS_WIDE_INT == 0
13622 if (!SCALAR_FLOAT_MODE_P (mode))
13624 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13625 loc_result->dw_loc_oprnd2.v.val_double
13626 = rtx_to_double_int (rtl);
13628 else
13629 #endif
13631 unsigned int length = GET_MODE_SIZE (mode);
13632 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13634 insert_float (rtl, array);
13635 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13636 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13637 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13638 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13641 break;
13643 case CONST_WIDE_INT:
13644 if (mode == VOIDmode)
13645 mode = GET_MODE (rtl);
13647 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13649 loc_result = new_loc_descr (DW_OP_implicit_value,
13650 GET_MODE_SIZE (mode), 0);
13651 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13652 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13653 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13655 break;
13657 case CONST_VECTOR:
13658 if (mode == VOIDmode)
13659 mode = GET_MODE (rtl);
13661 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13663 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13664 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13665 unsigned char *array
13666 = ggc_vec_alloc<unsigned char> (length * elt_size);
13667 unsigned int i;
13668 unsigned char *p;
13669 machine_mode imode = GET_MODE_INNER (mode);
13671 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13672 switch (GET_MODE_CLASS (mode))
13674 case MODE_VECTOR_INT:
13675 for (i = 0, p = array; i < length; i++, p += elt_size)
13677 rtx elt = CONST_VECTOR_ELT (rtl, i);
13678 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13680 break;
13682 case MODE_VECTOR_FLOAT:
13683 for (i = 0, p = array; i < length; i++, p += elt_size)
13685 rtx elt = CONST_VECTOR_ELT (rtl, i);
13686 insert_float (elt, p);
13688 break;
13690 default:
13691 gcc_unreachable ();
13694 loc_result = new_loc_descr (DW_OP_implicit_value,
13695 length * elt_size, 0);
13696 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13697 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13698 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13699 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13701 break;
13703 case CONST:
13704 if (mode == VOIDmode
13705 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13706 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13707 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13709 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13710 break;
13712 /* FALLTHROUGH */
13713 case SYMBOL_REF:
13714 if (!const_ok_for_output (rtl))
13715 break;
13716 case LABEL_REF:
13717 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13718 && (dwarf_version >= 4 || !dwarf_strict))
13720 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13721 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13722 vec_safe_push (used_rtx_array, rtl);
13724 break;
13726 case DEBUG_IMPLICIT_PTR:
13727 loc_result = implicit_ptr_descriptor (rtl, 0);
13728 break;
13730 case PLUS:
13731 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13732 && CONST_INT_P (XEXP (rtl, 1)))
13734 loc_result
13735 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13736 break;
13738 /* FALLTHRU */
13739 do_default:
13740 default:
13741 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13742 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13743 && dwarf_version >= 4)
13744 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13746 /* Value expression. */
13747 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13748 if (loc_result)
13749 add_loc_descr (&loc_result,
13750 new_loc_descr (DW_OP_stack_value, 0, 0));
13752 break;
13755 return loc_result;
13758 /* We need to figure out what section we should use as the base for the
13759 address ranges where a given location is valid.
13760 1. If this particular DECL has a section associated with it, use that.
13761 2. If this function has a section associated with it, use that.
13762 3. Otherwise, use the text section.
13763 XXX: If you split a variable across multiple sections, we won't notice. */
13765 static const char *
13766 secname_for_decl (const_tree decl)
13768 const char *secname;
13770 if (VAR_OR_FUNCTION_DECL_P (decl)
13771 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13772 && DECL_SECTION_NAME (decl))
13773 secname = DECL_SECTION_NAME (decl);
13774 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13775 secname = DECL_SECTION_NAME (current_function_decl);
13776 else if (cfun && in_cold_section_p)
13777 secname = crtl->subsections.cold_section_label;
13778 else
13779 secname = text_section_label;
13781 return secname;
13784 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13786 static bool
13787 decl_by_reference_p (tree decl)
13789 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13790 || TREE_CODE (decl) == VAR_DECL)
13791 && DECL_BY_REFERENCE (decl));
13794 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13795 for VARLOC. */
13797 static dw_loc_descr_ref
13798 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13799 enum var_init_status initialized)
13801 int have_address = 0;
13802 dw_loc_descr_ref descr;
13803 machine_mode mode;
13805 if (want_address != 2)
13807 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13808 /* Single part. */
13809 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13811 varloc = PAT_VAR_LOCATION_LOC (varloc);
13812 if (GET_CODE (varloc) == EXPR_LIST)
13813 varloc = XEXP (varloc, 0);
13814 mode = GET_MODE (varloc);
13815 if (MEM_P (varloc))
13817 rtx addr = XEXP (varloc, 0);
13818 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13819 mode, initialized);
13820 if (descr)
13821 have_address = 1;
13822 else
13824 rtx x = avoid_constant_pool_reference (varloc);
13825 if (x != varloc)
13826 descr = mem_loc_descriptor (x, mode, VOIDmode,
13827 initialized);
13830 else
13831 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13833 else
13834 return 0;
13836 else
13838 if (GET_CODE (varloc) == VAR_LOCATION)
13839 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13840 else
13841 mode = DECL_MODE (loc);
13842 descr = loc_descriptor (varloc, mode, initialized);
13843 have_address = 1;
13846 if (!descr)
13847 return 0;
13849 if (want_address == 2 && !have_address
13850 && (dwarf_version >= 4 || !dwarf_strict))
13852 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13854 expansion_failed (loc, NULL_RTX,
13855 "DWARF address size mismatch");
13856 return 0;
13858 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13859 have_address = 1;
13861 /* Show if we can't fill the request for an address. */
13862 if (want_address && !have_address)
13864 expansion_failed (loc, NULL_RTX,
13865 "Want address and only have value");
13866 return 0;
13869 /* If we've got an address and don't want one, dereference. */
13870 if (!want_address && have_address)
13872 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13873 enum dwarf_location_atom op;
13875 if (size > DWARF2_ADDR_SIZE || size == -1)
13877 expansion_failed (loc, NULL_RTX,
13878 "DWARF address size mismatch");
13879 return 0;
13881 else if (size == DWARF2_ADDR_SIZE)
13882 op = DW_OP_deref;
13883 else
13884 op = DW_OP_deref_size;
13886 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13889 return descr;
13892 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13893 if it is not possible. */
13895 static dw_loc_descr_ref
13896 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13898 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13899 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13900 else if (dwarf_version >= 3 || !dwarf_strict)
13901 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13902 else
13903 return NULL;
13906 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13907 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13909 static dw_loc_descr_ref
13910 dw_sra_loc_expr (tree decl, rtx loc)
13912 rtx p;
13913 unsigned int padsize = 0;
13914 dw_loc_descr_ref descr, *descr_tail;
13915 unsigned HOST_WIDE_INT decl_size;
13916 rtx varloc;
13917 enum var_init_status initialized;
13919 if (DECL_SIZE (decl) == NULL
13920 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13921 return NULL;
13923 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13924 descr = NULL;
13925 descr_tail = &descr;
13927 for (p = loc; p; p = XEXP (p, 1))
13929 unsigned int bitsize = decl_piece_bitsize (p);
13930 rtx loc_note = *decl_piece_varloc_ptr (p);
13931 dw_loc_descr_ref cur_descr;
13932 dw_loc_descr_ref *tail, last = NULL;
13933 unsigned int opsize = 0;
13935 if (loc_note == NULL_RTX
13936 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13938 padsize += bitsize;
13939 continue;
13941 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13942 varloc = NOTE_VAR_LOCATION (loc_note);
13943 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13944 if (cur_descr == NULL)
13946 padsize += bitsize;
13947 continue;
13950 /* Check that cur_descr either doesn't use
13951 DW_OP_*piece operations, or their sum is equal
13952 to bitsize. Otherwise we can't embed it. */
13953 for (tail = &cur_descr; *tail != NULL;
13954 tail = &(*tail)->dw_loc_next)
13955 if ((*tail)->dw_loc_opc == DW_OP_piece)
13957 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13958 * BITS_PER_UNIT;
13959 last = *tail;
13961 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13963 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13964 last = *tail;
13967 if (last != NULL && opsize != bitsize)
13969 padsize += bitsize;
13970 /* Discard the current piece of the descriptor and release any
13971 addr_table entries it uses. */
13972 remove_loc_list_addr_table_entries (cur_descr);
13973 continue;
13976 /* If there is a hole, add DW_OP_*piece after empty DWARF
13977 expression, which means that those bits are optimized out. */
13978 if (padsize)
13980 if (padsize > decl_size)
13982 remove_loc_list_addr_table_entries (cur_descr);
13983 goto discard_descr;
13985 decl_size -= padsize;
13986 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13987 if (*descr_tail == NULL)
13989 remove_loc_list_addr_table_entries (cur_descr);
13990 goto discard_descr;
13992 descr_tail = &(*descr_tail)->dw_loc_next;
13993 padsize = 0;
13995 *descr_tail = cur_descr;
13996 descr_tail = tail;
13997 if (bitsize > decl_size)
13998 goto discard_descr;
13999 decl_size -= bitsize;
14000 if (last == NULL)
14002 HOST_WIDE_INT offset = 0;
14003 if (GET_CODE (varloc) == VAR_LOCATION
14004 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14006 varloc = PAT_VAR_LOCATION_LOC (varloc);
14007 if (GET_CODE (varloc) == EXPR_LIST)
14008 varloc = XEXP (varloc, 0);
14012 if (GET_CODE (varloc) == CONST
14013 || GET_CODE (varloc) == SIGN_EXTEND
14014 || GET_CODE (varloc) == ZERO_EXTEND)
14015 varloc = XEXP (varloc, 0);
14016 else if (GET_CODE (varloc) == SUBREG)
14017 varloc = SUBREG_REG (varloc);
14018 else
14019 break;
14021 while (1);
14022 /* DW_OP_bit_size offset should be zero for register
14023 or implicit location descriptions and empty location
14024 descriptions, but for memory addresses needs big endian
14025 adjustment. */
14026 if (MEM_P (varloc))
14028 unsigned HOST_WIDE_INT memsize
14029 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14030 if (memsize != bitsize)
14032 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14033 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14034 goto discard_descr;
14035 if (memsize < bitsize)
14036 goto discard_descr;
14037 if (BITS_BIG_ENDIAN)
14038 offset = memsize - bitsize;
14042 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14043 if (*descr_tail == NULL)
14044 goto discard_descr;
14045 descr_tail = &(*descr_tail)->dw_loc_next;
14049 /* If there were any non-empty expressions, add padding till the end of
14050 the decl. */
14051 if (descr != NULL && decl_size != 0)
14053 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14054 if (*descr_tail == NULL)
14055 goto discard_descr;
14057 return descr;
14059 discard_descr:
14060 /* Discard the descriptor and release any addr_table entries it uses. */
14061 remove_loc_list_addr_table_entries (descr);
14062 return NULL;
14065 /* Return the dwarf representation of the location list LOC_LIST of
14066 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14067 function. */
14069 static dw_loc_list_ref
14070 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14072 const char *endname, *secname;
14073 rtx varloc;
14074 enum var_init_status initialized;
14075 struct var_loc_node *node;
14076 dw_loc_descr_ref descr;
14077 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14078 dw_loc_list_ref list = NULL;
14079 dw_loc_list_ref *listp = &list;
14081 /* Now that we know what section we are using for a base,
14082 actually construct the list of locations.
14083 The first location information is what is passed to the
14084 function that creates the location list, and the remaining
14085 locations just get added on to that list.
14086 Note that we only know the start address for a location
14087 (IE location changes), so to build the range, we use
14088 the range [current location start, next location start].
14089 This means we have to special case the last node, and generate
14090 a range of [last location start, end of function label]. */
14092 secname = secname_for_decl (decl);
14094 for (node = loc_list->first; node; node = node->next)
14095 if (GET_CODE (node->loc) == EXPR_LIST
14096 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14098 if (GET_CODE (node->loc) == EXPR_LIST)
14100 /* This requires DW_OP_{,bit_}piece, which is not usable
14101 inside DWARF expressions. */
14102 if (want_address != 2)
14103 continue;
14104 descr = dw_sra_loc_expr (decl, node->loc);
14105 if (descr == NULL)
14106 continue;
14108 else
14110 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14111 varloc = NOTE_VAR_LOCATION (node->loc);
14112 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14114 if (descr)
14116 bool range_across_switch = false;
14117 /* If section switch happens in between node->label
14118 and node->next->label (or end of function) and
14119 we can't emit it as a single entry list,
14120 emit two ranges, first one ending at the end
14121 of first partition and second one starting at the
14122 beginning of second partition. */
14123 if (node == loc_list->last_before_switch
14124 && (node != loc_list->first || loc_list->first->next)
14125 && current_function_decl)
14127 endname = cfun->fde->dw_fde_end;
14128 range_across_switch = true;
14130 /* The variable has a location between NODE->LABEL and
14131 NODE->NEXT->LABEL. */
14132 else if (node->next)
14133 endname = node->next->label;
14134 /* If the variable has a location at the last label
14135 it keeps its location until the end of function. */
14136 else if (!current_function_decl)
14137 endname = text_end_label;
14138 else
14140 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14141 current_function_funcdef_no);
14142 endname = ggc_strdup (label_id);
14145 *listp = new_loc_list (descr, node->label, endname, secname);
14146 if (TREE_CODE (decl) == PARM_DECL
14147 && node == loc_list->first
14148 && NOTE_P (node->loc)
14149 && strcmp (node->label, endname) == 0)
14150 (*listp)->force = true;
14151 listp = &(*listp)->dw_loc_next;
14153 if (range_across_switch)
14155 if (GET_CODE (node->loc) == EXPR_LIST)
14156 descr = dw_sra_loc_expr (decl, node->loc);
14157 else
14159 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14160 varloc = NOTE_VAR_LOCATION (node->loc);
14161 descr = dw_loc_list_1 (decl, varloc, want_address,
14162 initialized);
14164 gcc_assert (descr);
14165 /* The variable has a location between NODE->LABEL and
14166 NODE->NEXT->LABEL. */
14167 if (node->next)
14168 endname = node->next->label;
14169 else
14170 endname = cfun->fde->dw_fde_second_end;
14171 *listp = new_loc_list (descr,
14172 cfun->fde->dw_fde_second_begin,
14173 endname, secname);
14174 listp = &(*listp)->dw_loc_next;
14179 /* Try to avoid the overhead of a location list emitting a location
14180 expression instead, but only if we didn't have more than one
14181 location entry in the first place. If some entries were not
14182 representable, we don't want to pretend a single entry that was
14183 applies to the entire scope in which the variable is
14184 available. */
14185 if (list && loc_list->first->next)
14186 gen_llsym (list);
14188 return list;
14191 /* Return if the loc_list has only single element and thus can be represented
14192 as location description. */
14194 static bool
14195 single_element_loc_list_p (dw_loc_list_ref list)
14197 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14198 return !list->ll_symbol;
14201 /* To each location in list LIST add loc descr REF. */
14203 static void
14204 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14206 dw_loc_descr_ref copy;
14207 add_loc_descr (&list->expr, ref);
14208 list = list->dw_loc_next;
14209 while (list)
14211 copy = ggc_alloc<dw_loc_descr_node> ();
14212 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14213 add_loc_descr (&list->expr, copy);
14214 while (copy->dw_loc_next)
14216 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14217 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14218 copy->dw_loc_next = new_copy;
14219 copy = new_copy;
14221 list = list->dw_loc_next;
14225 /* Given two lists RET and LIST
14226 produce location list that is result of adding expression in LIST
14227 to expression in RET on each position in program.
14228 Might be destructive on both RET and LIST.
14230 TODO: We handle only simple cases of RET or LIST having at most one
14231 element. General case would inolve sorting the lists in program order
14232 and merging them that will need some additional work.
14233 Adding that will improve quality of debug info especially for SRA-ed
14234 structures. */
14236 static void
14237 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14239 if (!list)
14240 return;
14241 if (!*ret)
14243 *ret = list;
14244 return;
14246 if (!list->dw_loc_next)
14248 add_loc_descr_to_each (*ret, list->expr);
14249 return;
14251 if (!(*ret)->dw_loc_next)
14253 add_loc_descr_to_each (list, (*ret)->expr);
14254 *ret = list;
14255 return;
14257 expansion_failed (NULL_TREE, NULL_RTX,
14258 "Don't know how to merge two non-trivial"
14259 " location lists.\n");
14260 *ret = NULL;
14261 return;
14264 /* LOC is constant expression. Try a luck, look it up in constant
14265 pool and return its loc_descr of its address. */
14267 static dw_loc_descr_ref
14268 cst_pool_loc_descr (tree loc)
14270 /* Get an RTL for this, if something has been emitted. */
14271 rtx rtl = lookup_constant_def (loc);
14273 if (!rtl || !MEM_P (rtl))
14275 gcc_assert (!rtl);
14276 return 0;
14278 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14280 /* TODO: We might get more coverage if we was actually delaying expansion
14281 of all expressions till end of compilation when constant pools are fully
14282 populated. */
14283 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14285 expansion_failed (loc, NULL_RTX,
14286 "CST value in contant pool but not marked.");
14287 return 0;
14289 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14290 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14293 /* Return dw_loc_list representing address of addr_expr LOC
14294 by looking for inner INDIRECT_REF expression and turning
14295 it into simple arithmetics.
14297 See loc_list_from_tree for the meaning of CONTEXT. */
14299 static dw_loc_list_ref
14300 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14301 const loc_descr_context *context)
14303 tree obj, offset;
14304 HOST_WIDE_INT bitsize, bitpos, bytepos;
14305 machine_mode mode;
14306 int unsignedp, volatilep = 0;
14307 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14309 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14310 &bitsize, &bitpos, &offset, &mode,
14311 &unsignedp, &volatilep, false);
14312 STRIP_NOPS (obj);
14313 if (bitpos % BITS_PER_UNIT)
14315 expansion_failed (loc, NULL_RTX, "bitfield access");
14316 return 0;
14318 if (!INDIRECT_REF_P (obj))
14320 expansion_failed (obj,
14321 NULL_RTX, "no indirect ref in inner refrence");
14322 return 0;
14324 if (!offset && !bitpos)
14325 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14326 context);
14327 else if (toplev
14328 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14329 && (dwarf_version >= 4 || !dwarf_strict))
14331 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14332 if (!list_ret)
14333 return 0;
14334 if (offset)
14336 /* Variable offset. */
14337 list_ret1 = loc_list_from_tree (offset, 0, context);
14338 if (list_ret1 == 0)
14339 return 0;
14340 add_loc_list (&list_ret, list_ret1);
14341 if (!list_ret)
14342 return 0;
14343 add_loc_descr_to_each (list_ret,
14344 new_loc_descr (DW_OP_plus, 0, 0));
14346 bytepos = bitpos / BITS_PER_UNIT;
14347 if (bytepos > 0)
14348 add_loc_descr_to_each (list_ret,
14349 new_loc_descr (DW_OP_plus_uconst,
14350 bytepos, 0));
14351 else if (bytepos < 0)
14352 loc_list_plus_const (list_ret, bytepos);
14353 add_loc_descr_to_each (list_ret,
14354 new_loc_descr (DW_OP_stack_value, 0, 0));
14356 return list_ret;
14360 /* Helper structure for location descriptions generation. */
14361 struct loc_descr_context
14363 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14364 NULL_TREE if DW_OP_push_object_address in invalid for this location
14365 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14366 tree context_type;
14367 /* The ..._DECL node that should be translated as a
14368 DW_OP_push_object_address operation. */
14369 tree base_decl;
14372 /* Generate Dwarf location list representing LOC.
14373 If WANT_ADDRESS is false, expression computing LOC will be computed
14374 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14375 if WANT_ADDRESS is 2, expression computing address useable in location
14376 will be returned (i.e. DW_OP_reg can be used
14377 to refer to register values).
14379 CONTEXT provides information to customize the location descriptions
14380 generation. Its context_type field specifies what type is implicitly
14381 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14382 will not be generated.
14384 If CONTEXT is NULL, the behavior is the same as if both context_type and
14385 base_decl fields were NULL_TREE. */
14387 static dw_loc_list_ref
14388 loc_list_from_tree (tree loc, int want_address,
14389 const struct loc_descr_context *context)
14391 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14392 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14393 int have_address = 0;
14394 enum dwarf_location_atom op;
14396 /* ??? Most of the time we do not take proper care for sign/zero
14397 extending the values properly. Hopefully this won't be a real
14398 problem... */
14400 if (context != NULL
14401 && context->base_decl == loc
14402 && want_address == 0)
14404 if (dwarf_version >= 3 || !dwarf_strict)
14405 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14406 NULL, NULL, NULL);
14407 else
14408 return NULL;
14411 switch (TREE_CODE (loc))
14413 case ERROR_MARK:
14414 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14415 return 0;
14417 case PLACEHOLDER_EXPR:
14418 /* This case involves extracting fields from an object to determine the
14419 position of other fields. It is supposed to appear only as the first
14420 operand of COMPONENT_REF nodes and to reference precisely the type
14421 that the context allows. */
14422 if (context != NULL
14423 && TREE_TYPE (loc) == context->context_type
14424 && want_address >= 1)
14426 if (dwarf_version >= 3 || !dwarf_strict)
14428 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14429 have_address = 1;
14430 break;
14432 else
14433 return NULL;
14435 else
14436 expansion_failed (loc, NULL_RTX,
14437 "PLACEHOLDER_EXPR for an unexpected type");
14438 break;
14440 case CALL_EXPR:
14441 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14442 /* There are no opcodes for these operations. */
14443 return 0;
14445 case PREINCREMENT_EXPR:
14446 case PREDECREMENT_EXPR:
14447 case POSTINCREMENT_EXPR:
14448 case POSTDECREMENT_EXPR:
14449 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14450 /* There are no opcodes for these operations. */
14451 return 0;
14453 case ADDR_EXPR:
14454 /* If we already want an address, see if there is INDIRECT_REF inside
14455 e.g. for &this->field. */
14456 if (want_address)
14458 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14459 (loc, want_address == 2, context);
14460 if (list_ret)
14461 have_address = 1;
14462 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14463 && (ret = cst_pool_loc_descr (loc)))
14464 have_address = 1;
14466 /* Otherwise, process the argument and look for the address. */
14467 if (!list_ret && !ret)
14468 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14469 else
14471 if (want_address)
14472 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14473 return NULL;
14475 break;
14477 case VAR_DECL:
14478 if (DECL_THREAD_LOCAL_P (loc))
14480 rtx rtl;
14481 enum dwarf_location_atom tls_op;
14482 enum dtprel_bool dtprel = dtprel_false;
14484 if (targetm.have_tls)
14486 /* If this is not defined, we have no way to emit the
14487 data. */
14488 if (!targetm.asm_out.output_dwarf_dtprel)
14489 return 0;
14491 /* The way DW_OP_GNU_push_tls_address is specified, we
14492 can only look up addresses of objects in the current
14493 module. We used DW_OP_addr as first op, but that's
14494 wrong, because DW_OP_addr is relocated by the debug
14495 info consumer, while DW_OP_GNU_push_tls_address
14496 operand shouldn't be. */
14497 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14498 return 0;
14499 dtprel = dtprel_true;
14500 tls_op = DW_OP_GNU_push_tls_address;
14502 else
14504 if (!targetm.emutls.debug_form_tls_address
14505 || !(dwarf_version >= 3 || !dwarf_strict))
14506 return 0;
14507 /* We stuffed the control variable into the DECL_VALUE_EXPR
14508 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14509 no longer appear in gimple code. We used the control
14510 variable in specific so that we could pick it up here. */
14511 loc = DECL_VALUE_EXPR (loc);
14512 tls_op = DW_OP_form_tls_address;
14515 rtl = rtl_for_decl_location (loc);
14516 if (rtl == NULL_RTX)
14517 return 0;
14519 if (!MEM_P (rtl))
14520 return 0;
14521 rtl = XEXP (rtl, 0);
14522 if (! CONSTANT_P (rtl))
14523 return 0;
14525 ret = new_addr_loc_descr (rtl, dtprel);
14526 ret1 = new_loc_descr (tls_op, 0, 0);
14527 add_loc_descr (&ret, ret1);
14529 have_address = 1;
14530 break;
14532 /* FALLTHRU */
14534 case PARM_DECL:
14535 case RESULT_DECL:
14536 if (DECL_HAS_VALUE_EXPR_P (loc))
14537 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14538 want_address, context);
14539 /* FALLTHRU */
14541 case FUNCTION_DECL:
14543 rtx rtl;
14544 var_loc_list *loc_list = lookup_decl_loc (loc);
14546 if (loc_list && loc_list->first)
14548 list_ret = dw_loc_list (loc_list, loc, want_address);
14549 have_address = want_address != 0;
14550 break;
14552 rtl = rtl_for_decl_location (loc);
14553 if (rtl == NULL_RTX)
14555 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14556 return 0;
14558 else if (CONST_INT_P (rtl))
14560 HOST_WIDE_INT val = INTVAL (rtl);
14561 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14562 val &= GET_MODE_MASK (DECL_MODE (loc));
14563 ret = int_loc_descriptor (val);
14565 else if (GET_CODE (rtl) == CONST_STRING)
14567 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14568 return 0;
14570 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14571 ret = new_addr_loc_descr (rtl, dtprel_false);
14572 else
14574 machine_mode mode, mem_mode;
14576 /* Certain constructs can only be represented at top-level. */
14577 if (want_address == 2)
14579 ret = loc_descriptor (rtl, VOIDmode,
14580 VAR_INIT_STATUS_INITIALIZED);
14581 have_address = 1;
14583 else
14585 mode = GET_MODE (rtl);
14586 mem_mode = VOIDmode;
14587 if (MEM_P (rtl))
14589 mem_mode = mode;
14590 mode = get_address_mode (rtl);
14591 rtl = XEXP (rtl, 0);
14592 have_address = 1;
14594 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14595 VAR_INIT_STATUS_INITIALIZED);
14597 if (!ret)
14598 expansion_failed (loc, rtl,
14599 "failed to produce loc descriptor for rtl");
14602 break;
14604 case MEM_REF:
14605 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14607 have_address = 1;
14608 goto do_plus;
14610 /* Fallthru. */
14611 case INDIRECT_REF:
14612 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14613 have_address = 1;
14614 break;
14616 case TARGET_MEM_REF:
14617 case SSA_NAME:
14618 return NULL;
14620 case COMPOUND_EXPR:
14621 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14623 CASE_CONVERT:
14624 case VIEW_CONVERT_EXPR:
14625 case SAVE_EXPR:
14626 case MODIFY_EXPR:
14627 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14629 case COMPONENT_REF:
14630 case BIT_FIELD_REF:
14631 case ARRAY_REF:
14632 case ARRAY_RANGE_REF:
14633 case REALPART_EXPR:
14634 case IMAGPART_EXPR:
14636 tree obj, offset;
14637 HOST_WIDE_INT bitsize, bitpos, bytepos;
14638 machine_mode mode;
14639 int unsignedp, volatilep = 0;
14641 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14642 &unsignedp, &volatilep, false);
14644 gcc_assert (obj != loc);
14646 list_ret = loc_list_from_tree (obj,
14647 want_address == 2
14648 && !bitpos && !offset ? 2 : 1,
14649 context);
14650 /* TODO: We can extract value of the small expression via shifting even
14651 for nonzero bitpos. */
14652 if (list_ret == 0)
14653 return 0;
14654 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14656 expansion_failed (loc, NULL_RTX,
14657 "bitfield access");
14658 return 0;
14661 if (offset != NULL_TREE)
14663 /* Variable offset. */
14664 list_ret1 = loc_list_from_tree (offset, 0, context);
14665 if (list_ret1 == 0)
14666 return 0;
14667 add_loc_list (&list_ret, list_ret1);
14668 if (!list_ret)
14669 return 0;
14670 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14673 bytepos = bitpos / BITS_PER_UNIT;
14674 if (bytepos > 0)
14675 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14676 else if (bytepos < 0)
14677 loc_list_plus_const (list_ret, bytepos);
14679 have_address = 1;
14680 break;
14683 case INTEGER_CST:
14684 if ((want_address || !tree_fits_shwi_p (loc))
14685 && (ret = cst_pool_loc_descr (loc)))
14686 have_address = 1;
14687 else if (want_address == 2
14688 && tree_fits_shwi_p (loc)
14689 && (ret = address_of_int_loc_descriptor
14690 (int_size_in_bytes (TREE_TYPE (loc)),
14691 tree_to_shwi (loc))))
14692 have_address = 1;
14693 else if (tree_fits_shwi_p (loc))
14694 ret = int_loc_descriptor (tree_to_shwi (loc));
14695 else
14697 expansion_failed (loc, NULL_RTX,
14698 "Integer operand is not host integer");
14699 return 0;
14701 break;
14703 case CONSTRUCTOR:
14704 case REAL_CST:
14705 case STRING_CST:
14706 case COMPLEX_CST:
14707 if ((ret = cst_pool_loc_descr (loc)))
14708 have_address = 1;
14709 else
14710 /* We can construct small constants here using int_loc_descriptor. */
14711 expansion_failed (loc, NULL_RTX,
14712 "constructor or constant not in constant pool");
14713 break;
14715 case TRUTH_AND_EXPR:
14716 case TRUTH_ANDIF_EXPR:
14717 case BIT_AND_EXPR:
14718 op = DW_OP_and;
14719 goto do_binop;
14721 case TRUTH_XOR_EXPR:
14722 case BIT_XOR_EXPR:
14723 op = DW_OP_xor;
14724 goto do_binop;
14726 case TRUTH_OR_EXPR:
14727 case TRUTH_ORIF_EXPR:
14728 case BIT_IOR_EXPR:
14729 op = DW_OP_or;
14730 goto do_binop;
14732 case FLOOR_DIV_EXPR:
14733 case CEIL_DIV_EXPR:
14734 case ROUND_DIV_EXPR:
14735 case TRUNC_DIV_EXPR:
14736 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14737 return 0;
14738 op = DW_OP_div;
14739 goto do_binop;
14741 case MINUS_EXPR:
14742 op = DW_OP_minus;
14743 goto do_binop;
14745 case FLOOR_MOD_EXPR:
14746 case CEIL_MOD_EXPR:
14747 case ROUND_MOD_EXPR:
14748 case TRUNC_MOD_EXPR:
14749 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14751 op = DW_OP_mod;
14752 goto do_binop;
14754 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14755 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14756 if (list_ret == 0 || list_ret1 == 0)
14757 return 0;
14759 add_loc_list (&list_ret, list_ret1);
14760 if (list_ret == 0)
14761 return 0;
14762 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14763 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14764 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14765 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14766 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14767 break;
14769 case MULT_EXPR:
14770 op = DW_OP_mul;
14771 goto do_binop;
14773 case LSHIFT_EXPR:
14774 op = DW_OP_shl;
14775 goto do_binop;
14777 case RSHIFT_EXPR:
14778 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14779 goto do_binop;
14781 case POINTER_PLUS_EXPR:
14782 case PLUS_EXPR:
14783 do_plus:
14784 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14786 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14787 if (list_ret == 0)
14788 return 0;
14790 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14791 break;
14794 op = DW_OP_plus;
14795 goto do_binop;
14797 case LE_EXPR:
14798 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14799 return 0;
14801 op = DW_OP_le;
14802 goto do_binop;
14804 case GE_EXPR:
14805 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14806 return 0;
14808 op = DW_OP_ge;
14809 goto do_binop;
14811 case LT_EXPR:
14812 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14813 return 0;
14815 op = DW_OP_lt;
14816 goto do_binop;
14818 case GT_EXPR:
14819 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14820 return 0;
14822 op = DW_OP_gt;
14823 goto do_binop;
14825 case EQ_EXPR:
14826 op = DW_OP_eq;
14827 goto do_binop;
14829 case NE_EXPR:
14830 op = DW_OP_ne;
14831 goto do_binop;
14833 do_binop:
14834 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14835 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14836 if (list_ret == 0 || list_ret1 == 0)
14837 return 0;
14839 add_loc_list (&list_ret, list_ret1);
14840 if (list_ret == 0)
14841 return 0;
14842 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14843 break;
14845 case TRUTH_NOT_EXPR:
14846 case BIT_NOT_EXPR:
14847 op = DW_OP_not;
14848 goto do_unop;
14850 case ABS_EXPR:
14851 op = DW_OP_abs;
14852 goto do_unop;
14854 case NEGATE_EXPR:
14855 op = DW_OP_neg;
14856 goto do_unop;
14858 do_unop:
14859 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14860 if (list_ret == 0)
14861 return 0;
14863 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14864 break;
14866 case MIN_EXPR:
14867 case MAX_EXPR:
14869 const enum tree_code code =
14870 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14872 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14873 build2 (code, integer_type_node,
14874 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14875 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14878 /* ... fall through ... */
14880 case COND_EXPR:
14882 dw_loc_descr_ref lhs
14883 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
14884 dw_loc_list_ref rhs
14885 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
14886 dw_loc_descr_ref bra_node, jump_node, tmp;
14888 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14889 if (list_ret == 0 || lhs == 0 || rhs == 0)
14890 return 0;
14892 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14893 add_loc_descr_to_each (list_ret, bra_node);
14895 add_loc_list (&list_ret, rhs);
14896 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14897 add_loc_descr_to_each (list_ret, jump_node);
14899 add_loc_descr_to_each (list_ret, lhs);
14900 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14901 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14903 /* ??? Need a node to point the skip at. Use a nop. */
14904 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14905 add_loc_descr_to_each (list_ret, tmp);
14906 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14907 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14909 break;
14911 case FIX_TRUNC_EXPR:
14912 return 0;
14914 default:
14915 /* Leave front-end specific codes as simply unknown. This comes
14916 up, for instance, with the C STMT_EXPR. */
14917 if ((unsigned int) TREE_CODE (loc)
14918 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14920 expansion_failed (loc, NULL_RTX,
14921 "language specific tree node");
14922 return 0;
14925 #ifdef ENABLE_CHECKING
14926 /* Otherwise this is a generic code; we should just lists all of
14927 these explicitly. We forgot one. */
14928 gcc_unreachable ();
14929 #else
14930 /* In a release build, we want to degrade gracefully: better to
14931 generate incomplete debugging information than to crash. */
14932 return NULL;
14933 #endif
14936 if (!ret && !list_ret)
14937 return 0;
14939 if (want_address == 2 && !have_address
14940 && (dwarf_version >= 4 || !dwarf_strict))
14942 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14944 expansion_failed (loc, NULL_RTX,
14945 "DWARF address size mismatch");
14946 return 0;
14948 if (ret)
14949 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14950 else
14951 add_loc_descr_to_each (list_ret,
14952 new_loc_descr (DW_OP_stack_value, 0, 0));
14953 have_address = 1;
14955 /* Show if we can't fill the request for an address. */
14956 if (want_address && !have_address)
14958 expansion_failed (loc, NULL_RTX,
14959 "Want address and only have value");
14960 return 0;
14963 gcc_assert (!ret || !list_ret);
14965 /* If we've got an address and don't want one, dereference. */
14966 if (!want_address && have_address)
14968 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14970 if (size > DWARF2_ADDR_SIZE || size == -1)
14972 expansion_failed (loc, NULL_RTX,
14973 "DWARF address size mismatch");
14974 return 0;
14976 else if (size == DWARF2_ADDR_SIZE)
14977 op = DW_OP_deref;
14978 else
14979 op = DW_OP_deref_size;
14981 if (ret)
14982 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14983 else
14984 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14986 if (ret)
14987 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14989 return list_ret;
14992 /* Same as above but return only single location expression. */
14993 static dw_loc_descr_ref
14994 loc_descriptor_from_tree (tree loc, int want_address,
14995 const struct loc_descr_context *context)
14997 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
14998 if (!ret)
14999 return NULL;
15000 if (ret->dw_loc_next)
15002 expansion_failed (loc, NULL_RTX,
15003 "Location list where only loc descriptor needed");
15004 return NULL;
15006 return ret->expr;
15009 /* Given a value, round it up to the lowest multiple of `boundary'
15010 which is not less than the value itself. */
15012 static inline HOST_WIDE_INT
15013 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15015 return (((value + boundary - 1) / boundary) * boundary);
15018 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15019 pointer to the declared type for the relevant field variable, or return
15020 `integer_type_node' if the given node turns out to be an
15021 ERROR_MARK node. */
15023 static inline tree
15024 field_type (const_tree decl)
15026 tree type;
15028 if (TREE_CODE (decl) == ERROR_MARK)
15029 return integer_type_node;
15031 type = DECL_BIT_FIELD_TYPE (decl);
15032 if (type == NULL_TREE)
15033 type = TREE_TYPE (decl);
15035 return type;
15038 /* Given a pointer to a tree node, return the alignment in bits for
15039 it, or else return BITS_PER_WORD if the node actually turns out to
15040 be an ERROR_MARK node. */
15042 static inline unsigned
15043 simple_type_align_in_bits (const_tree type)
15045 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15048 static inline unsigned
15049 simple_decl_align_in_bits (const_tree decl)
15051 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15054 /* Return the result of rounding T up to ALIGN. */
15056 static inline offset_int
15057 round_up_to_align (const offset_int &t, unsigned int align)
15059 return wi::udiv_trunc (t + align - 1, align) * align;
15062 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15063 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15064 or return 0 if we are unable to determine what that offset is, either
15065 because the argument turns out to be a pointer to an ERROR_MARK node, or
15066 because the offset is actually variable. (We can't handle the latter case
15067 just yet). */
15069 static HOST_WIDE_INT
15070 field_byte_offset (const_tree decl)
15072 offset_int object_offset_in_bits;
15073 offset_int object_offset_in_bytes;
15074 offset_int bitpos_int;
15076 if (TREE_CODE (decl) == ERROR_MARK)
15077 return 0;
15079 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15081 /* We cannot yet cope with fields whose positions are variable, so
15082 for now, when we see such things, we simply return 0. Someday, we may
15083 be able to handle such cases, but it will be damn difficult. */
15084 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15085 return 0;
15087 bitpos_int = wi::to_offset (bit_position (decl));
15089 #ifdef PCC_BITFIELD_TYPE_MATTERS
15090 if (PCC_BITFIELD_TYPE_MATTERS)
15092 tree type;
15093 tree field_size_tree;
15094 offset_int deepest_bitpos;
15095 offset_int field_size_in_bits;
15096 unsigned int type_align_in_bits;
15097 unsigned int decl_align_in_bits;
15098 offset_int type_size_in_bits;
15100 type = field_type (decl);
15101 type_size_in_bits = offset_int_type_size_in_bits (type);
15102 type_align_in_bits = simple_type_align_in_bits (type);
15104 field_size_tree = DECL_SIZE (decl);
15106 /* The size could be unspecified if there was an error, or for
15107 a flexible array member. */
15108 if (!field_size_tree)
15109 field_size_tree = bitsize_zero_node;
15111 /* If the size of the field is not constant, use the type size. */
15112 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15113 field_size_in_bits = wi::to_offset (field_size_tree);
15114 else
15115 field_size_in_bits = type_size_in_bits;
15117 decl_align_in_bits = simple_decl_align_in_bits (decl);
15119 /* The GCC front-end doesn't make any attempt to keep track of the
15120 starting bit offset (relative to the start of the containing
15121 structure type) of the hypothetical "containing object" for a
15122 bit-field. Thus, when computing the byte offset value for the
15123 start of the "containing object" of a bit-field, we must deduce
15124 this information on our own. This can be rather tricky to do in
15125 some cases. For example, handling the following structure type
15126 definition when compiling for an i386/i486 target (which only
15127 aligns long long's to 32-bit boundaries) can be very tricky:
15129 struct S { int field1; long long field2:31; };
15131 Fortunately, there is a simple rule-of-thumb which can be used
15132 in such cases. When compiling for an i386/i486, GCC will
15133 allocate 8 bytes for the structure shown above. It decides to
15134 do this based upon one simple rule for bit-field allocation.
15135 GCC allocates each "containing object" for each bit-field at
15136 the first (i.e. lowest addressed) legitimate alignment boundary
15137 (based upon the required minimum alignment for the declared
15138 type of the field) which it can possibly use, subject to the
15139 condition that there is still enough available space remaining
15140 in the containing object (when allocated at the selected point)
15141 to fully accommodate all of the bits of the bit-field itself.
15143 This simple rule makes it obvious why GCC allocates 8 bytes for
15144 each object of the structure type shown above. When looking
15145 for a place to allocate the "containing object" for `field2',
15146 the compiler simply tries to allocate a 64-bit "containing
15147 object" at each successive 32-bit boundary (starting at zero)
15148 until it finds a place to allocate that 64- bit field such that
15149 at least 31 contiguous (and previously unallocated) bits remain
15150 within that selected 64 bit field. (As it turns out, for the
15151 example above, the compiler finds it is OK to allocate the
15152 "containing object" 64-bit field at bit-offset zero within the
15153 structure type.)
15155 Here we attempt to work backwards from the limited set of facts
15156 we're given, and we try to deduce from those facts, where GCC
15157 must have believed that the containing object started (within
15158 the structure type). The value we deduce is then used (by the
15159 callers of this routine) to generate DW_AT_location and
15160 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15161 the case of DW_AT_location, regular fields as well). */
15163 /* Figure out the bit-distance from the start of the structure to
15164 the "deepest" bit of the bit-field. */
15165 deepest_bitpos = bitpos_int + field_size_in_bits;
15167 /* This is the tricky part. Use some fancy footwork to deduce
15168 where the lowest addressed bit of the containing object must
15169 be. */
15170 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15172 /* Round up to type_align by default. This works best for
15173 bitfields. */
15174 object_offset_in_bits
15175 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15177 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15179 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15181 /* Round up to decl_align instead. */
15182 object_offset_in_bits
15183 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15186 else
15187 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15188 object_offset_in_bits = bitpos_int;
15190 object_offset_in_bytes
15191 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15192 return object_offset_in_bytes.to_shwi ();
15195 /* The following routines define various Dwarf attributes and any data
15196 associated with them. */
15198 /* Add a location description attribute value to a DIE.
15200 This emits location attributes suitable for whole variables and
15201 whole parameters. Note that the location attributes for struct fields are
15202 generated by the routine `data_member_location_attribute' below. */
15204 static inline void
15205 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15206 dw_loc_list_ref descr)
15208 if (descr == 0)
15209 return;
15210 if (single_element_loc_list_p (descr))
15211 add_AT_loc (die, attr_kind, descr->expr);
15212 else
15213 add_AT_loc_list (die, attr_kind, descr);
15216 /* Add DW_AT_accessibility attribute to DIE if needed. */
15218 static void
15219 add_accessibility_attribute (dw_die_ref die, tree decl)
15221 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15222 children, otherwise the default is DW_ACCESS_public. In DWARF2
15223 the default has always been DW_ACCESS_public. */
15224 if (TREE_PROTECTED (decl))
15225 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15226 else if (TREE_PRIVATE (decl))
15228 if (dwarf_version == 2
15229 || die->die_parent == NULL
15230 || die->die_parent->die_tag != DW_TAG_class_type)
15231 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15233 else if (dwarf_version > 2
15234 && die->die_parent
15235 && die->die_parent->die_tag == DW_TAG_class_type)
15236 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15239 /* Attach the specialized form of location attribute used for data members of
15240 struct and union types. In the special case of a FIELD_DECL node which
15241 represents a bit-field, the "offset" part of this special location
15242 descriptor must indicate the distance in bytes from the lowest-addressed
15243 byte of the containing struct or union type to the lowest-addressed byte of
15244 the "containing object" for the bit-field. (See the `field_byte_offset'
15245 function above).
15247 For any given bit-field, the "containing object" is a hypothetical object
15248 (of some integral or enum type) within which the given bit-field lives. The
15249 type of this hypothetical "containing object" is always the same as the
15250 declared type of the individual bit-field itself (for GCC anyway... the
15251 DWARF spec doesn't actually mandate this). Note that it is the size (in
15252 bytes) of the hypothetical "containing object" which will be given in the
15253 DW_AT_byte_size attribute for this bit-field. (See the
15254 `byte_size_attribute' function below.) It is also used when calculating the
15255 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15256 function below.) */
15258 static void
15259 add_data_member_location_attribute (dw_die_ref die, tree decl)
15261 HOST_WIDE_INT offset;
15262 dw_loc_descr_ref loc_descr = 0;
15264 if (TREE_CODE (decl) == TREE_BINFO)
15266 /* We're working on the TAG_inheritance for a base class. */
15267 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15269 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15270 aren't at a fixed offset from all (sub)objects of the same
15271 type. We need to extract the appropriate offset from our
15272 vtable. The following dwarf expression means
15274 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15276 This is specific to the V3 ABI, of course. */
15278 dw_loc_descr_ref tmp;
15280 /* Make a copy of the object address. */
15281 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15282 add_loc_descr (&loc_descr, tmp);
15284 /* Extract the vtable address. */
15285 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15286 add_loc_descr (&loc_descr, tmp);
15288 /* Calculate the address of the offset. */
15289 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15290 gcc_assert (offset < 0);
15292 tmp = int_loc_descriptor (-offset);
15293 add_loc_descr (&loc_descr, tmp);
15294 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15295 add_loc_descr (&loc_descr, tmp);
15297 /* Extract the offset. */
15298 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15299 add_loc_descr (&loc_descr, tmp);
15301 /* Add it to the object address. */
15302 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15303 add_loc_descr (&loc_descr, tmp);
15305 else
15306 offset = tree_to_shwi (BINFO_OFFSET (decl));
15308 else
15309 offset = field_byte_offset (decl);
15311 if (! loc_descr)
15313 if (dwarf_version > 2)
15315 /* Don't need to output a location expression, just the constant. */
15316 if (offset < 0)
15317 add_AT_int (die, DW_AT_data_member_location, offset);
15318 else
15319 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15320 return;
15322 else
15324 enum dwarf_location_atom op;
15326 /* The DWARF2 standard says that we should assume that the structure
15327 address is already on the stack, so we can specify a structure
15328 field address by using DW_OP_plus_uconst. */
15329 op = DW_OP_plus_uconst;
15330 loc_descr = new_loc_descr (op, offset, 0);
15334 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15337 /* Writes integer values to dw_vec_const array. */
15339 static void
15340 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15342 while (size != 0)
15344 *dest++ = val & 0xff;
15345 val >>= 8;
15346 --size;
15350 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15352 static HOST_WIDE_INT
15353 extract_int (const unsigned char *src, unsigned int size)
15355 HOST_WIDE_INT val = 0;
15357 src += size;
15358 while (size != 0)
15360 val <<= 8;
15361 val |= *--src & 0xff;
15362 --size;
15364 return val;
15367 /* Writes wide_int values to dw_vec_const array. */
15369 static void
15370 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15372 int i;
15374 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15376 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15377 return;
15380 /* We'd have to extend this code to support odd sizes. */
15381 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15383 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15385 if (WORDS_BIG_ENDIAN)
15386 for (i = n - 1; i >= 0; i--)
15388 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15389 dest += sizeof (HOST_WIDE_INT);
15391 else
15392 for (i = 0; i < n; i++)
15394 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15395 dest += sizeof (HOST_WIDE_INT);
15399 /* Writes floating point values to dw_vec_const array. */
15401 static void
15402 insert_float (const_rtx rtl, unsigned char *array)
15404 REAL_VALUE_TYPE rv;
15405 long val[4];
15406 int i;
15408 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15409 real_to_target (val, &rv, GET_MODE (rtl));
15411 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15412 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15414 insert_int (val[i], 4, array);
15415 array += 4;
15419 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15420 does not have a "location" either in memory or in a register. These
15421 things can arise in GNU C when a constant is passed as an actual parameter
15422 to an inlined function. They can also arise in C++ where declared
15423 constants do not necessarily get memory "homes". */
15425 static bool
15426 add_const_value_attribute (dw_die_ref die, rtx rtl)
15428 switch (GET_CODE (rtl))
15430 case CONST_INT:
15432 HOST_WIDE_INT val = INTVAL (rtl);
15434 if (val < 0)
15435 add_AT_int (die, DW_AT_const_value, val);
15436 else
15437 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15439 return true;
15441 case CONST_WIDE_INT:
15442 add_AT_wide (die, DW_AT_const_value,
15443 std::make_pair (rtl, GET_MODE (rtl)));
15444 return true;
15446 case CONST_DOUBLE:
15447 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15448 floating-point constant. A CONST_DOUBLE is used whenever the
15449 constant requires more than one word in order to be adequately
15450 represented. */
15452 machine_mode mode = GET_MODE (rtl);
15454 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15455 add_AT_double (die, DW_AT_const_value,
15456 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15457 else
15459 unsigned int length = GET_MODE_SIZE (mode);
15460 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15462 insert_float (rtl, array);
15463 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15466 return true;
15468 case CONST_VECTOR:
15470 machine_mode mode = GET_MODE (rtl);
15471 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15472 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15473 unsigned char *array
15474 = ggc_vec_alloc<unsigned char> (length * elt_size);
15475 unsigned int i;
15476 unsigned char *p;
15477 machine_mode imode = GET_MODE_INNER (mode);
15479 switch (GET_MODE_CLASS (mode))
15481 case MODE_VECTOR_INT:
15482 for (i = 0, p = array; i < length; i++, p += elt_size)
15484 rtx elt = CONST_VECTOR_ELT (rtl, i);
15485 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15487 break;
15489 case MODE_VECTOR_FLOAT:
15490 for (i = 0, p = array; i < length; i++, p += elt_size)
15492 rtx elt = CONST_VECTOR_ELT (rtl, i);
15493 insert_float (elt, p);
15495 break;
15497 default:
15498 gcc_unreachable ();
15501 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15503 return true;
15505 case CONST_STRING:
15506 if (dwarf_version >= 4 || !dwarf_strict)
15508 dw_loc_descr_ref loc_result;
15509 resolve_one_addr (&rtl);
15510 rtl_addr:
15511 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15512 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15513 add_AT_loc (die, DW_AT_location, loc_result);
15514 vec_safe_push (used_rtx_array, rtl);
15515 return true;
15517 return false;
15519 case CONST:
15520 if (CONSTANT_P (XEXP (rtl, 0)))
15521 return add_const_value_attribute (die, XEXP (rtl, 0));
15522 /* FALLTHROUGH */
15523 case SYMBOL_REF:
15524 if (!const_ok_for_output (rtl))
15525 return false;
15526 case LABEL_REF:
15527 if (dwarf_version >= 4 || !dwarf_strict)
15528 goto rtl_addr;
15529 return false;
15531 case PLUS:
15532 /* In cases where an inlined instance of an inline function is passed
15533 the address of an `auto' variable (which is local to the caller) we
15534 can get a situation where the DECL_RTL of the artificial local
15535 variable (for the inlining) which acts as a stand-in for the
15536 corresponding formal parameter (of the inline function) will look
15537 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15538 exactly a compile-time constant expression, but it isn't the address
15539 of the (artificial) local variable either. Rather, it represents the
15540 *value* which the artificial local variable always has during its
15541 lifetime. We currently have no way to represent such quasi-constant
15542 values in Dwarf, so for now we just punt and generate nothing. */
15543 return false;
15545 case HIGH:
15546 case CONST_FIXED:
15547 return false;
15549 case MEM:
15550 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15551 && MEM_READONLY_P (rtl)
15552 && GET_MODE (rtl) == BLKmode)
15554 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15555 return true;
15557 return false;
15559 default:
15560 /* No other kinds of rtx should be possible here. */
15561 gcc_unreachable ();
15563 return false;
15566 /* Determine whether the evaluation of EXPR references any variables
15567 or functions which aren't otherwise used (and therefore may not be
15568 output). */
15569 static tree
15570 reference_to_unused (tree * tp, int * walk_subtrees,
15571 void * data ATTRIBUTE_UNUSED)
15573 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15574 *walk_subtrees = 0;
15576 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15577 && ! TREE_ASM_WRITTEN (*tp))
15578 return *tp;
15579 /* ??? The C++ FE emits debug information for using decls, so
15580 putting gcc_unreachable here falls over. See PR31899. For now
15581 be conservative. */
15582 else if (!symtab->global_info_ready
15583 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15584 return *tp;
15585 else if (TREE_CODE (*tp) == VAR_DECL)
15587 varpool_node *node = varpool_node::get (*tp);
15588 if (!node || !node->definition)
15589 return *tp;
15591 else if (TREE_CODE (*tp) == FUNCTION_DECL
15592 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15594 /* The call graph machinery must have finished analyzing,
15595 optimizing and gimplifying the CU by now.
15596 So if *TP has no call graph node associated
15597 to it, it means *TP will not be emitted. */
15598 if (!cgraph_node::get (*tp))
15599 return *tp;
15601 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15602 return *tp;
15604 return NULL_TREE;
15607 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15608 for use in a later add_const_value_attribute call. */
15610 static rtx
15611 rtl_for_decl_init (tree init, tree type)
15613 rtx rtl = NULL_RTX;
15615 STRIP_NOPS (init);
15617 /* If a variable is initialized with a string constant without embedded
15618 zeros, build CONST_STRING. */
15619 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15621 tree enttype = TREE_TYPE (type);
15622 tree domain = TYPE_DOMAIN (type);
15623 machine_mode mode = TYPE_MODE (enttype);
15625 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15626 && domain
15627 && integer_zerop (TYPE_MIN_VALUE (domain))
15628 && compare_tree_int (TYPE_MAX_VALUE (domain),
15629 TREE_STRING_LENGTH (init) - 1) == 0
15630 && ((size_t) TREE_STRING_LENGTH (init)
15631 == strlen (TREE_STRING_POINTER (init)) + 1))
15633 rtl = gen_rtx_CONST_STRING (VOIDmode,
15634 ggc_strdup (TREE_STRING_POINTER (init)));
15635 rtl = gen_rtx_MEM (BLKmode, rtl);
15636 MEM_READONLY_P (rtl) = 1;
15639 /* Other aggregates, and complex values, could be represented using
15640 CONCAT: FIXME! */
15641 else if (AGGREGATE_TYPE_P (type)
15642 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15643 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15644 || TREE_CODE (type) == COMPLEX_TYPE)
15646 /* Vectors only work if their mode is supported by the target.
15647 FIXME: generic vectors ought to work too. */
15648 else if (TREE_CODE (type) == VECTOR_TYPE
15649 && !VECTOR_MODE_P (TYPE_MODE (type)))
15651 /* If the initializer is something that we know will expand into an
15652 immediate RTL constant, expand it now. We must be careful not to
15653 reference variables which won't be output. */
15654 else if (initializer_constant_valid_p (init, type)
15655 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15657 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15658 possible. */
15659 if (TREE_CODE (type) == VECTOR_TYPE)
15660 switch (TREE_CODE (init))
15662 case VECTOR_CST:
15663 break;
15664 case CONSTRUCTOR:
15665 if (TREE_CONSTANT (init))
15667 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15668 bool constant_p = true;
15669 tree value;
15670 unsigned HOST_WIDE_INT ix;
15672 /* Even when ctor is constant, it might contain non-*_CST
15673 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15674 belong into VECTOR_CST nodes. */
15675 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15676 if (!CONSTANT_CLASS_P (value))
15678 constant_p = false;
15679 break;
15682 if (constant_p)
15684 init = build_vector_from_ctor (type, elts);
15685 break;
15688 /* FALLTHRU */
15690 default:
15691 return NULL;
15694 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15696 /* If expand_expr returns a MEM, it wasn't immediate. */
15697 gcc_assert (!rtl || !MEM_P (rtl));
15700 return rtl;
15703 /* Generate RTL for the variable DECL to represent its location. */
15705 static rtx
15706 rtl_for_decl_location (tree decl)
15708 rtx rtl;
15710 /* Here we have to decide where we are going to say the parameter "lives"
15711 (as far as the debugger is concerned). We only have a couple of
15712 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15714 DECL_RTL normally indicates where the parameter lives during most of the
15715 activation of the function. If optimization is enabled however, this
15716 could be either NULL or else a pseudo-reg. Both of those cases indicate
15717 that the parameter doesn't really live anywhere (as far as the code
15718 generation parts of GCC are concerned) during most of the function's
15719 activation. That will happen (for example) if the parameter is never
15720 referenced within the function.
15722 We could just generate a location descriptor here for all non-NULL
15723 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15724 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15725 where DECL_RTL is NULL or is a pseudo-reg.
15727 Note however that we can only get away with using DECL_INCOMING_RTL as
15728 a backup substitute for DECL_RTL in certain limited cases. In cases
15729 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15730 we can be sure that the parameter was passed using the same type as it is
15731 declared to have within the function, and that its DECL_INCOMING_RTL
15732 points us to a place where a value of that type is passed.
15734 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15735 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15736 because in these cases DECL_INCOMING_RTL points us to a value of some
15737 type which is *different* from the type of the parameter itself. Thus,
15738 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15739 such cases, the debugger would end up (for example) trying to fetch a
15740 `float' from a place which actually contains the first part of a
15741 `double'. That would lead to really incorrect and confusing
15742 output at debug-time.
15744 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15745 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15746 are a couple of exceptions however. On little-endian machines we can
15747 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15748 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15749 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15750 when (on a little-endian machine) a non-prototyped function has a
15751 parameter declared to be of type `short' or `char'. In such cases,
15752 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15753 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15754 passed `int' value. If the debugger then uses that address to fetch
15755 a `short' or a `char' (on a little-endian machine) the result will be
15756 the correct data, so we allow for such exceptional cases below.
15758 Note that our goal here is to describe the place where the given formal
15759 parameter lives during most of the function's activation (i.e. between the
15760 end of the prologue and the start of the epilogue). We'll do that as best
15761 as we can. Note however that if the given formal parameter is modified
15762 sometime during the execution of the function, then a stack backtrace (at
15763 debug-time) will show the function as having been called with the *new*
15764 value rather than the value which was originally passed in. This happens
15765 rarely enough that it is not a major problem, but it *is* a problem, and
15766 I'd like to fix it.
15768 A future version of dwarf2out.c may generate two additional attributes for
15769 any given DW_TAG_formal_parameter DIE which will describe the "passed
15770 type" and the "passed location" for the given formal parameter in addition
15771 to the attributes we now generate to indicate the "declared type" and the
15772 "active location" for each parameter. This additional set of attributes
15773 could be used by debuggers for stack backtraces. Separately, note that
15774 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15775 This happens (for example) for inlined-instances of inline function formal
15776 parameters which are never referenced. This really shouldn't be
15777 happening. All PARM_DECL nodes should get valid non-NULL
15778 DECL_INCOMING_RTL values. FIXME. */
15780 /* Use DECL_RTL as the "location" unless we find something better. */
15781 rtl = DECL_RTL_IF_SET (decl);
15783 /* When generating abstract instances, ignore everything except
15784 constants, symbols living in memory, and symbols living in
15785 fixed registers. */
15786 if (! reload_completed)
15788 if (rtl
15789 && (CONSTANT_P (rtl)
15790 || (MEM_P (rtl)
15791 && CONSTANT_P (XEXP (rtl, 0)))
15792 || (REG_P (rtl)
15793 && TREE_CODE (decl) == VAR_DECL
15794 && TREE_STATIC (decl))))
15796 rtl = targetm.delegitimize_address (rtl);
15797 return rtl;
15799 rtl = NULL_RTX;
15801 else if (TREE_CODE (decl) == PARM_DECL)
15803 if (rtl == NULL_RTX
15804 || is_pseudo_reg (rtl)
15805 || (MEM_P (rtl)
15806 && is_pseudo_reg (XEXP (rtl, 0))
15807 && DECL_INCOMING_RTL (decl)
15808 && MEM_P (DECL_INCOMING_RTL (decl))
15809 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15811 tree declared_type = TREE_TYPE (decl);
15812 tree passed_type = DECL_ARG_TYPE (decl);
15813 machine_mode dmode = TYPE_MODE (declared_type);
15814 machine_mode pmode = TYPE_MODE (passed_type);
15816 /* This decl represents a formal parameter which was optimized out.
15817 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15818 all cases where (rtl == NULL_RTX) just below. */
15819 if (dmode == pmode)
15820 rtl = DECL_INCOMING_RTL (decl);
15821 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15822 && SCALAR_INT_MODE_P (dmode)
15823 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15824 && DECL_INCOMING_RTL (decl))
15826 rtx inc = DECL_INCOMING_RTL (decl);
15827 if (REG_P (inc))
15828 rtl = inc;
15829 else if (MEM_P (inc))
15831 if (BYTES_BIG_ENDIAN)
15832 rtl = adjust_address_nv (inc, dmode,
15833 GET_MODE_SIZE (pmode)
15834 - GET_MODE_SIZE (dmode));
15835 else
15836 rtl = inc;
15841 /* If the parm was passed in registers, but lives on the stack, then
15842 make a big endian correction if the mode of the type of the
15843 parameter is not the same as the mode of the rtl. */
15844 /* ??? This is the same series of checks that are made in dbxout.c before
15845 we reach the big endian correction code there. It isn't clear if all
15846 of these checks are necessary here, but keeping them all is the safe
15847 thing to do. */
15848 else if (MEM_P (rtl)
15849 && XEXP (rtl, 0) != const0_rtx
15850 && ! CONSTANT_P (XEXP (rtl, 0))
15851 /* Not passed in memory. */
15852 && !MEM_P (DECL_INCOMING_RTL (decl))
15853 /* Not passed by invisible reference. */
15854 && (!REG_P (XEXP (rtl, 0))
15855 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15856 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15857 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15858 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15859 #endif
15861 /* Big endian correction check. */
15862 && BYTES_BIG_ENDIAN
15863 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15864 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15865 < UNITS_PER_WORD))
15867 machine_mode addr_mode = get_address_mode (rtl);
15868 int offset = (UNITS_PER_WORD
15869 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15871 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15872 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15875 else if (TREE_CODE (decl) == VAR_DECL
15876 && rtl
15877 && MEM_P (rtl)
15878 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15879 && BYTES_BIG_ENDIAN)
15881 machine_mode addr_mode = get_address_mode (rtl);
15882 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15883 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15885 /* If a variable is declared "register" yet is smaller than
15886 a register, then if we store the variable to memory, it
15887 looks like we're storing a register-sized value, when in
15888 fact we are not. We need to adjust the offset of the
15889 storage location to reflect the actual value's bytes,
15890 else gdb will not be able to display it. */
15891 if (rsize > dsize)
15892 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15893 plus_constant (addr_mode, XEXP (rtl, 0),
15894 rsize - dsize));
15897 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15898 and will have been substituted directly into all expressions that use it.
15899 C does not have such a concept, but C++ and other languages do. */
15900 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15901 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15903 if (rtl)
15904 rtl = targetm.delegitimize_address (rtl);
15906 /* If we don't look past the constant pool, we risk emitting a
15907 reference to a constant pool entry that isn't referenced from
15908 code, and thus is not emitted. */
15909 if (rtl)
15910 rtl = avoid_constant_pool_reference (rtl);
15912 /* Try harder to get a rtl. If this symbol ends up not being emitted
15913 in the current CU, resolve_addr will remove the expression referencing
15914 it. */
15915 if (rtl == NULL_RTX
15916 && TREE_CODE (decl) == VAR_DECL
15917 && !DECL_EXTERNAL (decl)
15918 && TREE_STATIC (decl)
15919 && DECL_NAME (decl)
15920 && !DECL_HARD_REGISTER (decl)
15921 && DECL_MODE (decl) != VOIDmode)
15923 rtl = make_decl_rtl_for_debug (decl);
15924 if (!MEM_P (rtl)
15925 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15926 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15927 rtl = NULL_RTX;
15930 return rtl;
15933 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15934 returned. If so, the decl for the COMMON block is returned, and the
15935 value is the offset into the common block for the symbol. */
15937 static tree
15938 fortran_common (tree decl, HOST_WIDE_INT *value)
15940 tree val_expr, cvar;
15941 machine_mode mode;
15942 HOST_WIDE_INT bitsize, bitpos;
15943 tree offset;
15944 int unsignedp, volatilep = 0;
15946 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15947 it does not have a value (the offset into the common area), or if it
15948 is thread local (as opposed to global) then it isn't common, and shouldn't
15949 be handled as such. */
15950 if (TREE_CODE (decl) != VAR_DECL
15951 || !TREE_STATIC (decl)
15952 || !DECL_HAS_VALUE_EXPR_P (decl)
15953 || !is_fortran ())
15954 return NULL_TREE;
15956 val_expr = DECL_VALUE_EXPR (decl);
15957 if (TREE_CODE (val_expr) != COMPONENT_REF)
15958 return NULL_TREE;
15960 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15961 &mode, &unsignedp, &volatilep, true);
15963 if (cvar == NULL_TREE
15964 || TREE_CODE (cvar) != VAR_DECL
15965 || DECL_ARTIFICIAL (cvar)
15966 || !TREE_PUBLIC (cvar))
15967 return NULL_TREE;
15969 *value = 0;
15970 if (offset != NULL)
15972 if (!tree_fits_shwi_p (offset))
15973 return NULL_TREE;
15974 *value = tree_to_shwi (offset);
15976 if (bitpos != 0)
15977 *value += bitpos / BITS_PER_UNIT;
15979 return cvar;
15982 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15983 data attribute for a variable or a parameter. We generate the
15984 DW_AT_const_value attribute only in those cases where the given variable
15985 or parameter does not have a true "location" either in memory or in a
15986 register. This can happen (for example) when a constant is passed as an
15987 actual argument in a call to an inline function. (It's possible that
15988 these things can crop up in other ways also.) Note that one type of
15989 constant value which can be passed into an inlined function is a constant
15990 pointer. This can happen for example if an actual argument in an inlined
15991 function call evaluates to a compile-time constant address.
15993 CACHE_P is true if it is worth caching the location list for DECL,
15994 so that future calls can reuse it rather than regenerate it from scratch.
15995 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15996 since we will need to refer to them each time the function is inlined. */
15998 static bool
15999 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16000 enum dwarf_attribute attr)
16002 rtx rtl;
16003 dw_loc_list_ref list;
16004 var_loc_list *loc_list;
16005 cached_dw_loc_list *cache;
16007 if (TREE_CODE (decl) == ERROR_MARK)
16008 return false;
16010 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16011 || TREE_CODE (decl) == RESULT_DECL);
16013 /* Try to get some constant RTL for this decl, and use that as the value of
16014 the location. */
16016 rtl = rtl_for_decl_location (decl);
16017 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16018 && add_const_value_attribute (die, rtl))
16019 return true;
16021 /* See if we have single element location list that is equivalent to
16022 a constant value. That way we are better to use add_const_value_attribute
16023 rather than expanding constant value equivalent. */
16024 loc_list = lookup_decl_loc (decl);
16025 if (loc_list
16026 && loc_list->first
16027 && loc_list->first->next == NULL
16028 && NOTE_P (loc_list->first->loc)
16029 && NOTE_VAR_LOCATION (loc_list->first->loc)
16030 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16032 struct var_loc_node *node;
16034 node = loc_list->first;
16035 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16036 if (GET_CODE (rtl) == EXPR_LIST)
16037 rtl = XEXP (rtl, 0);
16038 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16039 && add_const_value_attribute (die, rtl))
16040 return true;
16042 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16043 list several times. See if we've already cached the contents. */
16044 list = NULL;
16045 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16046 cache_p = false;
16047 if (cache_p)
16049 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16050 if (cache)
16051 list = cache->loc_list;
16053 if (list == NULL)
16055 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16056 NULL);
16057 /* It is usually worth caching this result if the decl is from
16058 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16059 if (cache_p && list && list->dw_loc_next)
16061 cached_dw_loc_list **slot
16062 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16063 DECL_UID (decl),
16064 INSERT);
16065 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16066 cache->decl_id = DECL_UID (decl);
16067 cache->loc_list = list;
16068 *slot = cache;
16071 if (list)
16073 add_AT_location_description (die, attr, list);
16074 return true;
16076 /* None of that worked, so it must not really have a location;
16077 try adding a constant value attribute from the DECL_INITIAL. */
16078 return tree_add_const_value_attribute_for_decl (die, decl);
16081 /* Add VARIABLE and DIE into deferred locations list. */
16083 static void
16084 defer_location (tree variable, dw_die_ref die)
16086 deferred_locations entry;
16087 entry.variable = variable;
16088 entry.die = die;
16089 vec_safe_push (deferred_locations_list, entry);
16092 /* Helper function for tree_add_const_value_attribute. Natively encode
16093 initializer INIT into an array. Return true if successful. */
16095 static bool
16096 native_encode_initializer (tree init, unsigned char *array, int size)
16098 tree type;
16100 if (init == NULL_TREE)
16101 return false;
16103 STRIP_NOPS (init);
16104 switch (TREE_CODE (init))
16106 case STRING_CST:
16107 type = TREE_TYPE (init);
16108 if (TREE_CODE (type) == ARRAY_TYPE)
16110 tree enttype = TREE_TYPE (type);
16111 machine_mode mode = TYPE_MODE (enttype);
16113 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16114 return false;
16115 if (int_size_in_bytes (type) != size)
16116 return false;
16117 if (size > TREE_STRING_LENGTH (init))
16119 memcpy (array, TREE_STRING_POINTER (init),
16120 TREE_STRING_LENGTH (init));
16121 memset (array + TREE_STRING_LENGTH (init),
16122 '\0', size - TREE_STRING_LENGTH (init));
16124 else
16125 memcpy (array, TREE_STRING_POINTER (init), size);
16126 return true;
16128 return false;
16129 case CONSTRUCTOR:
16130 type = TREE_TYPE (init);
16131 if (int_size_in_bytes (type) != size)
16132 return false;
16133 if (TREE_CODE (type) == ARRAY_TYPE)
16135 HOST_WIDE_INT min_index;
16136 unsigned HOST_WIDE_INT cnt;
16137 int curpos = 0, fieldsize;
16138 constructor_elt *ce;
16140 if (TYPE_DOMAIN (type) == NULL_TREE
16141 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16142 return false;
16144 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16145 if (fieldsize <= 0)
16146 return false;
16148 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16149 memset (array, '\0', size);
16150 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16152 tree val = ce->value;
16153 tree index = ce->index;
16154 int pos = curpos;
16155 if (index && TREE_CODE (index) == RANGE_EXPR)
16156 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16157 * fieldsize;
16158 else if (index)
16159 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16161 if (val)
16163 STRIP_NOPS (val);
16164 if (!native_encode_initializer (val, array + pos, fieldsize))
16165 return false;
16167 curpos = pos + fieldsize;
16168 if (index && TREE_CODE (index) == RANGE_EXPR)
16170 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16171 - tree_to_shwi (TREE_OPERAND (index, 0));
16172 while (count-- > 0)
16174 if (val)
16175 memcpy (array + curpos, array + pos, fieldsize);
16176 curpos += fieldsize;
16179 gcc_assert (curpos <= size);
16181 return true;
16183 else if (TREE_CODE (type) == RECORD_TYPE
16184 || TREE_CODE (type) == UNION_TYPE)
16186 tree field = NULL_TREE;
16187 unsigned HOST_WIDE_INT cnt;
16188 constructor_elt *ce;
16190 if (int_size_in_bytes (type) != size)
16191 return false;
16193 if (TREE_CODE (type) == RECORD_TYPE)
16194 field = TYPE_FIELDS (type);
16196 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16198 tree val = ce->value;
16199 int pos, fieldsize;
16201 if (ce->index != 0)
16202 field = ce->index;
16204 if (val)
16205 STRIP_NOPS (val);
16207 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16208 return false;
16210 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16211 && TYPE_DOMAIN (TREE_TYPE (field))
16212 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16213 return false;
16214 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16215 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16216 return false;
16217 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16218 pos = int_byte_position (field);
16219 gcc_assert (pos + fieldsize <= size);
16220 if (val
16221 && !native_encode_initializer (val, array + pos, fieldsize))
16222 return false;
16224 return true;
16226 return false;
16227 case VIEW_CONVERT_EXPR:
16228 case NON_LVALUE_EXPR:
16229 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16230 default:
16231 return native_encode_expr (init, array, size) == size;
16235 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16236 attribute is the const value T. */
16238 static bool
16239 tree_add_const_value_attribute (dw_die_ref die, tree t)
16241 tree init;
16242 tree type = TREE_TYPE (t);
16243 rtx rtl;
16245 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16246 return false;
16248 init = t;
16249 gcc_assert (!DECL_P (init));
16251 rtl = rtl_for_decl_init (init, type);
16252 if (rtl)
16253 return add_const_value_attribute (die, rtl);
16254 /* If the host and target are sane, try harder. */
16255 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16256 && initializer_constant_valid_p (init, type))
16258 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16259 if (size > 0 && (int) size == size)
16261 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16263 if (native_encode_initializer (init, array, size))
16265 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16266 return true;
16268 ggc_free (array);
16271 return false;
16274 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16275 attribute is the const value of T, where T is an integral constant
16276 variable with static storage duration
16277 (so it can't be a PARM_DECL or a RESULT_DECL). */
16279 static bool
16280 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16283 if (!decl
16284 || (TREE_CODE (decl) != VAR_DECL
16285 && TREE_CODE (decl) != CONST_DECL)
16286 || (TREE_CODE (decl) == VAR_DECL
16287 && !TREE_STATIC (decl)))
16288 return false;
16290 if (TREE_READONLY (decl)
16291 && ! TREE_THIS_VOLATILE (decl)
16292 && DECL_INITIAL (decl))
16293 /* OK */;
16294 else
16295 return false;
16297 /* Don't add DW_AT_const_value if abstract origin already has one. */
16298 if (get_AT (var_die, DW_AT_const_value))
16299 return false;
16301 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16304 /* Convert the CFI instructions for the current function into a
16305 location list. This is used for DW_AT_frame_base when we targeting
16306 a dwarf2 consumer that does not support the dwarf3
16307 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16308 expressions. */
16310 static dw_loc_list_ref
16311 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16313 int ix;
16314 dw_fde_ref fde;
16315 dw_loc_list_ref list, *list_tail;
16316 dw_cfi_ref cfi;
16317 dw_cfa_location last_cfa, next_cfa;
16318 const char *start_label, *last_label, *section;
16319 dw_cfa_location remember;
16321 fde = cfun->fde;
16322 gcc_assert (fde != NULL);
16324 section = secname_for_decl (current_function_decl);
16325 list_tail = &list;
16326 list = NULL;
16328 memset (&next_cfa, 0, sizeof (next_cfa));
16329 next_cfa.reg = INVALID_REGNUM;
16330 remember = next_cfa;
16332 start_label = fde->dw_fde_begin;
16334 /* ??? Bald assumption that the CIE opcode list does not contain
16335 advance opcodes. */
16336 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16337 lookup_cfa_1 (cfi, &next_cfa, &remember);
16339 last_cfa = next_cfa;
16340 last_label = start_label;
16342 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16344 /* If the first partition contained no CFI adjustments, the
16345 CIE opcodes apply to the whole first partition. */
16346 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16347 fde->dw_fde_begin, fde->dw_fde_end, section);
16348 list_tail =&(*list_tail)->dw_loc_next;
16349 start_label = last_label = fde->dw_fde_second_begin;
16352 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16354 switch (cfi->dw_cfi_opc)
16356 case DW_CFA_set_loc:
16357 case DW_CFA_advance_loc1:
16358 case DW_CFA_advance_loc2:
16359 case DW_CFA_advance_loc4:
16360 if (!cfa_equal_p (&last_cfa, &next_cfa))
16362 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16363 start_label, last_label, section);
16365 list_tail = &(*list_tail)->dw_loc_next;
16366 last_cfa = next_cfa;
16367 start_label = last_label;
16369 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16370 break;
16372 case DW_CFA_advance_loc:
16373 /* The encoding is complex enough that we should never emit this. */
16374 gcc_unreachable ();
16376 default:
16377 lookup_cfa_1 (cfi, &next_cfa, &remember);
16378 break;
16380 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16382 if (!cfa_equal_p (&last_cfa, &next_cfa))
16384 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16385 start_label, last_label, section);
16387 list_tail = &(*list_tail)->dw_loc_next;
16388 last_cfa = next_cfa;
16389 start_label = last_label;
16391 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16392 start_label, fde->dw_fde_end, section);
16393 list_tail = &(*list_tail)->dw_loc_next;
16394 start_label = last_label = fde->dw_fde_second_begin;
16398 if (!cfa_equal_p (&last_cfa, &next_cfa))
16400 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16401 start_label, last_label, section);
16402 list_tail = &(*list_tail)->dw_loc_next;
16403 start_label = last_label;
16406 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16407 start_label,
16408 fde->dw_fde_second_begin
16409 ? fde->dw_fde_second_end : fde->dw_fde_end,
16410 section);
16412 if (list && list->dw_loc_next)
16413 gen_llsym (list);
16415 return list;
16418 /* Compute a displacement from the "steady-state frame pointer" to the
16419 frame base (often the same as the CFA), and store it in
16420 frame_pointer_fb_offset. OFFSET is added to the displacement
16421 before the latter is negated. */
16423 static void
16424 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16426 rtx reg, elim;
16428 #ifdef FRAME_POINTER_CFA_OFFSET
16429 reg = frame_pointer_rtx;
16430 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16431 #else
16432 reg = arg_pointer_rtx;
16433 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16434 #endif
16436 elim = (ira_use_lra_p
16437 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16438 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16439 if (GET_CODE (elim) == PLUS)
16441 offset += INTVAL (XEXP (elim, 1));
16442 elim = XEXP (elim, 0);
16445 frame_pointer_fb_offset = -offset;
16447 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16448 in which to eliminate. This is because it's stack pointer isn't
16449 directly accessible as a register within the ISA. To work around
16450 this, assume that while we cannot provide a proper value for
16451 frame_pointer_fb_offset, we won't need one either. */
16452 frame_pointer_fb_offset_valid
16453 = ((SUPPORTS_STACK_ALIGNMENT
16454 && (elim == hard_frame_pointer_rtx
16455 || elim == stack_pointer_rtx))
16456 || elim == (frame_pointer_needed
16457 ? hard_frame_pointer_rtx
16458 : stack_pointer_rtx));
16461 /* Generate a DW_AT_name attribute given some string value to be included as
16462 the value of the attribute. */
16464 static void
16465 add_name_attribute (dw_die_ref die, const char *name_string)
16467 if (name_string != NULL && *name_string != 0)
16469 if (demangle_name_func)
16470 name_string = (*demangle_name_func) (name_string);
16472 add_AT_string (die, DW_AT_name, name_string);
16476 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16477 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16478 of TYPE accordingly.
16480 ??? This is a temporary measure until after we're able to generate
16481 regular DWARF for the complex Ada type system. */
16483 static void
16484 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16485 dw_die_ref context_die)
16487 tree dtype;
16488 dw_die_ref dtype_die;
16490 if (!lang_hooks.types.descriptive_type)
16491 return;
16493 dtype = lang_hooks.types.descriptive_type (type);
16494 if (!dtype)
16495 return;
16497 dtype_die = lookup_type_die (dtype);
16498 if (!dtype_die)
16500 gen_type_die (dtype, context_die);
16501 dtype_die = lookup_type_die (dtype);
16502 gcc_assert (dtype_die);
16505 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16508 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16510 static const char *
16511 comp_dir_string (void)
16513 const char *wd;
16514 char *wd1;
16515 static const char *cached_wd = NULL;
16517 if (cached_wd != NULL)
16518 return cached_wd;
16520 wd = get_src_pwd ();
16521 if (wd == NULL)
16522 return NULL;
16524 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16526 int wdlen;
16528 wdlen = strlen (wd);
16529 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16530 strcpy (wd1, wd);
16531 wd1 [wdlen] = DIR_SEPARATOR;
16532 wd1 [wdlen + 1] = 0;
16533 wd = wd1;
16536 cached_wd = remap_debug_filename (wd);
16537 return cached_wd;
16540 /* Generate a DW_AT_comp_dir attribute for DIE. */
16542 static void
16543 add_comp_dir_attribute (dw_die_ref die)
16545 const char * wd = comp_dir_string ();
16546 if (wd != NULL)
16547 add_AT_string (die, DW_AT_comp_dir, wd);
16550 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16551 pointer computation, ...), output a representation for that bound according
16552 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16553 loc_list_from_tree for the meaning of CONTEXT. */
16555 static void
16556 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16557 int forms, const struct loc_descr_context *context)
16559 dw_die_ref ctx, decl_die;
16560 dw_loc_list_ref list;
16562 bool strip_conversions = true;
16564 while (strip_conversions)
16565 switch (TREE_CODE (value))
16567 case ERROR_MARK:
16568 case SAVE_EXPR:
16569 return;
16571 CASE_CONVERT:
16572 case VIEW_CONVERT_EXPR:
16573 value = TREE_OPERAND (value, 0);
16574 break;
16576 default:
16577 strip_conversions = false;
16578 break;
16581 /* If possible and permitted, output the attribute as a constant. */
16582 if ((forms & dw_scalar_form_constant) != 0
16583 && TREE_CODE (value) == INTEGER_CST)
16585 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16587 /* If HOST_WIDE_INT is big enough then represent the bound as
16588 a constant value. We need to choose a form based on
16589 whether the type is signed or unsigned. We cannot just
16590 call add_AT_unsigned if the value itself is positive
16591 (add_AT_unsigned might add the unsigned value encoded as
16592 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16593 bounds type and then sign extend any unsigned values found
16594 for signed types. This is needed only for
16595 DW_AT_{lower,upper}_bound, since for most other attributes,
16596 consumers will treat DW_FORM_data[1248] as unsigned values,
16597 regardless of the underlying type. */
16598 if (prec <= HOST_BITS_PER_WIDE_INT
16599 || tree_fits_uhwi_p (value))
16601 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16602 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16603 else
16604 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16606 else
16607 /* Otherwise represent the bound as an unsigned value with
16608 the precision of its type. The precision and signedness
16609 of the type will be necessary to re-interpret it
16610 unambiguously. */
16611 add_AT_wide (die, attr, value);
16612 return;
16615 /* Otherwise, if it's possible and permitted too, output a reference to
16616 another DIE. */
16617 if ((forms & dw_scalar_form_reference) != 0)
16619 tree decl = NULL_TREE;
16621 /* Some type attributes reference an outer type. For instance, the upper
16622 bound of an array may reference an embedding record (this happens in
16623 Ada). */
16624 if (TREE_CODE (value) == COMPONENT_REF
16625 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16626 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16627 decl = TREE_OPERAND (value, 1);
16629 else if (TREE_CODE (value) == VAR_DECL
16630 || TREE_CODE (value) == PARM_DECL
16631 || TREE_CODE (value) == RESULT_DECL)
16632 decl = value;
16634 if (decl != NULL_TREE)
16636 dw_die_ref decl_die = lookup_decl_die (decl);
16638 /* ??? Can this happen, or should the variable have been bound
16639 first? Probably it can, since I imagine that we try to create
16640 the types of parameters in the order in which they exist in
16641 the list, and won't have created a forward reference to a
16642 later parameter. */
16643 if (decl_die != NULL)
16645 add_AT_die_ref (die, attr, decl_die);
16646 return;
16651 /* Last chance: try to create a stack operation procedure to evaluate the
16652 value. Do nothing if even that is not possible or permitted. */
16653 if ((forms & dw_scalar_form_exprloc) == 0)
16654 return;
16656 list = loc_list_from_tree (value, 2, context);
16657 if (list == NULL || single_element_loc_list_p (list))
16659 /* If this attribute is not a reference nor constant, it is
16660 a DWARF expression rather than location description. For that
16661 loc_list_from_tree (value, 0, &context) is needed. */
16662 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16663 if (list2 && single_element_loc_list_p (list2))
16665 add_AT_loc (die, attr, list2->expr);
16666 return;
16670 /* If that failed to give a single element location list, fall back to
16671 outputting this as a reference... still if permitted. */
16672 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16673 return;
16675 if (current_function_decl == 0)
16676 ctx = comp_unit_die ();
16677 else
16678 ctx = lookup_decl_die (current_function_decl);
16680 decl_die = new_die (DW_TAG_variable, ctx, value);
16681 add_AT_flag (decl_die, DW_AT_artificial, 1);
16682 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16683 add_AT_location_description (decl_die, DW_AT_location, list);
16684 add_AT_die_ref (die, attr, decl_die);
16687 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16688 default. */
16690 static int
16691 lower_bound_default (void)
16693 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16695 case DW_LANG_C:
16696 case DW_LANG_C89:
16697 case DW_LANG_C99:
16698 case DW_LANG_C11:
16699 case DW_LANG_C_plus_plus:
16700 case DW_LANG_C_plus_plus_11:
16701 case DW_LANG_C_plus_plus_14:
16702 case DW_LANG_ObjC:
16703 case DW_LANG_ObjC_plus_plus:
16704 case DW_LANG_Java:
16705 return 0;
16706 case DW_LANG_Fortran77:
16707 case DW_LANG_Fortran90:
16708 case DW_LANG_Fortran95:
16709 return 1;
16710 case DW_LANG_UPC:
16711 case DW_LANG_D:
16712 case DW_LANG_Python:
16713 return dwarf_version >= 4 ? 0 : -1;
16714 case DW_LANG_Ada95:
16715 case DW_LANG_Ada83:
16716 case DW_LANG_Cobol74:
16717 case DW_LANG_Cobol85:
16718 case DW_LANG_Pascal83:
16719 case DW_LANG_Modula2:
16720 case DW_LANG_PLI:
16721 return dwarf_version >= 4 ? 1 : -1;
16722 default:
16723 return -1;
16727 /* Given a tree node describing an array bound (either lower or upper) output
16728 a representation for that bound. */
16730 static void
16731 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16732 tree bound, const struct loc_descr_context *context)
16734 int dflt;
16736 while (1)
16737 switch (TREE_CODE (bound))
16739 /* Strip all conversions. */
16740 CASE_CONVERT:
16741 case VIEW_CONVERT_EXPR:
16742 bound = TREE_OPERAND (bound, 0);
16743 break;
16745 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16746 are even omitted when they are the default. */
16747 case INTEGER_CST:
16748 /* If the value for this bound is the default one, we can even omit the
16749 attribute. */
16750 if (bound_attr == DW_AT_lower_bound
16751 && tree_fits_shwi_p (bound)
16752 && (dflt = lower_bound_default ()) != -1
16753 && tree_to_shwi (bound) == dflt)
16754 return;
16756 /* FALLTHRU */
16758 default:
16759 add_scalar_info (subrange_die, bound_attr, bound,
16760 dw_scalar_form_constant
16761 | dw_scalar_form_exprloc
16762 | dw_scalar_form_reference,
16763 context);
16764 return;
16768 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16769 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16770 Note that the block of subscript information for an array type also
16771 includes information about the element type of the given array type. */
16773 static void
16774 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16776 unsigned dimension_number;
16777 tree lower, upper;
16778 dw_die_ref subrange_die;
16780 for (dimension_number = 0;
16781 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16782 type = TREE_TYPE (type), dimension_number++)
16784 tree domain = TYPE_DOMAIN (type);
16786 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16787 break;
16789 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16790 and (in GNU C only) variable bounds. Handle all three forms
16791 here. */
16792 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16793 if (domain)
16795 /* We have an array type with specified bounds. */
16796 lower = TYPE_MIN_VALUE (domain);
16797 upper = TYPE_MAX_VALUE (domain);
16799 /* Define the index type. */
16800 if (TREE_TYPE (domain))
16802 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16803 TREE_TYPE field. We can't emit debug info for this
16804 because it is an unnamed integral type. */
16805 if (TREE_CODE (domain) == INTEGER_TYPE
16806 && TYPE_NAME (domain) == NULL_TREE
16807 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16808 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16810 else
16811 add_type_attribute (subrange_die, TREE_TYPE (domain),
16812 TYPE_UNQUALIFIED, type_die);
16815 /* ??? If upper is NULL, the array has unspecified length,
16816 but it does have a lower bound. This happens with Fortran
16817 dimension arr(N:*)
16818 Since the debugger is definitely going to need to know N
16819 to produce useful results, go ahead and output the lower
16820 bound solo, and hope the debugger can cope. */
16822 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16823 if (upper)
16824 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16827 /* Otherwise we have an array type with an unspecified length. The
16828 DWARF-2 spec does not say how to handle this; let's just leave out the
16829 bounds. */
16833 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16835 static void
16836 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16838 dw_die_ref decl_die;
16839 HOST_WIDE_INT size;
16841 switch (TREE_CODE (tree_node))
16843 case ERROR_MARK:
16844 size = 0;
16845 break;
16846 case ENUMERAL_TYPE:
16847 case RECORD_TYPE:
16848 case UNION_TYPE:
16849 case QUAL_UNION_TYPE:
16850 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16851 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16853 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16854 return;
16856 size = int_size_in_bytes (tree_node);
16857 break;
16858 case FIELD_DECL:
16859 /* For a data member of a struct or union, the DW_AT_byte_size is
16860 generally given as the number of bytes normally allocated for an
16861 object of the *declared* type of the member itself. This is true
16862 even for bit-fields. */
16863 size = int_size_in_bytes (field_type (tree_node));
16864 break;
16865 default:
16866 gcc_unreachable ();
16869 /* Note that `size' might be -1 when we get to this point. If it is, that
16870 indicates that the byte size of the entity in question is variable. We
16871 have no good way of expressing this fact in Dwarf at the present time,
16872 when location description was not used by the caller code instead. */
16873 if (size >= 0)
16874 add_AT_unsigned (die, DW_AT_byte_size, size);
16877 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16878 which specifies the distance in bits from the highest order bit of the
16879 "containing object" for the bit-field to the highest order bit of the
16880 bit-field itself.
16882 For any given bit-field, the "containing object" is a hypothetical object
16883 (of some integral or enum type) within which the given bit-field lives. The
16884 type of this hypothetical "containing object" is always the same as the
16885 declared type of the individual bit-field itself. The determination of the
16886 exact location of the "containing object" for a bit-field is rather
16887 complicated. It's handled by the `field_byte_offset' function (above).
16889 Note that it is the size (in bytes) of the hypothetical "containing object"
16890 which will be given in the DW_AT_byte_size attribute for this bit-field.
16891 (See `byte_size_attribute' above). */
16893 static inline void
16894 add_bit_offset_attribute (dw_die_ref die, tree decl)
16896 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16897 tree type = DECL_BIT_FIELD_TYPE (decl);
16898 HOST_WIDE_INT bitpos_int;
16899 HOST_WIDE_INT highest_order_object_bit_offset;
16900 HOST_WIDE_INT highest_order_field_bit_offset;
16901 HOST_WIDE_INT bit_offset;
16903 /* Must be a field and a bit field. */
16904 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16906 /* We can't yet handle bit-fields whose offsets are variable, so if we
16907 encounter such things, just return without generating any attribute
16908 whatsoever. Likewise for variable or too large size. */
16909 if (! tree_fits_shwi_p (bit_position (decl))
16910 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16911 return;
16913 bitpos_int = int_bit_position (decl);
16915 /* Note that the bit offset is always the distance (in bits) from the
16916 highest-order bit of the "containing object" to the highest-order bit of
16917 the bit-field itself. Since the "high-order end" of any object or field
16918 is different on big-endian and little-endian machines, the computation
16919 below must take account of these differences. */
16920 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16921 highest_order_field_bit_offset = bitpos_int;
16923 if (! BYTES_BIG_ENDIAN)
16925 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16926 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16929 bit_offset
16930 = (! BYTES_BIG_ENDIAN
16931 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16932 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16934 if (bit_offset < 0)
16935 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16936 else
16937 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16940 /* For a FIELD_DECL node which represents a bit field, output an attribute
16941 which specifies the length in bits of the given field. */
16943 static inline void
16944 add_bit_size_attribute (dw_die_ref die, tree decl)
16946 /* Must be a field and a bit field. */
16947 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16948 && DECL_BIT_FIELD_TYPE (decl));
16950 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16951 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16954 /* If the compiled language is ANSI C, then add a 'prototyped'
16955 attribute, if arg types are given for the parameters of a function. */
16957 static inline void
16958 add_prototyped_attribute (dw_die_ref die, tree func_type)
16960 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16962 case DW_LANG_C:
16963 case DW_LANG_C89:
16964 case DW_LANG_C99:
16965 case DW_LANG_C11:
16966 case DW_LANG_ObjC:
16967 if (prototype_p (func_type))
16968 add_AT_flag (die, DW_AT_prototyped, 1);
16969 break;
16970 default:
16971 break;
16975 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16976 by looking in either the type declaration or object declaration
16977 equate table. */
16979 static inline dw_die_ref
16980 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16982 dw_die_ref origin_die = NULL;
16984 if (TREE_CODE (origin) != FUNCTION_DECL)
16986 /* We may have gotten separated from the block for the inlined
16987 function, if we're in an exception handler or some such; make
16988 sure that the abstract function has been written out.
16990 Doing this for nested functions is wrong, however; functions are
16991 distinct units, and our context might not even be inline. */
16992 tree fn = origin;
16994 if (TYPE_P (fn))
16995 fn = TYPE_STUB_DECL (fn);
16997 fn = decl_function_context (fn);
16998 if (fn)
16999 dwarf2out_abstract_function (fn);
17002 if (DECL_P (origin))
17003 origin_die = lookup_decl_die (origin);
17004 else if (TYPE_P (origin))
17005 origin_die = lookup_type_die (origin);
17007 /* XXX: Functions that are never lowered don't always have correct block
17008 trees (in the case of java, they simply have no block tree, in some other
17009 languages). For these functions, there is nothing we can really do to
17010 output correct debug info for inlined functions in all cases. Rather
17011 than die, we'll just produce deficient debug info now, in that we will
17012 have variables without a proper abstract origin. In the future, when all
17013 functions are lowered, we should re-add a gcc_assert (origin_die)
17014 here. */
17016 if (origin_die)
17017 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17018 return origin_die;
17021 /* We do not currently support the pure_virtual attribute. */
17023 static inline void
17024 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17026 if (DECL_VINDEX (func_decl))
17028 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17030 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17031 add_AT_loc (die, DW_AT_vtable_elem_location,
17032 new_loc_descr (DW_OP_constu,
17033 tree_to_shwi (DECL_VINDEX (func_decl)),
17034 0));
17036 /* GNU extension: Record what type this method came from originally. */
17037 if (debug_info_level > DINFO_LEVEL_TERSE
17038 && DECL_CONTEXT (func_decl))
17039 add_AT_die_ref (die, DW_AT_containing_type,
17040 lookup_type_die (DECL_CONTEXT (func_decl)));
17044 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17045 given decl. This used to be a vendor extension until after DWARF 4
17046 standardized it. */
17048 static void
17049 add_linkage_attr (dw_die_ref die, tree decl)
17051 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17053 /* Mimic what assemble_name_raw does with a leading '*'. */
17054 if (name[0] == '*')
17055 name = &name[1];
17057 if (dwarf_version >= 4)
17058 add_AT_string (die, DW_AT_linkage_name, name);
17059 else
17060 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17063 /* Add source coordinate attributes for the given decl. */
17065 static void
17066 add_src_coords_attributes (dw_die_ref die, tree decl)
17068 expanded_location s;
17070 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17071 return;
17072 s = expand_location (DECL_SOURCE_LOCATION (decl));
17073 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17074 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17077 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17079 static void
17080 add_linkage_name (dw_die_ref die, tree decl)
17082 if (debug_info_level > DINFO_LEVEL_NONE
17083 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17084 && TREE_PUBLIC (decl)
17085 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17086 && die->die_tag != DW_TAG_member)
17088 /* Defer until we have an assembler name set. */
17089 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17091 limbo_die_node *asm_name;
17093 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17094 asm_name->die = die;
17095 asm_name->created_for = decl;
17096 asm_name->next = deferred_asm_name;
17097 deferred_asm_name = asm_name;
17099 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17100 add_linkage_attr (die, decl);
17104 /* Add a DW_AT_name attribute and source coordinate attribute for the
17105 given decl, but only if it actually has a name. */
17107 static void
17108 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17110 tree decl_name;
17112 decl_name = DECL_NAME (decl);
17113 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17115 const char *name = dwarf2_name (decl, 0);
17116 if (name)
17117 add_name_attribute (die, name);
17118 if (! DECL_ARTIFICIAL (decl))
17119 add_src_coords_attributes (die, decl);
17121 add_linkage_name (die, decl);
17124 #ifdef VMS_DEBUGGING_INFO
17125 /* Get the function's name, as described by its RTL. This may be different
17126 from the DECL_NAME name used in the source file. */
17127 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17129 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17130 XEXP (DECL_RTL (decl), 0), false);
17131 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17133 #endif /* VMS_DEBUGGING_INFO */
17136 #ifdef VMS_DEBUGGING_INFO
17137 /* Output the debug main pointer die for VMS */
17139 void
17140 dwarf2out_vms_debug_main_pointer (void)
17142 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17143 dw_die_ref die;
17145 /* Allocate the VMS debug main subprogram die. */
17146 die = ggc_cleared_alloc<die_node> ();
17147 die->die_tag = DW_TAG_subprogram;
17148 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17149 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17150 current_function_funcdef_no);
17151 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17153 /* Make it the first child of comp_unit_die (). */
17154 die->die_parent = comp_unit_die ();
17155 if (comp_unit_die ()->die_child)
17157 die->die_sib = comp_unit_die ()->die_child->die_sib;
17158 comp_unit_die ()->die_child->die_sib = die;
17160 else
17162 die->die_sib = die;
17163 comp_unit_die ()->die_child = die;
17166 #endif /* VMS_DEBUGGING_INFO */
17168 /* Push a new declaration scope. */
17170 static void
17171 push_decl_scope (tree scope)
17173 vec_safe_push (decl_scope_table, scope);
17176 /* Pop a declaration scope. */
17178 static inline void
17179 pop_decl_scope (void)
17181 decl_scope_table->pop ();
17184 /* walk_tree helper function for uses_local_type, below. */
17186 static tree
17187 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17189 if (!TYPE_P (*tp))
17190 *walk_subtrees = 0;
17191 else
17193 tree name = TYPE_NAME (*tp);
17194 if (name && DECL_P (name) && decl_function_context (name))
17195 return *tp;
17197 return NULL_TREE;
17200 /* If TYPE involves a function-local type (including a local typedef to a
17201 non-local type), returns that type; otherwise returns NULL_TREE. */
17203 static tree
17204 uses_local_type (tree type)
17206 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17207 return used;
17210 /* Return the DIE for the scope that immediately contains this type.
17211 Non-named types that do not involve a function-local type get global
17212 scope. Named types nested in namespaces or other types get their
17213 containing scope. All other types (i.e. function-local named types) get
17214 the current active scope. */
17216 static dw_die_ref
17217 scope_die_for (tree t, dw_die_ref context_die)
17219 dw_die_ref scope_die = NULL;
17220 tree containing_scope;
17222 /* Non-types always go in the current scope. */
17223 gcc_assert (TYPE_P (t));
17225 /* Use the scope of the typedef, rather than the scope of the type
17226 it refers to. */
17227 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17228 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17229 else
17230 containing_scope = TYPE_CONTEXT (t);
17232 /* Use the containing namespace if there is one. */
17233 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17235 if (context_die == lookup_decl_die (containing_scope))
17236 /* OK */;
17237 else if (debug_info_level > DINFO_LEVEL_TERSE)
17238 context_die = get_context_die (containing_scope);
17239 else
17240 containing_scope = NULL_TREE;
17243 /* Ignore function type "scopes" from the C frontend. They mean that
17244 a tagged type is local to a parmlist of a function declarator, but
17245 that isn't useful to DWARF. */
17246 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17247 containing_scope = NULL_TREE;
17249 if (SCOPE_FILE_SCOPE_P (containing_scope))
17251 /* If T uses a local type keep it local as well, to avoid references
17252 to function-local DIEs from outside the function. */
17253 if (current_function_decl && uses_local_type (t))
17254 scope_die = context_die;
17255 else
17256 scope_die = comp_unit_die ();
17258 else if (TYPE_P (containing_scope))
17260 /* For types, we can just look up the appropriate DIE. */
17261 if (debug_info_level > DINFO_LEVEL_TERSE)
17262 scope_die = get_context_die (containing_scope);
17263 else
17265 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17266 if (scope_die == NULL)
17267 scope_die = comp_unit_die ();
17270 else
17271 scope_die = context_die;
17273 return scope_die;
17276 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17278 static inline int
17279 local_scope_p (dw_die_ref context_die)
17281 for (; context_die; context_die = context_die->die_parent)
17282 if (context_die->die_tag == DW_TAG_inlined_subroutine
17283 || context_die->die_tag == DW_TAG_subprogram)
17284 return 1;
17286 return 0;
17289 /* Returns nonzero if CONTEXT_DIE is a class. */
17291 static inline int
17292 class_scope_p (dw_die_ref context_die)
17294 return (context_die
17295 && (context_die->die_tag == DW_TAG_structure_type
17296 || context_die->die_tag == DW_TAG_class_type
17297 || context_die->die_tag == DW_TAG_interface_type
17298 || context_die->die_tag == DW_TAG_union_type));
17301 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17302 whether or not to treat a DIE in this context as a declaration. */
17304 static inline int
17305 class_or_namespace_scope_p (dw_die_ref context_die)
17307 return (class_scope_p (context_die)
17308 || (context_die && context_die->die_tag == DW_TAG_namespace));
17311 /* Many forms of DIEs require a "type description" attribute. This
17312 routine locates the proper "type descriptor" die for the type given
17313 by 'type' plus any additional qualifiers given by 'cv_quals', and
17314 adds a DW_AT_type attribute below the given die. */
17316 static void
17317 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17318 dw_die_ref context_die)
17320 enum tree_code code = TREE_CODE (type);
17321 dw_die_ref type_die = NULL;
17323 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17324 or fixed-point type, use the inner type. This is because we have no
17325 support for unnamed types in base_type_die. This can happen if this is
17326 an Ada subrange type. Correct solution is emit a subrange type die. */
17327 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17328 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17329 type = TREE_TYPE (type), code = TREE_CODE (type);
17331 if (code == ERROR_MARK
17332 /* Handle a special case. For functions whose return type is void, we
17333 generate *no* type attribute. (Note that no object may have type
17334 `void', so this only applies to function return types). */
17335 || code == VOID_TYPE)
17336 return;
17338 type_die = modified_type_die (type,
17339 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17340 context_die);
17342 if (type_die != NULL)
17343 add_AT_die_ref (object_die, DW_AT_type, type_die);
17346 /* Given an object die, add the calling convention attribute for the
17347 function call type. */
17348 static void
17349 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17351 enum dwarf_calling_convention value = DW_CC_normal;
17353 value = ((enum dwarf_calling_convention)
17354 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17356 if (is_fortran ()
17357 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17359 /* DWARF 2 doesn't provide a way to identify a program's source-level
17360 entry point. DW_AT_calling_convention attributes are only meant
17361 to describe functions' calling conventions. However, lacking a
17362 better way to signal the Fortran main program, we used this for
17363 a long time, following existing custom. Now, DWARF 4 has
17364 DW_AT_main_subprogram, which we add below, but some tools still
17365 rely on the old way, which we thus keep. */
17366 value = DW_CC_program;
17368 if (dwarf_version >= 4 || !dwarf_strict)
17369 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17372 /* Only add the attribute if the backend requests it, and
17373 is not DW_CC_normal. */
17374 if (value && (value != DW_CC_normal))
17375 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17378 /* Given a tree pointer to a struct, class, union, or enum type node, return
17379 a pointer to the (string) tag name for the given type, or zero if the type
17380 was declared without a tag. */
17382 static const char *
17383 type_tag (const_tree type)
17385 const char *name = 0;
17387 if (TYPE_NAME (type) != 0)
17389 tree t = 0;
17391 /* Find the IDENTIFIER_NODE for the type name. */
17392 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17393 && !TYPE_NAMELESS (type))
17394 t = TYPE_NAME (type);
17396 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17397 a TYPE_DECL node, regardless of whether or not a `typedef' was
17398 involved. */
17399 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17400 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17402 /* We want to be extra verbose. Don't call dwarf_name if
17403 DECL_NAME isn't set. The default hook for decl_printable_name
17404 doesn't like that, and in this context it's correct to return
17405 0, instead of "<anonymous>" or the like. */
17406 if (DECL_NAME (TYPE_NAME (type))
17407 && !DECL_NAMELESS (TYPE_NAME (type)))
17408 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17411 /* Now get the name as a string, or invent one. */
17412 if (!name && t != 0)
17413 name = IDENTIFIER_POINTER (t);
17416 return (name == 0 || *name == '\0') ? 0 : name;
17419 /* Return the type associated with a data member, make a special check
17420 for bit field types. */
17422 static inline tree
17423 member_declared_type (const_tree member)
17425 return (DECL_BIT_FIELD_TYPE (member)
17426 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17429 /* Get the decl's label, as described by its RTL. This may be different
17430 from the DECL_NAME name used in the source file. */
17432 #if 0
17433 static const char *
17434 decl_start_label (tree decl)
17436 rtx x;
17437 const char *fnname;
17439 x = DECL_RTL (decl);
17440 gcc_assert (MEM_P (x));
17442 x = XEXP (x, 0);
17443 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17445 fnname = XSTR (x, 0);
17446 return fnname;
17448 #endif
17450 /* These routines generate the internal representation of the DIE's for
17451 the compilation unit. Debugging information is collected by walking
17452 the declaration trees passed in from dwarf2out_decl(). */
17454 static void
17455 gen_array_type_die (tree type, dw_die_ref context_die)
17457 dw_die_ref scope_die = scope_die_for (type, context_die);
17458 dw_die_ref array_die;
17460 /* GNU compilers represent multidimensional array types as sequences of one
17461 dimensional array types whose element types are themselves array types.
17462 We sometimes squish that down to a single array_type DIE with multiple
17463 subscripts in the Dwarf debugging info. The draft Dwarf specification
17464 say that we are allowed to do this kind of compression in C, because
17465 there is no difference between an array of arrays and a multidimensional
17466 array. We don't do this for Ada to remain as close as possible to the
17467 actual representation, which is especially important against the language
17468 flexibilty wrt arrays of variable size. */
17470 bool collapse_nested_arrays = !is_ada ();
17471 tree element_type;
17473 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17474 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17475 if (TYPE_STRING_FLAG (type)
17476 && TREE_CODE (type) == ARRAY_TYPE
17477 && is_fortran ()
17478 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17480 HOST_WIDE_INT size;
17482 array_die = new_die (DW_TAG_string_type, scope_die, type);
17483 add_name_attribute (array_die, type_tag (type));
17484 equate_type_number_to_die (type, array_die);
17485 size = int_size_in_bytes (type);
17486 if (size >= 0)
17487 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17488 else if (TYPE_DOMAIN (type) != NULL_TREE
17489 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17490 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17492 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17493 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17495 size = int_size_in_bytes (TREE_TYPE (szdecl));
17496 if (loc && size > 0)
17498 add_AT_location_description (array_die, DW_AT_string_length, loc);
17499 if (size != DWARF2_ADDR_SIZE)
17500 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17503 return;
17506 array_die = new_die (DW_TAG_array_type, scope_die, type);
17507 add_name_attribute (array_die, type_tag (type));
17508 equate_type_number_to_die (type, array_die);
17510 if (TREE_CODE (type) == VECTOR_TYPE)
17511 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17513 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17514 if (is_fortran ()
17515 && TREE_CODE (type) == ARRAY_TYPE
17516 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17517 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17518 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17520 #if 0
17521 /* We default the array ordering. SDB will probably do
17522 the right things even if DW_AT_ordering is not present. It's not even
17523 an issue until we start to get into multidimensional arrays anyway. If
17524 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17525 then we'll have to put the DW_AT_ordering attribute back in. (But if
17526 and when we find out that we need to put these in, we will only do so
17527 for multidimensional arrays. */
17528 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17529 #endif
17531 if (TREE_CODE (type) == VECTOR_TYPE)
17533 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17534 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17535 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17536 add_bound_info (subrange_die, DW_AT_upper_bound,
17537 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17539 else
17540 add_subscript_info (array_die, type, collapse_nested_arrays);
17542 /* Add representation of the type of the elements of this array type and
17543 emit the corresponding DIE if we haven't done it already. */
17544 element_type = TREE_TYPE (type);
17545 if (collapse_nested_arrays)
17546 while (TREE_CODE (element_type) == ARRAY_TYPE)
17548 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17549 break;
17550 element_type = TREE_TYPE (element_type);
17553 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17555 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17556 if (TYPE_ARTIFICIAL (type))
17557 add_AT_flag (array_die, DW_AT_artificial, 1);
17559 if (get_AT (array_die, DW_AT_name))
17560 add_pubtype (type, array_die);
17563 /* This routine generates DIE for array with hidden descriptor, details
17564 are filled into *info by a langhook. */
17566 static void
17567 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17568 dw_die_ref context_die)
17570 const dw_die_ref scope_die = scope_die_for (type, context_die);
17571 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17572 const struct loc_descr_context context = { type, info->base_decl };
17573 int dim;
17575 add_name_attribute (array_die, type_tag (type));
17576 equate_type_number_to_die (type, array_die);
17578 if (info->ndimensions > 1)
17579 switch (info->ordering)
17581 case array_descr_ordering_row_major:
17582 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17583 break;
17584 case array_descr_ordering_column_major:
17585 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17586 break;
17587 default:
17588 break;
17591 if (dwarf_version >= 3 || !dwarf_strict)
17593 if (info->data_location)
17594 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17595 dw_scalar_form_exprloc, &context);
17596 if (info->associated)
17597 add_scalar_info (array_die, DW_AT_associated, info->associated,
17598 dw_scalar_form_constant
17599 | dw_scalar_form_exprloc
17600 | dw_scalar_form_reference, &context);
17601 if (info->allocated)
17602 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17603 dw_scalar_form_constant
17604 | dw_scalar_form_exprloc
17605 | dw_scalar_form_reference, &context);
17608 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17610 for (dim = 0; dim < info->ndimensions; dim++)
17612 dw_die_ref subrange_die
17613 = new_die (DW_TAG_subrange_type, array_die, NULL);
17615 if (info->dimen[dim].bounds_type)
17616 add_type_attribute (subrange_die,
17617 info->dimen[dim].bounds_type, 0,
17618 context_die);
17619 if (info->dimen[dim].lower_bound)
17620 add_bound_info (subrange_die, DW_AT_lower_bound,
17621 info->dimen[dim].lower_bound, &context);
17622 if (info->dimen[dim].upper_bound)
17623 add_bound_info (subrange_die, DW_AT_upper_bound,
17624 info->dimen[dim].upper_bound, &context);
17625 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17626 add_scalar_info (subrange_die, DW_AT_byte_stride,
17627 info->dimen[dim].stride,
17628 dw_scalar_form_constant
17629 | dw_scalar_form_exprloc
17630 | dw_scalar_form_reference,
17631 &context);
17634 gen_type_die (info->element_type, context_die);
17635 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17636 context_die);
17638 if (get_AT (array_die, DW_AT_name))
17639 add_pubtype (type, array_die);
17642 #if 0
17643 static void
17644 gen_entry_point_die (tree decl, dw_die_ref context_die)
17646 tree origin = decl_ultimate_origin (decl);
17647 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17649 if (origin != NULL)
17650 add_abstract_origin_attribute (decl_die, origin);
17651 else
17653 add_name_and_src_coords_attributes (decl_die, decl);
17654 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17655 TYPE_UNQUALIFIED, context_die);
17658 if (DECL_ABSTRACT_P (decl))
17659 equate_decl_number_to_die (decl, decl_die);
17660 else
17661 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17663 #endif
17665 /* Walk through the list of incomplete types again, trying once more to
17666 emit full debugging info for them. */
17668 static void
17669 retry_incomplete_types (void)
17671 int i;
17673 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17674 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17675 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17678 /* Determine what tag to use for a record type. */
17680 static enum dwarf_tag
17681 record_type_tag (tree type)
17683 if (! lang_hooks.types.classify_record)
17684 return DW_TAG_structure_type;
17686 switch (lang_hooks.types.classify_record (type))
17688 case RECORD_IS_STRUCT:
17689 return DW_TAG_structure_type;
17691 case RECORD_IS_CLASS:
17692 return DW_TAG_class_type;
17694 case RECORD_IS_INTERFACE:
17695 if (dwarf_version >= 3 || !dwarf_strict)
17696 return DW_TAG_interface_type;
17697 return DW_TAG_structure_type;
17699 default:
17700 gcc_unreachable ();
17704 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17705 include all of the information about the enumeration values also. Each
17706 enumerated type name/value is listed as a child of the enumerated type
17707 DIE. */
17709 static dw_die_ref
17710 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17712 dw_die_ref type_die = lookup_type_die (type);
17714 if (type_die == NULL)
17716 type_die = new_die (DW_TAG_enumeration_type,
17717 scope_die_for (type, context_die), type);
17718 equate_type_number_to_die (type, type_die);
17719 add_name_attribute (type_die, type_tag (type));
17720 if (dwarf_version >= 4 || !dwarf_strict)
17722 if (ENUM_IS_SCOPED (type))
17723 add_AT_flag (type_die, DW_AT_enum_class, 1);
17724 if (ENUM_IS_OPAQUE (type))
17725 add_AT_flag (type_die, DW_AT_declaration, 1);
17728 else if (! TYPE_SIZE (type))
17729 return type_die;
17730 else
17731 remove_AT (type_die, DW_AT_declaration);
17733 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17734 given enum type is incomplete, do not generate the DW_AT_byte_size
17735 attribute or the DW_AT_element_list attribute. */
17736 if (TYPE_SIZE (type))
17738 tree link;
17740 TREE_ASM_WRITTEN (type) = 1;
17741 add_byte_size_attribute (type_die, type);
17742 if (dwarf_version >= 3 || !dwarf_strict)
17744 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17745 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17746 context_die);
17748 if (TYPE_STUB_DECL (type) != NULL_TREE)
17750 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17751 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17754 /* If the first reference to this type was as the return type of an
17755 inline function, then it may not have a parent. Fix this now. */
17756 if (type_die->die_parent == NULL)
17757 add_child_die (scope_die_for (type, context_die), type_die);
17759 for (link = TYPE_VALUES (type);
17760 link != NULL; link = TREE_CHAIN (link))
17762 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17763 tree value = TREE_VALUE (link);
17765 add_name_attribute (enum_die,
17766 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17768 if (TREE_CODE (value) == CONST_DECL)
17769 value = DECL_INITIAL (value);
17771 if (simple_type_size_in_bits (TREE_TYPE (value))
17772 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17774 /* For constant forms created by add_AT_unsigned DWARF
17775 consumers (GDB, elfutils, etc.) always zero extend
17776 the value. Only when the actual value is negative
17777 do we need to use add_AT_int to generate a constant
17778 form that can represent negative values. */
17779 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17780 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17781 add_AT_unsigned (enum_die, DW_AT_const_value,
17782 (unsigned HOST_WIDE_INT) val);
17783 else
17784 add_AT_int (enum_die, DW_AT_const_value, val);
17786 else
17787 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17788 that here. TODO: This should be re-worked to use correct
17789 signed/unsigned double tags for all cases. */
17790 add_AT_wide (enum_die, DW_AT_const_value, value);
17793 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17794 if (TYPE_ARTIFICIAL (type))
17795 add_AT_flag (type_die, DW_AT_artificial, 1);
17797 else
17798 add_AT_flag (type_die, DW_AT_declaration, 1);
17800 add_pubtype (type, type_die);
17802 return type_die;
17805 /* Generate a DIE to represent either a real live formal parameter decl or to
17806 represent just the type of some formal parameter position in some function
17807 type.
17809 Note that this routine is a bit unusual because its argument may be a
17810 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17811 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17812 node. If it's the former then this function is being called to output a
17813 DIE to represent a formal parameter object (or some inlining thereof). If
17814 it's the latter, then this function is only being called to output a
17815 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17816 argument type of some subprogram type.
17817 If EMIT_NAME_P is true, name and source coordinate attributes
17818 are emitted. */
17820 static dw_die_ref
17821 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17822 dw_die_ref context_die)
17824 tree node_or_origin = node ? node : origin;
17825 tree ultimate_origin;
17826 dw_die_ref parm_die
17827 = new_die (DW_TAG_formal_parameter, context_die, node);
17829 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17831 case tcc_declaration:
17832 ultimate_origin = decl_ultimate_origin (node_or_origin);
17833 if (node || ultimate_origin)
17834 origin = ultimate_origin;
17835 if (origin != NULL)
17836 add_abstract_origin_attribute (parm_die, origin);
17837 else if (emit_name_p)
17838 add_name_and_src_coords_attributes (parm_die, node);
17839 if (origin == NULL
17840 || (! DECL_ABSTRACT_P (node_or_origin)
17841 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17842 decl_function_context
17843 (node_or_origin))))
17845 tree type = TREE_TYPE (node_or_origin);
17846 if (decl_by_reference_p (node_or_origin))
17847 add_type_attribute (parm_die, TREE_TYPE (type),
17848 TYPE_UNQUALIFIED, context_die);
17849 else
17850 add_type_attribute (parm_die, type,
17851 decl_quals (node_or_origin),
17852 context_die);
17854 if (origin == NULL && DECL_ARTIFICIAL (node))
17855 add_AT_flag (parm_die, DW_AT_artificial, 1);
17857 if (node && node != origin)
17858 equate_decl_number_to_die (node, parm_die);
17859 if (! DECL_ABSTRACT_P (node_or_origin))
17860 add_location_or_const_value_attribute (parm_die, node_or_origin,
17861 node == NULL, DW_AT_location);
17863 break;
17865 case tcc_type:
17866 /* We were called with some kind of a ..._TYPE node. */
17867 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17868 context_die);
17869 break;
17871 default:
17872 gcc_unreachable ();
17875 return parm_die;
17878 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17879 children DW_TAG_formal_parameter DIEs representing the arguments of the
17880 parameter pack.
17882 PARM_PACK must be a function parameter pack.
17883 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17884 must point to the subsequent arguments of the function PACK_ARG belongs to.
17885 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17886 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17887 following the last one for which a DIE was generated. */
17889 static dw_die_ref
17890 gen_formal_parameter_pack_die (tree parm_pack,
17891 tree pack_arg,
17892 dw_die_ref subr_die,
17893 tree *next_arg)
17895 tree arg;
17896 dw_die_ref parm_pack_die;
17898 gcc_assert (parm_pack
17899 && lang_hooks.function_parameter_pack_p (parm_pack)
17900 && subr_die);
17902 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17903 add_src_coords_attributes (parm_pack_die, parm_pack);
17905 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17907 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17908 parm_pack))
17909 break;
17910 gen_formal_parameter_die (arg, NULL,
17911 false /* Don't emit name attribute. */,
17912 parm_pack_die);
17914 if (next_arg)
17915 *next_arg = arg;
17916 return parm_pack_die;
17919 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17920 at the end of an (ANSI prototyped) formal parameters list. */
17922 static void
17923 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17925 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17928 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17929 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17930 parameters as specified in some function type specification (except for
17931 those which appear as part of a function *definition*). */
17933 static void
17934 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17936 tree link;
17937 tree formal_type = NULL;
17938 tree first_parm_type;
17939 tree arg;
17941 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17943 arg = DECL_ARGUMENTS (function_or_method_type);
17944 function_or_method_type = TREE_TYPE (function_or_method_type);
17946 else
17947 arg = NULL_TREE;
17949 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17951 /* Make our first pass over the list of formal parameter types and output a
17952 DW_TAG_formal_parameter DIE for each one. */
17953 for (link = first_parm_type; link; )
17955 dw_die_ref parm_die;
17957 formal_type = TREE_VALUE (link);
17958 if (formal_type == void_type_node)
17959 break;
17961 /* Output a (nameless) DIE to represent the formal parameter itself. */
17962 if (!POINTER_BOUNDS_TYPE_P (formal_type))
17964 parm_die = gen_formal_parameter_die (formal_type, NULL,
17965 true /* Emit name attribute. */,
17966 context_die);
17967 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17968 && link == first_parm_type)
17970 add_AT_flag (parm_die, DW_AT_artificial, 1);
17971 if (dwarf_version >= 3 || !dwarf_strict)
17972 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17974 else if (arg && DECL_ARTIFICIAL (arg))
17975 add_AT_flag (parm_die, DW_AT_artificial, 1);
17978 link = TREE_CHAIN (link);
17979 if (arg)
17980 arg = DECL_CHAIN (arg);
17983 /* If this function type has an ellipsis, add a
17984 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17985 if (formal_type != void_type_node)
17986 gen_unspecified_parameters_die (function_or_method_type, context_die);
17988 /* Make our second (and final) pass over the list of formal parameter types
17989 and output DIEs to represent those types (as necessary). */
17990 for (link = TYPE_ARG_TYPES (function_or_method_type);
17991 link && TREE_VALUE (link);
17992 link = TREE_CHAIN (link))
17993 gen_type_die (TREE_VALUE (link), context_die);
17996 /* We want to generate the DIE for TYPE so that we can generate the
17997 die for MEMBER, which has been defined; we will need to refer back
17998 to the member declaration nested within TYPE. If we're trying to
17999 generate minimal debug info for TYPE, processing TYPE won't do the
18000 trick; we need to attach the member declaration by hand. */
18002 static void
18003 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18005 gen_type_die (type, context_die);
18007 /* If we're trying to avoid duplicate debug info, we may not have
18008 emitted the member decl for this function. Emit it now. */
18009 if (TYPE_STUB_DECL (type)
18010 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18011 && ! lookup_decl_die (member))
18013 dw_die_ref type_die;
18014 gcc_assert (!decl_ultimate_origin (member));
18016 push_decl_scope (type);
18017 type_die = lookup_type_die_strip_naming_typedef (type);
18018 if (TREE_CODE (member) == FUNCTION_DECL)
18019 gen_subprogram_die (member, type_die);
18020 else if (TREE_CODE (member) == FIELD_DECL)
18022 /* Ignore the nameless fields that are used to skip bits but handle
18023 C++ anonymous unions and structs. */
18024 if (DECL_NAME (member) != NULL_TREE
18025 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18026 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18028 gen_type_die (member_declared_type (member), type_die);
18029 gen_field_die (member, type_die);
18032 else
18033 gen_variable_die (member, NULL_TREE, type_die);
18035 pop_decl_scope ();
18039 /* Forward declare these functions, because they are mutually recursive
18040 with their set_block_* pairing functions. */
18041 static void set_decl_origin_self (tree);
18042 static void set_decl_abstract_flags (tree, int);
18044 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18045 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18046 that it points to the node itself, thus indicating that the node is its
18047 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18048 the given node is NULL, recursively descend the decl/block tree which
18049 it is the root of, and for each other ..._DECL or BLOCK node contained
18050 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18051 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18052 values to point to themselves. */
18054 static void
18055 set_block_origin_self (tree stmt)
18057 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18059 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18062 tree local_decl;
18064 for (local_decl = BLOCK_VARS (stmt);
18065 local_decl != NULL_TREE;
18066 local_decl = DECL_CHAIN (local_decl))
18067 /* Do not recurse on nested functions since the inlining status
18068 of parent and child can be different as per the DWARF spec. */
18069 if (TREE_CODE (local_decl) != FUNCTION_DECL
18070 && !DECL_EXTERNAL (local_decl))
18071 set_decl_origin_self (local_decl);
18075 tree subblock;
18077 for (subblock = BLOCK_SUBBLOCKS (stmt);
18078 subblock != NULL_TREE;
18079 subblock = BLOCK_CHAIN (subblock))
18080 set_block_origin_self (subblock); /* Recurse. */
18085 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18086 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18087 node to so that it points to the node itself, thus indicating that the
18088 node represents its own (abstract) origin. Additionally, if the
18089 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18090 the decl/block tree of which the given node is the root of, and for
18091 each other ..._DECL or BLOCK node contained therein whose
18092 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18093 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18094 point to themselves. */
18096 static void
18097 set_decl_origin_self (tree decl)
18099 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18101 DECL_ABSTRACT_ORIGIN (decl) = decl;
18102 if (TREE_CODE (decl) == FUNCTION_DECL)
18104 tree arg;
18106 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18107 DECL_ABSTRACT_ORIGIN (arg) = arg;
18108 if (DECL_INITIAL (decl) != NULL_TREE
18109 && DECL_INITIAL (decl) != error_mark_node)
18110 set_block_origin_self (DECL_INITIAL (decl));
18115 /* Given a pointer to some BLOCK node, and a boolean value to set the
18116 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
18117 the given block, and for all local decls and all local sub-blocks
18118 (recursively) which are contained therein. */
18120 static void
18121 set_block_abstract_flags (tree stmt, int setting)
18123 tree local_decl;
18124 tree subblock;
18125 unsigned int i;
18127 BLOCK_ABSTRACT (stmt) = setting;
18129 for (local_decl = BLOCK_VARS (stmt);
18130 local_decl != NULL_TREE;
18131 local_decl = DECL_CHAIN (local_decl))
18132 if (! DECL_EXTERNAL (local_decl))
18133 set_decl_abstract_flags (local_decl, setting);
18135 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18137 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18138 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18139 || TREE_CODE (local_decl) == PARM_DECL)
18140 set_decl_abstract_flags (local_decl, setting);
18143 for (subblock = BLOCK_SUBBLOCKS (stmt);
18144 subblock != NULL_TREE;
18145 subblock = BLOCK_CHAIN (subblock))
18146 set_block_abstract_flags (subblock, setting);
18149 /* Given a pointer to some ..._DECL node, and a boolean value to set the
18150 "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
18151 given decl, and (in the case where the decl is a FUNCTION_DECL) also
18152 set the abstract flags for all of the parameters, local vars, local
18153 blocks and sub-blocks (recursively) to the same setting. */
18155 static void
18156 set_decl_abstract_flags (tree decl, int setting)
18158 DECL_ABSTRACT_P (decl) = setting;
18159 if (TREE_CODE (decl) == FUNCTION_DECL)
18161 tree arg;
18163 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18164 DECL_ABSTRACT_P (arg) = setting;
18165 if (DECL_INITIAL (decl) != NULL_TREE
18166 && DECL_INITIAL (decl) != error_mark_node)
18167 set_block_abstract_flags (DECL_INITIAL (decl), setting);
18171 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18172 may later generate inlined and/or out-of-line instances of. */
18174 static void
18175 dwarf2out_abstract_function (tree decl)
18177 dw_die_ref old_die;
18178 tree save_fn;
18179 tree context;
18180 int was_abstract;
18181 hash_table<decl_loc_hasher> *old_decl_loc_table;
18182 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18183 int old_call_site_count, old_tail_call_site_count;
18184 struct call_arg_loc_node *old_call_arg_locations;
18186 /* Make sure we have the actual abstract inline, not a clone. */
18187 decl = DECL_ORIGIN (decl);
18189 old_die = lookup_decl_die (decl);
18190 if (old_die && get_AT (old_die, DW_AT_inline))
18191 /* We've already generated the abstract instance. */
18192 return;
18194 /* We can be called while recursively when seeing block defining inlined subroutine
18195 DIE. Be sure to not clobber the outer location table nor use it or we would
18196 get locations in abstract instantces. */
18197 old_decl_loc_table = decl_loc_table;
18198 decl_loc_table = NULL;
18199 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18200 cached_dw_loc_list_table = NULL;
18201 old_call_arg_locations = call_arg_locations;
18202 call_arg_locations = NULL;
18203 old_call_site_count = call_site_count;
18204 call_site_count = -1;
18205 old_tail_call_site_count = tail_call_site_count;
18206 tail_call_site_count = -1;
18208 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18209 we don't get confused by DECL_ABSTRACT_P. */
18210 if (debug_info_level > DINFO_LEVEL_TERSE)
18212 context = decl_class_context (decl);
18213 if (context)
18214 gen_type_die_for_member
18215 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18218 /* Pretend we've just finished compiling this function. */
18219 save_fn = current_function_decl;
18220 current_function_decl = decl;
18222 was_abstract = DECL_ABSTRACT_P (decl);
18223 set_decl_abstract_flags (decl, 1);
18224 dwarf2out_decl (decl);
18225 if (! was_abstract)
18226 set_decl_abstract_flags (decl, 0);
18228 current_function_decl = save_fn;
18229 decl_loc_table = old_decl_loc_table;
18230 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18231 call_arg_locations = old_call_arg_locations;
18232 call_site_count = old_call_site_count;
18233 tail_call_site_count = old_tail_call_site_count;
18236 /* Helper function of premark_used_types() which gets called through
18237 htab_traverse.
18239 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18240 marked as unused by prune_unused_types. */
18242 bool
18243 premark_used_types_helper (tree const &type, void *)
18245 dw_die_ref die;
18247 die = lookup_type_die (type);
18248 if (die != NULL)
18249 die->die_perennial_p = 1;
18250 return true;
18253 /* Helper function of premark_types_used_by_global_vars which gets called
18254 through htab_traverse.
18256 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18257 marked as unused by prune_unused_types. The DIE of the type is marked
18258 only if the global variable using the type will actually be emitted. */
18261 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18262 void *)
18264 struct types_used_by_vars_entry *entry;
18265 dw_die_ref die;
18267 entry = (struct types_used_by_vars_entry *) *slot;
18268 gcc_assert (entry->type != NULL
18269 && entry->var_decl != NULL);
18270 die = lookup_type_die (entry->type);
18271 if (die)
18273 /* Ask cgraph if the global variable really is to be emitted.
18274 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18275 varpool_node *node = varpool_node::get (entry->var_decl);
18276 if (node && node->definition)
18278 die->die_perennial_p = 1;
18279 /* Keep the parent DIEs as well. */
18280 while ((die = die->die_parent) && die->die_perennial_p == 0)
18281 die->die_perennial_p = 1;
18284 return 1;
18287 /* Mark all members of used_types_hash as perennial. */
18289 static void
18290 premark_used_types (struct function *fun)
18292 if (fun && fun->used_types_hash)
18293 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18296 /* Mark all members of types_used_by_vars_entry as perennial. */
18298 static void
18299 premark_types_used_by_global_vars (void)
18301 if (types_used_by_vars_hash)
18302 types_used_by_vars_hash
18303 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18306 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18307 for CA_LOC call arg loc node. */
18309 static dw_die_ref
18310 gen_call_site_die (tree decl, dw_die_ref subr_die,
18311 struct call_arg_loc_node *ca_loc)
18313 dw_die_ref stmt_die = NULL, die;
18314 tree block = ca_loc->block;
18316 while (block
18317 && block != DECL_INITIAL (decl)
18318 && TREE_CODE (block) == BLOCK)
18320 if (block_map.length () > BLOCK_NUMBER (block))
18321 stmt_die = block_map[BLOCK_NUMBER (block)];
18322 if (stmt_die)
18323 break;
18324 block = BLOCK_SUPERCONTEXT (block);
18326 if (stmt_die == NULL)
18327 stmt_die = subr_die;
18328 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18329 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18330 if (ca_loc->tail_call_p)
18331 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18332 if (ca_loc->symbol_ref)
18334 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18335 if (tdie)
18336 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18337 else
18338 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18340 return die;
18343 /* Generate a DIE to represent a declared function (either file-scope or
18344 block-local). */
18346 static void
18347 gen_subprogram_die (tree decl, dw_die_ref context_die)
18349 tree origin = decl_ultimate_origin (decl);
18350 dw_die_ref subr_die;
18351 tree outer_scope;
18352 dw_die_ref old_die = lookup_decl_die (decl);
18353 int declaration = (current_function_decl != decl
18354 || class_or_namespace_scope_p (context_die));
18356 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18358 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18359 started to generate the abstract instance of an inline, decided to output
18360 its containing class, and proceeded to emit the declaration of the inline
18361 from the member list for the class. If so, DECLARATION takes priority;
18362 we'll get back to the abstract instance when done with the class. */
18364 /* The class-scope declaration DIE must be the primary DIE. */
18365 if (origin && declaration && class_or_namespace_scope_p (context_die))
18367 origin = NULL;
18368 gcc_assert (!old_die);
18371 /* Now that the C++ front end lazily declares artificial member fns, we
18372 might need to retrofit the declaration into its class. */
18373 if (!declaration && !origin && !old_die
18374 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18375 && !class_or_namespace_scope_p (context_die)
18376 && debug_info_level > DINFO_LEVEL_TERSE)
18377 old_die = force_decl_die (decl);
18379 if (origin != NULL)
18381 gcc_assert (!declaration || local_scope_p (context_die));
18383 /* Fixup die_parent for the abstract instance of a nested
18384 inline function. */
18385 if (old_die && old_die->die_parent == NULL)
18386 add_child_die (context_die, old_die);
18388 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18389 add_abstract_origin_attribute (subr_die, origin);
18390 /* This is where the actual code for a cloned function is.
18391 Let's emit linkage name attribute for it. This helps
18392 debuggers to e.g, set breakpoints into
18393 constructors/destructors when the user asks "break
18394 K::K". */
18395 add_linkage_name (subr_die, decl);
18397 else if (old_die)
18399 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18400 struct dwarf_file_data * file_index = lookup_filename (s.file);
18402 if (!get_AT_flag (old_die, DW_AT_declaration)
18403 /* We can have a normal definition following an inline one in the
18404 case of redefinition of GNU C extern inlines.
18405 It seems reasonable to use AT_specification in this case. */
18406 && !get_AT (old_die, DW_AT_inline))
18408 /* Detect and ignore this case, where we are trying to output
18409 something we have already output. */
18410 return;
18413 /* If the definition comes from the same place as the declaration,
18414 maybe use the old DIE. We always want the DIE for this function
18415 that has the *_pc attributes to be under comp_unit_die so the
18416 debugger can find it. We also need to do this for abstract
18417 instances of inlines, since the spec requires the out-of-line copy
18418 to have the same parent. For local class methods, this doesn't
18419 apply; we just use the old DIE. */
18420 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18421 && (DECL_ARTIFICIAL (decl)
18422 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18423 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18424 == (unsigned) s.line))))
18426 subr_die = old_die;
18428 /* Clear out the declaration attribute and the formal parameters.
18429 Do not remove all children, because it is possible that this
18430 declaration die was forced using force_decl_die(). In such
18431 cases die that forced declaration die (e.g. TAG_imported_module)
18432 is one of the children that we do not want to remove. */
18433 remove_AT (subr_die, DW_AT_declaration);
18434 remove_AT (subr_die, DW_AT_object_pointer);
18435 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18437 else
18439 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18440 add_AT_specification (subr_die, old_die);
18441 add_pubname (decl, subr_die);
18442 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18443 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18444 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18445 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18447 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18448 emit the real type on the definition die. */
18449 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18451 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18452 if (die == auto_die || die == decltype_auto_die)
18453 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18454 TYPE_UNQUALIFIED, context_die);
18458 else
18460 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18462 if (TREE_PUBLIC (decl))
18463 add_AT_flag (subr_die, DW_AT_external, 1);
18465 add_name_and_src_coords_attributes (subr_die, decl);
18466 add_pubname (decl, subr_die);
18467 if (debug_info_level > DINFO_LEVEL_TERSE)
18469 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18470 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18471 TYPE_UNQUALIFIED, context_die);
18474 add_pure_or_virtual_attribute (subr_die, decl);
18475 if (DECL_ARTIFICIAL (decl))
18476 add_AT_flag (subr_die, DW_AT_artificial, 1);
18478 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18479 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18481 add_accessibility_attribute (subr_die, decl);
18484 if (declaration)
18486 if (!old_die || !get_AT (old_die, DW_AT_inline))
18488 add_AT_flag (subr_die, DW_AT_declaration, 1);
18490 /* If this is an explicit function declaration then generate
18491 a DW_AT_explicit attribute. */
18492 if (lang_hooks.decls.function_decl_explicit_p (decl)
18493 && (dwarf_version >= 3 || !dwarf_strict))
18494 add_AT_flag (subr_die, DW_AT_explicit, 1);
18496 /* If this is a C++11 deleted special function member then generate
18497 a DW_AT_GNU_deleted attribute. */
18498 if (lang_hooks.decls.function_decl_deleted_p (decl)
18499 && (! dwarf_strict))
18500 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18502 /* The first time we see a member function, it is in the context of
18503 the class to which it belongs. We make sure of this by emitting
18504 the class first. The next time is the definition, which is
18505 handled above. The two may come from the same source text.
18507 Note that force_decl_die() forces function declaration die. It is
18508 later reused to represent definition. */
18509 equate_decl_number_to_die (decl, subr_die);
18512 else if (DECL_ABSTRACT_P (decl))
18514 if (DECL_DECLARED_INLINE_P (decl))
18516 if (cgraph_function_possibly_inlined_p (decl))
18517 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18518 else
18519 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18521 else
18523 if (cgraph_function_possibly_inlined_p (decl))
18524 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18525 else
18526 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18529 if (DECL_DECLARED_INLINE_P (decl)
18530 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18531 add_AT_flag (subr_die, DW_AT_artificial, 1);
18533 equate_decl_number_to_die (decl, subr_die);
18535 else if (!DECL_EXTERNAL (decl))
18537 HOST_WIDE_INT cfa_fb_offset;
18538 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18540 if (!old_die || !get_AT (old_die, DW_AT_inline))
18541 equate_decl_number_to_die (decl, subr_die);
18543 gcc_checking_assert (fun);
18544 if (!flag_reorder_blocks_and_partition)
18546 dw_fde_ref fde = fun->fde;
18547 if (fde->dw_fde_begin)
18549 /* We have already generated the labels. */
18550 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18551 fde->dw_fde_end, false);
18553 else
18555 /* Create start/end labels and add the range. */
18556 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18557 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18558 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18559 current_function_funcdef_no);
18560 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18561 current_function_funcdef_no);
18562 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18563 false);
18566 #if VMS_DEBUGGING_INFO
18567 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18568 Section 2.3 Prologue and Epilogue Attributes:
18569 When a breakpoint is set on entry to a function, it is generally
18570 desirable for execution to be suspended, not on the very first
18571 instruction of the function, but rather at a point after the
18572 function's frame has been set up, after any language defined local
18573 declaration processing has been completed, and before execution of
18574 the first statement of the function begins. Debuggers generally
18575 cannot properly determine where this point is. Similarly for a
18576 breakpoint set on exit from a function. The prologue and epilogue
18577 attributes allow a compiler to communicate the location(s) to use. */
18580 if (fde->dw_fde_vms_end_prologue)
18581 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18582 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18584 if (fde->dw_fde_vms_begin_epilogue)
18585 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18586 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18588 #endif
18591 else
18593 /* Generate pubnames entries for the split function code ranges. */
18594 dw_fde_ref fde = fun->fde;
18596 if (fde->dw_fde_second_begin)
18598 if (dwarf_version >= 3 || !dwarf_strict)
18600 /* We should use ranges for non-contiguous code section
18601 addresses. Use the actual code range for the initial
18602 section, since the HOT/COLD labels might precede an
18603 alignment offset. */
18604 bool range_list_added = false;
18605 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18606 fde->dw_fde_end, &range_list_added,
18607 false);
18608 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18609 fde->dw_fde_second_end,
18610 &range_list_added, false);
18611 if (range_list_added)
18612 add_ranges (NULL);
18614 else
18616 /* There is no real support in DW2 for this .. so we make
18617 a work-around. First, emit the pub name for the segment
18618 containing the function label. Then make and emit a
18619 simplified subprogram DIE for the second segment with the
18620 name pre-fixed by __hot/cold_sect_of_. We use the same
18621 linkage name for the second die so that gdb will find both
18622 sections when given "b foo". */
18623 const char *name = NULL;
18624 tree decl_name = DECL_NAME (decl);
18625 dw_die_ref seg_die;
18627 /* Do the 'primary' section. */
18628 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18629 fde->dw_fde_end, false);
18631 /* Build a minimal DIE for the secondary section. */
18632 seg_die = new_die (DW_TAG_subprogram,
18633 subr_die->die_parent, decl);
18635 if (TREE_PUBLIC (decl))
18636 add_AT_flag (seg_die, DW_AT_external, 1);
18638 if (decl_name != NULL
18639 && IDENTIFIER_POINTER (decl_name) != NULL)
18641 name = dwarf2_name (decl, 1);
18642 if (! DECL_ARTIFICIAL (decl))
18643 add_src_coords_attributes (seg_die, decl);
18645 add_linkage_name (seg_die, decl);
18647 gcc_assert (name != NULL);
18648 add_pure_or_virtual_attribute (seg_die, decl);
18649 if (DECL_ARTIFICIAL (decl))
18650 add_AT_flag (seg_die, DW_AT_artificial, 1);
18652 name = concat ("__second_sect_of_", name, NULL);
18653 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18654 fde->dw_fde_second_end, false);
18655 add_name_attribute (seg_die, name);
18656 if (want_pubnames ())
18657 add_pubname_string (name, seg_die);
18660 else
18661 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18662 false);
18665 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18667 /* We define the "frame base" as the function's CFA. This is more
18668 convenient for several reasons: (1) It's stable across the prologue
18669 and epilogue, which makes it better than just a frame pointer,
18670 (2) With dwarf3, there exists a one-byte encoding that allows us
18671 to reference the .debug_frame data by proxy, but failing that,
18672 (3) We can at least reuse the code inspection and interpretation
18673 code that determines the CFA position at various points in the
18674 function. */
18675 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18677 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18678 add_AT_loc (subr_die, DW_AT_frame_base, op);
18680 else
18682 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18683 if (list->dw_loc_next)
18684 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18685 else
18686 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18689 /* Compute a displacement from the "steady-state frame pointer" to
18690 the CFA. The former is what all stack slots and argument slots
18691 will reference in the rtl; the latter is what we've told the
18692 debugger about. We'll need to adjust all frame_base references
18693 by this displacement. */
18694 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18696 if (fun->static_chain_decl)
18697 add_AT_location_description (subr_die, DW_AT_static_link,
18698 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
18701 /* Generate child dies for template paramaters. */
18702 if (debug_info_level > DINFO_LEVEL_TERSE)
18703 gen_generic_params_dies (decl);
18705 /* Now output descriptions of the arguments for this function. This gets
18706 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18707 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18708 `...' at the end of the formal parameter list. In order to find out if
18709 there was a trailing ellipsis or not, we must instead look at the type
18710 associated with the FUNCTION_DECL. This will be a node of type
18711 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18712 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18713 an ellipsis at the end. */
18715 /* In the case where we are describing a mere function declaration, all we
18716 need to do here (and all we *can* do here) is to describe the *types* of
18717 its formal parameters. */
18718 if (debug_info_level <= DINFO_LEVEL_TERSE)
18720 else if (declaration)
18721 gen_formal_types_die (decl, subr_die);
18722 else
18724 /* Generate DIEs to represent all known formal parameters. */
18725 tree parm = DECL_ARGUMENTS (decl);
18726 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18727 tree generic_decl_parm = generic_decl
18728 ? DECL_ARGUMENTS (generic_decl)
18729 : NULL;
18731 /* Now we want to walk the list of parameters of the function and
18732 emit their relevant DIEs.
18734 We consider the case of DECL being an instance of a generic function
18735 as well as it being a normal function.
18737 If DECL is an instance of a generic function we walk the
18738 parameters of the generic function declaration _and_ the parameters of
18739 DECL itself. This is useful because we want to emit specific DIEs for
18740 function parameter packs and those are declared as part of the
18741 generic function declaration. In that particular case,
18742 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18743 That DIE has children DIEs representing the set of arguments
18744 of the pack. Note that the set of pack arguments can be empty.
18745 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18746 children DIE.
18748 Otherwise, we just consider the parameters of DECL. */
18749 while (generic_decl_parm || parm)
18751 if (generic_decl_parm
18752 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18753 gen_formal_parameter_pack_die (generic_decl_parm,
18754 parm, subr_die,
18755 &parm);
18756 else if (parm && !POINTER_BOUNDS_P (parm))
18758 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18760 if (parm == DECL_ARGUMENTS (decl)
18761 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18762 && parm_die
18763 && (dwarf_version >= 3 || !dwarf_strict))
18764 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18766 parm = DECL_CHAIN (parm);
18768 else if (parm)
18769 parm = DECL_CHAIN (parm);
18771 if (generic_decl_parm)
18772 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18775 /* Decide whether we need an unspecified_parameters DIE at the end.
18776 There are 2 more cases to do this for: 1) the ansi ... declaration -
18777 this is detectable when the end of the arg list is not a
18778 void_type_node 2) an unprototyped function declaration (not a
18779 definition). This just means that we have no info about the
18780 parameters at all. */
18781 if (prototype_p (TREE_TYPE (decl)))
18783 /* This is the prototyped case, check for.... */
18784 if (stdarg_p (TREE_TYPE (decl)))
18785 gen_unspecified_parameters_die (decl, subr_die);
18787 else if (DECL_INITIAL (decl) == NULL_TREE)
18788 gen_unspecified_parameters_die (decl, subr_die);
18791 /* Output Dwarf info for all of the stuff within the body of the function
18792 (if it has one - it may be just a declaration). */
18793 outer_scope = DECL_INITIAL (decl);
18795 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18796 a function. This BLOCK actually represents the outermost binding contour
18797 for the function, i.e. the contour in which the function's formal
18798 parameters and labels get declared. Curiously, it appears that the front
18799 end doesn't actually put the PARM_DECL nodes for the current function onto
18800 the BLOCK_VARS list for this outer scope, but are strung off of the
18801 DECL_ARGUMENTS list for the function instead.
18803 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18804 the LABEL_DECL nodes for the function however, and we output DWARF info
18805 for those in decls_for_scope. Just within the `outer_scope' there will be
18806 a BLOCK node representing the function's outermost pair of curly braces,
18807 and any blocks used for the base and member initializers of a C++
18808 constructor function. */
18809 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18811 int call_site_note_count = 0;
18812 int tail_call_site_note_count = 0;
18814 /* Emit a DW_TAG_variable DIE for a named return value. */
18815 if (DECL_NAME (DECL_RESULT (decl)))
18816 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18818 decls_for_scope (outer_scope, subr_die);
18820 if (call_arg_locations && !dwarf_strict)
18822 struct call_arg_loc_node *ca_loc;
18823 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18825 dw_die_ref die = NULL;
18826 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18827 rtx arg, next_arg;
18829 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18830 arg; arg = next_arg)
18832 dw_loc_descr_ref reg, val;
18833 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18834 dw_die_ref cdie, tdie = NULL;
18836 next_arg = XEXP (arg, 1);
18837 if (REG_P (XEXP (XEXP (arg, 0), 0))
18838 && next_arg
18839 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18840 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18841 && REGNO (XEXP (XEXP (arg, 0), 0))
18842 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18843 next_arg = XEXP (next_arg, 1);
18844 if (mode == VOIDmode)
18846 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18847 if (mode == VOIDmode)
18848 mode = GET_MODE (XEXP (arg, 0));
18850 if (mode == VOIDmode || mode == BLKmode)
18851 continue;
18852 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18854 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18855 tloc = XEXP (XEXP (arg, 0), 1);
18856 continue;
18858 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18859 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18861 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18862 tlocc = XEXP (XEXP (arg, 0), 1);
18863 continue;
18865 reg = NULL;
18866 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18867 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18868 VAR_INIT_STATUS_INITIALIZED);
18869 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18871 rtx mem = XEXP (XEXP (arg, 0), 0);
18872 reg = mem_loc_descriptor (XEXP (mem, 0),
18873 get_address_mode (mem),
18874 GET_MODE (mem),
18875 VAR_INIT_STATUS_INITIALIZED);
18877 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18878 == DEBUG_PARAMETER_REF)
18880 tree tdecl
18881 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18882 tdie = lookup_decl_die (tdecl);
18883 if (tdie == NULL)
18884 continue;
18886 else
18887 continue;
18888 if (reg == NULL
18889 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18890 != DEBUG_PARAMETER_REF)
18891 continue;
18892 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18893 VOIDmode,
18894 VAR_INIT_STATUS_INITIALIZED);
18895 if (val == NULL)
18896 continue;
18897 if (die == NULL)
18898 die = gen_call_site_die (decl, subr_die, ca_loc);
18899 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18900 NULL_TREE);
18901 if (reg != NULL)
18902 add_AT_loc (cdie, DW_AT_location, reg);
18903 else if (tdie != NULL)
18904 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18905 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18906 if (next_arg != XEXP (arg, 1))
18908 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18909 if (mode == VOIDmode)
18910 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18911 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18912 0), 1),
18913 mode, VOIDmode,
18914 VAR_INIT_STATUS_INITIALIZED);
18915 if (val != NULL)
18916 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18919 if (die == NULL
18920 && (ca_loc->symbol_ref || tloc))
18921 die = gen_call_site_die (decl, subr_die, ca_loc);
18922 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18924 dw_loc_descr_ref tval = NULL;
18926 if (tloc != NULL_RTX)
18927 tval = mem_loc_descriptor (tloc,
18928 GET_MODE (tloc) == VOIDmode
18929 ? Pmode : GET_MODE (tloc),
18930 VOIDmode,
18931 VAR_INIT_STATUS_INITIALIZED);
18932 if (tval)
18933 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18934 else if (tlocc != NULL_RTX)
18936 tval = mem_loc_descriptor (tlocc,
18937 GET_MODE (tlocc) == VOIDmode
18938 ? Pmode : GET_MODE (tlocc),
18939 VOIDmode,
18940 VAR_INIT_STATUS_INITIALIZED);
18941 if (tval)
18942 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18943 tval);
18946 if (die != NULL)
18948 call_site_note_count++;
18949 if (ca_loc->tail_call_p)
18950 tail_call_site_note_count++;
18954 call_arg_locations = NULL;
18955 call_arg_loc_last = NULL;
18956 if (tail_call_site_count >= 0
18957 && tail_call_site_count == tail_call_site_note_count
18958 && !dwarf_strict)
18960 if (call_site_count >= 0
18961 && call_site_count == call_site_note_count)
18962 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18963 else
18964 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18966 call_site_count = -1;
18967 tail_call_site_count = -1;
18970 if (subr_die != old_die)
18971 /* Add the calling convention attribute if requested. */
18972 add_calling_convention_attribute (subr_die, decl);
18975 /* Returns a hash value for X (which really is a die_struct). */
18977 hashval_t
18978 block_die_hasher::hash (die_struct *d)
18980 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18983 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18984 as decl_id and die_parent of die_struct Y. */
18986 bool
18987 block_die_hasher::equal (die_struct *x, die_struct *y)
18989 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
18992 /* Generate a DIE to represent a declared data object.
18993 Either DECL or ORIGIN must be non-null. */
18995 static void
18996 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18998 HOST_WIDE_INT off = 0;
18999 tree com_decl;
19000 tree decl_or_origin = decl ? decl : origin;
19001 tree ultimate_origin;
19002 dw_die_ref var_die;
19003 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19004 dw_die_ref origin_die;
19005 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19006 || class_or_namespace_scope_p (context_die));
19007 bool specialization_p = false;
19009 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19010 if (decl || ultimate_origin)
19011 origin = ultimate_origin;
19012 com_decl = fortran_common (decl_or_origin, &off);
19014 /* Symbol in common gets emitted as a child of the common block, in the form
19015 of a data member. */
19016 if (com_decl)
19018 dw_die_ref com_die;
19019 dw_loc_list_ref loc;
19020 die_node com_die_arg;
19022 var_die = lookup_decl_die (decl_or_origin);
19023 if (var_die)
19025 if (get_AT (var_die, DW_AT_location) == NULL)
19027 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19028 if (loc)
19030 if (off)
19032 /* Optimize the common case. */
19033 if (single_element_loc_list_p (loc)
19034 && loc->expr->dw_loc_opc == DW_OP_addr
19035 && loc->expr->dw_loc_next == NULL
19036 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19037 == SYMBOL_REF)
19039 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19040 loc->expr->dw_loc_oprnd1.v.val_addr
19041 = plus_constant (GET_MODE (x), x , off);
19043 else
19044 loc_list_plus_const (loc, off);
19046 add_AT_location_description (var_die, DW_AT_location, loc);
19047 remove_AT (var_die, DW_AT_declaration);
19050 return;
19053 if (common_block_die_table == NULL)
19054 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19056 com_die_arg.decl_id = DECL_UID (com_decl);
19057 com_die_arg.die_parent = context_die;
19058 com_die = common_block_die_table->find (&com_die_arg);
19059 loc = loc_list_from_tree (com_decl, 2, NULL);
19060 if (com_die == NULL)
19062 const char *cnam
19063 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19064 die_node **slot;
19066 com_die = new_die (DW_TAG_common_block, context_die, decl);
19067 add_name_and_src_coords_attributes (com_die, com_decl);
19068 if (loc)
19070 add_AT_location_description (com_die, DW_AT_location, loc);
19071 /* Avoid sharing the same loc descriptor between
19072 DW_TAG_common_block and DW_TAG_variable. */
19073 loc = loc_list_from_tree (com_decl, 2, NULL);
19075 else if (DECL_EXTERNAL (decl))
19076 add_AT_flag (com_die, DW_AT_declaration, 1);
19077 if (want_pubnames ())
19078 add_pubname_string (cnam, com_die); /* ??? needed? */
19079 com_die->decl_id = DECL_UID (com_decl);
19080 slot = common_block_die_table->find_slot (com_die, INSERT);
19081 *slot = com_die;
19083 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19085 add_AT_location_description (com_die, DW_AT_location, loc);
19086 loc = loc_list_from_tree (com_decl, 2, NULL);
19087 remove_AT (com_die, DW_AT_declaration);
19089 var_die = new_die (DW_TAG_variable, com_die, decl);
19090 add_name_and_src_coords_attributes (var_die, decl);
19091 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19092 context_die);
19093 add_AT_flag (var_die, DW_AT_external, 1);
19094 if (loc)
19096 if (off)
19098 /* Optimize the common case. */
19099 if (single_element_loc_list_p (loc)
19100 && loc->expr->dw_loc_opc == DW_OP_addr
19101 && loc->expr->dw_loc_next == NULL
19102 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19104 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19105 loc->expr->dw_loc_oprnd1.v.val_addr
19106 = plus_constant (GET_MODE (x), x, off);
19108 else
19109 loc_list_plus_const (loc, off);
19111 add_AT_location_description (var_die, DW_AT_location, loc);
19113 else if (DECL_EXTERNAL (decl))
19114 add_AT_flag (var_die, DW_AT_declaration, 1);
19115 equate_decl_number_to_die (decl, var_die);
19116 return;
19119 /* If the compiler emitted a definition for the DECL declaration
19120 and if we already emitted a DIE for it, don't emit a second
19121 DIE for it again. Allow re-declarations of DECLs that are
19122 inside functions, though. */
19123 if (old_die && declaration && !local_scope_p (context_die))
19124 return;
19126 /* For static data members, the declaration in the class is supposed
19127 to have DW_TAG_member tag; the specification should still be
19128 DW_TAG_variable referencing the DW_TAG_member DIE. */
19129 if (declaration && class_scope_p (context_die))
19130 var_die = new_die (DW_TAG_member, context_die, decl);
19131 else
19132 var_die = new_die (DW_TAG_variable, context_die, decl);
19134 origin_die = NULL;
19135 if (origin != NULL)
19136 origin_die = add_abstract_origin_attribute (var_die, origin);
19138 /* Loop unrolling can create multiple blocks that refer to the same
19139 static variable, so we must test for the DW_AT_declaration flag.
19141 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19142 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19143 sharing them.
19145 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19147 ??? The declare_in_namespace support causes us to get two DIEs for one
19148 variable, both of which are declarations. We want to avoid considering
19149 one to be a specification, so we must test that this DIE is not a
19150 declaration. */
19151 else if (old_die && TREE_STATIC (decl) && ! declaration
19152 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19154 /* This is a definition of a C++ class level static. */
19155 add_AT_specification (var_die, old_die);
19156 specialization_p = true;
19157 if (DECL_NAME (decl))
19159 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19160 struct dwarf_file_data * file_index = lookup_filename (s.file);
19162 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19163 add_AT_file (var_die, DW_AT_decl_file, file_index);
19165 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19166 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19168 if (old_die->die_tag == DW_TAG_member)
19169 add_linkage_name (var_die, decl);
19172 else
19173 add_name_and_src_coords_attributes (var_die, decl);
19175 if ((origin == NULL && !specialization_p)
19176 || (origin != NULL
19177 && !DECL_ABSTRACT_P (decl_or_origin)
19178 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19179 decl_function_context
19180 (decl_or_origin))))
19182 tree type = TREE_TYPE (decl_or_origin);
19184 if (decl_by_reference_p (decl_or_origin))
19185 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19186 context_die);
19187 else
19188 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19189 context_die);
19192 if (origin == NULL && !specialization_p)
19194 if (TREE_PUBLIC (decl))
19195 add_AT_flag (var_die, DW_AT_external, 1);
19197 if (DECL_ARTIFICIAL (decl))
19198 add_AT_flag (var_die, DW_AT_artificial, 1);
19200 add_accessibility_attribute (var_die, decl);
19203 if (declaration)
19204 add_AT_flag (var_die, DW_AT_declaration, 1);
19206 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19207 equate_decl_number_to_die (decl, var_die);
19209 if (! declaration
19210 && (! DECL_ABSTRACT_P (decl_or_origin)
19211 /* Local static vars are shared between all clones/inlines,
19212 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19213 already set. */
19214 || (TREE_CODE (decl_or_origin) == VAR_DECL
19215 && TREE_STATIC (decl_or_origin)
19216 && DECL_RTL_SET_P (decl_or_origin)))
19217 /* When abstract origin already has DW_AT_location attribute, no need
19218 to add it again. */
19219 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19221 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19222 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19223 defer_location (decl_or_origin, var_die);
19224 else
19225 add_location_or_const_value_attribute (var_die, decl_or_origin,
19226 decl == NULL, DW_AT_location);
19227 add_pubname (decl_or_origin, var_die);
19229 else
19230 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19233 /* Generate a DIE to represent a named constant. */
19235 static void
19236 gen_const_die (tree decl, dw_die_ref context_die)
19238 dw_die_ref const_die;
19239 tree type = TREE_TYPE (decl);
19241 const_die = new_die (DW_TAG_constant, context_die, decl);
19242 add_name_and_src_coords_attributes (const_die, decl);
19243 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19244 if (TREE_PUBLIC (decl))
19245 add_AT_flag (const_die, DW_AT_external, 1);
19246 if (DECL_ARTIFICIAL (decl))
19247 add_AT_flag (const_die, DW_AT_artificial, 1);
19248 tree_add_const_value_attribute_for_decl (const_die, decl);
19251 /* Generate a DIE to represent a label identifier. */
19253 static void
19254 gen_label_die (tree decl, dw_die_ref context_die)
19256 tree origin = decl_ultimate_origin (decl);
19257 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19258 rtx insn;
19259 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19261 if (origin != NULL)
19262 add_abstract_origin_attribute (lbl_die, origin);
19263 else
19264 add_name_and_src_coords_attributes (lbl_die, decl);
19266 if (DECL_ABSTRACT_P (decl))
19267 equate_decl_number_to_die (decl, lbl_die);
19268 else
19270 insn = DECL_RTL_IF_SET (decl);
19272 /* Deleted labels are programmer specified labels which have been
19273 eliminated because of various optimizations. We still emit them
19274 here so that it is possible to put breakpoints on them. */
19275 if (insn
19276 && (LABEL_P (insn)
19277 || ((NOTE_P (insn)
19278 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19280 /* When optimization is enabled (via -O) some parts of the compiler
19281 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19282 represent source-level labels which were explicitly declared by
19283 the user. This really shouldn't be happening though, so catch
19284 it if it ever does happen. */
19285 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19287 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19288 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19290 else if (insn
19291 && NOTE_P (insn)
19292 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19293 && CODE_LABEL_NUMBER (insn) != -1)
19295 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19296 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19301 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19302 attributes to the DIE for a block STMT, to describe where the inlined
19303 function was called from. This is similar to add_src_coords_attributes. */
19305 static inline void
19306 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19308 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19310 if (dwarf_version >= 3 || !dwarf_strict)
19312 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19313 add_AT_unsigned (die, DW_AT_call_line, s.line);
19318 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19319 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19321 static inline void
19322 add_high_low_attributes (tree stmt, dw_die_ref die)
19324 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19326 if (BLOCK_FRAGMENT_CHAIN (stmt)
19327 && (dwarf_version >= 3 || !dwarf_strict))
19329 tree chain, superblock = NULL_TREE;
19330 dw_die_ref pdie;
19331 dw_attr_ref attr = NULL;
19333 if (inlined_function_outer_scope_p (stmt))
19335 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19336 BLOCK_NUMBER (stmt));
19337 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19340 /* Optimize duplicate .debug_ranges lists or even tails of
19341 lists. If this BLOCK has same ranges as its supercontext,
19342 lookup DW_AT_ranges attribute in the supercontext (and
19343 recursively so), verify that the ranges_table contains the
19344 right values and use it instead of adding a new .debug_range. */
19345 for (chain = stmt, pdie = die;
19346 BLOCK_SAME_RANGE (chain);
19347 chain = BLOCK_SUPERCONTEXT (chain))
19349 dw_attr_ref new_attr;
19351 pdie = pdie->die_parent;
19352 if (pdie == NULL)
19353 break;
19354 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19355 break;
19356 new_attr = get_AT (pdie, DW_AT_ranges);
19357 if (new_attr == NULL
19358 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19359 break;
19360 attr = new_attr;
19361 superblock = BLOCK_SUPERCONTEXT (chain);
19363 if (attr != NULL
19364 && (ranges_table[attr->dw_attr_val.v.val_offset
19365 / 2 / DWARF2_ADDR_SIZE].num
19366 == BLOCK_NUMBER (superblock))
19367 && BLOCK_FRAGMENT_CHAIN (superblock))
19369 unsigned long off = attr->dw_attr_val.v.val_offset
19370 / 2 / DWARF2_ADDR_SIZE;
19371 unsigned long supercnt = 0, thiscnt = 0;
19372 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19373 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19375 ++supercnt;
19376 gcc_checking_assert (ranges_table[off + supercnt].num
19377 == BLOCK_NUMBER (chain));
19379 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19380 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19381 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19382 ++thiscnt;
19383 gcc_assert (supercnt >= thiscnt);
19384 add_AT_range_list (die, DW_AT_ranges,
19385 ((off + supercnt - thiscnt)
19386 * 2 * DWARF2_ADDR_SIZE),
19387 false);
19388 return;
19391 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19393 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19396 add_ranges (chain);
19397 chain = BLOCK_FRAGMENT_CHAIN (chain);
19399 while (chain);
19400 add_ranges (NULL);
19402 else
19404 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19405 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19406 BLOCK_NUMBER (stmt));
19407 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19408 BLOCK_NUMBER (stmt));
19409 add_AT_low_high_pc (die, label, label_high, false);
19413 /* Generate a DIE for a lexical block. */
19415 static void
19416 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19418 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19420 if (call_arg_locations)
19422 if (block_map.length () <= BLOCK_NUMBER (stmt))
19423 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19424 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19427 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19428 add_high_low_attributes (stmt, stmt_die);
19430 decls_for_scope (stmt, stmt_die);
19433 /* Generate a DIE for an inlined subprogram. */
19435 static void
19436 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19438 tree decl;
19440 /* The instance of function that is effectively being inlined shall not
19441 be abstract. */
19442 gcc_assert (! BLOCK_ABSTRACT (stmt));
19444 decl = block_ultimate_origin (stmt);
19446 /* Emit info for the abstract instance first, if we haven't yet. We
19447 must emit this even if the block is abstract, otherwise when we
19448 emit the block below (or elsewhere), we may end up trying to emit
19449 a die whose origin die hasn't been emitted, and crashing. */
19450 dwarf2out_abstract_function (decl);
19452 if (! BLOCK_ABSTRACT (stmt))
19454 dw_die_ref subr_die
19455 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19457 if (call_arg_locations)
19459 if (block_map.length () <= BLOCK_NUMBER (stmt))
19460 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19461 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19463 add_abstract_origin_attribute (subr_die, decl);
19464 if (TREE_ASM_WRITTEN (stmt))
19465 add_high_low_attributes (stmt, subr_die);
19466 add_call_src_coords_attributes (stmt, subr_die);
19468 decls_for_scope (stmt, subr_die);
19472 /* Generate a DIE for a field in a record, or structure. */
19474 static void
19475 gen_field_die (tree decl, dw_die_ref context_die)
19477 dw_die_ref decl_die;
19479 if (TREE_TYPE (decl) == error_mark_node)
19480 return;
19482 decl_die = new_die (DW_TAG_member, context_die, decl);
19483 add_name_and_src_coords_attributes (decl_die, decl);
19484 add_type_attribute (decl_die, member_declared_type (decl),
19485 decl_quals (decl), context_die);
19487 if (DECL_BIT_FIELD_TYPE (decl))
19489 add_byte_size_attribute (decl_die, decl);
19490 add_bit_size_attribute (decl_die, decl);
19491 add_bit_offset_attribute (decl_die, decl);
19494 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19495 add_data_member_location_attribute (decl_die, decl);
19497 if (DECL_ARTIFICIAL (decl))
19498 add_AT_flag (decl_die, DW_AT_artificial, 1);
19500 add_accessibility_attribute (decl_die, decl);
19502 /* Equate decl number to die, so that we can look up this decl later on. */
19503 equate_decl_number_to_die (decl, decl_die);
19506 #if 0
19507 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19508 Use modified_type_die instead.
19509 We keep this code here just in case these types of DIEs may be needed to
19510 represent certain things in other languages (e.g. Pascal) someday. */
19512 static void
19513 gen_pointer_type_die (tree type, dw_die_ref context_die)
19515 dw_die_ref ptr_die
19516 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19518 equate_type_number_to_die (type, ptr_die);
19519 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19520 context_die);
19521 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19524 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19525 Use modified_type_die instead.
19526 We keep this code here just in case these types of DIEs may be needed to
19527 represent certain things in other languages (e.g. Pascal) someday. */
19529 static void
19530 gen_reference_type_die (tree type, dw_die_ref context_die)
19532 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19534 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19535 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19536 else
19537 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19539 equate_type_number_to_die (type, ref_die);
19540 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19541 context_die);
19542 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19544 #endif
19546 /* Generate a DIE for a pointer to a member type. */
19548 static void
19549 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19551 dw_die_ref ptr_die
19552 = new_die (DW_TAG_ptr_to_member_type,
19553 scope_die_for (type, context_die), type);
19555 equate_type_number_to_die (type, ptr_die);
19556 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19557 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19558 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19559 context_die);
19562 typedef const char *dchar_p; /* For DEF_VEC_P. */
19564 static char *producer_string;
19566 /* Return a heap allocated producer string including command line options
19567 if -grecord-gcc-switches. */
19569 static char *
19570 gen_producer_string (void)
19572 size_t j;
19573 auto_vec<dchar_p> switches;
19574 const char *language_string = lang_hooks.name;
19575 char *producer, *tail;
19576 const char *p;
19577 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19578 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19580 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19581 switch (save_decoded_options[j].opt_index)
19583 case OPT_o:
19584 case OPT_d:
19585 case OPT_dumpbase:
19586 case OPT_dumpdir:
19587 case OPT_auxbase:
19588 case OPT_auxbase_strip:
19589 case OPT_quiet:
19590 case OPT_version:
19591 case OPT_v:
19592 case OPT_w:
19593 case OPT_L:
19594 case OPT_D:
19595 case OPT_I:
19596 case OPT_U:
19597 case OPT_SPECIAL_unknown:
19598 case OPT_SPECIAL_ignore:
19599 case OPT_SPECIAL_program_name:
19600 case OPT_SPECIAL_input_file:
19601 case OPT_grecord_gcc_switches:
19602 case OPT_gno_record_gcc_switches:
19603 case OPT__output_pch_:
19604 case OPT_fdiagnostics_show_location_:
19605 case OPT_fdiagnostics_show_option:
19606 case OPT_fdiagnostics_show_caret:
19607 case OPT_fdiagnostics_color_:
19608 case OPT_fverbose_asm:
19609 case OPT____:
19610 case OPT__sysroot_:
19611 case OPT_nostdinc:
19612 case OPT_nostdinc__:
19613 /* Ignore these. */
19614 continue;
19615 default:
19616 if (cl_options[save_decoded_options[j].opt_index].flags
19617 & CL_NO_DWARF_RECORD)
19618 continue;
19619 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19620 == '-');
19621 switch (save_decoded_options[j].canonical_option[0][1])
19623 case 'M':
19624 case 'i':
19625 case 'W':
19626 continue;
19627 case 'f':
19628 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19629 "dump", 4) == 0)
19630 continue;
19631 break;
19632 default:
19633 break;
19635 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19636 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19637 break;
19640 producer = XNEWVEC (char, plen + 1 + len + 1);
19641 tail = producer;
19642 sprintf (tail, "%s %s", language_string, version_string);
19643 tail += plen;
19645 FOR_EACH_VEC_ELT (switches, j, p)
19647 len = strlen (p);
19648 *tail = ' ';
19649 memcpy (tail + 1, p, len);
19650 tail += len + 1;
19653 *tail = '\0';
19654 return producer;
19657 /* Given a C and/or C++ language/version string return the "highest".
19658 C++ is assumed to be "higher" than C in this case. Used for merging
19659 LTO translation unit languages. */
19660 static const char *
19661 highest_c_language (const char *lang1, const char *lang2)
19663 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19664 return "GNU C++14";
19665 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19666 return "GNU C++11";
19667 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19668 return "GNU C++98";
19670 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19671 return "GNU C11";
19672 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19673 return "GNU C99";
19674 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19675 return "GNU C89";
19677 gcc_unreachable ();
19681 /* Generate the DIE for the compilation unit. */
19683 static dw_die_ref
19684 gen_compile_unit_die (const char *filename)
19686 dw_die_ref die;
19687 const char *language_string = lang_hooks.name;
19688 int language;
19690 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19692 if (filename)
19694 add_name_attribute (die, filename);
19695 /* Don't add cwd for <built-in>. */
19696 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19697 add_comp_dir_attribute (die);
19700 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19702 /* If our producer is LTO try to figure out a common language to use
19703 from the global list of translation units. */
19704 if (strcmp (language_string, "GNU GIMPLE") == 0)
19706 unsigned i;
19707 tree t;
19708 const char *common_lang = NULL;
19710 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19712 if (!TRANSLATION_UNIT_LANGUAGE (t))
19713 continue;
19714 if (!common_lang)
19715 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19716 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19718 else if (strncmp (common_lang, "GNU C", 5) == 0
19719 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19720 /* Mixing C and C++ is ok, use C++ in that case. */
19721 common_lang = highest_c_language (common_lang,
19722 TRANSLATION_UNIT_LANGUAGE (t));
19723 else
19725 /* Fall back to C. */
19726 common_lang = NULL;
19727 break;
19731 if (common_lang)
19732 language_string = common_lang;
19735 language = DW_LANG_C;
19736 if (strncmp (language_string, "GNU C", 5) == 0
19737 && ISDIGIT (language_string[5]))
19739 language = DW_LANG_C89;
19740 if (dwarf_version >= 3 || !dwarf_strict)
19742 if (strcmp (language_string, "GNU C89") != 0)
19743 language = DW_LANG_C99;
19745 if (dwarf_version >= 5 /* || !dwarf_strict */)
19746 if (strcmp (language_string, "GNU C11") == 0)
19747 language = DW_LANG_C11;
19750 else if (strncmp (language_string, "GNU C++", 7) == 0)
19752 language = DW_LANG_C_plus_plus;
19753 if (dwarf_version >= 5 /* || !dwarf_strict */)
19755 if (strcmp (language_string, "GNU C++11") == 0)
19756 language = DW_LANG_C_plus_plus_11;
19757 else if (strcmp (language_string, "GNU C++14") == 0)
19758 language = DW_LANG_C_plus_plus_14;
19761 else if (strcmp (language_string, "GNU F77") == 0)
19762 language = DW_LANG_Fortran77;
19763 else if (strcmp (language_string, "GNU Pascal") == 0)
19764 language = DW_LANG_Pascal83;
19765 else if (dwarf_version >= 3 || !dwarf_strict)
19767 if (strcmp (language_string, "GNU Ada") == 0)
19768 language = DW_LANG_Ada95;
19769 else if (strcmp (language_string, "GNU Fortran") == 0)
19770 language = DW_LANG_Fortran95;
19771 else if (strcmp (language_string, "GNU Java") == 0)
19772 language = DW_LANG_Java;
19773 else if (strcmp (language_string, "GNU Objective-C") == 0)
19774 language = DW_LANG_ObjC;
19775 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19776 language = DW_LANG_ObjC_plus_plus;
19777 else if (dwarf_version >= 5 || !dwarf_strict)
19779 if (strcmp (language_string, "GNU Go") == 0)
19780 language = DW_LANG_Go;
19783 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19784 else if (strcmp (language_string, "GNU Fortran") == 0)
19785 language = DW_LANG_Fortran90;
19787 add_AT_unsigned (die, DW_AT_language, language);
19789 switch (language)
19791 case DW_LANG_Fortran77:
19792 case DW_LANG_Fortran90:
19793 case DW_LANG_Fortran95:
19794 /* Fortran has case insensitive identifiers and the front-end
19795 lowercases everything. */
19796 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19797 break;
19798 default:
19799 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19800 break;
19802 return die;
19805 /* Generate the DIE for a base class. */
19807 static void
19808 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19810 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19812 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19813 add_data_member_location_attribute (die, binfo);
19815 if (BINFO_VIRTUAL_P (binfo))
19816 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19818 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19819 children, otherwise the default is DW_ACCESS_public. In DWARF2
19820 the default has always been DW_ACCESS_private. */
19821 if (access == access_public_node)
19823 if (dwarf_version == 2
19824 || context_die->die_tag == DW_TAG_class_type)
19825 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19827 else if (access == access_protected_node)
19828 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19829 else if (dwarf_version > 2
19830 && context_die->die_tag != DW_TAG_class_type)
19831 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19834 /* Generate a DIE for a class member. */
19836 static void
19837 gen_member_die (tree type, dw_die_ref context_die)
19839 tree member;
19840 tree binfo = TYPE_BINFO (type);
19841 dw_die_ref child;
19843 /* If this is not an incomplete type, output descriptions of each of its
19844 members. Note that as we output the DIEs necessary to represent the
19845 members of this record or union type, we will also be trying to output
19846 DIEs to represent the *types* of those members. However the `type'
19847 function (above) will specifically avoid generating type DIEs for member
19848 types *within* the list of member DIEs for this (containing) type except
19849 for those types (of members) which are explicitly marked as also being
19850 members of this (containing) type themselves. The g++ front- end can
19851 force any given type to be treated as a member of some other (containing)
19852 type by setting the TYPE_CONTEXT of the given (member) type to point to
19853 the TREE node representing the appropriate (containing) type. */
19855 /* First output info about the base classes. */
19856 if (binfo)
19858 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19859 int i;
19860 tree base;
19862 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19863 gen_inheritance_die (base,
19864 (accesses ? (*accesses)[i] : access_public_node),
19865 context_die);
19868 /* Now output info about the data members and type members. */
19869 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19871 /* If we thought we were generating minimal debug info for TYPE
19872 and then changed our minds, some of the member declarations
19873 may have already been defined. Don't define them again, but
19874 do put them in the right order. */
19876 child = lookup_decl_die (member);
19877 if (child)
19878 splice_child_die (context_die, child);
19879 else
19880 gen_decl_die (member, NULL, context_die);
19883 /* Now output info about the function members (if any). */
19884 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19886 /* Don't include clones in the member list. */
19887 if (DECL_ABSTRACT_ORIGIN (member))
19888 continue;
19890 child = lookup_decl_die (member);
19891 if (child)
19892 splice_child_die (context_die, child);
19893 else
19894 gen_decl_die (member, NULL, context_die);
19898 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19899 is set, we pretend that the type was never defined, so we only get the
19900 member DIEs needed by later specification DIEs. */
19902 static void
19903 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19904 enum debug_info_usage usage)
19906 dw_die_ref type_die = lookup_type_die (type);
19907 dw_die_ref scope_die = 0;
19908 int nested = 0;
19909 int complete = (TYPE_SIZE (type)
19910 && (! TYPE_STUB_DECL (type)
19911 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19912 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19913 complete = complete && should_emit_struct_debug (type, usage);
19915 if (type_die && ! complete)
19916 return;
19918 if (TYPE_CONTEXT (type) != NULL_TREE
19919 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19920 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19921 nested = 1;
19923 scope_die = scope_die_for (type, context_die);
19925 /* Generate child dies for template paramaters. */
19926 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19927 schedule_generic_params_dies_gen (type);
19929 if (! type_die || (nested && is_cu_die (scope_die)))
19930 /* First occurrence of type or toplevel definition of nested class. */
19932 dw_die_ref old_die = type_die;
19934 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19935 ? record_type_tag (type) : DW_TAG_union_type,
19936 scope_die, type);
19937 equate_type_number_to_die (type, type_die);
19938 if (old_die)
19939 add_AT_specification (type_die, old_die);
19940 else
19941 add_name_attribute (type_die, type_tag (type));
19943 else
19944 remove_AT (type_die, DW_AT_declaration);
19946 /* If this type has been completed, then give it a byte_size attribute and
19947 then give a list of members. */
19948 if (complete && !ns_decl)
19950 /* Prevent infinite recursion in cases where the type of some member of
19951 this type is expressed in terms of this type itself. */
19952 TREE_ASM_WRITTEN (type) = 1;
19953 add_byte_size_attribute (type_die, type);
19954 if (TYPE_STUB_DECL (type) != NULL_TREE)
19956 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19957 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19960 /* If the first reference to this type was as the return type of an
19961 inline function, then it may not have a parent. Fix this now. */
19962 if (type_die->die_parent == NULL)
19963 add_child_die (scope_die, type_die);
19965 push_decl_scope (type);
19966 gen_member_die (type, type_die);
19967 pop_decl_scope ();
19969 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19970 if (TYPE_ARTIFICIAL (type))
19971 add_AT_flag (type_die, DW_AT_artificial, 1);
19973 /* GNU extension: Record what type our vtable lives in. */
19974 if (TYPE_VFIELD (type))
19976 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19978 gen_type_die (vtype, context_die);
19979 add_AT_die_ref (type_die, DW_AT_containing_type,
19980 lookup_type_die (vtype));
19983 else
19985 add_AT_flag (type_die, DW_AT_declaration, 1);
19987 /* We don't need to do this for function-local types. */
19988 if (TYPE_STUB_DECL (type)
19989 && ! decl_function_context (TYPE_STUB_DECL (type)))
19990 vec_safe_push (incomplete_types, type);
19993 if (get_AT (type_die, DW_AT_name))
19994 add_pubtype (type, type_die);
19997 /* Generate a DIE for a subroutine _type_. */
19999 static void
20000 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20002 tree return_type = TREE_TYPE (type);
20003 dw_die_ref subr_die
20004 = new_die (DW_TAG_subroutine_type,
20005 scope_die_for (type, context_die), type);
20007 equate_type_number_to_die (type, subr_die);
20008 add_prototyped_attribute (subr_die, type);
20009 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20010 gen_formal_types_die (type, subr_die);
20012 if (get_AT (subr_die, DW_AT_name))
20013 add_pubtype (type, subr_die);
20016 /* Generate a DIE for a type definition. */
20018 static void
20019 gen_typedef_die (tree decl, dw_die_ref context_die)
20021 dw_die_ref type_die;
20022 tree origin;
20024 if (TREE_ASM_WRITTEN (decl))
20025 return;
20027 TREE_ASM_WRITTEN (decl) = 1;
20028 type_die = new_die (DW_TAG_typedef, context_die, decl);
20029 origin = decl_ultimate_origin (decl);
20030 if (origin != NULL)
20031 add_abstract_origin_attribute (type_die, origin);
20032 else
20034 tree type;
20036 add_name_and_src_coords_attributes (type_die, decl);
20037 if (DECL_ORIGINAL_TYPE (decl))
20039 type = DECL_ORIGINAL_TYPE (decl);
20041 gcc_assert (type != TREE_TYPE (decl));
20042 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20044 else
20046 type = TREE_TYPE (decl);
20048 if (is_naming_typedef_decl (TYPE_NAME (type)))
20050 /* Here, we are in the case of decl being a typedef naming
20051 an anonymous type, e.g:
20052 typedef struct {...} foo;
20053 In that case TREE_TYPE (decl) is not a typedef variant
20054 type and TYPE_NAME of the anonymous type is set to the
20055 TYPE_DECL of the typedef. This construct is emitted by
20056 the C++ FE.
20058 TYPE is the anonymous struct named by the typedef
20059 DECL. As we need the DW_AT_type attribute of the
20060 DW_TAG_typedef to point to the DIE of TYPE, let's
20061 generate that DIE right away. add_type_attribute
20062 called below will then pick (via lookup_type_die) that
20063 anonymous struct DIE. */
20064 if (!TREE_ASM_WRITTEN (type))
20065 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20067 /* This is a GNU Extension. We are adding a
20068 DW_AT_linkage_name attribute to the DIE of the
20069 anonymous struct TYPE. The value of that attribute
20070 is the name of the typedef decl naming the anonymous
20071 struct. This greatly eases the work of consumers of
20072 this debug info. */
20073 add_linkage_attr (lookup_type_die (type), decl);
20077 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20079 if (is_naming_typedef_decl (decl))
20080 /* We want that all subsequent calls to lookup_type_die with
20081 TYPE in argument yield the DW_TAG_typedef we have just
20082 created. */
20083 equate_type_number_to_die (type, type_die);
20085 add_accessibility_attribute (type_die, decl);
20088 if (DECL_ABSTRACT_P (decl))
20089 equate_decl_number_to_die (decl, type_die);
20091 if (get_AT (type_die, DW_AT_name))
20092 add_pubtype (decl, type_die);
20095 /* Generate a DIE for a struct, class, enum or union type. */
20097 static void
20098 gen_tagged_type_die (tree type,
20099 dw_die_ref context_die,
20100 enum debug_info_usage usage)
20102 int need_pop;
20104 if (type == NULL_TREE
20105 || !is_tagged_type (type))
20106 return;
20108 /* If this is a nested type whose containing class hasn't been written
20109 out yet, writing it out will cover this one, too. This does not apply
20110 to instantiations of member class templates; they need to be added to
20111 the containing class as they are generated. FIXME: This hurts the
20112 idea of combining type decls from multiple TUs, since we can't predict
20113 what set of template instantiations we'll get. */
20114 if (TYPE_CONTEXT (type)
20115 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20116 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20118 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20120 if (TREE_ASM_WRITTEN (type))
20121 return;
20123 /* If that failed, attach ourselves to the stub. */
20124 push_decl_scope (TYPE_CONTEXT (type));
20125 context_die = lookup_type_die (TYPE_CONTEXT (type));
20126 need_pop = 1;
20128 else if (TYPE_CONTEXT (type) != NULL_TREE
20129 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20131 /* If this type is local to a function that hasn't been written
20132 out yet, use a NULL context for now; it will be fixed up in
20133 decls_for_scope. */
20134 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20135 /* A declaration DIE doesn't count; nested types need to go in the
20136 specification. */
20137 if (context_die && is_declaration_die (context_die))
20138 context_die = NULL;
20139 need_pop = 0;
20141 else
20143 context_die = declare_in_namespace (type, context_die);
20144 need_pop = 0;
20147 if (TREE_CODE (type) == ENUMERAL_TYPE)
20149 /* This might have been written out by the call to
20150 declare_in_namespace. */
20151 if (!TREE_ASM_WRITTEN (type))
20152 gen_enumeration_type_die (type, context_die);
20154 else
20155 gen_struct_or_union_type_die (type, context_die, usage);
20157 if (need_pop)
20158 pop_decl_scope ();
20160 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20161 it up if it is ever completed. gen_*_type_die will set it for us
20162 when appropriate. */
20165 /* Generate a type description DIE. */
20167 static void
20168 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20169 enum debug_info_usage usage)
20171 struct array_descr_info info;
20173 if (type == NULL_TREE || type == error_mark_node)
20174 return;
20176 if (TYPE_NAME (type) != NULL_TREE
20177 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20178 && is_redundant_typedef (TYPE_NAME (type))
20179 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20180 /* The DECL of this type is a typedef we don't want to emit debug
20181 info for but we want debug info for its underlying typedef.
20182 This can happen for e.g, the injected-class-name of a C++
20183 type. */
20184 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20186 /* If TYPE is a typedef type variant, let's generate debug info
20187 for the parent typedef which TYPE is a type of. */
20188 if (typedef_variant_p (type))
20190 if (TREE_ASM_WRITTEN (type))
20191 return;
20193 /* Prevent broken recursion; we can't hand off to the same type. */
20194 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20196 /* Give typedefs the right scope. */
20197 context_die = scope_die_for (type, context_die);
20199 TREE_ASM_WRITTEN (type) = 1;
20201 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20202 return;
20205 /* If type is an anonymous tagged type named by a typedef, let's
20206 generate debug info for the typedef. */
20207 if (is_naming_typedef_decl (TYPE_NAME (type)))
20209 /* Use the DIE of the containing namespace as the parent DIE of
20210 the type description DIE we want to generate. */
20211 if (DECL_CONTEXT (TYPE_NAME (type))
20212 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20213 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20215 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20216 return;
20219 /* We are going to output a DIE to represent the unqualified version
20220 of this type (i.e. without any const or volatile qualifiers) so
20221 get the main variant (i.e. the unqualified version) of this type
20222 now. (Vectors are special because the debugging info is in the
20223 cloned type itself). */
20224 if (TREE_CODE (type) != VECTOR_TYPE)
20225 type = type_main_variant (type);
20227 /* If this is an array type with hidden descriptor, handle it first. */
20228 if (!TREE_ASM_WRITTEN (type)
20229 && lang_hooks.types.get_array_descr_info)
20231 memset (&info, 0, sizeof (info));
20232 if (lang_hooks.types.get_array_descr_info (type, &info))
20234 gen_descr_array_type_die (type, &info, context_die);
20235 TREE_ASM_WRITTEN (type) = 1;
20236 return;
20240 if (TREE_ASM_WRITTEN (type))
20241 return;
20243 switch (TREE_CODE (type))
20245 case ERROR_MARK:
20246 break;
20248 case POINTER_TYPE:
20249 case REFERENCE_TYPE:
20250 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20251 ensures that the gen_type_die recursion will terminate even if the
20252 type is recursive. Recursive types are possible in Ada. */
20253 /* ??? We could perhaps do this for all types before the switch
20254 statement. */
20255 TREE_ASM_WRITTEN (type) = 1;
20257 /* For these types, all that is required is that we output a DIE (or a
20258 set of DIEs) to represent the "basis" type. */
20259 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20260 DINFO_USAGE_IND_USE);
20261 break;
20263 case OFFSET_TYPE:
20264 /* This code is used for C++ pointer-to-data-member types.
20265 Output a description of the relevant class type. */
20266 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20267 DINFO_USAGE_IND_USE);
20269 /* Output a description of the type of the object pointed to. */
20270 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20271 DINFO_USAGE_IND_USE);
20273 /* Now output a DIE to represent this pointer-to-data-member type
20274 itself. */
20275 gen_ptr_to_mbr_type_die (type, context_die);
20276 break;
20278 case FUNCTION_TYPE:
20279 /* Force out return type (in case it wasn't forced out already). */
20280 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20281 DINFO_USAGE_DIR_USE);
20282 gen_subroutine_type_die (type, context_die);
20283 break;
20285 case METHOD_TYPE:
20286 /* Force out return type (in case it wasn't forced out already). */
20287 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20288 DINFO_USAGE_DIR_USE);
20289 gen_subroutine_type_die (type, context_die);
20290 break;
20292 case ARRAY_TYPE:
20293 gen_array_type_die (type, context_die);
20294 break;
20296 case VECTOR_TYPE:
20297 gen_array_type_die (type, context_die);
20298 break;
20300 case ENUMERAL_TYPE:
20301 case RECORD_TYPE:
20302 case UNION_TYPE:
20303 case QUAL_UNION_TYPE:
20304 gen_tagged_type_die (type, context_die, usage);
20305 return;
20307 case VOID_TYPE:
20308 case INTEGER_TYPE:
20309 case REAL_TYPE:
20310 case FIXED_POINT_TYPE:
20311 case COMPLEX_TYPE:
20312 case BOOLEAN_TYPE:
20313 case POINTER_BOUNDS_TYPE:
20314 /* No DIEs needed for fundamental types. */
20315 break;
20317 case NULLPTR_TYPE:
20318 case LANG_TYPE:
20319 /* Just use DW_TAG_unspecified_type. */
20321 dw_die_ref type_die = lookup_type_die (type);
20322 if (type_die == NULL)
20324 tree name = TYPE_IDENTIFIER (type);
20325 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20326 type);
20327 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20328 equate_type_number_to_die (type, type_die);
20331 break;
20333 default:
20334 if (is_cxx_auto (type))
20336 tree name = TYPE_IDENTIFIER (type);
20337 dw_die_ref *die = (name == get_identifier ("auto")
20338 ? &auto_die : &decltype_auto_die);
20339 if (!*die)
20341 *die = new_die (DW_TAG_unspecified_type,
20342 comp_unit_die (), NULL_TREE);
20343 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20345 equate_type_number_to_die (type, *die);
20346 break;
20348 gcc_unreachable ();
20351 TREE_ASM_WRITTEN (type) = 1;
20354 static void
20355 gen_type_die (tree type, dw_die_ref context_die)
20357 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20360 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20361 things which are local to the given block. */
20363 static void
20364 gen_block_die (tree stmt, dw_die_ref context_die)
20366 int must_output_die = 0;
20367 bool inlined_func;
20369 /* Ignore blocks that are NULL. */
20370 if (stmt == NULL_TREE)
20371 return;
20373 inlined_func = inlined_function_outer_scope_p (stmt);
20375 /* If the block is one fragment of a non-contiguous block, do not
20376 process the variables, since they will have been done by the
20377 origin block. Do process subblocks. */
20378 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20380 tree sub;
20382 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20383 gen_block_die (sub, context_die);
20385 return;
20388 /* Determine if we need to output any Dwarf DIEs at all to represent this
20389 block. */
20390 if (inlined_func)
20391 /* The outer scopes for inlinings *must* always be represented. We
20392 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20393 must_output_die = 1;
20394 else
20396 /* Determine if this block directly contains any "significant"
20397 local declarations which we will need to output DIEs for. */
20398 if (debug_info_level > DINFO_LEVEL_TERSE)
20399 /* We are not in terse mode so *any* local declaration counts
20400 as being a "significant" one. */
20401 must_output_die = ((BLOCK_VARS (stmt) != NULL
20402 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20403 && (TREE_USED (stmt)
20404 || TREE_ASM_WRITTEN (stmt)
20405 || BLOCK_ABSTRACT (stmt)));
20406 else if ((TREE_USED (stmt)
20407 || TREE_ASM_WRITTEN (stmt)
20408 || BLOCK_ABSTRACT (stmt))
20409 && !dwarf2out_ignore_block (stmt))
20410 must_output_die = 1;
20413 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20414 DIE for any block which contains no significant local declarations at
20415 all. Rather, in such cases we just call `decls_for_scope' so that any
20416 needed Dwarf info for any sub-blocks will get properly generated. Note
20417 that in terse mode, our definition of what constitutes a "significant"
20418 local declaration gets restricted to include only inlined function
20419 instances and local (nested) function definitions. */
20420 if (must_output_die)
20422 if (inlined_func)
20424 /* If STMT block is abstract, that means we have been called
20425 indirectly from dwarf2out_abstract_function.
20426 That function rightfully marks the descendent blocks (of
20427 the abstract function it is dealing with) as being abstract,
20428 precisely to prevent us from emitting any
20429 DW_TAG_inlined_subroutine DIE as a descendent
20430 of an abstract function instance. So in that case, we should
20431 not call gen_inlined_subroutine_die.
20433 Later though, when cgraph asks dwarf2out to emit info
20434 for the concrete instance of the function decl into which
20435 the concrete instance of STMT got inlined, the later will lead
20436 to the generation of a DW_TAG_inlined_subroutine DIE. */
20437 if (! BLOCK_ABSTRACT (stmt))
20438 gen_inlined_subroutine_die (stmt, context_die);
20440 else
20441 gen_lexical_block_die (stmt, context_die);
20443 else
20444 decls_for_scope (stmt, context_die);
20447 /* Process variable DECL (or variable with origin ORIGIN) within
20448 block STMT and add it to CONTEXT_DIE. */
20449 static void
20450 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20452 dw_die_ref die;
20453 tree decl_or_origin = decl ? decl : origin;
20455 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20456 die = lookup_decl_die (decl_or_origin);
20457 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20458 && TYPE_DECL_IS_STUB (decl_or_origin))
20459 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20460 else
20461 die = NULL;
20463 if (die != NULL && die->die_parent == NULL)
20464 add_child_die (context_die, die);
20465 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20466 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20467 stmt, context_die);
20468 else
20469 gen_decl_die (decl, origin, context_die);
20472 /* Generate all of the decls declared within a given scope and (recursively)
20473 all of its sub-blocks. */
20475 static void
20476 decls_for_scope (tree stmt, dw_die_ref context_die)
20478 tree decl;
20479 unsigned int i;
20480 tree subblocks;
20482 /* Ignore NULL blocks. */
20483 if (stmt == NULL_TREE)
20484 return;
20486 /* Output the DIEs to represent all of the data objects and typedefs
20487 declared directly within this block but not within any nested
20488 sub-blocks. Also, nested function and tag DIEs have been
20489 generated with a parent of NULL; fix that up now. We don't
20490 have to do this if we're at -g1. */
20491 if (debug_info_level > DINFO_LEVEL_TERSE)
20493 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20494 process_scope_var (stmt, decl, NULL_TREE, context_die);
20495 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20496 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20497 context_die);
20500 /* Even if we're at -g1, we need to process the subblocks in order to get
20501 inlined call information. */
20503 /* Output the DIEs to represent all sub-blocks (and the items declared
20504 therein) of this block. */
20505 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20506 subblocks != NULL;
20507 subblocks = BLOCK_CHAIN (subblocks))
20508 gen_block_die (subblocks, context_die);
20511 /* Is this a typedef we can avoid emitting? */
20513 static inline int
20514 is_redundant_typedef (const_tree decl)
20516 if (TYPE_DECL_IS_STUB (decl))
20517 return 1;
20519 if (DECL_ARTIFICIAL (decl)
20520 && DECL_CONTEXT (decl)
20521 && is_tagged_type (DECL_CONTEXT (decl))
20522 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20523 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20524 /* Also ignore the artificial member typedef for the class name. */
20525 return 1;
20527 return 0;
20530 /* Return TRUE if TYPE is a typedef that names a type for linkage
20531 purposes. This kind of typedefs is produced by the C++ FE for
20532 constructs like:
20534 typedef struct {...} foo;
20536 In that case, there is no typedef variant type produced for foo.
20537 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20538 struct type. */
20540 static bool
20541 is_naming_typedef_decl (const_tree decl)
20543 if (decl == NULL_TREE
20544 || TREE_CODE (decl) != TYPE_DECL
20545 || !is_tagged_type (TREE_TYPE (decl))
20546 || DECL_IS_BUILTIN (decl)
20547 || is_redundant_typedef (decl)
20548 /* It looks like Ada produces TYPE_DECLs that are very similar
20549 to C++ naming typedefs but that have different
20550 semantics. Let's be specific to c++ for now. */
20551 || !is_cxx ())
20552 return FALSE;
20554 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20555 && TYPE_NAME (TREE_TYPE (decl)) == decl
20556 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20557 != TYPE_NAME (TREE_TYPE (decl))));
20560 /* Returns the DIE for a context. */
20562 static inline dw_die_ref
20563 get_context_die (tree context)
20565 if (context)
20567 /* Find die that represents this context. */
20568 if (TYPE_P (context))
20570 context = TYPE_MAIN_VARIANT (context);
20571 return strip_naming_typedef (context, force_type_die (context));
20573 else
20574 return force_decl_die (context);
20576 return comp_unit_die ();
20579 /* Returns the DIE for decl. A DIE will always be returned. */
20581 static dw_die_ref
20582 force_decl_die (tree decl)
20584 dw_die_ref decl_die;
20585 unsigned saved_external_flag;
20586 tree save_fn = NULL_TREE;
20587 decl_die = lookup_decl_die (decl);
20588 if (!decl_die)
20590 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20592 decl_die = lookup_decl_die (decl);
20593 if (decl_die)
20594 return decl_die;
20596 switch (TREE_CODE (decl))
20598 case FUNCTION_DECL:
20599 /* Clear current_function_decl, so that gen_subprogram_die thinks
20600 that this is a declaration. At this point, we just want to force
20601 declaration die. */
20602 save_fn = current_function_decl;
20603 current_function_decl = NULL_TREE;
20604 gen_subprogram_die (decl, context_die);
20605 current_function_decl = save_fn;
20606 break;
20608 case VAR_DECL:
20609 /* Set external flag to force declaration die. Restore it after
20610 gen_decl_die() call. */
20611 saved_external_flag = DECL_EXTERNAL (decl);
20612 DECL_EXTERNAL (decl) = 1;
20613 gen_decl_die (decl, NULL, context_die);
20614 DECL_EXTERNAL (decl) = saved_external_flag;
20615 break;
20617 case NAMESPACE_DECL:
20618 if (dwarf_version >= 3 || !dwarf_strict)
20619 dwarf2out_decl (decl);
20620 else
20621 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20622 decl_die = comp_unit_die ();
20623 break;
20625 case TRANSLATION_UNIT_DECL:
20626 decl_die = comp_unit_die ();
20627 break;
20629 default:
20630 gcc_unreachable ();
20633 /* We should be able to find the DIE now. */
20634 if (!decl_die)
20635 decl_die = lookup_decl_die (decl);
20636 gcc_assert (decl_die);
20639 return decl_die;
20642 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20643 always returned. */
20645 static dw_die_ref
20646 force_type_die (tree type)
20648 dw_die_ref type_die;
20650 type_die = lookup_type_die (type);
20651 if (!type_die)
20653 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20655 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20656 context_die);
20657 gcc_assert (type_die);
20659 return type_die;
20662 /* Force out any required namespaces to be able to output DECL,
20663 and return the new context_die for it, if it's changed. */
20665 static dw_die_ref
20666 setup_namespace_context (tree thing, dw_die_ref context_die)
20668 tree context = (DECL_P (thing)
20669 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20670 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20671 /* Force out the namespace. */
20672 context_die = force_decl_die (context);
20674 return context_die;
20677 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20678 type) within its namespace, if appropriate.
20680 For compatibility with older debuggers, namespace DIEs only contain
20681 declarations; all definitions are emitted at CU scope. */
20683 static dw_die_ref
20684 declare_in_namespace (tree thing, dw_die_ref context_die)
20686 dw_die_ref ns_context;
20688 if (debug_info_level <= DINFO_LEVEL_TERSE)
20689 return context_die;
20691 /* External declarations in the local scope only need to be emitted
20692 once, not once in the namespace and once in the scope.
20694 This avoids declaring the `extern' below in the
20695 namespace DIE as well as in the innermost scope:
20697 namespace S
20699 int i=5;
20700 int foo()
20702 int i=8;
20703 extern int i;
20704 return i;
20708 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20709 return context_die;
20711 /* If this decl is from an inlined function, then don't try to emit it in its
20712 namespace, as we will get confused. It would have already been emitted
20713 when the abstract instance of the inline function was emitted anyways. */
20714 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20715 return context_die;
20717 ns_context = setup_namespace_context (thing, context_die);
20719 if (ns_context != context_die)
20721 if (is_fortran ())
20722 return ns_context;
20723 if (DECL_P (thing))
20724 gen_decl_die (thing, NULL, ns_context);
20725 else
20726 gen_type_die (thing, ns_context);
20728 return context_die;
20731 /* Generate a DIE for a namespace or namespace alias. */
20733 static void
20734 gen_namespace_die (tree decl, dw_die_ref context_die)
20736 dw_die_ref namespace_die;
20738 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20739 they are an alias of. */
20740 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20742 /* Output a real namespace or module. */
20743 context_die = setup_namespace_context (decl, comp_unit_die ());
20744 namespace_die = new_die (is_fortran ()
20745 ? DW_TAG_module : DW_TAG_namespace,
20746 context_die, decl);
20747 /* For Fortran modules defined in different CU don't add src coords. */
20748 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20750 const char *name = dwarf2_name (decl, 0);
20751 if (name)
20752 add_name_attribute (namespace_die, name);
20754 else
20755 add_name_and_src_coords_attributes (namespace_die, decl);
20756 if (DECL_EXTERNAL (decl))
20757 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20758 equate_decl_number_to_die (decl, namespace_die);
20760 else
20762 /* Output a namespace alias. */
20764 /* Force out the namespace we are an alias of, if necessary. */
20765 dw_die_ref origin_die
20766 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20768 if (DECL_FILE_SCOPE_P (decl)
20769 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20770 context_die = setup_namespace_context (decl, comp_unit_die ());
20771 /* Now create the namespace alias DIE. */
20772 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20773 add_name_and_src_coords_attributes (namespace_die, decl);
20774 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20775 equate_decl_number_to_die (decl, namespace_die);
20777 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20778 if (want_pubnames ())
20779 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20782 /* Generate Dwarf debug information for a decl described by DECL.
20783 The return value is currently only meaningful for PARM_DECLs,
20784 for all other decls it returns NULL. */
20786 static dw_die_ref
20787 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20789 tree decl_or_origin = decl ? decl : origin;
20790 tree class_origin = NULL, ultimate_origin;
20792 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20793 return NULL;
20795 /* Ignore pointer bounds decls. */
20796 if (DECL_P (decl_or_origin)
20797 && TREE_TYPE (decl_or_origin)
20798 && POINTER_BOUNDS_P (decl_or_origin))
20799 return NULL;
20801 switch (TREE_CODE (decl_or_origin))
20803 case ERROR_MARK:
20804 break;
20806 case CONST_DECL:
20807 if (!is_fortran () && !is_ada ())
20809 /* The individual enumerators of an enum type get output when we output
20810 the Dwarf representation of the relevant enum type itself. */
20811 break;
20814 /* Emit its type. */
20815 gen_type_die (TREE_TYPE (decl), context_die);
20817 /* And its containing namespace. */
20818 context_die = declare_in_namespace (decl, context_die);
20820 gen_const_die (decl, context_die);
20821 break;
20823 case FUNCTION_DECL:
20824 /* Don't output any DIEs to represent mere function declarations,
20825 unless they are class members or explicit block externs. */
20826 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20827 && DECL_FILE_SCOPE_P (decl_or_origin)
20828 && (current_function_decl == NULL_TREE
20829 || DECL_ARTIFICIAL (decl_or_origin)))
20830 break;
20832 #if 0
20833 /* FIXME */
20834 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20835 on local redeclarations of global functions. That seems broken. */
20836 if (current_function_decl != decl)
20837 /* This is only a declaration. */;
20838 #endif
20840 /* If we're emitting a clone, emit info for the abstract instance. */
20841 if (origin || DECL_ORIGIN (decl) != decl)
20842 dwarf2out_abstract_function (origin
20843 ? DECL_ORIGIN (origin)
20844 : DECL_ABSTRACT_ORIGIN (decl));
20846 /* If we're emitting an out-of-line copy of an inline function,
20847 emit info for the abstract instance and set up to refer to it. */
20848 else if (cgraph_function_possibly_inlined_p (decl)
20849 && ! DECL_ABSTRACT_P (decl)
20850 && ! class_or_namespace_scope_p (context_die)
20851 /* dwarf2out_abstract_function won't emit a die if this is just
20852 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20853 that case, because that works only if we have a die. */
20854 && DECL_INITIAL (decl) != NULL_TREE)
20856 dwarf2out_abstract_function (decl);
20857 set_decl_origin_self (decl);
20860 /* Otherwise we're emitting the primary DIE for this decl. */
20861 else if (debug_info_level > DINFO_LEVEL_TERSE)
20863 /* Before we describe the FUNCTION_DECL itself, make sure that we
20864 have its containing type. */
20865 if (!origin)
20866 origin = decl_class_context (decl);
20867 if (origin != NULL_TREE)
20868 gen_type_die (origin, context_die);
20870 /* And its return type. */
20871 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20873 /* And its virtual context. */
20874 if (DECL_VINDEX (decl) != NULL_TREE)
20875 gen_type_die (DECL_CONTEXT (decl), context_die);
20877 /* Make sure we have a member DIE for decl. */
20878 if (origin != NULL_TREE)
20879 gen_type_die_for_member (origin, decl, context_die);
20881 /* And its containing namespace. */
20882 context_die = declare_in_namespace (decl, context_die);
20885 /* Now output a DIE to represent the function itself. */
20886 if (decl)
20887 gen_subprogram_die (decl, context_die);
20888 break;
20890 case TYPE_DECL:
20891 /* If we are in terse mode, don't generate any DIEs to represent any
20892 actual typedefs. */
20893 if (debug_info_level <= DINFO_LEVEL_TERSE)
20894 break;
20896 /* In the special case of a TYPE_DECL node representing the declaration
20897 of some type tag, if the given TYPE_DECL is marked as having been
20898 instantiated from some other (original) TYPE_DECL node (e.g. one which
20899 was generated within the original definition of an inline function) we
20900 used to generate a special (abbreviated) DW_TAG_structure_type,
20901 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20902 should be actually referencing those DIEs, as variable DIEs with that
20903 type would be emitted already in the abstract origin, so it was always
20904 removed during unused type prunning. Don't add anything in this
20905 case. */
20906 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20907 break;
20909 if (is_redundant_typedef (decl))
20910 gen_type_die (TREE_TYPE (decl), context_die);
20911 else
20912 /* Output a DIE to represent the typedef itself. */
20913 gen_typedef_die (decl, context_die);
20914 break;
20916 case LABEL_DECL:
20917 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20918 gen_label_die (decl, context_die);
20919 break;
20921 case VAR_DECL:
20922 case RESULT_DECL:
20923 /* If we are in terse mode, don't generate any DIEs to represent any
20924 variable declarations or definitions. */
20925 if (debug_info_level <= DINFO_LEVEL_TERSE)
20926 break;
20928 /* Output any DIEs that are needed to specify the type of this data
20929 object. */
20930 if (decl_by_reference_p (decl_or_origin))
20931 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20932 else
20933 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20935 /* And its containing type. */
20936 class_origin = decl_class_context (decl_or_origin);
20937 if (class_origin != NULL_TREE)
20938 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20940 /* And its containing namespace. */
20941 context_die = declare_in_namespace (decl_or_origin, context_die);
20943 /* Now output the DIE to represent the data object itself. This gets
20944 complicated because of the possibility that the VAR_DECL really
20945 represents an inlined instance of a formal parameter for an inline
20946 function. */
20947 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20948 if (ultimate_origin != NULL_TREE
20949 && TREE_CODE (ultimate_origin) == PARM_DECL)
20950 gen_formal_parameter_die (decl, origin,
20951 true /* Emit name attribute. */,
20952 context_die);
20953 else
20954 gen_variable_die (decl, origin, context_die);
20955 break;
20957 case FIELD_DECL:
20958 /* Ignore the nameless fields that are used to skip bits but handle C++
20959 anonymous unions and structs. */
20960 if (DECL_NAME (decl) != NULL_TREE
20961 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20962 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20964 gen_type_die (member_declared_type (decl), context_die);
20965 gen_field_die (decl, context_die);
20967 break;
20969 case PARM_DECL:
20970 if (DECL_BY_REFERENCE (decl_or_origin))
20971 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20972 else
20973 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20974 return gen_formal_parameter_die (decl, origin,
20975 true /* Emit name attribute. */,
20976 context_die);
20978 case NAMESPACE_DECL:
20979 case IMPORTED_DECL:
20980 if (dwarf_version >= 3 || !dwarf_strict)
20981 gen_namespace_die (decl, context_die);
20982 break;
20984 case NAMELIST_DECL:
20985 gen_namelist_decl (DECL_NAME (decl), context_die,
20986 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20987 break;
20989 default:
20990 /* Probably some frontend-internal decl. Assume we don't care. */
20991 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20992 break;
20995 return NULL;
20998 /* Output debug information for global decl DECL. Called from toplev.c after
20999 compilation proper has finished. */
21001 static void
21002 dwarf2out_global_decl (tree decl)
21004 /* Output DWARF2 information for file-scope tentative data object
21005 declarations, file-scope (extern) function declarations (which
21006 had no corresponding body) and file-scope tagged type declarations
21007 and definitions which have not yet been forced out. */
21008 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21009 && !POINTER_BOUNDS_P (decl))
21010 dwarf2out_decl (decl);
21013 /* Output debug information for type decl DECL. Called from toplev.c
21014 and from language front ends (to record built-in types). */
21015 static void
21016 dwarf2out_type_decl (tree decl, int local)
21018 if (!local)
21019 dwarf2out_decl (decl);
21022 /* Output debug information for imported module or decl DECL.
21023 NAME is non-NULL name in the lexical block if the decl has been renamed.
21024 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21025 that DECL belongs to.
21026 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21027 static void
21028 dwarf2out_imported_module_or_decl_1 (tree decl,
21029 tree name,
21030 tree lexical_block,
21031 dw_die_ref lexical_block_die)
21033 expanded_location xloc;
21034 dw_die_ref imported_die = NULL;
21035 dw_die_ref at_import_die;
21037 if (TREE_CODE (decl) == IMPORTED_DECL)
21039 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21040 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21041 gcc_assert (decl);
21043 else
21044 xloc = expand_location (input_location);
21046 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21048 at_import_die = force_type_die (TREE_TYPE (decl));
21049 /* For namespace N { typedef void T; } using N::T; base_type_die
21050 returns NULL, but DW_TAG_imported_declaration requires
21051 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21052 if (!at_import_die)
21054 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21055 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21056 at_import_die = lookup_type_die (TREE_TYPE (decl));
21057 gcc_assert (at_import_die);
21060 else
21062 at_import_die = lookup_decl_die (decl);
21063 if (!at_import_die)
21065 /* If we're trying to avoid duplicate debug info, we may not have
21066 emitted the member decl for this field. Emit it now. */
21067 if (TREE_CODE (decl) == FIELD_DECL)
21069 tree type = DECL_CONTEXT (decl);
21071 if (TYPE_CONTEXT (type)
21072 && TYPE_P (TYPE_CONTEXT (type))
21073 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21074 DINFO_USAGE_DIR_USE))
21075 return;
21076 gen_type_die_for_member (type, decl,
21077 get_context_die (TYPE_CONTEXT (type)));
21079 if (TREE_CODE (decl) == NAMELIST_DECL)
21080 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21081 get_context_die (DECL_CONTEXT (decl)),
21082 NULL_TREE);
21083 else
21084 at_import_die = force_decl_die (decl);
21088 if (TREE_CODE (decl) == NAMESPACE_DECL)
21090 if (dwarf_version >= 3 || !dwarf_strict)
21091 imported_die = new_die (DW_TAG_imported_module,
21092 lexical_block_die,
21093 lexical_block);
21094 else
21095 return;
21097 else
21098 imported_die = new_die (DW_TAG_imported_declaration,
21099 lexical_block_die,
21100 lexical_block);
21102 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21103 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21104 if (name)
21105 add_AT_string (imported_die, DW_AT_name,
21106 IDENTIFIER_POINTER (name));
21107 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21110 /* Output debug information for imported module or decl DECL.
21111 NAME is non-NULL name in context if the decl has been renamed.
21112 CHILD is true if decl is one of the renamed decls as part of
21113 importing whole module. */
21115 static void
21116 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21117 bool child)
21119 /* dw_die_ref at_import_die; */
21120 dw_die_ref scope_die;
21122 if (debug_info_level <= DINFO_LEVEL_TERSE)
21123 return;
21125 gcc_assert (decl);
21127 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21128 We need decl DIE for reference and scope die. First, get DIE for the decl
21129 itself. */
21131 /* Get the scope die for decl context. Use comp_unit_die for global module
21132 or decl. If die is not found for non globals, force new die. */
21133 if (context
21134 && TYPE_P (context)
21135 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21136 return;
21138 if (!(dwarf_version >= 3 || !dwarf_strict))
21139 return;
21141 scope_die = get_context_die (context);
21143 if (child)
21145 gcc_assert (scope_die->die_child);
21146 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21147 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21148 scope_die = scope_die->die_child;
21151 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21152 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21156 /* Output debug information for namelists. */
21158 static dw_die_ref
21159 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21161 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21162 tree value;
21163 unsigned i;
21165 if (debug_info_level <= DINFO_LEVEL_TERSE)
21166 return NULL;
21168 gcc_assert (scope_die != NULL);
21169 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21170 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21172 /* If there are no item_decls, we have a nondefining namelist, e.g.
21173 with USE association; hence, set DW_AT_declaration. */
21174 if (item_decls == NULL_TREE)
21176 add_AT_flag (nml_die, DW_AT_declaration, 1);
21177 return nml_die;
21180 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21182 nml_item_ref_die = lookup_decl_die (value);
21183 if (!nml_item_ref_die)
21184 nml_item_ref_die = force_decl_die (value);
21186 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21187 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21189 return nml_die;
21193 /* Write the debugging output for DECL. */
21195 static void
21196 dwarf2out_decl (tree decl)
21198 dw_die_ref context_die = comp_unit_die ();
21200 switch (TREE_CODE (decl))
21202 case ERROR_MARK:
21203 return;
21205 case FUNCTION_DECL:
21206 /* What we would really like to do here is to filter out all mere
21207 file-scope declarations of file-scope functions which are never
21208 referenced later within this translation unit (and keep all of ones
21209 that *are* referenced later on) but we aren't clairvoyant, so we have
21210 no idea which functions will be referenced in the future (i.e. later
21211 on within the current translation unit). So here we just ignore all
21212 file-scope function declarations which are not also definitions. If
21213 and when the debugger needs to know something about these functions,
21214 it will have to hunt around and find the DWARF information associated
21215 with the definition of the function.
21217 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21218 nodes represent definitions and which ones represent mere
21219 declarations. We have to check DECL_INITIAL instead. That's because
21220 the C front-end supports some weird semantics for "extern inline"
21221 function definitions. These can get inlined within the current
21222 translation unit (and thus, we need to generate Dwarf info for their
21223 abstract instances so that the Dwarf info for the concrete inlined
21224 instances can have something to refer to) but the compiler never
21225 generates any out-of-lines instances of such things (despite the fact
21226 that they *are* definitions).
21228 The important point is that the C front-end marks these "extern
21229 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21230 them anyway. Note that the C++ front-end also plays some similar games
21231 for inline function definitions appearing within include files which
21232 also contain `#pragma interface' pragmas.
21234 If we are called from dwarf2out_abstract_function output a DIE
21235 anyway. We can end up here this way with early inlining and LTO
21236 where the inlined function is output in a different LTRANS unit
21237 or not at all. */
21238 if (DECL_INITIAL (decl) == NULL_TREE
21239 && ! DECL_ABSTRACT_P (decl))
21240 return;
21242 /* If we're a nested function, initially use a parent of NULL; if we're
21243 a plain function, this will be fixed up in decls_for_scope. If
21244 we're a method, it will be ignored, since we already have a DIE. */
21245 if (decl_function_context (decl)
21246 /* But if we're in terse mode, we don't care about scope. */
21247 && debug_info_level > DINFO_LEVEL_TERSE)
21248 context_die = NULL;
21249 break;
21251 case VAR_DECL:
21252 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21253 declaration and if the declaration was never even referenced from
21254 within this entire compilation unit. We suppress these DIEs in
21255 order to save space in the .debug section (by eliminating entries
21256 which are probably useless). Note that we must not suppress
21257 block-local extern declarations (whether used or not) because that
21258 would screw-up the debugger's name lookup mechanism and cause it to
21259 miss things which really ought to be in scope at a given point. */
21260 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21261 return;
21263 /* For local statics lookup proper context die. */
21264 if (TREE_STATIC (decl)
21265 && DECL_CONTEXT (decl)
21266 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21267 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21269 /* If we are in terse mode, don't generate any DIEs to represent any
21270 variable declarations or definitions. */
21271 if (debug_info_level <= DINFO_LEVEL_TERSE)
21272 return;
21273 break;
21275 case CONST_DECL:
21276 if (debug_info_level <= DINFO_LEVEL_TERSE)
21277 return;
21278 if (!is_fortran () && !is_ada ())
21279 return;
21280 if (TREE_STATIC (decl) && decl_function_context (decl))
21281 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21282 break;
21284 case NAMESPACE_DECL:
21285 case IMPORTED_DECL:
21286 if (debug_info_level <= DINFO_LEVEL_TERSE)
21287 return;
21288 if (lookup_decl_die (decl) != NULL)
21289 return;
21290 break;
21292 case TYPE_DECL:
21293 /* Don't emit stubs for types unless they are needed by other DIEs. */
21294 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21295 return;
21297 /* Don't bother trying to generate any DIEs to represent any of the
21298 normal built-in types for the language we are compiling. */
21299 if (DECL_IS_BUILTIN (decl))
21300 return;
21302 /* If we are in terse mode, don't generate any DIEs for types. */
21303 if (debug_info_level <= DINFO_LEVEL_TERSE)
21304 return;
21306 /* If we're a function-scope tag, initially use a parent of NULL;
21307 this will be fixed up in decls_for_scope. */
21308 if (decl_function_context (decl))
21309 context_die = NULL;
21311 break;
21313 case NAMELIST_DECL:
21314 break;
21316 default:
21317 return;
21320 gen_decl_die (decl, NULL, context_die);
21323 /* Write the debugging output for DECL. */
21325 static void
21326 dwarf2out_function_decl (tree decl)
21328 dwarf2out_decl (decl);
21329 call_arg_locations = NULL;
21330 call_arg_loc_last = NULL;
21331 call_site_count = -1;
21332 tail_call_site_count = -1;
21333 block_map.release ();
21334 decl_loc_table->empty ();
21335 cached_dw_loc_list_table->empty ();
21338 /* Output a marker (i.e. a label) for the beginning of the generated code for
21339 a lexical block. */
21341 static void
21342 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21343 unsigned int blocknum)
21345 switch_to_section (current_function_section ());
21346 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21349 /* Output a marker (i.e. a label) for the end of the generated code for a
21350 lexical block. */
21352 static void
21353 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21355 switch_to_section (current_function_section ());
21356 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21359 /* Returns nonzero if it is appropriate not to emit any debugging
21360 information for BLOCK, because it doesn't contain any instructions.
21362 Don't allow this for blocks with nested functions or local classes
21363 as we would end up with orphans, and in the presence of scheduling
21364 we may end up calling them anyway. */
21366 static bool
21367 dwarf2out_ignore_block (const_tree block)
21369 tree decl;
21370 unsigned int i;
21372 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21373 if (TREE_CODE (decl) == FUNCTION_DECL
21374 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21375 return 0;
21376 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21378 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21379 if (TREE_CODE (decl) == FUNCTION_DECL
21380 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21381 return 0;
21384 return 1;
21387 /* Hash table routines for file_hash. */
21389 bool
21390 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21392 return filename_cmp (p1->filename, p2) == 0;
21395 hashval_t
21396 dwarf_file_hasher::hash (dwarf_file_data *p)
21398 return htab_hash_string (p->filename);
21401 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21402 dwarf2out.c) and return its "index". The index of each (known) filename is
21403 just a unique number which is associated with only that one filename. We
21404 need such numbers for the sake of generating labels (in the .debug_sfnames
21405 section) and references to those files numbers (in the .debug_srcinfo
21406 and.debug_macinfo sections). If the filename given as an argument is not
21407 found in our current list, add it to the list and assign it the next
21408 available unique index number. In order to speed up searches, we remember
21409 the index of the filename was looked up last. This handles the majority of
21410 all searches. */
21412 static struct dwarf_file_data *
21413 lookup_filename (const char *file_name)
21415 struct dwarf_file_data * created;
21417 /* Check to see if the file name that was searched on the previous
21418 call matches this file name. If so, return the index. */
21419 if (file_table_last_lookup
21420 && (file_name == file_table_last_lookup->filename
21421 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21422 return file_table_last_lookup;
21424 /* Didn't match the previous lookup, search the table. */
21425 dwarf_file_data **slot
21426 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21427 INSERT);
21428 if (*slot)
21429 return *slot;
21431 created = ggc_alloc<dwarf_file_data> ();
21432 created->filename = file_name;
21433 created->emitted_number = 0;
21434 *slot = created;
21435 return created;
21438 /* If the assembler will construct the file table, then translate the compiler
21439 internal file table number into the assembler file table number, and emit
21440 a .file directive if we haven't already emitted one yet. The file table
21441 numbers are different because we prune debug info for unused variables and
21442 types, which may include filenames. */
21444 static int
21445 maybe_emit_file (struct dwarf_file_data * fd)
21447 if (! fd->emitted_number)
21449 if (last_emitted_file)
21450 fd->emitted_number = last_emitted_file->emitted_number + 1;
21451 else
21452 fd->emitted_number = 1;
21453 last_emitted_file = fd;
21455 if (DWARF2_ASM_LINE_DEBUG_INFO)
21457 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21458 output_quoted_string (asm_out_file,
21459 remap_debug_filename (fd->filename));
21460 fputc ('\n', asm_out_file);
21464 return fd->emitted_number;
21467 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21468 That generation should happen after function debug info has been
21469 generated. The value of the attribute is the constant value of ARG. */
21471 static void
21472 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21474 die_arg_entry entry;
21476 if (!die || !arg)
21477 return;
21479 if (!tmpl_value_parm_die_table)
21480 vec_alloc (tmpl_value_parm_die_table, 32);
21482 entry.die = die;
21483 entry.arg = arg;
21484 vec_safe_push (tmpl_value_parm_die_table, entry);
21487 /* Return TRUE if T is an instance of generic type, FALSE
21488 otherwise. */
21490 static bool
21491 generic_type_p (tree t)
21493 if (t == NULL_TREE || !TYPE_P (t))
21494 return false;
21495 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21498 /* Schedule the generation of the generic parameter dies for the
21499 instance of generic type T. The proper generation itself is later
21500 done by gen_scheduled_generic_parms_dies. */
21502 static void
21503 schedule_generic_params_dies_gen (tree t)
21505 if (!generic_type_p (t))
21506 return;
21508 if (!generic_type_instances)
21509 vec_alloc (generic_type_instances, 256);
21511 vec_safe_push (generic_type_instances, t);
21514 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21515 by append_entry_to_tmpl_value_parm_die_table. This function must
21516 be called after function DIEs have been generated. */
21518 static void
21519 gen_remaining_tmpl_value_param_die_attribute (void)
21521 if (tmpl_value_parm_die_table)
21523 unsigned i;
21524 die_arg_entry *e;
21526 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21527 tree_add_const_value_attribute (e->die, e->arg);
21531 /* Generate generic parameters DIEs for instances of generic types
21532 that have been previously scheduled by
21533 schedule_generic_params_dies_gen. This function must be called
21534 after all the types of the CU have been laid out. */
21536 static void
21537 gen_scheduled_generic_parms_dies (void)
21539 unsigned i;
21540 tree t;
21542 if (!generic_type_instances)
21543 return;
21545 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21546 if (COMPLETE_TYPE_P (t))
21547 gen_generic_params_dies (t);
21551 /* Replace DW_AT_name for the decl with name. */
21553 static void
21554 dwarf2out_set_name (tree decl, tree name)
21556 dw_die_ref die;
21557 dw_attr_ref attr;
21558 const char *dname;
21560 die = TYPE_SYMTAB_DIE (decl);
21561 if (!die)
21562 return;
21564 dname = dwarf2_name (name, 0);
21565 if (!dname)
21566 return;
21568 attr = get_AT (die, DW_AT_name);
21569 if (attr)
21571 struct indirect_string_node *node;
21573 node = find_AT_string (dname);
21574 /* replace the string. */
21575 attr->dw_attr_val.v.val_str = node;
21578 else
21579 add_name_attribute (die, dname);
21582 /* True if before or during processing of the first function being emitted. */
21583 static bool in_first_function_p = true;
21584 /* True if loc_note during dwarf2out_var_location call might still be
21585 before first real instruction at address equal to .Ltext0. */
21586 static bool maybe_at_text_label_p = true;
21587 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21588 static unsigned int first_loclabel_num_not_at_text_label;
21590 /* Called by the final INSN scan whenever we see a var location. We
21591 use it to drop labels in the right places, and throw the location in
21592 our lookup table. */
21594 static void
21595 dwarf2out_var_location (rtx_insn *loc_note)
21597 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21598 struct var_loc_node *newloc;
21599 rtx_insn *next_real, *next_note;
21600 static const char *last_label;
21601 static const char *last_postcall_label;
21602 static bool last_in_cold_section_p;
21603 static rtx_insn *expected_next_loc_note;
21604 tree decl;
21605 bool var_loc_p;
21607 if (!NOTE_P (loc_note))
21609 if (CALL_P (loc_note))
21611 call_site_count++;
21612 if (SIBLING_CALL_P (loc_note))
21613 tail_call_site_count++;
21615 return;
21618 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21619 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21620 return;
21622 /* Optimize processing a large consecutive sequence of location
21623 notes so we don't spend too much time in next_real_insn. If the
21624 next insn is another location note, remember the next_real_insn
21625 calculation for next time. */
21626 next_real = cached_next_real_insn;
21627 if (next_real)
21629 if (expected_next_loc_note != loc_note)
21630 next_real = NULL;
21633 next_note = NEXT_INSN (loc_note);
21634 if (! next_note
21635 || next_note->deleted ()
21636 || ! NOTE_P (next_note)
21637 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21638 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21639 next_note = NULL;
21641 if (! next_real)
21642 next_real = next_real_insn (loc_note);
21644 if (next_note)
21646 expected_next_loc_note = next_note;
21647 cached_next_real_insn = next_real;
21649 else
21650 cached_next_real_insn = NULL;
21652 /* If there are no instructions which would be affected by this note,
21653 don't do anything. */
21654 if (var_loc_p
21655 && next_real == NULL_RTX
21656 && !NOTE_DURING_CALL_P (loc_note))
21657 return;
21659 if (next_real == NULL_RTX)
21660 next_real = get_last_insn ();
21662 /* If there were any real insns between note we processed last time
21663 and this note (or if it is the first note), clear
21664 last_{,postcall_}label so that they are not reused this time. */
21665 if (last_var_location_insn == NULL_RTX
21666 || last_var_location_insn != next_real
21667 || last_in_cold_section_p != in_cold_section_p)
21669 last_label = NULL;
21670 last_postcall_label = NULL;
21673 if (var_loc_p)
21675 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21676 newloc = add_var_loc_to_decl (decl, loc_note,
21677 NOTE_DURING_CALL_P (loc_note)
21678 ? last_postcall_label : last_label);
21679 if (newloc == NULL)
21680 return;
21682 else
21684 decl = NULL_TREE;
21685 newloc = NULL;
21688 /* If there were no real insns between note we processed last time
21689 and this note, use the label we emitted last time. Otherwise
21690 create a new label and emit it. */
21691 if (last_label == NULL)
21693 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21694 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21695 loclabel_num++;
21696 last_label = ggc_strdup (loclabel);
21697 /* See if loclabel might be equal to .Ltext0. If yes,
21698 bump first_loclabel_num_not_at_text_label. */
21699 if (!have_multiple_function_sections
21700 && in_first_function_p
21701 && maybe_at_text_label_p)
21703 static rtx_insn *last_start;
21704 rtx_insn *insn;
21705 for (insn = loc_note; insn; insn = previous_insn (insn))
21706 if (insn == last_start)
21707 break;
21708 else if (!NONDEBUG_INSN_P (insn))
21709 continue;
21710 else
21712 rtx body = PATTERN (insn);
21713 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21714 continue;
21715 /* Inline asm could occupy zero bytes. */
21716 else if (GET_CODE (body) == ASM_INPUT
21717 || asm_noperands (body) >= 0)
21718 continue;
21719 #ifdef HAVE_attr_length
21720 else if (get_attr_min_length (insn) == 0)
21721 continue;
21722 #endif
21723 else
21725 /* Assume insn has non-zero length. */
21726 maybe_at_text_label_p = false;
21727 break;
21730 if (maybe_at_text_label_p)
21732 last_start = loc_note;
21733 first_loclabel_num_not_at_text_label = loclabel_num;
21738 if (!var_loc_p)
21740 struct call_arg_loc_node *ca_loc
21741 = ggc_cleared_alloc<call_arg_loc_node> ();
21742 rtx_insn *prev = prev_real_insn (loc_note);
21743 rtx x;
21744 ca_loc->call_arg_loc_note = loc_note;
21745 ca_loc->next = NULL;
21746 ca_loc->label = last_label;
21747 gcc_assert (prev
21748 && (CALL_P (prev)
21749 || (NONJUMP_INSN_P (prev)
21750 && GET_CODE (PATTERN (prev)) == SEQUENCE
21751 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21752 if (!CALL_P (prev))
21753 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21754 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21755 x = get_call_rtx_from (PATTERN (prev));
21756 if (x)
21758 x = XEXP (XEXP (x, 0), 0);
21759 if (GET_CODE (x) == SYMBOL_REF
21760 && SYMBOL_REF_DECL (x)
21761 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21762 ca_loc->symbol_ref = x;
21764 ca_loc->block = insn_scope (prev);
21765 if (call_arg_locations)
21766 call_arg_loc_last->next = ca_loc;
21767 else
21768 call_arg_locations = ca_loc;
21769 call_arg_loc_last = ca_loc;
21771 else if (!NOTE_DURING_CALL_P (loc_note))
21772 newloc->label = last_label;
21773 else
21775 if (!last_postcall_label)
21777 sprintf (loclabel, "%s-1", last_label);
21778 last_postcall_label = ggc_strdup (loclabel);
21780 newloc->label = last_postcall_label;
21783 last_var_location_insn = next_real;
21784 last_in_cold_section_p = in_cold_section_p;
21787 /* Note in one location list that text section has changed. */
21790 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21792 var_loc_list *list = *slot;
21793 if (list->first)
21794 list->last_before_switch
21795 = list->last->next ? list->last->next : list->last;
21796 return 1;
21799 /* Note in all location lists that text section has changed. */
21801 static void
21802 var_location_switch_text_section (void)
21804 if (decl_loc_table == NULL)
21805 return;
21807 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21810 /* Create a new line number table. */
21812 static dw_line_info_table *
21813 new_line_info_table (void)
21815 dw_line_info_table *table;
21817 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21818 table->file_num = 1;
21819 table->line_num = 1;
21820 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21822 return table;
21825 /* Lookup the "current" table into which we emit line info, so
21826 that we don't have to do it for every source line. */
21828 static void
21829 set_cur_line_info_table (section *sec)
21831 dw_line_info_table *table;
21833 if (sec == text_section)
21834 table = text_section_line_info;
21835 else if (sec == cold_text_section)
21837 table = cold_text_section_line_info;
21838 if (!table)
21840 cold_text_section_line_info = table = new_line_info_table ();
21841 table->end_label = cold_end_label;
21844 else
21846 const char *end_label;
21848 if (flag_reorder_blocks_and_partition)
21850 if (in_cold_section_p)
21851 end_label = crtl->subsections.cold_section_end_label;
21852 else
21853 end_label = crtl->subsections.hot_section_end_label;
21855 else
21857 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21858 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21859 current_function_funcdef_no);
21860 end_label = ggc_strdup (label);
21863 table = new_line_info_table ();
21864 table->end_label = end_label;
21866 vec_safe_push (separate_line_info, table);
21869 if (DWARF2_ASM_LINE_DEBUG_INFO)
21870 table->is_stmt = (cur_line_info_table
21871 ? cur_line_info_table->is_stmt
21872 : DWARF_LINE_DEFAULT_IS_STMT_START);
21873 cur_line_info_table = table;
21877 /* We need to reset the locations at the beginning of each
21878 function. We can't do this in the end_function hook, because the
21879 declarations that use the locations won't have been output when
21880 that hook is called. Also compute have_multiple_function_sections here. */
21882 static void
21883 dwarf2out_begin_function (tree fun)
21885 section *sec = function_section (fun);
21887 if (sec != text_section)
21888 have_multiple_function_sections = true;
21890 if (flag_reorder_blocks_and_partition && !cold_text_section)
21892 gcc_assert (current_function_decl == fun);
21893 cold_text_section = unlikely_text_section ();
21894 switch_to_section (cold_text_section);
21895 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21896 switch_to_section (sec);
21899 dwarf2out_note_section_used ();
21900 call_site_count = 0;
21901 tail_call_site_count = 0;
21903 set_cur_line_info_table (sec);
21906 /* Helper function of dwarf2out_end_function, called only after emitting
21907 the very first function into assembly. Check if some .debug_loc range
21908 might end with a .LVL* label that could be equal to .Ltext0.
21909 In that case we must force using absolute addresses in .debug_loc ranges,
21910 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21911 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21912 list terminator.
21913 Set have_multiple_function_sections to true in that case and
21914 terminate htab traversal. */
21917 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21919 var_loc_list *entry = *slot;
21920 struct var_loc_node *node;
21922 node = entry->first;
21923 if (node && node->next && node->next->label)
21925 unsigned int i;
21926 const char *label = node->next->label;
21927 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21929 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21931 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21932 if (strcmp (label, loclabel) == 0)
21934 have_multiple_function_sections = true;
21935 return 0;
21939 return 1;
21942 /* Hook called after emitting a function into assembly.
21943 This does something only for the very first function emitted. */
21945 static void
21946 dwarf2out_end_function (unsigned int)
21948 if (in_first_function_p
21949 && !have_multiple_function_sections
21950 && first_loclabel_num_not_at_text_label
21951 && decl_loc_table)
21952 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
21953 in_first_function_p = false;
21954 maybe_at_text_label_p = false;
21957 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21959 static void
21960 push_dw_line_info_entry (dw_line_info_table *table,
21961 enum dw_line_info_opcode opcode, unsigned int val)
21963 dw_line_info_entry e;
21964 e.opcode = opcode;
21965 e.val = val;
21966 vec_safe_push (table->entries, e);
21969 /* Output a label to mark the beginning of a source code line entry
21970 and record information relating to this source line, in
21971 'line_info_table' for later output of the .debug_line section. */
21972 /* ??? The discriminator parameter ought to be unsigned. */
21974 static void
21975 dwarf2out_source_line (unsigned int line, const char *filename,
21976 int discriminator, bool is_stmt)
21978 unsigned int file_num;
21979 dw_line_info_table *table;
21981 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21982 return;
21984 /* The discriminator column was added in dwarf4. Simplify the below
21985 by simply removing it if we're not supposed to output it. */
21986 if (dwarf_version < 4 && dwarf_strict)
21987 discriminator = 0;
21989 table = cur_line_info_table;
21990 file_num = maybe_emit_file (lookup_filename (filename));
21992 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21993 the debugger has used the second (possibly duplicate) line number
21994 at the beginning of the function to mark the end of the prologue.
21995 We could eliminate any other duplicates within the function. For
21996 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21997 that second line number entry. */
21998 /* Recall that this end-of-prologue indication is *not* the same thing
21999 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22000 to which the hook corresponds, follows the last insn that was
22001 emitted by gen_prologue. What we need is to precede the first insn
22002 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22003 insn that corresponds to something the user wrote. These may be
22004 very different locations once scheduling is enabled. */
22006 if (0 && file_num == table->file_num
22007 && line == table->line_num
22008 && discriminator == table->discrim_num
22009 && is_stmt == table->is_stmt)
22010 return;
22012 switch_to_section (current_function_section ());
22014 /* If requested, emit something human-readable. */
22015 if (flag_debug_asm)
22016 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22018 if (DWARF2_ASM_LINE_DEBUG_INFO)
22020 /* Emit the .loc directive understood by GNU as. */
22021 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22022 file_num, line, is_stmt, discriminator */
22023 fputs ("\t.loc ", asm_out_file);
22024 fprint_ul (asm_out_file, file_num);
22025 putc (' ', asm_out_file);
22026 fprint_ul (asm_out_file, line);
22027 putc (' ', asm_out_file);
22028 putc ('0', asm_out_file);
22030 if (is_stmt != table->is_stmt)
22032 fputs (" is_stmt ", asm_out_file);
22033 putc (is_stmt ? '1' : '0', asm_out_file);
22035 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22037 gcc_assert (discriminator > 0);
22038 fputs (" discriminator ", asm_out_file);
22039 fprint_ul (asm_out_file, (unsigned long) discriminator);
22041 putc ('\n', asm_out_file);
22043 else
22045 unsigned int label_num = ++line_info_label_num;
22047 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22049 push_dw_line_info_entry (table, LI_set_address, label_num);
22050 if (file_num != table->file_num)
22051 push_dw_line_info_entry (table, LI_set_file, file_num);
22052 if (discriminator != table->discrim_num)
22053 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22054 if (is_stmt != table->is_stmt)
22055 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22056 push_dw_line_info_entry (table, LI_set_line, line);
22059 table->file_num = file_num;
22060 table->line_num = line;
22061 table->discrim_num = discriminator;
22062 table->is_stmt = is_stmt;
22063 table->in_use = true;
22066 /* Record the beginning of a new source file. */
22068 static void
22069 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22071 if (flag_eliminate_dwarf2_dups)
22073 /* Record the beginning of the file for break_out_includes. */
22074 dw_die_ref bincl_die;
22076 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22077 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22080 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22082 macinfo_entry e;
22083 e.code = DW_MACINFO_start_file;
22084 e.lineno = lineno;
22085 e.info = ggc_strdup (filename);
22086 vec_safe_push (macinfo_table, e);
22090 /* Record the end of a source file. */
22092 static void
22093 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22095 if (flag_eliminate_dwarf2_dups)
22096 /* Record the end of the file for break_out_includes. */
22097 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22099 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22101 macinfo_entry e;
22102 e.code = DW_MACINFO_end_file;
22103 e.lineno = lineno;
22104 e.info = NULL;
22105 vec_safe_push (macinfo_table, e);
22109 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22110 the tail part of the directive line, i.e. the part which is past the
22111 initial whitespace, #, whitespace, directive-name, whitespace part. */
22113 static void
22114 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22115 const char *buffer ATTRIBUTE_UNUSED)
22117 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22119 macinfo_entry e;
22120 /* Insert a dummy first entry to be able to optimize the whole
22121 predefined macro block using DW_MACRO_GNU_transparent_include. */
22122 if (macinfo_table->is_empty () && lineno <= 1)
22124 e.code = 0;
22125 e.lineno = 0;
22126 e.info = NULL;
22127 vec_safe_push (macinfo_table, e);
22129 e.code = DW_MACINFO_define;
22130 e.lineno = lineno;
22131 e.info = ggc_strdup (buffer);
22132 vec_safe_push (macinfo_table, e);
22136 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22137 the tail part of the directive line, i.e. the part which is past the
22138 initial whitespace, #, whitespace, directive-name, whitespace part. */
22140 static void
22141 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22142 const char *buffer ATTRIBUTE_UNUSED)
22144 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22146 macinfo_entry e;
22147 /* Insert a dummy first entry to be able to optimize the whole
22148 predefined macro block using DW_MACRO_GNU_transparent_include. */
22149 if (macinfo_table->is_empty () && lineno <= 1)
22151 e.code = 0;
22152 e.lineno = 0;
22153 e.info = NULL;
22154 vec_safe_push (macinfo_table, e);
22156 e.code = DW_MACINFO_undef;
22157 e.lineno = lineno;
22158 e.info = ggc_strdup (buffer);
22159 vec_safe_push (macinfo_table, e);
22163 /* Helpers to manipulate hash table of CUs. */
22165 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22167 typedef macinfo_entry value_type;
22168 typedef macinfo_entry compare_type;
22169 static inline hashval_t hash (const value_type *);
22170 static inline bool equal (const value_type *, const compare_type *);
22173 inline hashval_t
22174 macinfo_entry_hasher::hash (const value_type *entry)
22176 return htab_hash_string (entry->info);
22179 inline bool
22180 macinfo_entry_hasher::equal (const value_type *entry1,
22181 const compare_type *entry2)
22183 return !strcmp (entry1->info, entry2->info);
22186 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22188 /* Output a single .debug_macinfo entry. */
22190 static void
22191 output_macinfo_op (macinfo_entry *ref)
22193 int file_num;
22194 size_t len;
22195 struct indirect_string_node *node;
22196 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22197 struct dwarf_file_data *fd;
22199 switch (ref->code)
22201 case DW_MACINFO_start_file:
22202 fd = lookup_filename (ref->info);
22203 file_num = maybe_emit_file (fd);
22204 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22205 dw2_asm_output_data_uleb128 (ref->lineno,
22206 "Included from line number %lu",
22207 (unsigned long) ref->lineno);
22208 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22209 break;
22210 case DW_MACINFO_end_file:
22211 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22212 break;
22213 case DW_MACINFO_define:
22214 case DW_MACINFO_undef:
22215 len = strlen (ref->info) + 1;
22216 if (!dwarf_strict
22217 && len > DWARF_OFFSET_SIZE
22218 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22219 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22221 ref->code = ref->code == DW_MACINFO_define
22222 ? DW_MACRO_GNU_define_indirect
22223 : DW_MACRO_GNU_undef_indirect;
22224 output_macinfo_op (ref);
22225 return;
22227 dw2_asm_output_data (1, ref->code,
22228 ref->code == DW_MACINFO_define
22229 ? "Define macro" : "Undefine macro");
22230 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22231 (unsigned long) ref->lineno);
22232 dw2_asm_output_nstring (ref->info, -1, "The macro");
22233 break;
22234 case DW_MACRO_GNU_define_indirect:
22235 case DW_MACRO_GNU_undef_indirect:
22236 node = find_AT_string (ref->info);
22237 gcc_assert (node
22238 && ((node->form == DW_FORM_strp)
22239 || (node->form == DW_FORM_GNU_str_index)));
22240 dw2_asm_output_data (1, ref->code,
22241 ref->code == DW_MACRO_GNU_define_indirect
22242 ? "Define macro indirect"
22243 : "Undefine macro indirect");
22244 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22245 (unsigned long) ref->lineno);
22246 if (node->form == DW_FORM_strp)
22247 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22248 debug_str_section, "The macro: \"%s\"",
22249 ref->info);
22250 else
22251 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22252 ref->info);
22253 break;
22254 case DW_MACRO_GNU_transparent_include:
22255 dw2_asm_output_data (1, ref->code, "Transparent include");
22256 ASM_GENERATE_INTERNAL_LABEL (label,
22257 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22258 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22259 break;
22260 default:
22261 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22262 ASM_COMMENT_START, (unsigned long) ref->code);
22263 break;
22267 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22268 other compilation unit .debug_macinfo sections. IDX is the first
22269 index of a define/undef, return the number of ops that should be
22270 emitted in a comdat .debug_macinfo section and emit
22271 a DW_MACRO_GNU_transparent_include entry referencing it.
22272 If the define/undef entry should be emitted normally, return 0. */
22274 static unsigned
22275 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22276 macinfo_hash_type **macinfo_htab)
22278 macinfo_entry *first, *second, *cur, *inc;
22279 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22280 unsigned char checksum[16];
22281 struct md5_ctx ctx;
22282 char *grp_name, *tail;
22283 const char *base;
22284 unsigned int i, count, encoded_filename_len, linebuf_len;
22285 macinfo_entry **slot;
22287 first = &(*macinfo_table)[idx];
22288 second = &(*macinfo_table)[idx + 1];
22290 /* Optimize only if there are at least two consecutive define/undef ops,
22291 and either all of them are before first DW_MACINFO_start_file
22292 with lineno {0,1} (i.e. predefined macro block), or all of them are
22293 in some included header file. */
22294 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22295 return 0;
22296 if (vec_safe_is_empty (files))
22298 if (first->lineno > 1 || second->lineno > 1)
22299 return 0;
22301 else if (first->lineno == 0)
22302 return 0;
22304 /* Find the last define/undef entry that can be grouped together
22305 with first and at the same time compute md5 checksum of their
22306 codes, linenumbers and strings. */
22307 md5_init_ctx (&ctx);
22308 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22309 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22310 break;
22311 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22312 break;
22313 else
22315 unsigned char code = cur->code;
22316 md5_process_bytes (&code, 1, &ctx);
22317 checksum_uleb128 (cur->lineno, &ctx);
22318 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22320 md5_finish_ctx (&ctx, checksum);
22321 count = i - idx;
22323 /* From the containing include filename (if any) pick up just
22324 usable characters from its basename. */
22325 if (vec_safe_is_empty (files))
22326 base = "";
22327 else
22328 base = lbasename (files->last ().info);
22329 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22330 if (ISIDNUM (base[i]) || base[i] == '.')
22331 encoded_filename_len++;
22332 /* Count . at the end. */
22333 if (encoded_filename_len)
22334 encoded_filename_len++;
22336 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22337 linebuf_len = strlen (linebuf);
22339 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22340 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22341 + 16 * 2 + 1);
22342 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22343 tail = grp_name + 4;
22344 if (encoded_filename_len)
22346 for (i = 0; base[i]; i++)
22347 if (ISIDNUM (base[i]) || base[i] == '.')
22348 *tail++ = base[i];
22349 *tail++ = '.';
22351 memcpy (tail, linebuf, linebuf_len);
22352 tail += linebuf_len;
22353 *tail++ = '.';
22354 for (i = 0; i < 16; i++)
22355 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22357 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22358 in the empty vector entry before the first define/undef. */
22359 inc = &(*macinfo_table)[idx - 1];
22360 inc->code = DW_MACRO_GNU_transparent_include;
22361 inc->lineno = 0;
22362 inc->info = ggc_strdup (grp_name);
22363 if (!*macinfo_htab)
22364 *macinfo_htab = new macinfo_hash_type (10);
22365 /* Avoid emitting duplicates. */
22366 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22367 if (*slot != NULL)
22369 inc->code = 0;
22370 inc->info = NULL;
22371 /* If such an entry has been used before, just emit
22372 a DW_MACRO_GNU_transparent_include op. */
22373 inc = *slot;
22374 output_macinfo_op (inc);
22375 /* And clear all macinfo_entry in the range to avoid emitting them
22376 in the second pass. */
22377 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22379 cur->code = 0;
22380 cur->info = NULL;
22383 else
22385 *slot = inc;
22386 inc->lineno = (*macinfo_htab)->elements ();
22387 output_macinfo_op (inc);
22389 return count;
22392 /* Save any strings needed by the macinfo table in the debug str
22393 table. All strings must be collected into the table by the time
22394 index_string is called. */
22396 static void
22397 save_macinfo_strings (void)
22399 unsigned len;
22400 unsigned i;
22401 macinfo_entry *ref;
22403 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22405 switch (ref->code)
22407 /* Match the logic in output_macinfo_op to decide on
22408 indirect strings. */
22409 case DW_MACINFO_define:
22410 case DW_MACINFO_undef:
22411 len = strlen (ref->info) + 1;
22412 if (!dwarf_strict
22413 && len > DWARF_OFFSET_SIZE
22414 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22415 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22416 set_indirect_string (find_AT_string (ref->info));
22417 break;
22418 case DW_MACRO_GNU_define_indirect:
22419 case DW_MACRO_GNU_undef_indirect:
22420 set_indirect_string (find_AT_string (ref->info));
22421 break;
22422 default:
22423 break;
22428 /* Output macinfo section(s). */
22430 static void
22431 output_macinfo (void)
22433 unsigned i;
22434 unsigned long length = vec_safe_length (macinfo_table);
22435 macinfo_entry *ref;
22436 vec<macinfo_entry, va_gc> *files = NULL;
22437 macinfo_hash_type *macinfo_htab = NULL;
22439 if (! length)
22440 return;
22442 /* output_macinfo* uses these interchangeably. */
22443 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22444 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22445 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22446 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22448 /* For .debug_macro emit the section header. */
22449 if (!dwarf_strict)
22451 dw2_asm_output_data (2, 4, "DWARF macro version number");
22452 if (DWARF_OFFSET_SIZE == 8)
22453 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22454 else
22455 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22456 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22457 (!dwarf_split_debug_info ? debug_line_section_label
22458 : debug_skeleton_line_section_label),
22459 debug_line_section, NULL);
22462 /* In the first loop, it emits the primary .debug_macinfo section
22463 and after each emitted op the macinfo_entry is cleared.
22464 If a longer range of define/undef ops can be optimized using
22465 DW_MACRO_GNU_transparent_include, the
22466 DW_MACRO_GNU_transparent_include op is emitted and kept in
22467 the vector before the first define/undef in the range and the
22468 whole range of define/undef ops is not emitted and kept. */
22469 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22471 switch (ref->code)
22473 case DW_MACINFO_start_file:
22474 vec_safe_push (files, *ref);
22475 break;
22476 case DW_MACINFO_end_file:
22477 if (!vec_safe_is_empty (files))
22478 files->pop ();
22479 break;
22480 case DW_MACINFO_define:
22481 case DW_MACINFO_undef:
22482 if (!dwarf_strict
22483 && HAVE_COMDAT_GROUP
22484 && vec_safe_length (files) != 1
22485 && i > 0
22486 && i + 1 < length
22487 && (*macinfo_table)[i - 1].code == 0)
22489 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22490 if (count)
22492 i += count - 1;
22493 continue;
22496 break;
22497 case 0:
22498 /* A dummy entry may be inserted at the beginning to be able
22499 to optimize the whole block of predefined macros. */
22500 if (i == 0)
22501 continue;
22502 default:
22503 break;
22505 output_macinfo_op (ref);
22506 ref->info = NULL;
22507 ref->code = 0;
22510 if (!macinfo_htab)
22511 return;
22513 delete macinfo_htab;
22514 macinfo_htab = NULL;
22516 /* If any DW_MACRO_GNU_transparent_include were used, on those
22517 DW_MACRO_GNU_transparent_include entries terminate the
22518 current chain and switch to a new comdat .debug_macinfo
22519 section and emit the define/undef entries within it. */
22520 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22521 switch (ref->code)
22523 case 0:
22524 continue;
22525 case DW_MACRO_GNU_transparent_include:
22527 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22528 tree comdat_key = get_identifier (ref->info);
22529 /* Terminate the previous .debug_macinfo section. */
22530 dw2_asm_output_data (1, 0, "End compilation unit");
22531 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22532 SECTION_DEBUG
22533 | SECTION_LINKONCE,
22534 comdat_key);
22535 ASM_GENERATE_INTERNAL_LABEL (label,
22536 DEBUG_MACRO_SECTION_LABEL,
22537 ref->lineno);
22538 ASM_OUTPUT_LABEL (asm_out_file, label);
22539 ref->code = 0;
22540 ref->info = NULL;
22541 dw2_asm_output_data (2, 4, "DWARF macro version number");
22542 if (DWARF_OFFSET_SIZE == 8)
22543 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22544 else
22545 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22547 break;
22548 case DW_MACINFO_define:
22549 case DW_MACINFO_undef:
22550 output_macinfo_op (ref);
22551 ref->code = 0;
22552 ref->info = NULL;
22553 break;
22554 default:
22555 gcc_unreachable ();
22559 /* Set up for Dwarf output at the start of compilation. */
22561 static void
22562 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22564 /* Allocate the file_table. */
22565 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22567 /* Allocate the decl_die_table. */
22568 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22570 /* Allocate the decl_loc_table. */
22571 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22573 /* Allocate the cached_dw_loc_list_table. */
22574 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22576 /* Allocate the initial hunk of the decl_scope_table. */
22577 vec_alloc (decl_scope_table, 256);
22579 /* Allocate the initial hunk of the abbrev_die_table. */
22580 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22581 (ABBREV_DIE_TABLE_INCREMENT);
22582 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22583 /* Zero-th entry is allocated, but unused. */
22584 abbrev_die_table_in_use = 1;
22586 /* Allocate the pubtypes and pubnames vectors. */
22587 vec_alloc (pubname_table, 32);
22588 vec_alloc (pubtype_table, 32);
22590 vec_alloc (incomplete_types, 64);
22592 vec_alloc (used_rtx_array, 32);
22594 if (!dwarf_split_debug_info)
22596 debug_info_section = get_section (DEBUG_INFO_SECTION,
22597 SECTION_DEBUG, NULL);
22598 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22599 SECTION_DEBUG, NULL);
22600 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22601 SECTION_DEBUG, NULL);
22603 else
22605 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22606 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22607 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22608 SECTION_DEBUG | SECTION_EXCLUDE,
22609 NULL);
22610 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22611 SECTION_DEBUG, NULL);
22612 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22613 SECTION_DEBUG, NULL);
22614 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22615 SECTION_DEBUG, NULL);
22616 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22617 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22619 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22620 the main .o, but the skeleton_line goes into the split off dwo. */
22621 debug_skeleton_line_section
22622 = get_section (DEBUG_DWO_LINE_SECTION,
22623 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22624 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22625 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22626 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22627 SECTION_DEBUG | SECTION_EXCLUDE,
22628 NULL);
22629 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22630 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22631 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22632 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22633 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22634 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22636 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22637 SECTION_DEBUG, NULL);
22638 debug_macinfo_section = get_section (dwarf_strict
22639 ? DEBUG_MACINFO_SECTION
22640 : DEBUG_MACRO_SECTION,
22641 DEBUG_MACRO_SECTION_FLAGS, NULL);
22642 debug_line_section = get_section (DEBUG_LINE_SECTION,
22643 SECTION_DEBUG, NULL);
22644 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22645 SECTION_DEBUG, NULL);
22646 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22647 SECTION_DEBUG, NULL);
22648 debug_str_section = get_section (DEBUG_STR_SECTION,
22649 DEBUG_STR_SECTION_FLAGS, NULL);
22650 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22651 SECTION_DEBUG, NULL);
22652 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22653 SECTION_DEBUG, NULL);
22655 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22656 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22657 DEBUG_ABBREV_SECTION_LABEL, 0);
22658 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22659 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22660 COLD_TEXT_SECTION_LABEL, 0);
22661 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22663 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22664 DEBUG_INFO_SECTION_LABEL, 0);
22665 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22666 DEBUG_LINE_SECTION_LABEL, 0);
22667 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22668 DEBUG_RANGES_SECTION_LABEL, 0);
22669 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22670 DEBUG_ADDR_SECTION_LABEL, 0);
22671 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22672 dwarf_strict
22673 ? DEBUG_MACINFO_SECTION_LABEL
22674 : DEBUG_MACRO_SECTION_LABEL, 0);
22675 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22677 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22678 vec_alloc (macinfo_table, 64);
22680 switch_to_section (text_section);
22681 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22683 /* Make sure the line number table for .text always exists. */
22684 text_section_line_info = new_line_info_table ();
22685 text_section_line_info->end_label = text_end_label;
22688 /* Called before compile () starts outputtting functions, variables
22689 and toplevel asms into assembly. */
22691 static void
22692 dwarf2out_assembly_start (void)
22694 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22695 && dwarf2out_do_cfi_asm ()
22696 && (!(flag_unwind_tables || flag_exceptions)
22697 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22698 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22701 /* A helper function for dwarf2out_finish called through
22702 htab_traverse. Assign a string its index. All strings must be
22703 collected into the table by the time index_string is called,
22704 because the indexing code relies on htab_traverse to traverse nodes
22705 in the same order for each run. */
22708 index_string (indirect_string_node **h, unsigned int *index)
22710 indirect_string_node *node = *h;
22712 find_string_form (node);
22713 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22715 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22716 node->index = *index;
22717 *index += 1;
22719 return 1;
22722 /* A helper function for output_indirect_strings called through
22723 htab_traverse. Output the offset to a string and update the
22724 current offset. */
22727 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22729 indirect_string_node *node = *h;
22731 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22733 /* Assert that this node has been assigned an index. */
22734 gcc_assert (node->index != NO_INDEX_ASSIGNED
22735 && node->index != NOT_INDEXED);
22736 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22737 "indexed string 0x%x: %s", node->index, node->str);
22738 *offset += strlen (node->str) + 1;
22740 return 1;
22743 /* A helper function for dwarf2out_finish called through
22744 htab_traverse. Output the indexed string. */
22747 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22749 struct indirect_string_node *node = *h;
22751 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22753 /* Assert that the strings are output in the same order as their
22754 indexes were assigned. */
22755 gcc_assert (*cur_idx == node->index);
22756 assemble_string (node->str, strlen (node->str) + 1);
22757 *cur_idx += 1;
22759 return 1;
22762 /* A helper function for dwarf2out_finish called through
22763 htab_traverse. Emit one queued .debug_str string. */
22766 output_indirect_string (indirect_string_node **h, void *)
22768 struct indirect_string_node *node = *h;
22770 node->form = find_string_form (node);
22771 if (node->form == DW_FORM_strp && node->refcount > 0)
22773 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22774 assemble_string (node->str, strlen (node->str) + 1);
22777 return 1;
22780 /* Output the indexed string table. */
22782 static void
22783 output_indirect_strings (void)
22785 switch_to_section (debug_str_section);
22786 if (!dwarf_split_debug_info)
22787 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22788 else
22790 unsigned int offset = 0;
22791 unsigned int cur_idx = 0;
22793 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22795 switch_to_section (debug_str_offsets_section);
22796 debug_str_hash->traverse_noresize
22797 <unsigned int *, output_index_string_offset> (&offset);
22798 switch_to_section (debug_str_dwo_section);
22799 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22800 (&cur_idx);
22804 /* Callback for htab_traverse to assign an index to an entry in the
22805 table, and to write that entry to the .debug_addr section. */
22808 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22810 addr_table_entry *entry = *slot;
22812 if (entry->refcount == 0)
22814 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22815 || entry->index == NOT_INDEXED);
22816 return 1;
22819 gcc_assert (entry->index == *cur_index);
22820 (*cur_index)++;
22822 switch (entry->kind)
22824 case ate_kind_rtx:
22825 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22826 "0x%x", entry->index);
22827 break;
22828 case ate_kind_rtx_dtprel:
22829 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22830 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22831 DWARF2_ADDR_SIZE,
22832 entry->addr.rtl);
22833 fputc ('\n', asm_out_file);
22834 break;
22835 case ate_kind_label:
22836 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22837 "0x%x", entry->index);
22838 break;
22839 default:
22840 gcc_unreachable ();
22842 return 1;
22845 /* Produce the .debug_addr section. */
22847 static void
22848 output_addr_table (void)
22850 unsigned int index = 0;
22851 if (addr_index_table == NULL || addr_index_table->size () == 0)
22852 return;
22854 switch_to_section (debug_addr_section);
22855 addr_index_table
22856 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22859 #if ENABLE_ASSERT_CHECKING
22860 /* Verify that all marks are clear. */
22862 static void
22863 verify_marks_clear (dw_die_ref die)
22865 dw_die_ref c;
22867 gcc_assert (! die->die_mark);
22868 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22870 #endif /* ENABLE_ASSERT_CHECKING */
22872 /* Clear the marks for a die and its children.
22873 Be cool if the mark isn't set. */
22875 static void
22876 prune_unmark_dies (dw_die_ref die)
22878 dw_die_ref c;
22880 if (die->die_mark)
22881 die->die_mark = 0;
22882 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22885 /* Given DIE that we're marking as used, find any other dies
22886 it references as attributes and mark them as used. */
22888 static void
22889 prune_unused_types_walk_attribs (dw_die_ref die)
22891 dw_attr_ref a;
22892 unsigned ix;
22894 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22896 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22898 /* A reference to another DIE.
22899 Make sure that it will get emitted.
22900 If it was broken out into a comdat group, don't follow it. */
22901 if (! AT_ref (a)->comdat_type_p
22902 || a->dw_attr == DW_AT_specification)
22903 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22905 /* Set the string's refcount to 0 so that prune_unused_types_mark
22906 accounts properly for it. */
22907 if (AT_class (a) == dw_val_class_str)
22908 a->dw_attr_val.v.val_str->refcount = 0;
22912 /* Mark the generic parameters and arguments children DIEs of DIE. */
22914 static void
22915 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22917 dw_die_ref c;
22919 if (die == NULL || die->die_child == NULL)
22920 return;
22921 c = die->die_child;
22924 if (is_template_parameter (c))
22925 prune_unused_types_mark (c, 1);
22926 c = c->die_sib;
22927 } while (c && c != die->die_child);
22930 /* Mark DIE as being used. If DOKIDS is true, then walk down
22931 to DIE's children. */
22933 static void
22934 prune_unused_types_mark (dw_die_ref die, int dokids)
22936 dw_die_ref c;
22938 if (die->die_mark == 0)
22940 /* We haven't done this node yet. Mark it as used. */
22941 die->die_mark = 1;
22942 /* If this is the DIE of a generic type instantiation,
22943 mark the children DIEs that describe its generic parms and
22944 args. */
22945 prune_unused_types_mark_generic_parms_dies (die);
22947 /* We also have to mark its parents as used.
22948 (But we don't want to mark our parent's kids due to this,
22949 unless it is a class.) */
22950 if (die->die_parent)
22951 prune_unused_types_mark (die->die_parent,
22952 class_scope_p (die->die_parent));
22954 /* Mark any referenced nodes. */
22955 prune_unused_types_walk_attribs (die);
22957 /* If this node is a specification,
22958 also mark the definition, if it exists. */
22959 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22960 prune_unused_types_mark (die->die_definition, 1);
22963 if (dokids && die->die_mark != 2)
22965 /* We need to walk the children, but haven't done so yet.
22966 Remember that we've walked the kids. */
22967 die->die_mark = 2;
22969 /* If this is an array type, we need to make sure our
22970 kids get marked, even if they're types. If we're
22971 breaking out types into comdat sections, do this
22972 for all type definitions. */
22973 if (die->die_tag == DW_TAG_array_type
22974 || (use_debug_types
22975 && is_type_die (die) && ! is_declaration_die (die)))
22976 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22977 else
22978 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22982 /* For local classes, look if any static member functions were emitted
22983 and if so, mark them. */
22985 static void
22986 prune_unused_types_walk_local_classes (dw_die_ref die)
22988 dw_die_ref c;
22990 if (die->die_mark == 2)
22991 return;
22993 switch (die->die_tag)
22995 case DW_TAG_structure_type:
22996 case DW_TAG_union_type:
22997 case DW_TAG_class_type:
22998 break;
23000 case DW_TAG_subprogram:
23001 if (!get_AT_flag (die, DW_AT_declaration)
23002 || die->die_definition != NULL)
23003 prune_unused_types_mark (die, 1);
23004 return;
23006 default:
23007 return;
23010 /* Mark children. */
23011 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23014 /* Walk the tree DIE and mark types that we actually use. */
23016 static void
23017 prune_unused_types_walk (dw_die_ref die)
23019 dw_die_ref c;
23021 /* Don't do anything if this node is already marked and
23022 children have been marked as well. */
23023 if (die->die_mark == 2)
23024 return;
23026 switch (die->die_tag)
23028 case DW_TAG_structure_type:
23029 case DW_TAG_union_type:
23030 case DW_TAG_class_type:
23031 if (die->die_perennial_p)
23032 break;
23034 for (c = die->die_parent; c; c = c->die_parent)
23035 if (c->die_tag == DW_TAG_subprogram)
23036 break;
23038 /* Finding used static member functions inside of classes
23039 is needed just for local classes, because for other classes
23040 static member function DIEs with DW_AT_specification
23041 are emitted outside of the DW_TAG_*_type. If we ever change
23042 it, we'd need to call this even for non-local classes. */
23043 if (c)
23044 prune_unused_types_walk_local_classes (die);
23046 /* It's a type node --- don't mark it. */
23047 return;
23049 case DW_TAG_const_type:
23050 case DW_TAG_packed_type:
23051 case DW_TAG_pointer_type:
23052 case DW_TAG_reference_type:
23053 case DW_TAG_rvalue_reference_type:
23054 case DW_TAG_volatile_type:
23055 case DW_TAG_typedef:
23056 case DW_TAG_array_type:
23057 case DW_TAG_interface_type:
23058 case DW_TAG_friend:
23059 case DW_TAG_variant_part:
23060 case DW_TAG_enumeration_type:
23061 case DW_TAG_subroutine_type:
23062 case DW_TAG_string_type:
23063 case DW_TAG_set_type:
23064 case DW_TAG_subrange_type:
23065 case DW_TAG_ptr_to_member_type:
23066 case DW_TAG_file_type:
23067 if (die->die_perennial_p)
23068 break;
23070 /* It's a type node --- don't mark it. */
23071 return;
23073 default:
23074 /* Mark everything else. */
23075 break;
23078 if (die->die_mark == 0)
23080 die->die_mark = 1;
23082 /* Now, mark any dies referenced from here. */
23083 prune_unused_types_walk_attribs (die);
23086 die->die_mark = 2;
23088 /* Mark children. */
23089 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23092 /* Increment the string counts on strings referred to from DIE's
23093 attributes. */
23095 static void
23096 prune_unused_types_update_strings (dw_die_ref die)
23098 dw_attr_ref a;
23099 unsigned ix;
23101 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23102 if (AT_class (a) == dw_val_class_str)
23104 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23105 s->refcount++;
23106 /* Avoid unnecessarily putting strings that are used less than
23107 twice in the hash table. */
23108 if (s->refcount
23109 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23111 indirect_string_node **slot
23112 = debug_str_hash->find_slot_with_hash (s->str,
23113 htab_hash_string (s->str),
23114 INSERT);
23115 gcc_assert (*slot == NULL);
23116 *slot = s;
23121 /* Remove from the tree DIE any dies that aren't marked. */
23123 static void
23124 prune_unused_types_prune (dw_die_ref die)
23126 dw_die_ref c;
23128 gcc_assert (die->die_mark);
23129 prune_unused_types_update_strings (die);
23131 if (! die->die_child)
23132 return;
23134 c = die->die_child;
23135 do {
23136 dw_die_ref prev = c;
23137 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23138 if (c == die->die_child)
23140 /* No marked children between 'prev' and the end of the list. */
23141 if (prev == c)
23142 /* No marked children at all. */
23143 die->die_child = NULL;
23144 else
23146 prev->die_sib = c->die_sib;
23147 die->die_child = prev;
23149 return;
23152 if (c != prev->die_sib)
23153 prev->die_sib = c;
23154 prune_unused_types_prune (c);
23155 } while (c != die->die_child);
23158 /* Remove dies representing declarations that we never use. */
23160 static void
23161 prune_unused_types (void)
23163 unsigned int i;
23164 limbo_die_node *node;
23165 comdat_type_node *ctnode;
23166 pubname_ref pub;
23167 dw_die_ref base_type;
23169 #if ENABLE_ASSERT_CHECKING
23170 /* All the marks should already be clear. */
23171 verify_marks_clear (comp_unit_die ());
23172 for (node = limbo_die_list; node; node = node->next)
23173 verify_marks_clear (node->die);
23174 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23175 verify_marks_clear (ctnode->root_die);
23176 #endif /* ENABLE_ASSERT_CHECKING */
23178 /* Mark types that are used in global variables. */
23179 premark_types_used_by_global_vars ();
23181 /* Set the mark on nodes that are actually used. */
23182 prune_unused_types_walk (comp_unit_die ());
23183 for (node = limbo_die_list; node; node = node->next)
23184 prune_unused_types_walk (node->die);
23185 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23187 prune_unused_types_walk (ctnode->root_die);
23188 prune_unused_types_mark (ctnode->type_die, 1);
23191 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23192 are unusual in that they are pubnames that are the children of pubtypes.
23193 They should only be marked via their parent DW_TAG_enumeration_type die,
23194 not as roots in themselves. */
23195 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23196 if (pub->die->die_tag != DW_TAG_enumerator)
23197 prune_unused_types_mark (pub->die, 1);
23198 for (i = 0; base_types.iterate (i, &base_type); i++)
23199 prune_unused_types_mark (base_type, 1);
23201 if (debug_str_hash)
23202 debug_str_hash->empty ();
23203 if (skeleton_debug_str_hash)
23204 skeleton_debug_str_hash->empty ();
23205 prune_unused_types_prune (comp_unit_die ());
23206 for (node = limbo_die_list; node; node = node->next)
23207 prune_unused_types_prune (node->die);
23208 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23209 prune_unused_types_prune (ctnode->root_die);
23211 /* Leave the marks clear. */
23212 prune_unmark_dies (comp_unit_die ());
23213 for (node = limbo_die_list; node; node = node->next)
23214 prune_unmark_dies (node->die);
23215 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23216 prune_unmark_dies (ctnode->root_die);
23219 /* Set the parameter to true if there are any relative pathnames in
23220 the file table. */
23222 file_table_relative_p (dwarf_file_data **slot, bool *p)
23224 struct dwarf_file_data *d = *slot;
23225 if (!IS_ABSOLUTE_PATH (d->filename))
23227 *p = true;
23228 return 0;
23230 return 1;
23233 /* Helpers to manipulate hash table of comdat type units. */
23235 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23237 typedef comdat_type_node value_type;
23238 typedef comdat_type_node compare_type;
23239 static inline hashval_t hash (const value_type *);
23240 static inline bool equal (const value_type *, const compare_type *);
23243 inline hashval_t
23244 comdat_type_hasher::hash (const value_type *type_node)
23246 hashval_t h;
23247 memcpy (&h, type_node->signature, sizeof (h));
23248 return h;
23251 inline bool
23252 comdat_type_hasher::equal (const value_type *type_node_1,
23253 const compare_type *type_node_2)
23255 return (! memcmp (type_node_1->signature, type_node_2->signature,
23256 DWARF_TYPE_SIGNATURE_SIZE));
23259 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23260 to the location it would have been added, should we know its
23261 DECL_ASSEMBLER_NAME when we added other attributes. This will
23262 probably improve compactness of debug info, removing equivalent
23263 abbrevs, and hide any differences caused by deferring the
23264 computation of the assembler name, triggered by e.g. PCH. */
23266 static inline void
23267 move_linkage_attr (dw_die_ref die)
23269 unsigned ix = vec_safe_length (die->die_attr);
23270 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23272 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23273 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23275 while (--ix > 0)
23277 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23279 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23280 break;
23283 if (ix != vec_safe_length (die->die_attr) - 1)
23285 die->die_attr->pop ();
23286 die->die_attr->quick_insert (ix, linkage);
23290 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23291 referenced from typed stack ops and count how often they are used. */
23293 static void
23294 mark_base_types (dw_loc_descr_ref loc)
23296 dw_die_ref base_type = NULL;
23298 for (; loc; loc = loc->dw_loc_next)
23300 switch (loc->dw_loc_opc)
23302 case DW_OP_GNU_regval_type:
23303 case DW_OP_GNU_deref_type:
23304 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23305 break;
23306 case DW_OP_GNU_convert:
23307 case DW_OP_GNU_reinterpret:
23308 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23309 continue;
23310 /* FALLTHRU */
23311 case DW_OP_GNU_const_type:
23312 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23313 break;
23314 case DW_OP_GNU_entry_value:
23315 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23316 continue;
23317 default:
23318 continue;
23320 gcc_assert (base_type->die_parent == comp_unit_die ());
23321 if (base_type->die_mark)
23322 base_type->die_mark++;
23323 else
23325 base_types.safe_push (base_type);
23326 base_type->die_mark = 1;
23331 /* Comparison function for sorting marked base types. */
23333 static int
23334 base_type_cmp (const void *x, const void *y)
23336 dw_die_ref dx = *(const dw_die_ref *) x;
23337 dw_die_ref dy = *(const dw_die_ref *) y;
23338 unsigned int byte_size1, byte_size2;
23339 unsigned int encoding1, encoding2;
23340 if (dx->die_mark > dy->die_mark)
23341 return -1;
23342 if (dx->die_mark < dy->die_mark)
23343 return 1;
23344 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23345 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23346 if (byte_size1 < byte_size2)
23347 return 1;
23348 if (byte_size1 > byte_size2)
23349 return -1;
23350 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23351 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23352 if (encoding1 < encoding2)
23353 return 1;
23354 if (encoding1 > encoding2)
23355 return -1;
23356 return 0;
23359 /* Move base types marked by mark_base_types as early as possible
23360 in the CU, sorted by decreasing usage count both to make the
23361 uleb128 references as small as possible and to make sure they
23362 will have die_offset already computed by calc_die_sizes when
23363 sizes of typed stack loc ops is computed. */
23365 static void
23366 move_marked_base_types (void)
23368 unsigned int i;
23369 dw_die_ref base_type, die, c;
23371 if (base_types.is_empty ())
23372 return;
23374 /* Sort by decreasing usage count, they will be added again in that
23375 order later on. */
23376 base_types.qsort (base_type_cmp);
23377 die = comp_unit_die ();
23378 c = die->die_child;
23381 dw_die_ref prev = c;
23382 c = c->die_sib;
23383 while (c->die_mark)
23385 remove_child_with_prev (c, prev);
23386 /* As base types got marked, there must be at least
23387 one node other than DW_TAG_base_type. */
23388 gcc_assert (c != c->die_sib);
23389 c = c->die_sib;
23392 while (c != die->die_child);
23393 gcc_assert (die->die_child);
23394 c = die->die_child;
23395 for (i = 0; base_types.iterate (i, &base_type); i++)
23397 base_type->die_mark = 0;
23398 base_type->die_sib = c->die_sib;
23399 c->die_sib = base_type;
23400 c = base_type;
23404 /* Helper function for resolve_addr, attempt to resolve
23405 one CONST_STRING, return true if successful. Similarly verify that
23406 SYMBOL_REFs refer to variables emitted in the current CU. */
23408 static bool
23409 resolve_one_addr (rtx *addr)
23411 rtx rtl = *addr;
23413 if (GET_CODE (rtl) == CONST_STRING)
23415 size_t len = strlen (XSTR (rtl, 0)) + 1;
23416 tree t = build_string (len, XSTR (rtl, 0));
23417 tree tlen = size_int (len - 1);
23418 TREE_TYPE (t)
23419 = build_array_type (char_type_node, build_index_type (tlen));
23420 rtl = lookup_constant_def (t);
23421 if (!rtl || !MEM_P (rtl))
23422 return false;
23423 rtl = XEXP (rtl, 0);
23424 if (GET_CODE (rtl) == SYMBOL_REF
23425 && SYMBOL_REF_DECL (rtl)
23426 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23427 return false;
23428 vec_safe_push (used_rtx_array, rtl);
23429 *addr = rtl;
23430 return true;
23433 if (GET_CODE (rtl) == SYMBOL_REF
23434 && SYMBOL_REF_DECL (rtl))
23436 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23438 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23439 return false;
23441 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23442 return false;
23445 if (GET_CODE (rtl) == CONST)
23447 subrtx_ptr_iterator::array_type array;
23448 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23449 if (!resolve_one_addr (*iter))
23450 return false;
23453 return true;
23456 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23457 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23458 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23460 static rtx
23461 string_cst_pool_decl (tree t)
23463 rtx rtl = output_constant_def (t, 1);
23464 unsigned char *array;
23465 dw_loc_descr_ref l;
23466 tree decl;
23467 size_t len;
23468 dw_die_ref ref;
23470 if (!rtl || !MEM_P (rtl))
23471 return NULL_RTX;
23472 rtl = XEXP (rtl, 0);
23473 if (GET_CODE (rtl) != SYMBOL_REF
23474 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23475 return NULL_RTX;
23477 decl = SYMBOL_REF_DECL (rtl);
23478 if (!lookup_decl_die (decl))
23480 len = TREE_STRING_LENGTH (t);
23481 vec_safe_push (used_rtx_array, rtl);
23482 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23483 array = ggc_vec_alloc<unsigned char> (len);
23484 memcpy (array, TREE_STRING_POINTER (t), len);
23485 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23486 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23487 l->dw_loc_oprnd2.v.val_vec.length = len;
23488 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23489 l->dw_loc_oprnd2.v.val_vec.array = array;
23490 add_AT_loc (ref, DW_AT_location, l);
23491 equate_decl_number_to_die (decl, ref);
23493 return rtl;
23496 /* Helper function of resolve_addr_in_expr. LOC is
23497 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23498 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23499 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23500 with DW_OP_GNU_implicit_pointer if possible
23501 and return true, if unsuccessful, return false. */
23503 static bool
23504 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23506 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23507 HOST_WIDE_INT offset = 0;
23508 dw_die_ref ref = NULL;
23509 tree decl;
23511 if (GET_CODE (rtl) == CONST
23512 && GET_CODE (XEXP (rtl, 0)) == PLUS
23513 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23515 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23516 rtl = XEXP (XEXP (rtl, 0), 0);
23518 if (GET_CODE (rtl) == CONST_STRING)
23520 size_t len = strlen (XSTR (rtl, 0)) + 1;
23521 tree t = build_string (len, XSTR (rtl, 0));
23522 tree tlen = size_int (len - 1);
23524 TREE_TYPE (t)
23525 = build_array_type (char_type_node, build_index_type (tlen));
23526 rtl = string_cst_pool_decl (t);
23527 if (!rtl)
23528 return false;
23530 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23532 decl = SYMBOL_REF_DECL (rtl);
23533 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23535 ref = lookup_decl_die (decl);
23536 if (ref && (get_AT (ref, DW_AT_location)
23537 || get_AT (ref, DW_AT_const_value)))
23539 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23540 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23541 loc->dw_loc_oprnd1.val_entry = NULL;
23542 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23543 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23544 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23545 loc->dw_loc_oprnd2.v.val_int = offset;
23546 return true;
23550 return false;
23553 /* Helper function for resolve_addr, handle one location
23554 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23555 the location list couldn't be resolved. */
23557 static bool
23558 resolve_addr_in_expr (dw_loc_descr_ref loc)
23560 dw_loc_descr_ref keep = NULL;
23561 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23562 switch (loc->dw_loc_opc)
23564 case DW_OP_addr:
23565 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23567 if ((prev == NULL
23568 || prev->dw_loc_opc == DW_OP_piece
23569 || prev->dw_loc_opc == DW_OP_bit_piece)
23570 && loc->dw_loc_next
23571 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23572 && !dwarf_strict
23573 && optimize_one_addr_into_implicit_ptr (loc))
23574 break;
23575 return false;
23577 break;
23578 case DW_OP_GNU_addr_index:
23579 case DW_OP_GNU_const_index:
23580 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23581 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23583 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23584 if (!resolve_one_addr (&rtl))
23585 return false;
23586 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23587 loc->dw_loc_oprnd1.val_entry =
23588 add_addr_table_entry (rtl, ate_kind_rtx);
23590 break;
23591 case DW_OP_const4u:
23592 case DW_OP_const8u:
23593 if (loc->dtprel
23594 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23595 return false;
23596 break;
23597 case DW_OP_plus_uconst:
23598 if (size_of_loc_descr (loc)
23599 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23601 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23603 dw_loc_descr_ref repl
23604 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23605 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23606 add_loc_descr (&repl, loc->dw_loc_next);
23607 *loc = *repl;
23609 break;
23610 case DW_OP_implicit_value:
23611 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23612 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23613 return false;
23614 break;
23615 case DW_OP_GNU_implicit_pointer:
23616 case DW_OP_GNU_parameter_ref:
23617 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23619 dw_die_ref ref
23620 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23621 if (ref == NULL)
23622 return false;
23623 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23624 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23625 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23627 break;
23628 case DW_OP_GNU_const_type:
23629 case DW_OP_GNU_regval_type:
23630 case DW_OP_GNU_deref_type:
23631 case DW_OP_GNU_convert:
23632 case DW_OP_GNU_reinterpret:
23633 while (loc->dw_loc_next
23634 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23636 dw_die_ref base1, base2;
23637 unsigned enc1, enc2, size1, size2;
23638 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23639 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23640 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23641 else if (loc->dw_loc_oprnd1.val_class
23642 == dw_val_class_unsigned_const)
23643 break;
23644 else
23645 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23646 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23647 == dw_val_class_unsigned_const)
23648 break;
23649 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23650 gcc_assert (base1->die_tag == DW_TAG_base_type
23651 && base2->die_tag == DW_TAG_base_type);
23652 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23653 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23654 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23655 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23656 if (size1 == size2
23657 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23658 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23659 && loc != keep)
23660 || enc1 == enc2))
23662 /* Optimize away next DW_OP_GNU_convert after
23663 adjusting LOC's base type die reference. */
23664 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23665 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23666 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23667 else
23668 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23669 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23670 continue;
23672 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23673 point typed stack entry. */
23674 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23675 keep = loc->dw_loc_next;
23676 break;
23678 break;
23679 default:
23680 break;
23682 return true;
23685 /* Helper function of resolve_addr. DIE had DW_AT_location of
23686 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23687 and DW_OP_addr couldn't be resolved. resolve_addr has already
23688 removed the DW_AT_location attribute. This function attempts to
23689 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23690 to it or DW_AT_const_value attribute, if possible. */
23692 static void
23693 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23695 if (TREE_CODE (decl) != VAR_DECL
23696 || lookup_decl_die (decl) != die
23697 || DECL_EXTERNAL (decl)
23698 || !TREE_STATIC (decl)
23699 || DECL_INITIAL (decl) == NULL_TREE
23700 || DECL_P (DECL_INITIAL (decl))
23701 || get_AT (die, DW_AT_const_value))
23702 return;
23704 tree init = DECL_INITIAL (decl);
23705 HOST_WIDE_INT offset = 0;
23706 /* For variables that have been optimized away and thus
23707 don't have a memory location, see if we can emit
23708 DW_AT_const_value instead. */
23709 if (tree_add_const_value_attribute (die, init))
23710 return;
23711 if (dwarf_strict)
23712 return;
23713 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23714 and ADDR_EXPR refers to a decl that has DW_AT_location or
23715 DW_AT_const_value (but isn't addressable, otherwise
23716 resolving the original DW_OP_addr wouldn't fail), see if
23717 we can add DW_OP_GNU_implicit_pointer. */
23718 STRIP_NOPS (init);
23719 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23720 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23722 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23723 init = TREE_OPERAND (init, 0);
23724 STRIP_NOPS (init);
23726 if (TREE_CODE (init) != ADDR_EXPR)
23727 return;
23728 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23729 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23730 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23731 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23732 && TREE_OPERAND (init, 0) != decl))
23734 dw_die_ref ref;
23735 dw_loc_descr_ref l;
23737 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23739 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23740 if (!rtl)
23741 return;
23742 decl = SYMBOL_REF_DECL (rtl);
23744 else
23745 decl = TREE_OPERAND (init, 0);
23746 ref = lookup_decl_die (decl);
23747 if (ref == NULL
23748 || (!get_AT (ref, DW_AT_location)
23749 && !get_AT (ref, DW_AT_const_value)))
23750 return;
23751 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23752 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23753 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23754 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23755 add_AT_loc (die, DW_AT_location, l);
23759 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23760 an address in .rodata section if the string literal is emitted there,
23761 or remove the containing location list or replace DW_AT_const_value
23762 with DW_AT_location and empty location expression, if it isn't found
23763 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23764 to something that has been emitted in the current CU. */
23766 static void
23767 resolve_addr (dw_die_ref die)
23769 dw_die_ref c;
23770 dw_attr_ref a;
23771 dw_loc_list_ref *curr, *start, loc;
23772 unsigned ix;
23774 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23775 switch (AT_class (a))
23777 case dw_val_class_loc_list:
23778 start = curr = AT_loc_list_ptr (a);
23779 loc = *curr;
23780 gcc_assert (loc);
23781 /* The same list can be referenced more than once. See if we have
23782 already recorded the result from a previous pass. */
23783 if (loc->replaced)
23784 *curr = loc->dw_loc_next;
23785 else if (!loc->resolved_addr)
23787 /* As things stand, we do not expect or allow one die to
23788 reference a suffix of another die's location list chain.
23789 References must be identical or completely separate.
23790 There is therefore no need to cache the result of this
23791 pass on any list other than the first; doing so
23792 would lead to unnecessary writes. */
23793 while (*curr)
23795 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23796 if (!resolve_addr_in_expr ((*curr)->expr))
23798 dw_loc_list_ref next = (*curr)->dw_loc_next;
23799 dw_loc_descr_ref l = (*curr)->expr;
23801 if (next && (*curr)->ll_symbol)
23803 gcc_assert (!next->ll_symbol);
23804 next->ll_symbol = (*curr)->ll_symbol;
23806 if (dwarf_split_debug_info)
23807 remove_loc_list_addr_table_entries (l);
23808 *curr = next;
23810 else
23812 mark_base_types ((*curr)->expr);
23813 curr = &(*curr)->dw_loc_next;
23816 if (loc == *start)
23817 loc->resolved_addr = 1;
23818 else
23820 loc->replaced = 1;
23821 loc->dw_loc_next = *start;
23824 if (!*start)
23826 remove_AT (die, a->dw_attr);
23827 ix--;
23829 break;
23830 case dw_val_class_loc:
23832 dw_loc_descr_ref l = AT_loc (a);
23833 /* For -gdwarf-2 don't attempt to optimize
23834 DW_AT_data_member_location containing
23835 DW_OP_plus_uconst - older consumers might
23836 rely on it being that op instead of a more complex,
23837 but shorter, location description. */
23838 if ((dwarf_version > 2
23839 || a->dw_attr != DW_AT_data_member_location
23840 || l == NULL
23841 || l->dw_loc_opc != DW_OP_plus_uconst
23842 || l->dw_loc_next != NULL)
23843 && !resolve_addr_in_expr (l))
23845 if (dwarf_split_debug_info)
23846 remove_loc_list_addr_table_entries (l);
23847 if (l != NULL
23848 && l->dw_loc_next == NULL
23849 && l->dw_loc_opc == DW_OP_addr
23850 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23851 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23852 && a->dw_attr == DW_AT_location)
23854 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23855 remove_AT (die, a->dw_attr);
23856 ix--;
23857 optimize_location_into_implicit_ptr (die, decl);
23858 break;
23860 remove_AT (die, a->dw_attr);
23861 ix--;
23863 else
23864 mark_base_types (l);
23866 break;
23867 case dw_val_class_addr:
23868 if (a->dw_attr == DW_AT_const_value
23869 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23871 if (AT_index (a) != NOT_INDEXED)
23872 remove_addr_table_entry (a->dw_attr_val.val_entry);
23873 remove_AT (die, a->dw_attr);
23874 ix--;
23876 if (die->die_tag == DW_TAG_GNU_call_site
23877 && a->dw_attr == DW_AT_abstract_origin)
23879 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23880 dw_die_ref tdie = lookup_decl_die (tdecl);
23881 if (tdie == NULL
23882 && DECL_EXTERNAL (tdecl)
23883 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23885 force_decl_die (tdecl);
23886 tdie = lookup_decl_die (tdecl);
23888 if (tdie)
23890 a->dw_attr_val.val_class = dw_val_class_die_ref;
23891 a->dw_attr_val.v.val_die_ref.die = tdie;
23892 a->dw_attr_val.v.val_die_ref.external = 0;
23894 else
23896 if (AT_index (a) != NOT_INDEXED)
23897 remove_addr_table_entry (a->dw_attr_val.val_entry);
23898 remove_AT (die, a->dw_attr);
23899 ix--;
23902 break;
23903 default:
23904 break;
23907 FOR_EACH_CHILD (die, c, resolve_addr (c));
23910 /* Helper routines for optimize_location_lists.
23911 This pass tries to share identical local lists in .debug_loc
23912 section. */
23914 /* Iteratively hash operands of LOC opcode into HSTATE. */
23916 static void
23917 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23919 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23920 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23922 switch (loc->dw_loc_opc)
23924 case DW_OP_const4u:
23925 case DW_OP_const8u:
23926 if (loc->dtprel)
23927 goto hash_addr;
23928 /* FALLTHRU */
23929 case DW_OP_const1u:
23930 case DW_OP_const1s:
23931 case DW_OP_const2u:
23932 case DW_OP_const2s:
23933 case DW_OP_const4s:
23934 case DW_OP_const8s:
23935 case DW_OP_constu:
23936 case DW_OP_consts:
23937 case DW_OP_pick:
23938 case DW_OP_plus_uconst:
23939 case DW_OP_breg0:
23940 case DW_OP_breg1:
23941 case DW_OP_breg2:
23942 case DW_OP_breg3:
23943 case DW_OP_breg4:
23944 case DW_OP_breg5:
23945 case DW_OP_breg6:
23946 case DW_OP_breg7:
23947 case DW_OP_breg8:
23948 case DW_OP_breg9:
23949 case DW_OP_breg10:
23950 case DW_OP_breg11:
23951 case DW_OP_breg12:
23952 case DW_OP_breg13:
23953 case DW_OP_breg14:
23954 case DW_OP_breg15:
23955 case DW_OP_breg16:
23956 case DW_OP_breg17:
23957 case DW_OP_breg18:
23958 case DW_OP_breg19:
23959 case DW_OP_breg20:
23960 case DW_OP_breg21:
23961 case DW_OP_breg22:
23962 case DW_OP_breg23:
23963 case DW_OP_breg24:
23964 case DW_OP_breg25:
23965 case DW_OP_breg26:
23966 case DW_OP_breg27:
23967 case DW_OP_breg28:
23968 case DW_OP_breg29:
23969 case DW_OP_breg30:
23970 case DW_OP_breg31:
23971 case DW_OP_regx:
23972 case DW_OP_fbreg:
23973 case DW_OP_piece:
23974 case DW_OP_deref_size:
23975 case DW_OP_xderef_size:
23976 hstate.add_object (val1->v.val_int);
23977 break;
23978 case DW_OP_skip:
23979 case DW_OP_bra:
23981 int offset;
23983 gcc_assert (val1->val_class == dw_val_class_loc);
23984 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23985 hstate.add_object (offset);
23987 break;
23988 case DW_OP_implicit_value:
23989 hstate.add_object (val1->v.val_unsigned);
23990 switch (val2->val_class)
23992 case dw_val_class_const:
23993 hstate.add_object (val2->v.val_int);
23994 break;
23995 case dw_val_class_vec:
23997 unsigned int elt_size = val2->v.val_vec.elt_size;
23998 unsigned int len = val2->v.val_vec.length;
24000 hstate.add_int (elt_size);
24001 hstate.add_int (len);
24002 hstate.add (val2->v.val_vec.array, len * elt_size);
24004 break;
24005 case dw_val_class_const_double:
24006 hstate.add_object (val2->v.val_double.low);
24007 hstate.add_object (val2->v.val_double.high);
24008 break;
24009 case dw_val_class_wide_int:
24010 hstate.add_object (*val2->v.val_wide);
24011 break;
24012 case dw_val_class_addr:
24013 inchash::add_rtx (val2->v.val_addr, hstate);
24014 break;
24015 default:
24016 gcc_unreachable ();
24018 break;
24019 case DW_OP_bregx:
24020 case DW_OP_bit_piece:
24021 hstate.add_object (val1->v.val_int);
24022 hstate.add_object (val2->v.val_int);
24023 break;
24024 case DW_OP_addr:
24025 hash_addr:
24026 if (loc->dtprel)
24028 unsigned char dtprel = 0xd1;
24029 hstate.add_object (dtprel);
24031 inchash::add_rtx (val1->v.val_addr, hstate);
24032 break;
24033 case DW_OP_GNU_addr_index:
24034 case DW_OP_GNU_const_index:
24036 if (loc->dtprel)
24038 unsigned char dtprel = 0xd1;
24039 hstate.add_object (dtprel);
24041 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24043 break;
24044 case DW_OP_GNU_implicit_pointer:
24045 hstate.add_int (val2->v.val_int);
24046 break;
24047 case DW_OP_GNU_entry_value:
24048 hstate.add_object (val1->v.val_loc);
24049 break;
24050 case DW_OP_GNU_regval_type:
24051 case DW_OP_GNU_deref_type:
24053 unsigned int byte_size
24054 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24055 unsigned int encoding
24056 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24057 hstate.add_object (val1->v.val_int);
24058 hstate.add_object (byte_size);
24059 hstate.add_object (encoding);
24061 break;
24062 case DW_OP_GNU_convert:
24063 case DW_OP_GNU_reinterpret:
24064 if (val1->val_class == dw_val_class_unsigned_const)
24066 hstate.add_object (val1->v.val_unsigned);
24067 break;
24069 /* FALLTHRU */
24070 case DW_OP_GNU_const_type:
24072 unsigned int byte_size
24073 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24074 unsigned int encoding
24075 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24076 hstate.add_object (byte_size);
24077 hstate.add_object (encoding);
24078 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24079 break;
24080 hstate.add_object (val2->val_class);
24081 switch (val2->val_class)
24083 case dw_val_class_const:
24084 hstate.add_object (val2->v.val_int);
24085 break;
24086 case dw_val_class_vec:
24088 unsigned int elt_size = val2->v.val_vec.elt_size;
24089 unsigned int len = val2->v.val_vec.length;
24091 hstate.add_object (elt_size);
24092 hstate.add_object (len);
24093 hstate.add (val2->v.val_vec.array, len * elt_size);
24095 break;
24096 case dw_val_class_const_double:
24097 hstate.add_object (val2->v.val_double.low);
24098 hstate.add_object (val2->v.val_double.high);
24099 break;
24100 case dw_val_class_wide_int:
24101 hstate.add_object (*val2->v.val_wide);
24102 break;
24103 default:
24104 gcc_unreachable ();
24107 break;
24109 default:
24110 /* Other codes have no operands. */
24111 break;
24115 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24117 static inline void
24118 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24120 dw_loc_descr_ref l;
24121 bool sizes_computed = false;
24122 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24123 size_of_locs (loc);
24125 for (l = loc; l != NULL; l = l->dw_loc_next)
24127 enum dwarf_location_atom opc = l->dw_loc_opc;
24128 hstate.add_object (opc);
24129 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24131 size_of_locs (loc);
24132 sizes_computed = true;
24134 hash_loc_operands (l, hstate);
24138 /* Compute hash of the whole location list LIST_HEAD. */
24140 static inline void
24141 hash_loc_list (dw_loc_list_ref list_head)
24143 dw_loc_list_ref curr = list_head;
24144 inchash::hash hstate;
24146 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24148 hstate.add (curr->begin, strlen (curr->begin) + 1);
24149 hstate.add (curr->end, strlen (curr->end) + 1);
24150 if (curr->section)
24151 hstate.add (curr->section, strlen (curr->section) + 1);
24152 hash_locs (curr->expr, hstate);
24154 list_head->hash = hstate.end ();
24157 /* Return true if X and Y opcodes have the same operands. */
24159 static inline bool
24160 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24162 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24163 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24164 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24165 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24167 switch (x->dw_loc_opc)
24169 case DW_OP_const4u:
24170 case DW_OP_const8u:
24171 if (x->dtprel)
24172 goto hash_addr;
24173 /* FALLTHRU */
24174 case DW_OP_const1u:
24175 case DW_OP_const1s:
24176 case DW_OP_const2u:
24177 case DW_OP_const2s:
24178 case DW_OP_const4s:
24179 case DW_OP_const8s:
24180 case DW_OP_constu:
24181 case DW_OP_consts:
24182 case DW_OP_pick:
24183 case DW_OP_plus_uconst:
24184 case DW_OP_breg0:
24185 case DW_OP_breg1:
24186 case DW_OP_breg2:
24187 case DW_OP_breg3:
24188 case DW_OP_breg4:
24189 case DW_OP_breg5:
24190 case DW_OP_breg6:
24191 case DW_OP_breg7:
24192 case DW_OP_breg8:
24193 case DW_OP_breg9:
24194 case DW_OP_breg10:
24195 case DW_OP_breg11:
24196 case DW_OP_breg12:
24197 case DW_OP_breg13:
24198 case DW_OP_breg14:
24199 case DW_OP_breg15:
24200 case DW_OP_breg16:
24201 case DW_OP_breg17:
24202 case DW_OP_breg18:
24203 case DW_OP_breg19:
24204 case DW_OP_breg20:
24205 case DW_OP_breg21:
24206 case DW_OP_breg22:
24207 case DW_OP_breg23:
24208 case DW_OP_breg24:
24209 case DW_OP_breg25:
24210 case DW_OP_breg26:
24211 case DW_OP_breg27:
24212 case DW_OP_breg28:
24213 case DW_OP_breg29:
24214 case DW_OP_breg30:
24215 case DW_OP_breg31:
24216 case DW_OP_regx:
24217 case DW_OP_fbreg:
24218 case DW_OP_piece:
24219 case DW_OP_deref_size:
24220 case DW_OP_xderef_size:
24221 return valx1->v.val_int == valy1->v.val_int;
24222 case DW_OP_skip:
24223 case DW_OP_bra:
24224 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24225 can cause irrelevant differences in dw_loc_addr. */
24226 gcc_assert (valx1->val_class == dw_val_class_loc
24227 && valy1->val_class == dw_val_class_loc
24228 && (dwarf_split_debug_info
24229 || x->dw_loc_addr == y->dw_loc_addr));
24230 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24231 case DW_OP_implicit_value:
24232 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24233 || valx2->val_class != valy2->val_class)
24234 return false;
24235 switch (valx2->val_class)
24237 case dw_val_class_const:
24238 return valx2->v.val_int == valy2->v.val_int;
24239 case dw_val_class_vec:
24240 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24241 && valx2->v.val_vec.length == valy2->v.val_vec.length
24242 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24243 valx2->v.val_vec.elt_size
24244 * valx2->v.val_vec.length) == 0;
24245 case dw_val_class_const_double:
24246 return valx2->v.val_double.low == valy2->v.val_double.low
24247 && valx2->v.val_double.high == valy2->v.val_double.high;
24248 case dw_val_class_wide_int:
24249 return *valx2->v.val_wide == *valy2->v.val_wide;
24250 case dw_val_class_addr:
24251 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24252 default:
24253 gcc_unreachable ();
24255 case DW_OP_bregx:
24256 case DW_OP_bit_piece:
24257 return valx1->v.val_int == valy1->v.val_int
24258 && valx2->v.val_int == valy2->v.val_int;
24259 case DW_OP_addr:
24260 hash_addr:
24261 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24262 case DW_OP_GNU_addr_index:
24263 case DW_OP_GNU_const_index:
24265 rtx ax1 = valx1->val_entry->addr.rtl;
24266 rtx ay1 = valy1->val_entry->addr.rtl;
24267 return rtx_equal_p (ax1, ay1);
24269 case DW_OP_GNU_implicit_pointer:
24270 return valx1->val_class == dw_val_class_die_ref
24271 && valx1->val_class == valy1->val_class
24272 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24273 && valx2->v.val_int == valy2->v.val_int;
24274 case DW_OP_GNU_entry_value:
24275 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24276 case DW_OP_GNU_const_type:
24277 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24278 || valx2->val_class != valy2->val_class)
24279 return false;
24280 switch (valx2->val_class)
24282 case dw_val_class_const:
24283 return valx2->v.val_int == valy2->v.val_int;
24284 case dw_val_class_vec:
24285 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24286 && valx2->v.val_vec.length == valy2->v.val_vec.length
24287 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24288 valx2->v.val_vec.elt_size
24289 * valx2->v.val_vec.length) == 0;
24290 case dw_val_class_const_double:
24291 return valx2->v.val_double.low == valy2->v.val_double.low
24292 && valx2->v.val_double.high == valy2->v.val_double.high;
24293 case dw_val_class_wide_int:
24294 return *valx2->v.val_wide == *valy2->v.val_wide;
24295 default:
24296 gcc_unreachable ();
24298 case DW_OP_GNU_regval_type:
24299 case DW_OP_GNU_deref_type:
24300 return valx1->v.val_int == valy1->v.val_int
24301 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24302 case DW_OP_GNU_convert:
24303 case DW_OP_GNU_reinterpret:
24304 if (valx1->val_class != valy1->val_class)
24305 return false;
24306 if (valx1->val_class == dw_val_class_unsigned_const)
24307 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24308 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24309 case DW_OP_GNU_parameter_ref:
24310 return valx1->val_class == dw_val_class_die_ref
24311 && valx1->val_class == valy1->val_class
24312 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24313 default:
24314 /* Other codes have no operands. */
24315 return true;
24319 /* Return true if DWARF location expressions X and Y are the same. */
24321 static inline bool
24322 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24324 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24325 if (x->dw_loc_opc != y->dw_loc_opc
24326 || x->dtprel != y->dtprel
24327 || !compare_loc_operands (x, y))
24328 break;
24329 return x == NULL && y == NULL;
24332 /* Hashtable helpers. */
24334 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24336 typedef dw_loc_list_struct value_type;
24337 typedef dw_loc_list_struct compare_type;
24338 static inline hashval_t hash (const value_type *);
24339 static inline bool equal (const value_type *, const compare_type *);
24342 /* Return precomputed hash of location list X. */
24344 inline hashval_t
24345 loc_list_hasher::hash (const value_type *x)
24347 return x->hash;
24350 /* Return true if location lists A and B are the same. */
24352 inline bool
24353 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24355 if (a == b)
24356 return 1;
24357 if (a->hash != b->hash)
24358 return 0;
24359 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24360 if (strcmp (a->begin, b->begin) != 0
24361 || strcmp (a->end, b->end) != 0
24362 || (a->section == NULL) != (b->section == NULL)
24363 || (a->section && strcmp (a->section, b->section) != 0)
24364 || !compare_locs (a->expr, b->expr))
24365 break;
24366 return a == NULL && b == NULL;
24369 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24372 /* Recursively optimize location lists referenced from DIE
24373 children and share them whenever possible. */
24375 static void
24376 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24378 dw_die_ref c;
24379 dw_attr_ref a;
24380 unsigned ix;
24381 dw_loc_list_struct **slot;
24383 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24384 if (AT_class (a) == dw_val_class_loc_list)
24386 dw_loc_list_ref list = AT_loc_list (a);
24387 /* TODO: perform some optimizations here, before hashing
24388 it and storing into the hash table. */
24389 hash_loc_list (list);
24390 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24391 if (*slot == NULL)
24392 *slot = list;
24393 else
24394 a->dw_attr_val.v.val_loc_list = *slot;
24397 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24401 /* Recursively assign each location list a unique index into the debug_addr
24402 section. */
24404 static void
24405 index_location_lists (dw_die_ref die)
24407 dw_die_ref c;
24408 dw_attr_ref a;
24409 unsigned ix;
24411 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24412 if (AT_class (a) == dw_val_class_loc_list)
24414 dw_loc_list_ref list = AT_loc_list (a);
24415 dw_loc_list_ref curr;
24416 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24418 /* Don't index an entry that has already been indexed
24419 or won't be output. */
24420 if (curr->begin_entry != NULL
24421 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24422 continue;
24424 curr->begin_entry
24425 = add_addr_table_entry (xstrdup (curr->begin),
24426 ate_kind_label);
24430 FOR_EACH_CHILD (die, c, index_location_lists (c));
24433 /* Optimize location lists referenced from DIE
24434 children and share them whenever possible. */
24436 static void
24437 optimize_location_lists (dw_die_ref die)
24439 loc_list_hash_type htab (500);
24440 optimize_location_lists_1 (die, &htab);
24443 /* Output stuff that dwarf requires at the end of every file,
24444 and generate the DWARF-2 debugging info. */
24446 static void
24447 dwarf2out_finish (const char *filename)
24449 limbo_die_node *node, *next_node;
24450 comdat_type_node *ctnode;
24451 unsigned int i;
24452 dw_die_ref main_comp_unit_die;
24454 /* PCH might result in DW_AT_producer string being restored from the
24455 header compilation, so always fill it with empty string initially
24456 and overwrite only here. */
24457 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24458 producer_string = gen_producer_string ();
24459 producer->dw_attr_val.v.val_str->refcount--;
24460 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24462 gen_scheduled_generic_parms_dies ();
24463 gen_remaining_tmpl_value_param_die_attribute ();
24465 /* Add the name for the main input file now. We delayed this from
24466 dwarf2out_init to avoid complications with PCH. */
24467 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24468 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24469 add_comp_dir_attribute (comp_unit_die ());
24470 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24472 bool p = false;
24473 file_table->traverse<bool *, file_table_relative_p> (&p);
24474 if (p)
24475 add_comp_dir_attribute (comp_unit_die ());
24478 if (deferred_locations_list)
24479 for (i = 0; i < deferred_locations_list->length (); i++)
24481 add_location_or_const_value_attribute (
24482 (*deferred_locations_list)[i].die,
24483 (*deferred_locations_list)[i].variable,
24484 false,
24485 DW_AT_location);
24488 /* Traverse the limbo die list, and add parent/child links. The only
24489 dies without parents that should be here are concrete instances of
24490 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24491 For concrete instances, we can get the parent die from the abstract
24492 instance. */
24493 for (node = limbo_die_list; node; node = next_node)
24495 dw_die_ref die = node->die;
24496 next_node = node->next;
24498 if (die->die_parent == NULL)
24500 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24502 if (origin && origin->die_parent)
24503 add_child_die (origin->die_parent, die);
24504 else if (is_cu_die (die))
24506 else if (seen_error ())
24507 /* It's OK to be confused by errors in the input. */
24508 add_child_die (comp_unit_die (), die);
24509 else
24511 /* In certain situations, the lexical block containing a
24512 nested function can be optimized away, which results
24513 in the nested function die being orphaned. Likewise
24514 with the return type of that nested function. Force
24515 this to be a child of the containing function.
24517 It may happen that even the containing function got fully
24518 inlined and optimized out. In that case we are lost and
24519 assign the empty child. This should not be big issue as
24520 the function is likely unreachable too. */
24521 gcc_assert (node->created_for);
24523 if (DECL_P (node->created_for))
24524 origin = get_context_die (DECL_CONTEXT (node->created_for));
24525 else if (TYPE_P (node->created_for))
24526 origin = scope_die_for (node->created_for, comp_unit_die ());
24527 else
24528 origin = comp_unit_die ();
24530 add_child_die (origin, die);
24535 limbo_die_list = NULL;
24537 #if ENABLE_ASSERT_CHECKING
24539 dw_die_ref die = comp_unit_die (), c;
24540 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24542 #endif
24543 resolve_addr (comp_unit_die ());
24544 move_marked_base_types ();
24546 for (node = deferred_asm_name; node; node = node->next)
24548 tree decl = node->created_for;
24549 /* When generating LTO bytecode we can not generate new assembler
24550 names at this point and all important decls got theirs via
24551 free-lang-data. */
24552 if (((!flag_generate_lto && !flag_generate_offload)
24553 || DECL_ASSEMBLER_NAME_SET_P (decl))
24554 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24556 add_linkage_attr (node->die, decl);
24557 move_linkage_attr (node->die);
24561 deferred_asm_name = NULL;
24563 /* Walk through the list of incomplete types again, trying once more to
24564 emit full debugging info for them. */
24565 retry_incomplete_types ();
24567 if (flag_eliminate_unused_debug_types)
24568 prune_unused_types ();
24570 /* Generate separate COMDAT sections for type DIEs. */
24571 if (use_debug_types)
24573 break_out_comdat_types (comp_unit_die ());
24575 /* Each new type_unit DIE was added to the limbo die list when created.
24576 Since these have all been added to comdat_type_list, clear the
24577 limbo die list. */
24578 limbo_die_list = NULL;
24580 /* For each new comdat type unit, copy declarations for incomplete
24581 types to make the new unit self-contained (i.e., no direct
24582 references to the main compile unit). */
24583 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24584 copy_decls_for_unworthy_types (ctnode->root_die);
24585 copy_decls_for_unworthy_types (comp_unit_die ());
24587 /* In the process of copying declarations from one unit to another,
24588 we may have left some declarations behind that are no longer
24589 referenced. Prune them. */
24590 prune_unused_types ();
24593 /* Generate separate CUs for each of the include files we've seen.
24594 They will go into limbo_die_list. */
24595 if (flag_eliminate_dwarf2_dups)
24596 break_out_includes (comp_unit_die ());
24598 /* Traverse the DIE's and add add sibling attributes to those DIE's
24599 that have children. */
24600 add_sibling_attributes (comp_unit_die ());
24601 for (node = limbo_die_list; node; node = node->next)
24602 add_sibling_attributes (node->die);
24603 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24604 add_sibling_attributes (ctnode->root_die);
24606 /* When splitting DWARF info, we put some attributes in the
24607 skeleton compile_unit DIE that remains in the .o, while
24608 most attributes go in the DWO compile_unit_die. */
24609 if (dwarf_split_debug_info)
24610 main_comp_unit_die = gen_compile_unit_die (NULL);
24611 else
24612 main_comp_unit_die = comp_unit_die ();
24614 /* Output a terminator label for the .text section. */
24615 switch_to_section (text_section);
24616 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24617 if (cold_text_section)
24619 switch_to_section (cold_text_section);
24620 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24623 /* We can only use the low/high_pc attributes if all of the code was
24624 in .text. */
24625 if (!have_multiple_function_sections
24626 || (dwarf_version < 3 && dwarf_strict))
24628 /* Don't add if the CU has no associated code. */
24629 if (text_section_used)
24630 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24631 text_end_label, true);
24633 else
24635 unsigned fde_idx;
24636 dw_fde_ref fde;
24637 bool range_list_added = false;
24639 if (text_section_used)
24640 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24641 text_end_label, &range_list_added, true);
24642 if (cold_text_section_used)
24643 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24644 cold_end_label, &range_list_added, true);
24646 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24648 if (DECL_IGNORED_P (fde->decl))
24649 continue;
24650 if (!fde->in_std_section)
24651 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24652 fde->dw_fde_end, &range_list_added,
24653 true);
24654 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24655 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24656 fde->dw_fde_second_end, &range_list_added,
24657 true);
24660 if (range_list_added)
24662 /* We need to give .debug_loc and .debug_ranges an appropriate
24663 "base address". Use zero so that these addresses become
24664 absolute. Historically, we've emitted the unexpected
24665 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24666 Emit both to give time for other tools to adapt. */
24667 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24668 if (! dwarf_strict && dwarf_version < 4)
24669 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24671 add_ranges (NULL);
24675 if (debug_info_level >= DINFO_LEVEL_TERSE)
24676 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24677 debug_line_section_label);
24679 if (have_macinfo)
24680 add_AT_macptr (comp_unit_die (),
24681 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24682 macinfo_section_label);
24684 if (dwarf_split_debug_info)
24686 /* optimize_location_lists calculates the size of the lists,
24687 so index them first, and assign indices to the entries.
24688 Although optimize_location_lists will remove entries from
24689 the table, it only does so for duplicates, and therefore
24690 only reduces ref_counts to 1. */
24691 index_location_lists (comp_unit_die ());
24693 if (addr_index_table != NULL)
24695 unsigned int index = 0;
24696 addr_index_table
24697 ->traverse_noresize<unsigned int *, index_addr_table_entry>
24698 (&index);
24702 if (have_location_lists)
24703 optimize_location_lists (comp_unit_die ());
24705 save_macinfo_strings ();
24707 if (dwarf_split_debug_info)
24709 unsigned int index = 0;
24711 /* Add attributes common to skeleton compile_units and
24712 type_units. Because these attributes include strings, it
24713 must be done before freezing the string table. Top-level
24714 skeleton die attrs are added when the skeleton type unit is
24715 created, so ensure it is created by this point. */
24716 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24717 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24720 /* Output all of the compilation units. We put the main one last so that
24721 the offsets are available to output_pubnames. */
24722 for (node = limbo_die_list; node; node = node->next)
24723 output_comp_unit (node->die, 0);
24725 hash_table<comdat_type_hasher> comdat_type_table (100);
24726 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24728 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24730 /* Don't output duplicate types. */
24731 if (*slot != HTAB_EMPTY_ENTRY)
24732 continue;
24734 /* Add a pointer to the line table for the main compilation unit
24735 so that the debugger can make sense of DW_AT_decl_file
24736 attributes. */
24737 if (debug_info_level >= DINFO_LEVEL_TERSE)
24738 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24739 (!dwarf_split_debug_info
24740 ? debug_line_section_label
24741 : debug_skeleton_line_section_label));
24743 output_comdat_type_unit (ctnode);
24744 *slot = ctnode;
24747 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24748 both the main_cu and all skeleton TUs. Making this call unconditional
24749 would end up either adding a second copy of the AT_pubnames attribute, or
24750 requiring a special case in add_top_level_skeleton_die_attrs. */
24751 if (!dwarf_split_debug_info)
24752 add_AT_pubnames (comp_unit_die ());
24754 if (dwarf_split_debug_info)
24756 int mark;
24757 unsigned char checksum[16];
24758 struct md5_ctx ctx;
24760 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24761 md5_init_ctx (&ctx);
24762 mark = 0;
24763 die_checksum (comp_unit_die (), &ctx, &mark);
24764 unmark_all_dies (comp_unit_die ());
24765 md5_finish_ctx (&ctx, checksum);
24767 /* Use the first 8 bytes of the checksum as the dwo_id,
24768 and add it to both comp-unit DIEs. */
24769 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24770 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24772 /* Add the base offset of the ranges table to the skeleton
24773 comp-unit DIE. */
24774 if (ranges_table_in_use)
24775 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24776 ranges_section_label);
24778 switch_to_section (debug_addr_section);
24779 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24780 output_addr_table ();
24783 /* Output the main compilation unit if non-empty or if .debug_macinfo
24784 or .debug_macro will be emitted. */
24785 output_comp_unit (comp_unit_die (), have_macinfo);
24787 if (dwarf_split_debug_info && info_section_emitted)
24788 output_skeleton_debug_sections (main_comp_unit_die);
24790 /* Output the abbreviation table. */
24791 if (abbrev_die_table_in_use != 1)
24793 switch_to_section (debug_abbrev_section);
24794 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24795 output_abbrev_section ();
24798 /* Output location list section if necessary. */
24799 if (have_location_lists)
24801 /* Output the location lists info. */
24802 switch_to_section (debug_loc_section);
24803 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24804 output_location_lists (comp_unit_die ());
24807 output_pubtables ();
24809 /* Output the address range information if a CU (.debug_info section)
24810 was emitted. We output an empty table even if we had no functions
24811 to put in it. This because the consumer has no way to tell the
24812 difference between an empty table that we omitted and failure to
24813 generate a table that would have contained data. */
24814 if (info_section_emitted)
24816 unsigned long aranges_length = size_of_aranges ();
24818 switch_to_section (debug_aranges_section);
24819 output_aranges (aranges_length);
24822 /* Output ranges section if necessary. */
24823 if (ranges_table_in_use)
24825 switch_to_section (debug_ranges_section);
24826 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24827 output_ranges ();
24830 /* Have to end the macro section. */
24831 if (have_macinfo)
24833 switch_to_section (debug_macinfo_section);
24834 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24835 output_macinfo ();
24836 dw2_asm_output_data (1, 0, "End compilation unit");
24839 /* Output the source line correspondence table. We must do this
24840 even if there is no line information. Otherwise, on an empty
24841 translation unit, we will generate a present, but empty,
24842 .debug_info section. IRIX 6.5 `nm' will then complain when
24843 examining the file. This is done late so that any filenames
24844 used by the debug_info section are marked as 'used'. */
24845 switch_to_section (debug_line_section);
24846 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24847 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24848 output_line_info (false);
24850 if (dwarf_split_debug_info && info_section_emitted)
24852 switch_to_section (debug_skeleton_line_section);
24853 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24854 output_line_info (true);
24857 /* If we emitted any indirect strings, output the string table too. */
24858 if (debug_str_hash || skeleton_debug_str_hash)
24859 output_indirect_strings ();
24862 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24863 within the same process. For use by toplev::finalize. */
24865 void
24866 dwarf2out_c_finalize (void)
24868 last_var_location_insn = NULL;
24869 cached_next_real_insn = NULL;
24870 used_rtx_array = NULL;
24871 incomplete_types = NULL;
24872 decl_scope_table = NULL;
24873 debug_info_section = NULL;
24874 debug_skeleton_info_section = NULL;
24875 debug_abbrev_section = NULL;
24876 debug_skeleton_abbrev_section = NULL;
24877 debug_aranges_section = NULL;
24878 debug_addr_section = NULL;
24879 debug_macinfo_section = NULL;
24880 debug_line_section = NULL;
24881 debug_skeleton_line_section = NULL;
24882 debug_loc_section = NULL;
24883 debug_pubnames_section = NULL;
24884 debug_pubtypes_section = NULL;
24885 debug_str_section = NULL;
24886 debug_str_dwo_section = NULL;
24887 debug_str_offsets_section = NULL;
24888 debug_ranges_section = NULL;
24889 debug_frame_section = NULL;
24890 fde_vec = NULL;
24891 debug_str_hash = NULL;
24892 skeleton_debug_str_hash = NULL;
24893 dw2_string_counter = 0;
24894 have_multiple_function_sections = false;
24895 text_section_used = false;
24896 cold_text_section_used = false;
24897 cold_text_section = NULL;
24898 current_unit_personality = NULL;
24900 deferred_locations_list = NULL;
24902 next_die_offset = 0;
24903 single_comp_unit_die = NULL;
24904 comdat_type_list = NULL;
24905 limbo_die_list = NULL;
24906 deferred_asm_name = NULL;
24907 file_table = NULL;
24908 decl_die_table = NULL;
24909 common_block_die_table = NULL;
24910 decl_loc_table = NULL;
24911 call_arg_locations = NULL;
24912 call_arg_loc_last = NULL;
24913 call_site_count = -1;
24914 tail_call_site_count = -1;
24915 //block_map = NULL;
24916 cached_dw_loc_list_table = NULL;
24917 abbrev_die_table = NULL;
24918 abbrev_die_table_allocated = 0;
24919 abbrev_die_table_in_use = 0;
24920 line_info_label_num = 0;
24921 cur_line_info_table = NULL;
24922 text_section_line_info = NULL;
24923 cold_text_section_line_info = NULL;
24924 separate_line_info = NULL;
24925 info_section_emitted = false;
24926 pubname_table = NULL;
24927 pubtype_table = NULL;
24928 macinfo_table = NULL;
24929 ranges_table = NULL;
24930 ranges_table_allocated = 0;
24931 ranges_table_in_use = 0;
24932 ranges_by_label = 0;
24933 ranges_by_label_allocated = 0;
24934 ranges_by_label_in_use = 0;
24935 have_location_lists = false;
24936 loclabel_num = 0;
24937 poc_label_num = 0;
24938 last_emitted_file = NULL;
24939 label_num = 0;
24940 file_table_last_lookup = NULL;
24941 tmpl_value_parm_die_table = NULL;
24942 generic_type_instances = NULL;
24943 frame_pointer_fb_offset = 0;
24944 frame_pointer_fb_offset_valid = false;
24945 base_types.release ();
24946 XDELETEVEC (producer_string);
24947 producer_string = NULL;
24950 #include "gt-dwarf2out.h"