Merge trunk version 220104 into gupc branch.
[official-gcc.git] / gcc / dwarf2out.c
blobe9f975ded7d0bb7ed64c810496e06a8bafe6d61d
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_cleared_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);
4742 /* Return TRUE if the language is Ada. */
4744 static inline bool
4745 is_ada (void)
4747 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4749 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4752 /* Remove the specified attribute if present. */
4754 static void
4755 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4757 dw_attr_ref a;
4758 unsigned ix;
4760 if (! die)
4761 return;
4763 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4764 if (a->dw_attr == attr_kind)
4766 if (AT_class (a) == dw_val_class_str)
4767 if (a->dw_attr_val.v.val_str->refcount)
4768 a->dw_attr_val.v.val_str->refcount--;
4770 /* vec::ordered_remove should help reduce the number of abbrevs
4771 that are needed. */
4772 die->die_attr->ordered_remove (ix);
4773 return;
4777 /* Remove CHILD from its parent. PREV must have the property that
4778 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4780 static void
4781 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4783 gcc_assert (child->die_parent == prev->die_parent);
4784 gcc_assert (prev->die_sib == child);
4785 if (prev == child)
4787 gcc_assert (child->die_parent->die_child == child);
4788 prev = NULL;
4790 else
4791 prev->die_sib = child->die_sib;
4792 if (child->die_parent->die_child == child)
4793 child->die_parent->die_child = prev;
4796 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4797 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4799 static void
4800 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4802 dw_die_ref parent = old_child->die_parent;
4804 gcc_assert (parent == prev->die_parent);
4805 gcc_assert (prev->die_sib == old_child);
4807 new_child->die_parent = parent;
4808 if (prev == old_child)
4810 gcc_assert (parent->die_child == old_child);
4811 new_child->die_sib = new_child;
4813 else
4815 prev->die_sib = new_child;
4816 new_child->die_sib = old_child->die_sib;
4818 if (old_child->die_parent->die_child == old_child)
4819 old_child->die_parent->die_child = new_child;
4822 /* Move all children from OLD_PARENT to NEW_PARENT. */
4824 static void
4825 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4827 dw_die_ref c;
4828 new_parent->die_child = old_parent->die_child;
4829 old_parent->die_child = NULL;
4830 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4833 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4834 matches TAG. */
4836 static void
4837 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4839 dw_die_ref c;
4841 c = die->die_child;
4842 if (c) do {
4843 dw_die_ref prev = c;
4844 c = c->die_sib;
4845 while (c->die_tag == tag)
4847 remove_child_with_prev (c, prev);
4848 /* Might have removed every child. */
4849 if (c == c->die_sib)
4850 return;
4851 c = c->die_sib;
4853 } while (c != die->die_child);
4856 /* Add a CHILD_DIE as the last child of DIE. */
4858 static void
4859 add_child_die (dw_die_ref die, dw_die_ref child_die)
4861 /* FIXME this should probably be an assert. */
4862 if (! die || ! child_die)
4863 return;
4864 gcc_assert (die != child_die);
4866 child_die->die_parent = die;
4867 if (die->die_child)
4869 child_die->die_sib = die->die_child->die_sib;
4870 die->die_child->die_sib = child_die;
4872 else
4873 child_die->die_sib = child_die;
4874 die->die_child = child_die;
4877 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4878 is the specification, to the end of PARENT's list of children.
4879 This is done by removing and re-adding it. */
4881 static void
4882 splice_child_die (dw_die_ref parent, dw_die_ref child)
4884 dw_die_ref p;
4886 /* We want the declaration DIE from inside the class, not the
4887 specification DIE at toplevel. */
4888 if (child->die_parent != parent)
4890 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4892 if (tmp)
4893 child = tmp;
4896 gcc_assert (child->die_parent == parent
4897 || (child->die_parent
4898 == get_AT_ref (parent, DW_AT_specification)));
4900 for (p = child->die_parent->die_child; ; p = p->die_sib)
4901 if (p->die_sib == child)
4903 remove_child_with_prev (child, p);
4904 break;
4907 add_child_die (parent, child);
4910 /* Return a pointer to a newly created DIE node. */
4912 static inline dw_die_ref
4913 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4915 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4917 die->die_tag = tag_value;
4919 if (parent_die != NULL)
4920 add_child_die (parent_die, die);
4921 else
4923 limbo_die_node *limbo_node;
4925 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4926 limbo_node->die = die;
4927 limbo_node->created_for = t;
4928 limbo_node->next = limbo_die_list;
4929 limbo_die_list = limbo_node;
4932 return die;
4935 /* Return the DIE associated with the given type specifier. */
4937 static inline dw_die_ref
4938 lookup_type_die (tree type)
4940 return TYPE_SYMTAB_DIE (type);
4943 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4944 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4945 anonymous type instead the one of the naming typedef. */
4947 static inline dw_die_ref
4948 strip_naming_typedef (tree type, dw_die_ref type_die)
4950 if (type
4951 && TREE_CODE (type) == RECORD_TYPE
4952 && type_die
4953 && type_die->die_tag == DW_TAG_typedef
4954 && is_naming_typedef_decl (TYPE_NAME (type)))
4955 type_die = get_AT_ref (type_die, DW_AT_type);
4956 return type_die;
4959 /* Like lookup_type_die, but if type is an anonymous type named by a
4960 typedef[1], return the DIE of the anonymous type instead the one of
4961 the naming typedef. This is because in gen_typedef_die, we did
4962 equate the anonymous struct named by the typedef with the DIE of
4963 the naming typedef. So by default, lookup_type_die on an anonymous
4964 struct yields the DIE of the naming typedef.
4966 [1]: Read the comment of is_naming_typedef_decl to learn about what
4967 a naming typedef is. */
4969 static inline dw_die_ref
4970 lookup_type_die_strip_naming_typedef (tree type)
4972 dw_die_ref die = lookup_type_die (type);
4973 return strip_naming_typedef (type, die);
4976 /* Equate a DIE to a given type specifier. */
4978 static inline void
4979 equate_type_number_to_die (tree type, dw_die_ref type_die)
4981 TYPE_SYMTAB_DIE (type) = type_die;
4984 /* Returns a hash value for X (which really is a die_struct). */
4986 inline hashval_t
4987 decl_die_hasher::hash (die_node *x)
4989 return (hashval_t) x->decl_id;
4992 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4994 inline bool
4995 decl_die_hasher::equal (die_node *x, tree y)
4997 return (x->decl_id == DECL_UID (y));
5000 /* Return the DIE associated with a given declaration. */
5002 static inline dw_die_ref
5003 lookup_decl_die (tree decl)
5005 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5008 /* Returns a hash value for X (which really is a var_loc_list). */
5010 inline hashval_t
5011 decl_loc_hasher::hash (var_loc_list *x)
5013 return (hashval_t) x->decl_id;
5016 /* Return nonzero if decl_id of var_loc_list X is the same as
5017 UID of decl *Y. */
5019 inline bool
5020 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5022 return (x->decl_id == DECL_UID (y));
5025 /* Return the var_loc list associated with a given declaration. */
5027 static inline var_loc_list *
5028 lookup_decl_loc (const_tree decl)
5030 if (!decl_loc_table)
5031 return NULL;
5032 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5035 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5037 inline hashval_t
5038 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5040 return (hashval_t) x->decl_id;
5043 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5044 UID of decl *Y. */
5046 inline bool
5047 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5049 return (x->decl_id == DECL_UID (y));
5052 /* Equate a DIE to a particular declaration. */
5054 static void
5055 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5057 unsigned int decl_id = DECL_UID (decl);
5059 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5060 decl_die->decl_id = decl_id;
5063 /* Return how many bits covers PIECE EXPR_LIST. */
5065 static HOST_WIDE_INT
5066 decl_piece_bitsize (rtx piece)
5068 int ret = (int) GET_MODE (piece);
5069 if (ret)
5070 return ret;
5071 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5072 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5073 return INTVAL (XEXP (XEXP (piece, 0), 0));
5076 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5078 static rtx *
5079 decl_piece_varloc_ptr (rtx piece)
5081 if ((int) GET_MODE (piece))
5082 return &XEXP (piece, 0);
5083 else
5084 return &XEXP (XEXP (piece, 0), 1);
5087 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5088 Next is the chain of following piece nodes. */
5090 static rtx_expr_list *
5091 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5093 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5094 return alloc_EXPR_LIST (bitsize, loc_note, next);
5095 else
5096 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5097 GEN_INT (bitsize),
5098 loc_note), next);
5101 /* Return rtx that should be stored into loc field for
5102 LOC_NOTE and BITPOS/BITSIZE. */
5104 static rtx
5105 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5106 HOST_WIDE_INT bitsize)
5108 if (bitsize != -1)
5110 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5111 if (bitpos != 0)
5112 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5114 return loc_note;
5117 /* This function either modifies location piece list *DEST in
5118 place (if SRC and INNER is NULL), or copies location piece list
5119 *SRC to *DEST while modifying it. Location BITPOS is modified
5120 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5121 not copied and if needed some padding around it is added.
5122 When modifying in place, DEST should point to EXPR_LIST where
5123 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5124 to the start of the whole list and INNER points to the EXPR_LIST
5125 where earlier pieces cover PIECE_BITPOS bits. */
5127 static void
5128 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5129 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5130 HOST_WIDE_INT bitsize, rtx loc_note)
5132 HOST_WIDE_INT diff;
5133 bool copy = inner != NULL;
5135 if (copy)
5137 /* First copy all nodes preceding the current bitpos. */
5138 while (src != inner)
5140 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5141 decl_piece_bitsize (*src), NULL_RTX);
5142 dest = &XEXP (*dest, 1);
5143 src = &XEXP (*src, 1);
5146 /* Add padding if needed. */
5147 if (bitpos != piece_bitpos)
5149 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5150 copy ? NULL_RTX : *dest);
5151 dest = &XEXP (*dest, 1);
5153 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5155 gcc_assert (!copy);
5156 /* A piece with correct bitpos and bitsize already exist,
5157 just update the location for it and return. */
5158 *decl_piece_varloc_ptr (*dest) = loc_note;
5159 return;
5161 /* Add the piece that changed. */
5162 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5163 dest = &XEXP (*dest, 1);
5164 /* Skip over pieces that overlap it. */
5165 diff = bitpos - piece_bitpos + bitsize;
5166 if (!copy)
5167 src = dest;
5168 while (diff > 0 && *src)
5170 rtx piece = *src;
5171 diff -= decl_piece_bitsize (piece);
5172 if (copy)
5173 src = &XEXP (piece, 1);
5174 else
5176 *src = XEXP (piece, 1);
5177 free_EXPR_LIST_node (piece);
5180 /* Add padding if needed. */
5181 if (diff < 0 && *src)
5183 if (!copy)
5184 dest = src;
5185 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5186 dest = &XEXP (*dest, 1);
5188 if (!copy)
5189 return;
5190 /* Finally copy all nodes following it. */
5191 while (*src)
5193 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5194 decl_piece_bitsize (*src), NULL_RTX);
5195 dest = &XEXP (*dest, 1);
5196 src = &XEXP (*src, 1);
5200 /* Add a variable location node to the linked list for DECL. */
5202 static struct var_loc_node *
5203 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5205 unsigned int decl_id;
5206 var_loc_list *temp;
5207 struct var_loc_node *loc = NULL;
5208 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5210 if (TREE_CODE (decl) == VAR_DECL
5211 && DECL_HAS_DEBUG_EXPR_P (decl))
5213 tree realdecl = DECL_DEBUG_EXPR (decl);
5214 if (handled_component_p (realdecl)
5215 || (TREE_CODE (realdecl) == MEM_REF
5216 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5218 HOST_WIDE_INT maxsize;
5219 tree innerdecl;
5220 innerdecl
5221 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5222 if (!DECL_P (innerdecl)
5223 || DECL_IGNORED_P (innerdecl)
5224 || TREE_STATIC (innerdecl)
5225 || bitsize <= 0
5226 || bitpos + bitsize > 256
5227 || bitsize != maxsize)
5228 return NULL;
5229 decl = innerdecl;
5233 decl_id = DECL_UID (decl);
5234 var_loc_list **slot
5235 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5236 if (*slot == NULL)
5238 temp = ggc_cleared_alloc<var_loc_list> ();
5239 temp->decl_id = decl_id;
5240 *slot = temp;
5242 else
5243 temp = *slot;
5245 /* For PARM_DECLs try to keep around the original incoming value,
5246 even if that means we'll emit a zero-range .debug_loc entry. */
5247 if (temp->last
5248 && temp->first == temp->last
5249 && TREE_CODE (decl) == PARM_DECL
5250 && NOTE_P (temp->first->loc)
5251 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5252 && DECL_INCOMING_RTL (decl)
5253 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5254 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5255 == GET_CODE (DECL_INCOMING_RTL (decl))
5256 && prev_real_insn (temp->first->loc) == NULL_RTX
5257 && (bitsize != -1
5258 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5259 NOTE_VAR_LOCATION_LOC (loc_note))
5260 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5261 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5263 loc = ggc_cleared_alloc<var_loc_node> ();
5264 temp->first->next = loc;
5265 temp->last = loc;
5266 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5268 else if (temp->last)
5270 struct var_loc_node *last = temp->last, *unused = NULL;
5271 rtx *piece_loc = NULL, last_loc_note;
5272 HOST_WIDE_INT piece_bitpos = 0;
5273 if (last->next)
5275 last = last->next;
5276 gcc_assert (last->next == NULL);
5278 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5280 piece_loc = &last->loc;
5283 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5284 if (piece_bitpos + cur_bitsize > bitpos)
5285 break;
5286 piece_bitpos += cur_bitsize;
5287 piece_loc = &XEXP (*piece_loc, 1);
5289 while (*piece_loc);
5291 /* TEMP->LAST here is either pointer to the last but one or
5292 last element in the chained list, LAST is pointer to the
5293 last element. */
5294 if (label && strcmp (last->label, label) == 0)
5296 /* For SRA optimized variables if there weren't any real
5297 insns since last note, just modify the last node. */
5298 if (piece_loc != NULL)
5300 adjust_piece_list (piece_loc, NULL, NULL,
5301 bitpos, piece_bitpos, bitsize, loc_note);
5302 return NULL;
5304 /* If the last note doesn't cover any instructions, remove it. */
5305 if (temp->last != last)
5307 temp->last->next = NULL;
5308 unused = last;
5309 last = temp->last;
5310 gcc_assert (strcmp (last->label, label) != 0);
5312 else
5314 gcc_assert (temp->first == temp->last
5315 || (temp->first->next == temp->last
5316 && TREE_CODE (decl) == PARM_DECL));
5317 memset (temp->last, '\0', sizeof (*temp->last));
5318 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5319 return temp->last;
5322 if (bitsize == -1 && NOTE_P (last->loc))
5323 last_loc_note = last->loc;
5324 else if (piece_loc != NULL
5325 && *piece_loc != NULL_RTX
5326 && piece_bitpos == bitpos
5327 && decl_piece_bitsize (*piece_loc) == bitsize)
5328 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5329 else
5330 last_loc_note = NULL_RTX;
5331 /* If the current location is the same as the end of the list,
5332 and either both or neither of the locations is uninitialized,
5333 we have nothing to do. */
5334 if (last_loc_note == NULL_RTX
5335 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5336 NOTE_VAR_LOCATION_LOC (loc_note)))
5337 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5338 != NOTE_VAR_LOCATION_STATUS (loc_note))
5339 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5340 == VAR_INIT_STATUS_UNINITIALIZED)
5341 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5342 == VAR_INIT_STATUS_UNINITIALIZED))))
5344 /* Add LOC to the end of list and update LAST. If the last
5345 element of the list has been removed above, reuse its
5346 memory for the new node, otherwise allocate a new one. */
5347 if (unused)
5349 loc = unused;
5350 memset (loc, '\0', sizeof (*loc));
5352 else
5353 loc = ggc_cleared_alloc<var_loc_node> ();
5354 if (bitsize == -1 || piece_loc == NULL)
5355 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5356 else
5357 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5358 bitpos, piece_bitpos, bitsize, loc_note);
5359 last->next = loc;
5360 /* Ensure TEMP->LAST will point either to the new last but one
5361 element of the chain, or to the last element in it. */
5362 if (last != temp->last)
5363 temp->last = last;
5365 else if (unused)
5366 ggc_free (unused);
5368 else
5370 loc = ggc_cleared_alloc<var_loc_node> ();
5371 temp->first = loc;
5372 temp->last = loc;
5373 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5375 return loc;
5378 /* Keep track of the number of spaces used to indent the
5379 output of the debugging routines that print the structure of
5380 the DIE internal representation. */
5381 static int print_indent;
5383 /* Indent the line the number of spaces given by print_indent. */
5385 static inline void
5386 print_spaces (FILE *outfile)
5388 fprintf (outfile, "%*s", print_indent, "");
5391 /* Print a type signature in hex. */
5393 static inline void
5394 print_signature (FILE *outfile, char *sig)
5396 int i;
5398 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5399 fprintf (outfile, "%02x", sig[i] & 0xff);
5402 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5404 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5405 RECURSE, output location descriptor operations. */
5407 static void
5408 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5410 switch (val->val_class)
5412 case dw_val_class_addr:
5413 fprintf (outfile, "address");
5414 break;
5415 case dw_val_class_offset:
5416 fprintf (outfile, "offset");
5417 break;
5418 case dw_val_class_loc:
5419 fprintf (outfile, "location descriptor");
5420 if (val->v.val_loc == NULL)
5421 fprintf (outfile, " -> <null>\n");
5422 else if (recurse)
5424 fprintf (outfile, ":\n");
5425 print_indent += 4;
5426 print_loc_descr (val->v.val_loc, outfile);
5427 print_indent -= 4;
5429 else
5430 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5431 break;
5432 case dw_val_class_loc_list:
5433 fprintf (outfile, "location list -> label:%s",
5434 val->v.val_loc_list->ll_symbol);
5435 break;
5436 case dw_val_class_range_list:
5437 fprintf (outfile, "range list");
5438 break;
5439 case dw_val_class_const:
5440 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5441 break;
5442 case dw_val_class_unsigned_const:
5443 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5444 break;
5445 case dw_val_class_const_double:
5446 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5447 HOST_WIDE_INT_PRINT_UNSIGNED")",
5448 val->v.val_double.high,
5449 val->v.val_double.low);
5450 break;
5451 case dw_val_class_wide_int:
5453 int i = val->v.val_wide->get_len ();
5454 fprintf (outfile, "constant (");
5455 gcc_assert (i > 0);
5456 if (val->v.val_wide->elt (i - 1) == 0)
5457 fprintf (outfile, "0x");
5458 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5459 val->v.val_wide->elt (--i));
5460 while (--i >= 0)
5461 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5462 val->v.val_wide->elt (i));
5463 fprintf (outfile, ")");
5464 break;
5466 case dw_val_class_vec:
5467 fprintf (outfile, "floating-point or vector constant");
5468 break;
5469 case dw_val_class_flag:
5470 fprintf (outfile, "%u", val->v.val_flag);
5471 break;
5472 case dw_val_class_die_ref:
5473 if (val->v.val_die_ref.die != NULL)
5475 dw_die_ref die = val->v.val_die_ref.die;
5477 if (die->comdat_type_p)
5479 fprintf (outfile, "die -> signature: ");
5480 print_signature (outfile,
5481 die->die_id.die_type_node->signature);
5483 else if (die->die_id.die_symbol)
5484 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5485 else
5486 fprintf (outfile, "die -> %ld", die->die_offset);
5487 fprintf (outfile, " (%p)", (void *) die);
5489 else
5490 fprintf (outfile, "die -> <null>");
5491 break;
5492 case dw_val_class_vms_delta:
5493 fprintf (outfile, "delta: @slotcount(%s-%s)",
5494 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5495 break;
5496 case dw_val_class_lbl_id:
5497 case dw_val_class_lineptr:
5498 case dw_val_class_macptr:
5499 case dw_val_class_high_pc:
5500 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5501 break;
5502 case dw_val_class_str:
5503 if (val->v.val_str->str != NULL)
5504 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5505 else
5506 fprintf (outfile, "<null>");
5507 break;
5508 case dw_val_class_file:
5509 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5510 val->v.val_file->emitted_number);
5511 break;
5512 case dw_val_class_data8:
5514 int i;
5516 for (i = 0; i < 8; i++)
5517 fprintf (outfile, "%02x", val->v.val_data8[i]);
5518 break;
5520 default:
5521 break;
5525 /* Likewise, for a DIE attribute. */
5527 static void
5528 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5530 print_dw_val (&a->dw_attr_val, recurse, outfile);
5534 /* Print the list of operands in the LOC location description to OUTFILE. This
5535 routine is a debugging aid only. */
5537 static void
5538 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5540 dw_loc_descr_ref l = loc;
5542 if (loc == NULL)
5544 print_spaces (outfile);
5545 fprintf (outfile, "<null>\n");
5546 return;
5549 for (l = loc; l != NULL; l = l->dw_loc_next)
5551 print_spaces (outfile);
5552 fprintf (outfile, "(%p) %s",
5553 (void *) l,
5554 dwarf_stack_op_name (l->dw_loc_opc));
5555 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5557 fprintf (outfile, " ");
5558 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5560 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5562 fprintf (outfile, ", ");
5563 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5565 fprintf (outfile, "\n");
5569 /* Print the information associated with a given DIE, and its children.
5570 This routine is a debugging aid only. */
5572 static void
5573 print_die (dw_die_ref die, FILE *outfile)
5575 dw_attr_ref a;
5576 dw_die_ref c;
5577 unsigned ix;
5579 print_spaces (outfile);
5580 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5581 die->die_offset, dwarf_tag_name (die->die_tag),
5582 (void*) die);
5583 print_spaces (outfile);
5584 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5585 fprintf (outfile, " offset: %ld", die->die_offset);
5586 fprintf (outfile, " mark: %d\n", die->die_mark);
5588 if (die->comdat_type_p)
5590 print_spaces (outfile);
5591 fprintf (outfile, " signature: ");
5592 print_signature (outfile, die->die_id.die_type_node->signature);
5593 fprintf (outfile, "\n");
5596 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5598 print_spaces (outfile);
5599 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5601 print_attribute (a, true, outfile);
5602 fprintf (outfile, "\n");
5605 if (die->die_child != NULL)
5607 print_indent += 4;
5608 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5609 print_indent -= 4;
5611 if (print_indent == 0)
5612 fprintf (outfile, "\n");
5615 /* Print the list of operations in the LOC location description. */
5617 DEBUG_FUNCTION void
5618 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5620 print_loc_descr (loc, stderr);
5623 /* Print the information collected for a given DIE. */
5625 DEBUG_FUNCTION void
5626 debug_dwarf_die (dw_die_ref die)
5628 print_die (die, stderr);
5631 DEBUG_FUNCTION void
5632 debug (die_struct &ref)
5634 print_die (&ref, stderr);
5637 DEBUG_FUNCTION void
5638 debug (die_struct *ptr)
5640 if (ptr)
5641 debug (*ptr);
5642 else
5643 fprintf (stderr, "<nil>\n");
5647 /* Print all DWARF information collected for the compilation unit.
5648 This routine is a debugging aid only. */
5650 DEBUG_FUNCTION void
5651 debug_dwarf (void)
5653 print_indent = 0;
5654 print_die (comp_unit_die (), stderr);
5657 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5658 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5659 DIE that marks the start of the DIEs for this include file. */
5661 static dw_die_ref
5662 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5664 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5665 dw_die_ref new_unit = gen_compile_unit_die (filename);
5667 new_unit->die_sib = old_unit;
5668 return new_unit;
5671 /* Close an include-file CU and reopen the enclosing one. */
5673 static dw_die_ref
5674 pop_compile_unit (dw_die_ref old_unit)
5676 dw_die_ref new_unit = old_unit->die_sib;
5678 old_unit->die_sib = NULL;
5679 return new_unit;
5682 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5683 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5684 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5686 /* Calculate the checksum of a location expression. */
5688 static inline void
5689 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5691 int tem;
5692 inchash::hash hstate;
5693 hashval_t hash;
5695 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5696 CHECKSUM (tem);
5697 hash_loc_operands (loc, hstate);
5698 hash = hstate.end();
5699 CHECKSUM (hash);
5702 /* Calculate the checksum of an attribute. */
5704 static void
5705 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5707 dw_loc_descr_ref loc;
5708 rtx r;
5710 CHECKSUM (at->dw_attr);
5712 /* We don't care that this was compiled with a different compiler
5713 snapshot; if the output is the same, that's what matters. */
5714 if (at->dw_attr == DW_AT_producer)
5715 return;
5717 switch (AT_class (at))
5719 case dw_val_class_const:
5720 CHECKSUM (at->dw_attr_val.v.val_int);
5721 break;
5722 case dw_val_class_unsigned_const:
5723 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5724 break;
5725 case dw_val_class_const_double:
5726 CHECKSUM (at->dw_attr_val.v.val_double);
5727 break;
5728 case dw_val_class_wide_int:
5729 CHECKSUM (*at->dw_attr_val.v.val_wide);
5730 break;
5731 case dw_val_class_vec:
5732 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5733 (at->dw_attr_val.v.val_vec.length
5734 * at->dw_attr_val.v.val_vec.elt_size));
5735 break;
5736 case dw_val_class_flag:
5737 CHECKSUM (at->dw_attr_val.v.val_flag);
5738 break;
5739 case dw_val_class_str:
5740 CHECKSUM_STRING (AT_string (at));
5741 break;
5743 case dw_val_class_addr:
5744 r = AT_addr (at);
5745 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5746 CHECKSUM_STRING (XSTR (r, 0));
5747 break;
5749 case dw_val_class_offset:
5750 CHECKSUM (at->dw_attr_val.v.val_offset);
5751 break;
5753 case dw_val_class_loc:
5754 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5755 loc_checksum (loc, ctx);
5756 break;
5758 case dw_val_class_die_ref:
5759 die_checksum (AT_ref (at), ctx, mark);
5760 break;
5762 case dw_val_class_fde_ref:
5763 case dw_val_class_vms_delta:
5764 case dw_val_class_lbl_id:
5765 case dw_val_class_lineptr:
5766 case dw_val_class_macptr:
5767 case dw_val_class_high_pc:
5768 break;
5770 case dw_val_class_file:
5771 CHECKSUM_STRING (AT_file (at)->filename);
5772 break;
5774 case dw_val_class_data8:
5775 CHECKSUM (at->dw_attr_val.v.val_data8);
5776 break;
5778 default:
5779 break;
5783 /* Calculate the checksum of a DIE. */
5785 static void
5786 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5788 dw_die_ref c;
5789 dw_attr_ref a;
5790 unsigned ix;
5792 /* To avoid infinite recursion. */
5793 if (die->die_mark)
5795 CHECKSUM (die->die_mark);
5796 return;
5798 die->die_mark = ++(*mark);
5800 CHECKSUM (die->die_tag);
5802 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5803 attr_checksum (a, ctx, mark);
5805 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5808 #undef CHECKSUM
5809 #undef CHECKSUM_BLOCK
5810 #undef CHECKSUM_STRING
5812 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5813 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5814 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5815 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5816 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5817 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5818 #define CHECKSUM_ATTR(FOO) \
5819 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5821 /* Calculate the checksum of a number in signed LEB128 format. */
5823 static void
5824 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5826 unsigned char byte;
5827 bool more;
5829 while (1)
5831 byte = (value & 0x7f);
5832 value >>= 7;
5833 more = !((value == 0 && (byte & 0x40) == 0)
5834 || (value == -1 && (byte & 0x40) != 0));
5835 if (more)
5836 byte |= 0x80;
5837 CHECKSUM (byte);
5838 if (!more)
5839 break;
5843 /* Calculate the checksum of a number in unsigned LEB128 format. */
5845 static void
5846 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5848 while (1)
5850 unsigned char byte = (value & 0x7f);
5851 value >>= 7;
5852 if (value != 0)
5853 /* More bytes to follow. */
5854 byte |= 0x80;
5855 CHECKSUM (byte);
5856 if (value == 0)
5857 break;
5861 /* Checksum the context of the DIE. This adds the names of any
5862 surrounding namespaces or structures to the checksum. */
5864 static void
5865 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5867 const char *name;
5868 dw_die_ref spec;
5869 int tag = die->die_tag;
5871 if (tag != DW_TAG_namespace
5872 && tag != DW_TAG_structure_type
5873 && tag != DW_TAG_class_type)
5874 return;
5876 name = get_AT_string (die, DW_AT_name);
5878 spec = get_AT_ref (die, DW_AT_specification);
5879 if (spec != NULL)
5880 die = spec;
5882 if (die->die_parent != NULL)
5883 checksum_die_context (die->die_parent, ctx);
5885 CHECKSUM_ULEB128 ('C');
5886 CHECKSUM_ULEB128 (tag);
5887 if (name != NULL)
5888 CHECKSUM_STRING (name);
5891 /* Calculate the checksum of a location expression. */
5893 static inline void
5894 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5896 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5897 were emitted as a DW_FORM_sdata instead of a location expression. */
5898 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5900 CHECKSUM_ULEB128 (DW_FORM_sdata);
5901 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5902 return;
5905 /* Otherwise, just checksum the raw location expression. */
5906 while (loc != NULL)
5908 inchash::hash hstate;
5909 hashval_t hash;
5911 CHECKSUM_ULEB128 (loc->dtprel);
5912 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5913 hash_loc_operands (loc, hstate);
5914 hash = hstate.end ();
5915 CHECKSUM (hash);
5916 loc = loc->dw_loc_next;
5920 /* Calculate the checksum of an attribute. */
5922 static void
5923 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5924 struct md5_ctx *ctx, int *mark)
5926 dw_loc_descr_ref loc;
5927 rtx r;
5929 if (AT_class (at) == dw_val_class_die_ref)
5931 dw_die_ref target_die = AT_ref (at);
5933 /* For pointer and reference types, we checksum only the (qualified)
5934 name of the target type (if there is a name). For friend entries,
5935 we checksum only the (qualified) name of the target type or function.
5936 This allows the checksum to remain the same whether the target type
5937 is complete or not. */
5938 if ((at->dw_attr == DW_AT_type
5939 && (tag == DW_TAG_pointer_type
5940 || tag == DW_TAG_reference_type
5941 || tag == DW_TAG_rvalue_reference_type
5942 || tag == DW_TAG_ptr_to_member_type))
5943 || (at->dw_attr == DW_AT_friend
5944 && tag == DW_TAG_friend))
5946 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5948 if (name_attr != NULL)
5950 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5952 if (decl == NULL)
5953 decl = target_die;
5954 CHECKSUM_ULEB128 ('N');
5955 CHECKSUM_ULEB128 (at->dw_attr);
5956 if (decl->die_parent != NULL)
5957 checksum_die_context (decl->die_parent, ctx);
5958 CHECKSUM_ULEB128 ('E');
5959 CHECKSUM_STRING (AT_string (name_attr));
5960 return;
5964 /* For all other references to another DIE, we check to see if the
5965 target DIE has already been visited. If it has, we emit a
5966 backward reference; if not, we descend recursively. */
5967 if (target_die->die_mark > 0)
5969 CHECKSUM_ULEB128 ('R');
5970 CHECKSUM_ULEB128 (at->dw_attr);
5971 CHECKSUM_ULEB128 (target_die->die_mark);
5973 else
5975 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5977 if (decl == NULL)
5978 decl = target_die;
5979 target_die->die_mark = ++(*mark);
5980 CHECKSUM_ULEB128 ('T');
5981 CHECKSUM_ULEB128 (at->dw_attr);
5982 if (decl->die_parent != NULL)
5983 checksum_die_context (decl->die_parent, ctx);
5984 die_checksum_ordered (target_die, ctx, mark);
5986 return;
5989 CHECKSUM_ULEB128 ('A');
5990 CHECKSUM_ULEB128 (at->dw_attr);
5992 switch (AT_class (at))
5994 case dw_val_class_const:
5995 CHECKSUM_ULEB128 (DW_FORM_sdata);
5996 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5997 break;
5999 case dw_val_class_unsigned_const:
6000 CHECKSUM_ULEB128 (DW_FORM_sdata);
6001 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6002 break;
6004 case dw_val_class_const_double:
6005 CHECKSUM_ULEB128 (DW_FORM_block);
6006 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6007 CHECKSUM (at->dw_attr_val.v.val_double);
6008 break;
6010 case dw_val_class_wide_int:
6011 CHECKSUM_ULEB128 (DW_FORM_block);
6012 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
6013 CHECKSUM (*at->dw_attr_val.v.val_wide);
6014 break;
6016 case dw_val_class_vec:
6017 CHECKSUM_ULEB128 (DW_FORM_block);
6018 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6019 * at->dw_attr_val.v.val_vec.elt_size);
6020 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6021 (at->dw_attr_val.v.val_vec.length
6022 * at->dw_attr_val.v.val_vec.elt_size));
6023 break;
6025 case dw_val_class_flag:
6026 CHECKSUM_ULEB128 (DW_FORM_flag);
6027 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6028 break;
6030 case dw_val_class_str:
6031 CHECKSUM_ULEB128 (DW_FORM_string);
6032 CHECKSUM_STRING (AT_string (at));
6033 break;
6035 case dw_val_class_addr:
6036 r = AT_addr (at);
6037 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6038 CHECKSUM_ULEB128 (DW_FORM_string);
6039 CHECKSUM_STRING (XSTR (r, 0));
6040 break;
6042 case dw_val_class_offset:
6043 CHECKSUM_ULEB128 (DW_FORM_sdata);
6044 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6045 break;
6047 case dw_val_class_loc:
6048 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6049 loc_checksum_ordered (loc, ctx);
6050 break;
6052 case dw_val_class_fde_ref:
6053 case dw_val_class_lbl_id:
6054 case dw_val_class_lineptr:
6055 case dw_val_class_macptr:
6056 case dw_val_class_high_pc:
6057 break;
6059 case dw_val_class_file:
6060 CHECKSUM_ULEB128 (DW_FORM_string);
6061 CHECKSUM_STRING (AT_file (at)->filename);
6062 break;
6064 case dw_val_class_data8:
6065 CHECKSUM (at->dw_attr_val.v.val_data8);
6066 break;
6068 default:
6069 break;
6073 struct checksum_attributes
6075 dw_attr_ref at_name;
6076 dw_attr_ref at_type;
6077 dw_attr_ref at_friend;
6078 dw_attr_ref at_accessibility;
6079 dw_attr_ref at_address_class;
6080 dw_attr_ref at_allocated;
6081 dw_attr_ref at_artificial;
6082 dw_attr_ref at_associated;
6083 dw_attr_ref at_binary_scale;
6084 dw_attr_ref at_bit_offset;
6085 dw_attr_ref at_bit_size;
6086 dw_attr_ref at_bit_stride;
6087 dw_attr_ref at_byte_size;
6088 dw_attr_ref at_byte_stride;
6089 dw_attr_ref at_const_value;
6090 dw_attr_ref at_containing_type;
6091 dw_attr_ref at_count;
6092 dw_attr_ref at_data_location;
6093 dw_attr_ref at_data_member_location;
6094 dw_attr_ref at_decimal_scale;
6095 dw_attr_ref at_decimal_sign;
6096 dw_attr_ref at_default_value;
6097 dw_attr_ref at_digit_count;
6098 dw_attr_ref at_discr;
6099 dw_attr_ref at_discr_list;
6100 dw_attr_ref at_discr_value;
6101 dw_attr_ref at_encoding;
6102 dw_attr_ref at_endianity;
6103 dw_attr_ref at_explicit;
6104 dw_attr_ref at_is_optional;
6105 dw_attr_ref at_location;
6106 dw_attr_ref at_lower_bound;
6107 dw_attr_ref at_mutable;
6108 dw_attr_ref at_ordering;
6109 dw_attr_ref at_picture_string;
6110 dw_attr_ref at_prototyped;
6111 dw_attr_ref at_small;
6112 dw_attr_ref at_segment;
6113 dw_attr_ref at_string_length;
6114 dw_attr_ref at_threads_scaled;
6115 dw_attr_ref at_upper_bound;
6116 dw_attr_ref at_use_location;
6117 dw_attr_ref at_use_UTF8;
6118 dw_attr_ref at_variable_parameter;
6119 dw_attr_ref at_virtuality;
6120 dw_attr_ref at_visibility;
6121 dw_attr_ref at_vtable_elem_location;
6124 /* Collect the attributes that we will want to use for the checksum. */
6126 static void
6127 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6129 dw_attr_ref a;
6130 unsigned ix;
6132 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6134 switch (a->dw_attr)
6136 case DW_AT_name:
6137 attrs->at_name = a;
6138 break;
6139 case DW_AT_type:
6140 attrs->at_type = a;
6141 break;
6142 case DW_AT_friend:
6143 attrs->at_friend = a;
6144 break;
6145 case DW_AT_accessibility:
6146 attrs->at_accessibility = a;
6147 break;
6148 case DW_AT_address_class:
6149 attrs->at_address_class = a;
6150 break;
6151 case DW_AT_allocated:
6152 attrs->at_allocated = a;
6153 break;
6154 case DW_AT_artificial:
6155 attrs->at_artificial = a;
6156 break;
6157 case DW_AT_associated:
6158 attrs->at_associated = a;
6159 break;
6160 case DW_AT_binary_scale:
6161 attrs->at_binary_scale = a;
6162 break;
6163 case DW_AT_bit_offset:
6164 attrs->at_bit_offset = a;
6165 break;
6166 case DW_AT_bit_size:
6167 attrs->at_bit_size = a;
6168 break;
6169 case DW_AT_bit_stride:
6170 attrs->at_bit_stride = a;
6171 break;
6172 case DW_AT_byte_size:
6173 attrs->at_byte_size = a;
6174 break;
6175 case DW_AT_byte_stride:
6176 attrs->at_byte_stride = a;
6177 break;
6178 case DW_AT_const_value:
6179 attrs->at_const_value = a;
6180 break;
6181 case DW_AT_containing_type:
6182 attrs->at_containing_type = a;
6183 break;
6184 case DW_AT_count:
6185 attrs->at_count = a;
6186 break;
6187 case DW_AT_data_location:
6188 attrs->at_data_location = a;
6189 break;
6190 case DW_AT_data_member_location:
6191 attrs->at_data_member_location = a;
6192 break;
6193 case DW_AT_decimal_scale:
6194 attrs->at_decimal_scale = a;
6195 break;
6196 case DW_AT_decimal_sign:
6197 attrs->at_decimal_sign = a;
6198 break;
6199 case DW_AT_default_value:
6200 attrs->at_default_value = a;
6201 break;
6202 case DW_AT_digit_count:
6203 attrs->at_digit_count = a;
6204 break;
6205 case DW_AT_discr:
6206 attrs->at_discr = a;
6207 break;
6208 case DW_AT_discr_list:
6209 attrs->at_discr_list = a;
6210 break;
6211 case DW_AT_discr_value:
6212 attrs->at_discr_value = a;
6213 break;
6214 case DW_AT_encoding:
6215 attrs->at_encoding = a;
6216 break;
6217 case DW_AT_endianity:
6218 attrs->at_endianity = a;
6219 break;
6220 case DW_AT_explicit:
6221 attrs->at_explicit = a;
6222 break;
6223 case DW_AT_is_optional:
6224 attrs->at_is_optional = a;
6225 break;
6226 case DW_AT_location:
6227 attrs->at_location = a;
6228 break;
6229 case DW_AT_lower_bound:
6230 attrs->at_lower_bound = a;
6231 break;
6232 case DW_AT_mutable:
6233 attrs->at_mutable = a;
6234 break;
6235 case DW_AT_ordering:
6236 attrs->at_ordering = a;
6237 break;
6238 case DW_AT_picture_string:
6239 attrs->at_picture_string = a;
6240 break;
6241 case DW_AT_prototyped:
6242 attrs->at_prototyped = a;
6243 break;
6244 case DW_AT_small:
6245 attrs->at_small = a;
6246 break;
6247 case DW_AT_segment:
6248 attrs->at_segment = a;
6249 break;
6250 case DW_AT_string_length:
6251 attrs->at_string_length = a;
6252 break;
6253 case DW_AT_threads_scaled:
6254 attrs->at_threads_scaled = a;
6255 break;
6256 case DW_AT_upper_bound:
6257 attrs->at_upper_bound = a;
6258 break;
6259 case DW_AT_use_location:
6260 attrs->at_use_location = a;
6261 break;
6262 case DW_AT_use_UTF8:
6263 attrs->at_use_UTF8 = a;
6264 break;
6265 case DW_AT_variable_parameter:
6266 attrs->at_variable_parameter = a;
6267 break;
6268 case DW_AT_virtuality:
6269 attrs->at_virtuality = a;
6270 break;
6271 case DW_AT_visibility:
6272 attrs->at_visibility = a;
6273 break;
6274 case DW_AT_vtable_elem_location:
6275 attrs->at_vtable_elem_location = a;
6276 break;
6277 default:
6278 break;
6283 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6285 static void
6286 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6288 dw_die_ref c;
6289 dw_die_ref decl;
6290 struct checksum_attributes attrs;
6292 CHECKSUM_ULEB128 ('D');
6293 CHECKSUM_ULEB128 (die->die_tag);
6295 memset (&attrs, 0, sizeof (attrs));
6297 decl = get_AT_ref (die, DW_AT_specification);
6298 if (decl != NULL)
6299 collect_checksum_attributes (&attrs, decl);
6300 collect_checksum_attributes (&attrs, die);
6302 CHECKSUM_ATTR (attrs.at_name);
6303 CHECKSUM_ATTR (attrs.at_accessibility);
6304 CHECKSUM_ATTR (attrs.at_address_class);
6305 CHECKSUM_ATTR (attrs.at_allocated);
6306 CHECKSUM_ATTR (attrs.at_artificial);
6307 CHECKSUM_ATTR (attrs.at_associated);
6308 CHECKSUM_ATTR (attrs.at_binary_scale);
6309 CHECKSUM_ATTR (attrs.at_bit_offset);
6310 CHECKSUM_ATTR (attrs.at_bit_size);
6311 CHECKSUM_ATTR (attrs.at_bit_stride);
6312 CHECKSUM_ATTR (attrs.at_byte_size);
6313 CHECKSUM_ATTR (attrs.at_byte_stride);
6314 CHECKSUM_ATTR (attrs.at_const_value);
6315 CHECKSUM_ATTR (attrs.at_containing_type);
6316 CHECKSUM_ATTR (attrs.at_count);
6317 CHECKSUM_ATTR (attrs.at_data_location);
6318 CHECKSUM_ATTR (attrs.at_data_member_location);
6319 CHECKSUM_ATTR (attrs.at_decimal_scale);
6320 CHECKSUM_ATTR (attrs.at_decimal_sign);
6321 CHECKSUM_ATTR (attrs.at_default_value);
6322 CHECKSUM_ATTR (attrs.at_digit_count);
6323 CHECKSUM_ATTR (attrs.at_discr);
6324 CHECKSUM_ATTR (attrs.at_discr_list);
6325 CHECKSUM_ATTR (attrs.at_discr_value);
6326 CHECKSUM_ATTR (attrs.at_encoding);
6327 CHECKSUM_ATTR (attrs.at_endianity);
6328 CHECKSUM_ATTR (attrs.at_explicit);
6329 CHECKSUM_ATTR (attrs.at_is_optional);
6330 CHECKSUM_ATTR (attrs.at_location);
6331 CHECKSUM_ATTR (attrs.at_lower_bound);
6332 CHECKSUM_ATTR (attrs.at_mutable);
6333 CHECKSUM_ATTR (attrs.at_ordering);
6334 CHECKSUM_ATTR (attrs.at_picture_string);
6335 CHECKSUM_ATTR (attrs.at_prototyped);
6336 CHECKSUM_ATTR (attrs.at_small);
6337 CHECKSUM_ATTR (attrs.at_segment);
6338 CHECKSUM_ATTR (attrs.at_string_length);
6339 CHECKSUM_ATTR (attrs.at_threads_scaled);
6340 CHECKSUM_ATTR (attrs.at_upper_bound);
6341 CHECKSUM_ATTR (attrs.at_use_location);
6342 CHECKSUM_ATTR (attrs.at_use_UTF8);
6343 CHECKSUM_ATTR (attrs.at_variable_parameter);
6344 CHECKSUM_ATTR (attrs.at_virtuality);
6345 CHECKSUM_ATTR (attrs.at_visibility);
6346 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6347 CHECKSUM_ATTR (attrs.at_type);
6348 CHECKSUM_ATTR (attrs.at_friend);
6350 /* Checksum the child DIEs. */
6351 c = die->die_child;
6352 if (c) do {
6353 dw_attr_ref name_attr;
6355 c = c->die_sib;
6356 name_attr = get_AT (c, DW_AT_name);
6357 if (is_template_instantiation (c))
6359 /* Ignore instantiations of member type and function templates. */
6361 else if (name_attr != NULL
6362 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6364 /* Use a shallow checksum for named nested types and member
6365 functions. */
6366 CHECKSUM_ULEB128 ('S');
6367 CHECKSUM_ULEB128 (c->die_tag);
6368 CHECKSUM_STRING (AT_string (name_attr));
6370 else
6372 /* Use a deep checksum for other children. */
6373 /* Mark this DIE so it gets processed when unmarking. */
6374 if (c->die_mark == 0)
6375 c->die_mark = -1;
6376 die_checksum_ordered (c, ctx, mark);
6378 } while (c != die->die_child);
6380 CHECKSUM_ULEB128 (0);
6383 /* Add a type name and tag to a hash. */
6384 static void
6385 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6387 CHECKSUM_ULEB128 (tag);
6388 CHECKSUM_STRING (name);
6391 #undef CHECKSUM
6392 #undef CHECKSUM_STRING
6393 #undef CHECKSUM_ATTR
6394 #undef CHECKSUM_LEB128
6395 #undef CHECKSUM_ULEB128
6397 /* Generate the type signature for DIE. This is computed by generating an
6398 MD5 checksum over the DIE's tag, its relevant attributes, and its
6399 children. Attributes that are references to other DIEs are processed
6400 by recursion, using the MARK field to prevent infinite recursion.
6401 If the DIE is nested inside a namespace or another type, we also
6402 need to include that context in the signature. The lower 64 bits
6403 of the resulting MD5 checksum comprise the signature. */
6405 static void
6406 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6408 int mark;
6409 const char *name;
6410 unsigned char checksum[16];
6411 struct md5_ctx ctx;
6412 dw_die_ref decl;
6413 dw_die_ref parent;
6415 name = get_AT_string (die, DW_AT_name);
6416 decl = get_AT_ref (die, DW_AT_specification);
6417 parent = get_die_parent (die);
6419 /* First, compute a signature for just the type name (and its surrounding
6420 context, if any. This is stored in the type unit DIE for link-time
6421 ODR (one-definition rule) checking. */
6423 if (is_cxx () && name != NULL)
6425 md5_init_ctx (&ctx);
6427 /* Checksum the names of surrounding namespaces and structures. */
6428 if (parent != NULL)
6429 checksum_die_context (parent, &ctx);
6431 /* Checksum the current DIE. */
6432 die_odr_checksum (die->die_tag, name, &ctx);
6433 md5_finish_ctx (&ctx, checksum);
6435 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6438 /* Next, compute the complete type signature. */
6440 md5_init_ctx (&ctx);
6441 mark = 1;
6442 die->die_mark = mark;
6444 /* Checksum the names of surrounding namespaces and structures. */
6445 if (parent != NULL)
6446 checksum_die_context (parent, &ctx);
6448 /* Checksum the DIE and its children. */
6449 die_checksum_ordered (die, &ctx, &mark);
6450 unmark_all_dies (die);
6451 md5_finish_ctx (&ctx, checksum);
6453 /* Store the signature in the type node and link the type DIE and the
6454 type node together. */
6455 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6456 DWARF_TYPE_SIGNATURE_SIZE);
6457 die->comdat_type_p = true;
6458 die->die_id.die_type_node = type_node;
6459 type_node->type_die = die;
6461 /* If the DIE is a specification, link its declaration to the type node
6462 as well. */
6463 if (decl != NULL)
6465 decl->comdat_type_p = true;
6466 decl->die_id.die_type_node = type_node;
6470 /* Do the location expressions look same? */
6471 static inline int
6472 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6474 return loc1->dw_loc_opc == loc2->dw_loc_opc
6475 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6476 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6479 /* Do the values look the same? */
6480 static int
6481 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6483 dw_loc_descr_ref loc1, loc2;
6484 rtx r1, r2;
6486 if (v1->val_class != v2->val_class)
6487 return 0;
6489 switch (v1->val_class)
6491 case dw_val_class_const:
6492 return v1->v.val_int == v2->v.val_int;
6493 case dw_val_class_unsigned_const:
6494 return v1->v.val_unsigned == v2->v.val_unsigned;
6495 case dw_val_class_const_double:
6496 return v1->v.val_double.high == v2->v.val_double.high
6497 && v1->v.val_double.low == v2->v.val_double.low;
6498 case dw_val_class_wide_int:
6499 return *v1->v.val_wide == *v2->v.val_wide;
6500 case dw_val_class_vec:
6501 if (v1->v.val_vec.length != v2->v.val_vec.length
6502 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6503 return 0;
6504 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6505 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6506 return 0;
6507 return 1;
6508 case dw_val_class_flag:
6509 return v1->v.val_flag == v2->v.val_flag;
6510 case dw_val_class_str:
6511 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6513 case dw_val_class_addr:
6514 r1 = v1->v.val_addr;
6515 r2 = v2->v.val_addr;
6516 if (GET_CODE (r1) != GET_CODE (r2))
6517 return 0;
6518 return !rtx_equal_p (r1, r2);
6520 case dw_val_class_offset:
6521 return v1->v.val_offset == v2->v.val_offset;
6523 case dw_val_class_loc:
6524 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6525 loc1 && loc2;
6526 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6527 if (!same_loc_p (loc1, loc2, mark))
6528 return 0;
6529 return !loc1 && !loc2;
6531 case dw_val_class_die_ref:
6532 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6534 case dw_val_class_fde_ref:
6535 case dw_val_class_vms_delta:
6536 case dw_val_class_lbl_id:
6537 case dw_val_class_lineptr:
6538 case dw_val_class_macptr:
6539 case dw_val_class_high_pc:
6540 return 1;
6542 case dw_val_class_file:
6543 return v1->v.val_file == v2->v.val_file;
6545 case dw_val_class_data8:
6546 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6548 default:
6549 return 1;
6553 /* Do the attributes look the same? */
6555 static int
6556 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6558 if (at1->dw_attr != at2->dw_attr)
6559 return 0;
6561 /* We don't care that this was compiled with a different compiler
6562 snapshot; if the output is the same, that's what matters. */
6563 if (at1->dw_attr == DW_AT_producer)
6564 return 1;
6566 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6569 /* Do the dies look the same? */
6571 static int
6572 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6574 dw_die_ref c1, c2;
6575 dw_attr_ref a1;
6576 unsigned ix;
6578 /* To avoid infinite recursion. */
6579 if (die1->die_mark)
6580 return die1->die_mark == die2->die_mark;
6581 die1->die_mark = die2->die_mark = ++(*mark);
6583 if (die1->die_tag != die2->die_tag)
6584 return 0;
6586 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6587 return 0;
6589 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6590 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6591 return 0;
6593 c1 = die1->die_child;
6594 c2 = die2->die_child;
6595 if (! c1)
6597 if (c2)
6598 return 0;
6600 else
6601 for (;;)
6603 if (!same_die_p (c1, c2, mark))
6604 return 0;
6605 c1 = c1->die_sib;
6606 c2 = c2->die_sib;
6607 if (c1 == die1->die_child)
6609 if (c2 == die2->die_child)
6610 break;
6611 else
6612 return 0;
6616 return 1;
6619 /* Do the dies look the same? Wrapper around same_die_p. */
6621 static int
6622 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6624 int mark = 0;
6625 int ret = same_die_p (die1, die2, &mark);
6627 unmark_all_dies (die1);
6628 unmark_all_dies (die2);
6630 return ret;
6633 /* The prefix to attach to symbols on DIEs in the current comdat debug
6634 info section. */
6635 static const char *comdat_symbol_id;
6637 /* The index of the current symbol within the current comdat CU. */
6638 static unsigned int comdat_symbol_number;
6640 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6641 children, and set comdat_symbol_id accordingly. */
6643 static void
6644 compute_section_prefix (dw_die_ref unit_die)
6646 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6647 const char *base = die_name ? lbasename (die_name) : "anonymous";
6648 char *name = XALLOCAVEC (char, strlen (base) + 64);
6649 char *p;
6650 int i, mark;
6651 unsigned char checksum[16];
6652 struct md5_ctx ctx;
6654 /* Compute the checksum of the DIE, then append part of it as hex digits to
6655 the name filename of the unit. */
6657 md5_init_ctx (&ctx);
6658 mark = 0;
6659 die_checksum (unit_die, &ctx, &mark);
6660 unmark_all_dies (unit_die);
6661 md5_finish_ctx (&ctx, checksum);
6663 sprintf (name, "%s.", base);
6664 clean_symbol_name (name);
6666 p = name + strlen (name);
6667 for (i = 0; i < 4; i++)
6669 sprintf (p, "%.2x", checksum[i]);
6670 p += 2;
6673 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6674 comdat_symbol_number = 0;
6677 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6679 static int
6680 is_type_die (dw_die_ref die)
6682 switch (die->die_tag)
6684 case DW_TAG_array_type:
6685 case DW_TAG_class_type:
6686 case DW_TAG_interface_type:
6687 case DW_TAG_enumeration_type:
6688 case DW_TAG_pointer_type:
6689 case DW_TAG_reference_type:
6690 case DW_TAG_rvalue_reference_type:
6691 case DW_TAG_string_type:
6692 case DW_TAG_structure_type:
6693 case DW_TAG_subroutine_type:
6694 case DW_TAG_union_type:
6695 case DW_TAG_ptr_to_member_type:
6696 case DW_TAG_set_type:
6697 case DW_TAG_subrange_type:
6698 case DW_TAG_base_type:
6699 case DW_TAG_const_type:
6700 case DW_TAG_file_type:
6701 case DW_TAG_packed_type:
6702 case DW_TAG_volatile_type:
6703 case DW_TAG_typedef:
6704 return 1;
6705 default:
6706 return 0;
6710 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6711 Basically, we want to choose the bits that are likely to be shared between
6712 compilations (types) and leave out the bits that are specific to individual
6713 compilations (functions). */
6715 static int
6716 is_comdat_die (dw_die_ref c)
6718 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6719 we do for stabs. The advantage is a greater likelihood of sharing between
6720 objects that don't include headers in the same order (and therefore would
6721 put the base types in a different comdat). jason 8/28/00 */
6723 if (c->die_tag == DW_TAG_base_type)
6724 return 0;
6726 if (c->die_tag == DW_TAG_pointer_type
6727 || c->die_tag == DW_TAG_reference_type
6728 || c->die_tag == DW_TAG_rvalue_reference_type
6729 || c->die_tag == DW_TAG_const_type
6730 || c->die_tag == DW_TAG_volatile_type)
6732 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6734 return t ? is_comdat_die (t) : 0;
6737 return is_type_die (c);
6740 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6741 compilation unit. */
6743 static int
6744 is_symbol_die (dw_die_ref c)
6746 return (is_type_die (c)
6747 || is_declaration_die (c)
6748 || c->die_tag == DW_TAG_namespace
6749 || c->die_tag == DW_TAG_module);
6752 /* Returns true iff C is a compile-unit DIE. */
6754 static inline bool
6755 is_cu_die (dw_die_ref c)
6757 return c && c->die_tag == DW_TAG_compile_unit;
6760 /* Returns true iff C is a unit DIE of some sort. */
6762 static inline bool
6763 is_unit_die (dw_die_ref c)
6765 return c && (c->die_tag == DW_TAG_compile_unit
6766 || c->die_tag == DW_TAG_partial_unit
6767 || c->die_tag == DW_TAG_type_unit);
6770 /* Returns true iff C is a namespace DIE. */
6772 static inline bool
6773 is_namespace_die (dw_die_ref c)
6775 return c && c->die_tag == DW_TAG_namespace;
6778 /* Returns true iff C is a class or structure DIE. */
6780 static inline bool
6781 is_class_die (dw_die_ref c)
6783 return c && (c->die_tag == DW_TAG_class_type
6784 || c->die_tag == DW_TAG_structure_type);
6787 /* Return non-zero if this DIE is a template parameter. */
6789 static inline bool
6790 is_template_parameter (dw_die_ref die)
6792 switch (die->die_tag)
6794 case DW_TAG_template_type_param:
6795 case DW_TAG_template_value_param:
6796 case DW_TAG_GNU_template_template_param:
6797 case DW_TAG_GNU_template_parameter_pack:
6798 return true;
6799 default:
6800 return false;
6804 /* Return non-zero if this DIE represents a template instantiation. */
6806 static inline bool
6807 is_template_instantiation (dw_die_ref die)
6809 dw_die_ref c;
6811 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6812 return false;
6813 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6814 return false;
6817 static char *
6818 gen_internal_sym (const char *prefix)
6820 char buf[256];
6822 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6823 return xstrdup (buf);
6826 /* Assign symbols to all worthy DIEs under DIE. */
6828 static void
6829 assign_symbol_names (dw_die_ref die)
6831 dw_die_ref c;
6833 if (is_symbol_die (die) && !die->comdat_type_p)
6835 if (comdat_symbol_id)
6837 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6839 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6840 comdat_symbol_id, comdat_symbol_number++);
6841 die->die_id.die_symbol = xstrdup (p);
6843 else
6844 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6847 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6850 struct cu_hash_table_entry
6852 dw_die_ref cu;
6853 unsigned min_comdat_num, max_comdat_num;
6854 struct cu_hash_table_entry *next;
6857 /* Helpers to manipulate hash table of CUs. */
6859 struct cu_hash_table_entry_hasher
6861 typedef cu_hash_table_entry value_type;
6862 typedef die_struct compare_type;
6863 static inline hashval_t hash (const value_type *);
6864 static inline bool equal (const value_type *, const compare_type *);
6865 static inline void remove (value_type *);
6868 inline hashval_t
6869 cu_hash_table_entry_hasher::hash (const value_type *entry)
6871 return htab_hash_string (entry->cu->die_id.die_symbol);
6874 inline bool
6875 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6876 const compare_type *entry2)
6878 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6881 inline void
6882 cu_hash_table_entry_hasher::remove (value_type *entry)
6884 struct cu_hash_table_entry *next;
6886 while (entry)
6888 next = entry->next;
6889 free (entry);
6890 entry = next;
6894 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6896 /* Check whether we have already seen this CU and set up SYM_NUM
6897 accordingly. */
6898 static int
6899 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6901 struct cu_hash_table_entry dummy;
6902 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6904 dummy.max_comdat_num = 0;
6906 slot = htable->find_slot_with_hash (cu,
6907 htab_hash_string (cu->die_id.die_symbol),
6908 INSERT);
6909 entry = *slot;
6911 for (; entry; last = entry, entry = entry->next)
6913 if (same_die_p_wrap (cu, entry->cu))
6914 break;
6917 if (entry)
6919 *sym_num = entry->min_comdat_num;
6920 return 1;
6923 entry = XCNEW (struct cu_hash_table_entry);
6924 entry->cu = cu;
6925 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6926 entry->next = *slot;
6927 *slot = entry;
6929 return 0;
6932 /* Record SYM_NUM to record of CU in HTABLE. */
6933 static void
6934 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6935 unsigned int sym_num)
6937 struct cu_hash_table_entry **slot, *entry;
6939 slot = htable->find_slot_with_hash (cu,
6940 htab_hash_string (cu->die_id.die_symbol),
6941 NO_INSERT);
6942 entry = *slot;
6944 entry->max_comdat_num = sym_num;
6947 /* Traverse the DIE (which is always comp_unit_die), and set up
6948 additional compilation units for each of the include files we see
6949 bracketed by BINCL/EINCL. */
6951 static void
6952 break_out_includes (dw_die_ref die)
6954 dw_die_ref c;
6955 dw_die_ref unit = NULL;
6956 limbo_die_node *node, **pnode;
6958 c = die->die_child;
6959 if (c) do {
6960 dw_die_ref prev = c;
6961 c = c->die_sib;
6962 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6963 || (unit && is_comdat_die (c)))
6965 dw_die_ref next = c->die_sib;
6967 /* This DIE is for a secondary CU; remove it from the main one. */
6968 remove_child_with_prev (c, prev);
6970 if (c->die_tag == DW_TAG_GNU_BINCL)
6971 unit = push_new_compile_unit (unit, c);
6972 else if (c->die_tag == DW_TAG_GNU_EINCL)
6973 unit = pop_compile_unit (unit);
6974 else
6975 add_child_die (unit, c);
6976 c = next;
6977 if (c == die->die_child)
6978 break;
6980 } while (c != die->die_child);
6982 #if 0
6983 /* We can only use this in debugging, since the frontend doesn't check
6984 to make sure that we leave every include file we enter. */
6985 gcc_assert (!unit);
6986 #endif
6988 assign_symbol_names (die);
6989 cu_hash_type cu_hash_table (10);
6990 for (node = limbo_die_list, pnode = &limbo_die_list;
6991 node;
6992 node = node->next)
6994 int is_dupl;
6996 compute_section_prefix (node->die);
6997 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6998 &comdat_symbol_number);
6999 assign_symbol_names (node->die);
7000 if (is_dupl)
7001 *pnode = node->next;
7002 else
7004 pnode = &node->next;
7005 record_comdat_symbol_number (node->die, &cu_hash_table,
7006 comdat_symbol_number);
7011 /* Return non-zero if this DIE is a declaration. */
7013 static int
7014 is_declaration_die (dw_die_ref die)
7016 dw_attr_ref a;
7017 unsigned ix;
7019 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7020 if (a->dw_attr == DW_AT_declaration)
7021 return 1;
7023 return 0;
7026 /* Return non-zero if this DIE is nested inside a subprogram. */
7028 static int
7029 is_nested_in_subprogram (dw_die_ref die)
7031 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7033 if (decl == NULL)
7034 decl = die;
7035 return local_scope_p (decl);
7038 /* Return non-zero if this DIE contains a defining declaration of a
7039 subprogram. */
7041 static int
7042 contains_subprogram_definition (dw_die_ref die)
7044 dw_die_ref c;
7046 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7047 return 1;
7048 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7049 return 0;
7052 /* Return non-zero if this is a type DIE that should be moved to a
7053 COMDAT .debug_types section. */
7055 static int
7056 should_move_die_to_comdat (dw_die_ref die)
7058 switch (die->die_tag)
7060 case DW_TAG_class_type:
7061 case DW_TAG_structure_type:
7062 case DW_TAG_enumeration_type:
7063 case DW_TAG_union_type:
7064 /* Don't move declarations, inlined instances, types nested in a
7065 subprogram, or types that contain subprogram definitions. */
7066 if (is_declaration_die (die)
7067 || get_AT (die, DW_AT_abstract_origin)
7068 || is_nested_in_subprogram (die)
7069 || contains_subprogram_definition (die))
7070 return 0;
7071 return 1;
7072 case DW_TAG_array_type:
7073 case DW_TAG_interface_type:
7074 case DW_TAG_pointer_type:
7075 case DW_TAG_reference_type:
7076 case DW_TAG_rvalue_reference_type:
7077 case DW_TAG_string_type:
7078 case DW_TAG_subroutine_type:
7079 case DW_TAG_ptr_to_member_type:
7080 case DW_TAG_set_type:
7081 case DW_TAG_subrange_type:
7082 case DW_TAG_base_type:
7083 case DW_TAG_const_type:
7084 case DW_TAG_file_type:
7085 case DW_TAG_packed_type:
7086 case DW_TAG_volatile_type:
7087 case DW_TAG_typedef:
7088 default:
7089 return 0;
7093 /* Make a clone of DIE. */
7095 static dw_die_ref
7096 clone_die (dw_die_ref die)
7098 dw_die_ref clone;
7099 dw_attr_ref a;
7100 unsigned ix;
7102 clone = ggc_cleared_alloc<die_node> ();
7103 clone->die_tag = die->die_tag;
7105 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7106 add_dwarf_attr (clone, a);
7108 return clone;
7111 /* Make a clone of the tree rooted at DIE. */
7113 static dw_die_ref
7114 clone_tree (dw_die_ref die)
7116 dw_die_ref c;
7117 dw_die_ref clone = clone_die (die);
7119 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7121 return clone;
7124 /* Make a clone of DIE as a declaration. */
7126 static dw_die_ref
7127 clone_as_declaration (dw_die_ref die)
7129 dw_die_ref clone;
7130 dw_die_ref decl;
7131 dw_attr_ref a;
7132 unsigned ix;
7134 /* If the DIE is already a declaration, just clone it. */
7135 if (is_declaration_die (die))
7136 return clone_die (die);
7138 /* If the DIE is a specification, just clone its declaration DIE. */
7139 decl = get_AT_ref (die, DW_AT_specification);
7140 if (decl != NULL)
7142 clone = clone_die (decl);
7143 if (die->comdat_type_p)
7144 add_AT_die_ref (clone, DW_AT_signature, die);
7145 return clone;
7148 clone = ggc_cleared_alloc<die_node> ();
7149 clone->die_tag = die->die_tag;
7151 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7153 /* We don't want to copy over all attributes.
7154 For example we don't want DW_AT_byte_size because otherwise we will no
7155 longer have a declaration and GDB will treat it as a definition. */
7157 switch (a->dw_attr)
7159 case DW_AT_abstract_origin:
7160 case DW_AT_artificial:
7161 case DW_AT_containing_type:
7162 case DW_AT_external:
7163 case DW_AT_name:
7164 case DW_AT_type:
7165 case DW_AT_virtuality:
7166 case DW_AT_linkage_name:
7167 case DW_AT_MIPS_linkage_name:
7168 add_dwarf_attr (clone, a);
7169 break;
7170 case DW_AT_byte_size:
7171 default:
7172 break;
7176 if (die->comdat_type_p)
7177 add_AT_die_ref (clone, DW_AT_signature, die);
7179 add_AT_flag (clone, DW_AT_declaration, 1);
7180 return clone;
7184 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7186 struct decl_table_entry
7188 dw_die_ref orig;
7189 dw_die_ref copy;
7192 /* Helpers to manipulate hash table of copied declarations. */
7194 /* Hashtable helpers. */
7196 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7198 typedef decl_table_entry value_type;
7199 typedef die_struct compare_type;
7200 static inline hashval_t hash (const value_type *);
7201 static inline bool equal (const value_type *, const compare_type *);
7204 inline hashval_t
7205 decl_table_entry_hasher::hash (const value_type *entry)
7207 return htab_hash_pointer (entry->orig);
7210 inline bool
7211 decl_table_entry_hasher::equal (const value_type *entry1,
7212 const compare_type *entry2)
7214 return entry1->orig == entry2;
7217 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7219 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7220 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7221 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7222 to check if the ancestor has already been copied into UNIT. */
7224 static dw_die_ref
7225 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7226 decl_hash_type *decl_table)
7228 dw_die_ref parent = die->die_parent;
7229 dw_die_ref new_parent = unit;
7230 dw_die_ref copy;
7231 decl_table_entry **slot = NULL;
7232 struct decl_table_entry *entry = NULL;
7234 if (decl_table)
7236 /* Check if the entry has already been copied to UNIT. */
7237 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7238 INSERT);
7239 if (*slot != HTAB_EMPTY_ENTRY)
7241 entry = *slot;
7242 return entry->copy;
7245 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7246 entry = XCNEW (struct decl_table_entry);
7247 entry->orig = die;
7248 entry->copy = NULL;
7249 *slot = entry;
7252 if (parent != NULL)
7254 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7255 if (spec != NULL)
7256 parent = spec;
7257 if (!is_unit_die (parent))
7258 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7261 copy = clone_as_declaration (die);
7262 add_child_die (new_parent, copy);
7264 if (decl_table)
7266 /* Record the pointer to the copy. */
7267 entry->copy = copy;
7270 return copy;
7272 /* Copy the declaration context to the new type unit DIE. This includes
7273 any surrounding namespace or type declarations. If the DIE has an
7274 AT_specification attribute, it also includes attributes and children
7275 attached to the specification, and returns a pointer to the original
7276 parent of the declaration DIE. Returns NULL otherwise. */
7278 static dw_die_ref
7279 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7281 dw_die_ref decl;
7282 dw_die_ref new_decl;
7283 dw_die_ref orig_parent = NULL;
7285 decl = get_AT_ref (die, DW_AT_specification);
7286 if (decl == NULL)
7287 decl = die;
7288 else
7290 unsigned ix;
7291 dw_die_ref c;
7292 dw_attr_ref a;
7294 /* The original DIE will be changed to a declaration, and must
7295 be moved to be a child of the original declaration DIE. */
7296 orig_parent = decl->die_parent;
7298 /* Copy the type node pointer from the new DIE to the original
7299 declaration DIE so we can forward references later. */
7300 decl->comdat_type_p = true;
7301 decl->die_id.die_type_node = die->die_id.die_type_node;
7303 remove_AT (die, DW_AT_specification);
7305 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7307 if (a->dw_attr != DW_AT_name
7308 && a->dw_attr != DW_AT_declaration
7309 && a->dw_attr != DW_AT_external)
7310 add_dwarf_attr (die, a);
7313 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7316 if (decl->die_parent != NULL
7317 && !is_unit_die (decl->die_parent))
7319 new_decl = copy_ancestor_tree (unit, decl, NULL);
7320 if (new_decl != NULL)
7322 remove_AT (new_decl, DW_AT_signature);
7323 add_AT_specification (die, new_decl);
7327 return orig_parent;
7330 /* Generate the skeleton ancestor tree for the given NODE, then clone
7331 the DIE and add the clone into the tree. */
7333 static void
7334 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7336 if (node->new_die != NULL)
7337 return;
7339 node->new_die = clone_as_declaration (node->old_die);
7341 if (node->parent != NULL)
7343 generate_skeleton_ancestor_tree (node->parent);
7344 add_child_die (node->parent->new_die, node->new_die);
7348 /* Generate a skeleton tree of DIEs containing any declarations that are
7349 found in the original tree. We traverse the tree looking for declaration
7350 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7352 static void
7353 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7355 skeleton_chain_node node;
7356 dw_die_ref c;
7357 dw_die_ref first;
7358 dw_die_ref prev = NULL;
7359 dw_die_ref next = NULL;
7361 node.parent = parent;
7363 first = c = parent->old_die->die_child;
7364 if (c)
7365 next = c->die_sib;
7366 if (c) do {
7367 if (prev == NULL || prev->die_sib == c)
7368 prev = c;
7369 c = next;
7370 next = (c == first ? NULL : c->die_sib);
7371 node.old_die = c;
7372 node.new_die = NULL;
7373 if (is_declaration_die (c))
7375 if (is_template_instantiation (c))
7377 /* Instantiated templates do not need to be cloned into the
7378 type unit. Just move the DIE and its children back to
7379 the skeleton tree (in the main CU). */
7380 remove_child_with_prev (c, prev);
7381 add_child_die (parent->new_die, c);
7382 c = prev;
7384 else
7386 /* Clone the existing DIE, move the original to the skeleton
7387 tree (which is in the main CU), and put the clone, with
7388 all the original's children, where the original came from
7389 (which is about to be moved to the type unit). */
7390 dw_die_ref clone = clone_die (c);
7391 move_all_children (c, clone);
7393 /* If the original has a DW_AT_object_pointer attribute,
7394 it would now point to a child DIE just moved to the
7395 cloned tree, so we need to remove that attribute from
7396 the original. */
7397 remove_AT (c, DW_AT_object_pointer);
7399 replace_child (c, clone, prev);
7400 generate_skeleton_ancestor_tree (parent);
7401 add_child_die (parent->new_die, c);
7402 node.new_die = c;
7403 c = clone;
7406 generate_skeleton_bottom_up (&node);
7407 } while (next != NULL);
7410 /* Wrapper function for generate_skeleton_bottom_up. */
7412 static dw_die_ref
7413 generate_skeleton (dw_die_ref die)
7415 skeleton_chain_node node;
7417 node.old_die = die;
7418 node.new_die = NULL;
7419 node.parent = NULL;
7421 /* If this type definition is nested inside another type,
7422 and is not an instantiation of a template, always leave
7423 at least a declaration in its place. */
7424 if (die->die_parent != NULL
7425 && is_type_die (die->die_parent)
7426 && !is_template_instantiation (die))
7427 node.new_die = clone_as_declaration (die);
7429 generate_skeleton_bottom_up (&node);
7430 return node.new_die;
7433 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7434 declaration. The original DIE is moved to a new compile unit so that
7435 existing references to it follow it to the new location. If any of the
7436 original DIE's descendants is a declaration, we need to replace the
7437 original DIE with a skeleton tree and move the declarations back into the
7438 skeleton tree. */
7440 static dw_die_ref
7441 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7442 dw_die_ref prev)
7444 dw_die_ref skeleton, orig_parent;
7446 /* Copy the declaration context to the type unit DIE. If the returned
7447 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7448 that DIE. */
7449 orig_parent = copy_declaration_context (unit, child);
7451 skeleton = generate_skeleton (child);
7452 if (skeleton == NULL)
7453 remove_child_with_prev (child, prev);
7454 else
7456 skeleton->comdat_type_p = true;
7457 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7459 /* If the original DIE was a specification, we need to put
7460 the skeleton under the parent DIE of the declaration.
7461 This leaves the original declaration in the tree, but
7462 it will be pruned later since there are no longer any
7463 references to it. */
7464 if (orig_parent != NULL)
7466 remove_child_with_prev (child, prev);
7467 add_child_die (orig_parent, skeleton);
7469 else
7470 replace_child (child, skeleton, prev);
7473 return skeleton;
7476 /* Traverse the DIE and set up additional .debug_types sections for each
7477 type worthy of being placed in a COMDAT section. */
7479 static void
7480 break_out_comdat_types (dw_die_ref die)
7482 dw_die_ref c;
7483 dw_die_ref first;
7484 dw_die_ref prev = NULL;
7485 dw_die_ref next = NULL;
7486 dw_die_ref unit = NULL;
7488 first = c = die->die_child;
7489 if (c)
7490 next = c->die_sib;
7491 if (c) do {
7492 if (prev == NULL || prev->die_sib == c)
7493 prev = c;
7494 c = next;
7495 next = (c == first ? NULL : c->die_sib);
7496 if (should_move_die_to_comdat (c))
7498 dw_die_ref replacement;
7499 comdat_type_node_ref type_node;
7501 /* Break out nested types into their own type units. */
7502 break_out_comdat_types (c);
7504 /* Create a new type unit DIE as the root for the new tree, and
7505 add it to the list of comdat types. */
7506 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7507 add_AT_unsigned (unit, DW_AT_language,
7508 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7509 type_node = ggc_cleared_alloc<comdat_type_node> ();
7510 type_node->root_die = unit;
7511 type_node->next = comdat_type_list;
7512 comdat_type_list = type_node;
7514 /* Generate the type signature. */
7515 generate_type_signature (c, type_node);
7517 /* Copy the declaration context, attributes, and children of the
7518 declaration into the new type unit DIE, then remove this DIE
7519 from the main CU (or replace it with a skeleton if necessary). */
7520 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7521 type_node->skeleton_die = replacement;
7523 /* Add the DIE to the new compunit. */
7524 add_child_die (unit, c);
7526 if (replacement != NULL)
7527 c = replacement;
7529 else if (c->die_tag == DW_TAG_namespace
7530 || c->die_tag == DW_TAG_class_type
7531 || c->die_tag == DW_TAG_structure_type
7532 || c->die_tag == DW_TAG_union_type)
7534 /* Look for nested types that can be broken out. */
7535 break_out_comdat_types (c);
7537 } while (next != NULL);
7540 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7541 Enter all the cloned children into the hash table decl_table. */
7543 static dw_die_ref
7544 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7546 dw_die_ref c;
7547 dw_die_ref clone;
7548 struct decl_table_entry *entry;
7549 decl_table_entry **slot;
7551 if (die->die_tag == DW_TAG_subprogram)
7552 clone = clone_as_declaration (die);
7553 else
7554 clone = clone_die (die);
7556 slot = decl_table->find_slot_with_hash (die,
7557 htab_hash_pointer (die), INSERT);
7559 /* Assert that DIE isn't in the hash table yet. If it would be there
7560 before, the ancestors would be necessarily there as well, therefore
7561 clone_tree_partial wouldn't be called. */
7562 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7564 entry = XCNEW (struct decl_table_entry);
7565 entry->orig = die;
7566 entry->copy = clone;
7567 *slot = entry;
7569 if (die->die_tag != DW_TAG_subprogram)
7570 FOR_EACH_CHILD (die, c,
7571 add_child_die (clone, clone_tree_partial (c, decl_table)));
7573 return clone;
7576 /* Walk the DIE and its children, looking for references to incomplete
7577 or trivial types that are unmarked (i.e., that are not in the current
7578 type_unit). */
7580 static void
7581 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7583 dw_die_ref c;
7584 dw_attr_ref a;
7585 unsigned ix;
7587 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7589 if (AT_class (a) == dw_val_class_die_ref)
7591 dw_die_ref targ = AT_ref (a);
7592 decl_table_entry **slot;
7593 struct decl_table_entry *entry;
7595 if (targ->die_mark != 0 || targ->comdat_type_p)
7596 continue;
7598 slot = decl_table->find_slot_with_hash (targ,
7599 htab_hash_pointer (targ),
7600 INSERT);
7602 if (*slot != HTAB_EMPTY_ENTRY)
7604 /* TARG has already been copied, so we just need to
7605 modify the reference to point to the copy. */
7606 entry = *slot;
7607 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7609 else
7611 dw_die_ref parent = unit;
7612 dw_die_ref copy = clone_die (targ);
7614 /* Record in DECL_TABLE that TARG has been copied.
7615 Need to do this now, before the recursive call,
7616 because DECL_TABLE may be expanded and SLOT
7617 would no longer be a valid pointer. */
7618 entry = XCNEW (struct decl_table_entry);
7619 entry->orig = targ;
7620 entry->copy = copy;
7621 *slot = entry;
7623 /* If TARG is not a declaration DIE, we need to copy its
7624 children. */
7625 if (!is_declaration_die (targ))
7627 FOR_EACH_CHILD (
7628 targ, c,
7629 add_child_die (copy,
7630 clone_tree_partial (c, decl_table)));
7633 /* Make sure the cloned tree is marked as part of the
7634 type unit. */
7635 mark_dies (copy);
7637 /* If TARG has surrounding context, copy its ancestor tree
7638 into the new type unit. */
7639 if (targ->die_parent != NULL
7640 && !is_unit_die (targ->die_parent))
7641 parent = copy_ancestor_tree (unit, targ->die_parent,
7642 decl_table);
7644 add_child_die (parent, copy);
7645 a->dw_attr_val.v.val_die_ref.die = copy;
7647 /* Make sure the newly-copied DIE is walked. If it was
7648 installed in a previously-added context, it won't
7649 get visited otherwise. */
7650 if (parent != unit)
7652 /* Find the highest point of the newly-added tree,
7653 mark each node along the way, and walk from there. */
7654 parent->die_mark = 1;
7655 while (parent->die_parent
7656 && parent->die_parent->die_mark == 0)
7658 parent = parent->die_parent;
7659 parent->die_mark = 1;
7661 copy_decls_walk (unit, parent, decl_table);
7667 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7670 /* Copy declarations for "unworthy" types into the new comdat section.
7671 Incomplete types, modified types, and certain other types aren't broken
7672 out into comdat sections of their own, so they don't have a signature,
7673 and we need to copy the declaration into the same section so that we
7674 don't have an external reference. */
7676 static void
7677 copy_decls_for_unworthy_types (dw_die_ref unit)
7679 mark_dies (unit);
7680 decl_hash_type decl_table (10);
7681 copy_decls_walk (unit, unit, &decl_table);
7682 unmark_dies (unit);
7685 /* Traverse the DIE and add a sibling attribute if it may have the
7686 effect of speeding up access to siblings. To save some space,
7687 avoid generating sibling attributes for DIE's without children. */
7689 static void
7690 add_sibling_attributes (dw_die_ref die)
7692 dw_die_ref c;
7694 if (! die->die_child)
7695 return;
7697 if (die->die_parent && die != die->die_parent->die_child)
7698 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7700 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7703 /* Output all location lists for the DIE and its children. */
7705 static void
7706 output_location_lists (dw_die_ref die)
7708 dw_die_ref c;
7709 dw_attr_ref a;
7710 unsigned ix;
7712 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7713 if (AT_class (a) == dw_val_class_loc_list)
7714 output_loc_list (AT_loc_list (a));
7716 FOR_EACH_CHILD (die, c, output_location_lists (c));
7719 /* We want to limit the number of external references, because they are
7720 larger than local references: a relocation takes multiple words, and
7721 even a sig8 reference is always eight bytes, whereas a local reference
7722 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7723 So if we encounter multiple external references to the same type DIE, we
7724 make a local typedef stub for it and redirect all references there.
7726 This is the element of the hash table for keeping track of these
7727 references. */
7729 struct external_ref
7731 dw_die_ref type;
7732 dw_die_ref stub;
7733 unsigned n_refs;
7736 /* Hashtable helpers. */
7738 struct external_ref_hasher : typed_free_remove <external_ref>
7740 typedef external_ref value_type;
7741 typedef external_ref compare_type;
7742 static inline hashval_t hash (const value_type *);
7743 static inline bool equal (const value_type *, const compare_type *);
7746 inline hashval_t
7747 external_ref_hasher::hash (const value_type *r)
7749 dw_die_ref die = r->type;
7750 hashval_t h = 0;
7752 /* We can't use the address of the DIE for hashing, because
7753 that will make the order of the stub DIEs non-deterministic. */
7754 if (! die->comdat_type_p)
7755 /* We have a symbol; use it to compute a hash. */
7756 h = htab_hash_string (die->die_id.die_symbol);
7757 else
7759 /* We have a type signature; use a subset of the bits as the hash.
7760 The 8-byte signature is at least as large as hashval_t. */
7761 comdat_type_node_ref type_node = die->die_id.die_type_node;
7762 memcpy (&h, type_node->signature, sizeof (h));
7764 return h;
7767 inline bool
7768 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7770 return r1->type == r2->type;
7773 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7775 /* Return a pointer to the external_ref for references to DIE. */
7777 static struct external_ref *
7778 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7780 struct external_ref ref, *ref_p;
7781 external_ref **slot;
7783 ref.type = die;
7784 slot = map->find_slot (&ref, INSERT);
7785 if (*slot != HTAB_EMPTY_ENTRY)
7786 return *slot;
7788 ref_p = XCNEW (struct external_ref);
7789 ref_p->type = die;
7790 *slot = ref_p;
7791 return ref_p;
7794 /* Subroutine of optimize_external_refs, below.
7796 If we see a type skeleton, record it as our stub. If we see external
7797 references, remember how many we've seen. */
7799 static void
7800 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7802 dw_die_ref c;
7803 dw_attr_ref a;
7804 unsigned ix;
7805 struct external_ref *ref_p;
7807 if (is_type_die (die)
7808 && (c = get_AT_ref (die, DW_AT_signature)))
7810 /* This is a local skeleton; use it for local references. */
7811 ref_p = lookup_external_ref (map, c);
7812 ref_p->stub = die;
7815 /* Scan the DIE references, and remember any that refer to DIEs from
7816 other CUs (i.e. those which are not marked). */
7817 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7818 if (AT_class (a) == dw_val_class_die_ref
7819 && (c = AT_ref (a))->die_mark == 0
7820 && is_type_die (c))
7822 ref_p = lookup_external_ref (map, c);
7823 ref_p->n_refs++;
7826 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7829 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7830 points to an external_ref, DATA is the CU we're processing. If we don't
7831 already have a local stub, and we have multiple refs, build a stub. */
7834 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7836 struct external_ref *ref_p = *slot;
7838 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7840 /* We have multiple references to this type, so build a small stub.
7841 Both of these forms are a bit dodgy from the perspective of the
7842 DWARF standard, since technically they should have names. */
7843 dw_die_ref cu = data;
7844 dw_die_ref type = ref_p->type;
7845 dw_die_ref stub = NULL;
7847 if (type->comdat_type_p)
7849 /* If we refer to this type via sig8, use AT_signature. */
7850 stub = new_die (type->die_tag, cu, NULL_TREE);
7851 add_AT_die_ref (stub, DW_AT_signature, type);
7853 else
7855 /* Otherwise, use a typedef with no name. */
7856 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7857 add_AT_die_ref (stub, DW_AT_type, type);
7860 stub->die_mark++;
7861 ref_p->stub = stub;
7863 return 1;
7866 /* DIE is a unit; look through all the DIE references to see if there are
7867 any external references to types, and if so, create local stubs for
7868 them which will be applied in build_abbrev_table. This is useful because
7869 references to local DIEs are smaller. */
7871 static external_ref_hash_type *
7872 optimize_external_refs (dw_die_ref die)
7874 external_ref_hash_type *map = new external_ref_hash_type (10);
7875 optimize_external_refs_1 (die, map);
7876 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7877 return map;
7880 /* The format of each DIE (and its attribute value pairs) is encoded in an
7881 abbreviation table. This routine builds the abbreviation table and assigns
7882 a unique abbreviation id for each abbreviation entry. The children of each
7883 die are visited recursively. */
7885 static void
7886 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7888 unsigned long abbrev_id;
7889 unsigned int n_alloc;
7890 dw_die_ref c;
7891 dw_attr_ref a;
7892 unsigned ix;
7894 /* Scan the DIE references, and replace any that refer to
7895 DIEs from other CUs (i.e. those which are not marked) with
7896 the local stubs we built in optimize_external_refs. */
7897 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7898 if (AT_class (a) == dw_val_class_die_ref
7899 && (c = AT_ref (a))->die_mark == 0)
7901 struct external_ref *ref_p;
7902 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7904 ref_p = lookup_external_ref (extern_map, c);
7905 if (ref_p->stub && ref_p->stub != die)
7906 change_AT_die_ref (a, ref_p->stub);
7907 else
7908 /* We aren't changing this reference, so mark it external. */
7909 set_AT_ref_external (a, 1);
7912 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7914 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7915 dw_attr_ref die_a, abbrev_a;
7916 unsigned ix;
7917 bool ok = true;
7919 if (abbrev->die_tag != die->die_tag)
7920 continue;
7921 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7922 continue;
7924 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7925 continue;
7927 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7929 abbrev_a = &(*abbrev->die_attr)[ix];
7930 if ((abbrev_a->dw_attr != die_a->dw_attr)
7931 || (value_format (abbrev_a) != value_format (die_a)))
7933 ok = false;
7934 break;
7937 if (ok)
7938 break;
7941 if (abbrev_id >= abbrev_die_table_in_use)
7943 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7945 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7946 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7947 n_alloc);
7949 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7950 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7951 abbrev_die_table_allocated = n_alloc;
7954 ++abbrev_die_table_in_use;
7955 abbrev_die_table[abbrev_id] = die;
7958 die->die_abbrev = abbrev_id;
7959 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7962 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7964 static int
7965 constant_size (unsigned HOST_WIDE_INT value)
7967 int log;
7969 if (value == 0)
7970 log = 0;
7971 else
7972 log = floor_log2 (value);
7974 log = log / 8;
7975 log = 1 << (floor_log2 (log) + 1);
7977 return log;
7980 /* Return the size of a DIE as it is represented in the
7981 .debug_info section. */
7983 static unsigned long
7984 size_of_die (dw_die_ref die)
7986 unsigned long size = 0;
7987 dw_attr_ref a;
7988 unsigned ix;
7989 enum dwarf_form form;
7991 size += size_of_uleb128 (die->die_abbrev);
7992 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7994 switch (AT_class (a))
7996 case dw_val_class_addr:
7997 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7999 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8000 size += size_of_uleb128 (AT_index (a));
8002 else
8003 size += DWARF2_ADDR_SIZE;
8004 break;
8005 case dw_val_class_offset:
8006 size += DWARF_OFFSET_SIZE;
8007 break;
8008 case dw_val_class_loc:
8010 unsigned long lsize = size_of_locs (AT_loc (a));
8012 /* Block length. */
8013 if (dwarf_version >= 4)
8014 size += size_of_uleb128 (lsize);
8015 else
8016 size += constant_size (lsize);
8017 size += lsize;
8019 break;
8020 case dw_val_class_loc_list:
8021 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8023 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8024 size += size_of_uleb128 (AT_index (a));
8026 else
8027 size += DWARF_OFFSET_SIZE;
8028 break;
8029 case dw_val_class_range_list:
8030 size += DWARF_OFFSET_SIZE;
8031 break;
8032 case dw_val_class_const:
8033 size += size_of_sleb128 (AT_int (a));
8034 break;
8035 case dw_val_class_unsigned_const:
8037 int csize = constant_size (AT_unsigned (a));
8038 if (dwarf_version == 3
8039 && a->dw_attr == DW_AT_data_member_location
8040 && csize >= 4)
8041 size += size_of_uleb128 (AT_unsigned (a));
8042 else
8043 size += csize;
8045 break;
8046 case dw_val_class_const_double:
8047 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8048 if (HOST_BITS_PER_WIDE_INT >= 64)
8049 size++; /* block */
8050 break;
8051 case dw_val_class_wide_int:
8052 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8053 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8054 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8055 > 64)
8056 size++; /* block */
8057 break;
8058 case dw_val_class_vec:
8059 size += constant_size (a->dw_attr_val.v.val_vec.length
8060 * a->dw_attr_val.v.val_vec.elt_size)
8061 + a->dw_attr_val.v.val_vec.length
8062 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8063 break;
8064 case dw_val_class_flag:
8065 if (dwarf_version >= 4)
8066 /* Currently all add_AT_flag calls pass in 1 as last argument,
8067 so DW_FORM_flag_present can be used. If that ever changes,
8068 we'll need to use DW_FORM_flag and have some optimization
8069 in build_abbrev_table that will change those to
8070 DW_FORM_flag_present if it is set to 1 in all DIEs using
8071 the same abbrev entry. */
8072 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8073 else
8074 size += 1;
8075 break;
8076 case dw_val_class_die_ref:
8077 if (AT_ref_external (a))
8079 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8080 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8081 is sized by target address length, whereas in DWARF3
8082 it's always sized as an offset. */
8083 if (use_debug_types)
8084 size += DWARF_TYPE_SIGNATURE_SIZE;
8085 else if (dwarf_version == 2)
8086 size += DWARF2_ADDR_SIZE;
8087 else
8088 size += DWARF_OFFSET_SIZE;
8090 else
8091 size += DWARF_OFFSET_SIZE;
8092 break;
8093 case dw_val_class_fde_ref:
8094 size += DWARF_OFFSET_SIZE;
8095 break;
8096 case dw_val_class_lbl_id:
8097 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8099 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8100 size += size_of_uleb128 (AT_index (a));
8102 else
8103 size += DWARF2_ADDR_SIZE;
8104 break;
8105 case dw_val_class_lineptr:
8106 case dw_val_class_macptr:
8107 size += DWARF_OFFSET_SIZE;
8108 break;
8109 case dw_val_class_str:
8110 form = AT_string_form (a);
8111 if (form == DW_FORM_strp)
8112 size += DWARF_OFFSET_SIZE;
8113 else if (form == DW_FORM_GNU_str_index)
8114 size += size_of_uleb128 (AT_index (a));
8115 else
8116 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8117 break;
8118 case dw_val_class_file:
8119 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8120 break;
8121 case dw_val_class_data8:
8122 size += 8;
8123 break;
8124 case dw_val_class_vms_delta:
8125 size += DWARF_OFFSET_SIZE;
8126 break;
8127 case dw_val_class_high_pc:
8128 size += DWARF2_ADDR_SIZE;
8129 break;
8130 default:
8131 gcc_unreachable ();
8135 return size;
8138 /* Size the debugging information associated with a given DIE. Visits the
8139 DIE's children recursively. Updates the global variable next_die_offset, on
8140 each time through. Uses the current value of next_die_offset to update the
8141 die_offset field in each DIE. */
8143 static void
8144 calc_die_sizes (dw_die_ref die)
8146 dw_die_ref c;
8148 gcc_assert (die->die_offset == 0
8149 || (unsigned long int) die->die_offset == next_die_offset);
8150 die->die_offset = next_die_offset;
8151 next_die_offset += size_of_die (die);
8153 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8155 if (die->die_child != NULL)
8156 /* Count the null byte used to terminate sibling lists. */
8157 next_die_offset += 1;
8160 /* Size just the base type children at the start of the CU.
8161 This is needed because build_abbrev needs to size locs
8162 and sizing of type based stack ops needs to know die_offset
8163 values for the base types. */
8165 static void
8166 calc_base_type_die_sizes (void)
8168 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8169 unsigned int i;
8170 dw_die_ref base_type;
8171 #if ENABLE_ASSERT_CHECKING
8172 dw_die_ref prev = comp_unit_die ()->die_child;
8173 #endif
8175 die_offset += size_of_die (comp_unit_die ());
8176 for (i = 0; base_types.iterate (i, &base_type); i++)
8178 #if ENABLE_ASSERT_CHECKING
8179 gcc_assert (base_type->die_offset == 0
8180 && prev->die_sib == base_type
8181 && base_type->die_child == NULL
8182 && base_type->die_abbrev);
8183 prev = base_type;
8184 #endif
8185 base_type->die_offset = die_offset;
8186 die_offset += size_of_die (base_type);
8190 /* Set the marks for a die and its children. We do this so
8191 that we know whether or not a reference needs to use FORM_ref_addr; only
8192 DIEs in the same CU will be marked. We used to clear out the offset
8193 and use that as the flag, but ran into ordering problems. */
8195 static void
8196 mark_dies (dw_die_ref die)
8198 dw_die_ref c;
8200 gcc_assert (!die->die_mark);
8202 die->die_mark = 1;
8203 FOR_EACH_CHILD (die, c, mark_dies (c));
8206 /* Clear the marks for a die and its children. */
8208 static void
8209 unmark_dies (dw_die_ref die)
8211 dw_die_ref c;
8213 if (! use_debug_types)
8214 gcc_assert (die->die_mark);
8216 die->die_mark = 0;
8217 FOR_EACH_CHILD (die, c, unmark_dies (c));
8220 /* Clear the marks for a die, its children and referred dies. */
8222 static void
8223 unmark_all_dies (dw_die_ref die)
8225 dw_die_ref c;
8226 dw_attr_ref a;
8227 unsigned ix;
8229 if (!die->die_mark)
8230 return;
8231 die->die_mark = 0;
8233 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8235 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8236 if (AT_class (a) == dw_val_class_die_ref)
8237 unmark_all_dies (AT_ref (a));
8240 /* Calculate if the entry should appear in the final output file. It may be
8241 from a pruned a type. */
8243 static bool
8244 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8246 /* By limiting gnu pubnames to definitions only, gold can generate a
8247 gdb index without entries for declarations, which don't include
8248 enough information to be useful. */
8249 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8250 return false;
8252 if (table == pubname_table)
8254 /* Enumerator names are part of the pubname table, but the
8255 parent DW_TAG_enumeration_type die may have been pruned.
8256 Don't output them if that is the case. */
8257 if (p->die->die_tag == DW_TAG_enumerator &&
8258 (p->die->die_parent == NULL
8259 || !p->die->die_parent->die_perennial_p))
8260 return false;
8262 /* Everything else in the pubname table is included. */
8263 return true;
8266 /* The pubtypes table shouldn't include types that have been
8267 pruned. */
8268 return (p->die->die_offset != 0
8269 || !flag_eliminate_unused_debug_types);
8272 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8273 generated for the compilation unit. */
8275 static unsigned long
8276 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8278 unsigned long size;
8279 unsigned i;
8280 pubname_ref p;
8281 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8283 size = DWARF_PUBNAMES_HEADER_SIZE;
8284 FOR_EACH_VEC_ELT (*names, i, p)
8285 if (include_pubname_in_output (names, p))
8286 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8288 size += DWARF_OFFSET_SIZE;
8289 return size;
8292 /* Return the size of the information in the .debug_aranges section. */
8294 static unsigned long
8295 size_of_aranges (void)
8297 unsigned long size;
8299 size = DWARF_ARANGES_HEADER_SIZE;
8301 /* Count the address/length pair for this compilation unit. */
8302 if (text_section_used)
8303 size += 2 * DWARF2_ADDR_SIZE;
8304 if (cold_text_section_used)
8305 size += 2 * DWARF2_ADDR_SIZE;
8306 if (have_multiple_function_sections)
8308 unsigned fde_idx;
8309 dw_fde_ref fde;
8311 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8313 if (DECL_IGNORED_P (fde->decl))
8314 continue;
8315 if (!fde->in_std_section)
8316 size += 2 * DWARF2_ADDR_SIZE;
8317 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8318 size += 2 * DWARF2_ADDR_SIZE;
8322 /* Count the two zero words used to terminated the address range table. */
8323 size += 2 * DWARF2_ADDR_SIZE;
8324 return size;
8327 /* Select the encoding of an attribute value. */
8329 static enum dwarf_form
8330 value_format (dw_attr_ref a)
8332 switch (AT_class (a))
8334 case dw_val_class_addr:
8335 /* Only very few attributes allow DW_FORM_addr. */
8336 switch (a->dw_attr)
8338 case DW_AT_low_pc:
8339 case DW_AT_high_pc:
8340 case DW_AT_entry_pc:
8341 case DW_AT_trampoline:
8342 return (AT_index (a) == NOT_INDEXED
8343 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8344 default:
8345 break;
8347 switch (DWARF2_ADDR_SIZE)
8349 case 1:
8350 return DW_FORM_data1;
8351 case 2:
8352 return DW_FORM_data2;
8353 case 4:
8354 return DW_FORM_data4;
8355 case 8:
8356 return DW_FORM_data8;
8357 default:
8358 gcc_unreachable ();
8360 case dw_val_class_range_list:
8361 case dw_val_class_loc_list:
8362 if (dwarf_version >= 4)
8363 return DW_FORM_sec_offset;
8364 /* FALLTHRU */
8365 case dw_val_class_vms_delta:
8366 case dw_val_class_offset:
8367 switch (DWARF_OFFSET_SIZE)
8369 case 4:
8370 return DW_FORM_data4;
8371 case 8:
8372 return DW_FORM_data8;
8373 default:
8374 gcc_unreachable ();
8376 case dw_val_class_loc:
8377 if (dwarf_version >= 4)
8378 return DW_FORM_exprloc;
8379 switch (constant_size (size_of_locs (AT_loc (a))))
8381 case 1:
8382 return DW_FORM_block1;
8383 case 2:
8384 return DW_FORM_block2;
8385 case 4:
8386 return DW_FORM_block4;
8387 default:
8388 gcc_unreachable ();
8390 case dw_val_class_const:
8391 return DW_FORM_sdata;
8392 case dw_val_class_unsigned_const:
8393 switch (constant_size (AT_unsigned (a)))
8395 case 1:
8396 return DW_FORM_data1;
8397 case 2:
8398 return DW_FORM_data2;
8399 case 4:
8400 /* In DWARF3 DW_AT_data_member_location with
8401 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8402 constant, so we need to use DW_FORM_udata if we need
8403 a large constant. */
8404 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8405 return DW_FORM_udata;
8406 return DW_FORM_data4;
8407 case 8:
8408 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8409 return DW_FORM_udata;
8410 return DW_FORM_data8;
8411 default:
8412 gcc_unreachable ();
8414 case dw_val_class_const_double:
8415 switch (HOST_BITS_PER_WIDE_INT)
8417 case 8:
8418 return DW_FORM_data2;
8419 case 16:
8420 return DW_FORM_data4;
8421 case 32:
8422 return DW_FORM_data8;
8423 case 64:
8424 default:
8425 return DW_FORM_block1;
8427 case dw_val_class_wide_int:
8428 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8430 case 8:
8431 return DW_FORM_data1;
8432 case 16:
8433 return DW_FORM_data2;
8434 case 32:
8435 return DW_FORM_data4;
8436 case 64:
8437 return DW_FORM_data8;
8438 default:
8439 return DW_FORM_block1;
8441 case dw_val_class_vec:
8442 switch (constant_size (a->dw_attr_val.v.val_vec.length
8443 * a->dw_attr_val.v.val_vec.elt_size))
8445 case 1:
8446 return DW_FORM_block1;
8447 case 2:
8448 return DW_FORM_block2;
8449 case 4:
8450 return DW_FORM_block4;
8451 default:
8452 gcc_unreachable ();
8454 case dw_val_class_flag:
8455 if (dwarf_version >= 4)
8457 /* Currently all add_AT_flag calls pass in 1 as last argument,
8458 so DW_FORM_flag_present can be used. If that ever changes,
8459 we'll need to use DW_FORM_flag and have some optimization
8460 in build_abbrev_table that will change those to
8461 DW_FORM_flag_present if it is set to 1 in all DIEs using
8462 the same abbrev entry. */
8463 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8464 return DW_FORM_flag_present;
8466 return DW_FORM_flag;
8467 case dw_val_class_die_ref:
8468 if (AT_ref_external (a))
8469 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8470 else
8471 return DW_FORM_ref;
8472 case dw_val_class_fde_ref:
8473 return DW_FORM_data;
8474 case dw_val_class_lbl_id:
8475 return (AT_index (a) == NOT_INDEXED
8476 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8477 case dw_val_class_lineptr:
8478 case dw_val_class_macptr:
8479 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8480 case dw_val_class_str:
8481 return AT_string_form (a);
8482 case dw_val_class_file:
8483 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8485 case 1:
8486 return DW_FORM_data1;
8487 case 2:
8488 return DW_FORM_data2;
8489 case 4:
8490 return DW_FORM_data4;
8491 default:
8492 gcc_unreachable ();
8495 case dw_val_class_data8:
8496 return DW_FORM_data8;
8498 case dw_val_class_high_pc:
8499 switch (DWARF2_ADDR_SIZE)
8501 case 1:
8502 return DW_FORM_data1;
8503 case 2:
8504 return DW_FORM_data2;
8505 case 4:
8506 return DW_FORM_data4;
8507 case 8:
8508 return DW_FORM_data8;
8509 default:
8510 gcc_unreachable ();
8513 default:
8514 gcc_unreachable ();
8518 /* Output the encoding of an attribute value. */
8520 static void
8521 output_value_format (dw_attr_ref a)
8523 enum dwarf_form form = value_format (a);
8525 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8528 /* Given a die and id, produce the appropriate abbreviations. */
8530 static void
8531 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8533 unsigned ix;
8534 dw_attr_ref a_attr;
8536 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8537 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8538 dwarf_tag_name (abbrev->die_tag));
8540 if (abbrev->die_child != NULL)
8541 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8542 else
8543 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8545 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8547 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8548 dwarf_attr_name (a_attr->dw_attr));
8549 output_value_format (a_attr);
8552 dw2_asm_output_data (1, 0, NULL);
8553 dw2_asm_output_data (1, 0, NULL);
8557 /* Output the .debug_abbrev section which defines the DIE abbreviation
8558 table. */
8560 static void
8561 output_abbrev_section (void)
8563 unsigned long abbrev_id;
8565 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8566 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8568 /* Terminate the table. */
8569 dw2_asm_output_data (1, 0, NULL);
8572 /* Output a symbol we can use to refer to this DIE from another CU. */
8574 static inline void
8575 output_die_symbol (dw_die_ref die)
8577 const char *sym = die->die_id.die_symbol;
8579 gcc_assert (!die->comdat_type_p);
8581 if (sym == 0)
8582 return;
8584 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8585 /* We make these global, not weak; if the target doesn't support
8586 .linkonce, it doesn't support combining the sections, so debugging
8587 will break. */
8588 targetm.asm_out.globalize_label (asm_out_file, sym);
8590 ASM_OUTPUT_LABEL (asm_out_file, sym);
8593 /* Return a new location list, given the begin and end range, and the
8594 expression. */
8596 static inline dw_loc_list_ref
8597 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8598 const char *section)
8600 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8602 retlist->begin = begin;
8603 retlist->begin_entry = NULL;
8604 retlist->end = end;
8605 retlist->expr = expr;
8606 retlist->section = section;
8608 return retlist;
8611 /* Generate a new internal symbol for this location list node, if it
8612 hasn't got one yet. */
8614 static inline void
8615 gen_llsym (dw_loc_list_ref list)
8617 gcc_assert (!list->ll_symbol);
8618 list->ll_symbol = gen_internal_sym ("LLST");
8621 /* Output the location list given to us. */
8623 static void
8624 output_loc_list (dw_loc_list_ref list_head)
8626 dw_loc_list_ref curr = list_head;
8628 if (list_head->emitted)
8629 return;
8630 list_head->emitted = true;
8632 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8634 /* Walk the location list, and output each range + expression. */
8635 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8637 unsigned long size;
8638 /* Don't output an entry that starts and ends at the same address. */
8639 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8640 continue;
8641 size = size_of_locs (curr->expr);
8642 /* If the expression is too large, drop it on the floor. We could
8643 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8644 in the expression, but >= 64KB expressions for a single value
8645 in a single range are unlikely very useful. */
8646 if (size > 0xffff)
8647 continue;
8648 if (dwarf_split_debug_info)
8650 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8651 "Location list start/length entry (%s)",
8652 list_head->ll_symbol);
8653 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8654 "Location list range start index (%s)",
8655 curr->begin);
8656 /* The length field is 4 bytes. If we ever need to support
8657 an 8-byte length, we can add a new DW_LLE code or fall back
8658 to DW_LLE_GNU_start_end_entry. */
8659 dw2_asm_output_delta (4, curr->end, curr->begin,
8660 "Location list range length (%s)",
8661 list_head->ll_symbol);
8663 else if (!have_multiple_function_sections)
8665 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8666 "Location list begin address (%s)",
8667 list_head->ll_symbol);
8668 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8669 "Location list end address (%s)",
8670 list_head->ll_symbol);
8672 else
8674 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8675 "Location list begin address (%s)",
8676 list_head->ll_symbol);
8677 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8678 "Location list end address (%s)",
8679 list_head->ll_symbol);
8682 /* Output the block length for this list of location operations. */
8683 gcc_assert (size <= 0xffff);
8684 dw2_asm_output_data (2, size, "%s", "Location expression size");
8686 output_loc_sequence (curr->expr, -1);
8689 if (dwarf_split_debug_info)
8690 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8691 "Location list terminator (%s)",
8692 list_head->ll_symbol);
8693 else
8695 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8696 "Location list terminator begin (%s)",
8697 list_head->ll_symbol);
8698 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8699 "Location list terminator end (%s)",
8700 list_head->ll_symbol);
8704 /* Output a range_list offset into the debug_range section. Emit a
8705 relocated reference if val_entry is NULL, otherwise, emit an
8706 indirect reference. */
8708 static void
8709 output_range_list_offset (dw_attr_ref a)
8711 const char *name = dwarf_attr_name (a->dw_attr);
8713 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8715 char *p = strchr (ranges_section_label, '\0');
8716 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8717 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8718 debug_ranges_section, "%s", name);
8719 *p = '\0';
8721 else
8722 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8723 "%s (offset from %s)", name, ranges_section_label);
8726 /* Output the offset into the debug_loc section. */
8728 static void
8729 output_loc_list_offset (dw_attr_ref a)
8731 char *sym = AT_loc_list (a)->ll_symbol;
8733 gcc_assert (sym);
8734 if (dwarf_split_debug_info)
8735 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8736 "%s", dwarf_attr_name (a->dw_attr));
8737 else
8738 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8739 "%s", dwarf_attr_name (a->dw_attr));
8742 /* Output an attribute's index or value appropriately. */
8744 static void
8745 output_attr_index_or_value (dw_attr_ref a)
8747 const char *name = dwarf_attr_name (a->dw_attr);
8749 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8751 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8752 return;
8754 switch (AT_class (a))
8756 case dw_val_class_addr:
8757 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8758 break;
8759 case dw_val_class_high_pc:
8760 case dw_val_class_lbl_id:
8761 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8762 break;
8763 case dw_val_class_loc_list:
8764 output_loc_list_offset (a);
8765 break;
8766 default:
8767 gcc_unreachable ();
8771 /* Output a type signature. */
8773 static inline void
8774 output_signature (const char *sig, const char *name)
8776 int i;
8778 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8779 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8782 /* Output the DIE and its attributes. Called recursively to generate
8783 the definitions of each child DIE. */
8785 static void
8786 output_die (dw_die_ref die)
8788 dw_attr_ref a;
8789 dw_die_ref c;
8790 unsigned long size;
8791 unsigned ix;
8793 /* If someone in another CU might refer to us, set up a symbol for
8794 them to point to. */
8795 if (! die->comdat_type_p && die->die_id.die_symbol)
8796 output_die_symbol (die);
8798 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8799 (unsigned long)die->die_offset,
8800 dwarf_tag_name (die->die_tag));
8802 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8804 const char *name = dwarf_attr_name (a->dw_attr);
8806 switch (AT_class (a))
8808 case dw_val_class_addr:
8809 output_attr_index_or_value (a);
8810 break;
8812 case dw_val_class_offset:
8813 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8814 "%s", name);
8815 break;
8817 case dw_val_class_range_list:
8818 output_range_list_offset (a);
8819 break;
8821 case dw_val_class_loc:
8822 size = size_of_locs (AT_loc (a));
8824 /* Output the block length for this list of location operations. */
8825 if (dwarf_version >= 4)
8826 dw2_asm_output_data_uleb128 (size, "%s", name);
8827 else
8828 dw2_asm_output_data (constant_size (size), size, "%s", name);
8830 output_loc_sequence (AT_loc (a), -1);
8831 break;
8833 case dw_val_class_const:
8834 /* ??? It would be slightly more efficient to use a scheme like is
8835 used for unsigned constants below, but gdb 4.x does not sign
8836 extend. Gdb 5.x does sign extend. */
8837 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8838 break;
8840 case dw_val_class_unsigned_const:
8842 int csize = constant_size (AT_unsigned (a));
8843 if (dwarf_version == 3
8844 && a->dw_attr == DW_AT_data_member_location
8845 && csize >= 4)
8846 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8847 else
8848 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8850 break;
8852 case dw_val_class_const_double:
8854 unsigned HOST_WIDE_INT first, second;
8856 if (HOST_BITS_PER_WIDE_INT >= 64)
8857 dw2_asm_output_data (1,
8858 HOST_BITS_PER_DOUBLE_INT
8859 / HOST_BITS_PER_CHAR,
8860 NULL);
8862 if (WORDS_BIG_ENDIAN)
8864 first = a->dw_attr_val.v.val_double.high;
8865 second = a->dw_attr_val.v.val_double.low;
8867 else
8869 first = a->dw_attr_val.v.val_double.low;
8870 second = a->dw_attr_val.v.val_double.high;
8873 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8874 first, "%s", name);
8875 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8876 second, NULL);
8878 break;
8880 case dw_val_class_wide_int:
8882 int i;
8883 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8884 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8885 if (len * HOST_BITS_PER_WIDE_INT > 64)
8886 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8887 NULL);
8889 if (WORDS_BIG_ENDIAN)
8890 for (i = len - 1; i >= 0; --i)
8892 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8893 name);
8894 name = NULL;
8896 else
8897 for (i = 0; i < len; ++i)
8899 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8900 name);
8901 name = NULL;
8904 break;
8906 case dw_val_class_vec:
8908 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8909 unsigned int len = a->dw_attr_val.v.val_vec.length;
8910 unsigned int i;
8911 unsigned char *p;
8913 dw2_asm_output_data (constant_size (len * elt_size),
8914 len * elt_size, "%s", name);
8915 if (elt_size > sizeof (HOST_WIDE_INT))
8917 elt_size /= 2;
8918 len *= 2;
8920 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8921 i < len;
8922 i++, p += elt_size)
8923 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8924 "fp or vector constant word %u", i);
8925 break;
8928 case dw_val_class_flag:
8929 if (dwarf_version >= 4)
8931 /* Currently all add_AT_flag calls pass in 1 as last argument,
8932 so DW_FORM_flag_present can be used. If that ever changes,
8933 we'll need to use DW_FORM_flag and have some optimization
8934 in build_abbrev_table that will change those to
8935 DW_FORM_flag_present if it is set to 1 in all DIEs using
8936 the same abbrev entry. */
8937 gcc_assert (AT_flag (a) == 1);
8938 if (flag_debug_asm)
8939 fprintf (asm_out_file, "\t\t\t%s %s\n",
8940 ASM_COMMENT_START, name);
8941 break;
8943 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8944 break;
8946 case dw_val_class_loc_list:
8947 output_attr_index_or_value (a);
8948 break;
8950 case dw_val_class_die_ref:
8951 if (AT_ref_external (a))
8953 if (AT_ref (a)->comdat_type_p)
8955 comdat_type_node_ref type_node =
8956 AT_ref (a)->die_id.die_type_node;
8958 gcc_assert (type_node);
8959 output_signature (type_node->signature, name);
8961 else
8963 const char *sym = AT_ref (a)->die_id.die_symbol;
8964 int size;
8966 gcc_assert (sym);
8967 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8968 length, whereas in DWARF3 it's always sized as an
8969 offset. */
8970 if (dwarf_version == 2)
8971 size = DWARF2_ADDR_SIZE;
8972 else
8973 size = DWARF_OFFSET_SIZE;
8974 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8975 name);
8978 else
8980 gcc_assert (AT_ref (a)->die_offset);
8981 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8982 "%s", name);
8984 break;
8986 case dw_val_class_fde_ref:
8988 char l1[20];
8990 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8991 a->dw_attr_val.v.val_fde_index * 2);
8992 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8993 "%s", name);
8995 break;
8997 case dw_val_class_vms_delta:
8998 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8999 AT_vms_delta2 (a), AT_vms_delta1 (a),
9000 "%s", name);
9001 break;
9003 case dw_val_class_lbl_id:
9004 output_attr_index_or_value (a);
9005 break;
9007 case dw_val_class_lineptr:
9008 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9009 debug_line_section, "%s", name);
9010 break;
9012 case dw_val_class_macptr:
9013 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9014 debug_macinfo_section, "%s", name);
9015 break;
9017 case dw_val_class_str:
9018 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9019 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9020 a->dw_attr_val.v.val_str->label,
9021 debug_str_section,
9022 "%s: \"%s\"", name, AT_string (a));
9023 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9024 dw2_asm_output_data_uleb128 (AT_index (a),
9025 "%s: \"%s\"", name, AT_string (a));
9026 else
9027 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9028 break;
9030 case dw_val_class_file:
9032 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9034 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9035 a->dw_attr_val.v.val_file->filename);
9036 break;
9039 case dw_val_class_data8:
9041 int i;
9043 for (i = 0; i < 8; i++)
9044 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9045 i == 0 ? "%s" : NULL, name);
9046 break;
9049 case dw_val_class_high_pc:
9050 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9051 get_AT_low_pc (die), "DW_AT_high_pc");
9052 break;
9054 default:
9055 gcc_unreachable ();
9059 FOR_EACH_CHILD (die, c, output_die (c));
9061 /* Add null byte to terminate sibling list. */
9062 if (die->die_child != NULL)
9063 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9064 (unsigned long) die->die_offset);
9067 /* Output the compilation unit that appears at the beginning of the
9068 .debug_info section, and precedes the DIE descriptions. */
9070 static void
9071 output_compilation_unit_header (void)
9073 /* We don't support actual DWARFv5 units yet, we just use some
9074 DWARFv5 draft DIE tags in DWARFv4 format. */
9075 int ver = dwarf_version < 5 ? dwarf_version : 4;
9077 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9078 dw2_asm_output_data (4, 0xffffffff,
9079 "Initial length escape value indicating 64-bit DWARF extension");
9080 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9081 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9082 "Length of Compilation Unit Info");
9083 dw2_asm_output_data (2, ver, "DWARF version number");
9084 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9085 debug_abbrev_section,
9086 "Offset Into Abbrev. Section");
9087 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9090 /* Output the compilation unit DIE and its children. */
9092 static void
9093 output_comp_unit (dw_die_ref die, int output_if_empty)
9095 const char *secname, *oldsym;
9096 char *tmp;
9098 /* Unless we are outputting main CU, we may throw away empty ones. */
9099 if (!output_if_empty && die->die_child == NULL)
9100 return;
9102 /* Even if there are no children of this DIE, we must output the information
9103 about the compilation unit. Otherwise, on an empty translation unit, we
9104 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9105 will then complain when examining the file. First mark all the DIEs in
9106 this CU so we know which get local refs. */
9107 mark_dies (die);
9109 external_ref_hash_type *extern_map = optimize_external_refs (die);
9111 build_abbrev_table (die, extern_map);
9113 delete extern_map;
9115 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9116 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9117 calc_die_sizes (die);
9119 oldsym = die->die_id.die_symbol;
9120 if (oldsym)
9122 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9124 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9125 secname = tmp;
9126 die->die_id.die_symbol = NULL;
9127 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9129 else
9131 switch_to_section (debug_info_section);
9132 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9133 info_section_emitted = true;
9136 /* Output debugging information. */
9137 output_compilation_unit_header ();
9138 output_die (die);
9140 /* Leave the marks on the main CU, so we can check them in
9141 output_pubnames. */
9142 if (oldsym)
9144 unmark_dies (die);
9145 die->die_id.die_symbol = oldsym;
9149 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9150 and .debug_pubtypes. This is configured per-target, but can be
9151 overridden by the -gpubnames or -gno-pubnames options. */
9153 static inline bool
9154 want_pubnames (void)
9156 if (debug_info_level <= DINFO_LEVEL_TERSE)
9157 return false;
9158 if (debug_generate_pub_sections != -1)
9159 return debug_generate_pub_sections;
9160 return targetm.want_debug_pub_sections;
9163 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9165 static void
9166 add_AT_pubnames (dw_die_ref die)
9168 if (want_pubnames ())
9169 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9172 /* Add a string attribute value to a skeleton DIE. */
9174 static inline void
9175 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9176 const char *str)
9178 dw_attr_node attr;
9179 struct indirect_string_node *node;
9181 if (! skeleton_debug_str_hash)
9182 skeleton_debug_str_hash
9183 = hash_table<indirect_string_hasher>::create_ggc (10);
9185 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9186 find_string_form (node);
9187 if (node->form == DW_FORM_GNU_str_index)
9188 node->form = DW_FORM_strp;
9190 attr.dw_attr = attr_kind;
9191 attr.dw_attr_val.val_class = dw_val_class_str;
9192 attr.dw_attr_val.val_entry = NULL;
9193 attr.dw_attr_val.v.val_str = node;
9194 add_dwarf_attr (die, &attr);
9197 /* Helper function to generate top-level dies for skeleton debug_info and
9198 debug_types. */
9200 static void
9201 add_top_level_skeleton_die_attrs (dw_die_ref die)
9203 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9204 const char *comp_dir = comp_dir_string ();
9206 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9207 if (comp_dir != NULL)
9208 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9209 add_AT_pubnames (die);
9210 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9213 /* Output skeleton debug sections that point to the dwo file. */
9215 static void
9216 output_skeleton_debug_sections (dw_die_ref comp_unit)
9218 /* We don't support actual DWARFv5 units yet, we just use some
9219 DWARFv5 draft DIE tags in DWARFv4 format. */
9220 int ver = dwarf_version < 5 ? dwarf_version : 4;
9222 /* These attributes will be found in the full debug_info section. */
9223 remove_AT (comp_unit, DW_AT_producer);
9224 remove_AT (comp_unit, DW_AT_language);
9226 switch_to_section (debug_skeleton_info_section);
9227 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9229 /* Produce the skeleton compilation-unit header. This one differs enough from
9230 a normal CU header that it's better not to call output_compilation_unit
9231 header. */
9232 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9233 dw2_asm_output_data (4, 0xffffffff,
9234 "Initial length escape value indicating 64-bit DWARF extension");
9236 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9237 DWARF_COMPILE_UNIT_HEADER_SIZE
9238 - DWARF_INITIAL_LENGTH_SIZE
9239 + size_of_die (comp_unit),
9240 "Length of Compilation Unit Info");
9241 dw2_asm_output_data (2, ver, "DWARF version number");
9242 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9243 debug_abbrev_section,
9244 "Offset Into Abbrev. Section");
9245 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9247 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9248 output_die (comp_unit);
9250 /* Build the skeleton debug_abbrev section. */
9251 switch_to_section (debug_skeleton_abbrev_section);
9252 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9254 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9256 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9259 /* Output a comdat type unit DIE and its children. */
9261 static void
9262 output_comdat_type_unit (comdat_type_node *node)
9264 const char *secname;
9265 char *tmp;
9266 int i;
9267 #if defined (OBJECT_FORMAT_ELF)
9268 tree comdat_key;
9269 #endif
9271 /* First mark all the DIEs in this CU so we know which get local refs. */
9272 mark_dies (node->root_die);
9274 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9276 build_abbrev_table (node->root_die, extern_map);
9278 delete extern_map;
9279 extern_map = NULL;
9281 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9282 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9283 calc_die_sizes (node->root_die);
9285 #if defined (OBJECT_FORMAT_ELF)
9286 if (!dwarf_split_debug_info)
9287 secname = ".debug_types";
9288 else
9289 secname = ".debug_types.dwo";
9291 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9292 sprintf (tmp, "wt.");
9293 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9294 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9295 comdat_key = get_identifier (tmp);
9296 targetm.asm_out.named_section (secname,
9297 SECTION_DEBUG | SECTION_LINKONCE,
9298 comdat_key);
9299 #else
9300 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9301 sprintf (tmp, ".gnu.linkonce.wt.");
9302 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9303 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9304 secname = tmp;
9305 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9306 #endif
9308 /* Output debugging information. */
9309 output_compilation_unit_header ();
9310 output_signature (node->signature, "Type Signature");
9311 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9312 "Offset to Type DIE");
9313 output_die (node->root_die);
9315 unmark_dies (node->root_die);
9318 /* Return the DWARF2/3 pubname associated with a decl. */
9320 static const char *
9321 dwarf2_name (tree decl, int scope)
9323 if (DECL_NAMELESS (decl))
9324 return NULL;
9325 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9328 /* Add a new entry to .debug_pubnames if appropriate. */
9330 static void
9331 add_pubname_string (const char *str, dw_die_ref die)
9333 pubname_entry e;
9335 e.die = die;
9336 e.name = xstrdup (str);
9337 vec_safe_push (pubname_table, e);
9340 static void
9341 add_pubname (tree decl, dw_die_ref die)
9343 if (!want_pubnames ())
9344 return;
9346 /* Don't add items to the table when we expect that the consumer will have
9347 just read the enclosing die. For example, if the consumer is looking at a
9348 class_member, it will either be inside the class already, or will have just
9349 looked up the class to find the member. Either way, searching the class is
9350 faster than searching the index. */
9351 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9352 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9354 const char *name = dwarf2_name (decl, 1);
9356 if (name)
9357 add_pubname_string (name, die);
9361 /* Add an enumerator to the pubnames section. */
9363 static void
9364 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9366 pubname_entry e;
9368 gcc_assert (scope_name);
9369 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9370 e.die = die;
9371 vec_safe_push (pubname_table, e);
9374 /* Add a new entry to .debug_pubtypes if appropriate. */
9376 static void
9377 add_pubtype (tree decl, dw_die_ref die)
9379 pubname_entry e;
9381 if (!want_pubnames ())
9382 return;
9384 if ((TREE_PUBLIC (decl)
9385 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9386 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9388 tree scope = NULL;
9389 const char *scope_name = "";
9390 const char *sep = is_cxx () ? "::" : ".";
9391 const char *name;
9393 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9394 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9396 scope_name = lang_hooks.dwarf_name (scope, 1);
9397 if (scope_name != NULL && scope_name[0] != '\0')
9398 scope_name = concat (scope_name, sep, NULL);
9399 else
9400 scope_name = "";
9403 if (TYPE_P (decl))
9404 name = type_tag (decl);
9405 else
9406 name = lang_hooks.dwarf_name (decl, 1);
9408 /* If we don't have a name for the type, there's no point in adding
9409 it to the table. */
9410 if (name != NULL && name[0] != '\0')
9412 e.die = die;
9413 e.name = concat (scope_name, name, NULL);
9414 vec_safe_push (pubtype_table, e);
9417 /* Although it might be more consistent to add the pubinfo for the
9418 enumerators as their dies are created, they should only be added if the
9419 enum type meets the criteria above. So rather than re-check the parent
9420 enum type whenever an enumerator die is created, just output them all
9421 here. This isn't protected by the name conditional because anonymous
9422 enums don't have names. */
9423 if (die->die_tag == DW_TAG_enumeration_type)
9425 dw_die_ref c;
9427 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9432 /* Output a single entry in the pubnames table. */
9434 static void
9435 output_pubname (dw_offset die_offset, pubname_entry *entry)
9437 dw_die_ref die = entry->die;
9438 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9440 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9442 if (debug_generate_pub_sections == 2)
9444 /* This logic follows gdb's method for determining the value of the flag
9445 byte. */
9446 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9447 switch (die->die_tag)
9449 case DW_TAG_typedef:
9450 case DW_TAG_base_type:
9451 case DW_TAG_subrange_type:
9452 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9453 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9454 break;
9455 case DW_TAG_enumerator:
9456 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9457 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9458 if (!is_cxx () && !is_java ())
9459 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9460 break;
9461 case DW_TAG_subprogram:
9462 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9463 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9464 if (!is_ada ())
9465 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9466 break;
9467 case DW_TAG_constant:
9468 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9469 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9470 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9471 break;
9472 case DW_TAG_variable:
9473 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9474 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9475 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9476 break;
9477 case DW_TAG_namespace:
9478 case DW_TAG_imported_declaration:
9479 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9480 break;
9481 case DW_TAG_class_type:
9482 case DW_TAG_interface_type:
9483 case DW_TAG_structure_type:
9484 case DW_TAG_union_type:
9485 case DW_TAG_enumeration_type:
9486 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9487 if (!is_cxx () && !is_java ())
9488 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9489 break;
9490 default:
9491 /* An unusual tag. Leave the flag-byte empty. */
9492 break;
9494 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9495 "GDB-index flags");
9498 dw2_asm_output_nstring (entry->name, -1, "external name");
9502 /* Output the public names table used to speed up access to externally
9503 visible names; or the public types table used to find type definitions. */
9505 static void
9506 output_pubnames (vec<pubname_entry, va_gc> *names)
9508 unsigned i;
9509 unsigned long pubnames_length = size_of_pubnames (names);
9510 pubname_ref pub;
9512 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9513 dw2_asm_output_data (4, 0xffffffff,
9514 "Initial length escape value indicating 64-bit DWARF extension");
9515 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9517 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9518 dw2_asm_output_data (2, 2, "DWARF Version");
9520 if (dwarf_split_debug_info)
9521 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9522 debug_skeleton_info_section,
9523 "Offset of Compilation Unit Info");
9524 else
9525 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9526 debug_info_section,
9527 "Offset of Compilation Unit Info");
9528 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9529 "Compilation Unit Length");
9531 FOR_EACH_VEC_ELT (*names, i, pub)
9533 if (include_pubname_in_output (names, pub))
9535 dw_offset die_offset = pub->die->die_offset;
9537 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9538 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9539 gcc_assert (pub->die->die_mark);
9541 /* If we're putting types in their own .debug_types sections,
9542 the .debug_pubtypes table will still point to the compile
9543 unit (not the type unit), so we want to use the offset of
9544 the skeleton DIE (if there is one). */
9545 if (pub->die->comdat_type_p && names == pubtype_table)
9547 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9549 if (type_node != NULL)
9550 die_offset = (type_node->skeleton_die != NULL
9551 ? type_node->skeleton_die->die_offset
9552 : comp_unit_die ()->die_offset);
9555 output_pubname (die_offset, pub);
9559 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9562 /* Output public names and types tables if necessary. */
9564 static void
9565 output_pubtables (void)
9567 if (!want_pubnames () || !info_section_emitted)
9568 return;
9570 switch_to_section (debug_pubnames_section);
9571 output_pubnames (pubname_table);
9572 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9573 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9574 simply won't look for the section. */
9575 switch_to_section (debug_pubtypes_section);
9576 output_pubnames (pubtype_table);
9580 /* Output the information that goes into the .debug_aranges table.
9581 Namely, define the beginning and ending address range of the
9582 text section generated for this compilation unit. */
9584 static void
9585 output_aranges (unsigned long aranges_length)
9587 unsigned i;
9589 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9590 dw2_asm_output_data (4, 0xffffffff,
9591 "Initial length escape value indicating 64-bit DWARF extension");
9592 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9593 "Length of Address Ranges Info");
9594 /* Version number for aranges is still 2, even up to DWARF5. */
9595 dw2_asm_output_data (2, 2, "DWARF Version");
9596 if (dwarf_split_debug_info)
9597 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9598 debug_skeleton_info_section,
9599 "Offset of Compilation Unit Info");
9600 else
9601 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9602 debug_info_section,
9603 "Offset of Compilation Unit Info");
9604 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9605 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9607 /* We need to align to twice the pointer size here. */
9608 if (DWARF_ARANGES_PAD_SIZE)
9610 /* Pad using a 2 byte words so that padding is correct for any
9611 pointer size. */
9612 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9613 2 * DWARF2_ADDR_SIZE);
9614 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9615 dw2_asm_output_data (2, 0, NULL);
9618 /* It is necessary not to output these entries if the sections were
9619 not used; if the sections were not used, the length will be 0 and
9620 the address may end up as 0 if the section is discarded by ld
9621 --gc-sections, leaving an invalid (0, 0) entry that can be
9622 confused with the terminator. */
9623 if (text_section_used)
9625 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9626 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9627 text_section_label, "Length");
9629 if (cold_text_section_used)
9631 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9632 "Address");
9633 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9634 cold_text_section_label, "Length");
9637 if (have_multiple_function_sections)
9639 unsigned fde_idx;
9640 dw_fde_ref fde;
9642 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9644 if (DECL_IGNORED_P (fde->decl))
9645 continue;
9646 if (!fde->in_std_section)
9648 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9649 "Address");
9650 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9651 fde->dw_fde_begin, "Length");
9653 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9655 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9656 "Address");
9657 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9658 fde->dw_fde_second_begin, "Length");
9663 /* Output the terminator words. */
9664 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9665 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9668 /* Add a new entry to .debug_ranges. Return the offset at which it
9669 was placed. */
9671 static unsigned int
9672 add_ranges_num (int num)
9674 unsigned int in_use = ranges_table_in_use;
9676 if (in_use == ranges_table_allocated)
9678 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9679 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9680 ranges_table_allocated);
9681 memset (ranges_table + ranges_table_in_use, 0,
9682 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9685 ranges_table[in_use].num = num;
9686 ranges_table_in_use = in_use + 1;
9688 return in_use * 2 * DWARF2_ADDR_SIZE;
9691 /* Add a new entry to .debug_ranges corresponding to a block, or a
9692 range terminator if BLOCK is NULL. */
9694 static unsigned int
9695 add_ranges (const_tree block)
9697 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9700 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9701 When using dwarf_split_debug_info, address attributes in dies destined
9702 for the final executable should be direct references--setting the
9703 parameter force_direct ensures this behavior. */
9705 static void
9706 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9707 bool *added, bool force_direct)
9709 unsigned int in_use = ranges_by_label_in_use;
9710 unsigned int offset;
9712 if (in_use == ranges_by_label_allocated)
9714 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9715 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9716 ranges_by_label,
9717 ranges_by_label_allocated);
9718 memset (ranges_by_label + ranges_by_label_in_use, 0,
9719 RANGES_TABLE_INCREMENT
9720 * sizeof (struct dw_ranges_by_label_struct));
9723 ranges_by_label[in_use].begin = begin;
9724 ranges_by_label[in_use].end = end;
9725 ranges_by_label_in_use = in_use + 1;
9727 offset = add_ranges_num (-(int)in_use - 1);
9728 if (!*added)
9730 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9731 *added = true;
9735 static void
9736 output_ranges (void)
9738 unsigned i;
9739 static const char *const start_fmt = "Offset %#x";
9740 const char *fmt = start_fmt;
9742 for (i = 0; i < ranges_table_in_use; i++)
9744 int block_num = ranges_table[i].num;
9746 if (block_num > 0)
9748 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9749 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9751 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9752 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9754 /* If all code is in the text section, then the compilation
9755 unit base address defaults to DW_AT_low_pc, which is the
9756 base of the text section. */
9757 if (!have_multiple_function_sections)
9759 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9760 text_section_label,
9761 fmt, i * 2 * DWARF2_ADDR_SIZE);
9762 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9763 text_section_label, NULL);
9766 /* Otherwise, the compilation unit base address is zero,
9767 which allows us to use absolute addresses, and not worry
9768 about whether the target supports cross-section
9769 arithmetic. */
9770 else
9772 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9773 fmt, i * 2 * DWARF2_ADDR_SIZE);
9774 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9777 fmt = NULL;
9780 /* Negative block_num stands for an index into ranges_by_label. */
9781 else if (block_num < 0)
9783 int lab_idx = - block_num - 1;
9785 if (!have_multiple_function_sections)
9787 gcc_unreachable ();
9788 #if 0
9789 /* If we ever use add_ranges_by_labels () for a single
9790 function section, all we have to do is to take out
9791 the #if 0 above. */
9792 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9793 ranges_by_label[lab_idx].begin,
9794 text_section_label,
9795 fmt, i * 2 * DWARF2_ADDR_SIZE);
9796 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9797 ranges_by_label[lab_idx].end,
9798 text_section_label, NULL);
9799 #endif
9801 else
9803 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9804 ranges_by_label[lab_idx].begin,
9805 fmt, i * 2 * DWARF2_ADDR_SIZE);
9806 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9807 ranges_by_label[lab_idx].end,
9808 NULL);
9811 else
9813 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9814 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9815 fmt = start_fmt;
9820 /* Data structure containing information about input files. */
9821 struct file_info
9823 const char *path; /* Complete file name. */
9824 const char *fname; /* File name part. */
9825 int length; /* Length of entire string. */
9826 struct dwarf_file_data * file_idx; /* Index in input file table. */
9827 int dir_idx; /* Index in directory table. */
9830 /* Data structure containing information about directories with source
9831 files. */
9832 struct dir_info
9834 const char *path; /* Path including directory name. */
9835 int length; /* Path length. */
9836 int prefix; /* Index of directory entry which is a prefix. */
9837 int count; /* Number of files in this directory. */
9838 int dir_idx; /* Index of directory used as base. */
9841 /* Callback function for file_info comparison. We sort by looking at
9842 the directories in the path. */
9844 static int
9845 file_info_cmp (const void *p1, const void *p2)
9847 const struct file_info *const s1 = (const struct file_info *) p1;
9848 const struct file_info *const s2 = (const struct file_info *) p2;
9849 const unsigned char *cp1;
9850 const unsigned char *cp2;
9852 /* Take care of file names without directories. We need to make sure that
9853 we return consistent values to qsort since some will get confused if
9854 we return the same value when identical operands are passed in opposite
9855 orders. So if neither has a directory, return 0 and otherwise return
9856 1 or -1 depending on which one has the directory. */
9857 if ((s1->path == s1->fname || s2->path == s2->fname))
9858 return (s2->path == s2->fname) - (s1->path == s1->fname);
9860 cp1 = (const unsigned char *) s1->path;
9861 cp2 = (const unsigned char *) s2->path;
9863 while (1)
9865 ++cp1;
9866 ++cp2;
9867 /* Reached the end of the first path? If so, handle like above. */
9868 if ((cp1 == (const unsigned char *) s1->fname)
9869 || (cp2 == (const unsigned char *) s2->fname))
9870 return ((cp2 == (const unsigned char *) s2->fname)
9871 - (cp1 == (const unsigned char *) s1->fname));
9873 /* Character of current path component the same? */
9874 else if (*cp1 != *cp2)
9875 return *cp1 - *cp2;
9879 struct file_name_acquire_data
9881 struct file_info *files;
9882 int used_files;
9883 int max_files;
9886 /* Traversal function for the hash table. */
9889 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9891 struct dwarf_file_data *d = *slot;
9892 struct file_info *fi;
9893 const char *f;
9895 gcc_assert (fnad->max_files >= d->emitted_number);
9897 if (! d->emitted_number)
9898 return 1;
9900 gcc_assert (fnad->max_files != fnad->used_files);
9902 fi = fnad->files + fnad->used_files++;
9904 /* Skip all leading "./". */
9905 f = d->filename;
9906 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9907 f += 2;
9909 /* Create a new array entry. */
9910 fi->path = f;
9911 fi->length = strlen (f);
9912 fi->file_idx = d;
9914 /* Search for the file name part. */
9915 f = strrchr (f, DIR_SEPARATOR);
9916 #if defined (DIR_SEPARATOR_2)
9918 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9920 if (g != NULL)
9922 if (f == NULL || f < g)
9923 f = g;
9926 #endif
9928 fi->fname = f == NULL ? fi->path : f + 1;
9929 return 1;
9932 /* Output the directory table and the file name table. We try to minimize
9933 the total amount of memory needed. A heuristic is used to avoid large
9934 slowdowns with many input files. */
9936 static void
9937 output_file_names (void)
9939 struct file_name_acquire_data fnad;
9940 int numfiles;
9941 struct file_info *files;
9942 struct dir_info *dirs;
9943 int *saved;
9944 int *savehere;
9945 int *backmap;
9946 int ndirs;
9947 int idx_offset;
9948 int i;
9950 if (!last_emitted_file)
9952 dw2_asm_output_data (1, 0, "End directory table");
9953 dw2_asm_output_data (1, 0, "End file name table");
9954 return;
9957 numfiles = last_emitted_file->emitted_number;
9959 /* Allocate the various arrays we need. */
9960 files = XALLOCAVEC (struct file_info, numfiles);
9961 dirs = XALLOCAVEC (struct dir_info, numfiles);
9963 fnad.files = files;
9964 fnad.used_files = 0;
9965 fnad.max_files = numfiles;
9966 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9967 gcc_assert (fnad.used_files == fnad.max_files);
9969 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9971 /* Find all the different directories used. */
9972 dirs[0].path = files[0].path;
9973 dirs[0].length = files[0].fname - files[0].path;
9974 dirs[0].prefix = -1;
9975 dirs[0].count = 1;
9976 dirs[0].dir_idx = 0;
9977 files[0].dir_idx = 0;
9978 ndirs = 1;
9980 for (i = 1; i < numfiles; i++)
9981 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9982 && memcmp (dirs[ndirs - 1].path, files[i].path,
9983 dirs[ndirs - 1].length) == 0)
9985 /* Same directory as last entry. */
9986 files[i].dir_idx = ndirs - 1;
9987 ++dirs[ndirs - 1].count;
9989 else
9991 int j;
9993 /* This is a new directory. */
9994 dirs[ndirs].path = files[i].path;
9995 dirs[ndirs].length = files[i].fname - files[i].path;
9996 dirs[ndirs].count = 1;
9997 dirs[ndirs].dir_idx = ndirs;
9998 files[i].dir_idx = ndirs;
10000 /* Search for a prefix. */
10001 dirs[ndirs].prefix = -1;
10002 for (j = 0; j < ndirs; j++)
10003 if (dirs[j].length < dirs[ndirs].length
10004 && dirs[j].length > 1
10005 && (dirs[ndirs].prefix == -1
10006 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10007 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10008 dirs[ndirs].prefix = j;
10010 ++ndirs;
10013 /* Now to the actual work. We have to find a subset of the directories which
10014 allow expressing the file name using references to the directory table
10015 with the least amount of characters. We do not do an exhaustive search
10016 where we would have to check out every combination of every single
10017 possible prefix. Instead we use a heuristic which provides nearly optimal
10018 results in most cases and never is much off. */
10019 saved = XALLOCAVEC (int, ndirs);
10020 savehere = XALLOCAVEC (int, ndirs);
10022 memset (saved, '\0', ndirs * sizeof (saved[0]));
10023 for (i = 0; i < ndirs; i++)
10025 int j;
10026 int total;
10028 /* We can always save some space for the current directory. But this
10029 does not mean it will be enough to justify adding the directory. */
10030 savehere[i] = dirs[i].length;
10031 total = (savehere[i] - saved[i]) * dirs[i].count;
10033 for (j = i + 1; j < ndirs; j++)
10035 savehere[j] = 0;
10036 if (saved[j] < dirs[i].length)
10038 /* Determine whether the dirs[i] path is a prefix of the
10039 dirs[j] path. */
10040 int k;
10042 k = dirs[j].prefix;
10043 while (k != -1 && k != (int) i)
10044 k = dirs[k].prefix;
10046 if (k == (int) i)
10048 /* Yes it is. We can possibly save some memory by
10049 writing the filenames in dirs[j] relative to
10050 dirs[i]. */
10051 savehere[j] = dirs[i].length;
10052 total += (savehere[j] - saved[j]) * dirs[j].count;
10057 /* Check whether we can save enough to justify adding the dirs[i]
10058 directory. */
10059 if (total > dirs[i].length + 1)
10061 /* It's worthwhile adding. */
10062 for (j = i; j < ndirs; j++)
10063 if (savehere[j] > 0)
10065 /* Remember how much we saved for this directory so far. */
10066 saved[j] = savehere[j];
10068 /* Remember the prefix directory. */
10069 dirs[j].dir_idx = i;
10074 /* Emit the directory name table. */
10075 idx_offset = dirs[0].length > 0 ? 1 : 0;
10076 for (i = 1 - idx_offset; i < ndirs; i++)
10077 dw2_asm_output_nstring (dirs[i].path,
10078 dirs[i].length
10079 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10080 "Directory Entry: %#x", i + idx_offset);
10082 dw2_asm_output_data (1, 0, "End directory table");
10084 /* We have to emit them in the order of emitted_number since that's
10085 used in the debug info generation. To do this efficiently we
10086 generate a back-mapping of the indices first. */
10087 backmap = XALLOCAVEC (int, numfiles);
10088 for (i = 0; i < numfiles; i++)
10089 backmap[files[i].file_idx->emitted_number - 1] = i;
10091 /* Now write all the file names. */
10092 for (i = 0; i < numfiles; i++)
10094 int file_idx = backmap[i];
10095 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10097 #ifdef VMS_DEBUGGING_INFO
10098 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10100 /* Setting these fields can lead to debugger miscomparisons,
10101 but VMS Debug requires them to be set correctly. */
10103 int ver;
10104 long long cdt;
10105 long siz;
10106 int maxfilelen = strlen (files[file_idx].path)
10107 + dirs[dir_idx].length
10108 + MAX_VMS_VERSION_LEN + 1;
10109 char *filebuf = XALLOCAVEC (char, maxfilelen);
10111 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10112 snprintf (filebuf, maxfilelen, "%s;%d",
10113 files[file_idx].path + dirs[dir_idx].length, ver);
10115 dw2_asm_output_nstring
10116 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10118 /* Include directory index. */
10119 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10121 /* Modification time. */
10122 dw2_asm_output_data_uleb128
10123 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10124 ? cdt : 0,
10125 NULL);
10127 /* File length in bytes. */
10128 dw2_asm_output_data_uleb128
10129 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10130 ? siz : 0,
10131 NULL);
10132 #else
10133 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10134 "File Entry: %#x", (unsigned) i + 1);
10136 /* Include directory index. */
10137 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10139 /* Modification time. */
10140 dw2_asm_output_data_uleb128 (0, NULL);
10142 /* File length in bytes. */
10143 dw2_asm_output_data_uleb128 (0, NULL);
10144 #endif /* VMS_DEBUGGING_INFO */
10147 dw2_asm_output_data (1, 0, "End file name table");
10151 /* Output one line number table into the .debug_line section. */
10153 static void
10154 output_one_line_info_table (dw_line_info_table *table)
10156 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10157 unsigned int current_line = 1;
10158 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10159 dw_line_info_entry *ent;
10160 size_t i;
10162 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10164 switch (ent->opcode)
10166 case LI_set_address:
10167 /* ??? Unfortunately, we have little choice here currently, and
10168 must always use the most general form. GCC does not know the
10169 address delta itself, so we can't use DW_LNS_advance_pc. Many
10170 ports do have length attributes which will give an upper bound
10171 on the address range. We could perhaps use length attributes
10172 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10173 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10175 /* This can handle any delta. This takes
10176 4+DWARF2_ADDR_SIZE bytes. */
10177 dw2_asm_output_data (1, 0, "set address %s", line_label);
10178 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10179 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10180 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10181 break;
10183 case LI_set_line:
10184 if (ent->val == current_line)
10186 /* We still need to start a new row, so output a copy insn. */
10187 dw2_asm_output_data (1, DW_LNS_copy,
10188 "copy line %u", current_line);
10190 else
10192 int line_offset = ent->val - current_line;
10193 int line_delta = line_offset - DWARF_LINE_BASE;
10195 current_line = ent->val;
10196 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10198 /* This can handle deltas from -10 to 234, using the current
10199 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10200 This takes 1 byte. */
10201 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10202 "line %u", current_line);
10204 else
10206 /* This can handle any delta. This takes at least 4 bytes,
10207 depending on the value being encoded. */
10208 dw2_asm_output_data (1, DW_LNS_advance_line,
10209 "advance to line %u", current_line);
10210 dw2_asm_output_data_sleb128 (line_offset, NULL);
10211 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10214 break;
10216 case LI_set_file:
10217 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10218 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10219 break;
10221 case LI_set_column:
10222 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10223 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10224 break;
10226 case LI_negate_stmt:
10227 current_is_stmt = !current_is_stmt;
10228 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10229 "is_stmt %d", current_is_stmt);
10230 break;
10232 case LI_set_prologue_end:
10233 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10234 "set prologue end");
10235 break;
10237 case LI_set_epilogue_begin:
10238 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10239 "set epilogue begin");
10240 break;
10242 case LI_set_discriminator:
10243 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10244 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10245 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10246 dw2_asm_output_data_uleb128 (ent->val, NULL);
10247 break;
10251 /* Emit debug info for the address of the end of the table. */
10252 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10253 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10254 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10255 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10257 dw2_asm_output_data (1, 0, "end sequence");
10258 dw2_asm_output_data_uleb128 (1, NULL);
10259 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10262 /* Output the source line number correspondence information. This
10263 information goes into the .debug_line section. */
10265 static void
10266 output_line_info (bool prologue_only)
10268 char l1[20], l2[20], p1[20], p2[20];
10269 /* We don't support DWARFv5 line tables yet. */
10270 int ver = dwarf_version < 5 ? dwarf_version : 4;
10271 bool saw_one = false;
10272 int opc;
10274 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10275 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10276 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10277 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10279 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10280 dw2_asm_output_data (4, 0xffffffff,
10281 "Initial length escape value indicating 64-bit DWARF extension");
10282 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10283 "Length of Source Line Info");
10284 ASM_OUTPUT_LABEL (asm_out_file, l1);
10286 dw2_asm_output_data (2, ver, "DWARF Version");
10287 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10288 ASM_OUTPUT_LABEL (asm_out_file, p1);
10290 /* Define the architecture-dependent minimum instruction length (in bytes).
10291 In this implementation of DWARF, this field is used for information
10292 purposes only. Since GCC generates assembly language, we have no
10293 a priori knowledge of how many instruction bytes are generated for each
10294 source line, and therefore can use only the DW_LNE_set_address and
10295 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10296 this as '1', which is "correct enough" for all architectures,
10297 and don't let the target override. */
10298 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10300 if (ver >= 4)
10301 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10302 "Maximum Operations Per Instruction");
10303 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10304 "Default is_stmt_start flag");
10305 dw2_asm_output_data (1, DWARF_LINE_BASE,
10306 "Line Base Value (Special Opcodes)");
10307 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10308 "Line Range Value (Special Opcodes)");
10309 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10310 "Special Opcode Base");
10312 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10314 int n_op_args;
10315 switch (opc)
10317 case DW_LNS_advance_pc:
10318 case DW_LNS_advance_line:
10319 case DW_LNS_set_file:
10320 case DW_LNS_set_column:
10321 case DW_LNS_fixed_advance_pc:
10322 case DW_LNS_set_isa:
10323 n_op_args = 1;
10324 break;
10325 default:
10326 n_op_args = 0;
10327 break;
10330 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10331 opc, n_op_args);
10334 /* Write out the information about the files we use. */
10335 output_file_names ();
10336 ASM_OUTPUT_LABEL (asm_out_file, p2);
10337 if (prologue_only)
10339 /* Output the marker for the end of the line number info. */
10340 ASM_OUTPUT_LABEL (asm_out_file, l2);
10341 return;
10344 if (separate_line_info)
10346 dw_line_info_table *table;
10347 size_t i;
10349 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10350 if (table->in_use)
10352 output_one_line_info_table (table);
10353 saw_one = true;
10356 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10358 output_one_line_info_table (cold_text_section_line_info);
10359 saw_one = true;
10362 /* ??? Some Darwin linkers crash on a .debug_line section with no
10363 sequences. Further, merely a DW_LNE_end_sequence entry is not
10364 sufficient -- the address column must also be initialized.
10365 Make sure to output at least one set_address/end_sequence pair,
10366 choosing .text since that section is always present. */
10367 if (text_section_line_info->in_use || !saw_one)
10368 output_one_line_info_table (text_section_line_info);
10370 /* Output the marker for the end of the line number info. */
10371 ASM_OUTPUT_LABEL (asm_out_file, l2);
10374 /* Given a pointer to a tree node for some base type, return a pointer to
10375 a DIE that describes the given type.
10377 This routine must only be called for GCC type nodes that correspond to
10378 Dwarf base (fundamental) types. */
10380 static dw_die_ref
10381 base_type_die (tree type)
10383 dw_die_ref base_type_result;
10384 enum dwarf_type encoding;
10386 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10387 return 0;
10389 /* If this is a subtype that should not be emitted as a subrange type,
10390 use the base type. See subrange_type_for_debug_p. */
10391 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10392 type = TREE_TYPE (type);
10394 switch (TREE_CODE (type))
10396 case INTEGER_TYPE:
10397 if ((dwarf_version >= 4 || !dwarf_strict)
10398 && TYPE_NAME (type)
10399 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10400 && DECL_IS_BUILTIN (TYPE_NAME (type))
10401 && DECL_NAME (TYPE_NAME (type)))
10403 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10404 if (strcmp (name, "char16_t") == 0
10405 || strcmp (name, "char32_t") == 0)
10407 encoding = DW_ATE_UTF;
10408 break;
10411 if (TYPE_STRING_FLAG (type))
10413 if (TYPE_UNSIGNED (type))
10414 encoding = DW_ATE_unsigned_char;
10415 else
10416 encoding = DW_ATE_signed_char;
10418 else if (TYPE_UNSIGNED (type))
10419 encoding = DW_ATE_unsigned;
10420 else
10421 encoding = DW_ATE_signed;
10422 break;
10424 case REAL_TYPE:
10425 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10427 if (dwarf_version >= 3 || !dwarf_strict)
10428 encoding = DW_ATE_decimal_float;
10429 else
10430 encoding = DW_ATE_lo_user;
10432 else
10433 encoding = DW_ATE_float;
10434 break;
10436 case FIXED_POINT_TYPE:
10437 if (!(dwarf_version >= 3 || !dwarf_strict))
10438 encoding = DW_ATE_lo_user;
10439 else if (TYPE_UNSIGNED (type))
10440 encoding = DW_ATE_unsigned_fixed;
10441 else
10442 encoding = DW_ATE_signed_fixed;
10443 break;
10445 /* Dwarf2 doesn't know anything about complex ints, so use
10446 a user defined type for it. */
10447 case COMPLEX_TYPE:
10448 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10449 encoding = DW_ATE_complex_float;
10450 else
10451 encoding = DW_ATE_lo_user;
10452 break;
10454 case BOOLEAN_TYPE:
10455 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10456 encoding = DW_ATE_boolean;
10457 break;
10459 default:
10460 /* No other TREE_CODEs are Dwarf fundamental types. */
10461 gcc_unreachable ();
10464 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10466 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10467 int_size_in_bytes (type));
10468 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10469 add_pubtype (type, base_type_result);
10471 return base_type_result;
10474 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10475 named 'auto' in its type: return true for it, false otherwise. */
10477 static inline bool
10478 is_cxx_auto (tree type)
10480 if (is_cxx ())
10482 tree name = TYPE_IDENTIFIER (type);
10483 if (name == get_identifier ("auto")
10484 || name == get_identifier ("decltype(auto)"))
10485 return true;
10487 return false;
10490 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10491 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10493 static inline int
10494 is_base_type (tree type)
10496 switch (TREE_CODE (type))
10498 case ERROR_MARK:
10499 case VOID_TYPE:
10500 case INTEGER_TYPE:
10501 case REAL_TYPE:
10502 case FIXED_POINT_TYPE:
10503 case COMPLEX_TYPE:
10504 case BOOLEAN_TYPE:
10505 case POINTER_BOUNDS_TYPE:
10506 return 1;
10508 case ARRAY_TYPE:
10509 case RECORD_TYPE:
10510 case UNION_TYPE:
10511 case QUAL_UNION_TYPE:
10512 case ENUMERAL_TYPE:
10513 case FUNCTION_TYPE:
10514 case METHOD_TYPE:
10515 case POINTER_TYPE:
10516 case REFERENCE_TYPE:
10517 case NULLPTR_TYPE:
10518 case OFFSET_TYPE:
10519 case LANG_TYPE:
10520 case VECTOR_TYPE:
10521 return 0;
10523 default:
10524 if (is_cxx_auto (type))
10525 return 0;
10526 gcc_unreachable ();
10529 return 0;
10532 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10533 node, return the size in bits for the type if it is a constant, or else
10534 return the alignment for the type if the type's size is not constant, or
10535 else return BITS_PER_WORD if the type actually turns out to be an
10536 ERROR_MARK node. */
10538 static inline unsigned HOST_WIDE_INT
10539 simple_type_size_in_bits (const_tree type)
10541 if (TREE_CODE (type) == ERROR_MARK)
10542 return BITS_PER_WORD;
10543 else if (TYPE_SIZE (type) == NULL_TREE)
10544 return 0;
10545 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10546 return tree_to_uhwi (TYPE_SIZE (type));
10547 else
10548 return TYPE_ALIGN (type);
10551 /* Similarly, but return an offset_int instead of UHWI. */
10553 static inline offset_int
10554 offset_int_type_size_in_bits (const_tree type)
10556 if (TREE_CODE (type) == ERROR_MARK)
10557 return BITS_PER_WORD;
10558 else if (TYPE_SIZE (type) == NULL_TREE)
10559 return 0;
10560 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10561 return wi::to_offset (TYPE_SIZE (type));
10562 else
10563 return TYPE_ALIGN (type);
10566 /* Given a pointer to a tree node for a subrange type, return a pointer
10567 to a DIE that describes the given type. */
10569 static dw_die_ref
10570 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10572 dw_die_ref subrange_die;
10573 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10575 if (context_die == NULL)
10576 context_die = comp_unit_die ();
10578 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10580 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10582 /* The size of the subrange type and its base type do not match,
10583 so we need to generate a size attribute for the subrange type. */
10584 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10587 if (low)
10588 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10589 if (high)
10590 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10592 return subrange_die;
10595 /* Returns the (const and/or volatile) cv_qualifiers associated with
10596 the decl node. This will normally be augmented with the
10597 cv_qualifiers of the underlying type in add_type_attribute. */
10599 static int
10600 decl_quals (const_tree decl)
10602 return ((TREE_READONLY (decl)
10603 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10604 | (TREE_THIS_VOLATILE (decl)
10605 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10608 /* Determine the TYPE whose qualifiers match the largest strict subset
10609 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10610 qualifiers outside QUAL_MASK. */
10612 static int
10613 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10615 tree t;
10616 int best_rank = 0, best_qual = 0, max_rank;
10618 type_quals &= qual_mask;
10619 max_rank = popcount_hwi (type_quals) - 1;
10621 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10622 t = TYPE_NEXT_VARIANT (t))
10624 int q = TYPE_QUALS (t) & qual_mask;
10626 if ((q & type_quals) == q && q != type_quals
10627 && check_base_type (t, type))
10629 int rank = popcount_hwi (q);
10631 if (rank > best_rank)
10633 best_rank = rank;
10634 best_qual = q;
10639 return best_qual;
10642 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10643 entry that chains various modifiers in front of the given type. */
10645 static dw_die_ref
10646 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10648 enum tree_code code = TREE_CODE (type);
10649 dw_die_ref mod_type_die;
10650 dw_die_ref sub_die = NULL;
10651 tree item_type = NULL;
10652 tree qualified_type;
10653 tree name, low, high;
10654 dw_die_ref mod_scope;
10655 /* Only these cv-qualifiers are currently handled. */
10656 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10657 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10659 if (code == ERROR_MARK)
10660 return NULL;
10662 cv_quals &= cv_qual_mask;
10664 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10665 tag modifier (and not an attribute) old consumers won't be able
10666 to handle it. */
10667 if (dwarf_version < 3)
10668 cv_quals &= ~TYPE_QUAL_RESTRICT;
10670 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10671 if (dwarf_version < 5)
10672 cv_quals &= ~TYPE_QUAL_ATOMIC;
10674 /* See if we already have the appropriately qualified variant of
10675 this type. */
10676 qualified_type = get_qualified_type (type, cv_quals);
10678 if (qualified_type == sizetype
10679 && TYPE_NAME (qualified_type)
10680 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10682 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10684 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10685 && TYPE_PRECISION (t)
10686 == TYPE_PRECISION (qualified_type)
10687 && TYPE_UNSIGNED (t)
10688 == TYPE_UNSIGNED (qualified_type));
10689 qualified_type = t;
10692 /* If we do, then we can just use its DIE, if it exists. */
10693 if (qualified_type)
10695 mod_type_die = lookup_type_die (qualified_type);
10696 if (mod_type_die)
10697 return mod_type_die;
10700 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10702 /* Handle C typedef types. */
10703 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10704 && !DECL_ARTIFICIAL (name))
10706 tree dtype = TREE_TYPE (name);
10708 if (qualified_type == dtype)
10710 /* For a named type, use the typedef. */
10711 gen_type_die (qualified_type, context_die);
10712 return lookup_type_die (qualified_type);
10714 else
10716 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10717 dquals &= cv_qual_mask;
10718 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10719 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10720 /* cv-unqualified version of named type. Just use
10721 the unnamed type to which it refers. */
10722 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10723 cv_quals, context_die);
10724 /* Else cv-qualified version of named type; fall through. */
10728 mod_scope = scope_die_for (type, context_die);
10730 if (cv_quals)
10732 struct qual_info { int q; enum dwarf_tag t; };
10733 static const struct qual_info qual_info[] =
10735 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10736 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10737 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10738 { TYPE_QUAL_CONST, DW_TAG_const_type },
10740 int sub_quals;
10741 unsigned i;
10743 /* Determine a lesser qualified type that most closely matches
10744 this one. Then generate DW_TAG_* entries for the remaining
10745 qualifiers. */
10746 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10747 cv_qual_mask);
10748 mod_type_die = modified_type_die (type, sub_quals, context_die);
10750 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10751 if (qual_info[i].q & cv_quals & ~sub_quals)
10753 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10754 if (mod_type_die)
10755 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10756 mod_type_die = d;
10759 else if (use_upc_dwarf2_extensions
10760 && (cv_quals & TYPE_QUAL_UPC_SHARED))
10762 HOST_WIDE_INT block_factor = 1;
10764 /* Inside the compiler,
10765 "shared int x;" TYPE_UPC_BLOCK_FACTOR is null.
10766 "shared [] int *p;" TYPE_UPC_BLOCK_FACTOR is zero.
10767 "shared [10] int x[50];" TYPE_UPC_BLOCK_FACTOR is 10 * bitsize(int)
10768 The DWARF2 encoding is as follows:
10769 "shared int x;" DW_AT_count: 1
10770 "shared [] int *p;" <no DW_AT_count attribute>
10771 "shared [10] int x[50];" DW_AT_count: 10
10772 The logic below handles thse various contingencies. */
10774 mod_type_die = new_die (DW_TAG_upc_shared_type,
10775 comp_unit_die (), type);
10777 if (TYPE_HAS_UPC_BLOCK_FACTOR (type))
10778 block_factor = TREE_INT_CST_LOW (TYPE_UPC_BLOCK_FACTOR (type));
10780 if (block_factor != 0)
10781 add_AT_unsigned (mod_type_die, DW_AT_count, block_factor);
10783 sub_die = modified_type_die (type,
10784 cv_quals & ~TYPE_QUAL_UPC_SHARED,
10785 context_die);
10787 else if (use_upc_dwarf2_extensions && cv_quals & TYPE_QUAL_UPC_STRICT)
10789 mod_type_die = new_die (DW_TAG_upc_strict_type,
10790 comp_unit_die (), type);
10791 sub_die = modified_type_die (type,
10792 cv_quals & ~TYPE_QUAL_UPC_STRICT,
10793 context_die);
10795 else if (use_upc_dwarf2_extensions && cv_quals & TYPE_QUAL_UPC_RELAXED)
10797 mod_type_die = new_die (DW_TAG_upc_relaxed_type,
10798 comp_unit_die (), type);
10799 sub_die = modified_type_die (type,
10800 cv_quals & ~TYPE_QUAL_UPC_RELAXED,
10801 context_die);
10803 else if (code == POINTER_TYPE)
10805 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10806 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10807 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10808 item_type = TREE_TYPE (type);
10809 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10810 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10811 TYPE_ADDR_SPACE (item_type));
10813 else if (code == REFERENCE_TYPE)
10815 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10816 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10817 type);
10818 else
10819 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10820 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10821 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10822 item_type = TREE_TYPE (type);
10823 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10824 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10825 TYPE_ADDR_SPACE (item_type));
10827 else if (code == INTEGER_TYPE
10828 && TREE_TYPE (type) != NULL_TREE
10829 && subrange_type_for_debug_p (type, &low, &high))
10831 mod_type_die = subrange_type_die (type, low, high, context_die);
10832 item_type = TREE_TYPE (type);
10834 else if (is_base_type (type))
10835 mod_type_die = base_type_die (type);
10836 else
10838 gen_type_die (type, context_die);
10840 /* We have to get the type_main_variant here (and pass that to the
10841 `lookup_type_die' routine) because the ..._TYPE node we have
10842 might simply be a *copy* of some original type node (where the
10843 copy was created to help us keep track of typedef names) and
10844 that copy might have a different TYPE_UID from the original
10845 ..._TYPE node. */
10846 if (TREE_CODE (type) != VECTOR_TYPE)
10847 return lookup_type_die (type_main_variant (type));
10848 else
10849 /* Vectors have the debugging information in the type,
10850 not the main variant. */
10851 return lookup_type_die (type);
10854 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10855 don't output a DW_TAG_typedef, since there isn't one in the
10856 user's program; just attach a DW_AT_name to the type.
10857 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10858 if the base type already has the same name. */
10859 if (name
10860 && ((TREE_CODE (name) != TYPE_DECL
10861 && (qualified_type == TYPE_MAIN_VARIANT (type)
10862 || (cv_quals == TYPE_UNQUALIFIED)))
10863 || (TREE_CODE (name) == TYPE_DECL
10864 && TREE_TYPE (name) == qualified_type
10865 && DECL_NAME (name))))
10867 if (TREE_CODE (name) == TYPE_DECL)
10868 /* Could just call add_name_and_src_coords_attributes here,
10869 but since this is a builtin type it doesn't have any
10870 useful source coordinates anyway. */
10871 name = DECL_NAME (name);
10872 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10874 /* This probably indicates a bug. */
10875 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10877 name = TYPE_IDENTIFIER (type);
10878 add_name_attribute (mod_type_die,
10879 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10882 if (qualified_type)
10883 equate_type_number_to_die (qualified_type, mod_type_die);
10885 if (item_type)
10886 /* We must do this after the equate_type_number_to_die call, in case
10887 this is a recursive type. This ensures that the modified_type_die
10888 recursion will terminate even if the type is recursive. Recursive
10889 types are possible in Ada. */
10890 sub_die = modified_type_die (item_type,
10891 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10892 context_die);
10894 if (sub_die != NULL)
10895 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10897 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10898 if (TYPE_ARTIFICIAL (type))
10899 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10901 return mod_type_die;
10904 /* Generate DIEs for the generic parameters of T.
10905 T must be either a generic type or a generic function.
10906 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10908 static void
10909 gen_generic_params_dies (tree t)
10911 tree parms, args;
10912 int parms_num, i;
10913 dw_die_ref die = NULL;
10914 int non_default;
10916 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10917 return;
10919 if (TYPE_P (t))
10920 die = lookup_type_die (t);
10921 else if (DECL_P (t))
10922 die = lookup_decl_die (t);
10924 gcc_assert (die);
10926 parms = lang_hooks.get_innermost_generic_parms (t);
10927 if (!parms)
10928 /* T has no generic parameter. It means T is neither a generic type
10929 or function. End of story. */
10930 return;
10932 parms_num = TREE_VEC_LENGTH (parms);
10933 args = lang_hooks.get_innermost_generic_args (t);
10934 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10935 non_default = int_cst_value (TREE_CHAIN (args));
10936 else
10937 non_default = TREE_VEC_LENGTH (args);
10938 for (i = 0; i < parms_num; i++)
10940 tree parm, arg, arg_pack_elems;
10941 dw_die_ref parm_die;
10943 parm = TREE_VEC_ELT (parms, i);
10944 arg = TREE_VEC_ELT (args, i);
10945 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10946 gcc_assert (parm && TREE_VALUE (parm) && arg);
10948 if (parm && TREE_VALUE (parm) && arg)
10950 /* If PARM represents a template parameter pack,
10951 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10952 by DW_TAG_template_*_parameter DIEs for the argument
10953 pack elements of ARG. Note that ARG would then be
10954 an argument pack. */
10955 if (arg_pack_elems)
10956 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10957 arg_pack_elems,
10958 die);
10959 else
10960 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10961 true /* emit name */, die);
10962 if (i >= non_default)
10963 add_AT_flag (parm_die, DW_AT_default_value, 1);
10968 /* Create and return a DIE for PARM which should be
10969 the representation of a generic type parameter.
10970 For instance, in the C++ front end, PARM would be a template parameter.
10971 ARG is the argument to PARM.
10972 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10973 name of the PARM.
10974 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10975 as a child node. */
10977 static dw_die_ref
10978 generic_parameter_die (tree parm, tree arg,
10979 bool emit_name_p,
10980 dw_die_ref parent_die)
10982 dw_die_ref tmpl_die = NULL;
10983 const char *name = NULL;
10985 if (!parm || !DECL_NAME (parm) || !arg)
10986 return NULL;
10988 /* We support non-type generic parameters and arguments,
10989 type generic parameters and arguments, as well as
10990 generic generic parameters (a.k.a. template template parameters in C++)
10991 and arguments. */
10992 if (TREE_CODE (parm) == PARM_DECL)
10993 /* PARM is a nontype generic parameter */
10994 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10995 else if (TREE_CODE (parm) == TYPE_DECL)
10996 /* PARM is a type generic parameter. */
10997 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10998 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10999 /* PARM is a generic generic parameter.
11000 Its DIE is a GNU extension. It shall have a
11001 DW_AT_name attribute to represent the name of the template template
11002 parameter, and a DW_AT_GNU_template_name attribute to represent the
11003 name of the template template argument. */
11004 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11005 parent_die, parm);
11006 else
11007 gcc_unreachable ();
11009 if (tmpl_die)
11011 tree tmpl_type;
11013 /* If PARM is a generic parameter pack, it means we are
11014 emitting debug info for a template argument pack element.
11015 In other terms, ARG is a template argument pack element.
11016 In that case, we don't emit any DW_AT_name attribute for
11017 the die. */
11018 if (emit_name_p)
11020 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11021 gcc_assert (name);
11022 add_AT_string (tmpl_die, DW_AT_name, name);
11025 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11027 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11028 TMPL_DIE should have a child DW_AT_type attribute that is set
11029 to the type of the argument to PARM, which is ARG.
11030 If PARM is a type generic parameter, TMPL_DIE should have a
11031 child DW_AT_type that is set to ARG. */
11032 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11033 add_type_attribute (tmpl_die, tmpl_type,
11034 (TREE_THIS_VOLATILE (tmpl_type)
11035 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11036 parent_die);
11038 else
11040 /* So TMPL_DIE is a DIE representing a
11041 a generic generic template parameter, a.k.a template template
11042 parameter in C++ and arg is a template. */
11044 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11045 to the name of the argument. */
11046 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11047 if (name)
11048 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11051 if (TREE_CODE (parm) == PARM_DECL)
11052 /* So PARM is a non-type generic parameter.
11053 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11054 attribute of TMPL_DIE which value represents the value
11055 of ARG.
11056 We must be careful here:
11057 The value of ARG might reference some function decls.
11058 We might currently be emitting debug info for a generic
11059 type and types are emitted before function decls, we don't
11060 know if the function decls referenced by ARG will actually be
11061 emitted after cgraph computations.
11062 So must defer the generation of the DW_AT_const_value to
11063 after cgraph is ready. */
11064 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11067 return tmpl_die;
11070 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11071 PARM_PACK must be a template parameter pack. The returned DIE
11072 will be child DIE of PARENT_DIE. */
11074 static dw_die_ref
11075 template_parameter_pack_die (tree parm_pack,
11076 tree parm_pack_args,
11077 dw_die_ref parent_die)
11079 dw_die_ref die;
11080 int j;
11082 gcc_assert (parent_die && parm_pack);
11084 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11085 add_name_and_src_coords_attributes (die, parm_pack);
11086 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11087 generic_parameter_die (parm_pack,
11088 TREE_VEC_ELT (parm_pack_args, j),
11089 false /* Don't emit DW_AT_name */,
11090 die);
11091 return die;
11094 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11095 an enumerated type. */
11097 static inline int
11098 type_is_enum (const_tree type)
11100 return TREE_CODE (type) == ENUMERAL_TYPE;
11103 /* Return the DBX register number described by a given RTL node. */
11105 static unsigned int
11106 dbx_reg_number (const_rtx rtl)
11108 unsigned regno = REGNO (rtl);
11110 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11112 #ifdef LEAF_REG_REMAP
11113 if (crtl->uses_only_leaf_regs)
11115 int leaf_reg = LEAF_REG_REMAP (regno);
11116 if (leaf_reg != -1)
11117 regno = (unsigned) leaf_reg;
11119 #endif
11121 regno = DBX_REGISTER_NUMBER (regno);
11122 gcc_assert (regno != INVALID_REGNUM);
11123 return regno;
11126 /* Optionally add a DW_OP_piece term to a location description expression.
11127 DW_OP_piece is only added if the location description expression already
11128 doesn't end with DW_OP_piece. */
11130 static void
11131 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11133 dw_loc_descr_ref loc;
11135 if (*list_head != NULL)
11137 /* Find the end of the chain. */
11138 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11141 if (loc->dw_loc_opc != DW_OP_piece)
11142 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11146 /* Return a location descriptor that designates a machine register or
11147 zero if there is none. */
11149 static dw_loc_descr_ref
11150 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11152 rtx regs;
11154 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11155 return 0;
11157 /* We only use "frame base" when we're sure we're talking about the
11158 post-prologue local stack frame. We do this by *not* running
11159 register elimination until this point, and recognizing the special
11160 argument pointer and soft frame pointer rtx's.
11161 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11162 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11163 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11165 dw_loc_descr_ref result = NULL;
11167 if (dwarf_version >= 4 || !dwarf_strict)
11169 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11170 initialized);
11171 if (result)
11172 add_loc_descr (&result,
11173 new_loc_descr (DW_OP_stack_value, 0, 0));
11175 return result;
11178 regs = targetm.dwarf_register_span (rtl);
11180 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11181 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11182 else
11184 unsigned int dbx_regnum = dbx_reg_number (rtl);
11185 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11186 return 0;
11187 return one_reg_loc_descriptor (dbx_regnum, initialized);
11191 /* Return a location descriptor that designates a machine register for
11192 a given hard register number. */
11194 static dw_loc_descr_ref
11195 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11197 dw_loc_descr_ref reg_loc_descr;
11199 if (regno <= 31)
11200 reg_loc_descr
11201 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11202 else
11203 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11205 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11206 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11208 return reg_loc_descr;
11211 /* Given an RTL of a register, return a location descriptor that
11212 designates a value that spans more than one register. */
11214 static dw_loc_descr_ref
11215 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11216 enum var_init_status initialized)
11218 int size, i;
11219 dw_loc_descr_ref loc_result = NULL;
11221 /* Simple, contiguous registers. */
11222 if (regs == NULL_RTX)
11224 unsigned reg = REGNO (rtl);
11225 int nregs;
11227 #ifdef LEAF_REG_REMAP
11228 if (crtl->uses_only_leaf_regs)
11230 int leaf_reg = LEAF_REG_REMAP (reg);
11231 if (leaf_reg != -1)
11232 reg = (unsigned) leaf_reg;
11234 #endif
11236 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11237 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11239 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11241 loc_result = NULL;
11242 while (nregs--)
11244 dw_loc_descr_ref t;
11246 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11247 VAR_INIT_STATUS_INITIALIZED);
11248 add_loc_descr (&loc_result, t);
11249 add_loc_descr_op_piece (&loc_result, size);
11250 ++reg;
11252 return loc_result;
11255 /* Now onto stupid register sets in non contiguous locations. */
11257 gcc_assert (GET_CODE (regs) == PARALLEL);
11259 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11260 loc_result = NULL;
11262 for (i = 0; i < XVECLEN (regs, 0); ++i)
11264 dw_loc_descr_ref t;
11266 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11267 VAR_INIT_STATUS_INITIALIZED);
11268 add_loc_descr (&loc_result, t);
11269 add_loc_descr_op_piece (&loc_result, size);
11272 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11273 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11274 return loc_result;
11277 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11279 /* Return a location descriptor that designates a constant i,
11280 as a compound operation from constant (i >> shift), constant shift
11281 and DW_OP_shl. */
11283 static dw_loc_descr_ref
11284 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11286 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11287 add_loc_descr (&ret, int_loc_descriptor (shift));
11288 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11289 return ret;
11292 /* Return a location descriptor that designates a constant. */
11294 static dw_loc_descr_ref
11295 int_loc_descriptor (HOST_WIDE_INT i)
11297 enum dwarf_location_atom op;
11299 /* Pick the smallest representation of a constant, rather than just
11300 defaulting to the LEB encoding. */
11301 if (i >= 0)
11303 int clz = clz_hwi (i);
11304 int ctz = ctz_hwi (i);
11305 if (i <= 31)
11306 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11307 else if (i <= 0xff)
11308 op = DW_OP_const1u;
11309 else if (i <= 0xffff)
11310 op = DW_OP_const2u;
11311 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11312 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11313 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11314 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11315 while DW_OP_const4u is 5 bytes. */
11316 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11317 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11318 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11319 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11320 while DW_OP_const4u is 5 bytes. */
11321 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11322 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11323 op = DW_OP_const4u;
11324 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11325 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11326 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11327 while DW_OP_constu of constant >= 0x100000000 takes at least
11328 6 bytes. */
11329 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11330 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11331 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11332 >= HOST_BITS_PER_WIDE_INT)
11333 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11334 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11335 while DW_OP_constu takes in this case at least 6 bytes. */
11336 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11337 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11338 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11339 && size_of_uleb128 (i) > 6)
11340 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11341 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11342 else
11343 op = DW_OP_constu;
11345 else
11347 if (i >= -0x80)
11348 op = DW_OP_const1s;
11349 else if (i >= -0x8000)
11350 op = DW_OP_const2s;
11351 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11353 if (size_of_int_loc_descriptor (i) < 5)
11355 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11356 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11357 return ret;
11359 op = DW_OP_const4s;
11361 else
11363 if (size_of_int_loc_descriptor (i)
11364 < (unsigned long) 1 + size_of_sleb128 (i))
11366 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11367 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11368 return ret;
11370 op = DW_OP_consts;
11374 return new_loc_descr (op, i, 0);
11377 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11378 without actually allocating it. */
11380 static unsigned long
11381 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11383 return size_of_int_loc_descriptor (i >> shift)
11384 + size_of_int_loc_descriptor (shift)
11385 + 1;
11388 /* Return size_of_locs (int_loc_descriptor (i)) without
11389 actually allocating it. */
11391 static unsigned long
11392 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11394 unsigned long s;
11396 if (i >= 0)
11398 int clz, ctz;
11399 if (i <= 31)
11400 return 1;
11401 else if (i <= 0xff)
11402 return 2;
11403 else if (i <= 0xffff)
11404 return 3;
11405 clz = clz_hwi (i);
11406 ctz = ctz_hwi (i);
11407 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11408 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11409 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11410 - clz - 5);
11411 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11412 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11413 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11414 - clz - 8);
11415 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11416 return 5;
11417 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11418 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11419 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11420 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11421 - clz - 8);
11422 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11423 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11424 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11425 - clz - 16);
11426 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11427 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11428 && s > 6)
11429 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11430 - clz - 32);
11431 else
11432 return 1 + s;
11434 else
11436 if (i >= -0x80)
11437 return 2;
11438 else if (i >= -0x8000)
11439 return 3;
11440 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11442 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11444 s = size_of_int_loc_descriptor (-i) + 1;
11445 if (s < 5)
11446 return s;
11448 return 5;
11450 else
11452 unsigned long r = 1 + size_of_sleb128 (i);
11453 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11455 s = size_of_int_loc_descriptor (-i) + 1;
11456 if (s < r)
11457 return s;
11459 return r;
11464 /* Return loc description representing "address" of integer value.
11465 This can appear only as toplevel expression. */
11467 static dw_loc_descr_ref
11468 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11470 int litsize;
11471 dw_loc_descr_ref loc_result = NULL;
11473 if (!(dwarf_version >= 4 || !dwarf_strict))
11474 return NULL;
11476 litsize = size_of_int_loc_descriptor (i);
11477 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11478 is more compact. For DW_OP_stack_value we need:
11479 litsize + 1 (DW_OP_stack_value)
11480 and for DW_OP_implicit_value:
11481 1 (DW_OP_implicit_value) + 1 (length) + size. */
11482 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11484 loc_result = int_loc_descriptor (i);
11485 add_loc_descr (&loc_result,
11486 new_loc_descr (DW_OP_stack_value, 0, 0));
11487 return loc_result;
11490 loc_result = new_loc_descr (DW_OP_implicit_value,
11491 size, 0);
11492 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11493 loc_result->dw_loc_oprnd2.v.val_int = i;
11494 return loc_result;
11497 /* Return a location descriptor that designates a base+offset location. */
11499 static dw_loc_descr_ref
11500 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11501 enum var_init_status initialized)
11503 unsigned int regno;
11504 dw_loc_descr_ref result;
11505 dw_fde_ref fde = cfun->fde;
11507 /* We only use "frame base" when we're sure we're talking about the
11508 post-prologue local stack frame. We do this by *not* running
11509 register elimination until this point, and recognizing the special
11510 argument pointer and soft frame pointer rtx's. */
11511 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11513 rtx elim = (ira_use_lra_p
11514 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11515 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11517 if (elim != reg)
11519 if (GET_CODE (elim) == PLUS)
11521 offset += INTVAL (XEXP (elim, 1));
11522 elim = XEXP (elim, 0);
11524 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11525 && (elim == hard_frame_pointer_rtx
11526 || elim == stack_pointer_rtx))
11527 || elim == (frame_pointer_needed
11528 ? hard_frame_pointer_rtx
11529 : stack_pointer_rtx));
11531 /* If drap register is used to align stack, use frame
11532 pointer + offset to access stack variables. If stack
11533 is aligned without drap, use stack pointer + offset to
11534 access stack variables. */
11535 if (crtl->stack_realign_tried
11536 && reg == frame_pointer_rtx)
11538 int base_reg
11539 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11540 ? HARD_FRAME_POINTER_REGNUM
11541 : REGNO (elim));
11542 return new_reg_loc_descr (base_reg, offset);
11545 gcc_assert (frame_pointer_fb_offset_valid);
11546 offset += frame_pointer_fb_offset;
11547 return new_loc_descr (DW_OP_fbreg, offset, 0);
11551 regno = REGNO (reg);
11552 #ifdef LEAF_REG_REMAP
11553 if (crtl->uses_only_leaf_regs)
11555 int leaf_reg = LEAF_REG_REMAP (regno);
11556 if (leaf_reg != -1)
11557 regno = (unsigned) leaf_reg;
11559 #endif
11560 regno = DWARF_FRAME_REGNUM (regno);
11562 if (!optimize && fde
11563 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11565 /* Use cfa+offset to represent the location of arguments passed
11566 on the stack when drap is used to align stack.
11567 Only do this when not optimizing, for optimized code var-tracking
11568 is supposed to track where the arguments live and the register
11569 used as vdrap or drap in some spot might be used for something
11570 else in other part of the routine. */
11571 return new_loc_descr (DW_OP_fbreg, offset, 0);
11574 if (regno <= 31)
11575 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11576 offset, 0);
11577 else
11578 result = new_loc_descr (DW_OP_bregx, regno, offset);
11580 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11581 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11583 return result;
11586 /* Return true if this RTL expression describes a base+offset calculation. */
11588 static inline int
11589 is_based_loc (const_rtx rtl)
11591 return (GET_CODE (rtl) == PLUS
11592 && ((REG_P (XEXP (rtl, 0))
11593 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11594 && CONST_INT_P (XEXP (rtl, 1)))));
11597 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11598 failed. */
11600 static dw_loc_descr_ref
11601 tls_mem_loc_descriptor (rtx mem)
11603 tree base;
11604 dw_loc_descr_ref loc_result;
11606 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11607 return NULL;
11609 base = get_base_address (MEM_EXPR (mem));
11610 if (base == NULL
11611 || TREE_CODE (base) != VAR_DECL
11612 || !DECL_THREAD_LOCAL_P (base))
11613 return NULL;
11615 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11616 if (loc_result == NULL)
11617 return NULL;
11619 if (MEM_OFFSET (mem))
11620 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11622 return loc_result;
11625 /* Output debug info about reason why we failed to expand expression as dwarf
11626 expression. */
11628 static void
11629 expansion_failed (tree expr, rtx rtl, char const *reason)
11631 if (dump_file && (dump_flags & TDF_DETAILS))
11633 fprintf (dump_file, "Failed to expand as dwarf: ");
11634 if (expr)
11635 print_generic_expr (dump_file, expr, dump_flags);
11636 if (rtl)
11638 fprintf (dump_file, "\n");
11639 print_rtl (dump_file, rtl);
11641 fprintf (dump_file, "\nReason: %s\n", reason);
11645 /* Helper function for const_ok_for_output. */
11647 static bool
11648 const_ok_for_output_1 (rtx rtl)
11650 if (GET_CODE (rtl) == UNSPEC)
11652 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11653 we can't express it in the debug info. */
11654 #ifdef ENABLE_CHECKING
11655 /* Don't complain about TLS UNSPECs, those are just too hard to
11656 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11657 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11658 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11659 if (XVECLEN (rtl, 0) == 0
11660 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11661 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11662 inform (current_function_decl
11663 ? DECL_SOURCE_LOCATION (current_function_decl)
11664 : UNKNOWN_LOCATION,
11665 #if NUM_UNSPEC_VALUES > 0
11666 "non-delegitimized UNSPEC %s (%d) found in variable location",
11667 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11668 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11669 XINT (rtl, 1));
11670 #else
11671 "non-delegitimized UNSPEC %d found in variable location",
11672 XINT (rtl, 1));
11673 #endif
11674 #endif
11675 expansion_failed (NULL_TREE, rtl,
11676 "UNSPEC hasn't been delegitimized.\n");
11677 return false;
11680 if (targetm.const_not_ok_for_debug_p (rtl))
11682 expansion_failed (NULL_TREE, rtl,
11683 "Expression rejected for debug by the backend.\n");
11684 return false;
11687 /* FIXME: Refer to PR60655. It is possible for simplification
11688 of rtl expressions in var tracking to produce such expressions.
11689 We should really identify / validate expressions
11690 enclosed in CONST that can be handled by assemblers on various
11691 targets and only handle legitimate cases here. */
11692 if (GET_CODE (rtl) != SYMBOL_REF)
11694 if (GET_CODE (rtl) == NOT)
11695 return false;
11696 return true;
11699 if (CONSTANT_POOL_ADDRESS_P (rtl))
11701 bool marked;
11702 get_pool_constant_mark (rtl, &marked);
11703 /* If all references to this pool constant were optimized away,
11704 it was not output and thus we can't represent it. */
11705 if (!marked)
11707 expansion_failed (NULL_TREE, rtl,
11708 "Constant was removed from constant pool.\n");
11709 return false;
11713 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11714 return false;
11716 /* Avoid references to external symbols in debug info, on several targets
11717 the linker might even refuse to link when linking a shared library,
11718 and in many other cases the relocations for .debug_info/.debug_loc are
11719 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11720 to be defined within the same shared library or executable are fine. */
11721 if (SYMBOL_REF_EXTERNAL_P (rtl))
11723 tree decl = SYMBOL_REF_DECL (rtl);
11725 if (decl == NULL || !targetm.binds_local_p (decl))
11727 expansion_failed (NULL_TREE, rtl,
11728 "Symbol not defined in current TU.\n");
11729 return false;
11733 return true;
11736 /* Return true if constant RTL can be emitted in DW_OP_addr or
11737 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11738 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11740 static bool
11741 const_ok_for_output (rtx rtl)
11743 if (GET_CODE (rtl) == SYMBOL_REF)
11744 return const_ok_for_output_1 (rtl);
11746 if (GET_CODE (rtl) == CONST)
11748 subrtx_var_iterator::array_type array;
11749 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11750 if (!const_ok_for_output_1 (*iter))
11751 return false;
11752 return true;
11755 return true;
11758 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11759 if possible, NULL otherwise. */
11761 static dw_die_ref
11762 base_type_for_mode (machine_mode mode, bool unsignedp)
11764 dw_die_ref type_die;
11765 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11767 if (type == NULL)
11768 return NULL;
11769 switch (TREE_CODE (type))
11771 case INTEGER_TYPE:
11772 case REAL_TYPE:
11773 break;
11774 default:
11775 return NULL;
11777 type_die = lookup_type_die (type);
11778 if (!type_die)
11779 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11780 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11781 return NULL;
11782 return type_die;
11785 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11786 type matching MODE, or, if MODE is narrower than or as wide as
11787 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11788 possible. */
11790 static dw_loc_descr_ref
11791 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11793 machine_mode outer_mode = mode;
11794 dw_die_ref type_die;
11795 dw_loc_descr_ref cvt;
11797 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11799 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11800 return op;
11802 type_die = base_type_for_mode (outer_mode, 1);
11803 if (type_die == NULL)
11804 return NULL;
11805 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11806 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11807 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11808 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11809 add_loc_descr (&op, cvt);
11810 return op;
11813 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11815 static dw_loc_descr_ref
11816 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11817 dw_loc_descr_ref op1)
11819 dw_loc_descr_ref ret = op0;
11820 add_loc_descr (&ret, op1);
11821 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11822 if (STORE_FLAG_VALUE != 1)
11824 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11825 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11827 return ret;
11830 /* Return location descriptor for signed comparison OP RTL. */
11832 static dw_loc_descr_ref
11833 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11834 machine_mode mem_mode)
11836 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11837 dw_loc_descr_ref op0, op1;
11838 int shift;
11840 if (op_mode == VOIDmode)
11841 op_mode = GET_MODE (XEXP (rtl, 1));
11842 if (op_mode == VOIDmode)
11843 return NULL;
11845 if (dwarf_strict
11846 && (GET_MODE_CLASS (op_mode) != MODE_INT
11847 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11848 return NULL;
11850 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11851 VAR_INIT_STATUS_INITIALIZED);
11852 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11853 VAR_INIT_STATUS_INITIALIZED);
11855 if (op0 == NULL || op1 == NULL)
11856 return NULL;
11858 if (GET_MODE_CLASS (op_mode) != MODE_INT
11859 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11860 return compare_loc_descriptor (op, op0, op1);
11862 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11864 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11865 dw_loc_descr_ref cvt;
11867 if (type_die == NULL)
11868 return NULL;
11869 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11870 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11871 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11872 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11873 add_loc_descr (&op0, cvt);
11874 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11875 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11876 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11877 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11878 add_loc_descr (&op1, cvt);
11879 return compare_loc_descriptor (op, op0, op1);
11882 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11883 /* For eq/ne, if the operands are known to be zero-extended,
11884 there is no need to do the fancy shifting up. */
11885 if (op == DW_OP_eq || op == DW_OP_ne)
11887 dw_loc_descr_ref last0, last1;
11888 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11890 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11892 /* deref_size zero extends, and for constants we can check
11893 whether they are zero extended or not. */
11894 if (((last0->dw_loc_opc == DW_OP_deref_size
11895 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11896 || (CONST_INT_P (XEXP (rtl, 0))
11897 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11898 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11899 && ((last1->dw_loc_opc == DW_OP_deref_size
11900 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11901 || (CONST_INT_P (XEXP (rtl, 1))
11902 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11903 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11904 return compare_loc_descriptor (op, op0, op1);
11906 /* EQ/NE comparison against constant in narrower type than
11907 DWARF2_ADDR_SIZE can be performed either as
11908 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11909 DW_OP_{eq,ne}
11911 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11912 DW_OP_{eq,ne}. Pick whatever is shorter. */
11913 if (CONST_INT_P (XEXP (rtl, 1))
11914 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11915 && (size_of_int_loc_descriptor (shift) + 1
11916 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11917 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11918 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11919 & GET_MODE_MASK (op_mode))))
11921 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11922 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11923 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11924 & GET_MODE_MASK (op_mode));
11925 return compare_loc_descriptor (op, op0, op1);
11928 add_loc_descr (&op0, int_loc_descriptor (shift));
11929 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11930 if (CONST_INT_P (XEXP (rtl, 1)))
11931 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11932 else
11934 add_loc_descr (&op1, int_loc_descriptor (shift));
11935 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11937 return compare_loc_descriptor (op, op0, op1);
11940 /* Return location descriptor for unsigned comparison OP RTL. */
11942 static dw_loc_descr_ref
11943 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11944 machine_mode mem_mode)
11946 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11947 dw_loc_descr_ref op0, op1;
11949 if (op_mode == VOIDmode)
11950 op_mode = GET_MODE (XEXP (rtl, 1));
11951 if (op_mode == VOIDmode)
11952 return NULL;
11953 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11954 return NULL;
11956 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11957 return NULL;
11959 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11960 VAR_INIT_STATUS_INITIALIZED);
11961 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11962 VAR_INIT_STATUS_INITIALIZED);
11964 if (op0 == NULL || op1 == NULL)
11965 return NULL;
11967 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11969 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11970 dw_loc_descr_ref last0, last1;
11971 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11973 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11975 if (CONST_INT_P (XEXP (rtl, 0)))
11976 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11977 /* deref_size zero extends, so no need to mask it again. */
11978 else if (last0->dw_loc_opc != DW_OP_deref_size
11979 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11981 add_loc_descr (&op0, int_loc_descriptor (mask));
11982 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11984 if (CONST_INT_P (XEXP (rtl, 1)))
11985 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11986 /* deref_size zero extends, so no need to mask it again. */
11987 else if (last1->dw_loc_opc != DW_OP_deref_size
11988 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11990 add_loc_descr (&op1, int_loc_descriptor (mask));
11991 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11994 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11996 HOST_WIDE_INT bias = 1;
11997 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11998 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11999 if (CONST_INT_P (XEXP (rtl, 1)))
12000 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12001 + INTVAL (XEXP (rtl, 1)));
12002 else
12003 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12004 bias, 0));
12006 return compare_loc_descriptor (op, op0, op1);
12009 /* Return location descriptor for {U,S}{MIN,MAX}. */
12011 static dw_loc_descr_ref
12012 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12013 machine_mode mem_mode)
12015 enum dwarf_location_atom op;
12016 dw_loc_descr_ref op0, op1, ret;
12017 dw_loc_descr_ref bra_node, drop_node;
12019 if (dwarf_strict
12020 && (GET_MODE_CLASS (mode) != MODE_INT
12021 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12022 return NULL;
12024 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12025 VAR_INIT_STATUS_INITIALIZED);
12026 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12027 VAR_INIT_STATUS_INITIALIZED);
12029 if (op0 == NULL || op1 == NULL)
12030 return NULL;
12032 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12033 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12034 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12035 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12037 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12039 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12040 add_loc_descr (&op0, int_loc_descriptor (mask));
12041 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12042 add_loc_descr (&op1, int_loc_descriptor (mask));
12043 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12045 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12047 HOST_WIDE_INT bias = 1;
12048 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12049 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12050 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12053 else if (GET_MODE_CLASS (mode) == MODE_INT
12054 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12056 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12057 add_loc_descr (&op0, int_loc_descriptor (shift));
12058 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12059 add_loc_descr (&op1, int_loc_descriptor (shift));
12060 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12062 else if (GET_MODE_CLASS (mode) == MODE_INT
12063 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12065 dw_die_ref type_die = base_type_for_mode (mode, 0);
12066 dw_loc_descr_ref cvt;
12067 if (type_die == NULL)
12068 return NULL;
12069 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12070 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12071 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12072 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12073 add_loc_descr (&op0, cvt);
12074 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12075 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12076 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12077 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12078 add_loc_descr (&op1, cvt);
12081 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12082 op = DW_OP_lt;
12083 else
12084 op = DW_OP_gt;
12085 ret = op0;
12086 add_loc_descr (&ret, op1);
12087 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12088 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12089 add_loc_descr (&ret, bra_node);
12090 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12091 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12092 add_loc_descr (&ret, drop_node);
12093 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12094 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12095 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12096 && GET_MODE_CLASS (mode) == MODE_INT
12097 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12098 ret = convert_descriptor_to_mode (mode, ret);
12099 return ret;
12102 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12103 but after converting arguments to type_die, afterwards
12104 convert back to unsigned. */
12106 static dw_loc_descr_ref
12107 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12108 machine_mode mode, machine_mode mem_mode)
12110 dw_loc_descr_ref cvt, op0, op1;
12112 if (type_die == NULL)
12113 return NULL;
12114 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12115 VAR_INIT_STATUS_INITIALIZED);
12116 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12117 VAR_INIT_STATUS_INITIALIZED);
12118 if (op0 == NULL || op1 == NULL)
12119 return NULL;
12120 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12121 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12122 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12123 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12124 add_loc_descr (&op0, cvt);
12125 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12126 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12127 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12128 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12129 add_loc_descr (&op1, cvt);
12130 add_loc_descr (&op0, op1);
12131 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12132 return convert_descriptor_to_mode (mode, op0);
12135 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12136 const0 is DW_OP_lit0 or corresponding typed constant,
12137 const1 is DW_OP_lit1 or corresponding typed constant
12138 and constMSB is constant with just the MSB bit set
12139 for the mode):
12140 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12141 L1: const0 DW_OP_swap
12142 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12143 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12144 L3: DW_OP_drop
12145 L4: DW_OP_nop
12147 CTZ is similar:
12148 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12149 L1: const0 DW_OP_swap
12150 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12151 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12152 L3: DW_OP_drop
12153 L4: DW_OP_nop
12155 FFS is similar:
12156 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12157 L1: const1 DW_OP_swap
12158 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12159 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12160 L3: DW_OP_drop
12161 L4: DW_OP_nop */
12163 static dw_loc_descr_ref
12164 clz_loc_descriptor (rtx rtl, machine_mode mode,
12165 machine_mode mem_mode)
12167 dw_loc_descr_ref op0, ret, tmp;
12168 HOST_WIDE_INT valv;
12169 dw_loc_descr_ref l1jump, l1label;
12170 dw_loc_descr_ref l2jump, l2label;
12171 dw_loc_descr_ref l3jump, l3label;
12172 dw_loc_descr_ref l4jump, l4label;
12173 rtx msb;
12175 if (GET_MODE_CLASS (mode) != MODE_INT
12176 || GET_MODE (XEXP (rtl, 0)) != mode)
12177 return NULL;
12179 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12180 VAR_INIT_STATUS_INITIALIZED);
12181 if (op0 == NULL)
12182 return NULL;
12183 ret = op0;
12184 if (GET_CODE (rtl) == CLZ)
12186 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12187 valv = GET_MODE_BITSIZE (mode);
12189 else if (GET_CODE (rtl) == FFS)
12190 valv = 0;
12191 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12192 valv = GET_MODE_BITSIZE (mode);
12193 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12194 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12195 add_loc_descr (&ret, l1jump);
12196 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12197 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12198 VAR_INIT_STATUS_INITIALIZED);
12199 if (tmp == NULL)
12200 return NULL;
12201 add_loc_descr (&ret, tmp);
12202 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12203 add_loc_descr (&ret, l4jump);
12204 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12205 ? const1_rtx : const0_rtx,
12206 mode, mem_mode,
12207 VAR_INIT_STATUS_INITIALIZED);
12208 if (l1label == NULL)
12209 return NULL;
12210 add_loc_descr (&ret, l1label);
12211 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12212 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12213 add_loc_descr (&ret, l2label);
12214 if (GET_CODE (rtl) != CLZ)
12215 msb = const1_rtx;
12216 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12217 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12218 << (GET_MODE_BITSIZE (mode) - 1));
12219 else
12220 msb = immed_wide_int_const
12221 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12222 GET_MODE_PRECISION (mode)), mode);
12223 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12224 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12225 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12226 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12227 else
12228 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12229 VAR_INIT_STATUS_INITIALIZED);
12230 if (tmp == NULL)
12231 return NULL;
12232 add_loc_descr (&ret, tmp);
12233 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12234 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12235 add_loc_descr (&ret, l3jump);
12236 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12237 VAR_INIT_STATUS_INITIALIZED);
12238 if (tmp == NULL)
12239 return NULL;
12240 add_loc_descr (&ret, tmp);
12241 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12242 ? DW_OP_shl : DW_OP_shr, 0, 0));
12243 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12244 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12245 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12246 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12247 add_loc_descr (&ret, l2jump);
12248 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12249 add_loc_descr (&ret, l3label);
12250 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12251 add_loc_descr (&ret, l4label);
12252 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12253 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12254 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12255 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12256 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12257 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12258 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12259 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12260 return ret;
12263 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12264 const1 is DW_OP_lit1 or corresponding typed constant):
12265 const0 DW_OP_swap
12266 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12267 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12268 L2: DW_OP_drop
12270 PARITY is similar:
12271 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12272 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12273 L2: DW_OP_drop */
12275 static dw_loc_descr_ref
12276 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12277 machine_mode mem_mode)
12279 dw_loc_descr_ref op0, ret, tmp;
12280 dw_loc_descr_ref l1jump, l1label;
12281 dw_loc_descr_ref l2jump, l2label;
12283 if (GET_MODE_CLASS (mode) != MODE_INT
12284 || GET_MODE (XEXP (rtl, 0)) != mode)
12285 return NULL;
12287 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12288 VAR_INIT_STATUS_INITIALIZED);
12289 if (op0 == NULL)
12290 return NULL;
12291 ret = op0;
12292 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12293 VAR_INIT_STATUS_INITIALIZED);
12294 if (tmp == NULL)
12295 return NULL;
12296 add_loc_descr (&ret, tmp);
12297 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12298 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12299 add_loc_descr (&ret, l1label);
12300 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12301 add_loc_descr (&ret, l2jump);
12302 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12303 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12304 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12305 VAR_INIT_STATUS_INITIALIZED);
12306 if (tmp == NULL)
12307 return NULL;
12308 add_loc_descr (&ret, tmp);
12309 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12310 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12311 ? DW_OP_plus : DW_OP_xor, 0, 0));
12312 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12313 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12314 VAR_INIT_STATUS_INITIALIZED);
12315 add_loc_descr (&ret, tmp);
12316 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12317 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12318 add_loc_descr (&ret, l1jump);
12319 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12320 add_loc_descr (&ret, l2label);
12321 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12322 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12323 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12324 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12325 return ret;
12328 /* BSWAP (constS is initial shift count, either 56 or 24):
12329 constS const0
12330 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12331 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12332 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12333 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12334 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12336 static dw_loc_descr_ref
12337 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12338 machine_mode mem_mode)
12340 dw_loc_descr_ref op0, ret, tmp;
12341 dw_loc_descr_ref l1jump, l1label;
12342 dw_loc_descr_ref l2jump, l2label;
12344 if (GET_MODE_CLASS (mode) != MODE_INT
12345 || BITS_PER_UNIT != 8
12346 || (GET_MODE_BITSIZE (mode) != 32
12347 && GET_MODE_BITSIZE (mode) != 64))
12348 return NULL;
12350 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12351 VAR_INIT_STATUS_INITIALIZED);
12352 if (op0 == NULL)
12353 return NULL;
12355 ret = op0;
12356 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12357 mode, mem_mode,
12358 VAR_INIT_STATUS_INITIALIZED);
12359 if (tmp == NULL)
12360 return NULL;
12361 add_loc_descr (&ret, tmp);
12362 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12363 VAR_INIT_STATUS_INITIALIZED);
12364 if (tmp == NULL)
12365 return NULL;
12366 add_loc_descr (&ret, tmp);
12367 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12368 add_loc_descr (&ret, l1label);
12369 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12370 mode, mem_mode,
12371 VAR_INIT_STATUS_INITIALIZED);
12372 add_loc_descr (&ret, tmp);
12373 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12374 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12375 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12376 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12377 VAR_INIT_STATUS_INITIALIZED);
12378 if (tmp == NULL)
12379 return NULL;
12380 add_loc_descr (&ret, tmp);
12381 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12382 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12383 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12384 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12385 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12386 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12387 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12388 VAR_INIT_STATUS_INITIALIZED);
12389 add_loc_descr (&ret, tmp);
12390 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12391 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12392 add_loc_descr (&ret, l2jump);
12393 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12394 VAR_INIT_STATUS_INITIALIZED);
12395 add_loc_descr (&ret, tmp);
12396 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12397 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12398 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12399 add_loc_descr (&ret, l1jump);
12400 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12401 add_loc_descr (&ret, l2label);
12402 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12403 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12404 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12405 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12406 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12407 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12408 return ret;
12411 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12412 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12413 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12414 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12416 ROTATERT is similar:
12417 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12418 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12419 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12421 static dw_loc_descr_ref
12422 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12423 machine_mode mem_mode)
12425 rtx rtlop1 = XEXP (rtl, 1);
12426 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12427 int i;
12429 if (GET_MODE_CLASS (mode) != MODE_INT)
12430 return NULL;
12432 if (GET_MODE (rtlop1) != VOIDmode
12433 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12434 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12435 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12436 VAR_INIT_STATUS_INITIALIZED);
12437 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12438 VAR_INIT_STATUS_INITIALIZED);
12439 if (op0 == NULL || op1 == NULL)
12440 return NULL;
12441 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12442 for (i = 0; i < 2; i++)
12444 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12445 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12446 mode, mem_mode,
12447 VAR_INIT_STATUS_INITIALIZED);
12448 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12449 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12450 ? DW_OP_const4u
12451 : HOST_BITS_PER_WIDE_INT == 64
12452 ? DW_OP_const8u : DW_OP_constu,
12453 GET_MODE_MASK (mode), 0);
12454 else
12455 mask[i] = NULL;
12456 if (mask[i] == NULL)
12457 return NULL;
12458 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12460 ret = op0;
12461 add_loc_descr (&ret, op1);
12462 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12463 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12464 if (GET_CODE (rtl) == ROTATERT)
12466 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12467 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12468 GET_MODE_BITSIZE (mode), 0));
12470 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12471 if (mask[0] != NULL)
12472 add_loc_descr (&ret, mask[0]);
12473 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12474 if (mask[1] != NULL)
12476 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12477 add_loc_descr (&ret, mask[1]);
12478 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12480 if (GET_CODE (rtl) == ROTATE)
12482 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12483 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12484 GET_MODE_BITSIZE (mode), 0));
12486 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12487 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12488 return ret;
12491 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12492 for DEBUG_PARAMETER_REF RTL. */
12494 static dw_loc_descr_ref
12495 parameter_ref_descriptor (rtx rtl)
12497 dw_loc_descr_ref ret;
12498 dw_die_ref ref;
12500 if (dwarf_strict)
12501 return NULL;
12502 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12503 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12504 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12505 if (ref)
12507 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12508 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12509 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12511 else
12513 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12514 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12516 return ret;
12519 /* The following routine converts the RTL for a variable or parameter
12520 (resident in memory) into an equivalent Dwarf representation of a
12521 mechanism for getting the address of that same variable onto the top of a
12522 hypothetical "address evaluation" stack.
12524 When creating memory location descriptors, we are effectively transforming
12525 the RTL for a memory-resident object into its Dwarf postfix expression
12526 equivalent. This routine recursively descends an RTL tree, turning
12527 it into Dwarf postfix code as it goes.
12529 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12531 MEM_MODE is the mode of the memory reference, needed to handle some
12532 autoincrement addressing modes.
12534 Return 0 if we can't represent the location. */
12536 dw_loc_descr_ref
12537 mem_loc_descriptor (rtx rtl, machine_mode mode,
12538 machine_mode mem_mode,
12539 enum var_init_status initialized)
12541 dw_loc_descr_ref mem_loc_result = NULL;
12542 enum dwarf_location_atom op;
12543 dw_loc_descr_ref op0, op1;
12544 rtx inner = NULL_RTX;
12546 if (mode == VOIDmode)
12547 mode = GET_MODE (rtl);
12549 /* Note that for a dynamically sized array, the location we will generate a
12550 description of here will be the lowest numbered location which is
12551 actually within the array. That's *not* necessarily the same as the
12552 zeroth element of the array. */
12554 rtl = targetm.delegitimize_address (rtl);
12556 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12557 return NULL;
12559 switch (GET_CODE (rtl))
12561 case POST_INC:
12562 case POST_DEC:
12563 case POST_MODIFY:
12564 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12566 case SUBREG:
12567 /* The case of a subreg may arise when we have a local (register)
12568 variable or a formal (register) parameter which doesn't quite fill
12569 up an entire register. For now, just assume that it is
12570 legitimate to make the Dwarf info refer to the whole register which
12571 contains the given subreg. */
12572 if (!subreg_lowpart_p (rtl))
12573 break;
12574 inner = SUBREG_REG (rtl);
12575 case TRUNCATE:
12576 if (inner == NULL_RTX)
12577 inner = XEXP (rtl, 0);
12578 if (GET_MODE_CLASS (mode) == MODE_INT
12579 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12580 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12581 #ifdef POINTERS_EXTEND_UNSIGNED
12582 || (mode == Pmode && mem_mode != VOIDmode)
12583 #endif
12585 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12587 mem_loc_result = mem_loc_descriptor (inner,
12588 GET_MODE (inner),
12589 mem_mode, initialized);
12590 break;
12592 if (dwarf_strict)
12593 break;
12594 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12595 break;
12596 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12597 && (GET_MODE_CLASS (mode) != MODE_INT
12598 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12599 break;
12600 else
12602 dw_die_ref type_die;
12603 dw_loc_descr_ref cvt;
12605 mem_loc_result = mem_loc_descriptor (inner,
12606 GET_MODE (inner),
12607 mem_mode, initialized);
12608 if (mem_loc_result == NULL)
12609 break;
12610 type_die = base_type_for_mode (mode,
12611 GET_MODE_CLASS (mode) == MODE_INT);
12612 if (type_die == NULL)
12614 mem_loc_result = NULL;
12615 break;
12617 if (GET_MODE_SIZE (mode)
12618 != GET_MODE_SIZE (GET_MODE (inner)))
12619 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12620 else
12621 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12622 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12623 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12624 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12625 add_loc_descr (&mem_loc_result, cvt);
12627 break;
12629 case REG:
12630 if (GET_MODE_CLASS (mode) != MODE_INT
12631 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12632 && rtl != arg_pointer_rtx
12633 && rtl != frame_pointer_rtx
12634 #ifdef POINTERS_EXTEND_UNSIGNED
12635 && (mode != Pmode || mem_mode == VOIDmode)
12636 #endif
12639 dw_die_ref type_die;
12640 unsigned int dbx_regnum;
12642 if (dwarf_strict)
12643 break;
12644 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12645 break;
12646 type_die = base_type_for_mode (mode,
12647 GET_MODE_CLASS (mode) == MODE_INT);
12648 if (type_die == NULL)
12649 break;
12651 dbx_regnum = dbx_reg_number (rtl);
12652 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12653 break;
12654 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12655 dbx_regnum, 0);
12656 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12657 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12658 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12659 break;
12661 /* Whenever a register number forms a part of the description of the
12662 method for calculating the (dynamic) address of a memory resident
12663 object, DWARF rules require the register number be referred to as
12664 a "base register". This distinction is not based in any way upon
12665 what category of register the hardware believes the given register
12666 belongs to. This is strictly DWARF terminology we're dealing with
12667 here. Note that in cases where the location of a memory-resident
12668 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12669 OP_CONST (0)) the actual DWARF location descriptor that we generate
12670 may just be OP_BASEREG (basereg). This may look deceptively like
12671 the object in question was allocated to a register (rather than in
12672 memory) so DWARF consumers need to be aware of the subtle
12673 distinction between OP_REG and OP_BASEREG. */
12674 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12675 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12676 else if (stack_realign_drap
12677 && crtl->drap_reg
12678 && crtl->args.internal_arg_pointer == rtl
12679 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12681 /* If RTL is internal_arg_pointer, which has been optimized
12682 out, use DRAP instead. */
12683 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12684 VAR_INIT_STATUS_INITIALIZED);
12686 break;
12688 case SIGN_EXTEND:
12689 case ZERO_EXTEND:
12690 if (GET_MODE_CLASS (mode) != MODE_INT)
12691 break;
12692 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12693 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12694 if (op0 == 0)
12695 break;
12696 else if (GET_CODE (rtl) == ZERO_EXTEND
12697 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12698 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12699 < HOST_BITS_PER_WIDE_INT
12700 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12701 to expand zero extend as two shifts instead of
12702 masking. */
12703 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12705 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12706 mem_loc_result = op0;
12707 add_loc_descr (&mem_loc_result,
12708 int_loc_descriptor (GET_MODE_MASK (imode)));
12709 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12711 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12713 int shift = DWARF2_ADDR_SIZE
12714 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12715 shift *= BITS_PER_UNIT;
12716 if (GET_CODE (rtl) == SIGN_EXTEND)
12717 op = DW_OP_shra;
12718 else
12719 op = DW_OP_shr;
12720 mem_loc_result = op0;
12721 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12722 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12723 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12724 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12726 else if (!dwarf_strict)
12728 dw_die_ref type_die1, type_die2;
12729 dw_loc_descr_ref cvt;
12731 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12732 GET_CODE (rtl) == ZERO_EXTEND);
12733 if (type_die1 == NULL)
12734 break;
12735 type_die2 = base_type_for_mode (mode, 1);
12736 if (type_die2 == NULL)
12737 break;
12738 mem_loc_result = op0;
12739 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12740 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12741 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12742 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12743 add_loc_descr (&mem_loc_result, cvt);
12744 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12745 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12746 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12747 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12748 add_loc_descr (&mem_loc_result, cvt);
12750 break;
12752 case MEM:
12754 rtx new_rtl = avoid_constant_pool_reference (rtl);
12755 if (new_rtl != rtl)
12757 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12758 initialized);
12759 if (mem_loc_result != NULL)
12760 return mem_loc_result;
12763 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12764 get_address_mode (rtl), mode,
12765 VAR_INIT_STATUS_INITIALIZED);
12766 if (mem_loc_result == NULL)
12767 mem_loc_result = tls_mem_loc_descriptor (rtl);
12768 if (mem_loc_result != NULL)
12770 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12771 || GET_MODE_CLASS (mode) != MODE_INT)
12773 dw_die_ref type_die;
12774 dw_loc_descr_ref deref;
12776 if (dwarf_strict)
12777 return NULL;
12778 type_die
12779 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12780 if (type_die == NULL)
12781 return NULL;
12782 deref = new_loc_descr (DW_OP_GNU_deref_type,
12783 GET_MODE_SIZE (mode), 0);
12784 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12785 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12786 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12787 add_loc_descr (&mem_loc_result, deref);
12789 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12790 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12791 else
12792 add_loc_descr (&mem_loc_result,
12793 new_loc_descr (DW_OP_deref_size,
12794 GET_MODE_SIZE (mode), 0));
12796 break;
12798 case LO_SUM:
12799 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12801 case LABEL_REF:
12802 /* Some ports can transform a symbol ref into a label ref, because
12803 the symbol ref is too far away and has to be dumped into a constant
12804 pool. */
12805 case CONST:
12806 case SYMBOL_REF:
12807 if ((GET_MODE_CLASS (mode) != MODE_INT
12808 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12809 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12810 #ifdef POINTERS_EXTEND_UNSIGNED
12811 && (mode != Pmode || mem_mode == VOIDmode)
12812 #endif
12814 break;
12815 if (GET_CODE (rtl) == SYMBOL_REF
12816 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12818 dw_loc_descr_ref temp;
12820 /* If this is not defined, we have no way to emit the data. */
12821 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12822 break;
12824 temp = new_addr_loc_descr (rtl, dtprel_true);
12826 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12827 add_loc_descr (&mem_loc_result, temp);
12829 break;
12832 if (!const_ok_for_output (rtl))
12833 break;
12835 symref:
12836 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12837 vec_safe_push (used_rtx_array, rtl);
12838 break;
12840 case CONCAT:
12841 case CONCATN:
12842 case VAR_LOCATION:
12843 case DEBUG_IMPLICIT_PTR:
12844 expansion_failed (NULL_TREE, rtl,
12845 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12846 return 0;
12848 case ENTRY_VALUE:
12849 if (dwarf_strict)
12850 return NULL;
12851 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12853 if (GET_MODE_CLASS (mode) != MODE_INT
12854 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12855 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12856 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12857 else
12859 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12860 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12861 return NULL;
12862 op0 = one_reg_loc_descriptor (dbx_regnum,
12863 VAR_INIT_STATUS_INITIALIZED);
12866 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12867 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12869 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12870 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12871 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12872 return NULL;
12874 else
12875 gcc_unreachable ();
12876 if (op0 == NULL)
12877 return NULL;
12878 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12879 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12880 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12881 break;
12883 case DEBUG_PARAMETER_REF:
12884 mem_loc_result = parameter_ref_descriptor (rtl);
12885 break;
12887 case PRE_MODIFY:
12888 /* Extract the PLUS expression nested inside and fall into
12889 PLUS code below. */
12890 rtl = XEXP (rtl, 1);
12891 goto plus;
12893 case PRE_INC:
12894 case PRE_DEC:
12895 /* Turn these into a PLUS expression and fall into the PLUS code
12896 below. */
12897 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12898 gen_int_mode (GET_CODE (rtl) == PRE_INC
12899 ? GET_MODE_UNIT_SIZE (mem_mode)
12900 : -GET_MODE_UNIT_SIZE (mem_mode),
12901 mode));
12903 /* ... fall through ... */
12905 case PLUS:
12906 plus:
12907 if (is_based_loc (rtl)
12908 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12909 || XEXP (rtl, 0) == arg_pointer_rtx
12910 || XEXP (rtl, 0) == frame_pointer_rtx)
12911 && GET_MODE_CLASS (mode) == MODE_INT)
12912 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12913 INTVAL (XEXP (rtl, 1)),
12914 VAR_INIT_STATUS_INITIALIZED);
12915 else
12917 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12918 VAR_INIT_STATUS_INITIALIZED);
12919 if (mem_loc_result == 0)
12920 break;
12922 if (CONST_INT_P (XEXP (rtl, 1))
12923 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12924 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12925 else
12927 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12928 VAR_INIT_STATUS_INITIALIZED);
12929 if (op1 == 0)
12930 return NULL;
12931 add_loc_descr (&mem_loc_result, op1);
12932 add_loc_descr (&mem_loc_result,
12933 new_loc_descr (DW_OP_plus, 0, 0));
12936 break;
12938 /* If a pseudo-reg is optimized away, it is possible for it to
12939 be replaced with a MEM containing a multiply or shift. */
12940 case MINUS:
12941 op = DW_OP_minus;
12942 goto do_binop;
12944 case MULT:
12945 op = DW_OP_mul;
12946 goto do_binop;
12948 case DIV:
12949 if (!dwarf_strict
12950 && GET_MODE_CLASS (mode) == MODE_INT
12951 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12953 mem_loc_result = typed_binop (DW_OP_div, rtl,
12954 base_type_for_mode (mode, 0),
12955 mode, mem_mode);
12956 break;
12958 op = DW_OP_div;
12959 goto do_binop;
12961 case UMOD:
12962 op = DW_OP_mod;
12963 goto do_binop;
12965 case ASHIFT:
12966 op = DW_OP_shl;
12967 goto do_shift;
12969 case ASHIFTRT:
12970 op = DW_OP_shra;
12971 goto do_shift;
12973 case LSHIFTRT:
12974 op = DW_OP_shr;
12975 goto do_shift;
12977 do_shift:
12978 if (GET_MODE_CLASS (mode) != MODE_INT)
12979 break;
12980 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12981 VAR_INIT_STATUS_INITIALIZED);
12983 rtx rtlop1 = XEXP (rtl, 1);
12984 if (GET_MODE (rtlop1) != VOIDmode
12985 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12986 < GET_MODE_BITSIZE (mode))
12987 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12988 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12989 VAR_INIT_STATUS_INITIALIZED);
12992 if (op0 == 0 || op1 == 0)
12993 break;
12995 mem_loc_result = op0;
12996 add_loc_descr (&mem_loc_result, op1);
12997 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12998 break;
13000 case AND:
13001 op = DW_OP_and;
13002 goto do_binop;
13004 case IOR:
13005 op = DW_OP_or;
13006 goto do_binop;
13008 case XOR:
13009 op = DW_OP_xor;
13010 goto do_binop;
13012 do_binop:
13013 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13014 VAR_INIT_STATUS_INITIALIZED);
13015 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13016 VAR_INIT_STATUS_INITIALIZED);
13018 if (op0 == 0 || op1 == 0)
13019 break;
13021 mem_loc_result = op0;
13022 add_loc_descr (&mem_loc_result, op1);
13023 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13024 break;
13026 case MOD:
13027 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13029 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13030 base_type_for_mode (mode, 0),
13031 mode, mem_mode);
13032 break;
13035 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13036 VAR_INIT_STATUS_INITIALIZED);
13037 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13038 VAR_INIT_STATUS_INITIALIZED);
13040 if (op0 == 0 || op1 == 0)
13041 break;
13043 mem_loc_result = op0;
13044 add_loc_descr (&mem_loc_result, op1);
13045 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13046 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13047 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13048 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13049 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13050 break;
13052 case UDIV:
13053 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13055 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13057 op = DW_OP_div;
13058 goto do_binop;
13060 mem_loc_result = typed_binop (DW_OP_div, rtl,
13061 base_type_for_mode (mode, 1),
13062 mode, mem_mode);
13064 break;
13066 case NOT:
13067 op = DW_OP_not;
13068 goto do_unop;
13070 case ABS:
13071 op = DW_OP_abs;
13072 goto do_unop;
13074 case NEG:
13075 op = DW_OP_neg;
13076 goto do_unop;
13078 do_unop:
13079 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13080 VAR_INIT_STATUS_INITIALIZED);
13082 if (op0 == 0)
13083 break;
13085 mem_loc_result = op0;
13086 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13087 break;
13089 case CONST_INT:
13090 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13091 #ifdef POINTERS_EXTEND_UNSIGNED
13092 || (mode == Pmode
13093 && mem_mode != VOIDmode
13094 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13095 #endif
13098 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13099 break;
13101 if (!dwarf_strict
13102 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13103 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13105 dw_die_ref type_die = base_type_for_mode (mode, 1);
13106 machine_mode amode;
13107 if (type_die == NULL)
13108 return NULL;
13109 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13110 MODE_INT, 0);
13111 if (INTVAL (rtl) >= 0
13112 && amode != BLKmode
13113 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13114 /* const DW_OP_GNU_convert <XXX> vs.
13115 DW_OP_GNU_const_type <XXX, 1, const>. */
13116 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13117 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13119 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13120 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13121 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13122 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13123 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13124 add_loc_descr (&mem_loc_result, op0);
13125 return mem_loc_result;
13127 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13128 INTVAL (rtl));
13129 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13130 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13131 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13132 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13133 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13134 else
13136 mem_loc_result->dw_loc_oprnd2.val_class
13137 = dw_val_class_const_double;
13138 mem_loc_result->dw_loc_oprnd2.v.val_double
13139 = double_int::from_shwi (INTVAL (rtl));
13142 break;
13144 case CONST_DOUBLE:
13145 if (!dwarf_strict)
13147 dw_die_ref type_die;
13149 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13150 CONST_DOUBLE rtx could represent either a large integer
13151 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13152 the value is always a floating point constant.
13154 When it is an integer, a CONST_DOUBLE is used whenever
13155 the constant requires 2 HWIs to be adequately represented.
13156 We output CONST_DOUBLEs as blocks. */
13157 if (mode == VOIDmode
13158 || (GET_MODE (rtl) == VOIDmode
13159 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13160 break;
13161 type_die = base_type_for_mode (mode,
13162 GET_MODE_CLASS (mode) == MODE_INT);
13163 if (type_die == NULL)
13164 return NULL;
13165 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13166 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13167 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13168 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13169 #if TARGET_SUPPORTS_WIDE_INT == 0
13170 if (!SCALAR_FLOAT_MODE_P (mode))
13172 mem_loc_result->dw_loc_oprnd2.val_class
13173 = dw_val_class_const_double;
13174 mem_loc_result->dw_loc_oprnd2.v.val_double
13175 = rtx_to_double_int (rtl);
13177 else
13178 #endif
13180 unsigned int length = GET_MODE_SIZE (mode);
13181 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13183 insert_float (rtl, array);
13184 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13185 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13186 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13187 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13190 break;
13192 case CONST_WIDE_INT:
13193 if (!dwarf_strict)
13195 dw_die_ref type_die;
13197 type_die = base_type_for_mode (mode,
13198 GET_MODE_CLASS (mode) == MODE_INT);
13199 if (type_die == NULL)
13200 return NULL;
13201 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13202 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13203 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13204 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13205 mem_loc_result->dw_loc_oprnd2.val_class
13206 = dw_val_class_wide_int;
13207 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13208 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13210 break;
13212 case EQ:
13213 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13214 break;
13216 case GE:
13217 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13218 break;
13220 case GT:
13221 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13222 break;
13224 case LE:
13225 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13226 break;
13228 case LT:
13229 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13230 break;
13232 case NE:
13233 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13234 break;
13236 case GEU:
13237 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13238 break;
13240 case GTU:
13241 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13242 break;
13244 case LEU:
13245 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13246 break;
13248 case LTU:
13249 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13250 break;
13252 case UMIN:
13253 case UMAX:
13254 if (GET_MODE_CLASS (mode) != MODE_INT)
13255 break;
13256 /* FALLTHRU */
13257 case SMIN:
13258 case SMAX:
13259 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13260 break;
13262 case ZERO_EXTRACT:
13263 case SIGN_EXTRACT:
13264 if (CONST_INT_P (XEXP (rtl, 1))
13265 && CONST_INT_P (XEXP (rtl, 2))
13266 && ((unsigned) INTVAL (XEXP (rtl, 1))
13267 + (unsigned) INTVAL (XEXP (rtl, 2))
13268 <= GET_MODE_BITSIZE (mode))
13269 && GET_MODE_CLASS (mode) == MODE_INT
13270 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13271 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13273 int shift, size;
13274 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13275 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13276 if (op0 == 0)
13277 break;
13278 if (GET_CODE (rtl) == SIGN_EXTRACT)
13279 op = DW_OP_shra;
13280 else
13281 op = DW_OP_shr;
13282 mem_loc_result = op0;
13283 size = INTVAL (XEXP (rtl, 1));
13284 shift = INTVAL (XEXP (rtl, 2));
13285 if (BITS_BIG_ENDIAN)
13286 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13287 - shift - size;
13288 if (shift + size != (int) DWARF2_ADDR_SIZE)
13290 add_loc_descr (&mem_loc_result,
13291 int_loc_descriptor (DWARF2_ADDR_SIZE
13292 - shift - size));
13293 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13295 if (size != (int) DWARF2_ADDR_SIZE)
13297 add_loc_descr (&mem_loc_result,
13298 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13299 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13302 break;
13304 case IF_THEN_ELSE:
13306 dw_loc_descr_ref op2, bra_node, drop_node;
13307 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13308 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13309 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13310 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13311 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13312 VAR_INIT_STATUS_INITIALIZED);
13313 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13314 VAR_INIT_STATUS_INITIALIZED);
13315 if (op0 == NULL || op1 == NULL || op2 == NULL)
13316 break;
13318 mem_loc_result = op1;
13319 add_loc_descr (&mem_loc_result, op2);
13320 add_loc_descr (&mem_loc_result, op0);
13321 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13322 add_loc_descr (&mem_loc_result, bra_node);
13323 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13324 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13325 add_loc_descr (&mem_loc_result, drop_node);
13326 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13327 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13329 break;
13331 case FLOAT_EXTEND:
13332 case FLOAT_TRUNCATE:
13333 case FLOAT:
13334 case UNSIGNED_FLOAT:
13335 case FIX:
13336 case UNSIGNED_FIX:
13337 if (!dwarf_strict)
13339 dw_die_ref type_die;
13340 dw_loc_descr_ref cvt;
13342 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13343 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13344 if (op0 == NULL)
13345 break;
13346 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13347 && (GET_CODE (rtl) == FLOAT
13348 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13349 <= DWARF2_ADDR_SIZE))
13351 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13352 GET_CODE (rtl) == UNSIGNED_FLOAT);
13353 if (type_die == NULL)
13354 break;
13355 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13356 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13357 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13358 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13359 add_loc_descr (&op0, cvt);
13361 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13362 if (type_die == NULL)
13363 break;
13364 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13365 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13366 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13367 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13368 add_loc_descr (&op0, cvt);
13369 if (GET_MODE_CLASS (mode) == MODE_INT
13370 && (GET_CODE (rtl) == FIX
13371 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13373 op0 = convert_descriptor_to_mode (mode, op0);
13374 if (op0 == NULL)
13375 break;
13377 mem_loc_result = op0;
13379 break;
13381 case CLZ:
13382 case CTZ:
13383 case FFS:
13384 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13385 break;
13387 case POPCOUNT:
13388 case PARITY:
13389 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13390 break;
13392 case BSWAP:
13393 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13394 break;
13396 case ROTATE:
13397 case ROTATERT:
13398 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13399 break;
13401 case COMPARE:
13402 /* In theory, we could implement the above. */
13403 /* DWARF cannot represent the unsigned compare operations
13404 natively. */
13405 case SS_MULT:
13406 case US_MULT:
13407 case SS_DIV:
13408 case US_DIV:
13409 case SS_PLUS:
13410 case US_PLUS:
13411 case SS_MINUS:
13412 case US_MINUS:
13413 case SS_NEG:
13414 case US_NEG:
13415 case SS_ABS:
13416 case SS_ASHIFT:
13417 case US_ASHIFT:
13418 case SS_TRUNCATE:
13419 case US_TRUNCATE:
13420 case UNORDERED:
13421 case ORDERED:
13422 case UNEQ:
13423 case UNGE:
13424 case UNGT:
13425 case UNLE:
13426 case UNLT:
13427 case LTGT:
13428 case FRACT_CONVERT:
13429 case UNSIGNED_FRACT_CONVERT:
13430 case SAT_FRACT:
13431 case UNSIGNED_SAT_FRACT:
13432 case SQRT:
13433 case ASM_OPERANDS:
13434 case VEC_MERGE:
13435 case VEC_SELECT:
13436 case VEC_CONCAT:
13437 case VEC_DUPLICATE:
13438 case UNSPEC:
13439 case HIGH:
13440 case FMA:
13441 case STRICT_LOW_PART:
13442 case CONST_VECTOR:
13443 case CONST_FIXED:
13444 case CLRSB:
13445 case CLOBBER:
13446 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13447 can't express it in the debug info. This can happen e.g. with some
13448 TLS UNSPECs. */
13449 break;
13451 case CONST_STRING:
13452 resolve_one_addr (&rtl);
13453 goto symref;
13455 default:
13456 #ifdef ENABLE_CHECKING
13457 print_rtl (stderr, rtl);
13458 gcc_unreachable ();
13459 #else
13460 break;
13461 #endif
13464 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13465 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13467 return mem_loc_result;
13470 /* Return a descriptor that describes the concatenation of two locations.
13471 This is typically a complex variable. */
13473 static dw_loc_descr_ref
13474 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13476 dw_loc_descr_ref cc_loc_result = NULL;
13477 dw_loc_descr_ref x0_ref
13478 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13479 dw_loc_descr_ref x1_ref
13480 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13482 if (x0_ref == 0 || x1_ref == 0)
13483 return 0;
13485 cc_loc_result = x0_ref;
13486 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13488 add_loc_descr (&cc_loc_result, x1_ref);
13489 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13491 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13492 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13494 return cc_loc_result;
13497 /* Return a descriptor that describes the concatenation of N
13498 locations. */
13500 static dw_loc_descr_ref
13501 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13503 unsigned int i;
13504 dw_loc_descr_ref cc_loc_result = NULL;
13505 unsigned int n = XVECLEN (concatn, 0);
13507 for (i = 0; i < n; ++i)
13509 dw_loc_descr_ref ref;
13510 rtx x = XVECEXP (concatn, 0, i);
13512 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13513 if (ref == NULL)
13514 return NULL;
13516 add_loc_descr (&cc_loc_result, ref);
13517 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13520 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13521 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13523 return cc_loc_result;
13526 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13527 for DEBUG_IMPLICIT_PTR RTL. */
13529 static dw_loc_descr_ref
13530 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13532 dw_loc_descr_ref ret;
13533 dw_die_ref ref;
13535 if (dwarf_strict)
13536 return NULL;
13537 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13538 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13539 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13540 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13541 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13542 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13543 if (ref)
13545 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13546 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13547 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13549 else
13551 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13552 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13554 return ret;
13557 /* Output a proper Dwarf location descriptor for a variable or parameter
13558 which is either allocated in a register or in a memory location. For a
13559 register, we just generate an OP_REG and the register number. For a
13560 memory location we provide a Dwarf postfix expression describing how to
13561 generate the (dynamic) address of the object onto the address stack.
13563 MODE is mode of the decl if this loc_descriptor is going to be used in
13564 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13565 allowed, VOIDmode otherwise.
13567 If we don't know how to describe it, return 0. */
13569 static dw_loc_descr_ref
13570 loc_descriptor (rtx rtl, machine_mode mode,
13571 enum var_init_status initialized)
13573 dw_loc_descr_ref loc_result = NULL;
13575 switch (GET_CODE (rtl))
13577 case SUBREG:
13578 /* The case of a subreg may arise when we have a local (register)
13579 variable or a formal (register) parameter which doesn't quite fill
13580 up an entire register. For now, just assume that it is
13581 legitimate to make the Dwarf info refer to the whole register which
13582 contains the given subreg. */
13583 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13584 loc_result = loc_descriptor (SUBREG_REG (rtl),
13585 GET_MODE (SUBREG_REG (rtl)), initialized);
13586 else
13587 goto do_default;
13588 break;
13590 case REG:
13591 loc_result = reg_loc_descriptor (rtl, initialized);
13592 break;
13594 case MEM:
13595 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13596 GET_MODE (rtl), initialized);
13597 if (loc_result == NULL)
13598 loc_result = tls_mem_loc_descriptor (rtl);
13599 if (loc_result == NULL)
13601 rtx new_rtl = avoid_constant_pool_reference (rtl);
13602 if (new_rtl != rtl)
13603 loc_result = loc_descriptor (new_rtl, mode, initialized);
13605 break;
13607 case CONCAT:
13608 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13609 initialized);
13610 break;
13612 case CONCATN:
13613 loc_result = concatn_loc_descriptor (rtl, initialized);
13614 break;
13616 case VAR_LOCATION:
13617 /* Single part. */
13618 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13620 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13621 if (GET_CODE (loc) == EXPR_LIST)
13622 loc = XEXP (loc, 0);
13623 loc_result = loc_descriptor (loc, mode, initialized);
13624 break;
13627 rtl = XEXP (rtl, 1);
13628 /* FALLTHRU */
13630 case PARALLEL:
13632 rtvec par_elems = XVEC (rtl, 0);
13633 int num_elem = GET_NUM_ELEM (par_elems);
13634 machine_mode mode;
13635 int i;
13637 /* Create the first one, so we have something to add to. */
13638 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13639 VOIDmode, initialized);
13640 if (loc_result == NULL)
13641 return NULL;
13642 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13643 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13644 for (i = 1; i < num_elem; i++)
13646 dw_loc_descr_ref temp;
13648 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13649 VOIDmode, initialized);
13650 if (temp == NULL)
13651 return NULL;
13652 add_loc_descr (&loc_result, temp);
13653 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13654 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13657 break;
13659 case CONST_INT:
13660 if (mode != VOIDmode && mode != BLKmode)
13661 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13662 INTVAL (rtl));
13663 break;
13665 case CONST_DOUBLE:
13666 if (mode == VOIDmode)
13667 mode = GET_MODE (rtl);
13669 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13671 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13673 /* Note that a CONST_DOUBLE rtx could represent either an integer
13674 or a floating-point constant. A CONST_DOUBLE is used whenever
13675 the constant requires more than one word in order to be
13676 adequately represented. We output CONST_DOUBLEs as blocks. */
13677 loc_result = new_loc_descr (DW_OP_implicit_value,
13678 GET_MODE_SIZE (mode), 0);
13679 #if TARGET_SUPPORTS_WIDE_INT == 0
13680 if (!SCALAR_FLOAT_MODE_P (mode))
13682 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13683 loc_result->dw_loc_oprnd2.v.val_double
13684 = rtx_to_double_int (rtl);
13686 else
13687 #endif
13689 unsigned int length = GET_MODE_SIZE (mode);
13690 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13692 insert_float (rtl, array);
13693 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13694 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13695 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13696 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13699 break;
13701 case CONST_WIDE_INT:
13702 if (mode == VOIDmode)
13703 mode = GET_MODE (rtl);
13705 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13707 loc_result = new_loc_descr (DW_OP_implicit_value,
13708 GET_MODE_SIZE (mode), 0);
13709 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13710 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13711 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13713 break;
13715 case CONST_VECTOR:
13716 if (mode == VOIDmode)
13717 mode = GET_MODE (rtl);
13719 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13721 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13722 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13723 unsigned char *array
13724 = ggc_vec_alloc<unsigned char> (length * elt_size);
13725 unsigned int i;
13726 unsigned char *p;
13727 machine_mode imode = GET_MODE_INNER (mode);
13729 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13730 switch (GET_MODE_CLASS (mode))
13732 case MODE_VECTOR_INT:
13733 for (i = 0, p = array; i < length; i++, p += elt_size)
13735 rtx elt = CONST_VECTOR_ELT (rtl, i);
13736 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13738 break;
13740 case MODE_VECTOR_FLOAT:
13741 for (i = 0, p = array; i < length; i++, p += elt_size)
13743 rtx elt = CONST_VECTOR_ELT (rtl, i);
13744 insert_float (elt, p);
13746 break;
13748 default:
13749 gcc_unreachable ();
13752 loc_result = new_loc_descr (DW_OP_implicit_value,
13753 length * elt_size, 0);
13754 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13755 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13756 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13757 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13759 break;
13761 case CONST:
13762 if (mode == VOIDmode
13763 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13764 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13765 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13767 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13768 break;
13770 /* FALLTHROUGH */
13771 case SYMBOL_REF:
13772 if (!const_ok_for_output (rtl))
13773 break;
13774 case LABEL_REF:
13775 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13776 && (dwarf_version >= 4 || !dwarf_strict))
13778 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13779 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13780 vec_safe_push (used_rtx_array, rtl);
13782 break;
13784 case DEBUG_IMPLICIT_PTR:
13785 loc_result = implicit_ptr_descriptor (rtl, 0);
13786 break;
13788 case PLUS:
13789 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13790 && CONST_INT_P (XEXP (rtl, 1)))
13792 loc_result
13793 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13794 break;
13796 /* FALLTHRU */
13797 do_default:
13798 default:
13799 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13800 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13801 && dwarf_version >= 4)
13802 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13804 /* Value expression. */
13805 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13806 if (loc_result)
13807 add_loc_descr (&loc_result,
13808 new_loc_descr (DW_OP_stack_value, 0, 0));
13810 break;
13813 return loc_result;
13816 /* We need to figure out what section we should use as the base for the
13817 address ranges where a given location is valid.
13818 1. If this particular DECL has a section associated with it, use that.
13819 2. If this function has a section associated with it, use that.
13820 3. Otherwise, use the text section.
13821 XXX: If you split a variable across multiple sections, we won't notice. */
13823 static const char *
13824 secname_for_decl (const_tree decl)
13826 const char *secname;
13828 if (VAR_OR_FUNCTION_DECL_P (decl)
13829 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13830 && DECL_SECTION_NAME (decl))
13831 secname = DECL_SECTION_NAME (decl);
13832 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13833 secname = DECL_SECTION_NAME (current_function_decl);
13834 else if (cfun && in_cold_section_p)
13835 secname = crtl->subsections.cold_section_label;
13836 else
13837 secname = text_section_label;
13839 return secname;
13842 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13844 static bool
13845 decl_by_reference_p (tree decl)
13847 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13848 || TREE_CODE (decl) == VAR_DECL)
13849 && DECL_BY_REFERENCE (decl));
13852 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13853 for VARLOC. */
13855 static dw_loc_descr_ref
13856 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13857 enum var_init_status initialized)
13859 int have_address = 0;
13860 dw_loc_descr_ref descr;
13861 machine_mode mode;
13863 if (want_address != 2)
13865 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13866 /* Single part. */
13867 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13869 varloc = PAT_VAR_LOCATION_LOC (varloc);
13870 if (GET_CODE (varloc) == EXPR_LIST)
13871 varloc = XEXP (varloc, 0);
13872 mode = GET_MODE (varloc);
13873 if (MEM_P (varloc))
13875 rtx addr = XEXP (varloc, 0);
13876 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13877 mode, initialized);
13878 if (descr)
13879 have_address = 1;
13880 else
13882 rtx x = avoid_constant_pool_reference (varloc);
13883 if (x != varloc)
13884 descr = mem_loc_descriptor (x, mode, VOIDmode,
13885 initialized);
13888 else
13889 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13891 else
13892 return 0;
13894 else
13896 if (GET_CODE (varloc) == VAR_LOCATION)
13897 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13898 else
13899 mode = DECL_MODE (loc);
13900 descr = loc_descriptor (varloc, mode, initialized);
13901 have_address = 1;
13904 if (!descr)
13905 return 0;
13907 if (want_address == 2 && !have_address
13908 && (dwarf_version >= 4 || !dwarf_strict))
13910 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13912 expansion_failed (loc, NULL_RTX,
13913 "DWARF address size mismatch");
13914 return 0;
13916 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13917 have_address = 1;
13919 /* Show if we can't fill the request for an address. */
13920 if (want_address && !have_address)
13922 expansion_failed (loc, NULL_RTX,
13923 "Want address and only have value");
13924 return 0;
13927 /* If we've got an address and don't want one, dereference. */
13928 if (!want_address && have_address)
13930 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13931 enum dwarf_location_atom op;
13933 if (size > DWARF2_ADDR_SIZE || size == -1)
13935 expansion_failed (loc, NULL_RTX,
13936 "DWARF address size mismatch");
13937 return 0;
13939 else if (size == DWARF2_ADDR_SIZE)
13940 op = DW_OP_deref;
13941 else
13942 op = DW_OP_deref_size;
13944 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13947 return descr;
13950 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13951 if it is not possible. */
13953 static dw_loc_descr_ref
13954 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13956 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13957 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13958 else if (dwarf_version >= 3 || !dwarf_strict)
13959 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13960 else
13961 return NULL;
13964 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13965 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13967 static dw_loc_descr_ref
13968 dw_sra_loc_expr (tree decl, rtx loc)
13970 rtx p;
13971 unsigned HOST_WIDE_INT padsize = 0;
13972 dw_loc_descr_ref descr, *descr_tail;
13973 unsigned HOST_WIDE_INT decl_size;
13974 rtx varloc;
13975 enum var_init_status initialized;
13977 if (DECL_SIZE (decl) == NULL
13978 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13979 return NULL;
13981 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13982 descr = NULL;
13983 descr_tail = &descr;
13985 for (p = loc; p; p = XEXP (p, 1))
13987 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
13988 rtx loc_note = *decl_piece_varloc_ptr (p);
13989 dw_loc_descr_ref cur_descr;
13990 dw_loc_descr_ref *tail, last = NULL;
13991 unsigned HOST_WIDE_INT opsize = 0;
13993 if (loc_note == NULL_RTX
13994 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13996 padsize += bitsize;
13997 continue;
13999 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14000 varloc = NOTE_VAR_LOCATION (loc_note);
14001 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14002 if (cur_descr == NULL)
14004 padsize += bitsize;
14005 continue;
14008 /* Check that cur_descr either doesn't use
14009 DW_OP_*piece operations, or their sum is equal
14010 to bitsize. Otherwise we can't embed it. */
14011 for (tail = &cur_descr; *tail != NULL;
14012 tail = &(*tail)->dw_loc_next)
14013 if ((*tail)->dw_loc_opc == DW_OP_piece)
14015 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14016 * BITS_PER_UNIT;
14017 last = *tail;
14019 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14021 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14022 last = *tail;
14025 if (last != NULL && opsize != bitsize)
14027 padsize += bitsize;
14028 /* Discard the current piece of the descriptor and release any
14029 addr_table entries it uses. */
14030 remove_loc_list_addr_table_entries (cur_descr);
14031 continue;
14034 /* If there is a hole, add DW_OP_*piece after empty DWARF
14035 expression, which means that those bits are optimized out. */
14036 if (padsize)
14038 if (padsize > decl_size)
14040 remove_loc_list_addr_table_entries (cur_descr);
14041 goto discard_descr;
14043 decl_size -= padsize;
14044 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14045 if (*descr_tail == NULL)
14047 remove_loc_list_addr_table_entries (cur_descr);
14048 goto discard_descr;
14050 descr_tail = &(*descr_tail)->dw_loc_next;
14051 padsize = 0;
14053 *descr_tail = cur_descr;
14054 descr_tail = tail;
14055 if (bitsize > decl_size)
14056 goto discard_descr;
14057 decl_size -= bitsize;
14058 if (last == NULL)
14060 HOST_WIDE_INT offset = 0;
14061 if (GET_CODE (varloc) == VAR_LOCATION
14062 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14064 varloc = PAT_VAR_LOCATION_LOC (varloc);
14065 if (GET_CODE (varloc) == EXPR_LIST)
14066 varloc = XEXP (varloc, 0);
14070 if (GET_CODE (varloc) == CONST
14071 || GET_CODE (varloc) == SIGN_EXTEND
14072 || GET_CODE (varloc) == ZERO_EXTEND)
14073 varloc = XEXP (varloc, 0);
14074 else if (GET_CODE (varloc) == SUBREG)
14075 varloc = SUBREG_REG (varloc);
14076 else
14077 break;
14079 while (1);
14080 /* DW_OP_bit_size offset should be zero for register
14081 or implicit location descriptions and empty location
14082 descriptions, but for memory addresses needs big endian
14083 adjustment. */
14084 if (MEM_P (varloc))
14086 unsigned HOST_WIDE_INT memsize
14087 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14088 if (memsize != bitsize)
14090 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14091 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14092 goto discard_descr;
14093 if (memsize < bitsize)
14094 goto discard_descr;
14095 if (BITS_BIG_ENDIAN)
14096 offset = memsize - bitsize;
14100 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14101 if (*descr_tail == NULL)
14102 goto discard_descr;
14103 descr_tail = &(*descr_tail)->dw_loc_next;
14107 /* If there were any non-empty expressions, add padding till the end of
14108 the decl. */
14109 if (descr != NULL && decl_size != 0)
14111 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14112 if (*descr_tail == NULL)
14113 goto discard_descr;
14115 return descr;
14117 discard_descr:
14118 /* Discard the descriptor and release any addr_table entries it uses. */
14119 remove_loc_list_addr_table_entries (descr);
14120 return NULL;
14123 /* Return the dwarf representation of the location list LOC_LIST of
14124 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14125 function. */
14127 static dw_loc_list_ref
14128 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14130 const char *endname, *secname;
14131 rtx varloc;
14132 enum var_init_status initialized;
14133 struct var_loc_node *node;
14134 dw_loc_descr_ref descr;
14135 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14136 dw_loc_list_ref list = NULL;
14137 dw_loc_list_ref *listp = &list;
14139 /* Now that we know what section we are using for a base,
14140 actually construct the list of locations.
14141 The first location information is what is passed to the
14142 function that creates the location list, and the remaining
14143 locations just get added on to that list.
14144 Note that we only know the start address for a location
14145 (IE location changes), so to build the range, we use
14146 the range [current location start, next location start].
14147 This means we have to special case the last node, and generate
14148 a range of [last location start, end of function label]. */
14150 secname = secname_for_decl (decl);
14152 for (node = loc_list->first; node; node = node->next)
14153 if (GET_CODE (node->loc) == EXPR_LIST
14154 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14156 if (GET_CODE (node->loc) == EXPR_LIST)
14158 /* This requires DW_OP_{,bit_}piece, which is not usable
14159 inside DWARF expressions. */
14160 if (want_address != 2)
14161 continue;
14162 descr = dw_sra_loc_expr (decl, node->loc);
14163 if (descr == NULL)
14164 continue;
14166 else
14168 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14169 varloc = NOTE_VAR_LOCATION (node->loc);
14170 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14172 if (descr)
14174 bool range_across_switch = false;
14175 /* If section switch happens in between node->label
14176 and node->next->label (or end of function) and
14177 we can't emit it as a single entry list,
14178 emit two ranges, first one ending at the end
14179 of first partition and second one starting at the
14180 beginning of second partition. */
14181 if (node == loc_list->last_before_switch
14182 && (node != loc_list->first || loc_list->first->next)
14183 && current_function_decl)
14185 endname = cfun->fde->dw_fde_end;
14186 range_across_switch = true;
14188 /* The variable has a location between NODE->LABEL and
14189 NODE->NEXT->LABEL. */
14190 else if (node->next)
14191 endname = node->next->label;
14192 /* If the variable has a location at the last label
14193 it keeps its location until the end of function. */
14194 else if (!current_function_decl)
14195 endname = text_end_label;
14196 else
14198 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14199 current_function_funcdef_no);
14200 endname = ggc_strdup (label_id);
14203 *listp = new_loc_list (descr, node->label, endname, secname);
14204 if (TREE_CODE (decl) == PARM_DECL
14205 && node == loc_list->first
14206 && NOTE_P (node->loc)
14207 && strcmp (node->label, endname) == 0)
14208 (*listp)->force = true;
14209 listp = &(*listp)->dw_loc_next;
14211 if (range_across_switch)
14213 if (GET_CODE (node->loc) == EXPR_LIST)
14214 descr = dw_sra_loc_expr (decl, node->loc);
14215 else
14217 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14218 varloc = NOTE_VAR_LOCATION (node->loc);
14219 descr = dw_loc_list_1 (decl, varloc, want_address,
14220 initialized);
14222 gcc_assert (descr);
14223 /* The variable has a location between NODE->LABEL and
14224 NODE->NEXT->LABEL. */
14225 if (node->next)
14226 endname = node->next->label;
14227 else
14228 endname = cfun->fde->dw_fde_second_end;
14229 *listp = new_loc_list (descr,
14230 cfun->fde->dw_fde_second_begin,
14231 endname, secname);
14232 listp = &(*listp)->dw_loc_next;
14237 /* Try to avoid the overhead of a location list emitting a location
14238 expression instead, but only if we didn't have more than one
14239 location entry in the first place. If some entries were not
14240 representable, we don't want to pretend a single entry that was
14241 applies to the entire scope in which the variable is
14242 available. */
14243 if (list && loc_list->first->next)
14244 gen_llsym (list);
14246 return list;
14249 /* Return if the loc_list has only single element and thus can be represented
14250 as location description. */
14252 static bool
14253 single_element_loc_list_p (dw_loc_list_ref list)
14255 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14256 return !list->ll_symbol;
14259 /* To each location in list LIST add loc descr REF. */
14261 static void
14262 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14264 dw_loc_descr_ref copy;
14265 add_loc_descr (&list->expr, ref);
14266 list = list->dw_loc_next;
14267 while (list)
14269 copy = ggc_alloc<dw_loc_descr_node> ();
14270 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14271 add_loc_descr (&list->expr, copy);
14272 while (copy->dw_loc_next)
14274 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14275 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14276 copy->dw_loc_next = new_copy;
14277 copy = new_copy;
14279 list = list->dw_loc_next;
14283 /* Given two lists RET and LIST
14284 produce location list that is result of adding expression in LIST
14285 to expression in RET on each position in program.
14286 Might be destructive on both RET and LIST.
14288 TODO: We handle only simple cases of RET or LIST having at most one
14289 element. General case would inolve sorting the lists in program order
14290 and merging them that will need some additional work.
14291 Adding that will improve quality of debug info especially for SRA-ed
14292 structures. */
14294 static void
14295 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14297 if (!list)
14298 return;
14299 if (!*ret)
14301 *ret = list;
14302 return;
14304 if (!list->dw_loc_next)
14306 add_loc_descr_to_each (*ret, list->expr);
14307 return;
14309 if (!(*ret)->dw_loc_next)
14311 add_loc_descr_to_each (list, (*ret)->expr);
14312 *ret = list;
14313 return;
14315 expansion_failed (NULL_TREE, NULL_RTX,
14316 "Don't know how to merge two non-trivial"
14317 " location lists.\n");
14318 *ret = NULL;
14319 return;
14322 /* LOC is constant expression. Try a luck, look it up in constant
14323 pool and return its loc_descr of its address. */
14325 static dw_loc_descr_ref
14326 cst_pool_loc_descr (tree loc)
14328 /* Get an RTL for this, if something has been emitted. */
14329 rtx rtl = lookup_constant_def (loc);
14331 if (!rtl || !MEM_P (rtl))
14333 gcc_assert (!rtl);
14334 return 0;
14336 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14338 /* TODO: We might get more coverage if we was actually delaying expansion
14339 of all expressions till end of compilation when constant pools are fully
14340 populated. */
14341 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14343 expansion_failed (loc, NULL_RTX,
14344 "CST value in contant pool but not marked.");
14345 return 0;
14347 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14348 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14351 /* Return dw_loc_list representing address of addr_expr LOC
14352 by looking for inner INDIRECT_REF expression and turning
14353 it into simple arithmetics.
14355 See loc_list_from_tree for the meaning of CONTEXT. */
14357 static dw_loc_list_ref
14358 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14359 const loc_descr_context *context)
14361 tree obj, offset;
14362 HOST_WIDE_INT bitsize, bitpos, bytepos;
14363 machine_mode mode;
14364 int unsignedp, volatilep = 0;
14365 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14367 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14368 &bitsize, &bitpos, &offset, &mode,
14369 &unsignedp, &volatilep, false);
14370 STRIP_NOPS (obj);
14371 if (bitpos % BITS_PER_UNIT)
14373 expansion_failed (loc, NULL_RTX, "bitfield access");
14374 return 0;
14376 if (!INDIRECT_REF_P (obj))
14378 expansion_failed (obj,
14379 NULL_RTX, "no indirect ref in inner refrence");
14380 return 0;
14382 if (!offset && !bitpos)
14383 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14384 context);
14385 else if (toplev
14386 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14387 && (dwarf_version >= 4 || !dwarf_strict))
14389 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14390 if (!list_ret)
14391 return 0;
14392 if (offset)
14394 /* Variable offset. */
14395 list_ret1 = loc_list_from_tree (offset, 0, context);
14396 if (list_ret1 == 0)
14397 return 0;
14398 add_loc_list (&list_ret, list_ret1);
14399 if (!list_ret)
14400 return 0;
14401 add_loc_descr_to_each (list_ret,
14402 new_loc_descr (DW_OP_plus, 0, 0));
14404 bytepos = bitpos / BITS_PER_UNIT;
14405 if (bytepos > 0)
14406 add_loc_descr_to_each (list_ret,
14407 new_loc_descr (DW_OP_plus_uconst,
14408 bytepos, 0));
14409 else if (bytepos < 0)
14410 loc_list_plus_const (list_ret, bytepos);
14411 add_loc_descr_to_each (list_ret,
14412 new_loc_descr (DW_OP_stack_value, 0, 0));
14414 return list_ret;
14418 /* Helper structure for location descriptions generation. */
14419 struct loc_descr_context
14421 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14422 NULL_TREE if DW_OP_push_object_address in invalid for this location
14423 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14424 tree context_type;
14425 /* The ..._DECL node that should be translated as a
14426 DW_OP_push_object_address operation. */
14427 tree base_decl;
14430 /* Generate Dwarf location list representing LOC.
14431 If WANT_ADDRESS is false, expression computing LOC will be computed
14432 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14433 if WANT_ADDRESS is 2, expression computing address useable in location
14434 will be returned (i.e. DW_OP_reg can be used
14435 to refer to register values).
14437 CONTEXT provides information to customize the location descriptions
14438 generation. Its context_type field specifies what type is implicitly
14439 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14440 will not be generated.
14442 If CONTEXT is NULL, the behavior is the same as if both context_type and
14443 base_decl fields were NULL_TREE. */
14445 static dw_loc_list_ref
14446 loc_list_from_tree (tree loc, int want_address,
14447 const struct loc_descr_context *context)
14449 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14450 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14451 int have_address = 0;
14452 enum dwarf_location_atom op;
14454 /* ??? Most of the time we do not take proper care for sign/zero
14455 extending the values properly. Hopefully this won't be a real
14456 problem... */
14458 if (context != NULL
14459 && context->base_decl == loc
14460 && want_address == 0)
14462 if (dwarf_version >= 3 || !dwarf_strict)
14463 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14464 NULL, NULL, NULL);
14465 else
14466 return NULL;
14469 switch (TREE_CODE (loc))
14471 case ERROR_MARK:
14472 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14473 return 0;
14475 case PLACEHOLDER_EXPR:
14476 /* This case involves extracting fields from an object to determine the
14477 position of other fields. It is supposed to appear only as the first
14478 operand of COMPONENT_REF nodes and to reference precisely the type
14479 that the context allows. */
14480 if (context != NULL
14481 && TREE_TYPE (loc) == context->context_type
14482 && want_address >= 1)
14484 if (dwarf_version >= 3 || !dwarf_strict)
14486 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14487 have_address = 1;
14488 break;
14490 else
14491 return NULL;
14493 else
14494 expansion_failed (loc, NULL_RTX,
14495 "PLACEHOLDER_EXPR for an unexpected type");
14496 break;
14498 case CALL_EXPR:
14499 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14500 /* There are no opcodes for these operations. */
14501 return 0;
14503 case PREINCREMENT_EXPR:
14504 case PREDECREMENT_EXPR:
14505 case POSTINCREMENT_EXPR:
14506 case POSTDECREMENT_EXPR:
14507 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14508 /* There are no opcodes for these operations. */
14509 return 0;
14511 case ADDR_EXPR:
14512 /* If we already want an address, see if there is INDIRECT_REF inside
14513 e.g. for &this->field. */
14514 if (want_address)
14516 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14517 (loc, want_address == 2, context);
14518 if (list_ret)
14519 have_address = 1;
14520 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14521 && (ret = cst_pool_loc_descr (loc)))
14522 have_address = 1;
14524 /* Otherwise, process the argument and look for the address. */
14525 if (!list_ret && !ret)
14526 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14527 else
14529 if (want_address)
14530 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14531 return NULL;
14533 break;
14535 case VAR_DECL:
14536 if (DECL_THREAD_LOCAL_P (loc))
14538 rtx rtl;
14539 enum dwarf_location_atom tls_op;
14540 enum dtprel_bool dtprel = dtprel_false;
14542 if (targetm.have_tls)
14544 /* If this is not defined, we have no way to emit the
14545 data. */
14546 if (!targetm.asm_out.output_dwarf_dtprel)
14547 return 0;
14549 /* The way DW_OP_GNU_push_tls_address is specified, we
14550 can only look up addresses of objects in the current
14551 module. We used DW_OP_addr as first op, but that's
14552 wrong, because DW_OP_addr is relocated by the debug
14553 info consumer, while DW_OP_GNU_push_tls_address
14554 operand shouldn't be. */
14555 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14556 return 0;
14557 dtprel = dtprel_true;
14558 tls_op = DW_OP_GNU_push_tls_address;
14560 else
14562 if (!targetm.emutls.debug_form_tls_address
14563 || !(dwarf_version >= 3 || !dwarf_strict))
14564 return 0;
14565 /* We stuffed the control variable into the DECL_VALUE_EXPR
14566 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14567 no longer appear in gimple code. We used the control
14568 variable in specific so that we could pick it up here. */
14569 loc = DECL_VALUE_EXPR (loc);
14570 tls_op = DW_OP_form_tls_address;
14573 rtl = rtl_for_decl_location (loc);
14574 if (rtl == NULL_RTX)
14575 return 0;
14577 if (!MEM_P (rtl))
14578 return 0;
14579 rtl = XEXP (rtl, 0);
14580 if (! CONSTANT_P (rtl))
14581 return 0;
14583 ret = new_addr_loc_descr (rtl, dtprel);
14584 ret1 = new_loc_descr (tls_op, 0, 0);
14585 add_loc_descr (&ret, ret1);
14587 have_address = 1;
14588 break;
14590 /* FALLTHRU */
14592 case PARM_DECL:
14593 case RESULT_DECL:
14594 if (DECL_HAS_VALUE_EXPR_P (loc))
14595 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14596 want_address, context);
14597 /* FALLTHRU */
14599 case FUNCTION_DECL:
14601 rtx rtl;
14602 var_loc_list *loc_list = lookup_decl_loc (loc);
14604 if (loc_list && loc_list->first)
14606 list_ret = dw_loc_list (loc_list, loc, want_address);
14607 have_address = want_address != 0;
14608 break;
14610 rtl = rtl_for_decl_location (loc);
14611 if (rtl == NULL_RTX)
14613 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14614 return 0;
14616 else if (CONST_INT_P (rtl))
14618 HOST_WIDE_INT val = INTVAL (rtl);
14619 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14620 val &= GET_MODE_MASK (DECL_MODE (loc));
14621 ret = int_loc_descriptor (val);
14623 else if (GET_CODE (rtl) == CONST_STRING)
14625 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14626 return 0;
14628 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14629 ret = new_addr_loc_descr (rtl, dtprel_false);
14630 else
14632 machine_mode mode, mem_mode;
14634 /* Certain constructs can only be represented at top-level. */
14635 if (want_address == 2)
14637 ret = loc_descriptor (rtl, VOIDmode,
14638 VAR_INIT_STATUS_INITIALIZED);
14639 have_address = 1;
14641 else
14643 mode = GET_MODE (rtl);
14644 mem_mode = VOIDmode;
14645 if (MEM_P (rtl))
14647 mem_mode = mode;
14648 mode = get_address_mode (rtl);
14649 rtl = XEXP (rtl, 0);
14650 have_address = 1;
14652 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14653 VAR_INIT_STATUS_INITIALIZED);
14655 if (!ret)
14656 expansion_failed (loc, rtl,
14657 "failed to produce loc descriptor for rtl");
14660 break;
14662 case MEM_REF:
14663 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14665 have_address = 1;
14666 goto do_plus;
14668 /* Fallthru. */
14669 case INDIRECT_REF:
14670 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14671 have_address = 1;
14672 break;
14674 case TARGET_MEM_REF:
14675 case SSA_NAME:
14676 return NULL;
14678 case COMPOUND_EXPR:
14679 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14681 CASE_CONVERT:
14682 case VIEW_CONVERT_EXPR:
14683 case SAVE_EXPR:
14684 case MODIFY_EXPR:
14685 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14687 case COMPONENT_REF:
14688 case BIT_FIELD_REF:
14689 case ARRAY_REF:
14690 case ARRAY_RANGE_REF:
14691 case REALPART_EXPR:
14692 case IMAGPART_EXPR:
14694 tree obj, offset;
14695 HOST_WIDE_INT bitsize, bitpos, bytepos;
14696 machine_mode mode;
14697 int unsignedp, volatilep = 0;
14699 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14700 &unsignedp, &volatilep, false);
14702 gcc_assert (obj != loc);
14704 list_ret = loc_list_from_tree (obj,
14705 want_address == 2
14706 && !bitpos && !offset ? 2 : 1,
14707 context);
14708 /* TODO: We can extract value of the small expression via shifting even
14709 for nonzero bitpos. */
14710 if (list_ret == 0)
14711 return 0;
14712 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14714 expansion_failed (loc, NULL_RTX,
14715 "bitfield access");
14716 return 0;
14719 if (offset != NULL_TREE)
14721 /* Variable offset. */
14722 list_ret1 = loc_list_from_tree (offset, 0, context);
14723 if (list_ret1 == 0)
14724 return 0;
14725 add_loc_list (&list_ret, list_ret1);
14726 if (!list_ret)
14727 return 0;
14728 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14731 bytepos = bitpos / BITS_PER_UNIT;
14732 if (bytepos > 0)
14733 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14734 else if (bytepos < 0)
14735 loc_list_plus_const (list_ret, bytepos);
14737 have_address = 1;
14738 break;
14741 case INTEGER_CST:
14742 if ((want_address || !tree_fits_shwi_p (loc))
14743 && (ret = cst_pool_loc_descr (loc)))
14744 have_address = 1;
14745 else if (want_address == 2
14746 && tree_fits_shwi_p (loc)
14747 && (ret = address_of_int_loc_descriptor
14748 (int_size_in_bytes (TREE_TYPE (loc)),
14749 tree_to_shwi (loc))))
14750 have_address = 1;
14751 else if (tree_fits_shwi_p (loc))
14752 ret = int_loc_descriptor (tree_to_shwi (loc));
14753 else
14755 expansion_failed (loc, NULL_RTX,
14756 "Integer operand is not host integer");
14757 return 0;
14759 break;
14761 case CONSTRUCTOR:
14762 case REAL_CST:
14763 case STRING_CST:
14764 case COMPLEX_CST:
14765 if ((ret = cst_pool_loc_descr (loc)))
14766 have_address = 1;
14767 else
14768 /* We can construct small constants here using int_loc_descriptor. */
14769 expansion_failed (loc, NULL_RTX,
14770 "constructor or constant not in constant pool");
14771 break;
14773 case TRUTH_AND_EXPR:
14774 case TRUTH_ANDIF_EXPR:
14775 case BIT_AND_EXPR:
14776 op = DW_OP_and;
14777 goto do_binop;
14779 case TRUTH_XOR_EXPR:
14780 case BIT_XOR_EXPR:
14781 op = DW_OP_xor;
14782 goto do_binop;
14784 case TRUTH_OR_EXPR:
14785 case TRUTH_ORIF_EXPR:
14786 case BIT_IOR_EXPR:
14787 op = DW_OP_or;
14788 goto do_binop;
14790 case FLOOR_DIV_EXPR:
14791 case CEIL_DIV_EXPR:
14792 case ROUND_DIV_EXPR:
14793 case TRUNC_DIV_EXPR:
14794 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14795 return 0;
14796 op = DW_OP_div;
14797 goto do_binop;
14799 case MINUS_EXPR:
14800 op = DW_OP_minus;
14801 goto do_binop;
14803 case FLOOR_MOD_EXPR:
14804 case CEIL_MOD_EXPR:
14805 case ROUND_MOD_EXPR:
14806 case TRUNC_MOD_EXPR:
14807 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14809 op = DW_OP_mod;
14810 goto do_binop;
14812 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14813 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14814 if (list_ret == 0 || list_ret1 == 0)
14815 return 0;
14817 add_loc_list (&list_ret, list_ret1);
14818 if (list_ret == 0)
14819 return 0;
14820 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14821 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14822 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14823 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14824 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14825 break;
14827 case MULT_EXPR:
14828 op = DW_OP_mul;
14829 goto do_binop;
14831 case LSHIFT_EXPR:
14832 op = DW_OP_shl;
14833 goto do_binop;
14835 case RSHIFT_EXPR:
14836 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14837 goto do_binop;
14839 case POINTER_PLUS_EXPR:
14840 case PLUS_EXPR:
14841 do_plus:
14842 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14844 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14845 if (list_ret == 0)
14846 return 0;
14848 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14849 break;
14852 op = DW_OP_plus;
14853 goto do_binop;
14855 case LE_EXPR:
14856 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14857 return 0;
14859 op = DW_OP_le;
14860 goto do_binop;
14862 case GE_EXPR:
14863 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14864 return 0;
14866 op = DW_OP_ge;
14867 goto do_binop;
14869 case LT_EXPR:
14870 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14871 return 0;
14873 op = DW_OP_lt;
14874 goto do_binop;
14876 case GT_EXPR:
14877 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14878 return 0;
14880 op = DW_OP_gt;
14881 goto do_binop;
14883 case EQ_EXPR:
14884 op = DW_OP_eq;
14885 goto do_binop;
14887 case NE_EXPR:
14888 op = DW_OP_ne;
14889 goto do_binop;
14891 do_binop:
14892 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14893 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14894 if (list_ret == 0 || list_ret1 == 0)
14895 return 0;
14897 add_loc_list (&list_ret, list_ret1);
14898 if (list_ret == 0)
14899 return 0;
14900 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14901 break;
14903 case TRUTH_NOT_EXPR:
14904 case BIT_NOT_EXPR:
14905 op = DW_OP_not;
14906 goto do_unop;
14908 case ABS_EXPR:
14909 op = DW_OP_abs;
14910 goto do_unop;
14912 case NEGATE_EXPR:
14913 op = DW_OP_neg;
14914 goto do_unop;
14916 do_unop:
14917 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14918 if (list_ret == 0)
14919 return 0;
14921 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14922 break;
14924 case MIN_EXPR:
14925 case MAX_EXPR:
14927 const enum tree_code code =
14928 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14930 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14931 build2 (code, integer_type_node,
14932 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14933 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14936 /* ... fall through ... */
14938 case COND_EXPR:
14940 dw_loc_descr_ref lhs
14941 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
14942 dw_loc_list_ref rhs
14943 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
14944 dw_loc_descr_ref bra_node, jump_node, tmp;
14946 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14947 if (list_ret == 0 || lhs == 0 || rhs == 0)
14948 return 0;
14950 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14951 add_loc_descr_to_each (list_ret, bra_node);
14953 add_loc_list (&list_ret, rhs);
14954 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14955 add_loc_descr_to_each (list_ret, jump_node);
14957 add_loc_descr_to_each (list_ret, lhs);
14958 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14959 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14961 /* ??? Need a node to point the skip at. Use a nop. */
14962 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14963 add_loc_descr_to_each (list_ret, tmp);
14964 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14965 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14967 break;
14969 case FIX_TRUNC_EXPR:
14970 return 0;
14972 default:
14973 /* Leave front-end specific codes as simply unknown. This comes
14974 up, for instance, with the C STMT_EXPR. */
14975 if ((unsigned int) TREE_CODE (loc)
14976 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14978 expansion_failed (loc, NULL_RTX,
14979 "language specific tree node");
14980 return 0;
14983 #ifdef ENABLE_CHECKING
14984 /* Otherwise this is a generic code; we should just lists all of
14985 these explicitly. We forgot one. */
14986 gcc_unreachable ();
14987 #else
14988 /* In a release build, we want to degrade gracefully: better to
14989 generate incomplete debugging information than to crash. */
14990 return NULL;
14991 #endif
14994 if (!ret && !list_ret)
14995 return 0;
14997 if (want_address == 2 && !have_address
14998 && (dwarf_version >= 4 || !dwarf_strict))
15000 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15002 expansion_failed (loc, NULL_RTX,
15003 "DWARF address size mismatch");
15004 return 0;
15006 if (ret)
15007 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15008 else
15009 add_loc_descr_to_each (list_ret,
15010 new_loc_descr (DW_OP_stack_value, 0, 0));
15011 have_address = 1;
15013 /* Show if we can't fill the request for an address. */
15014 if (want_address && !have_address)
15016 expansion_failed (loc, NULL_RTX,
15017 "Want address and only have value");
15018 return 0;
15021 gcc_assert (!ret || !list_ret);
15023 /* If we've got an address and don't want one, dereference. */
15024 if (!want_address && have_address)
15026 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15028 if (size > DWARF2_ADDR_SIZE || size == -1)
15030 expansion_failed (loc, NULL_RTX,
15031 "DWARF address size mismatch");
15032 return 0;
15034 else if (size == DWARF2_ADDR_SIZE)
15035 op = DW_OP_deref;
15036 else
15037 op = DW_OP_deref_size;
15039 if (ret)
15040 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15041 else
15042 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15044 if (ret)
15045 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15047 return list_ret;
15050 /* Same as above but return only single location expression. */
15051 static dw_loc_descr_ref
15052 loc_descriptor_from_tree (tree loc, int want_address,
15053 const struct loc_descr_context *context)
15055 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15056 if (!ret)
15057 return NULL;
15058 if (ret->dw_loc_next)
15060 expansion_failed (loc, NULL_RTX,
15061 "Location list where only loc descriptor needed");
15062 return NULL;
15064 return ret->expr;
15067 /* Given a value, round it up to the lowest multiple of `boundary'
15068 which is not less than the value itself. */
15070 static inline HOST_WIDE_INT
15071 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15073 return (((value + boundary - 1) / boundary) * boundary);
15076 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15077 pointer to the declared type for the relevant field variable, or return
15078 `integer_type_node' if the given node turns out to be an
15079 ERROR_MARK node. */
15081 static inline tree
15082 field_type (const_tree decl)
15084 tree type;
15086 if (TREE_CODE (decl) == ERROR_MARK)
15087 return integer_type_node;
15089 type = DECL_BIT_FIELD_TYPE (decl);
15090 if (type == NULL_TREE)
15091 type = TREE_TYPE (decl);
15093 return type;
15096 /* Given a pointer to a tree node, return the alignment in bits for
15097 it, or else return BITS_PER_WORD if the node actually turns out to
15098 be an ERROR_MARK node. */
15100 static inline unsigned
15101 simple_type_align_in_bits (const_tree type)
15103 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15106 static inline unsigned
15107 simple_decl_align_in_bits (const_tree decl)
15109 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15112 /* Return the result of rounding T up to ALIGN. */
15114 static inline offset_int
15115 round_up_to_align (const offset_int &t, unsigned int align)
15117 return wi::udiv_trunc (t + align - 1, align) * align;
15120 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15121 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15122 or return 0 if we are unable to determine what that offset is, either
15123 because the argument turns out to be a pointer to an ERROR_MARK node, or
15124 because the offset is actually variable. (We can't handle the latter case
15125 just yet). */
15127 static HOST_WIDE_INT
15128 field_byte_offset (const_tree decl)
15130 offset_int object_offset_in_bits;
15131 offset_int object_offset_in_bytes;
15132 offset_int bitpos_int;
15134 if (TREE_CODE (decl) == ERROR_MARK)
15135 return 0;
15137 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15139 /* We cannot yet cope with fields whose positions are variable, so
15140 for now, when we see such things, we simply return 0. Someday, we may
15141 be able to handle such cases, but it will be damn difficult. */
15142 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15143 return 0;
15145 bitpos_int = wi::to_offset (bit_position (decl));
15147 #ifdef PCC_BITFIELD_TYPE_MATTERS
15148 if (PCC_BITFIELD_TYPE_MATTERS)
15150 tree type;
15151 tree field_size_tree;
15152 offset_int deepest_bitpos;
15153 offset_int field_size_in_bits;
15154 unsigned int type_align_in_bits;
15155 unsigned int decl_align_in_bits;
15156 offset_int type_size_in_bits;
15158 type = field_type (decl);
15159 type_size_in_bits = offset_int_type_size_in_bits (type);
15160 type_align_in_bits = simple_type_align_in_bits (type);
15162 field_size_tree = DECL_SIZE (decl);
15164 /* The size could be unspecified if there was an error, or for
15165 a flexible array member. */
15166 if (!field_size_tree)
15167 field_size_tree = bitsize_zero_node;
15169 /* If the size of the field is not constant, use the type size. */
15170 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15171 field_size_in_bits = wi::to_offset (field_size_tree);
15172 else
15173 field_size_in_bits = type_size_in_bits;
15175 decl_align_in_bits = simple_decl_align_in_bits (decl);
15177 /* The GCC front-end doesn't make any attempt to keep track of the
15178 starting bit offset (relative to the start of the containing
15179 structure type) of the hypothetical "containing object" for a
15180 bit-field. Thus, when computing the byte offset value for the
15181 start of the "containing object" of a bit-field, we must deduce
15182 this information on our own. This can be rather tricky to do in
15183 some cases. For example, handling the following structure type
15184 definition when compiling for an i386/i486 target (which only
15185 aligns long long's to 32-bit boundaries) can be very tricky:
15187 struct S { int field1; long long field2:31; };
15189 Fortunately, there is a simple rule-of-thumb which can be used
15190 in such cases. When compiling for an i386/i486, GCC will
15191 allocate 8 bytes for the structure shown above. It decides to
15192 do this based upon one simple rule for bit-field allocation.
15193 GCC allocates each "containing object" for each bit-field at
15194 the first (i.e. lowest addressed) legitimate alignment boundary
15195 (based upon the required minimum alignment for the declared
15196 type of the field) which it can possibly use, subject to the
15197 condition that there is still enough available space remaining
15198 in the containing object (when allocated at the selected point)
15199 to fully accommodate all of the bits of the bit-field itself.
15201 This simple rule makes it obvious why GCC allocates 8 bytes for
15202 each object of the structure type shown above. When looking
15203 for a place to allocate the "containing object" for `field2',
15204 the compiler simply tries to allocate a 64-bit "containing
15205 object" at each successive 32-bit boundary (starting at zero)
15206 until it finds a place to allocate that 64- bit field such that
15207 at least 31 contiguous (and previously unallocated) bits remain
15208 within that selected 64 bit field. (As it turns out, for the
15209 example above, the compiler finds it is OK to allocate the
15210 "containing object" 64-bit field at bit-offset zero within the
15211 structure type.)
15213 Here we attempt to work backwards from the limited set of facts
15214 we're given, and we try to deduce from those facts, where GCC
15215 must have believed that the containing object started (within
15216 the structure type). The value we deduce is then used (by the
15217 callers of this routine) to generate DW_AT_location and
15218 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15219 the case of DW_AT_location, regular fields as well). */
15221 /* Figure out the bit-distance from the start of the structure to
15222 the "deepest" bit of the bit-field. */
15223 deepest_bitpos = bitpos_int + field_size_in_bits;
15225 /* This is the tricky part. Use some fancy footwork to deduce
15226 where the lowest addressed bit of the containing object must
15227 be. */
15228 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15230 /* Round up to type_align by default. This works best for
15231 bitfields. */
15232 object_offset_in_bits
15233 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15235 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15237 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15239 /* Round up to decl_align instead. */
15240 object_offset_in_bits
15241 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15244 else
15245 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15246 object_offset_in_bits = bitpos_int;
15248 object_offset_in_bytes
15249 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15250 return object_offset_in_bytes.to_shwi ();
15253 /* The following routines define various Dwarf attributes and any data
15254 associated with them. */
15256 /* Add a location description attribute value to a DIE.
15258 This emits location attributes suitable for whole variables and
15259 whole parameters. Note that the location attributes for struct fields are
15260 generated by the routine `data_member_location_attribute' below. */
15262 static inline void
15263 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15264 dw_loc_list_ref descr)
15266 if (descr == 0)
15267 return;
15268 if (single_element_loc_list_p (descr))
15269 add_AT_loc (die, attr_kind, descr->expr);
15270 else
15271 add_AT_loc_list (die, attr_kind, descr);
15274 /* Add DW_AT_accessibility attribute to DIE if needed. */
15276 static void
15277 add_accessibility_attribute (dw_die_ref die, tree decl)
15279 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15280 children, otherwise the default is DW_ACCESS_public. In DWARF2
15281 the default has always been DW_ACCESS_public. */
15282 if (TREE_PROTECTED (decl))
15283 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15284 else if (TREE_PRIVATE (decl))
15286 if (dwarf_version == 2
15287 || die->die_parent == NULL
15288 || die->die_parent->die_tag != DW_TAG_class_type)
15289 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15291 else if (dwarf_version > 2
15292 && die->die_parent
15293 && die->die_parent->die_tag == DW_TAG_class_type)
15294 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15297 /* Attach the specialized form of location attribute used for data members of
15298 struct and union types. In the special case of a FIELD_DECL node which
15299 represents a bit-field, the "offset" part of this special location
15300 descriptor must indicate the distance in bytes from the lowest-addressed
15301 byte of the containing struct or union type to the lowest-addressed byte of
15302 the "containing object" for the bit-field. (See the `field_byte_offset'
15303 function above).
15305 For any given bit-field, the "containing object" is a hypothetical object
15306 (of some integral or enum type) within which the given bit-field lives. The
15307 type of this hypothetical "containing object" is always the same as the
15308 declared type of the individual bit-field itself (for GCC anyway... the
15309 DWARF spec doesn't actually mandate this). Note that it is the size (in
15310 bytes) of the hypothetical "containing object" which will be given in the
15311 DW_AT_byte_size attribute for this bit-field. (See the
15312 `byte_size_attribute' function below.) It is also used when calculating the
15313 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15314 function below.) */
15316 static void
15317 add_data_member_location_attribute (dw_die_ref die, tree decl)
15319 HOST_WIDE_INT offset;
15320 dw_loc_descr_ref loc_descr = 0;
15322 if (TREE_CODE (decl) == TREE_BINFO)
15324 /* We're working on the TAG_inheritance for a base class. */
15325 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15327 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15328 aren't at a fixed offset from all (sub)objects of the same
15329 type. We need to extract the appropriate offset from our
15330 vtable. The following dwarf expression means
15332 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15334 This is specific to the V3 ABI, of course. */
15336 dw_loc_descr_ref tmp;
15338 /* Make a copy of the object address. */
15339 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15340 add_loc_descr (&loc_descr, tmp);
15342 /* Extract the vtable address. */
15343 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15344 add_loc_descr (&loc_descr, tmp);
15346 /* Calculate the address of the offset. */
15347 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15348 gcc_assert (offset < 0);
15350 tmp = int_loc_descriptor (-offset);
15351 add_loc_descr (&loc_descr, tmp);
15352 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15353 add_loc_descr (&loc_descr, tmp);
15355 /* Extract the offset. */
15356 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15357 add_loc_descr (&loc_descr, tmp);
15359 /* Add it to the object address. */
15360 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15361 add_loc_descr (&loc_descr, tmp);
15363 else
15364 offset = tree_to_shwi (BINFO_OFFSET (decl));
15366 else
15367 offset = field_byte_offset (decl);
15369 if (! loc_descr)
15371 if (dwarf_version > 2)
15373 /* Don't need to output a location expression, just the constant. */
15374 if (offset < 0)
15375 add_AT_int (die, DW_AT_data_member_location, offset);
15376 else
15377 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15378 return;
15380 else
15382 enum dwarf_location_atom op;
15384 /* The DWARF2 standard says that we should assume that the structure
15385 address is already on the stack, so we can specify a structure
15386 field address by using DW_OP_plus_uconst. */
15387 op = DW_OP_plus_uconst;
15388 loc_descr = new_loc_descr (op, offset, 0);
15392 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15395 /* Writes integer values to dw_vec_const array. */
15397 static void
15398 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15400 while (size != 0)
15402 *dest++ = val & 0xff;
15403 val >>= 8;
15404 --size;
15408 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15410 static HOST_WIDE_INT
15411 extract_int (const unsigned char *src, unsigned int size)
15413 HOST_WIDE_INT val = 0;
15415 src += size;
15416 while (size != 0)
15418 val <<= 8;
15419 val |= *--src & 0xff;
15420 --size;
15422 return val;
15425 /* Writes wide_int values to dw_vec_const array. */
15427 static void
15428 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15430 int i;
15432 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15434 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15435 return;
15438 /* We'd have to extend this code to support odd sizes. */
15439 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15441 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15443 if (WORDS_BIG_ENDIAN)
15444 for (i = n - 1; i >= 0; i--)
15446 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15447 dest += sizeof (HOST_WIDE_INT);
15449 else
15450 for (i = 0; i < n; i++)
15452 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15453 dest += sizeof (HOST_WIDE_INT);
15457 /* Writes floating point values to dw_vec_const array. */
15459 static void
15460 insert_float (const_rtx rtl, unsigned char *array)
15462 REAL_VALUE_TYPE rv;
15463 long val[4];
15464 int i;
15466 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15467 real_to_target (val, &rv, GET_MODE (rtl));
15469 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15470 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15472 insert_int (val[i], 4, array);
15473 array += 4;
15477 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15478 does not have a "location" either in memory or in a register. These
15479 things can arise in GNU C when a constant is passed as an actual parameter
15480 to an inlined function. They can also arise in C++ where declared
15481 constants do not necessarily get memory "homes". */
15483 static bool
15484 add_const_value_attribute (dw_die_ref die, rtx rtl)
15486 switch (GET_CODE (rtl))
15488 case CONST_INT:
15490 HOST_WIDE_INT val = INTVAL (rtl);
15492 if (val < 0)
15493 add_AT_int (die, DW_AT_const_value, val);
15494 else
15495 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15497 return true;
15499 case CONST_WIDE_INT:
15500 add_AT_wide (die, DW_AT_const_value,
15501 std::make_pair (rtl, GET_MODE (rtl)));
15502 return true;
15504 case CONST_DOUBLE:
15505 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15506 floating-point constant. A CONST_DOUBLE is used whenever the
15507 constant requires more than one word in order to be adequately
15508 represented. */
15510 machine_mode mode = GET_MODE (rtl);
15512 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15513 add_AT_double (die, DW_AT_const_value,
15514 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15515 else
15517 unsigned int length = GET_MODE_SIZE (mode);
15518 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15520 insert_float (rtl, array);
15521 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15524 return true;
15526 case CONST_VECTOR:
15528 machine_mode mode = GET_MODE (rtl);
15529 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15530 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15531 unsigned char *array
15532 = ggc_vec_alloc<unsigned char> (length * elt_size);
15533 unsigned int i;
15534 unsigned char *p;
15535 machine_mode imode = GET_MODE_INNER (mode);
15537 switch (GET_MODE_CLASS (mode))
15539 case MODE_VECTOR_INT:
15540 for (i = 0, p = array; i < length; i++, p += elt_size)
15542 rtx elt = CONST_VECTOR_ELT (rtl, i);
15543 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15545 break;
15547 case MODE_VECTOR_FLOAT:
15548 for (i = 0, p = array; i < length; i++, p += elt_size)
15550 rtx elt = CONST_VECTOR_ELT (rtl, i);
15551 insert_float (elt, p);
15553 break;
15555 default:
15556 gcc_unreachable ();
15559 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15561 return true;
15563 case CONST_STRING:
15564 if (dwarf_version >= 4 || !dwarf_strict)
15566 dw_loc_descr_ref loc_result;
15567 resolve_one_addr (&rtl);
15568 rtl_addr:
15569 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15570 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15571 add_AT_loc (die, DW_AT_location, loc_result);
15572 vec_safe_push (used_rtx_array, rtl);
15573 return true;
15575 return false;
15577 case CONST:
15578 if (CONSTANT_P (XEXP (rtl, 0)))
15579 return add_const_value_attribute (die, XEXP (rtl, 0));
15580 /* FALLTHROUGH */
15581 case SYMBOL_REF:
15582 if (!const_ok_for_output (rtl))
15583 return false;
15584 case LABEL_REF:
15585 if (dwarf_version >= 4 || !dwarf_strict)
15586 goto rtl_addr;
15587 return false;
15589 case PLUS:
15590 /* In cases where an inlined instance of an inline function is passed
15591 the address of an `auto' variable (which is local to the caller) we
15592 can get a situation where the DECL_RTL of the artificial local
15593 variable (for the inlining) which acts as a stand-in for the
15594 corresponding formal parameter (of the inline function) will look
15595 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15596 exactly a compile-time constant expression, but it isn't the address
15597 of the (artificial) local variable either. Rather, it represents the
15598 *value* which the artificial local variable always has during its
15599 lifetime. We currently have no way to represent such quasi-constant
15600 values in Dwarf, so for now we just punt and generate nothing. */
15601 return false;
15603 case HIGH:
15604 case CONST_FIXED:
15605 return false;
15607 case MEM:
15608 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15609 && MEM_READONLY_P (rtl)
15610 && GET_MODE (rtl) == BLKmode)
15612 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15613 return true;
15615 return false;
15617 default:
15618 /* No other kinds of rtx should be possible here. */
15619 gcc_unreachable ();
15621 return false;
15624 /* Determine whether the evaluation of EXPR references any variables
15625 or functions which aren't otherwise used (and therefore may not be
15626 output). */
15627 static tree
15628 reference_to_unused (tree * tp, int * walk_subtrees,
15629 void * data ATTRIBUTE_UNUSED)
15631 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15632 *walk_subtrees = 0;
15634 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15635 && ! TREE_ASM_WRITTEN (*tp))
15636 return *tp;
15637 /* ??? The C++ FE emits debug information for using decls, so
15638 putting gcc_unreachable here falls over. See PR31899. For now
15639 be conservative. */
15640 else if (!symtab->global_info_ready
15641 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15642 return *tp;
15643 else if (TREE_CODE (*tp) == VAR_DECL)
15645 varpool_node *node = varpool_node::get (*tp);
15646 if (!node || !node->definition)
15647 return *tp;
15649 else if (TREE_CODE (*tp) == FUNCTION_DECL
15650 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15652 /* The call graph machinery must have finished analyzing,
15653 optimizing and gimplifying the CU by now.
15654 So if *TP has no call graph node associated
15655 to it, it means *TP will not be emitted. */
15656 if (!cgraph_node::get (*tp))
15657 return *tp;
15659 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15660 return *tp;
15662 return NULL_TREE;
15665 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15666 for use in a later add_const_value_attribute call. */
15668 static rtx
15669 rtl_for_decl_init (tree init, tree type)
15671 rtx rtl = NULL_RTX;
15673 STRIP_NOPS (init);
15675 /* If a variable is initialized with a string constant without embedded
15676 zeros, build CONST_STRING. */
15677 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15679 tree enttype = TREE_TYPE (type);
15680 tree domain = TYPE_DOMAIN (type);
15681 machine_mode mode = TYPE_MODE (enttype);
15683 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15684 && domain
15685 && integer_zerop (TYPE_MIN_VALUE (domain))
15686 && compare_tree_int (TYPE_MAX_VALUE (domain),
15687 TREE_STRING_LENGTH (init) - 1) == 0
15688 && ((size_t) TREE_STRING_LENGTH (init)
15689 == strlen (TREE_STRING_POINTER (init)) + 1))
15691 rtl = gen_rtx_CONST_STRING (VOIDmode,
15692 ggc_strdup (TREE_STRING_POINTER (init)));
15693 rtl = gen_rtx_MEM (BLKmode, rtl);
15694 MEM_READONLY_P (rtl) = 1;
15697 /* Other aggregates, and complex values, could be represented using
15698 CONCAT: FIXME! */
15699 else if (AGGREGATE_TYPE_P (type)
15700 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15701 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15702 || TREE_CODE (type) == COMPLEX_TYPE)
15704 /* Vectors only work if their mode is supported by the target.
15705 FIXME: generic vectors ought to work too. */
15706 else if (TREE_CODE (type) == VECTOR_TYPE
15707 && !VECTOR_MODE_P (TYPE_MODE (type)))
15709 /* If the initializer is something that we know will expand into an
15710 immediate RTL constant, expand it now. We must be careful not to
15711 reference variables which won't be output. */
15712 else if (initializer_constant_valid_p (init, type)
15713 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15715 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15716 possible. */
15717 if (TREE_CODE (type) == VECTOR_TYPE)
15718 switch (TREE_CODE (init))
15720 case VECTOR_CST:
15721 break;
15722 case CONSTRUCTOR:
15723 if (TREE_CONSTANT (init))
15725 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15726 bool constant_p = true;
15727 tree value;
15728 unsigned HOST_WIDE_INT ix;
15730 /* Even when ctor is constant, it might contain non-*_CST
15731 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15732 belong into VECTOR_CST nodes. */
15733 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15734 if (!CONSTANT_CLASS_P (value))
15736 constant_p = false;
15737 break;
15740 if (constant_p)
15742 init = build_vector_from_ctor (type, elts);
15743 break;
15746 /* FALLTHRU */
15748 default:
15749 return NULL;
15752 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15754 /* If expand_expr returns a MEM, it wasn't immediate. */
15755 gcc_assert (!rtl || !MEM_P (rtl));
15758 return rtl;
15761 /* Generate RTL for the variable DECL to represent its location. */
15763 static rtx
15764 rtl_for_decl_location (tree decl)
15766 rtx rtl;
15768 /* Here we have to decide where we are going to say the parameter "lives"
15769 (as far as the debugger is concerned). We only have a couple of
15770 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15772 DECL_RTL normally indicates where the parameter lives during most of the
15773 activation of the function. If optimization is enabled however, this
15774 could be either NULL or else a pseudo-reg. Both of those cases indicate
15775 that the parameter doesn't really live anywhere (as far as the code
15776 generation parts of GCC are concerned) during most of the function's
15777 activation. That will happen (for example) if the parameter is never
15778 referenced within the function.
15780 We could just generate a location descriptor here for all non-NULL
15781 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15782 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15783 where DECL_RTL is NULL or is a pseudo-reg.
15785 Note however that we can only get away with using DECL_INCOMING_RTL as
15786 a backup substitute for DECL_RTL in certain limited cases. In cases
15787 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15788 we can be sure that the parameter was passed using the same type as it is
15789 declared to have within the function, and that its DECL_INCOMING_RTL
15790 points us to a place where a value of that type is passed.
15792 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15793 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15794 because in these cases DECL_INCOMING_RTL points us to a value of some
15795 type which is *different* from the type of the parameter itself. Thus,
15796 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15797 such cases, the debugger would end up (for example) trying to fetch a
15798 `float' from a place which actually contains the first part of a
15799 `double'. That would lead to really incorrect and confusing
15800 output at debug-time.
15802 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15803 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15804 are a couple of exceptions however. On little-endian machines we can
15805 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15806 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15807 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15808 when (on a little-endian machine) a non-prototyped function has a
15809 parameter declared to be of type `short' or `char'. In such cases,
15810 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15811 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15812 passed `int' value. If the debugger then uses that address to fetch
15813 a `short' or a `char' (on a little-endian machine) the result will be
15814 the correct data, so we allow for such exceptional cases below.
15816 Note that our goal here is to describe the place where the given formal
15817 parameter lives during most of the function's activation (i.e. between the
15818 end of the prologue and the start of the epilogue). We'll do that as best
15819 as we can. Note however that if the given formal parameter is modified
15820 sometime during the execution of the function, then a stack backtrace (at
15821 debug-time) will show the function as having been called with the *new*
15822 value rather than the value which was originally passed in. This happens
15823 rarely enough that it is not a major problem, but it *is* a problem, and
15824 I'd like to fix it.
15826 A future version of dwarf2out.c may generate two additional attributes for
15827 any given DW_TAG_formal_parameter DIE which will describe the "passed
15828 type" and the "passed location" for the given formal parameter in addition
15829 to the attributes we now generate to indicate the "declared type" and the
15830 "active location" for each parameter. This additional set of attributes
15831 could be used by debuggers for stack backtraces. Separately, note that
15832 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15833 This happens (for example) for inlined-instances of inline function formal
15834 parameters which are never referenced. This really shouldn't be
15835 happening. All PARM_DECL nodes should get valid non-NULL
15836 DECL_INCOMING_RTL values. FIXME. */
15838 /* Use DECL_RTL as the "location" unless we find something better. */
15839 rtl = DECL_RTL_IF_SET (decl);
15841 /* When generating abstract instances, ignore everything except
15842 constants, symbols living in memory, and symbols living in
15843 fixed registers. */
15844 if (! reload_completed)
15846 if (rtl
15847 && (CONSTANT_P (rtl)
15848 || (MEM_P (rtl)
15849 && CONSTANT_P (XEXP (rtl, 0)))
15850 || (REG_P (rtl)
15851 && TREE_CODE (decl) == VAR_DECL
15852 && TREE_STATIC (decl))))
15854 rtl = targetm.delegitimize_address (rtl);
15855 return rtl;
15857 rtl = NULL_RTX;
15859 else if (TREE_CODE (decl) == PARM_DECL)
15861 if (rtl == NULL_RTX
15862 || is_pseudo_reg (rtl)
15863 || (MEM_P (rtl)
15864 && is_pseudo_reg (XEXP (rtl, 0))
15865 && DECL_INCOMING_RTL (decl)
15866 && MEM_P (DECL_INCOMING_RTL (decl))
15867 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15869 tree declared_type = TREE_TYPE (decl);
15870 tree passed_type = DECL_ARG_TYPE (decl);
15871 machine_mode dmode = TYPE_MODE (declared_type);
15872 machine_mode pmode = TYPE_MODE (passed_type);
15874 /* This decl represents a formal parameter which was optimized out.
15875 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15876 all cases where (rtl == NULL_RTX) just below. */
15877 if (dmode == pmode)
15878 rtl = DECL_INCOMING_RTL (decl);
15879 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15880 && SCALAR_INT_MODE_P (dmode)
15881 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15882 && DECL_INCOMING_RTL (decl))
15884 rtx inc = DECL_INCOMING_RTL (decl);
15885 if (REG_P (inc))
15886 rtl = inc;
15887 else if (MEM_P (inc))
15889 if (BYTES_BIG_ENDIAN)
15890 rtl = adjust_address_nv (inc, dmode,
15891 GET_MODE_SIZE (pmode)
15892 - GET_MODE_SIZE (dmode));
15893 else
15894 rtl = inc;
15899 /* If the parm was passed in registers, but lives on the stack, then
15900 make a big endian correction if the mode of the type of the
15901 parameter is not the same as the mode of the rtl. */
15902 /* ??? This is the same series of checks that are made in dbxout.c before
15903 we reach the big endian correction code there. It isn't clear if all
15904 of these checks are necessary here, but keeping them all is the safe
15905 thing to do. */
15906 else if (MEM_P (rtl)
15907 && XEXP (rtl, 0) != const0_rtx
15908 && ! CONSTANT_P (XEXP (rtl, 0))
15909 /* Not passed in memory. */
15910 && !MEM_P (DECL_INCOMING_RTL (decl))
15911 /* Not passed by invisible reference. */
15912 && (!REG_P (XEXP (rtl, 0))
15913 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15914 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15915 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15916 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15917 #endif
15919 /* Big endian correction check. */
15920 && BYTES_BIG_ENDIAN
15921 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15922 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15923 < UNITS_PER_WORD))
15925 machine_mode addr_mode = get_address_mode (rtl);
15926 int offset = (UNITS_PER_WORD
15927 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15929 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15930 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15933 else if (TREE_CODE (decl) == VAR_DECL
15934 && rtl
15935 && MEM_P (rtl)
15936 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15937 && BYTES_BIG_ENDIAN)
15939 machine_mode addr_mode = get_address_mode (rtl);
15940 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15941 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15943 /* If a variable is declared "register" yet is smaller than
15944 a register, then if we store the variable to memory, it
15945 looks like we're storing a register-sized value, when in
15946 fact we are not. We need to adjust the offset of the
15947 storage location to reflect the actual value's bytes,
15948 else gdb will not be able to display it. */
15949 if (rsize > dsize)
15950 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15951 plus_constant (addr_mode, XEXP (rtl, 0),
15952 rsize - dsize));
15955 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15956 and will have been substituted directly into all expressions that use it.
15957 C does not have such a concept, but C++ and other languages do. */
15958 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15959 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15961 if (rtl)
15962 rtl = targetm.delegitimize_address (rtl);
15964 /* If we don't look past the constant pool, we risk emitting a
15965 reference to a constant pool entry that isn't referenced from
15966 code, and thus is not emitted. */
15967 if (rtl)
15968 rtl = avoid_constant_pool_reference (rtl);
15970 /* Try harder to get a rtl. If this symbol ends up not being emitted
15971 in the current CU, resolve_addr will remove the expression referencing
15972 it. */
15973 if (rtl == NULL_RTX
15974 && TREE_CODE (decl) == VAR_DECL
15975 && !DECL_EXTERNAL (decl)
15976 && TREE_STATIC (decl)
15977 && DECL_NAME (decl)
15978 && !DECL_HARD_REGISTER (decl)
15979 && DECL_MODE (decl) != VOIDmode)
15981 rtl = make_decl_rtl_for_debug (decl);
15982 if (!MEM_P (rtl)
15983 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15984 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15985 rtl = NULL_RTX;
15988 return rtl;
15991 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15992 returned. If so, the decl for the COMMON block is returned, and the
15993 value is the offset into the common block for the symbol. */
15995 static tree
15996 fortran_common (tree decl, HOST_WIDE_INT *value)
15998 tree val_expr, cvar;
15999 machine_mode mode;
16000 HOST_WIDE_INT bitsize, bitpos;
16001 tree offset;
16002 int unsignedp, volatilep = 0;
16004 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16005 it does not have a value (the offset into the common area), or if it
16006 is thread local (as opposed to global) then it isn't common, and shouldn't
16007 be handled as such. */
16008 if (TREE_CODE (decl) != VAR_DECL
16009 || !TREE_STATIC (decl)
16010 || !DECL_HAS_VALUE_EXPR_P (decl)
16011 || !is_fortran ())
16012 return NULL_TREE;
16014 val_expr = DECL_VALUE_EXPR (decl);
16015 if (TREE_CODE (val_expr) != COMPONENT_REF)
16016 return NULL_TREE;
16018 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16019 &mode, &unsignedp, &volatilep, true);
16021 if (cvar == NULL_TREE
16022 || TREE_CODE (cvar) != VAR_DECL
16023 || DECL_ARTIFICIAL (cvar)
16024 || !TREE_PUBLIC (cvar))
16025 return NULL_TREE;
16027 *value = 0;
16028 if (offset != NULL)
16030 if (!tree_fits_shwi_p (offset))
16031 return NULL_TREE;
16032 *value = tree_to_shwi (offset);
16034 if (bitpos != 0)
16035 *value += bitpos / BITS_PER_UNIT;
16037 return cvar;
16040 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16041 data attribute for a variable or a parameter. We generate the
16042 DW_AT_const_value attribute only in those cases where the given variable
16043 or parameter does not have a true "location" either in memory or in a
16044 register. This can happen (for example) when a constant is passed as an
16045 actual argument in a call to an inline function. (It's possible that
16046 these things can crop up in other ways also.) Note that one type of
16047 constant value which can be passed into an inlined function is a constant
16048 pointer. This can happen for example if an actual argument in an inlined
16049 function call evaluates to a compile-time constant address.
16051 CACHE_P is true if it is worth caching the location list for DECL,
16052 so that future calls can reuse it rather than regenerate it from scratch.
16053 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16054 since we will need to refer to them each time the function is inlined. */
16056 static bool
16057 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16058 enum dwarf_attribute attr)
16060 rtx rtl;
16061 dw_loc_list_ref list;
16062 var_loc_list *loc_list;
16063 cached_dw_loc_list *cache;
16065 if (TREE_CODE (decl) == ERROR_MARK)
16066 return false;
16068 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16069 || TREE_CODE (decl) == RESULT_DECL);
16071 /* Try to get some constant RTL for this decl, and use that as the value of
16072 the location. */
16074 rtl = rtl_for_decl_location (decl);
16075 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16076 && add_const_value_attribute (die, rtl))
16077 return true;
16079 /* See if we have single element location list that is equivalent to
16080 a constant value. That way we are better to use add_const_value_attribute
16081 rather than expanding constant value equivalent. */
16082 loc_list = lookup_decl_loc (decl);
16083 if (loc_list
16084 && loc_list->first
16085 && loc_list->first->next == NULL
16086 && NOTE_P (loc_list->first->loc)
16087 && NOTE_VAR_LOCATION (loc_list->first->loc)
16088 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16090 struct var_loc_node *node;
16092 node = loc_list->first;
16093 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16094 if (GET_CODE (rtl) == EXPR_LIST)
16095 rtl = XEXP (rtl, 0);
16096 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16097 && add_const_value_attribute (die, rtl))
16098 return true;
16100 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16101 list several times. See if we've already cached the contents. */
16102 list = NULL;
16103 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16104 cache_p = false;
16105 if (cache_p)
16107 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16108 if (cache)
16109 list = cache->loc_list;
16111 if (list == NULL)
16113 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16114 NULL);
16115 /* It is usually worth caching this result if the decl is from
16116 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16117 if (cache_p && list && list->dw_loc_next)
16119 cached_dw_loc_list **slot
16120 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16121 DECL_UID (decl),
16122 INSERT);
16123 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16124 cache->decl_id = DECL_UID (decl);
16125 cache->loc_list = list;
16126 *slot = cache;
16129 if (list)
16131 add_AT_location_description (die, attr, list);
16132 return true;
16134 /* None of that worked, so it must not really have a location;
16135 try adding a constant value attribute from the DECL_INITIAL. */
16136 return tree_add_const_value_attribute_for_decl (die, decl);
16139 /* Add VARIABLE and DIE into deferred locations list. */
16141 static void
16142 defer_location (tree variable, dw_die_ref die)
16144 deferred_locations entry;
16145 entry.variable = variable;
16146 entry.die = die;
16147 vec_safe_push (deferred_locations_list, entry);
16150 /* Helper function for tree_add_const_value_attribute. Natively encode
16151 initializer INIT into an array. Return true if successful. */
16153 static bool
16154 native_encode_initializer (tree init, unsigned char *array, int size)
16156 tree type;
16158 if (init == NULL_TREE)
16159 return false;
16161 STRIP_NOPS (init);
16162 switch (TREE_CODE (init))
16164 case STRING_CST:
16165 type = TREE_TYPE (init);
16166 if (TREE_CODE (type) == ARRAY_TYPE)
16168 tree enttype = TREE_TYPE (type);
16169 machine_mode mode = TYPE_MODE (enttype);
16171 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16172 return false;
16173 if (int_size_in_bytes (type) != size)
16174 return false;
16175 if (size > TREE_STRING_LENGTH (init))
16177 memcpy (array, TREE_STRING_POINTER (init),
16178 TREE_STRING_LENGTH (init));
16179 memset (array + TREE_STRING_LENGTH (init),
16180 '\0', size - TREE_STRING_LENGTH (init));
16182 else
16183 memcpy (array, TREE_STRING_POINTER (init), size);
16184 return true;
16186 return false;
16187 case CONSTRUCTOR:
16188 type = TREE_TYPE (init);
16189 if (int_size_in_bytes (type) != size)
16190 return false;
16191 if (TREE_CODE (type) == ARRAY_TYPE)
16193 HOST_WIDE_INT min_index;
16194 unsigned HOST_WIDE_INT cnt;
16195 int curpos = 0, fieldsize;
16196 constructor_elt *ce;
16198 if (TYPE_DOMAIN (type) == NULL_TREE
16199 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16200 return false;
16202 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16203 if (fieldsize <= 0)
16204 return false;
16206 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16207 memset (array, '\0', size);
16208 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16210 tree val = ce->value;
16211 tree index = ce->index;
16212 int pos = curpos;
16213 if (index && TREE_CODE (index) == RANGE_EXPR)
16214 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16215 * fieldsize;
16216 else if (index)
16217 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16219 if (val)
16221 STRIP_NOPS (val);
16222 if (!native_encode_initializer (val, array + pos, fieldsize))
16223 return false;
16225 curpos = pos + fieldsize;
16226 if (index && TREE_CODE (index) == RANGE_EXPR)
16228 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16229 - tree_to_shwi (TREE_OPERAND (index, 0));
16230 while (count-- > 0)
16232 if (val)
16233 memcpy (array + curpos, array + pos, fieldsize);
16234 curpos += fieldsize;
16237 gcc_assert (curpos <= size);
16239 return true;
16241 else if (TREE_CODE (type) == RECORD_TYPE
16242 || TREE_CODE (type) == UNION_TYPE)
16244 tree field = NULL_TREE;
16245 unsigned HOST_WIDE_INT cnt;
16246 constructor_elt *ce;
16248 if (int_size_in_bytes (type) != size)
16249 return false;
16251 if (TREE_CODE (type) == RECORD_TYPE)
16252 field = TYPE_FIELDS (type);
16254 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16256 tree val = ce->value;
16257 int pos, fieldsize;
16259 if (ce->index != 0)
16260 field = ce->index;
16262 if (val)
16263 STRIP_NOPS (val);
16265 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16266 return false;
16268 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16269 && TYPE_DOMAIN (TREE_TYPE (field))
16270 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16271 return false;
16272 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16273 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16274 return false;
16275 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16276 pos = int_byte_position (field);
16277 gcc_assert (pos + fieldsize <= size);
16278 if (val
16279 && !native_encode_initializer (val, array + pos, fieldsize))
16280 return false;
16282 return true;
16284 return false;
16285 case VIEW_CONVERT_EXPR:
16286 case NON_LVALUE_EXPR:
16287 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16288 default:
16289 return native_encode_expr (init, array, size) == size;
16293 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16294 attribute is the const value T. */
16296 static bool
16297 tree_add_const_value_attribute (dw_die_ref die, tree t)
16299 tree init;
16300 tree type = TREE_TYPE (t);
16301 rtx rtl;
16303 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16304 return false;
16306 init = t;
16307 gcc_assert (!DECL_P (init));
16309 rtl = rtl_for_decl_init (init, type);
16310 if (rtl)
16311 return add_const_value_attribute (die, rtl);
16312 /* If the host and target are sane, try harder. */
16313 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16314 && initializer_constant_valid_p (init, type))
16316 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16317 if (size > 0 && (int) size == size)
16319 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16321 if (native_encode_initializer (init, array, size))
16323 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16324 return true;
16326 ggc_free (array);
16329 return false;
16332 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16333 attribute is the const value of T, where T is an integral constant
16334 variable with static storage duration
16335 (so it can't be a PARM_DECL or a RESULT_DECL). */
16337 static bool
16338 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16341 if (!decl
16342 || (TREE_CODE (decl) != VAR_DECL
16343 && TREE_CODE (decl) != CONST_DECL)
16344 || (TREE_CODE (decl) == VAR_DECL
16345 && !TREE_STATIC (decl)))
16346 return false;
16348 if (TREE_READONLY (decl)
16349 && ! TREE_THIS_VOLATILE (decl)
16350 && DECL_INITIAL (decl))
16351 /* OK */;
16352 else
16353 return false;
16355 /* Don't add DW_AT_const_value if abstract origin already has one. */
16356 if (get_AT (var_die, DW_AT_const_value))
16357 return false;
16359 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16362 /* Convert the CFI instructions for the current function into a
16363 location list. This is used for DW_AT_frame_base when we targeting
16364 a dwarf2 consumer that does not support the dwarf3
16365 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16366 expressions. */
16368 static dw_loc_list_ref
16369 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16371 int ix;
16372 dw_fde_ref fde;
16373 dw_loc_list_ref list, *list_tail;
16374 dw_cfi_ref cfi;
16375 dw_cfa_location last_cfa, next_cfa;
16376 const char *start_label, *last_label, *section;
16377 dw_cfa_location remember;
16379 fde = cfun->fde;
16380 gcc_assert (fde != NULL);
16382 section = secname_for_decl (current_function_decl);
16383 list_tail = &list;
16384 list = NULL;
16386 memset (&next_cfa, 0, sizeof (next_cfa));
16387 next_cfa.reg = INVALID_REGNUM;
16388 remember = next_cfa;
16390 start_label = fde->dw_fde_begin;
16392 /* ??? Bald assumption that the CIE opcode list does not contain
16393 advance opcodes. */
16394 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16395 lookup_cfa_1 (cfi, &next_cfa, &remember);
16397 last_cfa = next_cfa;
16398 last_label = start_label;
16400 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16402 /* If the first partition contained no CFI adjustments, the
16403 CIE opcodes apply to the whole first partition. */
16404 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16405 fde->dw_fde_begin, fde->dw_fde_end, section);
16406 list_tail =&(*list_tail)->dw_loc_next;
16407 start_label = last_label = fde->dw_fde_second_begin;
16410 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16412 switch (cfi->dw_cfi_opc)
16414 case DW_CFA_set_loc:
16415 case DW_CFA_advance_loc1:
16416 case DW_CFA_advance_loc2:
16417 case DW_CFA_advance_loc4:
16418 if (!cfa_equal_p (&last_cfa, &next_cfa))
16420 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16421 start_label, last_label, section);
16423 list_tail = &(*list_tail)->dw_loc_next;
16424 last_cfa = next_cfa;
16425 start_label = last_label;
16427 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16428 break;
16430 case DW_CFA_advance_loc:
16431 /* The encoding is complex enough that we should never emit this. */
16432 gcc_unreachable ();
16434 default:
16435 lookup_cfa_1 (cfi, &next_cfa, &remember);
16436 break;
16438 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16440 if (!cfa_equal_p (&last_cfa, &next_cfa))
16442 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16443 start_label, last_label, section);
16445 list_tail = &(*list_tail)->dw_loc_next;
16446 last_cfa = next_cfa;
16447 start_label = last_label;
16449 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16450 start_label, fde->dw_fde_end, section);
16451 list_tail = &(*list_tail)->dw_loc_next;
16452 start_label = last_label = fde->dw_fde_second_begin;
16456 if (!cfa_equal_p (&last_cfa, &next_cfa))
16458 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16459 start_label, last_label, section);
16460 list_tail = &(*list_tail)->dw_loc_next;
16461 start_label = last_label;
16464 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16465 start_label,
16466 fde->dw_fde_second_begin
16467 ? fde->dw_fde_second_end : fde->dw_fde_end,
16468 section);
16470 if (list && list->dw_loc_next)
16471 gen_llsym (list);
16473 return list;
16476 /* Compute a displacement from the "steady-state frame pointer" to the
16477 frame base (often the same as the CFA), and store it in
16478 frame_pointer_fb_offset. OFFSET is added to the displacement
16479 before the latter is negated. */
16481 static void
16482 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16484 rtx reg, elim;
16486 #ifdef FRAME_POINTER_CFA_OFFSET
16487 reg = frame_pointer_rtx;
16488 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16489 #else
16490 reg = arg_pointer_rtx;
16491 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16492 #endif
16494 elim = (ira_use_lra_p
16495 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16496 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16497 if (GET_CODE (elim) == PLUS)
16499 offset += INTVAL (XEXP (elim, 1));
16500 elim = XEXP (elim, 0);
16503 frame_pointer_fb_offset = -offset;
16505 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16506 in which to eliminate. This is because it's stack pointer isn't
16507 directly accessible as a register within the ISA. To work around
16508 this, assume that while we cannot provide a proper value for
16509 frame_pointer_fb_offset, we won't need one either. */
16510 frame_pointer_fb_offset_valid
16511 = ((SUPPORTS_STACK_ALIGNMENT
16512 && (elim == hard_frame_pointer_rtx
16513 || elim == stack_pointer_rtx))
16514 || elim == (frame_pointer_needed
16515 ? hard_frame_pointer_rtx
16516 : stack_pointer_rtx));
16519 /* Generate a DW_AT_name attribute given some string value to be included as
16520 the value of the attribute. */
16522 static void
16523 add_name_attribute (dw_die_ref die, const char *name_string)
16525 if (name_string != NULL && *name_string != 0)
16527 if (demangle_name_func)
16528 name_string = (*demangle_name_func) (name_string);
16530 add_AT_string (die, DW_AT_name, name_string);
16534 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16535 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16536 of TYPE accordingly.
16538 ??? This is a temporary measure until after we're able to generate
16539 regular DWARF for the complex Ada type system. */
16541 static void
16542 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16543 dw_die_ref context_die)
16545 tree dtype;
16546 dw_die_ref dtype_die;
16548 if (!lang_hooks.types.descriptive_type)
16549 return;
16551 dtype = lang_hooks.types.descriptive_type (type);
16552 if (!dtype)
16553 return;
16555 dtype_die = lookup_type_die (dtype);
16556 if (!dtype_die)
16558 gen_type_die (dtype, context_die);
16559 dtype_die = lookup_type_die (dtype);
16560 gcc_assert (dtype_die);
16563 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16566 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16568 static const char *
16569 comp_dir_string (void)
16571 const char *wd;
16572 char *wd1;
16573 static const char *cached_wd = NULL;
16575 if (cached_wd != NULL)
16576 return cached_wd;
16578 wd = get_src_pwd ();
16579 if (wd == NULL)
16580 return NULL;
16582 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16584 int wdlen;
16586 wdlen = strlen (wd);
16587 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16588 strcpy (wd1, wd);
16589 wd1 [wdlen] = DIR_SEPARATOR;
16590 wd1 [wdlen + 1] = 0;
16591 wd = wd1;
16594 cached_wd = remap_debug_filename (wd);
16595 return cached_wd;
16598 /* Generate a DW_AT_comp_dir attribute for DIE. */
16600 static void
16601 add_comp_dir_attribute (dw_die_ref die)
16603 const char * wd = comp_dir_string ();
16604 if (wd != NULL)
16605 add_AT_string (die, DW_AT_comp_dir, wd);
16608 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16609 pointer computation, ...), output a representation for that bound according
16610 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16611 loc_list_from_tree for the meaning of CONTEXT. */
16613 static void
16614 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16615 int forms, const struct loc_descr_context *context)
16617 dw_die_ref ctx, decl_die;
16618 dw_loc_list_ref list;
16620 bool strip_conversions = true;
16622 while (strip_conversions)
16623 switch (TREE_CODE (value))
16625 case ERROR_MARK:
16626 case SAVE_EXPR:
16627 return;
16629 CASE_CONVERT:
16630 case VIEW_CONVERT_EXPR:
16631 value = TREE_OPERAND (value, 0);
16632 break;
16634 default:
16635 strip_conversions = false;
16636 break;
16639 /* If possible and permitted, output the attribute as a constant. */
16640 if ((forms & dw_scalar_form_constant) != 0
16641 && TREE_CODE (value) == INTEGER_CST)
16643 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16645 /* If HOST_WIDE_INT is big enough then represent the bound as
16646 a constant value. We need to choose a form based on
16647 whether the type is signed or unsigned. We cannot just
16648 call add_AT_unsigned if the value itself is positive
16649 (add_AT_unsigned might add the unsigned value encoded as
16650 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16651 bounds type and then sign extend any unsigned values found
16652 for signed types. This is needed only for
16653 DW_AT_{lower,upper}_bound, since for most other attributes,
16654 consumers will treat DW_FORM_data[1248] as unsigned values,
16655 regardless of the underlying type. */
16656 if (prec <= HOST_BITS_PER_WIDE_INT
16657 || tree_fits_uhwi_p (value))
16659 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16660 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16661 else
16662 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16664 else
16665 /* Otherwise represent the bound as an unsigned value with
16666 the precision of its type. The precision and signedness
16667 of the type will be necessary to re-interpret it
16668 unambiguously. */
16669 add_AT_wide (die, attr, value);
16670 return;
16673 /* Otherwise, if it's possible and permitted too, output a reference to
16674 another DIE. */
16675 if ((forms & dw_scalar_form_reference) != 0)
16677 tree decl = NULL_TREE;
16679 /* Some type attributes reference an outer type. For instance, the upper
16680 bound of an array may reference an embedding record (this happens in
16681 Ada). */
16682 if (TREE_CODE (value) == COMPONENT_REF
16683 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16684 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16685 decl = TREE_OPERAND (value, 1);
16687 else if (TREE_CODE (value) == VAR_DECL
16688 || TREE_CODE (value) == PARM_DECL
16689 || TREE_CODE (value) == RESULT_DECL)
16690 decl = value;
16692 if (decl != NULL_TREE)
16694 dw_die_ref decl_die = lookup_decl_die (decl);
16696 /* ??? Can this happen, or should the variable have been bound
16697 first? Probably it can, since I imagine that we try to create
16698 the types of parameters in the order in which they exist in
16699 the list, and won't have created a forward reference to a
16700 later parameter. */
16701 if (decl_die != NULL)
16703 add_AT_die_ref (die, attr, decl_die);
16704 return;
16709 /* Last chance: try to create a stack operation procedure to evaluate the
16710 value. Do nothing if even that is not possible or permitted. */
16711 if ((forms & dw_scalar_form_exprloc) == 0)
16712 return;
16714 list = loc_list_from_tree (value, 2, context);
16715 if (list == NULL || single_element_loc_list_p (list))
16717 /* If this attribute is not a reference nor constant, it is
16718 a DWARF expression rather than location description. For that
16719 loc_list_from_tree (value, 0, &context) is needed. */
16720 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16721 if (list2 && single_element_loc_list_p (list2))
16723 add_AT_loc (die, attr, list2->expr);
16724 return;
16728 /* If that failed to give a single element location list, fall back to
16729 outputting this as a reference... still if permitted. */
16730 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16731 return;
16733 if (current_function_decl == 0)
16734 ctx = comp_unit_die ();
16735 else
16736 ctx = lookup_decl_die (current_function_decl);
16738 decl_die = new_die (DW_TAG_variable, ctx, value);
16739 add_AT_flag (decl_die, DW_AT_artificial, 1);
16740 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16741 add_AT_location_description (decl_die, DW_AT_location, list);
16742 add_AT_die_ref (die, attr, decl_die);
16745 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16746 default. */
16748 static int
16749 lower_bound_default (void)
16751 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16753 case DW_LANG_C:
16754 case DW_LANG_C89:
16755 case DW_LANG_C99:
16756 case DW_LANG_C11:
16757 case DW_LANG_C_plus_plus:
16758 case DW_LANG_C_plus_plus_11:
16759 case DW_LANG_C_plus_plus_14:
16760 case DW_LANG_ObjC:
16761 case DW_LANG_ObjC_plus_plus:
16762 case DW_LANG_Java:
16763 return 0;
16764 case DW_LANG_Fortran77:
16765 case DW_LANG_Fortran90:
16766 case DW_LANG_Fortran95:
16767 return 1;
16768 case DW_LANG_UPC:
16769 case DW_LANG_D:
16770 case DW_LANG_Python:
16771 return dwarf_version >= 4 ? 0 : -1;
16772 case DW_LANG_Ada95:
16773 case DW_LANG_Ada83:
16774 case DW_LANG_Cobol74:
16775 case DW_LANG_Cobol85:
16776 case DW_LANG_Pascal83:
16777 case DW_LANG_Modula2:
16778 case DW_LANG_PLI:
16779 return dwarf_version >= 4 ? 1 : -1;
16780 default:
16781 return -1;
16785 /* Given a tree node describing an array bound (either lower or upper) output
16786 a representation for that bound. */
16788 static void
16789 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16790 tree bound, const struct loc_descr_context *context)
16792 int dflt;
16794 while (1)
16795 switch (TREE_CODE (bound))
16797 /* Strip all conversions. */
16798 CASE_CONVERT:
16799 case VIEW_CONVERT_EXPR:
16800 bound = TREE_OPERAND (bound, 0);
16801 break;
16803 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16804 are even omitted when they are the default. */
16805 case INTEGER_CST:
16806 /* If the value for this bound is the default one, we can even omit the
16807 attribute. */
16808 if (bound_attr == DW_AT_lower_bound
16809 && tree_fits_shwi_p (bound)
16810 && (dflt = lower_bound_default ()) != -1
16811 && tree_to_shwi (bound) == dflt)
16812 return;
16814 /* FALLTHRU */
16816 default:
16817 add_scalar_info (subrange_die, bound_attr, bound,
16818 dw_scalar_form_constant
16819 | dw_scalar_form_exprloc
16820 | dw_scalar_form_reference,
16821 context);
16822 return;
16826 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16827 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16828 Note that the block of subscript information for an array type also
16829 includes information about the element type of the given array type. */
16831 static void
16832 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16834 unsigned dimension_number;
16835 tree lower, upper;
16836 dw_die_ref subrange_die;
16838 for (dimension_number = 0;
16839 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16840 type = TREE_TYPE (type), dimension_number++)
16842 tree domain = TYPE_DOMAIN (type);
16844 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16845 break;
16847 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16848 and (in GNU C only) variable bounds. Handle all three forms
16849 here. */
16850 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16852 if (use_upc_dwarf2_extensions && TYPE_HAS_THREADS_FACTOR (type))
16854 add_AT_flag (subrange_die, DW_AT_upc_threads_scaled, 1);
16857 if (domain)
16859 /* We have an array type with specified bounds. */
16860 lower = TYPE_MIN_VALUE (domain);
16861 upper = TYPE_MAX_VALUE (domain);
16863 /* Define the index type. */
16864 if (TREE_TYPE (domain))
16866 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16867 TREE_TYPE field. We can't emit debug info for this
16868 because it is an unnamed integral type. */
16869 if (TREE_CODE (domain) == INTEGER_TYPE
16870 && TYPE_NAME (domain) == NULL_TREE
16871 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16872 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16874 else
16875 add_type_attribute (subrange_die, TREE_TYPE (domain),
16876 TYPE_UNQUALIFIED, type_die);
16879 /* ??? If upper is NULL, the array has unspecified length,
16880 but it does have a lower bound. This happens with Fortran
16881 dimension arr(N:*)
16882 Since the debugger is definitely going to need to know N
16883 to produce useful results, go ahead and output the lower
16884 bound solo, and hope the debugger can cope. */
16886 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16887 if (upper)
16888 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16891 /* Otherwise we have an array type with an unspecified length. The
16892 DWARF-2 spec does not say how to handle this; let's just leave out the
16893 bounds. */
16897 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16899 static void
16900 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16902 dw_die_ref decl_die;
16903 HOST_WIDE_INT size;
16905 switch (TREE_CODE (tree_node))
16907 case ERROR_MARK:
16908 size = 0;
16909 break;
16910 case ENUMERAL_TYPE:
16911 case RECORD_TYPE:
16912 case UNION_TYPE:
16913 case QUAL_UNION_TYPE:
16914 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16915 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16917 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16918 return;
16920 size = int_size_in_bytes (tree_node);
16921 break;
16922 case FIELD_DECL:
16923 /* For a data member of a struct or union, the DW_AT_byte_size is
16924 generally given as the number of bytes normally allocated for an
16925 object of the *declared* type of the member itself. This is true
16926 even for bit-fields. */
16927 size = int_size_in_bytes (field_type (tree_node));
16928 break;
16929 default:
16930 gcc_unreachable ();
16933 /* Note that `size' might be -1 when we get to this point. If it is, that
16934 indicates that the byte size of the entity in question is variable. We
16935 have no good way of expressing this fact in Dwarf at the present time,
16936 when location description was not used by the caller code instead. */
16937 if (size >= 0)
16938 add_AT_unsigned (die, DW_AT_byte_size, size);
16941 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16942 which specifies the distance in bits from the highest order bit of the
16943 "containing object" for the bit-field to the highest order bit of the
16944 bit-field itself.
16946 For any given bit-field, the "containing object" is a hypothetical object
16947 (of some integral or enum type) within which the given bit-field lives. The
16948 type of this hypothetical "containing object" is always the same as the
16949 declared type of the individual bit-field itself. The determination of the
16950 exact location of the "containing object" for a bit-field is rather
16951 complicated. It's handled by the `field_byte_offset' function (above).
16953 Note that it is the size (in bytes) of the hypothetical "containing object"
16954 which will be given in the DW_AT_byte_size attribute for this bit-field.
16955 (See `byte_size_attribute' above). */
16957 static inline void
16958 add_bit_offset_attribute (dw_die_ref die, tree decl)
16960 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16961 tree type = DECL_BIT_FIELD_TYPE (decl);
16962 HOST_WIDE_INT bitpos_int;
16963 HOST_WIDE_INT highest_order_object_bit_offset;
16964 HOST_WIDE_INT highest_order_field_bit_offset;
16965 HOST_WIDE_INT bit_offset;
16967 /* Must be a field and a bit field. */
16968 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16970 /* We can't yet handle bit-fields whose offsets are variable, so if we
16971 encounter such things, just return without generating any attribute
16972 whatsoever. Likewise for variable or too large size. */
16973 if (! tree_fits_shwi_p (bit_position (decl))
16974 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16975 return;
16977 bitpos_int = int_bit_position (decl);
16979 /* Note that the bit offset is always the distance (in bits) from the
16980 highest-order bit of the "containing object" to the highest-order bit of
16981 the bit-field itself. Since the "high-order end" of any object or field
16982 is different on big-endian and little-endian machines, the computation
16983 below must take account of these differences. */
16984 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16985 highest_order_field_bit_offset = bitpos_int;
16987 if (! BYTES_BIG_ENDIAN)
16989 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16990 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16993 bit_offset
16994 = (! BYTES_BIG_ENDIAN
16995 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16996 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16998 if (bit_offset < 0)
16999 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17000 else
17001 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17004 /* For a FIELD_DECL node which represents a bit field, output an attribute
17005 which specifies the length in bits of the given field. */
17007 static inline void
17008 add_bit_size_attribute (dw_die_ref die, tree decl)
17010 /* Must be a field and a bit field. */
17011 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17012 && DECL_BIT_FIELD_TYPE (decl));
17014 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17015 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17018 /* If the compiled language is ANSI C, then add a 'prototyped'
17019 attribute, if arg types are given for the parameters of a function. */
17021 static inline void
17022 add_prototyped_attribute (dw_die_ref die, tree func_type)
17024 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17026 case DW_LANG_C:
17027 case DW_LANG_C89:
17028 case DW_LANG_C99:
17029 case DW_LANG_C11:
17030 case DW_LANG_ObjC:
17031 if (prototype_p (func_type))
17032 add_AT_flag (die, DW_AT_prototyped, 1);
17033 break;
17034 default:
17035 break;
17039 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17040 by looking in either the type declaration or object declaration
17041 equate table. */
17043 static inline dw_die_ref
17044 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17046 dw_die_ref origin_die = NULL;
17048 if (TREE_CODE (origin) != FUNCTION_DECL)
17050 /* We may have gotten separated from the block for the inlined
17051 function, if we're in an exception handler or some such; make
17052 sure that the abstract function has been written out.
17054 Doing this for nested functions is wrong, however; functions are
17055 distinct units, and our context might not even be inline. */
17056 tree fn = origin;
17058 if (TYPE_P (fn))
17059 fn = TYPE_STUB_DECL (fn);
17061 fn = decl_function_context (fn);
17062 if (fn)
17063 dwarf2out_abstract_function (fn);
17066 if (DECL_P (origin))
17067 origin_die = lookup_decl_die (origin);
17068 else if (TYPE_P (origin))
17069 origin_die = lookup_type_die (origin);
17071 /* XXX: Functions that are never lowered don't always have correct block
17072 trees (in the case of java, they simply have no block tree, in some other
17073 languages). For these functions, there is nothing we can really do to
17074 output correct debug info for inlined functions in all cases. Rather
17075 than die, we'll just produce deficient debug info now, in that we will
17076 have variables without a proper abstract origin. In the future, when all
17077 functions are lowered, we should re-add a gcc_assert (origin_die)
17078 here. */
17080 if (origin_die)
17081 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17082 return origin_die;
17085 /* We do not currently support the pure_virtual attribute. */
17087 static inline void
17088 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17090 if (DECL_VINDEX (func_decl))
17092 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17094 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17095 add_AT_loc (die, DW_AT_vtable_elem_location,
17096 new_loc_descr (DW_OP_constu,
17097 tree_to_shwi (DECL_VINDEX (func_decl)),
17098 0));
17100 /* GNU extension: Record what type this method came from originally. */
17101 if (debug_info_level > DINFO_LEVEL_TERSE
17102 && DECL_CONTEXT (func_decl))
17103 add_AT_die_ref (die, DW_AT_containing_type,
17104 lookup_type_die (DECL_CONTEXT (func_decl)));
17108 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17109 given decl. This used to be a vendor extension until after DWARF 4
17110 standardized it. */
17112 static void
17113 add_linkage_attr (dw_die_ref die, tree decl)
17115 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17117 /* Mimic what assemble_name_raw does with a leading '*'. */
17118 if (name[0] == '*')
17119 name = &name[1];
17121 if (dwarf_version >= 4)
17122 add_AT_string (die, DW_AT_linkage_name, name);
17123 else
17124 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17127 /* Add source coordinate attributes for the given decl. */
17129 static void
17130 add_src_coords_attributes (dw_die_ref die, tree decl)
17132 expanded_location s;
17134 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17135 return;
17136 s = expand_location (DECL_SOURCE_LOCATION (decl));
17137 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17138 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17141 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17143 static void
17144 add_linkage_name (dw_die_ref die, tree decl)
17146 if (debug_info_level > DINFO_LEVEL_NONE
17147 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17148 && TREE_PUBLIC (decl)
17149 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17150 && die->die_tag != DW_TAG_member)
17152 /* Defer until we have an assembler name set. */
17153 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17155 limbo_die_node *asm_name;
17157 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17158 asm_name->die = die;
17159 asm_name->created_for = decl;
17160 asm_name->next = deferred_asm_name;
17161 deferred_asm_name = asm_name;
17163 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17164 add_linkage_attr (die, decl);
17168 /* Add a DW_AT_name attribute and source coordinate attribute for the
17169 given decl, but only if it actually has a name. */
17171 static void
17172 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17174 tree decl_name;
17176 decl_name = DECL_NAME (decl);
17177 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17179 const char *name = dwarf2_name (decl, 0);
17180 if (name)
17181 add_name_attribute (die, name);
17182 if (! DECL_ARTIFICIAL (decl))
17183 add_src_coords_attributes (die, decl);
17185 add_linkage_name (die, decl);
17188 #ifdef VMS_DEBUGGING_INFO
17189 /* Get the function's name, as described by its RTL. This may be different
17190 from the DECL_NAME name used in the source file. */
17191 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17193 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17194 XEXP (DECL_RTL (decl), 0), false);
17195 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17197 #endif /* VMS_DEBUGGING_INFO */
17200 #ifdef VMS_DEBUGGING_INFO
17201 /* Output the debug main pointer die for VMS */
17203 void
17204 dwarf2out_vms_debug_main_pointer (void)
17206 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17207 dw_die_ref die;
17209 /* Allocate the VMS debug main subprogram die. */
17210 die = ggc_cleared_alloc<die_node> ();
17211 die->die_tag = DW_TAG_subprogram;
17212 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17213 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17214 current_function_funcdef_no);
17215 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17217 /* Make it the first child of comp_unit_die (). */
17218 die->die_parent = comp_unit_die ();
17219 if (comp_unit_die ()->die_child)
17221 die->die_sib = comp_unit_die ()->die_child->die_sib;
17222 comp_unit_die ()->die_child->die_sib = die;
17224 else
17226 die->die_sib = die;
17227 comp_unit_die ()->die_child = die;
17230 #endif /* VMS_DEBUGGING_INFO */
17232 /* Push a new declaration scope. */
17234 static void
17235 push_decl_scope (tree scope)
17237 vec_safe_push (decl_scope_table, scope);
17240 /* Pop a declaration scope. */
17242 static inline void
17243 pop_decl_scope (void)
17245 decl_scope_table->pop ();
17248 /* walk_tree helper function for uses_local_type, below. */
17250 static tree
17251 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17253 if (!TYPE_P (*tp))
17254 *walk_subtrees = 0;
17255 else
17257 tree name = TYPE_NAME (*tp);
17258 if (name && DECL_P (name) && decl_function_context (name))
17259 return *tp;
17261 return NULL_TREE;
17264 /* If TYPE involves a function-local type (including a local typedef to a
17265 non-local type), returns that type; otherwise returns NULL_TREE. */
17267 static tree
17268 uses_local_type (tree type)
17270 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17271 return used;
17274 /* Return the DIE for the scope that immediately contains this type.
17275 Non-named types that do not involve a function-local type get global
17276 scope. Named types nested in namespaces or other types get their
17277 containing scope. All other types (i.e. function-local named types) get
17278 the current active scope. */
17280 static dw_die_ref
17281 scope_die_for (tree t, dw_die_ref context_die)
17283 dw_die_ref scope_die = NULL;
17284 tree containing_scope;
17286 /* Non-types always go in the current scope. */
17287 gcc_assert (TYPE_P (t));
17289 /* Use the scope of the typedef, rather than the scope of the type
17290 it refers to. */
17291 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17292 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17293 else
17294 containing_scope = TYPE_CONTEXT (t);
17296 /* Use the containing namespace if there is one. */
17297 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17299 if (context_die == lookup_decl_die (containing_scope))
17300 /* OK */;
17301 else if (debug_info_level > DINFO_LEVEL_TERSE)
17302 context_die = get_context_die (containing_scope);
17303 else
17304 containing_scope = NULL_TREE;
17307 /* Ignore function type "scopes" from the C frontend. They mean that
17308 a tagged type is local to a parmlist of a function declarator, but
17309 that isn't useful to DWARF. */
17310 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17311 containing_scope = NULL_TREE;
17313 if (SCOPE_FILE_SCOPE_P (containing_scope))
17315 /* If T uses a local type keep it local as well, to avoid references
17316 to function-local DIEs from outside the function. */
17317 if (current_function_decl && uses_local_type (t))
17318 scope_die = context_die;
17319 else
17320 scope_die = comp_unit_die ();
17322 else if (TYPE_P (containing_scope))
17324 /* For types, we can just look up the appropriate DIE. */
17325 if (debug_info_level > DINFO_LEVEL_TERSE)
17326 scope_die = get_context_die (containing_scope);
17327 else
17329 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17330 if (scope_die == NULL)
17331 scope_die = comp_unit_die ();
17334 else
17335 scope_die = context_die;
17337 return scope_die;
17340 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17342 static inline int
17343 local_scope_p (dw_die_ref context_die)
17345 for (; context_die; context_die = context_die->die_parent)
17346 if (context_die->die_tag == DW_TAG_inlined_subroutine
17347 || context_die->die_tag == DW_TAG_subprogram)
17348 return 1;
17350 return 0;
17353 /* Returns nonzero if CONTEXT_DIE is a class. */
17355 static inline int
17356 class_scope_p (dw_die_ref context_die)
17358 return (context_die
17359 && (context_die->die_tag == DW_TAG_structure_type
17360 || context_die->die_tag == DW_TAG_class_type
17361 || context_die->die_tag == DW_TAG_interface_type
17362 || context_die->die_tag == DW_TAG_union_type));
17365 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17366 whether or not to treat a DIE in this context as a declaration. */
17368 static inline int
17369 class_or_namespace_scope_p (dw_die_ref context_die)
17371 return (class_scope_p (context_die)
17372 || (context_die && context_die->die_tag == DW_TAG_namespace));
17375 /* Many forms of DIEs require a "type description" attribute. This
17376 routine locates the proper "type descriptor" die for the type given
17377 by 'type' plus any additional qualifiers given by 'cv_quals', and
17378 adds a DW_AT_type attribute below the given die. */
17380 static void
17381 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17382 dw_die_ref context_die)
17384 enum tree_code code = TREE_CODE (type);
17385 dw_die_ref type_die = NULL;
17387 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17388 or fixed-point type, use the inner type. This is because we have no
17389 support for unnamed types in base_type_die. This can happen if this is
17390 an Ada subrange type. Correct solution is emit a subrange type die. */
17391 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17392 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17393 type = TREE_TYPE (type), code = TREE_CODE (type);
17395 if (code == ERROR_MARK
17396 /* Handle a special case. For functions whose return type is void, we
17397 generate *no* type attribute. (Note that no object may have type
17398 `void', so this only applies to function return types). */
17399 || code == VOID_TYPE)
17400 return;
17402 type_die = modified_type_die (type,
17403 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17404 context_die);
17406 if (type_die != NULL)
17407 add_AT_die_ref (object_die, DW_AT_type, type_die);
17410 /* Given an object die, add the calling convention attribute for the
17411 function call type. */
17412 static void
17413 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17415 enum dwarf_calling_convention value = DW_CC_normal;
17417 value = ((enum dwarf_calling_convention)
17418 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17420 if (is_fortran ()
17421 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17423 /* DWARF 2 doesn't provide a way to identify a program's source-level
17424 entry point. DW_AT_calling_convention attributes are only meant
17425 to describe functions' calling conventions. However, lacking a
17426 better way to signal the Fortran main program, we used this for
17427 a long time, following existing custom. Now, DWARF 4 has
17428 DW_AT_main_subprogram, which we add below, but some tools still
17429 rely on the old way, which we thus keep. */
17430 value = DW_CC_program;
17432 if (dwarf_version >= 4 || !dwarf_strict)
17433 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17436 /* Only add the attribute if the backend requests it, and
17437 is not DW_CC_normal. */
17438 if (value && (value != DW_CC_normal))
17439 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17442 /* Given a tree pointer to a struct, class, union, or enum type node, return
17443 a pointer to the (string) tag name for the given type, or zero if the type
17444 was declared without a tag. */
17446 static const char *
17447 type_tag (const_tree type)
17449 const char *name = 0;
17451 if (TYPE_NAME (type) != 0)
17453 tree t = 0;
17455 /* Find the IDENTIFIER_NODE for the type name. */
17456 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17457 && !TYPE_NAMELESS (type))
17458 t = TYPE_NAME (type);
17460 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17461 a TYPE_DECL node, regardless of whether or not a `typedef' was
17462 involved. */
17463 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17464 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17466 /* We want to be extra verbose. Don't call dwarf_name if
17467 DECL_NAME isn't set. The default hook for decl_printable_name
17468 doesn't like that, and in this context it's correct to return
17469 0, instead of "<anonymous>" or the like. */
17470 if (DECL_NAME (TYPE_NAME (type))
17471 && !DECL_NAMELESS (TYPE_NAME (type)))
17472 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17475 /* Now get the name as a string, or invent one. */
17476 if (!name && t != 0)
17477 name = IDENTIFIER_POINTER (t);
17480 return (name == 0 || *name == '\0') ? 0 : name;
17483 /* Return the type associated with a data member, make a special check
17484 for bit field types. */
17486 static inline tree
17487 member_declared_type (const_tree member)
17489 return (DECL_BIT_FIELD_TYPE (member)
17490 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17493 /* Get the decl's label, as described by its RTL. This may be different
17494 from the DECL_NAME name used in the source file. */
17496 #if 0
17497 static const char *
17498 decl_start_label (tree decl)
17500 rtx x;
17501 const char *fnname;
17503 x = DECL_RTL (decl);
17504 gcc_assert (MEM_P (x));
17506 x = XEXP (x, 0);
17507 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17509 fnname = XSTR (x, 0);
17510 return fnname;
17512 #endif
17514 /* These routines generate the internal representation of the DIE's for
17515 the compilation unit. Debugging information is collected by walking
17516 the declaration trees passed in from dwarf2out_decl(). */
17518 static void
17519 gen_array_type_die (tree type, dw_die_ref context_die)
17521 dw_die_ref scope_die = scope_die_for (type, context_die);
17522 dw_die_ref array_die;
17524 /* GNU compilers represent multidimensional array types as sequences of one
17525 dimensional array types whose element types are themselves array types.
17526 We sometimes squish that down to a single array_type DIE with multiple
17527 subscripts in the Dwarf debugging info. The draft Dwarf specification
17528 say that we are allowed to do this kind of compression in C, because
17529 there is no difference between an array of arrays and a multidimensional
17530 array. We don't do this for Ada to remain as close as possible to the
17531 actual representation, which is especially important against the language
17532 flexibilty wrt arrays of variable size. */
17534 bool collapse_nested_arrays = !is_ada ();
17535 tree element_type;
17537 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17538 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17539 if (TYPE_STRING_FLAG (type)
17540 && TREE_CODE (type) == ARRAY_TYPE
17541 && is_fortran ()
17542 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17544 HOST_WIDE_INT size;
17546 array_die = new_die (DW_TAG_string_type, scope_die, type);
17547 add_name_attribute (array_die, type_tag (type));
17548 equate_type_number_to_die (type, array_die);
17549 size = int_size_in_bytes (type);
17550 if (size >= 0)
17551 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17552 else if (TYPE_DOMAIN (type) != NULL_TREE
17553 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17554 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17556 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17557 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17559 size = int_size_in_bytes (TREE_TYPE (szdecl));
17560 if (loc && size > 0)
17562 add_AT_location_description (array_die, DW_AT_string_length, loc);
17563 if (size != DWARF2_ADDR_SIZE)
17564 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17567 return;
17570 array_die = new_die (DW_TAG_array_type, scope_die, type);
17571 add_name_attribute (array_die, type_tag (type));
17572 equate_type_number_to_die (type, array_die);
17574 if (TREE_CODE (type) == VECTOR_TYPE)
17575 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17577 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17578 if (is_fortran ()
17579 && TREE_CODE (type) == ARRAY_TYPE
17580 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17581 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17582 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17584 #if 0
17585 /* We default the array ordering. SDB will probably do
17586 the right things even if DW_AT_ordering is not present. It's not even
17587 an issue until we start to get into multidimensional arrays anyway. If
17588 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17589 then we'll have to put the DW_AT_ordering attribute back in. (But if
17590 and when we find out that we need to put these in, we will only do so
17591 for multidimensional arrays. */
17592 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17593 #endif
17595 if (TREE_CODE (type) == VECTOR_TYPE)
17597 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17598 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17599 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17600 add_bound_info (subrange_die, DW_AT_upper_bound,
17601 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17603 else
17604 add_subscript_info (array_die, type, collapse_nested_arrays);
17606 /* Add representation of the type of the elements of this array type and
17607 emit the corresponding DIE if we haven't done it already. */
17608 element_type = TREE_TYPE (type);
17609 if (collapse_nested_arrays)
17610 while (TREE_CODE (element_type) == ARRAY_TYPE)
17612 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17613 break;
17614 element_type = TREE_TYPE (element_type);
17617 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17619 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17620 if (TYPE_ARTIFICIAL (type))
17621 add_AT_flag (array_die, DW_AT_artificial, 1);
17623 if (get_AT (array_die, DW_AT_name))
17624 add_pubtype (type, array_die);
17627 /* This routine generates DIE for array with hidden descriptor, details
17628 are filled into *info by a langhook. */
17630 static void
17631 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17632 dw_die_ref context_die)
17634 const dw_die_ref scope_die = scope_die_for (type, context_die);
17635 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17636 const struct loc_descr_context context = { type, info->base_decl };
17637 int dim;
17639 add_name_attribute (array_die, type_tag (type));
17640 equate_type_number_to_die (type, array_die);
17642 if (info->ndimensions > 1)
17643 switch (info->ordering)
17645 case array_descr_ordering_row_major:
17646 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17647 break;
17648 case array_descr_ordering_column_major:
17649 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17650 break;
17651 default:
17652 break;
17655 if (dwarf_version >= 3 || !dwarf_strict)
17657 if (info->data_location)
17658 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17659 dw_scalar_form_exprloc, &context);
17660 if (info->associated)
17661 add_scalar_info (array_die, DW_AT_associated, info->associated,
17662 dw_scalar_form_constant
17663 | dw_scalar_form_exprloc
17664 | dw_scalar_form_reference, &context);
17665 if (info->allocated)
17666 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17667 dw_scalar_form_constant
17668 | dw_scalar_form_exprloc
17669 | dw_scalar_form_reference, &context);
17672 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17674 for (dim = 0; dim < info->ndimensions; dim++)
17676 dw_die_ref subrange_die
17677 = new_die (DW_TAG_subrange_type, array_die, NULL);
17679 if (info->dimen[dim].bounds_type)
17680 add_type_attribute (subrange_die,
17681 info->dimen[dim].bounds_type, 0,
17682 context_die);
17683 if (info->dimen[dim].lower_bound)
17684 add_bound_info (subrange_die, DW_AT_lower_bound,
17685 info->dimen[dim].lower_bound, &context);
17686 if (info->dimen[dim].upper_bound)
17687 add_bound_info (subrange_die, DW_AT_upper_bound,
17688 info->dimen[dim].upper_bound, &context);
17689 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17690 add_scalar_info (subrange_die, DW_AT_byte_stride,
17691 info->dimen[dim].stride,
17692 dw_scalar_form_constant
17693 | dw_scalar_form_exprloc
17694 | dw_scalar_form_reference,
17695 &context);
17698 gen_type_die (info->element_type, context_die);
17699 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17700 context_die);
17702 if (get_AT (array_die, DW_AT_name))
17703 add_pubtype (type, array_die);
17706 #if 0
17707 static void
17708 gen_entry_point_die (tree decl, dw_die_ref context_die)
17710 tree origin = decl_ultimate_origin (decl);
17711 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17713 if (origin != NULL)
17714 add_abstract_origin_attribute (decl_die, origin);
17715 else
17717 add_name_and_src_coords_attributes (decl_die, decl);
17718 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17719 TYPE_UNQUALIFIED, context_die);
17722 if (DECL_ABSTRACT_P (decl))
17723 equate_decl_number_to_die (decl, decl_die);
17724 else
17725 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17727 #endif
17729 /* Walk through the list of incomplete types again, trying once more to
17730 emit full debugging info for them. */
17732 static void
17733 retry_incomplete_types (void)
17735 int i;
17737 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17738 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17739 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17742 /* Determine what tag to use for a record type. */
17744 static enum dwarf_tag
17745 record_type_tag (tree type)
17747 if (! lang_hooks.types.classify_record)
17748 return DW_TAG_structure_type;
17750 switch (lang_hooks.types.classify_record (type))
17752 case RECORD_IS_STRUCT:
17753 return DW_TAG_structure_type;
17755 case RECORD_IS_CLASS:
17756 return DW_TAG_class_type;
17758 case RECORD_IS_INTERFACE:
17759 if (dwarf_version >= 3 || !dwarf_strict)
17760 return DW_TAG_interface_type;
17761 return DW_TAG_structure_type;
17763 default:
17764 gcc_unreachable ();
17768 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17769 include all of the information about the enumeration values also. Each
17770 enumerated type name/value is listed as a child of the enumerated type
17771 DIE. */
17773 static dw_die_ref
17774 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17776 dw_die_ref type_die = lookup_type_die (type);
17778 if (type_die == NULL)
17780 type_die = new_die (DW_TAG_enumeration_type,
17781 scope_die_for (type, context_die), type);
17782 equate_type_number_to_die (type, type_die);
17783 add_name_attribute (type_die, type_tag (type));
17784 if (dwarf_version >= 4 || !dwarf_strict)
17786 if (ENUM_IS_SCOPED (type))
17787 add_AT_flag (type_die, DW_AT_enum_class, 1);
17788 if (ENUM_IS_OPAQUE (type))
17789 add_AT_flag (type_die, DW_AT_declaration, 1);
17792 else if (! TYPE_SIZE (type))
17793 return type_die;
17794 else
17795 remove_AT (type_die, DW_AT_declaration);
17797 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17798 given enum type is incomplete, do not generate the DW_AT_byte_size
17799 attribute or the DW_AT_element_list attribute. */
17800 if (TYPE_SIZE (type))
17802 tree link;
17804 TREE_ASM_WRITTEN (type) = 1;
17805 add_byte_size_attribute (type_die, type);
17806 if (dwarf_version >= 3 || !dwarf_strict)
17808 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17809 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17810 context_die);
17812 if (TYPE_STUB_DECL (type) != NULL_TREE)
17814 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17815 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17818 /* If the first reference to this type was as the return type of an
17819 inline function, then it may not have a parent. Fix this now. */
17820 if (type_die->die_parent == NULL)
17821 add_child_die (scope_die_for (type, context_die), type_die);
17823 for (link = TYPE_VALUES (type);
17824 link != NULL; link = TREE_CHAIN (link))
17826 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17827 tree value = TREE_VALUE (link);
17829 add_name_attribute (enum_die,
17830 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17832 if (TREE_CODE (value) == CONST_DECL)
17833 value = DECL_INITIAL (value);
17835 if (simple_type_size_in_bits (TREE_TYPE (value))
17836 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17838 /* For constant forms created by add_AT_unsigned DWARF
17839 consumers (GDB, elfutils, etc.) always zero extend
17840 the value. Only when the actual value is negative
17841 do we need to use add_AT_int to generate a constant
17842 form that can represent negative values. */
17843 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17844 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17845 add_AT_unsigned (enum_die, DW_AT_const_value,
17846 (unsigned HOST_WIDE_INT) val);
17847 else
17848 add_AT_int (enum_die, DW_AT_const_value, val);
17850 else
17851 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17852 that here. TODO: This should be re-worked to use correct
17853 signed/unsigned double tags for all cases. */
17854 add_AT_wide (enum_die, DW_AT_const_value, value);
17857 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17858 if (TYPE_ARTIFICIAL (type))
17859 add_AT_flag (type_die, DW_AT_artificial, 1);
17861 else
17862 add_AT_flag (type_die, DW_AT_declaration, 1);
17864 add_pubtype (type, type_die);
17866 return type_die;
17869 /* Generate a DIE to represent either a real live formal parameter decl or to
17870 represent just the type of some formal parameter position in some function
17871 type.
17873 Note that this routine is a bit unusual because its argument may be a
17874 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17875 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17876 node. If it's the former then this function is being called to output a
17877 DIE to represent a formal parameter object (or some inlining thereof). If
17878 it's the latter, then this function is only being called to output a
17879 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17880 argument type of some subprogram type.
17881 If EMIT_NAME_P is true, name and source coordinate attributes
17882 are emitted. */
17884 static dw_die_ref
17885 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17886 dw_die_ref context_die)
17888 tree node_or_origin = node ? node : origin;
17889 tree ultimate_origin;
17890 dw_die_ref parm_die
17891 = new_die (DW_TAG_formal_parameter, context_die, node);
17893 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17895 case tcc_declaration:
17896 ultimate_origin = decl_ultimate_origin (node_or_origin);
17897 if (node || ultimate_origin)
17898 origin = ultimate_origin;
17899 if (origin != NULL)
17900 add_abstract_origin_attribute (parm_die, origin);
17901 else if (emit_name_p)
17902 add_name_and_src_coords_attributes (parm_die, node);
17903 if (origin == NULL
17904 || (! DECL_ABSTRACT_P (node_or_origin)
17905 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17906 decl_function_context
17907 (node_or_origin))))
17909 tree type = TREE_TYPE (node_or_origin);
17910 if (decl_by_reference_p (node_or_origin))
17911 add_type_attribute (parm_die, TREE_TYPE (type),
17912 TYPE_UNQUALIFIED, context_die);
17913 else
17914 add_type_attribute (parm_die, type,
17915 decl_quals (node_or_origin),
17916 context_die);
17918 if (origin == NULL && DECL_ARTIFICIAL (node))
17919 add_AT_flag (parm_die, DW_AT_artificial, 1);
17921 if (node && node != origin)
17922 equate_decl_number_to_die (node, parm_die);
17923 if (! DECL_ABSTRACT_P (node_or_origin))
17924 add_location_or_const_value_attribute (parm_die, node_or_origin,
17925 node == NULL, DW_AT_location);
17927 break;
17929 case tcc_type:
17930 /* We were called with some kind of a ..._TYPE node. */
17931 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17932 context_die);
17933 break;
17935 default:
17936 gcc_unreachable ();
17939 return parm_die;
17942 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17943 children DW_TAG_formal_parameter DIEs representing the arguments of the
17944 parameter pack.
17946 PARM_PACK must be a function parameter pack.
17947 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17948 must point to the subsequent arguments of the function PACK_ARG belongs to.
17949 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17950 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17951 following the last one for which a DIE was generated. */
17953 static dw_die_ref
17954 gen_formal_parameter_pack_die (tree parm_pack,
17955 tree pack_arg,
17956 dw_die_ref subr_die,
17957 tree *next_arg)
17959 tree arg;
17960 dw_die_ref parm_pack_die;
17962 gcc_assert (parm_pack
17963 && lang_hooks.function_parameter_pack_p (parm_pack)
17964 && subr_die);
17966 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17967 add_src_coords_attributes (parm_pack_die, parm_pack);
17969 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17971 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17972 parm_pack))
17973 break;
17974 gen_formal_parameter_die (arg, NULL,
17975 false /* Don't emit name attribute. */,
17976 parm_pack_die);
17978 if (next_arg)
17979 *next_arg = arg;
17980 return parm_pack_die;
17983 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17984 at the end of an (ANSI prototyped) formal parameters list. */
17986 static void
17987 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17989 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17992 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17993 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17994 parameters as specified in some function type specification (except for
17995 those which appear as part of a function *definition*). */
17997 static void
17998 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18000 tree link;
18001 tree formal_type = NULL;
18002 tree first_parm_type;
18003 tree arg;
18005 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18007 arg = DECL_ARGUMENTS (function_or_method_type);
18008 function_or_method_type = TREE_TYPE (function_or_method_type);
18010 else
18011 arg = NULL_TREE;
18013 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18015 /* Make our first pass over the list of formal parameter types and output a
18016 DW_TAG_formal_parameter DIE for each one. */
18017 for (link = first_parm_type; link; )
18019 dw_die_ref parm_die;
18021 formal_type = TREE_VALUE (link);
18022 if (formal_type == void_type_node)
18023 break;
18025 /* Output a (nameless) DIE to represent the formal parameter itself. */
18026 if (!POINTER_BOUNDS_TYPE_P (formal_type))
18028 parm_die = gen_formal_parameter_die (formal_type, NULL,
18029 true /* Emit name attribute. */,
18030 context_die);
18031 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18032 && link == first_parm_type)
18034 add_AT_flag (parm_die, DW_AT_artificial, 1);
18035 if (dwarf_version >= 3 || !dwarf_strict)
18036 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18038 else if (arg && DECL_ARTIFICIAL (arg))
18039 add_AT_flag (parm_die, DW_AT_artificial, 1);
18042 link = TREE_CHAIN (link);
18043 if (arg)
18044 arg = DECL_CHAIN (arg);
18047 /* If this function type has an ellipsis, add a
18048 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18049 if (formal_type != void_type_node)
18050 gen_unspecified_parameters_die (function_or_method_type, context_die);
18052 /* Make our second (and final) pass over the list of formal parameter types
18053 and output DIEs to represent those types (as necessary). */
18054 for (link = TYPE_ARG_TYPES (function_or_method_type);
18055 link && TREE_VALUE (link);
18056 link = TREE_CHAIN (link))
18057 gen_type_die (TREE_VALUE (link), context_die);
18060 /* We want to generate the DIE for TYPE so that we can generate the
18061 die for MEMBER, which has been defined; we will need to refer back
18062 to the member declaration nested within TYPE. If we're trying to
18063 generate minimal debug info for TYPE, processing TYPE won't do the
18064 trick; we need to attach the member declaration by hand. */
18066 static void
18067 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18069 gen_type_die (type, context_die);
18071 /* If we're trying to avoid duplicate debug info, we may not have
18072 emitted the member decl for this function. Emit it now. */
18073 if (TYPE_STUB_DECL (type)
18074 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18075 && ! lookup_decl_die (member))
18077 dw_die_ref type_die;
18078 gcc_assert (!decl_ultimate_origin (member));
18080 push_decl_scope (type);
18081 type_die = lookup_type_die_strip_naming_typedef (type);
18082 if (TREE_CODE (member) == FUNCTION_DECL)
18083 gen_subprogram_die (member, type_die);
18084 else if (TREE_CODE (member) == FIELD_DECL)
18086 /* Ignore the nameless fields that are used to skip bits but handle
18087 C++ anonymous unions and structs. */
18088 if (DECL_NAME (member) != NULL_TREE
18089 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18090 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18092 gen_type_die (member_declared_type (member), type_die);
18093 gen_field_die (member, type_die);
18096 else
18097 gen_variable_die (member, NULL_TREE, type_die);
18099 pop_decl_scope ();
18103 /* Forward declare these functions, because they are mutually recursive
18104 with their set_block_* pairing functions. */
18105 static void set_decl_origin_self (tree);
18106 static void set_decl_abstract_flags (tree, int);
18108 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18109 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18110 that it points to the node itself, thus indicating that the node is its
18111 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18112 the given node is NULL, recursively descend the decl/block tree which
18113 it is the root of, and for each other ..._DECL or BLOCK node contained
18114 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18115 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18116 values to point to themselves. */
18118 static void
18119 set_block_origin_self (tree stmt)
18121 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18123 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18126 tree local_decl;
18128 for (local_decl = BLOCK_VARS (stmt);
18129 local_decl != NULL_TREE;
18130 local_decl = DECL_CHAIN (local_decl))
18131 /* Do not recurse on nested functions since the inlining status
18132 of parent and child can be different as per the DWARF spec. */
18133 if (TREE_CODE (local_decl) != FUNCTION_DECL
18134 && !DECL_EXTERNAL (local_decl))
18135 set_decl_origin_self (local_decl);
18139 tree subblock;
18141 for (subblock = BLOCK_SUBBLOCKS (stmt);
18142 subblock != NULL_TREE;
18143 subblock = BLOCK_CHAIN (subblock))
18144 set_block_origin_self (subblock); /* Recurse. */
18149 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18150 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18151 node to so that it points to the node itself, thus indicating that the
18152 node represents its own (abstract) origin. Additionally, if the
18153 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18154 the decl/block tree of which the given node is the root of, and for
18155 each other ..._DECL or BLOCK node contained therein whose
18156 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18157 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18158 point to themselves. */
18160 static void
18161 set_decl_origin_self (tree decl)
18163 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18165 DECL_ABSTRACT_ORIGIN (decl) = decl;
18166 if (TREE_CODE (decl) == FUNCTION_DECL)
18168 tree arg;
18170 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18171 DECL_ABSTRACT_ORIGIN (arg) = arg;
18172 if (DECL_INITIAL (decl) != NULL_TREE
18173 && DECL_INITIAL (decl) != error_mark_node)
18174 set_block_origin_self (DECL_INITIAL (decl));
18179 /* Given a pointer to some BLOCK node, and a boolean value to set the
18180 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
18181 the given block, and for all local decls and all local sub-blocks
18182 (recursively) which are contained therein. */
18184 static void
18185 set_block_abstract_flags (tree stmt, int setting)
18187 tree local_decl;
18188 tree subblock;
18189 unsigned int i;
18191 BLOCK_ABSTRACT (stmt) = setting;
18193 for (local_decl = BLOCK_VARS (stmt);
18194 local_decl != NULL_TREE;
18195 local_decl = DECL_CHAIN (local_decl))
18196 if (! DECL_EXTERNAL (local_decl))
18197 set_decl_abstract_flags (local_decl, setting);
18199 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18201 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18202 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18203 || TREE_CODE (local_decl) == PARM_DECL)
18204 set_decl_abstract_flags (local_decl, setting);
18207 for (subblock = BLOCK_SUBBLOCKS (stmt);
18208 subblock != NULL_TREE;
18209 subblock = BLOCK_CHAIN (subblock))
18210 set_block_abstract_flags (subblock, setting);
18213 /* Given a pointer to some ..._DECL node, and a boolean value to set the
18214 "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
18215 given decl, and (in the case where the decl is a FUNCTION_DECL) also
18216 set the abstract flags for all of the parameters, local vars, local
18217 blocks and sub-blocks (recursively) to the same setting. */
18219 static void
18220 set_decl_abstract_flags (tree decl, int setting)
18222 DECL_ABSTRACT_P (decl) = setting;
18223 if (TREE_CODE (decl) == FUNCTION_DECL)
18225 tree arg;
18227 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18228 DECL_ABSTRACT_P (arg) = setting;
18229 if (DECL_INITIAL (decl) != NULL_TREE
18230 && DECL_INITIAL (decl) != error_mark_node)
18231 set_block_abstract_flags (DECL_INITIAL (decl), setting);
18235 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18236 may later generate inlined and/or out-of-line instances of. */
18238 static void
18239 dwarf2out_abstract_function (tree decl)
18241 dw_die_ref old_die;
18242 tree save_fn;
18243 tree context;
18244 int was_abstract;
18245 hash_table<decl_loc_hasher> *old_decl_loc_table;
18246 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18247 int old_call_site_count, old_tail_call_site_count;
18248 struct call_arg_loc_node *old_call_arg_locations;
18250 /* Make sure we have the actual abstract inline, not a clone. */
18251 decl = DECL_ORIGIN (decl);
18253 old_die = lookup_decl_die (decl);
18254 if (old_die && get_AT (old_die, DW_AT_inline))
18255 /* We've already generated the abstract instance. */
18256 return;
18258 /* We can be called while recursively when seeing block defining inlined subroutine
18259 DIE. Be sure to not clobber the outer location table nor use it or we would
18260 get locations in abstract instantces. */
18261 old_decl_loc_table = decl_loc_table;
18262 decl_loc_table = NULL;
18263 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18264 cached_dw_loc_list_table = NULL;
18265 old_call_arg_locations = call_arg_locations;
18266 call_arg_locations = NULL;
18267 old_call_site_count = call_site_count;
18268 call_site_count = -1;
18269 old_tail_call_site_count = tail_call_site_count;
18270 tail_call_site_count = -1;
18272 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18273 we don't get confused by DECL_ABSTRACT_P. */
18274 if (debug_info_level > DINFO_LEVEL_TERSE)
18276 context = decl_class_context (decl);
18277 if (context)
18278 gen_type_die_for_member
18279 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18282 /* Pretend we've just finished compiling this function. */
18283 save_fn = current_function_decl;
18284 current_function_decl = decl;
18286 was_abstract = DECL_ABSTRACT_P (decl);
18287 set_decl_abstract_flags (decl, 1);
18288 dwarf2out_decl (decl);
18289 if (! was_abstract)
18290 set_decl_abstract_flags (decl, 0);
18292 current_function_decl = save_fn;
18293 decl_loc_table = old_decl_loc_table;
18294 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18295 call_arg_locations = old_call_arg_locations;
18296 call_site_count = old_call_site_count;
18297 tail_call_site_count = old_tail_call_site_count;
18300 /* Helper function of premark_used_types() which gets called through
18301 htab_traverse.
18303 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18304 marked as unused by prune_unused_types. */
18306 bool
18307 premark_used_types_helper (tree const &type, void *)
18309 dw_die_ref die;
18311 die = lookup_type_die (type);
18312 if (die != NULL)
18313 die->die_perennial_p = 1;
18314 return true;
18317 /* Helper function of premark_types_used_by_global_vars which gets called
18318 through htab_traverse.
18320 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18321 marked as unused by prune_unused_types. The DIE of the type is marked
18322 only if the global variable using the type will actually be emitted. */
18325 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18326 void *)
18328 struct types_used_by_vars_entry *entry;
18329 dw_die_ref die;
18331 entry = (struct types_used_by_vars_entry *) *slot;
18332 gcc_assert (entry->type != NULL
18333 && entry->var_decl != NULL);
18334 die = lookup_type_die (entry->type);
18335 if (die)
18337 /* Ask cgraph if the global variable really is to be emitted.
18338 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18339 varpool_node *node = varpool_node::get (entry->var_decl);
18340 if (node && node->definition)
18342 die->die_perennial_p = 1;
18343 /* Keep the parent DIEs as well. */
18344 while ((die = die->die_parent) && die->die_perennial_p == 0)
18345 die->die_perennial_p = 1;
18348 return 1;
18351 /* Mark all members of used_types_hash as perennial. */
18353 static void
18354 premark_used_types (struct function *fun)
18356 if (fun && fun->used_types_hash)
18357 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18360 /* Mark all members of types_used_by_vars_entry as perennial. */
18362 static void
18363 premark_types_used_by_global_vars (void)
18365 if (types_used_by_vars_hash)
18366 types_used_by_vars_hash
18367 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18370 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18371 for CA_LOC call arg loc node. */
18373 static dw_die_ref
18374 gen_call_site_die (tree decl, dw_die_ref subr_die,
18375 struct call_arg_loc_node *ca_loc)
18377 dw_die_ref stmt_die = NULL, die;
18378 tree block = ca_loc->block;
18380 while (block
18381 && block != DECL_INITIAL (decl)
18382 && TREE_CODE (block) == BLOCK)
18384 if (block_map.length () > BLOCK_NUMBER (block))
18385 stmt_die = block_map[BLOCK_NUMBER (block)];
18386 if (stmt_die)
18387 break;
18388 block = BLOCK_SUPERCONTEXT (block);
18390 if (stmt_die == NULL)
18391 stmt_die = subr_die;
18392 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18393 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18394 if (ca_loc->tail_call_p)
18395 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18396 if (ca_loc->symbol_ref)
18398 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18399 if (tdie)
18400 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18401 else
18402 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18404 return die;
18407 /* Generate a DIE to represent a declared function (either file-scope or
18408 block-local). */
18410 static void
18411 gen_subprogram_die (tree decl, dw_die_ref context_die)
18413 tree origin = decl_ultimate_origin (decl);
18414 dw_die_ref subr_die;
18415 tree outer_scope;
18416 dw_die_ref old_die = lookup_decl_die (decl);
18417 int declaration = (current_function_decl != decl
18418 || class_or_namespace_scope_p (context_die));
18420 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18422 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18423 started to generate the abstract instance of an inline, decided to output
18424 its containing class, and proceeded to emit the declaration of the inline
18425 from the member list for the class. If so, DECLARATION takes priority;
18426 we'll get back to the abstract instance when done with the class. */
18428 /* The class-scope declaration DIE must be the primary DIE. */
18429 if (origin && declaration && class_or_namespace_scope_p (context_die))
18431 origin = NULL;
18432 gcc_assert (!old_die);
18435 /* Now that the C++ front end lazily declares artificial member fns, we
18436 might need to retrofit the declaration into its class. */
18437 if (!declaration && !origin && !old_die
18438 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18439 && !class_or_namespace_scope_p (context_die)
18440 && debug_info_level > DINFO_LEVEL_TERSE)
18441 old_die = force_decl_die (decl);
18443 if (origin != NULL)
18445 gcc_assert (!declaration || local_scope_p (context_die));
18447 /* Fixup die_parent for the abstract instance of a nested
18448 inline function. */
18449 if (old_die && old_die->die_parent == NULL)
18450 add_child_die (context_die, old_die);
18452 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18453 add_abstract_origin_attribute (subr_die, origin);
18454 /* This is where the actual code for a cloned function is.
18455 Let's emit linkage name attribute for it. This helps
18456 debuggers to e.g, set breakpoints into
18457 constructors/destructors when the user asks "break
18458 K::K". */
18459 add_linkage_name (subr_die, decl);
18461 else if (old_die)
18463 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18464 struct dwarf_file_data * file_index = lookup_filename (s.file);
18466 if (!get_AT_flag (old_die, DW_AT_declaration)
18467 /* We can have a normal definition following an inline one in the
18468 case of redefinition of GNU C extern inlines.
18469 It seems reasonable to use AT_specification in this case. */
18470 && !get_AT (old_die, DW_AT_inline))
18472 /* Detect and ignore this case, where we are trying to output
18473 something we have already output. */
18474 return;
18477 /* If the definition comes from the same place as the declaration,
18478 maybe use the old DIE. We always want the DIE for this function
18479 that has the *_pc attributes to be under comp_unit_die so the
18480 debugger can find it. We also need to do this for abstract
18481 instances of inlines, since the spec requires the out-of-line copy
18482 to have the same parent. For local class methods, this doesn't
18483 apply; we just use the old DIE. */
18484 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18485 && (DECL_ARTIFICIAL (decl)
18486 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18487 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18488 == (unsigned) s.line))))
18490 subr_die = old_die;
18492 /* Clear out the declaration attribute and the formal parameters.
18493 Do not remove all children, because it is possible that this
18494 declaration die was forced using force_decl_die(). In such
18495 cases die that forced declaration die (e.g. TAG_imported_module)
18496 is one of the children that we do not want to remove. */
18497 remove_AT (subr_die, DW_AT_declaration);
18498 remove_AT (subr_die, DW_AT_object_pointer);
18499 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18501 else
18503 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18504 add_AT_specification (subr_die, old_die);
18505 add_pubname (decl, subr_die);
18506 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18507 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18508 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18509 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18511 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18512 emit the real type on the definition die. */
18513 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18515 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18516 if (die == auto_die || die == decltype_auto_die)
18517 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18518 TYPE_UNQUALIFIED, context_die);
18522 else
18524 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18526 if (TREE_PUBLIC (decl))
18527 add_AT_flag (subr_die, DW_AT_external, 1);
18529 add_name_and_src_coords_attributes (subr_die, decl);
18530 add_pubname (decl, subr_die);
18531 if (debug_info_level > DINFO_LEVEL_TERSE)
18533 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18534 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18535 TYPE_UNQUALIFIED, context_die);
18538 add_pure_or_virtual_attribute (subr_die, decl);
18539 if (DECL_ARTIFICIAL (decl))
18540 add_AT_flag (subr_die, DW_AT_artificial, 1);
18542 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18543 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18545 add_accessibility_attribute (subr_die, decl);
18548 if (declaration)
18550 if (!old_die || !get_AT (old_die, DW_AT_inline))
18552 add_AT_flag (subr_die, DW_AT_declaration, 1);
18554 /* If this is an explicit function declaration then generate
18555 a DW_AT_explicit attribute. */
18556 if (lang_hooks.decls.function_decl_explicit_p (decl)
18557 && (dwarf_version >= 3 || !dwarf_strict))
18558 add_AT_flag (subr_die, DW_AT_explicit, 1);
18560 /* If this is a C++11 deleted special function member then generate
18561 a DW_AT_GNU_deleted attribute. */
18562 if (lang_hooks.decls.function_decl_deleted_p (decl)
18563 && (! dwarf_strict))
18564 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18566 /* The first time we see a member function, it is in the context of
18567 the class to which it belongs. We make sure of this by emitting
18568 the class first. The next time is the definition, which is
18569 handled above. The two may come from the same source text.
18571 Note that force_decl_die() forces function declaration die. It is
18572 later reused to represent definition. */
18573 equate_decl_number_to_die (decl, subr_die);
18576 else if (DECL_ABSTRACT_P (decl))
18578 if (DECL_DECLARED_INLINE_P (decl))
18580 if (cgraph_function_possibly_inlined_p (decl))
18581 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18582 else
18583 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18585 else
18587 if (cgraph_function_possibly_inlined_p (decl))
18588 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18589 else
18590 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18593 if (DECL_DECLARED_INLINE_P (decl)
18594 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18595 add_AT_flag (subr_die, DW_AT_artificial, 1);
18597 equate_decl_number_to_die (decl, subr_die);
18599 else if (!DECL_EXTERNAL (decl))
18601 HOST_WIDE_INT cfa_fb_offset;
18602 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18604 if (!old_die || !get_AT (old_die, DW_AT_inline))
18605 equate_decl_number_to_die (decl, subr_die);
18607 gcc_checking_assert (fun);
18608 if (!flag_reorder_blocks_and_partition)
18610 dw_fde_ref fde = fun->fde;
18611 if (fde->dw_fde_begin)
18613 /* We have already generated the labels. */
18614 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18615 fde->dw_fde_end, false);
18617 else
18619 /* Create start/end labels and add the range. */
18620 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18621 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18622 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18623 current_function_funcdef_no);
18624 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18625 current_function_funcdef_no);
18626 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18627 false);
18630 #if VMS_DEBUGGING_INFO
18631 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18632 Section 2.3 Prologue and Epilogue Attributes:
18633 When a breakpoint is set on entry to a function, it is generally
18634 desirable for execution to be suspended, not on the very first
18635 instruction of the function, but rather at a point after the
18636 function's frame has been set up, after any language defined local
18637 declaration processing has been completed, and before execution of
18638 the first statement of the function begins. Debuggers generally
18639 cannot properly determine where this point is. Similarly for a
18640 breakpoint set on exit from a function. The prologue and epilogue
18641 attributes allow a compiler to communicate the location(s) to use. */
18644 if (fde->dw_fde_vms_end_prologue)
18645 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18646 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18648 if (fde->dw_fde_vms_begin_epilogue)
18649 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18650 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18652 #endif
18655 else
18657 /* Generate pubnames entries for the split function code ranges. */
18658 dw_fde_ref fde = fun->fde;
18660 if (fde->dw_fde_second_begin)
18662 if (dwarf_version >= 3 || !dwarf_strict)
18664 /* We should use ranges for non-contiguous code section
18665 addresses. Use the actual code range for the initial
18666 section, since the HOT/COLD labels might precede an
18667 alignment offset. */
18668 bool range_list_added = false;
18669 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18670 fde->dw_fde_end, &range_list_added,
18671 false);
18672 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18673 fde->dw_fde_second_end,
18674 &range_list_added, false);
18675 if (range_list_added)
18676 add_ranges (NULL);
18678 else
18680 /* There is no real support in DW2 for this .. so we make
18681 a work-around. First, emit the pub name for the segment
18682 containing the function label. Then make and emit a
18683 simplified subprogram DIE for the second segment with the
18684 name pre-fixed by __hot/cold_sect_of_. We use the same
18685 linkage name for the second die so that gdb will find both
18686 sections when given "b foo". */
18687 const char *name = NULL;
18688 tree decl_name = DECL_NAME (decl);
18689 dw_die_ref seg_die;
18691 /* Do the 'primary' section. */
18692 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18693 fde->dw_fde_end, false);
18695 /* Build a minimal DIE for the secondary section. */
18696 seg_die = new_die (DW_TAG_subprogram,
18697 subr_die->die_parent, decl);
18699 if (TREE_PUBLIC (decl))
18700 add_AT_flag (seg_die, DW_AT_external, 1);
18702 if (decl_name != NULL
18703 && IDENTIFIER_POINTER (decl_name) != NULL)
18705 name = dwarf2_name (decl, 1);
18706 if (! DECL_ARTIFICIAL (decl))
18707 add_src_coords_attributes (seg_die, decl);
18709 add_linkage_name (seg_die, decl);
18711 gcc_assert (name != NULL);
18712 add_pure_or_virtual_attribute (seg_die, decl);
18713 if (DECL_ARTIFICIAL (decl))
18714 add_AT_flag (seg_die, DW_AT_artificial, 1);
18716 name = concat ("__second_sect_of_", name, NULL);
18717 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18718 fde->dw_fde_second_end, false);
18719 add_name_attribute (seg_die, name);
18720 if (want_pubnames ())
18721 add_pubname_string (name, seg_die);
18724 else
18725 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18726 false);
18729 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18731 /* We define the "frame base" as the function's CFA. This is more
18732 convenient for several reasons: (1) It's stable across the prologue
18733 and epilogue, which makes it better than just a frame pointer,
18734 (2) With dwarf3, there exists a one-byte encoding that allows us
18735 to reference the .debug_frame data by proxy, but failing that,
18736 (3) We can at least reuse the code inspection and interpretation
18737 code that determines the CFA position at various points in the
18738 function. */
18739 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18741 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18742 add_AT_loc (subr_die, DW_AT_frame_base, op);
18744 else
18746 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18747 if (list->dw_loc_next)
18748 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18749 else
18750 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18753 /* Compute a displacement from the "steady-state frame pointer" to
18754 the CFA. The former is what all stack slots and argument slots
18755 will reference in the rtl; the latter is what we've told the
18756 debugger about. We'll need to adjust all frame_base references
18757 by this displacement. */
18758 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18760 if (fun->static_chain_decl)
18761 add_AT_location_description (subr_die, DW_AT_static_link,
18762 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
18765 /* Generate child dies for template paramaters. */
18766 if (debug_info_level > DINFO_LEVEL_TERSE)
18767 gen_generic_params_dies (decl);
18769 /* Now output descriptions of the arguments for this function. This gets
18770 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18771 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18772 `...' at the end of the formal parameter list. In order to find out if
18773 there was a trailing ellipsis or not, we must instead look at the type
18774 associated with the FUNCTION_DECL. This will be a node of type
18775 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18776 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18777 an ellipsis at the end. */
18779 /* In the case where we are describing a mere function declaration, all we
18780 need to do here (and all we *can* do here) is to describe the *types* of
18781 its formal parameters. */
18782 if (debug_info_level <= DINFO_LEVEL_TERSE)
18784 else if (declaration)
18785 gen_formal_types_die (decl, subr_die);
18786 else
18788 /* Generate DIEs to represent all known formal parameters. */
18789 tree parm = DECL_ARGUMENTS (decl);
18790 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18791 tree generic_decl_parm = generic_decl
18792 ? DECL_ARGUMENTS (generic_decl)
18793 : NULL;
18795 /* Now we want to walk the list of parameters of the function and
18796 emit their relevant DIEs.
18798 We consider the case of DECL being an instance of a generic function
18799 as well as it being a normal function.
18801 If DECL is an instance of a generic function we walk the
18802 parameters of the generic function declaration _and_ the parameters of
18803 DECL itself. This is useful because we want to emit specific DIEs for
18804 function parameter packs and those are declared as part of the
18805 generic function declaration. In that particular case,
18806 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18807 That DIE has children DIEs representing the set of arguments
18808 of the pack. Note that the set of pack arguments can be empty.
18809 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18810 children DIE.
18812 Otherwise, we just consider the parameters of DECL. */
18813 while (generic_decl_parm || parm)
18815 if (generic_decl_parm
18816 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18817 gen_formal_parameter_pack_die (generic_decl_parm,
18818 parm, subr_die,
18819 &parm);
18820 else if (parm && !POINTER_BOUNDS_P (parm))
18822 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18824 if (parm == DECL_ARGUMENTS (decl)
18825 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18826 && parm_die
18827 && (dwarf_version >= 3 || !dwarf_strict))
18828 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18830 parm = DECL_CHAIN (parm);
18832 else if (parm)
18833 parm = DECL_CHAIN (parm);
18835 if (generic_decl_parm)
18836 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18839 /* Decide whether we need an unspecified_parameters DIE at the end.
18840 There are 2 more cases to do this for: 1) the ansi ... declaration -
18841 this is detectable when the end of the arg list is not a
18842 void_type_node 2) an unprototyped function declaration (not a
18843 definition). This just means that we have no info about the
18844 parameters at all. */
18845 if (prototype_p (TREE_TYPE (decl)))
18847 /* This is the prototyped case, check for.... */
18848 if (stdarg_p (TREE_TYPE (decl)))
18849 gen_unspecified_parameters_die (decl, subr_die);
18851 else if (DECL_INITIAL (decl) == NULL_TREE)
18852 gen_unspecified_parameters_die (decl, subr_die);
18855 /* Output Dwarf info for all of the stuff within the body of the function
18856 (if it has one - it may be just a declaration). */
18857 outer_scope = DECL_INITIAL (decl);
18859 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18860 a function. This BLOCK actually represents the outermost binding contour
18861 for the function, i.e. the contour in which the function's formal
18862 parameters and labels get declared. Curiously, it appears that the front
18863 end doesn't actually put the PARM_DECL nodes for the current function onto
18864 the BLOCK_VARS list for this outer scope, but are strung off of the
18865 DECL_ARGUMENTS list for the function instead.
18867 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18868 the LABEL_DECL nodes for the function however, and we output DWARF info
18869 for those in decls_for_scope. Just within the `outer_scope' there will be
18870 a BLOCK node representing the function's outermost pair of curly braces,
18871 and any blocks used for the base and member initializers of a C++
18872 constructor function. */
18873 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18875 int call_site_note_count = 0;
18876 int tail_call_site_note_count = 0;
18878 /* Emit a DW_TAG_variable DIE for a named return value. */
18879 if (DECL_NAME (DECL_RESULT (decl)))
18880 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18882 decls_for_scope (outer_scope, subr_die);
18884 if (call_arg_locations && !dwarf_strict)
18886 struct call_arg_loc_node *ca_loc;
18887 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18889 dw_die_ref die = NULL;
18890 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18891 rtx arg, next_arg;
18893 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18894 arg; arg = next_arg)
18896 dw_loc_descr_ref reg, val;
18897 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18898 dw_die_ref cdie, tdie = NULL;
18900 next_arg = XEXP (arg, 1);
18901 if (REG_P (XEXP (XEXP (arg, 0), 0))
18902 && next_arg
18903 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18904 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18905 && REGNO (XEXP (XEXP (arg, 0), 0))
18906 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18907 next_arg = XEXP (next_arg, 1);
18908 if (mode == VOIDmode)
18910 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18911 if (mode == VOIDmode)
18912 mode = GET_MODE (XEXP (arg, 0));
18914 if (mode == VOIDmode || mode == BLKmode)
18915 continue;
18916 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18918 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18919 tloc = XEXP (XEXP (arg, 0), 1);
18920 continue;
18922 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18923 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18925 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18926 tlocc = XEXP (XEXP (arg, 0), 1);
18927 continue;
18929 reg = NULL;
18930 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18931 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18932 VAR_INIT_STATUS_INITIALIZED);
18933 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18935 rtx mem = XEXP (XEXP (arg, 0), 0);
18936 reg = mem_loc_descriptor (XEXP (mem, 0),
18937 get_address_mode (mem),
18938 GET_MODE (mem),
18939 VAR_INIT_STATUS_INITIALIZED);
18941 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18942 == DEBUG_PARAMETER_REF)
18944 tree tdecl
18945 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18946 tdie = lookup_decl_die (tdecl);
18947 if (tdie == NULL)
18948 continue;
18950 else
18951 continue;
18952 if (reg == NULL
18953 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18954 != DEBUG_PARAMETER_REF)
18955 continue;
18956 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18957 VOIDmode,
18958 VAR_INIT_STATUS_INITIALIZED);
18959 if (val == NULL)
18960 continue;
18961 if (die == NULL)
18962 die = gen_call_site_die (decl, subr_die, ca_loc);
18963 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18964 NULL_TREE);
18965 if (reg != NULL)
18966 add_AT_loc (cdie, DW_AT_location, reg);
18967 else if (tdie != NULL)
18968 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18969 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18970 if (next_arg != XEXP (arg, 1))
18972 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18973 if (mode == VOIDmode)
18974 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18975 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18976 0), 1),
18977 mode, VOIDmode,
18978 VAR_INIT_STATUS_INITIALIZED);
18979 if (val != NULL)
18980 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18983 if (die == NULL
18984 && (ca_loc->symbol_ref || tloc))
18985 die = gen_call_site_die (decl, subr_die, ca_loc);
18986 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18988 dw_loc_descr_ref tval = NULL;
18990 if (tloc != NULL_RTX)
18991 tval = mem_loc_descriptor (tloc,
18992 GET_MODE (tloc) == VOIDmode
18993 ? Pmode : GET_MODE (tloc),
18994 VOIDmode,
18995 VAR_INIT_STATUS_INITIALIZED);
18996 if (tval)
18997 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18998 else if (tlocc != NULL_RTX)
19000 tval = mem_loc_descriptor (tlocc,
19001 GET_MODE (tlocc) == VOIDmode
19002 ? Pmode : GET_MODE (tlocc),
19003 VOIDmode,
19004 VAR_INIT_STATUS_INITIALIZED);
19005 if (tval)
19006 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19007 tval);
19010 if (die != NULL)
19012 call_site_note_count++;
19013 if (ca_loc->tail_call_p)
19014 tail_call_site_note_count++;
19018 call_arg_locations = NULL;
19019 call_arg_loc_last = NULL;
19020 if (tail_call_site_count >= 0
19021 && tail_call_site_count == tail_call_site_note_count
19022 && !dwarf_strict)
19024 if (call_site_count >= 0
19025 && call_site_count == call_site_note_count)
19026 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19027 else
19028 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19030 call_site_count = -1;
19031 tail_call_site_count = -1;
19034 if (subr_die != old_die)
19035 /* Add the calling convention attribute if requested. */
19036 add_calling_convention_attribute (subr_die, decl);
19039 /* Returns a hash value for X (which really is a die_struct). */
19041 hashval_t
19042 block_die_hasher::hash (die_struct *d)
19044 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19047 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19048 as decl_id and die_parent of die_struct Y. */
19050 bool
19051 block_die_hasher::equal (die_struct *x, die_struct *y)
19053 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19056 /* Generate a DIE to represent a declared data object.
19057 Either DECL or ORIGIN must be non-null. */
19059 static void
19060 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19062 HOST_WIDE_INT off = 0;
19063 tree com_decl;
19064 tree decl_or_origin = decl ? decl : origin;
19065 tree ultimate_origin;
19066 dw_die_ref var_die;
19067 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19068 dw_die_ref origin_die;
19069 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19070 || class_or_namespace_scope_p (context_die));
19071 bool specialization_p = false;
19073 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19074 if (decl || ultimate_origin)
19075 origin = ultimate_origin;
19076 com_decl = fortran_common (decl_or_origin, &off);
19078 /* Symbol in common gets emitted as a child of the common block, in the form
19079 of a data member. */
19080 if (com_decl)
19082 dw_die_ref com_die;
19083 dw_loc_list_ref loc;
19084 die_node com_die_arg;
19086 var_die = lookup_decl_die (decl_or_origin);
19087 if (var_die)
19089 if (get_AT (var_die, DW_AT_location) == NULL)
19091 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19092 if (loc)
19094 if (off)
19096 /* Optimize the common case. */
19097 if (single_element_loc_list_p (loc)
19098 && loc->expr->dw_loc_opc == DW_OP_addr
19099 && loc->expr->dw_loc_next == NULL
19100 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19101 == SYMBOL_REF)
19103 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19104 loc->expr->dw_loc_oprnd1.v.val_addr
19105 = plus_constant (GET_MODE (x), x , off);
19107 else
19108 loc_list_plus_const (loc, off);
19110 add_AT_location_description (var_die, DW_AT_location, loc);
19111 remove_AT (var_die, DW_AT_declaration);
19114 return;
19117 if (common_block_die_table == NULL)
19118 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19120 com_die_arg.decl_id = DECL_UID (com_decl);
19121 com_die_arg.die_parent = context_die;
19122 com_die = common_block_die_table->find (&com_die_arg);
19123 loc = loc_list_from_tree (com_decl, 2, NULL);
19124 if (com_die == NULL)
19126 const char *cnam
19127 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19128 die_node **slot;
19130 com_die = new_die (DW_TAG_common_block, context_die, decl);
19131 add_name_and_src_coords_attributes (com_die, com_decl);
19132 if (loc)
19134 add_AT_location_description (com_die, DW_AT_location, loc);
19135 /* Avoid sharing the same loc descriptor between
19136 DW_TAG_common_block and DW_TAG_variable. */
19137 loc = loc_list_from_tree (com_decl, 2, NULL);
19139 else if (DECL_EXTERNAL (decl))
19140 add_AT_flag (com_die, DW_AT_declaration, 1);
19141 if (want_pubnames ())
19142 add_pubname_string (cnam, com_die); /* ??? needed? */
19143 com_die->decl_id = DECL_UID (com_decl);
19144 slot = common_block_die_table->find_slot (com_die, INSERT);
19145 *slot = com_die;
19147 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19149 add_AT_location_description (com_die, DW_AT_location, loc);
19150 loc = loc_list_from_tree (com_decl, 2, NULL);
19151 remove_AT (com_die, DW_AT_declaration);
19153 var_die = new_die (DW_TAG_variable, com_die, decl);
19154 add_name_and_src_coords_attributes (var_die, decl);
19155 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19156 context_die);
19157 add_AT_flag (var_die, DW_AT_external, 1);
19158 if (loc)
19160 if (off)
19162 /* Optimize the common case. */
19163 if (single_element_loc_list_p (loc)
19164 && loc->expr->dw_loc_opc == DW_OP_addr
19165 && loc->expr->dw_loc_next == NULL
19166 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19168 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19169 loc->expr->dw_loc_oprnd1.v.val_addr
19170 = plus_constant (GET_MODE (x), x, off);
19172 else
19173 loc_list_plus_const (loc, off);
19175 add_AT_location_description (var_die, DW_AT_location, loc);
19177 else if (DECL_EXTERNAL (decl))
19178 add_AT_flag (var_die, DW_AT_declaration, 1);
19179 equate_decl_number_to_die (decl, var_die);
19180 return;
19183 /* If the compiler emitted a definition for the DECL declaration
19184 and if we already emitted a DIE for it, don't emit a second
19185 DIE for it again. Allow re-declarations of DECLs that are
19186 inside functions, though. */
19187 if (old_die && declaration && !local_scope_p (context_die))
19188 return;
19190 /* For static data members, the declaration in the class is supposed
19191 to have DW_TAG_member tag; the specification should still be
19192 DW_TAG_variable referencing the DW_TAG_member DIE. */
19193 if (declaration && class_scope_p (context_die))
19194 var_die = new_die (DW_TAG_member, context_die, decl);
19195 else
19196 var_die = new_die (DW_TAG_variable, context_die, decl);
19198 origin_die = NULL;
19199 if (origin != NULL)
19200 origin_die = add_abstract_origin_attribute (var_die, origin);
19202 /* Loop unrolling can create multiple blocks that refer to the same
19203 static variable, so we must test for the DW_AT_declaration flag.
19205 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19206 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19207 sharing them.
19209 ??? Duplicated blocks have been rewritten to use .debug_ranges.
19211 ??? The declare_in_namespace support causes us to get two DIEs for one
19212 variable, both of which are declarations. We want to avoid considering
19213 one to be a specification, so we must test that this DIE is not a
19214 declaration. */
19215 else if (old_die && TREE_STATIC (decl) && ! declaration
19216 && get_AT_flag (old_die, DW_AT_declaration) == 1)
19218 /* This is a definition of a C++ class level static. */
19219 add_AT_specification (var_die, old_die);
19220 specialization_p = true;
19221 if (DECL_NAME (decl))
19223 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19224 struct dwarf_file_data * file_index = lookup_filename (s.file);
19226 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19227 add_AT_file (var_die, DW_AT_decl_file, file_index);
19229 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19230 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19232 if (old_die->die_tag == DW_TAG_member)
19233 add_linkage_name (var_die, decl);
19236 else
19237 add_name_and_src_coords_attributes (var_die, decl);
19239 if ((origin == NULL && !specialization_p)
19240 || (origin != NULL
19241 && !DECL_ABSTRACT_P (decl_or_origin)
19242 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19243 decl_function_context
19244 (decl_or_origin))))
19246 tree type = TREE_TYPE (decl_or_origin);
19248 if (decl_by_reference_p (decl_or_origin))
19249 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19250 context_die);
19251 else
19252 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19253 context_die);
19256 if (origin == NULL && !specialization_p)
19258 if (TREE_PUBLIC (decl))
19259 add_AT_flag (var_die, DW_AT_external, 1);
19261 if (DECL_ARTIFICIAL (decl))
19262 add_AT_flag (var_die, DW_AT_artificial, 1);
19264 add_accessibility_attribute (var_die, decl);
19267 if (declaration)
19268 add_AT_flag (var_die, DW_AT_declaration, 1);
19270 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19271 equate_decl_number_to_die (decl, var_die);
19273 if (! declaration
19274 && (! DECL_ABSTRACT_P (decl_or_origin)
19275 /* Local static vars are shared between all clones/inlines,
19276 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19277 already set. */
19278 || (TREE_CODE (decl_or_origin) == VAR_DECL
19279 && TREE_STATIC (decl_or_origin)
19280 && DECL_RTL_SET_P (decl_or_origin)))
19281 /* When abstract origin already has DW_AT_location attribute, no need
19282 to add it again. */
19283 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19285 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19286 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19287 defer_location (decl_or_origin, var_die);
19288 else
19289 add_location_or_const_value_attribute (var_die, decl_or_origin,
19290 decl == NULL, DW_AT_location);
19291 add_pubname (decl_or_origin, var_die);
19293 else
19294 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19297 /* Generate a DIE to represent a named constant. */
19299 static void
19300 gen_const_die (tree decl, dw_die_ref context_die)
19302 dw_die_ref const_die;
19303 tree type = TREE_TYPE (decl);
19305 const_die = new_die (DW_TAG_constant, context_die, decl);
19306 add_name_and_src_coords_attributes (const_die, decl);
19307 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19308 if (TREE_PUBLIC (decl))
19309 add_AT_flag (const_die, DW_AT_external, 1);
19310 if (DECL_ARTIFICIAL (decl))
19311 add_AT_flag (const_die, DW_AT_artificial, 1);
19312 tree_add_const_value_attribute_for_decl (const_die, decl);
19315 /* Generate a DIE to represent a label identifier. */
19317 static void
19318 gen_label_die (tree decl, dw_die_ref context_die)
19320 tree origin = decl_ultimate_origin (decl);
19321 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19322 rtx insn;
19323 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19325 if (origin != NULL)
19326 add_abstract_origin_attribute (lbl_die, origin);
19327 else
19328 add_name_and_src_coords_attributes (lbl_die, decl);
19330 if (DECL_ABSTRACT_P (decl))
19331 equate_decl_number_to_die (decl, lbl_die);
19332 else
19334 insn = DECL_RTL_IF_SET (decl);
19336 /* Deleted labels are programmer specified labels which have been
19337 eliminated because of various optimizations. We still emit them
19338 here so that it is possible to put breakpoints on them. */
19339 if (insn
19340 && (LABEL_P (insn)
19341 || ((NOTE_P (insn)
19342 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19344 /* When optimization is enabled (via -O) some parts of the compiler
19345 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19346 represent source-level labels which were explicitly declared by
19347 the user. This really shouldn't be happening though, so catch
19348 it if it ever does happen. */
19349 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19351 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19352 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19354 else if (insn
19355 && NOTE_P (insn)
19356 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19357 && CODE_LABEL_NUMBER (insn) != -1)
19359 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19360 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19365 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19366 attributes to the DIE for a block STMT, to describe where the inlined
19367 function was called from. This is similar to add_src_coords_attributes. */
19369 static inline void
19370 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19372 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19374 if (dwarf_version >= 3 || !dwarf_strict)
19376 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19377 add_AT_unsigned (die, DW_AT_call_line, s.line);
19382 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19383 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19385 static inline void
19386 add_high_low_attributes (tree stmt, dw_die_ref die)
19388 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19390 if (BLOCK_FRAGMENT_CHAIN (stmt)
19391 && (dwarf_version >= 3 || !dwarf_strict))
19393 tree chain, superblock = NULL_TREE;
19394 dw_die_ref pdie;
19395 dw_attr_ref attr = NULL;
19397 if (inlined_function_outer_scope_p (stmt))
19399 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19400 BLOCK_NUMBER (stmt));
19401 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19404 /* Optimize duplicate .debug_ranges lists or even tails of
19405 lists. If this BLOCK has same ranges as its supercontext,
19406 lookup DW_AT_ranges attribute in the supercontext (and
19407 recursively so), verify that the ranges_table contains the
19408 right values and use it instead of adding a new .debug_range. */
19409 for (chain = stmt, pdie = die;
19410 BLOCK_SAME_RANGE (chain);
19411 chain = BLOCK_SUPERCONTEXT (chain))
19413 dw_attr_ref new_attr;
19415 pdie = pdie->die_parent;
19416 if (pdie == NULL)
19417 break;
19418 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19419 break;
19420 new_attr = get_AT (pdie, DW_AT_ranges);
19421 if (new_attr == NULL
19422 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19423 break;
19424 attr = new_attr;
19425 superblock = BLOCK_SUPERCONTEXT (chain);
19427 if (attr != NULL
19428 && (ranges_table[attr->dw_attr_val.v.val_offset
19429 / 2 / DWARF2_ADDR_SIZE].num
19430 == BLOCK_NUMBER (superblock))
19431 && BLOCK_FRAGMENT_CHAIN (superblock))
19433 unsigned long off = attr->dw_attr_val.v.val_offset
19434 / 2 / DWARF2_ADDR_SIZE;
19435 unsigned long supercnt = 0, thiscnt = 0;
19436 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19437 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19439 ++supercnt;
19440 gcc_checking_assert (ranges_table[off + supercnt].num
19441 == BLOCK_NUMBER (chain));
19443 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19444 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19445 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19446 ++thiscnt;
19447 gcc_assert (supercnt >= thiscnt);
19448 add_AT_range_list (die, DW_AT_ranges,
19449 ((off + supercnt - thiscnt)
19450 * 2 * DWARF2_ADDR_SIZE),
19451 false);
19452 return;
19455 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19457 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19460 add_ranges (chain);
19461 chain = BLOCK_FRAGMENT_CHAIN (chain);
19463 while (chain);
19464 add_ranges (NULL);
19466 else
19468 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19469 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19470 BLOCK_NUMBER (stmt));
19471 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19472 BLOCK_NUMBER (stmt));
19473 add_AT_low_high_pc (die, label, label_high, false);
19477 /* Generate a DIE for a lexical block. */
19479 static void
19480 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19482 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19484 if (call_arg_locations)
19486 if (block_map.length () <= BLOCK_NUMBER (stmt))
19487 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19488 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19491 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19492 add_high_low_attributes (stmt, stmt_die);
19494 decls_for_scope (stmt, stmt_die);
19497 /* Generate a DIE for an inlined subprogram. */
19499 static void
19500 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19502 tree decl;
19504 /* The instance of function that is effectively being inlined shall not
19505 be abstract. */
19506 gcc_assert (! BLOCK_ABSTRACT (stmt));
19508 decl = block_ultimate_origin (stmt);
19510 /* Emit info for the abstract instance first, if we haven't yet. We
19511 must emit this even if the block is abstract, otherwise when we
19512 emit the block below (or elsewhere), we may end up trying to emit
19513 a die whose origin die hasn't been emitted, and crashing. */
19514 dwarf2out_abstract_function (decl);
19516 if (! BLOCK_ABSTRACT (stmt))
19518 dw_die_ref subr_die
19519 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19521 if (call_arg_locations)
19523 if (block_map.length () <= BLOCK_NUMBER (stmt))
19524 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19525 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19527 add_abstract_origin_attribute (subr_die, decl);
19528 if (TREE_ASM_WRITTEN (stmt))
19529 add_high_low_attributes (stmt, subr_die);
19530 add_call_src_coords_attributes (stmt, subr_die);
19532 decls_for_scope (stmt, subr_die);
19536 /* Generate a DIE for a field in a record, or structure. */
19538 static void
19539 gen_field_die (tree decl, dw_die_ref context_die)
19541 dw_die_ref decl_die;
19543 if (TREE_TYPE (decl) == error_mark_node)
19544 return;
19546 decl_die = new_die (DW_TAG_member, context_die, decl);
19547 add_name_and_src_coords_attributes (decl_die, decl);
19548 add_type_attribute (decl_die, member_declared_type (decl),
19549 decl_quals (decl), context_die);
19551 if (DECL_BIT_FIELD_TYPE (decl))
19553 add_byte_size_attribute (decl_die, decl);
19554 add_bit_size_attribute (decl_die, decl);
19555 add_bit_offset_attribute (decl_die, decl);
19558 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19559 add_data_member_location_attribute (decl_die, decl);
19561 if (DECL_ARTIFICIAL (decl))
19562 add_AT_flag (decl_die, DW_AT_artificial, 1);
19564 add_accessibility_attribute (decl_die, decl);
19566 /* Equate decl number to die, so that we can look up this decl later on. */
19567 equate_decl_number_to_die (decl, decl_die);
19570 #if 0
19571 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19572 Use modified_type_die instead.
19573 We keep this code here just in case these types of DIEs may be needed to
19574 represent certain things in other languages (e.g. Pascal) someday. */
19576 static void
19577 gen_pointer_type_die (tree type, dw_die_ref context_die)
19579 dw_die_ref ptr_die
19580 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19582 equate_type_number_to_die (type, ptr_die);
19583 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19584 context_die);
19585 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19588 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19589 Use modified_type_die instead.
19590 We keep this code here just in case these types of DIEs may be needed to
19591 represent certain things in other languages (e.g. Pascal) someday. */
19593 static void
19594 gen_reference_type_die (tree type, dw_die_ref context_die)
19596 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19598 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19599 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19600 else
19601 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19603 equate_type_number_to_die (type, ref_die);
19604 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19605 context_die);
19606 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19608 #endif
19610 /* Generate a DIE for a pointer to a member type. */
19612 static void
19613 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19615 dw_die_ref ptr_die
19616 = new_die (DW_TAG_ptr_to_member_type,
19617 scope_die_for (type, context_die), type);
19619 equate_type_number_to_die (type, ptr_die);
19620 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19621 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19622 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19623 context_die);
19626 typedef const char *dchar_p; /* For DEF_VEC_P. */
19628 static char *producer_string;
19630 /* Return a heap allocated producer string including command line options
19631 if -grecord-gcc-switches. */
19633 static char *
19634 gen_producer_string (void)
19636 size_t j;
19637 auto_vec<dchar_p> switches;
19638 const char *language_string = lang_hooks.name;
19639 char *producer, *tail;
19640 const char *p;
19641 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19642 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19644 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19645 switch (save_decoded_options[j].opt_index)
19647 case OPT_o:
19648 case OPT_d:
19649 case OPT_dumpbase:
19650 case OPT_dumpdir:
19651 case OPT_auxbase:
19652 case OPT_auxbase_strip:
19653 case OPT_quiet:
19654 case OPT_version:
19655 case OPT_v:
19656 case OPT_w:
19657 case OPT_L:
19658 case OPT_D:
19659 case OPT_I:
19660 case OPT_U:
19661 case OPT_SPECIAL_unknown:
19662 case OPT_SPECIAL_ignore:
19663 case OPT_SPECIAL_program_name:
19664 case OPT_SPECIAL_input_file:
19665 case OPT_grecord_gcc_switches:
19666 case OPT_gno_record_gcc_switches:
19667 case OPT__output_pch_:
19668 case OPT_fdiagnostics_show_location_:
19669 case OPT_fdiagnostics_show_option:
19670 case OPT_fdiagnostics_show_caret:
19671 case OPT_fdiagnostics_color_:
19672 case OPT_fverbose_asm:
19673 case OPT____:
19674 case OPT__sysroot_:
19675 case OPT_nostdinc:
19676 case OPT_nostdinc__:
19677 case OPT_fpreprocessed:
19678 /* Ignore these. */
19679 continue;
19680 default:
19681 if (cl_options[save_decoded_options[j].opt_index].flags
19682 & CL_NO_DWARF_RECORD)
19683 continue;
19684 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19685 == '-');
19686 switch (save_decoded_options[j].canonical_option[0][1])
19688 case 'M':
19689 case 'i':
19690 case 'W':
19691 continue;
19692 case 'f':
19693 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19694 "dump", 4) == 0)
19695 continue;
19696 break;
19697 default:
19698 break;
19700 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19701 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19702 break;
19705 producer = XNEWVEC (char, plen + 1 + len + 1);
19706 tail = producer;
19707 sprintf (tail, "%s %s", language_string, version_string);
19708 tail += plen;
19710 FOR_EACH_VEC_ELT (switches, j, p)
19712 len = strlen (p);
19713 *tail = ' ';
19714 memcpy (tail + 1, p, len);
19715 tail += len + 1;
19718 *tail = '\0';
19719 return producer;
19722 /* Given a C and/or C++ language/version string return the "highest".
19723 C++ is assumed to be "higher" than C in this case. Used for merging
19724 LTO translation unit languages. */
19725 static const char *
19726 highest_c_language (const char *lang1, const char *lang2)
19728 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
19729 return "GNU C++14";
19730 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
19731 return "GNU C++11";
19732 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
19733 return "GNU C++98";
19735 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
19736 return "GNU C11";
19737 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
19738 return "GNU C99";
19739 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
19740 return "GNU C89";
19742 gcc_unreachable ();
19746 /* Generate the DIE for the compilation unit. */
19748 static dw_die_ref
19749 gen_compile_unit_die (const char *filename)
19751 dw_die_ref die;
19752 const char *language_string = lang_hooks.name;
19753 int language;
19755 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19757 if (filename)
19759 add_name_attribute (die, filename);
19760 /* Don't add cwd for <built-in>. */
19761 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19762 add_comp_dir_attribute (die);
19765 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19767 /* If our producer is LTO try to figure out a common language to use
19768 from the global list of translation units. */
19769 if (strcmp (language_string, "GNU GIMPLE") == 0)
19771 unsigned i;
19772 tree t;
19773 const char *common_lang = NULL;
19775 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19777 if (!TRANSLATION_UNIT_LANGUAGE (t))
19778 continue;
19779 if (!common_lang)
19780 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19781 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19783 else if (strncmp (common_lang, "GNU C", 5) == 0
19784 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19785 /* Mixing C and C++ is ok, use C++ in that case. */
19786 common_lang = highest_c_language (common_lang,
19787 TRANSLATION_UNIT_LANGUAGE (t));
19788 else
19790 /* Fall back to C. */
19791 common_lang = NULL;
19792 break;
19796 if (common_lang)
19797 language_string = common_lang;
19800 language = DW_LANG_C;
19801 if (strncmp (language_string, "GNU C", 5) == 0
19802 && ISDIGIT (language_string[5]))
19804 language = DW_LANG_C89;
19805 if (dwarf_version >= 3 || !dwarf_strict)
19807 if (strcmp (language_string, "GNU C89") != 0)
19808 language = DW_LANG_C99;
19810 if (dwarf_version >= 5 /* || !dwarf_strict */)
19811 if (strcmp (language_string, "GNU C11") == 0)
19812 language = DW_LANG_C11;
19814 if (use_upc_dwarf2_extensions && flag_upc)
19815 language = DW_LANG_Upc;
19819 else if (strncmp (language_string, "GNU C++", 7) == 0)
19821 language = DW_LANG_C_plus_plus;
19822 if (dwarf_version >= 5 /* || !dwarf_strict */)
19824 if (strcmp (language_string, "GNU C++11") == 0)
19825 language = DW_LANG_C_plus_plus_11;
19826 else if (strcmp (language_string, "GNU C++14") == 0)
19827 language = DW_LANG_C_plus_plus_14;
19830 else if (strcmp (language_string, "GNU F77") == 0)
19831 language = DW_LANG_Fortran77;
19832 else if (strcmp (language_string, "GNU Pascal") == 0)
19833 language = DW_LANG_Pascal83;
19834 else if (dwarf_version >= 3 || !dwarf_strict)
19836 if (strcmp (language_string, "GNU Ada") == 0)
19837 language = DW_LANG_Ada95;
19838 else if (strcmp (language_string, "GNU Fortran") == 0)
19839 language = DW_LANG_Fortran95;
19840 else if (strcmp (language_string, "GNU Java") == 0)
19841 language = DW_LANG_Java;
19842 else if (strcmp (language_string, "GNU Objective-C") == 0)
19843 language = DW_LANG_ObjC;
19844 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19845 language = DW_LANG_ObjC_plus_plus;
19846 else if (dwarf_version >= 5 || !dwarf_strict)
19848 if (strcmp (language_string, "GNU Go") == 0)
19849 language = DW_LANG_Go;
19852 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19853 else if (strcmp (language_string, "GNU Fortran") == 0)
19854 language = DW_LANG_Fortran90;
19856 add_AT_unsigned (die, DW_AT_language, language);
19858 switch (language)
19860 case DW_LANG_Fortran77:
19861 case DW_LANG_Fortran90:
19862 case DW_LANG_Fortran95:
19863 /* Fortran has case insensitive identifiers and the front-end
19864 lowercases everything. */
19865 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19866 break;
19867 default:
19868 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19869 break;
19871 return die;
19874 /* Generate the DIE for a base class. */
19876 static void
19877 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19879 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19881 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19882 add_data_member_location_attribute (die, binfo);
19884 if (BINFO_VIRTUAL_P (binfo))
19885 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19887 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19888 children, otherwise the default is DW_ACCESS_public. In DWARF2
19889 the default has always been DW_ACCESS_private. */
19890 if (access == access_public_node)
19892 if (dwarf_version == 2
19893 || context_die->die_tag == DW_TAG_class_type)
19894 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19896 else if (access == access_protected_node)
19897 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19898 else if (dwarf_version > 2
19899 && context_die->die_tag != DW_TAG_class_type)
19900 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19903 /* Generate a DIE for a class member. */
19905 static void
19906 gen_member_die (tree type, dw_die_ref context_die)
19908 tree member;
19909 tree binfo = TYPE_BINFO (type);
19910 dw_die_ref child;
19912 /* If this is not an incomplete type, output descriptions of each of its
19913 members. Note that as we output the DIEs necessary to represent the
19914 members of this record or union type, we will also be trying to output
19915 DIEs to represent the *types* of those members. However the `type'
19916 function (above) will specifically avoid generating type DIEs for member
19917 types *within* the list of member DIEs for this (containing) type except
19918 for those types (of members) which are explicitly marked as also being
19919 members of this (containing) type themselves. The g++ front- end can
19920 force any given type to be treated as a member of some other (containing)
19921 type by setting the TYPE_CONTEXT of the given (member) type to point to
19922 the TREE node representing the appropriate (containing) type. */
19924 /* First output info about the base classes. */
19925 if (binfo)
19927 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19928 int i;
19929 tree base;
19931 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19932 gen_inheritance_die (base,
19933 (accesses ? (*accesses)[i] : access_public_node),
19934 context_die);
19937 /* Now output info about the data members and type members. */
19938 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19940 /* If we thought we were generating minimal debug info for TYPE
19941 and then changed our minds, some of the member declarations
19942 may have already been defined. Don't define them again, but
19943 do put them in the right order. */
19945 child = lookup_decl_die (member);
19946 if (child)
19947 splice_child_die (context_die, child);
19948 else
19949 gen_decl_die (member, NULL, context_die);
19952 /* Now output info about the function members (if any). */
19953 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19955 /* Don't include clones in the member list. */
19956 if (DECL_ABSTRACT_ORIGIN (member))
19957 continue;
19959 child = lookup_decl_die (member);
19960 if (child)
19961 splice_child_die (context_die, child);
19962 else
19963 gen_decl_die (member, NULL, context_die);
19967 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19968 is set, we pretend that the type was never defined, so we only get the
19969 member DIEs needed by later specification DIEs. */
19971 static void
19972 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19973 enum debug_info_usage usage)
19975 dw_die_ref type_die = lookup_type_die (type);
19976 dw_die_ref scope_die = 0;
19977 int nested = 0;
19978 int complete = (TYPE_SIZE (type)
19979 && (! TYPE_STUB_DECL (type)
19980 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19981 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19982 complete = complete && should_emit_struct_debug (type, usage);
19984 if (type_die && ! complete)
19985 return;
19987 if (TYPE_CONTEXT (type) != NULL_TREE
19988 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19989 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19990 nested = 1;
19992 scope_die = scope_die_for (type, context_die);
19994 /* Generate child dies for template paramaters. */
19995 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19996 schedule_generic_params_dies_gen (type);
19998 if (! type_die || (nested && is_cu_die (scope_die)))
19999 /* First occurrence of type or toplevel definition of nested class. */
20001 dw_die_ref old_die = type_die;
20003 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20004 ? record_type_tag (type) : DW_TAG_union_type,
20005 scope_die, type);
20006 equate_type_number_to_die (type, type_die);
20007 if (old_die)
20008 add_AT_specification (type_die, old_die);
20009 else
20010 add_name_attribute (type_die, type_tag (type));
20012 else
20013 remove_AT (type_die, DW_AT_declaration);
20015 /* If this type has been completed, then give it a byte_size attribute and
20016 then give a list of members. */
20017 if (complete && !ns_decl)
20019 /* Prevent infinite recursion in cases where the type of some member of
20020 this type is expressed in terms of this type itself. */
20021 TREE_ASM_WRITTEN (type) = 1;
20022 add_byte_size_attribute (type_die, type);
20023 if (TYPE_STUB_DECL (type) != NULL_TREE)
20025 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20026 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20029 /* If the first reference to this type was as the return type of an
20030 inline function, then it may not have a parent. Fix this now. */
20031 if (type_die->die_parent == NULL)
20032 add_child_die (scope_die, type_die);
20034 push_decl_scope (type);
20035 gen_member_die (type, type_die);
20036 pop_decl_scope ();
20038 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20039 if (TYPE_ARTIFICIAL (type))
20040 add_AT_flag (type_die, DW_AT_artificial, 1);
20042 /* GNU extension: Record what type our vtable lives in. */
20043 if (TYPE_VFIELD (type))
20045 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20047 gen_type_die (vtype, context_die);
20048 add_AT_die_ref (type_die, DW_AT_containing_type,
20049 lookup_type_die (vtype));
20052 else
20054 add_AT_flag (type_die, DW_AT_declaration, 1);
20056 /* We don't need to do this for function-local types. */
20057 if (TYPE_STUB_DECL (type)
20058 && ! decl_function_context (TYPE_STUB_DECL (type)))
20059 vec_safe_push (incomplete_types, type);
20062 if (get_AT (type_die, DW_AT_name))
20063 add_pubtype (type, type_die);
20066 /* Generate a DIE for a subroutine _type_. */
20068 static void
20069 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20071 tree return_type = TREE_TYPE (type);
20072 dw_die_ref subr_die
20073 = new_die (DW_TAG_subroutine_type,
20074 scope_die_for (type, context_die), type);
20076 equate_type_number_to_die (type, subr_die);
20077 add_prototyped_attribute (subr_die, type);
20078 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20079 gen_formal_types_die (type, subr_die);
20081 if (get_AT (subr_die, DW_AT_name))
20082 add_pubtype (type, subr_die);
20085 /* Generate a DIE for a type definition. */
20087 static void
20088 gen_typedef_die (tree decl, dw_die_ref context_die)
20090 dw_die_ref type_die;
20091 tree origin;
20093 if (TREE_ASM_WRITTEN (decl))
20094 return;
20096 TREE_ASM_WRITTEN (decl) = 1;
20097 type_die = new_die (DW_TAG_typedef, context_die, decl);
20098 origin = decl_ultimate_origin (decl);
20099 if (origin != NULL)
20100 add_abstract_origin_attribute (type_die, origin);
20101 else
20103 tree type;
20105 add_name_and_src_coords_attributes (type_die, decl);
20106 if (DECL_ORIGINAL_TYPE (decl))
20108 type = DECL_ORIGINAL_TYPE (decl);
20110 gcc_assert (type != TREE_TYPE (decl));
20111 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20113 else
20115 type = TREE_TYPE (decl);
20117 if (is_naming_typedef_decl (TYPE_NAME (type)))
20119 /* Here, we are in the case of decl being a typedef naming
20120 an anonymous type, e.g:
20121 typedef struct {...} foo;
20122 In that case TREE_TYPE (decl) is not a typedef variant
20123 type and TYPE_NAME of the anonymous type is set to the
20124 TYPE_DECL of the typedef. This construct is emitted by
20125 the C++ FE.
20127 TYPE is the anonymous struct named by the typedef
20128 DECL. As we need the DW_AT_type attribute of the
20129 DW_TAG_typedef to point to the DIE of TYPE, let's
20130 generate that DIE right away. add_type_attribute
20131 called below will then pick (via lookup_type_die) that
20132 anonymous struct DIE. */
20133 if (!TREE_ASM_WRITTEN (type))
20134 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20136 /* This is a GNU Extension. We are adding a
20137 DW_AT_linkage_name attribute to the DIE of the
20138 anonymous struct TYPE. The value of that attribute
20139 is the name of the typedef decl naming the anonymous
20140 struct. This greatly eases the work of consumers of
20141 this debug info. */
20142 add_linkage_attr (lookup_type_die (type), decl);
20146 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20148 if (is_naming_typedef_decl (decl))
20149 /* We want that all subsequent calls to lookup_type_die with
20150 TYPE in argument yield the DW_TAG_typedef we have just
20151 created. */
20152 equate_type_number_to_die (type, type_die);
20154 add_accessibility_attribute (type_die, decl);
20157 if (DECL_ABSTRACT_P (decl))
20158 equate_decl_number_to_die (decl, type_die);
20160 if (get_AT (type_die, DW_AT_name))
20161 add_pubtype (decl, type_die);
20164 /* Generate a DIE for a struct, class, enum or union type. */
20166 static void
20167 gen_tagged_type_die (tree type,
20168 dw_die_ref context_die,
20169 enum debug_info_usage usage)
20171 int need_pop;
20173 if (type == NULL_TREE
20174 || !is_tagged_type (type))
20175 return;
20177 /* If this is a nested type whose containing class hasn't been written
20178 out yet, writing it out will cover this one, too. This does not apply
20179 to instantiations of member class templates; they need to be added to
20180 the containing class as they are generated. FIXME: This hurts the
20181 idea of combining type decls from multiple TUs, since we can't predict
20182 what set of template instantiations we'll get. */
20183 if (TYPE_CONTEXT (type)
20184 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20185 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20187 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20189 if (TREE_ASM_WRITTEN (type))
20190 return;
20192 /* If that failed, attach ourselves to the stub. */
20193 push_decl_scope (TYPE_CONTEXT (type));
20194 context_die = lookup_type_die (TYPE_CONTEXT (type));
20195 need_pop = 1;
20197 else if (TYPE_CONTEXT (type) != NULL_TREE
20198 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20200 /* If this type is local to a function that hasn't been written
20201 out yet, use a NULL context for now; it will be fixed up in
20202 decls_for_scope. */
20203 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20204 /* A declaration DIE doesn't count; nested types need to go in the
20205 specification. */
20206 if (context_die && is_declaration_die (context_die))
20207 context_die = NULL;
20208 need_pop = 0;
20210 else
20212 context_die = declare_in_namespace (type, context_die);
20213 need_pop = 0;
20216 if (TREE_CODE (type) == ENUMERAL_TYPE)
20218 /* This might have been written out by the call to
20219 declare_in_namespace. */
20220 if (!TREE_ASM_WRITTEN (type))
20221 gen_enumeration_type_die (type, context_die);
20223 else
20224 gen_struct_or_union_type_die (type, context_die, usage);
20226 if (need_pop)
20227 pop_decl_scope ();
20229 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20230 it up if it is ever completed. gen_*_type_die will set it for us
20231 when appropriate. */
20234 /* Generate a type description DIE. */
20236 static void
20237 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20238 enum debug_info_usage usage)
20240 struct array_descr_info info;
20242 if (type == NULL_TREE || type == error_mark_node)
20243 return;
20245 if (TYPE_NAME (type) != NULL_TREE
20246 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20247 && is_redundant_typedef (TYPE_NAME (type))
20248 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20249 /* The DECL of this type is a typedef we don't want to emit debug
20250 info for but we want debug info for its underlying typedef.
20251 This can happen for e.g, the injected-class-name of a C++
20252 type. */
20253 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20255 /* If TYPE is a typedef type variant, let's generate debug info
20256 for the parent typedef which TYPE is a type of. */
20257 if (typedef_variant_p (type))
20259 if (TREE_ASM_WRITTEN (type))
20260 return;
20262 /* Prevent broken recursion; we can't hand off to the same type. */
20263 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20265 /* Give typedefs the right scope. */
20266 context_die = scope_die_for (type, context_die);
20268 TREE_ASM_WRITTEN (type) = 1;
20270 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20271 return;
20274 /* If type is an anonymous tagged type named by a typedef, let's
20275 generate debug info for the typedef. */
20276 if (is_naming_typedef_decl (TYPE_NAME (type)))
20278 /* Use the DIE of the containing namespace as the parent DIE of
20279 the type description DIE we want to generate. */
20280 if (DECL_CONTEXT (TYPE_NAME (type))
20281 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20282 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20284 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20285 return;
20288 /* We are going to output a DIE to represent the unqualified version
20289 of this type (i.e. without any const or volatile qualifiers) so
20290 get the main variant (i.e. the unqualified version) of this type
20291 now. (Vectors are special because the debugging info is in the
20292 cloned type itself). */
20293 if (TREE_CODE (type) != VECTOR_TYPE)
20294 type = type_main_variant (type);
20296 /* If this is an array type with hidden descriptor, handle it first. */
20297 if (!TREE_ASM_WRITTEN (type)
20298 && lang_hooks.types.get_array_descr_info)
20300 memset (&info, 0, sizeof (info));
20301 if (lang_hooks.types.get_array_descr_info (type, &info))
20303 gen_descr_array_type_die (type, &info, context_die);
20304 TREE_ASM_WRITTEN (type) = 1;
20305 return;
20309 if (TREE_ASM_WRITTEN (type))
20310 return;
20312 switch (TREE_CODE (type))
20314 case ERROR_MARK:
20315 break;
20317 case POINTER_TYPE:
20318 case REFERENCE_TYPE:
20319 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20320 ensures that the gen_type_die recursion will terminate even if the
20321 type is recursive. Recursive types are possible in Ada. */
20322 /* ??? We could perhaps do this for all types before the switch
20323 statement. */
20324 TREE_ASM_WRITTEN (type) = 1;
20326 /* For these types, all that is required is that we output a DIE (or a
20327 set of DIEs) to represent the "basis" type. */
20328 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20329 DINFO_USAGE_IND_USE);
20330 break;
20332 case OFFSET_TYPE:
20333 /* This code is used for C++ pointer-to-data-member types.
20334 Output a description of the relevant class type. */
20335 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20336 DINFO_USAGE_IND_USE);
20338 /* Output a description of the type of the object pointed to. */
20339 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20340 DINFO_USAGE_IND_USE);
20342 /* Now output a DIE to represent this pointer-to-data-member type
20343 itself. */
20344 gen_ptr_to_mbr_type_die (type, context_die);
20345 break;
20347 case FUNCTION_TYPE:
20348 /* Force out return type (in case it wasn't forced out already). */
20349 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20350 DINFO_USAGE_DIR_USE);
20351 gen_subroutine_type_die (type, context_die);
20352 break;
20354 case METHOD_TYPE:
20355 /* Force out return type (in case it wasn't forced out already). */
20356 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20357 DINFO_USAGE_DIR_USE);
20358 gen_subroutine_type_die (type, context_die);
20359 break;
20361 case ARRAY_TYPE:
20362 gen_array_type_die (type, context_die);
20363 break;
20365 case VECTOR_TYPE:
20366 gen_array_type_die (type, context_die);
20367 break;
20369 case ENUMERAL_TYPE:
20370 case RECORD_TYPE:
20371 case UNION_TYPE:
20372 case QUAL_UNION_TYPE:
20373 gen_tagged_type_die (type, context_die, usage);
20374 return;
20376 case VOID_TYPE:
20377 case INTEGER_TYPE:
20378 case REAL_TYPE:
20379 case FIXED_POINT_TYPE:
20380 case COMPLEX_TYPE:
20381 case BOOLEAN_TYPE:
20382 case POINTER_BOUNDS_TYPE:
20383 /* No DIEs needed for fundamental types. */
20384 break;
20386 case NULLPTR_TYPE:
20387 case LANG_TYPE:
20388 /* Just use DW_TAG_unspecified_type. */
20390 dw_die_ref type_die = lookup_type_die (type);
20391 if (type_die == NULL)
20393 tree name = TYPE_IDENTIFIER (type);
20394 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20395 type);
20396 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20397 equate_type_number_to_die (type, type_die);
20400 break;
20402 default:
20403 if (is_cxx_auto (type))
20405 tree name = TYPE_IDENTIFIER (type);
20406 dw_die_ref *die = (name == get_identifier ("auto")
20407 ? &auto_die : &decltype_auto_die);
20408 if (!*die)
20410 *die = new_die (DW_TAG_unspecified_type,
20411 comp_unit_die (), NULL_TREE);
20412 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20414 equate_type_number_to_die (type, *die);
20415 break;
20417 gcc_unreachable ();
20420 TREE_ASM_WRITTEN (type) = 1;
20423 static void
20424 gen_type_die (tree type, dw_die_ref context_die)
20426 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20429 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20430 things which are local to the given block. */
20432 static void
20433 gen_block_die (tree stmt, dw_die_ref context_die)
20435 int must_output_die = 0;
20436 bool inlined_func;
20438 /* Ignore blocks that are NULL. */
20439 if (stmt == NULL_TREE)
20440 return;
20442 inlined_func = inlined_function_outer_scope_p (stmt);
20444 /* If the block is one fragment of a non-contiguous block, do not
20445 process the variables, since they will have been done by the
20446 origin block. Do process subblocks. */
20447 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20449 tree sub;
20451 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20452 gen_block_die (sub, context_die);
20454 return;
20457 /* Determine if we need to output any Dwarf DIEs at all to represent this
20458 block. */
20459 if (inlined_func)
20460 /* The outer scopes for inlinings *must* always be represented. We
20461 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20462 must_output_die = 1;
20463 else
20465 /* Determine if this block directly contains any "significant"
20466 local declarations which we will need to output DIEs for. */
20467 if (debug_info_level > DINFO_LEVEL_TERSE)
20468 /* We are not in terse mode so *any* local declaration counts
20469 as being a "significant" one. */
20470 must_output_die = ((BLOCK_VARS (stmt) != NULL
20471 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20472 && (TREE_USED (stmt)
20473 || TREE_ASM_WRITTEN (stmt)
20474 || BLOCK_ABSTRACT (stmt)));
20475 else if ((TREE_USED (stmt)
20476 || TREE_ASM_WRITTEN (stmt)
20477 || BLOCK_ABSTRACT (stmt))
20478 && !dwarf2out_ignore_block (stmt))
20479 must_output_die = 1;
20482 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20483 DIE for any block which contains no significant local declarations at
20484 all. Rather, in such cases we just call `decls_for_scope' so that any
20485 needed Dwarf info for any sub-blocks will get properly generated. Note
20486 that in terse mode, our definition of what constitutes a "significant"
20487 local declaration gets restricted to include only inlined function
20488 instances and local (nested) function definitions. */
20489 if (must_output_die)
20491 if (inlined_func)
20493 /* If STMT block is abstract, that means we have been called
20494 indirectly from dwarf2out_abstract_function.
20495 That function rightfully marks the descendent blocks (of
20496 the abstract function it is dealing with) as being abstract,
20497 precisely to prevent us from emitting any
20498 DW_TAG_inlined_subroutine DIE as a descendent
20499 of an abstract function instance. So in that case, we should
20500 not call gen_inlined_subroutine_die.
20502 Later though, when cgraph asks dwarf2out to emit info
20503 for the concrete instance of the function decl into which
20504 the concrete instance of STMT got inlined, the later will lead
20505 to the generation of a DW_TAG_inlined_subroutine DIE. */
20506 if (! BLOCK_ABSTRACT (stmt))
20507 gen_inlined_subroutine_die (stmt, context_die);
20509 else
20510 gen_lexical_block_die (stmt, context_die);
20512 else
20513 decls_for_scope (stmt, context_die);
20516 /* Process variable DECL (or variable with origin ORIGIN) within
20517 block STMT and add it to CONTEXT_DIE. */
20518 static void
20519 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20521 dw_die_ref die;
20522 tree decl_or_origin = decl ? decl : origin;
20524 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20525 die = lookup_decl_die (decl_or_origin);
20526 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20527 && TYPE_DECL_IS_STUB (decl_or_origin))
20528 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20529 else
20530 die = NULL;
20532 if (die != NULL && die->die_parent == NULL)
20533 add_child_die (context_die, die);
20534 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20535 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20536 stmt, context_die);
20537 else
20538 gen_decl_die (decl, origin, context_die);
20541 /* Generate all of the decls declared within a given scope and (recursively)
20542 all of its sub-blocks. */
20544 static void
20545 decls_for_scope (tree stmt, dw_die_ref context_die)
20547 tree decl;
20548 unsigned int i;
20549 tree subblocks;
20551 /* Ignore NULL blocks. */
20552 if (stmt == NULL_TREE)
20553 return;
20555 /* Output the DIEs to represent all of the data objects and typedefs
20556 declared directly within this block but not within any nested
20557 sub-blocks. Also, nested function and tag DIEs have been
20558 generated with a parent of NULL; fix that up now. We don't
20559 have to do this if we're at -g1. */
20560 if (debug_info_level > DINFO_LEVEL_TERSE)
20562 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20563 process_scope_var (stmt, decl, NULL_TREE, context_die);
20564 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20565 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20566 context_die);
20569 /* Even if we're at -g1, we need to process the subblocks in order to get
20570 inlined call information. */
20572 /* Output the DIEs to represent all sub-blocks (and the items declared
20573 therein) of this block. */
20574 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20575 subblocks != NULL;
20576 subblocks = BLOCK_CHAIN (subblocks))
20577 gen_block_die (subblocks, context_die);
20580 /* Is this a typedef we can avoid emitting? */
20582 static inline int
20583 is_redundant_typedef (const_tree decl)
20585 if (TYPE_DECL_IS_STUB (decl))
20586 return 1;
20588 if (DECL_ARTIFICIAL (decl)
20589 && DECL_CONTEXT (decl)
20590 && is_tagged_type (DECL_CONTEXT (decl))
20591 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20592 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20593 /* Also ignore the artificial member typedef for the class name. */
20594 return 1;
20596 return 0;
20599 /* Return TRUE if TYPE is a typedef that names a type for linkage
20600 purposes. This kind of typedefs is produced by the C++ FE for
20601 constructs like:
20603 typedef struct {...} foo;
20605 In that case, there is no typedef variant type produced for foo.
20606 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20607 struct type. */
20609 static bool
20610 is_naming_typedef_decl (const_tree decl)
20612 if (decl == NULL_TREE
20613 || TREE_CODE (decl) != TYPE_DECL
20614 || !is_tagged_type (TREE_TYPE (decl))
20615 || DECL_IS_BUILTIN (decl)
20616 || is_redundant_typedef (decl)
20617 /* It looks like Ada produces TYPE_DECLs that are very similar
20618 to C++ naming typedefs but that have different
20619 semantics. Let's be specific to c++ for now. */
20620 || !is_cxx ())
20621 return FALSE;
20623 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20624 && TYPE_NAME (TREE_TYPE (decl)) == decl
20625 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20626 != TYPE_NAME (TREE_TYPE (decl))));
20629 /* Returns the DIE for a context. */
20631 static inline dw_die_ref
20632 get_context_die (tree context)
20634 if (context)
20636 /* Find die that represents this context. */
20637 if (TYPE_P (context))
20639 context = TYPE_MAIN_VARIANT (context);
20640 return strip_naming_typedef (context, force_type_die (context));
20642 else
20643 return force_decl_die (context);
20645 return comp_unit_die ();
20648 /* Returns the DIE for decl. A DIE will always be returned. */
20650 static dw_die_ref
20651 force_decl_die (tree decl)
20653 dw_die_ref decl_die;
20654 unsigned saved_external_flag;
20655 tree save_fn = NULL_TREE;
20656 decl_die = lookup_decl_die (decl);
20657 if (!decl_die)
20659 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20661 decl_die = lookup_decl_die (decl);
20662 if (decl_die)
20663 return decl_die;
20665 switch (TREE_CODE (decl))
20667 case FUNCTION_DECL:
20668 /* Clear current_function_decl, so that gen_subprogram_die thinks
20669 that this is a declaration. At this point, we just want to force
20670 declaration die. */
20671 save_fn = current_function_decl;
20672 current_function_decl = NULL_TREE;
20673 gen_subprogram_die (decl, context_die);
20674 current_function_decl = save_fn;
20675 break;
20677 case VAR_DECL:
20678 /* Set external flag to force declaration die. Restore it after
20679 gen_decl_die() call. */
20680 saved_external_flag = DECL_EXTERNAL (decl);
20681 DECL_EXTERNAL (decl) = 1;
20682 gen_decl_die (decl, NULL, context_die);
20683 DECL_EXTERNAL (decl) = saved_external_flag;
20684 break;
20686 case NAMESPACE_DECL:
20687 if (dwarf_version >= 3 || !dwarf_strict)
20688 dwarf2out_decl (decl);
20689 else
20690 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20691 decl_die = comp_unit_die ();
20692 break;
20694 case TRANSLATION_UNIT_DECL:
20695 decl_die = comp_unit_die ();
20696 break;
20698 default:
20699 gcc_unreachable ();
20702 /* We should be able to find the DIE now. */
20703 if (!decl_die)
20704 decl_die = lookup_decl_die (decl);
20705 gcc_assert (decl_die);
20708 return decl_die;
20711 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20712 always returned. */
20714 static dw_die_ref
20715 force_type_die (tree type)
20717 dw_die_ref type_die;
20719 type_die = lookup_type_die (type);
20720 if (!type_die)
20722 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20724 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20725 context_die);
20726 gcc_assert (type_die);
20728 return type_die;
20731 /* Force out any required namespaces to be able to output DECL,
20732 and return the new context_die for it, if it's changed. */
20734 static dw_die_ref
20735 setup_namespace_context (tree thing, dw_die_ref context_die)
20737 tree context = (DECL_P (thing)
20738 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20739 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20740 /* Force out the namespace. */
20741 context_die = force_decl_die (context);
20743 return context_die;
20746 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20747 type) within its namespace, if appropriate.
20749 For compatibility with older debuggers, namespace DIEs only contain
20750 declarations; all definitions are emitted at CU scope. */
20752 static dw_die_ref
20753 declare_in_namespace (tree thing, dw_die_ref context_die)
20755 dw_die_ref ns_context;
20757 if (debug_info_level <= DINFO_LEVEL_TERSE)
20758 return context_die;
20760 /* External declarations in the local scope only need to be emitted
20761 once, not once in the namespace and once in the scope.
20763 This avoids declaring the `extern' below in the
20764 namespace DIE as well as in the innermost scope:
20766 namespace S
20768 int i=5;
20769 int foo()
20771 int i=8;
20772 extern int i;
20773 return i;
20777 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
20778 return context_die;
20780 /* If this decl is from an inlined function, then don't try to emit it in its
20781 namespace, as we will get confused. It would have already been emitted
20782 when the abstract instance of the inline function was emitted anyways. */
20783 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20784 return context_die;
20786 ns_context = setup_namespace_context (thing, context_die);
20788 if (ns_context != context_die)
20790 if (is_fortran ())
20791 return ns_context;
20792 if (DECL_P (thing))
20793 gen_decl_die (thing, NULL, ns_context);
20794 else
20795 gen_type_die (thing, ns_context);
20797 return context_die;
20800 /* Generate a DIE for a namespace or namespace alias. */
20802 static void
20803 gen_namespace_die (tree decl, dw_die_ref context_die)
20805 dw_die_ref namespace_die;
20807 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20808 they are an alias of. */
20809 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20811 /* Output a real namespace or module. */
20812 context_die = setup_namespace_context (decl, comp_unit_die ());
20813 namespace_die = new_die (is_fortran ()
20814 ? DW_TAG_module : DW_TAG_namespace,
20815 context_die, decl);
20816 /* For Fortran modules defined in different CU don't add src coords. */
20817 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20819 const char *name = dwarf2_name (decl, 0);
20820 if (name)
20821 add_name_attribute (namespace_die, name);
20823 else
20824 add_name_and_src_coords_attributes (namespace_die, decl);
20825 if (DECL_EXTERNAL (decl))
20826 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20827 equate_decl_number_to_die (decl, namespace_die);
20829 else
20831 /* Output a namespace alias. */
20833 /* Force out the namespace we are an alias of, if necessary. */
20834 dw_die_ref origin_die
20835 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20837 if (DECL_FILE_SCOPE_P (decl)
20838 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20839 context_die = setup_namespace_context (decl, comp_unit_die ());
20840 /* Now create the namespace alias DIE. */
20841 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20842 add_name_and_src_coords_attributes (namespace_die, decl);
20843 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20844 equate_decl_number_to_die (decl, namespace_die);
20846 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20847 if (want_pubnames ())
20848 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20851 /* Generate Dwarf debug information for a decl described by DECL.
20852 The return value is currently only meaningful for PARM_DECLs,
20853 for all other decls it returns NULL. */
20855 static dw_die_ref
20856 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20858 tree decl_or_origin = decl ? decl : origin;
20859 tree class_origin = NULL, ultimate_origin;
20861 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20862 return NULL;
20864 /* Ignore pointer bounds decls. */
20865 if (DECL_P (decl_or_origin)
20866 && TREE_TYPE (decl_or_origin)
20867 && POINTER_BOUNDS_P (decl_or_origin))
20868 return NULL;
20870 switch (TREE_CODE (decl_or_origin))
20872 case ERROR_MARK:
20873 break;
20875 case CONST_DECL:
20876 if (!is_fortran () && !is_ada ())
20878 /* The individual enumerators of an enum type get output when we output
20879 the Dwarf representation of the relevant enum type itself. */
20880 break;
20883 /* Emit its type. */
20884 gen_type_die (TREE_TYPE (decl), context_die);
20886 /* And its containing namespace. */
20887 context_die = declare_in_namespace (decl, context_die);
20889 gen_const_die (decl, context_die);
20890 break;
20892 case FUNCTION_DECL:
20893 /* Don't output any DIEs to represent mere function declarations,
20894 unless they are class members or explicit block externs. */
20895 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20896 && DECL_FILE_SCOPE_P (decl_or_origin)
20897 && (current_function_decl == NULL_TREE
20898 || DECL_ARTIFICIAL (decl_or_origin)))
20899 break;
20901 #if 0
20902 /* FIXME */
20903 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20904 on local redeclarations of global functions. That seems broken. */
20905 if (current_function_decl != decl)
20906 /* This is only a declaration. */;
20907 #endif
20909 /* If we're emitting a clone, emit info for the abstract instance. */
20910 if (origin || DECL_ORIGIN (decl) != decl)
20911 dwarf2out_abstract_function (origin
20912 ? DECL_ORIGIN (origin)
20913 : DECL_ABSTRACT_ORIGIN (decl));
20915 /* If we're emitting an out-of-line copy of an inline function,
20916 emit info for the abstract instance and set up to refer to it. */
20917 else if (cgraph_function_possibly_inlined_p (decl)
20918 && ! DECL_ABSTRACT_P (decl)
20919 && ! class_or_namespace_scope_p (context_die)
20920 /* dwarf2out_abstract_function won't emit a die if this is just
20921 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20922 that case, because that works only if we have a die. */
20923 && DECL_INITIAL (decl) != NULL_TREE)
20925 dwarf2out_abstract_function (decl);
20926 set_decl_origin_self (decl);
20929 /* Otherwise we're emitting the primary DIE for this decl. */
20930 else if (debug_info_level > DINFO_LEVEL_TERSE)
20932 /* Before we describe the FUNCTION_DECL itself, make sure that we
20933 have its containing type. */
20934 if (!origin)
20935 origin = decl_class_context (decl);
20936 if (origin != NULL_TREE)
20937 gen_type_die (origin, context_die);
20939 /* And its return type. */
20940 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20942 /* And its virtual context. */
20943 if (DECL_VINDEX (decl) != NULL_TREE)
20944 gen_type_die (DECL_CONTEXT (decl), context_die);
20946 /* Make sure we have a member DIE for decl. */
20947 if (origin != NULL_TREE)
20948 gen_type_die_for_member (origin, decl, context_die);
20950 /* And its containing namespace. */
20951 context_die = declare_in_namespace (decl, context_die);
20954 /* Now output a DIE to represent the function itself. */
20955 if (decl)
20956 gen_subprogram_die (decl, context_die);
20957 break;
20959 case TYPE_DECL:
20960 /* If we are in terse mode, don't generate any DIEs to represent any
20961 actual typedefs. */
20962 if (debug_info_level <= DINFO_LEVEL_TERSE)
20963 break;
20965 /* In the special case of a TYPE_DECL node representing the declaration
20966 of some type tag, if the given TYPE_DECL is marked as having been
20967 instantiated from some other (original) TYPE_DECL node (e.g. one which
20968 was generated within the original definition of an inline function) we
20969 used to generate a special (abbreviated) DW_TAG_structure_type,
20970 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20971 should be actually referencing those DIEs, as variable DIEs with that
20972 type would be emitted already in the abstract origin, so it was always
20973 removed during unused type prunning. Don't add anything in this
20974 case. */
20975 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20976 break;
20978 if (is_redundant_typedef (decl))
20979 gen_type_die (TREE_TYPE (decl), context_die);
20980 else
20981 /* Output a DIE to represent the typedef itself. */
20982 gen_typedef_die (decl, context_die);
20983 break;
20985 case LABEL_DECL:
20986 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20987 gen_label_die (decl, context_die);
20988 break;
20990 case VAR_DECL:
20991 case RESULT_DECL:
20992 /* If we are in terse mode, don't generate any DIEs to represent any
20993 variable declarations or definitions. */
20994 if (debug_info_level <= DINFO_LEVEL_TERSE)
20995 break;
20997 /* Output any DIEs that are needed to specify the type of this data
20998 object. */
20999 if (decl_by_reference_p (decl_or_origin))
21000 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21001 else
21002 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21004 /* And its containing type. */
21005 class_origin = decl_class_context (decl_or_origin);
21006 if (class_origin != NULL_TREE)
21007 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21009 /* And its containing namespace. */
21010 context_die = declare_in_namespace (decl_or_origin, context_die);
21012 /* Now output the DIE to represent the data object itself. This gets
21013 complicated because of the possibility that the VAR_DECL really
21014 represents an inlined instance of a formal parameter for an inline
21015 function. */
21016 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21017 if (ultimate_origin != NULL_TREE
21018 && TREE_CODE (ultimate_origin) == PARM_DECL)
21019 gen_formal_parameter_die (decl, origin,
21020 true /* Emit name attribute. */,
21021 context_die);
21022 else
21023 gen_variable_die (decl, origin, context_die);
21024 break;
21026 case FIELD_DECL:
21027 /* Ignore the nameless fields that are used to skip bits but handle C++
21028 anonymous unions and structs. */
21029 if (DECL_NAME (decl) != NULL_TREE
21030 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21031 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21033 gen_type_die (member_declared_type (decl), context_die);
21034 gen_field_die (decl, context_die);
21036 break;
21038 case PARM_DECL:
21039 if (DECL_BY_REFERENCE (decl_or_origin))
21040 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21041 else
21042 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21043 return gen_formal_parameter_die (decl, origin,
21044 true /* Emit name attribute. */,
21045 context_die);
21047 case NAMESPACE_DECL:
21048 case IMPORTED_DECL:
21049 if (dwarf_version >= 3 || !dwarf_strict)
21050 gen_namespace_die (decl, context_die);
21051 break;
21053 case NAMELIST_DECL:
21054 gen_namelist_decl (DECL_NAME (decl), context_die,
21055 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21056 break;
21058 default:
21059 /* Probably some frontend-internal decl. Assume we don't care. */
21060 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21061 break;
21064 return NULL;
21067 /* Output debug information for global decl DECL. Called from toplev.c after
21068 compilation proper has finished. */
21070 static void
21071 dwarf2out_global_decl (tree decl)
21073 /* Output DWARF2 information for file-scope tentative data object
21074 declarations, file-scope (extern) function declarations (which
21075 had no corresponding body) and file-scope tagged type declarations
21076 and definitions which have not yet been forced out. */
21077 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21078 && !POINTER_BOUNDS_P (decl))
21079 dwarf2out_decl (decl);
21082 /* Output debug information for type decl DECL. Called from toplev.c
21083 and from language front ends (to record built-in types). */
21084 static void
21085 dwarf2out_type_decl (tree decl, int local)
21087 if (!local)
21088 dwarf2out_decl (decl);
21091 /* Output debug information for imported module or decl DECL.
21092 NAME is non-NULL name in the lexical block if the decl has been renamed.
21093 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21094 that DECL belongs to.
21095 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21096 static void
21097 dwarf2out_imported_module_or_decl_1 (tree decl,
21098 tree name,
21099 tree lexical_block,
21100 dw_die_ref lexical_block_die)
21102 expanded_location xloc;
21103 dw_die_ref imported_die = NULL;
21104 dw_die_ref at_import_die;
21106 if (TREE_CODE (decl) == IMPORTED_DECL)
21108 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21109 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21110 gcc_assert (decl);
21112 else
21113 xloc = expand_location (input_location);
21115 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21117 at_import_die = force_type_die (TREE_TYPE (decl));
21118 /* For namespace N { typedef void T; } using N::T; base_type_die
21119 returns NULL, but DW_TAG_imported_declaration requires
21120 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21121 if (!at_import_die)
21123 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21124 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21125 at_import_die = lookup_type_die (TREE_TYPE (decl));
21126 gcc_assert (at_import_die);
21129 else
21131 at_import_die = lookup_decl_die (decl);
21132 if (!at_import_die)
21134 /* If we're trying to avoid duplicate debug info, we may not have
21135 emitted the member decl for this field. Emit it now. */
21136 if (TREE_CODE (decl) == FIELD_DECL)
21138 tree type = DECL_CONTEXT (decl);
21140 if (TYPE_CONTEXT (type)
21141 && TYPE_P (TYPE_CONTEXT (type))
21142 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21143 DINFO_USAGE_DIR_USE))
21144 return;
21145 gen_type_die_for_member (type, decl,
21146 get_context_die (TYPE_CONTEXT (type)));
21148 if (TREE_CODE (decl) == NAMELIST_DECL)
21149 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21150 get_context_die (DECL_CONTEXT (decl)),
21151 NULL_TREE);
21152 else
21153 at_import_die = force_decl_die (decl);
21157 if (TREE_CODE (decl) == NAMESPACE_DECL)
21159 if (dwarf_version >= 3 || !dwarf_strict)
21160 imported_die = new_die (DW_TAG_imported_module,
21161 lexical_block_die,
21162 lexical_block);
21163 else
21164 return;
21166 else
21167 imported_die = new_die (DW_TAG_imported_declaration,
21168 lexical_block_die,
21169 lexical_block);
21171 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21172 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21173 if (name)
21174 add_AT_string (imported_die, DW_AT_name,
21175 IDENTIFIER_POINTER (name));
21176 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21179 /* Output debug information for imported module or decl DECL.
21180 NAME is non-NULL name in context if the decl has been renamed.
21181 CHILD is true if decl is one of the renamed decls as part of
21182 importing whole module. */
21184 static void
21185 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21186 bool child)
21188 /* dw_die_ref at_import_die; */
21189 dw_die_ref scope_die;
21191 if (debug_info_level <= DINFO_LEVEL_TERSE)
21192 return;
21194 gcc_assert (decl);
21196 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21197 We need decl DIE for reference and scope die. First, get DIE for the decl
21198 itself. */
21200 /* Get the scope die for decl context. Use comp_unit_die for global module
21201 or decl. If die is not found for non globals, force new die. */
21202 if (context
21203 && TYPE_P (context)
21204 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21205 return;
21207 if (!(dwarf_version >= 3 || !dwarf_strict))
21208 return;
21210 scope_die = get_context_die (context);
21212 if (child)
21214 gcc_assert (scope_die->die_child);
21215 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21216 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21217 scope_die = scope_die->die_child;
21220 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21221 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21225 /* Output debug information for namelists. */
21227 static dw_die_ref
21228 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21230 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21231 tree value;
21232 unsigned i;
21234 if (debug_info_level <= DINFO_LEVEL_TERSE)
21235 return NULL;
21237 gcc_assert (scope_die != NULL);
21238 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21239 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21241 /* If there are no item_decls, we have a nondefining namelist, e.g.
21242 with USE association; hence, set DW_AT_declaration. */
21243 if (item_decls == NULL_TREE)
21245 add_AT_flag (nml_die, DW_AT_declaration, 1);
21246 return nml_die;
21249 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21251 nml_item_ref_die = lookup_decl_die (value);
21252 if (!nml_item_ref_die)
21253 nml_item_ref_die = force_decl_die (value);
21255 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21256 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21258 return nml_die;
21262 /* Write the debugging output for DECL. */
21264 static void
21265 dwarf2out_decl (tree decl)
21267 dw_die_ref context_die = comp_unit_die ();
21269 switch (TREE_CODE (decl))
21271 case ERROR_MARK:
21272 return;
21274 case FUNCTION_DECL:
21275 /* What we would really like to do here is to filter out all mere
21276 file-scope declarations of file-scope functions which are never
21277 referenced later within this translation unit (and keep all of ones
21278 that *are* referenced later on) but we aren't clairvoyant, so we have
21279 no idea which functions will be referenced in the future (i.e. later
21280 on within the current translation unit). So here we just ignore all
21281 file-scope function declarations which are not also definitions. If
21282 and when the debugger needs to know something about these functions,
21283 it will have to hunt around and find the DWARF information associated
21284 with the definition of the function.
21286 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21287 nodes represent definitions and which ones represent mere
21288 declarations. We have to check DECL_INITIAL instead. That's because
21289 the C front-end supports some weird semantics for "extern inline"
21290 function definitions. These can get inlined within the current
21291 translation unit (and thus, we need to generate Dwarf info for their
21292 abstract instances so that the Dwarf info for the concrete inlined
21293 instances can have something to refer to) but the compiler never
21294 generates any out-of-lines instances of such things (despite the fact
21295 that they *are* definitions).
21297 The important point is that the C front-end marks these "extern
21298 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21299 them anyway. Note that the C++ front-end also plays some similar games
21300 for inline function definitions appearing within include files which
21301 also contain `#pragma interface' pragmas.
21303 If we are called from dwarf2out_abstract_function output a DIE
21304 anyway. We can end up here this way with early inlining and LTO
21305 where the inlined function is output in a different LTRANS unit
21306 or not at all. */
21307 if (DECL_INITIAL (decl) == NULL_TREE
21308 && ! DECL_ABSTRACT_P (decl))
21309 return;
21311 /* If we're a nested function, initially use a parent of NULL; if we're
21312 a plain function, this will be fixed up in decls_for_scope. If
21313 we're a method, it will be ignored, since we already have a DIE. */
21314 if (decl_function_context (decl)
21315 /* But if we're in terse mode, we don't care about scope. */
21316 && debug_info_level > DINFO_LEVEL_TERSE)
21317 context_die = NULL;
21318 break;
21320 case VAR_DECL:
21321 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21322 declaration and if the declaration was never even referenced from
21323 within this entire compilation unit. We suppress these DIEs in
21324 order to save space in the .debug section (by eliminating entries
21325 which are probably useless). Note that we must not suppress
21326 block-local extern declarations (whether used or not) because that
21327 would screw-up the debugger's name lookup mechanism and cause it to
21328 miss things which really ought to be in scope at a given point. */
21329 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21330 return;
21332 /* For local statics lookup proper context die. */
21333 if (TREE_STATIC (decl)
21334 && DECL_CONTEXT (decl)
21335 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21336 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21338 /* If we are in terse mode, don't generate any DIEs to represent any
21339 variable declarations or definitions. */
21340 if (debug_info_level <= DINFO_LEVEL_TERSE)
21341 return;
21342 break;
21344 case CONST_DECL:
21345 if (debug_info_level <= DINFO_LEVEL_TERSE)
21346 return;
21347 if (!is_fortran () && !is_ada ())
21348 return;
21349 if (TREE_STATIC (decl) && decl_function_context (decl))
21350 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21351 break;
21353 case NAMESPACE_DECL:
21354 case IMPORTED_DECL:
21355 if (debug_info_level <= DINFO_LEVEL_TERSE)
21356 return;
21357 if (lookup_decl_die (decl) != NULL)
21358 return;
21359 break;
21361 case TYPE_DECL:
21362 /* Don't emit stubs for types unless they are needed by other DIEs. */
21363 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21364 return;
21366 /* Don't bother trying to generate any DIEs to represent any of the
21367 normal built-in types for the language we are compiling. */
21368 if (DECL_IS_BUILTIN (decl))
21369 return;
21371 /* If we are in terse mode, don't generate any DIEs for types. */
21372 if (debug_info_level <= DINFO_LEVEL_TERSE)
21373 return;
21375 /* If we're a function-scope tag, initially use a parent of NULL;
21376 this will be fixed up in decls_for_scope. */
21377 if (decl_function_context (decl))
21378 context_die = NULL;
21380 break;
21382 case NAMELIST_DECL:
21383 break;
21385 default:
21386 return;
21389 gen_decl_die (decl, NULL, context_die);
21392 /* Write the debugging output for DECL. */
21394 static void
21395 dwarf2out_function_decl (tree decl)
21397 dwarf2out_decl (decl);
21398 call_arg_locations = NULL;
21399 call_arg_loc_last = NULL;
21400 call_site_count = -1;
21401 tail_call_site_count = -1;
21402 block_map.release ();
21403 decl_loc_table->empty ();
21404 cached_dw_loc_list_table->empty ();
21407 /* Output a marker (i.e. a label) for the beginning of the generated code for
21408 a lexical block. */
21410 static void
21411 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21412 unsigned int blocknum)
21414 switch_to_section (current_function_section ());
21415 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21418 /* Output a marker (i.e. a label) for the end of the generated code for a
21419 lexical block. */
21421 static void
21422 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21424 switch_to_section (current_function_section ());
21425 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21428 /* Returns nonzero if it is appropriate not to emit any debugging
21429 information for BLOCK, because it doesn't contain any instructions.
21431 Don't allow this for blocks with nested functions or local classes
21432 as we would end up with orphans, and in the presence of scheduling
21433 we may end up calling them anyway. */
21435 static bool
21436 dwarf2out_ignore_block (const_tree block)
21438 tree decl;
21439 unsigned int i;
21441 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21442 if (TREE_CODE (decl) == FUNCTION_DECL
21443 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21444 return 0;
21445 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21447 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21448 if (TREE_CODE (decl) == FUNCTION_DECL
21449 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21450 return 0;
21453 return 1;
21456 /* Hash table routines for file_hash. */
21458 bool
21459 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21461 return filename_cmp (p1->filename, p2) == 0;
21464 hashval_t
21465 dwarf_file_hasher::hash (dwarf_file_data *p)
21467 return htab_hash_string (p->filename);
21470 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21471 dwarf2out.c) and return its "index". The index of each (known) filename is
21472 just a unique number which is associated with only that one filename. We
21473 need such numbers for the sake of generating labels (in the .debug_sfnames
21474 section) and references to those files numbers (in the .debug_srcinfo
21475 and.debug_macinfo sections). If the filename given as an argument is not
21476 found in our current list, add it to the list and assign it the next
21477 available unique index number. In order to speed up searches, we remember
21478 the index of the filename was looked up last. This handles the majority of
21479 all searches. */
21481 static struct dwarf_file_data *
21482 lookup_filename (const char *file_name)
21484 struct dwarf_file_data * created;
21486 /* Check to see if the file name that was searched on the previous
21487 call matches this file name. If so, return the index. */
21488 if (file_table_last_lookup
21489 && (file_name == file_table_last_lookup->filename
21490 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21491 return file_table_last_lookup;
21493 /* Didn't match the previous lookup, search the table. */
21494 dwarf_file_data **slot
21495 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21496 INSERT);
21497 if (*slot)
21498 return *slot;
21500 created = ggc_alloc<dwarf_file_data> ();
21501 created->filename = file_name;
21502 created->emitted_number = 0;
21503 *slot = created;
21504 return created;
21507 /* If the assembler will construct the file table, then translate the compiler
21508 internal file table number into the assembler file table number, and emit
21509 a .file directive if we haven't already emitted one yet. The file table
21510 numbers are different because we prune debug info for unused variables and
21511 types, which may include filenames. */
21513 static int
21514 maybe_emit_file (struct dwarf_file_data * fd)
21516 if (! fd->emitted_number)
21518 if (last_emitted_file)
21519 fd->emitted_number = last_emitted_file->emitted_number + 1;
21520 else
21521 fd->emitted_number = 1;
21522 last_emitted_file = fd;
21524 if (DWARF2_ASM_LINE_DEBUG_INFO)
21526 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21527 output_quoted_string (asm_out_file,
21528 remap_debug_filename (fd->filename));
21529 fputc ('\n', asm_out_file);
21533 return fd->emitted_number;
21536 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21537 That generation should happen after function debug info has been
21538 generated. The value of the attribute is the constant value of ARG. */
21540 static void
21541 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21543 die_arg_entry entry;
21545 if (!die || !arg)
21546 return;
21548 if (!tmpl_value_parm_die_table)
21549 vec_alloc (tmpl_value_parm_die_table, 32);
21551 entry.die = die;
21552 entry.arg = arg;
21553 vec_safe_push (tmpl_value_parm_die_table, entry);
21556 /* Return TRUE if T is an instance of generic type, FALSE
21557 otherwise. */
21559 static bool
21560 generic_type_p (tree t)
21562 if (t == NULL_TREE || !TYPE_P (t))
21563 return false;
21564 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21567 /* Schedule the generation of the generic parameter dies for the
21568 instance of generic type T. The proper generation itself is later
21569 done by gen_scheduled_generic_parms_dies. */
21571 static void
21572 schedule_generic_params_dies_gen (tree t)
21574 if (!generic_type_p (t))
21575 return;
21577 if (!generic_type_instances)
21578 vec_alloc (generic_type_instances, 256);
21580 vec_safe_push (generic_type_instances, t);
21583 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21584 by append_entry_to_tmpl_value_parm_die_table. This function must
21585 be called after function DIEs have been generated. */
21587 static void
21588 gen_remaining_tmpl_value_param_die_attribute (void)
21590 if (tmpl_value_parm_die_table)
21592 unsigned i;
21593 die_arg_entry *e;
21595 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21596 tree_add_const_value_attribute (e->die, e->arg);
21600 /* Generate generic parameters DIEs for instances of generic types
21601 that have been previously scheduled by
21602 schedule_generic_params_dies_gen. This function must be called
21603 after all the types of the CU have been laid out. */
21605 static void
21606 gen_scheduled_generic_parms_dies (void)
21608 unsigned i;
21609 tree t;
21611 if (!generic_type_instances)
21612 return;
21614 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21615 if (COMPLETE_TYPE_P (t))
21616 gen_generic_params_dies (t);
21620 /* Replace DW_AT_name for the decl with name. */
21622 static void
21623 dwarf2out_set_name (tree decl, tree name)
21625 dw_die_ref die;
21626 dw_attr_ref attr;
21627 const char *dname;
21629 die = TYPE_SYMTAB_DIE (decl);
21630 if (!die)
21631 return;
21633 dname = dwarf2_name (name, 0);
21634 if (!dname)
21635 return;
21637 attr = get_AT (die, DW_AT_name);
21638 if (attr)
21640 struct indirect_string_node *node;
21642 node = find_AT_string (dname);
21643 /* replace the string. */
21644 attr->dw_attr_val.v.val_str = node;
21647 else
21648 add_name_attribute (die, dname);
21651 /* True if before or during processing of the first function being emitted. */
21652 static bool in_first_function_p = true;
21653 /* True if loc_note during dwarf2out_var_location call might still be
21654 before first real instruction at address equal to .Ltext0. */
21655 static bool maybe_at_text_label_p = true;
21656 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21657 static unsigned int first_loclabel_num_not_at_text_label;
21659 /* Called by the final INSN scan whenever we see a var location. We
21660 use it to drop labels in the right places, and throw the location in
21661 our lookup table. */
21663 static void
21664 dwarf2out_var_location (rtx_insn *loc_note)
21666 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21667 struct var_loc_node *newloc;
21668 rtx_insn *next_real, *next_note;
21669 static const char *last_label;
21670 static const char *last_postcall_label;
21671 static bool last_in_cold_section_p;
21672 static rtx_insn *expected_next_loc_note;
21673 tree decl;
21674 bool var_loc_p;
21676 if (!NOTE_P (loc_note))
21678 if (CALL_P (loc_note))
21680 call_site_count++;
21681 if (SIBLING_CALL_P (loc_note))
21682 tail_call_site_count++;
21684 return;
21687 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21688 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21689 return;
21691 /* Optimize processing a large consecutive sequence of location
21692 notes so we don't spend too much time in next_real_insn. If the
21693 next insn is another location note, remember the next_real_insn
21694 calculation for next time. */
21695 next_real = cached_next_real_insn;
21696 if (next_real)
21698 if (expected_next_loc_note != loc_note)
21699 next_real = NULL;
21702 next_note = NEXT_INSN (loc_note);
21703 if (! next_note
21704 || next_note->deleted ()
21705 || ! NOTE_P (next_note)
21706 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21707 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21708 next_note = NULL;
21710 if (! next_real)
21711 next_real = next_real_insn (loc_note);
21713 if (next_note)
21715 expected_next_loc_note = next_note;
21716 cached_next_real_insn = next_real;
21718 else
21719 cached_next_real_insn = NULL;
21721 /* If there are no instructions which would be affected by this note,
21722 don't do anything. */
21723 if (var_loc_p
21724 && next_real == NULL_RTX
21725 && !NOTE_DURING_CALL_P (loc_note))
21726 return;
21728 if (next_real == NULL_RTX)
21729 next_real = get_last_insn ();
21731 /* If there were any real insns between note we processed last time
21732 and this note (or if it is the first note), clear
21733 last_{,postcall_}label so that they are not reused this time. */
21734 if (last_var_location_insn == NULL_RTX
21735 || last_var_location_insn != next_real
21736 || last_in_cold_section_p != in_cold_section_p)
21738 last_label = NULL;
21739 last_postcall_label = NULL;
21742 if (var_loc_p)
21744 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21745 newloc = add_var_loc_to_decl (decl, loc_note,
21746 NOTE_DURING_CALL_P (loc_note)
21747 ? last_postcall_label : last_label);
21748 if (newloc == NULL)
21749 return;
21751 else
21753 decl = NULL_TREE;
21754 newloc = NULL;
21757 /* If there were no real insns between note we processed last time
21758 and this note, use the label we emitted last time. Otherwise
21759 create a new label and emit it. */
21760 if (last_label == NULL)
21762 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21763 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21764 loclabel_num++;
21765 last_label = ggc_strdup (loclabel);
21766 /* See if loclabel might be equal to .Ltext0. If yes,
21767 bump first_loclabel_num_not_at_text_label. */
21768 if (!have_multiple_function_sections
21769 && in_first_function_p
21770 && maybe_at_text_label_p)
21772 static rtx_insn *last_start;
21773 rtx_insn *insn;
21774 for (insn = loc_note; insn; insn = previous_insn (insn))
21775 if (insn == last_start)
21776 break;
21777 else if (!NONDEBUG_INSN_P (insn))
21778 continue;
21779 else
21781 rtx body = PATTERN (insn);
21782 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21783 continue;
21784 /* Inline asm could occupy zero bytes. */
21785 else if (GET_CODE (body) == ASM_INPUT
21786 || asm_noperands (body) >= 0)
21787 continue;
21788 #ifdef HAVE_attr_length
21789 else if (get_attr_min_length (insn) == 0)
21790 continue;
21791 #endif
21792 else
21794 /* Assume insn has non-zero length. */
21795 maybe_at_text_label_p = false;
21796 break;
21799 if (maybe_at_text_label_p)
21801 last_start = loc_note;
21802 first_loclabel_num_not_at_text_label = loclabel_num;
21807 if (!var_loc_p)
21809 struct call_arg_loc_node *ca_loc
21810 = ggc_cleared_alloc<call_arg_loc_node> ();
21811 rtx_insn *prev = prev_real_insn (loc_note);
21812 rtx x;
21813 ca_loc->call_arg_loc_note = loc_note;
21814 ca_loc->next = NULL;
21815 ca_loc->label = last_label;
21816 gcc_assert (prev
21817 && (CALL_P (prev)
21818 || (NONJUMP_INSN_P (prev)
21819 && GET_CODE (PATTERN (prev)) == SEQUENCE
21820 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21821 if (!CALL_P (prev))
21822 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21823 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21824 x = get_call_rtx_from (PATTERN (prev));
21825 if (x)
21827 x = XEXP (XEXP (x, 0), 0);
21828 if (GET_CODE (x) == SYMBOL_REF
21829 && SYMBOL_REF_DECL (x)
21830 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21831 ca_loc->symbol_ref = x;
21833 ca_loc->block = insn_scope (prev);
21834 if (call_arg_locations)
21835 call_arg_loc_last->next = ca_loc;
21836 else
21837 call_arg_locations = ca_loc;
21838 call_arg_loc_last = ca_loc;
21840 else if (!NOTE_DURING_CALL_P (loc_note))
21841 newloc->label = last_label;
21842 else
21844 if (!last_postcall_label)
21846 sprintf (loclabel, "%s-1", last_label);
21847 last_postcall_label = ggc_strdup (loclabel);
21849 newloc->label = last_postcall_label;
21852 last_var_location_insn = next_real;
21853 last_in_cold_section_p = in_cold_section_p;
21856 /* Note in one location list that text section has changed. */
21859 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21861 var_loc_list *list = *slot;
21862 if (list->first)
21863 list->last_before_switch
21864 = list->last->next ? list->last->next : list->last;
21865 return 1;
21868 /* Note in all location lists that text section has changed. */
21870 static void
21871 var_location_switch_text_section (void)
21873 if (decl_loc_table == NULL)
21874 return;
21876 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21879 /* Create a new line number table. */
21881 static dw_line_info_table *
21882 new_line_info_table (void)
21884 dw_line_info_table *table;
21886 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21887 table->file_num = 1;
21888 table->line_num = 1;
21889 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21891 return table;
21894 /* Lookup the "current" table into which we emit line info, so
21895 that we don't have to do it for every source line. */
21897 static void
21898 set_cur_line_info_table (section *sec)
21900 dw_line_info_table *table;
21902 if (sec == text_section)
21903 table = text_section_line_info;
21904 else if (sec == cold_text_section)
21906 table = cold_text_section_line_info;
21907 if (!table)
21909 cold_text_section_line_info = table = new_line_info_table ();
21910 table->end_label = cold_end_label;
21913 else
21915 const char *end_label;
21917 if (flag_reorder_blocks_and_partition)
21919 if (in_cold_section_p)
21920 end_label = crtl->subsections.cold_section_end_label;
21921 else
21922 end_label = crtl->subsections.hot_section_end_label;
21924 else
21926 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21927 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21928 current_function_funcdef_no);
21929 end_label = ggc_strdup (label);
21932 table = new_line_info_table ();
21933 table->end_label = end_label;
21935 vec_safe_push (separate_line_info, table);
21938 if (DWARF2_ASM_LINE_DEBUG_INFO)
21939 table->is_stmt = (cur_line_info_table
21940 ? cur_line_info_table->is_stmt
21941 : DWARF_LINE_DEFAULT_IS_STMT_START);
21942 cur_line_info_table = table;
21946 /* We need to reset the locations at the beginning of each
21947 function. We can't do this in the end_function hook, because the
21948 declarations that use the locations won't have been output when
21949 that hook is called. Also compute have_multiple_function_sections here. */
21951 static void
21952 dwarf2out_begin_function (tree fun)
21954 section *sec = function_section (fun);
21956 if (sec != text_section)
21957 have_multiple_function_sections = true;
21959 if (flag_reorder_blocks_and_partition && !cold_text_section)
21961 gcc_assert (current_function_decl == fun);
21962 cold_text_section = unlikely_text_section ();
21963 switch_to_section (cold_text_section);
21964 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21965 switch_to_section (sec);
21968 dwarf2out_note_section_used ();
21969 call_site_count = 0;
21970 tail_call_site_count = 0;
21972 set_cur_line_info_table (sec);
21975 /* Helper function of dwarf2out_end_function, called only after emitting
21976 the very first function into assembly. Check if some .debug_loc range
21977 might end with a .LVL* label that could be equal to .Ltext0.
21978 In that case we must force using absolute addresses in .debug_loc ranges,
21979 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21980 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21981 list terminator.
21982 Set have_multiple_function_sections to true in that case and
21983 terminate htab traversal. */
21986 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21988 var_loc_list *entry = *slot;
21989 struct var_loc_node *node;
21991 node = entry->first;
21992 if (node && node->next && node->next->label)
21994 unsigned int i;
21995 const char *label = node->next->label;
21996 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21998 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22000 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22001 if (strcmp (label, loclabel) == 0)
22003 have_multiple_function_sections = true;
22004 return 0;
22008 return 1;
22011 /* Hook called after emitting a function into assembly.
22012 This does something only for the very first function emitted. */
22014 static void
22015 dwarf2out_end_function (unsigned int)
22017 if (in_first_function_p
22018 && !have_multiple_function_sections
22019 && first_loclabel_num_not_at_text_label
22020 && decl_loc_table)
22021 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22022 in_first_function_p = false;
22023 maybe_at_text_label_p = false;
22026 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22028 static void
22029 push_dw_line_info_entry (dw_line_info_table *table,
22030 enum dw_line_info_opcode opcode, unsigned int val)
22032 dw_line_info_entry e;
22033 e.opcode = opcode;
22034 e.val = val;
22035 vec_safe_push (table->entries, e);
22038 /* Output a label to mark the beginning of a source code line entry
22039 and record information relating to this source line, in
22040 'line_info_table' for later output of the .debug_line section. */
22041 /* ??? The discriminator parameter ought to be unsigned. */
22043 static void
22044 dwarf2out_source_line (unsigned int line, const char *filename,
22045 int discriminator, bool is_stmt)
22047 unsigned int file_num;
22048 dw_line_info_table *table;
22050 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22051 return;
22053 /* The discriminator column was added in dwarf4. Simplify the below
22054 by simply removing it if we're not supposed to output it. */
22055 if (dwarf_version < 4 && dwarf_strict)
22056 discriminator = 0;
22058 table = cur_line_info_table;
22059 file_num = maybe_emit_file (lookup_filename (filename));
22061 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22062 the debugger has used the second (possibly duplicate) line number
22063 at the beginning of the function to mark the end of the prologue.
22064 We could eliminate any other duplicates within the function. For
22065 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22066 that second line number entry. */
22067 /* Recall that this end-of-prologue indication is *not* the same thing
22068 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22069 to which the hook corresponds, follows the last insn that was
22070 emitted by gen_prologue. What we need is to precede the first insn
22071 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22072 insn that corresponds to something the user wrote. These may be
22073 very different locations once scheduling is enabled. */
22075 if (0 && file_num == table->file_num
22076 && line == table->line_num
22077 && discriminator == table->discrim_num
22078 && is_stmt == table->is_stmt)
22079 return;
22081 switch_to_section (current_function_section ());
22083 /* If requested, emit something human-readable. */
22084 if (flag_debug_asm)
22085 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22087 if (DWARF2_ASM_LINE_DEBUG_INFO)
22089 /* Emit the .loc directive understood by GNU as. */
22090 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22091 file_num, line, is_stmt, discriminator */
22092 fputs ("\t.loc ", asm_out_file);
22093 fprint_ul (asm_out_file, file_num);
22094 putc (' ', asm_out_file);
22095 fprint_ul (asm_out_file, line);
22096 putc (' ', asm_out_file);
22097 putc ('0', asm_out_file);
22099 if (is_stmt != table->is_stmt)
22101 fputs (" is_stmt ", asm_out_file);
22102 putc (is_stmt ? '1' : '0', asm_out_file);
22104 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22106 gcc_assert (discriminator > 0);
22107 fputs (" discriminator ", asm_out_file);
22108 fprint_ul (asm_out_file, (unsigned long) discriminator);
22110 putc ('\n', asm_out_file);
22112 else
22114 unsigned int label_num = ++line_info_label_num;
22116 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22118 push_dw_line_info_entry (table, LI_set_address, label_num);
22119 if (file_num != table->file_num)
22120 push_dw_line_info_entry (table, LI_set_file, file_num);
22121 if (discriminator != table->discrim_num)
22122 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22123 if (is_stmt != table->is_stmt)
22124 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22125 push_dw_line_info_entry (table, LI_set_line, line);
22128 table->file_num = file_num;
22129 table->line_num = line;
22130 table->discrim_num = discriminator;
22131 table->is_stmt = is_stmt;
22132 table->in_use = true;
22135 /* Record the beginning of a new source file. */
22137 static void
22138 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22140 if (flag_eliminate_dwarf2_dups)
22142 /* Record the beginning of the file for break_out_includes. */
22143 dw_die_ref bincl_die;
22145 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22146 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22149 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22151 macinfo_entry e;
22152 e.code = DW_MACINFO_start_file;
22153 e.lineno = lineno;
22154 e.info = ggc_strdup (filename);
22155 vec_safe_push (macinfo_table, e);
22159 /* Record the end of a source file. */
22161 static void
22162 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22164 if (flag_eliminate_dwarf2_dups)
22165 /* Record the end of the file for break_out_includes. */
22166 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22168 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22170 macinfo_entry e;
22171 e.code = DW_MACINFO_end_file;
22172 e.lineno = lineno;
22173 e.info = NULL;
22174 vec_safe_push (macinfo_table, e);
22178 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22179 the tail part of the directive line, i.e. the part which is past the
22180 initial whitespace, #, whitespace, directive-name, whitespace part. */
22182 static void
22183 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22184 const char *buffer ATTRIBUTE_UNUSED)
22186 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22188 macinfo_entry e;
22189 /* Insert a dummy first entry to be able to optimize the whole
22190 predefined macro block using DW_MACRO_GNU_transparent_include. */
22191 if (macinfo_table->is_empty () && lineno <= 1)
22193 e.code = 0;
22194 e.lineno = 0;
22195 e.info = NULL;
22196 vec_safe_push (macinfo_table, e);
22198 e.code = DW_MACINFO_define;
22199 e.lineno = lineno;
22200 e.info = ggc_strdup (buffer);
22201 vec_safe_push (macinfo_table, e);
22205 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22206 the tail part of the directive line, i.e. the part which is past the
22207 initial whitespace, #, whitespace, directive-name, whitespace part. */
22209 static void
22210 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22211 const char *buffer ATTRIBUTE_UNUSED)
22213 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22215 macinfo_entry e;
22216 /* Insert a dummy first entry to be able to optimize the whole
22217 predefined macro block using DW_MACRO_GNU_transparent_include. */
22218 if (macinfo_table->is_empty () && lineno <= 1)
22220 e.code = 0;
22221 e.lineno = 0;
22222 e.info = NULL;
22223 vec_safe_push (macinfo_table, e);
22225 e.code = DW_MACINFO_undef;
22226 e.lineno = lineno;
22227 e.info = ggc_strdup (buffer);
22228 vec_safe_push (macinfo_table, e);
22232 /* Helpers to manipulate hash table of CUs. */
22234 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
22236 typedef macinfo_entry value_type;
22237 typedef macinfo_entry compare_type;
22238 static inline hashval_t hash (const value_type *);
22239 static inline bool equal (const value_type *, const compare_type *);
22242 inline hashval_t
22243 macinfo_entry_hasher::hash (const value_type *entry)
22245 return htab_hash_string (entry->info);
22248 inline bool
22249 macinfo_entry_hasher::equal (const value_type *entry1,
22250 const compare_type *entry2)
22252 return !strcmp (entry1->info, entry2->info);
22255 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22257 /* Output a single .debug_macinfo entry. */
22259 static void
22260 output_macinfo_op (macinfo_entry *ref)
22262 int file_num;
22263 size_t len;
22264 struct indirect_string_node *node;
22265 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22266 struct dwarf_file_data *fd;
22268 switch (ref->code)
22270 case DW_MACINFO_start_file:
22271 fd = lookup_filename (ref->info);
22272 file_num = maybe_emit_file (fd);
22273 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22274 dw2_asm_output_data_uleb128 (ref->lineno,
22275 "Included from line number %lu",
22276 (unsigned long) ref->lineno);
22277 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22278 break;
22279 case DW_MACINFO_end_file:
22280 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22281 break;
22282 case DW_MACINFO_define:
22283 case DW_MACINFO_undef:
22284 len = strlen (ref->info) + 1;
22285 if (!dwarf_strict
22286 && len > DWARF_OFFSET_SIZE
22287 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22288 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22290 ref->code = ref->code == DW_MACINFO_define
22291 ? DW_MACRO_GNU_define_indirect
22292 : DW_MACRO_GNU_undef_indirect;
22293 output_macinfo_op (ref);
22294 return;
22296 dw2_asm_output_data (1, ref->code,
22297 ref->code == DW_MACINFO_define
22298 ? "Define macro" : "Undefine macro");
22299 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22300 (unsigned long) ref->lineno);
22301 dw2_asm_output_nstring (ref->info, -1, "The macro");
22302 break;
22303 case DW_MACRO_GNU_define_indirect:
22304 case DW_MACRO_GNU_undef_indirect:
22305 node = find_AT_string (ref->info);
22306 gcc_assert (node
22307 && ((node->form == DW_FORM_strp)
22308 || (node->form == DW_FORM_GNU_str_index)));
22309 dw2_asm_output_data (1, ref->code,
22310 ref->code == DW_MACRO_GNU_define_indirect
22311 ? "Define macro indirect"
22312 : "Undefine macro indirect");
22313 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22314 (unsigned long) ref->lineno);
22315 if (node->form == DW_FORM_strp)
22316 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22317 debug_str_section, "The macro: \"%s\"",
22318 ref->info);
22319 else
22320 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22321 ref->info);
22322 break;
22323 case DW_MACRO_GNU_transparent_include:
22324 dw2_asm_output_data (1, ref->code, "Transparent include");
22325 ASM_GENERATE_INTERNAL_LABEL (label,
22326 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22327 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22328 break;
22329 default:
22330 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22331 ASM_COMMENT_START, (unsigned long) ref->code);
22332 break;
22336 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22337 other compilation unit .debug_macinfo sections. IDX is the first
22338 index of a define/undef, return the number of ops that should be
22339 emitted in a comdat .debug_macinfo section and emit
22340 a DW_MACRO_GNU_transparent_include entry referencing it.
22341 If the define/undef entry should be emitted normally, return 0. */
22343 static unsigned
22344 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22345 macinfo_hash_type **macinfo_htab)
22347 macinfo_entry *first, *second, *cur, *inc;
22348 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22349 unsigned char checksum[16];
22350 struct md5_ctx ctx;
22351 char *grp_name, *tail;
22352 const char *base;
22353 unsigned int i, count, encoded_filename_len, linebuf_len;
22354 macinfo_entry **slot;
22356 first = &(*macinfo_table)[idx];
22357 second = &(*macinfo_table)[idx + 1];
22359 /* Optimize only if there are at least two consecutive define/undef ops,
22360 and either all of them are before first DW_MACINFO_start_file
22361 with lineno {0,1} (i.e. predefined macro block), or all of them are
22362 in some included header file. */
22363 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22364 return 0;
22365 if (vec_safe_is_empty (files))
22367 if (first->lineno > 1 || second->lineno > 1)
22368 return 0;
22370 else if (first->lineno == 0)
22371 return 0;
22373 /* Find the last define/undef entry that can be grouped together
22374 with first and at the same time compute md5 checksum of their
22375 codes, linenumbers and strings. */
22376 md5_init_ctx (&ctx);
22377 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22378 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22379 break;
22380 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22381 break;
22382 else
22384 unsigned char code = cur->code;
22385 md5_process_bytes (&code, 1, &ctx);
22386 checksum_uleb128 (cur->lineno, &ctx);
22387 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22389 md5_finish_ctx (&ctx, checksum);
22390 count = i - idx;
22392 /* From the containing include filename (if any) pick up just
22393 usable characters from its basename. */
22394 if (vec_safe_is_empty (files))
22395 base = "";
22396 else
22397 base = lbasename (files->last ().info);
22398 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22399 if (ISIDNUM (base[i]) || base[i] == '.')
22400 encoded_filename_len++;
22401 /* Count . at the end. */
22402 if (encoded_filename_len)
22403 encoded_filename_len++;
22405 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22406 linebuf_len = strlen (linebuf);
22408 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22409 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22410 + 16 * 2 + 1);
22411 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22412 tail = grp_name + 4;
22413 if (encoded_filename_len)
22415 for (i = 0; base[i]; i++)
22416 if (ISIDNUM (base[i]) || base[i] == '.')
22417 *tail++ = base[i];
22418 *tail++ = '.';
22420 memcpy (tail, linebuf, linebuf_len);
22421 tail += linebuf_len;
22422 *tail++ = '.';
22423 for (i = 0; i < 16; i++)
22424 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22426 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22427 in the empty vector entry before the first define/undef. */
22428 inc = &(*macinfo_table)[idx - 1];
22429 inc->code = DW_MACRO_GNU_transparent_include;
22430 inc->lineno = 0;
22431 inc->info = ggc_strdup (grp_name);
22432 if (!*macinfo_htab)
22433 *macinfo_htab = new macinfo_hash_type (10);
22434 /* Avoid emitting duplicates. */
22435 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22436 if (*slot != NULL)
22438 inc->code = 0;
22439 inc->info = NULL;
22440 /* If such an entry has been used before, just emit
22441 a DW_MACRO_GNU_transparent_include op. */
22442 inc = *slot;
22443 output_macinfo_op (inc);
22444 /* And clear all macinfo_entry in the range to avoid emitting them
22445 in the second pass. */
22446 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22448 cur->code = 0;
22449 cur->info = NULL;
22452 else
22454 *slot = inc;
22455 inc->lineno = (*macinfo_htab)->elements ();
22456 output_macinfo_op (inc);
22458 return count;
22461 /* Save any strings needed by the macinfo table in the debug str
22462 table. All strings must be collected into the table by the time
22463 index_string is called. */
22465 static void
22466 save_macinfo_strings (void)
22468 unsigned len;
22469 unsigned i;
22470 macinfo_entry *ref;
22472 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22474 switch (ref->code)
22476 /* Match the logic in output_macinfo_op to decide on
22477 indirect strings. */
22478 case DW_MACINFO_define:
22479 case DW_MACINFO_undef:
22480 len = strlen (ref->info) + 1;
22481 if (!dwarf_strict
22482 && len > DWARF_OFFSET_SIZE
22483 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22484 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22485 set_indirect_string (find_AT_string (ref->info));
22486 break;
22487 case DW_MACRO_GNU_define_indirect:
22488 case DW_MACRO_GNU_undef_indirect:
22489 set_indirect_string (find_AT_string (ref->info));
22490 break;
22491 default:
22492 break;
22497 /* Output macinfo section(s). */
22499 static void
22500 output_macinfo (void)
22502 unsigned i;
22503 unsigned long length = vec_safe_length (macinfo_table);
22504 macinfo_entry *ref;
22505 vec<macinfo_entry, va_gc> *files = NULL;
22506 macinfo_hash_type *macinfo_htab = NULL;
22508 if (! length)
22509 return;
22511 /* output_macinfo* uses these interchangeably. */
22512 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22513 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22514 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22515 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22517 /* For .debug_macro emit the section header. */
22518 if (!dwarf_strict)
22520 dw2_asm_output_data (2, 4, "DWARF macro version number");
22521 if (DWARF_OFFSET_SIZE == 8)
22522 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22523 else
22524 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22525 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22526 (!dwarf_split_debug_info ? debug_line_section_label
22527 : debug_skeleton_line_section_label),
22528 debug_line_section, NULL);
22531 /* In the first loop, it emits the primary .debug_macinfo section
22532 and after each emitted op the macinfo_entry is cleared.
22533 If a longer range of define/undef ops can be optimized using
22534 DW_MACRO_GNU_transparent_include, the
22535 DW_MACRO_GNU_transparent_include op is emitted and kept in
22536 the vector before the first define/undef in the range and the
22537 whole range of define/undef ops is not emitted and kept. */
22538 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22540 switch (ref->code)
22542 case DW_MACINFO_start_file:
22543 vec_safe_push (files, *ref);
22544 break;
22545 case DW_MACINFO_end_file:
22546 if (!vec_safe_is_empty (files))
22547 files->pop ();
22548 break;
22549 case DW_MACINFO_define:
22550 case DW_MACINFO_undef:
22551 if (!dwarf_strict
22552 && HAVE_COMDAT_GROUP
22553 && vec_safe_length (files) != 1
22554 && i > 0
22555 && i + 1 < length
22556 && (*macinfo_table)[i - 1].code == 0)
22558 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22559 if (count)
22561 i += count - 1;
22562 continue;
22565 break;
22566 case 0:
22567 /* A dummy entry may be inserted at the beginning to be able
22568 to optimize the whole block of predefined macros. */
22569 if (i == 0)
22570 continue;
22571 default:
22572 break;
22574 output_macinfo_op (ref);
22575 ref->info = NULL;
22576 ref->code = 0;
22579 if (!macinfo_htab)
22580 return;
22582 delete macinfo_htab;
22583 macinfo_htab = NULL;
22585 /* If any DW_MACRO_GNU_transparent_include were used, on those
22586 DW_MACRO_GNU_transparent_include entries terminate the
22587 current chain and switch to a new comdat .debug_macinfo
22588 section and emit the define/undef entries within it. */
22589 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22590 switch (ref->code)
22592 case 0:
22593 continue;
22594 case DW_MACRO_GNU_transparent_include:
22596 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22597 tree comdat_key = get_identifier (ref->info);
22598 /* Terminate the previous .debug_macinfo section. */
22599 dw2_asm_output_data (1, 0, "End compilation unit");
22600 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22601 SECTION_DEBUG
22602 | SECTION_LINKONCE,
22603 comdat_key);
22604 ASM_GENERATE_INTERNAL_LABEL (label,
22605 DEBUG_MACRO_SECTION_LABEL,
22606 ref->lineno);
22607 ASM_OUTPUT_LABEL (asm_out_file, label);
22608 ref->code = 0;
22609 ref->info = NULL;
22610 dw2_asm_output_data (2, 4, "DWARF macro version number");
22611 if (DWARF_OFFSET_SIZE == 8)
22612 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22613 else
22614 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22616 break;
22617 case DW_MACINFO_define:
22618 case DW_MACINFO_undef:
22619 output_macinfo_op (ref);
22620 ref->code = 0;
22621 ref->info = NULL;
22622 break;
22623 default:
22624 gcc_unreachable ();
22628 /* Set up for Dwarf output at the start of compilation. */
22630 static void
22631 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22633 /* Allocate the file_table. */
22634 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22636 /* Allocate the decl_die_table. */
22637 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22639 /* Allocate the decl_loc_table. */
22640 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22642 /* Allocate the cached_dw_loc_list_table. */
22643 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22645 /* Allocate the initial hunk of the decl_scope_table. */
22646 vec_alloc (decl_scope_table, 256);
22648 /* Allocate the initial hunk of the abbrev_die_table. */
22649 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22650 (ABBREV_DIE_TABLE_INCREMENT);
22651 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22652 /* Zero-th entry is allocated, but unused. */
22653 abbrev_die_table_in_use = 1;
22655 /* Allocate the pubtypes and pubnames vectors. */
22656 vec_alloc (pubname_table, 32);
22657 vec_alloc (pubtype_table, 32);
22659 vec_alloc (incomplete_types, 64);
22661 vec_alloc (used_rtx_array, 32);
22663 if (!dwarf_split_debug_info)
22665 debug_info_section = get_section (DEBUG_INFO_SECTION,
22666 SECTION_DEBUG, NULL);
22667 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22668 SECTION_DEBUG, NULL);
22669 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22670 SECTION_DEBUG, NULL);
22672 else
22674 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22675 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22676 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22677 SECTION_DEBUG | SECTION_EXCLUDE,
22678 NULL);
22679 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22680 SECTION_DEBUG, NULL);
22681 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22682 SECTION_DEBUG, NULL);
22683 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22684 SECTION_DEBUG, NULL);
22685 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22686 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22688 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22689 the main .o, but the skeleton_line goes into the split off dwo. */
22690 debug_skeleton_line_section
22691 = get_section (DEBUG_DWO_LINE_SECTION,
22692 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22693 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22694 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22695 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22696 SECTION_DEBUG | SECTION_EXCLUDE,
22697 NULL);
22698 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22699 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22700 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22701 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22702 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22703 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22705 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22706 SECTION_DEBUG, NULL);
22707 debug_macinfo_section = get_section (dwarf_strict
22708 ? DEBUG_MACINFO_SECTION
22709 : DEBUG_MACRO_SECTION,
22710 DEBUG_MACRO_SECTION_FLAGS, NULL);
22711 debug_line_section = get_section (DEBUG_LINE_SECTION,
22712 SECTION_DEBUG, NULL);
22713 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22714 SECTION_DEBUG, NULL);
22715 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22716 SECTION_DEBUG, NULL);
22717 debug_str_section = get_section (DEBUG_STR_SECTION,
22718 DEBUG_STR_SECTION_FLAGS, NULL);
22719 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22720 SECTION_DEBUG, NULL);
22721 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22722 SECTION_DEBUG, NULL);
22724 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22725 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22726 DEBUG_ABBREV_SECTION_LABEL, 0);
22727 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22728 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22729 COLD_TEXT_SECTION_LABEL, 0);
22730 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22732 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22733 DEBUG_INFO_SECTION_LABEL, 0);
22734 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22735 DEBUG_LINE_SECTION_LABEL, 0);
22736 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22737 DEBUG_RANGES_SECTION_LABEL, 0);
22738 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22739 DEBUG_ADDR_SECTION_LABEL, 0);
22740 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22741 dwarf_strict
22742 ? DEBUG_MACINFO_SECTION_LABEL
22743 : DEBUG_MACRO_SECTION_LABEL, 0);
22744 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22746 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22747 vec_alloc (macinfo_table, 64);
22749 switch_to_section (text_section);
22750 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22752 /* Make sure the line number table for .text always exists. */
22753 text_section_line_info = new_line_info_table ();
22754 text_section_line_info->end_label = text_end_label;
22757 /* Called before compile () starts outputtting functions, variables
22758 and toplevel asms into assembly. */
22760 static void
22761 dwarf2out_assembly_start (void)
22763 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22764 && dwarf2out_do_cfi_asm ()
22765 && (!(flag_unwind_tables || flag_exceptions)
22766 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22767 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22770 /* A helper function for dwarf2out_finish called through
22771 htab_traverse. Assign a string its index. All strings must be
22772 collected into the table by the time index_string is called,
22773 because the indexing code relies on htab_traverse to traverse nodes
22774 in the same order for each run. */
22777 index_string (indirect_string_node **h, unsigned int *index)
22779 indirect_string_node *node = *h;
22781 find_string_form (node);
22782 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22784 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22785 node->index = *index;
22786 *index += 1;
22788 return 1;
22791 /* A helper function for output_indirect_strings called through
22792 htab_traverse. Output the offset to a string and update the
22793 current offset. */
22796 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22798 indirect_string_node *node = *h;
22800 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22802 /* Assert that this node has been assigned an index. */
22803 gcc_assert (node->index != NO_INDEX_ASSIGNED
22804 && node->index != NOT_INDEXED);
22805 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22806 "indexed string 0x%x: %s", node->index, node->str);
22807 *offset += strlen (node->str) + 1;
22809 return 1;
22812 /* A helper function for dwarf2out_finish called through
22813 htab_traverse. Output the indexed string. */
22816 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22818 struct indirect_string_node *node = *h;
22820 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22822 /* Assert that the strings are output in the same order as their
22823 indexes were assigned. */
22824 gcc_assert (*cur_idx == node->index);
22825 assemble_string (node->str, strlen (node->str) + 1);
22826 *cur_idx += 1;
22828 return 1;
22831 /* A helper function for dwarf2out_finish called through
22832 htab_traverse. Emit one queued .debug_str string. */
22835 output_indirect_string (indirect_string_node **h, void *)
22837 struct indirect_string_node *node = *h;
22839 node->form = find_string_form (node);
22840 if (node->form == DW_FORM_strp && node->refcount > 0)
22842 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22843 assemble_string (node->str, strlen (node->str) + 1);
22846 return 1;
22849 /* Output the indexed string table. */
22851 static void
22852 output_indirect_strings (void)
22854 switch_to_section (debug_str_section);
22855 if (!dwarf_split_debug_info)
22856 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22857 else
22859 unsigned int offset = 0;
22860 unsigned int cur_idx = 0;
22862 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22864 switch_to_section (debug_str_offsets_section);
22865 debug_str_hash->traverse_noresize
22866 <unsigned int *, output_index_string_offset> (&offset);
22867 switch_to_section (debug_str_dwo_section);
22868 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22869 (&cur_idx);
22873 /* Callback for htab_traverse to assign an index to an entry in the
22874 table, and to write that entry to the .debug_addr section. */
22877 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22879 addr_table_entry *entry = *slot;
22881 if (entry->refcount == 0)
22883 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22884 || entry->index == NOT_INDEXED);
22885 return 1;
22888 gcc_assert (entry->index == *cur_index);
22889 (*cur_index)++;
22891 switch (entry->kind)
22893 case ate_kind_rtx:
22894 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22895 "0x%x", entry->index);
22896 break;
22897 case ate_kind_rtx_dtprel:
22898 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22899 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22900 DWARF2_ADDR_SIZE,
22901 entry->addr.rtl);
22902 fputc ('\n', asm_out_file);
22903 break;
22904 case ate_kind_label:
22905 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22906 "0x%x", entry->index);
22907 break;
22908 default:
22909 gcc_unreachable ();
22911 return 1;
22914 /* Produce the .debug_addr section. */
22916 static void
22917 output_addr_table (void)
22919 unsigned int index = 0;
22920 if (addr_index_table == NULL || addr_index_table->size () == 0)
22921 return;
22923 switch_to_section (debug_addr_section);
22924 addr_index_table
22925 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22928 #if ENABLE_ASSERT_CHECKING
22929 /* Verify that all marks are clear. */
22931 static void
22932 verify_marks_clear (dw_die_ref die)
22934 dw_die_ref c;
22936 gcc_assert (! die->die_mark);
22937 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22939 #endif /* ENABLE_ASSERT_CHECKING */
22941 /* Clear the marks for a die and its children.
22942 Be cool if the mark isn't set. */
22944 static void
22945 prune_unmark_dies (dw_die_ref die)
22947 dw_die_ref c;
22949 if (die->die_mark)
22950 die->die_mark = 0;
22951 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22954 /* Given DIE that we're marking as used, find any other dies
22955 it references as attributes and mark them as used. */
22957 static void
22958 prune_unused_types_walk_attribs (dw_die_ref die)
22960 dw_attr_ref a;
22961 unsigned ix;
22963 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22965 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22967 /* A reference to another DIE.
22968 Make sure that it will get emitted.
22969 If it was broken out into a comdat group, don't follow it. */
22970 if (! AT_ref (a)->comdat_type_p
22971 || a->dw_attr == DW_AT_specification)
22972 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22974 /* Set the string's refcount to 0 so that prune_unused_types_mark
22975 accounts properly for it. */
22976 if (AT_class (a) == dw_val_class_str)
22977 a->dw_attr_val.v.val_str->refcount = 0;
22981 /* Mark the generic parameters and arguments children DIEs of DIE. */
22983 static void
22984 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22986 dw_die_ref c;
22988 if (die == NULL || die->die_child == NULL)
22989 return;
22990 c = die->die_child;
22993 if (is_template_parameter (c))
22994 prune_unused_types_mark (c, 1);
22995 c = c->die_sib;
22996 } while (c && c != die->die_child);
22999 /* Mark DIE as being used. If DOKIDS is true, then walk down
23000 to DIE's children. */
23002 static void
23003 prune_unused_types_mark (dw_die_ref die, int dokids)
23005 dw_die_ref c;
23007 if (die->die_mark == 0)
23009 /* We haven't done this node yet. Mark it as used. */
23010 die->die_mark = 1;
23011 /* If this is the DIE of a generic type instantiation,
23012 mark the children DIEs that describe its generic parms and
23013 args. */
23014 prune_unused_types_mark_generic_parms_dies (die);
23016 /* We also have to mark its parents as used.
23017 (But we don't want to mark our parent's kids due to this,
23018 unless it is a class.) */
23019 if (die->die_parent)
23020 prune_unused_types_mark (die->die_parent,
23021 class_scope_p (die->die_parent));
23023 /* Mark any referenced nodes. */
23024 prune_unused_types_walk_attribs (die);
23026 /* If this node is a specification,
23027 also mark the definition, if it exists. */
23028 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23029 prune_unused_types_mark (die->die_definition, 1);
23032 if (dokids && die->die_mark != 2)
23034 /* We need to walk the children, but haven't done so yet.
23035 Remember that we've walked the kids. */
23036 die->die_mark = 2;
23038 /* If this is an array type, we need to make sure our
23039 kids get marked, even if they're types. If we're
23040 breaking out types into comdat sections, do this
23041 for all type definitions. */
23042 if (die->die_tag == DW_TAG_array_type
23043 || (use_debug_types
23044 && is_type_die (die) && ! is_declaration_die (die)))
23045 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23046 else
23047 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23051 /* For local classes, look if any static member functions were emitted
23052 and if so, mark them. */
23054 static void
23055 prune_unused_types_walk_local_classes (dw_die_ref die)
23057 dw_die_ref c;
23059 if (die->die_mark == 2)
23060 return;
23062 switch (die->die_tag)
23064 case DW_TAG_structure_type:
23065 case DW_TAG_union_type:
23066 case DW_TAG_class_type:
23067 break;
23069 case DW_TAG_subprogram:
23070 if (!get_AT_flag (die, DW_AT_declaration)
23071 || die->die_definition != NULL)
23072 prune_unused_types_mark (die, 1);
23073 return;
23075 default:
23076 return;
23079 /* Mark children. */
23080 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23083 /* Walk the tree DIE and mark types that we actually use. */
23085 static void
23086 prune_unused_types_walk (dw_die_ref die)
23088 dw_die_ref c;
23090 /* Don't do anything if this node is already marked and
23091 children have been marked as well. */
23092 if (die->die_mark == 2)
23093 return;
23095 switch (die->die_tag)
23097 case DW_TAG_structure_type:
23098 case DW_TAG_union_type:
23099 case DW_TAG_class_type:
23100 if (die->die_perennial_p)
23101 break;
23103 for (c = die->die_parent; c; c = c->die_parent)
23104 if (c->die_tag == DW_TAG_subprogram)
23105 break;
23107 /* Finding used static member functions inside of classes
23108 is needed just for local classes, because for other classes
23109 static member function DIEs with DW_AT_specification
23110 are emitted outside of the DW_TAG_*_type. If we ever change
23111 it, we'd need to call this even for non-local classes. */
23112 if (c)
23113 prune_unused_types_walk_local_classes (die);
23115 /* It's a type node --- don't mark it. */
23116 return;
23118 case DW_TAG_const_type:
23119 case DW_TAG_packed_type:
23120 case DW_TAG_pointer_type:
23121 case DW_TAG_reference_type:
23122 case DW_TAG_rvalue_reference_type:
23123 case DW_TAG_volatile_type:
23124 case DW_TAG_typedef:
23125 case DW_TAG_array_type:
23126 case DW_TAG_interface_type:
23127 case DW_TAG_friend:
23128 case DW_TAG_variant_part:
23129 case DW_TAG_enumeration_type:
23130 case DW_TAG_subroutine_type:
23131 case DW_TAG_string_type:
23132 case DW_TAG_set_type:
23133 case DW_TAG_subrange_type:
23134 case DW_TAG_ptr_to_member_type:
23135 case DW_TAG_file_type:
23136 if (die->die_perennial_p)
23137 break;
23139 /* It's a type node --- don't mark it. */
23140 return;
23142 default:
23143 /* Mark everything else. */
23144 break;
23147 if (die->die_mark == 0)
23149 die->die_mark = 1;
23151 /* Now, mark any dies referenced from here. */
23152 prune_unused_types_walk_attribs (die);
23155 die->die_mark = 2;
23157 /* Mark children. */
23158 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23161 /* Increment the string counts on strings referred to from DIE's
23162 attributes. */
23164 static void
23165 prune_unused_types_update_strings (dw_die_ref die)
23167 dw_attr_ref a;
23168 unsigned ix;
23170 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23171 if (AT_class (a) == dw_val_class_str)
23173 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23174 s->refcount++;
23175 /* Avoid unnecessarily putting strings that are used less than
23176 twice in the hash table. */
23177 if (s->refcount
23178 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23180 indirect_string_node **slot
23181 = debug_str_hash->find_slot_with_hash (s->str,
23182 htab_hash_string (s->str),
23183 INSERT);
23184 gcc_assert (*slot == NULL);
23185 *slot = s;
23190 /* Remove from the tree DIE any dies that aren't marked. */
23192 static void
23193 prune_unused_types_prune (dw_die_ref die)
23195 dw_die_ref c;
23197 gcc_assert (die->die_mark);
23198 prune_unused_types_update_strings (die);
23200 if (! die->die_child)
23201 return;
23203 c = die->die_child;
23204 do {
23205 dw_die_ref prev = c;
23206 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23207 if (c == die->die_child)
23209 /* No marked children between 'prev' and the end of the list. */
23210 if (prev == c)
23211 /* No marked children at all. */
23212 die->die_child = NULL;
23213 else
23215 prev->die_sib = c->die_sib;
23216 die->die_child = prev;
23218 return;
23221 if (c != prev->die_sib)
23222 prev->die_sib = c;
23223 prune_unused_types_prune (c);
23224 } while (c != die->die_child);
23227 /* Remove dies representing declarations that we never use. */
23229 static void
23230 prune_unused_types (void)
23232 unsigned int i;
23233 limbo_die_node *node;
23234 comdat_type_node *ctnode;
23235 pubname_ref pub;
23236 dw_die_ref base_type;
23238 #if ENABLE_ASSERT_CHECKING
23239 /* All the marks should already be clear. */
23240 verify_marks_clear (comp_unit_die ());
23241 for (node = limbo_die_list; node; node = node->next)
23242 verify_marks_clear (node->die);
23243 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23244 verify_marks_clear (ctnode->root_die);
23245 #endif /* ENABLE_ASSERT_CHECKING */
23247 /* Mark types that are used in global variables. */
23248 premark_types_used_by_global_vars ();
23250 /* Set the mark on nodes that are actually used. */
23251 prune_unused_types_walk (comp_unit_die ());
23252 for (node = limbo_die_list; node; node = node->next)
23253 prune_unused_types_walk (node->die);
23254 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23256 prune_unused_types_walk (ctnode->root_die);
23257 prune_unused_types_mark (ctnode->type_die, 1);
23260 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23261 are unusual in that they are pubnames that are the children of pubtypes.
23262 They should only be marked via their parent DW_TAG_enumeration_type die,
23263 not as roots in themselves. */
23264 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23265 if (pub->die->die_tag != DW_TAG_enumerator)
23266 prune_unused_types_mark (pub->die, 1);
23267 for (i = 0; base_types.iterate (i, &base_type); i++)
23268 prune_unused_types_mark (base_type, 1);
23270 if (debug_str_hash)
23271 debug_str_hash->empty ();
23272 if (skeleton_debug_str_hash)
23273 skeleton_debug_str_hash->empty ();
23274 prune_unused_types_prune (comp_unit_die ());
23275 for (node = limbo_die_list; node; node = node->next)
23276 prune_unused_types_prune (node->die);
23277 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23278 prune_unused_types_prune (ctnode->root_die);
23280 /* Leave the marks clear. */
23281 prune_unmark_dies (comp_unit_die ());
23282 for (node = limbo_die_list; node; node = node->next)
23283 prune_unmark_dies (node->die);
23284 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23285 prune_unmark_dies (ctnode->root_die);
23288 /* Set the parameter to true if there are any relative pathnames in
23289 the file table. */
23291 file_table_relative_p (dwarf_file_data **slot, bool *p)
23293 struct dwarf_file_data *d = *slot;
23294 if (!IS_ABSOLUTE_PATH (d->filename))
23296 *p = true;
23297 return 0;
23299 return 1;
23302 /* Helpers to manipulate hash table of comdat type units. */
23304 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23306 typedef comdat_type_node value_type;
23307 typedef comdat_type_node compare_type;
23308 static inline hashval_t hash (const value_type *);
23309 static inline bool equal (const value_type *, const compare_type *);
23312 inline hashval_t
23313 comdat_type_hasher::hash (const value_type *type_node)
23315 hashval_t h;
23316 memcpy (&h, type_node->signature, sizeof (h));
23317 return h;
23320 inline bool
23321 comdat_type_hasher::equal (const value_type *type_node_1,
23322 const compare_type *type_node_2)
23324 return (! memcmp (type_node_1->signature, type_node_2->signature,
23325 DWARF_TYPE_SIGNATURE_SIZE));
23328 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23329 to the location it would have been added, should we know its
23330 DECL_ASSEMBLER_NAME when we added other attributes. This will
23331 probably improve compactness of debug info, removing equivalent
23332 abbrevs, and hide any differences caused by deferring the
23333 computation of the assembler name, triggered by e.g. PCH. */
23335 static inline void
23336 move_linkage_attr (dw_die_ref die)
23338 unsigned ix = vec_safe_length (die->die_attr);
23339 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23341 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23342 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23344 while (--ix > 0)
23346 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23348 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23349 break;
23352 if (ix != vec_safe_length (die->die_attr) - 1)
23354 die->die_attr->pop ();
23355 die->die_attr->quick_insert (ix, linkage);
23359 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23360 referenced from typed stack ops and count how often they are used. */
23362 static void
23363 mark_base_types (dw_loc_descr_ref loc)
23365 dw_die_ref base_type = NULL;
23367 for (; loc; loc = loc->dw_loc_next)
23369 switch (loc->dw_loc_opc)
23371 case DW_OP_GNU_regval_type:
23372 case DW_OP_GNU_deref_type:
23373 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23374 break;
23375 case DW_OP_GNU_convert:
23376 case DW_OP_GNU_reinterpret:
23377 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23378 continue;
23379 /* FALLTHRU */
23380 case DW_OP_GNU_const_type:
23381 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23382 break;
23383 case DW_OP_GNU_entry_value:
23384 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23385 continue;
23386 default:
23387 continue;
23389 gcc_assert (base_type->die_parent == comp_unit_die ());
23390 if (base_type->die_mark)
23391 base_type->die_mark++;
23392 else
23394 base_types.safe_push (base_type);
23395 base_type->die_mark = 1;
23400 /* Comparison function for sorting marked base types. */
23402 static int
23403 base_type_cmp (const void *x, const void *y)
23405 dw_die_ref dx = *(const dw_die_ref *) x;
23406 dw_die_ref dy = *(const dw_die_ref *) y;
23407 unsigned int byte_size1, byte_size2;
23408 unsigned int encoding1, encoding2;
23409 if (dx->die_mark > dy->die_mark)
23410 return -1;
23411 if (dx->die_mark < dy->die_mark)
23412 return 1;
23413 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23414 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23415 if (byte_size1 < byte_size2)
23416 return 1;
23417 if (byte_size1 > byte_size2)
23418 return -1;
23419 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23420 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23421 if (encoding1 < encoding2)
23422 return 1;
23423 if (encoding1 > encoding2)
23424 return -1;
23425 return 0;
23428 /* Move base types marked by mark_base_types as early as possible
23429 in the CU, sorted by decreasing usage count both to make the
23430 uleb128 references as small as possible and to make sure they
23431 will have die_offset already computed by calc_die_sizes when
23432 sizes of typed stack loc ops is computed. */
23434 static void
23435 move_marked_base_types (void)
23437 unsigned int i;
23438 dw_die_ref base_type, die, c;
23440 if (base_types.is_empty ())
23441 return;
23443 /* Sort by decreasing usage count, they will be added again in that
23444 order later on. */
23445 base_types.qsort (base_type_cmp);
23446 die = comp_unit_die ();
23447 c = die->die_child;
23450 dw_die_ref prev = c;
23451 c = c->die_sib;
23452 while (c->die_mark)
23454 remove_child_with_prev (c, prev);
23455 /* As base types got marked, there must be at least
23456 one node other than DW_TAG_base_type. */
23457 gcc_assert (c != c->die_sib);
23458 c = c->die_sib;
23461 while (c != die->die_child);
23462 gcc_assert (die->die_child);
23463 c = die->die_child;
23464 for (i = 0; base_types.iterate (i, &base_type); i++)
23466 base_type->die_mark = 0;
23467 base_type->die_sib = c->die_sib;
23468 c->die_sib = base_type;
23469 c = base_type;
23473 /* Helper function for resolve_addr, attempt to resolve
23474 one CONST_STRING, return true if successful. Similarly verify that
23475 SYMBOL_REFs refer to variables emitted in the current CU. */
23477 static bool
23478 resolve_one_addr (rtx *addr)
23480 rtx rtl = *addr;
23482 if (GET_CODE (rtl) == CONST_STRING)
23484 size_t len = strlen (XSTR (rtl, 0)) + 1;
23485 tree t = build_string (len, XSTR (rtl, 0));
23486 tree tlen = size_int (len - 1);
23487 TREE_TYPE (t)
23488 = build_array_type (char_type_node, build_index_type (tlen));
23489 rtl = lookup_constant_def (t);
23490 if (!rtl || !MEM_P (rtl))
23491 return false;
23492 rtl = XEXP (rtl, 0);
23493 if (GET_CODE (rtl) == SYMBOL_REF
23494 && SYMBOL_REF_DECL (rtl)
23495 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23496 return false;
23497 vec_safe_push (used_rtx_array, rtl);
23498 *addr = rtl;
23499 return true;
23502 if (GET_CODE (rtl) == SYMBOL_REF
23503 && SYMBOL_REF_DECL (rtl))
23505 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23507 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23508 return false;
23510 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23511 return false;
23514 if (GET_CODE (rtl) == CONST)
23516 subrtx_ptr_iterator::array_type array;
23517 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23518 if (!resolve_one_addr (*iter))
23519 return false;
23522 return true;
23525 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23526 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23527 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23529 static rtx
23530 string_cst_pool_decl (tree t)
23532 rtx rtl = output_constant_def (t, 1);
23533 unsigned char *array;
23534 dw_loc_descr_ref l;
23535 tree decl;
23536 size_t len;
23537 dw_die_ref ref;
23539 if (!rtl || !MEM_P (rtl))
23540 return NULL_RTX;
23541 rtl = XEXP (rtl, 0);
23542 if (GET_CODE (rtl) != SYMBOL_REF
23543 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23544 return NULL_RTX;
23546 decl = SYMBOL_REF_DECL (rtl);
23547 if (!lookup_decl_die (decl))
23549 len = TREE_STRING_LENGTH (t);
23550 vec_safe_push (used_rtx_array, rtl);
23551 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23552 array = ggc_vec_alloc<unsigned char> (len);
23553 memcpy (array, TREE_STRING_POINTER (t), len);
23554 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23555 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23556 l->dw_loc_oprnd2.v.val_vec.length = len;
23557 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23558 l->dw_loc_oprnd2.v.val_vec.array = array;
23559 add_AT_loc (ref, DW_AT_location, l);
23560 equate_decl_number_to_die (decl, ref);
23562 return rtl;
23565 /* Helper function of resolve_addr_in_expr. LOC is
23566 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23567 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23568 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23569 with DW_OP_GNU_implicit_pointer if possible
23570 and return true, if unsuccessful, return false. */
23572 static bool
23573 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23575 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23576 HOST_WIDE_INT offset = 0;
23577 dw_die_ref ref = NULL;
23578 tree decl;
23580 if (GET_CODE (rtl) == CONST
23581 && GET_CODE (XEXP (rtl, 0)) == PLUS
23582 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23584 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23585 rtl = XEXP (XEXP (rtl, 0), 0);
23587 if (GET_CODE (rtl) == CONST_STRING)
23589 size_t len = strlen (XSTR (rtl, 0)) + 1;
23590 tree t = build_string (len, XSTR (rtl, 0));
23591 tree tlen = size_int (len - 1);
23593 TREE_TYPE (t)
23594 = build_array_type (char_type_node, build_index_type (tlen));
23595 rtl = string_cst_pool_decl (t);
23596 if (!rtl)
23597 return false;
23599 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23601 decl = SYMBOL_REF_DECL (rtl);
23602 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23604 ref = lookup_decl_die (decl);
23605 if (ref && (get_AT (ref, DW_AT_location)
23606 || get_AT (ref, DW_AT_const_value)))
23608 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23609 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23610 loc->dw_loc_oprnd1.val_entry = NULL;
23611 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23612 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23613 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23614 loc->dw_loc_oprnd2.v.val_int = offset;
23615 return true;
23619 return false;
23622 /* Helper function for resolve_addr, handle one location
23623 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23624 the location list couldn't be resolved. */
23626 static bool
23627 resolve_addr_in_expr (dw_loc_descr_ref loc)
23629 dw_loc_descr_ref keep = NULL;
23630 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23631 switch (loc->dw_loc_opc)
23633 case DW_OP_addr:
23634 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23636 if ((prev == NULL
23637 || prev->dw_loc_opc == DW_OP_piece
23638 || prev->dw_loc_opc == DW_OP_bit_piece)
23639 && loc->dw_loc_next
23640 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23641 && !dwarf_strict
23642 && optimize_one_addr_into_implicit_ptr (loc))
23643 break;
23644 return false;
23646 break;
23647 case DW_OP_GNU_addr_index:
23648 case DW_OP_GNU_const_index:
23649 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23650 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23652 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23653 if (!resolve_one_addr (&rtl))
23654 return false;
23655 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23656 loc->dw_loc_oprnd1.val_entry =
23657 add_addr_table_entry (rtl, ate_kind_rtx);
23659 break;
23660 case DW_OP_const4u:
23661 case DW_OP_const8u:
23662 if (loc->dtprel
23663 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23664 return false;
23665 break;
23666 case DW_OP_plus_uconst:
23667 if (size_of_loc_descr (loc)
23668 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23670 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23672 dw_loc_descr_ref repl
23673 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23674 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23675 add_loc_descr (&repl, loc->dw_loc_next);
23676 *loc = *repl;
23678 break;
23679 case DW_OP_implicit_value:
23680 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23681 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23682 return false;
23683 break;
23684 case DW_OP_GNU_implicit_pointer:
23685 case DW_OP_GNU_parameter_ref:
23686 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23688 dw_die_ref ref
23689 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23690 if (ref == NULL)
23691 return false;
23692 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23693 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23694 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23696 break;
23697 case DW_OP_GNU_const_type:
23698 case DW_OP_GNU_regval_type:
23699 case DW_OP_GNU_deref_type:
23700 case DW_OP_GNU_convert:
23701 case DW_OP_GNU_reinterpret:
23702 while (loc->dw_loc_next
23703 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23705 dw_die_ref base1, base2;
23706 unsigned enc1, enc2, size1, size2;
23707 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23708 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23709 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23710 else if (loc->dw_loc_oprnd1.val_class
23711 == dw_val_class_unsigned_const)
23712 break;
23713 else
23714 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23715 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23716 == dw_val_class_unsigned_const)
23717 break;
23718 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23719 gcc_assert (base1->die_tag == DW_TAG_base_type
23720 && base2->die_tag == DW_TAG_base_type);
23721 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23722 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23723 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23724 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23725 if (size1 == size2
23726 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23727 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23728 && loc != keep)
23729 || enc1 == enc2))
23731 /* Optimize away next DW_OP_GNU_convert after
23732 adjusting LOC's base type die reference. */
23733 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23734 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23735 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23736 else
23737 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23738 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23739 continue;
23741 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23742 point typed stack entry. */
23743 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23744 keep = loc->dw_loc_next;
23745 break;
23747 break;
23748 default:
23749 break;
23751 return true;
23754 /* Helper function of resolve_addr. DIE had DW_AT_location of
23755 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23756 and DW_OP_addr couldn't be resolved. resolve_addr has already
23757 removed the DW_AT_location attribute. This function attempts to
23758 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23759 to it or DW_AT_const_value attribute, if possible. */
23761 static void
23762 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23764 if (TREE_CODE (decl) != VAR_DECL
23765 || lookup_decl_die (decl) != die
23766 || DECL_EXTERNAL (decl)
23767 || !TREE_STATIC (decl)
23768 || DECL_INITIAL (decl) == NULL_TREE
23769 || DECL_P (DECL_INITIAL (decl))
23770 || get_AT (die, DW_AT_const_value))
23771 return;
23773 tree init = DECL_INITIAL (decl);
23774 HOST_WIDE_INT offset = 0;
23775 /* For variables that have been optimized away and thus
23776 don't have a memory location, see if we can emit
23777 DW_AT_const_value instead. */
23778 if (tree_add_const_value_attribute (die, init))
23779 return;
23780 if (dwarf_strict)
23781 return;
23782 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23783 and ADDR_EXPR refers to a decl that has DW_AT_location or
23784 DW_AT_const_value (but isn't addressable, otherwise
23785 resolving the original DW_OP_addr wouldn't fail), see if
23786 we can add DW_OP_GNU_implicit_pointer. */
23787 STRIP_NOPS (init);
23788 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23789 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23791 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23792 init = TREE_OPERAND (init, 0);
23793 STRIP_NOPS (init);
23795 if (TREE_CODE (init) != ADDR_EXPR)
23796 return;
23797 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23798 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23799 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23800 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23801 && TREE_OPERAND (init, 0) != decl))
23803 dw_die_ref ref;
23804 dw_loc_descr_ref l;
23806 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23808 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23809 if (!rtl)
23810 return;
23811 decl = SYMBOL_REF_DECL (rtl);
23813 else
23814 decl = TREE_OPERAND (init, 0);
23815 ref = lookup_decl_die (decl);
23816 if (ref == NULL
23817 || (!get_AT (ref, DW_AT_location)
23818 && !get_AT (ref, DW_AT_const_value)))
23819 return;
23820 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23821 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23822 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23823 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23824 add_AT_loc (die, DW_AT_location, l);
23828 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23829 an address in .rodata section if the string literal is emitted there,
23830 or remove the containing location list or replace DW_AT_const_value
23831 with DW_AT_location and empty location expression, if it isn't found
23832 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23833 to something that has been emitted in the current CU. */
23835 static void
23836 resolve_addr (dw_die_ref die)
23838 dw_die_ref c;
23839 dw_attr_ref a;
23840 dw_loc_list_ref *curr, *start, loc;
23841 unsigned ix;
23843 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23844 switch (AT_class (a))
23846 case dw_val_class_loc_list:
23847 start = curr = AT_loc_list_ptr (a);
23848 loc = *curr;
23849 gcc_assert (loc);
23850 /* The same list can be referenced more than once. See if we have
23851 already recorded the result from a previous pass. */
23852 if (loc->replaced)
23853 *curr = loc->dw_loc_next;
23854 else if (!loc->resolved_addr)
23856 /* As things stand, we do not expect or allow one die to
23857 reference a suffix of another die's location list chain.
23858 References must be identical or completely separate.
23859 There is therefore no need to cache the result of this
23860 pass on any list other than the first; doing so
23861 would lead to unnecessary writes. */
23862 while (*curr)
23864 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23865 if (!resolve_addr_in_expr ((*curr)->expr))
23867 dw_loc_list_ref next = (*curr)->dw_loc_next;
23868 dw_loc_descr_ref l = (*curr)->expr;
23870 if (next && (*curr)->ll_symbol)
23872 gcc_assert (!next->ll_symbol);
23873 next->ll_symbol = (*curr)->ll_symbol;
23875 if (dwarf_split_debug_info)
23876 remove_loc_list_addr_table_entries (l);
23877 *curr = next;
23879 else
23881 mark_base_types ((*curr)->expr);
23882 curr = &(*curr)->dw_loc_next;
23885 if (loc == *start)
23886 loc->resolved_addr = 1;
23887 else
23889 loc->replaced = 1;
23890 loc->dw_loc_next = *start;
23893 if (!*start)
23895 remove_AT (die, a->dw_attr);
23896 ix--;
23898 break;
23899 case dw_val_class_loc:
23901 dw_loc_descr_ref l = AT_loc (a);
23902 /* For -gdwarf-2 don't attempt to optimize
23903 DW_AT_data_member_location containing
23904 DW_OP_plus_uconst - older consumers might
23905 rely on it being that op instead of a more complex,
23906 but shorter, location description. */
23907 if ((dwarf_version > 2
23908 || a->dw_attr != DW_AT_data_member_location
23909 || l == NULL
23910 || l->dw_loc_opc != DW_OP_plus_uconst
23911 || l->dw_loc_next != NULL)
23912 && !resolve_addr_in_expr (l))
23914 if (dwarf_split_debug_info)
23915 remove_loc_list_addr_table_entries (l);
23916 if (l != NULL
23917 && l->dw_loc_next == NULL
23918 && l->dw_loc_opc == DW_OP_addr
23919 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23920 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23921 && a->dw_attr == DW_AT_location)
23923 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23924 remove_AT (die, a->dw_attr);
23925 ix--;
23926 optimize_location_into_implicit_ptr (die, decl);
23927 break;
23929 remove_AT (die, a->dw_attr);
23930 ix--;
23932 else
23933 mark_base_types (l);
23935 break;
23936 case dw_val_class_addr:
23937 if (a->dw_attr == DW_AT_const_value
23938 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23940 if (AT_index (a) != NOT_INDEXED)
23941 remove_addr_table_entry (a->dw_attr_val.val_entry);
23942 remove_AT (die, a->dw_attr);
23943 ix--;
23945 if (die->die_tag == DW_TAG_GNU_call_site
23946 && a->dw_attr == DW_AT_abstract_origin)
23948 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23949 dw_die_ref tdie = lookup_decl_die (tdecl);
23950 if (tdie == NULL
23951 && DECL_EXTERNAL (tdecl)
23952 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23954 force_decl_die (tdecl);
23955 tdie = lookup_decl_die (tdecl);
23957 if (tdie)
23959 a->dw_attr_val.val_class = dw_val_class_die_ref;
23960 a->dw_attr_val.v.val_die_ref.die = tdie;
23961 a->dw_attr_val.v.val_die_ref.external = 0;
23963 else
23965 if (AT_index (a) != NOT_INDEXED)
23966 remove_addr_table_entry (a->dw_attr_val.val_entry);
23967 remove_AT (die, a->dw_attr);
23968 ix--;
23971 break;
23972 default:
23973 break;
23976 FOR_EACH_CHILD (die, c, resolve_addr (c));
23979 /* Helper routines for optimize_location_lists.
23980 This pass tries to share identical local lists in .debug_loc
23981 section. */
23983 /* Iteratively hash operands of LOC opcode into HSTATE. */
23985 static void
23986 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23988 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23989 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23991 switch (loc->dw_loc_opc)
23993 case DW_OP_const4u:
23994 case DW_OP_const8u:
23995 if (loc->dtprel)
23996 goto hash_addr;
23997 /* FALLTHRU */
23998 case DW_OP_const1u:
23999 case DW_OP_const1s:
24000 case DW_OP_const2u:
24001 case DW_OP_const2s:
24002 case DW_OP_const4s:
24003 case DW_OP_const8s:
24004 case DW_OP_constu:
24005 case DW_OP_consts:
24006 case DW_OP_pick:
24007 case DW_OP_plus_uconst:
24008 case DW_OP_breg0:
24009 case DW_OP_breg1:
24010 case DW_OP_breg2:
24011 case DW_OP_breg3:
24012 case DW_OP_breg4:
24013 case DW_OP_breg5:
24014 case DW_OP_breg6:
24015 case DW_OP_breg7:
24016 case DW_OP_breg8:
24017 case DW_OP_breg9:
24018 case DW_OP_breg10:
24019 case DW_OP_breg11:
24020 case DW_OP_breg12:
24021 case DW_OP_breg13:
24022 case DW_OP_breg14:
24023 case DW_OP_breg15:
24024 case DW_OP_breg16:
24025 case DW_OP_breg17:
24026 case DW_OP_breg18:
24027 case DW_OP_breg19:
24028 case DW_OP_breg20:
24029 case DW_OP_breg21:
24030 case DW_OP_breg22:
24031 case DW_OP_breg23:
24032 case DW_OP_breg24:
24033 case DW_OP_breg25:
24034 case DW_OP_breg26:
24035 case DW_OP_breg27:
24036 case DW_OP_breg28:
24037 case DW_OP_breg29:
24038 case DW_OP_breg30:
24039 case DW_OP_breg31:
24040 case DW_OP_regx:
24041 case DW_OP_fbreg:
24042 case DW_OP_piece:
24043 case DW_OP_deref_size:
24044 case DW_OP_xderef_size:
24045 hstate.add_object (val1->v.val_int);
24046 break;
24047 case DW_OP_skip:
24048 case DW_OP_bra:
24050 int offset;
24052 gcc_assert (val1->val_class == dw_val_class_loc);
24053 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24054 hstate.add_object (offset);
24056 break;
24057 case DW_OP_implicit_value:
24058 hstate.add_object (val1->v.val_unsigned);
24059 switch (val2->val_class)
24061 case dw_val_class_const:
24062 hstate.add_object (val2->v.val_int);
24063 break;
24064 case dw_val_class_vec:
24066 unsigned int elt_size = val2->v.val_vec.elt_size;
24067 unsigned int len = val2->v.val_vec.length;
24069 hstate.add_int (elt_size);
24070 hstate.add_int (len);
24071 hstate.add (val2->v.val_vec.array, len * elt_size);
24073 break;
24074 case dw_val_class_const_double:
24075 hstate.add_object (val2->v.val_double.low);
24076 hstate.add_object (val2->v.val_double.high);
24077 break;
24078 case dw_val_class_wide_int:
24079 hstate.add_object (*val2->v.val_wide);
24080 break;
24081 case dw_val_class_addr:
24082 inchash::add_rtx (val2->v.val_addr, hstate);
24083 break;
24084 default:
24085 gcc_unreachable ();
24087 break;
24088 case DW_OP_bregx:
24089 case DW_OP_bit_piece:
24090 hstate.add_object (val1->v.val_int);
24091 hstate.add_object (val2->v.val_int);
24092 break;
24093 case DW_OP_addr:
24094 hash_addr:
24095 if (loc->dtprel)
24097 unsigned char dtprel = 0xd1;
24098 hstate.add_object (dtprel);
24100 inchash::add_rtx (val1->v.val_addr, hstate);
24101 break;
24102 case DW_OP_GNU_addr_index:
24103 case DW_OP_GNU_const_index:
24105 if (loc->dtprel)
24107 unsigned char dtprel = 0xd1;
24108 hstate.add_object (dtprel);
24110 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24112 break;
24113 case DW_OP_GNU_implicit_pointer:
24114 hstate.add_int (val2->v.val_int);
24115 break;
24116 case DW_OP_GNU_entry_value:
24117 hstate.add_object (val1->v.val_loc);
24118 break;
24119 case DW_OP_GNU_regval_type:
24120 case DW_OP_GNU_deref_type:
24122 unsigned int byte_size
24123 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24124 unsigned int encoding
24125 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24126 hstate.add_object (val1->v.val_int);
24127 hstate.add_object (byte_size);
24128 hstate.add_object (encoding);
24130 break;
24131 case DW_OP_GNU_convert:
24132 case DW_OP_GNU_reinterpret:
24133 if (val1->val_class == dw_val_class_unsigned_const)
24135 hstate.add_object (val1->v.val_unsigned);
24136 break;
24138 /* FALLTHRU */
24139 case DW_OP_GNU_const_type:
24141 unsigned int byte_size
24142 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24143 unsigned int encoding
24144 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24145 hstate.add_object (byte_size);
24146 hstate.add_object (encoding);
24147 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24148 break;
24149 hstate.add_object (val2->val_class);
24150 switch (val2->val_class)
24152 case dw_val_class_const:
24153 hstate.add_object (val2->v.val_int);
24154 break;
24155 case dw_val_class_vec:
24157 unsigned int elt_size = val2->v.val_vec.elt_size;
24158 unsigned int len = val2->v.val_vec.length;
24160 hstate.add_object (elt_size);
24161 hstate.add_object (len);
24162 hstate.add (val2->v.val_vec.array, len * elt_size);
24164 break;
24165 case dw_val_class_const_double:
24166 hstate.add_object (val2->v.val_double.low);
24167 hstate.add_object (val2->v.val_double.high);
24168 break;
24169 case dw_val_class_wide_int:
24170 hstate.add_object (*val2->v.val_wide);
24171 break;
24172 default:
24173 gcc_unreachable ();
24176 break;
24178 default:
24179 /* Other codes have no operands. */
24180 break;
24184 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24186 static inline void
24187 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24189 dw_loc_descr_ref l;
24190 bool sizes_computed = false;
24191 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24192 size_of_locs (loc);
24194 for (l = loc; l != NULL; l = l->dw_loc_next)
24196 enum dwarf_location_atom opc = l->dw_loc_opc;
24197 hstate.add_object (opc);
24198 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24200 size_of_locs (loc);
24201 sizes_computed = true;
24203 hash_loc_operands (l, hstate);
24207 /* Compute hash of the whole location list LIST_HEAD. */
24209 static inline void
24210 hash_loc_list (dw_loc_list_ref list_head)
24212 dw_loc_list_ref curr = list_head;
24213 inchash::hash hstate;
24215 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24217 hstate.add (curr->begin, strlen (curr->begin) + 1);
24218 hstate.add (curr->end, strlen (curr->end) + 1);
24219 if (curr->section)
24220 hstate.add (curr->section, strlen (curr->section) + 1);
24221 hash_locs (curr->expr, hstate);
24223 list_head->hash = hstate.end ();
24226 /* Return true if X and Y opcodes have the same operands. */
24228 static inline bool
24229 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24231 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24232 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24233 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24234 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24236 switch (x->dw_loc_opc)
24238 case DW_OP_const4u:
24239 case DW_OP_const8u:
24240 if (x->dtprel)
24241 goto hash_addr;
24242 /* FALLTHRU */
24243 case DW_OP_const1u:
24244 case DW_OP_const1s:
24245 case DW_OP_const2u:
24246 case DW_OP_const2s:
24247 case DW_OP_const4s:
24248 case DW_OP_const8s:
24249 case DW_OP_constu:
24250 case DW_OP_consts:
24251 case DW_OP_pick:
24252 case DW_OP_plus_uconst:
24253 case DW_OP_breg0:
24254 case DW_OP_breg1:
24255 case DW_OP_breg2:
24256 case DW_OP_breg3:
24257 case DW_OP_breg4:
24258 case DW_OP_breg5:
24259 case DW_OP_breg6:
24260 case DW_OP_breg7:
24261 case DW_OP_breg8:
24262 case DW_OP_breg9:
24263 case DW_OP_breg10:
24264 case DW_OP_breg11:
24265 case DW_OP_breg12:
24266 case DW_OP_breg13:
24267 case DW_OP_breg14:
24268 case DW_OP_breg15:
24269 case DW_OP_breg16:
24270 case DW_OP_breg17:
24271 case DW_OP_breg18:
24272 case DW_OP_breg19:
24273 case DW_OP_breg20:
24274 case DW_OP_breg21:
24275 case DW_OP_breg22:
24276 case DW_OP_breg23:
24277 case DW_OP_breg24:
24278 case DW_OP_breg25:
24279 case DW_OP_breg26:
24280 case DW_OP_breg27:
24281 case DW_OP_breg28:
24282 case DW_OP_breg29:
24283 case DW_OP_breg30:
24284 case DW_OP_breg31:
24285 case DW_OP_regx:
24286 case DW_OP_fbreg:
24287 case DW_OP_piece:
24288 case DW_OP_deref_size:
24289 case DW_OP_xderef_size:
24290 return valx1->v.val_int == valy1->v.val_int;
24291 case DW_OP_skip:
24292 case DW_OP_bra:
24293 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24294 can cause irrelevant differences in dw_loc_addr. */
24295 gcc_assert (valx1->val_class == dw_val_class_loc
24296 && valy1->val_class == dw_val_class_loc
24297 && (dwarf_split_debug_info
24298 || x->dw_loc_addr == y->dw_loc_addr));
24299 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24300 case DW_OP_implicit_value:
24301 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24302 || valx2->val_class != valy2->val_class)
24303 return false;
24304 switch (valx2->val_class)
24306 case dw_val_class_const:
24307 return valx2->v.val_int == valy2->v.val_int;
24308 case dw_val_class_vec:
24309 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24310 && valx2->v.val_vec.length == valy2->v.val_vec.length
24311 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24312 valx2->v.val_vec.elt_size
24313 * valx2->v.val_vec.length) == 0;
24314 case dw_val_class_const_double:
24315 return valx2->v.val_double.low == valy2->v.val_double.low
24316 && valx2->v.val_double.high == valy2->v.val_double.high;
24317 case dw_val_class_wide_int:
24318 return *valx2->v.val_wide == *valy2->v.val_wide;
24319 case dw_val_class_addr:
24320 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24321 default:
24322 gcc_unreachable ();
24324 case DW_OP_bregx:
24325 case DW_OP_bit_piece:
24326 return valx1->v.val_int == valy1->v.val_int
24327 && valx2->v.val_int == valy2->v.val_int;
24328 case DW_OP_addr:
24329 hash_addr:
24330 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24331 case DW_OP_GNU_addr_index:
24332 case DW_OP_GNU_const_index:
24334 rtx ax1 = valx1->val_entry->addr.rtl;
24335 rtx ay1 = valy1->val_entry->addr.rtl;
24336 return rtx_equal_p (ax1, ay1);
24338 case DW_OP_GNU_implicit_pointer:
24339 return valx1->val_class == dw_val_class_die_ref
24340 && valx1->val_class == valy1->val_class
24341 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24342 && valx2->v.val_int == valy2->v.val_int;
24343 case DW_OP_GNU_entry_value:
24344 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24345 case DW_OP_GNU_const_type:
24346 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24347 || valx2->val_class != valy2->val_class)
24348 return false;
24349 switch (valx2->val_class)
24351 case dw_val_class_const:
24352 return valx2->v.val_int == valy2->v.val_int;
24353 case dw_val_class_vec:
24354 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24355 && valx2->v.val_vec.length == valy2->v.val_vec.length
24356 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24357 valx2->v.val_vec.elt_size
24358 * valx2->v.val_vec.length) == 0;
24359 case dw_val_class_const_double:
24360 return valx2->v.val_double.low == valy2->v.val_double.low
24361 && valx2->v.val_double.high == valy2->v.val_double.high;
24362 case dw_val_class_wide_int:
24363 return *valx2->v.val_wide == *valy2->v.val_wide;
24364 default:
24365 gcc_unreachable ();
24367 case DW_OP_GNU_regval_type:
24368 case DW_OP_GNU_deref_type:
24369 return valx1->v.val_int == valy1->v.val_int
24370 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24371 case DW_OP_GNU_convert:
24372 case DW_OP_GNU_reinterpret:
24373 if (valx1->val_class != valy1->val_class)
24374 return false;
24375 if (valx1->val_class == dw_val_class_unsigned_const)
24376 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24377 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24378 case DW_OP_GNU_parameter_ref:
24379 return valx1->val_class == dw_val_class_die_ref
24380 && valx1->val_class == valy1->val_class
24381 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24382 default:
24383 /* Other codes have no operands. */
24384 return true;
24388 /* Return true if DWARF location expressions X and Y are the same. */
24390 static inline bool
24391 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24393 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24394 if (x->dw_loc_opc != y->dw_loc_opc
24395 || x->dtprel != y->dtprel
24396 || !compare_loc_operands (x, y))
24397 break;
24398 return x == NULL && y == NULL;
24401 /* Hashtable helpers. */
24403 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24405 typedef dw_loc_list_struct value_type;
24406 typedef dw_loc_list_struct compare_type;
24407 static inline hashval_t hash (const value_type *);
24408 static inline bool equal (const value_type *, const compare_type *);
24411 /* Return precomputed hash of location list X. */
24413 inline hashval_t
24414 loc_list_hasher::hash (const value_type *x)
24416 return x->hash;
24419 /* Return true if location lists A and B are the same. */
24421 inline bool
24422 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24424 if (a == b)
24425 return 1;
24426 if (a->hash != b->hash)
24427 return 0;
24428 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24429 if (strcmp (a->begin, b->begin) != 0
24430 || strcmp (a->end, b->end) != 0
24431 || (a->section == NULL) != (b->section == NULL)
24432 || (a->section && strcmp (a->section, b->section) != 0)
24433 || !compare_locs (a->expr, b->expr))
24434 break;
24435 return a == NULL && b == NULL;
24438 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24441 /* Recursively optimize location lists referenced from DIE
24442 children and share them whenever possible. */
24444 static void
24445 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24447 dw_die_ref c;
24448 dw_attr_ref a;
24449 unsigned ix;
24450 dw_loc_list_struct **slot;
24452 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24453 if (AT_class (a) == dw_val_class_loc_list)
24455 dw_loc_list_ref list = AT_loc_list (a);
24456 /* TODO: perform some optimizations here, before hashing
24457 it and storing into the hash table. */
24458 hash_loc_list (list);
24459 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24460 if (*slot == NULL)
24461 *slot = list;
24462 else
24463 a->dw_attr_val.v.val_loc_list = *slot;
24466 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24470 /* Recursively assign each location list a unique index into the debug_addr
24471 section. */
24473 static void
24474 index_location_lists (dw_die_ref die)
24476 dw_die_ref c;
24477 dw_attr_ref a;
24478 unsigned ix;
24480 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24481 if (AT_class (a) == dw_val_class_loc_list)
24483 dw_loc_list_ref list = AT_loc_list (a);
24484 dw_loc_list_ref curr;
24485 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24487 /* Don't index an entry that has already been indexed
24488 or won't be output. */
24489 if (curr->begin_entry != NULL
24490 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24491 continue;
24493 curr->begin_entry
24494 = add_addr_table_entry (xstrdup (curr->begin),
24495 ate_kind_label);
24499 FOR_EACH_CHILD (die, c, index_location_lists (c));
24502 /* Optimize location lists referenced from DIE
24503 children and share them whenever possible. */
24505 static void
24506 optimize_location_lists (dw_die_ref die)
24508 loc_list_hash_type htab (500);
24509 optimize_location_lists_1 (die, &htab);
24512 /* Output stuff that dwarf requires at the end of every file,
24513 and generate the DWARF-2 debugging info. */
24515 static void
24516 dwarf2out_finish (const char *filename)
24518 limbo_die_node *node, *next_node;
24519 comdat_type_node *ctnode;
24520 unsigned int i;
24521 dw_die_ref main_comp_unit_die;
24523 /* PCH might result in DW_AT_producer string being restored from the
24524 header compilation, so always fill it with empty string initially
24525 and overwrite only here. */
24526 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24527 producer_string = gen_producer_string ();
24528 producer->dw_attr_val.v.val_str->refcount--;
24529 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24531 gen_scheduled_generic_parms_dies ();
24532 gen_remaining_tmpl_value_param_die_attribute ();
24534 /* Add the name for the main input file now. We delayed this from
24535 dwarf2out_init to avoid complications with PCH. */
24536 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24537 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24538 add_comp_dir_attribute (comp_unit_die ());
24539 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24541 bool p = false;
24542 file_table->traverse<bool *, file_table_relative_p> (&p);
24543 if (p)
24544 add_comp_dir_attribute (comp_unit_die ());
24547 if (deferred_locations_list)
24548 for (i = 0; i < deferred_locations_list->length (); i++)
24550 add_location_or_const_value_attribute (
24551 (*deferred_locations_list)[i].die,
24552 (*deferred_locations_list)[i].variable,
24553 false,
24554 DW_AT_location);
24557 /* Traverse the limbo die list, and add parent/child links. The only
24558 dies without parents that should be here are concrete instances of
24559 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24560 For concrete instances, we can get the parent die from the abstract
24561 instance. */
24562 for (node = limbo_die_list; node; node = next_node)
24564 dw_die_ref die = node->die;
24565 next_node = node->next;
24567 if (die->die_parent == NULL)
24569 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24571 if (origin && origin->die_parent)
24572 add_child_die (origin->die_parent, die);
24573 else if (is_cu_die (die))
24575 else if (seen_error ())
24576 /* It's OK to be confused by errors in the input. */
24577 add_child_die (comp_unit_die (), die);
24578 else
24580 /* In certain situations, the lexical block containing a
24581 nested function can be optimized away, which results
24582 in the nested function die being orphaned. Likewise
24583 with the return type of that nested function. Force
24584 this to be a child of the containing function.
24586 It may happen that even the containing function got fully
24587 inlined and optimized out. In that case we are lost and
24588 assign the empty child. This should not be big issue as
24589 the function is likely unreachable too. */
24590 gcc_assert (node->created_for);
24592 if (DECL_P (node->created_for))
24593 origin = get_context_die (DECL_CONTEXT (node->created_for));
24594 else if (TYPE_P (node->created_for))
24595 origin = scope_die_for (node->created_for, comp_unit_die ());
24596 else
24597 origin = comp_unit_die ();
24599 add_child_die (origin, die);
24604 limbo_die_list = NULL;
24606 #if ENABLE_ASSERT_CHECKING
24608 dw_die_ref die = comp_unit_die (), c;
24609 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24611 #endif
24612 resolve_addr (comp_unit_die ());
24613 move_marked_base_types ();
24615 for (node = deferred_asm_name; node; node = node->next)
24617 tree decl = node->created_for;
24618 /* When generating LTO bytecode we can not generate new assembler
24619 names at this point and all important decls got theirs via
24620 free-lang-data. */
24621 if (((!flag_generate_lto && !flag_generate_offload)
24622 || DECL_ASSEMBLER_NAME_SET_P (decl))
24623 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24625 add_linkage_attr (node->die, decl);
24626 move_linkage_attr (node->die);
24630 deferred_asm_name = NULL;
24632 /* Walk through the list of incomplete types again, trying once more to
24633 emit full debugging info for them. */
24634 retry_incomplete_types ();
24636 if (flag_eliminate_unused_debug_types)
24637 prune_unused_types ();
24639 /* Generate separate COMDAT sections for type DIEs. */
24640 if (use_debug_types)
24642 break_out_comdat_types (comp_unit_die ());
24644 /* Each new type_unit DIE was added to the limbo die list when created.
24645 Since these have all been added to comdat_type_list, clear the
24646 limbo die list. */
24647 limbo_die_list = NULL;
24649 /* For each new comdat type unit, copy declarations for incomplete
24650 types to make the new unit self-contained (i.e., no direct
24651 references to the main compile unit). */
24652 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24653 copy_decls_for_unworthy_types (ctnode->root_die);
24654 copy_decls_for_unworthy_types (comp_unit_die ());
24656 /* In the process of copying declarations from one unit to another,
24657 we may have left some declarations behind that are no longer
24658 referenced. Prune them. */
24659 prune_unused_types ();
24662 /* Generate separate CUs for each of the include files we've seen.
24663 They will go into limbo_die_list. */
24664 if (flag_eliminate_dwarf2_dups)
24665 break_out_includes (comp_unit_die ());
24667 /* Traverse the DIE's and add add sibling attributes to those DIE's
24668 that have children. */
24669 add_sibling_attributes (comp_unit_die ());
24670 for (node = limbo_die_list; node; node = node->next)
24671 add_sibling_attributes (node->die);
24672 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24673 add_sibling_attributes (ctnode->root_die);
24675 /* When splitting DWARF info, we put some attributes in the
24676 skeleton compile_unit DIE that remains in the .o, while
24677 most attributes go in the DWO compile_unit_die. */
24678 if (dwarf_split_debug_info)
24679 main_comp_unit_die = gen_compile_unit_die (NULL);
24680 else
24681 main_comp_unit_die = comp_unit_die ();
24683 /* Output a terminator label for the .text section. */
24684 switch_to_section (text_section);
24685 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24686 if (cold_text_section)
24688 switch_to_section (cold_text_section);
24689 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24692 /* We can only use the low/high_pc attributes if all of the code was
24693 in .text. */
24694 if (!have_multiple_function_sections
24695 || (dwarf_version < 3 && dwarf_strict))
24697 /* Don't add if the CU has no associated code. */
24698 if (text_section_used)
24699 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24700 text_end_label, true);
24702 else
24704 unsigned fde_idx;
24705 dw_fde_ref fde;
24706 bool range_list_added = false;
24708 if (text_section_used)
24709 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24710 text_end_label, &range_list_added, true);
24711 if (cold_text_section_used)
24712 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24713 cold_end_label, &range_list_added, true);
24715 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24717 if (DECL_IGNORED_P (fde->decl))
24718 continue;
24719 if (!fde->in_std_section)
24720 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24721 fde->dw_fde_end, &range_list_added,
24722 true);
24723 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24724 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24725 fde->dw_fde_second_end, &range_list_added,
24726 true);
24729 if (range_list_added)
24731 /* We need to give .debug_loc and .debug_ranges an appropriate
24732 "base address". Use zero so that these addresses become
24733 absolute. Historically, we've emitted the unexpected
24734 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24735 Emit both to give time for other tools to adapt. */
24736 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24737 if (! dwarf_strict && dwarf_version < 4)
24738 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24740 add_ranges (NULL);
24744 if (debug_info_level >= DINFO_LEVEL_TERSE)
24745 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24746 debug_line_section_label);
24748 if (have_macinfo)
24749 add_AT_macptr (comp_unit_die (),
24750 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24751 macinfo_section_label);
24753 if (dwarf_split_debug_info)
24755 /* optimize_location_lists calculates the size of the lists,
24756 so index them first, and assign indices to the entries.
24757 Although optimize_location_lists will remove entries from
24758 the table, it only does so for duplicates, and therefore
24759 only reduces ref_counts to 1. */
24760 index_location_lists (comp_unit_die ());
24762 if (addr_index_table != NULL)
24764 unsigned int index = 0;
24765 addr_index_table
24766 ->traverse_noresize<unsigned int *, index_addr_table_entry>
24767 (&index);
24771 if (have_location_lists)
24772 optimize_location_lists (comp_unit_die ());
24774 save_macinfo_strings ();
24776 if (dwarf_split_debug_info)
24778 unsigned int index = 0;
24780 /* Add attributes common to skeleton compile_units and
24781 type_units. Because these attributes include strings, it
24782 must be done before freezing the string table. Top-level
24783 skeleton die attrs are added when the skeleton type unit is
24784 created, so ensure it is created by this point. */
24785 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24786 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24789 /* Output all of the compilation units. We put the main one last so that
24790 the offsets are available to output_pubnames. */
24791 for (node = limbo_die_list; node; node = node->next)
24792 output_comp_unit (node->die, 0);
24794 hash_table<comdat_type_hasher> comdat_type_table (100);
24795 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24797 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24799 /* Don't output duplicate types. */
24800 if (*slot != HTAB_EMPTY_ENTRY)
24801 continue;
24803 /* Add a pointer to the line table for the main compilation unit
24804 so that the debugger can make sense of DW_AT_decl_file
24805 attributes. */
24806 if (debug_info_level >= DINFO_LEVEL_TERSE)
24807 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24808 (!dwarf_split_debug_info
24809 ? debug_line_section_label
24810 : debug_skeleton_line_section_label));
24812 output_comdat_type_unit (ctnode);
24813 *slot = ctnode;
24816 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24817 both the main_cu and all skeleton TUs. Making this call unconditional
24818 would end up either adding a second copy of the AT_pubnames attribute, or
24819 requiring a special case in add_top_level_skeleton_die_attrs. */
24820 if (!dwarf_split_debug_info)
24821 add_AT_pubnames (comp_unit_die ());
24823 if (dwarf_split_debug_info)
24825 int mark;
24826 unsigned char checksum[16];
24827 struct md5_ctx ctx;
24829 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24830 md5_init_ctx (&ctx);
24831 mark = 0;
24832 die_checksum (comp_unit_die (), &ctx, &mark);
24833 unmark_all_dies (comp_unit_die ());
24834 md5_finish_ctx (&ctx, checksum);
24836 /* Use the first 8 bytes of the checksum as the dwo_id,
24837 and add it to both comp-unit DIEs. */
24838 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24839 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24841 /* Add the base offset of the ranges table to the skeleton
24842 comp-unit DIE. */
24843 if (ranges_table_in_use)
24844 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24845 ranges_section_label);
24847 switch_to_section (debug_addr_section);
24848 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24849 output_addr_table ();
24852 /* Output the main compilation unit if non-empty or if .debug_macinfo
24853 or .debug_macro will be emitted. */
24854 output_comp_unit (comp_unit_die (), have_macinfo);
24856 if (dwarf_split_debug_info && info_section_emitted)
24857 output_skeleton_debug_sections (main_comp_unit_die);
24859 /* Output the abbreviation table. */
24860 if (abbrev_die_table_in_use != 1)
24862 switch_to_section (debug_abbrev_section);
24863 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24864 output_abbrev_section ();
24867 /* Output location list section if necessary. */
24868 if (have_location_lists)
24870 /* Output the location lists info. */
24871 switch_to_section (debug_loc_section);
24872 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24873 output_location_lists (comp_unit_die ());
24876 output_pubtables ();
24878 /* Output the address range information if a CU (.debug_info section)
24879 was emitted. We output an empty table even if we had no functions
24880 to put in it. This because the consumer has no way to tell the
24881 difference between an empty table that we omitted and failure to
24882 generate a table that would have contained data. */
24883 if (info_section_emitted)
24885 unsigned long aranges_length = size_of_aranges ();
24887 switch_to_section (debug_aranges_section);
24888 output_aranges (aranges_length);
24891 /* Output ranges section if necessary. */
24892 if (ranges_table_in_use)
24894 switch_to_section (debug_ranges_section);
24895 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24896 output_ranges ();
24899 /* Have to end the macro section. */
24900 if (have_macinfo)
24902 switch_to_section (debug_macinfo_section);
24903 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24904 output_macinfo ();
24905 dw2_asm_output_data (1, 0, "End compilation unit");
24908 /* Output the source line correspondence table. We must do this
24909 even if there is no line information. Otherwise, on an empty
24910 translation unit, we will generate a present, but empty,
24911 .debug_info section. IRIX 6.5 `nm' will then complain when
24912 examining the file. This is done late so that any filenames
24913 used by the debug_info section are marked as 'used'. */
24914 switch_to_section (debug_line_section);
24915 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24916 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24917 output_line_info (false);
24919 if (dwarf_split_debug_info && info_section_emitted)
24921 switch_to_section (debug_skeleton_line_section);
24922 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24923 output_line_info (true);
24926 /* If we emitted any indirect strings, output the string table too. */
24927 if (debug_str_hash || skeleton_debug_str_hash)
24928 output_indirect_strings ();
24931 /* Reset all state within dwarf2out.c so that we can rerun the compiler
24932 within the same process. For use by toplev::finalize. */
24934 void
24935 dwarf2out_c_finalize (void)
24937 last_var_location_insn = NULL;
24938 cached_next_real_insn = NULL;
24939 used_rtx_array = NULL;
24940 incomplete_types = NULL;
24941 decl_scope_table = NULL;
24942 debug_info_section = NULL;
24943 debug_skeleton_info_section = NULL;
24944 debug_abbrev_section = NULL;
24945 debug_skeleton_abbrev_section = NULL;
24946 debug_aranges_section = NULL;
24947 debug_addr_section = NULL;
24948 debug_macinfo_section = NULL;
24949 debug_line_section = NULL;
24950 debug_skeleton_line_section = NULL;
24951 debug_loc_section = NULL;
24952 debug_pubnames_section = NULL;
24953 debug_pubtypes_section = NULL;
24954 debug_str_section = NULL;
24955 debug_str_dwo_section = NULL;
24956 debug_str_offsets_section = NULL;
24957 debug_ranges_section = NULL;
24958 debug_frame_section = NULL;
24959 fde_vec = NULL;
24960 debug_str_hash = NULL;
24961 skeleton_debug_str_hash = NULL;
24962 dw2_string_counter = 0;
24963 have_multiple_function_sections = false;
24964 text_section_used = false;
24965 cold_text_section_used = false;
24966 cold_text_section = NULL;
24967 current_unit_personality = NULL;
24969 deferred_locations_list = NULL;
24971 next_die_offset = 0;
24972 single_comp_unit_die = NULL;
24973 comdat_type_list = NULL;
24974 limbo_die_list = NULL;
24975 deferred_asm_name = NULL;
24976 file_table = NULL;
24977 decl_die_table = NULL;
24978 common_block_die_table = NULL;
24979 decl_loc_table = NULL;
24980 call_arg_locations = NULL;
24981 call_arg_loc_last = NULL;
24982 call_site_count = -1;
24983 tail_call_site_count = -1;
24984 //block_map = NULL;
24985 cached_dw_loc_list_table = NULL;
24986 abbrev_die_table = NULL;
24987 abbrev_die_table_allocated = 0;
24988 abbrev_die_table_in_use = 0;
24989 line_info_label_num = 0;
24990 cur_line_info_table = NULL;
24991 text_section_line_info = NULL;
24992 cold_text_section_line_info = NULL;
24993 separate_line_info = NULL;
24994 info_section_emitted = false;
24995 pubname_table = NULL;
24996 pubtype_table = NULL;
24997 macinfo_table = NULL;
24998 ranges_table = NULL;
24999 ranges_table_allocated = 0;
25000 ranges_table_in_use = 0;
25001 ranges_by_label = 0;
25002 ranges_by_label_allocated = 0;
25003 ranges_by_label_in_use = 0;
25004 have_location_lists = false;
25005 loclabel_num = 0;
25006 poc_label_num = 0;
25007 last_emitted_file = NULL;
25008 label_num = 0;
25009 file_table_last_lookup = NULL;
25010 tmpl_value_parm_die_table = NULL;
25011 generic_type_instances = NULL;
25012 frame_pointer_fb_offset = 0;
25013 frame_pointer_fb_offset_valid = false;
25014 base_types.release ();
25015 XDELETEVEC (producer_string);
25016 producer_string = NULL;
25019 #include "gt-dwarf2out.h"