Merge trunk version 221103 into gupc branch.
[official-gcc.git] / gcc / dwarf2out.c
blob2c1a05aa329de4e61ec957d92567bcd19de2a2ee
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "hash-set.h"
64 #include "machmode.h"
65 #include "vec.h"
66 #include "double-int.h"
67 #include "input.h"
68 #include "alias.h"
69 #include "symtab.h"
70 #include "wide-int.h"
71 #include "inchash.h"
72 #include "real.h"
73 #include "tree.h"
74 #include "tree-upc.h"
75 #include "fold-const.h"
76 #include "stringpool.h"
77 #include "stor-layout.h"
78 #include "varasm.h"
79 #include "hashtab.h"
80 #include "hard-reg-set.h"
81 #include "function.h"
82 #include "emit-rtl.h"
83 #include "hash-table.h"
84 #include "version.h"
85 #include "flags.h"
86 #include "regs.h"
87 #include "rtlhash.h"
88 #include "insn-config.h"
89 #include "reload.h"
90 #include "output.h"
91 #include "statistics.h"
92 #include "fixed-value.h"
93 #include "expmed.h"
94 #include "dojump.h"
95 #include "explow.h"
96 #include "calls.h"
97 #include "stmt.h"
98 #include "expr.h"
99 #include "except.h"
100 #include "dwarf2.h"
101 #include "dwarf2out.h"
102 #include "dwarf2asm.h"
103 #include "toplev.h"
104 #include "md5.h"
105 #include "tm_p.h"
106 #include "diagnostic.h"
107 #include "tree-pretty-print.h"
108 #include "debug.h"
109 #include "target.h"
110 #include "common/common-target.h"
111 #include "langhooks.h"
112 #include "hash-map.h"
113 #include "is-a.h"
114 #include "plugin-api.h"
115 #include "ipa-ref.h"
116 #include "cgraph.h"
117 #include "ira.h"
118 #include "lra.h"
119 #include "dumpfile.h"
120 #include "opts.h"
121 #include "tree-dfa.h"
122 #include "gdb/gdb-index.h"
123 #include "rtl-iter.h"
125 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
126 static rtx_insn *last_var_location_insn;
127 static rtx_insn *cached_next_real_insn;
128 static void dwarf2out_decl (tree);
130 #ifdef VMS_DEBUGGING_INFO
131 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
133 /* Define this macro to be a nonzero value if the directory specifications
134 which are output in the debug info should end with a separator. */
135 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
136 /* Define this macro to evaluate to a nonzero value if GCC should refrain
137 from generating indirect strings in DWARF2 debug information, for instance
138 if your target is stuck with an old version of GDB that is unable to
139 process them properly or uses VMS Debug. */
140 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
141 #else
142 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
143 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
144 #endif
146 /* ??? Poison these here until it can be done generically. They've been
147 totally replaced in this file; make sure it stays that way. */
148 #undef DWARF2_UNWIND_INFO
149 #undef DWARF2_FRAME_INFO
150 #if (GCC_VERSION >= 3000)
151 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
152 #endif
154 /* The size of the target's pointer type. */
155 #ifndef PTR_SIZE
156 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
157 #endif
159 /* Array of RTXes referenced by the debugging information, which therefore
160 must be kept around forever. */
161 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
163 /* A pointer to the base of a list of incomplete types which might be
164 completed at some later time. incomplete_types_list needs to be a
165 vec<tree, va_gc> *because we want to tell the garbage collector about
166 it. */
167 static GTY(()) vec<tree, va_gc> *incomplete_types;
169 /* A pointer to the base of a table of references to declaration
170 scopes. This table is a display which tracks the nesting
171 of declaration scopes at the current scope and containing
172 scopes. This table is used to find the proper place to
173 define type declaration DIE's. */
174 static GTY(()) vec<tree, va_gc> *decl_scope_table;
176 /* Pointers to various DWARF2 sections. */
177 static GTY(()) section *debug_info_section;
178 static GTY(()) section *debug_skeleton_info_section;
179 static GTY(()) section *debug_abbrev_section;
180 static GTY(()) section *debug_skeleton_abbrev_section;
181 static GTY(()) section *debug_aranges_section;
182 static GTY(()) section *debug_addr_section;
183 static GTY(()) section *debug_macinfo_section;
184 static GTY(()) section *debug_line_section;
185 static GTY(()) section *debug_skeleton_line_section;
186 static GTY(()) section *debug_loc_section;
187 static GTY(()) section *debug_pubnames_section;
188 static GTY(()) section *debug_pubtypes_section;
189 static GTY(()) section *debug_str_section;
190 static GTY(()) section *debug_str_dwo_section;
191 static GTY(()) section *debug_str_offsets_section;
192 static GTY(()) section *debug_ranges_section;
193 static GTY(()) section *debug_frame_section;
195 /* Maximum size (in bytes) of an artificially generated label. */
196 #define MAX_ARTIFICIAL_LABEL_BYTES 30
198 /* According to the (draft) DWARF 3 specification, the initial length
199 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
200 bytes are 0xffffffff, followed by the length stored in the next 8
201 bytes.
203 However, the SGI/MIPS ABI uses an initial length which is equal to
204 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
206 #ifndef DWARF_INITIAL_LENGTH_SIZE
207 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
208 #endif
210 /* Round SIZE up to the nearest BOUNDARY. */
211 #define DWARF_ROUND(SIZE,BOUNDARY) \
212 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
214 /* CIE identifier. */
215 #if HOST_BITS_PER_WIDE_INT >= 64
216 #define DWARF_CIE_ID \
217 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
218 #else
219 #define DWARF_CIE_ID DW_CIE_ID
220 #endif
223 /* A vector for a table that contains frame description
224 information for each routine. */
225 #define NOT_INDEXED (-1U)
226 #define NO_INDEX_ASSIGNED (-2U)
228 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
230 struct GTY((for_user)) indirect_string_node {
231 const char *str;
232 unsigned int refcount;
233 enum dwarf_form form;
234 char *label;
235 unsigned int index;
238 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
240 typedef const char *compare_type;
242 static hashval_t hash (indirect_string_node *);
243 static bool equal (indirect_string_node *, const char *);
246 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
248 /* With split_debug_info, both the comp_dir and dwo_name go in the
249 main object file, rather than the dwo, similar to the force_direct
250 parameter elsewhere but with additional complications:
252 1) The string is needed in both the main object file and the dwo.
253 That is, the comp_dir and dwo_name will appear in both places.
255 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
256 DW_FORM_GNU_str_index.
258 3) GCC chooses the form to use late, depending on the size and
259 reference count.
261 Rather than forcing the all debug string handling functions and
262 callers to deal with these complications, simply use a separate,
263 special-cased string table for any attribute that should go in the
264 main object file. This limits the complexity to just the places
265 that need it. */
267 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
269 static GTY(()) int dw2_string_counter;
271 /* True if the compilation unit places functions in more than one section. */
272 static GTY(()) bool have_multiple_function_sections = false;
274 /* Whether the default text and cold text sections have been used at all. */
276 static GTY(()) bool text_section_used = false;
277 static GTY(()) bool cold_text_section_used = false;
279 /* The default cold text section. */
280 static GTY(()) section *cold_text_section;
282 /* The DIE for C++14 'auto' in a function return type. */
283 static GTY(()) dw_die_ref auto_die;
285 /* The DIE for C++14 'decltype(auto)' in a function return type. */
286 static GTY(()) dw_die_ref decltype_auto_die;
288 /* Forward declarations for functions defined in this file. */
290 static char *stripattributes (const char *);
291 static void output_call_frame_info (int);
292 static void dwarf2out_note_section_used (void);
294 /* Personality decl of current unit. Used only when assembler does not support
295 personality CFI. */
296 static GTY(()) rtx current_unit_personality;
298 /* Data and reference forms for relocatable data. */
299 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
300 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
302 #ifndef DEBUG_FRAME_SECTION
303 #define DEBUG_FRAME_SECTION ".debug_frame"
304 #endif
306 #ifndef FUNC_BEGIN_LABEL
307 #define FUNC_BEGIN_LABEL "LFB"
308 #endif
310 #ifndef FUNC_END_LABEL
311 #define FUNC_END_LABEL "LFE"
312 #endif
314 #ifndef PROLOGUE_END_LABEL
315 #define PROLOGUE_END_LABEL "LPE"
316 #endif
318 #ifndef EPILOGUE_BEGIN_LABEL
319 #define EPILOGUE_BEGIN_LABEL "LEB"
320 #endif
322 #ifndef FRAME_BEGIN_LABEL
323 #define FRAME_BEGIN_LABEL "Lframe"
324 #endif
325 #define CIE_AFTER_SIZE_LABEL "LSCIE"
326 #define CIE_END_LABEL "LECIE"
327 #define FDE_LABEL "LSFDE"
328 #define FDE_AFTER_SIZE_LABEL "LASFDE"
329 #define FDE_END_LABEL "LEFDE"
330 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
331 #define LINE_NUMBER_END_LABEL "LELT"
332 #define LN_PROLOG_AS_LABEL "LASLTP"
333 #define LN_PROLOG_END_LABEL "LELTP"
334 #define DIE_LABEL_PREFIX "DW"
336 /* Match the base name of a file to the base name of a compilation unit. */
338 static int
339 matches_main_base (const char *path)
341 /* Cache the last query. */
342 static const char *last_path = NULL;
343 static int last_match = 0;
344 if (path != last_path)
346 const char *base;
347 int length = base_of_path (path, &base);
348 last_path = path;
349 last_match = (length == main_input_baselength
350 && memcmp (base, main_input_basename, length) == 0);
352 return last_match;
355 #ifdef DEBUG_DEBUG_STRUCT
357 static int
358 dump_struct_debug (tree type, enum debug_info_usage usage,
359 enum debug_struct_file criterion, int generic,
360 int matches, int result)
362 /* Find the type name. */
363 tree type_decl = TYPE_STUB_DECL (type);
364 tree t = type_decl;
365 const char *name = 0;
366 if (TREE_CODE (t) == TYPE_DECL)
367 t = DECL_NAME (t);
368 if (t)
369 name = IDENTIFIER_POINTER (t);
371 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
372 criterion,
373 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
374 matches ? "bas" : "hdr",
375 generic ? "gen" : "ord",
376 usage == DINFO_USAGE_DFN ? ";" :
377 usage == DINFO_USAGE_DIR_USE ? "." : "*",
378 result,
379 (void*) type_decl, name);
380 return result;
382 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
383 dump_struct_debug (type, usage, criterion, generic, matches, result)
385 #else
387 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
388 (result)
390 #endif
392 /* Get the number of HOST_WIDE_INTs needed to represent the precision
393 of the number. */
395 static unsigned int
396 get_full_len (const wide_int &op)
398 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
399 / HOST_BITS_PER_WIDE_INT);
402 static bool
403 should_emit_struct_debug (tree type, enum debug_info_usage usage)
405 enum debug_struct_file criterion;
406 tree type_decl;
407 bool generic = lang_hooks.types.generic_p (type);
409 if (generic)
410 criterion = debug_struct_generic[usage];
411 else
412 criterion = debug_struct_ordinary[usage];
414 if (criterion == DINFO_STRUCT_FILE_NONE)
415 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
416 if (criterion == DINFO_STRUCT_FILE_ANY)
417 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
419 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
421 if (type_decl != NULL)
423 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
424 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
426 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
427 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
430 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
433 /* Return a pointer to a copy of the section string name S with all
434 attributes stripped off, and an asterisk prepended (for assemble_name). */
436 static inline char *
437 stripattributes (const char *s)
439 char *stripped = XNEWVEC (char, strlen (s) + 2);
440 char *p = stripped;
442 *p++ = '*';
444 while (*s && *s != ',')
445 *p++ = *s++;
447 *p = '\0';
448 return stripped;
451 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
452 switch to the data section instead, and write out a synthetic start label
453 for collect2 the first time around. */
455 static void
456 switch_to_eh_frame_section (bool back)
458 tree label;
460 #ifdef EH_FRAME_SECTION_NAME
461 if (eh_frame_section == 0)
463 int flags;
465 if (EH_TABLES_CAN_BE_READ_ONLY)
467 int fde_encoding;
468 int per_encoding;
469 int lsda_encoding;
471 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
472 /*global=*/0);
473 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
474 /*global=*/1);
475 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
476 /*global=*/0);
477 flags = ((! flag_pic
478 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
479 && (fde_encoding & 0x70) != DW_EH_PE_aligned
480 && (per_encoding & 0x70) != DW_EH_PE_absptr
481 && (per_encoding & 0x70) != DW_EH_PE_aligned
482 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
483 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
484 ? 0 : SECTION_WRITE);
486 else
487 flags = SECTION_WRITE;
488 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
490 #endif /* EH_FRAME_SECTION_NAME */
492 if (eh_frame_section)
493 switch_to_section (eh_frame_section);
494 else
496 /* We have no special eh_frame section. Put the information in
497 the data section and emit special labels to guide collect2. */
498 switch_to_section (data_section);
500 if (!back)
502 label = get_file_function_name ("F");
503 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
504 targetm.asm_out.globalize_label (asm_out_file,
505 IDENTIFIER_POINTER (label));
506 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
511 /* Switch [BACK] to the eh or debug frame table section, depending on
512 FOR_EH. */
514 static void
515 switch_to_frame_table_section (int for_eh, bool back)
517 if (for_eh)
518 switch_to_eh_frame_section (back);
519 else
521 if (!debug_frame_section)
522 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
523 SECTION_DEBUG, NULL);
524 switch_to_section (debug_frame_section);
528 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
530 enum dw_cfi_oprnd_type
531 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
533 switch (cfi)
535 case DW_CFA_nop:
536 case DW_CFA_GNU_window_save:
537 case DW_CFA_remember_state:
538 case DW_CFA_restore_state:
539 return dw_cfi_oprnd_unused;
541 case DW_CFA_set_loc:
542 case DW_CFA_advance_loc1:
543 case DW_CFA_advance_loc2:
544 case DW_CFA_advance_loc4:
545 case DW_CFA_MIPS_advance_loc8:
546 return dw_cfi_oprnd_addr;
548 case DW_CFA_offset:
549 case DW_CFA_offset_extended:
550 case DW_CFA_def_cfa:
551 case DW_CFA_offset_extended_sf:
552 case DW_CFA_def_cfa_sf:
553 case DW_CFA_restore:
554 case DW_CFA_restore_extended:
555 case DW_CFA_undefined:
556 case DW_CFA_same_value:
557 case DW_CFA_def_cfa_register:
558 case DW_CFA_register:
559 case DW_CFA_expression:
560 return dw_cfi_oprnd_reg_num;
562 case DW_CFA_def_cfa_offset:
563 case DW_CFA_GNU_args_size:
564 case DW_CFA_def_cfa_offset_sf:
565 return dw_cfi_oprnd_offset;
567 case DW_CFA_def_cfa_expression:
568 return dw_cfi_oprnd_loc;
570 default:
571 gcc_unreachable ();
575 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
577 enum dw_cfi_oprnd_type
578 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
580 switch (cfi)
582 case DW_CFA_def_cfa:
583 case DW_CFA_def_cfa_sf:
584 case DW_CFA_offset:
585 case DW_CFA_offset_extended_sf:
586 case DW_CFA_offset_extended:
587 return dw_cfi_oprnd_offset;
589 case DW_CFA_register:
590 return dw_cfi_oprnd_reg_num;
592 case DW_CFA_expression:
593 return dw_cfi_oprnd_loc;
595 default:
596 return dw_cfi_oprnd_unused;
600 /* Output one FDE. */
602 static void
603 output_fde (dw_fde_ref fde, bool for_eh, bool second,
604 char *section_start_label, int fde_encoding, char *augmentation,
605 bool any_lsda_needed, int lsda_encoding)
607 const char *begin, *end;
608 static unsigned int j;
609 char l1[20], l2[20];
611 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
612 /* empty */ 0);
613 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
614 for_eh + j);
615 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
616 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
617 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
618 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
619 " indicating 64-bit DWARF extension");
620 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
621 "FDE Length");
622 ASM_OUTPUT_LABEL (asm_out_file, l1);
624 if (for_eh)
625 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
626 else
627 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
628 debug_frame_section, "FDE CIE offset");
630 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
631 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
633 if (for_eh)
635 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
636 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
637 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
638 "FDE initial location");
639 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
640 end, begin, "FDE address range");
642 else
644 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
645 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
648 if (augmentation[0])
650 if (any_lsda_needed)
652 int size = size_of_encoded_value (lsda_encoding);
654 if (lsda_encoding == DW_EH_PE_aligned)
656 int offset = ( 4 /* Length */
657 + 4 /* CIE offset */
658 + 2 * size_of_encoded_value (fde_encoding)
659 + 1 /* Augmentation size */ );
660 int pad = -offset & (PTR_SIZE - 1);
662 size += pad;
663 gcc_assert (size_of_uleb128 (size) == 1);
666 dw2_asm_output_data_uleb128 (size, "Augmentation size");
668 if (fde->uses_eh_lsda)
670 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
671 fde->funcdef_number);
672 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
673 gen_rtx_SYMBOL_REF (Pmode, l1),
674 false,
675 "Language Specific Data Area");
677 else
679 if (lsda_encoding == DW_EH_PE_aligned)
680 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
681 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
682 "Language Specific Data Area (none)");
685 else
686 dw2_asm_output_data_uleb128 (0, "Augmentation size");
689 /* Loop through the Call Frame Instructions associated with this FDE. */
690 fde->dw_fde_current_label = begin;
692 size_t from, until, i;
694 from = 0;
695 until = vec_safe_length (fde->dw_fde_cfi);
697 if (fde->dw_fde_second_begin == NULL)
699 else if (!second)
700 until = fde->dw_fde_switch_cfi_index;
701 else
702 from = fde->dw_fde_switch_cfi_index;
704 for (i = from; i < until; i++)
705 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
708 /* If we are to emit a ref/link from function bodies to their frame tables,
709 do it now. This is typically performed to make sure that tables
710 associated with functions are dragged with them and not discarded in
711 garbage collecting links. We need to do this on a per function basis to
712 cope with -ffunction-sections. */
714 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
715 /* Switch to the function section, emit the ref to the tables, and
716 switch *back* into the table section. */
717 switch_to_section (function_section (fde->decl));
718 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
719 switch_to_frame_table_section (for_eh, true);
720 #endif
722 /* Pad the FDE out to an address sized boundary. */
723 ASM_OUTPUT_ALIGN (asm_out_file,
724 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
725 ASM_OUTPUT_LABEL (asm_out_file, l2);
727 j += 2;
730 /* Return true if frame description entry FDE is needed for EH. */
732 static bool
733 fde_needed_for_eh_p (dw_fde_ref fde)
735 if (flag_asynchronous_unwind_tables)
736 return true;
738 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
739 return true;
741 if (fde->uses_eh_lsda)
742 return true;
744 /* If exceptions are enabled, we have collected nothrow info. */
745 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
746 return false;
748 return true;
751 /* Output the call frame information used to record information
752 that relates to calculating the frame pointer, and records the
753 location of saved registers. */
755 static void
756 output_call_frame_info (int for_eh)
758 unsigned int i;
759 dw_fde_ref fde;
760 dw_cfi_ref cfi;
761 char l1[20], l2[20], section_start_label[20];
762 bool any_lsda_needed = false;
763 char augmentation[6];
764 int augmentation_size;
765 int fde_encoding = DW_EH_PE_absptr;
766 int per_encoding = DW_EH_PE_absptr;
767 int lsda_encoding = DW_EH_PE_absptr;
768 int return_reg;
769 rtx personality = NULL;
770 int dw_cie_version;
772 /* Don't emit a CIE if there won't be any FDEs. */
773 if (!fde_vec)
774 return;
776 /* Nothing to do if the assembler's doing it all. */
777 if (dwarf2out_do_cfi_asm ())
778 return;
780 /* If we don't have any functions we'll want to unwind out of, don't emit
781 any EH unwind information. If we make FDEs linkonce, we may have to
782 emit an empty label for an FDE that wouldn't otherwise be emitted. We
783 want to avoid having an FDE kept around when the function it refers to
784 is discarded. Example where this matters: a primary function template
785 in C++ requires EH information, an explicit specialization doesn't. */
786 if (for_eh)
788 bool any_eh_needed = false;
790 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
792 if (fde->uses_eh_lsda)
793 any_eh_needed = any_lsda_needed = true;
794 else if (fde_needed_for_eh_p (fde))
795 any_eh_needed = true;
796 else if (TARGET_USES_WEAK_UNWIND_INFO)
797 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
800 if (!any_eh_needed)
801 return;
804 /* We're going to be generating comments, so turn on app. */
805 if (flag_debug_asm)
806 app_enable ();
808 /* Switch to the proper frame section, first time. */
809 switch_to_frame_table_section (for_eh, false);
811 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
812 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
814 /* Output the CIE. */
815 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
816 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
817 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
818 dw2_asm_output_data (4, 0xffffffff,
819 "Initial length escape value indicating 64-bit DWARF extension");
820 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
821 "Length of Common Information Entry");
822 ASM_OUTPUT_LABEL (asm_out_file, l1);
824 /* Now that the CIE pointer is PC-relative for EH,
825 use 0 to identify the CIE. */
826 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
827 (for_eh ? 0 : DWARF_CIE_ID),
828 "CIE Identifier Tag");
830 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
831 use CIE version 1, unless that would produce incorrect results
832 due to overflowing the return register column. */
833 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
834 dw_cie_version = 1;
835 if (return_reg >= 256 || dwarf_version > 2)
836 dw_cie_version = 3;
837 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
839 augmentation[0] = 0;
840 augmentation_size = 0;
842 personality = current_unit_personality;
843 if (for_eh)
845 char *p;
847 /* Augmentation:
848 z Indicates that a uleb128 is present to size the
849 augmentation section.
850 L Indicates the encoding (and thus presence) of
851 an LSDA pointer in the FDE augmentation.
852 R Indicates a non-default pointer encoding for
853 FDE code pointers.
854 P Indicates the presence of an encoding + language
855 personality routine in the CIE augmentation. */
857 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
858 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
859 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
861 p = augmentation + 1;
862 if (personality)
864 *p++ = 'P';
865 augmentation_size += 1 + size_of_encoded_value (per_encoding);
866 assemble_external_libcall (personality);
868 if (any_lsda_needed)
870 *p++ = 'L';
871 augmentation_size += 1;
873 if (fde_encoding != DW_EH_PE_absptr)
875 *p++ = 'R';
876 augmentation_size += 1;
878 if (p > augmentation + 1)
880 augmentation[0] = 'z';
881 *p = '\0';
884 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
885 if (personality && per_encoding == DW_EH_PE_aligned)
887 int offset = ( 4 /* Length */
888 + 4 /* CIE Id */
889 + 1 /* CIE version */
890 + strlen (augmentation) + 1 /* Augmentation */
891 + size_of_uleb128 (1) /* Code alignment */
892 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
893 + 1 /* RA column */
894 + 1 /* Augmentation size */
895 + 1 /* Personality encoding */ );
896 int pad = -offset & (PTR_SIZE - 1);
898 augmentation_size += pad;
900 /* Augmentations should be small, so there's scarce need to
901 iterate for a solution. Die if we exceed one uleb128 byte. */
902 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
906 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
907 if (dw_cie_version >= 4)
909 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
910 dw2_asm_output_data (1, 0, "CIE Segment Size");
912 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
913 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
914 "CIE Data Alignment Factor");
916 if (dw_cie_version == 1)
917 dw2_asm_output_data (1, return_reg, "CIE RA Column");
918 else
919 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
921 if (augmentation[0])
923 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
924 if (personality)
926 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
927 eh_data_format_name (per_encoding));
928 dw2_asm_output_encoded_addr_rtx (per_encoding,
929 personality,
930 true, NULL);
933 if (any_lsda_needed)
934 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
935 eh_data_format_name (lsda_encoding));
937 if (fde_encoding != DW_EH_PE_absptr)
938 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
939 eh_data_format_name (fde_encoding));
942 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
943 output_cfi (cfi, NULL, for_eh);
945 /* Pad the CIE out to an address sized boundary. */
946 ASM_OUTPUT_ALIGN (asm_out_file,
947 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
948 ASM_OUTPUT_LABEL (asm_out_file, l2);
950 /* Loop through all of the FDE's. */
951 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
953 unsigned int k;
955 /* Don't emit EH unwind info for leaf functions that don't need it. */
956 if (for_eh && !fde_needed_for_eh_p (fde))
957 continue;
959 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
960 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
961 augmentation, any_lsda_needed, lsda_encoding);
964 if (for_eh && targetm.terminate_dw2_eh_frame_info)
965 dw2_asm_output_data (4, 0, "End of Table");
967 /* Turn off app to make assembly quicker. */
968 if (flag_debug_asm)
969 app_disable ();
972 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
974 static void
975 dwarf2out_do_cfi_startproc (bool second)
977 int enc;
978 rtx ref;
979 rtx personality = get_personality_function (current_function_decl);
981 fprintf (asm_out_file, "\t.cfi_startproc\n");
983 if (personality)
985 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
986 ref = personality;
988 /* ??? The GAS support isn't entirely consistent. We have to
989 handle indirect support ourselves, but PC-relative is done
990 in the assembler. Further, the assembler can't handle any
991 of the weirder relocation types. */
992 if (enc & DW_EH_PE_indirect)
993 ref = dw2_force_const_mem (ref, true);
995 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
996 output_addr_const (asm_out_file, ref);
997 fputc ('\n', asm_out_file);
1000 if (crtl->uses_eh_lsda)
1002 char lab[20];
1004 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1005 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1006 current_function_funcdef_no);
1007 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1008 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1010 if (enc & DW_EH_PE_indirect)
1011 ref = dw2_force_const_mem (ref, true);
1013 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1014 output_addr_const (asm_out_file, ref);
1015 fputc ('\n', asm_out_file);
1019 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1020 this allocation may be done before pass_final. */
1022 dw_fde_ref
1023 dwarf2out_alloc_current_fde (void)
1025 dw_fde_ref fde;
1027 fde = ggc_cleared_alloc<dw_fde_node> ();
1028 fde->decl = current_function_decl;
1029 fde->funcdef_number = current_function_funcdef_no;
1030 fde->fde_index = vec_safe_length (fde_vec);
1031 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1032 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1033 fde->nothrow = crtl->nothrow;
1034 fde->drap_reg = INVALID_REGNUM;
1035 fde->vdrap_reg = INVALID_REGNUM;
1037 /* Record the FDE associated with this function. */
1038 cfun->fde = fde;
1039 vec_safe_push (fde_vec, fde);
1041 return fde;
1044 /* Output a marker (i.e. a label) for the beginning of a function, before
1045 the prologue. */
1047 void
1048 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1049 const char *file ATTRIBUTE_UNUSED)
1051 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1052 char * dup_label;
1053 dw_fde_ref fde;
1054 section *fnsec;
1055 bool do_frame;
1057 current_function_func_begin_label = NULL;
1059 do_frame = dwarf2out_do_frame ();
1061 /* ??? current_function_func_begin_label is also used by except.c for
1062 call-site information. We must emit this label if it might be used. */
1063 if (!do_frame
1064 && (!flag_exceptions
1065 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1066 return;
1068 fnsec = function_section (current_function_decl);
1069 switch_to_section (fnsec);
1070 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1071 current_function_funcdef_no);
1072 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1073 current_function_funcdef_no);
1074 dup_label = xstrdup (label);
1075 current_function_func_begin_label = dup_label;
1077 /* We can elide the fde allocation if we're not emitting debug info. */
1078 if (!do_frame)
1079 return;
1081 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1082 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1083 would include pass_dwarf2_frame. If we've not created the FDE yet,
1084 do so now. */
1085 fde = cfun->fde;
1086 if (fde == NULL)
1087 fde = dwarf2out_alloc_current_fde ();
1089 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1090 fde->dw_fde_begin = dup_label;
1091 fde->dw_fde_current_label = dup_label;
1092 fde->in_std_section = (fnsec == text_section
1093 || (cold_text_section && fnsec == cold_text_section));
1095 /* We only want to output line number information for the genuine dwarf2
1096 prologue case, not the eh frame case. */
1097 #ifdef DWARF2_DEBUGGING_INFO
1098 if (file)
1099 dwarf2out_source_line (line, file, 0, true);
1100 #endif
1102 if (dwarf2out_do_cfi_asm ())
1103 dwarf2out_do_cfi_startproc (false);
1104 else
1106 rtx personality = get_personality_function (current_function_decl);
1107 if (!current_unit_personality)
1108 current_unit_personality = personality;
1110 /* We cannot keep a current personality per function as without CFI
1111 asm, at the point where we emit the CFI data, there is no current
1112 function anymore. */
1113 if (personality && current_unit_personality != personality)
1114 sorry ("multiple EH personalities are supported only with assemblers "
1115 "supporting .cfi_personality directive");
1119 /* Output a marker (i.e. a label) for the end of the generated code
1120 for a function prologue. This gets called *after* the prologue code has
1121 been generated. */
1123 void
1124 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1125 const char *file ATTRIBUTE_UNUSED)
1127 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1129 /* Output a label to mark the endpoint of the code generated for this
1130 function. */
1131 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1132 current_function_funcdef_no);
1133 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1134 current_function_funcdef_no);
1135 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1138 /* Output a marker (i.e. a label) for the beginning of the generated code
1139 for a function epilogue. This gets called *before* the prologue code has
1140 been generated. */
1142 void
1143 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1144 const char *file ATTRIBUTE_UNUSED)
1146 dw_fde_ref fde = cfun->fde;
1147 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1149 if (fde->dw_fde_vms_begin_epilogue)
1150 return;
1152 /* Output a label to mark the endpoint of the code generated for this
1153 function. */
1154 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1155 current_function_funcdef_no);
1156 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1157 current_function_funcdef_no);
1158 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1161 /* Output a marker (i.e. a label) for the absolute end of the generated code
1162 for a function definition. This gets called *after* the epilogue code has
1163 been generated. */
1165 void
1166 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1167 const char *file ATTRIBUTE_UNUSED)
1169 dw_fde_ref fde;
1170 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1172 last_var_location_insn = NULL;
1173 cached_next_real_insn = NULL;
1175 if (dwarf2out_do_cfi_asm ())
1176 fprintf (asm_out_file, "\t.cfi_endproc\n");
1178 /* Output a label to mark the endpoint of the code generated for this
1179 function. */
1180 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1181 current_function_funcdef_no);
1182 ASM_OUTPUT_LABEL (asm_out_file, label);
1183 fde = cfun->fde;
1184 gcc_assert (fde != NULL);
1185 if (fde->dw_fde_second_begin == NULL)
1186 fde->dw_fde_end = xstrdup (label);
1189 void
1190 dwarf2out_frame_finish (void)
1192 /* Output call frame information. */
1193 if (targetm.debug_unwind_info () == UI_DWARF2)
1194 output_call_frame_info (0);
1196 /* Output another copy for the unwinder. */
1197 if ((flag_unwind_tables || flag_exceptions)
1198 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1199 output_call_frame_info (1);
1202 /* Note that the current function section is being used for code. */
1204 static void
1205 dwarf2out_note_section_used (void)
1207 section *sec = current_function_section ();
1208 if (sec == text_section)
1209 text_section_used = true;
1210 else if (sec == cold_text_section)
1211 cold_text_section_used = true;
1214 static void var_location_switch_text_section (void);
1215 static void set_cur_line_info_table (section *);
1217 void
1218 dwarf2out_switch_text_section (void)
1220 section *sect;
1221 dw_fde_ref fde = cfun->fde;
1223 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1225 if (!in_cold_section_p)
1227 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1228 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1229 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1231 else
1233 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1234 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1235 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1237 have_multiple_function_sections = true;
1239 /* There is no need to mark used sections when not debugging. */
1240 if (cold_text_section != NULL)
1241 dwarf2out_note_section_used ();
1243 if (dwarf2out_do_cfi_asm ())
1244 fprintf (asm_out_file, "\t.cfi_endproc\n");
1246 /* Now do the real section switch. */
1247 sect = current_function_section ();
1248 switch_to_section (sect);
1250 fde->second_in_std_section
1251 = (sect == text_section
1252 || (cold_text_section && sect == cold_text_section));
1254 if (dwarf2out_do_cfi_asm ())
1255 dwarf2out_do_cfi_startproc (true);
1257 var_location_switch_text_section ();
1259 if (cold_text_section != NULL)
1260 set_cur_line_info_table (sect);
1263 /* And now, the subset of the debugging information support code necessary
1264 for emitting location expressions. */
1266 /* Data about a single source file. */
1267 struct GTY((for_user)) dwarf_file_data {
1268 const char * filename;
1269 int emitted_number;
1272 typedef struct GTY(()) deferred_locations_struct
1274 tree variable;
1275 dw_die_ref die;
1276 } deferred_locations;
1279 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1282 /* Describe an entry into the .debug_addr section. */
1284 enum ate_kind {
1285 ate_kind_rtx,
1286 ate_kind_rtx_dtprel,
1287 ate_kind_label
1290 typedef struct GTY((for_user)) addr_table_entry_struct {
1291 enum ate_kind kind;
1292 unsigned int refcount;
1293 unsigned int index;
1294 union addr_table_entry_struct_union
1296 rtx GTY ((tag ("0"))) rtl;
1297 char * GTY ((tag ("1"))) label;
1299 GTY ((desc ("%1.kind"))) addr;
1301 addr_table_entry;
1303 /* Location lists are ranges + location descriptions for that range,
1304 so you can track variables that are in different places over
1305 their entire life. */
1306 typedef struct GTY(()) dw_loc_list_struct {
1307 dw_loc_list_ref dw_loc_next;
1308 const char *begin; /* Label and addr_entry for start of range */
1309 addr_table_entry *begin_entry;
1310 const char *end; /* Label for end of range */
1311 char *ll_symbol; /* Label for beginning of location list.
1312 Only on head of list */
1313 const char *section; /* Section this loclist is relative to */
1314 dw_loc_descr_ref expr;
1315 hashval_t hash;
1316 /* True if all addresses in this and subsequent lists are known to be
1317 resolved. */
1318 bool resolved_addr;
1319 /* True if this list has been replaced by dw_loc_next. */
1320 bool replaced;
1321 bool emitted;
1322 /* True if the range should be emitted even if begin and end
1323 are the same. */
1324 bool force;
1325 } dw_loc_list_node;
1327 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1329 /* Convert a DWARF stack opcode into its string name. */
1331 static const char *
1332 dwarf_stack_op_name (unsigned int op)
1334 const char *name = get_DW_OP_name (op);
1336 if (name != NULL)
1337 return name;
1339 return "OP_<unknown>";
1342 /* Return a pointer to a newly allocated location description. Location
1343 descriptions are simple expression terms that can be strung
1344 together to form more complicated location (address) descriptions. */
1346 static inline dw_loc_descr_ref
1347 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1348 unsigned HOST_WIDE_INT oprnd2)
1350 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1352 descr->dw_loc_opc = op;
1353 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1354 descr->dw_loc_oprnd1.val_entry = NULL;
1355 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1356 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1357 descr->dw_loc_oprnd2.val_entry = NULL;
1358 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1360 return descr;
1363 /* Return a pointer to a newly allocated location description for
1364 REG and OFFSET. */
1366 static inline dw_loc_descr_ref
1367 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1369 if (reg <= 31)
1370 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1371 offset, 0);
1372 else
1373 return new_loc_descr (DW_OP_bregx, reg, offset);
1376 /* Add a location description term to a location description expression. */
1378 static inline void
1379 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1381 dw_loc_descr_ref *d;
1383 /* Find the end of the chain. */
1384 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1387 *d = descr;
1390 /* Compare two location operands for exact equality. */
1392 static bool
1393 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1395 if (a->val_class != b->val_class)
1396 return false;
1397 switch (a->val_class)
1399 case dw_val_class_none:
1400 return true;
1401 case dw_val_class_addr:
1402 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1404 case dw_val_class_offset:
1405 case dw_val_class_unsigned_const:
1406 case dw_val_class_const:
1407 case dw_val_class_range_list:
1408 case dw_val_class_lineptr:
1409 case dw_val_class_macptr:
1410 /* These are all HOST_WIDE_INT, signed or unsigned. */
1411 return a->v.val_unsigned == b->v.val_unsigned;
1413 case dw_val_class_loc:
1414 return a->v.val_loc == b->v.val_loc;
1415 case dw_val_class_loc_list:
1416 return a->v.val_loc_list == b->v.val_loc_list;
1417 case dw_val_class_die_ref:
1418 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1419 case dw_val_class_fde_ref:
1420 return a->v.val_fde_index == b->v.val_fde_index;
1421 case dw_val_class_lbl_id:
1422 case dw_val_class_high_pc:
1423 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1424 case dw_val_class_str:
1425 return a->v.val_str == b->v.val_str;
1426 case dw_val_class_flag:
1427 return a->v.val_flag == b->v.val_flag;
1428 case dw_val_class_file:
1429 return a->v.val_file == b->v.val_file;
1430 case dw_val_class_decl_ref:
1431 return a->v.val_decl_ref == b->v.val_decl_ref;
1433 case dw_val_class_const_double:
1434 return (a->v.val_double.high == b->v.val_double.high
1435 && a->v.val_double.low == b->v.val_double.low);
1437 case dw_val_class_wide_int:
1438 return *a->v.val_wide == *b->v.val_wide;
1440 case dw_val_class_vec:
1442 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1443 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1445 return (a_len == b_len
1446 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1449 case dw_val_class_data8:
1450 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1452 case dw_val_class_vms_delta:
1453 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1454 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1456 gcc_unreachable ();
1459 /* Compare two location atoms for exact equality. */
1461 static bool
1462 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1464 if (a->dw_loc_opc != b->dw_loc_opc)
1465 return false;
1467 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1468 address size, but since we always allocate cleared storage it
1469 should be zero for other types of locations. */
1470 if (a->dtprel != b->dtprel)
1471 return false;
1473 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1474 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1477 /* Compare two complete location expressions for exact equality. */
1479 bool
1480 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1482 while (1)
1484 if (a == b)
1485 return true;
1486 if (a == NULL || b == NULL)
1487 return false;
1488 if (!loc_descr_equal_p_1 (a, b))
1489 return false;
1491 a = a->dw_loc_next;
1492 b = b->dw_loc_next;
1497 /* Add a constant OFFSET to a location expression. */
1499 static void
1500 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1502 dw_loc_descr_ref loc;
1503 HOST_WIDE_INT *p;
1505 gcc_assert (*list_head != NULL);
1507 if (!offset)
1508 return;
1510 /* Find the end of the chain. */
1511 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1514 p = NULL;
1515 if (loc->dw_loc_opc == DW_OP_fbreg
1516 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1517 p = &loc->dw_loc_oprnd1.v.val_int;
1518 else if (loc->dw_loc_opc == DW_OP_bregx)
1519 p = &loc->dw_loc_oprnd2.v.val_int;
1521 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1522 offset. Don't optimize if an signed integer overflow would happen. */
1523 if (p != NULL
1524 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1525 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1526 *p += offset;
1528 else if (offset > 0)
1529 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1531 else
1533 loc->dw_loc_next = int_loc_descriptor (-offset);
1534 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1538 /* Add a constant OFFSET to a location list. */
1540 static void
1541 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1543 dw_loc_list_ref d;
1544 for (d = list_head; d != NULL; d = d->dw_loc_next)
1545 loc_descr_plus_const (&d->expr, offset);
1548 #define DWARF_REF_SIZE \
1549 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1551 static unsigned long int get_base_type_offset (dw_die_ref);
1553 /* Return the size of a location descriptor. */
1555 static unsigned long
1556 size_of_loc_descr (dw_loc_descr_ref loc)
1558 unsigned long size = 1;
1560 switch (loc->dw_loc_opc)
1562 case DW_OP_addr:
1563 size += DWARF2_ADDR_SIZE;
1564 break;
1565 case DW_OP_GNU_addr_index:
1566 case DW_OP_GNU_const_index:
1567 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1568 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1569 break;
1570 case DW_OP_const1u:
1571 case DW_OP_const1s:
1572 size += 1;
1573 break;
1574 case DW_OP_const2u:
1575 case DW_OP_const2s:
1576 size += 2;
1577 break;
1578 case DW_OP_const4u:
1579 case DW_OP_const4s:
1580 size += 4;
1581 break;
1582 case DW_OP_const8u:
1583 case DW_OP_const8s:
1584 size += 8;
1585 break;
1586 case DW_OP_constu:
1587 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1588 break;
1589 case DW_OP_consts:
1590 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1591 break;
1592 case DW_OP_pick:
1593 size += 1;
1594 break;
1595 case DW_OP_plus_uconst:
1596 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1597 break;
1598 case DW_OP_skip:
1599 case DW_OP_bra:
1600 size += 2;
1601 break;
1602 case DW_OP_breg0:
1603 case DW_OP_breg1:
1604 case DW_OP_breg2:
1605 case DW_OP_breg3:
1606 case DW_OP_breg4:
1607 case DW_OP_breg5:
1608 case DW_OP_breg6:
1609 case DW_OP_breg7:
1610 case DW_OP_breg8:
1611 case DW_OP_breg9:
1612 case DW_OP_breg10:
1613 case DW_OP_breg11:
1614 case DW_OP_breg12:
1615 case DW_OP_breg13:
1616 case DW_OP_breg14:
1617 case DW_OP_breg15:
1618 case DW_OP_breg16:
1619 case DW_OP_breg17:
1620 case DW_OP_breg18:
1621 case DW_OP_breg19:
1622 case DW_OP_breg20:
1623 case DW_OP_breg21:
1624 case DW_OP_breg22:
1625 case DW_OP_breg23:
1626 case DW_OP_breg24:
1627 case DW_OP_breg25:
1628 case DW_OP_breg26:
1629 case DW_OP_breg27:
1630 case DW_OP_breg28:
1631 case DW_OP_breg29:
1632 case DW_OP_breg30:
1633 case DW_OP_breg31:
1634 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1635 break;
1636 case DW_OP_regx:
1637 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1638 break;
1639 case DW_OP_fbreg:
1640 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1641 break;
1642 case DW_OP_bregx:
1643 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1644 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1645 break;
1646 case DW_OP_piece:
1647 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1648 break;
1649 case DW_OP_bit_piece:
1650 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1651 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1652 break;
1653 case DW_OP_deref_size:
1654 case DW_OP_xderef_size:
1655 size += 1;
1656 break;
1657 case DW_OP_call2:
1658 size += 2;
1659 break;
1660 case DW_OP_call4:
1661 size += 4;
1662 break;
1663 case DW_OP_call_ref:
1664 size += DWARF_REF_SIZE;
1665 break;
1666 case DW_OP_implicit_value:
1667 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1668 + loc->dw_loc_oprnd1.v.val_unsigned;
1669 break;
1670 case DW_OP_GNU_implicit_pointer:
1671 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1672 break;
1673 case DW_OP_GNU_entry_value:
1675 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1676 size += size_of_uleb128 (op_size) + op_size;
1677 break;
1679 case DW_OP_GNU_const_type:
1681 unsigned long o
1682 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1683 size += size_of_uleb128 (o) + 1;
1684 switch (loc->dw_loc_oprnd2.val_class)
1686 case dw_val_class_vec:
1687 size += loc->dw_loc_oprnd2.v.val_vec.length
1688 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1689 break;
1690 case dw_val_class_const:
1691 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1692 break;
1693 case dw_val_class_const_double:
1694 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1695 break;
1696 case dw_val_class_wide_int:
1697 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1698 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1699 break;
1700 default:
1701 gcc_unreachable ();
1703 break;
1705 case DW_OP_GNU_regval_type:
1707 unsigned long o
1708 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1709 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1710 + size_of_uleb128 (o);
1712 break;
1713 case DW_OP_GNU_deref_type:
1715 unsigned long o
1716 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1717 size += 1 + size_of_uleb128 (o);
1719 break;
1720 case DW_OP_GNU_convert:
1721 case DW_OP_GNU_reinterpret:
1722 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1723 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1724 else
1726 unsigned long o
1727 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1728 size += size_of_uleb128 (o);
1730 break;
1731 case DW_OP_GNU_parameter_ref:
1732 size += 4;
1733 break;
1734 default:
1735 break;
1738 return size;
1741 /* Return the size of a series of location descriptors. */
1743 unsigned long
1744 size_of_locs (dw_loc_descr_ref loc)
1746 dw_loc_descr_ref l;
1747 unsigned long size;
1749 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1750 field, to avoid writing to a PCH file. */
1751 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1753 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1754 break;
1755 size += size_of_loc_descr (l);
1757 if (! l)
1758 return size;
1760 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1762 l->dw_loc_addr = size;
1763 size += size_of_loc_descr (l);
1766 return size;
1769 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1770 static void get_ref_die_offset_label (char *, dw_die_ref);
1771 static unsigned long int get_ref_die_offset (dw_die_ref);
1773 /* Output location description stack opcode's operands (if any).
1774 The for_eh_or_skip parameter controls whether register numbers are
1775 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1776 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1777 info). This should be suppressed for the cases that have not been converted
1778 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1780 static void
1781 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1783 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1784 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1786 switch (loc->dw_loc_opc)
1788 #ifdef DWARF2_DEBUGGING_INFO
1789 case DW_OP_const2u:
1790 case DW_OP_const2s:
1791 dw2_asm_output_data (2, val1->v.val_int, NULL);
1792 break;
1793 case DW_OP_const4u:
1794 if (loc->dtprel)
1796 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1797 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1798 val1->v.val_addr);
1799 fputc ('\n', asm_out_file);
1800 break;
1802 /* FALLTHRU */
1803 case DW_OP_const4s:
1804 dw2_asm_output_data (4, val1->v.val_int, NULL);
1805 break;
1806 case DW_OP_const8u:
1807 if (loc->dtprel)
1809 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1810 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1811 val1->v.val_addr);
1812 fputc ('\n', asm_out_file);
1813 break;
1815 /* FALLTHRU */
1816 case DW_OP_const8s:
1817 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1818 dw2_asm_output_data (8, val1->v.val_int, NULL);
1819 break;
1820 case DW_OP_skip:
1821 case DW_OP_bra:
1823 int offset;
1825 gcc_assert (val1->val_class == dw_val_class_loc);
1826 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1828 dw2_asm_output_data (2, offset, NULL);
1830 break;
1831 case DW_OP_implicit_value:
1832 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1833 switch (val2->val_class)
1835 case dw_val_class_const:
1836 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1837 break;
1838 case dw_val_class_vec:
1840 unsigned int elt_size = val2->v.val_vec.elt_size;
1841 unsigned int len = val2->v.val_vec.length;
1842 unsigned int i;
1843 unsigned char *p;
1845 if (elt_size > sizeof (HOST_WIDE_INT))
1847 elt_size /= 2;
1848 len *= 2;
1850 for (i = 0, p = val2->v.val_vec.array;
1851 i < len;
1852 i++, p += elt_size)
1853 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1854 "fp or vector constant word %u", i);
1856 break;
1857 case dw_val_class_const_double:
1859 unsigned HOST_WIDE_INT first, second;
1861 if (WORDS_BIG_ENDIAN)
1863 first = val2->v.val_double.high;
1864 second = val2->v.val_double.low;
1866 else
1868 first = val2->v.val_double.low;
1869 second = val2->v.val_double.high;
1871 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1872 first, NULL);
1873 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1874 second, NULL);
1876 break;
1877 case dw_val_class_wide_int:
1879 int i;
1880 int len = get_full_len (*val2->v.val_wide);
1881 if (WORDS_BIG_ENDIAN)
1882 for (i = len - 1; i >= 0; --i)
1883 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1884 val2->v.val_wide->elt (i), NULL);
1885 else
1886 for (i = 0; i < len; ++i)
1887 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1888 val2->v.val_wide->elt (i), NULL);
1890 break;
1891 case dw_val_class_addr:
1892 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1893 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1894 break;
1895 default:
1896 gcc_unreachable ();
1898 break;
1899 #else
1900 case DW_OP_const2u:
1901 case DW_OP_const2s:
1902 case DW_OP_const4u:
1903 case DW_OP_const4s:
1904 case DW_OP_const8u:
1905 case DW_OP_const8s:
1906 case DW_OP_skip:
1907 case DW_OP_bra:
1908 case DW_OP_implicit_value:
1909 /* We currently don't make any attempt to make sure these are
1910 aligned properly like we do for the main unwind info, so
1911 don't support emitting things larger than a byte if we're
1912 only doing unwinding. */
1913 gcc_unreachable ();
1914 #endif
1915 case DW_OP_const1u:
1916 case DW_OP_const1s:
1917 dw2_asm_output_data (1, val1->v.val_int, NULL);
1918 break;
1919 case DW_OP_constu:
1920 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1921 break;
1922 case DW_OP_consts:
1923 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1924 break;
1925 case DW_OP_pick:
1926 dw2_asm_output_data (1, val1->v.val_int, NULL);
1927 break;
1928 case DW_OP_plus_uconst:
1929 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1930 break;
1931 case DW_OP_breg0:
1932 case DW_OP_breg1:
1933 case DW_OP_breg2:
1934 case DW_OP_breg3:
1935 case DW_OP_breg4:
1936 case DW_OP_breg5:
1937 case DW_OP_breg6:
1938 case DW_OP_breg7:
1939 case DW_OP_breg8:
1940 case DW_OP_breg9:
1941 case DW_OP_breg10:
1942 case DW_OP_breg11:
1943 case DW_OP_breg12:
1944 case DW_OP_breg13:
1945 case DW_OP_breg14:
1946 case DW_OP_breg15:
1947 case DW_OP_breg16:
1948 case DW_OP_breg17:
1949 case DW_OP_breg18:
1950 case DW_OP_breg19:
1951 case DW_OP_breg20:
1952 case DW_OP_breg21:
1953 case DW_OP_breg22:
1954 case DW_OP_breg23:
1955 case DW_OP_breg24:
1956 case DW_OP_breg25:
1957 case DW_OP_breg26:
1958 case DW_OP_breg27:
1959 case DW_OP_breg28:
1960 case DW_OP_breg29:
1961 case DW_OP_breg30:
1962 case DW_OP_breg31:
1963 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1964 break;
1965 case DW_OP_regx:
1967 unsigned r = val1->v.val_unsigned;
1968 if (for_eh_or_skip >= 0)
1969 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1970 gcc_assert (size_of_uleb128 (r)
1971 == size_of_uleb128 (val1->v.val_unsigned));
1972 dw2_asm_output_data_uleb128 (r, NULL);
1974 break;
1975 case DW_OP_fbreg:
1976 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1977 break;
1978 case DW_OP_bregx:
1980 unsigned r = val1->v.val_unsigned;
1981 if (for_eh_or_skip >= 0)
1982 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1983 gcc_assert (size_of_uleb128 (r)
1984 == size_of_uleb128 (val1->v.val_unsigned));
1985 dw2_asm_output_data_uleb128 (r, NULL);
1986 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1988 break;
1989 case DW_OP_piece:
1990 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1991 break;
1992 case DW_OP_bit_piece:
1993 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1994 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1995 break;
1996 case DW_OP_deref_size:
1997 case DW_OP_xderef_size:
1998 dw2_asm_output_data (1, val1->v.val_int, NULL);
1999 break;
2001 case DW_OP_addr:
2002 if (loc->dtprel)
2004 if (targetm.asm_out.output_dwarf_dtprel)
2006 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2007 DWARF2_ADDR_SIZE,
2008 val1->v.val_addr);
2009 fputc ('\n', asm_out_file);
2011 else
2012 gcc_unreachable ();
2014 else
2016 #ifdef DWARF2_DEBUGGING_INFO
2017 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2018 #else
2019 gcc_unreachable ();
2020 #endif
2022 break;
2024 case DW_OP_GNU_addr_index:
2025 case DW_OP_GNU_const_index:
2026 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2027 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2028 "(index into .debug_addr)");
2029 break;
2031 case DW_OP_GNU_implicit_pointer:
2033 char label[MAX_ARTIFICIAL_LABEL_BYTES
2034 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2035 gcc_assert (val1->val_class == dw_val_class_die_ref);
2036 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2037 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2038 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2040 break;
2042 case DW_OP_GNU_entry_value:
2043 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2044 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2045 break;
2047 case DW_OP_GNU_const_type:
2049 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2050 gcc_assert (o);
2051 dw2_asm_output_data_uleb128 (o, NULL);
2052 switch (val2->val_class)
2054 case dw_val_class_const:
2055 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2056 dw2_asm_output_data (1, l, NULL);
2057 dw2_asm_output_data (l, val2->v.val_int, NULL);
2058 break;
2059 case dw_val_class_vec:
2061 unsigned int elt_size = val2->v.val_vec.elt_size;
2062 unsigned int len = val2->v.val_vec.length;
2063 unsigned int i;
2064 unsigned char *p;
2066 l = len * elt_size;
2067 dw2_asm_output_data (1, l, NULL);
2068 if (elt_size > sizeof (HOST_WIDE_INT))
2070 elt_size /= 2;
2071 len *= 2;
2073 for (i = 0, p = val2->v.val_vec.array;
2074 i < len;
2075 i++, p += elt_size)
2076 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2077 "fp or vector constant word %u", i);
2079 break;
2080 case dw_val_class_const_double:
2082 unsigned HOST_WIDE_INT first, second;
2083 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2085 dw2_asm_output_data (1, 2 * l, NULL);
2086 if (WORDS_BIG_ENDIAN)
2088 first = val2->v.val_double.high;
2089 second = val2->v.val_double.low;
2091 else
2093 first = val2->v.val_double.low;
2094 second = val2->v.val_double.high;
2096 dw2_asm_output_data (l, first, NULL);
2097 dw2_asm_output_data (l, second, NULL);
2099 break;
2100 case dw_val_class_wide_int:
2102 int i;
2103 int len = get_full_len (*val2->v.val_wide);
2104 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2106 dw2_asm_output_data (1, len * l, NULL);
2107 if (WORDS_BIG_ENDIAN)
2108 for (i = len - 1; i >= 0; --i)
2109 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2110 else
2111 for (i = 0; i < len; ++i)
2112 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2114 break;
2115 default:
2116 gcc_unreachable ();
2119 break;
2120 case DW_OP_GNU_regval_type:
2122 unsigned r = val1->v.val_unsigned;
2123 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2124 gcc_assert (o);
2125 if (for_eh_or_skip >= 0)
2127 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2128 gcc_assert (size_of_uleb128 (r)
2129 == size_of_uleb128 (val1->v.val_unsigned));
2131 dw2_asm_output_data_uleb128 (r, NULL);
2132 dw2_asm_output_data_uleb128 (o, NULL);
2134 break;
2135 case DW_OP_GNU_deref_type:
2137 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2138 gcc_assert (o);
2139 dw2_asm_output_data (1, val1->v.val_int, NULL);
2140 dw2_asm_output_data_uleb128 (o, NULL);
2142 break;
2143 case DW_OP_GNU_convert:
2144 case DW_OP_GNU_reinterpret:
2145 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2146 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2147 else
2149 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2150 gcc_assert (o);
2151 dw2_asm_output_data_uleb128 (o, NULL);
2153 break;
2155 case DW_OP_GNU_parameter_ref:
2157 unsigned long o;
2158 gcc_assert (val1->val_class == dw_val_class_die_ref);
2159 o = get_ref_die_offset (val1->v.val_die_ref.die);
2160 dw2_asm_output_data (4, o, NULL);
2162 break;
2164 default:
2165 /* Other codes have no operands. */
2166 break;
2170 /* Output a sequence of location operations.
2171 The for_eh_or_skip parameter controls whether register numbers are
2172 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2173 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2174 info). This should be suppressed for the cases that have not been converted
2175 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2177 void
2178 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2180 for (; loc != NULL; loc = loc->dw_loc_next)
2182 enum dwarf_location_atom opc = loc->dw_loc_opc;
2183 /* Output the opcode. */
2184 if (for_eh_or_skip >= 0
2185 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2187 unsigned r = (opc - DW_OP_breg0);
2188 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2189 gcc_assert (r <= 31);
2190 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2192 else if (for_eh_or_skip >= 0
2193 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2195 unsigned r = (opc - DW_OP_reg0);
2196 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2197 gcc_assert (r <= 31);
2198 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2201 dw2_asm_output_data (1, opc,
2202 "%s", dwarf_stack_op_name (opc));
2204 /* Output the operand(s) (if any). */
2205 output_loc_operands (loc, for_eh_or_skip);
2209 /* Output location description stack opcode's operands (if any).
2210 The output is single bytes on a line, suitable for .cfi_escape. */
2212 static void
2213 output_loc_operands_raw (dw_loc_descr_ref loc)
2215 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2216 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2218 switch (loc->dw_loc_opc)
2220 case DW_OP_addr:
2221 case DW_OP_GNU_addr_index:
2222 case DW_OP_GNU_const_index:
2223 case DW_OP_implicit_value:
2224 /* We cannot output addresses in .cfi_escape, only bytes. */
2225 gcc_unreachable ();
2227 case DW_OP_const1u:
2228 case DW_OP_const1s:
2229 case DW_OP_pick:
2230 case DW_OP_deref_size:
2231 case DW_OP_xderef_size:
2232 fputc (',', asm_out_file);
2233 dw2_asm_output_data_raw (1, val1->v.val_int);
2234 break;
2236 case DW_OP_const2u:
2237 case DW_OP_const2s:
2238 fputc (',', asm_out_file);
2239 dw2_asm_output_data_raw (2, val1->v.val_int);
2240 break;
2242 case DW_OP_const4u:
2243 case DW_OP_const4s:
2244 fputc (',', asm_out_file);
2245 dw2_asm_output_data_raw (4, val1->v.val_int);
2246 break;
2248 case DW_OP_const8u:
2249 case DW_OP_const8s:
2250 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2251 fputc (',', asm_out_file);
2252 dw2_asm_output_data_raw (8, val1->v.val_int);
2253 break;
2255 case DW_OP_skip:
2256 case DW_OP_bra:
2258 int offset;
2260 gcc_assert (val1->val_class == dw_val_class_loc);
2261 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2263 fputc (',', asm_out_file);
2264 dw2_asm_output_data_raw (2, offset);
2266 break;
2268 case DW_OP_regx:
2270 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2271 gcc_assert (size_of_uleb128 (r)
2272 == size_of_uleb128 (val1->v.val_unsigned));
2273 fputc (',', asm_out_file);
2274 dw2_asm_output_data_uleb128_raw (r);
2276 break;
2278 case DW_OP_constu:
2279 case DW_OP_plus_uconst:
2280 case DW_OP_piece:
2281 fputc (',', asm_out_file);
2282 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2283 break;
2285 case DW_OP_bit_piece:
2286 fputc (',', asm_out_file);
2287 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2288 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2289 break;
2291 case DW_OP_consts:
2292 case DW_OP_breg0:
2293 case DW_OP_breg1:
2294 case DW_OP_breg2:
2295 case DW_OP_breg3:
2296 case DW_OP_breg4:
2297 case DW_OP_breg5:
2298 case DW_OP_breg6:
2299 case DW_OP_breg7:
2300 case DW_OP_breg8:
2301 case DW_OP_breg9:
2302 case DW_OP_breg10:
2303 case DW_OP_breg11:
2304 case DW_OP_breg12:
2305 case DW_OP_breg13:
2306 case DW_OP_breg14:
2307 case DW_OP_breg15:
2308 case DW_OP_breg16:
2309 case DW_OP_breg17:
2310 case DW_OP_breg18:
2311 case DW_OP_breg19:
2312 case DW_OP_breg20:
2313 case DW_OP_breg21:
2314 case DW_OP_breg22:
2315 case DW_OP_breg23:
2316 case DW_OP_breg24:
2317 case DW_OP_breg25:
2318 case DW_OP_breg26:
2319 case DW_OP_breg27:
2320 case DW_OP_breg28:
2321 case DW_OP_breg29:
2322 case DW_OP_breg30:
2323 case DW_OP_breg31:
2324 case DW_OP_fbreg:
2325 fputc (',', asm_out_file);
2326 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2327 break;
2329 case DW_OP_bregx:
2331 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2332 gcc_assert (size_of_uleb128 (r)
2333 == size_of_uleb128 (val1->v.val_unsigned));
2334 fputc (',', asm_out_file);
2335 dw2_asm_output_data_uleb128_raw (r);
2336 fputc (',', asm_out_file);
2337 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2339 break;
2341 case DW_OP_GNU_implicit_pointer:
2342 case DW_OP_GNU_entry_value:
2343 case DW_OP_GNU_const_type:
2344 case DW_OP_GNU_regval_type:
2345 case DW_OP_GNU_deref_type:
2346 case DW_OP_GNU_convert:
2347 case DW_OP_GNU_reinterpret:
2348 case DW_OP_GNU_parameter_ref:
2349 gcc_unreachable ();
2350 break;
2352 default:
2353 /* Other codes have no operands. */
2354 break;
2358 void
2359 output_loc_sequence_raw (dw_loc_descr_ref loc)
2361 while (1)
2363 enum dwarf_location_atom opc = loc->dw_loc_opc;
2364 /* Output the opcode. */
2365 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2367 unsigned r = (opc - DW_OP_breg0);
2368 r = DWARF2_FRAME_REG_OUT (r, 1);
2369 gcc_assert (r <= 31);
2370 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2372 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2374 unsigned r = (opc - DW_OP_reg0);
2375 r = DWARF2_FRAME_REG_OUT (r, 1);
2376 gcc_assert (r <= 31);
2377 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2379 /* Output the opcode. */
2380 fprintf (asm_out_file, "%#x", opc);
2381 output_loc_operands_raw (loc);
2383 if (!loc->dw_loc_next)
2384 break;
2385 loc = loc->dw_loc_next;
2387 fputc (',', asm_out_file);
2391 /* This function builds a dwarf location descriptor sequence from a
2392 dw_cfa_location, adding the given OFFSET to the result of the
2393 expression. */
2395 struct dw_loc_descr_node *
2396 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2398 struct dw_loc_descr_node *head, *tmp;
2400 offset += cfa->offset;
2402 if (cfa->indirect)
2404 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2405 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2406 head->dw_loc_oprnd1.val_entry = NULL;
2407 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2408 add_loc_descr (&head, tmp);
2409 if (offset != 0)
2411 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2412 add_loc_descr (&head, tmp);
2415 else
2416 head = new_reg_loc_descr (cfa->reg, offset);
2418 return head;
2421 /* This function builds a dwarf location descriptor sequence for
2422 the address at OFFSET from the CFA when stack is aligned to
2423 ALIGNMENT byte. */
2425 struct dw_loc_descr_node *
2426 build_cfa_aligned_loc (dw_cfa_location *cfa,
2427 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2429 struct dw_loc_descr_node *head;
2430 unsigned int dwarf_fp
2431 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2433 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2434 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2436 head = new_reg_loc_descr (dwarf_fp, 0);
2437 add_loc_descr (&head, int_loc_descriptor (alignment));
2438 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2439 loc_descr_plus_const (&head, offset);
2441 else
2442 head = new_reg_loc_descr (dwarf_fp, offset);
2443 return head;
2446 /* And now, the support for symbolic debugging information. */
2448 /* .debug_str support. */
2450 static void dwarf2out_init (const char *);
2451 static void dwarf2out_finish (const char *);
2452 static void dwarf2out_assembly_start (void);
2453 static void dwarf2out_define (unsigned int, const char *);
2454 static void dwarf2out_undef (unsigned int, const char *);
2455 static void dwarf2out_start_source_file (unsigned, const char *);
2456 static void dwarf2out_end_source_file (unsigned);
2457 static void dwarf2out_function_decl (tree);
2458 static void dwarf2out_begin_block (unsigned, unsigned);
2459 static void dwarf2out_end_block (unsigned, unsigned);
2460 static bool dwarf2out_ignore_block (const_tree);
2461 static void dwarf2out_global_decl (tree);
2462 static void dwarf2out_type_decl (tree, int);
2463 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2464 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2465 dw_die_ref);
2466 static void dwarf2out_abstract_function (tree);
2467 static void dwarf2out_var_location (rtx_insn *);
2468 static void dwarf2out_begin_function (tree);
2469 static void dwarf2out_end_function (unsigned int);
2470 static void dwarf2out_set_name (tree, tree);
2472 /* The debug hooks structure. */
2474 const struct gcc_debug_hooks dwarf2_debug_hooks =
2476 dwarf2out_init,
2477 dwarf2out_finish,
2478 dwarf2out_assembly_start,
2479 dwarf2out_define,
2480 dwarf2out_undef,
2481 dwarf2out_start_source_file,
2482 dwarf2out_end_source_file,
2483 dwarf2out_begin_block,
2484 dwarf2out_end_block,
2485 dwarf2out_ignore_block,
2486 dwarf2out_source_line,
2487 dwarf2out_begin_prologue,
2488 #if VMS_DEBUGGING_INFO
2489 dwarf2out_vms_end_prologue,
2490 dwarf2out_vms_begin_epilogue,
2491 #else
2492 debug_nothing_int_charstar,
2493 debug_nothing_int_charstar,
2494 #endif
2495 dwarf2out_end_epilogue,
2496 dwarf2out_begin_function,
2497 dwarf2out_end_function, /* end_function */
2498 dwarf2out_function_decl, /* function_decl */
2499 dwarf2out_global_decl,
2500 dwarf2out_type_decl, /* type_decl */
2501 dwarf2out_imported_module_or_decl,
2502 debug_nothing_tree, /* deferred_inline_function */
2503 /* The DWARF 2 backend tries to reduce debugging bloat by not
2504 emitting the abstract description of inline functions until
2505 something tries to reference them. */
2506 dwarf2out_abstract_function, /* outlining_inline_function */
2507 debug_nothing_rtx_code_label, /* label */
2508 debug_nothing_int, /* handle_pch */
2509 dwarf2out_var_location,
2510 dwarf2out_switch_text_section,
2511 dwarf2out_set_name,
2512 1, /* start_end_main_source_file */
2513 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2516 /* NOTE: In the comments in this file, many references are made to
2517 "Debugging Information Entries". This term is abbreviated as `DIE'
2518 throughout the remainder of this file. */
2520 /* An internal representation of the DWARF output is built, and then
2521 walked to generate the DWARF debugging info. The walk of the internal
2522 representation is done after the entire program has been compiled.
2523 The types below are used to describe the internal representation. */
2525 /* Whether to put type DIEs into their own section .debug_types instead
2526 of making them part of the .debug_info section. Only supported for
2527 Dwarf V4 or higher and the user didn't disable them through
2528 -fno-debug-types-section. It is more efficient to put them in a
2529 separate comdat sections since the linker will then be able to
2530 remove duplicates. But not all tools support .debug_types sections
2531 yet. */
2533 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2535 /* Various DIE's use offsets relative to the beginning of the
2536 .debug_info section to refer to each other. */
2538 typedef long int dw_offset;
2540 /* Define typedefs here to avoid circular dependencies. */
2542 typedef struct dw_attr_struct *dw_attr_ref;
2543 typedef struct dw_line_info_struct *dw_line_info_ref;
2544 typedef struct pubname_struct *pubname_ref;
2545 typedef struct dw_ranges_struct *dw_ranges_ref;
2546 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2547 typedef struct comdat_type_struct *comdat_type_node_ref;
2549 /* The entries in the line_info table more-or-less mirror the opcodes
2550 that are used in the real dwarf line table. Arrays of these entries
2551 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2552 supported. */
2554 enum dw_line_info_opcode {
2555 /* Emit DW_LNE_set_address; the operand is the label index. */
2556 LI_set_address,
2558 /* Emit a row to the matrix with the given line. This may be done
2559 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2560 special opcodes. */
2561 LI_set_line,
2563 /* Emit a DW_LNS_set_file. */
2564 LI_set_file,
2566 /* Emit a DW_LNS_set_column. */
2567 LI_set_column,
2569 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2570 LI_negate_stmt,
2572 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2573 LI_set_prologue_end,
2574 LI_set_epilogue_begin,
2576 /* Emit a DW_LNE_set_discriminator. */
2577 LI_set_discriminator
2580 typedef struct GTY(()) dw_line_info_struct {
2581 enum dw_line_info_opcode opcode;
2582 unsigned int val;
2583 } dw_line_info_entry;
2586 typedef struct GTY(()) dw_line_info_table_struct {
2587 /* The label that marks the end of this section. */
2588 const char *end_label;
2590 /* The values for the last row of the matrix, as collected in the table.
2591 These are used to minimize the changes to the next row. */
2592 unsigned int file_num;
2593 unsigned int line_num;
2594 unsigned int column_num;
2595 int discrim_num;
2596 bool is_stmt;
2597 bool in_use;
2599 vec<dw_line_info_entry, va_gc> *entries;
2600 } dw_line_info_table;
2602 typedef dw_line_info_table *dw_line_info_table_p;
2605 /* Each DIE attribute has a field specifying the attribute kind,
2606 a link to the next attribute in the chain, and an attribute value.
2607 Attributes are typically linked below the DIE they modify. */
2609 typedef struct GTY(()) dw_attr_struct {
2610 enum dwarf_attribute dw_attr;
2611 dw_val_node dw_attr_val;
2613 dw_attr_node;
2616 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2617 The children of each node form a circular list linked by
2618 die_sib. die_child points to the node *before* the "first" child node. */
2620 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2621 union die_symbol_or_type_node
2623 const char * GTY ((tag ("0"))) die_symbol;
2624 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2626 GTY ((desc ("%0.comdat_type_p"))) die_id;
2627 vec<dw_attr_node, va_gc> *die_attr;
2628 dw_die_ref die_parent;
2629 dw_die_ref die_child;
2630 dw_die_ref die_sib;
2631 dw_die_ref die_definition; /* ref from a specification to its definition */
2632 dw_offset die_offset;
2633 unsigned long die_abbrev;
2634 int die_mark;
2635 unsigned int decl_id;
2636 enum dwarf_tag die_tag;
2637 /* Die is used and must not be pruned as unused. */
2638 BOOL_BITFIELD die_perennial_p : 1;
2639 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2640 /* Lots of spare bits. */
2642 die_node;
2644 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2645 #define FOR_EACH_CHILD(die, c, expr) do { \
2646 c = die->die_child; \
2647 if (c) do { \
2648 c = c->die_sib; \
2649 expr; \
2650 } while (c != die->die_child); \
2651 } while (0)
2653 /* The pubname structure */
2655 typedef struct GTY(()) pubname_struct {
2656 dw_die_ref die;
2657 const char *name;
2659 pubname_entry;
2662 struct GTY(()) dw_ranges_struct {
2663 /* If this is positive, it's a block number, otherwise it's a
2664 bitwise-negated index into dw_ranges_by_label. */
2665 int num;
2668 /* A structure to hold a macinfo entry. */
2670 typedef struct GTY(()) macinfo_struct {
2671 unsigned char code;
2672 unsigned HOST_WIDE_INT lineno;
2673 const char *info;
2675 macinfo_entry;
2678 struct GTY(()) dw_ranges_by_label_struct {
2679 const char *begin;
2680 const char *end;
2683 /* The comdat type node structure. */
2684 typedef struct GTY(()) comdat_type_struct
2686 dw_die_ref root_die;
2687 dw_die_ref type_die;
2688 dw_die_ref skeleton_die;
2689 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2690 struct comdat_type_struct *next;
2692 comdat_type_node;
2694 /* The limbo die list structure. */
2695 typedef struct GTY(()) limbo_die_struct {
2696 dw_die_ref die;
2697 tree created_for;
2698 struct limbo_die_struct *next;
2700 limbo_die_node;
2702 typedef struct skeleton_chain_struct
2704 dw_die_ref old_die;
2705 dw_die_ref new_die;
2706 struct skeleton_chain_struct *parent;
2708 skeleton_chain_node;
2710 /* Define a macro which returns nonzero for a TYPE_DECL which was
2711 implicitly generated for a type.
2713 Note that, unlike the C front-end (which generates a NULL named
2714 TYPE_DECL node for each complete tagged type, each array type,
2715 and each function type node created) the C++ front-end generates
2716 a _named_ TYPE_DECL node for each tagged type node created.
2717 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2718 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2719 front-end, but for each type, tagged or not. */
2721 #define TYPE_DECL_IS_STUB(decl) \
2722 (DECL_NAME (decl) == NULL_TREE \
2723 || (DECL_ARTIFICIAL (decl) \
2724 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2725 /* This is necessary for stub decls that \
2726 appear in nested inline functions. */ \
2727 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2728 && (decl_ultimate_origin (decl) \
2729 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2731 /* Information concerning the compilation unit's programming
2732 language, and compiler version. */
2734 /* Fixed size portion of the DWARF compilation unit header. */
2735 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2736 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2738 /* Fixed size portion of the DWARF comdat type unit header. */
2739 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2740 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2741 + DWARF_OFFSET_SIZE)
2743 /* Fixed size portion of public names info. */
2744 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2746 /* Fixed size portion of the address range info. */
2747 #define DWARF_ARANGES_HEADER_SIZE \
2748 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2749 DWARF2_ADDR_SIZE * 2) \
2750 - DWARF_INITIAL_LENGTH_SIZE)
2752 /* Size of padding portion in the address range info. It must be
2753 aligned to twice the pointer size. */
2754 #define DWARF_ARANGES_PAD_SIZE \
2755 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2756 DWARF2_ADDR_SIZE * 2) \
2757 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2759 /* Use assembler line directives if available. */
2760 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2761 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2762 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2763 #else
2764 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2765 #endif
2766 #endif
2768 /* Minimum line offset in a special line info. opcode.
2769 This value was chosen to give a reasonable range of values. */
2770 #define DWARF_LINE_BASE -10
2772 /* First special line opcode - leave room for the standard opcodes. */
2773 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2775 /* Range of line offsets in a special line info. opcode. */
2776 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2778 /* Flag that indicates the initial value of the is_stmt_start flag.
2779 In the present implementation, we do not mark any lines as
2780 the beginning of a source statement, because that information
2781 is not made available by the GCC front-end. */
2782 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2784 /* Maximum number of operations per instruction bundle. */
2785 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2786 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2787 #endif
2789 /* This location is used by calc_die_sizes() to keep track
2790 the offset of each DIE within the .debug_info section. */
2791 static unsigned long next_die_offset;
2793 /* Record the root of the DIE's built for the current compilation unit. */
2794 static GTY(()) dw_die_ref single_comp_unit_die;
2796 /* A list of type DIEs that have been separated into comdat sections. */
2797 static GTY(()) comdat_type_node *comdat_type_list;
2799 /* A list of DIEs with a NULL parent waiting to be relocated. */
2800 static GTY(()) limbo_die_node *limbo_die_list;
2802 /* A list of DIEs for which we may have to generate
2803 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2804 static GTY(()) limbo_die_node *deferred_asm_name;
2806 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2808 typedef const char *compare_type;
2810 static hashval_t hash (dwarf_file_data *);
2811 static bool equal (dwarf_file_data *, const char *);
2814 /* Filenames referenced by this compilation unit. */
2815 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2817 struct decl_die_hasher : ggc_hasher<die_node *>
2819 typedef tree compare_type;
2821 static hashval_t hash (die_node *);
2822 static bool equal (die_node *, tree);
2824 /* A hash table of references to DIE's that describe declarations.
2825 The key is a DECL_UID() which is a unique number identifying each decl. */
2826 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2828 struct block_die_hasher : ggc_hasher<die_struct *>
2830 static hashval_t hash (die_struct *);
2831 static bool equal (die_struct *, die_struct *);
2834 /* A hash table of references to DIE's that describe COMMON blocks.
2835 The key is DECL_UID() ^ die_parent. */
2836 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2838 typedef struct GTY(()) die_arg_entry_struct {
2839 dw_die_ref die;
2840 tree arg;
2841 } die_arg_entry;
2844 /* Node of the variable location list. */
2845 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2846 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2847 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2848 in mode of the EXPR_LIST node and first EXPR_LIST operand
2849 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2850 location or NULL for padding. For larger bitsizes,
2851 mode is 0 and first operand is a CONCAT with bitsize
2852 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2853 NULL as second operand. */
2854 rtx GTY (()) loc;
2855 const char * GTY (()) label;
2856 struct var_loc_node * GTY (()) next;
2859 /* Variable location list. */
2860 struct GTY ((for_user)) var_loc_list_def {
2861 struct var_loc_node * GTY (()) first;
2863 /* Pointer to the last but one or last element of the
2864 chained list. If the list is empty, both first and
2865 last are NULL, if the list contains just one node
2866 or the last node certainly is not redundant, it points
2867 to the last node, otherwise points to the last but one.
2868 Do not mark it for GC because it is marked through the chain. */
2869 struct var_loc_node * GTY ((skip ("%h"))) last;
2871 /* Pointer to the last element before section switch,
2872 if NULL, either sections weren't switched or first
2873 is after section switch. */
2874 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2876 /* DECL_UID of the variable decl. */
2877 unsigned int decl_id;
2879 typedef struct var_loc_list_def var_loc_list;
2881 /* Call argument location list. */
2882 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2883 rtx GTY (()) call_arg_loc_note;
2884 const char * GTY (()) label;
2885 tree GTY (()) block;
2886 bool tail_call_p;
2887 rtx GTY (()) symbol_ref;
2888 struct call_arg_loc_node * GTY (()) next;
2892 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2894 typedef const_tree compare_type;
2896 static hashval_t hash (var_loc_list *);
2897 static bool equal (var_loc_list *, const_tree);
2900 /* Table of decl location linked lists. */
2901 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2903 /* Head and tail of call_arg_loc chain. */
2904 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2905 static struct call_arg_loc_node *call_arg_loc_last;
2907 /* Number of call sites in the current function. */
2908 static int call_site_count = -1;
2909 /* Number of tail call sites in the current function. */
2910 static int tail_call_site_count = -1;
2912 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2913 DIEs. */
2914 static vec<dw_die_ref> block_map;
2916 /* A cached location list. */
2917 struct GTY ((for_user)) cached_dw_loc_list_def {
2918 /* The DECL_UID of the decl that this entry describes. */
2919 unsigned int decl_id;
2921 /* The cached location list. */
2922 dw_loc_list_ref loc_list;
2924 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2926 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2929 typedef const_tree compare_type;
2931 static hashval_t hash (cached_dw_loc_list *);
2932 static bool equal (cached_dw_loc_list *, const_tree);
2935 /* Table of cached location lists. */
2936 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2938 /* A pointer to the base of a list of references to DIE's that
2939 are uniquely identified by their tag, presence/absence of
2940 children DIE's, and list of attribute/value pairs. */
2941 static GTY((length ("abbrev_die_table_allocated")))
2942 dw_die_ref *abbrev_die_table;
2944 /* Number of elements currently allocated for abbrev_die_table. */
2945 static GTY(()) unsigned abbrev_die_table_allocated;
2947 /* Number of elements in type_die_table currently in use. */
2948 static GTY(()) unsigned abbrev_die_table_in_use;
2950 /* Size (in elements) of increments by which we may expand the
2951 abbrev_die_table. */
2952 #define ABBREV_DIE_TABLE_INCREMENT 256
2954 /* A global counter for generating labels for line number data. */
2955 static unsigned int line_info_label_num;
2957 /* The current table to which we should emit line number information
2958 for the current function. This will be set up at the beginning of
2959 assembly for the function. */
2960 static dw_line_info_table *cur_line_info_table;
2962 /* The two default tables of line number info. */
2963 static GTY(()) dw_line_info_table *text_section_line_info;
2964 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2966 /* The set of all non-default tables of line number info. */
2967 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2969 /* A flag to tell pubnames/types export if there is an info section to
2970 refer to. */
2971 static bool info_section_emitted;
2973 /* A pointer to the base of a table that contains a list of publicly
2974 accessible names. */
2975 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2977 /* A pointer to the base of a table that contains a list of publicly
2978 accessible types. */
2979 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2981 /* A pointer to the base of a table that contains a list of macro
2982 defines/undefines (and file start/end markers). */
2983 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2985 /* True if .debug_macinfo or .debug_macros section is going to be
2986 emitted. */
2987 #define have_macinfo \
2988 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2989 && !macinfo_table->is_empty ())
2991 /* Array of dies for which we should generate .debug_ranges info. */
2992 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2994 /* Number of elements currently allocated for ranges_table. */
2995 static GTY(()) unsigned ranges_table_allocated;
2997 /* Number of elements in ranges_table currently in use. */
2998 static GTY(()) unsigned ranges_table_in_use;
3000 /* Array of pairs of labels referenced in ranges_table. */
3001 static GTY ((length ("ranges_by_label_allocated")))
3002 dw_ranges_by_label_ref ranges_by_label;
3004 /* Number of elements currently allocated for ranges_by_label. */
3005 static GTY(()) unsigned ranges_by_label_allocated;
3007 /* Number of elements in ranges_by_label currently in use. */
3008 static GTY(()) unsigned ranges_by_label_in_use;
3010 /* Size (in elements) of increments by which we may expand the
3011 ranges_table. */
3012 #define RANGES_TABLE_INCREMENT 64
3014 /* Whether we have location lists that need outputting */
3015 static GTY(()) bool have_location_lists;
3017 /* Unique label counter. */
3018 static GTY(()) unsigned int loclabel_num;
3020 /* Unique label counter for point-of-call tables. */
3021 static GTY(()) unsigned int poc_label_num;
3023 /* The last file entry emitted by maybe_emit_file(). */
3024 static GTY(()) struct dwarf_file_data * last_emitted_file;
3026 /* Number of internal labels generated by gen_internal_sym(). */
3027 static GTY(()) int label_num;
3029 /* Cached result of previous call to lookup_filename. */
3030 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3032 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3034 /* Instances of generic types for which we need to generate debug
3035 info that describe their generic parameters and arguments. That
3036 generation needs to happen once all types are properly laid out so
3037 we do it at the end of compilation. */
3038 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3040 /* Offset from the "steady-state frame pointer" to the frame base,
3041 within the current function. */
3042 static HOST_WIDE_INT frame_pointer_fb_offset;
3043 static bool frame_pointer_fb_offset_valid;
3045 static vec<dw_die_ref> base_types;
3047 /* Flags to represent a set of attribute classes for attributes that represent
3048 a scalar value (bounds, pointers, ...). */
3049 enum dw_scalar_form
3051 dw_scalar_form_constant = 0x01,
3052 dw_scalar_form_exprloc = 0x02,
3053 dw_scalar_form_reference = 0x04
3056 /* Forward declarations for functions defined in this file. */
3058 static int is_pseudo_reg (const_rtx);
3059 static int is_tagged_type (const_tree);
3060 static const char *dwarf_tag_name (unsigned);
3061 static const char *dwarf_attr_name (unsigned);
3062 static const char *dwarf_form_name (unsigned);
3063 static tree decl_ultimate_origin (const_tree);
3064 static tree decl_class_context (tree);
3065 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3066 static inline enum dw_val_class AT_class (dw_attr_ref);
3067 static inline unsigned int AT_index (dw_attr_ref);
3068 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3069 static inline unsigned AT_flag (dw_attr_ref);
3070 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3071 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3072 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3073 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3074 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3075 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3076 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3077 unsigned int, unsigned char *);
3078 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3079 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3080 static inline const char *AT_string (dw_attr_ref);
3081 static enum dwarf_form AT_string_form (dw_attr_ref);
3082 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3083 static void add_AT_specification (dw_die_ref, dw_die_ref);
3084 static inline dw_die_ref AT_ref (dw_attr_ref);
3085 static inline int AT_ref_external (dw_attr_ref);
3086 static inline void set_AT_ref_external (dw_attr_ref, int);
3087 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3088 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3089 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3090 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3091 dw_loc_list_ref);
3092 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3093 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3094 static void remove_addr_table_entry (addr_table_entry *);
3095 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3096 static inline rtx AT_addr (dw_attr_ref);
3097 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3098 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3099 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3100 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3101 unsigned HOST_WIDE_INT);
3102 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3103 unsigned long, bool);
3104 static inline const char *AT_lbl (dw_attr_ref);
3105 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3106 static const char *get_AT_low_pc (dw_die_ref);
3107 static const char *get_AT_hi_pc (dw_die_ref);
3108 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3109 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3110 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3111 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3112 static bool is_cxx (void);
3113 static bool is_fortran (void);
3114 static bool is_ada (void);
3115 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3116 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3117 static void add_child_die (dw_die_ref, dw_die_ref);
3118 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3119 static dw_die_ref lookup_type_die (tree);
3120 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3121 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3122 static void equate_type_number_to_die (tree, dw_die_ref);
3123 static dw_die_ref lookup_decl_die (tree);
3124 static var_loc_list *lookup_decl_loc (const_tree);
3125 static void equate_decl_number_to_die (tree, dw_die_ref);
3126 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3127 static void print_spaces (FILE *);
3128 static void print_die (dw_die_ref, FILE *);
3129 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3130 static dw_die_ref pop_compile_unit (dw_die_ref);
3131 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3132 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3133 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3134 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3135 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3136 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3137 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3138 struct md5_ctx *, int *);
3139 struct checksum_attributes;
3140 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3141 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3142 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3143 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3144 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3145 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3146 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3147 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3148 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3149 static void compute_section_prefix (dw_die_ref);
3150 static int is_type_die (dw_die_ref);
3151 static int is_comdat_die (dw_die_ref);
3152 static int is_symbol_die (dw_die_ref);
3153 static inline bool is_template_instantiation (dw_die_ref);
3154 static void assign_symbol_names (dw_die_ref);
3155 static void break_out_includes (dw_die_ref);
3156 static int is_declaration_die (dw_die_ref);
3157 static int should_move_die_to_comdat (dw_die_ref);
3158 static dw_die_ref clone_as_declaration (dw_die_ref);
3159 static dw_die_ref clone_die (dw_die_ref);
3160 static dw_die_ref clone_tree (dw_die_ref);
3161 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3162 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3163 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3164 static dw_die_ref generate_skeleton (dw_die_ref);
3165 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3166 dw_die_ref,
3167 dw_die_ref);
3168 static void break_out_comdat_types (dw_die_ref);
3169 static void copy_decls_for_unworthy_types (dw_die_ref);
3171 static void add_sibling_attributes (dw_die_ref);
3172 static void output_location_lists (dw_die_ref);
3173 static int constant_size (unsigned HOST_WIDE_INT);
3174 static unsigned long size_of_die (dw_die_ref);
3175 static void calc_die_sizes (dw_die_ref);
3176 static void calc_base_type_die_sizes (void);
3177 static void mark_dies (dw_die_ref);
3178 static void unmark_dies (dw_die_ref);
3179 static void unmark_all_dies (dw_die_ref);
3180 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3181 static unsigned long size_of_aranges (void);
3182 static enum dwarf_form value_format (dw_attr_ref);
3183 static void output_value_format (dw_attr_ref);
3184 static void output_abbrev_section (void);
3185 static void output_die_abbrevs (unsigned long, dw_die_ref);
3186 static void output_die_symbol (dw_die_ref);
3187 static void output_die (dw_die_ref);
3188 static void output_compilation_unit_header (void);
3189 static void output_comp_unit (dw_die_ref, int);
3190 static void output_comdat_type_unit (comdat_type_node *);
3191 static const char *dwarf2_name (tree, int);
3192 static void add_pubname (tree, dw_die_ref);
3193 static void add_enumerator_pubname (const char *, dw_die_ref);
3194 static void add_pubname_string (const char *, dw_die_ref);
3195 static void add_pubtype (tree, dw_die_ref);
3196 static void output_pubnames (vec<pubname_entry, va_gc> *);
3197 static void output_aranges (unsigned long);
3198 static unsigned int add_ranges_num (int);
3199 static unsigned int add_ranges (const_tree);
3200 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3201 bool *, bool);
3202 static void output_ranges (void);
3203 static dw_line_info_table *new_line_info_table (void);
3204 static void output_line_info (bool);
3205 static void output_file_names (void);
3206 static dw_die_ref base_type_die (tree);
3207 static int is_base_type (tree);
3208 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3209 static int decl_quals (const_tree);
3210 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3211 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3212 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3213 static int type_is_enum (const_tree);
3214 static unsigned int dbx_reg_number (const_rtx);
3215 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3216 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3217 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3218 enum var_init_status);
3219 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3220 enum var_init_status);
3221 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3222 enum var_init_status);
3223 static int is_based_loc (const_rtx);
3224 static bool resolve_one_addr (rtx *);
3225 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3226 enum var_init_status);
3227 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3228 enum var_init_status);
3229 struct loc_descr_context;
3230 static dw_loc_list_ref loc_list_from_tree (tree, int,
3231 const struct loc_descr_context *);
3232 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3233 const struct loc_descr_context *);
3234 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3235 static tree field_type (const_tree);
3236 static unsigned int simple_type_align_in_bits (const_tree);
3237 static unsigned int simple_decl_align_in_bits (const_tree);
3238 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3239 static HOST_WIDE_INT field_byte_offset (const_tree);
3240 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3241 dw_loc_list_ref);
3242 static void add_data_member_location_attribute (dw_die_ref, tree);
3243 static bool add_const_value_attribute (dw_die_ref, rtx);
3244 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3245 static void insert_wide_int (const wide_int &, unsigned char *, int);
3246 static void insert_float (const_rtx, unsigned char *);
3247 static rtx rtl_for_decl_location (tree);
3248 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3249 enum dwarf_attribute);
3250 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3251 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3252 static void add_name_attribute (dw_die_ref, const char *);
3253 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3254 static void add_comp_dir_attribute (dw_die_ref);
3255 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3256 const struct loc_descr_context *);
3257 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3258 const struct loc_descr_context *);
3259 static void add_subscript_info (dw_die_ref, tree, bool);
3260 static void add_byte_size_attribute (dw_die_ref, tree);
3261 static void add_bit_offset_attribute (dw_die_ref, tree);
3262 static void add_bit_size_attribute (dw_die_ref, tree);
3263 static void add_prototyped_attribute (dw_die_ref, tree);
3264 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3265 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3266 static void add_src_coords_attributes (dw_die_ref, tree);
3267 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3268 static void push_decl_scope (tree);
3269 static void pop_decl_scope (void);
3270 static dw_die_ref scope_die_for (tree, dw_die_ref);
3271 static inline int local_scope_p (dw_die_ref);
3272 static inline int class_scope_p (dw_die_ref);
3273 static inline int class_or_namespace_scope_p (dw_die_ref);
3274 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3275 static void add_calling_convention_attribute (dw_die_ref, tree);
3276 static const char *type_tag (const_tree);
3277 static tree member_declared_type (const_tree);
3278 #if 0
3279 static const char *decl_start_label (tree);
3280 #endif
3281 static void gen_array_type_die (tree, dw_die_ref);
3282 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3283 #if 0
3284 static void gen_entry_point_die (tree, dw_die_ref);
3285 #endif
3286 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3287 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3288 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3289 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3290 static void gen_formal_types_die (tree, dw_die_ref);
3291 static void gen_subprogram_die (tree, dw_die_ref);
3292 static void gen_variable_die (tree, tree, dw_die_ref);
3293 static void gen_const_die (tree, dw_die_ref);
3294 static void gen_label_die (tree, dw_die_ref);
3295 static void gen_lexical_block_die (tree, dw_die_ref);
3296 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3297 static void gen_field_die (tree, dw_die_ref);
3298 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3299 static dw_die_ref gen_compile_unit_die (const char *);
3300 static void gen_inheritance_die (tree, tree, dw_die_ref);
3301 static void gen_member_die (tree, dw_die_ref);
3302 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3303 enum debug_info_usage);
3304 static void gen_subroutine_type_die (tree, dw_die_ref);
3305 static void gen_typedef_die (tree, dw_die_ref);
3306 static void gen_type_die (tree, dw_die_ref);
3307 static void gen_block_die (tree, dw_die_ref);
3308 static void decls_for_scope (tree, dw_die_ref);
3309 static inline int is_redundant_typedef (const_tree);
3310 static bool is_naming_typedef_decl (const_tree);
3311 static inline dw_die_ref get_context_die (tree);
3312 static void gen_namespace_die (tree, dw_die_ref);
3313 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3314 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3315 static dw_die_ref force_decl_die (tree);
3316 static dw_die_ref force_type_die (tree);
3317 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3318 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3319 static struct dwarf_file_data * lookup_filename (const char *);
3320 static void retry_incomplete_types (void);
3321 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3322 static void gen_generic_params_dies (tree);
3323 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3324 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3325 static void splice_child_die (dw_die_ref, dw_die_ref);
3326 static int file_info_cmp (const void *, const void *);
3327 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3328 const char *, const char *);
3329 static void output_loc_list (dw_loc_list_ref);
3330 static char *gen_internal_sym (const char *);
3331 static bool want_pubnames (void);
3333 static void prune_unmark_dies (dw_die_ref);
3334 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3335 static void prune_unused_types_mark (dw_die_ref, int);
3336 static void prune_unused_types_walk (dw_die_ref);
3337 static void prune_unused_types_walk_attribs (dw_die_ref);
3338 static void prune_unused_types_prune (dw_die_ref);
3339 static void prune_unused_types (void);
3340 static int maybe_emit_file (struct dwarf_file_data *fd);
3341 static inline const char *AT_vms_delta1 (dw_attr_ref);
3342 static inline const char *AT_vms_delta2 (dw_attr_ref);
3343 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3344 const char *, const char *);
3345 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3346 static void gen_remaining_tmpl_value_param_die_attribute (void);
3347 static bool generic_type_p (tree);
3348 static void schedule_generic_params_dies_gen (tree t);
3349 static void gen_scheduled_generic_parms_dies (void);
3351 static const char *comp_dir_string (void);
3353 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3355 /* enum for tracking thread-local variables whose address is really an offset
3356 relative to the TLS pointer, which will need link-time relocation, but will
3357 not need relocation by the DWARF consumer. */
3359 enum dtprel_bool
3361 dtprel_false = 0,
3362 dtprel_true = 1
3365 /* Return the operator to use for an address of a variable. For dtprel_true, we
3366 use DW_OP_const*. For regular variables, which need both link-time
3367 relocation and consumer-level relocation (e.g., to account for shared objects
3368 loaded at a random address), we use DW_OP_addr*. */
3370 static inline enum dwarf_location_atom
3371 dw_addr_op (enum dtprel_bool dtprel)
3373 if (dtprel == dtprel_true)
3374 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3375 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3376 else
3377 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3380 /* Return a pointer to a newly allocated address location description. If
3381 dwarf_split_debug_info is true, then record the address with the appropriate
3382 relocation. */
3383 static inline dw_loc_descr_ref
3384 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3386 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3388 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3389 ref->dw_loc_oprnd1.v.val_addr = addr;
3390 ref->dtprel = dtprel;
3391 if (dwarf_split_debug_info)
3392 ref->dw_loc_oprnd1.val_entry
3393 = add_addr_table_entry (addr,
3394 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3395 else
3396 ref->dw_loc_oprnd1.val_entry = NULL;
3398 return ref;
3401 /* Section names used to hold DWARF debugging information. */
3403 #ifndef DEBUG_INFO_SECTION
3404 #define DEBUG_INFO_SECTION ".debug_info"
3405 #endif
3406 #ifndef DEBUG_DWO_INFO_SECTION
3407 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3408 #endif
3409 #ifndef DEBUG_ABBREV_SECTION
3410 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3411 #endif
3412 #ifndef DEBUG_DWO_ABBREV_SECTION
3413 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3414 #endif
3415 #ifndef DEBUG_ARANGES_SECTION
3416 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3417 #endif
3418 #ifndef DEBUG_ADDR_SECTION
3419 #define DEBUG_ADDR_SECTION ".debug_addr"
3420 #endif
3421 #ifndef DEBUG_NORM_MACINFO_SECTION
3422 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3423 #endif
3424 #ifndef DEBUG_DWO_MACINFO_SECTION
3425 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3426 #endif
3427 #ifndef DEBUG_MACINFO_SECTION
3428 #define DEBUG_MACINFO_SECTION \
3429 (!dwarf_split_debug_info \
3430 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3431 #endif
3432 #ifndef DEBUG_NORM_MACRO_SECTION
3433 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3434 #endif
3435 #ifndef DEBUG_DWO_MACRO_SECTION
3436 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3437 #endif
3438 #ifndef DEBUG_MACRO_SECTION
3439 #define DEBUG_MACRO_SECTION \
3440 (!dwarf_split_debug_info \
3441 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3442 #endif
3443 #ifndef DEBUG_LINE_SECTION
3444 #define DEBUG_LINE_SECTION ".debug_line"
3445 #endif
3446 #ifndef DEBUG_DWO_LINE_SECTION
3447 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3448 #endif
3449 #ifndef DEBUG_LOC_SECTION
3450 #define DEBUG_LOC_SECTION ".debug_loc"
3451 #endif
3452 #ifndef DEBUG_DWO_LOC_SECTION
3453 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3454 #endif
3455 #ifndef DEBUG_PUBNAMES_SECTION
3456 #define DEBUG_PUBNAMES_SECTION \
3457 ((debug_generate_pub_sections == 2) \
3458 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3459 #endif
3460 #ifndef DEBUG_PUBTYPES_SECTION
3461 #define DEBUG_PUBTYPES_SECTION \
3462 ((debug_generate_pub_sections == 2) \
3463 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3464 #endif
3465 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3466 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3467 #ifndef DEBUG_STR_OFFSETS_SECTION
3468 #define DEBUG_STR_OFFSETS_SECTION \
3469 (!dwarf_split_debug_info \
3470 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3471 #endif
3472 #ifndef DEBUG_STR_DWO_SECTION
3473 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3474 #endif
3475 #ifndef DEBUG_STR_SECTION
3476 #define DEBUG_STR_SECTION ".debug_str"
3477 #endif
3478 #ifndef DEBUG_RANGES_SECTION
3479 #define DEBUG_RANGES_SECTION ".debug_ranges"
3480 #endif
3482 /* Standard ELF section names for compiled code and data. */
3483 #ifndef TEXT_SECTION_NAME
3484 #define TEXT_SECTION_NAME ".text"
3485 #endif
3487 /* Section flags for .debug_macinfo/.debug_macro section. */
3488 #define DEBUG_MACRO_SECTION_FLAGS \
3489 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3491 /* Section flags for .debug_str section. */
3492 #define DEBUG_STR_SECTION_FLAGS \
3493 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3494 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3495 : SECTION_DEBUG)
3497 /* Section flags for .debug_str.dwo section. */
3498 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3500 /* Labels we insert at beginning sections we can reference instead of
3501 the section names themselves. */
3503 #ifndef TEXT_SECTION_LABEL
3504 #define TEXT_SECTION_LABEL "Ltext"
3505 #endif
3506 #ifndef COLD_TEXT_SECTION_LABEL
3507 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3508 #endif
3509 #ifndef DEBUG_LINE_SECTION_LABEL
3510 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3511 #endif
3512 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3513 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3514 #endif
3515 #ifndef DEBUG_INFO_SECTION_LABEL
3516 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3517 #endif
3518 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3519 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3520 #endif
3521 #ifndef DEBUG_ABBREV_SECTION_LABEL
3522 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3523 #endif
3524 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3525 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3526 #endif
3527 #ifndef DEBUG_ADDR_SECTION_LABEL
3528 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3529 #endif
3530 #ifndef DEBUG_LOC_SECTION_LABEL
3531 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3532 #endif
3533 #ifndef DEBUG_RANGES_SECTION_LABEL
3534 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3535 #endif
3536 #ifndef DEBUG_MACINFO_SECTION_LABEL
3537 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3538 #endif
3539 #ifndef DEBUG_MACRO_SECTION_LABEL
3540 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3541 #endif
3542 #define SKELETON_COMP_DIE_ABBREV 1
3543 #define SKELETON_TYPE_DIE_ABBREV 2
3545 /* Definitions of defaults for formats and names of various special
3546 (artificial) labels which may be generated within this file (when the -g
3547 options is used and DWARF2_DEBUGGING_INFO is in effect.
3548 If necessary, these may be overridden from within the tm.h file, but
3549 typically, overriding these defaults is unnecessary. */
3551 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3552 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3553 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3554 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3555 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3556 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3557 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3558 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3559 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3560 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3561 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3562 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3563 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3564 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3566 #ifndef TEXT_END_LABEL
3567 #define TEXT_END_LABEL "Letext"
3568 #endif
3569 #ifndef COLD_END_LABEL
3570 #define COLD_END_LABEL "Letext_cold"
3571 #endif
3572 #ifndef BLOCK_BEGIN_LABEL
3573 #define BLOCK_BEGIN_LABEL "LBB"
3574 #endif
3575 #ifndef BLOCK_END_LABEL
3576 #define BLOCK_END_LABEL "LBE"
3577 #endif
3578 #ifndef LINE_CODE_LABEL
3579 #define LINE_CODE_LABEL "LM"
3580 #endif
3583 /* Return the root of the DIE's built for the current compilation unit. */
3584 static dw_die_ref
3585 comp_unit_die (void)
3587 if (!single_comp_unit_die)
3588 single_comp_unit_die = gen_compile_unit_die (NULL);
3589 return single_comp_unit_die;
3592 /* We allow a language front-end to designate a function that is to be
3593 called to "demangle" any name before it is put into a DIE. */
3595 static const char *(*demangle_name_func) (const char *);
3597 void
3598 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3600 demangle_name_func = func;
3603 /* Test if rtl node points to a pseudo register. */
3605 static inline int
3606 is_pseudo_reg (const_rtx rtl)
3608 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3609 || (GET_CODE (rtl) == SUBREG
3610 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3613 /* Return a reference to a type, with its const and volatile qualifiers
3614 removed. */
3616 static inline tree
3617 type_main_variant (tree type)
3619 type = TYPE_MAIN_VARIANT (type);
3621 /* ??? There really should be only one main variant among any group of
3622 variants of a given type (and all of the MAIN_VARIANT values for all
3623 members of the group should point to that one type) but sometimes the C
3624 front-end messes this up for array types, so we work around that bug
3625 here. */
3626 if (TREE_CODE (type) == ARRAY_TYPE)
3627 while (type != TYPE_MAIN_VARIANT (type))
3628 type = TYPE_MAIN_VARIANT (type);
3630 return type;
3633 /* Return nonzero if the given type node represents a tagged type. */
3635 static inline int
3636 is_tagged_type (const_tree type)
3638 enum tree_code code = TREE_CODE (type);
3640 return (code == RECORD_TYPE || code == UNION_TYPE
3641 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3644 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3646 static void
3647 get_ref_die_offset_label (char *label, dw_die_ref ref)
3649 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3652 /* Return die_offset of a DIE reference to a base type. */
3654 static unsigned long int
3655 get_base_type_offset (dw_die_ref ref)
3657 if (ref->die_offset)
3658 return ref->die_offset;
3659 if (comp_unit_die ()->die_abbrev)
3661 calc_base_type_die_sizes ();
3662 gcc_assert (ref->die_offset);
3664 return ref->die_offset;
3667 /* Return die_offset of a DIE reference other than base type. */
3669 static unsigned long int
3670 get_ref_die_offset (dw_die_ref ref)
3672 gcc_assert (ref->die_offset);
3673 return ref->die_offset;
3676 /* Convert a DIE tag into its string name. */
3678 static const char *
3679 dwarf_tag_name (unsigned int tag)
3681 const char *name = get_DW_TAG_name (tag);
3683 if (name != NULL)
3684 return name;
3686 return "DW_TAG_<unknown>";
3689 /* Convert a DWARF attribute code into its string name. */
3691 static const char *
3692 dwarf_attr_name (unsigned int attr)
3694 const char *name;
3696 switch (attr)
3698 #if VMS_DEBUGGING_INFO
3699 case DW_AT_HP_prologue:
3700 return "DW_AT_HP_prologue";
3701 #else
3702 case DW_AT_MIPS_loop_unroll_factor:
3703 return "DW_AT_MIPS_loop_unroll_factor";
3704 #endif
3706 #if VMS_DEBUGGING_INFO
3707 case DW_AT_HP_epilogue:
3708 return "DW_AT_HP_epilogue";
3709 #else
3710 case DW_AT_MIPS_stride:
3711 return "DW_AT_MIPS_stride";
3712 #endif
3715 name = get_DW_AT_name (attr);
3717 if (name != NULL)
3718 return name;
3720 return "DW_AT_<unknown>";
3723 /* Convert a DWARF value form code into its string name. */
3725 static const char *
3726 dwarf_form_name (unsigned int form)
3728 const char *name = get_DW_FORM_name (form);
3730 if (name != NULL)
3731 return name;
3733 return "DW_FORM_<unknown>";
3736 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3737 instance of an inlined instance of a decl which is local to an inline
3738 function, so we have to trace all of the way back through the origin chain
3739 to find out what sort of node actually served as the original seed for the
3740 given block. */
3742 static tree
3743 decl_ultimate_origin (const_tree decl)
3745 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3746 return NULL_TREE;
3748 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3749 we're trying to output the abstract instance of this function. */
3750 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3751 return NULL_TREE;
3753 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3754 most distant ancestor, this should never happen. */
3755 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3757 return DECL_ABSTRACT_ORIGIN (decl);
3760 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3761 of a virtual function may refer to a base class, so we check the 'this'
3762 parameter. */
3764 static tree
3765 decl_class_context (tree decl)
3767 tree context = NULL_TREE;
3769 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3770 context = DECL_CONTEXT (decl);
3771 else
3772 context = TYPE_MAIN_VARIANT
3773 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3775 if (context && !TYPE_P (context))
3776 context = NULL_TREE;
3778 return context;
3781 /* Add an attribute/value pair to a DIE. */
3783 static inline void
3784 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3786 /* Maybe this should be an assert? */
3787 if (die == NULL)
3788 return;
3790 vec_safe_reserve (die->die_attr, 1);
3791 vec_safe_push (die->die_attr, *attr);
3794 static inline enum dw_val_class
3795 AT_class (dw_attr_ref a)
3797 return a->dw_attr_val.val_class;
3800 /* Return the index for any attribute that will be referenced with a
3801 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3802 are stored in dw_attr_val.v.val_str for reference counting
3803 pruning. */
3805 static inline unsigned int
3806 AT_index (dw_attr_ref a)
3808 if (AT_class (a) == dw_val_class_str)
3809 return a->dw_attr_val.v.val_str->index;
3810 else if (a->dw_attr_val.val_entry != NULL)
3811 return a->dw_attr_val.val_entry->index;
3812 return NOT_INDEXED;
3815 /* Add a flag value attribute to a DIE. */
3817 static inline void
3818 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3820 dw_attr_node attr;
3822 attr.dw_attr = attr_kind;
3823 attr.dw_attr_val.val_class = dw_val_class_flag;
3824 attr.dw_attr_val.val_entry = NULL;
3825 attr.dw_attr_val.v.val_flag = flag;
3826 add_dwarf_attr (die, &attr);
3829 static inline unsigned
3830 AT_flag (dw_attr_ref a)
3832 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3833 return a->dw_attr_val.v.val_flag;
3836 /* Add a signed integer attribute value to a DIE. */
3838 static inline void
3839 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3841 dw_attr_node attr;
3843 attr.dw_attr = attr_kind;
3844 attr.dw_attr_val.val_class = dw_val_class_const;
3845 attr.dw_attr_val.val_entry = NULL;
3846 attr.dw_attr_val.v.val_int = int_val;
3847 add_dwarf_attr (die, &attr);
3850 static inline HOST_WIDE_INT
3851 AT_int (dw_attr_ref a)
3853 gcc_assert (a && AT_class (a) == dw_val_class_const);
3854 return a->dw_attr_val.v.val_int;
3857 /* Add an unsigned integer attribute value to a DIE. */
3859 static inline void
3860 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3861 unsigned HOST_WIDE_INT unsigned_val)
3863 dw_attr_node attr;
3865 attr.dw_attr = attr_kind;
3866 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3867 attr.dw_attr_val.val_entry = NULL;
3868 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3869 add_dwarf_attr (die, &attr);
3872 static inline unsigned HOST_WIDE_INT
3873 AT_unsigned (dw_attr_ref a)
3875 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3876 return a->dw_attr_val.v.val_unsigned;
3879 /* Add an unsigned wide integer attribute value to a DIE. */
3881 static inline void
3882 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3883 const wide_int& w)
3885 dw_attr_node attr;
3887 attr.dw_attr = attr_kind;
3888 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3889 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3890 *attr.dw_attr_val.v.val_wide = w;
3891 add_dwarf_attr (die, &attr);
3894 /* Add an unsigned double integer attribute value to a DIE. */
3896 static inline void
3897 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3898 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3900 dw_attr_node attr;
3902 attr.dw_attr = attr_kind;
3903 attr.dw_attr_val.val_class = dw_val_class_const_double;
3904 attr.dw_attr_val.val_entry = NULL;
3905 attr.dw_attr_val.v.val_double.high = high;
3906 attr.dw_attr_val.v.val_double.low = low;
3907 add_dwarf_attr (die, &attr);
3910 /* Add a floating point attribute value to a DIE and return it. */
3912 static inline void
3913 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3914 unsigned int length, unsigned int elt_size, unsigned char *array)
3916 dw_attr_node attr;
3918 attr.dw_attr = attr_kind;
3919 attr.dw_attr_val.val_class = dw_val_class_vec;
3920 attr.dw_attr_val.val_entry = NULL;
3921 attr.dw_attr_val.v.val_vec.length = length;
3922 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3923 attr.dw_attr_val.v.val_vec.array = array;
3924 add_dwarf_attr (die, &attr);
3927 /* Add an 8-byte data attribute value to a DIE. */
3929 static inline void
3930 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3931 unsigned char data8[8])
3933 dw_attr_node attr;
3935 attr.dw_attr = attr_kind;
3936 attr.dw_attr_val.val_class = dw_val_class_data8;
3937 attr.dw_attr_val.val_entry = NULL;
3938 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3939 add_dwarf_attr (die, &attr);
3942 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3943 dwarf_split_debug_info, address attributes in dies destined for the
3944 final executable have force_direct set to avoid using indexed
3945 references. */
3947 static inline void
3948 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3949 bool force_direct)
3951 dw_attr_node attr;
3952 char * lbl_id;
3954 lbl_id = xstrdup (lbl_low);
3955 attr.dw_attr = DW_AT_low_pc;
3956 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3957 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3958 if (dwarf_split_debug_info && !force_direct)
3959 attr.dw_attr_val.val_entry
3960 = add_addr_table_entry (lbl_id, ate_kind_label);
3961 else
3962 attr.dw_attr_val.val_entry = NULL;
3963 add_dwarf_attr (die, &attr);
3965 attr.dw_attr = DW_AT_high_pc;
3966 if (dwarf_version < 4)
3967 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3968 else
3969 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3970 lbl_id = xstrdup (lbl_high);
3971 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3972 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3973 && dwarf_split_debug_info && !force_direct)
3974 attr.dw_attr_val.val_entry
3975 = add_addr_table_entry (lbl_id, ate_kind_label);
3976 else
3977 attr.dw_attr_val.val_entry = NULL;
3978 add_dwarf_attr (die, &attr);
3981 /* Hash and equality functions for debug_str_hash. */
3983 hashval_t
3984 indirect_string_hasher::hash (indirect_string_node *x)
3986 return htab_hash_string (x->str);
3989 bool
3990 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3992 return strcmp (x1->str, x2) == 0;
3995 /* Add STR to the given string hash table. */
3997 static struct indirect_string_node *
3998 find_AT_string_in_table (const char *str,
3999 hash_table<indirect_string_hasher> *table)
4001 struct indirect_string_node *node;
4003 indirect_string_node **slot
4004 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4005 if (*slot == NULL)
4007 node = ggc_cleared_alloc<indirect_string_node> ();
4008 node->str = ggc_strdup (str);
4009 *slot = node;
4011 else
4012 node = *slot;
4014 node->refcount++;
4015 return node;
4018 /* Add STR to the indirect string hash table. */
4020 static struct indirect_string_node *
4021 find_AT_string (const char *str)
4023 if (! debug_str_hash)
4024 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4026 return find_AT_string_in_table (str, debug_str_hash);
4029 /* Add a string attribute value to a DIE. */
4031 static inline void
4032 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4034 dw_attr_node attr;
4035 struct indirect_string_node *node;
4037 node = find_AT_string (str);
4039 attr.dw_attr = attr_kind;
4040 attr.dw_attr_val.val_class = dw_val_class_str;
4041 attr.dw_attr_val.val_entry = NULL;
4042 attr.dw_attr_val.v.val_str = node;
4043 add_dwarf_attr (die, &attr);
4046 static inline const char *
4047 AT_string (dw_attr_ref a)
4049 gcc_assert (a && AT_class (a) == dw_val_class_str);
4050 return a->dw_attr_val.v.val_str->str;
4053 /* Call this function directly to bypass AT_string_form's logic to put
4054 the string inline in the die. */
4056 static void
4057 set_indirect_string (struct indirect_string_node *node)
4059 char label[32];
4060 /* Already indirect is a no op. */
4061 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4063 gcc_assert (node->label);
4064 return;
4066 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4067 ++dw2_string_counter;
4068 node->label = xstrdup (label);
4070 if (!dwarf_split_debug_info)
4072 node->form = DW_FORM_strp;
4073 node->index = NOT_INDEXED;
4075 else
4077 node->form = DW_FORM_GNU_str_index;
4078 node->index = NO_INDEX_ASSIGNED;
4082 /* Find out whether a string should be output inline in DIE
4083 or out-of-line in .debug_str section. */
4085 static enum dwarf_form
4086 find_string_form (struct indirect_string_node *node)
4088 unsigned int len;
4090 if (node->form)
4091 return node->form;
4093 len = strlen (node->str) + 1;
4095 /* If the string is shorter or equal to the size of the reference, it is
4096 always better to put it inline. */
4097 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4098 return node->form = DW_FORM_string;
4100 /* If we cannot expect the linker to merge strings in .debug_str
4101 section, only put it into .debug_str if it is worth even in this
4102 single module. */
4103 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4104 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4105 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4106 return node->form = DW_FORM_string;
4108 set_indirect_string (node);
4110 return node->form;
4113 /* Find out whether the string referenced from the attribute should be
4114 output inline in DIE or out-of-line in .debug_str section. */
4116 static enum dwarf_form
4117 AT_string_form (dw_attr_ref a)
4119 gcc_assert (a && AT_class (a) == dw_val_class_str);
4120 return find_string_form (a->dw_attr_val.v.val_str);
4123 /* Add a DIE reference attribute value to a DIE. */
4125 static inline void
4126 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4128 dw_attr_node attr;
4130 #ifdef ENABLE_CHECKING
4131 gcc_assert (targ_die != NULL);
4132 #else
4133 /* With LTO we can end up trying to reference something we didn't create
4134 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4135 if (targ_die == NULL)
4136 return;
4137 #endif
4139 attr.dw_attr = attr_kind;
4140 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4141 attr.dw_attr_val.val_entry = NULL;
4142 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4143 attr.dw_attr_val.v.val_die_ref.external = 0;
4144 add_dwarf_attr (die, &attr);
4147 /* Change DIE reference REF to point to NEW_DIE instead. */
4149 static inline void
4150 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4152 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4153 ref->dw_attr_val.v.val_die_ref.die = new_die;
4154 ref->dw_attr_val.v.val_die_ref.external = 0;
4157 /* Add an AT_specification attribute to a DIE, and also make the back
4158 pointer from the specification to the definition. */
4160 static inline void
4161 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4163 add_AT_die_ref (die, DW_AT_specification, targ_die);
4164 gcc_assert (!targ_die->die_definition);
4165 targ_die->die_definition = die;
4168 static inline dw_die_ref
4169 AT_ref (dw_attr_ref a)
4171 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4172 return a->dw_attr_val.v.val_die_ref.die;
4175 static inline int
4176 AT_ref_external (dw_attr_ref a)
4178 if (a && AT_class (a) == dw_val_class_die_ref)
4179 return a->dw_attr_val.v.val_die_ref.external;
4181 return 0;
4184 static inline void
4185 set_AT_ref_external (dw_attr_ref a, int i)
4187 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4188 a->dw_attr_val.v.val_die_ref.external = i;
4191 /* Add an FDE reference attribute value to a DIE. */
4193 static inline void
4194 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4196 dw_attr_node attr;
4198 attr.dw_attr = attr_kind;
4199 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4200 attr.dw_attr_val.val_entry = NULL;
4201 attr.dw_attr_val.v.val_fde_index = targ_fde;
4202 add_dwarf_attr (die, &attr);
4205 /* Add a location description attribute value to a DIE. */
4207 static inline void
4208 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4210 dw_attr_node attr;
4212 attr.dw_attr = attr_kind;
4213 attr.dw_attr_val.val_class = dw_val_class_loc;
4214 attr.dw_attr_val.val_entry = NULL;
4215 attr.dw_attr_val.v.val_loc = loc;
4216 add_dwarf_attr (die, &attr);
4219 static inline dw_loc_descr_ref
4220 AT_loc (dw_attr_ref a)
4222 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4223 return a->dw_attr_val.v.val_loc;
4226 static inline void
4227 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4229 dw_attr_node attr;
4231 attr.dw_attr = attr_kind;
4232 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4233 attr.dw_attr_val.val_entry = NULL;
4234 attr.dw_attr_val.v.val_loc_list = loc_list;
4235 add_dwarf_attr (die, &attr);
4236 have_location_lists = true;
4239 static inline dw_loc_list_ref
4240 AT_loc_list (dw_attr_ref a)
4242 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4243 return a->dw_attr_val.v.val_loc_list;
4246 static inline dw_loc_list_ref *
4247 AT_loc_list_ptr (dw_attr_ref a)
4249 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4250 return &a->dw_attr_val.v.val_loc_list;
4253 struct addr_hasher : ggc_hasher<addr_table_entry *>
4255 static hashval_t hash (addr_table_entry *);
4256 static bool equal (addr_table_entry *, addr_table_entry *);
4259 /* Table of entries into the .debug_addr section. */
4261 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4263 /* Hash an address_table_entry. */
4265 hashval_t
4266 addr_hasher::hash (addr_table_entry *a)
4268 inchash::hash hstate;
4269 switch (a->kind)
4271 case ate_kind_rtx:
4272 hstate.add_int (0);
4273 break;
4274 case ate_kind_rtx_dtprel:
4275 hstate.add_int (1);
4276 break;
4277 case ate_kind_label:
4278 return htab_hash_string (a->addr.label);
4279 default:
4280 gcc_unreachable ();
4282 inchash::add_rtx (a->addr.rtl, hstate);
4283 return hstate.end ();
4286 /* Determine equality for two address_table_entries. */
4288 bool
4289 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4291 if (a1->kind != a2->kind)
4292 return 0;
4293 switch (a1->kind)
4295 case ate_kind_rtx:
4296 case ate_kind_rtx_dtprel:
4297 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4298 case ate_kind_label:
4299 return strcmp (a1->addr.label, a2->addr.label) == 0;
4300 default:
4301 gcc_unreachable ();
4305 /* Initialize an addr_table_entry. */
4307 void
4308 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4310 e->kind = kind;
4311 switch (kind)
4313 case ate_kind_rtx:
4314 case ate_kind_rtx_dtprel:
4315 e->addr.rtl = (rtx) addr;
4316 break;
4317 case ate_kind_label:
4318 e->addr.label = (char *) addr;
4319 break;
4321 e->refcount = 0;
4322 e->index = NO_INDEX_ASSIGNED;
4325 /* Add attr to the address table entry to the table. Defer setting an
4326 index until output time. */
4328 static addr_table_entry *
4329 add_addr_table_entry (void *addr, enum ate_kind kind)
4331 addr_table_entry *node;
4332 addr_table_entry finder;
4334 gcc_assert (dwarf_split_debug_info);
4335 if (! addr_index_table)
4336 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4337 init_addr_table_entry (&finder, kind, addr);
4338 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4340 if (*slot == HTAB_EMPTY_ENTRY)
4342 node = ggc_cleared_alloc<addr_table_entry> ();
4343 init_addr_table_entry (node, kind, addr);
4344 *slot = node;
4346 else
4347 node = *slot;
4349 node->refcount++;
4350 return node;
4353 /* Remove an entry from the addr table by decrementing its refcount.
4354 Strictly, decrementing the refcount would be enough, but the
4355 assertion that the entry is actually in the table has found
4356 bugs. */
4358 static void
4359 remove_addr_table_entry (addr_table_entry *entry)
4361 gcc_assert (dwarf_split_debug_info && addr_index_table);
4362 /* After an index is assigned, the table is frozen. */
4363 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4364 entry->refcount--;
4367 /* Given a location list, remove all addresses it refers to from the
4368 address_table. */
4370 static void
4371 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4373 for (; descr; descr = descr->dw_loc_next)
4374 if (descr->dw_loc_oprnd1.val_entry != NULL)
4376 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4377 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4381 /* A helper function for dwarf2out_finish called through
4382 htab_traverse. Assign an addr_table_entry its index. All entries
4383 must be collected into the table when this function is called,
4384 because the indexing code relies on htab_traverse to traverse nodes
4385 in the same order for each run. */
4388 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4390 addr_table_entry *node = *h;
4392 /* Don't index unreferenced nodes. */
4393 if (node->refcount == 0)
4394 return 1;
4396 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4397 node->index = *index;
4398 *index += 1;
4400 return 1;
4403 /* Add an address constant attribute value to a DIE. When using
4404 dwarf_split_debug_info, address attributes in dies destined for the
4405 final executable should be direct references--setting the parameter
4406 force_direct ensures this behavior. */
4408 static inline void
4409 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4410 bool force_direct)
4412 dw_attr_node attr;
4414 attr.dw_attr = attr_kind;
4415 attr.dw_attr_val.val_class = dw_val_class_addr;
4416 attr.dw_attr_val.v.val_addr = addr;
4417 if (dwarf_split_debug_info && !force_direct)
4418 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4419 else
4420 attr.dw_attr_val.val_entry = NULL;
4421 add_dwarf_attr (die, &attr);
4424 /* Get the RTX from to an address DIE attribute. */
4426 static inline rtx
4427 AT_addr (dw_attr_ref a)
4429 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4430 return a->dw_attr_val.v.val_addr;
4433 /* Add a file attribute value to a DIE. */
4435 static inline void
4436 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4437 struct dwarf_file_data *fd)
4439 dw_attr_node attr;
4441 attr.dw_attr = attr_kind;
4442 attr.dw_attr_val.val_class = dw_val_class_file;
4443 attr.dw_attr_val.val_entry = NULL;
4444 attr.dw_attr_val.v.val_file = fd;
4445 add_dwarf_attr (die, &attr);
4448 /* Get the dwarf_file_data from a file DIE attribute. */
4450 static inline struct dwarf_file_data *
4451 AT_file (dw_attr_ref a)
4453 gcc_assert (a && AT_class (a) == dw_val_class_file);
4454 return a->dw_attr_val.v.val_file;
4457 /* Add a vms delta attribute value to a DIE. */
4459 static inline void
4460 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4461 const char *lbl1, const char *lbl2)
4463 dw_attr_node attr;
4465 attr.dw_attr = attr_kind;
4466 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4467 attr.dw_attr_val.val_entry = NULL;
4468 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4469 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4470 add_dwarf_attr (die, &attr);
4473 /* Add a label identifier attribute value to a DIE. */
4475 static inline void
4476 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4477 const char *lbl_id)
4479 dw_attr_node attr;
4481 attr.dw_attr = attr_kind;
4482 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4483 attr.dw_attr_val.val_entry = NULL;
4484 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4485 if (dwarf_split_debug_info)
4486 attr.dw_attr_val.val_entry
4487 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4488 ate_kind_label);
4489 add_dwarf_attr (die, &attr);
4492 /* Add a section offset attribute value to a DIE, an offset into the
4493 debug_line section. */
4495 static inline void
4496 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4497 const char *label)
4499 dw_attr_node attr;
4501 attr.dw_attr = attr_kind;
4502 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4503 attr.dw_attr_val.val_entry = NULL;
4504 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4505 add_dwarf_attr (die, &attr);
4508 /* Add a section offset attribute value to a DIE, an offset into the
4509 debug_macinfo section. */
4511 static inline void
4512 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4513 const char *label)
4515 dw_attr_node attr;
4517 attr.dw_attr = attr_kind;
4518 attr.dw_attr_val.val_class = dw_val_class_macptr;
4519 attr.dw_attr_val.val_entry = NULL;
4520 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4521 add_dwarf_attr (die, &attr);
4524 /* Add an offset attribute value to a DIE. */
4526 static inline void
4527 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4528 unsigned HOST_WIDE_INT offset)
4530 dw_attr_node attr;
4532 attr.dw_attr = attr_kind;
4533 attr.dw_attr_val.val_class = dw_val_class_offset;
4534 attr.dw_attr_val.val_entry = NULL;
4535 attr.dw_attr_val.v.val_offset = offset;
4536 add_dwarf_attr (die, &attr);
4539 /* Add a range_list attribute value to a DIE. When using
4540 dwarf_split_debug_info, address attributes in dies destined for the
4541 final executable should be direct references--setting the parameter
4542 force_direct ensures this behavior. */
4544 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4545 #define RELOCATED_OFFSET (NULL)
4547 static void
4548 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4549 long unsigned int offset, bool force_direct)
4551 dw_attr_node attr;
4553 attr.dw_attr = attr_kind;
4554 attr.dw_attr_val.val_class = dw_val_class_range_list;
4555 /* For the range_list attribute, use val_entry to store whether the
4556 offset should follow split-debug-info or normal semantics. This
4557 value is read in output_range_list_offset. */
4558 if (dwarf_split_debug_info && !force_direct)
4559 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4560 else
4561 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4562 attr.dw_attr_val.v.val_offset = offset;
4563 add_dwarf_attr (die, &attr);
4566 /* Return the start label of a delta attribute. */
4568 static inline const char *
4569 AT_vms_delta1 (dw_attr_ref a)
4571 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4572 return a->dw_attr_val.v.val_vms_delta.lbl1;
4575 /* Return the end label of a delta attribute. */
4577 static inline const char *
4578 AT_vms_delta2 (dw_attr_ref a)
4580 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4581 return a->dw_attr_val.v.val_vms_delta.lbl2;
4584 static inline const char *
4585 AT_lbl (dw_attr_ref a)
4587 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4588 || AT_class (a) == dw_val_class_lineptr
4589 || AT_class (a) == dw_val_class_macptr
4590 || AT_class (a) == dw_val_class_high_pc));
4591 return a->dw_attr_val.v.val_lbl_id;
4594 /* Get the attribute of type attr_kind. */
4596 static dw_attr_ref
4597 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4599 dw_attr_ref a;
4600 unsigned ix;
4601 dw_die_ref spec = NULL;
4603 if (! die)
4604 return NULL;
4606 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4607 if (a->dw_attr == attr_kind)
4608 return a;
4609 else if (a->dw_attr == DW_AT_specification
4610 || a->dw_attr == DW_AT_abstract_origin)
4611 spec = AT_ref (a);
4613 if (spec)
4614 return get_AT (spec, attr_kind);
4616 return NULL;
4619 /* Returns the parent of the declaration of DIE. */
4621 static dw_die_ref
4622 get_die_parent (dw_die_ref die)
4624 dw_die_ref t;
4626 if (!die)
4627 return NULL;
4629 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4630 || (t = get_AT_ref (die, DW_AT_specification)))
4631 die = t;
4633 return die->die_parent;
4636 /* Return the "low pc" attribute value, typically associated with a subprogram
4637 DIE. Return null if the "low pc" attribute is either not present, or if it
4638 cannot be represented as an assembler label identifier. */
4640 static inline const char *
4641 get_AT_low_pc (dw_die_ref die)
4643 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4645 return a ? AT_lbl (a) : NULL;
4648 /* Return the "high pc" attribute value, typically associated with a subprogram
4649 DIE. Return null if the "high pc" attribute is either not present, or if it
4650 cannot be represented as an assembler label identifier. */
4652 static inline const char *
4653 get_AT_hi_pc (dw_die_ref die)
4655 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4657 return a ? AT_lbl (a) : NULL;
4660 /* Return the value of the string attribute designated by ATTR_KIND, or
4661 NULL if it is not present. */
4663 static inline const char *
4664 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4666 dw_attr_ref a = get_AT (die, attr_kind);
4668 return a ? AT_string (a) : NULL;
4671 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4672 if it is not present. */
4674 static inline int
4675 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4677 dw_attr_ref a = get_AT (die, attr_kind);
4679 return a ? AT_flag (a) : 0;
4682 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4683 if it is not present. */
4685 static inline unsigned
4686 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4688 dw_attr_ref a = get_AT (die, attr_kind);
4690 return a ? AT_unsigned (a) : 0;
4693 static inline dw_die_ref
4694 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4696 dw_attr_ref a = get_AT (die, attr_kind);
4698 return a ? AT_ref (a) : NULL;
4701 static inline struct dwarf_file_data *
4702 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4704 dw_attr_ref a = get_AT (die, attr_kind);
4706 return a ? AT_file (a) : NULL;
4709 /* Return TRUE if the language is C++. */
4711 static inline bool
4712 is_cxx (void)
4714 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4716 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4717 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4720 /* Return TRUE if the language is Java. */
4722 static inline bool
4723 is_java (void)
4725 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4727 return lang == DW_LANG_Java;
4730 /* Return TRUE if the language is Fortran. */
4732 static inline bool
4733 is_fortran (void)
4735 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4737 return (lang == DW_LANG_Fortran77
4738 || lang == DW_LANG_Fortran90
4739 || lang == DW_LANG_Fortran95
4740 || lang == DW_LANG_Fortran03
4741 || lang == DW_LANG_Fortran08);
4744 /* Return TRUE if the language is Ada. */
4746 static inline bool
4747 is_ada (void)
4749 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4751 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4754 /* Remove the specified attribute if present. */
4756 static void
4757 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4759 dw_attr_ref a;
4760 unsigned ix;
4762 if (! die)
4763 return;
4765 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4766 if (a->dw_attr == attr_kind)
4768 if (AT_class (a) == dw_val_class_str)
4769 if (a->dw_attr_val.v.val_str->refcount)
4770 a->dw_attr_val.v.val_str->refcount--;
4772 /* vec::ordered_remove should help reduce the number of abbrevs
4773 that are needed. */
4774 die->die_attr->ordered_remove (ix);
4775 return;
4779 /* Remove CHILD from its parent. PREV must have the property that
4780 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4782 static void
4783 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4785 gcc_assert (child->die_parent == prev->die_parent);
4786 gcc_assert (prev->die_sib == child);
4787 if (prev == child)
4789 gcc_assert (child->die_parent->die_child == child);
4790 prev = NULL;
4792 else
4793 prev->die_sib = child->die_sib;
4794 if (child->die_parent->die_child == child)
4795 child->die_parent->die_child = prev;
4798 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4799 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4801 static void
4802 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4804 dw_die_ref parent = old_child->die_parent;
4806 gcc_assert (parent == prev->die_parent);
4807 gcc_assert (prev->die_sib == old_child);
4809 new_child->die_parent = parent;
4810 if (prev == old_child)
4812 gcc_assert (parent->die_child == old_child);
4813 new_child->die_sib = new_child;
4815 else
4817 prev->die_sib = new_child;
4818 new_child->die_sib = old_child->die_sib;
4820 if (old_child->die_parent->die_child == old_child)
4821 old_child->die_parent->die_child = new_child;
4824 /* Move all children from OLD_PARENT to NEW_PARENT. */
4826 static void
4827 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4829 dw_die_ref c;
4830 new_parent->die_child = old_parent->die_child;
4831 old_parent->die_child = NULL;
4832 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4835 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4836 matches TAG. */
4838 static void
4839 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4841 dw_die_ref c;
4843 c = die->die_child;
4844 if (c) do {
4845 dw_die_ref prev = c;
4846 c = c->die_sib;
4847 while (c->die_tag == tag)
4849 remove_child_with_prev (c, prev);
4850 /* Might have removed every child. */
4851 if (c == c->die_sib)
4852 return;
4853 c = c->die_sib;
4855 } while (c != die->die_child);
4858 /* Add a CHILD_DIE as the last child of DIE. */
4860 static void
4861 add_child_die (dw_die_ref die, dw_die_ref child_die)
4863 /* FIXME this should probably be an assert. */
4864 if (! die || ! child_die)
4865 return;
4866 gcc_assert (die != child_die);
4868 child_die->die_parent = die;
4869 if (die->die_child)
4871 child_die->die_sib = die->die_child->die_sib;
4872 die->die_child->die_sib = child_die;
4874 else
4875 child_die->die_sib = child_die;
4876 die->die_child = child_die;
4879 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4880 is the specification, to the end of PARENT's list of children.
4881 This is done by removing and re-adding it. */
4883 static void
4884 splice_child_die (dw_die_ref parent, dw_die_ref child)
4886 dw_die_ref p;
4888 /* We want the declaration DIE from inside the class, not the
4889 specification DIE at toplevel. */
4890 if (child->die_parent != parent)
4892 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4894 if (tmp)
4895 child = tmp;
4898 gcc_assert (child->die_parent == parent
4899 || (child->die_parent
4900 == get_AT_ref (parent, DW_AT_specification)));
4902 for (p = child->die_parent->die_child; ; p = p->die_sib)
4903 if (p->die_sib == child)
4905 remove_child_with_prev (child, p);
4906 break;
4909 add_child_die (parent, child);
4912 /* Return a pointer to a newly created DIE node. */
4914 static inline dw_die_ref
4915 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4917 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4919 die->die_tag = tag_value;
4921 if (parent_die != NULL)
4922 add_child_die (parent_die, die);
4923 else
4925 limbo_die_node *limbo_node;
4927 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4928 limbo_node->die = die;
4929 limbo_node->created_for = t;
4930 limbo_node->next = limbo_die_list;
4931 limbo_die_list = limbo_node;
4934 return die;
4937 /* Return the DIE associated with the given type specifier. */
4939 static inline dw_die_ref
4940 lookup_type_die (tree type)
4942 return TYPE_SYMTAB_DIE (type);
4945 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4946 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4947 anonymous type instead the one of the naming typedef. */
4949 static inline dw_die_ref
4950 strip_naming_typedef (tree type, dw_die_ref type_die)
4952 if (type
4953 && TREE_CODE (type) == RECORD_TYPE
4954 && type_die
4955 && type_die->die_tag == DW_TAG_typedef
4956 && is_naming_typedef_decl (TYPE_NAME (type)))
4957 type_die = get_AT_ref (type_die, DW_AT_type);
4958 return type_die;
4961 /* Like lookup_type_die, but if type is an anonymous type named by a
4962 typedef[1], return the DIE of the anonymous type instead the one of
4963 the naming typedef. This is because in gen_typedef_die, we did
4964 equate the anonymous struct named by the typedef with the DIE of
4965 the naming typedef. So by default, lookup_type_die on an anonymous
4966 struct yields the DIE of the naming typedef.
4968 [1]: Read the comment of is_naming_typedef_decl to learn about what
4969 a naming typedef is. */
4971 static inline dw_die_ref
4972 lookup_type_die_strip_naming_typedef (tree type)
4974 dw_die_ref die = lookup_type_die (type);
4975 return strip_naming_typedef (type, die);
4978 /* Equate a DIE to a given type specifier. */
4980 static inline void
4981 equate_type_number_to_die (tree type, dw_die_ref type_die)
4983 TYPE_SYMTAB_DIE (type) = type_die;
4986 /* Returns a hash value for X (which really is a die_struct). */
4988 inline hashval_t
4989 decl_die_hasher::hash (die_node *x)
4991 return (hashval_t) x->decl_id;
4994 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4996 inline bool
4997 decl_die_hasher::equal (die_node *x, tree y)
4999 return (x->decl_id == DECL_UID (y));
5002 /* Return the DIE associated with a given declaration. */
5004 static inline dw_die_ref
5005 lookup_decl_die (tree decl)
5007 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5010 /* Returns a hash value for X (which really is a var_loc_list). */
5012 inline hashval_t
5013 decl_loc_hasher::hash (var_loc_list *x)
5015 return (hashval_t) x->decl_id;
5018 /* Return nonzero if decl_id of var_loc_list X is the same as
5019 UID of decl *Y. */
5021 inline bool
5022 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5024 return (x->decl_id == DECL_UID (y));
5027 /* Return the var_loc list associated with a given declaration. */
5029 static inline var_loc_list *
5030 lookup_decl_loc (const_tree decl)
5032 if (!decl_loc_table)
5033 return NULL;
5034 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5037 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5039 inline hashval_t
5040 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5042 return (hashval_t) x->decl_id;
5045 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5046 UID of decl *Y. */
5048 inline bool
5049 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5051 return (x->decl_id == DECL_UID (y));
5054 /* Equate a DIE to a particular declaration. */
5056 static void
5057 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5059 unsigned int decl_id = DECL_UID (decl);
5061 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5062 decl_die->decl_id = decl_id;
5065 /* Return how many bits covers PIECE EXPR_LIST. */
5067 static HOST_WIDE_INT
5068 decl_piece_bitsize (rtx piece)
5070 int ret = (int) GET_MODE (piece);
5071 if (ret)
5072 return ret;
5073 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5074 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5075 return INTVAL (XEXP (XEXP (piece, 0), 0));
5078 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5080 static rtx *
5081 decl_piece_varloc_ptr (rtx piece)
5083 if ((int) GET_MODE (piece))
5084 return &XEXP (piece, 0);
5085 else
5086 return &XEXP (XEXP (piece, 0), 1);
5089 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5090 Next is the chain of following piece nodes. */
5092 static rtx_expr_list *
5093 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5095 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5096 return alloc_EXPR_LIST (bitsize, loc_note, next);
5097 else
5098 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5099 GEN_INT (bitsize),
5100 loc_note), next);
5103 /* Return rtx that should be stored into loc field for
5104 LOC_NOTE and BITPOS/BITSIZE. */
5106 static rtx
5107 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5108 HOST_WIDE_INT bitsize)
5110 if (bitsize != -1)
5112 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5113 if (bitpos != 0)
5114 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5116 return loc_note;
5119 /* This function either modifies location piece list *DEST in
5120 place (if SRC and INNER is NULL), or copies location piece list
5121 *SRC to *DEST while modifying it. Location BITPOS is modified
5122 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5123 not copied and if needed some padding around it is added.
5124 When modifying in place, DEST should point to EXPR_LIST where
5125 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5126 to the start of the whole list and INNER points to the EXPR_LIST
5127 where earlier pieces cover PIECE_BITPOS bits. */
5129 static void
5130 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5131 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5132 HOST_WIDE_INT bitsize, rtx loc_note)
5134 HOST_WIDE_INT diff;
5135 bool copy = inner != NULL;
5137 if (copy)
5139 /* First copy all nodes preceding the current bitpos. */
5140 while (src != inner)
5142 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5143 decl_piece_bitsize (*src), NULL_RTX);
5144 dest = &XEXP (*dest, 1);
5145 src = &XEXP (*src, 1);
5148 /* Add padding if needed. */
5149 if (bitpos != piece_bitpos)
5151 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5152 copy ? NULL_RTX : *dest);
5153 dest = &XEXP (*dest, 1);
5155 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5157 gcc_assert (!copy);
5158 /* A piece with correct bitpos and bitsize already exist,
5159 just update the location for it and return. */
5160 *decl_piece_varloc_ptr (*dest) = loc_note;
5161 return;
5163 /* Add the piece that changed. */
5164 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5165 dest = &XEXP (*dest, 1);
5166 /* Skip over pieces that overlap it. */
5167 diff = bitpos - piece_bitpos + bitsize;
5168 if (!copy)
5169 src = dest;
5170 while (diff > 0 && *src)
5172 rtx piece = *src;
5173 diff -= decl_piece_bitsize (piece);
5174 if (copy)
5175 src = &XEXP (piece, 1);
5176 else
5178 *src = XEXP (piece, 1);
5179 free_EXPR_LIST_node (piece);
5182 /* Add padding if needed. */
5183 if (diff < 0 && *src)
5185 if (!copy)
5186 dest = src;
5187 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5188 dest = &XEXP (*dest, 1);
5190 if (!copy)
5191 return;
5192 /* Finally copy all nodes following it. */
5193 while (*src)
5195 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5196 decl_piece_bitsize (*src), NULL_RTX);
5197 dest = &XEXP (*dest, 1);
5198 src = &XEXP (*src, 1);
5202 /* Add a variable location node to the linked list for DECL. */
5204 static struct var_loc_node *
5205 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5207 unsigned int decl_id;
5208 var_loc_list *temp;
5209 struct var_loc_node *loc = NULL;
5210 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5212 if (TREE_CODE (decl) == VAR_DECL
5213 && DECL_HAS_DEBUG_EXPR_P (decl))
5215 tree realdecl = DECL_DEBUG_EXPR (decl);
5216 if (handled_component_p (realdecl)
5217 || (TREE_CODE (realdecl) == MEM_REF
5218 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5220 HOST_WIDE_INT maxsize;
5221 tree innerdecl;
5222 innerdecl
5223 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5224 if (!DECL_P (innerdecl)
5225 || DECL_IGNORED_P (innerdecl)
5226 || TREE_STATIC (innerdecl)
5227 || bitsize <= 0
5228 || bitpos + bitsize > 256
5229 || bitsize != maxsize)
5230 return NULL;
5231 decl = innerdecl;
5235 decl_id = DECL_UID (decl);
5236 var_loc_list **slot
5237 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5238 if (*slot == NULL)
5240 temp = ggc_cleared_alloc<var_loc_list> ();
5241 temp->decl_id = decl_id;
5242 *slot = temp;
5244 else
5245 temp = *slot;
5247 /* For PARM_DECLs try to keep around the original incoming value,
5248 even if that means we'll emit a zero-range .debug_loc entry. */
5249 if (temp->last
5250 && temp->first == temp->last
5251 && TREE_CODE (decl) == PARM_DECL
5252 && NOTE_P (temp->first->loc)
5253 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5254 && DECL_INCOMING_RTL (decl)
5255 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5256 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5257 == GET_CODE (DECL_INCOMING_RTL (decl))
5258 && prev_real_insn (temp->first->loc) == NULL_RTX
5259 && (bitsize != -1
5260 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5261 NOTE_VAR_LOCATION_LOC (loc_note))
5262 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5263 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5265 loc = ggc_cleared_alloc<var_loc_node> ();
5266 temp->first->next = loc;
5267 temp->last = loc;
5268 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5270 else if (temp->last)
5272 struct var_loc_node *last = temp->last, *unused = NULL;
5273 rtx *piece_loc = NULL, last_loc_note;
5274 HOST_WIDE_INT piece_bitpos = 0;
5275 if (last->next)
5277 last = last->next;
5278 gcc_assert (last->next == NULL);
5280 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5282 piece_loc = &last->loc;
5285 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5286 if (piece_bitpos + cur_bitsize > bitpos)
5287 break;
5288 piece_bitpos += cur_bitsize;
5289 piece_loc = &XEXP (*piece_loc, 1);
5291 while (*piece_loc);
5293 /* TEMP->LAST here is either pointer to the last but one or
5294 last element in the chained list, LAST is pointer to the
5295 last element. */
5296 if (label && strcmp (last->label, label) == 0)
5298 /* For SRA optimized variables if there weren't any real
5299 insns since last note, just modify the last node. */
5300 if (piece_loc != NULL)
5302 adjust_piece_list (piece_loc, NULL, NULL,
5303 bitpos, piece_bitpos, bitsize, loc_note);
5304 return NULL;
5306 /* If the last note doesn't cover any instructions, remove it. */
5307 if (temp->last != last)
5309 temp->last->next = NULL;
5310 unused = last;
5311 last = temp->last;
5312 gcc_assert (strcmp (last->label, label) != 0);
5314 else
5316 gcc_assert (temp->first == temp->last
5317 || (temp->first->next == temp->last
5318 && TREE_CODE (decl) == PARM_DECL));
5319 memset (temp->last, '\0', sizeof (*temp->last));
5320 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5321 return temp->last;
5324 if (bitsize == -1 && NOTE_P (last->loc))
5325 last_loc_note = last->loc;
5326 else if (piece_loc != NULL
5327 && *piece_loc != NULL_RTX
5328 && piece_bitpos == bitpos
5329 && decl_piece_bitsize (*piece_loc) == bitsize)
5330 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5331 else
5332 last_loc_note = NULL_RTX;
5333 /* If the current location is the same as the end of the list,
5334 and either both or neither of the locations is uninitialized,
5335 we have nothing to do. */
5336 if (last_loc_note == NULL_RTX
5337 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5338 NOTE_VAR_LOCATION_LOC (loc_note)))
5339 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5340 != NOTE_VAR_LOCATION_STATUS (loc_note))
5341 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5342 == VAR_INIT_STATUS_UNINITIALIZED)
5343 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5344 == VAR_INIT_STATUS_UNINITIALIZED))))
5346 /* Add LOC to the end of list and update LAST. If the last
5347 element of the list has been removed above, reuse its
5348 memory for the new node, otherwise allocate a new one. */
5349 if (unused)
5351 loc = unused;
5352 memset (loc, '\0', sizeof (*loc));
5354 else
5355 loc = ggc_cleared_alloc<var_loc_node> ();
5356 if (bitsize == -1 || piece_loc == NULL)
5357 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5358 else
5359 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5360 bitpos, piece_bitpos, bitsize, loc_note);
5361 last->next = loc;
5362 /* Ensure TEMP->LAST will point either to the new last but one
5363 element of the chain, or to the last element in it. */
5364 if (last != temp->last)
5365 temp->last = last;
5367 else if (unused)
5368 ggc_free (unused);
5370 else
5372 loc = ggc_cleared_alloc<var_loc_node> ();
5373 temp->first = loc;
5374 temp->last = loc;
5375 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5377 return loc;
5380 /* Keep track of the number of spaces used to indent the
5381 output of the debugging routines that print the structure of
5382 the DIE internal representation. */
5383 static int print_indent;
5385 /* Indent the line the number of spaces given by print_indent. */
5387 static inline void
5388 print_spaces (FILE *outfile)
5390 fprintf (outfile, "%*s", print_indent, "");
5393 /* Print a type signature in hex. */
5395 static inline void
5396 print_signature (FILE *outfile, char *sig)
5398 int i;
5400 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5401 fprintf (outfile, "%02x", sig[i] & 0xff);
5404 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5406 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5407 RECURSE, output location descriptor operations. */
5409 static void
5410 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5412 switch (val->val_class)
5414 case dw_val_class_addr:
5415 fprintf (outfile, "address");
5416 break;
5417 case dw_val_class_offset:
5418 fprintf (outfile, "offset");
5419 break;
5420 case dw_val_class_loc:
5421 fprintf (outfile, "location descriptor");
5422 if (val->v.val_loc == NULL)
5423 fprintf (outfile, " -> <null>\n");
5424 else if (recurse)
5426 fprintf (outfile, ":\n");
5427 print_indent += 4;
5428 print_loc_descr (val->v.val_loc, outfile);
5429 print_indent -= 4;
5431 else
5432 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5433 break;
5434 case dw_val_class_loc_list:
5435 fprintf (outfile, "location list -> label:%s",
5436 val->v.val_loc_list->ll_symbol);
5437 break;
5438 case dw_val_class_range_list:
5439 fprintf (outfile, "range list");
5440 break;
5441 case dw_val_class_const:
5442 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5443 break;
5444 case dw_val_class_unsigned_const:
5445 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5446 break;
5447 case dw_val_class_const_double:
5448 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5449 HOST_WIDE_INT_PRINT_UNSIGNED")",
5450 val->v.val_double.high,
5451 val->v.val_double.low);
5452 break;
5453 case dw_val_class_wide_int:
5455 int i = val->v.val_wide->get_len ();
5456 fprintf (outfile, "constant (");
5457 gcc_assert (i > 0);
5458 if (val->v.val_wide->elt (i - 1) == 0)
5459 fprintf (outfile, "0x");
5460 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5461 val->v.val_wide->elt (--i));
5462 while (--i >= 0)
5463 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5464 val->v.val_wide->elt (i));
5465 fprintf (outfile, ")");
5466 break;
5468 case dw_val_class_vec:
5469 fprintf (outfile, "floating-point or vector constant");
5470 break;
5471 case dw_val_class_flag:
5472 fprintf (outfile, "%u", val->v.val_flag);
5473 break;
5474 case dw_val_class_die_ref:
5475 if (val->v.val_die_ref.die != NULL)
5477 dw_die_ref die = val->v.val_die_ref.die;
5479 if (die->comdat_type_p)
5481 fprintf (outfile, "die -> signature: ");
5482 print_signature (outfile,
5483 die->die_id.die_type_node->signature);
5485 else if (die->die_id.die_symbol)
5486 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5487 else
5488 fprintf (outfile, "die -> %ld", die->die_offset);
5489 fprintf (outfile, " (%p)", (void *) die);
5491 else
5492 fprintf (outfile, "die -> <null>");
5493 break;
5494 case dw_val_class_vms_delta:
5495 fprintf (outfile, "delta: @slotcount(%s-%s)",
5496 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5497 break;
5498 case dw_val_class_lbl_id:
5499 case dw_val_class_lineptr:
5500 case dw_val_class_macptr:
5501 case dw_val_class_high_pc:
5502 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5503 break;
5504 case dw_val_class_str:
5505 if (val->v.val_str->str != NULL)
5506 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5507 else
5508 fprintf (outfile, "<null>");
5509 break;
5510 case dw_val_class_file:
5511 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5512 val->v.val_file->emitted_number);
5513 break;
5514 case dw_val_class_data8:
5516 int i;
5518 for (i = 0; i < 8; i++)
5519 fprintf (outfile, "%02x", val->v.val_data8[i]);
5520 break;
5522 default:
5523 break;
5527 /* Likewise, for a DIE attribute. */
5529 static void
5530 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5532 print_dw_val (&a->dw_attr_val, recurse, outfile);
5536 /* Print the list of operands in the LOC location description to OUTFILE. This
5537 routine is a debugging aid only. */
5539 static void
5540 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5542 dw_loc_descr_ref l = loc;
5544 if (loc == NULL)
5546 print_spaces (outfile);
5547 fprintf (outfile, "<null>\n");
5548 return;
5551 for (l = loc; l != NULL; l = l->dw_loc_next)
5553 print_spaces (outfile);
5554 fprintf (outfile, "(%p) %s",
5555 (void *) l,
5556 dwarf_stack_op_name (l->dw_loc_opc));
5557 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5559 fprintf (outfile, " ");
5560 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5562 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5564 fprintf (outfile, ", ");
5565 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5567 fprintf (outfile, "\n");
5571 /* Print the information associated with a given DIE, and its children.
5572 This routine is a debugging aid only. */
5574 static void
5575 print_die (dw_die_ref die, FILE *outfile)
5577 dw_attr_ref a;
5578 dw_die_ref c;
5579 unsigned ix;
5581 print_spaces (outfile);
5582 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5583 die->die_offset, dwarf_tag_name (die->die_tag),
5584 (void*) die);
5585 print_spaces (outfile);
5586 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5587 fprintf (outfile, " offset: %ld", die->die_offset);
5588 fprintf (outfile, " mark: %d\n", die->die_mark);
5590 if (die->comdat_type_p)
5592 print_spaces (outfile);
5593 fprintf (outfile, " signature: ");
5594 print_signature (outfile, die->die_id.die_type_node->signature);
5595 fprintf (outfile, "\n");
5598 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5600 print_spaces (outfile);
5601 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5603 print_attribute (a, true, outfile);
5604 fprintf (outfile, "\n");
5607 if (die->die_child != NULL)
5609 print_indent += 4;
5610 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5611 print_indent -= 4;
5613 if (print_indent == 0)
5614 fprintf (outfile, "\n");
5617 /* Print the list of operations in the LOC location description. */
5619 DEBUG_FUNCTION void
5620 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5622 print_loc_descr (loc, stderr);
5625 /* Print the information collected for a given DIE. */
5627 DEBUG_FUNCTION void
5628 debug_dwarf_die (dw_die_ref die)
5630 print_die (die, stderr);
5633 DEBUG_FUNCTION void
5634 debug (die_struct &ref)
5636 print_die (&ref, stderr);
5639 DEBUG_FUNCTION void
5640 debug (die_struct *ptr)
5642 if (ptr)
5643 debug (*ptr);
5644 else
5645 fprintf (stderr, "<nil>\n");
5649 /* Print all DWARF information collected for the compilation unit.
5650 This routine is a debugging aid only. */
5652 DEBUG_FUNCTION void
5653 debug_dwarf (void)
5655 print_indent = 0;
5656 print_die (comp_unit_die (), stderr);
5659 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5660 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5661 DIE that marks the start of the DIEs for this include file. */
5663 static dw_die_ref
5664 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5666 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5667 dw_die_ref new_unit = gen_compile_unit_die (filename);
5669 new_unit->die_sib = old_unit;
5670 return new_unit;
5673 /* Close an include-file CU and reopen the enclosing one. */
5675 static dw_die_ref
5676 pop_compile_unit (dw_die_ref old_unit)
5678 dw_die_ref new_unit = old_unit->die_sib;
5680 old_unit->die_sib = NULL;
5681 return new_unit;
5684 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5685 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5686 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5688 /* Calculate the checksum of a location expression. */
5690 static inline void
5691 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5693 int tem;
5694 inchash::hash hstate;
5695 hashval_t hash;
5697 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5698 CHECKSUM (tem);
5699 hash_loc_operands (loc, hstate);
5700 hash = hstate.end();
5701 CHECKSUM (hash);
5704 /* Calculate the checksum of an attribute. */
5706 static void
5707 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5709 dw_loc_descr_ref loc;
5710 rtx r;
5712 CHECKSUM (at->dw_attr);
5714 /* We don't care that this was compiled with a different compiler
5715 snapshot; if the output is the same, that's what matters. */
5716 if (at->dw_attr == DW_AT_producer)
5717 return;
5719 switch (AT_class (at))
5721 case dw_val_class_const:
5722 CHECKSUM (at->dw_attr_val.v.val_int);
5723 break;
5724 case dw_val_class_unsigned_const:
5725 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5726 break;
5727 case dw_val_class_const_double:
5728 CHECKSUM (at->dw_attr_val.v.val_double);
5729 break;
5730 case dw_val_class_wide_int:
5731 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5732 get_full_len (*at->dw_attr_val.v.val_wide)
5733 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5734 break;
5735 case dw_val_class_vec:
5736 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5737 (at->dw_attr_val.v.val_vec.length
5738 * at->dw_attr_val.v.val_vec.elt_size));
5739 break;
5740 case dw_val_class_flag:
5741 CHECKSUM (at->dw_attr_val.v.val_flag);
5742 break;
5743 case dw_val_class_str:
5744 CHECKSUM_STRING (AT_string (at));
5745 break;
5747 case dw_val_class_addr:
5748 r = AT_addr (at);
5749 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5750 CHECKSUM_STRING (XSTR (r, 0));
5751 break;
5753 case dw_val_class_offset:
5754 CHECKSUM (at->dw_attr_val.v.val_offset);
5755 break;
5757 case dw_val_class_loc:
5758 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5759 loc_checksum (loc, ctx);
5760 break;
5762 case dw_val_class_die_ref:
5763 die_checksum (AT_ref (at), ctx, mark);
5764 break;
5766 case dw_val_class_fde_ref:
5767 case dw_val_class_vms_delta:
5768 case dw_val_class_lbl_id:
5769 case dw_val_class_lineptr:
5770 case dw_val_class_macptr:
5771 case dw_val_class_high_pc:
5772 break;
5774 case dw_val_class_file:
5775 CHECKSUM_STRING (AT_file (at)->filename);
5776 break;
5778 case dw_val_class_data8:
5779 CHECKSUM (at->dw_attr_val.v.val_data8);
5780 break;
5782 default:
5783 break;
5787 /* Calculate the checksum of a DIE. */
5789 static void
5790 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5792 dw_die_ref c;
5793 dw_attr_ref a;
5794 unsigned ix;
5796 /* To avoid infinite recursion. */
5797 if (die->die_mark)
5799 CHECKSUM (die->die_mark);
5800 return;
5802 die->die_mark = ++(*mark);
5804 CHECKSUM (die->die_tag);
5806 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5807 attr_checksum (a, ctx, mark);
5809 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5812 #undef CHECKSUM
5813 #undef CHECKSUM_BLOCK
5814 #undef CHECKSUM_STRING
5816 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5817 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5818 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5819 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5820 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5821 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5822 #define CHECKSUM_ATTR(FOO) \
5823 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5825 /* Calculate the checksum of a number in signed LEB128 format. */
5827 static void
5828 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5830 unsigned char byte;
5831 bool more;
5833 while (1)
5835 byte = (value & 0x7f);
5836 value >>= 7;
5837 more = !((value == 0 && (byte & 0x40) == 0)
5838 || (value == -1 && (byte & 0x40) != 0));
5839 if (more)
5840 byte |= 0x80;
5841 CHECKSUM (byte);
5842 if (!more)
5843 break;
5847 /* Calculate the checksum of a number in unsigned LEB128 format. */
5849 static void
5850 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5852 while (1)
5854 unsigned char byte = (value & 0x7f);
5855 value >>= 7;
5856 if (value != 0)
5857 /* More bytes to follow. */
5858 byte |= 0x80;
5859 CHECKSUM (byte);
5860 if (value == 0)
5861 break;
5865 /* Checksum the context of the DIE. This adds the names of any
5866 surrounding namespaces or structures to the checksum. */
5868 static void
5869 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5871 const char *name;
5872 dw_die_ref spec;
5873 int tag = die->die_tag;
5875 if (tag != DW_TAG_namespace
5876 && tag != DW_TAG_structure_type
5877 && tag != DW_TAG_class_type)
5878 return;
5880 name = get_AT_string (die, DW_AT_name);
5882 spec = get_AT_ref (die, DW_AT_specification);
5883 if (spec != NULL)
5884 die = spec;
5886 if (die->die_parent != NULL)
5887 checksum_die_context (die->die_parent, ctx);
5889 CHECKSUM_ULEB128 ('C');
5890 CHECKSUM_ULEB128 (tag);
5891 if (name != NULL)
5892 CHECKSUM_STRING (name);
5895 /* Calculate the checksum of a location expression. */
5897 static inline void
5898 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5900 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5901 were emitted as a DW_FORM_sdata instead of a location expression. */
5902 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5904 CHECKSUM_ULEB128 (DW_FORM_sdata);
5905 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5906 return;
5909 /* Otherwise, just checksum the raw location expression. */
5910 while (loc != NULL)
5912 inchash::hash hstate;
5913 hashval_t hash;
5915 CHECKSUM_ULEB128 (loc->dtprel);
5916 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5917 hash_loc_operands (loc, hstate);
5918 hash = hstate.end ();
5919 CHECKSUM (hash);
5920 loc = loc->dw_loc_next;
5924 /* Calculate the checksum of an attribute. */
5926 static void
5927 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5928 struct md5_ctx *ctx, int *mark)
5930 dw_loc_descr_ref loc;
5931 rtx r;
5933 if (AT_class (at) == dw_val_class_die_ref)
5935 dw_die_ref target_die = AT_ref (at);
5937 /* For pointer and reference types, we checksum only the (qualified)
5938 name of the target type (if there is a name). For friend entries,
5939 we checksum only the (qualified) name of the target type or function.
5940 This allows the checksum to remain the same whether the target type
5941 is complete or not. */
5942 if ((at->dw_attr == DW_AT_type
5943 && (tag == DW_TAG_pointer_type
5944 || tag == DW_TAG_reference_type
5945 || tag == DW_TAG_rvalue_reference_type
5946 || tag == DW_TAG_ptr_to_member_type))
5947 || (at->dw_attr == DW_AT_friend
5948 && tag == DW_TAG_friend))
5950 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5952 if (name_attr != NULL)
5954 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5956 if (decl == NULL)
5957 decl = target_die;
5958 CHECKSUM_ULEB128 ('N');
5959 CHECKSUM_ULEB128 (at->dw_attr);
5960 if (decl->die_parent != NULL)
5961 checksum_die_context (decl->die_parent, ctx);
5962 CHECKSUM_ULEB128 ('E');
5963 CHECKSUM_STRING (AT_string (name_attr));
5964 return;
5968 /* For all other references to another DIE, we check to see if the
5969 target DIE has already been visited. If it has, we emit a
5970 backward reference; if not, we descend recursively. */
5971 if (target_die->die_mark > 0)
5973 CHECKSUM_ULEB128 ('R');
5974 CHECKSUM_ULEB128 (at->dw_attr);
5975 CHECKSUM_ULEB128 (target_die->die_mark);
5977 else
5979 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5981 if (decl == NULL)
5982 decl = target_die;
5983 target_die->die_mark = ++(*mark);
5984 CHECKSUM_ULEB128 ('T');
5985 CHECKSUM_ULEB128 (at->dw_attr);
5986 if (decl->die_parent != NULL)
5987 checksum_die_context (decl->die_parent, ctx);
5988 die_checksum_ordered (target_die, ctx, mark);
5990 return;
5993 CHECKSUM_ULEB128 ('A');
5994 CHECKSUM_ULEB128 (at->dw_attr);
5996 switch (AT_class (at))
5998 case dw_val_class_const:
5999 CHECKSUM_ULEB128 (DW_FORM_sdata);
6000 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6001 break;
6003 case dw_val_class_unsigned_const:
6004 CHECKSUM_ULEB128 (DW_FORM_sdata);
6005 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6006 break;
6008 case dw_val_class_const_double:
6009 CHECKSUM_ULEB128 (DW_FORM_block);
6010 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6011 CHECKSUM (at->dw_attr_val.v.val_double);
6012 break;
6014 case dw_val_class_wide_int:
6015 CHECKSUM_ULEB128 (DW_FORM_block);
6016 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6017 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6018 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6019 get_full_len (*at->dw_attr_val.v.val_wide)
6020 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6021 break;
6023 case dw_val_class_vec:
6024 CHECKSUM_ULEB128 (DW_FORM_block);
6025 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6026 * at->dw_attr_val.v.val_vec.elt_size);
6027 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6028 (at->dw_attr_val.v.val_vec.length
6029 * at->dw_attr_val.v.val_vec.elt_size));
6030 break;
6032 case dw_val_class_flag:
6033 CHECKSUM_ULEB128 (DW_FORM_flag);
6034 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6035 break;
6037 case dw_val_class_str:
6038 CHECKSUM_ULEB128 (DW_FORM_string);
6039 CHECKSUM_STRING (AT_string (at));
6040 break;
6042 case dw_val_class_addr:
6043 r = AT_addr (at);
6044 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6045 CHECKSUM_ULEB128 (DW_FORM_string);
6046 CHECKSUM_STRING (XSTR (r, 0));
6047 break;
6049 case dw_val_class_offset:
6050 CHECKSUM_ULEB128 (DW_FORM_sdata);
6051 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6052 break;
6054 case dw_val_class_loc:
6055 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6056 loc_checksum_ordered (loc, ctx);
6057 break;
6059 case dw_val_class_fde_ref:
6060 case dw_val_class_lbl_id:
6061 case dw_val_class_lineptr:
6062 case dw_val_class_macptr:
6063 case dw_val_class_high_pc:
6064 break;
6066 case dw_val_class_file:
6067 CHECKSUM_ULEB128 (DW_FORM_string);
6068 CHECKSUM_STRING (AT_file (at)->filename);
6069 break;
6071 case dw_val_class_data8:
6072 CHECKSUM (at->dw_attr_val.v.val_data8);
6073 break;
6075 default:
6076 break;
6080 struct checksum_attributes
6082 dw_attr_ref at_name;
6083 dw_attr_ref at_type;
6084 dw_attr_ref at_friend;
6085 dw_attr_ref at_accessibility;
6086 dw_attr_ref at_address_class;
6087 dw_attr_ref at_allocated;
6088 dw_attr_ref at_artificial;
6089 dw_attr_ref at_associated;
6090 dw_attr_ref at_binary_scale;
6091 dw_attr_ref at_bit_offset;
6092 dw_attr_ref at_bit_size;
6093 dw_attr_ref at_bit_stride;
6094 dw_attr_ref at_byte_size;
6095 dw_attr_ref at_byte_stride;
6096 dw_attr_ref at_const_value;
6097 dw_attr_ref at_containing_type;
6098 dw_attr_ref at_count;
6099 dw_attr_ref at_data_location;
6100 dw_attr_ref at_data_member_location;
6101 dw_attr_ref at_decimal_scale;
6102 dw_attr_ref at_decimal_sign;
6103 dw_attr_ref at_default_value;
6104 dw_attr_ref at_digit_count;
6105 dw_attr_ref at_discr;
6106 dw_attr_ref at_discr_list;
6107 dw_attr_ref at_discr_value;
6108 dw_attr_ref at_encoding;
6109 dw_attr_ref at_endianity;
6110 dw_attr_ref at_explicit;
6111 dw_attr_ref at_is_optional;
6112 dw_attr_ref at_location;
6113 dw_attr_ref at_lower_bound;
6114 dw_attr_ref at_mutable;
6115 dw_attr_ref at_ordering;
6116 dw_attr_ref at_picture_string;
6117 dw_attr_ref at_prototyped;
6118 dw_attr_ref at_small;
6119 dw_attr_ref at_segment;
6120 dw_attr_ref at_string_length;
6121 dw_attr_ref at_threads_scaled;
6122 dw_attr_ref at_upper_bound;
6123 dw_attr_ref at_use_location;
6124 dw_attr_ref at_use_UTF8;
6125 dw_attr_ref at_variable_parameter;
6126 dw_attr_ref at_virtuality;
6127 dw_attr_ref at_visibility;
6128 dw_attr_ref at_vtable_elem_location;
6131 /* Collect the attributes that we will want to use for the checksum. */
6133 static void
6134 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6136 dw_attr_ref a;
6137 unsigned ix;
6139 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6141 switch (a->dw_attr)
6143 case DW_AT_name:
6144 attrs->at_name = a;
6145 break;
6146 case DW_AT_type:
6147 attrs->at_type = a;
6148 break;
6149 case DW_AT_friend:
6150 attrs->at_friend = a;
6151 break;
6152 case DW_AT_accessibility:
6153 attrs->at_accessibility = a;
6154 break;
6155 case DW_AT_address_class:
6156 attrs->at_address_class = a;
6157 break;
6158 case DW_AT_allocated:
6159 attrs->at_allocated = a;
6160 break;
6161 case DW_AT_artificial:
6162 attrs->at_artificial = a;
6163 break;
6164 case DW_AT_associated:
6165 attrs->at_associated = a;
6166 break;
6167 case DW_AT_binary_scale:
6168 attrs->at_binary_scale = a;
6169 break;
6170 case DW_AT_bit_offset:
6171 attrs->at_bit_offset = a;
6172 break;
6173 case DW_AT_bit_size:
6174 attrs->at_bit_size = a;
6175 break;
6176 case DW_AT_bit_stride:
6177 attrs->at_bit_stride = a;
6178 break;
6179 case DW_AT_byte_size:
6180 attrs->at_byte_size = a;
6181 break;
6182 case DW_AT_byte_stride:
6183 attrs->at_byte_stride = a;
6184 break;
6185 case DW_AT_const_value:
6186 attrs->at_const_value = a;
6187 break;
6188 case DW_AT_containing_type:
6189 attrs->at_containing_type = a;
6190 break;
6191 case DW_AT_count:
6192 attrs->at_count = a;
6193 break;
6194 case DW_AT_data_location:
6195 attrs->at_data_location = a;
6196 break;
6197 case DW_AT_data_member_location:
6198 attrs->at_data_member_location = a;
6199 break;
6200 case DW_AT_decimal_scale:
6201 attrs->at_decimal_scale = a;
6202 break;
6203 case DW_AT_decimal_sign:
6204 attrs->at_decimal_sign = a;
6205 break;
6206 case DW_AT_default_value:
6207 attrs->at_default_value = a;
6208 break;
6209 case DW_AT_digit_count:
6210 attrs->at_digit_count = a;
6211 break;
6212 case DW_AT_discr:
6213 attrs->at_discr = a;
6214 break;
6215 case DW_AT_discr_list:
6216 attrs->at_discr_list = a;
6217 break;
6218 case DW_AT_discr_value:
6219 attrs->at_discr_value = a;
6220 break;
6221 case DW_AT_encoding:
6222 attrs->at_encoding = a;
6223 break;
6224 case DW_AT_endianity:
6225 attrs->at_endianity = a;
6226 break;
6227 case DW_AT_explicit:
6228 attrs->at_explicit = a;
6229 break;
6230 case DW_AT_is_optional:
6231 attrs->at_is_optional = a;
6232 break;
6233 case DW_AT_location:
6234 attrs->at_location = a;
6235 break;
6236 case DW_AT_lower_bound:
6237 attrs->at_lower_bound = a;
6238 break;
6239 case DW_AT_mutable:
6240 attrs->at_mutable = a;
6241 break;
6242 case DW_AT_ordering:
6243 attrs->at_ordering = a;
6244 break;
6245 case DW_AT_picture_string:
6246 attrs->at_picture_string = a;
6247 break;
6248 case DW_AT_prototyped:
6249 attrs->at_prototyped = a;
6250 break;
6251 case DW_AT_small:
6252 attrs->at_small = a;
6253 break;
6254 case DW_AT_segment:
6255 attrs->at_segment = a;
6256 break;
6257 case DW_AT_string_length:
6258 attrs->at_string_length = a;
6259 break;
6260 case DW_AT_threads_scaled:
6261 attrs->at_threads_scaled = a;
6262 break;
6263 case DW_AT_upper_bound:
6264 attrs->at_upper_bound = a;
6265 break;
6266 case DW_AT_use_location:
6267 attrs->at_use_location = a;
6268 break;
6269 case DW_AT_use_UTF8:
6270 attrs->at_use_UTF8 = a;
6271 break;
6272 case DW_AT_variable_parameter:
6273 attrs->at_variable_parameter = a;
6274 break;
6275 case DW_AT_virtuality:
6276 attrs->at_virtuality = a;
6277 break;
6278 case DW_AT_visibility:
6279 attrs->at_visibility = a;
6280 break;
6281 case DW_AT_vtable_elem_location:
6282 attrs->at_vtable_elem_location = a;
6283 break;
6284 default:
6285 break;
6290 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6292 static void
6293 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6295 dw_die_ref c;
6296 dw_die_ref decl;
6297 struct checksum_attributes attrs;
6299 CHECKSUM_ULEB128 ('D');
6300 CHECKSUM_ULEB128 (die->die_tag);
6302 memset (&attrs, 0, sizeof (attrs));
6304 decl = get_AT_ref (die, DW_AT_specification);
6305 if (decl != NULL)
6306 collect_checksum_attributes (&attrs, decl);
6307 collect_checksum_attributes (&attrs, die);
6309 CHECKSUM_ATTR (attrs.at_name);
6310 CHECKSUM_ATTR (attrs.at_accessibility);
6311 CHECKSUM_ATTR (attrs.at_address_class);
6312 CHECKSUM_ATTR (attrs.at_allocated);
6313 CHECKSUM_ATTR (attrs.at_artificial);
6314 CHECKSUM_ATTR (attrs.at_associated);
6315 CHECKSUM_ATTR (attrs.at_binary_scale);
6316 CHECKSUM_ATTR (attrs.at_bit_offset);
6317 CHECKSUM_ATTR (attrs.at_bit_size);
6318 CHECKSUM_ATTR (attrs.at_bit_stride);
6319 CHECKSUM_ATTR (attrs.at_byte_size);
6320 CHECKSUM_ATTR (attrs.at_byte_stride);
6321 CHECKSUM_ATTR (attrs.at_const_value);
6322 CHECKSUM_ATTR (attrs.at_containing_type);
6323 CHECKSUM_ATTR (attrs.at_count);
6324 CHECKSUM_ATTR (attrs.at_data_location);
6325 CHECKSUM_ATTR (attrs.at_data_member_location);
6326 CHECKSUM_ATTR (attrs.at_decimal_scale);
6327 CHECKSUM_ATTR (attrs.at_decimal_sign);
6328 CHECKSUM_ATTR (attrs.at_default_value);
6329 CHECKSUM_ATTR (attrs.at_digit_count);
6330 CHECKSUM_ATTR (attrs.at_discr);
6331 CHECKSUM_ATTR (attrs.at_discr_list);
6332 CHECKSUM_ATTR (attrs.at_discr_value);
6333 CHECKSUM_ATTR (attrs.at_encoding);
6334 CHECKSUM_ATTR (attrs.at_endianity);
6335 CHECKSUM_ATTR (attrs.at_explicit);
6336 CHECKSUM_ATTR (attrs.at_is_optional);
6337 CHECKSUM_ATTR (attrs.at_location);
6338 CHECKSUM_ATTR (attrs.at_lower_bound);
6339 CHECKSUM_ATTR (attrs.at_mutable);
6340 CHECKSUM_ATTR (attrs.at_ordering);
6341 CHECKSUM_ATTR (attrs.at_picture_string);
6342 CHECKSUM_ATTR (attrs.at_prototyped);
6343 CHECKSUM_ATTR (attrs.at_small);
6344 CHECKSUM_ATTR (attrs.at_segment);
6345 CHECKSUM_ATTR (attrs.at_string_length);
6346 CHECKSUM_ATTR (attrs.at_threads_scaled);
6347 CHECKSUM_ATTR (attrs.at_upper_bound);
6348 CHECKSUM_ATTR (attrs.at_use_location);
6349 CHECKSUM_ATTR (attrs.at_use_UTF8);
6350 CHECKSUM_ATTR (attrs.at_variable_parameter);
6351 CHECKSUM_ATTR (attrs.at_virtuality);
6352 CHECKSUM_ATTR (attrs.at_visibility);
6353 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6354 CHECKSUM_ATTR (attrs.at_type);
6355 CHECKSUM_ATTR (attrs.at_friend);
6357 /* Checksum the child DIEs. */
6358 c = die->die_child;
6359 if (c) do {
6360 dw_attr_ref name_attr;
6362 c = c->die_sib;
6363 name_attr = get_AT (c, DW_AT_name);
6364 if (is_template_instantiation (c))
6366 /* Ignore instantiations of member type and function templates. */
6368 else if (name_attr != NULL
6369 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6371 /* Use a shallow checksum for named nested types and member
6372 functions. */
6373 CHECKSUM_ULEB128 ('S');
6374 CHECKSUM_ULEB128 (c->die_tag);
6375 CHECKSUM_STRING (AT_string (name_attr));
6377 else
6379 /* Use a deep checksum for other children. */
6380 /* Mark this DIE so it gets processed when unmarking. */
6381 if (c->die_mark == 0)
6382 c->die_mark = -1;
6383 die_checksum_ordered (c, ctx, mark);
6385 } while (c != die->die_child);
6387 CHECKSUM_ULEB128 (0);
6390 /* Add a type name and tag to a hash. */
6391 static void
6392 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6394 CHECKSUM_ULEB128 (tag);
6395 CHECKSUM_STRING (name);
6398 #undef CHECKSUM
6399 #undef CHECKSUM_STRING
6400 #undef CHECKSUM_ATTR
6401 #undef CHECKSUM_LEB128
6402 #undef CHECKSUM_ULEB128
6404 /* Generate the type signature for DIE. This is computed by generating an
6405 MD5 checksum over the DIE's tag, its relevant attributes, and its
6406 children. Attributes that are references to other DIEs are processed
6407 by recursion, using the MARK field to prevent infinite recursion.
6408 If the DIE is nested inside a namespace or another type, we also
6409 need to include that context in the signature. The lower 64 bits
6410 of the resulting MD5 checksum comprise the signature. */
6412 static void
6413 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6415 int mark;
6416 const char *name;
6417 unsigned char checksum[16];
6418 struct md5_ctx ctx;
6419 dw_die_ref decl;
6420 dw_die_ref parent;
6422 name = get_AT_string (die, DW_AT_name);
6423 decl = get_AT_ref (die, DW_AT_specification);
6424 parent = get_die_parent (die);
6426 /* First, compute a signature for just the type name (and its surrounding
6427 context, if any. This is stored in the type unit DIE for link-time
6428 ODR (one-definition rule) checking. */
6430 if (is_cxx () && name != NULL)
6432 md5_init_ctx (&ctx);
6434 /* Checksum the names of surrounding namespaces and structures. */
6435 if (parent != NULL)
6436 checksum_die_context (parent, &ctx);
6438 /* Checksum the current DIE. */
6439 die_odr_checksum (die->die_tag, name, &ctx);
6440 md5_finish_ctx (&ctx, checksum);
6442 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6445 /* Next, compute the complete type signature. */
6447 md5_init_ctx (&ctx);
6448 mark = 1;
6449 die->die_mark = mark;
6451 /* Checksum the names of surrounding namespaces and structures. */
6452 if (parent != NULL)
6453 checksum_die_context (parent, &ctx);
6455 /* Checksum the DIE and its children. */
6456 die_checksum_ordered (die, &ctx, &mark);
6457 unmark_all_dies (die);
6458 md5_finish_ctx (&ctx, checksum);
6460 /* Store the signature in the type node and link the type DIE and the
6461 type node together. */
6462 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6463 DWARF_TYPE_SIGNATURE_SIZE);
6464 die->comdat_type_p = true;
6465 die->die_id.die_type_node = type_node;
6466 type_node->type_die = die;
6468 /* If the DIE is a specification, link its declaration to the type node
6469 as well. */
6470 if (decl != NULL)
6472 decl->comdat_type_p = true;
6473 decl->die_id.die_type_node = type_node;
6477 /* Do the location expressions look same? */
6478 static inline int
6479 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6481 return loc1->dw_loc_opc == loc2->dw_loc_opc
6482 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6483 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6486 /* Do the values look the same? */
6487 static int
6488 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6490 dw_loc_descr_ref loc1, loc2;
6491 rtx r1, r2;
6493 if (v1->val_class != v2->val_class)
6494 return 0;
6496 switch (v1->val_class)
6498 case dw_val_class_const:
6499 return v1->v.val_int == v2->v.val_int;
6500 case dw_val_class_unsigned_const:
6501 return v1->v.val_unsigned == v2->v.val_unsigned;
6502 case dw_val_class_const_double:
6503 return v1->v.val_double.high == v2->v.val_double.high
6504 && v1->v.val_double.low == v2->v.val_double.low;
6505 case dw_val_class_wide_int:
6506 return *v1->v.val_wide == *v2->v.val_wide;
6507 case dw_val_class_vec:
6508 if (v1->v.val_vec.length != v2->v.val_vec.length
6509 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6510 return 0;
6511 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6512 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6513 return 0;
6514 return 1;
6515 case dw_val_class_flag:
6516 return v1->v.val_flag == v2->v.val_flag;
6517 case dw_val_class_str:
6518 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6520 case dw_val_class_addr:
6521 r1 = v1->v.val_addr;
6522 r2 = v2->v.val_addr;
6523 if (GET_CODE (r1) != GET_CODE (r2))
6524 return 0;
6525 return !rtx_equal_p (r1, r2);
6527 case dw_val_class_offset:
6528 return v1->v.val_offset == v2->v.val_offset;
6530 case dw_val_class_loc:
6531 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6532 loc1 && loc2;
6533 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6534 if (!same_loc_p (loc1, loc2, mark))
6535 return 0;
6536 return !loc1 && !loc2;
6538 case dw_val_class_die_ref:
6539 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6541 case dw_val_class_fde_ref:
6542 case dw_val_class_vms_delta:
6543 case dw_val_class_lbl_id:
6544 case dw_val_class_lineptr:
6545 case dw_val_class_macptr:
6546 case dw_val_class_high_pc:
6547 return 1;
6549 case dw_val_class_file:
6550 return v1->v.val_file == v2->v.val_file;
6552 case dw_val_class_data8:
6553 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6555 default:
6556 return 1;
6560 /* Do the attributes look the same? */
6562 static int
6563 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6565 if (at1->dw_attr != at2->dw_attr)
6566 return 0;
6568 /* We don't care that this was compiled with a different compiler
6569 snapshot; if the output is the same, that's what matters. */
6570 if (at1->dw_attr == DW_AT_producer)
6571 return 1;
6573 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6576 /* Do the dies look the same? */
6578 static int
6579 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6581 dw_die_ref c1, c2;
6582 dw_attr_ref a1;
6583 unsigned ix;
6585 /* To avoid infinite recursion. */
6586 if (die1->die_mark)
6587 return die1->die_mark == die2->die_mark;
6588 die1->die_mark = die2->die_mark = ++(*mark);
6590 if (die1->die_tag != die2->die_tag)
6591 return 0;
6593 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6594 return 0;
6596 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6597 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6598 return 0;
6600 c1 = die1->die_child;
6601 c2 = die2->die_child;
6602 if (! c1)
6604 if (c2)
6605 return 0;
6607 else
6608 for (;;)
6610 if (!same_die_p (c1, c2, mark))
6611 return 0;
6612 c1 = c1->die_sib;
6613 c2 = c2->die_sib;
6614 if (c1 == die1->die_child)
6616 if (c2 == die2->die_child)
6617 break;
6618 else
6619 return 0;
6623 return 1;
6626 /* Do the dies look the same? Wrapper around same_die_p. */
6628 static int
6629 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6631 int mark = 0;
6632 int ret = same_die_p (die1, die2, &mark);
6634 unmark_all_dies (die1);
6635 unmark_all_dies (die2);
6637 return ret;
6640 /* The prefix to attach to symbols on DIEs in the current comdat debug
6641 info section. */
6642 static const char *comdat_symbol_id;
6644 /* The index of the current symbol within the current comdat CU. */
6645 static unsigned int comdat_symbol_number;
6647 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6648 children, and set comdat_symbol_id accordingly. */
6650 static void
6651 compute_section_prefix (dw_die_ref unit_die)
6653 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6654 const char *base = die_name ? lbasename (die_name) : "anonymous";
6655 char *name = XALLOCAVEC (char, strlen (base) + 64);
6656 char *p;
6657 int i, mark;
6658 unsigned char checksum[16];
6659 struct md5_ctx ctx;
6661 /* Compute the checksum of the DIE, then append part of it as hex digits to
6662 the name filename of the unit. */
6664 md5_init_ctx (&ctx);
6665 mark = 0;
6666 die_checksum (unit_die, &ctx, &mark);
6667 unmark_all_dies (unit_die);
6668 md5_finish_ctx (&ctx, checksum);
6670 sprintf (name, "%s.", base);
6671 clean_symbol_name (name);
6673 p = name + strlen (name);
6674 for (i = 0; i < 4; i++)
6676 sprintf (p, "%.2x", checksum[i]);
6677 p += 2;
6680 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6681 comdat_symbol_number = 0;
6684 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6686 static int
6687 is_type_die (dw_die_ref die)
6689 switch (die->die_tag)
6691 case DW_TAG_array_type:
6692 case DW_TAG_class_type:
6693 case DW_TAG_interface_type:
6694 case DW_TAG_enumeration_type:
6695 case DW_TAG_pointer_type:
6696 case DW_TAG_reference_type:
6697 case DW_TAG_rvalue_reference_type:
6698 case DW_TAG_string_type:
6699 case DW_TAG_structure_type:
6700 case DW_TAG_subroutine_type:
6701 case DW_TAG_union_type:
6702 case DW_TAG_ptr_to_member_type:
6703 case DW_TAG_set_type:
6704 case DW_TAG_subrange_type:
6705 case DW_TAG_base_type:
6706 case DW_TAG_const_type:
6707 case DW_TAG_file_type:
6708 case DW_TAG_packed_type:
6709 case DW_TAG_volatile_type:
6710 case DW_TAG_typedef:
6711 return 1;
6712 default:
6713 return 0;
6717 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6718 Basically, we want to choose the bits that are likely to be shared between
6719 compilations (types) and leave out the bits that are specific to individual
6720 compilations (functions). */
6722 static int
6723 is_comdat_die (dw_die_ref c)
6725 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6726 we do for stabs. The advantage is a greater likelihood of sharing between
6727 objects that don't include headers in the same order (and therefore would
6728 put the base types in a different comdat). jason 8/28/00 */
6730 if (c->die_tag == DW_TAG_base_type)
6731 return 0;
6733 if (c->die_tag == DW_TAG_pointer_type
6734 || c->die_tag == DW_TAG_reference_type
6735 || c->die_tag == DW_TAG_rvalue_reference_type
6736 || c->die_tag == DW_TAG_const_type
6737 || c->die_tag == DW_TAG_volatile_type)
6739 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6741 return t ? is_comdat_die (t) : 0;
6744 return is_type_die (c);
6747 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6748 compilation unit. */
6750 static int
6751 is_symbol_die (dw_die_ref c)
6753 return (is_type_die (c)
6754 || is_declaration_die (c)
6755 || c->die_tag == DW_TAG_namespace
6756 || c->die_tag == DW_TAG_module);
6759 /* Returns true iff C is a compile-unit DIE. */
6761 static inline bool
6762 is_cu_die (dw_die_ref c)
6764 return c && c->die_tag == DW_TAG_compile_unit;
6767 /* Returns true iff C is a unit DIE of some sort. */
6769 static inline bool
6770 is_unit_die (dw_die_ref c)
6772 return c && (c->die_tag == DW_TAG_compile_unit
6773 || c->die_tag == DW_TAG_partial_unit
6774 || c->die_tag == DW_TAG_type_unit);
6777 /* Returns true iff C is a namespace DIE. */
6779 static inline bool
6780 is_namespace_die (dw_die_ref c)
6782 return c && c->die_tag == DW_TAG_namespace;
6785 /* Returns true iff C is a class or structure DIE. */
6787 static inline bool
6788 is_class_die (dw_die_ref c)
6790 return c && (c->die_tag == DW_TAG_class_type
6791 || c->die_tag == DW_TAG_structure_type);
6794 /* Return non-zero if this DIE is a template parameter. */
6796 static inline bool
6797 is_template_parameter (dw_die_ref die)
6799 switch (die->die_tag)
6801 case DW_TAG_template_type_param:
6802 case DW_TAG_template_value_param:
6803 case DW_TAG_GNU_template_template_param:
6804 case DW_TAG_GNU_template_parameter_pack:
6805 return true;
6806 default:
6807 return false;
6811 /* Return non-zero if this DIE represents a template instantiation. */
6813 static inline bool
6814 is_template_instantiation (dw_die_ref die)
6816 dw_die_ref c;
6818 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6819 return false;
6820 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6821 return false;
6824 static char *
6825 gen_internal_sym (const char *prefix)
6827 char buf[256];
6829 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6830 return xstrdup (buf);
6833 /* Assign symbols to all worthy DIEs under DIE. */
6835 static void
6836 assign_symbol_names (dw_die_ref die)
6838 dw_die_ref c;
6840 if (is_symbol_die (die) && !die->comdat_type_p)
6842 if (comdat_symbol_id)
6844 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6846 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6847 comdat_symbol_id, comdat_symbol_number++);
6848 die->die_id.die_symbol = xstrdup (p);
6850 else
6851 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6854 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6857 struct cu_hash_table_entry
6859 dw_die_ref cu;
6860 unsigned min_comdat_num, max_comdat_num;
6861 struct cu_hash_table_entry *next;
6864 /* Helpers to manipulate hash table of CUs. */
6866 struct cu_hash_table_entry_hasher
6868 typedef cu_hash_table_entry value_type;
6869 typedef die_struct compare_type;
6870 static inline hashval_t hash (const value_type *);
6871 static inline bool equal (const value_type *, const compare_type *);
6872 static inline void remove (value_type *);
6875 inline hashval_t
6876 cu_hash_table_entry_hasher::hash (const value_type *entry)
6878 return htab_hash_string (entry->cu->die_id.die_symbol);
6881 inline bool
6882 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6883 const compare_type *entry2)
6885 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6888 inline void
6889 cu_hash_table_entry_hasher::remove (value_type *entry)
6891 struct cu_hash_table_entry *next;
6893 while (entry)
6895 next = entry->next;
6896 free (entry);
6897 entry = next;
6901 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6903 /* Check whether we have already seen this CU and set up SYM_NUM
6904 accordingly. */
6905 static int
6906 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6908 struct cu_hash_table_entry dummy;
6909 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6911 dummy.max_comdat_num = 0;
6913 slot = htable->find_slot_with_hash (cu,
6914 htab_hash_string (cu->die_id.die_symbol),
6915 INSERT);
6916 entry = *slot;
6918 for (; entry; last = entry, entry = entry->next)
6920 if (same_die_p_wrap (cu, entry->cu))
6921 break;
6924 if (entry)
6926 *sym_num = entry->min_comdat_num;
6927 return 1;
6930 entry = XCNEW (struct cu_hash_table_entry);
6931 entry->cu = cu;
6932 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6933 entry->next = *slot;
6934 *slot = entry;
6936 return 0;
6939 /* Record SYM_NUM to record of CU in HTABLE. */
6940 static void
6941 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6942 unsigned int sym_num)
6944 struct cu_hash_table_entry **slot, *entry;
6946 slot = htable->find_slot_with_hash (cu,
6947 htab_hash_string (cu->die_id.die_symbol),
6948 NO_INSERT);
6949 entry = *slot;
6951 entry->max_comdat_num = sym_num;
6954 /* Traverse the DIE (which is always comp_unit_die), and set up
6955 additional compilation units for each of the include files we see
6956 bracketed by BINCL/EINCL. */
6958 static void
6959 break_out_includes (dw_die_ref die)
6961 dw_die_ref c;
6962 dw_die_ref unit = NULL;
6963 limbo_die_node *node, **pnode;
6965 c = die->die_child;
6966 if (c) do {
6967 dw_die_ref prev = c;
6968 c = c->die_sib;
6969 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6970 || (unit && is_comdat_die (c)))
6972 dw_die_ref next = c->die_sib;
6974 /* This DIE is for a secondary CU; remove it from the main one. */
6975 remove_child_with_prev (c, prev);
6977 if (c->die_tag == DW_TAG_GNU_BINCL)
6978 unit = push_new_compile_unit (unit, c);
6979 else if (c->die_tag == DW_TAG_GNU_EINCL)
6980 unit = pop_compile_unit (unit);
6981 else
6982 add_child_die (unit, c);
6983 c = next;
6984 if (c == die->die_child)
6985 break;
6987 } while (c != die->die_child);
6989 #if 0
6990 /* We can only use this in debugging, since the frontend doesn't check
6991 to make sure that we leave every include file we enter. */
6992 gcc_assert (!unit);
6993 #endif
6995 assign_symbol_names (die);
6996 cu_hash_type cu_hash_table (10);
6997 for (node = limbo_die_list, pnode = &limbo_die_list;
6998 node;
6999 node = node->next)
7001 int is_dupl;
7003 compute_section_prefix (node->die);
7004 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7005 &comdat_symbol_number);
7006 assign_symbol_names (node->die);
7007 if (is_dupl)
7008 *pnode = node->next;
7009 else
7011 pnode = &node->next;
7012 record_comdat_symbol_number (node->die, &cu_hash_table,
7013 comdat_symbol_number);
7018 /* Return non-zero if this DIE is a declaration. */
7020 static int
7021 is_declaration_die (dw_die_ref die)
7023 dw_attr_ref a;
7024 unsigned ix;
7026 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7027 if (a->dw_attr == DW_AT_declaration)
7028 return 1;
7030 return 0;
7033 /* Return non-zero if this DIE is nested inside a subprogram. */
7035 static int
7036 is_nested_in_subprogram (dw_die_ref die)
7038 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7040 if (decl == NULL)
7041 decl = die;
7042 return local_scope_p (decl);
7045 /* Return non-zero if this DIE contains a defining declaration of a
7046 subprogram. */
7048 static int
7049 contains_subprogram_definition (dw_die_ref die)
7051 dw_die_ref c;
7053 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7054 return 1;
7055 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7056 return 0;
7059 /* Return non-zero if this is a type DIE that should be moved to a
7060 COMDAT .debug_types section. */
7062 static int
7063 should_move_die_to_comdat (dw_die_ref die)
7065 switch (die->die_tag)
7067 case DW_TAG_class_type:
7068 case DW_TAG_structure_type:
7069 case DW_TAG_enumeration_type:
7070 case DW_TAG_union_type:
7071 /* Don't move declarations, inlined instances, types nested in a
7072 subprogram, or types that contain subprogram definitions. */
7073 if (is_declaration_die (die)
7074 || get_AT (die, DW_AT_abstract_origin)
7075 || is_nested_in_subprogram (die)
7076 || contains_subprogram_definition (die))
7077 return 0;
7078 return 1;
7079 case DW_TAG_array_type:
7080 case DW_TAG_interface_type:
7081 case DW_TAG_pointer_type:
7082 case DW_TAG_reference_type:
7083 case DW_TAG_rvalue_reference_type:
7084 case DW_TAG_string_type:
7085 case DW_TAG_subroutine_type:
7086 case DW_TAG_ptr_to_member_type:
7087 case DW_TAG_set_type:
7088 case DW_TAG_subrange_type:
7089 case DW_TAG_base_type:
7090 case DW_TAG_const_type:
7091 case DW_TAG_file_type:
7092 case DW_TAG_packed_type:
7093 case DW_TAG_volatile_type:
7094 case DW_TAG_typedef:
7095 default:
7096 return 0;
7100 /* Make a clone of DIE. */
7102 static dw_die_ref
7103 clone_die (dw_die_ref die)
7105 dw_die_ref clone;
7106 dw_attr_ref a;
7107 unsigned ix;
7109 clone = ggc_cleared_alloc<die_node> ();
7110 clone->die_tag = die->die_tag;
7112 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7113 add_dwarf_attr (clone, a);
7115 return clone;
7118 /* Make a clone of the tree rooted at DIE. */
7120 static dw_die_ref
7121 clone_tree (dw_die_ref die)
7123 dw_die_ref c;
7124 dw_die_ref clone = clone_die (die);
7126 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7128 return clone;
7131 /* Make a clone of DIE as a declaration. */
7133 static dw_die_ref
7134 clone_as_declaration (dw_die_ref die)
7136 dw_die_ref clone;
7137 dw_die_ref decl;
7138 dw_attr_ref a;
7139 unsigned ix;
7141 /* If the DIE is already a declaration, just clone it. */
7142 if (is_declaration_die (die))
7143 return clone_die (die);
7145 /* If the DIE is a specification, just clone its declaration DIE. */
7146 decl = get_AT_ref (die, DW_AT_specification);
7147 if (decl != NULL)
7149 clone = clone_die (decl);
7150 if (die->comdat_type_p)
7151 add_AT_die_ref (clone, DW_AT_signature, die);
7152 return clone;
7155 clone = ggc_cleared_alloc<die_node> ();
7156 clone->die_tag = die->die_tag;
7158 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7160 /* We don't want to copy over all attributes.
7161 For example we don't want DW_AT_byte_size because otherwise we will no
7162 longer have a declaration and GDB will treat it as a definition. */
7164 switch (a->dw_attr)
7166 case DW_AT_abstract_origin:
7167 case DW_AT_artificial:
7168 case DW_AT_containing_type:
7169 case DW_AT_external:
7170 case DW_AT_name:
7171 case DW_AT_type:
7172 case DW_AT_virtuality:
7173 case DW_AT_linkage_name:
7174 case DW_AT_MIPS_linkage_name:
7175 add_dwarf_attr (clone, a);
7176 break;
7177 case DW_AT_byte_size:
7178 default:
7179 break;
7183 if (die->comdat_type_p)
7184 add_AT_die_ref (clone, DW_AT_signature, die);
7186 add_AT_flag (clone, DW_AT_declaration, 1);
7187 return clone;
7191 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7193 struct decl_table_entry
7195 dw_die_ref orig;
7196 dw_die_ref copy;
7199 /* Helpers to manipulate hash table of copied declarations. */
7201 /* Hashtable helpers. */
7203 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7205 typedef decl_table_entry value_type;
7206 typedef die_struct compare_type;
7207 static inline hashval_t hash (const value_type *);
7208 static inline bool equal (const value_type *, const compare_type *);
7211 inline hashval_t
7212 decl_table_entry_hasher::hash (const value_type *entry)
7214 return htab_hash_pointer (entry->orig);
7217 inline bool
7218 decl_table_entry_hasher::equal (const value_type *entry1,
7219 const compare_type *entry2)
7221 return entry1->orig == entry2;
7224 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7226 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7227 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7228 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7229 to check if the ancestor has already been copied into UNIT. */
7231 static dw_die_ref
7232 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7233 decl_hash_type *decl_table)
7235 dw_die_ref parent = die->die_parent;
7236 dw_die_ref new_parent = unit;
7237 dw_die_ref copy;
7238 decl_table_entry **slot = NULL;
7239 struct decl_table_entry *entry = NULL;
7241 if (decl_table)
7243 /* Check if the entry has already been copied to UNIT. */
7244 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7245 INSERT);
7246 if (*slot != HTAB_EMPTY_ENTRY)
7248 entry = *slot;
7249 return entry->copy;
7252 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7253 entry = XCNEW (struct decl_table_entry);
7254 entry->orig = die;
7255 entry->copy = NULL;
7256 *slot = entry;
7259 if (parent != NULL)
7261 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7262 if (spec != NULL)
7263 parent = spec;
7264 if (!is_unit_die (parent))
7265 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7268 copy = clone_as_declaration (die);
7269 add_child_die (new_parent, copy);
7271 if (decl_table)
7273 /* Record the pointer to the copy. */
7274 entry->copy = copy;
7277 return copy;
7279 /* Copy the declaration context to the new type unit DIE. This includes
7280 any surrounding namespace or type declarations. If the DIE has an
7281 AT_specification attribute, it also includes attributes and children
7282 attached to the specification, and returns a pointer to the original
7283 parent of the declaration DIE. Returns NULL otherwise. */
7285 static dw_die_ref
7286 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7288 dw_die_ref decl;
7289 dw_die_ref new_decl;
7290 dw_die_ref orig_parent = NULL;
7292 decl = get_AT_ref (die, DW_AT_specification);
7293 if (decl == NULL)
7294 decl = die;
7295 else
7297 unsigned ix;
7298 dw_die_ref c;
7299 dw_attr_ref a;
7301 /* The original DIE will be changed to a declaration, and must
7302 be moved to be a child of the original declaration DIE. */
7303 orig_parent = decl->die_parent;
7305 /* Copy the type node pointer from the new DIE to the original
7306 declaration DIE so we can forward references later. */
7307 decl->comdat_type_p = true;
7308 decl->die_id.die_type_node = die->die_id.die_type_node;
7310 remove_AT (die, DW_AT_specification);
7312 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7314 if (a->dw_attr != DW_AT_name
7315 && a->dw_attr != DW_AT_declaration
7316 && a->dw_attr != DW_AT_external)
7317 add_dwarf_attr (die, a);
7320 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7323 if (decl->die_parent != NULL
7324 && !is_unit_die (decl->die_parent))
7326 new_decl = copy_ancestor_tree (unit, decl, NULL);
7327 if (new_decl != NULL)
7329 remove_AT (new_decl, DW_AT_signature);
7330 add_AT_specification (die, new_decl);
7334 return orig_parent;
7337 /* Generate the skeleton ancestor tree for the given NODE, then clone
7338 the DIE and add the clone into the tree. */
7340 static void
7341 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7343 if (node->new_die != NULL)
7344 return;
7346 node->new_die = clone_as_declaration (node->old_die);
7348 if (node->parent != NULL)
7350 generate_skeleton_ancestor_tree (node->parent);
7351 add_child_die (node->parent->new_die, node->new_die);
7355 /* Generate a skeleton tree of DIEs containing any declarations that are
7356 found in the original tree. We traverse the tree looking for declaration
7357 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7359 static void
7360 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7362 skeleton_chain_node node;
7363 dw_die_ref c;
7364 dw_die_ref first;
7365 dw_die_ref prev = NULL;
7366 dw_die_ref next = NULL;
7368 node.parent = parent;
7370 first = c = parent->old_die->die_child;
7371 if (c)
7372 next = c->die_sib;
7373 if (c) do {
7374 if (prev == NULL || prev->die_sib == c)
7375 prev = c;
7376 c = next;
7377 next = (c == first ? NULL : c->die_sib);
7378 node.old_die = c;
7379 node.new_die = NULL;
7380 if (is_declaration_die (c))
7382 if (is_template_instantiation (c))
7384 /* Instantiated templates do not need to be cloned into the
7385 type unit. Just move the DIE and its children back to
7386 the skeleton tree (in the main CU). */
7387 remove_child_with_prev (c, prev);
7388 add_child_die (parent->new_die, c);
7389 c = prev;
7391 else
7393 /* Clone the existing DIE, move the original to the skeleton
7394 tree (which is in the main CU), and put the clone, with
7395 all the original's children, where the original came from
7396 (which is about to be moved to the type unit). */
7397 dw_die_ref clone = clone_die (c);
7398 move_all_children (c, clone);
7400 /* If the original has a DW_AT_object_pointer attribute,
7401 it would now point to a child DIE just moved to the
7402 cloned tree, so we need to remove that attribute from
7403 the original. */
7404 remove_AT (c, DW_AT_object_pointer);
7406 replace_child (c, clone, prev);
7407 generate_skeleton_ancestor_tree (parent);
7408 add_child_die (parent->new_die, c);
7409 node.new_die = c;
7410 c = clone;
7413 generate_skeleton_bottom_up (&node);
7414 } while (next != NULL);
7417 /* Wrapper function for generate_skeleton_bottom_up. */
7419 static dw_die_ref
7420 generate_skeleton (dw_die_ref die)
7422 skeleton_chain_node node;
7424 node.old_die = die;
7425 node.new_die = NULL;
7426 node.parent = NULL;
7428 /* If this type definition is nested inside another type,
7429 and is not an instantiation of a template, always leave
7430 at least a declaration in its place. */
7431 if (die->die_parent != NULL
7432 && is_type_die (die->die_parent)
7433 && !is_template_instantiation (die))
7434 node.new_die = clone_as_declaration (die);
7436 generate_skeleton_bottom_up (&node);
7437 return node.new_die;
7440 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7441 declaration. The original DIE is moved to a new compile unit so that
7442 existing references to it follow it to the new location. If any of the
7443 original DIE's descendants is a declaration, we need to replace the
7444 original DIE with a skeleton tree and move the declarations back into the
7445 skeleton tree. */
7447 static dw_die_ref
7448 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7449 dw_die_ref prev)
7451 dw_die_ref skeleton, orig_parent;
7453 /* Copy the declaration context to the type unit DIE. If the returned
7454 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7455 that DIE. */
7456 orig_parent = copy_declaration_context (unit, child);
7458 skeleton = generate_skeleton (child);
7459 if (skeleton == NULL)
7460 remove_child_with_prev (child, prev);
7461 else
7463 skeleton->comdat_type_p = true;
7464 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7466 /* If the original DIE was a specification, we need to put
7467 the skeleton under the parent DIE of the declaration.
7468 This leaves the original declaration in the tree, but
7469 it will be pruned later since there are no longer any
7470 references to it. */
7471 if (orig_parent != NULL)
7473 remove_child_with_prev (child, prev);
7474 add_child_die (orig_parent, skeleton);
7476 else
7477 replace_child (child, skeleton, prev);
7480 return skeleton;
7483 /* Traverse the DIE and set up additional .debug_types sections for each
7484 type worthy of being placed in a COMDAT section. */
7486 static void
7487 break_out_comdat_types (dw_die_ref die)
7489 dw_die_ref c;
7490 dw_die_ref first;
7491 dw_die_ref prev = NULL;
7492 dw_die_ref next = NULL;
7493 dw_die_ref unit = NULL;
7495 first = c = die->die_child;
7496 if (c)
7497 next = c->die_sib;
7498 if (c) do {
7499 if (prev == NULL || prev->die_sib == c)
7500 prev = c;
7501 c = next;
7502 next = (c == first ? NULL : c->die_sib);
7503 if (should_move_die_to_comdat (c))
7505 dw_die_ref replacement;
7506 comdat_type_node_ref type_node;
7508 /* Break out nested types into their own type units. */
7509 break_out_comdat_types (c);
7511 /* Create a new type unit DIE as the root for the new tree, and
7512 add it to the list of comdat types. */
7513 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7514 add_AT_unsigned (unit, DW_AT_language,
7515 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7516 type_node = ggc_cleared_alloc<comdat_type_node> ();
7517 type_node->root_die = unit;
7518 type_node->next = comdat_type_list;
7519 comdat_type_list = type_node;
7521 /* Generate the type signature. */
7522 generate_type_signature (c, type_node);
7524 /* Copy the declaration context, attributes, and children of the
7525 declaration into the new type unit DIE, then remove this DIE
7526 from the main CU (or replace it with a skeleton if necessary). */
7527 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7528 type_node->skeleton_die = replacement;
7530 /* Add the DIE to the new compunit. */
7531 add_child_die (unit, c);
7533 if (replacement != NULL)
7534 c = replacement;
7536 else if (c->die_tag == DW_TAG_namespace
7537 || c->die_tag == DW_TAG_class_type
7538 || c->die_tag == DW_TAG_structure_type
7539 || c->die_tag == DW_TAG_union_type)
7541 /* Look for nested types that can be broken out. */
7542 break_out_comdat_types (c);
7544 } while (next != NULL);
7547 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7548 Enter all the cloned children into the hash table decl_table. */
7550 static dw_die_ref
7551 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7553 dw_die_ref c;
7554 dw_die_ref clone;
7555 struct decl_table_entry *entry;
7556 decl_table_entry **slot;
7558 if (die->die_tag == DW_TAG_subprogram)
7559 clone = clone_as_declaration (die);
7560 else
7561 clone = clone_die (die);
7563 slot = decl_table->find_slot_with_hash (die,
7564 htab_hash_pointer (die), INSERT);
7566 /* Assert that DIE isn't in the hash table yet. If it would be there
7567 before, the ancestors would be necessarily there as well, therefore
7568 clone_tree_partial wouldn't be called. */
7569 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7571 entry = XCNEW (struct decl_table_entry);
7572 entry->orig = die;
7573 entry->copy = clone;
7574 *slot = entry;
7576 if (die->die_tag != DW_TAG_subprogram)
7577 FOR_EACH_CHILD (die, c,
7578 add_child_die (clone, clone_tree_partial (c, decl_table)));
7580 return clone;
7583 /* Walk the DIE and its children, looking for references to incomplete
7584 or trivial types that are unmarked (i.e., that are not in the current
7585 type_unit). */
7587 static void
7588 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7590 dw_die_ref c;
7591 dw_attr_ref a;
7592 unsigned ix;
7594 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7596 if (AT_class (a) == dw_val_class_die_ref)
7598 dw_die_ref targ = AT_ref (a);
7599 decl_table_entry **slot;
7600 struct decl_table_entry *entry;
7602 if (targ->die_mark != 0 || targ->comdat_type_p)
7603 continue;
7605 slot = decl_table->find_slot_with_hash (targ,
7606 htab_hash_pointer (targ),
7607 INSERT);
7609 if (*slot != HTAB_EMPTY_ENTRY)
7611 /* TARG has already been copied, so we just need to
7612 modify the reference to point to the copy. */
7613 entry = *slot;
7614 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7616 else
7618 dw_die_ref parent = unit;
7619 dw_die_ref copy = clone_die (targ);
7621 /* Record in DECL_TABLE that TARG has been copied.
7622 Need to do this now, before the recursive call,
7623 because DECL_TABLE may be expanded and SLOT
7624 would no longer be a valid pointer. */
7625 entry = XCNEW (struct decl_table_entry);
7626 entry->orig = targ;
7627 entry->copy = copy;
7628 *slot = entry;
7630 /* If TARG is not a declaration DIE, we need to copy its
7631 children. */
7632 if (!is_declaration_die (targ))
7634 FOR_EACH_CHILD (
7635 targ, c,
7636 add_child_die (copy,
7637 clone_tree_partial (c, decl_table)));
7640 /* Make sure the cloned tree is marked as part of the
7641 type unit. */
7642 mark_dies (copy);
7644 /* If TARG has surrounding context, copy its ancestor tree
7645 into the new type unit. */
7646 if (targ->die_parent != NULL
7647 && !is_unit_die (targ->die_parent))
7648 parent = copy_ancestor_tree (unit, targ->die_parent,
7649 decl_table);
7651 add_child_die (parent, copy);
7652 a->dw_attr_val.v.val_die_ref.die = copy;
7654 /* Make sure the newly-copied DIE is walked. If it was
7655 installed in a previously-added context, it won't
7656 get visited otherwise. */
7657 if (parent != unit)
7659 /* Find the highest point of the newly-added tree,
7660 mark each node along the way, and walk from there. */
7661 parent->die_mark = 1;
7662 while (parent->die_parent
7663 && parent->die_parent->die_mark == 0)
7665 parent = parent->die_parent;
7666 parent->die_mark = 1;
7668 copy_decls_walk (unit, parent, decl_table);
7674 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7677 /* Copy declarations for "unworthy" types into the new comdat section.
7678 Incomplete types, modified types, and certain other types aren't broken
7679 out into comdat sections of their own, so they don't have a signature,
7680 and we need to copy the declaration into the same section so that we
7681 don't have an external reference. */
7683 static void
7684 copy_decls_for_unworthy_types (dw_die_ref unit)
7686 mark_dies (unit);
7687 decl_hash_type decl_table (10);
7688 copy_decls_walk (unit, unit, &decl_table);
7689 unmark_dies (unit);
7692 /* Traverse the DIE and add a sibling attribute if it may have the
7693 effect of speeding up access to siblings. To save some space,
7694 avoid generating sibling attributes for DIE's without children. */
7696 static void
7697 add_sibling_attributes (dw_die_ref die)
7699 dw_die_ref c;
7701 if (! die->die_child)
7702 return;
7704 if (die->die_parent && die != die->die_parent->die_child)
7705 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7707 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7710 /* Output all location lists for the DIE and its children. */
7712 static void
7713 output_location_lists (dw_die_ref die)
7715 dw_die_ref c;
7716 dw_attr_ref a;
7717 unsigned ix;
7719 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7720 if (AT_class (a) == dw_val_class_loc_list)
7721 output_loc_list (AT_loc_list (a));
7723 FOR_EACH_CHILD (die, c, output_location_lists (c));
7726 /* We want to limit the number of external references, because they are
7727 larger than local references: a relocation takes multiple words, and
7728 even a sig8 reference is always eight bytes, whereas a local reference
7729 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7730 So if we encounter multiple external references to the same type DIE, we
7731 make a local typedef stub for it and redirect all references there.
7733 This is the element of the hash table for keeping track of these
7734 references. */
7736 struct external_ref
7738 dw_die_ref type;
7739 dw_die_ref stub;
7740 unsigned n_refs;
7743 /* Hashtable helpers. */
7745 struct external_ref_hasher : typed_free_remove <external_ref>
7747 typedef external_ref value_type;
7748 typedef external_ref compare_type;
7749 static inline hashval_t hash (const value_type *);
7750 static inline bool equal (const value_type *, const compare_type *);
7753 inline hashval_t
7754 external_ref_hasher::hash (const value_type *r)
7756 dw_die_ref die = r->type;
7757 hashval_t h = 0;
7759 /* We can't use the address of the DIE for hashing, because
7760 that will make the order of the stub DIEs non-deterministic. */
7761 if (! die->comdat_type_p)
7762 /* We have a symbol; use it to compute a hash. */
7763 h = htab_hash_string (die->die_id.die_symbol);
7764 else
7766 /* We have a type signature; use a subset of the bits as the hash.
7767 The 8-byte signature is at least as large as hashval_t. */
7768 comdat_type_node_ref type_node = die->die_id.die_type_node;
7769 memcpy (&h, type_node->signature, sizeof (h));
7771 return h;
7774 inline bool
7775 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7777 return r1->type == r2->type;
7780 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7782 /* Return a pointer to the external_ref for references to DIE. */
7784 static struct external_ref *
7785 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7787 struct external_ref ref, *ref_p;
7788 external_ref **slot;
7790 ref.type = die;
7791 slot = map->find_slot (&ref, INSERT);
7792 if (*slot != HTAB_EMPTY_ENTRY)
7793 return *slot;
7795 ref_p = XCNEW (struct external_ref);
7796 ref_p->type = die;
7797 *slot = ref_p;
7798 return ref_p;
7801 /* Subroutine of optimize_external_refs, below.
7803 If we see a type skeleton, record it as our stub. If we see external
7804 references, remember how many we've seen. */
7806 static void
7807 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7809 dw_die_ref c;
7810 dw_attr_ref a;
7811 unsigned ix;
7812 struct external_ref *ref_p;
7814 if (is_type_die (die)
7815 && (c = get_AT_ref (die, DW_AT_signature)))
7817 /* This is a local skeleton; use it for local references. */
7818 ref_p = lookup_external_ref (map, c);
7819 ref_p->stub = die;
7822 /* Scan the DIE references, and remember any that refer to DIEs from
7823 other CUs (i.e. those which are not marked). */
7824 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7825 if (AT_class (a) == dw_val_class_die_ref
7826 && (c = AT_ref (a))->die_mark == 0
7827 && is_type_die (c))
7829 ref_p = lookup_external_ref (map, c);
7830 ref_p->n_refs++;
7833 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7836 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7837 points to an external_ref, DATA is the CU we're processing. If we don't
7838 already have a local stub, and we have multiple refs, build a stub. */
7841 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7843 struct external_ref *ref_p = *slot;
7845 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7847 /* We have multiple references to this type, so build a small stub.
7848 Both of these forms are a bit dodgy from the perspective of the
7849 DWARF standard, since technically they should have names. */
7850 dw_die_ref cu = data;
7851 dw_die_ref type = ref_p->type;
7852 dw_die_ref stub = NULL;
7854 if (type->comdat_type_p)
7856 /* If we refer to this type via sig8, use AT_signature. */
7857 stub = new_die (type->die_tag, cu, NULL_TREE);
7858 add_AT_die_ref (stub, DW_AT_signature, type);
7860 else
7862 /* Otherwise, use a typedef with no name. */
7863 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7864 add_AT_die_ref (stub, DW_AT_type, type);
7867 stub->die_mark++;
7868 ref_p->stub = stub;
7870 return 1;
7873 /* DIE is a unit; look through all the DIE references to see if there are
7874 any external references to types, and if so, create local stubs for
7875 them which will be applied in build_abbrev_table. This is useful because
7876 references to local DIEs are smaller. */
7878 static external_ref_hash_type *
7879 optimize_external_refs (dw_die_ref die)
7881 external_ref_hash_type *map = new external_ref_hash_type (10);
7882 optimize_external_refs_1 (die, map);
7883 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7884 return map;
7887 /* The format of each DIE (and its attribute value pairs) is encoded in an
7888 abbreviation table. This routine builds the abbreviation table and assigns
7889 a unique abbreviation id for each abbreviation entry. The children of each
7890 die are visited recursively. */
7892 static void
7893 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7895 unsigned long abbrev_id;
7896 unsigned int n_alloc;
7897 dw_die_ref c;
7898 dw_attr_ref a;
7899 unsigned ix;
7901 /* Scan the DIE references, and replace any that refer to
7902 DIEs from other CUs (i.e. those which are not marked) with
7903 the local stubs we built in optimize_external_refs. */
7904 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7905 if (AT_class (a) == dw_val_class_die_ref
7906 && (c = AT_ref (a))->die_mark == 0)
7908 struct external_ref *ref_p;
7909 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7911 ref_p = lookup_external_ref (extern_map, c);
7912 if (ref_p->stub && ref_p->stub != die)
7913 change_AT_die_ref (a, ref_p->stub);
7914 else
7915 /* We aren't changing this reference, so mark it external. */
7916 set_AT_ref_external (a, 1);
7919 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7921 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7922 dw_attr_ref die_a, abbrev_a;
7923 unsigned ix;
7924 bool ok = true;
7926 if (abbrev->die_tag != die->die_tag)
7927 continue;
7928 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7929 continue;
7931 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7932 continue;
7934 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7936 abbrev_a = &(*abbrev->die_attr)[ix];
7937 if ((abbrev_a->dw_attr != die_a->dw_attr)
7938 || (value_format (abbrev_a) != value_format (die_a)))
7940 ok = false;
7941 break;
7944 if (ok)
7945 break;
7948 if (abbrev_id >= abbrev_die_table_in_use)
7950 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7952 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7953 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7954 n_alloc);
7956 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7957 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7958 abbrev_die_table_allocated = n_alloc;
7961 ++abbrev_die_table_in_use;
7962 abbrev_die_table[abbrev_id] = die;
7965 die->die_abbrev = abbrev_id;
7966 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7969 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7971 static int
7972 constant_size (unsigned HOST_WIDE_INT value)
7974 int log;
7976 if (value == 0)
7977 log = 0;
7978 else
7979 log = floor_log2 (value);
7981 log = log / 8;
7982 log = 1 << (floor_log2 (log) + 1);
7984 return log;
7987 /* Return the size of a DIE as it is represented in the
7988 .debug_info section. */
7990 static unsigned long
7991 size_of_die (dw_die_ref die)
7993 unsigned long size = 0;
7994 dw_attr_ref a;
7995 unsigned ix;
7996 enum dwarf_form form;
7998 size += size_of_uleb128 (die->die_abbrev);
7999 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8001 switch (AT_class (a))
8003 case dw_val_class_addr:
8004 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8006 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8007 size += size_of_uleb128 (AT_index (a));
8009 else
8010 size += DWARF2_ADDR_SIZE;
8011 break;
8012 case dw_val_class_offset:
8013 size += DWARF_OFFSET_SIZE;
8014 break;
8015 case dw_val_class_loc:
8017 unsigned long lsize = size_of_locs (AT_loc (a));
8019 /* Block length. */
8020 if (dwarf_version >= 4)
8021 size += size_of_uleb128 (lsize);
8022 else
8023 size += constant_size (lsize);
8024 size += lsize;
8026 break;
8027 case dw_val_class_loc_list:
8028 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8030 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8031 size += size_of_uleb128 (AT_index (a));
8033 else
8034 size += DWARF_OFFSET_SIZE;
8035 break;
8036 case dw_val_class_range_list:
8037 size += DWARF_OFFSET_SIZE;
8038 break;
8039 case dw_val_class_const:
8040 size += size_of_sleb128 (AT_int (a));
8041 break;
8042 case dw_val_class_unsigned_const:
8044 int csize = constant_size (AT_unsigned (a));
8045 if (dwarf_version == 3
8046 && a->dw_attr == DW_AT_data_member_location
8047 && csize >= 4)
8048 size += size_of_uleb128 (AT_unsigned (a));
8049 else
8050 size += csize;
8052 break;
8053 case dw_val_class_const_double:
8054 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8055 if (HOST_BITS_PER_WIDE_INT >= 64)
8056 size++; /* block */
8057 break;
8058 case dw_val_class_wide_int:
8059 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8060 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8061 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8062 > 64)
8063 size++; /* block */
8064 break;
8065 case dw_val_class_vec:
8066 size += constant_size (a->dw_attr_val.v.val_vec.length
8067 * a->dw_attr_val.v.val_vec.elt_size)
8068 + a->dw_attr_val.v.val_vec.length
8069 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8070 break;
8071 case dw_val_class_flag:
8072 if (dwarf_version >= 4)
8073 /* Currently all add_AT_flag calls pass in 1 as last argument,
8074 so DW_FORM_flag_present can be used. If that ever changes,
8075 we'll need to use DW_FORM_flag and have some optimization
8076 in build_abbrev_table that will change those to
8077 DW_FORM_flag_present if it is set to 1 in all DIEs using
8078 the same abbrev entry. */
8079 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8080 else
8081 size += 1;
8082 break;
8083 case dw_val_class_die_ref:
8084 if (AT_ref_external (a))
8086 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8087 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8088 is sized by target address length, whereas in DWARF3
8089 it's always sized as an offset. */
8090 if (use_debug_types)
8091 size += DWARF_TYPE_SIGNATURE_SIZE;
8092 else if (dwarf_version == 2)
8093 size += DWARF2_ADDR_SIZE;
8094 else
8095 size += DWARF_OFFSET_SIZE;
8097 else
8098 size += DWARF_OFFSET_SIZE;
8099 break;
8100 case dw_val_class_fde_ref:
8101 size += DWARF_OFFSET_SIZE;
8102 break;
8103 case dw_val_class_lbl_id:
8104 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8106 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8107 size += size_of_uleb128 (AT_index (a));
8109 else
8110 size += DWARF2_ADDR_SIZE;
8111 break;
8112 case dw_val_class_lineptr:
8113 case dw_val_class_macptr:
8114 size += DWARF_OFFSET_SIZE;
8115 break;
8116 case dw_val_class_str:
8117 form = AT_string_form (a);
8118 if (form == DW_FORM_strp)
8119 size += DWARF_OFFSET_SIZE;
8120 else if (form == DW_FORM_GNU_str_index)
8121 size += size_of_uleb128 (AT_index (a));
8122 else
8123 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8124 break;
8125 case dw_val_class_file:
8126 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8127 break;
8128 case dw_val_class_data8:
8129 size += 8;
8130 break;
8131 case dw_val_class_vms_delta:
8132 size += DWARF_OFFSET_SIZE;
8133 break;
8134 case dw_val_class_high_pc:
8135 size += DWARF2_ADDR_SIZE;
8136 break;
8137 default:
8138 gcc_unreachable ();
8142 return size;
8145 /* Size the debugging information associated with a given DIE. Visits the
8146 DIE's children recursively. Updates the global variable next_die_offset, on
8147 each time through. Uses the current value of next_die_offset to update the
8148 die_offset field in each DIE. */
8150 static void
8151 calc_die_sizes (dw_die_ref die)
8153 dw_die_ref c;
8155 gcc_assert (die->die_offset == 0
8156 || (unsigned long int) die->die_offset == next_die_offset);
8157 die->die_offset = next_die_offset;
8158 next_die_offset += size_of_die (die);
8160 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8162 if (die->die_child != NULL)
8163 /* Count the null byte used to terminate sibling lists. */
8164 next_die_offset += 1;
8167 /* Size just the base type children at the start of the CU.
8168 This is needed because build_abbrev needs to size locs
8169 and sizing of type based stack ops needs to know die_offset
8170 values for the base types. */
8172 static void
8173 calc_base_type_die_sizes (void)
8175 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8176 unsigned int i;
8177 dw_die_ref base_type;
8178 #if ENABLE_ASSERT_CHECKING
8179 dw_die_ref prev = comp_unit_die ()->die_child;
8180 #endif
8182 die_offset += size_of_die (comp_unit_die ());
8183 for (i = 0; base_types.iterate (i, &base_type); i++)
8185 #if ENABLE_ASSERT_CHECKING
8186 gcc_assert (base_type->die_offset == 0
8187 && prev->die_sib == base_type
8188 && base_type->die_child == NULL
8189 && base_type->die_abbrev);
8190 prev = base_type;
8191 #endif
8192 base_type->die_offset = die_offset;
8193 die_offset += size_of_die (base_type);
8197 /* Set the marks for a die and its children. We do this so
8198 that we know whether or not a reference needs to use FORM_ref_addr; only
8199 DIEs in the same CU will be marked. We used to clear out the offset
8200 and use that as the flag, but ran into ordering problems. */
8202 static void
8203 mark_dies (dw_die_ref die)
8205 dw_die_ref c;
8207 gcc_assert (!die->die_mark);
8209 die->die_mark = 1;
8210 FOR_EACH_CHILD (die, c, mark_dies (c));
8213 /* Clear the marks for a die and its children. */
8215 static void
8216 unmark_dies (dw_die_ref die)
8218 dw_die_ref c;
8220 if (! use_debug_types)
8221 gcc_assert (die->die_mark);
8223 die->die_mark = 0;
8224 FOR_EACH_CHILD (die, c, unmark_dies (c));
8227 /* Clear the marks for a die, its children and referred dies. */
8229 static void
8230 unmark_all_dies (dw_die_ref die)
8232 dw_die_ref c;
8233 dw_attr_ref a;
8234 unsigned ix;
8236 if (!die->die_mark)
8237 return;
8238 die->die_mark = 0;
8240 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8242 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8243 if (AT_class (a) == dw_val_class_die_ref)
8244 unmark_all_dies (AT_ref (a));
8247 /* Calculate if the entry should appear in the final output file. It may be
8248 from a pruned a type. */
8250 static bool
8251 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8253 /* By limiting gnu pubnames to definitions only, gold can generate a
8254 gdb index without entries for declarations, which don't include
8255 enough information to be useful. */
8256 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8257 return false;
8259 if (table == pubname_table)
8261 /* Enumerator names are part of the pubname table, but the
8262 parent DW_TAG_enumeration_type die may have been pruned.
8263 Don't output them if that is the case. */
8264 if (p->die->die_tag == DW_TAG_enumerator &&
8265 (p->die->die_parent == NULL
8266 || !p->die->die_parent->die_perennial_p))
8267 return false;
8269 /* Everything else in the pubname table is included. */
8270 return true;
8273 /* The pubtypes table shouldn't include types that have been
8274 pruned. */
8275 return (p->die->die_offset != 0
8276 || !flag_eliminate_unused_debug_types);
8279 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8280 generated for the compilation unit. */
8282 static unsigned long
8283 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8285 unsigned long size;
8286 unsigned i;
8287 pubname_ref p;
8288 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8290 size = DWARF_PUBNAMES_HEADER_SIZE;
8291 FOR_EACH_VEC_ELT (*names, i, p)
8292 if (include_pubname_in_output (names, p))
8293 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8295 size += DWARF_OFFSET_SIZE;
8296 return size;
8299 /* Return the size of the information in the .debug_aranges section. */
8301 static unsigned long
8302 size_of_aranges (void)
8304 unsigned long size;
8306 size = DWARF_ARANGES_HEADER_SIZE;
8308 /* Count the address/length pair for this compilation unit. */
8309 if (text_section_used)
8310 size += 2 * DWARF2_ADDR_SIZE;
8311 if (cold_text_section_used)
8312 size += 2 * DWARF2_ADDR_SIZE;
8313 if (have_multiple_function_sections)
8315 unsigned fde_idx;
8316 dw_fde_ref fde;
8318 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8320 if (DECL_IGNORED_P (fde->decl))
8321 continue;
8322 if (!fde->in_std_section)
8323 size += 2 * DWARF2_ADDR_SIZE;
8324 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8325 size += 2 * DWARF2_ADDR_SIZE;
8329 /* Count the two zero words used to terminated the address range table. */
8330 size += 2 * DWARF2_ADDR_SIZE;
8331 return size;
8334 /* Select the encoding of an attribute value. */
8336 static enum dwarf_form
8337 value_format (dw_attr_ref a)
8339 switch (AT_class (a))
8341 case dw_val_class_addr:
8342 /* Only very few attributes allow DW_FORM_addr. */
8343 switch (a->dw_attr)
8345 case DW_AT_low_pc:
8346 case DW_AT_high_pc:
8347 case DW_AT_entry_pc:
8348 case DW_AT_trampoline:
8349 return (AT_index (a) == NOT_INDEXED
8350 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8351 default:
8352 break;
8354 switch (DWARF2_ADDR_SIZE)
8356 case 1:
8357 return DW_FORM_data1;
8358 case 2:
8359 return DW_FORM_data2;
8360 case 4:
8361 return DW_FORM_data4;
8362 case 8:
8363 return DW_FORM_data8;
8364 default:
8365 gcc_unreachable ();
8367 case dw_val_class_range_list:
8368 case dw_val_class_loc_list:
8369 if (dwarf_version >= 4)
8370 return DW_FORM_sec_offset;
8371 /* FALLTHRU */
8372 case dw_val_class_vms_delta:
8373 case dw_val_class_offset:
8374 switch (DWARF_OFFSET_SIZE)
8376 case 4:
8377 return DW_FORM_data4;
8378 case 8:
8379 return DW_FORM_data8;
8380 default:
8381 gcc_unreachable ();
8383 case dw_val_class_loc:
8384 if (dwarf_version >= 4)
8385 return DW_FORM_exprloc;
8386 switch (constant_size (size_of_locs (AT_loc (a))))
8388 case 1:
8389 return DW_FORM_block1;
8390 case 2:
8391 return DW_FORM_block2;
8392 case 4:
8393 return DW_FORM_block4;
8394 default:
8395 gcc_unreachable ();
8397 case dw_val_class_const:
8398 return DW_FORM_sdata;
8399 case dw_val_class_unsigned_const:
8400 switch (constant_size (AT_unsigned (a)))
8402 case 1:
8403 return DW_FORM_data1;
8404 case 2:
8405 return DW_FORM_data2;
8406 case 4:
8407 /* In DWARF3 DW_AT_data_member_location with
8408 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8409 constant, so we need to use DW_FORM_udata if we need
8410 a large constant. */
8411 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8412 return DW_FORM_udata;
8413 return DW_FORM_data4;
8414 case 8:
8415 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8416 return DW_FORM_udata;
8417 return DW_FORM_data8;
8418 default:
8419 gcc_unreachable ();
8421 case dw_val_class_const_double:
8422 switch (HOST_BITS_PER_WIDE_INT)
8424 case 8:
8425 return DW_FORM_data2;
8426 case 16:
8427 return DW_FORM_data4;
8428 case 32:
8429 return DW_FORM_data8;
8430 case 64:
8431 default:
8432 return DW_FORM_block1;
8434 case dw_val_class_wide_int:
8435 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8437 case 8:
8438 return DW_FORM_data1;
8439 case 16:
8440 return DW_FORM_data2;
8441 case 32:
8442 return DW_FORM_data4;
8443 case 64:
8444 return DW_FORM_data8;
8445 default:
8446 return DW_FORM_block1;
8448 case dw_val_class_vec:
8449 switch (constant_size (a->dw_attr_val.v.val_vec.length
8450 * a->dw_attr_val.v.val_vec.elt_size))
8452 case 1:
8453 return DW_FORM_block1;
8454 case 2:
8455 return DW_FORM_block2;
8456 case 4:
8457 return DW_FORM_block4;
8458 default:
8459 gcc_unreachable ();
8461 case dw_val_class_flag:
8462 if (dwarf_version >= 4)
8464 /* Currently all add_AT_flag calls pass in 1 as last argument,
8465 so DW_FORM_flag_present can be used. If that ever changes,
8466 we'll need to use DW_FORM_flag and have some optimization
8467 in build_abbrev_table that will change those to
8468 DW_FORM_flag_present if it is set to 1 in all DIEs using
8469 the same abbrev entry. */
8470 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8471 return DW_FORM_flag_present;
8473 return DW_FORM_flag;
8474 case dw_val_class_die_ref:
8475 if (AT_ref_external (a))
8476 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8477 else
8478 return DW_FORM_ref;
8479 case dw_val_class_fde_ref:
8480 return DW_FORM_data;
8481 case dw_val_class_lbl_id:
8482 return (AT_index (a) == NOT_INDEXED
8483 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8484 case dw_val_class_lineptr:
8485 case dw_val_class_macptr:
8486 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8487 case dw_val_class_str:
8488 return AT_string_form (a);
8489 case dw_val_class_file:
8490 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8492 case 1:
8493 return DW_FORM_data1;
8494 case 2:
8495 return DW_FORM_data2;
8496 case 4:
8497 return DW_FORM_data4;
8498 default:
8499 gcc_unreachable ();
8502 case dw_val_class_data8:
8503 return DW_FORM_data8;
8505 case dw_val_class_high_pc:
8506 switch (DWARF2_ADDR_SIZE)
8508 case 1:
8509 return DW_FORM_data1;
8510 case 2:
8511 return DW_FORM_data2;
8512 case 4:
8513 return DW_FORM_data4;
8514 case 8:
8515 return DW_FORM_data8;
8516 default:
8517 gcc_unreachable ();
8520 default:
8521 gcc_unreachable ();
8525 /* Output the encoding of an attribute value. */
8527 static void
8528 output_value_format (dw_attr_ref a)
8530 enum dwarf_form form = value_format (a);
8532 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8535 /* Given a die and id, produce the appropriate abbreviations. */
8537 static void
8538 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8540 unsigned ix;
8541 dw_attr_ref a_attr;
8543 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8544 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8545 dwarf_tag_name (abbrev->die_tag));
8547 if (abbrev->die_child != NULL)
8548 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8549 else
8550 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8552 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8554 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8555 dwarf_attr_name (a_attr->dw_attr));
8556 output_value_format (a_attr);
8559 dw2_asm_output_data (1, 0, NULL);
8560 dw2_asm_output_data (1, 0, NULL);
8564 /* Output the .debug_abbrev section which defines the DIE abbreviation
8565 table. */
8567 static void
8568 output_abbrev_section (void)
8570 unsigned long abbrev_id;
8572 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8573 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8575 /* Terminate the table. */
8576 dw2_asm_output_data (1, 0, NULL);
8579 /* Output a symbol we can use to refer to this DIE from another CU. */
8581 static inline void
8582 output_die_symbol (dw_die_ref die)
8584 const char *sym = die->die_id.die_symbol;
8586 gcc_assert (!die->comdat_type_p);
8588 if (sym == 0)
8589 return;
8591 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8592 /* We make these global, not weak; if the target doesn't support
8593 .linkonce, it doesn't support combining the sections, so debugging
8594 will break. */
8595 targetm.asm_out.globalize_label (asm_out_file, sym);
8597 ASM_OUTPUT_LABEL (asm_out_file, sym);
8600 /* Return a new location list, given the begin and end range, and the
8601 expression. */
8603 static inline dw_loc_list_ref
8604 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8605 const char *section)
8607 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8609 retlist->begin = begin;
8610 retlist->begin_entry = NULL;
8611 retlist->end = end;
8612 retlist->expr = expr;
8613 retlist->section = section;
8615 return retlist;
8618 /* Generate a new internal symbol for this location list node, if it
8619 hasn't got one yet. */
8621 static inline void
8622 gen_llsym (dw_loc_list_ref list)
8624 gcc_assert (!list->ll_symbol);
8625 list->ll_symbol = gen_internal_sym ("LLST");
8628 /* Output the location list given to us. */
8630 static void
8631 output_loc_list (dw_loc_list_ref list_head)
8633 dw_loc_list_ref curr = list_head;
8635 if (list_head->emitted)
8636 return;
8637 list_head->emitted = true;
8639 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8641 /* Walk the location list, and output each range + expression. */
8642 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8644 unsigned long size;
8645 /* Don't output an entry that starts and ends at the same address. */
8646 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8647 continue;
8648 size = size_of_locs (curr->expr);
8649 /* If the expression is too large, drop it on the floor. We could
8650 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8651 in the expression, but >= 64KB expressions for a single value
8652 in a single range are unlikely very useful. */
8653 if (size > 0xffff)
8654 continue;
8655 if (dwarf_split_debug_info)
8657 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8658 "Location list start/length entry (%s)",
8659 list_head->ll_symbol);
8660 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8661 "Location list range start index (%s)",
8662 curr->begin);
8663 /* The length field is 4 bytes. If we ever need to support
8664 an 8-byte length, we can add a new DW_LLE code or fall back
8665 to DW_LLE_GNU_start_end_entry. */
8666 dw2_asm_output_delta (4, curr->end, curr->begin,
8667 "Location list range length (%s)",
8668 list_head->ll_symbol);
8670 else if (!have_multiple_function_sections)
8672 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8673 "Location list begin address (%s)",
8674 list_head->ll_symbol);
8675 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8676 "Location list end address (%s)",
8677 list_head->ll_symbol);
8679 else
8681 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8682 "Location list begin address (%s)",
8683 list_head->ll_symbol);
8684 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8685 "Location list end address (%s)",
8686 list_head->ll_symbol);
8689 /* Output the block length for this list of location operations. */
8690 gcc_assert (size <= 0xffff);
8691 dw2_asm_output_data (2, size, "%s", "Location expression size");
8693 output_loc_sequence (curr->expr, -1);
8696 if (dwarf_split_debug_info)
8697 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8698 "Location list terminator (%s)",
8699 list_head->ll_symbol);
8700 else
8702 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8703 "Location list terminator begin (%s)",
8704 list_head->ll_symbol);
8705 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8706 "Location list terminator end (%s)",
8707 list_head->ll_symbol);
8711 /* Output a range_list offset into the debug_range section. Emit a
8712 relocated reference if val_entry is NULL, otherwise, emit an
8713 indirect reference. */
8715 static void
8716 output_range_list_offset (dw_attr_ref a)
8718 const char *name = dwarf_attr_name (a->dw_attr);
8720 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8722 char *p = strchr (ranges_section_label, '\0');
8723 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8724 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8725 debug_ranges_section, "%s", name);
8726 *p = '\0';
8728 else
8729 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8730 "%s (offset from %s)", name, ranges_section_label);
8733 /* Output the offset into the debug_loc section. */
8735 static void
8736 output_loc_list_offset (dw_attr_ref a)
8738 char *sym = AT_loc_list (a)->ll_symbol;
8740 gcc_assert (sym);
8741 if (dwarf_split_debug_info)
8742 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8743 "%s", dwarf_attr_name (a->dw_attr));
8744 else
8745 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8746 "%s", dwarf_attr_name (a->dw_attr));
8749 /* Output an attribute's index or value appropriately. */
8751 static void
8752 output_attr_index_or_value (dw_attr_ref a)
8754 const char *name = dwarf_attr_name (a->dw_attr);
8756 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8758 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8759 return;
8761 switch (AT_class (a))
8763 case dw_val_class_addr:
8764 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8765 break;
8766 case dw_val_class_high_pc:
8767 case dw_val_class_lbl_id:
8768 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8769 break;
8770 case dw_val_class_loc_list:
8771 output_loc_list_offset (a);
8772 break;
8773 default:
8774 gcc_unreachable ();
8778 /* Output a type signature. */
8780 static inline void
8781 output_signature (const char *sig, const char *name)
8783 int i;
8785 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8786 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8789 /* Output the DIE and its attributes. Called recursively to generate
8790 the definitions of each child DIE. */
8792 static void
8793 output_die (dw_die_ref die)
8795 dw_attr_ref a;
8796 dw_die_ref c;
8797 unsigned long size;
8798 unsigned ix;
8800 /* If someone in another CU might refer to us, set up a symbol for
8801 them to point to. */
8802 if (! die->comdat_type_p && die->die_id.die_symbol)
8803 output_die_symbol (die);
8805 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8806 (unsigned long)die->die_offset,
8807 dwarf_tag_name (die->die_tag));
8809 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8811 const char *name = dwarf_attr_name (a->dw_attr);
8813 switch (AT_class (a))
8815 case dw_val_class_addr:
8816 output_attr_index_or_value (a);
8817 break;
8819 case dw_val_class_offset:
8820 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8821 "%s", name);
8822 break;
8824 case dw_val_class_range_list:
8825 output_range_list_offset (a);
8826 break;
8828 case dw_val_class_loc:
8829 size = size_of_locs (AT_loc (a));
8831 /* Output the block length for this list of location operations. */
8832 if (dwarf_version >= 4)
8833 dw2_asm_output_data_uleb128 (size, "%s", name);
8834 else
8835 dw2_asm_output_data (constant_size (size), size, "%s", name);
8837 output_loc_sequence (AT_loc (a), -1);
8838 break;
8840 case dw_val_class_const:
8841 /* ??? It would be slightly more efficient to use a scheme like is
8842 used for unsigned constants below, but gdb 4.x does not sign
8843 extend. Gdb 5.x does sign extend. */
8844 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8845 break;
8847 case dw_val_class_unsigned_const:
8849 int csize = constant_size (AT_unsigned (a));
8850 if (dwarf_version == 3
8851 && a->dw_attr == DW_AT_data_member_location
8852 && csize >= 4)
8853 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8854 else
8855 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8857 break;
8859 case dw_val_class_const_double:
8861 unsigned HOST_WIDE_INT first, second;
8863 if (HOST_BITS_PER_WIDE_INT >= 64)
8864 dw2_asm_output_data (1,
8865 HOST_BITS_PER_DOUBLE_INT
8866 / HOST_BITS_PER_CHAR,
8867 NULL);
8869 if (WORDS_BIG_ENDIAN)
8871 first = a->dw_attr_val.v.val_double.high;
8872 second = a->dw_attr_val.v.val_double.low;
8874 else
8876 first = a->dw_attr_val.v.val_double.low;
8877 second = a->dw_attr_val.v.val_double.high;
8880 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8881 first, "%s", name);
8882 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8883 second, NULL);
8885 break;
8887 case dw_val_class_wide_int:
8889 int i;
8890 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8891 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8892 if (len * HOST_BITS_PER_WIDE_INT > 64)
8893 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8894 NULL);
8896 if (WORDS_BIG_ENDIAN)
8897 for (i = len - 1; i >= 0; --i)
8899 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8900 "%s", name);
8901 name = NULL;
8903 else
8904 for (i = 0; i < len; ++i)
8906 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8907 "%s", name);
8908 name = NULL;
8911 break;
8913 case dw_val_class_vec:
8915 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8916 unsigned int len = a->dw_attr_val.v.val_vec.length;
8917 unsigned int i;
8918 unsigned char *p;
8920 dw2_asm_output_data (constant_size (len * elt_size),
8921 len * elt_size, "%s", name);
8922 if (elt_size > sizeof (HOST_WIDE_INT))
8924 elt_size /= 2;
8925 len *= 2;
8927 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8928 i < len;
8929 i++, p += elt_size)
8930 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8931 "fp or vector constant word %u", i);
8932 break;
8935 case dw_val_class_flag:
8936 if (dwarf_version >= 4)
8938 /* Currently all add_AT_flag calls pass in 1 as last argument,
8939 so DW_FORM_flag_present can be used. If that ever changes,
8940 we'll need to use DW_FORM_flag and have some optimization
8941 in build_abbrev_table that will change those to
8942 DW_FORM_flag_present if it is set to 1 in all DIEs using
8943 the same abbrev entry. */
8944 gcc_assert (AT_flag (a) == 1);
8945 if (flag_debug_asm)
8946 fprintf (asm_out_file, "\t\t\t%s %s\n",
8947 ASM_COMMENT_START, name);
8948 break;
8950 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8951 break;
8953 case dw_val_class_loc_list:
8954 output_attr_index_or_value (a);
8955 break;
8957 case dw_val_class_die_ref:
8958 if (AT_ref_external (a))
8960 if (AT_ref (a)->comdat_type_p)
8962 comdat_type_node_ref type_node =
8963 AT_ref (a)->die_id.die_type_node;
8965 gcc_assert (type_node);
8966 output_signature (type_node->signature, name);
8968 else
8970 const char *sym = AT_ref (a)->die_id.die_symbol;
8971 int size;
8973 gcc_assert (sym);
8974 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8975 length, whereas in DWARF3 it's always sized as an
8976 offset. */
8977 if (dwarf_version == 2)
8978 size = DWARF2_ADDR_SIZE;
8979 else
8980 size = DWARF_OFFSET_SIZE;
8981 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8982 name);
8985 else
8987 gcc_assert (AT_ref (a)->die_offset);
8988 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8989 "%s", name);
8991 break;
8993 case dw_val_class_fde_ref:
8995 char l1[20];
8997 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8998 a->dw_attr_val.v.val_fde_index * 2);
8999 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9000 "%s", name);
9002 break;
9004 case dw_val_class_vms_delta:
9005 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9006 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9007 AT_vms_delta2 (a), AT_vms_delta1 (a),
9008 "%s", name);
9009 #else
9010 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9011 AT_vms_delta2 (a), AT_vms_delta1 (a),
9012 "%s", name);
9013 #endif
9014 break;
9016 case dw_val_class_lbl_id:
9017 output_attr_index_or_value (a);
9018 break;
9020 case dw_val_class_lineptr:
9021 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9022 debug_line_section, "%s", name);
9023 break;
9025 case dw_val_class_macptr:
9026 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9027 debug_macinfo_section, "%s", name);
9028 break;
9030 case dw_val_class_str:
9031 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9032 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9033 a->dw_attr_val.v.val_str->label,
9034 debug_str_section,
9035 "%s: \"%s\"", name, AT_string (a));
9036 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9037 dw2_asm_output_data_uleb128 (AT_index (a),
9038 "%s: \"%s\"", name, AT_string (a));
9039 else
9040 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9041 break;
9043 case dw_val_class_file:
9045 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9047 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9048 a->dw_attr_val.v.val_file->filename);
9049 break;
9052 case dw_val_class_data8:
9054 int i;
9056 for (i = 0; i < 8; i++)
9057 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9058 i == 0 ? "%s" : NULL, name);
9059 break;
9062 case dw_val_class_high_pc:
9063 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9064 get_AT_low_pc (die), "DW_AT_high_pc");
9065 break;
9067 default:
9068 gcc_unreachable ();
9072 FOR_EACH_CHILD (die, c, output_die (c));
9074 /* Add null byte to terminate sibling list. */
9075 if (die->die_child != NULL)
9076 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9077 (unsigned long) die->die_offset);
9080 /* Output the compilation unit that appears at the beginning of the
9081 .debug_info section, and precedes the DIE descriptions. */
9083 static void
9084 output_compilation_unit_header (void)
9086 /* We don't support actual DWARFv5 units yet, we just use some
9087 DWARFv5 draft DIE tags in DWARFv4 format. */
9088 int ver = dwarf_version < 5 ? dwarf_version : 4;
9090 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9091 dw2_asm_output_data (4, 0xffffffff,
9092 "Initial length escape value indicating 64-bit DWARF extension");
9093 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9094 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9095 "Length of Compilation Unit Info");
9096 dw2_asm_output_data (2, ver, "DWARF version number");
9097 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9098 debug_abbrev_section,
9099 "Offset Into Abbrev. Section");
9100 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9103 /* Output the compilation unit DIE and its children. */
9105 static void
9106 output_comp_unit (dw_die_ref die, int output_if_empty)
9108 const char *secname, *oldsym;
9109 char *tmp;
9111 /* Unless we are outputting main CU, we may throw away empty ones. */
9112 if (!output_if_empty && die->die_child == NULL)
9113 return;
9115 /* Even if there are no children of this DIE, we must output the information
9116 about the compilation unit. Otherwise, on an empty translation unit, we
9117 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9118 will then complain when examining the file. First mark all the DIEs in
9119 this CU so we know which get local refs. */
9120 mark_dies (die);
9122 external_ref_hash_type *extern_map = optimize_external_refs (die);
9124 build_abbrev_table (die, extern_map);
9126 delete extern_map;
9128 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9129 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9130 calc_die_sizes (die);
9132 oldsym = die->die_id.die_symbol;
9133 if (oldsym)
9135 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9137 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9138 secname = tmp;
9139 die->die_id.die_symbol = NULL;
9140 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9142 else
9144 switch_to_section (debug_info_section);
9145 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9146 info_section_emitted = true;
9149 /* Output debugging information. */
9150 output_compilation_unit_header ();
9151 output_die (die);
9153 /* Leave the marks on the main CU, so we can check them in
9154 output_pubnames. */
9155 if (oldsym)
9157 unmark_dies (die);
9158 die->die_id.die_symbol = oldsym;
9162 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9163 and .debug_pubtypes. This is configured per-target, but can be
9164 overridden by the -gpubnames or -gno-pubnames options. */
9166 static inline bool
9167 want_pubnames (void)
9169 if (debug_info_level <= DINFO_LEVEL_TERSE)
9170 return false;
9171 if (debug_generate_pub_sections != -1)
9172 return debug_generate_pub_sections;
9173 return targetm.want_debug_pub_sections;
9176 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9178 static void
9179 add_AT_pubnames (dw_die_ref die)
9181 if (want_pubnames ())
9182 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9185 /* Add a string attribute value to a skeleton DIE. */
9187 static inline void
9188 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9189 const char *str)
9191 dw_attr_node attr;
9192 struct indirect_string_node *node;
9194 if (! skeleton_debug_str_hash)
9195 skeleton_debug_str_hash
9196 = hash_table<indirect_string_hasher>::create_ggc (10);
9198 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9199 find_string_form (node);
9200 if (node->form == DW_FORM_GNU_str_index)
9201 node->form = DW_FORM_strp;
9203 attr.dw_attr = attr_kind;
9204 attr.dw_attr_val.val_class = dw_val_class_str;
9205 attr.dw_attr_val.val_entry = NULL;
9206 attr.dw_attr_val.v.val_str = node;
9207 add_dwarf_attr (die, &attr);
9210 /* Helper function to generate top-level dies for skeleton debug_info and
9211 debug_types. */
9213 static void
9214 add_top_level_skeleton_die_attrs (dw_die_ref die)
9216 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9217 const char *comp_dir = comp_dir_string ();
9219 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9220 if (comp_dir != NULL)
9221 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9222 add_AT_pubnames (die);
9223 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9226 /* Output skeleton debug sections that point to the dwo file. */
9228 static void
9229 output_skeleton_debug_sections (dw_die_ref comp_unit)
9231 /* We don't support actual DWARFv5 units yet, we just use some
9232 DWARFv5 draft DIE tags in DWARFv4 format. */
9233 int ver = dwarf_version < 5 ? dwarf_version : 4;
9235 /* These attributes will be found in the full debug_info section. */
9236 remove_AT (comp_unit, DW_AT_producer);
9237 remove_AT (comp_unit, DW_AT_language);
9239 switch_to_section (debug_skeleton_info_section);
9240 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9242 /* Produce the skeleton compilation-unit header. This one differs enough from
9243 a normal CU header that it's better not to call output_compilation_unit
9244 header. */
9245 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9246 dw2_asm_output_data (4, 0xffffffff,
9247 "Initial length escape value indicating 64-bit DWARF extension");
9249 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9250 DWARF_COMPILE_UNIT_HEADER_SIZE
9251 - DWARF_INITIAL_LENGTH_SIZE
9252 + size_of_die (comp_unit),
9253 "Length of Compilation Unit Info");
9254 dw2_asm_output_data (2, ver, "DWARF version number");
9255 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9256 debug_abbrev_section,
9257 "Offset Into Abbrev. Section");
9258 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9260 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9261 output_die (comp_unit);
9263 /* Build the skeleton debug_abbrev section. */
9264 switch_to_section (debug_skeleton_abbrev_section);
9265 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9267 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9269 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9272 /* Output a comdat type unit DIE and its children. */
9274 static void
9275 output_comdat_type_unit (comdat_type_node *node)
9277 const char *secname;
9278 char *tmp;
9279 int i;
9280 #if defined (OBJECT_FORMAT_ELF)
9281 tree comdat_key;
9282 #endif
9284 /* First mark all the DIEs in this CU so we know which get local refs. */
9285 mark_dies (node->root_die);
9287 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9289 build_abbrev_table (node->root_die, extern_map);
9291 delete extern_map;
9292 extern_map = NULL;
9294 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9295 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9296 calc_die_sizes (node->root_die);
9298 #if defined (OBJECT_FORMAT_ELF)
9299 if (!dwarf_split_debug_info)
9300 secname = ".debug_types";
9301 else
9302 secname = ".debug_types.dwo";
9304 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9305 sprintf (tmp, "wt.");
9306 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9307 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9308 comdat_key = get_identifier (tmp);
9309 targetm.asm_out.named_section (secname,
9310 SECTION_DEBUG | SECTION_LINKONCE,
9311 comdat_key);
9312 #else
9313 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9314 sprintf (tmp, ".gnu.linkonce.wt.");
9315 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9316 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9317 secname = tmp;
9318 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9319 #endif
9321 /* Output debugging information. */
9322 output_compilation_unit_header ();
9323 output_signature (node->signature, "Type Signature");
9324 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9325 "Offset to Type DIE");
9326 output_die (node->root_die);
9328 unmark_dies (node->root_die);
9331 /* Return the DWARF2/3 pubname associated with a decl. */
9333 static const char *
9334 dwarf2_name (tree decl, int scope)
9336 if (DECL_NAMELESS (decl))
9337 return NULL;
9338 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9341 /* Add a new entry to .debug_pubnames if appropriate. */
9343 static void
9344 add_pubname_string (const char *str, dw_die_ref die)
9346 pubname_entry e;
9348 e.die = die;
9349 e.name = xstrdup (str);
9350 vec_safe_push (pubname_table, e);
9353 static void
9354 add_pubname (tree decl, dw_die_ref die)
9356 if (!want_pubnames ())
9357 return;
9359 /* Don't add items to the table when we expect that the consumer will have
9360 just read the enclosing die. For example, if the consumer is looking at a
9361 class_member, it will either be inside the class already, or will have just
9362 looked up the class to find the member. Either way, searching the class is
9363 faster than searching the index. */
9364 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9365 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9367 const char *name = dwarf2_name (decl, 1);
9369 if (name)
9370 add_pubname_string (name, die);
9374 /* Add an enumerator to the pubnames section. */
9376 static void
9377 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9379 pubname_entry e;
9381 gcc_assert (scope_name);
9382 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9383 e.die = die;
9384 vec_safe_push (pubname_table, e);
9387 /* Add a new entry to .debug_pubtypes if appropriate. */
9389 static void
9390 add_pubtype (tree decl, dw_die_ref die)
9392 pubname_entry e;
9394 if (!want_pubnames ())
9395 return;
9397 if ((TREE_PUBLIC (decl)
9398 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9399 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9401 tree scope = NULL;
9402 const char *scope_name = "";
9403 const char *sep = is_cxx () ? "::" : ".";
9404 const char *name;
9406 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9407 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9409 scope_name = lang_hooks.dwarf_name (scope, 1);
9410 if (scope_name != NULL && scope_name[0] != '\0')
9411 scope_name = concat (scope_name, sep, NULL);
9412 else
9413 scope_name = "";
9416 if (TYPE_P (decl))
9417 name = type_tag (decl);
9418 else
9419 name = lang_hooks.dwarf_name (decl, 1);
9421 /* If we don't have a name for the type, there's no point in adding
9422 it to the table. */
9423 if (name != NULL && name[0] != '\0')
9425 e.die = die;
9426 e.name = concat (scope_name, name, NULL);
9427 vec_safe_push (pubtype_table, e);
9430 /* Although it might be more consistent to add the pubinfo for the
9431 enumerators as their dies are created, they should only be added if the
9432 enum type meets the criteria above. So rather than re-check the parent
9433 enum type whenever an enumerator die is created, just output them all
9434 here. This isn't protected by the name conditional because anonymous
9435 enums don't have names. */
9436 if (die->die_tag == DW_TAG_enumeration_type)
9438 dw_die_ref c;
9440 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9445 /* Output a single entry in the pubnames table. */
9447 static void
9448 output_pubname (dw_offset die_offset, pubname_entry *entry)
9450 dw_die_ref die = entry->die;
9451 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9453 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9455 if (debug_generate_pub_sections == 2)
9457 /* This logic follows gdb's method for determining the value of the flag
9458 byte. */
9459 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9460 switch (die->die_tag)
9462 case DW_TAG_typedef:
9463 case DW_TAG_base_type:
9464 case DW_TAG_subrange_type:
9465 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9466 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9467 break;
9468 case DW_TAG_enumerator:
9469 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9470 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9471 if (!is_cxx () && !is_java ())
9472 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9473 break;
9474 case DW_TAG_subprogram:
9475 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9476 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9477 if (!is_ada ())
9478 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9479 break;
9480 case DW_TAG_constant:
9481 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9482 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9483 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9484 break;
9485 case DW_TAG_variable:
9486 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9487 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9488 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9489 break;
9490 case DW_TAG_namespace:
9491 case DW_TAG_imported_declaration:
9492 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9493 break;
9494 case DW_TAG_class_type:
9495 case DW_TAG_interface_type:
9496 case DW_TAG_structure_type:
9497 case DW_TAG_union_type:
9498 case DW_TAG_enumeration_type:
9499 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9500 if (!is_cxx () && !is_java ())
9501 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9502 break;
9503 default:
9504 /* An unusual tag. Leave the flag-byte empty. */
9505 break;
9507 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9508 "GDB-index flags");
9511 dw2_asm_output_nstring (entry->name, -1, "external name");
9515 /* Output the public names table used to speed up access to externally
9516 visible names; or the public types table used to find type definitions. */
9518 static void
9519 output_pubnames (vec<pubname_entry, va_gc> *names)
9521 unsigned i;
9522 unsigned long pubnames_length = size_of_pubnames (names);
9523 pubname_ref pub;
9525 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9526 dw2_asm_output_data (4, 0xffffffff,
9527 "Initial length escape value indicating 64-bit DWARF extension");
9528 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9530 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9531 dw2_asm_output_data (2, 2, "DWARF Version");
9533 if (dwarf_split_debug_info)
9534 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9535 debug_skeleton_info_section,
9536 "Offset of Compilation Unit Info");
9537 else
9538 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9539 debug_info_section,
9540 "Offset of Compilation Unit Info");
9541 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9542 "Compilation Unit Length");
9544 FOR_EACH_VEC_ELT (*names, i, pub)
9546 if (include_pubname_in_output (names, pub))
9548 dw_offset die_offset = pub->die->die_offset;
9550 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9551 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9552 gcc_assert (pub->die->die_mark);
9554 /* If we're putting types in their own .debug_types sections,
9555 the .debug_pubtypes table will still point to the compile
9556 unit (not the type unit), so we want to use the offset of
9557 the skeleton DIE (if there is one). */
9558 if (pub->die->comdat_type_p && names == pubtype_table)
9560 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9562 if (type_node != NULL)
9563 die_offset = (type_node->skeleton_die != NULL
9564 ? type_node->skeleton_die->die_offset
9565 : comp_unit_die ()->die_offset);
9568 output_pubname (die_offset, pub);
9572 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9575 /* Output public names and types tables if necessary. */
9577 static void
9578 output_pubtables (void)
9580 if (!want_pubnames () || !info_section_emitted)
9581 return;
9583 switch_to_section (debug_pubnames_section);
9584 output_pubnames (pubname_table);
9585 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9586 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9587 simply won't look for the section. */
9588 switch_to_section (debug_pubtypes_section);
9589 output_pubnames (pubtype_table);
9593 /* Output the information that goes into the .debug_aranges table.
9594 Namely, define the beginning and ending address range of the
9595 text section generated for this compilation unit. */
9597 static void
9598 output_aranges (unsigned long aranges_length)
9600 unsigned i;
9602 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9603 dw2_asm_output_data (4, 0xffffffff,
9604 "Initial length escape value indicating 64-bit DWARF extension");
9605 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9606 "Length of Address Ranges Info");
9607 /* Version number for aranges is still 2, even up to DWARF5. */
9608 dw2_asm_output_data (2, 2, "DWARF Version");
9609 if (dwarf_split_debug_info)
9610 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9611 debug_skeleton_info_section,
9612 "Offset of Compilation Unit Info");
9613 else
9614 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9615 debug_info_section,
9616 "Offset of Compilation Unit Info");
9617 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9618 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9620 /* We need to align to twice the pointer size here. */
9621 if (DWARF_ARANGES_PAD_SIZE)
9623 /* Pad using a 2 byte words so that padding is correct for any
9624 pointer size. */
9625 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9626 2 * DWARF2_ADDR_SIZE);
9627 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9628 dw2_asm_output_data (2, 0, NULL);
9631 /* It is necessary not to output these entries if the sections were
9632 not used; if the sections were not used, the length will be 0 and
9633 the address may end up as 0 if the section is discarded by ld
9634 --gc-sections, leaving an invalid (0, 0) entry that can be
9635 confused with the terminator. */
9636 if (text_section_used)
9638 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9639 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9640 text_section_label, "Length");
9642 if (cold_text_section_used)
9644 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9645 "Address");
9646 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9647 cold_text_section_label, "Length");
9650 if (have_multiple_function_sections)
9652 unsigned fde_idx;
9653 dw_fde_ref fde;
9655 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9657 if (DECL_IGNORED_P (fde->decl))
9658 continue;
9659 if (!fde->in_std_section)
9661 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9662 "Address");
9663 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9664 fde->dw_fde_begin, "Length");
9666 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9668 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9669 "Address");
9670 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9671 fde->dw_fde_second_begin, "Length");
9676 /* Output the terminator words. */
9677 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9678 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9681 /* Add a new entry to .debug_ranges. Return the offset at which it
9682 was placed. */
9684 static unsigned int
9685 add_ranges_num (int num)
9687 unsigned int in_use = ranges_table_in_use;
9689 if (in_use == ranges_table_allocated)
9691 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9692 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9693 ranges_table_allocated);
9694 memset (ranges_table + ranges_table_in_use, 0,
9695 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9698 ranges_table[in_use].num = num;
9699 ranges_table_in_use = in_use + 1;
9701 return in_use * 2 * DWARF2_ADDR_SIZE;
9704 /* Add a new entry to .debug_ranges corresponding to a block, or a
9705 range terminator if BLOCK is NULL. */
9707 static unsigned int
9708 add_ranges (const_tree block)
9710 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9713 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9714 When using dwarf_split_debug_info, address attributes in dies destined
9715 for the final executable should be direct references--setting the
9716 parameter force_direct ensures this behavior. */
9718 static void
9719 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9720 bool *added, bool force_direct)
9722 unsigned int in_use = ranges_by_label_in_use;
9723 unsigned int offset;
9725 if (in_use == ranges_by_label_allocated)
9727 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9728 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9729 ranges_by_label,
9730 ranges_by_label_allocated);
9731 memset (ranges_by_label + ranges_by_label_in_use, 0,
9732 RANGES_TABLE_INCREMENT
9733 * sizeof (struct dw_ranges_by_label_struct));
9736 ranges_by_label[in_use].begin = begin;
9737 ranges_by_label[in_use].end = end;
9738 ranges_by_label_in_use = in_use + 1;
9740 offset = add_ranges_num (-(int)in_use - 1);
9741 if (!*added)
9743 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9744 *added = true;
9748 static void
9749 output_ranges (void)
9751 unsigned i;
9752 static const char *const start_fmt = "Offset %#x";
9753 const char *fmt = start_fmt;
9755 for (i = 0; i < ranges_table_in_use; i++)
9757 int block_num = ranges_table[i].num;
9759 if (block_num > 0)
9761 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9762 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9764 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9765 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9767 /* If all code is in the text section, then the compilation
9768 unit base address defaults to DW_AT_low_pc, which is the
9769 base of the text section. */
9770 if (!have_multiple_function_sections)
9772 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9773 text_section_label,
9774 fmt, i * 2 * DWARF2_ADDR_SIZE);
9775 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9776 text_section_label, NULL);
9779 /* Otherwise, the compilation unit base address is zero,
9780 which allows us to use absolute addresses, and not worry
9781 about whether the target supports cross-section
9782 arithmetic. */
9783 else
9785 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9786 fmt, i * 2 * DWARF2_ADDR_SIZE);
9787 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9790 fmt = NULL;
9793 /* Negative block_num stands for an index into ranges_by_label. */
9794 else if (block_num < 0)
9796 int lab_idx = - block_num - 1;
9798 if (!have_multiple_function_sections)
9800 gcc_unreachable ();
9801 #if 0
9802 /* If we ever use add_ranges_by_labels () for a single
9803 function section, all we have to do is to take out
9804 the #if 0 above. */
9805 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9806 ranges_by_label[lab_idx].begin,
9807 text_section_label,
9808 fmt, i * 2 * DWARF2_ADDR_SIZE);
9809 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9810 ranges_by_label[lab_idx].end,
9811 text_section_label, NULL);
9812 #endif
9814 else
9816 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9817 ranges_by_label[lab_idx].begin,
9818 fmt, i * 2 * DWARF2_ADDR_SIZE);
9819 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9820 ranges_by_label[lab_idx].end,
9821 NULL);
9824 else
9826 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9827 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9828 fmt = start_fmt;
9833 /* Data structure containing information about input files. */
9834 struct file_info
9836 const char *path; /* Complete file name. */
9837 const char *fname; /* File name part. */
9838 int length; /* Length of entire string. */
9839 struct dwarf_file_data * file_idx; /* Index in input file table. */
9840 int dir_idx; /* Index in directory table. */
9843 /* Data structure containing information about directories with source
9844 files. */
9845 struct dir_info
9847 const char *path; /* Path including directory name. */
9848 int length; /* Path length. */
9849 int prefix; /* Index of directory entry which is a prefix. */
9850 int count; /* Number of files in this directory. */
9851 int dir_idx; /* Index of directory used as base. */
9854 /* Callback function for file_info comparison. We sort by looking at
9855 the directories in the path. */
9857 static int
9858 file_info_cmp (const void *p1, const void *p2)
9860 const struct file_info *const s1 = (const struct file_info *) p1;
9861 const struct file_info *const s2 = (const struct file_info *) p2;
9862 const unsigned char *cp1;
9863 const unsigned char *cp2;
9865 /* Take care of file names without directories. We need to make sure that
9866 we return consistent values to qsort since some will get confused if
9867 we return the same value when identical operands are passed in opposite
9868 orders. So if neither has a directory, return 0 and otherwise return
9869 1 or -1 depending on which one has the directory. */
9870 if ((s1->path == s1->fname || s2->path == s2->fname))
9871 return (s2->path == s2->fname) - (s1->path == s1->fname);
9873 cp1 = (const unsigned char *) s1->path;
9874 cp2 = (const unsigned char *) s2->path;
9876 while (1)
9878 ++cp1;
9879 ++cp2;
9880 /* Reached the end of the first path? If so, handle like above. */
9881 if ((cp1 == (const unsigned char *) s1->fname)
9882 || (cp2 == (const unsigned char *) s2->fname))
9883 return ((cp2 == (const unsigned char *) s2->fname)
9884 - (cp1 == (const unsigned char *) s1->fname));
9886 /* Character of current path component the same? */
9887 else if (*cp1 != *cp2)
9888 return *cp1 - *cp2;
9892 struct file_name_acquire_data
9894 struct file_info *files;
9895 int used_files;
9896 int max_files;
9899 /* Traversal function for the hash table. */
9902 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9904 struct dwarf_file_data *d = *slot;
9905 struct file_info *fi;
9906 const char *f;
9908 gcc_assert (fnad->max_files >= d->emitted_number);
9910 if (! d->emitted_number)
9911 return 1;
9913 gcc_assert (fnad->max_files != fnad->used_files);
9915 fi = fnad->files + fnad->used_files++;
9917 /* Skip all leading "./". */
9918 f = d->filename;
9919 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9920 f += 2;
9922 /* Create a new array entry. */
9923 fi->path = f;
9924 fi->length = strlen (f);
9925 fi->file_idx = d;
9927 /* Search for the file name part. */
9928 f = strrchr (f, DIR_SEPARATOR);
9929 #if defined (DIR_SEPARATOR_2)
9931 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9933 if (g != NULL)
9935 if (f == NULL || f < g)
9936 f = g;
9939 #endif
9941 fi->fname = f == NULL ? fi->path : f + 1;
9942 return 1;
9945 /* Output the directory table and the file name table. We try to minimize
9946 the total amount of memory needed. A heuristic is used to avoid large
9947 slowdowns with many input files. */
9949 static void
9950 output_file_names (void)
9952 struct file_name_acquire_data fnad;
9953 int numfiles;
9954 struct file_info *files;
9955 struct dir_info *dirs;
9956 int *saved;
9957 int *savehere;
9958 int *backmap;
9959 int ndirs;
9960 int idx_offset;
9961 int i;
9963 if (!last_emitted_file)
9965 dw2_asm_output_data (1, 0, "End directory table");
9966 dw2_asm_output_data (1, 0, "End file name table");
9967 return;
9970 numfiles = last_emitted_file->emitted_number;
9972 /* Allocate the various arrays we need. */
9973 files = XALLOCAVEC (struct file_info, numfiles);
9974 dirs = XALLOCAVEC (struct dir_info, numfiles);
9976 fnad.files = files;
9977 fnad.used_files = 0;
9978 fnad.max_files = numfiles;
9979 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9980 gcc_assert (fnad.used_files == fnad.max_files);
9982 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9984 /* Find all the different directories used. */
9985 dirs[0].path = files[0].path;
9986 dirs[0].length = files[0].fname - files[0].path;
9987 dirs[0].prefix = -1;
9988 dirs[0].count = 1;
9989 dirs[0].dir_idx = 0;
9990 files[0].dir_idx = 0;
9991 ndirs = 1;
9993 for (i = 1; i < numfiles; i++)
9994 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9995 && memcmp (dirs[ndirs - 1].path, files[i].path,
9996 dirs[ndirs - 1].length) == 0)
9998 /* Same directory as last entry. */
9999 files[i].dir_idx = ndirs - 1;
10000 ++dirs[ndirs - 1].count;
10002 else
10004 int j;
10006 /* This is a new directory. */
10007 dirs[ndirs].path = files[i].path;
10008 dirs[ndirs].length = files[i].fname - files[i].path;
10009 dirs[ndirs].count = 1;
10010 dirs[ndirs].dir_idx = ndirs;
10011 files[i].dir_idx = ndirs;
10013 /* Search for a prefix. */
10014 dirs[ndirs].prefix = -1;
10015 for (j = 0; j < ndirs; j++)
10016 if (dirs[j].length < dirs[ndirs].length
10017 && dirs[j].length > 1
10018 && (dirs[ndirs].prefix == -1
10019 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10020 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10021 dirs[ndirs].prefix = j;
10023 ++ndirs;
10026 /* Now to the actual work. We have to find a subset of the directories which
10027 allow expressing the file name using references to the directory table
10028 with the least amount of characters. We do not do an exhaustive search
10029 where we would have to check out every combination of every single
10030 possible prefix. Instead we use a heuristic which provides nearly optimal
10031 results in most cases and never is much off. */
10032 saved = XALLOCAVEC (int, ndirs);
10033 savehere = XALLOCAVEC (int, ndirs);
10035 memset (saved, '\0', ndirs * sizeof (saved[0]));
10036 for (i = 0; i < ndirs; i++)
10038 int j;
10039 int total;
10041 /* We can always save some space for the current directory. But this
10042 does not mean it will be enough to justify adding the directory. */
10043 savehere[i] = dirs[i].length;
10044 total = (savehere[i] - saved[i]) * dirs[i].count;
10046 for (j = i + 1; j < ndirs; j++)
10048 savehere[j] = 0;
10049 if (saved[j] < dirs[i].length)
10051 /* Determine whether the dirs[i] path is a prefix of the
10052 dirs[j] path. */
10053 int k;
10055 k = dirs[j].prefix;
10056 while (k != -1 && k != (int) i)
10057 k = dirs[k].prefix;
10059 if (k == (int) i)
10061 /* Yes it is. We can possibly save some memory by
10062 writing the filenames in dirs[j] relative to
10063 dirs[i]. */
10064 savehere[j] = dirs[i].length;
10065 total += (savehere[j] - saved[j]) * dirs[j].count;
10070 /* Check whether we can save enough to justify adding the dirs[i]
10071 directory. */
10072 if (total > dirs[i].length + 1)
10074 /* It's worthwhile adding. */
10075 for (j = i; j < ndirs; j++)
10076 if (savehere[j] > 0)
10078 /* Remember how much we saved for this directory so far. */
10079 saved[j] = savehere[j];
10081 /* Remember the prefix directory. */
10082 dirs[j].dir_idx = i;
10087 /* Emit the directory name table. */
10088 idx_offset = dirs[0].length > 0 ? 1 : 0;
10089 for (i = 1 - idx_offset; i < ndirs; i++)
10090 dw2_asm_output_nstring (dirs[i].path,
10091 dirs[i].length
10092 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10093 "Directory Entry: %#x", i + idx_offset);
10095 dw2_asm_output_data (1, 0, "End directory table");
10097 /* We have to emit them in the order of emitted_number since that's
10098 used in the debug info generation. To do this efficiently we
10099 generate a back-mapping of the indices first. */
10100 backmap = XALLOCAVEC (int, numfiles);
10101 for (i = 0; i < numfiles; i++)
10102 backmap[files[i].file_idx->emitted_number - 1] = i;
10104 /* Now write all the file names. */
10105 for (i = 0; i < numfiles; i++)
10107 int file_idx = backmap[i];
10108 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10110 #ifdef VMS_DEBUGGING_INFO
10111 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10113 /* Setting these fields can lead to debugger miscomparisons,
10114 but VMS Debug requires them to be set correctly. */
10116 int ver;
10117 long long cdt;
10118 long siz;
10119 int maxfilelen = strlen (files[file_idx].path)
10120 + dirs[dir_idx].length
10121 + MAX_VMS_VERSION_LEN + 1;
10122 char *filebuf = XALLOCAVEC (char, maxfilelen);
10124 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10125 snprintf (filebuf, maxfilelen, "%s;%d",
10126 files[file_idx].path + dirs[dir_idx].length, ver);
10128 dw2_asm_output_nstring
10129 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10131 /* Include directory index. */
10132 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10134 /* Modification time. */
10135 dw2_asm_output_data_uleb128
10136 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10137 ? cdt : 0,
10138 NULL);
10140 /* File length in bytes. */
10141 dw2_asm_output_data_uleb128
10142 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10143 ? siz : 0,
10144 NULL);
10145 #else
10146 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10147 "File Entry: %#x", (unsigned) i + 1);
10149 /* Include directory index. */
10150 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10152 /* Modification time. */
10153 dw2_asm_output_data_uleb128 (0, NULL);
10155 /* File length in bytes. */
10156 dw2_asm_output_data_uleb128 (0, NULL);
10157 #endif /* VMS_DEBUGGING_INFO */
10160 dw2_asm_output_data (1, 0, "End file name table");
10164 /* Output one line number table into the .debug_line section. */
10166 static void
10167 output_one_line_info_table (dw_line_info_table *table)
10169 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10170 unsigned int current_line = 1;
10171 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10172 dw_line_info_entry *ent;
10173 size_t i;
10175 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10177 switch (ent->opcode)
10179 case LI_set_address:
10180 /* ??? Unfortunately, we have little choice here currently, and
10181 must always use the most general form. GCC does not know the
10182 address delta itself, so we can't use DW_LNS_advance_pc. Many
10183 ports do have length attributes which will give an upper bound
10184 on the address range. We could perhaps use length attributes
10185 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10186 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10188 /* This can handle any delta. This takes
10189 4+DWARF2_ADDR_SIZE bytes. */
10190 dw2_asm_output_data (1, 0, "set address %s", line_label);
10191 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10192 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10193 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10194 break;
10196 case LI_set_line:
10197 if (ent->val == current_line)
10199 /* We still need to start a new row, so output a copy insn. */
10200 dw2_asm_output_data (1, DW_LNS_copy,
10201 "copy line %u", current_line);
10203 else
10205 int line_offset = ent->val - current_line;
10206 int line_delta = line_offset - DWARF_LINE_BASE;
10208 current_line = ent->val;
10209 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10211 /* This can handle deltas from -10 to 234, using the current
10212 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10213 This takes 1 byte. */
10214 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10215 "line %u", current_line);
10217 else
10219 /* This can handle any delta. This takes at least 4 bytes,
10220 depending on the value being encoded. */
10221 dw2_asm_output_data (1, DW_LNS_advance_line,
10222 "advance to line %u", current_line);
10223 dw2_asm_output_data_sleb128 (line_offset, NULL);
10224 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10227 break;
10229 case LI_set_file:
10230 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10231 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10232 break;
10234 case LI_set_column:
10235 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10236 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10237 break;
10239 case LI_negate_stmt:
10240 current_is_stmt = !current_is_stmt;
10241 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10242 "is_stmt %d", current_is_stmt);
10243 break;
10245 case LI_set_prologue_end:
10246 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10247 "set prologue end");
10248 break;
10250 case LI_set_epilogue_begin:
10251 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10252 "set epilogue begin");
10253 break;
10255 case LI_set_discriminator:
10256 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10257 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10258 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10259 dw2_asm_output_data_uleb128 (ent->val, NULL);
10260 break;
10264 /* Emit debug info for the address of the end of the table. */
10265 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10266 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10267 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10268 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10270 dw2_asm_output_data (1, 0, "end sequence");
10271 dw2_asm_output_data_uleb128 (1, NULL);
10272 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10275 /* Output the source line number correspondence information. This
10276 information goes into the .debug_line section. */
10278 static void
10279 output_line_info (bool prologue_only)
10281 char l1[20], l2[20], p1[20], p2[20];
10282 /* We don't support DWARFv5 line tables yet. */
10283 int ver = dwarf_version < 5 ? dwarf_version : 4;
10284 bool saw_one = false;
10285 int opc;
10287 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10288 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10289 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10290 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10292 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10293 dw2_asm_output_data (4, 0xffffffff,
10294 "Initial length escape value indicating 64-bit DWARF extension");
10295 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10296 "Length of Source Line Info");
10297 ASM_OUTPUT_LABEL (asm_out_file, l1);
10299 dw2_asm_output_data (2, ver, "DWARF Version");
10300 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10301 ASM_OUTPUT_LABEL (asm_out_file, p1);
10303 /* Define the architecture-dependent minimum instruction length (in bytes).
10304 In this implementation of DWARF, this field is used for information
10305 purposes only. Since GCC generates assembly language, we have no
10306 a priori knowledge of how many instruction bytes are generated for each
10307 source line, and therefore can use only the DW_LNE_set_address and
10308 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10309 this as '1', which is "correct enough" for all architectures,
10310 and don't let the target override. */
10311 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10313 if (ver >= 4)
10314 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10315 "Maximum Operations Per Instruction");
10316 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10317 "Default is_stmt_start flag");
10318 dw2_asm_output_data (1, DWARF_LINE_BASE,
10319 "Line Base Value (Special Opcodes)");
10320 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10321 "Line Range Value (Special Opcodes)");
10322 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10323 "Special Opcode Base");
10325 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10327 int n_op_args;
10328 switch (opc)
10330 case DW_LNS_advance_pc:
10331 case DW_LNS_advance_line:
10332 case DW_LNS_set_file:
10333 case DW_LNS_set_column:
10334 case DW_LNS_fixed_advance_pc:
10335 case DW_LNS_set_isa:
10336 n_op_args = 1;
10337 break;
10338 default:
10339 n_op_args = 0;
10340 break;
10343 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10344 opc, n_op_args);
10347 /* Write out the information about the files we use. */
10348 output_file_names ();
10349 ASM_OUTPUT_LABEL (asm_out_file, p2);
10350 if (prologue_only)
10352 /* Output the marker for the end of the line number info. */
10353 ASM_OUTPUT_LABEL (asm_out_file, l2);
10354 return;
10357 if (separate_line_info)
10359 dw_line_info_table *table;
10360 size_t i;
10362 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10363 if (table->in_use)
10365 output_one_line_info_table (table);
10366 saw_one = true;
10369 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10371 output_one_line_info_table (cold_text_section_line_info);
10372 saw_one = true;
10375 /* ??? Some Darwin linkers crash on a .debug_line section with no
10376 sequences. Further, merely a DW_LNE_end_sequence entry is not
10377 sufficient -- the address column must also be initialized.
10378 Make sure to output at least one set_address/end_sequence pair,
10379 choosing .text since that section is always present. */
10380 if (text_section_line_info->in_use || !saw_one)
10381 output_one_line_info_table (text_section_line_info);
10383 /* Output the marker for the end of the line number info. */
10384 ASM_OUTPUT_LABEL (asm_out_file, l2);
10387 /* Given a pointer to a tree node for some base type, return a pointer to
10388 a DIE that describes the given type.
10390 This routine must only be called for GCC type nodes that correspond to
10391 Dwarf base (fundamental) types. */
10393 static dw_die_ref
10394 base_type_die (tree type)
10396 dw_die_ref base_type_result;
10397 enum dwarf_type encoding;
10399 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10400 return 0;
10402 /* If this is a subtype that should not be emitted as a subrange type,
10403 use the base type. See subrange_type_for_debug_p. */
10404 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10405 type = TREE_TYPE (type);
10407 switch (TREE_CODE (type))
10409 case INTEGER_TYPE:
10410 if ((dwarf_version >= 4 || !dwarf_strict)
10411 && TYPE_NAME (type)
10412 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10413 && DECL_IS_BUILTIN (TYPE_NAME (type))
10414 && DECL_NAME (TYPE_NAME (type)))
10416 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10417 if (strcmp (name, "char16_t") == 0
10418 || strcmp (name, "char32_t") == 0)
10420 encoding = DW_ATE_UTF;
10421 break;
10424 if (TYPE_STRING_FLAG (type))
10426 if (TYPE_UNSIGNED (type))
10427 encoding = DW_ATE_unsigned_char;
10428 else
10429 encoding = DW_ATE_signed_char;
10431 else if (TYPE_UNSIGNED (type))
10432 encoding = DW_ATE_unsigned;
10433 else
10434 encoding = DW_ATE_signed;
10435 break;
10437 case REAL_TYPE:
10438 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10440 if (dwarf_version >= 3 || !dwarf_strict)
10441 encoding = DW_ATE_decimal_float;
10442 else
10443 encoding = DW_ATE_lo_user;
10445 else
10446 encoding = DW_ATE_float;
10447 break;
10449 case FIXED_POINT_TYPE:
10450 if (!(dwarf_version >= 3 || !dwarf_strict))
10451 encoding = DW_ATE_lo_user;
10452 else if (TYPE_UNSIGNED (type))
10453 encoding = DW_ATE_unsigned_fixed;
10454 else
10455 encoding = DW_ATE_signed_fixed;
10456 break;
10458 /* Dwarf2 doesn't know anything about complex ints, so use
10459 a user defined type for it. */
10460 case COMPLEX_TYPE:
10461 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10462 encoding = DW_ATE_complex_float;
10463 else
10464 encoding = DW_ATE_lo_user;
10465 break;
10467 case BOOLEAN_TYPE:
10468 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10469 encoding = DW_ATE_boolean;
10470 break;
10472 default:
10473 /* No other TREE_CODEs are Dwarf fundamental types. */
10474 gcc_unreachable ();
10477 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10479 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10480 int_size_in_bytes (type));
10481 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10482 add_pubtype (type, base_type_result);
10484 return base_type_result;
10487 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10488 named 'auto' in its type: return true for it, false otherwise. */
10490 static inline bool
10491 is_cxx_auto (tree type)
10493 if (is_cxx ())
10495 tree name = TYPE_IDENTIFIER (type);
10496 if (name == get_identifier ("auto")
10497 || name == get_identifier ("decltype(auto)"))
10498 return true;
10500 return false;
10503 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10504 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10506 static inline int
10507 is_base_type (tree type)
10509 switch (TREE_CODE (type))
10511 case ERROR_MARK:
10512 case VOID_TYPE:
10513 case INTEGER_TYPE:
10514 case REAL_TYPE:
10515 case FIXED_POINT_TYPE:
10516 case COMPLEX_TYPE:
10517 case BOOLEAN_TYPE:
10518 case POINTER_BOUNDS_TYPE:
10519 return 1;
10521 case ARRAY_TYPE:
10522 case RECORD_TYPE:
10523 case UNION_TYPE:
10524 case QUAL_UNION_TYPE:
10525 case ENUMERAL_TYPE:
10526 case FUNCTION_TYPE:
10527 case METHOD_TYPE:
10528 case POINTER_TYPE:
10529 case REFERENCE_TYPE:
10530 case NULLPTR_TYPE:
10531 case OFFSET_TYPE:
10532 case LANG_TYPE:
10533 case VECTOR_TYPE:
10534 return 0;
10536 default:
10537 if (is_cxx_auto (type))
10538 return 0;
10539 gcc_unreachable ();
10542 return 0;
10545 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10546 node, return the size in bits for the type if it is a constant, or else
10547 return the alignment for the type if the type's size is not constant, or
10548 else return BITS_PER_WORD if the type actually turns out to be an
10549 ERROR_MARK node. */
10551 static inline unsigned HOST_WIDE_INT
10552 simple_type_size_in_bits (const_tree type)
10554 if (TREE_CODE (type) == ERROR_MARK)
10555 return BITS_PER_WORD;
10556 else if (TYPE_SIZE (type) == NULL_TREE)
10557 return 0;
10558 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10559 return tree_to_uhwi (TYPE_SIZE (type));
10560 else
10561 return TYPE_ALIGN (type);
10564 /* Similarly, but return an offset_int instead of UHWI. */
10566 static inline offset_int
10567 offset_int_type_size_in_bits (const_tree type)
10569 if (TREE_CODE (type) == ERROR_MARK)
10570 return BITS_PER_WORD;
10571 else if (TYPE_SIZE (type) == NULL_TREE)
10572 return 0;
10573 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10574 return wi::to_offset (TYPE_SIZE (type));
10575 else
10576 return TYPE_ALIGN (type);
10579 /* Given a pointer to a tree node for a subrange type, return a pointer
10580 to a DIE that describes the given type. */
10582 static dw_die_ref
10583 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10585 dw_die_ref subrange_die;
10586 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10588 if (context_die == NULL)
10589 context_die = comp_unit_die ();
10591 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10593 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10595 /* The size of the subrange type and its base type do not match,
10596 so we need to generate a size attribute for the subrange type. */
10597 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10600 if (low)
10601 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10602 if (high)
10603 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10605 return subrange_die;
10608 /* Returns the (const and/or volatile) cv_qualifiers associated with
10609 the decl node. This will normally be augmented with the
10610 cv_qualifiers of the underlying type in add_type_attribute. */
10612 static int
10613 decl_quals (const_tree decl)
10615 return ((TREE_READONLY (decl)
10616 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10617 | (TREE_THIS_VOLATILE (decl)
10618 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10621 /* Determine the TYPE whose qualifiers match the largest strict subset
10622 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10623 qualifiers outside QUAL_MASK. */
10625 static int
10626 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10628 tree t;
10629 int best_rank = 0, best_qual = 0, max_rank;
10631 type_quals &= qual_mask;
10632 max_rank = popcount_hwi (type_quals) - 1;
10634 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10635 t = TYPE_NEXT_VARIANT (t))
10637 int q = TYPE_QUALS (t) & qual_mask;
10639 if ((q & type_quals) == q && q != type_quals
10640 && check_base_type (t, type))
10642 int rank = popcount_hwi (q);
10644 if (rank > best_rank)
10646 best_rank = rank;
10647 best_qual = q;
10652 return best_qual;
10655 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10656 entry that chains various modifiers in front of the given type. */
10658 static dw_die_ref
10659 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10661 enum tree_code code = TREE_CODE (type);
10662 dw_die_ref mod_type_die;
10663 dw_die_ref sub_die = NULL;
10664 tree item_type = NULL;
10665 tree qualified_type;
10666 tree name, low, high;
10667 dw_die_ref mod_scope;
10668 /* Only these cv-qualifiers are currently handled. */
10669 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10670 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10672 if (code == ERROR_MARK)
10673 return NULL;
10675 cv_quals &= cv_qual_mask;
10677 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10678 tag modifier (and not an attribute) old consumers won't be able
10679 to handle it. */
10680 if (dwarf_version < 3)
10681 cv_quals &= ~TYPE_QUAL_RESTRICT;
10683 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10684 if (dwarf_version < 5)
10685 cv_quals &= ~TYPE_QUAL_ATOMIC;
10687 /* See if we already have the appropriately qualified variant of
10688 this type. */
10689 qualified_type = get_qualified_type (type, cv_quals);
10691 if (qualified_type == sizetype
10692 && TYPE_NAME (qualified_type)
10693 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10695 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10697 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10698 && TYPE_PRECISION (t)
10699 == TYPE_PRECISION (qualified_type)
10700 && TYPE_UNSIGNED (t)
10701 == TYPE_UNSIGNED (qualified_type));
10702 qualified_type = t;
10705 /* If we do, then we can just use its DIE, if it exists. */
10706 if (qualified_type)
10708 mod_type_die = lookup_type_die (qualified_type);
10709 if (mod_type_die)
10710 return mod_type_die;
10713 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10715 /* Handle C typedef types. */
10716 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10717 && !DECL_ARTIFICIAL (name))
10719 tree dtype = TREE_TYPE (name);
10721 if (qualified_type == dtype)
10723 /* For a named type, use the typedef. */
10724 gen_type_die (qualified_type, context_die);
10725 return lookup_type_die (qualified_type);
10727 else
10729 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10730 dquals &= cv_qual_mask;
10731 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10732 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10733 /* cv-unqualified version of named type. Just use
10734 the unnamed type to which it refers. */
10735 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10736 cv_quals, context_die);
10737 /* Else cv-qualified version of named type; fall through. */
10741 mod_scope = scope_die_for (type, context_die);
10743 if (cv_quals)
10745 struct qual_info { int q; enum dwarf_tag t; };
10746 static const struct qual_info qual_info[] =
10748 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10749 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10750 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10751 { TYPE_QUAL_CONST, DW_TAG_const_type },
10753 int sub_quals;
10754 unsigned i;
10756 /* Determine a lesser qualified type that most closely matches
10757 this one. Then generate DW_TAG_* entries for the remaining
10758 qualifiers. */
10759 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10760 cv_qual_mask);
10761 mod_type_die = modified_type_die (type, sub_quals, context_die);
10763 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10764 if (qual_info[i].q & cv_quals & ~sub_quals)
10766 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10767 if (mod_type_die)
10768 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10769 mod_type_die = d;
10772 else if (use_upc_dwarf2_extensions
10773 && (cv_quals & TYPE_QUAL_UPC_SHARED))
10775 HOST_WIDE_INT block_factor = 1;
10777 /* Inside the compiler,
10778 "shared int x;" TYPE_UPC_BLOCK_FACTOR is null.
10779 "shared [] int *p;" TYPE_UPC_BLOCK_FACTOR is zero.
10780 "shared [10] int x[50];" TYPE_UPC_BLOCK_FACTOR is 10 * bitsize(int)
10781 The DWARF2 encoding is as follows:
10782 "shared int x;" DW_AT_count: 1
10783 "shared [] int *p;" <no DW_AT_count attribute>
10784 "shared [10] int x[50];" DW_AT_count: 10
10785 The logic below handles thse various contingencies. */
10787 mod_type_die = new_die (DW_TAG_upc_shared_type,
10788 comp_unit_die (), type);
10790 if (TYPE_HAS_UPC_BLOCK_FACTOR (type))
10791 block_factor = TREE_INT_CST_LOW (TYPE_UPC_BLOCK_FACTOR (type));
10793 if (block_factor != 0)
10794 add_AT_unsigned (mod_type_die, DW_AT_count, block_factor);
10796 sub_die = modified_type_die (type,
10797 cv_quals & ~TYPE_QUAL_UPC_SHARED,
10798 context_die);
10800 else if (use_upc_dwarf2_extensions && cv_quals & TYPE_QUAL_UPC_STRICT)
10802 mod_type_die = new_die (DW_TAG_upc_strict_type,
10803 comp_unit_die (), type);
10804 sub_die = modified_type_die (type,
10805 cv_quals & ~TYPE_QUAL_UPC_STRICT,
10806 context_die);
10808 else if (use_upc_dwarf2_extensions && cv_quals & TYPE_QUAL_UPC_RELAXED)
10810 mod_type_die = new_die (DW_TAG_upc_relaxed_type,
10811 comp_unit_die (), type);
10812 sub_die = modified_type_die (type,
10813 cv_quals & ~TYPE_QUAL_UPC_RELAXED,
10814 context_die);
10816 else if (code == POINTER_TYPE)
10818 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10819 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10820 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10821 item_type = TREE_TYPE (type);
10822 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10823 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10824 TYPE_ADDR_SPACE (item_type));
10826 else if (code == REFERENCE_TYPE)
10828 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10829 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10830 type);
10831 else
10832 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10833 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10834 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10835 item_type = TREE_TYPE (type);
10836 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10837 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10838 TYPE_ADDR_SPACE (item_type));
10840 else if (code == INTEGER_TYPE
10841 && TREE_TYPE (type) != NULL_TREE
10842 && subrange_type_for_debug_p (type, &low, &high))
10844 mod_type_die = subrange_type_die (type, low, high, context_die);
10845 item_type = TREE_TYPE (type);
10847 else if (is_base_type (type))
10848 mod_type_die = base_type_die (type);
10849 else
10851 gen_type_die (type, context_die);
10853 /* We have to get the type_main_variant here (and pass that to the
10854 `lookup_type_die' routine) because the ..._TYPE node we have
10855 might simply be a *copy* of some original type node (where the
10856 copy was created to help us keep track of typedef names) and
10857 that copy might have a different TYPE_UID from the original
10858 ..._TYPE node. */
10859 if (TREE_CODE (type) != VECTOR_TYPE)
10860 return lookup_type_die (type_main_variant (type));
10861 else
10862 /* Vectors have the debugging information in the type,
10863 not the main variant. */
10864 return lookup_type_die (type);
10867 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10868 don't output a DW_TAG_typedef, since there isn't one in the
10869 user's program; just attach a DW_AT_name to the type.
10870 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10871 if the base type already has the same name. */
10872 if (name
10873 && ((TREE_CODE (name) != TYPE_DECL
10874 && (qualified_type == TYPE_MAIN_VARIANT (type)
10875 || (cv_quals == TYPE_UNQUALIFIED)))
10876 || (TREE_CODE (name) == TYPE_DECL
10877 && TREE_TYPE (name) == qualified_type
10878 && DECL_NAME (name))))
10880 if (TREE_CODE (name) == TYPE_DECL)
10881 /* Could just call add_name_and_src_coords_attributes here,
10882 but since this is a builtin type it doesn't have any
10883 useful source coordinates anyway. */
10884 name = DECL_NAME (name);
10885 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10887 /* This probably indicates a bug. */
10888 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10890 name = TYPE_IDENTIFIER (type);
10891 add_name_attribute (mod_type_die,
10892 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10895 if (qualified_type)
10896 equate_type_number_to_die (qualified_type, mod_type_die);
10898 if (item_type)
10899 /* We must do this after the equate_type_number_to_die call, in case
10900 this is a recursive type. This ensures that the modified_type_die
10901 recursion will terminate even if the type is recursive. Recursive
10902 types are possible in Ada. */
10903 sub_die = modified_type_die (item_type,
10904 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10905 context_die);
10907 if (sub_die != NULL)
10908 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10910 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10911 if (TYPE_ARTIFICIAL (type))
10912 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10914 return mod_type_die;
10917 /* Generate DIEs for the generic parameters of T.
10918 T must be either a generic type or a generic function.
10919 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10921 static void
10922 gen_generic_params_dies (tree t)
10924 tree parms, args;
10925 int parms_num, i;
10926 dw_die_ref die = NULL;
10927 int non_default;
10929 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10930 return;
10932 if (TYPE_P (t))
10933 die = lookup_type_die (t);
10934 else if (DECL_P (t))
10935 die = lookup_decl_die (t);
10937 gcc_assert (die);
10939 parms = lang_hooks.get_innermost_generic_parms (t);
10940 if (!parms)
10941 /* T has no generic parameter. It means T is neither a generic type
10942 or function. End of story. */
10943 return;
10945 parms_num = TREE_VEC_LENGTH (parms);
10946 args = lang_hooks.get_innermost_generic_args (t);
10947 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10948 non_default = int_cst_value (TREE_CHAIN (args));
10949 else
10950 non_default = TREE_VEC_LENGTH (args);
10951 for (i = 0; i < parms_num; i++)
10953 tree parm, arg, arg_pack_elems;
10954 dw_die_ref parm_die;
10956 parm = TREE_VEC_ELT (parms, i);
10957 arg = TREE_VEC_ELT (args, i);
10958 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10959 gcc_assert (parm && TREE_VALUE (parm) && arg);
10961 if (parm && TREE_VALUE (parm) && arg)
10963 /* If PARM represents a template parameter pack,
10964 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10965 by DW_TAG_template_*_parameter DIEs for the argument
10966 pack elements of ARG. Note that ARG would then be
10967 an argument pack. */
10968 if (arg_pack_elems)
10969 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10970 arg_pack_elems,
10971 die);
10972 else
10973 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10974 true /* emit name */, die);
10975 if (i >= non_default)
10976 add_AT_flag (parm_die, DW_AT_default_value, 1);
10981 /* Create and return a DIE for PARM which should be
10982 the representation of a generic type parameter.
10983 For instance, in the C++ front end, PARM would be a template parameter.
10984 ARG is the argument to PARM.
10985 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10986 name of the PARM.
10987 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10988 as a child node. */
10990 static dw_die_ref
10991 generic_parameter_die (tree parm, tree arg,
10992 bool emit_name_p,
10993 dw_die_ref parent_die)
10995 dw_die_ref tmpl_die = NULL;
10996 const char *name = NULL;
10998 if (!parm || !DECL_NAME (parm) || !arg)
10999 return NULL;
11001 /* We support non-type generic parameters and arguments,
11002 type generic parameters and arguments, as well as
11003 generic generic parameters (a.k.a. template template parameters in C++)
11004 and arguments. */
11005 if (TREE_CODE (parm) == PARM_DECL)
11006 /* PARM is a nontype generic parameter */
11007 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11008 else if (TREE_CODE (parm) == TYPE_DECL)
11009 /* PARM is a type generic parameter. */
11010 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11011 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11012 /* PARM is a generic generic parameter.
11013 Its DIE is a GNU extension. It shall have a
11014 DW_AT_name attribute to represent the name of the template template
11015 parameter, and a DW_AT_GNU_template_name attribute to represent the
11016 name of the template template argument. */
11017 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11018 parent_die, parm);
11019 else
11020 gcc_unreachable ();
11022 if (tmpl_die)
11024 tree tmpl_type;
11026 /* If PARM is a generic parameter pack, it means we are
11027 emitting debug info for a template argument pack element.
11028 In other terms, ARG is a template argument pack element.
11029 In that case, we don't emit any DW_AT_name attribute for
11030 the die. */
11031 if (emit_name_p)
11033 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11034 gcc_assert (name);
11035 add_AT_string (tmpl_die, DW_AT_name, name);
11038 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11040 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11041 TMPL_DIE should have a child DW_AT_type attribute that is set
11042 to the type of the argument to PARM, which is ARG.
11043 If PARM is a type generic parameter, TMPL_DIE should have a
11044 child DW_AT_type that is set to ARG. */
11045 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11046 add_type_attribute (tmpl_die, tmpl_type,
11047 (TREE_THIS_VOLATILE (tmpl_type)
11048 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11049 parent_die);
11051 else
11053 /* So TMPL_DIE is a DIE representing a
11054 a generic generic template parameter, a.k.a template template
11055 parameter in C++ and arg is a template. */
11057 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11058 to the name of the argument. */
11059 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11060 if (name)
11061 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11064 if (TREE_CODE (parm) == PARM_DECL)
11065 /* So PARM is a non-type generic parameter.
11066 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11067 attribute of TMPL_DIE which value represents the value
11068 of ARG.
11069 We must be careful here:
11070 The value of ARG might reference some function decls.
11071 We might currently be emitting debug info for a generic
11072 type and types are emitted before function decls, we don't
11073 know if the function decls referenced by ARG will actually be
11074 emitted after cgraph computations.
11075 So must defer the generation of the DW_AT_const_value to
11076 after cgraph is ready. */
11077 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11080 return tmpl_die;
11083 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11084 PARM_PACK must be a template parameter pack. The returned DIE
11085 will be child DIE of PARENT_DIE. */
11087 static dw_die_ref
11088 template_parameter_pack_die (tree parm_pack,
11089 tree parm_pack_args,
11090 dw_die_ref parent_die)
11092 dw_die_ref die;
11093 int j;
11095 gcc_assert (parent_die && parm_pack);
11097 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11098 add_name_and_src_coords_attributes (die, parm_pack);
11099 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11100 generic_parameter_die (parm_pack,
11101 TREE_VEC_ELT (parm_pack_args, j),
11102 false /* Don't emit DW_AT_name */,
11103 die);
11104 return die;
11107 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11108 an enumerated type. */
11110 static inline int
11111 type_is_enum (const_tree type)
11113 return TREE_CODE (type) == ENUMERAL_TYPE;
11116 /* Return the DBX register number described by a given RTL node. */
11118 static unsigned int
11119 dbx_reg_number (const_rtx rtl)
11121 unsigned regno = REGNO (rtl);
11123 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11125 #ifdef LEAF_REG_REMAP
11126 if (crtl->uses_only_leaf_regs)
11128 int leaf_reg = LEAF_REG_REMAP (regno);
11129 if (leaf_reg != -1)
11130 regno = (unsigned) leaf_reg;
11132 #endif
11134 regno = DBX_REGISTER_NUMBER (regno);
11135 gcc_assert (regno != INVALID_REGNUM);
11136 return regno;
11139 /* Optionally add a DW_OP_piece term to a location description expression.
11140 DW_OP_piece is only added if the location description expression already
11141 doesn't end with DW_OP_piece. */
11143 static void
11144 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11146 dw_loc_descr_ref loc;
11148 if (*list_head != NULL)
11150 /* Find the end of the chain. */
11151 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11154 if (loc->dw_loc_opc != DW_OP_piece)
11155 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11159 /* Return a location descriptor that designates a machine register or
11160 zero if there is none. */
11162 static dw_loc_descr_ref
11163 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11165 rtx regs;
11167 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11168 return 0;
11170 /* We only use "frame base" when we're sure we're talking about the
11171 post-prologue local stack frame. We do this by *not* running
11172 register elimination until this point, and recognizing the special
11173 argument pointer and soft frame pointer rtx's.
11174 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11175 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11176 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11178 dw_loc_descr_ref result = NULL;
11180 if (dwarf_version >= 4 || !dwarf_strict)
11182 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11183 initialized);
11184 if (result)
11185 add_loc_descr (&result,
11186 new_loc_descr (DW_OP_stack_value, 0, 0));
11188 return result;
11191 regs = targetm.dwarf_register_span (rtl);
11193 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11194 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11195 else
11197 unsigned int dbx_regnum = dbx_reg_number (rtl);
11198 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11199 return 0;
11200 return one_reg_loc_descriptor (dbx_regnum, initialized);
11204 /* Return a location descriptor that designates a machine register for
11205 a given hard register number. */
11207 static dw_loc_descr_ref
11208 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11210 dw_loc_descr_ref reg_loc_descr;
11212 if (regno <= 31)
11213 reg_loc_descr
11214 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11215 else
11216 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11218 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11219 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11221 return reg_loc_descr;
11224 /* Given an RTL of a register, return a location descriptor that
11225 designates a value that spans more than one register. */
11227 static dw_loc_descr_ref
11228 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11229 enum var_init_status initialized)
11231 int size, i;
11232 dw_loc_descr_ref loc_result = NULL;
11234 /* Simple, contiguous registers. */
11235 if (regs == NULL_RTX)
11237 unsigned reg = REGNO (rtl);
11238 int nregs;
11240 #ifdef LEAF_REG_REMAP
11241 if (crtl->uses_only_leaf_regs)
11243 int leaf_reg = LEAF_REG_REMAP (reg);
11244 if (leaf_reg != -1)
11245 reg = (unsigned) leaf_reg;
11247 #endif
11249 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11250 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11252 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11254 loc_result = NULL;
11255 while (nregs--)
11257 dw_loc_descr_ref t;
11259 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11260 VAR_INIT_STATUS_INITIALIZED);
11261 add_loc_descr (&loc_result, t);
11262 add_loc_descr_op_piece (&loc_result, size);
11263 ++reg;
11265 return loc_result;
11268 /* Now onto stupid register sets in non contiguous locations. */
11270 gcc_assert (GET_CODE (regs) == PARALLEL);
11272 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11273 loc_result = NULL;
11275 for (i = 0; i < XVECLEN (regs, 0); ++i)
11277 dw_loc_descr_ref t;
11279 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11280 VAR_INIT_STATUS_INITIALIZED);
11281 add_loc_descr (&loc_result, t);
11282 add_loc_descr_op_piece (&loc_result, size);
11285 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11286 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11287 return loc_result;
11290 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11292 /* Return a location descriptor that designates a constant i,
11293 as a compound operation from constant (i >> shift), constant shift
11294 and DW_OP_shl. */
11296 static dw_loc_descr_ref
11297 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11299 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11300 add_loc_descr (&ret, int_loc_descriptor (shift));
11301 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11302 return ret;
11305 /* Return a location descriptor that designates a constant. */
11307 static dw_loc_descr_ref
11308 int_loc_descriptor (HOST_WIDE_INT i)
11310 enum dwarf_location_atom op;
11312 /* Pick the smallest representation of a constant, rather than just
11313 defaulting to the LEB encoding. */
11314 if (i >= 0)
11316 int clz = clz_hwi (i);
11317 int ctz = ctz_hwi (i);
11318 if (i <= 31)
11319 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11320 else if (i <= 0xff)
11321 op = DW_OP_const1u;
11322 else if (i <= 0xffff)
11323 op = DW_OP_const2u;
11324 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11325 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11326 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11327 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11328 while DW_OP_const4u is 5 bytes. */
11329 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11330 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11331 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11332 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11333 while DW_OP_const4u is 5 bytes. */
11334 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11335 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11336 op = DW_OP_const4u;
11337 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11338 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11339 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11340 while DW_OP_constu of constant >= 0x100000000 takes at least
11341 6 bytes. */
11342 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11343 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11344 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11345 >= HOST_BITS_PER_WIDE_INT)
11346 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11347 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11348 while DW_OP_constu takes in this case at least 6 bytes. */
11349 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11350 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11351 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11352 && size_of_uleb128 (i) > 6)
11353 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11354 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11355 else
11356 op = DW_OP_constu;
11358 else
11360 if (i >= -0x80)
11361 op = DW_OP_const1s;
11362 else if (i >= -0x8000)
11363 op = DW_OP_const2s;
11364 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11366 if (size_of_int_loc_descriptor (i) < 5)
11368 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11369 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11370 return ret;
11372 op = DW_OP_const4s;
11374 else
11376 if (size_of_int_loc_descriptor (i)
11377 < (unsigned long) 1 + size_of_sleb128 (i))
11379 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11380 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11381 return ret;
11383 op = DW_OP_consts;
11387 return new_loc_descr (op, i, 0);
11390 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11391 without actually allocating it. */
11393 static unsigned long
11394 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11396 return size_of_int_loc_descriptor (i >> shift)
11397 + size_of_int_loc_descriptor (shift)
11398 + 1;
11401 /* Return size_of_locs (int_loc_descriptor (i)) without
11402 actually allocating it. */
11404 static unsigned long
11405 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11407 unsigned long s;
11409 if (i >= 0)
11411 int clz, ctz;
11412 if (i <= 31)
11413 return 1;
11414 else if (i <= 0xff)
11415 return 2;
11416 else if (i <= 0xffff)
11417 return 3;
11418 clz = clz_hwi (i);
11419 ctz = ctz_hwi (i);
11420 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11421 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11422 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11423 - clz - 5);
11424 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11425 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11426 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11427 - clz - 8);
11428 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11429 return 5;
11430 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11431 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11432 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11433 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11434 - clz - 8);
11435 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11436 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11437 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11438 - clz - 16);
11439 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11440 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11441 && s > 6)
11442 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11443 - clz - 32);
11444 else
11445 return 1 + s;
11447 else
11449 if (i >= -0x80)
11450 return 2;
11451 else if (i >= -0x8000)
11452 return 3;
11453 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11455 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11457 s = size_of_int_loc_descriptor (-i) + 1;
11458 if (s < 5)
11459 return s;
11461 return 5;
11463 else
11465 unsigned long r = 1 + size_of_sleb128 (i);
11466 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11468 s = size_of_int_loc_descriptor (-i) + 1;
11469 if (s < r)
11470 return s;
11472 return r;
11477 /* Return loc description representing "address" of integer value.
11478 This can appear only as toplevel expression. */
11480 static dw_loc_descr_ref
11481 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11483 int litsize;
11484 dw_loc_descr_ref loc_result = NULL;
11486 if (!(dwarf_version >= 4 || !dwarf_strict))
11487 return NULL;
11489 litsize = size_of_int_loc_descriptor (i);
11490 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11491 is more compact. For DW_OP_stack_value we need:
11492 litsize + 1 (DW_OP_stack_value)
11493 and for DW_OP_implicit_value:
11494 1 (DW_OP_implicit_value) + 1 (length) + size. */
11495 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11497 loc_result = int_loc_descriptor (i);
11498 add_loc_descr (&loc_result,
11499 new_loc_descr (DW_OP_stack_value, 0, 0));
11500 return loc_result;
11503 loc_result = new_loc_descr (DW_OP_implicit_value,
11504 size, 0);
11505 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11506 loc_result->dw_loc_oprnd2.v.val_int = i;
11507 return loc_result;
11510 /* Return a location descriptor that designates a base+offset location. */
11512 static dw_loc_descr_ref
11513 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11514 enum var_init_status initialized)
11516 unsigned int regno;
11517 dw_loc_descr_ref result;
11518 dw_fde_ref fde = cfun->fde;
11520 /* We only use "frame base" when we're sure we're talking about the
11521 post-prologue local stack frame. We do this by *not* running
11522 register elimination until this point, and recognizing the special
11523 argument pointer and soft frame pointer rtx's. */
11524 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11526 rtx elim = (ira_use_lra_p
11527 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11528 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11530 if (elim != reg)
11532 if (GET_CODE (elim) == PLUS)
11534 offset += INTVAL (XEXP (elim, 1));
11535 elim = XEXP (elim, 0);
11537 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11538 && (elim == hard_frame_pointer_rtx
11539 || elim == stack_pointer_rtx))
11540 || elim == (frame_pointer_needed
11541 ? hard_frame_pointer_rtx
11542 : stack_pointer_rtx));
11544 /* If drap register is used to align stack, use frame
11545 pointer + offset to access stack variables. If stack
11546 is aligned without drap, use stack pointer + offset to
11547 access stack variables. */
11548 if (crtl->stack_realign_tried
11549 && reg == frame_pointer_rtx)
11551 int base_reg
11552 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11553 ? HARD_FRAME_POINTER_REGNUM
11554 : REGNO (elim));
11555 return new_reg_loc_descr (base_reg, offset);
11558 gcc_assert (frame_pointer_fb_offset_valid);
11559 offset += frame_pointer_fb_offset;
11560 return new_loc_descr (DW_OP_fbreg, offset, 0);
11564 regno = REGNO (reg);
11565 #ifdef LEAF_REG_REMAP
11566 if (crtl->uses_only_leaf_regs)
11568 int leaf_reg = LEAF_REG_REMAP (regno);
11569 if (leaf_reg != -1)
11570 regno = (unsigned) leaf_reg;
11572 #endif
11573 regno = DWARF_FRAME_REGNUM (regno);
11575 if (!optimize && fde
11576 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11578 /* Use cfa+offset to represent the location of arguments passed
11579 on the stack when drap is used to align stack.
11580 Only do this when not optimizing, for optimized code var-tracking
11581 is supposed to track where the arguments live and the register
11582 used as vdrap or drap in some spot might be used for something
11583 else in other part of the routine. */
11584 return new_loc_descr (DW_OP_fbreg, offset, 0);
11587 if (regno <= 31)
11588 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11589 offset, 0);
11590 else
11591 result = new_loc_descr (DW_OP_bregx, regno, offset);
11593 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11594 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11596 return result;
11599 /* Return true if this RTL expression describes a base+offset calculation. */
11601 static inline int
11602 is_based_loc (const_rtx rtl)
11604 return (GET_CODE (rtl) == PLUS
11605 && ((REG_P (XEXP (rtl, 0))
11606 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11607 && CONST_INT_P (XEXP (rtl, 1)))));
11610 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11611 failed. */
11613 static dw_loc_descr_ref
11614 tls_mem_loc_descriptor (rtx mem)
11616 tree base;
11617 dw_loc_descr_ref loc_result;
11619 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11620 return NULL;
11622 base = get_base_address (MEM_EXPR (mem));
11623 if (base == NULL
11624 || TREE_CODE (base) != VAR_DECL
11625 || !DECL_THREAD_LOCAL_P (base))
11626 return NULL;
11628 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11629 if (loc_result == NULL)
11630 return NULL;
11632 if (MEM_OFFSET (mem))
11633 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11635 return loc_result;
11638 /* Output debug info about reason why we failed to expand expression as dwarf
11639 expression. */
11641 static void
11642 expansion_failed (tree expr, rtx rtl, char const *reason)
11644 if (dump_file && (dump_flags & TDF_DETAILS))
11646 fprintf (dump_file, "Failed to expand as dwarf: ");
11647 if (expr)
11648 print_generic_expr (dump_file, expr, dump_flags);
11649 if (rtl)
11651 fprintf (dump_file, "\n");
11652 print_rtl (dump_file, rtl);
11654 fprintf (dump_file, "\nReason: %s\n", reason);
11658 /* Helper function for const_ok_for_output. */
11660 static bool
11661 const_ok_for_output_1 (rtx rtl)
11663 if (GET_CODE (rtl) == UNSPEC)
11665 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11666 we can't express it in the debug info. */
11667 #ifdef ENABLE_CHECKING
11668 /* Don't complain about TLS UNSPECs, those are just too hard to
11669 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11670 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11671 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11672 if (XVECLEN (rtl, 0) == 0
11673 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11674 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11675 inform (current_function_decl
11676 ? DECL_SOURCE_LOCATION (current_function_decl)
11677 : UNKNOWN_LOCATION,
11678 #if NUM_UNSPEC_VALUES > 0
11679 "non-delegitimized UNSPEC %s (%d) found in variable location",
11680 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11681 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11682 XINT (rtl, 1));
11683 #else
11684 "non-delegitimized UNSPEC %d found in variable location",
11685 XINT (rtl, 1));
11686 #endif
11687 #endif
11688 expansion_failed (NULL_TREE, rtl,
11689 "UNSPEC hasn't been delegitimized.\n");
11690 return false;
11693 if (targetm.const_not_ok_for_debug_p (rtl))
11695 expansion_failed (NULL_TREE, rtl,
11696 "Expression rejected for debug by the backend.\n");
11697 return false;
11700 /* FIXME: Refer to PR60655. It is possible for simplification
11701 of rtl expressions in var tracking to produce such expressions.
11702 We should really identify / validate expressions
11703 enclosed in CONST that can be handled by assemblers on various
11704 targets and only handle legitimate cases here. */
11705 if (GET_CODE (rtl) != SYMBOL_REF)
11707 if (GET_CODE (rtl) == NOT)
11708 return false;
11709 return true;
11712 if (CONSTANT_POOL_ADDRESS_P (rtl))
11714 bool marked;
11715 get_pool_constant_mark (rtl, &marked);
11716 /* If all references to this pool constant were optimized away,
11717 it was not output and thus we can't represent it. */
11718 if (!marked)
11720 expansion_failed (NULL_TREE, rtl,
11721 "Constant was removed from constant pool.\n");
11722 return false;
11726 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11727 return false;
11729 /* Avoid references to external symbols in debug info, on several targets
11730 the linker might even refuse to link when linking a shared library,
11731 and in many other cases the relocations for .debug_info/.debug_loc are
11732 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11733 to be defined within the same shared library or executable are fine. */
11734 if (SYMBOL_REF_EXTERNAL_P (rtl))
11736 tree decl = SYMBOL_REF_DECL (rtl);
11738 if (decl == NULL || !targetm.binds_local_p (decl))
11740 expansion_failed (NULL_TREE, rtl,
11741 "Symbol not defined in current TU.\n");
11742 return false;
11746 return true;
11749 /* Return true if constant RTL can be emitted in DW_OP_addr or
11750 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11751 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11753 static bool
11754 const_ok_for_output (rtx rtl)
11756 if (GET_CODE (rtl) == SYMBOL_REF)
11757 return const_ok_for_output_1 (rtl);
11759 if (GET_CODE (rtl) == CONST)
11761 subrtx_var_iterator::array_type array;
11762 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11763 if (!const_ok_for_output_1 (*iter))
11764 return false;
11765 return true;
11768 return true;
11771 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11772 if possible, NULL otherwise. */
11774 static dw_die_ref
11775 base_type_for_mode (machine_mode mode, bool unsignedp)
11777 dw_die_ref type_die;
11778 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11780 if (type == NULL)
11781 return NULL;
11782 switch (TREE_CODE (type))
11784 case INTEGER_TYPE:
11785 case REAL_TYPE:
11786 break;
11787 default:
11788 return NULL;
11790 type_die = lookup_type_die (type);
11791 if (!type_die)
11792 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11793 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11794 return NULL;
11795 return type_die;
11798 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11799 type matching MODE, or, if MODE is narrower than or as wide as
11800 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11801 possible. */
11803 static dw_loc_descr_ref
11804 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11806 machine_mode outer_mode = mode;
11807 dw_die_ref type_die;
11808 dw_loc_descr_ref cvt;
11810 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11812 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11813 return op;
11815 type_die = base_type_for_mode (outer_mode, 1);
11816 if (type_die == NULL)
11817 return NULL;
11818 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11819 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11820 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11821 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11822 add_loc_descr (&op, cvt);
11823 return op;
11826 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11828 static dw_loc_descr_ref
11829 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11830 dw_loc_descr_ref op1)
11832 dw_loc_descr_ref ret = op0;
11833 add_loc_descr (&ret, op1);
11834 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11835 if (STORE_FLAG_VALUE != 1)
11837 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11838 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11840 return ret;
11843 /* Return location descriptor for signed comparison OP RTL. */
11845 static dw_loc_descr_ref
11846 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11847 machine_mode mem_mode)
11849 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11850 dw_loc_descr_ref op0, op1;
11851 int shift;
11853 if (op_mode == VOIDmode)
11854 op_mode = GET_MODE (XEXP (rtl, 1));
11855 if (op_mode == VOIDmode)
11856 return NULL;
11858 if (dwarf_strict
11859 && (GET_MODE_CLASS (op_mode) != MODE_INT
11860 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11861 return NULL;
11863 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11864 VAR_INIT_STATUS_INITIALIZED);
11865 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11866 VAR_INIT_STATUS_INITIALIZED);
11868 if (op0 == NULL || op1 == NULL)
11869 return NULL;
11871 if (GET_MODE_CLASS (op_mode) != MODE_INT
11872 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11873 return compare_loc_descriptor (op, op0, op1);
11875 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11877 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11878 dw_loc_descr_ref cvt;
11880 if (type_die == NULL)
11881 return NULL;
11882 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11883 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11884 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11885 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11886 add_loc_descr (&op0, cvt);
11887 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11888 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11889 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11890 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11891 add_loc_descr (&op1, cvt);
11892 return compare_loc_descriptor (op, op0, op1);
11895 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11896 /* For eq/ne, if the operands are known to be zero-extended,
11897 there is no need to do the fancy shifting up. */
11898 if (op == DW_OP_eq || op == DW_OP_ne)
11900 dw_loc_descr_ref last0, last1;
11901 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11903 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11905 /* deref_size zero extends, and for constants we can check
11906 whether they are zero extended or not. */
11907 if (((last0->dw_loc_opc == DW_OP_deref_size
11908 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11909 || (CONST_INT_P (XEXP (rtl, 0))
11910 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11911 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11912 && ((last1->dw_loc_opc == DW_OP_deref_size
11913 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11914 || (CONST_INT_P (XEXP (rtl, 1))
11915 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11916 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11917 return compare_loc_descriptor (op, op0, op1);
11919 /* EQ/NE comparison against constant in narrower type than
11920 DWARF2_ADDR_SIZE can be performed either as
11921 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11922 DW_OP_{eq,ne}
11924 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11925 DW_OP_{eq,ne}. Pick whatever is shorter. */
11926 if (CONST_INT_P (XEXP (rtl, 1))
11927 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11928 && (size_of_int_loc_descriptor (shift) + 1
11929 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11930 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11931 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11932 & GET_MODE_MASK (op_mode))))
11934 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11935 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11936 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11937 & GET_MODE_MASK (op_mode));
11938 return compare_loc_descriptor (op, op0, op1);
11941 add_loc_descr (&op0, int_loc_descriptor (shift));
11942 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11943 if (CONST_INT_P (XEXP (rtl, 1)))
11944 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11945 else
11947 add_loc_descr (&op1, int_loc_descriptor (shift));
11948 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11950 return compare_loc_descriptor (op, op0, op1);
11953 /* Return location descriptor for unsigned comparison OP RTL. */
11955 static dw_loc_descr_ref
11956 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11957 machine_mode mem_mode)
11959 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11960 dw_loc_descr_ref op0, op1;
11962 if (op_mode == VOIDmode)
11963 op_mode = GET_MODE (XEXP (rtl, 1));
11964 if (op_mode == VOIDmode)
11965 return NULL;
11966 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11967 return NULL;
11969 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11970 return NULL;
11972 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11973 VAR_INIT_STATUS_INITIALIZED);
11974 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11975 VAR_INIT_STATUS_INITIALIZED);
11977 if (op0 == NULL || op1 == NULL)
11978 return NULL;
11980 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11982 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11983 dw_loc_descr_ref last0, last1;
11984 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11986 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11988 if (CONST_INT_P (XEXP (rtl, 0)))
11989 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11990 /* deref_size zero extends, so no need to mask it again. */
11991 else if (last0->dw_loc_opc != DW_OP_deref_size
11992 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11994 add_loc_descr (&op0, int_loc_descriptor (mask));
11995 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11997 if (CONST_INT_P (XEXP (rtl, 1)))
11998 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11999 /* deref_size zero extends, so no need to mask it again. */
12000 else if (last1->dw_loc_opc != DW_OP_deref_size
12001 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12003 add_loc_descr (&op1, int_loc_descriptor (mask));
12004 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12007 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12009 HOST_WIDE_INT bias = 1;
12010 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12011 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12012 if (CONST_INT_P (XEXP (rtl, 1)))
12013 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12014 + INTVAL (XEXP (rtl, 1)));
12015 else
12016 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12017 bias, 0));
12019 return compare_loc_descriptor (op, op0, op1);
12022 /* Return location descriptor for {U,S}{MIN,MAX}. */
12024 static dw_loc_descr_ref
12025 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12026 machine_mode mem_mode)
12028 enum dwarf_location_atom op;
12029 dw_loc_descr_ref op0, op1, ret;
12030 dw_loc_descr_ref bra_node, drop_node;
12032 if (dwarf_strict
12033 && (GET_MODE_CLASS (mode) != MODE_INT
12034 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12035 return NULL;
12037 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12038 VAR_INIT_STATUS_INITIALIZED);
12039 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12040 VAR_INIT_STATUS_INITIALIZED);
12042 if (op0 == NULL || op1 == NULL)
12043 return NULL;
12045 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12046 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12047 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12048 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12050 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12052 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12053 add_loc_descr (&op0, int_loc_descriptor (mask));
12054 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12055 add_loc_descr (&op1, int_loc_descriptor (mask));
12056 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12058 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12060 HOST_WIDE_INT bias = 1;
12061 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12062 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12063 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12066 else if (GET_MODE_CLASS (mode) == MODE_INT
12067 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12069 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12070 add_loc_descr (&op0, int_loc_descriptor (shift));
12071 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12072 add_loc_descr (&op1, int_loc_descriptor (shift));
12073 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12075 else if (GET_MODE_CLASS (mode) == MODE_INT
12076 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12078 dw_die_ref type_die = base_type_for_mode (mode, 0);
12079 dw_loc_descr_ref cvt;
12080 if (type_die == NULL)
12081 return NULL;
12082 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12083 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12084 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12085 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12086 add_loc_descr (&op0, cvt);
12087 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12088 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12089 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12090 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12091 add_loc_descr (&op1, cvt);
12094 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12095 op = DW_OP_lt;
12096 else
12097 op = DW_OP_gt;
12098 ret = op0;
12099 add_loc_descr (&ret, op1);
12100 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12101 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12102 add_loc_descr (&ret, bra_node);
12103 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12104 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12105 add_loc_descr (&ret, drop_node);
12106 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12107 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12108 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12109 && GET_MODE_CLASS (mode) == MODE_INT
12110 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12111 ret = convert_descriptor_to_mode (mode, ret);
12112 return ret;
12115 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12116 but after converting arguments to type_die, afterwards
12117 convert back to unsigned. */
12119 static dw_loc_descr_ref
12120 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12121 machine_mode mode, machine_mode mem_mode)
12123 dw_loc_descr_ref cvt, op0, op1;
12125 if (type_die == NULL)
12126 return NULL;
12127 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12128 VAR_INIT_STATUS_INITIALIZED);
12129 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12130 VAR_INIT_STATUS_INITIALIZED);
12131 if (op0 == NULL || op1 == NULL)
12132 return NULL;
12133 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12134 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12135 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12136 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12137 add_loc_descr (&op0, cvt);
12138 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12139 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12140 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12141 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12142 add_loc_descr (&op1, cvt);
12143 add_loc_descr (&op0, op1);
12144 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12145 return convert_descriptor_to_mode (mode, op0);
12148 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12149 const0 is DW_OP_lit0 or corresponding typed constant,
12150 const1 is DW_OP_lit1 or corresponding typed constant
12151 and constMSB is constant with just the MSB bit set
12152 for the mode):
12153 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12154 L1: const0 DW_OP_swap
12155 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12156 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12157 L3: DW_OP_drop
12158 L4: DW_OP_nop
12160 CTZ is similar:
12161 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12162 L1: const0 DW_OP_swap
12163 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12164 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12165 L3: DW_OP_drop
12166 L4: DW_OP_nop
12168 FFS is similar:
12169 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12170 L1: const1 DW_OP_swap
12171 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12172 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12173 L3: DW_OP_drop
12174 L4: DW_OP_nop */
12176 static dw_loc_descr_ref
12177 clz_loc_descriptor (rtx rtl, machine_mode mode,
12178 machine_mode mem_mode)
12180 dw_loc_descr_ref op0, ret, tmp;
12181 HOST_WIDE_INT valv;
12182 dw_loc_descr_ref l1jump, l1label;
12183 dw_loc_descr_ref l2jump, l2label;
12184 dw_loc_descr_ref l3jump, l3label;
12185 dw_loc_descr_ref l4jump, l4label;
12186 rtx msb;
12188 if (GET_MODE_CLASS (mode) != MODE_INT
12189 || GET_MODE (XEXP (rtl, 0)) != mode)
12190 return NULL;
12192 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12193 VAR_INIT_STATUS_INITIALIZED);
12194 if (op0 == NULL)
12195 return NULL;
12196 ret = op0;
12197 if (GET_CODE (rtl) == CLZ)
12199 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12200 valv = GET_MODE_BITSIZE (mode);
12202 else if (GET_CODE (rtl) == FFS)
12203 valv = 0;
12204 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12205 valv = GET_MODE_BITSIZE (mode);
12206 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12207 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12208 add_loc_descr (&ret, l1jump);
12209 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12210 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12211 VAR_INIT_STATUS_INITIALIZED);
12212 if (tmp == NULL)
12213 return NULL;
12214 add_loc_descr (&ret, tmp);
12215 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12216 add_loc_descr (&ret, l4jump);
12217 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12218 ? const1_rtx : const0_rtx,
12219 mode, mem_mode,
12220 VAR_INIT_STATUS_INITIALIZED);
12221 if (l1label == NULL)
12222 return NULL;
12223 add_loc_descr (&ret, l1label);
12224 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12225 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12226 add_loc_descr (&ret, l2label);
12227 if (GET_CODE (rtl) != CLZ)
12228 msb = const1_rtx;
12229 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12230 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12231 << (GET_MODE_BITSIZE (mode) - 1));
12232 else
12233 msb = immed_wide_int_const
12234 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12235 GET_MODE_PRECISION (mode)), mode);
12236 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12237 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12238 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12239 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12240 else
12241 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12242 VAR_INIT_STATUS_INITIALIZED);
12243 if (tmp == NULL)
12244 return NULL;
12245 add_loc_descr (&ret, tmp);
12246 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12247 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12248 add_loc_descr (&ret, l3jump);
12249 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12250 VAR_INIT_STATUS_INITIALIZED);
12251 if (tmp == NULL)
12252 return NULL;
12253 add_loc_descr (&ret, tmp);
12254 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12255 ? DW_OP_shl : DW_OP_shr, 0, 0));
12256 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12257 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12258 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12259 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12260 add_loc_descr (&ret, l2jump);
12261 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12262 add_loc_descr (&ret, l3label);
12263 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12264 add_loc_descr (&ret, l4label);
12265 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12266 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12267 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12268 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12269 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12270 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12271 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12272 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12273 return ret;
12276 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12277 const1 is DW_OP_lit1 or corresponding typed constant):
12278 const0 DW_OP_swap
12279 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12280 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12281 L2: DW_OP_drop
12283 PARITY is similar:
12284 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12285 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12286 L2: DW_OP_drop */
12288 static dw_loc_descr_ref
12289 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12290 machine_mode mem_mode)
12292 dw_loc_descr_ref op0, ret, tmp;
12293 dw_loc_descr_ref l1jump, l1label;
12294 dw_loc_descr_ref l2jump, l2label;
12296 if (GET_MODE_CLASS (mode) != MODE_INT
12297 || GET_MODE (XEXP (rtl, 0)) != mode)
12298 return NULL;
12300 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12301 VAR_INIT_STATUS_INITIALIZED);
12302 if (op0 == NULL)
12303 return NULL;
12304 ret = op0;
12305 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12306 VAR_INIT_STATUS_INITIALIZED);
12307 if (tmp == NULL)
12308 return NULL;
12309 add_loc_descr (&ret, tmp);
12310 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12311 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12312 add_loc_descr (&ret, l1label);
12313 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12314 add_loc_descr (&ret, l2jump);
12315 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12316 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12317 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12318 VAR_INIT_STATUS_INITIALIZED);
12319 if (tmp == NULL)
12320 return NULL;
12321 add_loc_descr (&ret, tmp);
12322 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12323 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12324 ? DW_OP_plus : DW_OP_xor, 0, 0));
12325 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12326 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12327 VAR_INIT_STATUS_INITIALIZED);
12328 add_loc_descr (&ret, tmp);
12329 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12330 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12331 add_loc_descr (&ret, l1jump);
12332 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12333 add_loc_descr (&ret, l2label);
12334 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12335 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12336 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12337 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12338 return ret;
12341 /* BSWAP (constS is initial shift count, either 56 or 24):
12342 constS const0
12343 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12344 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12345 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12346 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12347 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12349 static dw_loc_descr_ref
12350 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12351 machine_mode mem_mode)
12353 dw_loc_descr_ref op0, ret, tmp;
12354 dw_loc_descr_ref l1jump, l1label;
12355 dw_loc_descr_ref l2jump, l2label;
12357 if (GET_MODE_CLASS (mode) != MODE_INT
12358 || BITS_PER_UNIT != 8
12359 || (GET_MODE_BITSIZE (mode) != 32
12360 && GET_MODE_BITSIZE (mode) != 64))
12361 return NULL;
12363 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12364 VAR_INIT_STATUS_INITIALIZED);
12365 if (op0 == NULL)
12366 return NULL;
12368 ret = op0;
12369 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12370 mode, mem_mode,
12371 VAR_INIT_STATUS_INITIALIZED);
12372 if (tmp == NULL)
12373 return NULL;
12374 add_loc_descr (&ret, tmp);
12375 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12376 VAR_INIT_STATUS_INITIALIZED);
12377 if (tmp == NULL)
12378 return NULL;
12379 add_loc_descr (&ret, tmp);
12380 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12381 add_loc_descr (&ret, l1label);
12382 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12383 mode, mem_mode,
12384 VAR_INIT_STATUS_INITIALIZED);
12385 add_loc_descr (&ret, tmp);
12386 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12387 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12388 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12389 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12390 VAR_INIT_STATUS_INITIALIZED);
12391 if (tmp == NULL)
12392 return NULL;
12393 add_loc_descr (&ret, tmp);
12394 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12395 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12396 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12397 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12398 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12399 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12400 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12401 VAR_INIT_STATUS_INITIALIZED);
12402 add_loc_descr (&ret, tmp);
12403 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12404 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12405 add_loc_descr (&ret, l2jump);
12406 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12407 VAR_INIT_STATUS_INITIALIZED);
12408 add_loc_descr (&ret, tmp);
12409 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12410 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12411 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12412 add_loc_descr (&ret, l1jump);
12413 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12414 add_loc_descr (&ret, l2label);
12415 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12416 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12417 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12418 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12419 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12420 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12421 return ret;
12424 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12425 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12426 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12427 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12429 ROTATERT is similar:
12430 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12431 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12432 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12434 static dw_loc_descr_ref
12435 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12436 machine_mode mem_mode)
12438 rtx rtlop1 = XEXP (rtl, 1);
12439 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12440 int i;
12442 if (GET_MODE_CLASS (mode) != MODE_INT)
12443 return NULL;
12445 if (GET_MODE (rtlop1) != VOIDmode
12446 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12447 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12448 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12449 VAR_INIT_STATUS_INITIALIZED);
12450 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12451 VAR_INIT_STATUS_INITIALIZED);
12452 if (op0 == NULL || op1 == NULL)
12453 return NULL;
12454 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12455 for (i = 0; i < 2; i++)
12457 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12458 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12459 mode, mem_mode,
12460 VAR_INIT_STATUS_INITIALIZED);
12461 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12462 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12463 ? DW_OP_const4u
12464 : HOST_BITS_PER_WIDE_INT == 64
12465 ? DW_OP_const8u : DW_OP_constu,
12466 GET_MODE_MASK (mode), 0);
12467 else
12468 mask[i] = NULL;
12469 if (mask[i] == NULL)
12470 return NULL;
12471 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12473 ret = op0;
12474 add_loc_descr (&ret, op1);
12475 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12476 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12477 if (GET_CODE (rtl) == ROTATERT)
12479 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12480 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12481 GET_MODE_BITSIZE (mode), 0));
12483 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12484 if (mask[0] != NULL)
12485 add_loc_descr (&ret, mask[0]);
12486 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12487 if (mask[1] != NULL)
12489 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12490 add_loc_descr (&ret, mask[1]);
12491 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12493 if (GET_CODE (rtl) == ROTATE)
12495 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12496 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12497 GET_MODE_BITSIZE (mode), 0));
12499 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12500 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12501 return ret;
12504 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12505 for DEBUG_PARAMETER_REF RTL. */
12507 static dw_loc_descr_ref
12508 parameter_ref_descriptor (rtx rtl)
12510 dw_loc_descr_ref ret;
12511 dw_die_ref ref;
12513 if (dwarf_strict)
12514 return NULL;
12515 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12516 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12517 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12518 if (ref)
12520 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12521 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12522 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12524 else
12526 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12527 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12529 return ret;
12532 /* The following routine converts the RTL for a variable or parameter
12533 (resident in memory) into an equivalent Dwarf representation of a
12534 mechanism for getting the address of that same variable onto the top of a
12535 hypothetical "address evaluation" stack.
12537 When creating memory location descriptors, we are effectively transforming
12538 the RTL for a memory-resident object into its Dwarf postfix expression
12539 equivalent. This routine recursively descends an RTL tree, turning
12540 it into Dwarf postfix code as it goes.
12542 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12544 MEM_MODE is the mode of the memory reference, needed to handle some
12545 autoincrement addressing modes.
12547 Return 0 if we can't represent the location. */
12549 dw_loc_descr_ref
12550 mem_loc_descriptor (rtx rtl, machine_mode mode,
12551 machine_mode mem_mode,
12552 enum var_init_status initialized)
12554 dw_loc_descr_ref mem_loc_result = NULL;
12555 enum dwarf_location_atom op;
12556 dw_loc_descr_ref op0, op1;
12557 rtx inner = NULL_RTX;
12559 if (mode == VOIDmode)
12560 mode = GET_MODE (rtl);
12562 /* Note that for a dynamically sized array, the location we will generate a
12563 description of here will be the lowest numbered location which is
12564 actually within the array. That's *not* necessarily the same as the
12565 zeroth element of the array. */
12567 rtl = targetm.delegitimize_address (rtl);
12569 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12570 return NULL;
12572 switch (GET_CODE (rtl))
12574 case POST_INC:
12575 case POST_DEC:
12576 case POST_MODIFY:
12577 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12579 case SUBREG:
12580 /* The case of a subreg may arise when we have a local (register)
12581 variable or a formal (register) parameter which doesn't quite fill
12582 up an entire register. For now, just assume that it is
12583 legitimate to make the Dwarf info refer to the whole register which
12584 contains the given subreg. */
12585 if (!subreg_lowpart_p (rtl))
12586 break;
12587 inner = SUBREG_REG (rtl);
12588 case TRUNCATE:
12589 if (inner == NULL_RTX)
12590 inner = XEXP (rtl, 0);
12591 if (GET_MODE_CLASS (mode) == MODE_INT
12592 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12593 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12594 #ifdef POINTERS_EXTEND_UNSIGNED
12595 || (mode == Pmode && mem_mode != VOIDmode)
12596 #endif
12598 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12600 mem_loc_result = mem_loc_descriptor (inner,
12601 GET_MODE (inner),
12602 mem_mode, initialized);
12603 break;
12605 if (dwarf_strict)
12606 break;
12607 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12608 break;
12609 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12610 && (GET_MODE_CLASS (mode) != MODE_INT
12611 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12612 break;
12613 else
12615 dw_die_ref type_die;
12616 dw_loc_descr_ref cvt;
12618 mem_loc_result = mem_loc_descriptor (inner,
12619 GET_MODE (inner),
12620 mem_mode, initialized);
12621 if (mem_loc_result == NULL)
12622 break;
12623 type_die = base_type_for_mode (mode,
12624 GET_MODE_CLASS (mode) == MODE_INT);
12625 if (type_die == NULL)
12627 mem_loc_result = NULL;
12628 break;
12630 if (GET_MODE_SIZE (mode)
12631 != GET_MODE_SIZE (GET_MODE (inner)))
12632 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12633 else
12634 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12635 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12636 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12637 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12638 add_loc_descr (&mem_loc_result, cvt);
12640 break;
12642 case REG:
12643 if (GET_MODE_CLASS (mode) != MODE_INT
12644 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12645 && rtl != arg_pointer_rtx
12646 && rtl != frame_pointer_rtx
12647 #ifdef POINTERS_EXTEND_UNSIGNED
12648 && (mode != Pmode || mem_mode == VOIDmode)
12649 #endif
12652 dw_die_ref type_die;
12653 unsigned int dbx_regnum;
12655 if (dwarf_strict)
12656 break;
12657 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12658 break;
12659 type_die = base_type_for_mode (mode,
12660 GET_MODE_CLASS (mode) == MODE_INT);
12661 if (type_die == NULL)
12662 break;
12664 dbx_regnum = dbx_reg_number (rtl);
12665 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12666 break;
12667 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12668 dbx_regnum, 0);
12669 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12670 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12671 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12672 break;
12674 /* Whenever a register number forms a part of the description of the
12675 method for calculating the (dynamic) address of a memory resident
12676 object, DWARF rules require the register number be referred to as
12677 a "base register". This distinction is not based in any way upon
12678 what category of register the hardware believes the given register
12679 belongs to. This is strictly DWARF terminology we're dealing with
12680 here. Note that in cases where the location of a memory-resident
12681 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12682 OP_CONST (0)) the actual DWARF location descriptor that we generate
12683 may just be OP_BASEREG (basereg). This may look deceptively like
12684 the object in question was allocated to a register (rather than in
12685 memory) so DWARF consumers need to be aware of the subtle
12686 distinction between OP_REG and OP_BASEREG. */
12687 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12688 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12689 else if (stack_realign_drap
12690 && crtl->drap_reg
12691 && crtl->args.internal_arg_pointer == rtl
12692 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12694 /* If RTL is internal_arg_pointer, which has been optimized
12695 out, use DRAP instead. */
12696 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12697 VAR_INIT_STATUS_INITIALIZED);
12699 break;
12701 case SIGN_EXTEND:
12702 case ZERO_EXTEND:
12703 if (GET_MODE_CLASS (mode) != MODE_INT)
12704 break;
12705 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12706 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12707 if (op0 == 0)
12708 break;
12709 else if (GET_CODE (rtl) == ZERO_EXTEND
12710 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12711 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12712 < HOST_BITS_PER_WIDE_INT
12713 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12714 to expand zero extend as two shifts instead of
12715 masking. */
12716 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12718 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12719 mem_loc_result = op0;
12720 add_loc_descr (&mem_loc_result,
12721 int_loc_descriptor (GET_MODE_MASK (imode)));
12722 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12724 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12726 int shift = DWARF2_ADDR_SIZE
12727 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12728 shift *= BITS_PER_UNIT;
12729 if (GET_CODE (rtl) == SIGN_EXTEND)
12730 op = DW_OP_shra;
12731 else
12732 op = DW_OP_shr;
12733 mem_loc_result = op0;
12734 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12735 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12736 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12737 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12739 else if (!dwarf_strict)
12741 dw_die_ref type_die1, type_die2;
12742 dw_loc_descr_ref cvt;
12744 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12745 GET_CODE (rtl) == ZERO_EXTEND);
12746 if (type_die1 == NULL)
12747 break;
12748 type_die2 = base_type_for_mode (mode, 1);
12749 if (type_die2 == NULL)
12750 break;
12751 mem_loc_result = op0;
12752 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12753 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12754 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12755 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12756 add_loc_descr (&mem_loc_result, cvt);
12757 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12758 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12759 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12760 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12761 add_loc_descr (&mem_loc_result, cvt);
12763 break;
12765 case MEM:
12767 rtx new_rtl = avoid_constant_pool_reference (rtl);
12768 if (new_rtl != rtl)
12770 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12771 initialized);
12772 if (mem_loc_result != NULL)
12773 return mem_loc_result;
12776 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12777 get_address_mode (rtl), mode,
12778 VAR_INIT_STATUS_INITIALIZED);
12779 if (mem_loc_result == NULL)
12780 mem_loc_result = tls_mem_loc_descriptor (rtl);
12781 if (mem_loc_result != NULL)
12783 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12784 || GET_MODE_CLASS (mode) != MODE_INT)
12786 dw_die_ref type_die;
12787 dw_loc_descr_ref deref;
12789 if (dwarf_strict)
12790 return NULL;
12791 type_die
12792 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12793 if (type_die == NULL)
12794 return NULL;
12795 deref = new_loc_descr (DW_OP_GNU_deref_type,
12796 GET_MODE_SIZE (mode), 0);
12797 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12798 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12799 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12800 add_loc_descr (&mem_loc_result, deref);
12802 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12803 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12804 else
12805 add_loc_descr (&mem_loc_result,
12806 new_loc_descr (DW_OP_deref_size,
12807 GET_MODE_SIZE (mode), 0));
12809 break;
12811 case LO_SUM:
12812 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12814 case LABEL_REF:
12815 /* Some ports can transform a symbol ref into a label ref, because
12816 the symbol ref is too far away and has to be dumped into a constant
12817 pool. */
12818 case CONST:
12819 case SYMBOL_REF:
12820 if ((GET_MODE_CLASS (mode) != MODE_INT
12821 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12822 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12823 #ifdef POINTERS_EXTEND_UNSIGNED
12824 && (mode != Pmode || mem_mode == VOIDmode)
12825 #endif
12827 break;
12828 if (GET_CODE (rtl) == SYMBOL_REF
12829 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12831 dw_loc_descr_ref temp;
12833 /* If this is not defined, we have no way to emit the data. */
12834 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12835 break;
12837 temp = new_addr_loc_descr (rtl, dtprel_true);
12839 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12840 add_loc_descr (&mem_loc_result, temp);
12842 break;
12845 if (!const_ok_for_output (rtl))
12846 break;
12848 symref:
12849 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12850 vec_safe_push (used_rtx_array, rtl);
12851 break;
12853 case CONCAT:
12854 case CONCATN:
12855 case VAR_LOCATION:
12856 case DEBUG_IMPLICIT_PTR:
12857 expansion_failed (NULL_TREE, rtl,
12858 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12859 return 0;
12861 case ENTRY_VALUE:
12862 if (dwarf_strict)
12863 return NULL;
12864 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12866 if (GET_MODE_CLASS (mode) != MODE_INT
12867 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12868 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12869 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12870 else
12872 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12873 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12874 return NULL;
12875 op0 = one_reg_loc_descriptor (dbx_regnum,
12876 VAR_INIT_STATUS_INITIALIZED);
12879 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12880 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12882 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12883 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12884 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12885 return NULL;
12887 else
12888 gcc_unreachable ();
12889 if (op0 == NULL)
12890 return NULL;
12891 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12892 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12893 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12894 break;
12896 case DEBUG_PARAMETER_REF:
12897 mem_loc_result = parameter_ref_descriptor (rtl);
12898 break;
12900 case PRE_MODIFY:
12901 /* Extract the PLUS expression nested inside and fall into
12902 PLUS code below. */
12903 rtl = XEXP (rtl, 1);
12904 goto plus;
12906 case PRE_INC:
12907 case PRE_DEC:
12908 /* Turn these into a PLUS expression and fall into the PLUS code
12909 below. */
12910 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12911 gen_int_mode (GET_CODE (rtl) == PRE_INC
12912 ? GET_MODE_UNIT_SIZE (mem_mode)
12913 : -GET_MODE_UNIT_SIZE (mem_mode),
12914 mode));
12916 /* ... fall through ... */
12918 case PLUS:
12919 plus:
12920 if (is_based_loc (rtl)
12921 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12922 || XEXP (rtl, 0) == arg_pointer_rtx
12923 || XEXP (rtl, 0) == frame_pointer_rtx)
12924 && GET_MODE_CLASS (mode) == MODE_INT)
12925 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12926 INTVAL (XEXP (rtl, 1)),
12927 VAR_INIT_STATUS_INITIALIZED);
12928 else
12930 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12931 VAR_INIT_STATUS_INITIALIZED);
12932 if (mem_loc_result == 0)
12933 break;
12935 if (CONST_INT_P (XEXP (rtl, 1))
12936 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12937 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12938 else
12940 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12941 VAR_INIT_STATUS_INITIALIZED);
12942 if (op1 == 0)
12943 return NULL;
12944 add_loc_descr (&mem_loc_result, op1);
12945 add_loc_descr (&mem_loc_result,
12946 new_loc_descr (DW_OP_plus, 0, 0));
12949 break;
12951 /* If a pseudo-reg is optimized away, it is possible for it to
12952 be replaced with a MEM containing a multiply or shift. */
12953 case MINUS:
12954 op = DW_OP_minus;
12955 goto do_binop;
12957 case MULT:
12958 op = DW_OP_mul;
12959 goto do_binop;
12961 case DIV:
12962 if (!dwarf_strict
12963 && GET_MODE_CLASS (mode) == MODE_INT
12964 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12966 mem_loc_result = typed_binop (DW_OP_div, rtl,
12967 base_type_for_mode (mode, 0),
12968 mode, mem_mode);
12969 break;
12971 op = DW_OP_div;
12972 goto do_binop;
12974 case UMOD:
12975 op = DW_OP_mod;
12976 goto do_binop;
12978 case ASHIFT:
12979 op = DW_OP_shl;
12980 goto do_shift;
12982 case ASHIFTRT:
12983 op = DW_OP_shra;
12984 goto do_shift;
12986 case LSHIFTRT:
12987 op = DW_OP_shr;
12988 goto do_shift;
12990 do_shift:
12991 if (GET_MODE_CLASS (mode) != MODE_INT)
12992 break;
12993 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12994 VAR_INIT_STATUS_INITIALIZED);
12996 rtx rtlop1 = XEXP (rtl, 1);
12997 if (GET_MODE (rtlop1) != VOIDmode
12998 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12999 < GET_MODE_BITSIZE (mode))
13000 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13001 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13002 VAR_INIT_STATUS_INITIALIZED);
13005 if (op0 == 0 || op1 == 0)
13006 break;
13008 mem_loc_result = op0;
13009 add_loc_descr (&mem_loc_result, op1);
13010 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13011 break;
13013 case AND:
13014 op = DW_OP_and;
13015 goto do_binop;
13017 case IOR:
13018 op = DW_OP_or;
13019 goto do_binop;
13021 case XOR:
13022 op = DW_OP_xor;
13023 goto do_binop;
13025 do_binop:
13026 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13027 VAR_INIT_STATUS_INITIALIZED);
13028 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13029 VAR_INIT_STATUS_INITIALIZED);
13031 if (op0 == 0 || op1 == 0)
13032 break;
13034 mem_loc_result = op0;
13035 add_loc_descr (&mem_loc_result, op1);
13036 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13037 break;
13039 case MOD:
13040 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13042 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13043 base_type_for_mode (mode, 0),
13044 mode, mem_mode);
13045 break;
13048 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13049 VAR_INIT_STATUS_INITIALIZED);
13050 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13051 VAR_INIT_STATUS_INITIALIZED);
13053 if (op0 == 0 || op1 == 0)
13054 break;
13056 mem_loc_result = op0;
13057 add_loc_descr (&mem_loc_result, op1);
13058 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13059 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13060 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13061 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13062 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13063 break;
13065 case UDIV:
13066 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13068 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13070 op = DW_OP_div;
13071 goto do_binop;
13073 mem_loc_result = typed_binop (DW_OP_div, rtl,
13074 base_type_for_mode (mode, 1),
13075 mode, mem_mode);
13077 break;
13079 case NOT:
13080 op = DW_OP_not;
13081 goto do_unop;
13083 case ABS:
13084 op = DW_OP_abs;
13085 goto do_unop;
13087 case NEG:
13088 op = DW_OP_neg;
13089 goto do_unop;
13091 do_unop:
13092 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13093 VAR_INIT_STATUS_INITIALIZED);
13095 if (op0 == 0)
13096 break;
13098 mem_loc_result = op0;
13099 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13100 break;
13102 case CONST_INT:
13103 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13104 #ifdef POINTERS_EXTEND_UNSIGNED
13105 || (mode == Pmode
13106 && mem_mode != VOIDmode
13107 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13108 #endif
13111 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13112 break;
13114 if (!dwarf_strict
13115 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13116 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13118 dw_die_ref type_die = base_type_for_mode (mode, 1);
13119 machine_mode amode;
13120 if (type_die == NULL)
13121 return NULL;
13122 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13123 MODE_INT, 0);
13124 if (INTVAL (rtl) >= 0
13125 && amode != BLKmode
13126 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13127 /* const DW_OP_GNU_convert <XXX> vs.
13128 DW_OP_GNU_const_type <XXX, 1, const>. */
13129 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13130 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13132 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13133 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13134 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13135 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13136 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13137 add_loc_descr (&mem_loc_result, op0);
13138 return mem_loc_result;
13140 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13141 INTVAL (rtl));
13142 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13143 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13144 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13145 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13146 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13147 else
13149 mem_loc_result->dw_loc_oprnd2.val_class
13150 = dw_val_class_const_double;
13151 mem_loc_result->dw_loc_oprnd2.v.val_double
13152 = double_int::from_shwi (INTVAL (rtl));
13155 break;
13157 case CONST_DOUBLE:
13158 if (!dwarf_strict)
13160 dw_die_ref type_die;
13162 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13163 CONST_DOUBLE rtx could represent either a large integer
13164 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13165 the value is always a floating point constant.
13167 When it is an integer, a CONST_DOUBLE is used whenever
13168 the constant requires 2 HWIs to be adequately represented.
13169 We output CONST_DOUBLEs as blocks. */
13170 if (mode == VOIDmode
13171 || (GET_MODE (rtl) == VOIDmode
13172 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13173 break;
13174 type_die = base_type_for_mode (mode,
13175 GET_MODE_CLASS (mode) == MODE_INT);
13176 if (type_die == NULL)
13177 return NULL;
13178 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13179 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13180 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13181 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13182 #if TARGET_SUPPORTS_WIDE_INT == 0
13183 if (!SCALAR_FLOAT_MODE_P (mode))
13185 mem_loc_result->dw_loc_oprnd2.val_class
13186 = dw_val_class_const_double;
13187 mem_loc_result->dw_loc_oprnd2.v.val_double
13188 = rtx_to_double_int (rtl);
13190 else
13191 #endif
13193 unsigned int length = GET_MODE_SIZE (mode);
13194 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13196 insert_float (rtl, array);
13197 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13198 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13199 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13200 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13203 break;
13205 case CONST_WIDE_INT:
13206 if (!dwarf_strict)
13208 dw_die_ref type_die;
13210 type_die = base_type_for_mode (mode,
13211 GET_MODE_CLASS (mode) == MODE_INT);
13212 if (type_die == NULL)
13213 return NULL;
13214 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13215 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13216 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13217 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13218 mem_loc_result->dw_loc_oprnd2.val_class
13219 = dw_val_class_wide_int;
13220 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13221 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13223 break;
13225 case EQ:
13226 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13227 break;
13229 case GE:
13230 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13231 break;
13233 case GT:
13234 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13235 break;
13237 case LE:
13238 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13239 break;
13241 case LT:
13242 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13243 break;
13245 case NE:
13246 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13247 break;
13249 case GEU:
13250 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13251 break;
13253 case GTU:
13254 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13255 break;
13257 case LEU:
13258 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13259 break;
13261 case LTU:
13262 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13263 break;
13265 case UMIN:
13266 case UMAX:
13267 if (GET_MODE_CLASS (mode) != MODE_INT)
13268 break;
13269 /* FALLTHRU */
13270 case SMIN:
13271 case SMAX:
13272 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13273 break;
13275 case ZERO_EXTRACT:
13276 case SIGN_EXTRACT:
13277 if (CONST_INT_P (XEXP (rtl, 1))
13278 && CONST_INT_P (XEXP (rtl, 2))
13279 && ((unsigned) INTVAL (XEXP (rtl, 1))
13280 + (unsigned) INTVAL (XEXP (rtl, 2))
13281 <= GET_MODE_BITSIZE (mode))
13282 && GET_MODE_CLASS (mode) == MODE_INT
13283 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13284 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13286 int shift, size;
13287 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13288 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13289 if (op0 == 0)
13290 break;
13291 if (GET_CODE (rtl) == SIGN_EXTRACT)
13292 op = DW_OP_shra;
13293 else
13294 op = DW_OP_shr;
13295 mem_loc_result = op0;
13296 size = INTVAL (XEXP (rtl, 1));
13297 shift = INTVAL (XEXP (rtl, 2));
13298 if (BITS_BIG_ENDIAN)
13299 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13300 - shift - size;
13301 if (shift + size != (int) DWARF2_ADDR_SIZE)
13303 add_loc_descr (&mem_loc_result,
13304 int_loc_descriptor (DWARF2_ADDR_SIZE
13305 - shift - size));
13306 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13308 if (size != (int) DWARF2_ADDR_SIZE)
13310 add_loc_descr (&mem_loc_result,
13311 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13312 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13315 break;
13317 case IF_THEN_ELSE:
13319 dw_loc_descr_ref op2, bra_node, drop_node;
13320 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13321 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13322 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13323 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13324 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13325 VAR_INIT_STATUS_INITIALIZED);
13326 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13327 VAR_INIT_STATUS_INITIALIZED);
13328 if (op0 == NULL || op1 == NULL || op2 == NULL)
13329 break;
13331 mem_loc_result = op1;
13332 add_loc_descr (&mem_loc_result, op2);
13333 add_loc_descr (&mem_loc_result, op0);
13334 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13335 add_loc_descr (&mem_loc_result, bra_node);
13336 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13337 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13338 add_loc_descr (&mem_loc_result, drop_node);
13339 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13340 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13342 break;
13344 case FLOAT_EXTEND:
13345 case FLOAT_TRUNCATE:
13346 case FLOAT:
13347 case UNSIGNED_FLOAT:
13348 case FIX:
13349 case UNSIGNED_FIX:
13350 if (!dwarf_strict)
13352 dw_die_ref type_die;
13353 dw_loc_descr_ref cvt;
13355 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13356 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13357 if (op0 == NULL)
13358 break;
13359 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13360 && (GET_CODE (rtl) == FLOAT
13361 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13362 <= DWARF2_ADDR_SIZE))
13364 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13365 GET_CODE (rtl) == UNSIGNED_FLOAT);
13366 if (type_die == NULL)
13367 break;
13368 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13369 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13370 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13371 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13372 add_loc_descr (&op0, cvt);
13374 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13375 if (type_die == NULL)
13376 break;
13377 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13378 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13379 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13380 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13381 add_loc_descr (&op0, cvt);
13382 if (GET_MODE_CLASS (mode) == MODE_INT
13383 && (GET_CODE (rtl) == FIX
13384 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13386 op0 = convert_descriptor_to_mode (mode, op0);
13387 if (op0 == NULL)
13388 break;
13390 mem_loc_result = op0;
13392 break;
13394 case CLZ:
13395 case CTZ:
13396 case FFS:
13397 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13398 break;
13400 case POPCOUNT:
13401 case PARITY:
13402 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13403 break;
13405 case BSWAP:
13406 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13407 break;
13409 case ROTATE:
13410 case ROTATERT:
13411 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13412 break;
13414 case COMPARE:
13415 /* In theory, we could implement the above. */
13416 /* DWARF cannot represent the unsigned compare operations
13417 natively. */
13418 case SS_MULT:
13419 case US_MULT:
13420 case SS_DIV:
13421 case US_DIV:
13422 case SS_PLUS:
13423 case US_PLUS:
13424 case SS_MINUS:
13425 case US_MINUS:
13426 case SS_NEG:
13427 case US_NEG:
13428 case SS_ABS:
13429 case SS_ASHIFT:
13430 case US_ASHIFT:
13431 case SS_TRUNCATE:
13432 case US_TRUNCATE:
13433 case UNORDERED:
13434 case ORDERED:
13435 case UNEQ:
13436 case UNGE:
13437 case UNGT:
13438 case UNLE:
13439 case UNLT:
13440 case LTGT:
13441 case FRACT_CONVERT:
13442 case UNSIGNED_FRACT_CONVERT:
13443 case SAT_FRACT:
13444 case UNSIGNED_SAT_FRACT:
13445 case SQRT:
13446 case ASM_OPERANDS:
13447 case VEC_MERGE:
13448 case VEC_SELECT:
13449 case VEC_CONCAT:
13450 case VEC_DUPLICATE:
13451 case UNSPEC:
13452 case HIGH:
13453 case FMA:
13454 case STRICT_LOW_PART:
13455 case CONST_VECTOR:
13456 case CONST_FIXED:
13457 case CLRSB:
13458 case CLOBBER:
13459 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13460 can't express it in the debug info. This can happen e.g. with some
13461 TLS UNSPECs. */
13462 break;
13464 case CONST_STRING:
13465 resolve_one_addr (&rtl);
13466 goto symref;
13468 default:
13469 #ifdef ENABLE_CHECKING
13470 print_rtl (stderr, rtl);
13471 gcc_unreachable ();
13472 #else
13473 break;
13474 #endif
13477 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13478 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13480 return mem_loc_result;
13483 /* Return a descriptor that describes the concatenation of two locations.
13484 This is typically a complex variable. */
13486 static dw_loc_descr_ref
13487 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13489 dw_loc_descr_ref cc_loc_result = NULL;
13490 dw_loc_descr_ref x0_ref
13491 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13492 dw_loc_descr_ref x1_ref
13493 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13495 if (x0_ref == 0 || x1_ref == 0)
13496 return 0;
13498 cc_loc_result = x0_ref;
13499 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13501 add_loc_descr (&cc_loc_result, x1_ref);
13502 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13504 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13505 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13507 return cc_loc_result;
13510 /* Return a descriptor that describes the concatenation of N
13511 locations. */
13513 static dw_loc_descr_ref
13514 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13516 unsigned int i;
13517 dw_loc_descr_ref cc_loc_result = NULL;
13518 unsigned int n = XVECLEN (concatn, 0);
13520 for (i = 0; i < n; ++i)
13522 dw_loc_descr_ref ref;
13523 rtx x = XVECEXP (concatn, 0, i);
13525 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13526 if (ref == NULL)
13527 return NULL;
13529 add_loc_descr (&cc_loc_result, ref);
13530 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13533 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13534 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13536 return cc_loc_result;
13539 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13540 for DEBUG_IMPLICIT_PTR RTL. */
13542 static dw_loc_descr_ref
13543 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13545 dw_loc_descr_ref ret;
13546 dw_die_ref ref;
13548 if (dwarf_strict)
13549 return NULL;
13550 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13551 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13552 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13553 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13554 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13555 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13556 if (ref)
13558 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13559 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13560 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13562 else
13564 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13565 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13567 return ret;
13570 /* Output a proper Dwarf location descriptor for a variable or parameter
13571 which is either allocated in a register or in a memory location. For a
13572 register, we just generate an OP_REG and the register number. For a
13573 memory location we provide a Dwarf postfix expression describing how to
13574 generate the (dynamic) address of the object onto the address stack.
13576 MODE is mode of the decl if this loc_descriptor is going to be used in
13577 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13578 allowed, VOIDmode otherwise.
13580 If we don't know how to describe it, return 0. */
13582 static dw_loc_descr_ref
13583 loc_descriptor (rtx rtl, machine_mode mode,
13584 enum var_init_status initialized)
13586 dw_loc_descr_ref loc_result = NULL;
13588 switch (GET_CODE (rtl))
13590 case SUBREG:
13591 /* The case of a subreg may arise when we have a local (register)
13592 variable or a formal (register) parameter which doesn't quite fill
13593 up an entire register. For now, just assume that it is
13594 legitimate to make the Dwarf info refer to the whole register which
13595 contains the given subreg. */
13596 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13597 loc_result = loc_descriptor (SUBREG_REG (rtl),
13598 GET_MODE (SUBREG_REG (rtl)), initialized);
13599 else
13600 goto do_default;
13601 break;
13603 case REG:
13604 loc_result = reg_loc_descriptor (rtl, initialized);
13605 break;
13607 case MEM:
13608 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13609 GET_MODE (rtl), initialized);
13610 if (loc_result == NULL)
13611 loc_result = tls_mem_loc_descriptor (rtl);
13612 if (loc_result == NULL)
13614 rtx new_rtl = avoid_constant_pool_reference (rtl);
13615 if (new_rtl != rtl)
13616 loc_result = loc_descriptor (new_rtl, mode, initialized);
13618 break;
13620 case CONCAT:
13621 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13622 initialized);
13623 break;
13625 case CONCATN:
13626 loc_result = concatn_loc_descriptor (rtl, initialized);
13627 break;
13629 case VAR_LOCATION:
13630 /* Single part. */
13631 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13633 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13634 if (GET_CODE (loc) == EXPR_LIST)
13635 loc = XEXP (loc, 0);
13636 loc_result = loc_descriptor (loc, mode, initialized);
13637 break;
13640 rtl = XEXP (rtl, 1);
13641 /* FALLTHRU */
13643 case PARALLEL:
13645 rtvec par_elems = XVEC (rtl, 0);
13646 int num_elem = GET_NUM_ELEM (par_elems);
13647 machine_mode mode;
13648 int i;
13650 /* Create the first one, so we have something to add to. */
13651 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13652 VOIDmode, initialized);
13653 if (loc_result == NULL)
13654 return NULL;
13655 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13656 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13657 for (i = 1; i < num_elem; i++)
13659 dw_loc_descr_ref temp;
13661 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13662 VOIDmode, initialized);
13663 if (temp == NULL)
13664 return NULL;
13665 add_loc_descr (&loc_result, temp);
13666 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13667 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13670 break;
13672 case CONST_INT:
13673 if (mode != VOIDmode && mode != BLKmode)
13674 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13675 INTVAL (rtl));
13676 break;
13678 case CONST_DOUBLE:
13679 if (mode == VOIDmode)
13680 mode = GET_MODE (rtl);
13682 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13684 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13686 /* Note that a CONST_DOUBLE rtx could represent either an integer
13687 or a floating-point constant. A CONST_DOUBLE is used whenever
13688 the constant requires more than one word in order to be
13689 adequately represented. We output CONST_DOUBLEs as blocks. */
13690 loc_result = new_loc_descr (DW_OP_implicit_value,
13691 GET_MODE_SIZE (mode), 0);
13692 #if TARGET_SUPPORTS_WIDE_INT == 0
13693 if (!SCALAR_FLOAT_MODE_P (mode))
13695 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13696 loc_result->dw_loc_oprnd2.v.val_double
13697 = rtx_to_double_int (rtl);
13699 else
13700 #endif
13702 unsigned int length = GET_MODE_SIZE (mode);
13703 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13705 insert_float (rtl, array);
13706 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13707 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13708 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13709 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13712 break;
13714 case CONST_WIDE_INT:
13715 if (mode == VOIDmode)
13716 mode = GET_MODE (rtl);
13718 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13720 loc_result = new_loc_descr (DW_OP_implicit_value,
13721 GET_MODE_SIZE (mode), 0);
13722 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13723 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13724 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13726 break;
13728 case CONST_VECTOR:
13729 if (mode == VOIDmode)
13730 mode = GET_MODE (rtl);
13732 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13734 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13735 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13736 unsigned char *array
13737 = ggc_vec_alloc<unsigned char> (length * elt_size);
13738 unsigned int i;
13739 unsigned char *p;
13740 machine_mode imode = GET_MODE_INNER (mode);
13742 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13743 switch (GET_MODE_CLASS (mode))
13745 case MODE_VECTOR_INT:
13746 for (i = 0, p = array; i < length; i++, p += elt_size)
13748 rtx elt = CONST_VECTOR_ELT (rtl, i);
13749 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13751 break;
13753 case MODE_VECTOR_FLOAT:
13754 for (i = 0, p = array; i < length; i++, p += elt_size)
13756 rtx elt = CONST_VECTOR_ELT (rtl, i);
13757 insert_float (elt, p);
13759 break;
13761 default:
13762 gcc_unreachable ();
13765 loc_result = new_loc_descr (DW_OP_implicit_value,
13766 length * elt_size, 0);
13767 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13768 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13769 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13770 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13772 break;
13774 case CONST:
13775 if (mode == VOIDmode
13776 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13777 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13778 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13780 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13781 break;
13783 /* FALLTHROUGH */
13784 case SYMBOL_REF:
13785 if (!const_ok_for_output (rtl))
13786 break;
13787 case LABEL_REF:
13788 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13789 && (dwarf_version >= 4 || !dwarf_strict))
13791 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13792 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13793 vec_safe_push (used_rtx_array, rtl);
13795 break;
13797 case DEBUG_IMPLICIT_PTR:
13798 loc_result = implicit_ptr_descriptor (rtl, 0);
13799 break;
13801 case PLUS:
13802 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13803 && CONST_INT_P (XEXP (rtl, 1)))
13805 loc_result
13806 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13807 break;
13809 /* FALLTHRU */
13810 do_default:
13811 default:
13812 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13813 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13814 && dwarf_version >= 4)
13815 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13817 /* Value expression. */
13818 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13819 if (loc_result)
13820 add_loc_descr (&loc_result,
13821 new_loc_descr (DW_OP_stack_value, 0, 0));
13823 break;
13826 return loc_result;
13829 /* We need to figure out what section we should use as the base for the
13830 address ranges where a given location is valid.
13831 1. If this particular DECL has a section associated with it, use that.
13832 2. If this function has a section associated with it, use that.
13833 3. Otherwise, use the text section.
13834 XXX: If you split a variable across multiple sections, we won't notice. */
13836 static const char *
13837 secname_for_decl (const_tree decl)
13839 const char *secname;
13841 if (VAR_OR_FUNCTION_DECL_P (decl)
13842 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13843 && DECL_SECTION_NAME (decl))
13844 secname = DECL_SECTION_NAME (decl);
13845 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13846 secname = DECL_SECTION_NAME (current_function_decl);
13847 else if (cfun && in_cold_section_p)
13848 secname = crtl->subsections.cold_section_label;
13849 else
13850 secname = text_section_label;
13852 return secname;
13855 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13857 static bool
13858 decl_by_reference_p (tree decl)
13860 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13861 || TREE_CODE (decl) == VAR_DECL)
13862 && DECL_BY_REFERENCE (decl));
13865 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13866 for VARLOC. */
13868 static dw_loc_descr_ref
13869 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13870 enum var_init_status initialized)
13872 int have_address = 0;
13873 dw_loc_descr_ref descr;
13874 machine_mode mode;
13876 if (want_address != 2)
13878 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13879 /* Single part. */
13880 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13882 varloc = PAT_VAR_LOCATION_LOC (varloc);
13883 if (GET_CODE (varloc) == EXPR_LIST)
13884 varloc = XEXP (varloc, 0);
13885 mode = GET_MODE (varloc);
13886 if (MEM_P (varloc))
13888 rtx addr = XEXP (varloc, 0);
13889 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13890 mode, initialized);
13891 if (descr)
13892 have_address = 1;
13893 else
13895 rtx x = avoid_constant_pool_reference (varloc);
13896 if (x != varloc)
13897 descr = mem_loc_descriptor (x, mode, VOIDmode,
13898 initialized);
13901 else
13902 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13904 else
13905 return 0;
13907 else
13909 if (GET_CODE (varloc) == VAR_LOCATION)
13910 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13911 else
13912 mode = DECL_MODE (loc);
13913 descr = loc_descriptor (varloc, mode, initialized);
13914 have_address = 1;
13917 if (!descr)
13918 return 0;
13920 if (want_address == 2 && !have_address
13921 && (dwarf_version >= 4 || !dwarf_strict))
13923 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13925 expansion_failed (loc, NULL_RTX,
13926 "DWARF address size mismatch");
13927 return 0;
13929 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13930 have_address = 1;
13932 /* Show if we can't fill the request for an address. */
13933 if (want_address && !have_address)
13935 expansion_failed (loc, NULL_RTX,
13936 "Want address and only have value");
13937 return 0;
13940 /* If we've got an address and don't want one, dereference. */
13941 if (!want_address && have_address)
13943 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13944 enum dwarf_location_atom op;
13946 if (size > DWARF2_ADDR_SIZE || size == -1)
13948 expansion_failed (loc, NULL_RTX,
13949 "DWARF address size mismatch");
13950 return 0;
13952 else if (size == DWARF2_ADDR_SIZE)
13953 op = DW_OP_deref;
13954 else
13955 op = DW_OP_deref_size;
13957 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13960 return descr;
13963 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13964 if it is not possible. */
13966 static dw_loc_descr_ref
13967 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13969 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13970 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13971 else if (dwarf_version >= 3 || !dwarf_strict)
13972 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13973 else
13974 return NULL;
13977 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13978 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13980 static dw_loc_descr_ref
13981 dw_sra_loc_expr (tree decl, rtx loc)
13983 rtx p;
13984 unsigned HOST_WIDE_INT padsize = 0;
13985 dw_loc_descr_ref descr, *descr_tail;
13986 unsigned HOST_WIDE_INT decl_size;
13987 rtx varloc;
13988 enum var_init_status initialized;
13990 if (DECL_SIZE (decl) == NULL
13991 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13992 return NULL;
13994 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13995 descr = NULL;
13996 descr_tail = &descr;
13998 for (p = loc; p; p = XEXP (p, 1))
14000 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14001 rtx loc_note = *decl_piece_varloc_ptr (p);
14002 dw_loc_descr_ref cur_descr;
14003 dw_loc_descr_ref *tail, last = NULL;
14004 unsigned HOST_WIDE_INT opsize = 0;
14006 if (loc_note == NULL_RTX
14007 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14009 padsize += bitsize;
14010 continue;
14012 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14013 varloc = NOTE_VAR_LOCATION (loc_note);
14014 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14015 if (cur_descr == NULL)
14017 padsize += bitsize;
14018 continue;
14021 /* Check that cur_descr either doesn't use
14022 DW_OP_*piece operations, or their sum is equal
14023 to bitsize. Otherwise we can't embed it. */
14024 for (tail = &cur_descr; *tail != NULL;
14025 tail = &(*tail)->dw_loc_next)
14026 if ((*tail)->dw_loc_opc == DW_OP_piece)
14028 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14029 * BITS_PER_UNIT;
14030 last = *tail;
14032 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14034 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14035 last = *tail;
14038 if (last != NULL && opsize != bitsize)
14040 padsize += bitsize;
14041 /* Discard the current piece of the descriptor and release any
14042 addr_table entries it uses. */
14043 remove_loc_list_addr_table_entries (cur_descr);
14044 continue;
14047 /* If there is a hole, add DW_OP_*piece after empty DWARF
14048 expression, which means that those bits are optimized out. */
14049 if (padsize)
14051 if (padsize > decl_size)
14053 remove_loc_list_addr_table_entries (cur_descr);
14054 goto discard_descr;
14056 decl_size -= padsize;
14057 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14058 if (*descr_tail == NULL)
14060 remove_loc_list_addr_table_entries (cur_descr);
14061 goto discard_descr;
14063 descr_tail = &(*descr_tail)->dw_loc_next;
14064 padsize = 0;
14066 *descr_tail = cur_descr;
14067 descr_tail = tail;
14068 if (bitsize > decl_size)
14069 goto discard_descr;
14070 decl_size -= bitsize;
14071 if (last == NULL)
14073 HOST_WIDE_INT offset = 0;
14074 if (GET_CODE (varloc) == VAR_LOCATION
14075 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14077 varloc = PAT_VAR_LOCATION_LOC (varloc);
14078 if (GET_CODE (varloc) == EXPR_LIST)
14079 varloc = XEXP (varloc, 0);
14083 if (GET_CODE (varloc) == CONST
14084 || GET_CODE (varloc) == SIGN_EXTEND
14085 || GET_CODE (varloc) == ZERO_EXTEND)
14086 varloc = XEXP (varloc, 0);
14087 else if (GET_CODE (varloc) == SUBREG)
14088 varloc = SUBREG_REG (varloc);
14089 else
14090 break;
14092 while (1);
14093 /* DW_OP_bit_size offset should be zero for register
14094 or implicit location descriptions and empty location
14095 descriptions, but for memory addresses needs big endian
14096 adjustment. */
14097 if (MEM_P (varloc))
14099 unsigned HOST_WIDE_INT memsize
14100 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14101 if (memsize != bitsize)
14103 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14104 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14105 goto discard_descr;
14106 if (memsize < bitsize)
14107 goto discard_descr;
14108 if (BITS_BIG_ENDIAN)
14109 offset = memsize - bitsize;
14113 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14114 if (*descr_tail == NULL)
14115 goto discard_descr;
14116 descr_tail = &(*descr_tail)->dw_loc_next;
14120 /* If there were any non-empty expressions, add padding till the end of
14121 the decl. */
14122 if (descr != NULL && decl_size != 0)
14124 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14125 if (*descr_tail == NULL)
14126 goto discard_descr;
14128 return descr;
14130 discard_descr:
14131 /* Discard the descriptor and release any addr_table entries it uses. */
14132 remove_loc_list_addr_table_entries (descr);
14133 return NULL;
14136 /* Return the dwarf representation of the location list LOC_LIST of
14137 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14138 function. */
14140 static dw_loc_list_ref
14141 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14143 const char *endname, *secname;
14144 rtx varloc;
14145 enum var_init_status initialized;
14146 struct var_loc_node *node;
14147 dw_loc_descr_ref descr;
14148 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14149 dw_loc_list_ref list = NULL;
14150 dw_loc_list_ref *listp = &list;
14152 /* Now that we know what section we are using for a base,
14153 actually construct the list of locations.
14154 The first location information is what is passed to the
14155 function that creates the location list, and the remaining
14156 locations just get added on to that list.
14157 Note that we only know the start address for a location
14158 (IE location changes), so to build the range, we use
14159 the range [current location start, next location start].
14160 This means we have to special case the last node, and generate
14161 a range of [last location start, end of function label]. */
14163 secname = secname_for_decl (decl);
14165 for (node = loc_list->first; node; node = node->next)
14166 if (GET_CODE (node->loc) == EXPR_LIST
14167 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14169 if (GET_CODE (node->loc) == EXPR_LIST)
14171 /* This requires DW_OP_{,bit_}piece, which is not usable
14172 inside DWARF expressions. */
14173 if (want_address != 2)
14174 continue;
14175 descr = dw_sra_loc_expr (decl, node->loc);
14176 if (descr == NULL)
14177 continue;
14179 else
14181 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14182 varloc = NOTE_VAR_LOCATION (node->loc);
14183 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14185 if (descr)
14187 bool range_across_switch = false;
14188 /* If section switch happens in between node->label
14189 and node->next->label (or end of function) and
14190 we can't emit it as a single entry list,
14191 emit two ranges, first one ending at the end
14192 of first partition and second one starting at the
14193 beginning of second partition. */
14194 if (node == loc_list->last_before_switch
14195 && (node != loc_list->first || loc_list->first->next)
14196 && current_function_decl)
14198 endname = cfun->fde->dw_fde_end;
14199 range_across_switch = true;
14201 /* The variable has a location between NODE->LABEL and
14202 NODE->NEXT->LABEL. */
14203 else if (node->next)
14204 endname = node->next->label;
14205 /* If the variable has a location at the last label
14206 it keeps its location until the end of function. */
14207 else if (!current_function_decl)
14208 endname = text_end_label;
14209 else
14211 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14212 current_function_funcdef_no);
14213 endname = ggc_strdup (label_id);
14216 *listp = new_loc_list (descr, node->label, endname, secname);
14217 if (TREE_CODE (decl) == PARM_DECL
14218 && node == loc_list->first
14219 && NOTE_P (node->loc)
14220 && strcmp (node->label, endname) == 0)
14221 (*listp)->force = true;
14222 listp = &(*listp)->dw_loc_next;
14224 if (range_across_switch)
14226 if (GET_CODE (node->loc) == EXPR_LIST)
14227 descr = dw_sra_loc_expr (decl, node->loc);
14228 else
14230 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14231 varloc = NOTE_VAR_LOCATION (node->loc);
14232 descr = dw_loc_list_1 (decl, varloc, want_address,
14233 initialized);
14235 gcc_assert (descr);
14236 /* The variable has a location between NODE->LABEL and
14237 NODE->NEXT->LABEL. */
14238 if (node->next)
14239 endname = node->next->label;
14240 else
14241 endname = cfun->fde->dw_fde_second_end;
14242 *listp = new_loc_list (descr,
14243 cfun->fde->dw_fde_second_begin,
14244 endname, secname);
14245 listp = &(*listp)->dw_loc_next;
14250 /* Try to avoid the overhead of a location list emitting a location
14251 expression instead, but only if we didn't have more than one
14252 location entry in the first place. If some entries were not
14253 representable, we don't want to pretend a single entry that was
14254 applies to the entire scope in which the variable is
14255 available. */
14256 if (list && loc_list->first->next)
14257 gen_llsym (list);
14259 return list;
14262 /* Return if the loc_list has only single element and thus can be represented
14263 as location description. */
14265 static bool
14266 single_element_loc_list_p (dw_loc_list_ref list)
14268 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14269 return !list->ll_symbol;
14272 /* To each location in list LIST add loc descr REF. */
14274 static void
14275 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14277 dw_loc_descr_ref copy;
14278 add_loc_descr (&list->expr, ref);
14279 list = list->dw_loc_next;
14280 while (list)
14282 copy = ggc_alloc<dw_loc_descr_node> ();
14283 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14284 add_loc_descr (&list->expr, copy);
14285 while (copy->dw_loc_next)
14287 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14288 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14289 copy->dw_loc_next = new_copy;
14290 copy = new_copy;
14292 list = list->dw_loc_next;
14296 /* Given two lists RET and LIST
14297 produce location list that is result of adding expression in LIST
14298 to expression in RET on each position in program.
14299 Might be destructive on both RET and LIST.
14301 TODO: We handle only simple cases of RET or LIST having at most one
14302 element. General case would inolve sorting the lists in program order
14303 and merging them that will need some additional work.
14304 Adding that will improve quality of debug info especially for SRA-ed
14305 structures. */
14307 static void
14308 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14310 if (!list)
14311 return;
14312 if (!*ret)
14314 *ret = list;
14315 return;
14317 if (!list->dw_loc_next)
14319 add_loc_descr_to_each (*ret, list->expr);
14320 return;
14322 if (!(*ret)->dw_loc_next)
14324 add_loc_descr_to_each (list, (*ret)->expr);
14325 *ret = list;
14326 return;
14328 expansion_failed (NULL_TREE, NULL_RTX,
14329 "Don't know how to merge two non-trivial"
14330 " location lists.\n");
14331 *ret = NULL;
14332 return;
14335 /* LOC is constant expression. Try a luck, look it up in constant
14336 pool and return its loc_descr of its address. */
14338 static dw_loc_descr_ref
14339 cst_pool_loc_descr (tree loc)
14341 /* Get an RTL for this, if something has been emitted. */
14342 rtx rtl = lookup_constant_def (loc);
14344 if (!rtl || !MEM_P (rtl))
14346 gcc_assert (!rtl);
14347 return 0;
14349 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14351 /* TODO: We might get more coverage if we was actually delaying expansion
14352 of all expressions till end of compilation when constant pools are fully
14353 populated. */
14354 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14356 expansion_failed (loc, NULL_RTX,
14357 "CST value in contant pool but not marked.");
14358 return 0;
14360 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14361 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14364 /* Return dw_loc_list representing address of addr_expr LOC
14365 by looking for inner INDIRECT_REF expression and turning
14366 it into simple arithmetics.
14368 See loc_list_from_tree for the meaning of CONTEXT. */
14370 static dw_loc_list_ref
14371 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14372 const loc_descr_context *context)
14374 tree obj, offset;
14375 HOST_WIDE_INT bitsize, bitpos, bytepos;
14376 machine_mode mode;
14377 int unsignedp, volatilep = 0;
14378 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14380 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14381 &bitsize, &bitpos, &offset, &mode,
14382 &unsignedp, &volatilep, false);
14383 STRIP_NOPS (obj);
14384 if (bitpos % BITS_PER_UNIT)
14386 expansion_failed (loc, NULL_RTX, "bitfield access");
14387 return 0;
14389 if (!INDIRECT_REF_P (obj))
14391 expansion_failed (obj,
14392 NULL_RTX, "no indirect ref in inner refrence");
14393 return 0;
14395 if (!offset && !bitpos)
14396 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14397 context);
14398 else if (toplev
14399 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14400 && (dwarf_version >= 4 || !dwarf_strict))
14402 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14403 if (!list_ret)
14404 return 0;
14405 if (offset)
14407 /* Variable offset. */
14408 list_ret1 = loc_list_from_tree (offset, 0, context);
14409 if (list_ret1 == 0)
14410 return 0;
14411 add_loc_list (&list_ret, list_ret1);
14412 if (!list_ret)
14413 return 0;
14414 add_loc_descr_to_each (list_ret,
14415 new_loc_descr (DW_OP_plus, 0, 0));
14417 bytepos = bitpos / BITS_PER_UNIT;
14418 if (bytepos > 0)
14419 add_loc_descr_to_each (list_ret,
14420 new_loc_descr (DW_OP_plus_uconst,
14421 bytepos, 0));
14422 else if (bytepos < 0)
14423 loc_list_plus_const (list_ret, bytepos);
14424 add_loc_descr_to_each (list_ret,
14425 new_loc_descr (DW_OP_stack_value, 0, 0));
14427 return list_ret;
14431 /* Helper structure for location descriptions generation. */
14432 struct loc_descr_context
14434 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14435 NULL_TREE if DW_OP_push_object_address in invalid for this location
14436 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14437 tree context_type;
14438 /* The ..._DECL node that should be translated as a
14439 DW_OP_push_object_address operation. */
14440 tree base_decl;
14443 /* Generate Dwarf location list representing LOC.
14444 If WANT_ADDRESS is false, expression computing LOC will be computed
14445 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14446 if WANT_ADDRESS is 2, expression computing address useable in location
14447 will be returned (i.e. DW_OP_reg can be used
14448 to refer to register values).
14450 CONTEXT provides information to customize the location descriptions
14451 generation. Its context_type field specifies what type is implicitly
14452 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14453 will not be generated.
14455 If CONTEXT is NULL, the behavior is the same as if both context_type and
14456 base_decl fields were NULL_TREE. */
14458 static dw_loc_list_ref
14459 loc_list_from_tree (tree loc, int want_address,
14460 const struct loc_descr_context *context)
14462 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14463 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14464 int have_address = 0;
14465 enum dwarf_location_atom op;
14467 /* ??? Most of the time we do not take proper care for sign/zero
14468 extending the values properly. Hopefully this won't be a real
14469 problem... */
14471 if (context != NULL
14472 && context->base_decl == loc
14473 && want_address == 0)
14475 if (dwarf_version >= 3 || !dwarf_strict)
14476 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14477 NULL, NULL, NULL);
14478 else
14479 return NULL;
14482 switch (TREE_CODE (loc))
14484 case ERROR_MARK:
14485 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14486 return 0;
14488 case PLACEHOLDER_EXPR:
14489 /* This case involves extracting fields from an object to determine the
14490 position of other fields. It is supposed to appear only as the first
14491 operand of COMPONENT_REF nodes and to reference precisely the type
14492 that the context allows. */
14493 if (context != NULL
14494 && TREE_TYPE (loc) == context->context_type
14495 && want_address >= 1)
14497 if (dwarf_version >= 3 || !dwarf_strict)
14499 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14500 have_address = 1;
14501 break;
14503 else
14504 return NULL;
14506 else
14507 expansion_failed (loc, NULL_RTX,
14508 "PLACEHOLDER_EXPR for an unexpected type");
14509 break;
14511 case CALL_EXPR:
14512 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14513 /* There are no opcodes for these operations. */
14514 return 0;
14516 case PREINCREMENT_EXPR:
14517 case PREDECREMENT_EXPR:
14518 case POSTINCREMENT_EXPR:
14519 case POSTDECREMENT_EXPR:
14520 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14521 /* There are no opcodes for these operations. */
14522 return 0;
14524 case ADDR_EXPR:
14525 /* If we already want an address, see if there is INDIRECT_REF inside
14526 e.g. for &this->field. */
14527 if (want_address)
14529 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14530 (loc, want_address == 2, context);
14531 if (list_ret)
14532 have_address = 1;
14533 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14534 && (ret = cst_pool_loc_descr (loc)))
14535 have_address = 1;
14537 /* Otherwise, process the argument and look for the address. */
14538 if (!list_ret && !ret)
14539 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14540 else
14542 if (want_address)
14543 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14544 return NULL;
14546 break;
14548 case VAR_DECL:
14549 if (DECL_THREAD_LOCAL_P (loc))
14551 rtx rtl;
14552 enum dwarf_location_atom tls_op;
14553 enum dtprel_bool dtprel = dtprel_false;
14555 if (targetm.have_tls)
14557 /* If this is not defined, we have no way to emit the
14558 data. */
14559 if (!targetm.asm_out.output_dwarf_dtprel)
14560 return 0;
14562 /* The way DW_OP_GNU_push_tls_address is specified, we
14563 can only look up addresses of objects in the current
14564 module. We used DW_OP_addr as first op, but that's
14565 wrong, because DW_OP_addr is relocated by the debug
14566 info consumer, while DW_OP_GNU_push_tls_address
14567 operand shouldn't be. */
14568 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14569 return 0;
14570 dtprel = dtprel_true;
14571 tls_op = DW_OP_GNU_push_tls_address;
14573 else
14575 if (!targetm.emutls.debug_form_tls_address
14576 || !(dwarf_version >= 3 || !dwarf_strict))
14577 return 0;
14578 /* We stuffed the control variable into the DECL_VALUE_EXPR
14579 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14580 no longer appear in gimple code. We used the control
14581 variable in specific so that we could pick it up here. */
14582 loc = DECL_VALUE_EXPR (loc);
14583 tls_op = DW_OP_form_tls_address;
14586 rtl = rtl_for_decl_location (loc);
14587 if (rtl == NULL_RTX)
14588 return 0;
14590 if (!MEM_P (rtl))
14591 return 0;
14592 rtl = XEXP (rtl, 0);
14593 if (! CONSTANT_P (rtl))
14594 return 0;
14596 ret = new_addr_loc_descr (rtl, dtprel);
14597 ret1 = new_loc_descr (tls_op, 0, 0);
14598 add_loc_descr (&ret, ret1);
14600 have_address = 1;
14601 break;
14603 /* FALLTHRU */
14605 case PARM_DECL:
14606 case RESULT_DECL:
14607 if (DECL_HAS_VALUE_EXPR_P (loc))
14608 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14609 want_address, context);
14610 /* FALLTHRU */
14612 case FUNCTION_DECL:
14614 rtx rtl;
14615 var_loc_list *loc_list = lookup_decl_loc (loc);
14617 if (loc_list && loc_list->first)
14619 list_ret = dw_loc_list (loc_list, loc, want_address);
14620 have_address = want_address != 0;
14621 break;
14623 rtl = rtl_for_decl_location (loc);
14624 if (rtl == NULL_RTX)
14626 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14627 return 0;
14629 else if (CONST_INT_P (rtl))
14631 HOST_WIDE_INT val = INTVAL (rtl);
14632 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14633 val &= GET_MODE_MASK (DECL_MODE (loc));
14634 ret = int_loc_descriptor (val);
14636 else if (GET_CODE (rtl) == CONST_STRING)
14638 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14639 return 0;
14641 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14642 ret = new_addr_loc_descr (rtl, dtprel_false);
14643 else
14645 machine_mode mode, mem_mode;
14647 /* Certain constructs can only be represented at top-level. */
14648 if (want_address == 2)
14650 ret = loc_descriptor (rtl, VOIDmode,
14651 VAR_INIT_STATUS_INITIALIZED);
14652 have_address = 1;
14654 else
14656 mode = GET_MODE (rtl);
14657 mem_mode = VOIDmode;
14658 if (MEM_P (rtl))
14660 mem_mode = mode;
14661 mode = get_address_mode (rtl);
14662 rtl = XEXP (rtl, 0);
14663 have_address = 1;
14665 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14666 VAR_INIT_STATUS_INITIALIZED);
14668 if (!ret)
14669 expansion_failed (loc, rtl,
14670 "failed to produce loc descriptor for rtl");
14673 break;
14675 case MEM_REF:
14676 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14678 have_address = 1;
14679 goto do_plus;
14681 /* Fallthru. */
14682 case INDIRECT_REF:
14683 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14684 have_address = 1;
14685 break;
14687 case TARGET_MEM_REF:
14688 case SSA_NAME:
14689 return NULL;
14691 case COMPOUND_EXPR:
14692 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14694 CASE_CONVERT:
14695 case VIEW_CONVERT_EXPR:
14696 case SAVE_EXPR:
14697 case MODIFY_EXPR:
14698 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14700 case COMPONENT_REF:
14701 case BIT_FIELD_REF:
14702 case ARRAY_REF:
14703 case ARRAY_RANGE_REF:
14704 case REALPART_EXPR:
14705 case IMAGPART_EXPR:
14707 tree obj, offset;
14708 HOST_WIDE_INT bitsize, bitpos, bytepos;
14709 machine_mode mode;
14710 int unsignedp, volatilep = 0;
14712 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14713 &unsignedp, &volatilep, false);
14715 gcc_assert (obj != loc);
14717 list_ret = loc_list_from_tree (obj,
14718 want_address == 2
14719 && !bitpos && !offset ? 2 : 1,
14720 context);
14721 /* TODO: We can extract value of the small expression via shifting even
14722 for nonzero bitpos. */
14723 if (list_ret == 0)
14724 return 0;
14725 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14727 expansion_failed (loc, NULL_RTX,
14728 "bitfield access");
14729 return 0;
14732 if (offset != NULL_TREE)
14734 /* Variable offset. */
14735 list_ret1 = loc_list_from_tree (offset, 0, context);
14736 if (list_ret1 == 0)
14737 return 0;
14738 add_loc_list (&list_ret, list_ret1);
14739 if (!list_ret)
14740 return 0;
14741 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14744 bytepos = bitpos / BITS_PER_UNIT;
14745 if (bytepos > 0)
14746 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14747 else if (bytepos < 0)
14748 loc_list_plus_const (list_ret, bytepos);
14750 have_address = 1;
14751 break;
14754 case INTEGER_CST:
14755 if ((want_address || !tree_fits_shwi_p (loc))
14756 && (ret = cst_pool_loc_descr (loc)))
14757 have_address = 1;
14758 else if (want_address == 2
14759 && tree_fits_shwi_p (loc)
14760 && (ret = address_of_int_loc_descriptor
14761 (int_size_in_bytes (TREE_TYPE (loc)),
14762 tree_to_shwi (loc))))
14763 have_address = 1;
14764 else if (tree_fits_shwi_p (loc))
14765 ret = int_loc_descriptor (tree_to_shwi (loc));
14766 else
14768 expansion_failed (loc, NULL_RTX,
14769 "Integer operand is not host integer");
14770 return 0;
14772 break;
14774 case CONSTRUCTOR:
14775 case REAL_CST:
14776 case STRING_CST:
14777 case COMPLEX_CST:
14778 if ((ret = cst_pool_loc_descr (loc)))
14779 have_address = 1;
14780 else
14781 /* We can construct small constants here using int_loc_descriptor. */
14782 expansion_failed (loc, NULL_RTX,
14783 "constructor or constant not in constant pool");
14784 break;
14786 case TRUTH_AND_EXPR:
14787 case TRUTH_ANDIF_EXPR:
14788 case BIT_AND_EXPR:
14789 op = DW_OP_and;
14790 goto do_binop;
14792 case TRUTH_XOR_EXPR:
14793 case BIT_XOR_EXPR:
14794 op = DW_OP_xor;
14795 goto do_binop;
14797 case TRUTH_OR_EXPR:
14798 case TRUTH_ORIF_EXPR:
14799 case BIT_IOR_EXPR:
14800 op = DW_OP_or;
14801 goto do_binop;
14803 case FLOOR_DIV_EXPR:
14804 case CEIL_DIV_EXPR:
14805 case ROUND_DIV_EXPR:
14806 case TRUNC_DIV_EXPR:
14807 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14808 return 0;
14809 op = DW_OP_div;
14810 goto do_binop;
14812 case MINUS_EXPR:
14813 op = DW_OP_minus;
14814 goto do_binop;
14816 case FLOOR_MOD_EXPR:
14817 case CEIL_MOD_EXPR:
14818 case ROUND_MOD_EXPR:
14819 case TRUNC_MOD_EXPR:
14820 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14822 op = DW_OP_mod;
14823 goto do_binop;
14825 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14826 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14827 if (list_ret == 0 || list_ret1 == 0)
14828 return 0;
14830 add_loc_list (&list_ret, list_ret1);
14831 if (list_ret == 0)
14832 return 0;
14833 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14834 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14835 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14836 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14837 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14838 break;
14840 case MULT_EXPR:
14841 op = DW_OP_mul;
14842 goto do_binop;
14844 case LSHIFT_EXPR:
14845 op = DW_OP_shl;
14846 goto do_binop;
14848 case RSHIFT_EXPR:
14849 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14850 goto do_binop;
14852 case POINTER_PLUS_EXPR:
14853 case PLUS_EXPR:
14854 do_plus:
14855 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14857 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14858 if (list_ret == 0)
14859 return 0;
14861 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14862 break;
14865 op = DW_OP_plus;
14866 goto do_binop;
14868 case LE_EXPR:
14869 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14870 return 0;
14872 op = DW_OP_le;
14873 goto do_binop;
14875 case GE_EXPR:
14876 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14877 return 0;
14879 op = DW_OP_ge;
14880 goto do_binop;
14882 case LT_EXPR:
14883 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14884 return 0;
14886 op = DW_OP_lt;
14887 goto do_binop;
14889 case GT_EXPR:
14890 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14891 return 0;
14893 op = DW_OP_gt;
14894 goto do_binop;
14896 case EQ_EXPR:
14897 op = DW_OP_eq;
14898 goto do_binop;
14900 case NE_EXPR:
14901 op = DW_OP_ne;
14902 goto do_binop;
14904 do_binop:
14905 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14906 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14907 if (list_ret == 0 || list_ret1 == 0)
14908 return 0;
14910 add_loc_list (&list_ret, list_ret1);
14911 if (list_ret == 0)
14912 return 0;
14913 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14914 break;
14916 case TRUTH_NOT_EXPR:
14917 case BIT_NOT_EXPR:
14918 op = DW_OP_not;
14919 goto do_unop;
14921 case ABS_EXPR:
14922 op = DW_OP_abs;
14923 goto do_unop;
14925 case NEGATE_EXPR:
14926 op = DW_OP_neg;
14927 goto do_unop;
14929 do_unop:
14930 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14931 if (list_ret == 0)
14932 return 0;
14934 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14935 break;
14937 case MIN_EXPR:
14938 case MAX_EXPR:
14940 const enum tree_code code =
14941 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14943 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14944 build2 (code, integer_type_node,
14945 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14946 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14949 /* ... fall through ... */
14951 case COND_EXPR:
14953 dw_loc_descr_ref lhs
14954 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
14955 dw_loc_list_ref rhs
14956 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
14957 dw_loc_descr_ref bra_node, jump_node, tmp;
14959 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14960 if (list_ret == 0 || lhs == 0 || rhs == 0)
14961 return 0;
14963 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14964 add_loc_descr_to_each (list_ret, bra_node);
14966 add_loc_list (&list_ret, rhs);
14967 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14968 add_loc_descr_to_each (list_ret, jump_node);
14970 add_loc_descr_to_each (list_ret, lhs);
14971 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14972 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14974 /* ??? Need a node to point the skip at. Use a nop. */
14975 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14976 add_loc_descr_to_each (list_ret, tmp);
14977 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14978 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14980 break;
14982 case FIX_TRUNC_EXPR:
14983 return 0;
14985 default:
14986 /* Leave front-end specific codes as simply unknown. This comes
14987 up, for instance, with the C STMT_EXPR. */
14988 if ((unsigned int) TREE_CODE (loc)
14989 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14991 expansion_failed (loc, NULL_RTX,
14992 "language specific tree node");
14993 return 0;
14996 #ifdef ENABLE_CHECKING
14997 /* Otherwise this is a generic code; we should just lists all of
14998 these explicitly. We forgot one. */
14999 gcc_unreachable ();
15000 #else
15001 /* In a release build, we want to degrade gracefully: better to
15002 generate incomplete debugging information than to crash. */
15003 return NULL;
15004 #endif
15007 if (!ret && !list_ret)
15008 return 0;
15010 if (want_address == 2 && !have_address
15011 && (dwarf_version >= 4 || !dwarf_strict))
15013 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15015 expansion_failed (loc, NULL_RTX,
15016 "DWARF address size mismatch");
15017 return 0;
15019 if (ret)
15020 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15021 else
15022 add_loc_descr_to_each (list_ret,
15023 new_loc_descr (DW_OP_stack_value, 0, 0));
15024 have_address = 1;
15026 /* Show if we can't fill the request for an address. */
15027 if (want_address && !have_address)
15029 expansion_failed (loc, NULL_RTX,
15030 "Want address and only have value");
15031 return 0;
15034 gcc_assert (!ret || !list_ret);
15036 /* If we've got an address and don't want one, dereference. */
15037 if (!want_address && have_address)
15039 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15041 if (size > DWARF2_ADDR_SIZE || size == -1)
15043 expansion_failed (loc, NULL_RTX,
15044 "DWARF address size mismatch");
15045 return 0;
15047 else if (size == DWARF2_ADDR_SIZE)
15048 op = DW_OP_deref;
15049 else
15050 op = DW_OP_deref_size;
15052 if (ret)
15053 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15054 else
15055 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15057 if (ret)
15058 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15060 return list_ret;
15063 /* Same as above but return only single location expression. */
15064 static dw_loc_descr_ref
15065 loc_descriptor_from_tree (tree loc, int want_address,
15066 const struct loc_descr_context *context)
15068 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15069 if (!ret)
15070 return NULL;
15071 if (ret->dw_loc_next)
15073 expansion_failed (loc, NULL_RTX,
15074 "Location list where only loc descriptor needed");
15075 return NULL;
15077 return ret->expr;
15080 /* Given a value, round it up to the lowest multiple of `boundary'
15081 which is not less than the value itself. */
15083 static inline HOST_WIDE_INT
15084 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15086 return (((value + boundary - 1) / boundary) * boundary);
15089 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15090 pointer to the declared type for the relevant field variable, or return
15091 `integer_type_node' if the given node turns out to be an
15092 ERROR_MARK node. */
15094 static inline tree
15095 field_type (const_tree decl)
15097 tree type;
15099 if (TREE_CODE (decl) == ERROR_MARK)
15100 return integer_type_node;
15102 type = DECL_BIT_FIELD_TYPE (decl);
15103 if (type == NULL_TREE)
15104 type = TREE_TYPE (decl);
15106 return type;
15109 /* Given a pointer to a tree node, return the alignment in bits for
15110 it, or else return BITS_PER_WORD if the node actually turns out to
15111 be an ERROR_MARK node. */
15113 static inline unsigned
15114 simple_type_align_in_bits (const_tree type)
15116 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15119 static inline unsigned
15120 simple_decl_align_in_bits (const_tree decl)
15122 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15125 /* Return the result of rounding T up to ALIGN. */
15127 static inline offset_int
15128 round_up_to_align (const offset_int &t, unsigned int align)
15130 return wi::udiv_trunc (t + align - 1, align) * align;
15133 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15134 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15135 or return 0 if we are unable to determine what that offset is, either
15136 because the argument turns out to be a pointer to an ERROR_MARK node, or
15137 because the offset is actually variable. (We can't handle the latter case
15138 just yet). */
15140 static HOST_WIDE_INT
15141 field_byte_offset (const_tree decl)
15143 offset_int object_offset_in_bits;
15144 offset_int object_offset_in_bytes;
15145 offset_int bitpos_int;
15147 if (TREE_CODE (decl) == ERROR_MARK)
15148 return 0;
15150 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15152 /* We cannot yet cope with fields whose positions are variable, so
15153 for now, when we see such things, we simply return 0. Someday, we may
15154 be able to handle such cases, but it will be damn difficult. */
15155 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15156 return 0;
15158 bitpos_int = wi::to_offset (bit_position (decl));
15160 #ifdef PCC_BITFIELD_TYPE_MATTERS
15161 if (PCC_BITFIELD_TYPE_MATTERS)
15163 tree type;
15164 tree field_size_tree;
15165 offset_int deepest_bitpos;
15166 offset_int field_size_in_bits;
15167 unsigned int type_align_in_bits;
15168 unsigned int decl_align_in_bits;
15169 offset_int type_size_in_bits;
15171 type = field_type (decl);
15172 type_size_in_bits = offset_int_type_size_in_bits (type);
15173 type_align_in_bits = simple_type_align_in_bits (type);
15175 field_size_tree = DECL_SIZE (decl);
15177 /* The size could be unspecified if there was an error, or for
15178 a flexible array member. */
15179 if (!field_size_tree)
15180 field_size_tree = bitsize_zero_node;
15182 /* If the size of the field is not constant, use the type size. */
15183 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15184 field_size_in_bits = wi::to_offset (field_size_tree);
15185 else
15186 field_size_in_bits = type_size_in_bits;
15188 decl_align_in_bits = simple_decl_align_in_bits (decl);
15190 /* The GCC front-end doesn't make any attempt to keep track of the
15191 starting bit offset (relative to the start of the containing
15192 structure type) of the hypothetical "containing object" for a
15193 bit-field. Thus, when computing the byte offset value for the
15194 start of the "containing object" of a bit-field, we must deduce
15195 this information on our own. This can be rather tricky to do in
15196 some cases. For example, handling the following structure type
15197 definition when compiling for an i386/i486 target (which only
15198 aligns long long's to 32-bit boundaries) can be very tricky:
15200 struct S { int field1; long long field2:31; };
15202 Fortunately, there is a simple rule-of-thumb which can be used
15203 in such cases. When compiling for an i386/i486, GCC will
15204 allocate 8 bytes for the structure shown above. It decides to
15205 do this based upon one simple rule for bit-field allocation.
15206 GCC allocates each "containing object" for each bit-field at
15207 the first (i.e. lowest addressed) legitimate alignment boundary
15208 (based upon the required minimum alignment for the declared
15209 type of the field) which it can possibly use, subject to the
15210 condition that there is still enough available space remaining
15211 in the containing object (when allocated at the selected point)
15212 to fully accommodate all of the bits of the bit-field itself.
15214 This simple rule makes it obvious why GCC allocates 8 bytes for
15215 each object of the structure type shown above. When looking
15216 for a place to allocate the "containing object" for `field2',
15217 the compiler simply tries to allocate a 64-bit "containing
15218 object" at each successive 32-bit boundary (starting at zero)
15219 until it finds a place to allocate that 64- bit field such that
15220 at least 31 contiguous (and previously unallocated) bits remain
15221 within that selected 64 bit field. (As it turns out, for the
15222 example above, the compiler finds it is OK to allocate the
15223 "containing object" 64-bit field at bit-offset zero within the
15224 structure type.)
15226 Here we attempt to work backwards from the limited set of facts
15227 we're given, and we try to deduce from those facts, where GCC
15228 must have believed that the containing object started (within
15229 the structure type). The value we deduce is then used (by the
15230 callers of this routine) to generate DW_AT_location and
15231 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15232 the case of DW_AT_location, regular fields as well). */
15234 /* Figure out the bit-distance from the start of the structure to
15235 the "deepest" bit of the bit-field. */
15236 deepest_bitpos = bitpos_int + field_size_in_bits;
15238 /* This is the tricky part. Use some fancy footwork to deduce
15239 where the lowest addressed bit of the containing object must
15240 be. */
15241 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15243 /* Round up to type_align by default. This works best for
15244 bitfields. */
15245 object_offset_in_bits
15246 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15248 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15250 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15252 /* Round up to decl_align instead. */
15253 object_offset_in_bits
15254 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15257 else
15258 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15259 object_offset_in_bits = bitpos_int;
15261 object_offset_in_bytes
15262 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15263 return object_offset_in_bytes.to_shwi ();
15266 /* The following routines define various Dwarf attributes and any data
15267 associated with them. */
15269 /* Add a location description attribute value to a DIE.
15271 This emits location attributes suitable for whole variables and
15272 whole parameters. Note that the location attributes for struct fields are
15273 generated by the routine `data_member_location_attribute' below. */
15275 static inline void
15276 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15277 dw_loc_list_ref descr)
15279 if (descr == 0)
15280 return;
15281 if (single_element_loc_list_p (descr))
15282 add_AT_loc (die, attr_kind, descr->expr);
15283 else
15284 add_AT_loc_list (die, attr_kind, descr);
15287 /* Add DW_AT_accessibility attribute to DIE if needed. */
15289 static void
15290 add_accessibility_attribute (dw_die_ref die, tree decl)
15292 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15293 children, otherwise the default is DW_ACCESS_public. In DWARF2
15294 the default has always been DW_ACCESS_public. */
15295 if (TREE_PROTECTED (decl))
15296 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15297 else if (TREE_PRIVATE (decl))
15299 if (dwarf_version == 2
15300 || die->die_parent == NULL
15301 || die->die_parent->die_tag != DW_TAG_class_type)
15302 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15304 else if (dwarf_version > 2
15305 && die->die_parent
15306 && die->die_parent->die_tag == DW_TAG_class_type)
15307 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15310 /* Attach the specialized form of location attribute used for data members of
15311 struct and union types. In the special case of a FIELD_DECL node which
15312 represents a bit-field, the "offset" part of this special location
15313 descriptor must indicate the distance in bytes from the lowest-addressed
15314 byte of the containing struct or union type to the lowest-addressed byte of
15315 the "containing object" for the bit-field. (See the `field_byte_offset'
15316 function above).
15318 For any given bit-field, the "containing object" is a hypothetical object
15319 (of some integral or enum type) within which the given bit-field lives. The
15320 type of this hypothetical "containing object" is always the same as the
15321 declared type of the individual bit-field itself (for GCC anyway... the
15322 DWARF spec doesn't actually mandate this). Note that it is the size (in
15323 bytes) of the hypothetical "containing object" which will be given in the
15324 DW_AT_byte_size attribute for this bit-field. (See the
15325 `byte_size_attribute' function below.) It is also used when calculating the
15326 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15327 function below.) */
15329 static void
15330 add_data_member_location_attribute (dw_die_ref die, tree decl)
15332 HOST_WIDE_INT offset;
15333 dw_loc_descr_ref loc_descr = 0;
15335 if (TREE_CODE (decl) == TREE_BINFO)
15337 /* We're working on the TAG_inheritance for a base class. */
15338 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15340 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15341 aren't at a fixed offset from all (sub)objects of the same
15342 type. We need to extract the appropriate offset from our
15343 vtable. The following dwarf expression means
15345 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15347 This is specific to the V3 ABI, of course. */
15349 dw_loc_descr_ref tmp;
15351 /* Make a copy of the object address. */
15352 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15353 add_loc_descr (&loc_descr, tmp);
15355 /* Extract the vtable address. */
15356 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15357 add_loc_descr (&loc_descr, tmp);
15359 /* Calculate the address of the offset. */
15360 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15361 gcc_assert (offset < 0);
15363 tmp = int_loc_descriptor (-offset);
15364 add_loc_descr (&loc_descr, tmp);
15365 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15366 add_loc_descr (&loc_descr, tmp);
15368 /* Extract the offset. */
15369 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15370 add_loc_descr (&loc_descr, tmp);
15372 /* Add it to the object address. */
15373 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15374 add_loc_descr (&loc_descr, tmp);
15376 else
15377 offset = tree_to_shwi (BINFO_OFFSET (decl));
15379 else
15380 offset = field_byte_offset (decl);
15382 if (! loc_descr)
15384 if (dwarf_version > 2)
15386 /* Don't need to output a location expression, just the constant. */
15387 if (offset < 0)
15388 add_AT_int (die, DW_AT_data_member_location, offset);
15389 else
15390 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15391 return;
15393 else
15395 enum dwarf_location_atom op;
15397 /* The DWARF2 standard says that we should assume that the structure
15398 address is already on the stack, so we can specify a structure
15399 field address by using DW_OP_plus_uconst. */
15400 op = DW_OP_plus_uconst;
15401 loc_descr = new_loc_descr (op, offset, 0);
15405 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15408 /* Writes integer values to dw_vec_const array. */
15410 static void
15411 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15413 while (size != 0)
15415 *dest++ = val & 0xff;
15416 val >>= 8;
15417 --size;
15421 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15423 static HOST_WIDE_INT
15424 extract_int (const unsigned char *src, unsigned int size)
15426 HOST_WIDE_INT val = 0;
15428 src += size;
15429 while (size != 0)
15431 val <<= 8;
15432 val |= *--src & 0xff;
15433 --size;
15435 return val;
15438 /* Writes wide_int values to dw_vec_const array. */
15440 static void
15441 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15443 int i;
15445 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15447 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15448 return;
15451 /* We'd have to extend this code to support odd sizes. */
15452 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15454 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15456 if (WORDS_BIG_ENDIAN)
15457 for (i = n - 1; i >= 0; i--)
15459 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15460 dest += sizeof (HOST_WIDE_INT);
15462 else
15463 for (i = 0; i < n; i++)
15465 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15466 dest += sizeof (HOST_WIDE_INT);
15470 /* Writes floating point values to dw_vec_const array. */
15472 static void
15473 insert_float (const_rtx rtl, unsigned char *array)
15475 REAL_VALUE_TYPE rv;
15476 long val[4];
15477 int i;
15479 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15480 real_to_target (val, &rv, GET_MODE (rtl));
15482 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15483 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15485 insert_int (val[i], 4, array);
15486 array += 4;
15490 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15491 does not have a "location" either in memory or in a register. These
15492 things can arise in GNU C when a constant is passed as an actual parameter
15493 to an inlined function. They can also arise in C++ where declared
15494 constants do not necessarily get memory "homes". */
15496 static bool
15497 add_const_value_attribute (dw_die_ref die, rtx rtl)
15499 switch (GET_CODE (rtl))
15501 case CONST_INT:
15503 HOST_WIDE_INT val = INTVAL (rtl);
15505 if (val < 0)
15506 add_AT_int (die, DW_AT_const_value, val);
15507 else
15508 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15510 return true;
15512 case CONST_WIDE_INT:
15513 add_AT_wide (die, DW_AT_const_value,
15514 std::make_pair (rtl, GET_MODE (rtl)));
15515 return true;
15517 case CONST_DOUBLE:
15518 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15519 floating-point constant. A CONST_DOUBLE is used whenever the
15520 constant requires more than one word in order to be adequately
15521 represented. */
15523 machine_mode mode = GET_MODE (rtl);
15525 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15526 add_AT_double (die, DW_AT_const_value,
15527 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15528 else
15530 unsigned int length = GET_MODE_SIZE (mode);
15531 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15533 insert_float (rtl, array);
15534 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15537 return true;
15539 case CONST_VECTOR:
15541 machine_mode mode = GET_MODE (rtl);
15542 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15543 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15544 unsigned char *array
15545 = ggc_vec_alloc<unsigned char> (length * elt_size);
15546 unsigned int i;
15547 unsigned char *p;
15548 machine_mode imode = GET_MODE_INNER (mode);
15550 switch (GET_MODE_CLASS (mode))
15552 case MODE_VECTOR_INT:
15553 for (i = 0, p = array; i < length; i++, p += elt_size)
15555 rtx elt = CONST_VECTOR_ELT (rtl, i);
15556 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15558 break;
15560 case MODE_VECTOR_FLOAT:
15561 for (i = 0, p = array; i < length; i++, p += elt_size)
15563 rtx elt = CONST_VECTOR_ELT (rtl, i);
15564 insert_float (elt, p);
15566 break;
15568 default:
15569 gcc_unreachable ();
15572 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15574 return true;
15576 case CONST_STRING:
15577 if (dwarf_version >= 4 || !dwarf_strict)
15579 dw_loc_descr_ref loc_result;
15580 resolve_one_addr (&rtl);
15581 rtl_addr:
15582 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15583 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15584 add_AT_loc (die, DW_AT_location, loc_result);
15585 vec_safe_push (used_rtx_array, rtl);
15586 return true;
15588 return false;
15590 case CONST:
15591 if (CONSTANT_P (XEXP (rtl, 0)))
15592 return add_const_value_attribute (die, XEXP (rtl, 0));
15593 /* FALLTHROUGH */
15594 case SYMBOL_REF:
15595 if (!const_ok_for_output (rtl))
15596 return false;
15597 case LABEL_REF:
15598 if (dwarf_version >= 4 || !dwarf_strict)
15599 goto rtl_addr;
15600 return false;
15602 case PLUS:
15603 /* In cases where an inlined instance of an inline function is passed
15604 the address of an `auto' variable (which is local to the caller) we
15605 can get a situation where the DECL_RTL of the artificial local
15606 variable (for the inlining) which acts as a stand-in for the
15607 corresponding formal parameter (of the inline function) will look
15608 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15609 exactly a compile-time constant expression, but it isn't the address
15610 of the (artificial) local variable either. Rather, it represents the
15611 *value* which the artificial local variable always has during its
15612 lifetime. We currently have no way to represent such quasi-constant
15613 values in Dwarf, so for now we just punt and generate nothing. */
15614 return false;
15616 case HIGH:
15617 case CONST_FIXED:
15618 return false;
15620 case MEM:
15621 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15622 && MEM_READONLY_P (rtl)
15623 && GET_MODE (rtl) == BLKmode)
15625 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15626 return true;
15628 return false;
15630 default:
15631 /* No other kinds of rtx should be possible here. */
15632 gcc_unreachable ();
15634 return false;
15637 /* Determine whether the evaluation of EXPR references any variables
15638 or functions which aren't otherwise used (and therefore may not be
15639 output). */
15640 static tree
15641 reference_to_unused (tree * tp, int * walk_subtrees,
15642 void * data ATTRIBUTE_UNUSED)
15644 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15645 *walk_subtrees = 0;
15647 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15648 && ! TREE_ASM_WRITTEN (*tp))
15649 return *tp;
15650 /* ??? The C++ FE emits debug information for using decls, so
15651 putting gcc_unreachable here falls over. See PR31899. For now
15652 be conservative. */
15653 else if (!symtab->global_info_ready
15654 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15655 return *tp;
15656 else if (TREE_CODE (*tp) == VAR_DECL)
15658 varpool_node *node = varpool_node::get (*tp);
15659 if (!node || !node->definition)
15660 return *tp;
15662 else if (TREE_CODE (*tp) == FUNCTION_DECL
15663 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15665 /* The call graph machinery must have finished analyzing,
15666 optimizing and gimplifying the CU by now.
15667 So if *TP has no call graph node associated
15668 to it, it means *TP will not be emitted. */
15669 if (!cgraph_node::get (*tp))
15670 return *tp;
15672 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15673 return *tp;
15675 return NULL_TREE;
15678 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15679 for use in a later add_const_value_attribute call. */
15681 static rtx
15682 rtl_for_decl_init (tree init, tree type)
15684 rtx rtl = NULL_RTX;
15686 STRIP_NOPS (init);
15688 /* If a variable is initialized with a string constant without embedded
15689 zeros, build CONST_STRING. */
15690 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15692 tree enttype = TREE_TYPE (type);
15693 tree domain = TYPE_DOMAIN (type);
15694 machine_mode mode = TYPE_MODE (enttype);
15696 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15697 && domain
15698 && integer_zerop (TYPE_MIN_VALUE (domain))
15699 && compare_tree_int (TYPE_MAX_VALUE (domain),
15700 TREE_STRING_LENGTH (init) - 1) == 0
15701 && ((size_t) TREE_STRING_LENGTH (init)
15702 == strlen (TREE_STRING_POINTER (init)) + 1))
15704 rtl = gen_rtx_CONST_STRING (VOIDmode,
15705 ggc_strdup (TREE_STRING_POINTER (init)));
15706 rtl = gen_rtx_MEM (BLKmode, rtl);
15707 MEM_READONLY_P (rtl) = 1;
15710 /* Other aggregates, and complex values, could be represented using
15711 CONCAT: FIXME! */
15712 else if (AGGREGATE_TYPE_P (type)
15713 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15714 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15715 || TREE_CODE (type) == COMPLEX_TYPE)
15717 /* Vectors only work if their mode is supported by the target.
15718 FIXME: generic vectors ought to work too. */
15719 else if (TREE_CODE (type) == VECTOR_TYPE
15720 && !VECTOR_MODE_P (TYPE_MODE (type)))
15722 /* If the initializer is something that we know will expand into an
15723 immediate RTL constant, expand it now. We must be careful not to
15724 reference variables which won't be output. */
15725 else if (initializer_constant_valid_p (init, type)
15726 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15728 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15729 possible. */
15730 if (TREE_CODE (type) == VECTOR_TYPE)
15731 switch (TREE_CODE (init))
15733 case VECTOR_CST:
15734 break;
15735 case CONSTRUCTOR:
15736 if (TREE_CONSTANT (init))
15738 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15739 bool constant_p = true;
15740 tree value;
15741 unsigned HOST_WIDE_INT ix;
15743 /* Even when ctor is constant, it might contain non-*_CST
15744 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15745 belong into VECTOR_CST nodes. */
15746 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15747 if (!CONSTANT_CLASS_P (value))
15749 constant_p = false;
15750 break;
15753 if (constant_p)
15755 init = build_vector_from_ctor (type, elts);
15756 break;
15759 /* FALLTHRU */
15761 default:
15762 return NULL;
15765 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15767 /* If expand_expr returns a MEM, it wasn't immediate. */
15768 gcc_assert (!rtl || !MEM_P (rtl));
15771 return rtl;
15774 /* Generate RTL for the variable DECL to represent its location. */
15776 static rtx
15777 rtl_for_decl_location (tree decl)
15779 rtx rtl;
15781 /* Here we have to decide where we are going to say the parameter "lives"
15782 (as far as the debugger is concerned). We only have a couple of
15783 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15785 DECL_RTL normally indicates where the parameter lives during most of the
15786 activation of the function. If optimization is enabled however, this
15787 could be either NULL or else a pseudo-reg. Both of those cases indicate
15788 that the parameter doesn't really live anywhere (as far as the code
15789 generation parts of GCC are concerned) during most of the function's
15790 activation. That will happen (for example) if the parameter is never
15791 referenced within the function.
15793 We could just generate a location descriptor here for all non-NULL
15794 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15795 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15796 where DECL_RTL is NULL or is a pseudo-reg.
15798 Note however that we can only get away with using DECL_INCOMING_RTL as
15799 a backup substitute for DECL_RTL in certain limited cases. In cases
15800 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15801 we can be sure that the parameter was passed using the same type as it is
15802 declared to have within the function, and that its DECL_INCOMING_RTL
15803 points us to a place where a value of that type is passed.
15805 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15806 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15807 because in these cases DECL_INCOMING_RTL points us to a value of some
15808 type which is *different* from the type of the parameter itself. Thus,
15809 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15810 such cases, the debugger would end up (for example) trying to fetch a
15811 `float' from a place which actually contains the first part of a
15812 `double'. That would lead to really incorrect and confusing
15813 output at debug-time.
15815 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15816 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15817 are a couple of exceptions however. On little-endian machines we can
15818 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15819 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15820 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15821 when (on a little-endian machine) a non-prototyped function has a
15822 parameter declared to be of type `short' or `char'. In such cases,
15823 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15824 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15825 passed `int' value. If the debugger then uses that address to fetch
15826 a `short' or a `char' (on a little-endian machine) the result will be
15827 the correct data, so we allow for such exceptional cases below.
15829 Note that our goal here is to describe the place where the given formal
15830 parameter lives during most of the function's activation (i.e. between the
15831 end of the prologue and the start of the epilogue). We'll do that as best
15832 as we can. Note however that if the given formal parameter is modified
15833 sometime during the execution of the function, then a stack backtrace (at
15834 debug-time) will show the function as having been called with the *new*
15835 value rather than the value which was originally passed in. This happens
15836 rarely enough that it is not a major problem, but it *is* a problem, and
15837 I'd like to fix it.
15839 A future version of dwarf2out.c may generate two additional attributes for
15840 any given DW_TAG_formal_parameter DIE which will describe the "passed
15841 type" and the "passed location" for the given formal parameter in addition
15842 to the attributes we now generate to indicate the "declared type" and the
15843 "active location" for each parameter. This additional set of attributes
15844 could be used by debuggers for stack backtraces. Separately, note that
15845 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15846 This happens (for example) for inlined-instances of inline function formal
15847 parameters which are never referenced. This really shouldn't be
15848 happening. All PARM_DECL nodes should get valid non-NULL
15849 DECL_INCOMING_RTL values. FIXME. */
15851 /* Use DECL_RTL as the "location" unless we find something better. */
15852 rtl = DECL_RTL_IF_SET (decl);
15854 /* When generating abstract instances, ignore everything except
15855 constants, symbols living in memory, and symbols living in
15856 fixed registers. */
15857 if (! reload_completed)
15859 if (rtl
15860 && (CONSTANT_P (rtl)
15861 || (MEM_P (rtl)
15862 && CONSTANT_P (XEXP (rtl, 0)))
15863 || (REG_P (rtl)
15864 && TREE_CODE (decl) == VAR_DECL
15865 && TREE_STATIC (decl))))
15867 rtl = targetm.delegitimize_address (rtl);
15868 return rtl;
15870 rtl = NULL_RTX;
15872 else if (TREE_CODE (decl) == PARM_DECL)
15874 if (rtl == NULL_RTX
15875 || is_pseudo_reg (rtl)
15876 || (MEM_P (rtl)
15877 && is_pseudo_reg (XEXP (rtl, 0))
15878 && DECL_INCOMING_RTL (decl)
15879 && MEM_P (DECL_INCOMING_RTL (decl))
15880 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15882 tree declared_type = TREE_TYPE (decl);
15883 tree passed_type = DECL_ARG_TYPE (decl);
15884 machine_mode dmode = TYPE_MODE (declared_type);
15885 machine_mode pmode = TYPE_MODE (passed_type);
15887 /* This decl represents a formal parameter which was optimized out.
15888 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15889 all cases where (rtl == NULL_RTX) just below. */
15890 if (dmode == pmode)
15891 rtl = DECL_INCOMING_RTL (decl);
15892 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15893 && SCALAR_INT_MODE_P (dmode)
15894 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15895 && DECL_INCOMING_RTL (decl))
15897 rtx inc = DECL_INCOMING_RTL (decl);
15898 if (REG_P (inc))
15899 rtl = inc;
15900 else if (MEM_P (inc))
15902 if (BYTES_BIG_ENDIAN)
15903 rtl = adjust_address_nv (inc, dmode,
15904 GET_MODE_SIZE (pmode)
15905 - GET_MODE_SIZE (dmode));
15906 else
15907 rtl = inc;
15912 /* If the parm was passed in registers, but lives on the stack, then
15913 make a big endian correction if the mode of the type of the
15914 parameter is not the same as the mode of the rtl. */
15915 /* ??? This is the same series of checks that are made in dbxout.c before
15916 we reach the big endian correction code there. It isn't clear if all
15917 of these checks are necessary here, but keeping them all is the safe
15918 thing to do. */
15919 else if (MEM_P (rtl)
15920 && XEXP (rtl, 0) != const0_rtx
15921 && ! CONSTANT_P (XEXP (rtl, 0))
15922 /* Not passed in memory. */
15923 && !MEM_P (DECL_INCOMING_RTL (decl))
15924 /* Not passed by invisible reference. */
15925 && (!REG_P (XEXP (rtl, 0))
15926 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15927 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15928 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15929 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15930 #endif
15932 /* Big endian correction check. */
15933 && BYTES_BIG_ENDIAN
15934 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15935 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15936 < UNITS_PER_WORD))
15938 machine_mode addr_mode = get_address_mode (rtl);
15939 int offset = (UNITS_PER_WORD
15940 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15942 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15943 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15946 else if (TREE_CODE (decl) == VAR_DECL
15947 && rtl
15948 && MEM_P (rtl)
15949 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15950 && BYTES_BIG_ENDIAN)
15952 machine_mode addr_mode = get_address_mode (rtl);
15953 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15954 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15956 /* If a variable is declared "register" yet is smaller than
15957 a register, then if we store the variable to memory, it
15958 looks like we're storing a register-sized value, when in
15959 fact we are not. We need to adjust the offset of the
15960 storage location to reflect the actual value's bytes,
15961 else gdb will not be able to display it. */
15962 if (rsize > dsize)
15963 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15964 plus_constant (addr_mode, XEXP (rtl, 0),
15965 rsize - dsize));
15968 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15969 and will have been substituted directly into all expressions that use it.
15970 C does not have such a concept, but C++ and other languages do. */
15971 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15972 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15974 if (rtl)
15975 rtl = targetm.delegitimize_address (rtl);
15977 /* If we don't look past the constant pool, we risk emitting a
15978 reference to a constant pool entry that isn't referenced from
15979 code, and thus is not emitted. */
15980 if (rtl)
15981 rtl = avoid_constant_pool_reference (rtl);
15983 /* Try harder to get a rtl. If this symbol ends up not being emitted
15984 in the current CU, resolve_addr will remove the expression referencing
15985 it. */
15986 if (rtl == NULL_RTX
15987 && TREE_CODE (decl) == VAR_DECL
15988 && !DECL_EXTERNAL (decl)
15989 && TREE_STATIC (decl)
15990 && DECL_NAME (decl)
15991 && !DECL_HARD_REGISTER (decl)
15992 && DECL_MODE (decl) != VOIDmode)
15994 rtl = make_decl_rtl_for_debug (decl);
15995 if (!MEM_P (rtl)
15996 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15997 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15998 rtl = NULL_RTX;
16001 return rtl;
16004 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16005 returned. If so, the decl for the COMMON block is returned, and the
16006 value is the offset into the common block for the symbol. */
16008 static tree
16009 fortran_common (tree decl, HOST_WIDE_INT *value)
16011 tree val_expr, cvar;
16012 machine_mode mode;
16013 HOST_WIDE_INT bitsize, bitpos;
16014 tree offset;
16015 int unsignedp, volatilep = 0;
16017 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16018 it does not have a value (the offset into the common area), or if it
16019 is thread local (as opposed to global) then it isn't common, and shouldn't
16020 be handled as such. */
16021 if (TREE_CODE (decl) != VAR_DECL
16022 || !TREE_STATIC (decl)
16023 || !DECL_HAS_VALUE_EXPR_P (decl)
16024 || !is_fortran ())
16025 return NULL_TREE;
16027 val_expr = DECL_VALUE_EXPR (decl);
16028 if (TREE_CODE (val_expr) != COMPONENT_REF)
16029 return NULL_TREE;
16031 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16032 &mode, &unsignedp, &volatilep, true);
16034 if (cvar == NULL_TREE
16035 || TREE_CODE (cvar) != VAR_DECL
16036 || DECL_ARTIFICIAL (cvar)
16037 || !TREE_PUBLIC (cvar))
16038 return NULL_TREE;
16040 *value = 0;
16041 if (offset != NULL)
16043 if (!tree_fits_shwi_p (offset))
16044 return NULL_TREE;
16045 *value = tree_to_shwi (offset);
16047 if (bitpos != 0)
16048 *value += bitpos / BITS_PER_UNIT;
16050 return cvar;
16053 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16054 data attribute for a variable or a parameter. We generate the
16055 DW_AT_const_value attribute only in those cases where the given variable
16056 or parameter does not have a true "location" either in memory or in a
16057 register. This can happen (for example) when a constant is passed as an
16058 actual argument in a call to an inline function. (It's possible that
16059 these things can crop up in other ways also.) Note that one type of
16060 constant value which can be passed into an inlined function is a constant
16061 pointer. This can happen for example if an actual argument in an inlined
16062 function call evaluates to a compile-time constant address.
16064 CACHE_P is true if it is worth caching the location list for DECL,
16065 so that future calls can reuse it rather than regenerate it from scratch.
16066 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16067 since we will need to refer to them each time the function is inlined. */
16069 static bool
16070 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16071 enum dwarf_attribute attr)
16073 rtx rtl;
16074 dw_loc_list_ref list;
16075 var_loc_list *loc_list;
16076 cached_dw_loc_list *cache;
16078 if (TREE_CODE (decl) == ERROR_MARK)
16079 return false;
16081 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16082 || TREE_CODE (decl) == RESULT_DECL);
16084 /* Try to get some constant RTL for this decl, and use that as the value of
16085 the location. */
16087 rtl = rtl_for_decl_location (decl);
16088 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16089 && add_const_value_attribute (die, rtl))
16090 return true;
16092 /* See if we have single element location list that is equivalent to
16093 a constant value. That way we are better to use add_const_value_attribute
16094 rather than expanding constant value equivalent. */
16095 loc_list = lookup_decl_loc (decl);
16096 if (loc_list
16097 && loc_list->first
16098 && loc_list->first->next == NULL
16099 && NOTE_P (loc_list->first->loc)
16100 && NOTE_VAR_LOCATION (loc_list->first->loc)
16101 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16103 struct var_loc_node *node;
16105 node = loc_list->first;
16106 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16107 if (GET_CODE (rtl) == EXPR_LIST)
16108 rtl = XEXP (rtl, 0);
16109 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16110 && add_const_value_attribute (die, rtl))
16111 return true;
16113 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16114 list several times. See if we've already cached the contents. */
16115 list = NULL;
16116 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16117 cache_p = false;
16118 if (cache_p)
16120 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16121 if (cache)
16122 list = cache->loc_list;
16124 if (list == NULL)
16126 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16127 NULL);
16128 /* It is usually worth caching this result if the decl is from
16129 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16130 if (cache_p && list && list->dw_loc_next)
16132 cached_dw_loc_list **slot
16133 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16134 DECL_UID (decl),
16135 INSERT);
16136 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16137 cache->decl_id = DECL_UID (decl);
16138 cache->loc_list = list;
16139 *slot = cache;
16142 if (list)
16144 add_AT_location_description (die, attr, list);
16145 return true;
16147 /* None of that worked, so it must not really have a location;
16148 try adding a constant value attribute from the DECL_INITIAL. */
16149 return tree_add_const_value_attribute_for_decl (die, decl);
16152 /* Add VARIABLE and DIE into deferred locations list. */
16154 static void
16155 defer_location (tree variable, dw_die_ref die)
16157 deferred_locations entry;
16158 entry.variable = variable;
16159 entry.die = die;
16160 vec_safe_push (deferred_locations_list, entry);
16163 /* Helper function for tree_add_const_value_attribute. Natively encode
16164 initializer INIT into an array. Return true if successful. */
16166 static bool
16167 native_encode_initializer (tree init, unsigned char *array, int size)
16169 tree type;
16171 if (init == NULL_TREE)
16172 return false;
16174 STRIP_NOPS (init);
16175 switch (TREE_CODE (init))
16177 case STRING_CST:
16178 type = TREE_TYPE (init);
16179 if (TREE_CODE (type) == ARRAY_TYPE)
16181 tree enttype = TREE_TYPE (type);
16182 machine_mode mode = TYPE_MODE (enttype);
16184 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16185 return false;
16186 if (int_size_in_bytes (type) != size)
16187 return false;
16188 if (size > TREE_STRING_LENGTH (init))
16190 memcpy (array, TREE_STRING_POINTER (init),
16191 TREE_STRING_LENGTH (init));
16192 memset (array + TREE_STRING_LENGTH (init),
16193 '\0', size - TREE_STRING_LENGTH (init));
16195 else
16196 memcpy (array, TREE_STRING_POINTER (init), size);
16197 return true;
16199 return false;
16200 case CONSTRUCTOR:
16201 type = TREE_TYPE (init);
16202 if (int_size_in_bytes (type) != size)
16203 return false;
16204 if (TREE_CODE (type) == ARRAY_TYPE)
16206 HOST_WIDE_INT min_index;
16207 unsigned HOST_WIDE_INT cnt;
16208 int curpos = 0, fieldsize;
16209 constructor_elt *ce;
16211 if (TYPE_DOMAIN (type) == NULL_TREE
16212 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16213 return false;
16215 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16216 if (fieldsize <= 0)
16217 return false;
16219 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16220 memset (array, '\0', size);
16221 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16223 tree val = ce->value;
16224 tree index = ce->index;
16225 int pos = curpos;
16226 if (index && TREE_CODE (index) == RANGE_EXPR)
16227 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16228 * fieldsize;
16229 else if (index)
16230 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16232 if (val)
16234 STRIP_NOPS (val);
16235 if (!native_encode_initializer (val, array + pos, fieldsize))
16236 return false;
16238 curpos = pos + fieldsize;
16239 if (index && TREE_CODE (index) == RANGE_EXPR)
16241 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16242 - tree_to_shwi (TREE_OPERAND (index, 0));
16243 while (count-- > 0)
16245 if (val)
16246 memcpy (array + curpos, array + pos, fieldsize);
16247 curpos += fieldsize;
16250 gcc_assert (curpos <= size);
16252 return true;
16254 else if (TREE_CODE (type) == RECORD_TYPE
16255 || TREE_CODE (type) == UNION_TYPE)
16257 tree field = NULL_TREE;
16258 unsigned HOST_WIDE_INT cnt;
16259 constructor_elt *ce;
16261 if (int_size_in_bytes (type) != size)
16262 return false;
16264 if (TREE_CODE (type) == RECORD_TYPE)
16265 field = TYPE_FIELDS (type);
16267 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16269 tree val = ce->value;
16270 int pos, fieldsize;
16272 if (ce->index != 0)
16273 field = ce->index;
16275 if (val)
16276 STRIP_NOPS (val);
16278 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16279 return false;
16281 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16282 && TYPE_DOMAIN (TREE_TYPE (field))
16283 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16284 return false;
16285 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16286 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16287 return false;
16288 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16289 pos = int_byte_position (field);
16290 gcc_assert (pos + fieldsize <= size);
16291 if (val
16292 && !native_encode_initializer (val, array + pos, fieldsize))
16293 return false;
16295 return true;
16297 return false;
16298 case VIEW_CONVERT_EXPR:
16299 case NON_LVALUE_EXPR:
16300 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16301 default:
16302 return native_encode_expr (init, array, size) == size;
16306 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16307 attribute is the const value T. */
16309 static bool
16310 tree_add_const_value_attribute (dw_die_ref die, tree t)
16312 tree init;
16313 tree type = TREE_TYPE (t);
16314 rtx rtl;
16316 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16317 return false;
16319 init = t;
16320 gcc_assert (!DECL_P (init));
16322 rtl = rtl_for_decl_init (init, type);
16323 if (rtl)
16324 return add_const_value_attribute (die, rtl);
16325 /* If the host and target are sane, try harder. */
16326 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16327 && initializer_constant_valid_p (init, type))
16329 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16330 if (size > 0 && (int) size == size)
16332 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16334 if (native_encode_initializer (init, array, size))
16336 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16337 return true;
16339 ggc_free (array);
16342 return false;
16345 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16346 attribute is the const value of T, where T is an integral constant
16347 variable with static storage duration
16348 (so it can't be a PARM_DECL or a RESULT_DECL). */
16350 static bool
16351 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16354 if (!decl
16355 || (TREE_CODE (decl) != VAR_DECL
16356 && TREE_CODE (decl) != CONST_DECL)
16357 || (TREE_CODE (decl) == VAR_DECL
16358 && !TREE_STATIC (decl)))
16359 return false;
16361 if (TREE_READONLY (decl)
16362 && ! TREE_THIS_VOLATILE (decl)
16363 && DECL_INITIAL (decl))
16364 /* OK */;
16365 else
16366 return false;
16368 /* Don't add DW_AT_const_value if abstract origin already has one. */
16369 if (get_AT (var_die, DW_AT_const_value))
16370 return false;
16372 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16375 /* Convert the CFI instructions for the current function into a
16376 location list. This is used for DW_AT_frame_base when we targeting
16377 a dwarf2 consumer that does not support the dwarf3
16378 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16379 expressions. */
16381 static dw_loc_list_ref
16382 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16384 int ix;
16385 dw_fde_ref fde;
16386 dw_loc_list_ref list, *list_tail;
16387 dw_cfi_ref cfi;
16388 dw_cfa_location last_cfa, next_cfa;
16389 const char *start_label, *last_label, *section;
16390 dw_cfa_location remember;
16392 fde = cfun->fde;
16393 gcc_assert (fde != NULL);
16395 section = secname_for_decl (current_function_decl);
16396 list_tail = &list;
16397 list = NULL;
16399 memset (&next_cfa, 0, sizeof (next_cfa));
16400 next_cfa.reg = INVALID_REGNUM;
16401 remember = next_cfa;
16403 start_label = fde->dw_fde_begin;
16405 /* ??? Bald assumption that the CIE opcode list does not contain
16406 advance opcodes. */
16407 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16408 lookup_cfa_1 (cfi, &next_cfa, &remember);
16410 last_cfa = next_cfa;
16411 last_label = start_label;
16413 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16415 /* If the first partition contained no CFI adjustments, the
16416 CIE opcodes apply to the whole first partition. */
16417 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16418 fde->dw_fde_begin, fde->dw_fde_end, section);
16419 list_tail =&(*list_tail)->dw_loc_next;
16420 start_label = last_label = fde->dw_fde_second_begin;
16423 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16425 switch (cfi->dw_cfi_opc)
16427 case DW_CFA_set_loc:
16428 case DW_CFA_advance_loc1:
16429 case DW_CFA_advance_loc2:
16430 case DW_CFA_advance_loc4:
16431 if (!cfa_equal_p (&last_cfa, &next_cfa))
16433 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16434 start_label, last_label, section);
16436 list_tail = &(*list_tail)->dw_loc_next;
16437 last_cfa = next_cfa;
16438 start_label = last_label;
16440 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16441 break;
16443 case DW_CFA_advance_loc:
16444 /* The encoding is complex enough that we should never emit this. */
16445 gcc_unreachable ();
16447 default:
16448 lookup_cfa_1 (cfi, &next_cfa, &remember);
16449 break;
16451 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16453 if (!cfa_equal_p (&last_cfa, &next_cfa))
16455 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16456 start_label, last_label, section);
16458 list_tail = &(*list_tail)->dw_loc_next;
16459 last_cfa = next_cfa;
16460 start_label = last_label;
16462 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16463 start_label, fde->dw_fde_end, section);
16464 list_tail = &(*list_tail)->dw_loc_next;
16465 start_label = last_label = fde->dw_fde_second_begin;
16469 if (!cfa_equal_p (&last_cfa, &next_cfa))
16471 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16472 start_label, last_label, section);
16473 list_tail = &(*list_tail)->dw_loc_next;
16474 start_label = last_label;
16477 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16478 start_label,
16479 fde->dw_fde_second_begin
16480 ? fde->dw_fde_second_end : fde->dw_fde_end,
16481 section);
16483 if (list && list->dw_loc_next)
16484 gen_llsym (list);
16486 return list;
16489 /* Compute a displacement from the "steady-state frame pointer" to the
16490 frame base (often the same as the CFA), and store it in
16491 frame_pointer_fb_offset. OFFSET is added to the displacement
16492 before the latter is negated. */
16494 static void
16495 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16497 rtx reg, elim;
16499 #ifdef FRAME_POINTER_CFA_OFFSET
16500 reg = frame_pointer_rtx;
16501 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16502 #else
16503 reg = arg_pointer_rtx;
16504 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16505 #endif
16507 elim = (ira_use_lra_p
16508 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16509 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16510 if (GET_CODE (elim) == PLUS)
16512 offset += INTVAL (XEXP (elim, 1));
16513 elim = XEXP (elim, 0);
16516 frame_pointer_fb_offset = -offset;
16518 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16519 in which to eliminate. This is because it's stack pointer isn't
16520 directly accessible as a register within the ISA. To work around
16521 this, assume that while we cannot provide a proper value for
16522 frame_pointer_fb_offset, we won't need one either. */
16523 frame_pointer_fb_offset_valid
16524 = ((SUPPORTS_STACK_ALIGNMENT
16525 && (elim == hard_frame_pointer_rtx
16526 || elim == stack_pointer_rtx))
16527 || elim == (frame_pointer_needed
16528 ? hard_frame_pointer_rtx
16529 : stack_pointer_rtx));
16532 /* Generate a DW_AT_name attribute given some string value to be included as
16533 the value of the attribute. */
16535 static void
16536 add_name_attribute (dw_die_ref die, const char *name_string)
16538 if (name_string != NULL && *name_string != 0)
16540 if (demangle_name_func)
16541 name_string = (*demangle_name_func) (name_string);
16543 add_AT_string (die, DW_AT_name, name_string);
16547 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16548 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16549 of TYPE accordingly.
16551 ??? This is a temporary measure until after we're able to generate
16552 regular DWARF for the complex Ada type system. */
16554 static void
16555 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16556 dw_die_ref context_die)
16558 tree dtype;
16559 dw_die_ref dtype_die;
16561 if (!lang_hooks.types.descriptive_type)
16562 return;
16564 dtype = lang_hooks.types.descriptive_type (type);
16565 if (!dtype)
16566 return;
16568 dtype_die = lookup_type_die (dtype);
16569 if (!dtype_die)
16571 gen_type_die (dtype, context_die);
16572 dtype_die = lookup_type_die (dtype);
16573 gcc_assert (dtype_die);
16576 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16579 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16581 static const char *
16582 comp_dir_string (void)
16584 const char *wd;
16585 char *wd1;
16586 static const char *cached_wd = NULL;
16588 if (cached_wd != NULL)
16589 return cached_wd;
16591 wd = get_src_pwd ();
16592 if (wd == NULL)
16593 return NULL;
16595 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16597 int wdlen;
16599 wdlen = strlen (wd);
16600 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16601 strcpy (wd1, wd);
16602 wd1 [wdlen] = DIR_SEPARATOR;
16603 wd1 [wdlen + 1] = 0;
16604 wd = wd1;
16607 cached_wd = remap_debug_filename (wd);
16608 return cached_wd;
16611 /* Generate a DW_AT_comp_dir attribute for DIE. */
16613 static void
16614 add_comp_dir_attribute (dw_die_ref die)
16616 const char * wd = comp_dir_string ();
16617 if (wd != NULL)
16618 add_AT_string (die, DW_AT_comp_dir, wd);
16621 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16622 pointer computation, ...), output a representation for that bound according
16623 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16624 loc_list_from_tree for the meaning of CONTEXT. */
16626 static void
16627 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16628 int forms, const struct loc_descr_context *context)
16630 dw_die_ref ctx, decl_die;
16631 dw_loc_list_ref list;
16633 bool strip_conversions = true;
16635 while (strip_conversions)
16636 switch (TREE_CODE (value))
16638 case ERROR_MARK:
16639 case SAVE_EXPR:
16640 return;
16642 CASE_CONVERT:
16643 case VIEW_CONVERT_EXPR:
16644 value = TREE_OPERAND (value, 0);
16645 break;
16647 default:
16648 strip_conversions = false;
16649 break;
16652 /* If possible and permitted, output the attribute as a constant. */
16653 if ((forms & dw_scalar_form_constant) != 0
16654 && TREE_CODE (value) == INTEGER_CST)
16656 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16658 /* If HOST_WIDE_INT is big enough then represent the bound as
16659 a constant value. We need to choose a form based on
16660 whether the type is signed or unsigned. We cannot just
16661 call add_AT_unsigned if the value itself is positive
16662 (add_AT_unsigned might add the unsigned value encoded as
16663 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16664 bounds type and then sign extend any unsigned values found
16665 for signed types. This is needed only for
16666 DW_AT_{lower,upper}_bound, since for most other attributes,
16667 consumers will treat DW_FORM_data[1248] as unsigned values,
16668 regardless of the underlying type. */
16669 if (prec <= HOST_BITS_PER_WIDE_INT
16670 || tree_fits_uhwi_p (value))
16672 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16673 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16674 else
16675 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16677 else
16678 /* Otherwise represent the bound as an unsigned value with
16679 the precision of its type. The precision and signedness
16680 of the type will be necessary to re-interpret it
16681 unambiguously. */
16682 add_AT_wide (die, attr, value);
16683 return;
16686 /* Otherwise, if it's possible and permitted too, output a reference to
16687 another DIE. */
16688 if ((forms & dw_scalar_form_reference) != 0)
16690 tree decl = NULL_TREE;
16692 /* Some type attributes reference an outer type. For instance, the upper
16693 bound of an array may reference an embedding record (this happens in
16694 Ada). */
16695 if (TREE_CODE (value) == COMPONENT_REF
16696 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16697 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16698 decl = TREE_OPERAND (value, 1);
16700 else if (TREE_CODE (value) == VAR_DECL
16701 || TREE_CODE (value) == PARM_DECL
16702 || TREE_CODE (value) == RESULT_DECL)
16703 decl = value;
16705 if (decl != NULL_TREE)
16707 dw_die_ref decl_die = lookup_decl_die (decl);
16709 /* ??? Can this happen, or should the variable have been bound
16710 first? Probably it can, since I imagine that we try to create
16711 the types of parameters in the order in which they exist in
16712 the list, and won't have created a forward reference to a
16713 later parameter. */
16714 if (decl_die != NULL)
16716 add_AT_die_ref (die, attr, decl_die);
16717 return;
16722 /* Last chance: try to create a stack operation procedure to evaluate the
16723 value. Do nothing if even that is not possible or permitted. */
16724 if ((forms & dw_scalar_form_exprloc) == 0)
16725 return;
16727 list = loc_list_from_tree (value, 2, context);
16728 if (list == NULL || single_element_loc_list_p (list))
16730 /* If this attribute is not a reference nor constant, it is
16731 a DWARF expression rather than location description. For that
16732 loc_list_from_tree (value, 0, &context) is needed. */
16733 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16734 if (list2 && single_element_loc_list_p (list2))
16736 add_AT_loc (die, attr, list2->expr);
16737 return;
16741 /* If that failed to give a single element location list, fall back to
16742 outputting this as a reference... still if permitted. */
16743 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16744 return;
16746 if (current_function_decl == 0)
16747 ctx = comp_unit_die ();
16748 else
16749 ctx = lookup_decl_die (current_function_decl);
16751 decl_die = new_die (DW_TAG_variable, ctx, value);
16752 add_AT_flag (decl_die, DW_AT_artificial, 1);
16753 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16754 add_AT_location_description (decl_die, DW_AT_location, list);
16755 add_AT_die_ref (die, attr, decl_die);
16758 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16759 default. */
16761 static int
16762 lower_bound_default (void)
16764 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16766 case DW_LANG_C:
16767 case DW_LANG_C89:
16768 case DW_LANG_C99:
16769 case DW_LANG_C11:
16770 case DW_LANG_C_plus_plus:
16771 case DW_LANG_C_plus_plus_11:
16772 case DW_LANG_C_plus_plus_14:
16773 case DW_LANG_ObjC:
16774 case DW_LANG_ObjC_plus_plus:
16775 case DW_LANG_Java:
16776 return 0;
16777 case DW_LANG_Fortran77:
16778 case DW_LANG_Fortran90:
16779 case DW_LANG_Fortran95:
16780 case DW_LANG_Fortran03:
16781 case DW_LANG_Fortran08:
16782 return 1;
16783 case DW_LANG_UPC:
16784 case DW_LANG_D:
16785 case DW_LANG_Python:
16786 return dwarf_version >= 4 ? 0 : -1;
16787 case DW_LANG_Ada95:
16788 case DW_LANG_Ada83:
16789 case DW_LANG_Cobol74:
16790 case DW_LANG_Cobol85:
16791 case DW_LANG_Pascal83:
16792 case DW_LANG_Modula2:
16793 case DW_LANG_PLI:
16794 return dwarf_version >= 4 ? 1 : -1;
16795 default:
16796 return -1;
16800 /* Given a tree node describing an array bound (either lower or upper) output
16801 a representation for that bound. */
16803 static void
16804 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16805 tree bound, const struct loc_descr_context *context)
16807 int dflt;
16809 while (1)
16810 switch (TREE_CODE (bound))
16812 /* Strip all conversions. */
16813 CASE_CONVERT:
16814 case VIEW_CONVERT_EXPR:
16815 bound = TREE_OPERAND (bound, 0);
16816 break;
16818 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16819 are even omitted when they are the default. */
16820 case INTEGER_CST:
16821 /* If the value for this bound is the default one, we can even omit the
16822 attribute. */
16823 if (bound_attr == DW_AT_lower_bound
16824 && tree_fits_shwi_p (bound)
16825 && (dflt = lower_bound_default ()) != -1
16826 && tree_to_shwi (bound) == dflt)
16827 return;
16829 /* FALLTHRU */
16831 default:
16832 add_scalar_info (subrange_die, bound_attr, bound,
16833 dw_scalar_form_constant
16834 | dw_scalar_form_exprloc
16835 | dw_scalar_form_reference,
16836 context);
16837 return;
16841 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16842 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16843 Note that the block of subscript information for an array type also
16844 includes information about the element type of the given array type. */
16846 static void
16847 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16849 unsigned dimension_number;
16850 tree lower, upper;
16851 dw_die_ref subrange_die;
16853 for (dimension_number = 0;
16854 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16855 type = TREE_TYPE (type), dimension_number++)
16857 tree domain = TYPE_DOMAIN (type);
16859 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16860 break;
16862 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16863 and (in GNU C only) variable bounds. Handle all three forms
16864 here. */
16865 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16867 if (use_upc_dwarf2_extensions && TYPE_HAS_THREADS_FACTOR (type))
16869 add_AT_flag (subrange_die, DW_AT_upc_threads_scaled, 1);
16872 if (domain)
16874 /* We have an array type with specified bounds. */
16875 lower = TYPE_MIN_VALUE (domain);
16876 upper = TYPE_MAX_VALUE (domain);
16878 /* Define the index type. */
16879 if (TREE_TYPE (domain))
16881 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16882 TREE_TYPE field. We can't emit debug info for this
16883 because it is an unnamed integral type. */
16884 if (TREE_CODE (domain) == INTEGER_TYPE
16885 && TYPE_NAME (domain) == NULL_TREE
16886 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16887 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16889 else
16890 add_type_attribute (subrange_die, TREE_TYPE (domain),
16891 TYPE_UNQUALIFIED, type_die);
16894 /* ??? If upper is NULL, the array has unspecified length,
16895 but it does have a lower bound. This happens with Fortran
16896 dimension arr(N:*)
16897 Since the debugger is definitely going to need to know N
16898 to produce useful results, go ahead and output the lower
16899 bound solo, and hope the debugger can cope. */
16901 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16902 if (upper)
16903 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16906 /* Otherwise we have an array type with an unspecified length. The
16907 DWARF-2 spec does not say how to handle this; let's just leave out the
16908 bounds. */
16912 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16914 static void
16915 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16917 dw_die_ref decl_die;
16918 HOST_WIDE_INT size;
16920 switch (TREE_CODE (tree_node))
16922 case ERROR_MARK:
16923 size = 0;
16924 break;
16925 case ENUMERAL_TYPE:
16926 case RECORD_TYPE:
16927 case UNION_TYPE:
16928 case QUAL_UNION_TYPE:
16929 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16930 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16932 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16933 return;
16935 size = int_size_in_bytes (tree_node);
16936 break;
16937 case FIELD_DECL:
16938 /* For a data member of a struct or union, the DW_AT_byte_size is
16939 generally given as the number of bytes normally allocated for an
16940 object of the *declared* type of the member itself. This is true
16941 even for bit-fields. */
16942 size = int_size_in_bytes (field_type (tree_node));
16943 break;
16944 default:
16945 gcc_unreachable ();
16948 /* Note that `size' might be -1 when we get to this point. If it is, that
16949 indicates that the byte size of the entity in question is variable. We
16950 have no good way of expressing this fact in Dwarf at the present time,
16951 when location description was not used by the caller code instead. */
16952 if (size >= 0)
16953 add_AT_unsigned (die, DW_AT_byte_size, size);
16956 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16957 which specifies the distance in bits from the highest order bit of the
16958 "containing object" for the bit-field to the highest order bit of the
16959 bit-field itself.
16961 For any given bit-field, the "containing object" is a hypothetical object
16962 (of some integral or enum type) within which the given bit-field lives. The
16963 type of this hypothetical "containing object" is always the same as the
16964 declared type of the individual bit-field itself. The determination of the
16965 exact location of the "containing object" for a bit-field is rather
16966 complicated. It's handled by the `field_byte_offset' function (above).
16968 Note that it is the size (in bytes) of the hypothetical "containing object"
16969 which will be given in the DW_AT_byte_size attribute for this bit-field.
16970 (See `byte_size_attribute' above). */
16972 static inline void
16973 add_bit_offset_attribute (dw_die_ref die, tree decl)
16975 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16976 tree type = DECL_BIT_FIELD_TYPE (decl);
16977 HOST_WIDE_INT bitpos_int;
16978 HOST_WIDE_INT highest_order_object_bit_offset;
16979 HOST_WIDE_INT highest_order_field_bit_offset;
16980 HOST_WIDE_INT bit_offset;
16982 /* Must be a field and a bit field. */
16983 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16985 /* We can't yet handle bit-fields whose offsets are variable, so if we
16986 encounter such things, just return without generating any attribute
16987 whatsoever. Likewise for variable or too large size. */
16988 if (! tree_fits_shwi_p (bit_position (decl))
16989 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16990 return;
16992 bitpos_int = int_bit_position (decl);
16994 /* Note that the bit offset is always the distance (in bits) from the
16995 highest-order bit of the "containing object" to the highest-order bit of
16996 the bit-field itself. Since the "high-order end" of any object or field
16997 is different on big-endian and little-endian machines, the computation
16998 below must take account of these differences. */
16999 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17000 highest_order_field_bit_offset = bitpos_int;
17002 if (! BYTES_BIG_ENDIAN)
17004 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17005 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17008 bit_offset
17009 = (! BYTES_BIG_ENDIAN
17010 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17011 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17013 if (bit_offset < 0)
17014 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17015 else
17016 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17019 /* For a FIELD_DECL node which represents a bit field, output an attribute
17020 which specifies the length in bits of the given field. */
17022 static inline void
17023 add_bit_size_attribute (dw_die_ref die, tree decl)
17025 /* Must be a field and a bit field. */
17026 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17027 && DECL_BIT_FIELD_TYPE (decl));
17029 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17030 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17033 /* If the compiled language is ANSI C, then add a 'prototyped'
17034 attribute, if arg types are given for the parameters of a function. */
17036 static inline void
17037 add_prototyped_attribute (dw_die_ref die, tree func_type)
17039 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17041 case DW_LANG_C:
17042 case DW_LANG_C89:
17043 case DW_LANG_C99:
17044 case DW_LANG_C11:
17045 case DW_LANG_ObjC:
17046 if (prototype_p (func_type))
17047 add_AT_flag (die, DW_AT_prototyped, 1);
17048 break;
17049 default:
17050 break;
17054 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17055 by looking in either the type declaration or object declaration
17056 equate table. */
17058 static inline dw_die_ref
17059 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17061 dw_die_ref origin_die = NULL;
17063 if (TREE_CODE (origin) != FUNCTION_DECL)
17065 /* We may have gotten separated from the block for the inlined
17066 function, if we're in an exception handler or some such; make
17067 sure that the abstract function has been written out.
17069 Doing this for nested functions is wrong, however; functions are
17070 distinct units, and our context might not even be inline. */
17071 tree fn = origin;
17073 if (TYPE_P (fn))
17074 fn = TYPE_STUB_DECL (fn);
17076 fn = decl_function_context (fn);
17077 if (fn)
17078 dwarf2out_abstract_function (fn);
17081 if (DECL_P (origin))
17082 origin_die = lookup_decl_die (origin);
17083 else if (TYPE_P (origin))
17084 origin_die = lookup_type_die (origin);
17086 /* XXX: Functions that are never lowered don't always have correct block
17087 trees (in the case of java, they simply have no block tree, in some other
17088 languages). For these functions, there is nothing we can really do to
17089 output correct debug info for inlined functions in all cases. Rather
17090 than die, we'll just produce deficient debug info now, in that we will
17091 have variables without a proper abstract origin. In the future, when all
17092 functions are lowered, we should re-add a gcc_assert (origin_die)
17093 here. */
17095 if (origin_die)
17096 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17097 return origin_die;
17100 /* We do not currently support the pure_virtual attribute. */
17102 static inline void
17103 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17105 if (DECL_VINDEX (func_decl))
17107 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17109 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17110 add_AT_loc (die, DW_AT_vtable_elem_location,
17111 new_loc_descr (DW_OP_constu,
17112 tree_to_shwi (DECL_VINDEX (func_decl)),
17113 0));
17115 /* GNU extension: Record what type this method came from originally. */
17116 if (debug_info_level > DINFO_LEVEL_TERSE
17117 && DECL_CONTEXT (func_decl))
17118 add_AT_die_ref (die, DW_AT_containing_type,
17119 lookup_type_die (DECL_CONTEXT (func_decl)));
17123 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17124 given decl. This used to be a vendor extension until after DWARF 4
17125 standardized it. */
17127 static void
17128 add_linkage_attr (dw_die_ref die, tree decl)
17130 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17132 /* Mimic what assemble_name_raw does with a leading '*'. */
17133 if (name[0] == '*')
17134 name = &name[1];
17136 if (dwarf_version >= 4)
17137 add_AT_string (die, DW_AT_linkage_name, name);
17138 else
17139 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17142 /* Add source coordinate attributes for the given decl. */
17144 static void
17145 add_src_coords_attributes (dw_die_ref die, tree decl)
17147 expanded_location s;
17149 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17150 return;
17151 s = expand_location (DECL_SOURCE_LOCATION (decl));
17152 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17153 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17156 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17158 static void
17159 add_linkage_name (dw_die_ref die, tree decl)
17161 if (debug_info_level > DINFO_LEVEL_NONE
17162 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17163 && TREE_PUBLIC (decl)
17164 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17165 && die->die_tag != DW_TAG_member)
17167 /* Defer until we have an assembler name set. */
17168 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17170 limbo_die_node *asm_name;
17172 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17173 asm_name->die = die;
17174 asm_name->created_for = decl;
17175 asm_name->next = deferred_asm_name;
17176 deferred_asm_name = asm_name;
17178 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17179 add_linkage_attr (die, decl);
17183 /* Add a DW_AT_name attribute and source coordinate attribute for the
17184 given decl, but only if it actually has a name. */
17186 static void
17187 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17189 tree decl_name;
17191 decl_name = DECL_NAME (decl);
17192 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17194 const char *name = dwarf2_name (decl, 0);
17195 if (name)
17196 add_name_attribute (die, name);
17197 if (! DECL_ARTIFICIAL (decl))
17198 add_src_coords_attributes (die, decl);
17200 add_linkage_name (die, decl);
17203 #ifdef VMS_DEBUGGING_INFO
17204 /* Get the function's name, as described by its RTL. This may be different
17205 from the DECL_NAME name used in the source file. */
17206 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17208 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17209 XEXP (DECL_RTL (decl), 0), false);
17210 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17212 #endif /* VMS_DEBUGGING_INFO */
17215 #ifdef VMS_DEBUGGING_INFO
17216 /* Output the debug main pointer die for VMS */
17218 void
17219 dwarf2out_vms_debug_main_pointer (void)
17221 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17222 dw_die_ref die;
17224 /* Allocate the VMS debug main subprogram die. */
17225 die = ggc_cleared_alloc<die_node> ();
17226 die->die_tag = DW_TAG_subprogram;
17227 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17228 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17229 current_function_funcdef_no);
17230 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17232 /* Make it the first child of comp_unit_die (). */
17233 die->die_parent = comp_unit_die ();
17234 if (comp_unit_die ()->die_child)
17236 die->die_sib = comp_unit_die ()->die_child->die_sib;
17237 comp_unit_die ()->die_child->die_sib = die;
17239 else
17241 die->die_sib = die;
17242 comp_unit_die ()->die_child = die;
17245 #endif /* VMS_DEBUGGING_INFO */
17247 /* Push a new declaration scope. */
17249 static void
17250 push_decl_scope (tree scope)
17252 vec_safe_push (decl_scope_table, scope);
17255 /* Pop a declaration scope. */
17257 static inline void
17258 pop_decl_scope (void)
17260 decl_scope_table->pop ();
17263 /* walk_tree helper function for uses_local_type, below. */
17265 static tree
17266 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17268 if (!TYPE_P (*tp))
17269 *walk_subtrees = 0;
17270 else
17272 tree name = TYPE_NAME (*tp);
17273 if (name && DECL_P (name) && decl_function_context (name))
17274 return *tp;
17276 return NULL_TREE;
17279 /* If TYPE involves a function-local type (including a local typedef to a
17280 non-local type), returns that type; otherwise returns NULL_TREE. */
17282 static tree
17283 uses_local_type (tree type)
17285 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17286 return used;
17289 /* Return the DIE for the scope that immediately contains this type.
17290 Non-named types that do not involve a function-local type get global
17291 scope. Named types nested in namespaces or other types get their
17292 containing scope. All other types (i.e. function-local named types) get
17293 the current active scope. */
17295 static dw_die_ref
17296 scope_die_for (tree t, dw_die_ref context_die)
17298 dw_die_ref scope_die = NULL;
17299 tree containing_scope;
17301 /* Non-types always go in the current scope. */
17302 gcc_assert (TYPE_P (t));
17304 /* Use the scope of the typedef, rather than the scope of the type
17305 it refers to. */
17306 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17307 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17308 else
17309 containing_scope = TYPE_CONTEXT (t);
17311 /* Use the containing namespace if there is one. */
17312 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17314 if (context_die == lookup_decl_die (containing_scope))
17315 /* OK */;
17316 else if (debug_info_level > DINFO_LEVEL_TERSE)
17317 context_die = get_context_die (containing_scope);
17318 else
17319 containing_scope = NULL_TREE;
17322 /* Ignore function type "scopes" from the C frontend. They mean that
17323 a tagged type is local to a parmlist of a function declarator, but
17324 that isn't useful to DWARF. */
17325 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17326 containing_scope = NULL_TREE;
17328 if (SCOPE_FILE_SCOPE_P (containing_scope))
17330 /* If T uses a local type keep it local as well, to avoid references
17331 to function-local DIEs from outside the function. */
17332 if (current_function_decl && uses_local_type (t))
17333 scope_die = context_die;
17334 else
17335 scope_die = comp_unit_die ();
17337 else if (TYPE_P (containing_scope))
17339 /* For types, we can just look up the appropriate DIE. */
17340 if (debug_info_level > DINFO_LEVEL_TERSE)
17341 scope_die = get_context_die (containing_scope);
17342 else
17344 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17345 if (scope_die == NULL)
17346 scope_die = comp_unit_die ();
17349 else
17350 scope_die = context_die;
17352 return scope_die;
17355 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17357 static inline int
17358 local_scope_p (dw_die_ref context_die)
17360 for (; context_die; context_die = context_die->die_parent)
17361 if (context_die->die_tag == DW_TAG_inlined_subroutine
17362 || context_die->die_tag == DW_TAG_subprogram)
17363 return 1;
17365 return 0;
17368 /* Returns nonzero if CONTEXT_DIE is a class. */
17370 static inline int
17371 class_scope_p (dw_die_ref context_die)
17373 return (context_die
17374 && (context_die->die_tag == DW_TAG_structure_type
17375 || context_die->die_tag == DW_TAG_class_type
17376 || context_die->die_tag == DW_TAG_interface_type
17377 || context_die->die_tag == DW_TAG_union_type));
17380 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17381 whether or not to treat a DIE in this context as a declaration. */
17383 static inline int
17384 class_or_namespace_scope_p (dw_die_ref context_die)
17386 return (class_scope_p (context_die)
17387 || (context_die && context_die->die_tag == DW_TAG_namespace));
17390 /* Many forms of DIEs require a "type description" attribute. This
17391 routine locates the proper "type descriptor" die for the type given
17392 by 'type' plus any additional qualifiers given by 'cv_quals', and
17393 adds a DW_AT_type attribute below the given die. */
17395 static void
17396 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17397 dw_die_ref context_die)
17399 enum tree_code code = TREE_CODE (type);
17400 dw_die_ref type_die = NULL;
17402 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17403 or fixed-point type, use the inner type. This is because we have no
17404 support for unnamed types in base_type_die. This can happen if this is
17405 an Ada subrange type. Correct solution is emit a subrange type die. */
17406 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17407 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17408 type = TREE_TYPE (type), code = TREE_CODE (type);
17410 if (code == ERROR_MARK
17411 /* Handle a special case. For functions whose return type is void, we
17412 generate *no* type attribute. (Note that no object may have type
17413 `void', so this only applies to function return types). */
17414 || code == VOID_TYPE)
17415 return;
17417 type_die = modified_type_die (type,
17418 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17419 context_die);
17421 if (type_die != NULL)
17422 add_AT_die_ref (object_die, DW_AT_type, type_die);
17425 /* Given an object die, add the calling convention attribute for the
17426 function call type. */
17427 static void
17428 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17430 enum dwarf_calling_convention value = DW_CC_normal;
17432 value = ((enum dwarf_calling_convention)
17433 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17435 if (is_fortran ()
17436 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17438 /* DWARF 2 doesn't provide a way to identify a program's source-level
17439 entry point. DW_AT_calling_convention attributes are only meant
17440 to describe functions' calling conventions. However, lacking a
17441 better way to signal the Fortran main program, we used this for
17442 a long time, following existing custom. Now, DWARF 4 has
17443 DW_AT_main_subprogram, which we add below, but some tools still
17444 rely on the old way, which we thus keep. */
17445 value = DW_CC_program;
17447 if (dwarf_version >= 4 || !dwarf_strict)
17448 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17451 /* Only add the attribute if the backend requests it, and
17452 is not DW_CC_normal. */
17453 if (value && (value != DW_CC_normal))
17454 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17457 /* Given a tree pointer to a struct, class, union, or enum type node, return
17458 a pointer to the (string) tag name for the given type, or zero if the type
17459 was declared without a tag. */
17461 static const char *
17462 type_tag (const_tree type)
17464 const char *name = 0;
17466 if (TYPE_NAME (type) != 0)
17468 tree t = 0;
17470 /* Find the IDENTIFIER_NODE for the type name. */
17471 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17472 && !TYPE_NAMELESS (type))
17473 t = TYPE_NAME (type);
17475 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17476 a TYPE_DECL node, regardless of whether or not a `typedef' was
17477 involved. */
17478 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17479 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17481 /* We want to be extra verbose. Don't call dwarf_name if
17482 DECL_NAME isn't set. The default hook for decl_printable_name
17483 doesn't like that, and in this context it's correct to return
17484 0, instead of "<anonymous>" or the like. */
17485 if (DECL_NAME (TYPE_NAME (type))
17486 && !DECL_NAMELESS (TYPE_NAME (type)))
17487 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17490 /* Now get the name as a string, or invent one. */
17491 if (!name && t != 0)
17492 name = IDENTIFIER_POINTER (t);
17495 return (name == 0 || *name == '\0') ? 0 : name;
17498 /* Return the type associated with a data member, make a special check
17499 for bit field types. */
17501 static inline tree
17502 member_declared_type (const_tree member)
17504 return (DECL_BIT_FIELD_TYPE (member)
17505 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17508 /* Get the decl's label, as described by its RTL. This may be different
17509 from the DECL_NAME name used in the source file. */
17511 #if 0
17512 static const char *
17513 decl_start_label (tree decl)
17515 rtx x;
17516 const char *fnname;
17518 x = DECL_RTL (decl);
17519 gcc_assert (MEM_P (x));
17521 x = XEXP (x, 0);
17522 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17524 fnname = XSTR (x, 0);
17525 return fnname;
17527 #endif
17529 /* These routines generate the internal representation of the DIE's for
17530 the compilation unit. Debugging information is collected by walking
17531 the declaration trees passed in from dwarf2out_decl(). */
17533 static void
17534 gen_array_type_die (tree type, dw_die_ref context_die)
17536 dw_die_ref scope_die = scope_die_for (type, context_die);
17537 dw_die_ref array_die;
17539 /* GNU compilers represent multidimensional array types as sequences of one
17540 dimensional array types whose element types are themselves array types.
17541 We sometimes squish that down to a single array_type DIE with multiple
17542 subscripts in the Dwarf debugging info. The draft Dwarf specification
17543 say that we are allowed to do this kind of compression in C, because
17544 there is no difference between an array of arrays and a multidimensional
17545 array. We don't do this for Ada to remain as close as possible to the
17546 actual representation, which is especially important against the language
17547 flexibilty wrt arrays of variable size. */
17549 bool collapse_nested_arrays = !is_ada ();
17550 tree element_type;
17552 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17553 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17554 if (TYPE_STRING_FLAG (type)
17555 && TREE_CODE (type) == ARRAY_TYPE
17556 && is_fortran ()
17557 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17559 HOST_WIDE_INT size;
17561 array_die = new_die (DW_TAG_string_type, scope_die, type);
17562 add_name_attribute (array_die, type_tag (type));
17563 equate_type_number_to_die (type, array_die);
17564 size = int_size_in_bytes (type);
17565 if (size >= 0)
17566 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17567 else if (TYPE_DOMAIN (type) != NULL_TREE
17568 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17569 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17571 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17572 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17574 size = int_size_in_bytes (TREE_TYPE (szdecl));
17575 if (loc && size > 0)
17577 add_AT_location_description (array_die, DW_AT_string_length, loc);
17578 if (size != DWARF2_ADDR_SIZE)
17579 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17582 return;
17585 array_die = new_die (DW_TAG_array_type, scope_die, type);
17586 add_name_attribute (array_die, type_tag (type));
17587 equate_type_number_to_die (type, array_die);
17589 if (TREE_CODE (type) == VECTOR_TYPE)
17590 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17592 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17593 if (is_fortran ()
17594 && TREE_CODE (type) == ARRAY_TYPE
17595 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17596 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17597 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17599 #if 0
17600 /* We default the array ordering. SDB will probably do
17601 the right things even if DW_AT_ordering is not present. It's not even
17602 an issue until we start to get into multidimensional arrays anyway. If
17603 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17604 then we'll have to put the DW_AT_ordering attribute back in. (But if
17605 and when we find out that we need to put these in, we will only do so
17606 for multidimensional arrays. */
17607 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17608 #endif
17610 if (TREE_CODE (type) == VECTOR_TYPE)
17612 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17613 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17614 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17615 add_bound_info (subrange_die, DW_AT_upper_bound,
17616 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17618 else
17619 add_subscript_info (array_die, type, collapse_nested_arrays);
17621 /* Add representation of the type of the elements of this array type and
17622 emit the corresponding DIE if we haven't done it already. */
17623 element_type = TREE_TYPE (type);
17624 if (collapse_nested_arrays)
17625 while (TREE_CODE (element_type) == ARRAY_TYPE)
17627 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17628 break;
17629 element_type = TREE_TYPE (element_type);
17632 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17634 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17635 if (TYPE_ARTIFICIAL (type))
17636 add_AT_flag (array_die, DW_AT_artificial, 1);
17638 if (get_AT (array_die, DW_AT_name))
17639 add_pubtype (type, array_die);
17642 /* This routine generates DIE for array with hidden descriptor, details
17643 are filled into *info by a langhook. */
17645 static void
17646 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17647 dw_die_ref context_die)
17649 const dw_die_ref scope_die = scope_die_for (type, context_die);
17650 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17651 const struct loc_descr_context context = { type, info->base_decl };
17652 int dim;
17654 add_name_attribute (array_die, type_tag (type));
17655 equate_type_number_to_die (type, array_die);
17657 if (info->ndimensions > 1)
17658 switch (info->ordering)
17660 case array_descr_ordering_row_major:
17661 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17662 break;
17663 case array_descr_ordering_column_major:
17664 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17665 break;
17666 default:
17667 break;
17670 if (dwarf_version >= 3 || !dwarf_strict)
17672 if (info->data_location)
17673 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17674 dw_scalar_form_exprloc, &context);
17675 if (info->associated)
17676 add_scalar_info (array_die, DW_AT_associated, info->associated,
17677 dw_scalar_form_constant
17678 | dw_scalar_form_exprloc
17679 | dw_scalar_form_reference, &context);
17680 if (info->allocated)
17681 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17682 dw_scalar_form_constant
17683 | dw_scalar_form_exprloc
17684 | dw_scalar_form_reference, &context);
17687 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17689 for (dim = 0; dim < info->ndimensions; dim++)
17691 dw_die_ref subrange_die
17692 = new_die (DW_TAG_subrange_type, array_die, NULL);
17694 if (info->dimen[dim].bounds_type)
17695 add_type_attribute (subrange_die,
17696 info->dimen[dim].bounds_type, 0,
17697 context_die);
17698 if (info->dimen[dim].lower_bound)
17699 add_bound_info (subrange_die, DW_AT_lower_bound,
17700 info->dimen[dim].lower_bound, &context);
17701 if (info->dimen[dim].upper_bound)
17702 add_bound_info (subrange_die, DW_AT_upper_bound,
17703 info->dimen[dim].upper_bound, &context);
17704 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17705 add_scalar_info (subrange_die, DW_AT_byte_stride,
17706 info->dimen[dim].stride,
17707 dw_scalar_form_constant
17708 | dw_scalar_form_exprloc
17709 | dw_scalar_form_reference,
17710 &context);
17713 gen_type_die (info->element_type, context_die);
17714 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17715 context_die);
17717 if (get_AT (array_die, DW_AT_name))
17718 add_pubtype (type, array_die);
17721 #if 0
17722 static void
17723 gen_entry_point_die (tree decl, dw_die_ref context_die)
17725 tree origin = decl_ultimate_origin (decl);
17726 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17728 if (origin != NULL)
17729 add_abstract_origin_attribute (decl_die, origin);
17730 else
17732 add_name_and_src_coords_attributes (decl_die, decl);
17733 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17734 TYPE_UNQUALIFIED, context_die);
17737 if (DECL_ABSTRACT_P (decl))
17738 equate_decl_number_to_die (decl, decl_die);
17739 else
17740 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17742 #endif
17744 /* Walk through the list of incomplete types again, trying once more to
17745 emit full debugging info for them. */
17747 static void
17748 retry_incomplete_types (void)
17750 int i;
17752 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17753 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17754 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17757 /* Determine what tag to use for a record type. */
17759 static enum dwarf_tag
17760 record_type_tag (tree type)
17762 if (! lang_hooks.types.classify_record)
17763 return DW_TAG_structure_type;
17765 switch (lang_hooks.types.classify_record (type))
17767 case RECORD_IS_STRUCT:
17768 return DW_TAG_structure_type;
17770 case RECORD_IS_CLASS:
17771 return DW_TAG_class_type;
17773 case RECORD_IS_INTERFACE:
17774 if (dwarf_version >= 3 || !dwarf_strict)
17775 return DW_TAG_interface_type;
17776 return DW_TAG_structure_type;
17778 default:
17779 gcc_unreachable ();
17783 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17784 include all of the information about the enumeration values also. Each
17785 enumerated type name/value is listed as a child of the enumerated type
17786 DIE. */
17788 static dw_die_ref
17789 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17791 dw_die_ref type_die = lookup_type_die (type);
17793 if (type_die == NULL)
17795 type_die = new_die (DW_TAG_enumeration_type,
17796 scope_die_for (type, context_die), type);
17797 equate_type_number_to_die (type, type_die);
17798 add_name_attribute (type_die, type_tag (type));
17799 if (dwarf_version >= 4 || !dwarf_strict)
17801 if (ENUM_IS_SCOPED (type))
17802 add_AT_flag (type_die, DW_AT_enum_class, 1);
17803 if (ENUM_IS_OPAQUE (type))
17804 add_AT_flag (type_die, DW_AT_declaration, 1);
17807 else if (! TYPE_SIZE (type))
17808 return type_die;
17809 else
17810 remove_AT (type_die, DW_AT_declaration);
17812 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17813 given enum type is incomplete, do not generate the DW_AT_byte_size
17814 attribute or the DW_AT_element_list attribute. */
17815 if (TYPE_SIZE (type))
17817 tree link;
17819 TREE_ASM_WRITTEN (type) = 1;
17820 add_byte_size_attribute (type_die, type);
17821 if (dwarf_version >= 3 || !dwarf_strict)
17823 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17824 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17825 context_die);
17827 if (TYPE_STUB_DECL (type) != NULL_TREE)
17829 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17830 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17833 /* If the first reference to this type was as the return type of an
17834 inline function, then it may not have a parent. Fix this now. */
17835 if (type_die->die_parent == NULL)
17836 add_child_die (scope_die_for (type, context_die), type_die);
17838 for (link = TYPE_VALUES (type);
17839 link != NULL; link = TREE_CHAIN (link))
17841 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17842 tree value = TREE_VALUE (link);
17844 add_name_attribute (enum_die,
17845 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17847 if (TREE_CODE (value) == CONST_DECL)
17848 value = DECL_INITIAL (value);
17850 if (simple_type_size_in_bits (TREE_TYPE (value))
17851 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17853 /* For constant forms created by add_AT_unsigned DWARF
17854 consumers (GDB, elfutils, etc.) always zero extend
17855 the value. Only when the actual value is negative
17856 do we need to use add_AT_int to generate a constant
17857 form that can represent negative values. */
17858 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17859 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17860 add_AT_unsigned (enum_die, DW_AT_const_value,
17861 (unsigned HOST_WIDE_INT) val);
17862 else
17863 add_AT_int (enum_die, DW_AT_const_value, val);
17865 else
17866 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17867 that here. TODO: This should be re-worked to use correct
17868 signed/unsigned double tags for all cases. */
17869 add_AT_wide (enum_die, DW_AT_const_value, value);
17872 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17873 if (TYPE_ARTIFICIAL (type))
17874 add_AT_flag (type_die, DW_AT_artificial, 1);
17876 else
17877 add_AT_flag (type_die, DW_AT_declaration, 1);
17879 add_pubtype (type, type_die);
17881 return type_die;
17884 /* Generate a DIE to represent either a real live formal parameter decl or to
17885 represent just the type of some formal parameter position in some function
17886 type.
17888 Note that this routine is a bit unusual because its argument may be a
17889 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17890 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17891 node. If it's the former then this function is being called to output a
17892 DIE to represent a formal parameter object (or some inlining thereof). If
17893 it's the latter, then this function is only being called to output a
17894 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17895 argument type of some subprogram type.
17896 If EMIT_NAME_P is true, name and source coordinate attributes
17897 are emitted. */
17899 static dw_die_ref
17900 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17901 dw_die_ref context_die)
17903 tree node_or_origin = node ? node : origin;
17904 tree ultimate_origin;
17905 dw_die_ref parm_die
17906 = new_die (DW_TAG_formal_parameter, context_die, node);
17908 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17910 case tcc_declaration:
17911 ultimate_origin = decl_ultimate_origin (node_or_origin);
17912 if (node || ultimate_origin)
17913 origin = ultimate_origin;
17914 if (origin != NULL)
17915 add_abstract_origin_attribute (parm_die, origin);
17916 else if (emit_name_p)
17917 add_name_and_src_coords_attributes (parm_die, node);
17918 if (origin == NULL
17919 || (! DECL_ABSTRACT_P (node_or_origin)
17920 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17921 decl_function_context
17922 (node_or_origin))))
17924 tree type = TREE_TYPE (node_or_origin);
17925 if (decl_by_reference_p (node_or_origin))
17926 add_type_attribute (parm_die, TREE_TYPE (type),
17927 TYPE_UNQUALIFIED, context_die);
17928 else
17929 add_type_attribute (parm_die, type,
17930 decl_quals (node_or_origin),
17931 context_die);
17933 if (origin == NULL && DECL_ARTIFICIAL (node))
17934 add_AT_flag (parm_die, DW_AT_artificial, 1);
17936 if (node && node != origin)
17937 equate_decl_number_to_die (node, parm_die);
17938 if (! DECL_ABSTRACT_P (node_or_origin))
17939 add_location_or_const_value_attribute (parm_die, node_or_origin,
17940 node == NULL, DW_AT_location);
17942 break;
17944 case tcc_type:
17945 /* We were called with some kind of a ..._TYPE node. */
17946 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17947 context_die);
17948 break;
17950 default:
17951 gcc_unreachable ();
17954 return parm_die;
17957 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17958 children DW_TAG_formal_parameter DIEs representing the arguments of the
17959 parameter pack.
17961 PARM_PACK must be a function parameter pack.
17962 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17963 must point to the subsequent arguments of the function PACK_ARG belongs to.
17964 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17965 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17966 following the last one for which a DIE was generated. */
17968 static dw_die_ref
17969 gen_formal_parameter_pack_die (tree parm_pack,
17970 tree pack_arg,
17971 dw_die_ref subr_die,
17972 tree *next_arg)
17974 tree arg;
17975 dw_die_ref parm_pack_die;
17977 gcc_assert (parm_pack
17978 && lang_hooks.function_parameter_pack_p (parm_pack)
17979 && subr_die);
17981 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17982 add_src_coords_attributes (parm_pack_die, parm_pack);
17984 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17986 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17987 parm_pack))
17988 break;
17989 gen_formal_parameter_die (arg, NULL,
17990 false /* Don't emit name attribute. */,
17991 parm_pack_die);
17993 if (next_arg)
17994 *next_arg = arg;
17995 return parm_pack_die;
17998 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17999 at the end of an (ANSI prototyped) formal parameters list. */
18001 static void
18002 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18004 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18007 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18008 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18009 parameters as specified in some function type specification (except for
18010 those which appear as part of a function *definition*). */
18012 static void
18013 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18015 tree link;
18016 tree formal_type = NULL;
18017 tree first_parm_type;
18018 tree arg;
18020 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18022 arg = DECL_ARGUMENTS (function_or_method_type);
18023 function_or_method_type = TREE_TYPE (function_or_method_type);
18025 else
18026 arg = NULL_TREE;
18028 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18030 /* Make our first pass over the list of formal parameter types and output a
18031 DW_TAG_formal_parameter DIE for each one. */
18032 for (link = first_parm_type; link; )
18034 dw_die_ref parm_die;
18036 formal_type = TREE_VALUE (link);
18037 if (formal_type == void_type_node)
18038 break;
18040 /* Output a (nameless) DIE to represent the formal parameter itself. */
18041 if (!POINTER_BOUNDS_TYPE_P (formal_type))
18043 parm_die = gen_formal_parameter_die (formal_type, NULL,
18044 true /* Emit name attribute. */,
18045 context_die);
18046 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18047 && link == first_parm_type)
18049 add_AT_flag (parm_die, DW_AT_artificial, 1);
18050 if (dwarf_version >= 3 || !dwarf_strict)
18051 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18053 else if (arg && DECL_ARTIFICIAL (arg))
18054 add_AT_flag (parm_die, DW_AT_artificial, 1);
18057 link = TREE_CHAIN (link);
18058 if (arg)
18059 arg = DECL_CHAIN (arg);
18062 /* If this function type has an ellipsis, add a
18063 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18064 if (formal_type != void_type_node)
18065 gen_unspecified_parameters_die (function_or_method_type, context_die);
18067 /* Make our second (and final) pass over the list of formal parameter types
18068 and output DIEs to represent those types (as necessary). */
18069 for (link = TYPE_ARG_TYPES (function_or_method_type);
18070 link && TREE_VALUE (link);
18071 link = TREE_CHAIN (link))
18072 gen_type_die (TREE_VALUE (link), context_die);
18075 /* We want to generate the DIE for TYPE so that we can generate the
18076 die for MEMBER, which has been defined; we will need to refer back
18077 to the member declaration nested within TYPE. If we're trying to
18078 generate minimal debug info for TYPE, processing TYPE won't do the
18079 trick; we need to attach the member declaration by hand. */
18081 static void
18082 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18084 gen_type_die (type, context_die);
18086 /* If we're trying to avoid duplicate debug info, we may not have
18087 emitted the member decl for this function. Emit it now. */
18088 if (TYPE_STUB_DECL (type)
18089 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18090 && ! lookup_decl_die (member))
18092 dw_die_ref type_die;
18093 gcc_assert (!decl_ultimate_origin (member));
18095 push_decl_scope (type);
18096 type_die = lookup_type_die_strip_naming_typedef (type);
18097 if (TREE_CODE (member) == FUNCTION_DECL)
18098 gen_subprogram_die (member, type_die);
18099 else if (TREE_CODE (member) == FIELD_DECL)
18101 /* Ignore the nameless fields that are used to skip bits but handle
18102 C++ anonymous unions and structs. */
18103 if (DECL_NAME (member) != NULL_TREE
18104 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18105 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18107 gen_type_die (member_declared_type (member), type_die);
18108 gen_field_die (member, type_die);
18111 else
18112 gen_variable_die (member, NULL_TREE, type_die);
18114 pop_decl_scope ();
18118 /* Forward declare these functions, because they are mutually recursive
18119 with their set_block_* pairing functions. */
18120 static void set_decl_origin_self (tree);
18121 static void set_decl_abstract_flags (tree, vec<tree> &);
18123 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18124 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18125 that it points to the node itself, thus indicating that the node is its
18126 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18127 the given node is NULL, recursively descend the decl/block tree which
18128 it is the root of, and for each other ..._DECL or BLOCK node contained
18129 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18130 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18131 values to point to themselves. */
18133 static void
18134 set_block_origin_self (tree stmt)
18136 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18138 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18141 tree local_decl;
18143 for (local_decl = BLOCK_VARS (stmt);
18144 local_decl != NULL_TREE;
18145 local_decl = DECL_CHAIN (local_decl))
18146 /* Do not recurse on nested functions since the inlining status
18147 of parent and child can be different as per the DWARF spec. */
18148 if (TREE_CODE (local_decl) != FUNCTION_DECL
18149 && !DECL_EXTERNAL (local_decl))
18150 set_decl_origin_self (local_decl);
18154 tree subblock;
18156 for (subblock = BLOCK_SUBBLOCKS (stmt);
18157 subblock != NULL_TREE;
18158 subblock = BLOCK_CHAIN (subblock))
18159 set_block_origin_self (subblock); /* Recurse. */
18164 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18165 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18166 node to so that it points to the node itself, thus indicating that the
18167 node represents its own (abstract) origin. Additionally, if the
18168 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18169 the decl/block tree of which the given node is the root of, and for
18170 each other ..._DECL or BLOCK node contained therein whose
18171 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18172 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18173 point to themselves. */
18175 static void
18176 set_decl_origin_self (tree decl)
18178 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18180 DECL_ABSTRACT_ORIGIN (decl) = decl;
18181 if (TREE_CODE (decl) == FUNCTION_DECL)
18183 tree arg;
18185 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18186 DECL_ABSTRACT_ORIGIN (arg) = arg;
18187 if (DECL_INITIAL (decl) != NULL_TREE
18188 && DECL_INITIAL (decl) != error_mark_node)
18189 set_block_origin_self (DECL_INITIAL (decl));
18194 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18195 and if it wasn't 1 before, push it to abstract_vec vector.
18196 For all local decls and all local sub-blocks (recursively) do it
18197 too. */
18199 static void
18200 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18202 tree local_decl;
18203 tree subblock;
18204 unsigned int i;
18206 if (!BLOCK_ABSTRACT (stmt))
18208 abstract_vec.safe_push (stmt);
18209 BLOCK_ABSTRACT (stmt) = 1;
18212 for (local_decl = BLOCK_VARS (stmt);
18213 local_decl != NULL_TREE;
18214 local_decl = DECL_CHAIN (local_decl))
18215 if (! DECL_EXTERNAL (local_decl))
18216 set_decl_abstract_flags (local_decl, abstract_vec);
18218 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18220 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18221 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18222 || TREE_CODE (local_decl) == PARM_DECL)
18223 set_decl_abstract_flags (local_decl, abstract_vec);
18226 for (subblock = BLOCK_SUBBLOCKS (stmt);
18227 subblock != NULL_TREE;
18228 subblock = BLOCK_CHAIN (subblock))
18229 set_block_abstract_flags (subblock, abstract_vec);
18232 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18233 to 1 and if it wasn't 1 before, push to abstract_vec vector.
18234 In the case where the decl is a FUNCTION_DECL also set the abstract
18235 flags for all of the parameters, local vars, local
18236 blocks and sub-blocks (recursively). */
18238 static void
18239 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18241 if (!DECL_ABSTRACT_P (decl))
18243 abstract_vec.safe_push (decl);
18244 DECL_ABSTRACT_P (decl) = 1;
18247 if (TREE_CODE (decl) == FUNCTION_DECL)
18249 tree arg;
18251 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18252 if (!DECL_ABSTRACT_P (arg))
18254 abstract_vec.safe_push (arg);
18255 DECL_ABSTRACT_P (arg) = 1;
18257 if (DECL_INITIAL (decl) != NULL_TREE
18258 && DECL_INITIAL (decl) != error_mark_node)
18259 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18263 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18264 may later generate inlined and/or out-of-line instances of. */
18266 static void
18267 dwarf2out_abstract_function (tree decl)
18269 dw_die_ref old_die;
18270 tree save_fn;
18271 tree context;
18272 hash_table<decl_loc_hasher> *old_decl_loc_table;
18273 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18274 int old_call_site_count, old_tail_call_site_count;
18275 struct call_arg_loc_node *old_call_arg_locations;
18277 /* Make sure we have the actual abstract inline, not a clone. */
18278 decl = DECL_ORIGIN (decl);
18280 old_die = lookup_decl_die (decl);
18281 if (old_die && get_AT (old_die, DW_AT_inline))
18282 /* We've already generated the abstract instance. */
18283 return;
18285 /* We can be called while recursively when seeing block defining inlined subroutine
18286 DIE. Be sure to not clobber the outer location table nor use it or we would
18287 get locations in abstract instantces. */
18288 old_decl_loc_table = decl_loc_table;
18289 decl_loc_table = NULL;
18290 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18291 cached_dw_loc_list_table = NULL;
18292 old_call_arg_locations = call_arg_locations;
18293 call_arg_locations = NULL;
18294 old_call_site_count = call_site_count;
18295 call_site_count = -1;
18296 old_tail_call_site_count = tail_call_site_count;
18297 tail_call_site_count = -1;
18299 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18300 we don't get confused by DECL_ABSTRACT_P. */
18301 if (debug_info_level > DINFO_LEVEL_TERSE)
18303 context = decl_class_context (decl);
18304 if (context)
18305 gen_type_die_for_member
18306 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18309 /* Pretend we've just finished compiling this function. */
18310 save_fn = current_function_decl;
18311 current_function_decl = decl;
18313 auto_vec<tree, 64> abstract_vec;
18314 set_decl_abstract_flags (decl, abstract_vec);
18315 dwarf2out_decl (decl);
18316 unsigned int i;
18317 tree t;
18318 FOR_EACH_VEC_ELT (abstract_vec, i, t)
18319 if (TREE_CODE (t) == BLOCK)
18320 BLOCK_ABSTRACT (t) = 0;
18321 else
18322 DECL_ABSTRACT_P (t) = 0;
18324 current_function_decl = save_fn;
18325 decl_loc_table = old_decl_loc_table;
18326 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18327 call_arg_locations = old_call_arg_locations;
18328 call_site_count = old_call_site_count;
18329 tail_call_site_count = old_tail_call_site_count;
18332 /* Helper function of premark_used_types() which gets called through
18333 htab_traverse.
18335 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18336 marked as unused by prune_unused_types. */
18338 bool
18339 premark_used_types_helper (tree const &type, void *)
18341 dw_die_ref die;
18343 die = lookup_type_die (type);
18344 if (die != NULL)
18345 die->die_perennial_p = 1;
18346 return true;
18349 /* Helper function of premark_types_used_by_global_vars which gets called
18350 through htab_traverse.
18352 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18353 marked as unused by prune_unused_types. The DIE of the type is marked
18354 only if the global variable using the type will actually be emitted. */
18357 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18358 void *)
18360 struct types_used_by_vars_entry *entry;
18361 dw_die_ref die;
18363 entry = (struct types_used_by_vars_entry *) *slot;
18364 gcc_assert (entry->type != NULL
18365 && entry->var_decl != NULL);
18366 die = lookup_type_die (entry->type);
18367 if (die)
18369 /* Ask cgraph if the global variable really is to be emitted.
18370 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18371 varpool_node *node = varpool_node::get (entry->var_decl);
18372 if (node && node->definition)
18374 die->die_perennial_p = 1;
18375 /* Keep the parent DIEs as well. */
18376 while ((die = die->die_parent) && die->die_perennial_p == 0)
18377 die->die_perennial_p = 1;
18380 return 1;
18383 /* Mark all members of used_types_hash as perennial. */
18385 static void
18386 premark_used_types (struct function *fun)
18388 if (fun && fun->used_types_hash)
18389 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18392 /* Mark all members of types_used_by_vars_entry as perennial. */
18394 static void
18395 premark_types_used_by_global_vars (void)
18397 if (types_used_by_vars_hash)
18398 types_used_by_vars_hash
18399 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18402 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18403 for CA_LOC call arg loc node. */
18405 static dw_die_ref
18406 gen_call_site_die (tree decl, dw_die_ref subr_die,
18407 struct call_arg_loc_node *ca_loc)
18409 dw_die_ref stmt_die = NULL, die;
18410 tree block = ca_loc->block;
18412 while (block
18413 && block != DECL_INITIAL (decl)
18414 && TREE_CODE (block) == BLOCK)
18416 if (block_map.length () > BLOCK_NUMBER (block))
18417 stmt_die = block_map[BLOCK_NUMBER (block)];
18418 if (stmt_die)
18419 break;
18420 block = BLOCK_SUPERCONTEXT (block);
18422 if (stmt_die == NULL)
18423 stmt_die = subr_die;
18424 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18425 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18426 if (ca_loc->tail_call_p)
18427 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18428 if (ca_loc->symbol_ref)
18430 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18431 if (tdie)
18432 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18433 else
18434 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18436 return die;
18439 /* Generate a DIE to represent a declared function (either file-scope or
18440 block-local). */
18442 static void
18443 gen_subprogram_die (tree decl, dw_die_ref context_die)
18445 tree origin = decl_ultimate_origin (decl);
18446 dw_die_ref subr_die;
18447 tree outer_scope;
18448 dw_die_ref old_die = lookup_decl_die (decl);
18449 int declaration = (current_function_decl != decl
18450 || class_or_namespace_scope_p (context_die));
18452 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18454 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18455 started to generate the abstract instance of an inline, decided to output
18456 its containing class, and proceeded to emit the declaration of the inline
18457 from the member list for the class. If so, DECLARATION takes priority;
18458 we'll get back to the abstract instance when done with the class. */
18460 /* The class-scope declaration DIE must be the primary DIE. */
18461 if (origin && declaration && class_or_namespace_scope_p (context_die))
18463 origin = NULL;
18464 gcc_assert (!old_die);
18467 /* Now that the C++ front end lazily declares artificial member fns, we
18468 might need to retrofit the declaration into its class. */
18469 if (!declaration && !origin && !old_die
18470 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18471 && !class_or_namespace_scope_p (context_die)
18472 && debug_info_level > DINFO_LEVEL_TERSE)
18473 old_die = force_decl_die (decl);
18475 if (origin != NULL)
18477 gcc_assert (!declaration || local_scope_p (context_die));
18479 /* Fixup die_parent for the abstract instance of a nested
18480 inline function. */
18481 if (old_die && old_die->die_parent == NULL)
18482 add_child_die (context_die, old_die);
18484 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18485 add_abstract_origin_attribute (subr_die, origin);
18486 /* This is where the actual code for a cloned function is.
18487 Let's emit linkage name attribute for it. This helps
18488 debuggers to e.g, set breakpoints into
18489 constructors/destructors when the user asks "break
18490 K::K". */
18491 add_linkage_name (subr_die, decl);
18493 else if (old_die)
18495 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18496 struct dwarf_file_data * file_index = lookup_filename (s.file);
18498 if (!get_AT_flag (old_die, DW_AT_declaration)
18499 /* We can have a normal definition following an inline one in the
18500 case of redefinition of GNU C extern inlines.
18501 It seems reasonable to use AT_specification in this case. */
18502 && !get_AT (old_die, DW_AT_inline))
18504 /* Detect and ignore this case, where we are trying to output
18505 something we have already output. */
18506 return;
18509 /* If the definition comes from the same place as the declaration,
18510 maybe use the old DIE. We always want the DIE for this function
18511 that has the *_pc attributes to be under comp_unit_die so the
18512 debugger can find it. We also need to do this for abstract
18513 instances of inlines, since the spec requires the out-of-line copy
18514 to have the same parent. For local class methods, this doesn't
18515 apply; we just use the old DIE. */
18516 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18517 && (DECL_ARTIFICIAL (decl)
18518 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18519 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18520 == (unsigned) s.line))))
18522 subr_die = old_die;
18524 /* Clear out the declaration attribute and the formal parameters.
18525 Do not remove all children, because it is possible that this
18526 declaration die was forced using force_decl_die(). In such
18527 cases die that forced declaration die (e.g. TAG_imported_module)
18528 is one of the children that we do not want to remove. */
18529 remove_AT (subr_die, DW_AT_declaration);
18530 remove_AT (subr_die, DW_AT_object_pointer);
18531 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18533 else
18535 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18536 add_AT_specification (subr_die, old_die);
18537 add_pubname (decl, subr_die);
18538 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18539 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18540 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18541 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18543 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18544 emit the real type on the definition die. */
18545 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18547 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18548 if (die == auto_die || die == decltype_auto_die)
18549 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18550 TYPE_UNQUALIFIED, context_die);
18554 else
18556 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18558 if (TREE_PUBLIC (decl))
18559 add_AT_flag (subr_die, DW_AT_external, 1);
18561 add_name_and_src_coords_attributes (subr_die, decl);
18562 add_pubname (decl, subr_die);
18563 if (debug_info_level > DINFO_LEVEL_TERSE)
18565 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18566 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18567 TYPE_UNQUALIFIED, context_die);
18570 add_pure_or_virtual_attribute (subr_die, decl);
18571 if (DECL_ARTIFICIAL (decl))
18572 add_AT_flag (subr_die, DW_AT_artificial, 1);
18574 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18575 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18577 add_accessibility_attribute (subr_die, decl);
18580 if (declaration)
18582 if (!old_die || !get_AT (old_die, DW_AT_inline))
18584 add_AT_flag (subr_die, DW_AT_declaration, 1);
18586 /* If this is an explicit function declaration then generate
18587 a DW_AT_explicit attribute. */
18588 if (lang_hooks.decls.function_decl_explicit_p (decl)
18589 && (dwarf_version >= 3 || !dwarf_strict))
18590 add_AT_flag (subr_die, DW_AT_explicit, 1);
18592 /* If this is a C++11 deleted special function member then generate
18593 a DW_AT_GNU_deleted attribute. */
18594 if (lang_hooks.decls.function_decl_deleted_p (decl)
18595 && (! dwarf_strict))
18596 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18598 /* The first time we see a member function, it is in the context of
18599 the class to which it belongs. We make sure of this by emitting
18600 the class first. The next time is the definition, which is
18601 handled above. The two may come from the same source text.
18603 Note that force_decl_die() forces function declaration die. It is
18604 later reused to represent definition. */
18605 equate_decl_number_to_die (decl, subr_die);
18608 else if (DECL_ABSTRACT_P (decl))
18610 if (DECL_DECLARED_INLINE_P (decl))
18612 if (cgraph_function_possibly_inlined_p (decl))
18613 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18614 else
18615 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18617 else
18619 if (cgraph_function_possibly_inlined_p (decl))
18620 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18621 else
18622 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18625 if (DECL_DECLARED_INLINE_P (decl)
18626 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18627 add_AT_flag (subr_die, DW_AT_artificial, 1);
18629 equate_decl_number_to_die (decl, subr_die);
18631 else if (!DECL_EXTERNAL (decl))
18633 HOST_WIDE_INT cfa_fb_offset;
18634 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18636 if (!old_die || !get_AT (old_die, DW_AT_inline))
18637 equate_decl_number_to_die (decl, subr_die);
18639 gcc_checking_assert (fun);
18640 if (!flag_reorder_blocks_and_partition)
18642 dw_fde_ref fde = fun->fde;
18643 if (fde->dw_fde_begin)
18645 /* We have already generated the labels. */
18646 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18647 fde->dw_fde_end, false);
18649 else
18651 /* Create start/end labels and add the range. */
18652 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18653 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18654 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18655 current_function_funcdef_no);
18656 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18657 current_function_funcdef_no);
18658 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18659 false);
18662 #if VMS_DEBUGGING_INFO
18663 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18664 Section 2.3 Prologue and Epilogue Attributes:
18665 When a breakpoint is set on entry to a function, it is generally
18666 desirable for execution to be suspended, not on the very first
18667 instruction of the function, but rather at a point after the
18668 function's frame has been set up, after any language defined local
18669 declaration processing has been completed, and before execution of
18670 the first statement of the function begins. Debuggers generally
18671 cannot properly determine where this point is. Similarly for a
18672 breakpoint set on exit from a function. The prologue and epilogue
18673 attributes allow a compiler to communicate the location(s) to use. */
18676 if (fde->dw_fde_vms_end_prologue)
18677 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18678 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18680 if (fde->dw_fde_vms_begin_epilogue)
18681 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18682 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18684 #endif
18687 else
18689 /* Generate pubnames entries for the split function code ranges. */
18690 dw_fde_ref fde = fun->fde;
18692 if (fde->dw_fde_second_begin)
18694 if (dwarf_version >= 3 || !dwarf_strict)
18696 /* We should use ranges for non-contiguous code section
18697 addresses. Use the actual code range for the initial
18698 section, since the HOT/COLD labels might precede an
18699 alignment offset. */
18700 bool range_list_added = false;
18701 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18702 fde->dw_fde_end, &range_list_added,
18703 false);
18704 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18705 fde->dw_fde_second_end,
18706 &range_list_added, false);
18707 if (range_list_added)
18708 add_ranges (NULL);
18710 else
18712 /* There is no real support in DW2 for this .. so we make
18713 a work-around. First, emit the pub name for the segment
18714 containing the function label. Then make and emit a
18715 simplified subprogram DIE for the second segment with the
18716 name pre-fixed by __hot/cold_sect_of_. We use the same
18717 linkage name for the second die so that gdb will find both
18718 sections when given "b foo". */
18719 const char *name = NULL;
18720 tree decl_name = DECL_NAME (decl);
18721 dw_die_ref seg_die;
18723 /* Do the 'primary' section. */
18724 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18725 fde->dw_fde_end, false);
18727 /* Build a minimal DIE for the secondary section. */
18728 seg_die = new_die (DW_TAG_subprogram,
18729 subr_die->die_parent, decl);
18731 if (TREE_PUBLIC (decl))
18732 add_AT_flag (seg_die, DW_AT_external, 1);
18734 if (decl_name != NULL
18735 && IDENTIFIER_POINTER (decl_name) != NULL)
18737 name = dwarf2_name (decl, 1);
18738 if (! DECL_ARTIFICIAL (decl))
18739 add_src_coords_attributes (seg_die, decl);
18741 add_linkage_name (seg_die, decl);
18743 gcc_assert (name != NULL);
18744 add_pure_or_virtual_attribute (seg_die, decl);
18745 if (DECL_ARTIFICIAL (decl))
18746 add_AT_flag (seg_die, DW_AT_artificial, 1);
18748 name = concat ("__second_sect_of_", name, NULL);
18749 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18750 fde->dw_fde_second_end, false);
18751 add_name_attribute (seg_die, name);
18752 if (want_pubnames ())
18753 add_pubname_string (name, seg_die);
18756 else
18757 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18758 false);
18761 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18763 /* We define the "frame base" as the function's CFA. This is more
18764 convenient for several reasons: (1) It's stable across the prologue
18765 and epilogue, which makes it better than just a frame pointer,
18766 (2) With dwarf3, there exists a one-byte encoding that allows us
18767 to reference the .debug_frame data by proxy, but failing that,
18768 (3) We can at least reuse the code inspection and interpretation
18769 code that determines the CFA position at various points in the
18770 function. */
18771 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18773 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18774 add_AT_loc (subr_die, DW_AT_frame_base, op);
18776 else
18778 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18779 if (list->dw_loc_next)
18780 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18781 else
18782 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18785 /* Compute a displacement from the "steady-state frame pointer" to
18786 the CFA. The former is what all stack slots and argument slots
18787 will reference in the rtl; the latter is what we've told the
18788 debugger about. We'll need to adjust all frame_base references
18789 by this displacement. */
18790 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18792 if (fun->static_chain_decl)
18793 add_AT_location_description (subr_die, DW_AT_static_link,
18794 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
18797 /* Generate child dies for template paramaters. */
18798 if (debug_info_level > DINFO_LEVEL_TERSE)
18799 gen_generic_params_dies (decl);
18801 /* Now output descriptions of the arguments for this function. This gets
18802 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18803 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18804 `...' at the end of the formal parameter list. In order to find out if
18805 there was a trailing ellipsis or not, we must instead look at the type
18806 associated with the FUNCTION_DECL. This will be a node of type
18807 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18808 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18809 an ellipsis at the end. */
18811 /* In the case where we are describing a mere function declaration, all we
18812 need to do here (and all we *can* do here) is to describe the *types* of
18813 its formal parameters. */
18814 if (debug_info_level <= DINFO_LEVEL_TERSE)
18816 else if (declaration)
18817 gen_formal_types_die (decl, subr_die);
18818 else
18820 /* Generate DIEs to represent all known formal parameters. */
18821 tree parm = DECL_ARGUMENTS (decl);
18822 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18823 tree generic_decl_parm = generic_decl
18824 ? DECL_ARGUMENTS (generic_decl)
18825 : NULL;
18827 /* Now we want to walk the list of parameters of the function and
18828 emit their relevant DIEs.
18830 We consider the case of DECL being an instance of a generic function
18831 as well as it being a normal function.
18833 If DECL is an instance of a generic function we walk the
18834 parameters of the generic function declaration _and_ the parameters of
18835 DECL itself. This is useful because we want to emit specific DIEs for
18836 function parameter packs and those are declared as part of the
18837 generic function declaration. In that particular case,
18838 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18839 That DIE has children DIEs representing the set of arguments
18840 of the pack. Note that the set of pack arguments can be empty.
18841 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18842 children DIE.
18844 Otherwise, we just consider the parameters of DECL. */
18845 while (generic_decl_parm || parm)
18847 if (generic_decl_parm
18848 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18849 gen_formal_parameter_pack_die (generic_decl_parm,
18850 parm, subr_die,
18851 &parm);
18852 else if (parm && !POINTER_BOUNDS_P (parm))
18854 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18856 if (parm == DECL_ARGUMENTS (decl)
18857 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18858 && parm_die
18859 && (dwarf_version >= 3 || !dwarf_strict))
18860 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18862 parm = DECL_CHAIN (parm);
18864 else if (parm)
18865 parm = DECL_CHAIN (parm);
18867 if (generic_decl_parm)
18868 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18871 /* Decide whether we need an unspecified_parameters DIE at the end.
18872 There are 2 more cases to do this for: 1) the ansi ... declaration -
18873 this is detectable when the end of the arg list is not a
18874 void_type_node 2) an unprototyped function declaration (not a
18875 definition). This just means that we have no info about the
18876 parameters at all. */
18877 if (prototype_p (TREE_TYPE (decl)))
18879 /* This is the prototyped case, check for.... */
18880 if (stdarg_p (TREE_TYPE (decl)))
18881 gen_unspecified_parameters_die (decl, subr_die);
18883 else if (DECL_INITIAL (decl) == NULL_TREE)
18884 gen_unspecified_parameters_die (decl, subr_die);
18887 /* Output Dwarf info for all of the stuff within the body of the function
18888 (if it has one - it may be just a declaration). */
18889 outer_scope = DECL_INITIAL (decl);
18891 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18892 a function. This BLOCK actually represents the outermost binding contour
18893 for the function, i.e. the contour in which the function's formal
18894 parameters and labels get declared. Curiously, it appears that the front
18895 end doesn't actually put the PARM_DECL nodes for the current function onto
18896 the BLOCK_VARS list for this outer scope, but are strung off of the
18897 DECL_ARGUMENTS list for the function instead.
18899 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18900 the LABEL_DECL nodes for the function however, and we output DWARF info
18901 for those in decls_for_scope. Just within the `outer_scope' there will be
18902 a BLOCK node representing the function's outermost pair of curly braces,
18903 and any blocks used for the base and member initializers of a C++
18904 constructor function. */
18905 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18907 int call_site_note_count = 0;
18908 int tail_call_site_note_count = 0;
18910 /* Emit a DW_TAG_variable DIE for a named return value. */
18911 if (DECL_NAME (DECL_RESULT (decl)))
18912 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18914 decls_for_scope (outer_scope, subr_die);
18916 if (call_arg_locations && !dwarf_strict)
18918 struct call_arg_loc_node *ca_loc;
18919 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18921 dw_die_ref die = NULL;
18922 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18923 rtx arg, next_arg;
18925 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18926 arg; arg = next_arg)
18928 dw_loc_descr_ref reg, val;
18929 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18930 dw_die_ref cdie, tdie = NULL;
18932 next_arg = XEXP (arg, 1);
18933 if (REG_P (XEXP (XEXP (arg, 0), 0))
18934 && next_arg
18935 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18936 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18937 && REGNO (XEXP (XEXP (arg, 0), 0))
18938 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18939 next_arg = XEXP (next_arg, 1);
18940 if (mode == VOIDmode)
18942 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18943 if (mode == VOIDmode)
18944 mode = GET_MODE (XEXP (arg, 0));
18946 if (mode == VOIDmode || mode == BLKmode)
18947 continue;
18948 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18950 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18951 tloc = XEXP (XEXP (arg, 0), 1);
18952 continue;
18954 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18955 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18957 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18958 tlocc = XEXP (XEXP (arg, 0), 1);
18959 continue;
18961 reg = NULL;
18962 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18963 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18964 VAR_INIT_STATUS_INITIALIZED);
18965 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18967 rtx mem = XEXP (XEXP (arg, 0), 0);
18968 reg = mem_loc_descriptor (XEXP (mem, 0),
18969 get_address_mode (mem),
18970 GET_MODE (mem),
18971 VAR_INIT_STATUS_INITIALIZED);
18973 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18974 == DEBUG_PARAMETER_REF)
18976 tree tdecl
18977 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18978 tdie = lookup_decl_die (tdecl);
18979 if (tdie == NULL)
18980 continue;
18982 else
18983 continue;
18984 if (reg == NULL
18985 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18986 != DEBUG_PARAMETER_REF)
18987 continue;
18988 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18989 VOIDmode,
18990 VAR_INIT_STATUS_INITIALIZED);
18991 if (val == NULL)
18992 continue;
18993 if (die == NULL)
18994 die = gen_call_site_die (decl, subr_die, ca_loc);
18995 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18996 NULL_TREE);
18997 if (reg != NULL)
18998 add_AT_loc (cdie, DW_AT_location, reg);
18999 else if (tdie != NULL)
19000 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19001 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19002 if (next_arg != XEXP (arg, 1))
19004 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19005 if (mode == VOIDmode)
19006 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19007 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19008 0), 1),
19009 mode, VOIDmode,
19010 VAR_INIT_STATUS_INITIALIZED);
19011 if (val != NULL)
19012 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19015 if (die == NULL
19016 && (ca_loc->symbol_ref || tloc))
19017 die = gen_call_site_die (decl, subr_die, ca_loc);
19018 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19020 dw_loc_descr_ref tval = NULL;
19022 if (tloc != NULL_RTX)
19023 tval = mem_loc_descriptor (tloc,
19024 GET_MODE (tloc) == VOIDmode
19025 ? Pmode : GET_MODE (tloc),
19026 VOIDmode,
19027 VAR_INIT_STATUS_INITIALIZED);
19028 if (tval)
19029 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19030 else if (tlocc != NULL_RTX)
19032 tval = mem_loc_descriptor (tlocc,
19033 GET_MODE (tlocc) == VOIDmode
19034 ? Pmode : GET_MODE (tlocc),
19035 VOIDmode,
19036 VAR_INIT_STATUS_INITIALIZED);
19037 if (tval)
19038 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19039 tval);
19042 if (die != NULL)
19044 call_site_note_count++;
19045 if (ca_loc->tail_call_p)
19046 tail_call_site_note_count++;
19050 call_arg_locations = NULL;
19051 call_arg_loc_last = NULL;
19052 if (tail_call_site_count >= 0
19053 && tail_call_site_count == tail_call_site_note_count
19054 && !dwarf_strict)
19056 if (call_site_count >= 0
19057 && call_site_count == call_site_note_count)
19058 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19059 else
19060 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19062 call_site_count = -1;
19063 tail_call_site_count = -1;
19066 if (subr_die != old_die)
19067 /* Add the calling convention attribute if requested. */
19068 add_calling_convention_attribute (subr_die, decl);
19071 /* Returns a hash value for X (which really is a die_struct). */
19073 hashval_t
19074 block_die_hasher::hash (die_struct *d)
19076 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19079 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19080 as decl_id and die_parent of die_struct Y. */
19082 bool
19083 block_die_hasher::equal (die_struct *x, die_struct *y)
19085 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19088 /* Generate a DIE to represent a declared data object.
19089 Either DECL or ORIGIN must be non-null. */
19091 static void
19092 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19094 HOST_WIDE_INT off = 0;
19095 tree com_decl;
19096 tree decl_or_origin = decl ? decl : origin;
19097 tree ultimate_origin;
19098 dw_die_ref var_die;
19099 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19100 dw_die_ref origin_die;
19101 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19102 || class_or_namespace_scope_p (context_die));
19103 bool specialization_p = false;
19105 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19106 if (decl || ultimate_origin)
19107 origin = ultimate_origin;
19108 com_decl = fortran_common (decl_or_origin, &off);
19110 /* Symbol in common gets emitted as a child of the common block, in the form
19111 of a data member. */
19112 if (com_decl)
19114 dw_die_ref com_die;
19115 dw_loc_list_ref loc;
19116 die_node com_die_arg;
19118 var_die = lookup_decl_die (decl_or_origin);
19119 if (var_die)
19121 if (get_AT (var_die, DW_AT_location) == NULL)
19123 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19124 if (loc)
19126 if (off)
19128 /* Optimize the common case. */
19129 if (single_element_loc_list_p (loc)
19130 && loc->expr->dw_loc_opc == DW_OP_addr
19131 && loc->expr->dw_loc_next == NULL
19132 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19133 == SYMBOL_REF)
19135 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19136 loc->expr->dw_loc_oprnd1.v.val_addr
19137 = plus_constant (GET_MODE (x), x , off);
19139 else
19140 loc_list_plus_const (loc, off);
19142 add_AT_location_description (var_die, DW_AT_location, loc);
19143 remove_AT (var_die, DW_AT_declaration);
19146 return;
19149 if (common_block_die_table == NULL)
19150 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19152 com_die_arg.decl_id = DECL_UID (com_decl);
19153 com_die_arg.die_parent = context_die;
19154 com_die = common_block_die_table->find (&com_die_arg);
19155 loc = loc_list_from_tree (com_decl, 2, NULL);
19156 if (com_die == NULL)
19158 const char *cnam
19159 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19160 die_node **slot;
19162 com_die = new_die (DW_TAG_common_block, context_die, decl);
19163 add_name_and_src_coords_attributes (com_die, com_decl);
19164 if (loc)
19166 add_AT_location_description (com_die, DW_AT_location, loc);
19167 /* Avoid sharing the same loc descriptor between
19168 DW_TAG_common_block and DW_TAG_variable. */
19169 loc = loc_list_from_tree (com_decl, 2, NULL);
19171 else if (DECL_EXTERNAL (decl))
19172 add_AT_flag (com_die, DW_AT_declaration, 1);
19173 if (want_pubnames ())
19174 add_pubname_string (cnam, com_die); /* ??? needed? */
19175 com_die->decl_id = DECL_UID (com_decl);
19176 slot = common_block_die_table->find_slot (com_die, INSERT);
19177 *slot = com_die;
19179 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19181 add_AT_location_description (com_die, DW_AT_location, loc);
19182 loc = loc_list_from_tree (com_decl, 2, NULL);
19183 remove_AT (com_die, DW_AT_declaration);
19185 var_die = new_die (DW_TAG_variable, com_die, decl);
19186 add_name_and_src_coords_attributes (var_die, decl);
19187 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19188 context_die);
19189 add_AT_flag (var_die, DW_AT_external, 1);
19190 if (loc)
19192 if (off)
19194 /* Optimize the common case. */
19195 if (single_element_loc_list_p (loc)
19196 && loc->expr->dw_loc_opc == DW_OP_addr
19197 && loc->expr->dw_loc_next == NULL
19198 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19200 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19201 loc->expr->dw_loc_oprnd1.v.val_addr
19202 = plus_constant (GET_MODE (x), x, off);
19204 else
19205 loc_list_plus_const (loc, off);
19207 add_AT_location_description (var_die, DW_AT_location, loc);
19209 else if (DECL_EXTERNAL (decl))
19210 add_AT_flag (var_die, DW_AT_declaration, 1);
19211 equate_decl_number_to_die (decl, var_die);
19212 return;
19215 /* If the compiler emitted a definition for the DECL declaration
19216 and if we already emitted a DIE for it, don't emit a second
19217 DIE for it again. Allow re-declarations of DECLs that are
19218 inside functions, though. */
19219 if (old_die && declaration && !local_scope_p (context_die))
19220 return;
19222 /* For static data members, the declaration in the class is supposed
19223 to have DW_TAG_member tag; the specification should still be
19224 DW_TAG_variable referencing the DW_TAG_member DIE. */
19225 if (declaration && class_scope_p (context_die))
19226 var_die = new_die (DW_TAG_member, context_die, decl);
19227 else
19228 var_die = new_die (DW_TAG_variable, context_die, decl);
19230 origin_die = NULL;
19231 if (origin != NULL)
19232 origin_die = add_abstract_origin_attribute (var_die, origin);
19234 /* Loop unrolling can create multiple blocks that refer to the same
19235 static variable, so we must test for the DW_AT_declaration flag.
19237 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19238 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19239 sharing them.
19241 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19243 ??? The declare_in_namespace support causes us to get two DIEs for one
19244 variable, both of which are declarations. We want to avoid considering
19245 one to be a specification, so we must test that this DIE is not a
19246 declaration. */
19247 else if (old_die && TREE_STATIC (decl) && ! declaration
19248 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19250 /* This is a definition of a C++ class level static. */
19251 add_AT_specification (var_die, old_die);
19252 specialization_p = true;
19253 if (DECL_NAME (decl))
19255 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19256 struct dwarf_file_data * file_index = lookup_filename (s.file);
19258 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19259 add_AT_file (var_die, DW_AT_decl_file, file_index);
19261 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19262 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19264 if (old_die->die_tag == DW_TAG_member)
19265 add_linkage_name (var_die, decl);
19268 else
19269 add_name_and_src_coords_attributes (var_die, decl);
19271 if ((origin == NULL && !specialization_p)
19272 || (origin != NULL
19273 && !DECL_ABSTRACT_P (decl_or_origin)
19274 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19275 decl_function_context
19276 (decl_or_origin))))
19278 tree type = TREE_TYPE (decl_or_origin);
19280 if (decl_by_reference_p (decl_or_origin))
19281 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19282 context_die);
19283 else
19284 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19285 context_die);
19288 if (origin == NULL && !specialization_p)
19290 if (TREE_PUBLIC (decl))
19291 add_AT_flag (var_die, DW_AT_external, 1);
19293 if (DECL_ARTIFICIAL (decl))
19294 add_AT_flag (var_die, DW_AT_artificial, 1);
19296 add_accessibility_attribute (var_die, decl);
19299 if (declaration)
19300 add_AT_flag (var_die, DW_AT_declaration, 1);
19302 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19303 equate_decl_number_to_die (decl, var_die);
19305 if (! declaration
19306 && (! DECL_ABSTRACT_P (decl_or_origin)
19307 /* Local static vars are shared between all clones/inlines,
19308 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19309 already set. */
19310 || (TREE_CODE (decl_or_origin) == VAR_DECL
19311 && TREE_STATIC (decl_or_origin)
19312 && DECL_RTL_SET_P (decl_or_origin)))
19313 /* When abstract origin already has DW_AT_location attribute, no need
19314 to add it again. */
19315 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19317 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19318 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19319 defer_location (decl_or_origin, var_die);
19320 else
19321 add_location_or_const_value_attribute (var_die, decl_or_origin,
19322 decl == NULL, DW_AT_location);
19323 add_pubname (decl_or_origin, var_die);
19325 else
19326 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19329 /* Generate a DIE to represent a named constant. */
19331 static void
19332 gen_const_die (tree decl, dw_die_ref context_die)
19334 dw_die_ref const_die;
19335 tree type = TREE_TYPE (decl);
19337 const_die = new_die (DW_TAG_constant, context_die, decl);
19338 add_name_and_src_coords_attributes (const_die, decl);
19339 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19340 if (TREE_PUBLIC (decl))
19341 add_AT_flag (const_die, DW_AT_external, 1);
19342 if (DECL_ARTIFICIAL (decl))
19343 add_AT_flag (const_die, DW_AT_artificial, 1);
19344 tree_add_const_value_attribute_for_decl (const_die, decl);
19347 /* Generate a DIE to represent a label identifier. */
19349 static void
19350 gen_label_die (tree decl, dw_die_ref context_die)
19352 tree origin = decl_ultimate_origin (decl);
19353 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19354 rtx insn;
19355 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19357 if (origin != NULL)
19358 add_abstract_origin_attribute (lbl_die, origin);
19359 else
19360 add_name_and_src_coords_attributes (lbl_die, decl);
19362 if (DECL_ABSTRACT_P (decl))
19363 equate_decl_number_to_die (decl, lbl_die);
19364 else
19366 insn = DECL_RTL_IF_SET (decl);
19368 /* Deleted labels are programmer specified labels which have been
19369 eliminated because of various optimizations. We still emit them
19370 here so that it is possible to put breakpoints on them. */
19371 if (insn
19372 && (LABEL_P (insn)
19373 || ((NOTE_P (insn)
19374 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19376 /* When optimization is enabled (via -O) some parts of the compiler
19377 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19378 represent source-level labels which were explicitly declared by
19379 the user. This really shouldn't be happening though, so catch
19380 it if it ever does happen. */
19381 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19383 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19384 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19386 else if (insn
19387 && NOTE_P (insn)
19388 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19389 && CODE_LABEL_NUMBER (insn) != -1)
19391 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19392 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19397 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19398 attributes to the DIE for a block STMT, to describe where the inlined
19399 function was called from. This is similar to add_src_coords_attributes. */
19401 static inline void
19402 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19404 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19406 if (dwarf_version >= 3 || !dwarf_strict)
19408 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19409 add_AT_unsigned (die, DW_AT_call_line, s.line);
19414 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19415 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19417 static inline void
19418 add_high_low_attributes (tree stmt, dw_die_ref die)
19420 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19422 if (BLOCK_FRAGMENT_CHAIN (stmt)
19423 && (dwarf_version >= 3 || !dwarf_strict))
19425 tree chain, superblock = NULL_TREE;
19426 dw_die_ref pdie;
19427 dw_attr_ref attr = NULL;
19429 if (inlined_function_outer_scope_p (stmt))
19431 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19432 BLOCK_NUMBER (stmt));
19433 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19436 /* Optimize duplicate .debug_ranges lists or even tails of
19437 lists. If this BLOCK has same ranges as its supercontext,
19438 lookup DW_AT_ranges attribute in the supercontext (and
19439 recursively so), verify that the ranges_table contains the
19440 right values and use it instead of adding a new .debug_range. */
19441 for (chain = stmt, pdie = die;
19442 BLOCK_SAME_RANGE (chain);
19443 chain = BLOCK_SUPERCONTEXT (chain))
19445 dw_attr_ref new_attr;
19447 pdie = pdie->die_parent;
19448 if (pdie == NULL)
19449 break;
19450 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19451 break;
19452 new_attr = get_AT (pdie, DW_AT_ranges);
19453 if (new_attr == NULL
19454 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19455 break;
19456 attr = new_attr;
19457 superblock = BLOCK_SUPERCONTEXT (chain);
19459 if (attr != NULL
19460 && (ranges_table[attr->dw_attr_val.v.val_offset
19461 / 2 / DWARF2_ADDR_SIZE].num
19462 == BLOCK_NUMBER (superblock))
19463 && BLOCK_FRAGMENT_CHAIN (superblock))
19465 unsigned long off = attr->dw_attr_val.v.val_offset
19466 / 2 / DWARF2_ADDR_SIZE;
19467 unsigned long supercnt = 0, thiscnt = 0;
19468 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19469 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19471 ++supercnt;
19472 gcc_checking_assert (ranges_table[off + supercnt].num
19473 == BLOCK_NUMBER (chain));
19475 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19476 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19477 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19478 ++thiscnt;
19479 gcc_assert (supercnt >= thiscnt);
19480 add_AT_range_list (die, DW_AT_ranges,
19481 ((off + supercnt - thiscnt)
19482 * 2 * DWARF2_ADDR_SIZE),
19483 false);
19484 return;
19487 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19489 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19492 add_ranges (chain);
19493 chain = BLOCK_FRAGMENT_CHAIN (chain);
19495 while (chain);
19496 add_ranges (NULL);
19498 else
19500 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19501 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19502 BLOCK_NUMBER (stmt));
19503 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19504 BLOCK_NUMBER (stmt));
19505 add_AT_low_high_pc (die, label, label_high, false);
19509 /* Generate a DIE for a lexical block. */
19511 static void
19512 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19514 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19516 if (call_arg_locations)
19518 if (block_map.length () <= BLOCK_NUMBER (stmt))
19519 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19520 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19523 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19524 add_high_low_attributes (stmt, stmt_die);
19526 decls_for_scope (stmt, stmt_die);
19529 /* Generate a DIE for an inlined subprogram. */
19531 static void
19532 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19534 tree decl;
19536 /* The instance of function that is effectively being inlined shall not
19537 be abstract. */
19538 gcc_assert (! BLOCK_ABSTRACT (stmt));
19540 decl = block_ultimate_origin (stmt);
19542 /* Emit info for the abstract instance first, if we haven't yet. We
19543 must emit this even if the block is abstract, otherwise when we
19544 emit the block below (or elsewhere), we may end up trying to emit
19545 a die whose origin die hasn't been emitted, and crashing. */
19546 dwarf2out_abstract_function (decl);
19548 if (! BLOCK_ABSTRACT (stmt))
19550 dw_die_ref subr_die
19551 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19553 if (call_arg_locations)
19555 if (block_map.length () <= BLOCK_NUMBER (stmt))
19556 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19557 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19559 add_abstract_origin_attribute (subr_die, decl);
19560 if (TREE_ASM_WRITTEN (stmt))
19561 add_high_low_attributes (stmt, subr_die);
19562 add_call_src_coords_attributes (stmt, subr_die);
19564 decls_for_scope (stmt, subr_die);
19568 /* Generate a DIE for a field in a record, or structure. */
19570 static void
19571 gen_field_die (tree decl, dw_die_ref context_die)
19573 dw_die_ref decl_die;
19575 if (TREE_TYPE (decl) == error_mark_node)
19576 return;
19578 decl_die = new_die (DW_TAG_member, context_die, decl);
19579 add_name_and_src_coords_attributes (decl_die, decl);
19580 add_type_attribute (decl_die, member_declared_type (decl),
19581 decl_quals (decl), context_die);
19583 if (DECL_BIT_FIELD_TYPE (decl))
19585 add_byte_size_attribute (decl_die, decl);
19586 add_bit_size_attribute (decl_die, decl);
19587 add_bit_offset_attribute (decl_die, decl);
19590 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19591 add_data_member_location_attribute (decl_die, decl);
19593 if (DECL_ARTIFICIAL (decl))
19594 add_AT_flag (decl_die, DW_AT_artificial, 1);
19596 add_accessibility_attribute (decl_die, decl);
19598 /* Equate decl number to die, so that we can look up this decl later on. */
19599 equate_decl_number_to_die (decl, decl_die);
19602 #if 0
19603 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19604 Use modified_type_die instead.
19605 We keep this code here just in case these types of DIEs may be needed to
19606 represent certain things in other languages (e.g. Pascal) someday. */
19608 static void
19609 gen_pointer_type_die (tree type, dw_die_ref context_die)
19611 dw_die_ref ptr_die
19612 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19614 equate_type_number_to_die (type, ptr_die);
19615 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19616 context_die);
19617 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19620 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19621 Use modified_type_die instead.
19622 We keep this code here just in case these types of DIEs may be needed to
19623 represent certain things in other languages (e.g. Pascal) someday. */
19625 static void
19626 gen_reference_type_die (tree type, dw_die_ref context_die)
19628 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19630 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19631 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19632 else
19633 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19635 equate_type_number_to_die (type, ref_die);
19636 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19637 context_die);
19638 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19640 #endif
19642 /* Generate a DIE for a pointer to a member type. */
19644 static void
19645 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19647 dw_die_ref ptr_die
19648 = new_die (DW_TAG_ptr_to_member_type,
19649 scope_die_for (type, context_die), type);
19651 equate_type_number_to_die (type, ptr_die);
19652 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19653 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19654 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19655 context_die);
19658 typedef const char *dchar_p; /* For DEF_VEC_P. */
19660 static char *producer_string;
19662 /* Return a heap allocated producer string including command line options
19663 if -grecord-gcc-switches. */
19665 static char *
19666 gen_producer_string (void)
19668 size_t j;
19669 auto_vec<dchar_p> switches;
19670 const char *language_string = lang_hooks.name;
19671 char *producer, *tail;
19672 const char *p;
19673 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19674 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19676 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19677 switch (save_decoded_options[j].opt_index)
19679 case OPT_o:
19680 case OPT_d:
19681 case OPT_dumpbase:
19682 case OPT_dumpdir:
19683 case OPT_auxbase:
19684 case OPT_auxbase_strip:
19685 case OPT_quiet:
19686 case OPT_version:
19687 case OPT_v:
19688 case OPT_w:
19689 case OPT_L:
19690 case OPT_D:
19691 case OPT_I:
19692 case OPT_U:
19693 case OPT_SPECIAL_unknown:
19694 case OPT_SPECIAL_ignore:
19695 case OPT_SPECIAL_program_name:
19696 case OPT_SPECIAL_input_file:
19697 case OPT_grecord_gcc_switches:
19698 case OPT_gno_record_gcc_switches:
19699 case OPT__output_pch_:
19700 case OPT_fdiagnostics_show_location_:
19701 case OPT_fdiagnostics_show_option:
19702 case OPT_fdiagnostics_show_caret:
19703 case OPT_fdiagnostics_color_:
19704 case OPT_fverbose_asm:
19705 case OPT____:
19706 case OPT__sysroot_:
19707 case OPT_nostdinc:
19708 case OPT_nostdinc__:
19709 case OPT_fpreprocessed:
19710 case OPT_fltrans_output_list_:
19711 case OPT_fresolution_:
19712 /* Ignore these. */
19713 continue;
19714 default:
19715 if (cl_options[save_decoded_options[j].opt_index].flags
19716 & CL_NO_DWARF_RECORD)
19717 continue;
19718 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19719 == '-');
19720 switch (save_decoded_options[j].canonical_option[0][1])
19722 case 'M':
19723 case 'i':
19724 case 'W':
19725 continue;
19726 case 'f':
19727 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19728 "dump", 4) == 0)
19729 continue;
19730 break;
19731 default:
19732 break;
19734 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19735 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19736 break;
19739 producer = XNEWVEC (char, plen + 1 + len + 1);
19740 tail = producer;
19741 sprintf (tail, "%s %s", language_string, version_string);
19742 tail += plen;
19744 FOR_EACH_VEC_ELT (switches, j, p)
19746 len = strlen (p);
19747 *tail = ' ';
19748 memcpy (tail + 1, p, len);
19749 tail += len + 1;
19752 *tail = '\0';
19753 return producer;
19756 /* Given a C and/or C++ language/version string return the "highest".
19757 C++ is assumed to be "higher" than C in this case. Used for merging
19758 LTO translation unit languages. */
19759 static const char *
19760 highest_c_language (const char *lang1, const char *lang2)
19762 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19763 return "GNU C++14";
19764 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19765 return "GNU C++11";
19766 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19767 return "GNU C++98";
19769 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19770 return "GNU C11";
19771 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19772 return "GNU C99";
19773 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19774 return "GNU C89";
19776 gcc_unreachable ();
19780 /* Generate the DIE for the compilation unit. */
19782 static dw_die_ref
19783 gen_compile_unit_die (const char *filename)
19785 dw_die_ref die;
19786 const char *language_string = lang_hooks.name;
19787 int language;
19789 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19791 if (filename)
19793 add_name_attribute (die, filename);
19794 /* Don't add cwd for <built-in>. */
19795 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19796 add_comp_dir_attribute (die);
19799 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19801 /* If our producer is LTO try to figure out a common language to use
19802 from the global list of translation units. */
19803 if (strcmp (language_string, "GNU GIMPLE") == 0)
19805 unsigned i;
19806 tree t;
19807 const char *common_lang = NULL;
19809 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19811 if (!TRANSLATION_UNIT_LANGUAGE (t))
19812 continue;
19813 if (!common_lang)
19814 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19815 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19817 else if (strncmp (common_lang, "GNU C", 5) == 0
19818 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19819 /* Mixing C and C++ is ok, use C++ in that case. */
19820 common_lang = highest_c_language (common_lang,
19821 TRANSLATION_UNIT_LANGUAGE (t));
19822 else
19824 /* Fall back to C. */
19825 common_lang = NULL;
19826 break;
19830 if (common_lang)
19831 language_string = common_lang;
19834 language = DW_LANG_C;
19835 if (strncmp (language_string, "GNU C", 5) == 0
19836 && ISDIGIT (language_string[5]))
19838 language = DW_LANG_C89;
19839 if (dwarf_version >= 3 || !dwarf_strict)
19841 if (strcmp (language_string, "GNU C89") != 0)
19842 language = DW_LANG_C99;
19844 if (dwarf_version >= 5 /* || !dwarf_strict */)
19845 if (strcmp (language_string, "GNU C11") == 0)
19846 language = DW_LANG_C11;
19848 if (use_upc_dwarf2_extensions && flag_upc)
19849 language = DW_LANG_Upc;
19853 else if (strncmp (language_string, "GNU C++", 7) == 0)
19855 language = DW_LANG_C_plus_plus;
19856 if (dwarf_version >= 5 /* || !dwarf_strict */)
19858 if (strcmp (language_string, "GNU C++11") == 0)
19859 language = DW_LANG_C_plus_plus_11;
19860 else if (strcmp (language_string, "GNU C++14") == 0)
19861 language = DW_LANG_C_plus_plus_14;
19864 else if (strcmp (language_string, "GNU F77") == 0)
19865 language = DW_LANG_Fortran77;
19866 else if (strcmp (language_string, "GNU Pascal") == 0)
19867 language = DW_LANG_Pascal83;
19868 else if (dwarf_version >= 3 || !dwarf_strict)
19870 if (strcmp (language_string, "GNU Ada") == 0)
19871 language = DW_LANG_Ada95;
19872 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
19874 language = DW_LANG_Fortran95;
19875 if (dwarf_version >= 5 /* || !dwarf_strict */)
19877 if (strcmp (language_string, "GNU Fortran2003") == 0)
19878 language = DW_LANG_Fortran03;
19879 else if (strcmp (language_string, "GNU Fortran2008") == 0)
19880 language = DW_LANG_Fortran08;
19883 else if (strcmp (language_string, "GNU Java") == 0)
19884 language = DW_LANG_Java;
19885 else if (strcmp (language_string, "GNU Objective-C") == 0)
19886 language = DW_LANG_ObjC;
19887 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19888 language = DW_LANG_ObjC_plus_plus;
19889 else if (dwarf_version >= 5 || !dwarf_strict)
19891 if (strcmp (language_string, "GNU Go") == 0)
19892 language = DW_LANG_Go;
19895 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19896 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
19897 language = DW_LANG_Fortran90;
19899 add_AT_unsigned (die, DW_AT_language, language);
19901 switch (language)
19903 case DW_LANG_Fortran77:
19904 case DW_LANG_Fortran90:
19905 case DW_LANG_Fortran95:
19906 case DW_LANG_Fortran03:
19907 case DW_LANG_Fortran08:
19908 /* Fortran has case insensitive identifiers and the front-end
19909 lowercases everything. */
19910 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19911 break;
19912 default:
19913 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19914 break;
19916 return die;
19919 /* Generate the DIE for a base class. */
19921 static void
19922 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19924 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19926 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19927 add_data_member_location_attribute (die, binfo);
19929 if (BINFO_VIRTUAL_P (binfo))
19930 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19932 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19933 children, otherwise the default is DW_ACCESS_public. In DWARF2
19934 the default has always been DW_ACCESS_private. */
19935 if (access == access_public_node)
19937 if (dwarf_version == 2
19938 || context_die->die_tag == DW_TAG_class_type)
19939 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19941 else if (access == access_protected_node)
19942 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19943 else if (dwarf_version > 2
19944 && context_die->die_tag != DW_TAG_class_type)
19945 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19948 /* Generate a DIE for a class member. */
19950 static void
19951 gen_member_die (tree type, dw_die_ref context_die)
19953 tree member;
19954 tree binfo = TYPE_BINFO (type);
19955 dw_die_ref child;
19957 /* If this is not an incomplete type, output descriptions of each of its
19958 members. Note that as we output the DIEs necessary to represent the
19959 members of this record or union type, we will also be trying to output
19960 DIEs to represent the *types* of those members. However the `type'
19961 function (above) will specifically avoid generating type DIEs for member
19962 types *within* the list of member DIEs for this (containing) type except
19963 for those types (of members) which are explicitly marked as also being
19964 members of this (containing) type themselves. The g++ front- end can
19965 force any given type to be treated as a member of some other (containing)
19966 type by setting the TYPE_CONTEXT of the given (member) type to point to
19967 the TREE node representing the appropriate (containing) type. */
19969 /* First output info about the base classes. */
19970 if (binfo)
19972 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19973 int i;
19974 tree base;
19976 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19977 gen_inheritance_die (base,
19978 (accesses ? (*accesses)[i] : access_public_node),
19979 context_die);
19982 /* Now output info about the data members and type members. */
19983 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19985 /* If we thought we were generating minimal debug info for TYPE
19986 and then changed our minds, some of the member declarations
19987 may have already been defined. Don't define them again, but
19988 do put them in the right order. */
19990 child = lookup_decl_die (member);
19991 if (child)
19992 splice_child_die (context_die, child);
19993 else
19994 gen_decl_die (member, NULL, context_die);
19997 /* Now output info about the function members (if any). */
19998 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20000 /* Don't include clones in the member list. */
20001 if (DECL_ABSTRACT_ORIGIN (member))
20002 continue;
20004 child = lookup_decl_die (member);
20005 if (child)
20006 splice_child_die (context_die, child);
20007 else
20008 gen_decl_die (member, NULL, context_die);
20012 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20013 is set, we pretend that the type was never defined, so we only get the
20014 member DIEs needed by later specification DIEs. */
20016 static void
20017 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20018 enum debug_info_usage usage)
20020 dw_die_ref type_die = lookup_type_die (type);
20021 dw_die_ref scope_die = 0;
20022 int nested = 0;
20023 int complete = (TYPE_SIZE (type)
20024 && (! TYPE_STUB_DECL (type)
20025 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20026 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20027 complete = complete && should_emit_struct_debug (type, usage);
20029 if (type_die && ! complete)
20030 return;
20032 if (TYPE_CONTEXT (type) != NULL_TREE
20033 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20034 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20035 nested = 1;
20037 scope_die = scope_die_for (type, context_die);
20039 /* Generate child dies for template paramaters. */
20040 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20041 schedule_generic_params_dies_gen (type);
20043 if (! type_die || (nested && is_cu_die (scope_die)))
20044 /* First occurrence of type or toplevel definition of nested class. */
20046 dw_die_ref old_die = type_die;
20048 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20049 ? record_type_tag (type) : DW_TAG_union_type,
20050 scope_die, type);
20051 equate_type_number_to_die (type, type_die);
20052 if (old_die)
20053 add_AT_specification (type_die, old_die);
20054 else
20055 add_name_attribute (type_die, type_tag (type));
20057 else
20058 remove_AT (type_die, DW_AT_declaration);
20060 /* If this type has been completed, then give it a byte_size attribute and
20061 then give a list of members. */
20062 if (complete && !ns_decl)
20064 /* Prevent infinite recursion in cases where the type of some member of
20065 this type is expressed in terms of this type itself. */
20066 TREE_ASM_WRITTEN (type) = 1;
20067 add_byte_size_attribute (type_die, type);
20068 if (TYPE_STUB_DECL (type) != NULL_TREE)
20070 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20071 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20074 /* If the first reference to this type was as the return type of an
20075 inline function, then it may not have a parent. Fix this now. */
20076 if (type_die->die_parent == NULL)
20077 add_child_die (scope_die, type_die);
20079 push_decl_scope (type);
20080 gen_member_die (type, type_die);
20081 pop_decl_scope ();
20083 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20084 if (TYPE_ARTIFICIAL (type))
20085 add_AT_flag (type_die, DW_AT_artificial, 1);
20087 /* GNU extension: Record what type our vtable lives in. */
20088 if (TYPE_VFIELD (type))
20090 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20092 gen_type_die (vtype, context_die);
20093 add_AT_die_ref (type_die, DW_AT_containing_type,
20094 lookup_type_die (vtype));
20097 else
20099 add_AT_flag (type_die, DW_AT_declaration, 1);
20101 /* We don't need to do this for function-local types. */
20102 if (TYPE_STUB_DECL (type)
20103 && ! decl_function_context (TYPE_STUB_DECL (type)))
20104 vec_safe_push (incomplete_types, type);
20107 if (get_AT (type_die, DW_AT_name))
20108 add_pubtype (type, type_die);
20111 /* Generate a DIE for a subroutine _type_. */
20113 static void
20114 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20116 tree return_type = TREE_TYPE (type);
20117 dw_die_ref subr_die
20118 = new_die (DW_TAG_subroutine_type,
20119 scope_die_for (type, context_die), type);
20121 equate_type_number_to_die (type, subr_die);
20122 add_prototyped_attribute (subr_die, type);
20123 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20124 gen_formal_types_die (type, subr_die);
20126 if (get_AT (subr_die, DW_AT_name))
20127 add_pubtype (type, subr_die);
20130 /* Generate a DIE for a type definition. */
20132 static void
20133 gen_typedef_die (tree decl, dw_die_ref context_die)
20135 dw_die_ref type_die;
20136 tree origin;
20138 if (TREE_ASM_WRITTEN (decl))
20139 return;
20141 TREE_ASM_WRITTEN (decl) = 1;
20142 type_die = new_die (DW_TAG_typedef, context_die, decl);
20143 origin = decl_ultimate_origin (decl);
20144 if (origin != NULL)
20145 add_abstract_origin_attribute (type_die, origin);
20146 else
20148 tree type;
20150 add_name_and_src_coords_attributes (type_die, decl);
20151 if (DECL_ORIGINAL_TYPE (decl))
20153 type = DECL_ORIGINAL_TYPE (decl);
20155 gcc_assert (type != TREE_TYPE (decl));
20156 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20158 else
20160 type = TREE_TYPE (decl);
20162 if (is_naming_typedef_decl (TYPE_NAME (type)))
20164 /* Here, we are in the case of decl being a typedef naming
20165 an anonymous type, e.g:
20166 typedef struct {...} foo;
20167 In that case TREE_TYPE (decl) is not a typedef variant
20168 type and TYPE_NAME of the anonymous type is set to the
20169 TYPE_DECL of the typedef. This construct is emitted by
20170 the C++ FE.
20172 TYPE is the anonymous struct named by the typedef
20173 DECL. As we need the DW_AT_type attribute of the
20174 DW_TAG_typedef to point to the DIE of TYPE, let's
20175 generate that DIE right away. add_type_attribute
20176 called below will then pick (via lookup_type_die) that
20177 anonymous struct DIE. */
20178 if (!TREE_ASM_WRITTEN (type))
20179 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20181 /* This is a GNU Extension. We are adding a
20182 DW_AT_linkage_name attribute to the DIE of the
20183 anonymous struct TYPE. The value of that attribute
20184 is the name of the typedef decl naming the anonymous
20185 struct. This greatly eases the work of consumers of
20186 this debug info. */
20187 add_linkage_attr (lookup_type_die (type), decl);
20191 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20193 if (is_naming_typedef_decl (decl))
20194 /* We want that all subsequent calls to lookup_type_die with
20195 TYPE in argument yield the DW_TAG_typedef we have just
20196 created. */
20197 equate_type_number_to_die (type, type_die);
20199 add_accessibility_attribute (type_die, decl);
20202 if (DECL_ABSTRACT_P (decl))
20203 equate_decl_number_to_die (decl, type_die);
20205 if (get_AT (type_die, DW_AT_name))
20206 add_pubtype (decl, type_die);
20209 /* Generate a DIE for a struct, class, enum or union type. */
20211 static void
20212 gen_tagged_type_die (tree type,
20213 dw_die_ref context_die,
20214 enum debug_info_usage usage)
20216 int need_pop;
20218 if (type == NULL_TREE
20219 || !is_tagged_type (type))
20220 return;
20222 /* If this is a nested type whose containing class hasn't been written
20223 out yet, writing it out will cover this one, too. This does not apply
20224 to instantiations of member class templates; they need to be added to
20225 the containing class as they are generated. FIXME: This hurts the
20226 idea of combining type decls from multiple TUs, since we can't predict
20227 what set of template instantiations we'll get. */
20228 if (TYPE_CONTEXT (type)
20229 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20230 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20232 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20234 if (TREE_ASM_WRITTEN (type))
20235 return;
20237 /* If that failed, attach ourselves to the stub. */
20238 push_decl_scope (TYPE_CONTEXT (type));
20239 context_die = lookup_type_die (TYPE_CONTEXT (type));
20240 need_pop = 1;
20242 else if (TYPE_CONTEXT (type) != NULL_TREE
20243 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20245 /* If this type is local to a function that hasn't been written
20246 out yet, use a NULL context for now; it will be fixed up in
20247 decls_for_scope. */
20248 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20249 /* A declaration DIE doesn't count; nested types need to go in the
20250 specification. */
20251 if (context_die && is_declaration_die (context_die))
20252 context_die = NULL;
20253 need_pop = 0;
20255 else
20257 context_die = declare_in_namespace (type, context_die);
20258 need_pop = 0;
20261 if (TREE_CODE (type) == ENUMERAL_TYPE)
20263 /* This might have been written out by the call to
20264 declare_in_namespace. */
20265 if (!TREE_ASM_WRITTEN (type))
20266 gen_enumeration_type_die (type, context_die);
20268 else
20269 gen_struct_or_union_type_die (type, context_die, usage);
20271 if (need_pop)
20272 pop_decl_scope ();
20274 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20275 it up if it is ever completed. gen_*_type_die will set it for us
20276 when appropriate. */
20279 /* Generate a type description DIE. */
20281 static void
20282 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20283 enum debug_info_usage usage)
20285 struct array_descr_info info;
20287 if (type == NULL_TREE || type == error_mark_node)
20288 return;
20290 if (TYPE_NAME (type) != NULL_TREE
20291 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20292 && is_redundant_typedef (TYPE_NAME (type))
20293 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20294 /* The DECL of this type is a typedef we don't want to emit debug
20295 info for but we want debug info for its underlying typedef.
20296 This can happen for e.g, the injected-class-name of a C++
20297 type. */
20298 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20300 /* If TYPE is a typedef type variant, let's generate debug info
20301 for the parent typedef which TYPE is a type of. */
20302 if (typedef_variant_p (type))
20304 if (TREE_ASM_WRITTEN (type))
20305 return;
20307 /* Prevent broken recursion; we can't hand off to the same type. */
20308 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20310 /* Give typedefs the right scope. */
20311 context_die = scope_die_for (type, context_die);
20313 TREE_ASM_WRITTEN (type) = 1;
20315 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20316 return;
20319 /* If type is an anonymous tagged type named by a typedef, let's
20320 generate debug info for the typedef. */
20321 if (is_naming_typedef_decl (TYPE_NAME (type)))
20323 /* Use the DIE of the containing namespace as the parent DIE of
20324 the type description DIE we want to generate. */
20325 if (DECL_CONTEXT (TYPE_NAME (type))
20326 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20327 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20329 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20330 return;
20333 /* We are going to output a DIE to represent the unqualified version
20334 of this type (i.e. without any const or volatile qualifiers) so
20335 get the main variant (i.e. the unqualified version) of this type
20336 now. (Vectors are special because the debugging info is in the
20337 cloned type itself). */
20338 if (TREE_CODE (type) != VECTOR_TYPE)
20339 type = type_main_variant (type);
20341 /* If this is an array type with hidden descriptor, handle it first. */
20342 if (!TREE_ASM_WRITTEN (type)
20343 && lang_hooks.types.get_array_descr_info)
20345 memset (&info, 0, sizeof (info));
20346 if (lang_hooks.types.get_array_descr_info (type, &info))
20348 gen_descr_array_type_die (type, &info, context_die);
20349 TREE_ASM_WRITTEN (type) = 1;
20350 return;
20354 if (TREE_ASM_WRITTEN (type))
20355 return;
20357 switch (TREE_CODE (type))
20359 case ERROR_MARK:
20360 break;
20362 case POINTER_TYPE:
20363 case REFERENCE_TYPE:
20364 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20365 ensures that the gen_type_die recursion will terminate even if the
20366 type is recursive. Recursive types are possible in Ada. */
20367 /* ??? We could perhaps do this for all types before the switch
20368 statement. */
20369 TREE_ASM_WRITTEN (type) = 1;
20371 /* For these types, all that is required is that we output a DIE (or a
20372 set of DIEs) to represent the "basis" type. */
20373 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20374 DINFO_USAGE_IND_USE);
20375 break;
20377 case OFFSET_TYPE:
20378 /* This code is used for C++ pointer-to-data-member types.
20379 Output a description of the relevant class type. */
20380 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20381 DINFO_USAGE_IND_USE);
20383 /* Output a description of the type of the object pointed to. */
20384 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20385 DINFO_USAGE_IND_USE);
20387 /* Now output a DIE to represent this pointer-to-data-member type
20388 itself. */
20389 gen_ptr_to_mbr_type_die (type, context_die);
20390 break;
20392 case FUNCTION_TYPE:
20393 /* Force out return type (in case it wasn't forced out already). */
20394 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20395 DINFO_USAGE_DIR_USE);
20396 gen_subroutine_type_die (type, context_die);
20397 break;
20399 case METHOD_TYPE:
20400 /* Force out return type (in case it wasn't forced out already). */
20401 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20402 DINFO_USAGE_DIR_USE);
20403 gen_subroutine_type_die (type, context_die);
20404 break;
20406 case ARRAY_TYPE:
20407 gen_array_type_die (type, context_die);
20408 break;
20410 case VECTOR_TYPE:
20411 gen_array_type_die (type, context_die);
20412 break;
20414 case ENUMERAL_TYPE:
20415 case RECORD_TYPE:
20416 case UNION_TYPE:
20417 case QUAL_UNION_TYPE:
20418 gen_tagged_type_die (type, context_die, usage);
20419 return;
20421 case VOID_TYPE:
20422 case INTEGER_TYPE:
20423 case REAL_TYPE:
20424 case FIXED_POINT_TYPE:
20425 case COMPLEX_TYPE:
20426 case BOOLEAN_TYPE:
20427 case POINTER_BOUNDS_TYPE:
20428 /* No DIEs needed for fundamental types. */
20429 break;
20431 case NULLPTR_TYPE:
20432 case LANG_TYPE:
20433 /* Just use DW_TAG_unspecified_type. */
20435 dw_die_ref type_die = lookup_type_die (type);
20436 if (type_die == NULL)
20438 tree name = TYPE_IDENTIFIER (type);
20439 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20440 type);
20441 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20442 equate_type_number_to_die (type, type_die);
20445 break;
20447 default:
20448 if (is_cxx_auto (type))
20450 tree name = TYPE_IDENTIFIER (type);
20451 dw_die_ref *die = (name == get_identifier ("auto")
20452 ? &auto_die : &decltype_auto_die);
20453 if (!*die)
20455 *die = new_die (DW_TAG_unspecified_type,
20456 comp_unit_die (), NULL_TREE);
20457 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20459 equate_type_number_to_die (type, *die);
20460 break;
20462 gcc_unreachable ();
20465 TREE_ASM_WRITTEN (type) = 1;
20468 static void
20469 gen_type_die (tree type, dw_die_ref context_die)
20471 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20474 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20475 things which are local to the given block. */
20477 static void
20478 gen_block_die (tree stmt, dw_die_ref context_die)
20480 int must_output_die = 0;
20481 bool inlined_func;
20483 /* Ignore blocks that are NULL. */
20484 if (stmt == NULL_TREE)
20485 return;
20487 inlined_func = inlined_function_outer_scope_p (stmt);
20489 /* If the block is one fragment of a non-contiguous block, do not
20490 process the variables, since they will have been done by the
20491 origin block. Do process subblocks. */
20492 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20494 tree sub;
20496 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20497 gen_block_die (sub, context_die);
20499 return;
20502 /* Determine if we need to output any Dwarf DIEs at all to represent this
20503 block. */
20504 if (inlined_func)
20505 /* The outer scopes for inlinings *must* always be represented. We
20506 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20507 must_output_die = 1;
20508 else
20510 /* Determine if this block directly contains any "significant"
20511 local declarations which we will need to output DIEs for. */
20512 if (debug_info_level > DINFO_LEVEL_TERSE)
20513 /* We are not in terse mode so *any* local declaration counts
20514 as being a "significant" one. */
20515 must_output_die = ((BLOCK_VARS (stmt) != NULL
20516 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20517 && (TREE_USED (stmt)
20518 || TREE_ASM_WRITTEN (stmt)
20519 || BLOCK_ABSTRACT (stmt)));
20520 else if ((TREE_USED (stmt)
20521 || TREE_ASM_WRITTEN (stmt)
20522 || BLOCK_ABSTRACT (stmt))
20523 && !dwarf2out_ignore_block (stmt))
20524 must_output_die = 1;
20527 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20528 DIE for any block which contains no significant local declarations at
20529 all. Rather, in such cases we just call `decls_for_scope' so that any
20530 needed Dwarf info for any sub-blocks will get properly generated. Note
20531 that in terse mode, our definition of what constitutes a "significant"
20532 local declaration gets restricted to include only inlined function
20533 instances and local (nested) function definitions. */
20534 if (must_output_die)
20536 if (inlined_func)
20538 /* If STMT block is abstract, that means we have been called
20539 indirectly from dwarf2out_abstract_function.
20540 That function rightfully marks the descendent blocks (of
20541 the abstract function it is dealing with) as being abstract,
20542 precisely to prevent us from emitting any
20543 DW_TAG_inlined_subroutine DIE as a descendent
20544 of an abstract function instance. So in that case, we should
20545 not call gen_inlined_subroutine_die.
20547 Later though, when cgraph asks dwarf2out to emit info
20548 for the concrete instance of the function decl into which
20549 the concrete instance of STMT got inlined, the later will lead
20550 to the generation of a DW_TAG_inlined_subroutine DIE. */
20551 if (! BLOCK_ABSTRACT (stmt))
20552 gen_inlined_subroutine_die (stmt, context_die);
20554 else
20555 gen_lexical_block_die (stmt, context_die);
20557 else
20558 decls_for_scope (stmt, context_die);
20561 /* Process variable DECL (or variable with origin ORIGIN) within
20562 block STMT and add it to CONTEXT_DIE. */
20563 static void
20564 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20566 dw_die_ref die;
20567 tree decl_or_origin = decl ? decl : origin;
20569 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20570 die = lookup_decl_die (decl_or_origin);
20571 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20572 && TYPE_DECL_IS_STUB (decl_or_origin))
20573 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20574 else
20575 die = NULL;
20577 if (die != NULL && die->die_parent == NULL)
20578 add_child_die (context_die, die);
20579 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20580 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20581 stmt, context_die);
20582 else
20583 gen_decl_die (decl, origin, context_die);
20586 /* Generate all of the decls declared within a given scope and (recursively)
20587 all of its sub-blocks. */
20589 static void
20590 decls_for_scope (tree stmt, dw_die_ref context_die)
20592 tree decl;
20593 unsigned int i;
20594 tree subblocks;
20596 /* Ignore NULL blocks. */
20597 if (stmt == NULL_TREE)
20598 return;
20600 /* Output the DIEs to represent all of the data objects and typedefs
20601 declared directly within this block but not within any nested
20602 sub-blocks. Also, nested function and tag DIEs have been
20603 generated with a parent of NULL; fix that up now. We don't
20604 have to do this if we're at -g1. */
20605 if (debug_info_level > DINFO_LEVEL_TERSE)
20607 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20608 process_scope_var (stmt, decl, NULL_TREE, context_die);
20609 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20610 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20611 context_die);
20614 /* Even if we're at -g1, we need to process the subblocks in order to get
20615 inlined call information. */
20617 /* Output the DIEs to represent all sub-blocks (and the items declared
20618 therein) of this block. */
20619 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20620 subblocks != NULL;
20621 subblocks = BLOCK_CHAIN (subblocks))
20622 gen_block_die (subblocks, context_die);
20625 /* Is this a typedef we can avoid emitting? */
20627 static inline int
20628 is_redundant_typedef (const_tree decl)
20630 if (TYPE_DECL_IS_STUB (decl))
20631 return 1;
20633 if (DECL_ARTIFICIAL (decl)
20634 && DECL_CONTEXT (decl)
20635 && is_tagged_type (DECL_CONTEXT (decl))
20636 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20637 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20638 /* Also ignore the artificial member typedef for the class name. */
20639 return 1;
20641 return 0;
20644 /* Return TRUE if TYPE is a typedef that names a type for linkage
20645 purposes. This kind of typedefs is produced by the C++ FE for
20646 constructs like:
20648 typedef struct {...} foo;
20650 In that case, there is no typedef variant type produced for foo.
20651 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20652 struct type. */
20654 static bool
20655 is_naming_typedef_decl (const_tree decl)
20657 if (decl == NULL_TREE
20658 || TREE_CODE (decl) != TYPE_DECL
20659 || !is_tagged_type (TREE_TYPE (decl))
20660 || DECL_IS_BUILTIN (decl)
20661 || is_redundant_typedef (decl)
20662 /* It looks like Ada produces TYPE_DECLs that are very similar
20663 to C++ naming typedefs but that have different
20664 semantics. Let's be specific to c++ for now. */
20665 || !is_cxx ())
20666 return FALSE;
20668 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20669 && TYPE_NAME (TREE_TYPE (decl)) == decl
20670 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20671 != TYPE_NAME (TREE_TYPE (decl))));
20674 /* Returns the DIE for a context. */
20676 static inline dw_die_ref
20677 get_context_die (tree context)
20679 if (context)
20681 /* Find die that represents this context. */
20682 if (TYPE_P (context))
20684 context = TYPE_MAIN_VARIANT (context);
20685 return strip_naming_typedef (context, force_type_die (context));
20687 else
20688 return force_decl_die (context);
20690 return comp_unit_die ();
20693 /* Returns the DIE for decl. A DIE will always be returned. */
20695 static dw_die_ref
20696 force_decl_die (tree decl)
20698 dw_die_ref decl_die;
20699 unsigned saved_external_flag;
20700 tree save_fn = NULL_TREE;
20701 decl_die = lookup_decl_die (decl);
20702 if (!decl_die)
20704 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20706 decl_die = lookup_decl_die (decl);
20707 if (decl_die)
20708 return decl_die;
20710 switch (TREE_CODE (decl))
20712 case FUNCTION_DECL:
20713 /* Clear current_function_decl, so that gen_subprogram_die thinks
20714 that this is a declaration. At this point, we just want to force
20715 declaration die. */
20716 save_fn = current_function_decl;
20717 current_function_decl = NULL_TREE;
20718 gen_subprogram_die (decl, context_die);
20719 current_function_decl = save_fn;
20720 break;
20722 case VAR_DECL:
20723 /* Set external flag to force declaration die. Restore it after
20724 gen_decl_die() call. */
20725 saved_external_flag = DECL_EXTERNAL (decl);
20726 DECL_EXTERNAL (decl) = 1;
20727 gen_decl_die (decl, NULL, context_die);
20728 DECL_EXTERNAL (decl) = saved_external_flag;
20729 break;
20731 case NAMESPACE_DECL:
20732 if (dwarf_version >= 3 || !dwarf_strict)
20733 dwarf2out_decl (decl);
20734 else
20735 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20736 decl_die = comp_unit_die ();
20737 break;
20739 case TRANSLATION_UNIT_DECL:
20740 decl_die = comp_unit_die ();
20741 break;
20743 default:
20744 gcc_unreachable ();
20747 /* We should be able to find the DIE now. */
20748 if (!decl_die)
20749 decl_die = lookup_decl_die (decl);
20750 gcc_assert (decl_die);
20753 return decl_die;
20756 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20757 always returned. */
20759 static dw_die_ref
20760 force_type_die (tree type)
20762 dw_die_ref type_die;
20764 type_die = lookup_type_die (type);
20765 if (!type_die)
20767 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20769 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20770 context_die);
20771 gcc_assert (type_die);
20773 return type_die;
20776 /* Force out any required namespaces to be able to output DECL,
20777 and return the new context_die for it, if it's changed. */
20779 static dw_die_ref
20780 setup_namespace_context (tree thing, dw_die_ref context_die)
20782 tree context = (DECL_P (thing)
20783 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20784 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20785 /* Force out the namespace. */
20786 context_die = force_decl_die (context);
20788 return context_die;
20791 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20792 type) within its namespace, if appropriate.
20794 For compatibility with older debuggers, namespace DIEs only contain
20795 declarations; all definitions are emitted at CU scope. */
20797 static dw_die_ref
20798 declare_in_namespace (tree thing, dw_die_ref context_die)
20800 dw_die_ref ns_context;
20802 if (debug_info_level <= DINFO_LEVEL_TERSE)
20803 return context_die;
20805 /* External declarations in the local scope only need to be emitted
20806 once, not once in the namespace and once in the scope.
20808 This avoids declaring the `extern' below in the
20809 namespace DIE as well as in the innermost scope:
20811 namespace S
20813 int i=5;
20814 int foo()
20816 int i=8;
20817 extern int i;
20818 return i;
20822 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20823 return context_die;
20825 /* If this decl is from an inlined function, then don't try to emit it in its
20826 namespace, as we will get confused. It would have already been emitted
20827 when the abstract instance of the inline function was emitted anyways. */
20828 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20829 return context_die;
20831 ns_context = setup_namespace_context (thing, context_die);
20833 if (ns_context != context_die)
20835 if (is_fortran ())
20836 return ns_context;
20837 if (DECL_P (thing))
20838 gen_decl_die (thing, NULL, ns_context);
20839 else
20840 gen_type_die (thing, ns_context);
20842 return context_die;
20845 /* Generate a DIE for a namespace or namespace alias. */
20847 static void
20848 gen_namespace_die (tree decl, dw_die_ref context_die)
20850 dw_die_ref namespace_die;
20852 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20853 they are an alias of. */
20854 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20856 /* Output a real namespace or module. */
20857 context_die = setup_namespace_context (decl, comp_unit_die ());
20858 namespace_die = new_die (is_fortran ()
20859 ? DW_TAG_module : DW_TAG_namespace,
20860 context_die, decl);
20861 /* For Fortran modules defined in different CU don't add src coords. */
20862 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20864 const char *name = dwarf2_name (decl, 0);
20865 if (name)
20866 add_name_attribute (namespace_die, name);
20868 else
20869 add_name_and_src_coords_attributes (namespace_die, decl);
20870 if (DECL_EXTERNAL (decl))
20871 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20872 equate_decl_number_to_die (decl, namespace_die);
20874 else
20876 /* Output a namespace alias. */
20878 /* Force out the namespace we are an alias of, if necessary. */
20879 dw_die_ref origin_die
20880 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20882 if (DECL_FILE_SCOPE_P (decl)
20883 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20884 context_die = setup_namespace_context (decl, comp_unit_die ());
20885 /* Now create the namespace alias DIE. */
20886 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20887 add_name_and_src_coords_attributes (namespace_die, decl);
20888 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20889 equate_decl_number_to_die (decl, namespace_die);
20891 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20892 if (want_pubnames ())
20893 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20896 /* Generate Dwarf debug information for a decl described by DECL.
20897 The return value is currently only meaningful for PARM_DECLs,
20898 for all other decls it returns NULL. */
20900 static dw_die_ref
20901 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20903 tree decl_or_origin = decl ? decl : origin;
20904 tree class_origin = NULL, ultimate_origin;
20906 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20907 return NULL;
20909 /* Ignore pointer bounds decls. */
20910 if (DECL_P (decl_or_origin)
20911 && TREE_TYPE (decl_or_origin)
20912 && POINTER_BOUNDS_P (decl_or_origin))
20913 return NULL;
20915 switch (TREE_CODE (decl_or_origin))
20917 case ERROR_MARK:
20918 break;
20920 case CONST_DECL:
20921 if (!is_fortran () && !is_ada ())
20923 /* The individual enumerators of an enum type get output when we output
20924 the Dwarf representation of the relevant enum type itself. */
20925 break;
20928 /* Emit its type. */
20929 gen_type_die (TREE_TYPE (decl), context_die);
20931 /* And its containing namespace. */
20932 context_die = declare_in_namespace (decl, context_die);
20934 gen_const_die (decl, context_die);
20935 break;
20937 case FUNCTION_DECL:
20938 /* Don't output any DIEs to represent mere function declarations,
20939 unless they are class members or explicit block externs. */
20940 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20941 && DECL_FILE_SCOPE_P (decl_or_origin)
20942 && (current_function_decl == NULL_TREE
20943 || DECL_ARTIFICIAL (decl_or_origin)))
20944 break;
20946 #if 0
20947 /* FIXME */
20948 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20949 on local redeclarations of global functions. That seems broken. */
20950 if (current_function_decl != decl)
20951 /* This is only a declaration. */;
20952 #endif
20954 /* If we're emitting a clone, emit info for the abstract instance. */
20955 if (origin || DECL_ORIGIN (decl) != decl)
20956 dwarf2out_abstract_function (origin
20957 ? DECL_ORIGIN (origin)
20958 : DECL_ABSTRACT_ORIGIN (decl));
20960 /* If we're emitting an out-of-line copy of an inline function,
20961 emit info for the abstract instance and set up to refer to it. */
20962 else if (cgraph_function_possibly_inlined_p (decl)
20963 && ! DECL_ABSTRACT_P (decl)
20964 && ! class_or_namespace_scope_p (context_die)
20965 /* dwarf2out_abstract_function won't emit a die if this is just
20966 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20967 that case, because that works only if we have a die. */
20968 && DECL_INITIAL (decl) != NULL_TREE)
20970 dwarf2out_abstract_function (decl);
20971 set_decl_origin_self (decl);
20974 /* Otherwise we're emitting the primary DIE for this decl. */
20975 else if (debug_info_level > DINFO_LEVEL_TERSE)
20977 /* Before we describe the FUNCTION_DECL itself, make sure that we
20978 have its containing type. */
20979 if (!origin)
20980 origin = decl_class_context (decl);
20981 if (origin != NULL_TREE)
20982 gen_type_die (origin, context_die);
20984 /* And its return type. */
20985 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20987 /* And its virtual context. */
20988 if (DECL_VINDEX (decl) != NULL_TREE)
20989 gen_type_die (DECL_CONTEXT (decl), context_die);
20991 /* Make sure we have a member DIE for decl. */
20992 if (origin != NULL_TREE)
20993 gen_type_die_for_member (origin, decl, context_die);
20995 /* And its containing namespace. */
20996 context_die = declare_in_namespace (decl, context_die);
20999 /* Now output a DIE to represent the function itself. */
21000 if (decl)
21001 gen_subprogram_die (decl, context_die);
21002 break;
21004 case TYPE_DECL:
21005 /* If we are in terse mode, don't generate any DIEs to represent any
21006 actual typedefs. */
21007 if (debug_info_level <= DINFO_LEVEL_TERSE)
21008 break;
21010 /* In the special case of a TYPE_DECL node representing the declaration
21011 of some type tag, if the given TYPE_DECL is marked as having been
21012 instantiated from some other (original) TYPE_DECL node (e.g. one which
21013 was generated within the original definition of an inline function) we
21014 used to generate a special (abbreviated) DW_TAG_structure_type,
21015 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21016 should be actually referencing those DIEs, as variable DIEs with that
21017 type would be emitted already in the abstract origin, so it was always
21018 removed during unused type prunning. Don't add anything in this
21019 case. */
21020 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21021 break;
21023 if (is_redundant_typedef (decl))
21024 gen_type_die (TREE_TYPE (decl), context_die);
21025 else
21026 /* Output a DIE to represent the typedef itself. */
21027 gen_typedef_die (decl, context_die);
21028 break;
21030 case LABEL_DECL:
21031 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21032 gen_label_die (decl, context_die);
21033 break;
21035 case VAR_DECL:
21036 case RESULT_DECL:
21037 /* If we are in terse mode, don't generate any DIEs to represent any
21038 variable declarations or definitions. */
21039 if (debug_info_level <= DINFO_LEVEL_TERSE)
21040 break;
21042 /* Output any DIEs that are needed to specify the type of this data
21043 object. */
21044 if (decl_by_reference_p (decl_or_origin))
21045 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21046 else
21047 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21049 /* And its containing type. */
21050 class_origin = decl_class_context (decl_or_origin);
21051 if (class_origin != NULL_TREE)
21052 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21054 /* And its containing namespace. */
21055 context_die = declare_in_namespace (decl_or_origin, context_die);
21057 /* Now output the DIE to represent the data object itself. This gets
21058 complicated because of the possibility that the VAR_DECL really
21059 represents an inlined instance of a formal parameter for an inline
21060 function. */
21061 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21062 if (ultimate_origin != NULL_TREE
21063 && TREE_CODE (ultimate_origin) == PARM_DECL)
21064 gen_formal_parameter_die (decl, origin,
21065 true /* Emit name attribute. */,
21066 context_die);
21067 else
21068 gen_variable_die (decl, origin, context_die);
21069 break;
21071 case FIELD_DECL:
21072 /* Ignore the nameless fields that are used to skip bits but handle C++
21073 anonymous unions and structs. */
21074 if (DECL_NAME (decl) != NULL_TREE
21075 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21076 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21078 gen_type_die (member_declared_type (decl), context_die);
21079 gen_field_die (decl, context_die);
21081 break;
21083 case PARM_DECL:
21084 if (DECL_BY_REFERENCE (decl_or_origin))
21085 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21086 else
21087 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21088 return gen_formal_parameter_die (decl, origin,
21089 true /* Emit name attribute. */,
21090 context_die);
21092 case NAMESPACE_DECL:
21093 case IMPORTED_DECL:
21094 if (dwarf_version >= 3 || !dwarf_strict)
21095 gen_namespace_die (decl, context_die);
21096 break;
21098 case NAMELIST_DECL:
21099 gen_namelist_decl (DECL_NAME (decl), context_die,
21100 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21101 break;
21103 default:
21104 /* Probably some frontend-internal decl. Assume we don't care. */
21105 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21106 break;
21109 return NULL;
21112 /* Output debug information for global decl DECL. Called from toplev.c after
21113 compilation proper has finished. */
21115 static void
21116 dwarf2out_global_decl (tree decl)
21118 /* Output DWARF2 information for file-scope tentative data object
21119 declarations, file-scope (extern) function declarations (which
21120 had no corresponding body) and file-scope tagged type declarations
21121 and definitions which have not yet been forced out. */
21122 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21123 && !POINTER_BOUNDS_P (decl))
21124 dwarf2out_decl (decl);
21127 /* Output debug information for type decl DECL. Called from toplev.c
21128 and from language front ends (to record built-in types). */
21129 static void
21130 dwarf2out_type_decl (tree decl, int local)
21132 if (!local)
21133 dwarf2out_decl (decl);
21136 /* Output debug information for imported module or decl DECL.
21137 NAME is non-NULL name in the lexical block if the decl has been renamed.
21138 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21139 that DECL belongs to.
21140 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21141 static void
21142 dwarf2out_imported_module_or_decl_1 (tree decl,
21143 tree name,
21144 tree lexical_block,
21145 dw_die_ref lexical_block_die)
21147 expanded_location xloc;
21148 dw_die_ref imported_die = NULL;
21149 dw_die_ref at_import_die;
21151 if (TREE_CODE (decl) == IMPORTED_DECL)
21153 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21154 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21155 gcc_assert (decl);
21157 else
21158 xloc = expand_location (input_location);
21160 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21162 at_import_die = force_type_die (TREE_TYPE (decl));
21163 /* For namespace N { typedef void T; } using N::T; base_type_die
21164 returns NULL, but DW_TAG_imported_declaration requires
21165 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21166 if (!at_import_die)
21168 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21169 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21170 at_import_die = lookup_type_die (TREE_TYPE (decl));
21171 gcc_assert (at_import_die);
21174 else
21176 at_import_die = lookup_decl_die (decl);
21177 if (!at_import_die)
21179 /* If we're trying to avoid duplicate debug info, we may not have
21180 emitted the member decl for this field. Emit it now. */
21181 if (TREE_CODE (decl) == FIELD_DECL)
21183 tree type = DECL_CONTEXT (decl);
21185 if (TYPE_CONTEXT (type)
21186 && TYPE_P (TYPE_CONTEXT (type))
21187 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21188 DINFO_USAGE_DIR_USE))
21189 return;
21190 gen_type_die_for_member (type, decl,
21191 get_context_die (TYPE_CONTEXT (type)));
21193 if (TREE_CODE (decl) == NAMELIST_DECL)
21194 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21195 get_context_die (DECL_CONTEXT (decl)),
21196 NULL_TREE);
21197 else
21198 at_import_die = force_decl_die (decl);
21202 if (TREE_CODE (decl) == NAMESPACE_DECL)
21204 if (dwarf_version >= 3 || !dwarf_strict)
21205 imported_die = new_die (DW_TAG_imported_module,
21206 lexical_block_die,
21207 lexical_block);
21208 else
21209 return;
21211 else
21212 imported_die = new_die (DW_TAG_imported_declaration,
21213 lexical_block_die,
21214 lexical_block);
21216 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21217 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21218 if (name)
21219 add_AT_string (imported_die, DW_AT_name,
21220 IDENTIFIER_POINTER (name));
21221 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21224 /* Output debug information for imported module or decl DECL.
21225 NAME is non-NULL name in context if the decl has been renamed.
21226 CHILD is true if decl is one of the renamed decls as part of
21227 importing whole module. */
21229 static void
21230 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21231 bool child)
21233 /* dw_die_ref at_import_die; */
21234 dw_die_ref scope_die;
21236 if (debug_info_level <= DINFO_LEVEL_TERSE)
21237 return;
21239 gcc_assert (decl);
21241 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21242 We need decl DIE for reference and scope die. First, get DIE for the decl
21243 itself. */
21245 /* Get the scope die for decl context. Use comp_unit_die for global module
21246 or decl. If die is not found for non globals, force new die. */
21247 if (context
21248 && TYPE_P (context)
21249 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21250 return;
21252 if (!(dwarf_version >= 3 || !dwarf_strict))
21253 return;
21255 scope_die = get_context_die (context);
21257 if (child)
21259 gcc_assert (scope_die->die_child);
21260 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21261 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21262 scope_die = scope_die->die_child;
21265 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21266 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21270 /* Output debug information for namelists. */
21272 static dw_die_ref
21273 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21275 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21276 tree value;
21277 unsigned i;
21279 if (debug_info_level <= DINFO_LEVEL_TERSE)
21280 return NULL;
21282 gcc_assert (scope_die != NULL);
21283 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21284 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21286 /* If there are no item_decls, we have a nondefining namelist, e.g.
21287 with USE association; hence, set DW_AT_declaration. */
21288 if (item_decls == NULL_TREE)
21290 add_AT_flag (nml_die, DW_AT_declaration, 1);
21291 return nml_die;
21294 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21296 nml_item_ref_die = lookup_decl_die (value);
21297 if (!nml_item_ref_die)
21298 nml_item_ref_die = force_decl_die (value);
21300 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21301 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21303 return nml_die;
21307 /* Write the debugging output for DECL. */
21309 static void
21310 dwarf2out_decl (tree decl)
21312 dw_die_ref context_die = comp_unit_die ();
21314 switch (TREE_CODE (decl))
21316 case ERROR_MARK:
21317 return;
21319 case FUNCTION_DECL:
21320 /* What we would really like to do here is to filter out all mere
21321 file-scope declarations of file-scope functions which are never
21322 referenced later within this translation unit (and keep all of ones
21323 that *are* referenced later on) but we aren't clairvoyant, so we have
21324 no idea which functions will be referenced in the future (i.e. later
21325 on within the current translation unit). So here we just ignore all
21326 file-scope function declarations which are not also definitions. If
21327 and when the debugger needs to know something about these functions,
21328 it will have to hunt around and find the DWARF information associated
21329 with the definition of the function.
21331 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21332 nodes represent definitions and which ones represent mere
21333 declarations. We have to check DECL_INITIAL instead. That's because
21334 the C front-end supports some weird semantics for "extern inline"
21335 function definitions. These can get inlined within the current
21336 translation unit (and thus, we need to generate Dwarf info for their
21337 abstract instances so that the Dwarf info for the concrete inlined
21338 instances can have something to refer to) but the compiler never
21339 generates any out-of-lines instances of such things (despite the fact
21340 that they *are* definitions).
21342 The important point is that the C front-end marks these "extern
21343 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21344 them anyway. Note that the C++ front-end also plays some similar games
21345 for inline function definitions appearing within include files which
21346 also contain `#pragma interface' pragmas.
21348 If we are called from dwarf2out_abstract_function output a DIE
21349 anyway. We can end up here this way with early inlining and LTO
21350 where the inlined function is output in a different LTRANS unit
21351 or not at all. */
21352 if (DECL_INITIAL (decl) == NULL_TREE
21353 && ! DECL_ABSTRACT_P (decl))
21354 return;
21356 /* If we're a nested function, initially use a parent of NULL; if we're
21357 a plain function, this will be fixed up in decls_for_scope. If
21358 we're a method, it will be ignored, since we already have a DIE. */
21359 if (decl_function_context (decl)
21360 /* But if we're in terse mode, we don't care about scope. */
21361 && debug_info_level > DINFO_LEVEL_TERSE)
21362 context_die = NULL;
21363 break;
21365 case VAR_DECL:
21366 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21367 declaration and if the declaration was never even referenced from
21368 within this entire compilation unit. We suppress these DIEs in
21369 order to save space in the .debug section (by eliminating entries
21370 which are probably useless). Note that we must not suppress
21371 block-local extern declarations (whether used or not) because that
21372 would screw-up the debugger's name lookup mechanism and cause it to
21373 miss things which really ought to be in scope at a given point. */
21374 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21375 return;
21377 /* For local statics lookup proper context die. */
21378 if (TREE_STATIC (decl)
21379 && DECL_CONTEXT (decl)
21380 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21381 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21383 /* If we are in terse mode, don't generate any DIEs to represent any
21384 variable declarations or definitions. */
21385 if (debug_info_level <= DINFO_LEVEL_TERSE)
21386 return;
21387 break;
21389 case CONST_DECL:
21390 if (debug_info_level <= DINFO_LEVEL_TERSE)
21391 return;
21392 if (!is_fortran () && !is_ada ())
21393 return;
21394 if (TREE_STATIC (decl) && decl_function_context (decl))
21395 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21396 break;
21398 case NAMESPACE_DECL:
21399 case IMPORTED_DECL:
21400 if (debug_info_level <= DINFO_LEVEL_TERSE)
21401 return;
21402 if (lookup_decl_die (decl) != NULL)
21403 return;
21404 break;
21406 case TYPE_DECL:
21407 /* Don't emit stubs for types unless they are needed by other DIEs. */
21408 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21409 return;
21411 /* Don't bother trying to generate any DIEs to represent any of the
21412 normal built-in types for the language we are compiling. */
21413 if (DECL_IS_BUILTIN (decl))
21414 return;
21416 /* If we are in terse mode, don't generate any DIEs for types. */
21417 if (debug_info_level <= DINFO_LEVEL_TERSE)
21418 return;
21420 /* If we're a function-scope tag, initially use a parent of NULL;
21421 this will be fixed up in decls_for_scope. */
21422 if (decl_function_context (decl))
21423 context_die = NULL;
21425 break;
21427 case NAMELIST_DECL:
21428 break;
21430 default:
21431 return;
21434 gen_decl_die (decl, NULL, context_die);
21437 /* Write the debugging output for DECL. */
21439 static void
21440 dwarf2out_function_decl (tree decl)
21442 dwarf2out_decl (decl);
21443 call_arg_locations = NULL;
21444 call_arg_loc_last = NULL;
21445 call_site_count = -1;
21446 tail_call_site_count = -1;
21447 block_map.release ();
21448 decl_loc_table->empty ();
21449 cached_dw_loc_list_table->empty ();
21452 /* Output a marker (i.e. a label) for the beginning of the generated code for
21453 a lexical block. */
21455 static void
21456 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21457 unsigned int blocknum)
21459 switch_to_section (current_function_section ());
21460 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21463 /* Output a marker (i.e. a label) for the end of the generated code for a
21464 lexical block. */
21466 static void
21467 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21469 switch_to_section (current_function_section ());
21470 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21473 /* Returns nonzero if it is appropriate not to emit any debugging
21474 information for BLOCK, because it doesn't contain any instructions.
21476 Don't allow this for blocks with nested functions or local classes
21477 as we would end up with orphans, and in the presence of scheduling
21478 we may end up calling them anyway. */
21480 static bool
21481 dwarf2out_ignore_block (const_tree block)
21483 tree decl;
21484 unsigned int i;
21486 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21487 if (TREE_CODE (decl) == FUNCTION_DECL
21488 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21489 return 0;
21490 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21492 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21493 if (TREE_CODE (decl) == FUNCTION_DECL
21494 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21495 return 0;
21498 return 1;
21501 /* Hash table routines for file_hash. */
21503 bool
21504 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21506 return filename_cmp (p1->filename, p2) == 0;
21509 hashval_t
21510 dwarf_file_hasher::hash (dwarf_file_data *p)
21512 return htab_hash_string (p->filename);
21515 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21516 dwarf2out.c) and return its "index". The index of each (known) filename is
21517 just a unique number which is associated with only that one filename. We
21518 need such numbers for the sake of generating labels (in the .debug_sfnames
21519 section) and references to those files numbers (in the .debug_srcinfo
21520 and.debug_macinfo sections). If the filename given as an argument is not
21521 found in our current list, add it to the list and assign it the next
21522 available unique index number. In order to speed up searches, we remember
21523 the index of the filename was looked up last. This handles the majority of
21524 all searches. */
21526 static struct dwarf_file_data *
21527 lookup_filename (const char *file_name)
21529 struct dwarf_file_data * created;
21531 /* Check to see if the file name that was searched on the previous
21532 call matches this file name. If so, return the index. */
21533 if (file_table_last_lookup
21534 && (file_name == file_table_last_lookup->filename
21535 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21536 return file_table_last_lookup;
21538 /* Didn't match the previous lookup, search the table. */
21539 dwarf_file_data **slot
21540 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21541 INSERT);
21542 if (*slot)
21543 return *slot;
21545 created = ggc_alloc<dwarf_file_data> ();
21546 created->filename = file_name;
21547 created->emitted_number = 0;
21548 *slot = created;
21549 return created;
21552 /* If the assembler will construct the file table, then translate the compiler
21553 internal file table number into the assembler file table number, and emit
21554 a .file directive if we haven't already emitted one yet. The file table
21555 numbers are different because we prune debug info for unused variables and
21556 types, which may include filenames. */
21558 static int
21559 maybe_emit_file (struct dwarf_file_data * fd)
21561 if (! fd->emitted_number)
21563 if (last_emitted_file)
21564 fd->emitted_number = last_emitted_file->emitted_number + 1;
21565 else
21566 fd->emitted_number = 1;
21567 last_emitted_file = fd;
21569 if (DWARF2_ASM_LINE_DEBUG_INFO)
21571 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21572 output_quoted_string (asm_out_file,
21573 remap_debug_filename (fd->filename));
21574 fputc ('\n', asm_out_file);
21578 return fd->emitted_number;
21581 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21582 That generation should happen after function debug info has been
21583 generated. The value of the attribute is the constant value of ARG. */
21585 static void
21586 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21588 die_arg_entry entry;
21590 if (!die || !arg)
21591 return;
21593 if (!tmpl_value_parm_die_table)
21594 vec_alloc (tmpl_value_parm_die_table, 32);
21596 entry.die = die;
21597 entry.arg = arg;
21598 vec_safe_push (tmpl_value_parm_die_table, entry);
21601 /* Return TRUE if T is an instance of generic type, FALSE
21602 otherwise. */
21604 static bool
21605 generic_type_p (tree t)
21607 if (t == NULL_TREE || !TYPE_P (t))
21608 return false;
21609 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21612 /* Schedule the generation of the generic parameter dies for the
21613 instance of generic type T. The proper generation itself is later
21614 done by gen_scheduled_generic_parms_dies. */
21616 static void
21617 schedule_generic_params_dies_gen (tree t)
21619 if (!generic_type_p (t))
21620 return;
21622 if (!generic_type_instances)
21623 vec_alloc (generic_type_instances, 256);
21625 vec_safe_push (generic_type_instances, t);
21628 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21629 by append_entry_to_tmpl_value_parm_die_table. This function must
21630 be called after function DIEs have been generated. */
21632 static void
21633 gen_remaining_tmpl_value_param_die_attribute (void)
21635 if (tmpl_value_parm_die_table)
21637 unsigned i;
21638 die_arg_entry *e;
21640 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21641 tree_add_const_value_attribute (e->die, e->arg);
21645 /* Generate generic parameters DIEs for instances of generic types
21646 that have been previously scheduled by
21647 schedule_generic_params_dies_gen. This function must be called
21648 after all the types of the CU have been laid out. */
21650 static void
21651 gen_scheduled_generic_parms_dies (void)
21653 unsigned i;
21654 tree t;
21656 if (!generic_type_instances)
21657 return;
21659 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21660 if (COMPLETE_TYPE_P (t))
21661 gen_generic_params_dies (t);
21665 /* Replace DW_AT_name for the decl with name. */
21667 static void
21668 dwarf2out_set_name (tree decl, tree name)
21670 dw_die_ref die;
21671 dw_attr_ref attr;
21672 const char *dname;
21674 die = TYPE_SYMTAB_DIE (decl);
21675 if (!die)
21676 return;
21678 dname = dwarf2_name (name, 0);
21679 if (!dname)
21680 return;
21682 attr = get_AT (die, DW_AT_name);
21683 if (attr)
21685 struct indirect_string_node *node;
21687 node = find_AT_string (dname);
21688 /* replace the string. */
21689 attr->dw_attr_val.v.val_str = node;
21692 else
21693 add_name_attribute (die, dname);
21696 /* True if before or during processing of the first function being emitted. */
21697 static bool in_first_function_p = true;
21698 /* True if loc_note during dwarf2out_var_location call might still be
21699 before first real instruction at address equal to .Ltext0. */
21700 static bool maybe_at_text_label_p = true;
21701 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21702 static unsigned int first_loclabel_num_not_at_text_label;
21704 /* Called by the final INSN scan whenever we see a var location. We
21705 use it to drop labels in the right places, and throw the location in
21706 our lookup table. */
21708 static void
21709 dwarf2out_var_location (rtx_insn *loc_note)
21711 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21712 struct var_loc_node *newloc;
21713 rtx_insn *next_real, *next_note;
21714 static const char *last_label;
21715 static const char *last_postcall_label;
21716 static bool last_in_cold_section_p;
21717 static rtx_insn *expected_next_loc_note;
21718 tree decl;
21719 bool var_loc_p;
21721 if (!NOTE_P (loc_note))
21723 if (CALL_P (loc_note))
21725 call_site_count++;
21726 if (SIBLING_CALL_P (loc_note))
21727 tail_call_site_count++;
21729 return;
21732 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21733 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21734 return;
21736 /* Optimize processing a large consecutive sequence of location
21737 notes so we don't spend too much time in next_real_insn. If the
21738 next insn is another location note, remember the next_real_insn
21739 calculation for next time. */
21740 next_real = cached_next_real_insn;
21741 if (next_real)
21743 if (expected_next_loc_note != loc_note)
21744 next_real = NULL;
21747 next_note = NEXT_INSN (loc_note);
21748 if (! next_note
21749 || next_note->deleted ()
21750 || ! NOTE_P (next_note)
21751 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21752 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21753 next_note = NULL;
21755 if (! next_real)
21756 next_real = next_real_insn (loc_note);
21758 if (next_note)
21760 expected_next_loc_note = next_note;
21761 cached_next_real_insn = next_real;
21763 else
21764 cached_next_real_insn = NULL;
21766 /* If there are no instructions which would be affected by this note,
21767 don't do anything. */
21768 if (var_loc_p
21769 && next_real == NULL_RTX
21770 && !NOTE_DURING_CALL_P (loc_note))
21771 return;
21773 if (next_real == NULL_RTX)
21774 next_real = get_last_insn ();
21776 /* If there were any real insns between note we processed last time
21777 and this note (or if it is the first note), clear
21778 last_{,postcall_}label so that they are not reused this time. */
21779 if (last_var_location_insn == NULL_RTX
21780 || last_var_location_insn != next_real
21781 || last_in_cold_section_p != in_cold_section_p)
21783 last_label = NULL;
21784 last_postcall_label = NULL;
21787 if (var_loc_p)
21789 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21790 newloc = add_var_loc_to_decl (decl, loc_note,
21791 NOTE_DURING_CALL_P (loc_note)
21792 ? last_postcall_label : last_label);
21793 if (newloc == NULL)
21794 return;
21796 else
21798 decl = NULL_TREE;
21799 newloc = NULL;
21802 /* If there were no real insns between note we processed last time
21803 and this note, use the label we emitted last time. Otherwise
21804 create a new label and emit it. */
21805 if (last_label == NULL)
21807 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21808 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21809 loclabel_num++;
21810 last_label = ggc_strdup (loclabel);
21811 /* See if loclabel might be equal to .Ltext0. If yes,
21812 bump first_loclabel_num_not_at_text_label. */
21813 if (!have_multiple_function_sections
21814 && in_first_function_p
21815 && maybe_at_text_label_p)
21817 static rtx_insn *last_start;
21818 rtx_insn *insn;
21819 for (insn = loc_note; insn; insn = previous_insn (insn))
21820 if (insn == last_start)
21821 break;
21822 else if (!NONDEBUG_INSN_P (insn))
21823 continue;
21824 else
21826 rtx body = PATTERN (insn);
21827 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21828 continue;
21829 /* Inline asm could occupy zero bytes. */
21830 else if (GET_CODE (body) == ASM_INPUT
21831 || asm_noperands (body) >= 0)
21832 continue;
21833 #ifdef HAVE_attr_length
21834 else if (get_attr_min_length (insn) == 0)
21835 continue;
21836 #endif
21837 else
21839 /* Assume insn has non-zero length. */
21840 maybe_at_text_label_p = false;
21841 break;
21844 if (maybe_at_text_label_p)
21846 last_start = loc_note;
21847 first_loclabel_num_not_at_text_label = loclabel_num;
21852 if (!var_loc_p)
21854 struct call_arg_loc_node *ca_loc
21855 = ggc_cleared_alloc<call_arg_loc_node> ();
21856 rtx_insn *prev = prev_real_insn (loc_note);
21857 rtx x;
21858 ca_loc->call_arg_loc_note = loc_note;
21859 ca_loc->next = NULL;
21860 ca_loc->label = last_label;
21861 gcc_assert (prev
21862 && (CALL_P (prev)
21863 || (NONJUMP_INSN_P (prev)
21864 && GET_CODE (PATTERN (prev)) == SEQUENCE
21865 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21866 if (!CALL_P (prev))
21867 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21868 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21869 x = get_call_rtx_from (PATTERN (prev));
21870 if (x)
21872 x = XEXP (XEXP (x, 0), 0);
21873 if (GET_CODE (x) == SYMBOL_REF
21874 && SYMBOL_REF_DECL (x)
21875 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21876 ca_loc->symbol_ref = x;
21878 ca_loc->block = insn_scope (prev);
21879 if (call_arg_locations)
21880 call_arg_loc_last->next = ca_loc;
21881 else
21882 call_arg_locations = ca_loc;
21883 call_arg_loc_last = ca_loc;
21885 else if (!NOTE_DURING_CALL_P (loc_note))
21886 newloc->label = last_label;
21887 else
21889 if (!last_postcall_label)
21891 sprintf (loclabel, "%s-1", last_label);
21892 last_postcall_label = ggc_strdup (loclabel);
21894 newloc->label = last_postcall_label;
21897 last_var_location_insn = next_real;
21898 last_in_cold_section_p = in_cold_section_p;
21901 /* Note in one location list that text section has changed. */
21904 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21906 var_loc_list *list = *slot;
21907 if (list->first)
21908 list->last_before_switch
21909 = list->last->next ? list->last->next : list->last;
21910 return 1;
21913 /* Note in all location lists that text section has changed. */
21915 static void
21916 var_location_switch_text_section (void)
21918 if (decl_loc_table == NULL)
21919 return;
21921 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21924 /* Create a new line number table. */
21926 static dw_line_info_table *
21927 new_line_info_table (void)
21929 dw_line_info_table *table;
21931 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21932 table->file_num = 1;
21933 table->line_num = 1;
21934 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21936 return table;
21939 /* Lookup the "current" table into which we emit line info, so
21940 that we don't have to do it for every source line. */
21942 static void
21943 set_cur_line_info_table (section *sec)
21945 dw_line_info_table *table;
21947 if (sec == text_section)
21948 table = text_section_line_info;
21949 else if (sec == cold_text_section)
21951 table = cold_text_section_line_info;
21952 if (!table)
21954 cold_text_section_line_info = table = new_line_info_table ();
21955 table->end_label = cold_end_label;
21958 else
21960 const char *end_label;
21962 if (flag_reorder_blocks_and_partition)
21964 if (in_cold_section_p)
21965 end_label = crtl->subsections.cold_section_end_label;
21966 else
21967 end_label = crtl->subsections.hot_section_end_label;
21969 else
21971 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21972 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21973 current_function_funcdef_no);
21974 end_label = ggc_strdup (label);
21977 table = new_line_info_table ();
21978 table->end_label = end_label;
21980 vec_safe_push (separate_line_info, table);
21983 if (DWARF2_ASM_LINE_DEBUG_INFO)
21984 table->is_stmt = (cur_line_info_table
21985 ? cur_line_info_table->is_stmt
21986 : DWARF_LINE_DEFAULT_IS_STMT_START);
21987 cur_line_info_table = table;
21991 /* We need to reset the locations at the beginning of each
21992 function. We can't do this in the end_function hook, because the
21993 declarations that use the locations won't have been output when
21994 that hook is called. Also compute have_multiple_function_sections here. */
21996 static void
21997 dwarf2out_begin_function (tree fun)
21999 section *sec = function_section (fun);
22001 if (sec != text_section)
22002 have_multiple_function_sections = true;
22004 if (flag_reorder_blocks_and_partition && !cold_text_section)
22006 gcc_assert (current_function_decl == fun);
22007 cold_text_section = unlikely_text_section ();
22008 switch_to_section (cold_text_section);
22009 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22010 switch_to_section (sec);
22013 dwarf2out_note_section_used ();
22014 call_site_count = 0;
22015 tail_call_site_count = 0;
22017 set_cur_line_info_table (sec);
22020 /* Helper function of dwarf2out_end_function, called only after emitting
22021 the very first function into assembly. Check if some .debug_loc range
22022 might end with a .LVL* label that could be equal to .Ltext0.
22023 In that case we must force using absolute addresses in .debug_loc ranges,
22024 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22025 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22026 list terminator.
22027 Set have_multiple_function_sections to true in that case and
22028 terminate htab traversal. */
22031 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22033 var_loc_list *entry = *slot;
22034 struct var_loc_node *node;
22036 node = entry->first;
22037 if (node && node->next && node->next->label)
22039 unsigned int i;
22040 const char *label = node->next->label;
22041 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22043 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22045 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22046 if (strcmp (label, loclabel) == 0)
22048 have_multiple_function_sections = true;
22049 return 0;
22053 return 1;
22056 /* Hook called after emitting a function into assembly.
22057 This does something only for the very first function emitted. */
22059 static void
22060 dwarf2out_end_function (unsigned int)
22062 if (in_first_function_p
22063 && !have_multiple_function_sections
22064 && first_loclabel_num_not_at_text_label
22065 && decl_loc_table)
22066 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22067 in_first_function_p = false;
22068 maybe_at_text_label_p = false;
22071 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22073 static void
22074 push_dw_line_info_entry (dw_line_info_table *table,
22075 enum dw_line_info_opcode opcode, unsigned int val)
22077 dw_line_info_entry e;
22078 e.opcode = opcode;
22079 e.val = val;
22080 vec_safe_push (table->entries, e);
22083 /* Output a label to mark the beginning of a source code line entry
22084 and record information relating to this source line, in
22085 'line_info_table' for later output of the .debug_line section. */
22086 /* ??? The discriminator parameter ought to be unsigned. */
22088 static void
22089 dwarf2out_source_line (unsigned int line, const char *filename,
22090 int discriminator, bool is_stmt)
22092 unsigned int file_num;
22093 dw_line_info_table *table;
22095 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22096 return;
22098 /* The discriminator column was added in dwarf4. Simplify the below
22099 by simply removing it if we're not supposed to output it. */
22100 if (dwarf_version < 4 && dwarf_strict)
22101 discriminator = 0;
22103 table = cur_line_info_table;
22104 file_num = maybe_emit_file (lookup_filename (filename));
22106 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22107 the debugger has used the second (possibly duplicate) line number
22108 at the beginning of the function to mark the end of the prologue.
22109 We could eliminate any other duplicates within the function. For
22110 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22111 that second line number entry. */
22112 /* Recall that this end-of-prologue indication is *not* the same thing
22113 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22114 to which the hook corresponds, follows the last insn that was
22115 emitted by gen_prologue. What we need is to precede the first insn
22116 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22117 insn that corresponds to something the user wrote. These may be
22118 very different locations once scheduling is enabled. */
22120 if (0 && file_num == table->file_num
22121 && line == table->line_num
22122 && discriminator == table->discrim_num
22123 && is_stmt == table->is_stmt)
22124 return;
22126 switch_to_section (current_function_section ());
22128 /* If requested, emit something human-readable. */
22129 if (flag_debug_asm)
22130 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22132 if (DWARF2_ASM_LINE_DEBUG_INFO)
22134 /* Emit the .loc directive understood by GNU as. */
22135 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22136 file_num, line, is_stmt, discriminator */
22137 fputs ("\t.loc ", asm_out_file);
22138 fprint_ul (asm_out_file, file_num);
22139 putc (' ', asm_out_file);
22140 fprint_ul (asm_out_file, line);
22141 putc (' ', asm_out_file);
22142 putc ('0', asm_out_file);
22144 if (is_stmt != table->is_stmt)
22146 fputs (" is_stmt ", asm_out_file);
22147 putc (is_stmt ? '1' : '0', asm_out_file);
22149 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22151 gcc_assert (discriminator > 0);
22152 fputs (" discriminator ", asm_out_file);
22153 fprint_ul (asm_out_file, (unsigned long) discriminator);
22155 putc ('\n', asm_out_file);
22157 else
22159 unsigned int label_num = ++line_info_label_num;
22161 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22163 push_dw_line_info_entry (table, LI_set_address, label_num);
22164 if (file_num != table->file_num)
22165 push_dw_line_info_entry (table, LI_set_file, file_num);
22166 if (discriminator != table->discrim_num)
22167 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22168 if (is_stmt != table->is_stmt)
22169 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22170 push_dw_line_info_entry (table, LI_set_line, line);
22173 table->file_num = file_num;
22174 table->line_num = line;
22175 table->discrim_num = discriminator;
22176 table->is_stmt = is_stmt;
22177 table->in_use = true;
22180 /* Record the beginning of a new source file. */
22182 static void
22183 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22185 if (flag_eliminate_dwarf2_dups)
22187 /* Record the beginning of the file for break_out_includes. */
22188 dw_die_ref bincl_die;
22190 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22191 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22194 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22196 macinfo_entry e;
22197 e.code = DW_MACINFO_start_file;
22198 e.lineno = lineno;
22199 e.info = ggc_strdup (filename);
22200 vec_safe_push (macinfo_table, e);
22204 /* Record the end of a source file. */
22206 static void
22207 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22209 if (flag_eliminate_dwarf2_dups)
22210 /* Record the end of the file for break_out_includes. */
22211 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22213 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22215 macinfo_entry e;
22216 e.code = DW_MACINFO_end_file;
22217 e.lineno = lineno;
22218 e.info = NULL;
22219 vec_safe_push (macinfo_table, e);
22223 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22224 the tail part of the directive line, i.e. the part which is past the
22225 initial whitespace, #, whitespace, directive-name, whitespace part. */
22227 static void
22228 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22229 const char *buffer ATTRIBUTE_UNUSED)
22231 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22233 macinfo_entry e;
22234 /* Insert a dummy first entry to be able to optimize the whole
22235 predefined macro block using DW_MACRO_GNU_transparent_include. */
22236 if (macinfo_table->is_empty () && lineno <= 1)
22238 e.code = 0;
22239 e.lineno = 0;
22240 e.info = NULL;
22241 vec_safe_push (macinfo_table, e);
22243 e.code = DW_MACINFO_define;
22244 e.lineno = lineno;
22245 e.info = ggc_strdup (buffer);
22246 vec_safe_push (macinfo_table, e);
22250 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22251 the tail part of the directive line, i.e. the part which is past the
22252 initial whitespace, #, whitespace, directive-name, whitespace part. */
22254 static void
22255 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22256 const char *buffer ATTRIBUTE_UNUSED)
22258 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22260 macinfo_entry e;
22261 /* Insert a dummy first entry to be able to optimize the whole
22262 predefined macro block using DW_MACRO_GNU_transparent_include. */
22263 if (macinfo_table->is_empty () && lineno <= 1)
22265 e.code = 0;
22266 e.lineno = 0;
22267 e.info = NULL;
22268 vec_safe_push (macinfo_table, e);
22270 e.code = DW_MACINFO_undef;
22271 e.lineno = lineno;
22272 e.info = ggc_strdup (buffer);
22273 vec_safe_push (macinfo_table, e);
22277 /* Helpers to manipulate hash table of CUs. */
22279 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22281 typedef macinfo_entry value_type;
22282 typedef macinfo_entry compare_type;
22283 static inline hashval_t hash (const value_type *);
22284 static inline bool equal (const value_type *, const compare_type *);
22287 inline hashval_t
22288 macinfo_entry_hasher::hash (const value_type *entry)
22290 return htab_hash_string (entry->info);
22293 inline bool
22294 macinfo_entry_hasher::equal (const value_type *entry1,
22295 const compare_type *entry2)
22297 return !strcmp (entry1->info, entry2->info);
22300 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22302 /* Output a single .debug_macinfo entry. */
22304 static void
22305 output_macinfo_op (macinfo_entry *ref)
22307 int file_num;
22308 size_t len;
22309 struct indirect_string_node *node;
22310 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22311 struct dwarf_file_data *fd;
22313 switch (ref->code)
22315 case DW_MACINFO_start_file:
22316 fd = lookup_filename (ref->info);
22317 file_num = maybe_emit_file (fd);
22318 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22319 dw2_asm_output_data_uleb128 (ref->lineno,
22320 "Included from line number %lu",
22321 (unsigned long) ref->lineno);
22322 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22323 break;
22324 case DW_MACINFO_end_file:
22325 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22326 break;
22327 case DW_MACINFO_define:
22328 case DW_MACINFO_undef:
22329 len = strlen (ref->info) + 1;
22330 if (!dwarf_strict
22331 && len > DWARF_OFFSET_SIZE
22332 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22333 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22335 ref->code = ref->code == DW_MACINFO_define
22336 ? DW_MACRO_GNU_define_indirect
22337 : DW_MACRO_GNU_undef_indirect;
22338 output_macinfo_op (ref);
22339 return;
22341 dw2_asm_output_data (1, ref->code,
22342 ref->code == DW_MACINFO_define
22343 ? "Define macro" : "Undefine macro");
22344 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22345 (unsigned long) ref->lineno);
22346 dw2_asm_output_nstring (ref->info, -1, "The macro");
22347 break;
22348 case DW_MACRO_GNU_define_indirect:
22349 case DW_MACRO_GNU_undef_indirect:
22350 node = find_AT_string (ref->info);
22351 gcc_assert (node
22352 && ((node->form == DW_FORM_strp)
22353 || (node->form == DW_FORM_GNU_str_index)));
22354 dw2_asm_output_data (1, ref->code,
22355 ref->code == DW_MACRO_GNU_define_indirect
22356 ? "Define macro indirect"
22357 : "Undefine macro indirect");
22358 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22359 (unsigned long) ref->lineno);
22360 if (node->form == DW_FORM_strp)
22361 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22362 debug_str_section, "The macro: \"%s\"",
22363 ref->info);
22364 else
22365 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22366 ref->info);
22367 break;
22368 case DW_MACRO_GNU_transparent_include:
22369 dw2_asm_output_data (1, ref->code, "Transparent include");
22370 ASM_GENERATE_INTERNAL_LABEL (label,
22371 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22372 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22373 break;
22374 default:
22375 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22376 ASM_COMMENT_START, (unsigned long) ref->code);
22377 break;
22381 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22382 other compilation unit .debug_macinfo sections. IDX is the first
22383 index of a define/undef, return the number of ops that should be
22384 emitted in a comdat .debug_macinfo section and emit
22385 a DW_MACRO_GNU_transparent_include entry referencing it.
22386 If the define/undef entry should be emitted normally, return 0. */
22388 static unsigned
22389 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22390 macinfo_hash_type **macinfo_htab)
22392 macinfo_entry *first, *second, *cur, *inc;
22393 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22394 unsigned char checksum[16];
22395 struct md5_ctx ctx;
22396 char *grp_name, *tail;
22397 const char *base;
22398 unsigned int i, count, encoded_filename_len, linebuf_len;
22399 macinfo_entry **slot;
22401 first = &(*macinfo_table)[idx];
22402 second = &(*macinfo_table)[idx + 1];
22404 /* Optimize only if there are at least two consecutive define/undef ops,
22405 and either all of them are before first DW_MACINFO_start_file
22406 with lineno {0,1} (i.e. predefined macro block), or all of them are
22407 in some included header file. */
22408 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22409 return 0;
22410 if (vec_safe_is_empty (files))
22412 if (first->lineno > 1 || second->lineno > 1)
22413 return 0;
22415 else if (first->lineno == 0)
22416 return 0;
22418 /* Find the last define/undef entry that can be grouped together
22419 with first and at the same time compute md5 checksum of their
22420 codes, linenumbers and strings. */
22421 md5_init_ctx (&ctx);
22422 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22423 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22424 break;
22425 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22426 break;
22427 else
22429 unsigned char code = cur->code;
22430 md5_process_bytes (&code, 1, &ctx);
22431 checksum_uleb128 (cur->lineno, &ctx);
22432 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22434 md5_finish_ctx (&ctx, checksum);
22435 count = i - idx;
22437 /* From the containing include filename (if any) pick up just
22438 usable characters from its basename. */
22439 if (vec_safe_is_empty (files))
22440 base = "";
22441 else
22442 base = lbasename (files->last ().info);
22443 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22444 if (ISIDNUM (base[i]) || base[i] == '.')
22445 encoded_filename_len++;
22446 /* Count . at the end. */
22447 if (encoded_filename_len)
22448 encoded_filename_len++;
22450 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22451 linebuf_len = strlen (linebuf);
22453 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22454 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22455 + 16 * 2 + 1);
22456 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22457 tail = grp_name + 4;
22458 if (encoded_filename_len)
22460 for (i = 0; base[i]; i++)
22461 if (ISIDNUM (base[i]) || base[i] == '.')
22462 *tail++ = base[i];
22463 *tail++ = '.';
22465 memcpy (tail, linebuf, linebuf_len);
22466 tail += linebuf_len;
22467 *tail++ = '.';
22468 for (i = 0; i < 16; i++)
22469 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22471 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22472 in the empty vector entry before the first define/undef. */
22473 inc = &(*macinfo_table)[idx - 1];
22474 inc->code = DW_MACRO_GNU_transparent_include;
22475 inc->lineno = 0;
22476 inc->info = ggc_strdup (grp_name);
22477 if (!*macinfo_htab)
22478 *macinfo_htab = new macinfo_hash_type (10);
22479 /* Avoid emitting duplicates. */
22480 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22481 if (*slot != NULL)
22483 inc->code = 0;
22484 inc->info = NULL;
22485 /* If such an entry has been used before, just emit
22486 a DW_MACRO_GNU_transparent_include op. */
22487 inc = *slot;
22488 output_macinfo_op (inc);
22489 /* And clear all macinfo_entry in the range to avoid emitting them
22490 in the second pass. */
22491 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22493 cur->code = 0;
22494 cur->info = NULL;
22497 else
22499 *slot = inc;
22500 inc->lineno = (*macinfo_htab)->elements ();
22501 output_macinfo_op (inc);
22503 return count;
22506 /* Save any strings needed by the macinfo table in the debug str
22507 table. All strings must be collected into the table by the time
22508 index_string is called. */
22510 static void
22511 save_macinfo_strings (void)
22513 unsigned len;
22514 unsigned i;
22515 macinfo_entry *ref;
22517 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22519 switch (ref->code)
22521 /* Match the logic in output_macinfo_op to decide on
22522 indirect strings. */
22523 case DW_MACINFO_define:
22524 case DW_MACINFO_undef:
22525 len = strlen (ref->info) + 1;
22526 if (!dwarf_strict
22527 && len > DWARF_OFFSET_SIZE
22528 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22529 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22530 set_indirect_string (find_AT_string (ref->info));
22531 break;
22532 case DW_MACRO_GNU_define_indirect:
22533 case DW_MACRO_GNU_undef_indirect:
22534 set_indirect_string (find_AT_string (ref->info));
22535 break;
22536 default:
22537 break;
22542 /* Output macinfo section(s). */
22544 static void
22545 output_macinfo (void)
22547 unsigned i;
22548 unsigned long length = vec_safe_length (macinfo_table);
22549 macinfo_entry *ref;
22550 vec<macinfo_entry, va_gc> *files = NULL;
22551 macinfo_hash_type *macinfo_htab = NULL;
22553 if (! length)
22554 return;
22556 /* output_macinfo* uses these interchangeably. */
22557 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22558 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22559 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22560 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22562 /* For .debug_macro emit the section header. */
22563 if (!dwarf_strict)
22565 dw2_asm_output_data (2, 4, "DWARF macro version number");
22566 if (DWARF_OFFSET_SIZE == 8)
22567 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22568 else
22569 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22570 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22571 (!dwarf_split_debug_info ? debug_line_section_label
22572 : debug_skeleton_line_section_label),
22573 debug_line_section, NULL);
22576 /* In the first loop, it emits the primary .debug_macinfo section
22577 and after each emitted op the macinfo_entry is cleared.
22578 If a longer range of define/undef ops can be optimized using
22579 DW_MACRO_GNU_transparent_include, the
22580 DW_MACRO_GNU_transparent_include op is emitted and kept in
22581 the vector before the first define/undef in the range and the
22582 whole range of define/undef ops is not emitted and kept. */
22583 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22585 switch (ref->code)
22587 case DW_MACINFO_start_file:
22588 vec_safe_push (files, *ref);
22589 break;
22590 case DW_MACINFO_end_file:
22591 if (!vec_safe_is_empty (files))
22592 files->pop ();
22593 break;
22594 case DW_MACINFO_define:
22595 case DW_MACINFO_undef:
22596 if (!dwarf_strict
22597 && HAVE_COMDAT_GROUP
22598 && vec_safe_length (files) != 1
22599 && i > 0
22600 && i + 1 < length
22601 && (*macinfo_table)[i - 1].code == 0)
22603 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22604 if (count)
22606 i += count - 1;
22607 continue;
22610 break;
22611 case 0:
22612 /* A dummy entry may be inserted at the beginning to be able
22613 to optimize the whole block of predefined macros. */
22614 if (i == 0)
22615 continue;
22616 default:
22617 break;
22619 output_macinfo_op (ref);
22620 ref->info = NULL;
22621 ref->code = 0;
22624 if (!macinfo_htab)
22625 return;
22627 delete macinfo_htab;
22628 macinfo_htab = NULL;
22630 /* If any DW_MACRO_GNU_transparent_include were used, on those
22631 DW_MACRO_GNU_transparent_include entries terminate the
22632 current chain and switch to a new comdat .debug_macinfo
22633 section and emit the define/undef entries within it. */
22634 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22635 switch (ref->code)
22637 case 0:
22638 continue;
22639 case DW_MACRO_GNU_transparent_include:
22641 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22642 tree comdat_key = get_identifier (ref->info);
22643 /* Terminate the previous .debug_macinfo section. */
22644 dw2_asm_output_data (1, 0, "End compilation unit");
22645 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22646 SECTION_DEBUG
22647 | SECTION_LINKONCE,
22648 comdat_key);
22649 ASM_GENERATE_INTERNAL_LABEL (label,
22650 DEBUG_MACRO_SECTION_LABEL,
22651 ref->lineno);
22652 ASM_OUTPUT_LABEL (asm_out_file, label);
22653 ref->code = 0;
22654 ref->info = NULL;
22655 dw2_asm_output_data (2, 4, "DWARF macro version number");
22656 if (DWARF_OFFSET_SIZE == 8)
22657 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22658 else
22659 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22661 break;
22662 case DW_MACINFO_define:
22663 case DW_MACINFO_undef:
22664 output_macinfo_op (ref);
22665 ref->code = 0;
22666 ref->info = NULL;
22667 break;
22668 default:
22669 gcc_unreachable ();
22673 /* Set up for Dwarf output at the start of compilation. */
22675 static void
22676 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22678 /* This option is currently broken, see (PR53118 and PR46102). */
22679 if (flag_eliminate_dwarf2_dups
22680 && strstr (lang_hooks.name, "C++"))
22682 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
22683 flag_eliminate_dwarf2_dups = 0;
22686 /* Allocate the file_table. */
22687 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22689 /* Allocate the decl_die_table. */
22690 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22692 /* Allocate the decl_loc_table. */
22693 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22695 /* Allocate the cached_dw_loc_list_table. */
22696 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22698 /* Allocate the initial hunk of the decl_scope_table. */
22699 vec_alloc (decl_scope_table, 256);
22701 /* Allocate the initial hunk of the abbrev_die_table. */
22702 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22703 (ABBREV_DIE_TABLE_INCREMENT);
22704 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22705 /* Zero-th entry is allocated, but unused. */
22706 abbrev_die_table_in_use = 1;
22708 /* Allocate the pubtypes and pubnames vectors. */
22709 vec_alloc (pubname_table, 32);
22710 vec_alloc (pubtype_table, 32);
22712 vec_alloc (incomplete_types, 64);
22714 vec_alloc (used_rtx_array, 32);
22716 if (!dwarf_split_debug_info)
22718 debug_info_section = get_section (DEBUG_INFO_SECTION,
22719 SECTION_DEBUG, NULL);
22720 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22721 SECTION_DEBUG, NULL);
22722 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22723 SECTION_DEBUG, NULL);
22725 else
22727 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22728 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22729 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22730 SECTION_DEBUG | SECTION_EXCLUDE,
22731 NULL);
22732 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22733 SECTION_DEBUG, NULL);
22734 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22735 SECTION_DEBUG, NULL);
22736 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22737 SECTION_DEBUG, NULL);
22738 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22739 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22741 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22742 the main .o, but the skeleton_line goes into the split off dwo. */
22743 debug_skeleton_line_section
22744 = get_section (DEBUG_DWO_LINE_SECTION,
22745 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22746 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22747 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22748 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22749 SECTION_DEBUG | SECTION_EXCLUDE,
22750 NULL);
22751 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22752 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22753 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22754 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22755 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22756 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22758 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22759 SECTION_DEBUG, NULL);
22760 debug_macinfo_section = get_section (dwarf_strict
22761 ? DEBUG_MACINFO_SECTION
22762 : DEBUG_MACRO_SECTION,
22763 DEBUG_MACRO_SECTION_FLAGS, NULL);
22764 debug_line_section = get_section (DEBUG_LINE_SECTION,
22765 SECTION_DEBUG, NULL);
22766 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22767 SECTION_DEBUG, NULL);
22768 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22769 SECTION_DEBUG, NULL);
22770 debug_str_section = get_section (DEBUG_STR_SECTION,
22771 DEBUG_STR_SECTION_FLAGS, NULL);
22772 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22773 SECTION_DEBUG, NULL);
22774 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22775 SECTION_DEBUG, NULL);
22777 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22778 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22779 DEBUG_ABBREV_SECTION_LABEL, 0);
22780 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22781 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22782 COLD_TEXT_SECTION_LABEL, 0);
22783 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22785 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22786 DEBUG_INFO_SECTION_LABEL, 0);
22787 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22788 DEBUG_LINE_SECTION_LABEL, 0);
22789 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22790 DEBUG_RANGES_SECTION_LABEL, 0);
22791 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22792 DEBUG_ADDR_SECTION_LABEL, 0);
22793 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22794 dwarf_strict
22795 ? DEBUG_MACINFO_SECTION_LABEL
22796 : DEBUG_MACRO_SECTION_LABEL, 0);
22797 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22799 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22800 vec_alloc (macinfo_table, 64);
22802 switch_to_section (text_section);
22803 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22805 /* Make sure the line number table for .text always exists. */
22806 text_section_line_info = new_line_info_table ();
22807 text_section_line_info->end_label = text_end_label;
22810 /* Called before compile () starts outputtting functions, variables
22811 and toplevel asms into assembly. */
22813 static void
22814 dwarf2out_assembly_start (void)
22816 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22817 && dwarf2out_do_cfi_asm ()
22818 && (!(flag_unwind_tables || flag_exceptions)
22819 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22820 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22823 /* A helper function for dwarf2out_finish called through
22824 htab_traverse. Assign a string its index. All strings must be
22825 collected into the table by the time index_string is called,
22826 because the indexing code relies on htab_traverse to traverse nodes
22827 in the same order for each run. */
22830 index_string (indirect_string_node **h, unsigned int *index)
22832 indirect_string_node *node = *h;
22834 find_string_form (node);
22835 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22837 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22838 node->index = *index;
22839 *index += 1;
22841 return 1;
22844 /* A helper function for output_indirect_strings called through
22845 htab_traverse. Output the offset to a string and update the
22846 current offset. */
22849 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22851 indirect_string_node *node = *h;
22853 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22855 /* Assert that this node has been assigned an index. */
22856 gcc_assert (node->index != NO_INDEX_ASSIGNED
22857 && node->index != NOT_INDEXED);
22858 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22859 "indexed string 0x%x: %s", node->index, node->str);
22860 *offset += strlen (node->str) + 1;
22862 return 1;
22865 /* A helper function for dwarf2out_finish called through
22866 htab_traverse. Output the indexed string. */
22869 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22871 struct indirect_string_node *node = *h;
22873 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22875 /* Assert that the strings are output in the same order as their
22876 indexes were assigned. */
22877 gcc_assert (*cur_idx == node->index);
22878 assemble_string (node->str, strlen (node->str) + 1);
22879 *cur_idx += 1;
22881 return 1;
22884 /* A helper function for dwarf2out_finish called through
22885 htab_traverse. Emit one queued .debug_str string. */
22888 output_indirect_string (indirect_string_node **h, void *)
22890 struct indirect_string_node *node = *h;
22892 node->form = find_string_form (node);
22893 if (node->form == DW_FORM_strp && node->refcount > 0)
22895 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22896 assemble_string (node->str, strlen (node->str) + 1);
22899 return 1;
22902 /* Output the indexed string table. */
22904 static void
22905 output_indirect_strings (void)
22907 switch_to_section (debug_str_section);
22908 if (!dwarf_split_debug_info)
22909 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22910 else
22912 unsigned int offset = 0;
22913 unsigned int cur_idx = 0;
22915 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22917 switch_to_section (debug_str_offsets_section);
22918 debug_str_hash->traverse_noresize
22919 <unsigned int *, output_index_string_offset> (&offset);
22920 switch_to_section (debug_str_dwo_section);
22921 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22922 (&cur_idx);
22926 /* Callback for htab_traverse to assign an index to an entry in the
22927 table, and to write that entry to the .debug_addr section. */
22930 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22932 addr_table_entry *entry = *slot;
22934 if (entry->refcount == 0)
22936 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22937 || entry->index == NOT_INDEXED);
22938 return 1;
22941 gcc_assert (entry->index == *cur_index);
22942 (*cur_index)++;
22944 switch (entry->kind)
22946 case ate_kind_rtx:
22947 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22948 "0x%x", entry->index);
22949 break;
22950 case ate_kind_rtx_dtprel:
22951 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22952 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22953 DWARF2_ADDR_SIZE,
22954 entry->addr.rtl);
22955 fputc ('\n', asm_out_file);
22956 break;
22957 case ate_kind_label:
22958 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22959 "0x%x", entry->index);
22960 break;
22961 default:
22962 gcc_unreachable ();
22964 return 1;
22967 /* Produce the .debug_addr section. */
22969 static void
22970 output_addr_table (void)
22972 unsigned int index = 0;
22973 if (addr_index_table == NULL || addr_index_table->size () == 0)
22974 return;
22976 switch_to_section (debug_addr_section);
22977 addr_index_table
22978 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22981 #if ENABLE_ASSERT_CHECKING
22982 /* Verify that all marks are clear. */
22984 static void
22985 verify_marks_clear (dw_die_ref die)
22987 dw_die_ref c;
22989 gcc_assert (! die->die_mark);
22990 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22992 #endif /* ENABLE_ASSERT_CHECKING */
22994 /* Clear the marks for a die and its children.
22995 Be cool if the mark isn't set. */
22997 static void
22998 prune_unmark_dies (dw_die_ref die)
23000 dw_die_ref c;
23002 if (die->die_mark)
23003 die->die_mark = 0;
23004 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23007 /* Given DIE that we're marking as used, find any other dies
23008 it references as attributes and mark them as used. */
23010 static void
23011 prune_unused_types_walk_attribs (dw_die_ref die)
23013 dw_attr_ref a;
23014 unsigned ix;
23016 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23018 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23020 /* A reference to another DIE.
23021 Make sure that it will get emitted.
23022 If it was broken out into a comdat group, don't follow it. */
23023 if (! AT_ref (a)->comdat_type_p
23024 || a->dw_attr == DW_AT_specification)
23025 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23027 /* Set the string's refcount to 0 so that prune_unused_types_mark
23028 accounts properly for it. */
23029 if (AT_class (a) == dw_val_class_str)
23030 a->dw_attr_val.v.val_str->refcount = 0;
23034 /* Mark the generic parameters and arguments children DIEs of DIE. */
23036 static void
23037 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23039 dw_die_ref c;
23041 if (die == NULL || die->die_child == NULL)
23042 return;
23043 c = die->die_child;
23046 if (is_template_parameter (c))
23047 prune_unused_types_mark (c, 1);
23048 c = c->die_sib;
23049 } while (c && c != die->die_child);
23052 /* Mark DIE as being used. If DOKIDS is true, then walk down
23053 to DIE's children. */
23055 static void
23056 prune_unused_types_mark (dw_die_ref die, int dokids)
23058 dw_die_ref c;
23060 if (die->die_mark == 0)
23062 /* We haven't done this node yet. Mark it as used. */
23063 die->die_mark = 1;
23064 /* If this is the DIE of a generic type instantiation,
23065 mark the children DIEs that describe its generic parms and
23066 args. */
23067 prune_unused_types_mark_generic_parms_dies (die);
23069 /* We also have to mark its parents as used.
23070 (But we don't want to mark our parent's kids due to this,
23071 unless it is a class.) */
23072 if (die->die_parent)
23073 prune_unused_types_mark (die->die_parent,
23074 class_scope_p (die->die_parent));
23076 /* Mark any referenced nodes. */
23077 prune_unused_types_walk_attribs (die);
23079 /* If this node is a specification,
23080 also mark the definition, if it exists. */
23081 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23082 prune_unused_types_mark (die->die_definition, 1);
23085 if (dokids && die->die_mark != 2)
23087 /* We need to walk the children, but haven't done so yet.
23088 Remember that we've walked the kids. */
23089 die->die_mark = 2;
23091 /* If this is an array type, we need to make sure our
23092 kids get marked, even if they're types. If we're
23093 breaking out types into comdat sections, do this
23094 for all type definitions. */
23095 if (die->die_tag == DW_TAG_array_type
23096 || (use_debug_types
23097 && is_type_die (die) && ! is_declaration_die (die)))
23098 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23099 else
23100 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23104 /* For local classes, look if any static member functions were emitted
23105 and if so, mark them. */
23107 static void
23108 prune_unused_types_walk_local_classes (dw_die_ref die)
23110 dw_die_ref c;
23112 if (die->die_mark == 2)
23113 return;
23115 switch (die->die_tag)
23117 case DW_TAG_structure_type:
23118 case DW_TAG_union_type:
23119 case DW_TAG_class_type:
23120 break;
23122 case DW_TAG_subprogram:
23123 if (!get_AT_flag (die, DW_AT_declaration)
23124 || die->die_definition != NULL)
23125 prune_unused_types_mark (die, 1);
23126 return;
23128 default:
23129 return;
23132 /* Mark children. */
23133 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23136 /* Walk the tree DIE and mark types that we actually use. */
23138 static void
23139 prune_unused_types_walk (dw_die_ref die)
23141 dw_die_ref c;
23143 /* Don't do anything if this node is already marked and
23144 children have been marked as well. */
23145 if (die->die_mark == 2)
23146 return;
23148 switch (die->die_tag)
23150 case DW_TAG_structure_type:
23151 case DW_TAG_union_type:
23152 case DW_TAG_class_type:
23153 if (die->die_perennial_p)
23154 break;
23156 for (c = die->die_parent; c; c = c->die_parent)
23157 if (c->die_tag == DW_TAG_subprogram)
23158 break;
23160 /* Finding used static member functions inside of classes
23161 is needed just for local classes, because for other classes
23162 static member function DIEs with DW_AT_specification
23163 are emitted outside of the DW_TAG_*_type. If we ever change
23164 it, we'd need to call this even for non-local classes. */
23165 if (c)
23166 prune_unused_types_walk_local_classes (die);
23168 /* It's a type node --- don't mark it. */
23169 return;
23171 case DW_TAG_const_type:
23172 case DW_TAG_packed_type:
23173 case DW_TAG_pointer_type:
23174 case DW_TAG_reference_type:
23175 case DW_TAG_rvalue_reference_type:
23176 case DW_TAG_volatile_type:
23177 case DW_TAG_typedef:
23178 case DW_TAG_array_type:
23179 case DW_TAG_interface_type:
23180 case DW_TAG_friend:
23181 case DW_TAG_variant_part:
23182 case DW_TAG_enumeration_type:
23183 case DW_TAG_subroutine_type:
23184 case DW_TAG_string_type:
23185 case DW_TAG_set_type:
23186 case DW_TAG_subrange_type:
23187 case DW_TAG_ptr_to_member_type:
23188 case DW_TAG_file_type:
23189 if (die->die_perennial_p)
23190 break;
23192 /* It's a type node --- don't mark it. */
23193 return;
23195 default:
23196 /* Mark everything else. */
23197 break;
23200 if (die->die_mark == 0)
23202 die->die_mark = 1;
23204 /* Now, mark any dies referenced from here. */
23205 prune_unused_types_walk_attribs (die);
23208 die->die_mark = 2;
23210 /* Mark children. */
23211 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23214 /* Increment the string counts on strings referred to from DIE's
23215 attributes. */
23217 static void
23218 prune_unused_types_update_strings (dw_die_ref die)
23220 dw_attr_ref a;
23221 unsigned ix;
23223 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23224 if (AT_class (a) == dw_val_class_str)
23226 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23227 s->refcount++;
23228 /* Avoid unnecessarily putting strings that are used less than
23229 twice in the hash table. */
23230 if (s->refcount
23231 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23233 indirect_string_node **slot
23234 = debug_str_hash->find_slot_with_hash (s->str,
23235 htab_hash_string (s->str),
23236 INSERT);
23237 gcc_assert (*slot == NULL);
23238 *slot = s;
23243 /* Remove from the tree DIE any dies that aren't marked. */
23245 static void
23246 prune_unused_types_prune (dw_die_ref die)
23248 dw_die_ref c;
23250 gcc_assert (die->die_mark);
23251 prune_unused_types_update_strings (die);
23253 if (! die->die_child)
23254 return;
23256 c = die->die_child;
23257 do {
23258 dw_die_ref prev = c;
23259 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23260 if (c == die->die_child)
23262 /* No marked children between 'prev' and the end of the list. */
23263 if (prev == c)
23264 /* No marked children at all. */
23265 die->die_child = NULL;
23266 else
23268 prev->die_sib = c->die_sib;
23269 die->die_child = prev;
23271 return;
23274 if (c != prev->die_sib)
23275 prev->die_sib = c;
23276 prune_unused_types_prune (c);
23277 } while (c != die->die_child);
23280 /* Remove dies representing declarations that we never use. */
23282 static void
23283 prune_unused_types (void)
23285 unsigned int i;
23286 limbo_die_node *node;
23287 comdat_type_node *ctnode;
23288 pubname_ref pub;
23289 dw_die_ref base_type;
23291 #if ENABLE_ASSERT_CHECKING
23292 /* All the marks should already be clear. */
23293 verify_marks_clear (comp_unit_die ());
23294 for (node = limbo_die_list; node; node = node->next)
23295 verify_marks_clear (node->die);
23296 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23297 verify_marks_clear (ctnode->root_die);
23298 #endif /* ENABLE_ASSERT_CHECKING */
23300 /* Mark types that are used in global variables. */
23301 premark_types_used_by_global_vars ();
23303 /* Set the mark on nodes that are actually used. */
23304 prune_unused_types_walk (comp_unit_die ());
23305 for (node = limbo_die_list; node; node = node->next)
23306 prune_unused_types_walk (node->die);
23307 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23309 prune_unused_types_walk (ctnode->root_die);
23310 prune_unused_types_mark (ctnode->type_die, 1);
23313 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23314 are unusual in that they are pubnames that are the children of pubtypes.
23315 They should only be marked via their parent DW_TAG_enumeration_type die,
23316 not as roots in themselves. */
23317 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23318 if (pub->die->die_tag != DW_TAG_enumerator)
23319 prune_unused_types_mark (pub->die, 1);
23320 for (i = 0; base_types.iterate (i, &base_type); i++)
23321 prune_unused_types_mark (base_type, 1);
23323 if (debug_str_hash)
23324 debug_str_hash->empty ();
23325 if (skeleton_debug_str_hash)
23326 skeleton_debug_str_hash->empty ();
23327 prune_unused_types_prune (comp_unit_die ());
23328 for (node = limbo_die_list; node; node = node->next)
23329 prune_unused_types_prune (node->die);
23330 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23331 prune_unused_types_prune (ctnode->root_die);
23333 /* Leave the marks clear. */
23334 prune_unmark_dies (comp_unit_die ());
23335 for (node = limbo_die_list; node; node = node->next)
23336 prune_unmark_dies (node->die);
23337 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23338 prune_unmark_dies (ctnode->root_die);
23341 /* Set the parameter to true if there are any relative pathnames in
23342 the file table. */
23344 file_table_relative_p (dwarf_file_data **slot, bool *p)
23346 struct dwarf_file_data *d = *slot;
23347 if (!IS_ABSOLUTE_PATH (d->filename))
23349 *p = true;
23350 return 0;
23352 return 1;
23355 /* Helpers to manipulate hash table of comdat type units. */
23357 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23359 typedef comdat_type_node value_type;
23360 typedef comdat_type_node compare_type;
23361 static inline hashval_t hash (const value_type *);
23362 static inline bool equal (const value_type *, const compare_type *);
23365 inline hashval_t
23366 comdat_type_hasher::hash (const value_type *type_node)
23368 hashval_t h;
23369 memcpy (&h, type_node->signature, sizeof (h));
23370 return h;
23373 inline bool
23374 comdat_type_hasher::equal (const value_type *type_node_1,
23375 const compare_type *type_node_2)
23377 return (! memcmp (type_node_1->signature, type_node_2->signature,
23378 DWARF_TYPE_SIGNATURE_SIZE));
23381 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23382 to the location it would have been added, should we know its
23383 DECL_ASSEMBLER_NAME when we added other attributes. This will
23384 probably improve compactness of debug info, removing equivalent
23385 abbrevs, and hide any differences caused by deferring the
23386 computation of the assembler name, triggered by e.g. PCH. */
23388 static inline void
23389 move_linkage_attr (dw_die_ref die)
23391 unsigned ix = vec_safe_length (die->die_attr);
23392 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23394 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23395 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23397 while (--ix > 0)
23399 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23401 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23402 break;
23405 if (ix != vec_safe_length (die->die_attr) - 1)
23407 die->die_attr->pop ();
23408 die->die_attr->quick_insert (ix, linkage);
23412 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23413 referenced from typed stack ops and count how often they are used. */
23415 static void
23416 mark_base_types (dw_loc_descr_ref loc)
23418 dw_die_ref base_type = NULL;
23420 for (; loc; loc = loc->dw_loc_next)
23422 switch (loc->dw_loc_opc)
23424 case DW_OP_GNU_regval_type:
23425 case DW_OP_GNU_deref_type:
23426 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23427 break;
23428 case DW_OP_GNU_convert:
23429 case DW_OP_GNU_reinterpret:
23430 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23431 continue;
23432 /* FALLTHRU */
23433 case DW_OP_GNU_const_type:
23434 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23435 break;
23436 case DW_OP_GNU_entry_value:
23437 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23438 continue;
23439 default:
23440 continue;
23442 gcc_assert (base_type->die_parent == comp_unit_die ());
23443 if (base_type->die_mark)
23444 base_type->die_mark++;
23445 else
23447 base_types.safe_push (base_type);
23448 base_type->die_mark = 1;
23453 /* Comparison function for sorting marked base types. */
23455 static int
23456 base_type_cmp (const void *x, const void *y)
23458 dw_die_ref dx = *(const dw_die_ref *) x;
23459 dw_die_ref dy = *(const dw_die_ref *) y;
23460 unsigned int byte_size1, byte_size2;
23461 unsigned int encoding1, encoding2;
23462 if (dx->die_mark > dy->die_mark)
23463 return -1;
23464 if (dx->die_mark < dy->die_mark)
23465 return 1;
23466 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23467 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23468 if (byte_size1 < byte_size2)
23469 return 1;
23470 if (byte_size1 > byte_size2)
23471 return -1;
23472 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23473 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23474 if (encoding1 < encoding2)
23475 return 1;
23476 if (encoding1 > encoding2)
23477 return -1;
23478 return 0;
23481 /* Move base types marked by mark_base_types as early as possible
23482 in the CU, sorted by decreasing usage count both to make the
23483 uleb128 references as small as possible and to make sure they
23484 will have die_offset already computed by calc_die_sizes when
23485 sizes of typed stack loc ops is computed. */
23487 static void
23488 move_marked_base_types (void)
23490 unsigned int i;
23491 dw_die_ref base_type, die, c;
23493 if (base_types.is_empty ())
23494 return;
23496 /* Sort by decreasing usage count, they will be added again in that
23497 order later on. */
23498 base_types.qsort (base_type_cmp);
23499 die = comp_unit_die ();
23500 c = die->die_child;
23503 dw_die_ref prev = c;
23504 c = c->die_sib;
23505 while (c->die_mark)
23507 remove_child_with_prev (c, prev);
23508 /* As base types got marked, there must be at least
23509 one node other than DW_TAG_base_type. */
23510 gcc_assert (c != c->die_sib);
23511 c = c->die_sib;
23514 while (c != die->die_child);
23515 gcc_assert (die->die_child);
23516 c = die->die_child;
23517 for (i = 0; base_types.iterate (i, &base_type); i++)
23519 base_type->die_mark = 0;
23520 base_type->die_sib = c->die_sib;
23521 c->die_sib = base_type;
23522 c = base_type;
23526 /* Helper function for resolve_addr, attempt to resolve
23527 one CONST_STRING, return true if successful. Similarly verify that
23528 SYMBOL_REFs refer to variables emitted in the current CU. */
23530 static bool
23531 resolve_one_addr (rtx *addr)
23533 rtx rtl = *addr;
23535 if (GET_CODE (rtl) == CONST_STRING)
23537 size_t len = strlen (XSTR (rtl, 0)) + 1;
23538 tree t = build_string (len, XSTR (rtl, 0));
23539 tree tlen = size_int (len - 1);
23540 TREE_TYPE (t)
23541 = build_array_type (char_type_node, build_index_type (tlen));
23542 rtl = lookup_constant_def (t);
23543 if (!rtl || !MEM_P (rtl))
23544 return false;
23545 rtl = XEXP (rtl, 0);
23546 if (GET_CODE (rtl) == SYMBOL_REF
23547 && SYMBOL_REF_DECL (rtl)
23548 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23549 return false;
23550 vec_safe_push (used_rtx_array, rtl);
23551 *addr = rtl;
23552 return true;
23555 if (GET_CODE (rtl) == SYMBOL_REF
23556 && SYMBOL_REF_DECL (rtl))
23558 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23560 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23561 return false;
23563 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23564 return false;
23567 if (GET_CODE (rtl) == CONST)
23569 subrtx_ptr_iterator::array_type array;
23570 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23571 if (!resolve_one_addr (*iter))
23572 return false;
23575 return true;
23578 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23579 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23580 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23582 static rtx
23583 string_cst_pool_decl (tree t)
23585 rtx rtl = output_constant_def (t, 1);
23586 unsigned char *array;
23587 dw_loc_descr_ref l;
23588 tree decl;
23589 size_t len;
23590 dw_die_ref ref;
23592 if (!rtl || !MEM_P (rtl))
23593 return NULL_RTX;
23594 rtl = XEXP (rtl, 0);
23595 if (GET_CODE (rtl) != SYMBOL_REF
23596 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23597 return NULL_RTX;
23599 decl = SYMBOL_REF_DECL (rtl);
23600 if (!lookup_decl_die (decl))
23602 len = TREE_STRING_LENGTH (t);
23603 vec_safe_push (used_rtx_array, rtl);
23604 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23605 array = ggc_vec_alloc<unsigned char> (len);
23606 memcpy (array, TREE_STRING_POINTER (t), len);
23607 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23608 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23609 l->dw_loc_oprnd2.v.val_vec.length = len;
23610 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23611 l->dw_loc_oprnd2.v.val_vec.array = array;
23612 add_AT_loc (ref, DW_AT_location, l);
23613 equate_decl_number_to_die (decl, ref);
23615 return rtl;
23618 /* Helper function of resolve_addr_in_expr. LOC is
23619 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23620 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23621 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23622 with DW_OP_GNU_implicit_pointer if possible
23623 and return true, if unsuccessful, return false. */
23625 static bool
23626 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23628 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23629 HOST_WIDE_INT offset = 0;
23630 dw_die_ref ref = NULL;
23631 tree decl;
23633 if (GET_CODE (rtl) == CONST
23634 && GET_CODE (XEXP (rtl, 0)) == PLUS
23635 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23637 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23638 rtl = XEXP (XEXP (rtl, 0), 0);
23640 if (GET_CODE (rtl) == CONST_STRING)
23642 size_t len = strlen (XSTR (rtl, 0)) + 1;
23643 tree t = build_string (len, XSTR (rtl, 0));
23644 tree tlen = size_int (len - 1);
23646 TREE_TYPE (t)
23647 = build_array_type (char_type_node, build_index_type (tlen));
23648 rtl = string_cst_pool_decl (t);
23649 if (!rtl)
23650 return false;
23652 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23654 decl = SYMBOL_REF_DECL (rtl);
23655 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23657 ref = lookup_decl_die (decl);
23658 if (ref && (get_AT (ref, DW_AT_location)
23659 || get_AT (ref, DW_AT_const_value)))
23661 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23662 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23663 loc->dw_loc_oprnd1.val_entry = NULL;
23664 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23665 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23666 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23667 loc->dw_loc_oprnd2.v.val_int = offset;
23668 return true;
23672 return false;
23675 /* Helper function for resolve_addr, handle one location
23676 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23677 the location list couldn't be resolved. */
23679 static bool
23680 resolve_addr_in_expr (dw_loc_descr_ref loc)
23682 dw_loc_descr_ref keep = NULL;
23683 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23684 switch (loc->dw_loc_opc)
23686 case DW_OP_addr:
23687 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23689 if ((prev == NULL
23690 || prev->dw_loc_opc == DW_OP_piece
23691 || prev->dw_loc_opc == DW_OP_bit_piece)
23692 && loc->dw_loc_next
23693 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23694 && !dwarf_strict
23695 && optimize_one_addr_into_implicit_ptr (loc))
23696 break;
23697 return false;
23699 break;
23700 case DW_OP_GNU_addr_index:
23701 case DW_OP_GNU_const_index:
23702 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23703 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23705 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23706 if (!resolve_one_addr (&rtl))
23707 return false;
23708 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23709 loc->dw_loc_oprnd1.val_entry =
23710 add_addr_table_entry (rtl, ate_kind_rtx);
23712 break;
23713 case DW_OP_const4u:
23714 case DW_OP_const8u:
23715 if (loc->dtprel
23716 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23717 return false;
23718 break;
23719 case DW_OP_plus_uconst:
23720 if (size_of_loc_descr (loc)
23721 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23723 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23725 dw_loc_descr_ref repl
23726 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23727 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23728 add_loc_descr (&repl, loc->dw_loc_next);
23729 *loc = *repl;
23731 break;
23732 case DW_OP_implicit_value:
23733 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23734 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23735 return false;
23736 break;
23737 case DW_OP_GNU_implicit_pointer:
23738 case DW_OP_GNU_parameter_ref:
23739 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23741 dw_die_ref ref
23742 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23743 if (ref == NULL)
23744 return false;
23745 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23746 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23747 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23749 break;
23750 case DW_OP_GNU_const_type:
23751 case DW_OP_GNU_regval_type:
23752 case DW_OP_GNU_deref_type:
23753 case DW_OP_GNU_convert:
23754 case DW_OP_GNU_reinterpret:
23755 while (loc->dw_loc_next
23756 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23758 dw_die_ref base1, base2;
23759 unsigned enc1, enc2, size1, size2;
23760 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23761 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23762 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23763 else if (loc->dw_loc_oprnd1.val_class
23764 == dw_val_class_unsigned_const)
23765 break;
23766 else
23767 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23768 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23769 == dw_val_class_unsigned_const)
23770 break;
23771 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23772 gcc_assert (base1->die_tag == DW_TAG_base_type
23773 && base2->die_tag == DW_TAG_base_type);
23774 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23775 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23776 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23777 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23778 if (size1 == size2
23779 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23780 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23781 && loc != keep)
23782 || enc1 == enc2))
23784 /* Optimize away next DW_OP_GNU_convert after
23785 adjusting LOC's base type die reference. */
23786 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23787 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23788 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23789 else
23790 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23791 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23792 continue;
23794 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23795 point typed stack entry. */
23796 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23797 keep = loc->dw_loc_next;
23798 break;
23800 break;
23801 default:
23802 break;
23804 return true;
23807 /* Helper function of resolve_addr. DIE had DW_AT_location of
23808 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23809 and DW_OP_addr couldn't be resolved. resolve_addr has already
23810 removed the DW_AT_location attribute. This function attempts to
23811 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23812 to it or DW_AT_const_value attribute, if possible. */
23814 static void
23815 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23817 if (TREE_CODE (decl) != VAR_DECL
23818 || lookup_decl_die (decl) != die
23819 || DECL_EXTERNAL (decl)
23820 || !TREE_STATIC (decl)
23821 || DECL_INITIAL (decl) == NULL_TREE
23822 || DECL_P (DECL_INITIAL (decl))
23823 || get_AT (die, DW_AT_const_value))
23824 return;
23826 tree init = DECL_INITIAL (decl);
23827 HOST_WIDE_INT offset = 0;
23828 /* For variables that have been optimized away and thus
23829 don't have a memory location, see if we can emit
23830 DW_AT_const_value instead. */
23831 if (tree_add_const_value_attribute (die, init))
23832 return;
23833 if (dwarf_strict)
23834 return;
23835 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23836 and ADDR_EXPR refers to a decl that has DW_AT_location or
23837 DW_AT_const_value (but isn't addressable, otherwise
23838 resolving the original DW_OP_addr wouldn't fail), see if
23839 we can add DW_OP_GNU_implicit_pointer. */
23840 STRIP_NOPS (init);
23841 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23842 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23844 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23845 init = TREE_OPERAND (init, 0);
23846 STRIP_NOPS (init);
23848 if (TREE_CODE (init) != ADDR_EXPR)
23849 return;
23850 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23851 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23852 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23853 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23854 && TREE_OPERAND (init, 0) != decl))
23856 dw_die_ref ref;
23857 dw_loc_descr_ref l;
23859 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23861 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23862 if (!rtl)
23863 return;
23864 decl = SYMBOL_REF_DECL (rtl);
23866 else
23867 decl = TREE_OPERAND (init, 0);
23868 ref = lookup_decl_die (decl);
23869 if (ref == NULL
23870 || (!get_AT (ref, DW_AT_location)
23871 && !get_AT (ref, DW_AT_const_value)))
23872 return;
23873 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23874 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23875 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23876 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23877 add_AT_loc (die, DW_AT_location, l);
23881 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23882 an address in .rodata section if the string literal is emitted there,
23883 or remove the containing location list or replace DW_AT_const_value
23884 with DW_AT_location and empty location expression, if it isn't found
23885 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23886 to something that has been emitted in the current CU. */
23888 static void
23889 resolve_addr (dw_die_ref die)
23891 dw_die_ref c;
23892 dw_attr_ref a;
23893 dw_loc_list_ref *curr, *start, loc;
23894 unsigned ix;
23896 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23897 switch (AT_class (a))
23899 case dw_val_class_loc_list:
23900 start = curr = AT_loc_list_ptr (a);
23901 loc = *curr;
23902 gcc_assert (loc);
23903 /* The same list can be referenced more than once. See if we have
23904 already recorded the result from a previous pass. */
23905 if (loc->replaced)
23906 *curr = loc->dw_loc_next;
23907 else if (!loc->resolved_addr)
23909 /* As things stand, we do not expect or allow one die to
23910 reference a suffix of another die's location list chain.
23911 References must be identical or completely separate.
23912 There is therefore no need to cache the result of this
23913 pass on any list other than the first; doing so
23914 would lead to unnecessary writes. */
23915 while (*curr)
23917 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23918 if (!resolve_addr_in_expr ((*curr)->expr))
23920 dw_loc_list_ref next = (*curr)->dw_loc_next;
23921 dw_loc_descr_ref l = (*curr)->expr;
23923 if (next && (*curr)->ll_symbol)
23925 gcc_assert (!next->ll_symbol);
23926 next->ll_symbol = (*curr)->ll_symbol;
23928 if (dwarf_split_debug_info)
23929 remove_loc_list_addr_table_entries (l);
23930 *curr = next;
23932 else
23934 mark_base_types ((*curr)->expr);
23935 curr = &(*curr)->dw_loc_next;
23938 if (loc == *start)
23939 loc->resolved_addr = 1;
23940 else
23942 loc->replaced = 1;
23943 loc->dw_loc_next = *start;
23946 if (!*start)
23948 remove_AT (die, a->dw_attr);
23949 ix--;
23951 break;
23952 case dw_val_class_loc:
23954 dw_loc_descr_ref l = AT_loc (a);
23955 /* For -gdwarf-2 don't attempt to optimize
23956 DW_AT_data_member_location containing
23957 DW_OP_plus_uconst - older consumers might
23958 rely on it being that op instead of a more complex,
23959 but shorter, location description. */
23960 if ((dwarf_version > 2
23961 || a->dw_attr != DW_AT_data_member_location
23962 || l == NULL
23963 || l->dw_loc_opc != DW_OP_plus_uconst
23964 || l->dw_loc_next != NULL)
23965 && !resolve_addr_in_expr (l))
23967 if (dwarf_split_debug_info)
23968 remove_loc_list_addr_table_entries (l);
23969 if (l != NULL
23970 && l->dw_loc_next == NULL
23971 && l->dw_loc_opc == DW_OP_addr
23972 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23973 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23974 && a->dw_attr == DW_AT_location)
23976 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23977 remove_AT (die, a->dw_attr);
23978 ix--;
23979 optimize_location_into_implicit_ptr (die, decl);
23980 break;
23982 remove_AT (die, a->dw_attr);
23983 ix--;
23985 else
23986 mark_base_types (l);
23988 break;
23989 case dw_val_class_addr:
23990 if (a->dw_attr == DW_AT_const_value
23991 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23993 if (AT_index (a) != NOT_INDEXED)
23994 remove_addr_table_entry (a->dw_attr_val.val_entry);
23995 remove_AT (die, a->dw_attr);
23996 ix--;
23998 if (die->die_tag == DW_TAG_GNU_call_site
23999 && a->dw_attr == DW_AT_abstract_origin)
24001 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24002 dw_die_ref tdie = lookup_decl_die (tdecl);
24003 if (tdie == NULL
24004 && DECL_EXTERNAL (tdecl)
24005 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
24007 force_decl_die (tdecl);
24008 tdie = lookup_decl_die (tdecl);
24010 if (tdie)
24012 a->dw_attr_val.val_class = dw_val_class_die_ref;
24013 a->dw_attr_val.v.val_die_ref.die = tdie;
24014 a->dw_attr_val.v.val_die_ref.external = 0;
24016 else
24018 if (AT_index (a) != NOT_INDEXED)
24019 remove_addr_table_entry (a->dw_attr_val.val_entry);
24020 remove_AT (die, a->dw_attr);
24021 ix--;
24024 break;
24025 default:
24026 break;
24029 FOR_EACH_CHILD (die, c, resolve_addr (c));
24032 /* Helper routines for optimize_location_lists.
24033 This pass tries to share identical local lists in .debug_loc
24034 section. */
24036 /* Iteratively hash operands of LOC opcode into HSTATE. */
24038 static void
24039 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24041 dw_val_ref val1 = &loc->dw_loc_oprnd1;
24042 dw_val_ref val2 = &loc->dw_loc_oprnd2;
24044 switch (loc->dw_loc_opc)
24046 case DW_OP_const4u:
24047 case DW_OP_const8u:
24048 if (loc->dtprel)
24049 goto hash_addr;
24050 /* FALLTHRU */
24051 case DW_OP_const1u:
24052 case DW_OP_const1s:
24053 case DW_OP_const2u:
24054 case DW_OP_const2s:
24055 case DW_OP_const4s:
24056 case DW_OP_const8s:
24057 case DW_OP_constu:
24058 case DW_OP_consts:
24059 case DW_OP_pick:
24060 case DW_OP_plus_uconst:
24061 case DW_OP_breg0:
24062 case DW_OP_breg1:
24063 case DW_OP_breg2:
24064 case DW_OP_breg3:
24065 case DW_OP_breg4:
24066 case DW_OP_breg5:
24067 case DW_OP_breg6:
24068 case DW_OP_breg7:
24069 case DW_OP_breg8:
24070 case DW_OP_breg9:
24071 case DW_OP_breg10:
24072 case DW_OP_breg11:
24073 case DW_OP_breg12:
24074 case DW_OP_breg13:
24075 case DW_OP_breg14:
24076 case DW_OP_breg15:
24077 case DW_OP_breg16:
24078 case DW_OP_breg17:
24079 case DW_OP_breg18:
24080 case DW_OP_breg19:
24081 case DW_OP_breg20:
24082 case DW_OP_breg21:
24083 case DW_OP_breg22:
24084 case DW_OP_breg23:
24085 case DW_OP_breg24:
24086 case DW_OP_breg25:
24087 case DW_OP_breg26:
24088 case DW_OP_breg27:
24089 case DW_OP_breg28:
24090 case DW_OP_breg29:
24091 case DW_OP_breg30:
24092 case DW_OP_breg31:
24093 case DW_OP_regx:
24094 case DW_OP_fbreg:
24095 case DW_OP_piece:
24096 case DW_OP_deref_size:
24097 case DW_OP_xderef_size:
24098 hstate.add_object (val1->v.val_int);
24099 break;
24100 case DW_OP_skip:
24101 case DW_OP_bra:
24103 int offset;
24105 gcc_assert (val1->val_class == dw_val_class_loc);
24106 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24107 hstate.add_object (offset);
24109 break;
24110 case DW_OP_implicit_value:
24111 hstate.add_object (val1->v.val_unsigned);
24112 switch (val2->val_class)
24114 case dw_val_class_const:
24115 hstate.add_object (val2->v.val_int);
24116 break;
24117 case dw_val_class_vec:
24119 unsigned int elt_size = val2->v.val_vec.elt_size;
24120 unsigned int len = val2->v.val_vec.length;
24122 hstate.add_int (elt_size);
24123 hstate.add_int (len);
24124 hstate.add (val2->v.val_vec.array, len * elt_size);
24126 break;
24127 case dw_val_class_const_double:
24128 hstate.add_object (val2->v.val_double.low);
24129 hstate.add_object (val2->v.val_double.high);
24130 break;
24131 case dw_val_class_wide_int:
24132 hstate.add (val2->v.val_wide->get_val (),
24133 get_full_len (*val2->v.val_wide)
24134 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24135 break;
24136 case dw_val_class_addr:
24137 inchash::add_rtx (val2->v.val_addr, hstate);
24138 break;
24139 default:
24140 gcc_unreachable ();
24142 break;
24143 case DW_OP_bregx:
24144 case DW_OP_bit_piece:
24145 hstate.add_object (val1->v.val_int);
24146 hstate.add_object (val2->v.val_int);
24147 break;
24148 case DW_OP_addr:
24149 hash_addr:
24150 if (loc->dtprel)
24152 unsigned char dtprel = 0xd1;
24153 hstate.add_object (dtprel);
24155 inchash::add_rtx (val1->v.val_addr, hstate);
24156 break;
24157 case DW_OP_GNU_addr_index:
24158 case DW_OP_GNU_const_index:
24160 if (loc->dtprel)
24162 unsigned char dtprel = 0xd1;
24163 hstate.add_object (dtprel);
24165 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24167 break;
24168 case DW_OP_GNU_implicit_pointer:
24169 hstate.add_int (val2->v.val_int);
24170 break;
24171 case DW_OP_GNU_entry_value:
24172 hstate.add_object (val1->v.val_loc);
24173 break;
24174 case DW_OP_GNU_regval_type:
24175 case DW_OP_GNU_deref_type:
24177 unsigned int byte_size
24178 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24179 unsigned int encoding
24180 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24181 hstate.add_object (val1->v.val_int);
24182 hstate.add_object (byte_size);
24183 hstate.add_object (encoding);
24185 break;
24186 case DW_OP_GNU_convert:
24187 case DW_OP_GNU_reinterpret:
24188 if (val1->val_class == dw_val_class_unsigned_const)
24190 hstate.add_object (val1->v.val_unsigned);
24191 break;
24193 /* FALLTHRU */
24194 case DW_OP_GNU_const_type:
24196 unsigned int byte_size
24197 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24198 unsigned int encoding
24199 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24200 hstate.add_object (byte_size);
24201 hstate.add_object (encoding);
24202 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24203 break;
24204 hstate.add_object (val2->val_class);
24205 switch (val2->val_class)
24207 case dw_val_class_const:
24208 hstate.add_object (val2->v.val_int);
24209 break;
24210 case dw_val_class_vec:
24212 unsigned int elt_size = val2->v.val_vec.elt_size;
24213 unsigned int len = val2->v.val_vec.length;
24215 hstate.add_object (elt_size);
24216 hstate.add_object (len);
24217 hstate.add (val2->v.val_vec.array, len * elt_size);
24219 break;
24220 case dw_val_class_const_double:
24221 hstate.add_object (val2->v.val_double.low);
24222 hstate.add_object (val2->v.val_double.high);
24223 break;
24224 case dw_val_class_wide_int:
24225 hstate.add (val2->v.val_wide->get_val (),
24226 get_full_len (*val2->v.val_wide)
24227 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24228 break;
24229 default:
24230 gcc_unreachable ();
24233 break;
24235 default:
24236 /* Other codes have no operands. */
24237 break;
24241 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24243 static inline void
24244 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24246 dw_loc_descr_ref l;
24247 bool sizes_computed = false;
24248 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24249 size_of_locs (loc);
24251 for (l = loc; l != NULL; l = l->dw_loc_next)
24253 enum dwarf_location_atom opc = l->dw_loc_opc;
24254 hstate.add_object (opc);
24255 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24257 size_of_locs (loc);
24258 sizes_computed = true;
24260 hash_loc_operands (l, hstate);
24264 /* Compute hash of the whole location list LIST_HEAD. */
24266 static inline void
24267 hash_loc_list (dw_loc_list_ref list_head)
24269 dw_loc_list_ref curr = list_head;
24270 inchash::hash hstate;
24272 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24274 hstate.add (curr->begin, strlen (curr->begin) + 1);
24275 hstate.add (curr->end, strlen (curr->end) + 1);
24276 if (curr->section)
24277 hstate.add (curr->section, strlen (curr->section) + 1);
24278 hash_locs (curr->expr, hstate);
24280 list_head->hash = hstate.end ();
24283 /* Return true if X and Y opcodes have the same operands. */
24285 static inline bool
24286 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24288 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24289 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24290 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24291 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24293 switch (x->dw_loc_opc)
24295 case DW_OP_const4u:
24296 case DW_OP_const8u:
24297 if (x->dtprel)
24298 goto hash_addr;
24299 /* FALLTHRU */
24300 case DW_OP_const1u:
24301 case DW_OP_const1s:
24302 case DW_OP_const2u:
24303 case DW_OP_const2s:
24304 case DW_OP_const4s:
24305 case DW_OP_const8s:
24306 case DW_OP_constu:
24307 case DW_OP_consts:
24308 case DW_OP_pick:
24309 case DW_OP_plus_uconst:
24310 case DW_OP_breg0:
24311 case DW_OP_breg1:
24312 case DW_OP_breg2:
24313 case DW_OP_breg3:
24314 case DW_OP_breg4:
24315 case DW_OP_breg5:
24316 case DW_OP_breg6:
24317 case DW_OP_breg7:
24318 case DW_OP_breg8:
24319 case DW_OP_breg9:
24320 case DW_OP_breg10:
24321 case DW_OP_breg11:
24322 case DW_OP_breg12:
24323 case DW_OP_breg13:
24324 case DW_OP_breg14:
24325 case DW_OP_breg15:
24326 case DW_OP_breg16:
24327 case DW_OP_breg17:
24328 case DW_OP_breg18:
24329 case DW_OP_breg19:
24330 case DW_OP_breg20:
24331 case DW_OP_breg21:
24332 case DW_OP_breg22:
24333 case DW_OP_breg23:
24334 case DW_OP_breg24:
24335 case DW_OP_breg25:
24336 case DW_OP_breg26:
24337 case DW_OP_breg27:
24338 case DW_OP_breg28:
24339 case DW_OP_breg29:
24340 case DW_OP_breg30:
24341 case DW_OP_breg31:
24342 case DW_OP_regx:
24343 case DW_OP_fbreg:
24344 case DW_OP_piece:
24345 case DW_OP_deref_size:
24346 case DW_OP_xderef_size:
24347 return valx1->v.val_int == valy1->v.val_int;
24348 case DW_OP_skip:
24349 case DW_OP_bra:
24350 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24351 can cause irrelevant differences in dw_loc_addr. */
24352 gcc_assert (valx1->val_class == dw_val_class_loc
24353 && valy1->val_class == dw_val_class_loc
24354 && (dwarf_split_debug_info
24355 || x->dw_loc_addr == y->dw_loc_addr));
24356 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24357 case DW_OP_implicit_value:
24358 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24359 || valx2->val_class != valy2->val_class)
24360 return false;
24361 switch (valx2->val_class)
24363 case dw_val_class_const:
24364 return valx2->v.val_int == valy2->v.val_int;
24365 case dw_val_class_vec:
24366 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24367 && valx2->v.val_vec.length == valy2->v.val_vec.length
24368 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24369 valx2->v.val_vec.elt_size
24370 * valx2->v.val_vec.length) == 0;
24371 case dw_val_class_const_double:
24372 return valx2->v.val_double.low == valy2->v.val_double.low
24373 && valx2->v.val_double.high == valy2->v.val_double.high;
24374 case dw_val_class_wide_int:
24375 return *valx2->v.val_wide == *valy2->v.val_wide;
24376 case dw_val_class_addr:
24377 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24378 default:
24379 gcc_unreachable ();
24381 case DW_OP_bregx:
24382 case DW_OP_bit_piece:
24383 return valx1->v.val_int == valy1->v.val_int
24384 && valx2->v.val_int == valy2->v.val_int;
24385 case DW_OP_addr:
24386 hash_addr:
24387 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24388 case DW_OP_GNU_addr_index:
24389 case DW_OP_GNU_const_index:
24391 rtx ax1 = valx1->val_entry->addr.rtl;
24392 rtx ay1 = valy1->val_entry->addr.rtl;
24393 return rtx_equal_p (ax1, ay1);
24395 case DW_OP_GNU_implicit_pointer:
24396 return valx1->val_class == dw_val_class_die_ref
24397 && valx1->val_class == valy1->val_class
24398 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24399 && valx2->v.val_int == valy2->v.val_int;
24400 case DW_OP_GNU_entry_value:
24401 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24402 case DW_OP_GNU_const_type:
24403 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24404 || valx2->val_class != valy2->val_class)
24405 return false;
24406 switch (valx2->val_class)
24408 case dw_val_class_const:
24409 return valx2->v.val_int == valy2->v.val_int;
24410 case dw_val_class_vec:
24411 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24412 && valx2->v.val_vec.length == valy2->v.val_vec.length
24413 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24414 valx2->v.val_vec.elt_size
24415 * valx2->v.val_vec.length) == 0;
24416 case dw_val_class_const_double:
24417 return valx2->v.val_double.low == valy2->v.val_double.low
24418 && valx2->v.val_double.high == valy2->v.val_double.high;
24419 case dw_val_class_wide_int:
24420 return *valx2->v.val_wide == *valy2->v.val_wide;
24421 default:
24422 gcc_unreachable ();
24424 case DW_OP_GNU_regval_type:
24425 case DW_OP_GNU_deref_type:
24426 return valx1->v.val_int == valy1->v.val_int
24427 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24428 case DW_OP_GNU_convert:
24429 case DW_OP_GNU_reinterpret:
24430 if (valx1->val_class != valy1->val_class)
24431 return false;
24432 if (valx1->val_class == dw_val_class_unsigned_const)
24433 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24434 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24435 case DW_OP_GNU_parameter_ref:
24436 return valx1->val_class == dw_val_class_die_ref
24437 && valx1->val_class == valy1->val_class
24438 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24439 default:
24440 /* Other codes have no operands. */
24441 return true;
24445 /* Return true if DWARF location expressions X and Y are the same. */
24447 static inline bool
24448 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24450 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24451 if (x->dw_loc_opc != y->dw_loc_opc
24452 || x->dtprel != y->dtprel
24453 || !compare_loc_operands (x, y))
24454 break;
24455 return x == NULL && y == NULL;
24458 /* Hashtable helpers. */
24460 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24462 typedef dw_loc_list_struct value_type;
24463 typedef dw_loc_list_struct compare_type;
24464 static inline hashval_t hash (const value_type *);
24465 static inline bool equal (const value_type *, const compare_type *);
24468 /* Return precomputed hash of location list X. */
24470 inline hashval_t
24471 loc_list_hasher::hash (const value_type *x)
24473 return x->hash;
24476 /* Return true if location lists A and B are the same. */
24478 inline bool
24479 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24481 if (a == b)
24482 return 1;
24483 if (a->hash != b->hash)
24484 return 0;
24485 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24486 if (strcmp (a->begin, b->begin) != 0
24487 || strcmp (a->end, b->end) != 0
24488 || (a->section == NULL) != (b->section == NULL)
24489 || (a->section && strcmp (a->section, b->section) != 0)
24490 || !compare_locs (a->expr, b->expr))
24491 break;
24492 return a == NULL && b == NULL;
24495 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24498 /* Recursively optimize location lists referenced from DIE
24499 children and share them whenever possible. */
24501 static void
24502 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24504 dw_die_ref c;
24505 dw_attr_ref a;
24506 unsigned ix;
24507 dw_loc_list_struct **slot;
24509 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24510 if (AT_class (a) == dw_val_class_loc_list)
24512 dw_loc_list_ref list = AT_loc_list (a);
24513 /* TODO: perform some optimizations here, before hashing
24514 it and storing into the hash table. */
24515 hash_loc_list (list);
24516 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24517 if (*slot == NULL)
24518 *slot = list;
24519 else
24520 a->dw_attr_val.v.val_loc_list = *slot;
24523 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24527 /* Recursively assign each location list a unique index into the debug_addr
24528 section. */
24530 static void
24531 index_location_lists (dw_die_ref die)
24533 dw_die_ref c;
24534 dw_attr_ref a;
24535 unsigned ix;
24537 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24538 if (AT_class (a) == dw_val_class_loc_list)
24540 dw_loc_list_ref list = AT_loc_list (a);
24541 dw_loc_list_ref curr;
24542 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24544 /* Don't index an entry that has already been indexed
24545 or won't be output. */
24546 if (curr->begin_entry != NULL
24547 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24548 continue;
24550 curr->begin_entry
24551 = add_addr_table_entry (xstrdup (curr->begin),
24552 ate_kind_label);
24556 FOR_EACH_CHILD (die, c, index_location_lists (c));
24559 /* Optimize location lists referenced from DIE
24560 children and share them whenever possible. */
24562 static void
24563 optimize_location_lists (dw_die_ref die)
24565 loc_list_hash_type htab (500);
24566 optimize_location_lists_1 (die, &htab);
24569 /* Output stuff that dwarf requires at the end of every file,
24570 and generate the DWARF-2 debugging info. */
24572 static void
24573 dwarf2out_finish (const char *filename)
24575 limbo_die_node *node, *next_node;
24576 comdat_type_node *ctnode;
24577 unsigned int i;
24578 dw_die_ref main_comp_unit_die;
24580 /* PCH might result in DW_AT_producer string being restored from the
24581 header compilation, so always fill it with empty string initially
24582 and overwrite only here. */
24583 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24584 producer_string = gen_producer_string ();
24585 producer->dw_attr_val.v.val_str->refcount--;
24586 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24588 gen_scheduled_generic_parms_dies ();
24589 gen_remaining_tmpl_value_param_die_attribute ();
24591 /* Add the name for the main input file now. We delayed this from
24592 dwarf2out_init to avoid complications with PCH.
24593 For LTO produced units use a fixed artificial name to avoid
24594 leaking tempfile names into the dwarf. */
24595 if (!in_lto_p)
24596 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24597 else
24598 add_name_attribute (comp_unit_die (), "<artificial>");
24599 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24600 add_comp_dir_attribute (comp_unit_die ());
24601 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24603 bool p = false;
24604 file_table->traverse<bool *, file_table_relative_p> (&p);
24605 if (p)
24606 add_comp_dir_attribute (comp_unit_die ());
24609 if (deferred_locations_list)
24610 for (i = 0; i < deferred_locations_list->length (); i++)
24612 add_location_or_const_value_attribute (
24613 (*deferred_locations_list)[i].die,
24614 (*deferred_locations_list)[i].variable,
24615 false,
24616 DW_AT_location);
24619 /* Traverse the limbo die list, and add parent/child links. The only
24620 dies without parents that should be here are concrete instances of
24621 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24622 For concrete instances, we can get the parent die from the abstract
24623 instance. */
24624 for (node = limbo_die_list; node; node = next_node)
24626 dw_die_ref die = node->die;
24627 next_node = node->next;
24629 if (die->die_parent == NULL)
24631 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24633 if (origin && origin->die_parent)
24634 add_child_die (origin->die_parent, die);
24635 else if (is_cu_die (die))
24637 else if (seen_error ())
24638 /* It's OK to be confused by errors in the input. */
24639 add_child_die (comp_unit_die (), die);
24640 else
24642 /* In certain situations, the lexical block containing a
24643 nested function can be optimized away, which results
24644 in the nested function die being orphaned. Likewise
24645 with the return type of that nested function. Force
24646 this to be a child of the containing function.
24648 It may happen that even the containing function got fully
24649 inlined and optimized out. In that case we are lost and
24650 assign the empty child. This should not be big issue as
24651 the function is likely unreachable too. */
24652 gcc_assert (node->created_for);
24654 if (DECL_P (node->created_for))
24655 origin = get_context_die (DECL_CONTEXT (node->created_for));
24656 else if (TYPE_P (node->created_for))
24657 origin = scope_die_for (node->created_for, comp_unit_die ());
24658 else
24659 origin = comp_unit_die ();
24661 add_child_die (origin, die);
24666 limbo_die_list = NULL;
24668 #if ENABLE_ASSERT_CHECKING
24670 dw_die_ref die = comp_unit_die (), c;
24671 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24673 #endif
24674 resolve_addr (comp_unit_die ());
24675 move_marked_base_types ();
24677 for (node = deferred_asm_name; node; node = node->next)
24679 tree decl = node->created_for;
24680 /* When generating LTO bytecode we can not generate new assembler
24681 names at this point and all important decls got theirs via
24682 free-lang-data. */
24683 if (((!flag_generate_lto && !flag_generate_offload)
24684 || DECL_ASSEMBLER_NAME_SET_P (decl))
24685 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24687 add_linkage_attr (node->die, decl);
24688 move_linkage_attr (node->die);
24692 deferred_asm_name = NULL;
24694 /* Walk through the list of incomplete types again, trying once more to
24695 emit full debugging info for them. */
24696 retry_incomplete_types ();
24698 if (flag_eliminate_unused_debug_types)
24699 prune_unused_types ();
24701 /* Generate separate COMDAT sections for type DIEs. */
24702 if (use_debug_types)
24704 break_out_comdat_types (comp_unit_die ());
24706 /* Each new type_unit DIE was added to the limbo die list when created.
24707 Since these have all been added to comdat_type_list, clear the
24708 limbo die list. */
24709 limbo_die_list = NULL;
24711 /* For each new comdat type unit, copy declarations for incomplete
24712 types to make the new unit self-contained (i.e., no direct
24713 references to the main compile unit). */
24714 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24715 copy_decls_for_unworthy_types (ctnode->root_die);
24716 copy_decls_for_unworthy_types (comp_unit_die ());
24718 /* In the process of copying declarations from one unit to another,
24719 we may have left some declarations behind that are no longer
24720 referenced. Prune them. */
24721 prune_unused_types ();
24724 /* Generate separate CUs for each of the include files we've seen.
24725 They will go into limbo_die_list. */
24726 if (flag_eliminate_dwarf2_dups)
24727 break_out_includes (comp_unit_die ());
24729 /* Traverse the DIE's and add add sibling attributes to those DIE's
24730 that have children. */
24731 add_sibling_attributes (comp_unit_die ());
24732 for (node = limbo_die_list; node; node = node->next)
24733 add_sibling_attributes (node->die);
24734 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24735 add_sibling_attributes (ctnode->root_die);
24737 /* When splitting DWARF info, we put some attributes in the
24738 skeleton compile_unit DIE that remains in the .o, while
24739 most attributes go in the DWO compile_unit_die. */
24740 if (dwarf_split_debug_info)
24741 main_comp_unit_die = gen_compile_unit_die (NULL);
24742 else
24743 main_comp_unit_die = comp_unit_die ();
24745 /* Output a terminator label for the .text section. */
24746 switch_to_section (text_section);
24747 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24748 if (cold_text_section)
24750 switch_to_section (cold_text_section);
24751 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24754 /* We can only use the low/high_pc attributes if all of the code was
24755 in .text. */
24756 if (!have_multiple_function_sections
24757 || (dwarf_version < 3 && dwarf_strict))
24759 /* Don't add if the CU has no associated code. */
24760 if (text_section_used)
24761 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24762 text_end_label, true);
24764 else
24766 unsigned fde_idx;
24767 dw_fde_ref fde;
24768 bool range_list_added = false;
24770 if (text_section_used)
24771 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24772 text_end_label, &range_list_added, true);
24773 if (cold_text_section_used)
24774 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24775 cold_end_label, &range_list_added, true);
24777 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24779 if (DECL_IGNORED_P (fde->decl))
24780 continue;
24781 if (!fde->in_std_section)
24782 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24783 fde->dw_fde_end, &range_list_added,
24784 true);
24785 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24786 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24787 fde->dw_fde_second_end, &range_list_added,
24788 true);
24791 if (range_list_added)
24793 /* We need to give .debug_loc and .debug_ranges an appropriate
24794 "base address". Use zero so that these addresses become
24795 absolute. Historically, we've emitted the unexpected
24796 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24797 Emit both to give time for other tools to adapt. */
24798 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24799 if (! dwarf_strict && dwarf_version < 4)
24800 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24802 add_ranges (NULL);
24806 if (debug_info_level >= DINFO_LEVEL_TERSE)
24807 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24808 debug_line_section_label);
24810 if (have_macinfo)
24811 add_AT_macptr (comp_unit_die (),
24812 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24813 macinfo_section_label);
24815 if (dwarf_split_debug_info)
24817 /* optimize_location_lists calculates the size of the lists,
24818 so index them first, and assign indices to the entries.
24819 Although optimize_location_lists will remove entries from
24820 the table, it only does so for duplicates, and therefore
24821 only reduces ref_counts to 1. */
24822 index_location_lists (comp_unit_die ());
24824 if (addr_index_table != NULL)
24826 unsigned int index = 0;
24827 addr_index_table
24828 ->traverse_noresize<unsigned int *, index_addr_table_entry>
24829 (&index);
24833 if (have_location_lists)
24834 optimize_location_lists (comp_unit_die ());
24836 save_macinfo_strings ();
24838 if (dwarf_split_debug_info)
24840 unsigned int index = 0;
24842 /* Add attributes common to skeleton compile_units and
24843 type_units. Because these attributes include strings, it
24844 must be done before freezing the string table. Top-level
24845 skeleton die attrs are added when the skeleton type unit is
24846 created, so ensure it is created by this point. */
24847 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24848 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24851 /* Output all of the compilation units. We put the main one last so that
24852 the offsets are available to output_pubnames. */
24853 for (node = limbo_die_list; node; node = node->next)
24854 output_comp_unit (node->die, 0);
24856 hash_table<comdat_type_hasher> comdat_type_table (100);
24857 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24859 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24861 /* Don't output duplicate types. */
24862 if (*slot != HTAB_EMPTY_ENTRY)
24863 continue;
24865 /* Add a pointer to the line table for the main compilation unit
24866 so that the debugger can make sense of DW_AT_decl_file
24867 attributes. */
24868 if (debug_info_level >= DINFO_LEVEL_TERSE)
24869 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24870 (!dwarf_split_debug_info
24871 ? debug_line_section_label
24872 : debug_skeleton_line_section_label));
24874 output_comdat_type_unit (ctnode);
24875 *slot = ctnode;
24878 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24879 both the main_cu and all skeleton TUs. Making this call unconditional
24880 would end up either adding a second copy of the AT_pubnames attribute, or
24881 requiring a special case in add_top_level_skeleton_die_attrs. */
24882 if (!dwarf_split_debug_info)
24883 add_AT_pubnames (comp_unit_die ());
24885 if (dwarf_split_debug_info)
24887 int mark;
24888 unsigned char checksum[16];
24889 struct md5_ctx ctx;
24891 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24892 md5_init_ctx (&ctx);
24893 mark = 0;
24894 die_checksum (comp_unit_die (), &ctx, &mark);
24895 unmark_all_dies (comp_unit_die ());
24896 md5_finish_ctx (&ctx, checksum);
24898 /* Use the first 8 bytes of the checksum as the dwo_id,
24899 and add it to both comp-unit DIEs. */
24900 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24901 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24903 /* Add the base offset of the ranges table to the skeleton
24904 comp-unit DIE. */
24905 if (ranges_table_in_use)
24906 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24907 ranges_section_label);
24909 switch_to_section (debug_addr_section);
24910 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24911 output_addr_table ();
24914 /* Output the main compilation unit if non-empty or if .debug_macinfo
24915 or .debug_macro will be emitted. */
24916 output_comp_unit (comp_unit_die (), have_macinfo);
24918 if (dwarf_split_debug_info && info_section_emitted)
24919 output_skeleton_debug_sections (main_comp_unit_die);
24921 /* Output the abbreviation table. */
24922 if (abbrev_die_table_in_use != 1)
24924 switch_to_section (debug_abbrev_section);
24925 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24926 output_abbrev_section ();
24929 /* Output location list section if necessary. */
24930 if (have_location_lists)
24932 /* Output the location lists info. */
24933 switch_to_section (debug_loc_section);
24934 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24935 output_location_lists (comp_unit_die ());
24938 output_pubtables ();
24940 /* Output the address range information if a CU (.debug_info section)
24941 was emitted. We output an empty table even if we had no functions
24942 to put in it. This because the consumer has no way to tell the
24943 difference between an empty table that we omitted and failure to
24944 generate a table that would have contained data. */
24945 if (info_section_emitted)
24947 unsigned long aranges_length = size_of_aranges ();
24949 switch_to_section (debug_aranges_section);
24950 output_aranges (aranges_length);
24953 /* Output ranges section if necessary. */
24954 if (ranges_table_in_use)
24956 switch_to_section (debug_ranges_section);
24957 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24958 output_ranges ();
24961 /* Have to end the macro section. */
24962 if (have_macinfo)
24964 switch_to_section (debug_macinfo_section);
24965 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24966 output_macinfo ();
24967 dw2_asm_output_data (1, 0, "End compilation unit");
24970 /* Output the source line correspondence table. We must do this
24971 even if there is no line information. Otherwise, on an empty
24972 translation unit, we will generate a present, but empty,
24973 .debug_info section. IRIX 6.5 `nm' will then complain when
24974 examining the file. This is done late so that any filenames
24975 used by the debug_info section are marked as 'used'. */
24976 switch_to_section (debug_line_section);
24977 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24978 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24979 output_line_info (false);
24981 if (dwarf_split_debug_info && info_section_emitted)
24983 switch_to_section (debug_skeleton_line_section);
24984 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24985 output_line_info (true);
24988 /* If we emitted any indirect strings, output the string table too. */
24989 if (debug_str_hash || skeleton_debug_str_hash)
24990 output_indirect_strings ();
24993 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24994 within the same process. For use by toplev::finalize. */
24996 void
24997 dwarf2out_c_finalize (void)
24999 last_var_location_insn = NULL;
25000 cached_next_real_insn = NULL;
25001 used_rtx_array = NULL;
25002 incomplete_types = NULL;
25003 decl_scope_table = NULL;
25004 debug_info_section = NULL;
25005 debug_skeleton_info_section = NULL;
25006 debug_abbrev_section = NULL;
25007 debug_skeleton_abbrev_section = NULL;
25008 debug_aranges_section = NULL;
25009 debug_addr_section = NULL;
25010 debug_macinfo_section = NULL;
25011 debug_line_section = NULL;
25012 debug_skeleton_line_section = NULL;
25013 debug_loc_section = NULL;
25014 debug_pubnames_section = NULL;
25015 debug_pubtypes_section = NULL;
25016 debug_str_section = NULL;
25017 debug_str_dwo_section = NULL;
25018 debug_str_offsets_section = NULL;
25019 debug_ranges_section = NULL;
25020 debug_frame_section = NULL;
25021 fde_vec = NULL;
25022 debug_str_hash = NULL;
25023 skeleton_debug_str_hash = NULL;
25024 dw2_string_counter = 0;
25025 have_multiple_function_sections = false;
25026 text_section_used = false;
25027 cold_text_section_used = false;
25028 cold_text_section = NULL;
25029 current_unit_personality = NULL;
25031 deferred_locations_list = NULL;
25033 next_die_offset = 0;
25034 single_comp_unit_die = NULL;
25035 comdat_type_list = NULL;
25036 limbo_die_list = NULL;
25037 deferred_asm_name = NULL;
25038 file_table = NULL;
25039 decl_die_table = NULL;
25040 common_block_die_table = NULL;
25041 decl_loc_table = NULL;
25042 call_arg_locations = NULL;
25043 call_arg_loc_last = NULL;
25044 call_site_count = -1;
25045 tail_call_site_count = -1;
25046 //block_map = NULL;
25047 cached_dw_loc_list_table = NULL;
25048 abbrev_die_table = NULL;
25049 abbrev_die_table_allocated = 0;
25050 abbrev_die_table_in_use = 0;
25051 line_info_label_num = 0;
25052 cur_line_info_table = NULL;
25053 text_section_line_info = NULL;
25054 cold_text_section_line_info = NULL;
25055 separate_line_info = NULL;
25056 info_section_emitted = false;
25057 pubname_table = NULL;
25058 pubtype_table = NULL;
25059 macinfo_table = NULL;
25060 ranges_table = NULL;
25061 ranges_table_allocated = 0;
25062 ranges_table_in_use = 0;
25063 ranges_by_label = 0;
25064 ranges_by_label_allocated = 0;
25065 ranges_by_label_in_use = 0;
25066 have_location_lists = false;
25067 loclabel_num = 0;
25068 poc_label_num = 0;
25069 last_emitted_file = NULL;
25070 label_num = 0;
25071 file_table_last_lookup = NULL;
25072 tmpl_value_parm_die_table = NULL;
25073 generic_type_instances = NULL;
25074 frame_pointer_fb_offset = 0;
25075 frame_pointer_fb_offset_valid = false;
25076 base_types.release ();
25077 XDELETEVEC (producer_string);
25078 producer_string = NULL;
25081 #include "gt-dwarf2out.h"