Merge trunk version 213968 into gupc branch.
[official-gcc.git] / gcc / dwarf2out.c
blob69f53741438549d30379ee76abca4d4447bc76bf
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "tree-upc.h"
65 #include "stringpool.h"
66 #include "stor-layout.h"
67 #include "varasm.h"
68 #include "function.h"
69 #include "emit-rtl.h"
70 #include "hash-table.h"
71 #include "version.h"
72 #include "flags.h"
73 #include "hard-reg-set.h"
74 #include "regs.h"
75 #include "rtlhash.h"
76 #include "insn-config.h"
77 #include "reload.h"
78 #include "function.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "except.h"
82 #include "dwarf2.h"
83 #include "dwarf2out.h"
84 #include "dwarf2asm.h"
85 #include "toplev.h"
86 #include "md5.h"
87 #include "tm_p.h"
88 #include "diagnostic.h"
89 #include "tree-pretty-print.h"
90 #include "debug.h"
91 #include "target.h"
92 #include "common/common-target.h"
93 #include "langhooks.h"
94 #include "cgraph.h"
95 #include "input.h"
96 #include "ira.h"
97 #include "lra.h"
98 #include "dumpfile.h"
99 #include "opts.h"
100 #include "tree-dfa.h"
101 #include "gdb/gdb-index.h"
103 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
104 static rtx last_var_location_insn;
105 static rtx cached_next_real_insn;
107 #ifdef VMS_DEBUGGING_INFO
108 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
110 /* Define this macro to be a nonzero value if the directory specifications
111 which are output in the debug info should end with a separator. */
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
113 /* Define this macro to evaluate to a nonzero value if GCC should refrain
114 from generating indirect strings in DWARF2 debug information, for instance
115 if your target is stuck with an old version of GDB that is unable to
116 process them properly or uses VMS Debug. */
117 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
118 #else
119 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
120 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
121 #endif
123 /* ??? Poison these here until it can be done generically. They've been
124 totally replaced in this file; make sure it stays that way. */
125 #undef DWARF2_UNWIND_INFO
126 #undef DWARF2_FRAME_INFO
127 #if (GCC_VERSION >= 3000)
128 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
129 #endif
131 /* The size of the target's pointer type. */
132 #ifndef PTR_SIZE
133 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
134 #endif
136 /* Array of RTXes referenced by the debugging information, which therefore
137 must be kept around forever. */
138 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
140 /* A pointer to the base of a list of incomplete types which might be
141 completed at some later time. incomplete_types_list needs to be a
142 vec<tree, va_gc> *because we want to tell the garbage collector about
143 it. */
144 static GTY(()) vec<tree, va_gc> *incomplete_types;
146 /* A pointer to the base of a table of references to declaration
147 scopes. This table is a display which tracks the nesting
148 of declaration scopes at the current scope and containing
149 scopes. This table is used to find the proper place to
150 define type declaration DIE's. */
151 static GTY(()) vec<tree, va_gc> *decl_scope_table;
153 /* Pointers to various DWARF2 sections. */
154 static GTY(()) section *debug_info_section;
155 static GTY(()) section *debug_skeleton_info_section;
156 static GTY(()) section *debug_abbrev_section;
157 static GTY(()) section *debug_skeleton_abbrev_section;
158 static GTY(()) section *debug_aranges_section;
159 static GTY(()) section *debug_addr_section;
160 static GTY(()) section *debug_macinfo_section;
161 static GTY(()) section *debug_line_section;
162 static GTY(()) section *debug_skeleton_line_section;
163 static GTY(()) section *debug_loc_section;
164 static GTY(()) section *debug_pubnames_section;
165 static GTY(()) section *debug_pubtypes_section;
166 static GTY(()) section *debug_str_section;
167 static GTY(()) section *debug_str_dwo_section;
168 static GTY(()) section *debug_str_offsets_section;
169 static GTY(()) section *debug_ranges_section;
170 static GTY(()) section *debug_frame_section;
172 /* Maximum size (in bytes) of an artificially generated label. */
173 #define MAX_ARTIFICIAL_LABEL_BYTES 30
175 /* According to the (draft) DWARF 3 specification, the initial length
176 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
177 bytes are 0xffffffff, followed by the length stored in the next 8
178 bytes.
180 However, the SGI/MIPS ABI uses an initial length which is equal to
181 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
183 #ifndef DWARF_INITIAL_LENGTH_SIZE
184 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
185 #endif
187 /* Round SIZE up to the nearest BOUNDARY. */
188 #define DWARF_ROUND(SIZE,BOUNDARY) \
189 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
191 /* CIE identifier. */
192 #if HOST_BITS_PER_WIDE_INT >= 64
193 #define DWARF_CIE_ID \
194 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
195 #else
196 #define DWARF_CIE_ID DW_CIE_ID
197 #endif
200 /* A vector for a table that contains frame description
201 information for each routine. */
202 #define NOT_INDEXED (-1U)
203 #define NO_INDEX_ASSIGNED (-2U)
205 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
207 struct GTY(()) indirect_string_node {
208 const char *str;
209 unsigned int refcount;
210 enum dwarf_form form;
211 char *label;
212 unsigned int index;
215 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
217 /* With split_debug_info, both the comp_dir and dwo_name go in the
218 main object file, rather than the dwo, similar to the force_direct
219 parameter elsewhere but with additional complications:
221 1) The string is needed in both the main object file and the dwo.
222 That is, the comp_dir and dwo_name will appear in both places.
224 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
225 DW_FORM_GNU_str_index.
227 3) GCC chooses the form to use late, depending on the size and
228 reference count.
230 Rather than forcing the all debug string handling functions and
231 callers to deal with these complications, simply use a separate,
232 special-cased string table for any attribute that should go in the
233 main object file. This limits the complexity to just the places
234 that need it. */
236 static GTY ((param_is (struct indirect_string_node)))
237 htab_t skeleton_debug_str_hash;
239 static GTY(()) int dw2_string_counter;
241 /* True if the compilation unit places functions in more than one section. */
242 static GTY(()) bool have_multiple_function_sections = false;
244 /* Whether the default text and cold text sections have been used at all. */
246 static GTY(()) bool text_section_used = false;
247 static GTY(()) bool cold_text_section_used = false;
249 /* The default cold text section. */
250 static GTY(()) section *cold_text_section;
252 /* The DIE for C++1y 'auto' in a function return type. */
253 static GTY(()) dw_die_ref auto_die;
255 /* The DIE for C++1y 'decltype(auto)' in a function return type. */
256 static GTY(()) dw_die_ref decltype_auto_die;
258 /* Forward declarations for functions defined in this file. */
260 static char *stripattributes (const char *);
261 static void output_call_frame_info (int);
262 static void dwarf2out_note_section_used (void);
264 /* Personality decl of current unit. Used only when assembler does not support
265 personality CFI. */
266 static GTY(()) rtx current_unit_personality;
268 /* Data and reference forms for relocatable data. */
269 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
270 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
272 #ifndef DEBUG_FRAME_SECTION
273 #define DEBUG_FRAME_SECTION ".debug_frame"
274 #endif
276 #ifndef FUNC_BEGIN_LABEL
277 #define FUNC_BEGIN_LABEL "LFB"
278 #endif
280 #ifndef FUNC_END_LABEL
281 #define FUNC_END_LABEL "LFE"
282 #endif
284 #ifndef PROLOGUE_END_LABEL
285 #define PROLOGUE_END_LABEL "LPE"
286 #endif
288 #ifndef EPILOGUE_BEGIN_LABEL
289 #define EPILOGUE_BEGIN_LABEL "LEB"
290 #endif
292 #ifndef FRAME_BEGIN_LABEL
293 #define FRAME_BEGIN_LABEL "Lframe"
294 #endif
295 #define CIE_AFTER_SIZE_LABEL "LSCIE"
296 #define CIE_END_LABEL "LECIE"
297 #define FDE_LABEL "LSFDE"
298 #define FDE_AFTER_SIZE_LABEL "LASFDE"
299 #define FDE_END_LABEL "LEFDE"
300 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
301 #define LINE_NUMBER_END_LABEL "LELT"
302 #define LN_PROLOG_AS_LABEL "LASLTP"
303 #define LN_PROLOG_END_LABEL "LELTP"
304 #define DIE_LABEL_PREFIX "DW"
306 /* Match the base name of a file to the base name of a compilation unit. */
308 static int
309 matches_main_base (const char *path)
311 /* Cache the last query. */
312 static const char *last_path = NULL;
313 static int last_match = 0;
314 if (path != last_path)
316 const char *base;
317 int length = base_of_path (path, &base);
318 last_path = path;
319 last_match = (length == main_input_baselength
320 && memcmp (base, main_input_basename, length) == 0);
322 return last_match;
325 #ifdef DEBUG_DEBUG_STRUCT
327 static int
328 dump_struct_debug (tree type, enum debug_info_usage usage,
329 enum debug_struct_file criterion, int generic,
330 int matches, int result)
332 /* Find the type name. */
333 tree type_decl = TYPE_STUB_DECL (type);
334 tree t = type_decl;
335 const char *name = 0;
336 if (TREE_CODE (t) == TYPE_DECL)
337 t = DECL_NAME (t);
338 if (t)
339 name = IDENTIFIER_POINTER (t);
341 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
342 criterion,
343 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
344 matches ? "bas" : "hdr",
345 generic ? "gen" : "ord",
346 usage == DINFO_USAGE_DFN ? ";" :
347 usage == DINFO_USAGE_DIR_USE ? "." : "*",
348 result,
349 (void*) type_decl, name);
350 return result;
352 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
353 dump_struct_debug (type, usage, criterion, generic, matches, result)
355 #else
357 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
358 (result)
360 #endif
362 /* Get the number of HOST_WIDE_INTs needed to represent the precision
363 of the number. */
365 static unsigned int
366 get_full_len (const wide_int &op)
368 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
369 / HOST_BITS_PER_WIDE_INT);
372 static bool
373 should_emit_struct_debug (tree type, enum debug_info_usage usage)
375 enum debug_struct_file criterion;
376 tree type_decl;
377 bool generic = lang_hooks.types.generic_p (type);
379 if (generic)
380 criterion = debug_struct_generic[usage];
381 else
382 criterion = debug_struct_ordinary[usage];
384 if (criterion == DINFO_STRUCT_FILE_NONE)
385 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
386 if (criterion == DINFO_STRUCT_FILE_ANY)
387 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
389 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
391 if (type_decl != NULL)
393 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
394 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
396 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
397 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
400 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
403 /* Return a pointer to a copy of the section string name S with all
404 attributes stripped off, and an asterisk prepended (for assemble_name). */
406 static inline char *
407 stripattributes (const char *s)
409 char *stripped = XNEWVEC (char, strlen (s) + 2);
410 char *p = stripped;
412 *p++ = '*';
414 while (*s && *s != ',')
415 *p++ = *s++;
417 *p = '\0';
418 return stripped;
421 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
422 switch to the data section instead, and write out a synthetic start label
423 for collect2 the first time around. */
425 static void
426 switch_to_eh_frame_section (bool back)
428 tree label;
430 #ifdef EH_FRAME_SECTION_NAME
431 if (eh_frame_section == 0)
433 int flags;
435 if (EH_TABLES_CAN_BE_READ_ONLY)
437 int fde_encoding;
438 int per_encoding;
439 int lsda_encoding;
441 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
442 /*global=*/0);
443 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
444 /*global=*/1);
445 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
446 /*global=*/0);
447 flags = ((! flag_pic
448 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
449 && (fde_encoding & 0x70) != DW_EH_PE_aligned
450 && (per_encoding & 0x70) != DW_EH_PE_absptr
451 && (per_encoding & 0x70) != DW_EH_PE_aligned
452 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
453 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
454 ? 0 : SECTION_WRITE);
456 else
457 flags = SECTION_WRITE;
458 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
460 #endif /* EH_FRAME_SECTION_NAME */
462 if (eh_frame_section)
463 switch_to_section (eh_frame_section);
464 else
466 /* We have no special eh_frame section. Put the information in
467 the data section and emit special labels to guide collect2. */
468 switch_to_section (data_section);
470 if (!back)
472 label = get_file_function_name ("F");
473 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
474 targetm.asm_out.globalize_label (asm_out_file,
475 IDENTIFIER_POINTER (label));
476 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
481 /* Switch [BACK] to the eh or debug frame table section, depending on
482 FOR_EH. */
484 static void
485 switch_to_frame_table_section (int for_eh, bool back)
487 if (for_eh)
488 switch_to_eh_frame_section (back);
489 else
491 if (!debug_frame_section)
492 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
493 SECTION_DEBUG, NULL);
494 switch_to_section (debug_frame_section);
498 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
500 enum dw_cfi_oprnd_type
501 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
503 switch (cfi)
505 case DW_CFA_nop:
506 case DW_CFA_GNU_window_save:
507 case DW_CFA_remember_state:
508 case DW_CFA_restore_state:
509 return dw_cfi_oprnd_unused;
511 case DW_CFA_set_loc:
512 case DW_CFA_advance_loc1:
513 case DW_CFA_advance_loc2:
514 case DW_CFA_advance_loc4:
515 case DW_CFA_MIPS_advance_loc8:
516 return dw_cfi_oprnd_addr;
518 case DW_CFA_offset:
519 case DW_CFA_offset_extended:
520 case DW_CFA_def_cfa:
521 case DW_CFA_offset_extended_sf:
522 case DW_CFA_def_cfa_sf:
523 case DW_CFA_restore:
524 case DW_CFA_restore_extended:
525 case DW_CFA_undefined:
526 case DW_CFA_same_value:
527 case DW_CFA_def_cfa_register:
528 case DW_CFA_register:
529 case DW_CFA_expression:
530 return dw_cfi_oprnd_reg_num;
532 case DW_CFA_def_cfa_offset:
533 case DW_CFA_GNU_args_size:
534 case DW_CFA_def_cfa_offset_sf:
535 return dw_cfi_oprnd_offset;
537 case DW_CFA_def_cfa_expression:
538 return dw_cfi_oprnd_loc;
540 default:
541 gcc_unreachable ();
545 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
547 enum dw_cfi_oprnd_type
548 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
550 switch (cfi)
552 case DW_CFA_def_cfa:
553 case DW_CFA_def_cfa_sf:
554 case DW_CFA_offset:
555 case DW_CFA_offset_extended_sf:
556 case DW_CFA_offset_extended:
557 return dw_cfi_oprnd_offset;
559 case DW_CFA_register:
560 return dw_cfi_oprnd_reg_num;
562 case DW_CFA_expression:
563 return dw_cfi_oprnd_loc;
565 default:
566 return dw_cfi_oprnd_unused;
570 /* Output one FDE. */
572 static void
573 output_fde (dw_fde_ref fde, bool for_eh, bool second,
574 char *section_start_label, int fde_encoding, char *augmentation,
575 bool any_lsda_needed, int lsda_encoding)
577 const char *begin, *end;
578 static unsigned int j;
579 char l1[20], l2[20];
581 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
582 /* empty */ 0);
583 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
584 for_eh + j);
585 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
586 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
587 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
588 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
589 " indicating 64-bit DWARF extension");
590 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
591 "FDE Length");
592 ASM_OUTPUT_LABEL (asm_out_file, l1);
594 if (for_eh)
595 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
596 else
597 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
598 debug_frame_section, "FDE CIE offset");
600 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
601 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
603 if (for_eh)
605 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
606 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
607 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
608 "FDE initial location");
609 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
610 end, begin, "FDE address range");
612 else
614 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
615 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
618 if (augmentation[0])
620 if (any_lsda_needed)
622 int size = size_of_encoded_value (lsda_encoding);
624 if (lsda_encoding == DW_EH_PE_aligned)
626 int offset = ( 4 /* Length */
627 + 4 /* CIE offset */
628 + 2 * size_of_encoded_value (fde_encoding)
629 + 1 /* Augmentation size */ );
630 int pad = -offset & (PTR_SIZE - 1);
632 size += pad;
633 gcc_assert (size_of_uleb128 (size) == 1);
636 dw2_asm_output_data_uleb128 (size, "Augmentation size");
638 if (fde->uses_eh_lsda)
640 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
641 fde->funcdef_number);
642 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
643 gen_rtx_SYMBOL_REF (Pmode, l1),
644 false,
645 "Language Specific Data Area");
647 else
649 if (lsda_encoding == DW_EH_PE_aligned)
650 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
651 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
652 "Language Specific Data Area (none)");
655 else
656 dw2_asm_output_data_uleb128 (0, "Augmentation size");
659 /* Loop through the Call Frame Instructions associated with this FDE. */
660 fde->dw_fde_current_label = begin;
662 size_t from, until, i;
664 from = 0;
665 until = vec_safe_length (fde->dw_fde_cfi);
667 if (fde->dw_fde_second_begin == NULL)
669 else if (!second)
670 until = fde->dw_fde_switch_cfi_index;
671 else
672 from = fde->dw_fde_switch_cfi_index;
674 for (i = from; i < until; i++)
675 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
678 /* If we are to emit a ref/link from function bodies to their frame tables,
679 do it now. This is typically performed to make sure that tables
680 associated with functions are dragged with them and not discarded in
681 garbage collecting links. We need to do this on a per function basis to
682 cope with -ffunction-sections. */
684 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
685 /* Switch to the function section, emit the ref to the tables, and
686 switch *back* into the table section. */
687 switch_to_section (function_section (fde->decl));
688 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
689 switch_to_frame_table_section (for_eh, true);
690 #endif
692 /* Pad the FDE out to an address sized boundary. */
693 ASM_OUTPUT_ALIGN (asm_out_file,
694 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
695 ASM_OUTPUT_LABEL (asm_out_file, l2);
697 j += 2;
700 /* Return true if frame description entry FDE is needed for EH. */
702 static bool
703 fde_needed_for_eh_p (dw_fde_ref fde)
705 if (flag_asynchronous_unwind_tables)
706 return true;
708 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
709 return true;
711 if (fde->uses_eh_lsda)
712 return true;
714 /* If exceptions are enabled, we have collected nothrow info. */
715 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
716 return false;
718 return true;
721 /* Output the call frame information used to record information
722 that relates to calculating the frame pointer, and records the
723 location of saved registers. */
725 static void
726 output_call_frame_info (int for_eh)
728 unsigned int i;
729 dw_fde_ref fde;
730 dw_cfi_ref cfi;
731 char l1[20], l2[20], section_start_label[20];
732 bool any_lsda_needed = false;
733 char augmentation[6];
734 int augmentation_size;
735 int fde_encoding = DW_EH_PE_absptr;
736 int per_encoding = DW_EH_PE_absptr;
737 int lsda_encoding = DW_EH_PE_absptr;
738 int return_reg;
739 rtx personality = NULL;
740 int dw_cie_version;
742 /* Don't emit a CIE if there won't be any FDEs. */
743 if (!fde_vec)
744 return;
746 /* Nothing to do if the assembler's doing it all. */
747 if (dwarf2out_do_cfi_asm ())
748 return;
750 /* If we don't have any functions we'll want to unwind out of, don't emit
751 any EH unwind information. If we make FDEs linkonce, we may have to
752 emit an empty label for an FDE that wouldn't otherwise be emitted. We
753 want to avoid having an FDE kept around when the function it refers to
754 is discarded. Example where this matters: a primary function template
755 in C++ requires EH information, an explicit specialization doesn't. */
756 if (for_eh)
758 bool any_eh_needed = false;
760 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
762 if (fde->uses_eh_lsda)
763 any_eh_needed = any_lsda_needed = true;
764 else if (fde_needed_for_eh_p (fde))
765 any_eh_needed = true;
766 else if (TARGET_USES_WEAK_UNWIND_INFO)
767 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
770 if (!any_eh_needed)
771 return;
774 /* We're going to be generating comments, so turn on app. */
775 if (flag_debug_asm)
776 app_enable ();
778 /* Switch to the proper frame section, first time. */
779 switch_to_frame_table_section (for_eh, false);
781 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
782 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
784 /* Output the CIE. */
785 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
786 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
787 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
788 dw2_asm_output_data (4, 0xffffffff,
789 "Initial length escape value indicating 64-bit DWARF extension");
790 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
791 "Length of Common Information Entry");
792 ASM_OUTPUT_LABEL (asm_out_file, l1);
794 /* Now that the CIE pointer is PC-relative for EH,
795 use 0 to identify the CIE. */
796 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
797 (for_eh ? 0 : DWARF_CIE_ID),
798 "CIE Identifier Tag");
800 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
801 use CIE version 1, unless that would produce incorrect results
802 due to overflowing the return register column. */
803 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
804 dw_cie_version = 1;
805 if (return_reg >= 256 || dwarf_version > 2)
806 dw_cie_version = 3;
807 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
809 augmentation[0] = 0;
810 augmentation_size = 0;
812 personality = current_unit_personality;
813 if (for_eh)
815 char *p;
817 /* Augmentation:
818 z Indicates that a uleb128 is present to size the
819 augmentation section.
820 L Indicates the encoding (and thus presence) of
821 an LSDA pointer in the FDE augmentation.
822 R Indicates a non-default pointer encoding for
823 FDE code pointers.
824 P Indicates the presence of an encoding + language
825 personality routine in the CIE augmentation. */
827 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
828 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
829 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
831 p = augmentation + 1;
832 if (personality)
834 *p++ = 'P';
835 augmentation_size += 1 + size_of_encoded_value (per_encoding);
836 assemble_external_libcall (personality);
838 if (any_lsda_needed)
840 *p++ = 'L';
841 augmentation_size += 1;
843 if (fde_encoding != DW_EH_PE_absptr)
845 *p++ = 'R';
846 augmentation_size += 1;
848 if (p > augmentation + 1)
850 augmentation[0] = 'z';
851 *p = '\0';
854 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
855 if (personality && per_encoding == DW_EH_PE_aligned)
857 int offset = ( 4 /* Length */
858 + 4 /* CIE Id */
859 + 1 /* CIE version */
860 + strlen (augmentation) + 1 /* Augmentation */
861 + size_of_uleb128 (1) /* Code alignment */
862 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
863 + 1 /* RA column */
864 + 1 /* Augmentation size */
865 + 1 /* Personality encoding */ );
866 int pad = -offset & (PTR_SIZE - 1);
868 augmentation_size += pad;
870 /* Augmentations should be small, so there's scarce need to
871 iterate for a solution. Die if we exceed one uleb128 byte. */
872 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
876 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
877 if (dw_cie_version >= 4)
879 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
880 dw2_asm_output_data (1, 0, "CIE Segment Size");
882 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
883 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
884 "CIE Data Alignment Factor");
886 if (dw_cie_version == 1)
887 dw2_asm_output_data (1, return_reg, "CIE RA Column");
888 else
889 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
891 if (augmentation[0])
893 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
894 if (personality)
896 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
897 eh_data_format_name (per_encoding));
898 dw2_asm_output_encoded_addr_rtx (per_encoding,
899 personality,
900 true, NULL);
903 if (any_lsda_needed)
904 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
905 eh_data_format_name (lsda_encoding));
907 if (fde_encoding != DW_EH_PE_absptr)
908 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
909 eh_data_format_name (fde_encoding));
912 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
913 output_cfi (cfi, NULL, for_eh);
915 /* Pad the CIE out to an address sized boundary. */
916 ASM_OUTPUT_ALIGN (asm_out_file,
917 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
918 ASM_OUTPUT_LABEL (asm_out_file, l2);
920 /* Loop through all of the FDE's. */
921 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
923 unsigned int k;
925 /* Don't emit EH unwind info for leaf functions that don't need it. */
926 if (for_eh && !fde_needed_for_eh_p (fde))
927 continue;
929 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
930 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
931 augmentation, any_lsda_needed, lsda_encoding);
934 if (for_eh && targetm.terminate_dw2_eh_frame_info)
935 dw2_asm_output_data (4, 0, "End of Table");
937 /* Turn off app to make assembly quicker. */
938 if (flag_debug_asm)
939 app_disable ();
942 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
944 static void
945 dwarf2out_do_cfi_startproc (bool second)
947 int enc;
948 rtx ref;
949 rtx personality = get_personality_function (current_function_decl);
951 fprintf (asm_out_file, "\t.cfi_startproc\n");
953 if (personality)
955 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
956 ref = personality;
958 /* ??? The GAS support isn't entirely consistent. We have to
959 handle indirect support ourselves, but PC-relative is done
960 in the assembler. Further, the assembler can't handle any
961 of the weirder relocation types. */
962 if (enc & DW_EH_PE_indirect)
963 ref = dw2_force_const_mem (ref, true);
965 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
966 output_addr_const (asm_out_file, ref);
967 fputc ('\n', asm_out_file);
970 if (crtl->uses_eh_lsda)
972 char lab[20];
974 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
975 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
976 current_function_funcdef_no);
977 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
978 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
980 if (enc & DW_EH_PE_indirect)
981 ref = dw2_force_const_mem (ref, true);
983 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
984 output_addr_const (asm_out_file, ref);
985 fputc ('\n', asm_out_file);
989 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
990 this allocation may be done before pass_final. */
992 dw_fde_ref
993 dwarf2out_alloc_current_fde (void)
995 dw_fde_ref fde;
997 fde = ggc_cleared_alloc<dw_fde_node> ();
998 fde->decl = current_function_decl;
999 fde->funcdef_number = current_function_funcdef_no;
1000 fde->fde_index = vec_safe_length (fde_vec);
1001 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1002 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1003 fde->nothrow = crtl->nothrow;
1004 fde->drap_reg = INVALID_REGNUM;
1005 fde->vdrap_reg = INVALID_REGNUM;
1007 /* Record the FDE associated with this function. */
1008 cfun->fde = fde;
1009 vec_safe_push (fde_vec, fde);
1011 return fde;
1014 /* Output a marker (i.e. a label) for the beginning of a function, before
1015 the prologue. */
1017 void
1018 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1019 const char *file ATTRIBUTE_UNUSED)
1021 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1022 char * dup_label;
1023 dw_fde_ref fde;
1024 section *fnsec;
1025 bool do_frame;
1027 current_function_func_begin_label = NULL;
1029 do_frame = dwarf2out_do_frame ();
1031 /* ??? current_function_func_begin_label is also used by except.c for
1032 call-site information. We must emit this label if it might be used. */
1033 if (!do_frame
1034 && (!flag_exceptions
1035 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1036 return;
1038 fnsec = function_section (current_function_decl);
1039 switch_to_section (fnsec);
1040 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1041 current_function_funcdef_no);
1042 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1043 current_function_funcdef_no);
1044 dup_label = xstrdup (label);
1045 current_function_func_begin_label = dup_label;
1047 /* We can elide the fde allocation if we're not emitting debug info. */
1048 if (!do_frame)
1049 return;
1051 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1052 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1053 would include pass_dwarf2_frame. If we've not created the FDE yet,
1054 do so now. */
1055 fde = cfun->fde;
1056 if (fde == NULL)
1057 fde = dwarf2out_alloc_current_fde ();
1059 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1060 fde->dw_fde_begin = dup_label;
1061 fde->dw_fde_current_label = dup_label;
1062 fde->in_std_section = (fnsec == text_section
1063 || (cold_text_section && fnsec == cold_text_section));
1065 /* We only want to output line number information for the genuine dwarf2
1066 prologue case, not the eh frame case. */
1067 #ifdef DWARF2_DEBUGGING_INFO
1068 if (file)
1069 dwarf2out_source_line (line, file, 0, true);
1070 #endif
1072 if (dwarf2out_do_cfi_asm ())
1073 dwarf2out_do_cfi_startproc (false);
1074 else
1076 rtx personality = get_personality_function (current_function_decl);
1077 if (!current_unit_personality)
1078 current_unit_personality = personality;
1080 /* We cannot keep a current personality per function as without CFI
1081 asm, at the point where we emit the CFI data, there is no current
1082 function anymore. */
1083 if (personality && current_unit_personality != personality)
1084 sorry ("multiple EH personalities are supported only with assemblers "
1085 "supporting .cfi_personality directive");
1089 /* Output a marker (i.e. a label) for the end of the generated code
1090 for a function prologue. This gets called *after* the prologue code has
1091 been generated. */
1093 void
1094 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1095 const char *file ATTRIBUTE_UNUSED)
1097 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1099 /* Output a label to mark the endpoint of the code generated for this
1100 function. */
1101 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1102 current_function_funcdef_no);
1103 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1104 current_function_funcdef_no);
1105 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1108 /* Output a marker (i.e. a label) for the beginning of the generated code
1109 for a function epilogue. This gets called *before* the prologue code has
1110 been generated. */
1112 void
1113 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1114 const char *file ATTRIBUTE_UNUSED)
1116 dw_fde_ref fde = cfun->fde;
1117 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1119 if (fde->dw_fde_vms_begin_epilogue)
1120 return;
1122 /* Output a label to mark the endpoint of the code generated for this
1123 function. */
1124 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1125 current_function_funcdef_no);
1126 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1127 current_function_funcdef_no);
1128 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1131 /* Output a marker (i.e. a label) for the absolute end of the generated code
1132 for a function definition. This gets called *after* the epilogue code has
1133 been generated. */
1135 void
1136 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1137 const char *file ATTRIBUTE_UNUSED)
1139 dw_fde_ref fde;
1140 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1142 last_var_location_insn = NULL_RTX;
1143 cached_next_real_insn = NULL_RTX;
1145 if (dwarf2out_do_cfi_asm ())
1146 fprintf (asm_out_file, "\t.cfi_endproc\n");
1148 /* Output a label to mark the endpoint of the code generated for this
1149 function. */
1150 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1151 current_function_funcdef_no);
1152 ASM_OUTPUT_LABEL (asm_out_file, label);
1153 fde = cfun->fde;
1154 gcc_assert (fde != NULL);
1155 if (fde->dw_fde_second_begin == NULL)
1156 fde->dw_fde_end = xstrdup (label);
1159 void
1160 dwarf2out_frame_finish (void)
1162 /* Output call frame information. */
1163 if (targetm.debug_unwind_info () == UI_DWARF2)
1164 output_call_frame_info (0);
1166 /* Output another copy for the unwinder. */
1167 if ((flag_unwind_tables || flag_exceptions)
1168 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1169 output_call_frame_info (1);
1172 /* Note that the current function section is being used for code. */
1174 static void
1175 dwarf2out_note_section_used (void)
1177 section *sec = current_function_section ();
1178 if (sec == text_section)
1179 text_section_used = true;
1180 else if (sec == cold_text_section)
1181 cold_text_section_used = true;
1184 static void var_location_switch_text_section (void);
1185 static void set_cur_line_info_table (section *);
1187 void
1188 dwarf2out_switch_text_section (void)
1190 section *sect;
1191 dw_fde_ref fde = cfun->fde;
1193 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1195 if (!in_cold_section_p)
1197 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1198 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1199 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1201 else
1203 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1204 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1205 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1207 have_multiple_function_sections = true;
1209 /* There is no need to mark used sections when not debugging. */
1210 if (cold_text_section != NULL)
1211 dwarf2out_note_section_used ();
1213 if (dwarf2out_do_cfi_asm ())
1214 fprintf (asm_out_file, "\t.cfi_endproc\n");
1216 /* Now do the real section switch. */
1217 sect = current_function_section ();
1218 switch_to_section (sect);
1220 fde->second_in_std_section
1221 = (sect == text_section
1222 || (cold_text_section && sect == cold_text_section));
1224 if (dwarf2out_do_cfi_asm ())
1225 dwarf2out_do_cfi_startproc (true);
1227 var_location_switch_text_section ();
1229 if (cold_text_section != NULL)
1230 set_cur_line_info_table (sect);
1233 /* And now, the subset of the debugging information support code necessary
1234 for emitting location expressions. */
1236 /* Data about a single source file. */
1237 struct GTY(()) dwarf_file_data {
1238 const char * filename;
1239 int emitted_number;
1242 typedef struct GTY(()) deferred_locations_struct
1244 tree variable;
1245 dw_die_ref die;
1246 } deferred_locations;
1249 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1252 /* Describe an entry into the .debug_addr section. */
1254 enum ate_kind {
1255 ate_kind_rtx,
1256 ate_kind_rtx_dtprel,
1257 ate_kind_label
1260 typedef struct GTY(()) addr_table_entry_struct {
1261 enum ate_kind kind;
1262 unsigned int refcount;
1263 unsigned int index;
1264 union addr_table_entry_struct_union
1266 rtx GTY ((tag ("0"))) rtl;
1267 char * GTY ((tag ("1"))) label;
1269 GTY ((desc ("%1.kind"))) addr;
1271 addr_table_entry;
1273 /* Location lists are ranges + location descriptions for that range,
1274 so you can track variables that are in different places over
1275 their entire life. */
1276 typedef struct GTY(()) dw_loc_list_struct {
1277 dw_loc_list_ref dw_loc_next;
1278 const char *begin; /* Label and addr_entry for start of range */
1279 addr_table_entry *begin_entry;
1280 const char *end; /* Label for end of range */
1281 char *ll_symbol; /* Label for beginning of location list.
1282 Only on head of list */
1283 const char *section; /* Section this loclist is relative to */
1284 dw_loc_descr_ref expr;
1285 hashval_t hash;
1286 /* True if all addresses in this and subsequent lists are known to be
1287 resolved. */
1288 bool resolved_addr;
1289 /* True if this list has been replaced by dw_loc_next. */
1290 bool replaced;
1291 bool emitted;
1292 /* True if the range should be emitted even if begin and end
1293 are the same. */
1294 bool force;
1295 } dw_loc_list_node;
1297 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1299 /* Convert a DWARF stack opcode into its string name. */
1301 static const char *
1302 dwarf_stack_op_name (unsigned int op)
1304 const char *name = get_DW_OP_name (op);
1306 if (name != NULL)
1307 return name;
1309 return "OP_<unknown>";
1312 /* Return a pointer to a newly allocated location description. Location
1313 descriptions are simple expression terms that can be strung
1314 together to form more complicated location (address) descriptions. */
1316 static inline dw_loc_descr_ref
1317 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1318 unsigned HOST_WIDE_INT oprnd2)
1320 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1322 descr->dw_loc_opc = op;
1323 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1324 descr->dw_loc_oprnd1.val_entry = NULL;
1325 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1326 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1327 descr->dw_loc_oprnd2.val_entry = NULL;
1328 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1330 return descr;
1333 /* Return a pointer to a newly allocated location description for
1334 REG and OFFSET. */
1336 static inline dw_loc_descr_ref
1337 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1339 if (reg <= 31)
1340 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1341 offset, 0);
1342 else
1343 return new_loc_descr (DW_OP_bregx, reg, offset);
1346 /* Add a location description term to a location description expression. */
1348 static inline void
1349 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1351 dw_loc_descr_ref *d;
1353 /* Find the end of the chain. */
1354 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1357 *d = descr;
1360 /* Compare two location operands for exact equality. */
1362 static bool
1363 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1365 if (a->val_class != b->val_class)
1366 return false;
1367 switch (a->val_class)
1369 case dw_val_class_none:
1370 return true;
1371 case dw_val_class_addr:
1372 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1374 case dw_val_class_offset:
1375 case dw_val_class_unsigned_const:
1376 case dw_val_class_const:
1377 case dw_val_class_range_list:
1378 case dw_val_class_lineptr:
1379 case dw_val_class_macptr:
1380 /* These are all HOST_WIDE_INT, signed or unsigned. */
1381 return a->v.val_unsigned == b->v.val_unsigned;
1383 case dw_val_class_loc:
1384 return a->v.val_loc == b->v.val_loc;
1385 case dw_val_class_loc_list:
1386 return a->v.val_loc_list == b->v.val_loc_list;
1387 case dw_val_class_die_ref:
1388 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1389 case dw_val_class_fde_ref:
1390 return a->v.val_fde_index == b->v.val_fde_index;
1391 case dw_val_class_lbl_id:
1392 case dw_val_class_high_pc:
1393 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1394 case dw_val_class_str:
1395 return a->v.val_str == b->v.val_str;
1396 case dw_val_class_flag:
1397 return a->v.val_flag == b->v.val_flag;
1398 case dw_val_class_file:
1399 return a->v.val_file == b->v.val_file;
1400 case dw_val_class_decl_ref:
1401 return a->v.val_decl_ref == b->v.val_decl_ref;
1403 case dw_val_class_const_double:
1404 return (a->v.val_double.high == b->v.val_double.high
1405 && a->v.val_double.low == b->v.val_double.low);
1407 case dw_val_class_wide_int:
1408 return *a->v.val_wide == *b->v.val_wide;
1410 case dw_val_class_vec:
1412 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1413 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1415 return (a_len == b_len
1416 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1419 case dw_val_class_data8:
1420 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1422 case dw_val_class_vms_delta:
1423 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1424 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1426 gcc_unreachable ();
1429 /* Compare two location atoms for exact equality. */
1431 static bool
1432 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1434 if (a->dw_loc_opc != b->dw_loc_opc)
1435 return false;
1437 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1438 address size, but since we always allocate cleared storage it
1439 should be zero for other types of locations. */
1440 if (a->dtprel != b->dtprel)
1441 return false;
1443 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1444 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1447 /* Compare two complete location expressions for exact equality. */
1449 bool
1450 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1452 while (1)
1454 if (a == b)
1455 return true;
1456 if (a == NULL || b == NULL)
1457 return false;
1458 if (!loc_descr_equal_p_1 (a, b))
1459 return false;
1461 a = a->dw_loc_next;
1462 b = b->dw_loc_next;
1467 /* Add a constant OFFSET to a location expression. */
1469 static void
1470 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1472 dw_loc_descr_ref loc;
1473 HOST_WIDE_INT *p;
1475 gcc_assert (*list_head != NULL);
1477 if (!offset)
1478 return;
1480 /* Find the end of the chain. */
1481 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1484 p = NULL;
1485 if (loc->dw_loc_opc == DW_OP_fbreg
1486 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1487 p = &loc->dw_loc_oprnd1.v.val_int;
1488 else if (loc->dw_loc_opc == DW_OP_bregx)
1489 p = &loc->dw_loc_oprnd2.v.val_int;
1491 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1492 offset. Don't optimize if an signed integer overflow would happen. */
1493 if (p != NULL
1494 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1495 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1496 *p += offset;
1498 else if (offset > 0)
1499 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1501 else
1503 loc->dw_loc_next = int_loc_descriptor (-offset);
1504 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1508 /* Add a constant OFFSET to a location list. */
1510 static void
1511 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1513 dw_loc_list_ref d;
1514 for (d = list_head; d != NULL; d = d->dw_loc_next)
1515 loc_descr_plus_const (&d->expr, offset);
1518 #define DWARF_REF_SIZE \
1519 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1521 static unsigned long int get_base_type_offset (dw_die_ref);
1523 /* Return the size of a location descriptor. */
1525 static unsigned long
1526 size_of_loc_descr (dw_loc_descr_ref loc)
1528 unsigned long size = 1;
1530 switch (loc->dw_loc_opc)
1532 case DW_OP_addr:
1533 size += DWARF2_ADDR_SIZE;
1534 break;
1535 case DW_OP_GNU_addr_index:
1536 case DW_OP_GNU_const_index:
1537 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1538 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1539 break;
1540 case DW_OP_const1u:
1541 case DW_OP_const1s:
1542 size += 1;
1543 break;
1544 case DW_OP_const2u:
1545 case DW_OP_const2s:
1546 size += 2;
1547 break;
1548 case DW_OP_const4u:
1549 case DW_OP_const4s:
1550 size += 4;
1551 break;
1552 case DW_OP_const8u:
1553 case DW_OP_const8s:
1554 size += 8;
1555 break;
1556 case DW_OP_constu:
1557 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1558 break;
1559 case DW_OP_consts:
1560 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1561 break;
1562 case DW_OP_pick:
1563 size += 1;
1564 break;
1565 case DW_OP_plus_uconst:
1566 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1567 break;
1568 case DW_OP_skip:
1569 case DW_OP_bra:
1570 size += 2;
1571 break;
1572 case DW_OP_breg0:
1573 case DW_OP_breg1:
1574 case DW_OP_breg2:
1575 case DW_OP_breg3:
1576 case DW_OP_breg4:
1577 case DW_OP_breg5:
1578 case DW_OP_breg6:
1579 case DW_OP_breg7:
1580 case DW_OP_breg8:
1581 case DW_OP_breg9:
1582 case DW_OP_breg10:
1583 case DW_OP_breg11:
1584 case DW_OP_breg12:
1585 case DW_OP_breg13:
1586 case DW_OP_breg14:
1587 case DW_OP_breg15:
1588 case DW_OP_breg16:
1589 case DW_OP_breg17:
1590 case DW_OP_breg18:
1591 case DW_OP_breg19:
1592 case DW_OP_breg20:
1593 case DW_OP_breg21:
1594 case DW_OP_breg22:
1595 case DW_OP_breg23:
1596 case DW_OP_breg24:
1597 case DW_OP_breg25:
1598 case DW_OP_breg26:
1599 case DW_OP_breg27:
1600 case DW_OP_breg28:
1601 case DW_OP_breg29:
1602 case DW_OP_breg30:
1603 case DW_OP_breg31:
1604 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1605 break;
1606 case DW_OP_regx:
1607 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1608 break;
1609 case DW_OP_fbreg:
1610 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1611 break;
1612 case DW_OP_bregx:
1613 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1614 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1615 break;
1616 case DW_OP_piece:
1617 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1618 break;
1619 case DW_OP_bit_piece:
1620 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1621 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1622 break;
1623 case DW_OP_deref_size:
1624 case DW_OP_xderef_size:
1625 size += 1;
1626 break;
1627 case DW_OP_call2:
1628 size += 2;
1629 break;
1630 case DW_OP_call4:
1631 size += 4;
1632 break;
1633 case DW_OP_call_ref:
1634 size += DWARF_REF_SIZE;
1635 break;
1636 case DW_OP_implicit_value:
1637 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1638 + loc->dw_loc_oprnd1.v.val_unsigned;
1639 break;
1640 case DW_OP_GNU_implicit_pointer:
1641 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1642 break;
1643 case DW_OP_GNU_entry_value:
1645 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1646 size += size_of_uleb128 (op_size) + op_size;
1647 break;
1649 case DW_OP_GNU_const_type:
1651 unsigned long o
1652 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1653 size += size_of_uleb128 (o) + 1;
1654 switch (loc->dw_loc_oprnd2.val_class)
1656 case dw_val_class_vec:
1657 size += loc->dw_loc_oprnd2.v.val_vec.length
1658 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1659 break;
1660 case dw_val_class_const:
1661 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1662 break;
1663 case dw_val_class_const_double:
1664 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1665 break;
1666 case dw_val_class_wide_int:
1667 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1668 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1669 break;
1670 default:
1671 gcc_unreachable ();
1673 break;
1675 case DW_OP_GNU_regval_type:
1677 unsigned long o
1678 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1679 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1680 + size_of_uleb128 (o);
1682 break;
1683 case DW_OP_GNU_deref_type:
1685 unsigned long o
1686 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1687 size += 1 + size_of_uleb128 (o);
1689 break;
1690 case DW_OP_GNU_convert:
1691 case DW_OP_GNU_reinterpret:
1692 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1693 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1694 else
1696 unsigned long o
1697 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1698 size += size_of_uleb128 (o);
1700 break;
1701 case DW_OP_GNU_parameter_ref:
1702 size += 4;
1703 break;
1704 default:
1705 break;
1708 return size;
1711 /* Return the size of a series of location descriptors. */
1713 unsigned long
1714 size_of_locs (dw_loc_descr_ref loc)
1716 dw_loc_descr_ref l;
1717 unsigned long size;
1719 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1720 field, to avoid writing to a PCH file. */
1721 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1723 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1724 break;
1725 size += size_of_loc_descr (l);
1727 if (! l)
1728 return size;
1730 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1732 l->dw_loc_addr = size;
1733 size += size_of_loc_descr (l);
1736 return size;
1739 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1740 static void get_ref_die_offset_label (char *, dw_die_ref);
1741 static unsigned long int get_ref_die_offset (dw_die_ref);
1743 /* Output location description stack opcode's operands (if any).
1744 The for_eh_or_skip parameter controls whether register numbers are
1745 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1746 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1747 info). This should be suppressed for the cases that have not been converted
1748 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1750 static void
1751 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1753 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1754 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1756 switch (loc->dw_loc_opc)
1758 #ifdef DWARF2_DEBUGGING_INFO
1759 case DW_OP_const2u:
1760 case DW_OP_const2s:
1761 dw2_asm_output_data (2, val1->v.val_int, NULL);
1762 break;
1763 case DW_OP_const4u:
1764 if (loc->dtprel)
1766 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1767 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1768 val1->v.val_addr);
1769 fputc ('\n', asm_out_file);
1770 break;
1772 /* FALLTHRU */
1773 case DW_OP_const4s:
1774 dw2_asm_output_data (4, val1->v.val_int, NULL);
1775 break;
1776 case DW_OP_const8u:
1777 if (loc->dtprel)
1779 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1780 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1781 val1->v.val_addr);
1782 fputc ('\n', asm_out_file);
1783 break;
1785 /* FALLTHRU */
1786 case DW_OP_const8s:
1787 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1788 dw2_asm_output_data (8, val1->v.val_int, NULL);
1789 break;
1790 case DW_OP_skip:
1791 case DW_OP_bra:
1793 int offset;
1795 gcc_assert (val1->val_class == dw_val_class_loc);
1796 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1798 dw2_asm_output_data (2, offset, NULL);
1800 break;
1801 case DW_OP_implicit_value:
1802 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1803 switch (val2->val_class)
1805 case dw_val_class_const:
1806 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1807 break;
1808 case dw_val_class_vec:
1810 unsigned int elt_size = val2->v.val_vec.elt_size;
1811 unsigned int len = val2->v.val_vec.length;
1812 unsigned int i;
1813 unsigned char *p;
1815 if (elt_size > sizeof (HOST_WIDE_INT))
1817 elt_size /= 2;
1818 len *= 2;
1820 for (i = 0, p = val2->v.val_vec.array;
1821 i < len;
1822 i++, p += elt_size)
1823 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1824 "fp or vector constant word %u", i);
1826 break;
1827 case dw_val_class_const_double:
1829 unsigned HOST_WIDE_INT first, second;
1831 if (WORDS_BIG_ENDIAN)
1833 first = val2->v.val_double.high;
1834 second = val2->v.val_double.low;
1836 else
1838 first = val2->v.val_double.low;
1839 second = val2->v.val_double.high;
1841 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1842 first, NULL);
1843 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1844 second, NULL);
1846 break;
1847 case dw_val_class_wide_int:
1849 int i;
1850 int len = get_full_len (*val2->v.val_wide);
1851 if (WORDS_BIG_ENDIAN)
1852 for (i = len - 1; i >= 0; --i)
1853 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1854 val2->v.val_wide->elt (i), NULL);
1855 else
1856 for (i = 0; i < len; ++i)
1857 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1858 val2->v.val_wide->elt (i), NULL);
1860 break;
1861 case dw_val_class_addr:
1862 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1863 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1864 break;
1865 default:
1866 gcc_unreachable ();
1868 break;
1869 #else
1870 case DW_OP_const2u:
1871 case DW_OP_const2s:
1872 case DW_OP_const4u:
1873 case DW_OP_const4s:
1874 case DW_OP_const8u:
1875 case DW_OP_const8s:
1876 case DW_OP_skip:
1877 case DW_OP_bra:
1878 case DW_OP_implicit_value:
1879 /* We currently don't make any attempt to make sure these are
1880 aligned properly like we do for the main unwind info, so
1881 don't support emitting things larger than a byte if we're
1882 only doing unwinding. */
1883 gcc_unreachable ();
1884 #endif
1885 case DW_OP_const1u:
1886 case DW_OP_const1s:
1887 dw2_asm_output_data (1, val1->v.val_int, NULL);
1888 break;
1889 case DW_OP_constu:
1890 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1891 break;
1892 case DW_OP_consts:
1893 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1894 break;
1895 case DW_OP_pick:
1896 dw2_asm_output_data (1, val1->v.val_int, NULL);
1897 break;
1898 case DW_OP_plus_uconst:
1899 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1900 break;
1901 case DW_OP_breg0:
1902 case DW_OP_breg1:
1903 case DW_OP_breg2:
1904 case DW_OP_breg3:
1905 case DW_OP_breg4:
1906 case DW_OP_breg5:
1907 case DW_OP_breg6:
1908 case DW_OP_breg7:
1909 case DW_OP_breg8:
1910 case DW_OP_breg9:
1911 case DW_OP_breg10:
1912 case DW_OP_breg11:
1913 case DW_OP_breg12:
1914 case DW_OP_breg13:
1915 case DW_OP_breg14:
1916 case DW_OP_breg15:
1917 case DW_OP_breg16:
1918 case DW_OP_breg17:
1919 case DW_OP_breg18:
1920 case DW_OP_breg19:
1921 case DW_OP_breg20:
1922 case DW_OP_breg21:
1923 case DW_OP_breg22:
1924 case DW_OP_breg23:
1925 case DW_OP_breg24:
1926 case DW_OP_breg25:
1927 case DW_OP_breg26:
1928 case DW_OP_breg27:
1929 case DW_OP_breg28:
1930 case DW_OP_breg29:
1931 case DW_OP_breg30:
1932 case DW_OP_breg31:
1933 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1934 break;
1935 case DW_OP_regx:
1937 unsigned r = val1->v.val_unsigned;
1938 if (for_eh_or_skip >= 0)
1939 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1940 gcc_assert (size_of_uleb128 (r)
1941 == size_of_uleb128 (val1->v.val_unsigned));
1942 dw2_asm_output_data_uleb128 (r, NULL);
1944 break;
1945 case DW_OP_fbreg:
1946 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1947 break;
1948 case DW_OP_bregx:
1950 unsigned r = val1->v.val_unsigned;
1951 if (for_eh_or_skip >= 0)
1952 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1953 gcc_assert (size_of_uleb128 (r)
1954 == size_of_uleb128 (val1->v.val_unsigned));
1955 dw2_asm_output_data_uleb128 (r, NULL);
1956 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1958 break;
1959 case DW_OP_piece:
1960 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1961 break;
1962 case DW_OP_bit_piece:
1963 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1964 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1965 break;
1966 case DW_OP_deref_size:
1967 case DW_OP_xderef_size:
1968 dw2_asm_output_data (1, val1->v.val_int, NULL);
1969 break;
1971 case DW_OP_addr:
1972 if (loc->dtprel)
1974 if (targetm.asm_out.output_dwarf_dtprel)
1976 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1977 DWARF2_ADDR_SIZE,
1978 val1->v.val_addr);
1979 fputc ('\n', asm_out_file);
1981 else
1982 gcc_unreachable ();
1984 else
1986 #ifdef DWARF2_DEBUGGING_INFO
1987 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1988 #else
1989 gcc_unreachable ();
1990 #endif
1992 break;
1994 case DW_OP_GNU_addr_index:
1995 case DW_OP_GNU_const_index:
1996 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1997 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1998 "(index into .debug_addr)");
1999 break;
2001 case DW_OP_GNU_implicit_pointer:
2003 char label[MAX_ARTIFICIAL_LABEL_BYTES
2004 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2005 gcc_assert (val1->val_class == dw_val_class_die_ref);
2006 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2007 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2008 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2010 break;
2012 case DW_OP_GNU_entry_value:
2013 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2014 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2015 break;
2017 case DW_OP_GNU_const_type:
2019 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2020 gcc_assert (o);
2021 dw2_asm_output_data_uleb128 (o, NULL);
2022 switch (val2->val_class)
2024 case dw_val_class_const:
2025 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2026 dw2_asm_output_data (1, l, NULL);
2027 dw2_asm_output_data (l, val2->v.val_int, NULL);
2028 break;
2029 case dw_val_class_vec:
2031 unsigned int elt_size = val2->v.val_vec.elt_size;
2032 unsigned int len = val2->v.val_vec.length;
2033 unsigned int i;
2034 unsigned char *p;
2036 l = len * elt_size;
2037 dw2_asm_output_data (1, l, NULL);
2038 if (elt_size > sizeof (HOST_WIDE_INT))
2040 elt_size /= 2;
2041 len *= 2;
2043 for (i = 0, p = val2->v.val_vec.array;
2044 i < len;
2045 i++, p += elt_size)
2046 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2047 "fp or vector constant word %u", i);
2049 break;
2050 case dw_val_class_const_double:
2052 unsigned HOST_WIDE_INT first, second;
2053 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2055 dw2_asm_output_data (1, 2 * l, NULL);
2056 if (WORDS_BIG_ENDIAN)
2058 first = val2->v.val_double.high;
2059 second = val2->v.val_double.low;
2061 else
2063 first = val2->v.val_double.low;
2064 second = val2->v.val_double.high;
2066 dw2_asm_output_data (l, first, NULL);
2067 dw2_asm_output_data (l, second, NULL);
2069 break;
2070 case dw_val_class_wide_int:
2072 int i;
2073 int len = get_full_len (*val2->v.val_wide);
2074 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2076 dw2_asm_output_data (1, len * l, NULL);
2077 if (WORDS_BIG_ENDIAN)
2078 for (i = len - 1; i >= 0; --i)
2079 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2080 else
2081 for (i = 0; i < len; ++i)
2082 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2084 break;
2085 default:
2086 gcc_unreachable ();
2089 break;
2090 case DW_OP_GNU_regval_type:
2092 unsigned r = val1->v.val_unsigned;
2093 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2094 gcc_assert (o);
2095 if (for_eh_or_skip >= 0)
2097 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2098 gcc_assert (size_of_uleb128 (r)
2099 == size_of_uleb128 (val1->v.val_unsigned));
2101 dw2_asm_output_data_uleb128 (r, NULL);
2102 dw2_asm_output_data_uleb128 (o, NULL);
2104 break;
2105 case DW_OP_GNU_deref_type:
2107 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2108 gcc_assert (o);
2109 dw2_asm_output_data (1, val1->v.val_int, NULL);
2110 dw2_asm_output_data_uleb128 (o, NULL);
2112 break;
2113 case DW_OP_GNU_convert:
2114 case DW_OP_GNU_reinterpret:
2115 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2116 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2117 else
2119 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2120 gcc_assert (o);
2121 dw2_asm_output_data_uleb128 (o, NULL);
2123 break;
2125 case DW_OP_GNU_parameter_ref:
2127 unsigned long o;
2128 gcc_assert (val1->val_class == dw_val_class_die_ref);
2129 o = get_ref_die_offset (val1->v.val_die_ref.die);
2130 dw2_asm_output_data (4, o, NULL);
2132 break;
2134 default:
2135 /* Other codes have no operands. */
2136 break;
2140 /* Output a sequence of location operations.
2141 The for_eh_or_skip parameter controls whether register numbers are
2142 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2143 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2144 info). This should be suppressed for the cases that have not been converted
2145 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2147 void
2148 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2150 for (; loc != NULL; loc = loc->dw_loc_next)
2152 enum dwarf_location_atom opc = loc->dw_loc_opc;
2153 /* Output the opcode. */
2154 if (for_eh_or_skip >= 0
2155 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2157 unsigned r = (opc - DW_OP_breg0);
2158 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2159 gcc_assert (r <= 31);
2160 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2162 else if (for_eh_or_skip >= 0
2163 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2165 unsigned r = (opc - DW_OP_reg0);
2166 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2167 gcc_assert (r <= 31);
2168 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2171 dw2_asm_output_data (1, opc,
2172 "%s", dwarf_stack_op_name (opc));
2174 /* Output the operand(s) (if any). */
2175 output_loc_operands (loc, for_eh_or_skip);
2179 /* Output location description stack opcode's operands (if any).
2180 The output is single bytes on a line, suitable for .cfi_escape. */
2182 static void
2183 output_loc_operands_raw (dw_loc_descr_ref loc)
2185 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2186 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2188 switch (loc->dw_loc_opc)
2190 case DW_OP_addr:
2191 case DW_OP_GNU_addr_index:
2192 case DW_OP_GNU_const_index:
2193 case DW_OP_implicit_value:
2194 /* We cannot output addresses in .cfi_escape, only bytes. */
2195 gcc_unreachable ();
2197 case DW_OP_const1u:
2198 case DW_OP_const1s:
2199 case DW_OP_pick:
2200 case DW_OP_deref_size:
2201 case DW_OP_xderef_size:
2202 fputc (',', asm_out_file);
2203 dw2_asm_output_data_raw (1, val1->v.val_int);
2204 break;
2206 case DW_OP_const2u:
2207 case DW_OP_const2s:
2208 fputc (',', asm_out_file);
2209 dw2_asm_output_data_raw (2, val1->v.val_int);
2210 break;
2212 case DW_OP_const4u:
2213 case DW_OP_const4s:
2214 fputc (',', asm_out_file);
2215 dw2_asm_output_data_raw (4, val1->v.val_int);
2216 break;
2218 case DW_OP_const8u:
2219 case DW_OP_const8s:
2220 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2221 fputc (',', asm_out_file);
2222 dw2_asm_output_data_raw (8, val1->v.val_int);
2223 break;
2225 case DW_OP_skip:
2226 case DW_OP_bra:
2228 int offset;
2230 gcc_assert (val1->val_class == dw_val_class_loc);
2231 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2233 fputc (',', asm_out_file);
2234 dw2_asm_output_data_raw (2, offset);
2236 break;
2238 case DW_OP_regx:
2240 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2241 gcc_assert (size_of_uleb128 (r)
2242 == size_of_uleb128 (val1->v.val_unsigned));
2243 fputc (',', asm_out_file);
2244 dw2_asm_output_data_uleb128_raw (r);
2246 break;
2248 case DW_OP_constu:
2249 case DW_OP_plus_uconst:
2250 case DW_OP_piece:
2251 fputc (',', asm_out_file);
2252 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2253 break;
2255 case DW_OP_bit_piece:
2256 fputc (',', asm_out_file);
2257 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2258 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2259 break;
2261 case DW_OP_consts:
2262 case DW_OP_breg0:
2263 case DW_OP_breg1:
2264 case DW_OP_breg2:
2265 case DW_OP_breg3:
2266 case DW_OP_breg4:
2267 case DW_OP_breg5:
2268 case DW_OP_breg6:
2269 case DW_OP_breg7:
2270 case DW_OP_breg8:
2271 case DW_OP_breg9:
2272 case DW_OP_breg10:
2273 case DW_OP_breg11:
2274 case DW_OP_breg12:
2275 case DW_OP_breg13:
2276 case DW_OP_breg14:
2277 case DW_OP_breg15:
2278 case DW_OP_breg16:
2279 case DW_OP_breg17:
2280 case DW_OP_breg18:
2281 case DW_OP_breg19:
2282 case DW_OP_breg20:
2283 case DW_OP_breg21:
2284 case DW_OP_breg22:
2285 case DW_OP_breg23:
2286 case DW_OP_breg24:
2287 case DW_OP_breg25:
2288 case DW_OP_breg26:
2289 case DW_OP_breg27:
2290 case DW_OP_breg28:
2291 case DW_OP_breg29:
2292 case DW_OP_breg30:
2293 case DW_OP_breg31:
2294 case DW_OP_fbreg:
2295 fputc (',', asm_out_file);
2296 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2297 break;
2299 case DW_OP_bregx:
2301 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2302 gcc_assert (size_of_uleb128 (r)
2303 == size_of_uleb128 (val1->v.val_unsigned));
2304 fputc (',', asm_out_file);
2305 dw2_asm_output_data_uleb128_raw (r);
2306 fputc (',', asm_out_file);
2307 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2309 break;
2311 case DW_OP_GNU_implicit_pointer:
2312 case DW_OP_GNU_entry_value:
2313 case DW_OP_GNU_const_type:
2314 case DW_OP_GNU_regval_type:
2315 case DW_OP_GNU_deref_type:
2316 case DW_OP_GNU_convert:
2317 case DW_OP_GNU_reinterpret:
2318 case DW_OP_GNU_parameter_ref:
2319 gcc_unreachable ();
2320 break;
2322 default:
2323 /* Other codes have no operands. */
2324 break;
2328 void
2329 output_loc_sequence_raw (dw_loc_descr_ref loc)
2331 while (1)
2333 enum dwarf_location_atom opc = loc->dw_loc_opc;
2334 /* Output the opcode. */
2335 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2337 unsigned r = (opc - DW_OP_breg0);
2338 r = DWARF2_FRAME_REG_OUT (r, 1);
2339 gcc_assert (r <= 31);
2340 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2342 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2344 unsigned r = (opc - DW_OP_reg0);
2345 r = DWARF2_FRAME_REG_OUT (r, 1);
2346 gcc_assert (r <= 31);
2347 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2349 /* Output the opcode. */
2350 fprintf (asm_out_file, "%#x", opc);
2351 output_loc_operands_raw (loc);
2353 if (!loc->dw_loc_next)
2354 break;
2355 loc = loc->dw_loc_next;
2357 fputc (',', asm_out_file);
2361 /* This function builds a dwarf location descriptor sequence from a
2362 dw_cfa_location, adding the given OFFSET to the result of the
2363 expression. */
2365 struct dw_loc_descr_node *
2366 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2368 struct dw_loc_descr_node *head, *tmp;
2370 offset += cfa->offset;
2372 if (cfa->indirect)
2374 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2375 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2376 head->dw_loc_oprnd1.val_entry = NULL;
2377 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2378 add_loc_descr (&head, tmp);
2379 if (offset != 0)
2381 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2382 add_loc_descr (&head, tmp);
2385 else
2386 head = new_reg_loc_descr (cfa->reg, offset);
2388 return head;
2391 /* This function builds a dwarf location descriptor sequence for
2392 the address at OFFSET from the CFA when stack is aligned to
2393 ALIGNMENT byte. */
2395 struct dw_loc_descr_node *
2396 build_cfa_aligned_loc (dw_cfa_location *cfa,
2397 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2399 struct dw_loc_descr_node *head;
2400 unsigned int dwarf_fp
2401 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2403 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2404 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2406 head = new_reg_loc_descr (dwarf_fp, 0);
2407 add_loc_descr (&head, int_loc_descriptor (alignment));
2408 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2409 loc_descr_plus_const (&head, offset);
2411 else
2412 head = new_reg_loc_descr (dwarf_fp, offset);
2413 return head;
2416 /* And now, the support for symbolic debugging information. */
2418 /* .debug_str support. */
2419 static int output_indirect_string (void **, void *);
2421 static void dwarf2out_init (const char *);
2422 static void dwarf2out_finish (const char *);
2423 static void dwarf2out_assembly_start (void);
2424 static void dwarf2out_define (unsigned int, const char *);
2425 static void dwarf2out_undef (unsigned int, const char *);
2426 static void dwarf2out_start_source_file (unsigned, const char *);
2427 static void dwarf2out_end_source_file (unsigned);
2428 static void dwarf2out_function_decl (tree);
2429 static void dwarf2out_begin_block (unsigned, unsigned);
2430 static void dwarf2out_end_block (unsigned, unsigned);
2431 static bool dwarf2out_ignore_block (const_tree);
2432 static void dwarf2out_global_decl (tree);
2433 static void dwarf2out_type_decl (tree, int);
2434 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2435 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2436 dw_die_ref);
2437 static void dwarf2out_abstract_function (tree);
2438 static void dwarf2out_var_location (rtx);
2439 static void dwarf2out_begin_function (tree);
2440 static void dwarf2out_end_function (unsigned int);
2441 static void dwarf2out_set_name (tree, tree);
2443 /* The debug hooks structure. */
2445 const struct gcc_debug_hooks dwarf2_debug_hooks =
2447 dwarf2out_init,
2448 dwarf2out_finish,
2449 dwarf2out_assembly_start,
2450 dwarf2out_define,
2451 dwarf2out_undef,
2452 dwarf2out_start_source_file,
2453 dwarf2out_end_source_file,
2454 dwarf2out_begin_block,
2455 dwarf2out_end_block,
2456 dwarf2out_ignore_block,
2457 dwarf2out_source_line,
2458 dwarf2out_begin_prologue,
2459 #if VMS_DEBUGGING_INFO
2460 dwarf2out_vms_end_prologue,
2461 dwarf2out_vms_begin_epilogue,
2462 #else
2463 debug_nothing_int_charstar,
2464 debug_nothing_int_charstar,
2465 #endif
2466 dwarf2out_end_epilogue,
2467 dwarf2out_begin_function,
2468 dwarf2out_end_function, /* end_function */
2469 dwarf2out_function_decl, /* function_decl */
2470 dwarf2out_global_decl,
2471 dwarf2out_type_decl, /* type_decl */
2472 dwarf2out_imported_module_or_decl,
2473 debug_nothing_tree, /* deferred_inline_function */
2474 /* The DWARF 2 backend tries to reduce debugging bloat by not
2475 emitting the abstract description of inline functions until
2476 something tries to reference them. */
2477 dwarf2out_abstract_function, /* outlining_inline_function */
2478 debug_nothing_rtx, /* label */
2479 debug_nothing_int, /* handle_pch */
2480 dwarf2out_var_location,
2481 dwarf2out_switch_text_section,
2482 dwarf2out_set_name,
2483 1, /* start_end_main_source_file */
2484 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2487 /* NOTE: In the comments in this file, many references are made to
2488 "Debugging Information Entries". This term is abbreviated as `DIE'
2489 throughout the remainder of this file. */
2491 /* An internal representation of the DWARF output is built, and then
2492 walked to generate the DWARF debugging info. The walk of the internal
2493 representation is done after the entire program has been compiled.
2494 The types below are used to describe the internal representation. */
2496 /* Whether to put type DIEs into their own section .debug_types instead
2497 of making them part of the .debug_info section. Only supported for
2498 Dwarf V4 or higher and the user didn't disable them through
2499 -fno-debug-types-section. It is more efficient to put them in a
2500 separate comdat sections since the linker will then be able to
2501 remove duplicates. But not all tools support .debug_types sections
2502 yet. */
2504 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2506 /* Various DIE's use offsets relative to the beginning of the
2507 .debug_info section to refer to each other. */
2509 typedef long int dw_offset;
2511 /* Define typedefs here to avoid circular dependencies. */
2513 typedef struct dw_attr_struct *dw_attr_ref;
2514 typedef struct dw_line_info_struct *dw_line_info_ref;
2515 typedef struct pubname_struct *pubname_ref;
2516 typedef struct dw_ranges_struct *dw_ranges_ref;
2517 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2518 typedef struct comdat_type_struct *comdat_type_node_ref;
2520 /* The entries in the line_info table more-or-less mirror the opcodes
2521 that are used in the real dwarf line table. Arrays of these entries
2522 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2523 supported. */
2525 enum dw_line_info_opcode {
2526 /* Emit DW_LNE_set_address; the operand is the label index. */
2527 LI_set_address,
2529 /* Emit a row to the matrix with the given line. This may be done
2530 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2531 special opcodes. */
2532 LI_set_line,
2534 /* Emit a DW_LNS_set_file. */
2535 LI_set_file,
2537 /* Emit a DW_LNS_set_column. */
2538 LI_set_column,
2540 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2541 LI_negate_stmt,
2543 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2544 LI_set_prologue_end,
2545 LI_set_epilogue_begin,
2547 /* Emit a DW_LNE_set_discriminator. */
2548 LI_set_discriminator
2551 typedef struct GTY(()) dw_line_info_struct {
2552 enum dw_line_info_opcode opcode;
2553 unsigned int val;
2554 } dw_line_info_entry;
2557 typedef struct GTY(()) dw_line_info_table_struct {
2558 /* The label that marks the end of this section. */
2559 const char *end_label;
2561 /* The values for the last row of the matrix, as collected in the table.
2562 These are used to minimize the changes to the next row. */
2563 unsigned int file_num;
2564 unsigned int line_num;
2565 unsigned int column_num;
2566 int discrim_num;
2567 bool is_stmt;
2568 bool in_use;
2570 vec<dw_line_info_entry, va_gc> *entries;
2571 } dw_line_info_table;
2573 typedef dw_line_info_table *dw_line_info_table_p;
2576 /* Each DIE attribute has a field specifying the attribute kind,
2577 a link to the next attribute in the chain, and an attribute value.
2578 Attributes are typically linked below the DIE they modify. */
2580 typedef struct GTY(()) dw_attr_struct {
2581 enum dwarf_attribute dw_attr;
2582 dw_val_node dw_attr_val;
2584 dw_attr_node;
2587 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2588 The children of each node form a circular list linked by
2589 die_sib. die_child points to the node *before* the "first" child node. */
2591 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2592 union die_symbol_or_type_node
2594 const char * GTY ((tag ("0"))) die_symbol;
2595 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2597 GTY ((desc ("%0.comdat_type_p"))) die_id;
2598 vec<dw_attr_node, va_gc> *die_attr;
2599 dw_die_ref die_parent;
2600 dw_die_ref die_child;
2601 dw_die_ref die_sib;
2602 dw_die_ref die_definition; /* ref from a specification to its definition */
2603 dw_offset die_offset;
2604 unsigned long die_abbrev;
2605 int die_mark;
2606 unsigned int decl_id;
2607 enum dwarf_tag die_tag;
2608 /* Die is used and must not be pruned as unused. */
2609 BOOL_BITFIELD die_perennial_p : 1;
2610 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2611 /* Lots of spare bits. */
2613 die_node;
2615 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2616 #define FOR_EACH_CHILD(die, c, expr) do { \
2617 c = die->die_child; \
2618 if (c) do { \
2619 c = c->die_sib; \
2620 expr; \
2621 } while (c != die->die_child); \
2622 } while (0)
2624 /* The pubname structure */
2626 typedef struct GTY(()) pubname_struct {
2627 dw_die_ref die;
2628 const char *name;
2630 pubname_entry;
2633 struct GTY(()) dw_ranges_struct {
2634 /* If this is positive, it's a block number, otherwise it's a
2635 bitwise-negated index into dw_ranges_by_label. */
2636 int num;
2639 /* A structure to hold a macinfo entry. */
2641 typedef struct GTY(()) macinfo_struct {
2642 unsigned char code;
2643 unsigned HOST_WIDE_INT lineno;
2644 const char *info;
2646 macinfo_entry;
2649 struct GTY(()) dw_ranges_by_label_struct {
2650 const char *begin;
2651 const char *end;
2654 /* The comdat type node structure. */
2655 typedef struct GTY(()) comdat_type_struct
2657 dw_die_ref root_die;
2658 dw_die_ref type_die;
2659 dw_die_ref skeleton_die;
2660 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2661 struct comdat_type_struct *next;
2663 comdat_type_node;
2665 /* The limbo die list structure. */
2666 typedef struct GTY(()) limbo_die_struct {
2667 dw_die_ref die;
2668 tree created_for;
2669 struct limbo_die_struct *next;
2671 limbo_die_node;
2673 typedef struct skeleton_chain_struct
2675 dw_die_ref old_die;
2676 dw_die_ref new_die;
2677 struct skeleton_chain_struct *parent;
2679 skeleton_chain_node;
2681 /* Define a macro which returns nonzero for a TYPE_DECL which was
2682 implicitly generated for a type.
2684 Note that, unlike the C front-end (which generates a NULL named
2685 TYPE_DECL node for each complete tagged type, each array type,
2686 and each function type node created) the C++ front-end generates
2687 a _named_ TYPE_DECL node for each tagged type node created.
2688 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2689 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2690 front-end, but for each type, tagged or not. */
2692 #define TYPE_DECL_IS_STUB(decl) \
2693 (DECL_NAME (decl) == NULL_TREE \
2694 || (DECL_ARTIFICIAL (decl) \
2695 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2696 /* This is necessary for stub decls that \
2697 appear in nested inline functions. */ \
2698 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2699 && (decl_ultimate_origin (decl) \
2700 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2702 /* Information concerning the compilation unit's programming
2703 language, and compiler version. */
2705 /* Fixed size portion of the DWARF compilation unit header. */
2706 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2707 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2709 /* Fixed size portion of the DWARF comdat type unit header. */
2710 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2711 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2712 + DWARF_OFFSET_SIZE)
2714 /* Fixed size portion of public names info. */
2715 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2717 /* Fixed size portion of the address range info. */
2718 #define DWARF_ARANGES_HEADER_SIZE \
2719 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2720 DWARF2_ADDR_SIZE * 2) \
2721 - DWARF_INITIAL_LENGTH_SIZE)
2723 /* Size of padding portion in the address range info. It must be
2724 aligned to twice the pointer size. */
2725 #define DWARF_ARANGES_PAD_SIZE \
2726 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2727 DWARF2_ADDR_SIZE * 2) \
2728 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2730 /* Use assembler line directives if available. */
2731 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2732 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2733 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2734 #else
2735 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2736 #endif
2737 #endif
2739 /* Minimum line offset in a special line info. opcode.
2740 This value was chosen to give a reasonable range of values. */
2741 #define DWARF_LINE_BASE -10
2743 /* First special line opcode - leave room for the standard opcodes. */
2744 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2746 /* Range of line offsets in a special line info. opcode. */
2747 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2749 /* Flag that indicates the initial value of the is_stmt_start flag.
2750 In the present implementation, we do not mark any lines as
2751 the beginning of a source statement, because that information
2752 is not made available by the GCC front-end. */
2753 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2755 /* Maximum number of operations per instruction bundle. */
2756 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2757 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2758 #endif
2760 /* This location is used by calc_die_sizes() to keep track
2761 the offset of each DIE within the .debug_info section. */
2762 static unsigned long next_die_offset;
2764 /* Record the root of the DIE's built for the current compilation unit. */
2765 static GTY(()) dw_die_ref single_comp_unit_die;
2767 /* A list of type DIEs that have been separated into comdat sections. */
2768 static GTY(()) comdat_type_node *comdat_type_list;
2770 /* A list of DIEs with a NULL parent waiting to be relocated. */
2771 static GTY(()) limbo_die_node *limbo_die_list;
2773 /* A list of DIEs for which we may have to generate
2774 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2775 static GTY(()) limbo_die_node *deferred_asm_name;
2777 /* Filenames referenced by this compilation unit. */
2778 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2780 /* A hash table of references to DIE's that describe declarations.
2781 The key is a DECL_UID() which is a unique number identifying each decl. */
2782 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2784 /* A hash table of references to DIE's that describe COMMON blocks.
2785 The key is DECL_UID() ^ die_parent. */
2786 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2788 typedef struct GTY(()) die_arg_entry_struct {
2789 dw_die_ref die;
2790 tree arg;
2791 } die_arg_entry;
2794 /* Node of the variable location list. */
2795 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2796 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2797 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2798 in mode of the EXPR_LIST node and first EXPR_LIST operand
2799 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2800 location or NULL for padding. For larger bitsizes,
2801 mode is 0 and first operand is a CONCAT with bitsize
2802 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2803 NULL as second operand. */
2804 rtx GTY (()) loc;
2805 const char * GTY (()) label;
2806 struct var_loc_node * GTY (()) next;
2809 /* Variable location list. */
2810 struct GTY (()) var_loc_list_def {
2811 struct var_loc_node * GTY (()) first;
2813 /* Pointer to the last but one or last element of the
2814 chained list. If the list is empty, both first and
2815 last are NULL, if the list contains just one node
2816 or the last node certainly is not redundant, it points
2817 to the last node, otherwise points to the last but one.
2818 Do not mark it for GC because it is marked through the chain. */
2819 struct var_loc_node * GTY ((skip ("%h"))) last;
2821 /* Pointer to the last element before section switch,
2822 if NULL, either sections weren't switched or first
2823 is after section switch. */
2824 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2826 /* DECL_UID of the variable decl. */
2827 unsigned int decl_id;
2829 typedef struct var_loc_list_def var_loc_list;
2831 /* Call argument location list. */
2832 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2833 rtx GTY (()) call_arg_loc_note;
2834 const char * GTY (()) label;
2835 tree GTY (()) block;
2836 bool tail_call_p;
2837 rtx GTY (()) symbol_ref;
2838 struct call_arg_loc_node * GTY (()) next;
2842 /* Table of decl location linked lists. */
2843 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2845 /* Head and tail of call_arg_loc chain. */
2846 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2847 static struct call_arg_loc_node *call_arg_loc_last;
2849 /* Number of call sites in the current function. */
2850 static int call_site_count = -1;
2851 /* Number of tail call sites in the current function. */
2852 static int tail_call_site_count = -1;
2854 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2855 DIEs. */
2856 static vec<dw_die_ref> block_map;
2858 /* A cached location list. */
2859 struct GTY (()) cached_dw_loc_list_def {
2860 /* The DECL_UID of the decl that this entry describes. */
2861 unsigned int decl_id;
2863 /* The cached location list. */
2864 dw_loc_list_ref loc_list;
2866 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2868 /* Table of cached location lists. */
2869 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2871 /* A pointer to the base of a list of references to DIE's that
2872 are uniquely identified by their tag, presence/absence of
2873 children DIE's, and list of attribute/value pairs. */
2874 static GTY((length ("abbrev_die_table_allocated")))
2875 dw_die_ref *abbrev_die_table;
2877 /* Number of elements currently allocated for abbrev_die_table. */
2878 static GTY(()) unsigned abbrev_die_table_allocated;
2880 /* Number of elements in type_die_table currently in use. */
2881 static GTY(()) unsigned abbrev_die_table_in_use;
2883 /* Size (in elements) of increments by which we may expand the
2884 abbrev_die_table. */
2885 #define ABBREV_DIE_TABLE_INCREMENT 256
2887 /* A global counter for generating labels for line number data. */
2888 static unsigned int line_info_label_num;
2890 /* The current table to which we should emit line number information
2891 for the current function. This will be set up at the beginning of
2892 assembly for the function. */
2893 static dw_line_info_table *cur_line_info_table;
2895 /* The two default tables of line number info. */
2896 static GTY(()) dw_line_info_table *text_section_line_info;
2897 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2899 /* The set of all non-default tables of line number info. */
2900 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2902 /* A flag to tell pubnames/types export if there is an info section to
2903 refer to. */
2904 static bool info_section_emitted;
2906 /* A pointer to the base of a table that contains a list of publicly
2907 accessible names. */
2908 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2910 /* A pointer to the base of a table that contains a list of publicly
2911 accessible types. */
2912 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2914 /* A pointer to the base of a table that contains a list of macro
2915 defines/undefines (and file start/end markers). */
2916 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2918 /* True if .debug_macinfo or .debug_macros section is going to be
2919 emitted. */
2920 #define have_macinfo \
2921 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2922 && !macinfo_table->is_empty ())
2924 /* Array of dies for which we should generate .debug_ranges info. */
2925 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2927 /* Number of elements currently allocated for ranges_table. */
2928 static GTY(()) unsigned ranges_table_allocated;
2930 /* Number of elements in ranges_table currently in use. */
2931 static GTY(()) unsigned ranges_table_in_use;
2933 /* Array of pairs of labels referenced in ranges_table. */
2934 static GTY ((length ("ranges_by_label_allocated")))
2935 dw_ranges_by_label_ref ranges_by_label;
2937 /* Number of elements currently allocated for ranges_by_label. */
2938 static GTY(()) unsigned ranges_by_label_allocated;
2940 /* Number of elements in ranges_by_label currently in use. */
2941 static GTY(()) unsigned ranges_by_label_in_use;
2943 /* Size (in elements) of increments by which we may expand the
2944 ranges_table. */
2945 #define RANGES_TABLE_INCREMENT 64
2947 /* Whether we have location lists that need outputting */
2948 static GTY(()) bool have_location_lists;
2950 /* Unique label counter. */
2951 static GTY(()) unsigned int loclabel_num;
2953 /* Unique label counter for point-of-call tables. */
2954 static GTY(()) unsigned int poc_label_num;
2956 /* Record whether the function being analyzed contains inlined functions. */
2957 static int current_function_has_inlines;
2959 /* The last file entry emitted by maybe_emit_file(). */
2960 static GTY(()) struct dwarf_file_data * last_emitted_file;
2962 /* Number of internal labels generated by gen_internal_sym(). */
2963 static GTY(()) int label_num;
2965 /* Cached result of previous call to lookup_filename. */
2966 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2968 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2970 /* Instances of generic types for which we need to generate debug
2971 info that describe their generic parameters and arguments. That
2972 generation needs to happen once all types are properly laid out so
2973 we do it at the end of compilation. */
2974 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2976 /* Offset from the "steady-state frame pointer" to the frame base,
2977 within the current function. */
2978 static HOST_WIDE_INT frame_pointer_fb_offset;
2979 static bool frame_pointer_fb_offset_valid;
2981 static vec<dw_die_ref> base_types;
2983 /* Forward declarations for functions defined in this file. */
2985 static int is_pseudo_reg (const_rtx);
2986 static int is_tagged_type (const_tree);
2987 static const char *dwarf_tag_name (unsigned);
2988 static const char *dwarf_attr_name (unsigned);
2989 static const char *dwarf_form_name (unsigned);
2990 static tree decl_ultimate_origin (const_tree);
2991 static tree decl_class_context (tree);
2992 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2993 static inline enum dw_val_class AT_class (dw_attr_ref);
2994 static inline unsigned int AT_index (dw_attr_ref);
2995 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2996 static inline unsigned AT_flag (dw_attr_ref);
2997 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2998 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2999 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3000 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3001 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3002 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3003 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3004 unsigned int, unsigned char *);
3005 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3006 static hashval_t debug_str_do_hash (const void *);
3007 static int debug_str_eq (const void *, const void *);
3008 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3009 static inline const char *AT_string (dw_attr_ref);
3010 static enum dwarf_form AT_string_form (dw_attr_ref);
3011 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3012 static void add_AT_specification (dw_die_ref, dw_die_ref);
3013 static inline dw_die_ref AT_ref (dw_attr_ref);
3014 static inline int AT_ref_external (dw_attr_ref);
3015 static inline void set_AT_ref_external (dw_attr_ref, int);
3016 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3017 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3018 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3019 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3020 dw_loc_list_ref);
3021 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3022 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3023 static void remove_addr_table_entry (addr_table_entry *);
3024 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3025 static inline rtx AT_addr (dw_attr_ref);
3026 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3027 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3028 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3029 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3030 unsigned HOST_WIDE_INT);
3031 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3032 unsigned long, bool);
3033 static inline const char *AT_lbl (dw_attr_ref);
3034 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3035 static const char *get_AT_low_pc (dw_die_ref);
3036 static const char *get_AT_hi_pc (dw_die_ref);
3037 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3038 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3039 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3040 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3041 static bool is_cxx (void);
3042 static bool is_fortran (void);
3043 static bool is_ada (void);
3044 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3045 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3046 static void add_child_die (dw_die_ref, dw_die_ref);
3047 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3048 static dw_die_ref lookup_type_die (tree);
3049 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3050 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3051 static void equate_type_number_to_die (tree, dw_die_ref);
3052 static hashval_t decl_die_table_hash (const void *);
3053 static int decl_die_table_eq (const void *, const void *);
3054 static dw_die_ref lookup_decl_die (tree);
3055 static hashval_t common_block_die_table_hash (const void *);
3056 static int common_block_die_table_eq (const void *, const void *);
3057 static hashval_t decl_loc_table_hash (const void *);
3058 static int decl_loc_table_eq (const void *, const void *);
3059 static var_loc_list *lookup_decl_loc (const_tree);
3060 static void equate_decl_number_to_die (tree, dw_die_ref);
3061 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3062 static void print_spaces (FILE *);
3063 static void print_die (dw_die_ref, FILE *);
3064 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3065 static dw_die_ref pop_compile_unit (dw_die_ref);
3066 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3067 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3068 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3069 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3070 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3071 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3072 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3073 struct md5_ctx *, int *);
3074 struct checksum_attributes;
3075 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3076 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3077 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3078 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3079 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3080 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3081 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3082 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3083 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3084 static void compute_section_prefix (dw_die_ref);
3085 static int is_type_die (dw_die_ref);
3086 static int is_comdat_die (dw_die_ref);
3087 static int is_symbol_die (dw_die_ref);
3088 static inline bool is_template_instantiation (dw_die_ref);
3089 static void assign_symbol_names (dw_die_ref);
3090 static void break_out_includes (dw_die_ref);
3091 static int is_declaration_die (dw_die_ref);
3092 static int should_move_die_to_comdat (dw_die_ref);
3093 static dw_die_ref clone_as_declaration (dw_die_ref);
3094 static dw_die_ref clone_die (dw_die_ref);
3095 static dw_die_ref clone_tree (dw_die_ref);
3096 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3097 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3098 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3099 static dw_die_ref generate_skeleton (dw_die_ref);
3100 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3101 dw_die_ref,
3102 dw_die_ref);
3103 static void break_out_comdat_types (dw_die_ref);
3104 static void copy_decls_for_unworthy_types (dw_die_ref);
3106 static void add_sibling_attributes (dw_die_ref);
3107 static void output_location_lists (dw_die_ref);
3108 static int constant_size (unsigned HOST_WIDE_INT);
3109 static unsigned long size_of_die (dw_die_ref);
3110 static void calc_die_sizes (dw_die_ref);
3111 static void calc_base_type_die_sizes (void);
3112 static void mark_dies (dw_die_ref);
3113 static void unmark_dies (dw_die_ref);
3114 static void unmark_all_dies (dw_die_ref);
3115 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3116 static unsigned long size_of_aranges (void);
3117 static enum dwarf_form value_format (dw_attr_ref);
3118 static void output_value_format (dw_attr_ref);
3119 static void output_abbrev_section (void);
3120 static void output_die_abbrevs (unsigned long, dw_die_ref);
3121 static void output_die_symbol (dw_die_ref);
3122 static void output_die (dw_die_ref);
3123 static void output_compilation_unit_header (void);
3124 static void output_comp_unit (dw_die_ref, int);
3125 static void output_comdat_type_unit (comdat_type_node *);
3126 static const char *dwarf2_name (tree, int);
3127 static void add_pubname (tree, dw_die_ref);
3128 static void add_enumerator_pubname (const char *, dw_die_ref);
3129 static void add_pubname_string (const char *, dw_die_ref);
3130 static void add_pubtype (tree, dw_die_ref);
3131 static void output_pubnames (vec<pubname_entry, va_gc> *);
3132 static void output_aranges (unsigned long);
3133 static unsigned int add_ranges_num (int);
3134 static unsigned int add_ranges (const_tree);
3135 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3136 bool *, bool);
3137 static void output_ranges (void);
3138 static dw_line_info_table *new_line_info_table (void);
3139 static void output_line_info (bool);
3140 static void output_file_names (void);
3141 static dw_die_ref base_type_die (tree);
3142 static int is_base_type (tree);
3143 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3144 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3145 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3146 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3147 static int type_is_enum (const_tree);
3148 static unsigned int dbx_reg_number (const_rtx);
3149 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3150 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3151 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3152 enum var_init_status);
3153 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3154 enum var_init_status);
3155 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3156 enum var_init_status);
3157 static int is_based_loc (const_rtx);
3158 static int resolve_one_addr (rtx *, void *);
3159 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3160 enum var_init_status);
3161 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3162 enum var_init_status);
3163 static dw_loc_list_ref loc_list_from_tree (tree, int);
3164 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3165 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3166 static tree field_type (const_tree);
3167 static unsigned int simple_type_align_in_bits (const_tree);
3168 static unsigned int simple_decl_align_in_bits (const_tree);
3169 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3170 static HOST_WIDE_INT field_byte_offset (const_tree);
3171 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3172 dw_loc_list_ref);
3173 static void add_data_member_location_attribute (dw_die_ref, tree);
3174 static bool add_const_value_attribute (dw_die_ref, rtx);
3175 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3176 static void insert_wide_int (const wide_int &, unsigned char *, int);
3177 static void insert_float (const_rtx, unsigned char *);
3178 static rtx rtl_for_decl_location (tree);
3179 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3180 enum dwarf_attribute);
3181 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3182 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3183 static void add_name_attribute (dw_die_ref, const char *);
3184 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3185 static void add_comp_dir_attribute (dw_die_ref);
3186 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3187 static void add_subscript_info (dw_die_ref, tree, bool);
3188 static void add_byte_size_attribute (dw_die_ref, tree);
3189 static void add_bit_offset_attribute (dw_die_ref, tree);
3190 static void add_bit_size_attribute (dw_die_ref, tree);
3191 static void add_prototyped_attribute (dw_die_ref, tree);
3192 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3193 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3194 static void add_src_coords_attributes (dw_die_ref, tree);
3195 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3196 static void push_decl_scope (tree);
3197 static void pop_decl_scope (void);
3198 static dw_die_ref scope_die_for (tree, dw_die_ref);
3199 static inline int local_scope_p (dw_die_ref);
3200 static inline int class_scope_p (dw_die_ref);
3201 static inline int class_or_namespace_scope_p (dw_die_ref);
3202 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3203 static void add_calling_convention_attribute (dw_die_ref, tree);
3204 static const char *type_tag (const_tree);
3205 static tree member_declared_type (const_tree);
3206 #if 0
3207 static const char *decl_start_label (tree);
3208 #endif
3209 static void gen_array_type_die (tree, dw_die_ref);
3210 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3211 #if 0
3212 static void gen_entry_point_die (tree, dw_die_ref);
3213 #endif
3214 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3215 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3216 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3217 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3218 static void gen_formal_types_die (tree, dw_die_ref);
3219 static void gen_subprogram_die (tree, dw_die_ref);
3220 static void gen_variable_die (tree, tree, dw_die_ref);
3221 static void gen_const_die (tree, dw_die_ref);
3222 static void gen_label_die (tree, dw_die_ref);
3223 static void gen_lexical_block_die (tree, dw_die_ref, int);
3224 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3225 static void gen_field_die (tree, dw_die_ref);
3226 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3227 static dw_die_ref gen_compile_unit_die (const char *);
3228 static void gen_inheritance_die (tree, tree, dw_die_ref);
3229 static void gen_member_die (tree, dw_die_ref);
3230 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3231 enum debug_info_usage);
3232 static void gen_subroutine_type_die (tree, dw_die_ref);
3233 static void gen_typedef_die (tree, dw_die_ref);
3234 static void gen_type_die (tree, dw_die_ref);
3235 static void gen_block_die (tree, dw_die_ref, int);
3236 static void decls_for_scope (tree, dw_die_ref, int);
3237 static inline int is_redundant_typedef (const_tree);
3238 static bool is_naming_typedef_decl (const_tree);
3239 static inline dw_die_ref get_context_die (tree);
3240 static void gen_namespace_die (tree, dw_die_ref);
3241 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3242 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3243 static dw_die_ref force_decl_die (tree);
3244 static dw_die_ref force_type_die (tree);
3245 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3246 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3247 static struct dwarf_file_data * lookup_filename (const char *);
3248 static void retry_incomplete_types (void);
3249 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3250 static void gen_generic_params_dies (tree);
3251 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3252 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3253 static void splice_child_die (dw_die_ref, dw_die_ref);
3254 static int file_info_cmp (const void *, const void *);
3255 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3256 const char *, const char *);
3257 static void output_loc_list (dw_loc_list_ref);
3258 static char *gen_internal_sym (const char *);
3259 static bool want_pubnames (void);
3261 static void prune_unmark_dies (dw_die_ref);
3262 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3263 static void prune_unused_types_mark (dw_die_ref, int);
3264 static void prune_unused_types_walk (dw_die_ref);
3265 static void prune_unused_types_walk_attribs (dw_die_ref);
3266 static void prune_unused_types_prune (dw_die_ref);
3267 static void prune_unused_types (void);
3268 static int maybe_emit_file (struct dwarf_file_data *fd);
3269 static inline const char *AT_vms_delta1 (dw_attr_ref);
3270 static inline const char *AT_vms_delta2 (dw_attr_ref);
3271 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3272 const char *, const char *);
3273 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3274 static void gen_remaining_tmpl_value_param_die_attribute (void);
3275 static bool generic_type_p (tree);
3276 static void schedule_generic_params_dies_gen (tree t);
3277 static void gen_scheduled_generic_parms_dies (void);
3279 static const char *comp_dir_string (void);
3281 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3283 /* enum for tracking thread-local variables whose address is really an offset
3284 relative to the TLS pointer, which will need link-time relocation, but will
3285 not need relocation by the DWARF consumer. */
3287 enum dtprel_bool
3289 dtprel_false = 0,
3290 dtprel_true = 1
3293 /* Return the operator to use for an address of a variable. For dtprel_true, we
3294 use DW_OP_const*. For regular variables, which need both link-time
3295 relocation and consumer-level relocation (e.g., to account for shared objects
3296 loaded at a random address), we use DW_OP_addr*. */
3298 static inline enum dwarf_location_atom
3299 dw_addr_op (enum dtprel_bool dtprel)
3301 if (dtprel == dtprel_true)
3302 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3303 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3304 else
3305 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3308 /* Return a pointer to a newly allocated address location description. If
3309 dwarf_split_debug_info is true, then record the address with the appropriate
3310 relocation. */
3311 static inline dw_loc_descr_ref
3312 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3314 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3316 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3317 ref->dw_loc_oprnd1.v.val_addr = addr;
3318 ref->dtprel = dtprel;
3319 if (dwarf_split_debug_info)
3320 ref->dw_loc_oprnd1.val_entry
3321 = add_addr_table_entry (addr,
3322 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3323 else
3324 ref->dw_loc_oprnd1.val_entry = NULL;
3326 return ref;
3329 /* Section names used to hold DWARF debugging information. */
3331 #ifndef DEBUG_INFO_SECTION
3332 #define DEBUG_INFO_SECTION ".debug_info"
3333 #endif
3334 #ifndef DEBUG_DWO_INFO_SECTION
3335 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3336 #endif
3337 #ifndef DEBUG_ABBREV_SECTION
3338 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3339 #endif
3340 #ifndef DEBUG_DWO_ABBREV_SECTION
3341 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3342 #endif
3343 #ifndef DEBUG_ARANGES_SECTION
3344 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3345 #endif
3346 #ifndef DEBUG_ADDR_SECTION
3347 #define DEBUG_ADDR_SECTION ".debug_addr"
3348 #endif
3349 #ifndef DEBUG_NORM_MACINFO_SECTION
3350 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3351 #endif
3352 #ifndef DEBUG_DWO_MACINFO_SECTION
3353 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3354 #endif
3355 #ifndef DEBUG_MACINFO_SECTION
3356 #define DEBUG_MACINFO_SECTION \
3357 (!dwarf_split_debug_info \
3358 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3359 #endif
3360 #ifndef DEBUG_NORM_MACRO_SECTION
3361 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3362 #endif
3363 #ifndef DEBUG_DWO_MACRO_SECTION
3364 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3365 #endif
3366 #ifndef DEBUG_MACRO_SECTION
3367 #define DEBUG_MACRO_SECTION \
3368 (!dwarf_split_debug_info \
3369 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3370 #endif
3371 #ifndef DEBUG_LINE_SECTION
3372 #define DEBUG_LINE_SECTION ".debug_line"
3373 #endif
3374 #ifndef DEBUG_DWO_LINE_SECTION
3375 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3376 #endif
3377 #ifndef DEBUG_LOC_SECTION
3378 #define DEBUG_LOC_SECTION ".debug_loc"
3379 #endif
3380 #ifndef DEBUG_DWO_LOC_SECTION
3381 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3382 #endif
3383 #ifndef DEBUG_PUBNAMES_SECTION
3384 #define DEBUG_PUBNAMES_SECTION \
3385 ((debug_generate_pub_sections == 2) \
3386 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3387 #endif
3388 #ifndef DEBUG_PUBTYPES_SECTION
3389 #define DEBUG_PUBTYPES_SECTION \
3390 ((debug_generate_pub_sections == 2) \
3391 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3392 #endif
3393 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3394 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3395 #ifndef DEBUG_STR_OFFSETS_SECTION
3396 #define DEBUG_STR_OFFSETS_SECTION \
3397 (!dwarf_split_debug_info \
3398 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3399 #endif
3400 #ifndef DEBUG_STR_DWO_SECTION
3401 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3402 #endif
3403 #ifndef DEBUG_STR_SECTION
3404 #define DEBUG_STR_SECTION ".debug_str"
3405 #endif
3406 #ifndef DEBUG_RANGES_SECTION
3407 #define DEBUG_RANGES_SECTION ".debug_ranges"
3408 #endif
3410 /* Standard ELF section names for compiled code and data. */
3411 #ifndef TEXT_SECTION_NAME
3412 #define TEXT_SECTION_NAME ".text"
3413 #endif
3415 /* Section flags for .debug_macinfo/.debug_macro section. */
3416 #define DEBUG_MACRO_SECTION_FLAGS \
3417 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3419 /* Section flags for .debug_str section. */
3420 #define DEBUG_STR_SECTION_FLAGS \
3421 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3422 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3423 : SECTION_DEBUG)
3425 /* Section flags for .debug_str.dwo section. */
3426 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3428 /* Labels we insert at beginning sections we can reference instead of
3429 the section names themselves. */
3431 #ifndef TEXT_SECTION_LABEL
3432 #define TEXT_SECTION_LABEL "Ltext"
3433 #endif
3434 #ifndef COLD_TEXT_SECTION_LABEL
3435 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3436 #endif
3437 #ifndef DEBUG_LINE_SECTION_LABEL
3438 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3439 #endif
3440 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3441 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3442 #endif
3443 #ifndef DEBUG_INFO_SECTION_LABEL
3444 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3445 #endif
3446 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3447 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3448 #endif
3449 #ifndef DEBUG_ABBREV_SECTION_LABEL
3450 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3451 #endif
3452 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3453 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3454 #endif
3455 #ifndef DEBUG_ADDR_SECTION_LABEL
3456 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3457 #endif
3458 #ifndef DEBUG_LOC_SECTION_LABEL
3459 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3460 #endif
3461 #ifndef DEBUG_RANGES_SECTION_LABEL
3462 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3463 #endif
3464 #ifndef DEBUG_MACINFO_SECTION_LABEL
3465 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3466 #endif
3467 #ifndef DEBUG_MACRO_SECTION_LABEL
3468 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3469 #endif
3470 #define SKELETON_COMP_DIE_ABBREV 1
3471 #define SKELETON_TYPE_DIE_ABBREV 2
3473 /* Definitions of defaults for formats and names of various special
3474 (artificial) labels which may be generated within this file (when the -g
3475 options is used and DWARF2_DEBUGGING_INFO is in effect.
3476 If necessary, these may be overridden from within the tm.h file, but
3477 typically, overriding these defaults is unnecessary. */
3479 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3480 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3481 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3482 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3483 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3484 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3485 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3486 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3487 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3488 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3489 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3490 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3491 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3492 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3494 #ifndef TEXT_END_LABEL
3495 #define TEXT_END_LABEL "Letext"
3496 #endif
3497 #ifndef COLD_END_LABEL
3498 #define COLD_END_LABEL "Letext_cold"
3499 #endif
3500 #ifndef BLOCK_BEGIN_LABEL
3501 #define BLOCK_BEGIN_LABEL "LBB"
3502 #endif
3503 #ifndef BLOCK_END_LABEL
3504 #define BLOCK_END_LABEL "LBE"
3505 #endif
3506 #ifndef LINE_CODE_LABEL
3507 #define LINE_CODE_LABEL "LM"
3508 #endif
3511 /* Return the root of the DIE's built for the current compilation unit. */
3512 static dw_die_ref
3513 comp_unit_die (void)
3515 if (!single_comp_unit_die)
3516 single_comp_unit_die = gen_compile_unit_die (NULL);
3517 return single_comp_unit_die;
3520 /* We allow a language front-end to designate a function that is to be
3521 called to "demangle" any name before it is put into a DIE. */
3523 static const char *(*demangle_name_func) (const char *);
3525 void
3526 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3528 demangle_name_func = func;
3531 /* Test if rtl node points to a pseudo register. */
3533 static inline int
3534 is_pseudo_reg (const_rtx rtl)
3536 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3537 || (GET_CODE (rtl) == SUBREG
3538 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3541 /* Return a reference to a type, with its const and volatile qualifiers
3542 removed. */
3544 static inline tree
3545 type_main_variant (tree type)
3547 type = TYPE_MAIN_VARIANT (type);
3549 /* ??? There really should be only one main variant among any group of
3550 variants of a given type (and all of the MAIN_VARIANT values for all
3551 members of the group should point to that one type) but sometimes the C
3552 front-end messes this up for array types, so we work around that bug
3553 here. */
3554 if (TREE_CODE (type) == ARRAY_TYPE)
3555 while (type != TYPE_MAIN_VARIANT (type))
3556 type = TYPE_MAIN_VARIANT (type);
3558 return type;
3561 /* Return nonzero if the given type node represents a tagged type. */
3563 static inline int
3564 is_tagged_type (const_tree type)
3566 enum tree_code code = TREE_CODE (type);
3568 return (code == RECORD_TYPE || code == UNION_TYPE
3569 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3572 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3574 static void
3575 get_ref_die_offset_label (char *label, dw_die_ref ref)
3577 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3580 /* Return die_offset of a DIE reference to a base type. */
3582 static unsigned long int
3583 get_base_type_offset (dw_die_ref ref)
3585 if (ref->die_offset)
3586 return ref->die_offset;
3587 if (comp_unit_die ()->die_abbrev)
3589 calc_base_type_die_sizes ();
3590 gcc_assert (ref->die_offset);
3592 return ref->die_offset;
3595 /* Return die_offset of a DIE reference other than base type. */
3597 static unsigned long int
3598 get_ref_die_offset (dw_die_ref ref)
3600 gcc_assert (ref->die_offset);
3601 return ref->die_offset;
3604 /* Convert a DIE tag into its string name. */
3606 static const char *
3607 dwarf_tag_name (unsigned int tag)
3609 const char *name = get_DW_TAG_name (tag);
3611 if (name != NULL)
3612 return name;
3614 return "DW_TAG_<unknown>";
3617 /* Convert a DWARF attribute code into its string name. */
3619 static const char *
3620 dwarf_attr_name (unsigned int attr)
3622 const char *name;
3624 switch (attr)
3626 #if VMS_DEBUGGING_INFO
3627 case DW_AT_HP_prologue:
3628 return "DW_AT_HP_prologue";
3629 #else
3630 case DW_AT_MIPS_loop_unroll_factor:
3631 return "DW_AT_MIPS_loop_unroll_factor";
3632 #endif
3634 #if VMS_DEBUGGING_INFO
3635 case DW_AT_HP_epilogue:
3636 return "DW_AT_HP_epilogue";
3637 #else
3638 case DW_AT_MIPS_stride:
3639 return "DW_AT_MIPS_stride";
3640 #endif
3643 name = get_DW_AT_name (attr);
3645 if (name != NULL)
3646 return name;
3648 return "DW_AT_<unknown>";
3651 /* Convert a DWARF value form code into its string name. */
3653 static const char *
3654 dwarf_form_name (unsigned int form)
3656 const char *name = get_DW_FORM_name (form);
3658 if (name != NULL)
3659 return name;
3661 return "DW_FORM_<unknown>";
3664 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3665 instance of an inlined instance of a decl which is local to an inline
3666 function, so we have to trace all of the way back through the origin chain
3667 to find out what sort of node actually served as the original seed for the
3668 given block. */
3670 static tree
3671 decl_ultimate_origin (const_tree decl)
3673 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3674 return NULL_TREE;
3676 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3677 nodes in the function to point to themselves; ignore that if
3678 we're trying to output the abstract instance of this function. */
3679 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3680 return NULL_TREE;
3682 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3683 most distant ancestor, this should never happen. */
3684 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3686 return DECL_ABSTRACT_ORIGIN (decl);
3689 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3690 of a virtual function may refer to a base class, so we check the 'this'
3691 parameter. */
3693 static tree
3694 decl_class_context (tree decl)
3696 tree context = NULL_TREE;
3698 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3699 context = DECL_CONTEXT (decl);
3700 else
3701 context = TYPE_MAIN_VARIANT
3702 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3704 if (context && !TYPE_P (context))
3705 context = NULL_TREE;
3707 return context;
3710 /* Add an attribute/value pair to a DIE. */
3712 static inline void
3713 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3715 /* Maybe this should be an assert? */
3716 if (die == NULL)
3717 return;
3719 vec_safe_reserve (die->die_attr, 1);
3720 vec_safe_push (die->die_attr, *attr);
3723 static inline enum dw_val_class
3724 AT_class (dw_attr_ref a)
3726 return a->dw_attr_val.val_class;
3729 /* Return the index for any attribute that will be referenced with a
3730 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3731 are stored in dw_attr_val.v.val_str for reference counting
3732 pruning. */
3734 static inline unsigned int
3735 AT_index (dw_attr_ref a)
3737 if (AT_class (a) == dw_val_class_str)
3738 return a->dw_attr_val.v.val_str->index;
3739 else if (a->dw_attr_val.val_entry != NULL)
3740 return a->dw_attr_val.val_entry->index;
3741 return NOT_INDEXED;
3744 /* Add a flag value attribute to a DIE. */
3746 static inline void
3747 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3749 dw_attr_node attr;
3751 attr.dw_attr = attr_kind;
3752 attr.dw_attr_val.val_class = dw_val_class_flag;
3753 attr.dw_attr_val.val_entry = NULL;
3754 attr.dw_attr_val.v.val_flag = flag;
3755 add_dwarf_attr (die, &attr);
3758 static inline unsigned
3759 AT_flag (dw_attr_ref a)
3761 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3762 return a->dw_attr_val.v.val_flag;
3765 /* Add a signed integer attribute value to a DIE. */
3767 static inline void
3768 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3770 dw_attr_node attr;
3772 attr.dw_attr = attr_kind;
3773 attr.dw_attr_val.val_class = dw_val_class_const;
3774 attr.dw_attr_val.val_entry = NULL;
3775 attr.dw_attr_val.v.val_int = int_val;
3776 add_dwarf_attr (die, &attr);
3779 static inline HOST_WIDE_INT
3780 AT_int (dw_attr_ref a)
3782 gcc_assert (a && AT_class (a) == dw_val_class_const);
3783 return a->dw_attr_val.v.val_int;
3786 /* Add an unsigned integer attribute value to a DIE. */
3788 static inline void
3789 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3790 unsigned HOST_WIDE_INT unsigned_val)
3792 dw_attr_node attr;
3794 attr.dw_attr = attr_kind;
3795 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3796 attr.dw_attr_val.val_entry = NULL;
3797 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3798 add_dwarf_attr (die, &attr);
3801 static inline unsigned HOST_WIDE_INT
3802 AT_unsigned (dw_attr_ref a)
3804 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3805 return a->dw_attr_val.v.val_unsigned;
3808 /* Add an unsigned wide integer attribute value to a DIE. */
3810 static inline void
3811 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3812 const wide_int& w)
3814 dw_attr_node attr;
3816 attr.dw_attr = attr_kind;
3817 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3818 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3819 *attr.dw_attr_val.v.val_wide = w;
3820 add_dwarf_attr (die, &attr);
3823 /* Add an unsigned double integer attribute value to a DIE. */
3825 static inline void
3826 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3827 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3829 dw_attr_node attr;
3831 attr.dw_attr = attr_kind;
3832 attr.dw_attr_val.val_class = dw_val_class_const_double;
3833 attr.dw_attr_val.val_entry = NULL;
3834 attr.dw_attr_val.v.val_double.high = high;
3835 attr.dw_attr_val.v.val_double.low = low;
3836 add_dwarf_attr (die, &attr);
3839 /* Add a floating point attribute value to a DIE and return it. */
3841 static inline void
3842 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3843 unsigned int length, unsigned int elt_size, unsigned char *array)
3845 dw_attr_node attr;
3847 attr.dw_attr = attr_kind;
3848 attr.dw_attr_val.val_class = dw_val_class_vec;
3849 attr.dw_attr_val.val_entry = NULL;
3850 attr.dw_attr_val.v.val_vec.length = length;
3851 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3852 attr.dw_attr_val.v.val_vec.array = array;
3853 add_dwarf_attr (die, &attr);
3856 /* Add an 8-byte data attribute value to a DIE. */
3858 static inline void
3859 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3860 unsigned char data8[8])
3862 dw_attr_node attr;
3864 attr.dw_attr = attr_kind;
3865 attr.dw_attr_val.val_class = dw_val_class_data8;
3866 attr.dw_attr_val.val_entry = NULL;
3867 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3868 add_dwarf_attr (die, &attr);
3871 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3872 dwarf_split_debug_info, address attributes in dies destined for the
3873 final executable have force_direct set to avoid using indexed
3874 references. */
3876 static inline void
3877 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3878 bool force_direct)
3880 dw_attr_node attr;
3881 char * lbl_id;
3883 lbl_id = xstrdup (lbl_low);
3884 attr.dw_attr = DW_AT_low_pc;
3885 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3886 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3887 if (dwarf_split_debug_info && !force_direct)
3888 attr.dw_attr_val.val_entry
3889 = add_addr_table_entry (lbl_id, ate_kind_label);
3890 else
3891 attr.dw_attr_val.val_entry = NULL;
3892 add_dwarf_attr (die, &attr);
3894 attr.dw_attr = DW_AT_high_pc;
3895 if (dwarf_version < 4)
3896 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3897 else
3898 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3899 lbl_id = xstrdup (lbl_high);
3900 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3901 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3902 && dwarf_split_debug_info && !force_direct)
3903 attr.dw_attr_val.val_entry
3904 = add_addr_table_entry (lbl_id, ate_kind_label);
3905 else
3906 attr.dw_attr_val.val_entry = NULL;
3907 add_dwarf_attr (die, &attr);
3910 /* Hash and equality functions for debug_str_hash. */
3912 static hashval_t
3913 debug_str_do_hash (const void *x)
3915 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3918 static int
3919 debug_str_eq (const void *x1, const void *x2)
3921 return strcmp ((((const struct indirect_string_node *)x1)->str),
3922 (const char *)x2) == 0;
3925 /* Add STR to the given string hash table. */
3927 static struct indirect_string_node *
3928 find_AT_string_in_table (const char *str, htab_t table)
3930 struct indirect_string_node *node;
3931 void **slot;
3933 slot = htab_find_slot_with_hash (table, str,
3934 htab_hash_string (str), INSERT);
3935 if (*slot == NULL)
3937 node = ggc_cleared_alloc<indirect_string_node> ();
3938 node->str = ggc_strdup (str);
3939 *slot = node;
3941 else
3942 node = (struct indirect_string_node *) *slot;
3944 node->refcount++;
3945 return node;
3948 /* Add STR to the indirect string hash table. */
3950 static struct indirect_string_node *
3951 find_AT_string (const char *str)
3953 if (! debug_str_hash)
3954 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3955 debug_str_eq, NULL);
3957 return find_AT_string_in_table (str, debug_str_hash);
3960 /* Add a string attribute value to a DIE. */
3962 static inline void
3963 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3965 dw_attr_node attr;
3966 struct indirect_string_node *node;
3968 node = find_AT_string (str);
3970 attr.dw_attr = attr_kind;
3971 attr.dw_attr_val.val_class = dw_val_class_str;
3972 attr.dw_attr_val.val_entry = NULL;
3973 attr.dw_attr_val.v.val_str = node;
3974 add_dwarf_attr (die, &attr);
3977 static inline const char *
3978 AT_string (dw_attr_ref a)
3980 gcc_assert (a && AT_class (a) == dw_val_class_str);
3981 return a->dw_attr_val.v.val_str->str;
3984 /* Call this function directly to bypass AT_string_form's logic to put
3985 the string inline in the die. */
3987 static void
3988 set_indirect_string (struct indirect_string_node *node)
3990 char label[32];
3991 /* Already indirect is a no op. */
3992 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3994 gcc_assert (node->label);
3995 return;
3997 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3998 ++dw2_string_counter;
3999 node->label = xstrdup (label);
4001 if (!dwarf_split_debug_info)
4003 node->form = DW_FORM_strp;
4004 node->index = NOT_INDEXED;
4006 else
4008 node->form = DW_FORM_GNU_str_index;
4009 node->index = NO_INDEX_ASSIGNED;
4013 /* Find out whether a string should be output inline in DIE
4014 or out-of-line in .debug_str section. */
4016 static enum dwarf_form
4017 find_string_form (struct indirect_string_node *node)
4019 unsigned int len;
4021 if (node->form)
4022 return node->form;
4024 len = strlen (node->str) + 1;
4026 /* If the string is shorter or equal to the size of the reference, it is
4027 always better to put it inline. */
4028 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4029 return node->form = DW_FORM_string;
4031 /* If we cannot expect the linker to merge strings in .debug_str
4032 section, only put it into .debug_str if it is worth even in this
4033 single module. */
4034 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4035 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4036 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4037 return node->form = DW_FORM_string;
4039 set_indirect_string (node);
4041 return node->form;
4044 /* Find out whether the string referenced from the attribute should be
4045 output inline in DIE or out-of-line in .debug_str section. */
4047 static enum dwarf_form
4048 AT_string_form (dw_attr_ref a)
4050 gcc_assert (a && AT_class (a) == dw_val_class_str);
4051 return find_string_form (a->dw_attr_val.v.val_str);
4054 /* Add a DIE reference attribute value to a DIE. */
4056 static inline void
4057 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4059 dw_attr_node attr;
4061 #ifdef ENABLE_CHECKING
4062 gcc_assert (targ_die != NULL);
4063 #else
4064 /* With LTO we can end up trying to reference something we didn't create
4065 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4066 if (targ_die == NULL)
4067 return;
4068 #endif
4070 attr.dw_attr = attr_kind;
4071 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4072 attr.dw_attr_val.val_entry = NULL;
4073 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4074 attr.dw_attr_val.v.val_die_ref.external = 0;
4075 add_dwarf_attr (die, &attr);
4078 /* Change DIE reference REF to point to NEW_DIE instead. */
4080 static inline void
4081 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4083 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4084 ref->dw_attr_val.v.val_die_ref.die = new_die;
4085 ref->dw_attr_val.v.val_die_ref.external = 0;
4088 /* Add an AT_specification attribute to a DIE, and also make the back
4089 pointer from the specification to the definition. */
4091 static inline void
4092 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4094 add_AT_die_ref (die, DW_AT_specification, targ_die);
4095 gcc_assert (!targ_die->die_definition);
4096 targ_die->die_definition = die;
4099 static inline dw_die_ref
4100 AT_ref (dw_attr_ref a)
4102 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4103 return a->dw_attr_val.v.val_die_ref.die;
4106 static inline int
4107 AT_ref_external (dw_attr_ref a)
4109 if (a && AT_class (a) == dw_val_class_die_ref)
4110 return a->dw_attr_val.v.val_die_ref.external;
4112 return 0;
4115 static inline void
4116 set_AT_ref_external (dw_attr_ref a, int i)
4118 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4119 a->dw_attr_val.v.val_die_ref.external = i;
4122 /* Add an FDE reference attribute value to a DIE. */
4124 static inline void
4125 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4127 dw_attr_node attr;
4129 attr.dw_attr = attr_kind;
4130 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4131 attr.dw_attr_val.val_entry = NULL;
4132 attr.dw_attr_val.v.val_fde_index = targ_fde;
4133 add_dwarf_attr (die, &attr);
4136 /* Add a location description attribute value to a DIE. */
4138 static inline void
4139 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4141 dw_attr_node attr;
4143 attr.dw_attr = attr_kind;
4144 attr.dw_attr_val.val_class = dw_val_class_loc;
4145 attr.dw_attr_val.val_entry = NULL;
4146 attr.dw_attr_val.v.val_loc = loc;
4147 add_dwarf_attr (die, &attr);
4150 static inline dw_loc_descr_ref
4151 AT_loc (dw_attr_ref a)
4153 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4154 return a->dw_attr_val.v.val_loc;
4157 static inline void
4158 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4160 dw_attr_node attr;
4162 attr.dw_attr = attr_kind;
4163 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4164 attr.dw_attr_val.val_entry = NULL;
4165 attr.dw_attr_val.v.val_loc_list = loc_list;
4166 add_dwarf_attr (die, &attr);
4167 have_location_lists = true;
4170 static inline dw_loc_list_ref
4171 AT_loc_list (dw_attr_ref a)
4173 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4174 return a->dw_attr_val.v.val_loc_list;
4177 static inline dw_loc_list_ref *
4178 AT_loc_list_ptr (dw_attr_ref a)
4180 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4181 return &a->dw_attr_val.v.val_loc_list;
4184 /* Table of entries into the .debug_addr section. */
4186 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4188 /* Hash an address_table_entry. */
4190 static hashval_t
4191 addr_table_entry_do_hash (const void *x)
4193 const addr_table_entry *a = (const addr_table_entry *) x;
4194 inchash::hash hstate;
4195 switch (a->kind)
4197 case ate_kind_rtx:
4198 hstate.add_int (0);
4199 break;
4200 case ate_kind_rtx_dtprel:
4201 hstate.add_int (1);
4202 break;
4203 case ate_kind_label:
4204 return htab_hash_string (a->addr.label);
4205 default:
4206 gcc_unreachable ();
4208 inchash::add_rtx (a->addr.rtl, hstate);
4209 return hstate.end ();
4212 /* Determine equality for two address_table_entries. */
4214 static int
4215 addr_table_entry_eq (const void *x1, const void *x2)
4217 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4218 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4220 if (a1->kind != a2->kind)
4221 return 0;
4222 switch (a1->kind)
4224 case ate_kind_rtx:
4225 case ate_kind_rtx_dtprel:
4226 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4227 case ate_kind_label:
4228 return strcmp (a1->addr.label, a2->addr.label) == 0;
4229 default:
4230 gcc_unreachable ();
4234 /* Initialize an addr_table_entry. */
4236 void
4237 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4239 e->kind = kind;
4240 switch (kind)
4242 case ate_kind_rtx:
4243 case ate_kind_rtx_dtprel:
4244 e->addr.rtl = (rtx) addr;
4245 break;
4246 case ate_kind_label:
4247 e->addr.label = (char *) addr;
4248 break;
4250 e->refcount = 0;
4251 e->index = NO_INDEX_ASSIGNED;
4254 /* Add attr to the address table entry to the table. Defer setting an
4255 index until output time. */
4257 static addr_table_entry *
4258 add_addr_table_entry (void *addr, enum ate_kind kind)
4260 addr_table_entry *node;
4261 addr_table_entry finder;
4262 void **slot;
4264 gcc_assert (dwarf_split_debug_info);
4265 if (! addr_index_table)
4266 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4267 addr_table_entry_eq, NULL);
4268 init_addr_table_entry (&finder, kind, addr);
4269 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4271 if (*slot == HTAB_EMPTY_ENTRY)
4273 node = ggc_cleared_alloc<addr_table_entry> ();
4274 init_addr_table_entry (node, kind, addr);
4275 *slot = node;
4277 else
4278 node = (addr_table_entry *) *slot;
4280 node->refcount++;
4281 return node;
4284 /* Remove an entry from the addr table by decrementing its refcount.
4285 Strictly, decrementing the refcount would be enough, but the
4286 assertion that the entry is actually in the table has found
4287 bugs. */
4289 static void
4290 remove_addr_table_entry (addr_table_entry *entry)
4292 gcc_assert (dwarf_split_debug_info && addr_index_table);
4293 /* After an index is assigned, the table is frozen. */
4294 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4295 entry->refcount--;
4298 /* Given a location list, remove all addresses it refers to from the
4299 address_table. */
4301 static void
4302 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4304 for (; descr; descr = descr->dw_loc_next)
4305 if (descr->dw_loc_oprnd1.val_entry != NULL)
4307 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4308 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4312 /* A helper function for dwarf2out_finish called through
4313 htab_traverse. Assign an addr_table_entry its index. All entries
4314 must be collected into the table when this function is called,
4315 because the indexing code relies on htab_traverse to traverse nodes
4316 in the same order for each run. */
4318 static int
4319 index_addr_table_entry (void **h, void *v)
4321 addr_table_entry *node = (addr_table_entry *) *h;
4322 unsigned int *index = (unsigned int *) v;
4324 /* Don't index unreferenced nodes. */
4325 if (node->refcount == 0)
4326 return 1;
4328 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4329 node->index = *index;
4330 *index += 1;
4332 return 1;
4335 /* Add an address constant attribute value to a DIE. When using
4336 dwarf_split_debug_info, address attributes in dies destined for the
4337 final executable should be direct references--setting the parameter
4338 force_direct ensures this behavior. */
4340 static inline void
4341 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4342 bool force_direct)
4344 dw_attr_node attr;
4346 attr.dw_attr = attr_kind;
4347 attr.dw_attr_val.val_class = dw_val_class_addr;
4348 attr.dw_attr_val.v.val_addr = addr;
4349 if (dwarf_split_debug_info && !force_direct)
4350 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4351 else
4352 attr.dw_attr_val.val_entry = NULL;
4353 add_dwarf_attr (die, &attr);
4356 /* Get the RTX from to an address DIE attribute. */
4358 static inline rtx
4359 AT_addr (dw_attr_ref a)
4361 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4362 return a->dw_attr_val.v.val_addr;
4365 /* Add a file attribute value to a DIE. */
4367 static inline void
4368 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4369 struct dwarf_file_data *fd)
4371 dw_attr_node attr;
4373 attr.dw_attr = attr_kind;
4374 attr.dw_attr_val.val_class = dw_val_class_file;
4375 attr.dw_attr_val.val_entry = NULL;
4376 attr.dw_attr_val.v.val_file = fd;
4377 add_dwarf_attr (die, &attr);
4380 /* Get the dwarf_file_data from a file DIE attribute. */
4382 static inline struct dwarf_file_data *
4383 AT_file (dw_attr_ref a)
4385 gcc_assert (a && AT_class (a) == dw_val_class_file);
4386 return a->dw_attr_val.v.val_file;
4389 /* Add a vms delta attribute value to a DIE. */
4391 static inline void
4392 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4393 const char *lbl1, const char *lbl2)
4395 dw_attr_node attr;
4397 attr.dw_attr = attr_kind;
4398 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4399 attr.dw_attr_val.val_entry = NULL;
4400 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4401 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4402 add_dwarf_attr (die, &attr);
4405 /* Add a label identifier attribute value to a DIE. */
4407 static inline void
4408 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4409 const char *lbl_id)
4411 dw_attr_node attr;
4413 attr.dw_attr = attr_kind;
4414 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4415 attr.dw_attr_val.val_entry = NULL;
4416 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4417 if (dwarf_split_debug_info)
4418 attr.dw_attr_val.val_entry
4419 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4420 ate_kind_label);
4421 add_dwarf_attr (die, &attr);
4424 /* Add a section offset attribute value to a DIE, an offset into the
4425 debug_line section. */
4427 static inline void
4428 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4429 const char *label)
4431 dw_attr_node attr;
4433 attr.dw_attr = attr_kind;
4434 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4435 attr.dw_attr_val.val_entry = NULL;
4436 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4437 add_dwarf_attr (die, &attr);
4440 /* Add a section offset attribute value to a DIE, an offset into the
4441 debug_macinfo section. */
4443 static inline void
4444 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4445 const char *label)
4447 dw_attr_node attr;
4449 attr.dw_attr = attr_kind;
4450 attr.dw_attr_val.val_class = dw_val_class_macptr;
4451 attr.dw_attr_val.val_entry = NULL;
4452 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4453 add_dwarf_attr (die, &attr);
4456 /* Add an offset attribute value to a DIE. */
4458 static inline void
4459 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4460 unsigned HOST_WIDE_INT offset)
4462 dw_attr_node attr;
4464 attr.dw_attr = attr_kind;
4465 attr.dw_attr_val.val_class = dw_val_class_offset;
4466 attr.dw_attr_val.val_entry = NULL;
4467 attr.dw_attr_val.v.val_offset = offset;
4468 add_dwarf_attr (die, &attr);
4471 /* Add a range_list attribute value to a DIE. When using
4472 dwarf_split_debug_info, address attributes in dies destined for the
4473 final executable should be direct references--setting the parameter
4474 force_direct ensures this behavior. */
4476 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4477 #define RELOCATED_OFFSET (NULL)
4479 static void
4480 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4481 long unsigned int offset, bool force_direct)
4483 dw_attr_node attr;
4485 attr.dw_attr = attr_kind;
4486 attr.dw_attr_val.val_class = dw_val_class_range_list;
4487 /* For the range_list attribute, use val_entry to store whether the
4488 offset should follow split-debug-info or normal semantics. This
4489 value is read in output_range_list_offset. */
4490 if (dwarf_split_debug_info && !force_direct)
4491 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4492 else
4493 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4494 attr.dw_attr_val.v.val_offset = offset;
4495 add_dwarf_attr (die, &attr);
4498 /* Return the start label of a delta attribute. */
4500 static inline const char *
4501 AT_vms_delta1 (dw_attr_ref a)
4503 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4504 return a->dw_attr_val.v.val_vms_delta.lbl1;
4507 /* Return the end label of a delta attribute. */
4509 static inline const char *
4510 AT_vms_delta2 (dw_attr_ref a)
4512 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4513 return a->dw_attr_val.v.val_vms_delta.lbl2;
4516 static inline const char *
4517 AT_lbl (dw_attr_ref a)
4519 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4520 || AT_class (a) == dw_val_class_lineptr
4521 || AT_class (a) == dw_val_class_macptr
4522 || AT_class (a) == dw_val_class_high_pc));
4523 return a->dw_attr_val.v.val_lbl_id;
4526 /* Get the attribute of type attr_kind. */
4528 static dw_attr_ref
4529 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4531 dw_attr_ref a;
4532 unsigned ix;
4533 dw_die_ref spec = NULL;
4535 if (! die)
4536 return NULL;
4538 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4539 if (a->dw_attr == attr_kind)
4540 return a;
4541 else if (a->dw_attr == DW_AT_specification
4542 || a->dw_attr == DW_AT_abstract_origin)
4543 spec = AT_ref (a);
4545 if (spec)
4546 return get_AT (spec, attr_kind);
4548 return NULL;
4551 /* Returns the parent of the declaration of DIE. */
4553 static dw_die_ref
4554 get_die_parent (dw_die_ref die)
4556 dw_die_ref t;
4558 if (!die)
4559 return NULL;
4561 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4562 || (t = get_AT_ref (die, DW_AT_specification)))
4563 die = t;
4565 return die->die_parent;
4568 /* Return the "low pc" attribute value, typically associated with a subprogram
4569 DIE. Return null if the "low pc" attribute is either not present, or if it
4570 cannot be represented as an assembler label identifier. */
4572 static inline const char *
4573 get_AT_low_pc (dw_die_ref die)
4575 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4577 return a ? AT_lbl (a) : NULL;
4580 /* Return the "high pc" attribute value, typically associated with a subprogram
4581 DIE. Return null if the "high pc" attribute is either not present, or if it
4582 cannot be represented as an assembler label identifier. */
4584 static inline const char *
4585 get_AT_hi_pc (dw_die_ref die)
4587 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4589 return a ? AT_lbl (a) : NULL;
4592 /* Return the value of the string attribute designated by ATTR_KIND, or
4593 NULL if it is not present. */
4595 static inline const char *
4596 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4598 dw_attr_ref a = get_AT (die, attr_kind);
4600 return a ? AT_string (a) : NULL;
4603 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4604 if it is not present. */
4606 static inline int
4607 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4609 dw_attr_ref a = get_AT (die, attr_kind);
4611 return a ? AT_flag (a) : 0;
4614 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4615 if it is not present. */
4617 static inline unsigned
4618 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4620 dw_attr_ref a = get_AT (die, attr_kind);
4622 return a ? AT_unsigned (a) : 0;
4625 static inline dw_die_ref
4626 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4628 dw_attr_ref a = get_AT (die, attr_kind);
4630 return a ? AT_ref (a) : NULL;
4633 static inline struct dwarf_file_data *
4634 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4636 dw_attr_ref a = get_AT (die, attr_kind);
4638 return a ? AT_file (a) : NULL;
4641 /* Return TRUE if the language is C++. */
4643 static inline bool
4644 is_cxx (void)
4646 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4648 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4651 /* Return TRUE if the language is Java. */
4653 static inline bool
4654 is_java (void)
4656 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4658 return lang == DW_LANG_Java;
4661 /* Return TRUE if the language is Fortran. */
4663 static inline bool
4664 is_fortran (void)
4666 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4668 return (lang == DW_LANG_Fortran77
4669 || lang == DW_LANG_Fortran90
4670 || lang == DW_LANG_Fortran95);
4673 /* Return TRUE if the language is Ada. */
4675 static inline bool
4676 is_ada (void)
4678 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4680 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4683 /* Remove the specified attribute if present. */
4685 static void
4686 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4688 dw_attr_ref a;
4689 unsigned ix;
4691 if (! die)
4692 return;
4694 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4695 if (a->dw_attr == attr_kind)
4697 if (AT_class (a) == dw_val_class_str)
4698 if (a->dw_attr_val.v.val_str->refcount)
4699 a->dw_attr_val.v.val_str->refcount--;
4701 /* vec::ordered_remove should help reduce the number of abbrevs
4702 that are needed. */
4703 die->die_attr->ordered_remove (ix);
4704 return;
4708 /* Remove CHILD from its parent. PREV must have the property that
4709 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4711 static void
4712 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4714 gcc_assert (child->die_parent == prev->die_parent);
4715 gcc_assert (prev->die_sib == child);
4716 if (prev == child)
4718 gcc_assert (child->die_parent->die_child == child);
4719 prev = NULL;
4721 else
4722 prev->die_sib = child->die_sib;
4723 if (child->die_parent->die_child == child)
4724 child->die_parent->die_child = prev;
4727 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4728 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4730 static void
4731 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4733 dw_die_ref parent = old_child->die_parent;
4735 gcc_assert (parent == prev->die_parent);
4736 gcc_assert (prev->die_sib == old_child);
4738 new_child->die_parent = parent;
4739 if (prev == old_child)
4741 gcc_assert (parent->die_child == old_child);
4742 new_child->die_sib = new_child;
4744 else
4746 prev->die_sib = new_child;
4747 new_child->die_sib = old_child->die_sib;
4749 if (old_child->die_parent->die_child == old_child)
4750 old_child->die_parent->die_child = new_child;
4753 /* Move all children from OLD_PARENT to NEW_PARENT. */
4755 static void
4756 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4758 dw_die_ref c;
4759 new_parent->die_child = old_parent->die_child;
4760 old_parent->die_child = NULL;
4761 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4764 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4765 matches TAG. */
4767 static void
4768 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4770 dw_die_ref c;
4772 c = die->die_child;
4773 if (c) do {
4774 dw_die_ref prev = c;
4775 c = c->die_sib;
4776 while (c->die_tag == tag)
4778 remove_child_with_prev (c, prev);
4779 /* Might have removed every child. */
4780 if (c == c->die_sib)
4781 return;
4782 c = c->die_sib;
4784 } while (c != die->die_child);
4787 /* Add a CHILD_DIE as the last child of DIE. */
4789 static void
4790 add_child_die (dw_die_ref die, dw_die_ref child_die)
4792 /* FIXME this should probably be an assert. */
4793 if (! die || ! child_die)
4794 return;
4795 gcc_assert (die != child_die);
4797 child_die->die_parent = die;
4798 if (die->die_child)
4800 child_die->die_sib = die->die_child->die_sib;
4801 die->die_child->die_sib = child_die;
4803 else
4804 child_die->die_sib = child_die;
4805 die->die_child = child_die;
4808 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4809 is the specification, to the end of PARENT's list of children.
4810 This is done by removing and re-adding it. */
4812 static void
4813 splice_child_die (dw_die_ref parent, dw_die_ref child)
4815 dw_die_ref p;
4817 /* We want the declaration DIE from inside the class, not the
4818 specification DIE at toplevel. */
4819 if (child->die_parent != parent)
4821 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4823 if (tmp)
4824 child = tmp;
4827 gcc_assert (child->die_parent == parent
4828 || (child->die_parent
4829 == get_AT_ref (parent, DW_AT_specification)));
4831 for (p = child->die_parent->die_child; ; p = p->die_sib)
4832 if (p->die_sib == child)
4834 remove_child_with_prev (child, p);
4835 break;
4838 add_child_die (parent, child);
4841 /* Return a pointer to a newly created DIE node. */
4843 static inline dw_die_ref
4844 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4846 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4848 die->die_tag = tag_value;
4850 if (parent_die != NULL)
4851 add_child_die (parent_die, die);
4852 else
4854 limbo_die_node *limbo_node;
4856 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4857 limbo_node->die = die;
4858 limbo_node->created_for = t;
4859 limbo_node->next = limbo_die_list;
4860 limbo_die_list = limbo_node;
4863 return die;
4866 /* Return the DIE associated with the given type specifier. */
4868 static inline dw_die_ref
4869 lookup_type_die (tree type)
4871 return TYPE_SYMTAB_DIE (type);
4874 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4875 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4876 anonymous type instead the one of the naming typedef. */
4878 static inline dw_die_ref
4879 strip_naming_typedef (tree type, dw_die_ref type_die)
4881 if (type
4882 && TREE_CODE (type) == RECORD_TYPE
4883 && type_die
4884 && type_die->die_tag == DW_TAG_typedef
4885 && is_naming_typedef_decl (TYPE_NAME (type)))
4886 type_die = get_AT_ref (type_die, DW_AT_type);
4887 return type_die;
4890 /* Like lookup_type_die, but if type is an anonymous type named by a
4891 typedef[1], return the DIE of the anonymous type instead the one of
4892 the naming typedef. This is because in gen_typedef_die, we did
4893 equate the anonymous struct named by the typedef with the DIE of
4894 the naming typedef. So by default, lookup_type_die on an anonymous
4895 struct yields the DIE of the naming typedef.
4897 [1]: Read the comment of is_naming_typedef_decl to learn about what
4898 a naming typedef is. */
4900 static inline dw_die_ref
4901 lookup_type_die_strip_naming_typedef (tree type)
4903 dw_die_ref die = lookup_type_die (type);
4904 return strip_naming_typedef (type, die);
4907 /* Equate a DIE to a given type specifier. */
4909 static inline void
4910 equate_type_number_to_die (tree type, dw_die_ref type_die)
4912 TYPE_SYMTAB_DIE (type) = type_die;
4915 /* Returns a hash value for X (which really is a die_struct). */
4917 static hashval_t
4918 decl_die_table_hash (const void *x)
4920 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4923 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4925 static int
4926 decl_die_table_eq (const void *x, const void *y)
4928 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4931 /* Return the DIE associated with a given declaration. */
4933 static inline dw_die_ref
4934 lookup_decl_die (tree decl)
4936 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4939 /* Returns a hash value for X (which really is a var_loc_list). */
4941 static hashval_t
4942 decl_loc_table_hash (const void *x)
4944 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4947 /* Return nonzero if decl_id of var_loc_list X is the same as
4948 UID of decl *Y. */
4950 static int
4951 decl_loc_table_eq (const void *x, const void *y)
4953 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4956 /* Return the var_loc list associated with a given declaration. */
4958 static inline var_loc_list *
4959 lookup_decl_loc (const_tree decl)
4961 if (!decl_loc_table)
4962 return NULL;
4963 return (var_loc_list *)
4964 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4967 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4969 static hashval_t
4970 cached_dw_loc_list_table_hash (const void *x)
4972 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4975 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4976 UID of decl *Y. */
4978 static int
4979 cached_dw_loc_list_table_eq (const void *x, const void *y)
4981 return (((const cached_dw_loc_list *) x)->decl_id
4982 == DECL_UID ((const_tree) y));
4985 /* Equate a DIE to a particular declaration. */
4987 static void
4988 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4990 unsigned int decl_id = DECL_UID (decl);
4991 void **slot;
4993 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4994 *slot = decl_die;
4995 decl_die->decl_id = decl_id;
4998 /* Return how many bits covers PIECE EXPR_LIST. */
5000 static int
5001 decl_piece_bitsize (rtx piece)
5003 int ret = (int) GET_MODE (piece);
5004 if (ret)
5005 return ret;
5006 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5007 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5008 return INTVAL (XEXP (XEXP (piece, 0), 0));
5011 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5013 static rtx *
5014 decl_piece_varloc_ptr (rtx piece)
5016 if ((int) GET_MODE (piece))
5017 return &XEXP (piece, 0);
5018 else
5019 return &XEXP (XEXP (piece, 0), 1);
5022 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5023 Next is the chain of following piece nodes. */
5025 static rtx
5026 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5028 if (bitsize <= (int) MAX_MACHINE_MODE)
5029 return alloc_EXPR_LIST (bitsize, loc_note, next);
5030 else
5031 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5032 GEN_INT (bitsize),
5033 loc_note), next);
5036 /* Return rtx that should be stored into loc field for
5037 LOC_NOTE and BITPOS/BITSIZE. */
5039 static rtx
5040 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5041 HOST_WIDE_INT bitsize)
5043 if (bitsize != -1)
5045 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5046 if (bitpos != 0)
5047 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5049 return loc_note;
5052 /* This function either modifies location piece list *DEST in
5053 place (if SRC and INNER is NULL), or copies location piece list
5054 *SRC to *DEST while modifying it. Location BITPOS is modified
5055 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5056 not copied and if needed some padding around it is added.
5057 When modifying in place, DEST should point to EXPR_LIST where
5058 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5059 to the start of the whole list and INNER points to the EXPR_LIST
5060 where earlier pieces cover PIECE_BITPOS bits. */
5062 static void
5063 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5064 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5065 HOST_WIDE_INT bitsize, rtx loc_note)
5067 int diff;
5068 bool copy = inner != NULL;
5070 if (copy)
5072 /* First copy all nodes preceding the current bitpos. */
5073 while (src != inner)
5075 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5076 decl_piece_bitsize (*src), NULL_RTX);
5077 dest = &XEXP (*dest, 1);
5078 src = &XEXP (*src, 1);
5081 /* Add padding if needed. */
5082 if (bitpos != piece_bitpos)
5084 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5085 copy ? NULL_RTX : *dest);
5086 dest = &XEXP (*dest, 1);
5088 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5090 gcc_assert (!copy);
5091 /* A piece with correct bitpos and bitsize already exist,
5092 just update the location for it and return. */
5093 *decl_piece_varloc_ptr (*dest) = loc_note;
5094 return;
5096 /* Add the piece that changed. */
5097 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5098 dest = &XEXP (*dest, 1);
5099 /* Skip over pieces that overlap it. */
5100 diff = bitpos - piece_bitpos + bitsize;
5101 if (!copy)
5102 src = dest;
5103 while (diff > 0 && *src)
5105 rtx piece = *src;
5106 diff -= decl_piece_bitsize (piece);
5107 if (copy)
5108 src = &XEXP (piece, 1);
5109 else
5111 *src = XEXP (piece, 1);
5112 free_EXPR_LIST_node (piece);
5115 /* Add padding if needed. */
5116 if (diff < 0 && *src)
5118 if (!copy)
5119 dest = src;
5120 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5121 dest = &XEXP (*dest, 1);
5123 if (!copy)
5124 return;
5125 /* Finally copy all nodes following it. */
5126 while (*src)
5128 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5129 decl_piece_bitsize (*src), NULL_RTX);
5130 dest = &XEXP (*dest, 1);
5131 src = &XEXP (*src, 1);
5135 /* Add a variable location node to the linked list for DECL. */
5137 static struct var_loc_node *
5138 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5140 unsigned int decl_id;
5141 var_loc_list *temp;
5142 void **slot;
5143 struct var_loc_node *loc = NULL;
5144 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5146 if (TREE_CODE (decl) == VAR_DECL
5147 && DECL_HAS_DEBUG_EXPR_P (decl))
5149 tree realdecl = DECL_DEBUG_EXPR (decl);
5150 if (handled_component_p (realdecl)
5151 || (TREE_CODE (realdecl) == MEM_REF
5152 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5154 HOST_WIDE_INT maxsize;
5155 tree innerdecl;
5156 innerdecl
5157 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5158 if (!DECL_P (innerdecl)
5159 || DECL_IGNORED_P (innerdecl)
5160 || TREE_STATIC (innerdecl)
5161 || bitsize <= 0
5162 || bitpos + bitsize > 256
5163 || bitsize != maxsize)
5164 return NULL;
5165 decl = innerdecl;
5169 decl_id = DECL_UID (decl);
5170 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5171 if (*slot == NULL)
5173 temp = ggc_cleared_alloc<var_loc_list> ();
5174 temp->decl_id = decl_id;
5175 *slot = temp;
5177 else
5178 temp = (var_loc_list *) *slot;
5180 /* For PARM_DECLs try to keep around the original incoming value,
5181 even if that means we'll emit a zero-range .debug_loc entry. */
5182 if (temp->last
5183 && temp->first == temp->last
5184 && TREE_CODE (decl) == PARM_DECL
5185 && NOTE_P (temp->first->loc)
5186 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5187 && DECL_INCOMING_RTL (decl)
5188 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5189 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5190 == GET_CODE (DECL_INCOMING_RTL (decl))
5191 && prev_real_insn (temp->first->loc) == NULL_RTX
5192 && (bitsize != -1
5193 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5194 NOTE_VAR_LOCATION_LOC (loc_note))
5195 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5196 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5198 loc = ggc_cleared_alloc<var_loc_node> ();
5199 temp->first->next = loc;
5200 temp->last = loc;
5201 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5203 else if (temp->last)
5205 struct var_loc_node *last = temp->last, *unused = NULL;
5206 rtx *piece_loc = NULL, last_loc_note;
5207 int piece_bitpos = 0;
5208 if (last->next)
5210 last = last->next;
5211 gcc_assert (last->next == NULL);
5213 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5215 piece_loc = &last->loc;
5218 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5219 if (piece_bitpos + cur_bitsize > bitpos)
5220 break;
5221 piece_bitpos += cur_bitsize;
5222 piece_loc = &XEXP (*piece_loc, 1);
5224 while (*piece_loc);
5226 /* TEMP->LAST here is either pointer to the last but one or
5227 last element in the chained list, LAST is pointer to the
5228 last element. */
5229 if (label && strcmp (last->label, label) == 0)
5231 /* For SRA optimized variables if there weren't any real
5232 insns since last note, just modify the last node. */
5233 if (piece_loc != NULL)
5235 adjust_piece_list (piece_loc, NULL, NULL,
5236 bitpos, piece_bitpos, bitsize, loc_note);
5237 return NULL;
5239 /* If the last note doesn't cover any instructions, remove it. */
5240 if (temp->last != last)
5242 temp->last->next = NULL;
5243 unused = last;
5244 last = temp->last;
5245 gcc_assert (strcmp (last->label, label) != 0);
5247 else
5249 gcc_assert (temp->first == temp->last
5250 || (temp->first->next == temp->last
5251 && TREE_CODE (decl) == PARM_DECL));
5252 memset (temp->last, '\0', sizeof (*temp->last));
5253 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5254 return temp->last;
5257 if (bitsize == -1 && NOTE_P (last->loc))
5258 last_loc_note = last->loc;
5259 else if (piece_loc != NULL
5260 && *piece_loc != NULL_RTX
5261 && piece_bitpos == bitpos
5262 && decl_piece_bitsize (*piece_loc) == bitsize)
5263 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5264 else
5265 last_loc_note = NULL_RTX;
5266 /* If the current location is the same as the end of the list,
5267 and either both or neither of the locations is uninitialized,
5268 we have nothing to do. */
5269 if (last_loc_note == NULL_RTX
5270 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5271 NOTE_VAR_LOCATION_LOC (loc_note)))
5272 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5273 != NOTE_VAR_LOCATION_STATUS (loc_note))
5274 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5275 == VAR_INIT_STATUS_UNINITIALIZED)
5276 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5277 == VAR_INIT_STATUS_UNINITIALIZED))))
5279 /* Add LOC to the end of list and update LAST. If the last
5280 element of the list has been removed above, reuse its
5281 memory for the new node, otherwise allocate a new one. */
5282 if (unused)
5284 loc = unused;
5285 memset (loc, '\0', sizeof (*loc));
5287 else
5288 loc = ggc_cleared_alloc<var_loc_node> ();
5289 if (bitsize == -1 || piece_loc == NULL)
5290 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5291 else
5292 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5293 bitpos, piece_bitpos, bitsize, loc_note);
5294 last->next = loc;
5295 /* Ensure TEMP->LAST will point either to the new last but one
5296 element of the chain, or to the last element in it. */
5297 if (last != temp->last)
5298 temp->last = last;
5300 else if (unused)
5301 ggc_free (unused);
5303 else
5305 loc = ggc_cleared_alloc<var_loc_node> ();
5306 temp->first = loc;
5307 temp->last = loc;
5308 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5310 return loc;
5313 /* Keep track of the number of spaces used to indent the
5314 output of the debugging routines that print the structure of
5315 the DIE internal representation. */
5316 static int print_indent;
5318 /* Indent the line the number of spaces given by print_indent. */
5320 static inline void
5321 print_spaces (FILE *outfile)
5323 fprintf (outfile, "%*s", print_indent, "");
5326 /* Print a type signature in hex. */
5328 static inline void
5329 print_signature (FILE *outfile, char *sig)
5331 int i;
5333 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5334 fprintf (outfile, "%02x", sig[i] & 0xff);
5337 /* Print the information associated with a given DIE, and its children.
5338 This routine is a debugging aid only. */
5340 static void
5341 print_die (dw_die_ref die, FILE *outfile)
5343 dw_attr_ref a;
5344 dw_die_ref c;
5345 unsigned ix;
5347 print_spaces (outfile);
5348 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5349 die->die_offset, dwarf_tag_name (die->die_tag),
5350 (void*) die);
5351 print_spaces (outfile);
5352 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5353 fprintf (outfile, " offset: %ld", die->die_offset);
5354 fprintf (outfile, " mark: %d\n", die->die_mark);
5356 if (die->comdat_type_p)
5358 print_spaces (outfile);
5359 fprintf (outfile, " signature: ");
5360 print_signature (outfile, die->die_id.die_type_node->signature);
5361 fprintf (outfile, "\n");
5364 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5366 print_spaces (outfile);
5367 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5369 switch (AT_class (a))
5371 case dw_val_class_addr:
5372 fprintf (outfile, "address");
5373 break;
5374 case dw_val_class_offset:
5375 fprintf (outfile, "offset");
5376 break;
5377 case dw_val_class_loc:
5378 fprintf (outfile, "location descriptor");
5379 break;
5380 case dw_val_class_loc_list:
5381 fprintf (outfile, "location list -> label:%s",
5382 AT_loc_list (a)->ll_symbol);
5383 break;
5384 case dw_val_class_range_list:
5385 fprintf (outfile, "range list");
5386 break;
5387 case dw_val_class_const:
5388 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5389 break;
5390 case dw_val_class_unsigned_const:
5391 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5392 break;
5393 case dw_val_class_const_double:
5394 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5395 HOST_WIDE_INT_PRINT_UNSIGNED")",
5396 a->dw_attr_val.v.val_double.high,
5397 a->dw_attr_val.v.val_double.low);
5398 break;
5399 case dw_val_class_wide_int:
5401 int i = a->dw_attr_val.v.val_wide->get_len ();
5402 fprintf (outfile, "constant (");
5403 gcc_assert (i > 0);
5404 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5405 fprintf (outfile, "0x");
5406 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5407 a->dw_attr_val.v.val_wide->elt (--i));
5408 while (--i >= 0)
5409 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5410 a->dw_attr_val.v.val_wide->elt (i));
5411 fprintf (outfile, ")");
5412 break;
5414 case dw_val_class_vec:
5415 fprintf (outfile, "floating-point or vector constant");
5416 break;
5417 case dw_val_class_flag:
5418 fprintf (outfile, "%u", AT_flag (a));
5419 break;
5420 case dw_val_class_die_ref:
5421 if (AT_ref (a) != NULL)
5423 if (AT_ref (a)->comdat_type_p)
5425 fprintf (outfile, "die -> signature: ");
5426 print_signature (outfile,
5427 AT_ref (a)->die_id.die_type_node->signature);
5429 else if (AT_ref (a)->die_id.die_symbol)
5430 fprintf (outfile, "die -> label: %s",
5431 AT_ref (a)->die_id.die_symbol);
5432 else
5433 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5434 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5436 else
5437 fprintf (outfile, "die -> <null>");
5438 break;
5439 case dw_val_class_vms_delta:
5440 fprintf (outfile, "delta: @slotcount(%s-%s)",
5441 AT_vms_delta2 (a), AT_vms_delta1 (a));
5442 break;
5443 case dw_val_class_lbl_id:
5444 case dw_val_class_lineptr:
5445 case dw_val_class_macptr:
5446 case dw_val_class_high_pc:
5447 fprintf (outfile, "label: %s", AT_lbl (a));
5448 break;
5449 case dw_val_class_str:
5450 if (AT_string (a) != NULL)
5451 fprintf (outfile, "\"%s\"", AT_string (a));
5452 else
5453 fprintf (outfile, "<null>");
5454 break;
5455 case dw_val_class_file:
5456 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5457 AT_file (a)->emitted_number);
5458 break;
5459 case dw_val_class_data8:
5461 int i;
5463 for (i = 0; i < 8; i++)
5464 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5465 break;
5467 default:
5468 break;
5471 fprintf (outfile, "\n");
5474 if (die->die_child != NULL)
5476 print_indent += 4;
5477 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5478 print_indent -= 4;
5480 if (print_indent == 0)
5481 fprintf (outfile, "\n");
5484 /* Print the information collected for a given DIE. */
5486 DEBUG_FUNCTION void
5487 debug_dwarf_die (dw_die_ref die)
5489 print_die (die, stderr);
5492 DEBUG_FUNCTION void
5493 debug (die_struct &ref)
5495 print_die (&ref, stderr);
5498 DEBUG_FUNCTION void
5499 debug (die_struct *ptr)
5501 if (ptr)
5502 debug (*ptr);
5503 else
5504 fprintf (stderr, "<nil>\n");
5508 /* Print all DWARF information collected for the compilation unit.
5509 This routine is a debugging aid only. */
5511 DEBUG_FUNCTION void
5512 debug_dwarf (void)
5514 print_indent = 0;
5515 print_die (comp_unit_die (), stderr);
5518 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5519 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5520 DIE that marks the start of the DIEs for this include file. */
5522 static dw_die_ref
5523 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5525 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5526 dw_die_ref new_unit = gen_compile_unit_die (filename);
5528 new_unit->die_sib = old_unit;
5529 return new_unit;
5532 /* Close an include-file CU and reopen the enclosing one. */
5534 static dw_die_ref
5535 pop_compile_unit (dw_die_ref old_unit)
5537 dw_die_ref new_unit = old_unit->die_sib;
5539 old_unit->die_sib = NULL;
5540 return new_unit;
5543 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5544 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5545 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5547 /* Calculate the checksum of a location expression. */
5549 static inline void
5550 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5552 int tem;
5553 inchash::hash hstate;
5554 hashval_t hash;
5556 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5557 CHECKSUM (tem);
5558 hash_loc_operands (loc, hstate);
5559 hash = hstate.end();
5560 CHECKSUM (hash);
5563 /* Calculate the checksum of an attribute. */
5565 static void
5566 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5568 dw_loc_descr_ref loc;
5569 rtx r;
5571 CHECKSUM (at->dw_attr);
5573 /* We don't care that this was compiled with a different compiler
5574 snapshot; if the output is the same, that's what matters. */
5575 if (at->dw_attr == DW_AT_producer)
5576 return;
5578 switch (AT_class (at))
5580 case dw_val_class_const:
5581 CHECKSUM (at->dw_attr_val.v.val_int);
5582 break;
5583 case dw_val_class_unsigned_const:
5584 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5585 break;
5586 case dw_val_class_const_double:
5587 CHECKSUM (at->dw_attr_val.v.val_double);
5588 break;
5589 case dw_val_class_wide_int:
5590 CHECKSUM (*at->dw_attr_val.v.val_wide);
5591 break;
5592 case dw_val_class_vec:
5593 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5594 (at->dw_attr_val.v.val_vec.length
5595 * at->dw_attr_val.v.val_vec.elt_size));
5596 break;
5597 case dw_val_class_flag:
5598 CHECKSUM (at->dw_attr_val.v.val_flag);
5599 break;
5600 case dw_val_class_str:
5601 CHECKSUM_STRING (AT_string (at));
5602 break;
5604 case dw_val_class_addr:
5605 r = AT_addr (at);
5606 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5607 CHECKSUM_STRING (XSTR (r, 0));
5608 break;
5610 case dw_val_class_offset:
5611 CHECKSUM (at->dw_attr_val.v.val_offset);
5612 break;
5614 case dw_val_class_loc:
5615 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5616 loc_checksum (loc, ctx);
5617 break;
5619 case dw_val_class_die_ref:
5620 die_checksum (AT_ref (at), ctx, mark);
5621 break;
5623 case dw_val_class_fde_ref:
5624 case dw_val_class_vms_delta:
5625 case dw_val_class_lbl_id:
5626 case dw_val_class_lineptr:
5627 case dw_val_class_macptr:
5628 case dw_val_class_high_pc:
5629 break;
5631 case dw_val_class_file:
5632 CHECKSUM_STRING (AT_file (at)->filename);
5633 break;
5635 case dw_val_class_data8:
5636 CHECKSUM (at->dw_attr_val.v.val_data8);
5637 break;
5639 default:
5640 break;
5644 /* Calculate the checksum of a DIE. */
5646 static void
5647 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5649 dw_die_ref c;
5650 dw_attr_ref a;
5651 unsigned ix;
5653 /* To avoid infinite recursion. */
5654 if (die->die_mark)
5656 CHECKSUM (die->die_mark);
5657 return;
5659 die->die_mark = ++(*mark);
5661 CHECKSUM (die->die_tag);
5663 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5664 attr_checksum (a, ctx, mark);
5666 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5669 #undef CHECKSUM
5670 #undef CHECKSUM_BLOCK
5671 #undef CHECKSUM_STRING
5673 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5674 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5675 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5676 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5677 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5678 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5679 #define CHECKSUM_ATTR(FOO) \
5680 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5682 /* Calculate the checksum of a number in signed LEB128 format. */
5684 static void
5685 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5687 unsigned char byte;
5688 bool more;
5690 while (1)
5692 byte = (value & 0x7f);
5693 value >>= 7;
5694 more = !((value == 0 && (byte & 0x40) == 0)
5695 || (value == -1 && (byte & 0x40) != 0));
5696 if (more)
5697 byte |= 0x80;
5698 CHECKSUM (byte);
5699 if (!more)
5700 break;
5704 /* Calculate the checksum of a number in unsigned LEB128 format. */
5706 static void
5707 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5709 while (1)
5711 unsigned char byte = (value & 0x7f);
5712 value >>= 7;
5713 if (value != 0)
5714 /* More bytes to follow. */
5715 byte |= 0x80;
5716 CHECKSUM (byte);
5717 if (value == 0)
5718 break;
5722 /* Checksum the context of the DIE. This adds the names of any
5723 surrounding namespaces or structures to the checksum. */
5725 static void
5726 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5728 const char *name;
5729 dw_die_ref spec;
5730 int tag = die->die_tag;
5732 if (tag != DW_TAG_namespace
5733 && tag != DW_TAG_structure_type
5734 && tag != DW_TAG_class_type)
5735 return;
5737 name = get_AT_string (die, DW_AT_name);
5739 spec = get_AT_ref (die, DW_AT_specification);
5740 if (spec != NULL)
5741 die = spec;
5743 if (die->die_parent != NULL)
5744 checksum_die_context (die->die_parent, ctx);
5746 CHECKSUM_ULEB128 ('C');
5747 CHECKSUM_ULEB128 (tag);
5748 if (name != NULL)
5749 CHECKSUM_STRING (name);
5752 /* Calculate the checksum of a location expression. */
5754 static inline void
5755 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5757 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5758 were emitted as a DW_FORM_sdata instead of a location expression. */
5759 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5761 CHECKSUM_ULEB128 (DW_FORM_sdata);
5762 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5763 return;
5766 /* Otherwise, just checksum the raw location expression. */
5767 while (loc != NULL)
5769 inchash::hash hstate;
5770 hashval_t hash;
5772 CHECKSUM_ULEB128 (loc->dtprel);
5773 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5774 hash_loc_operands (loc, hstate);
5775 hash = hstate.end ();
5776 CHECKSUM (hash);
5777 loc = loc->dw_loc_next;
5781 /* Calculate the checksum of an attribute. */
5783 static void
5784 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5785 struct md5_ctx *ctx, int *mark)
5787 dw_loc_descr_ref loc;
5788 rtx r;
5790 if (AT_class (at) == dw_val_class_die_ref)
5792 dw_die_ref target_die = AT_ref (at);
5794 /* For pointer and reference types, we checksum only the (qualified)
5795 name of the target type (if there is a name). For friend entries,
5796 we checksum only the (qualified) name of the target type or function.
5797 This allows the checksum to remain the same whether the target type
5798 is complete or not. */
5799 if ((at->dw_attr == DW_AT_type
5800 && (tag == DW_TAG_pointer_type
5801 || tag == DW_TAG_reference_type
5802 || tag == DW_TAG_rvalue_reference_type
5803 || tag == DW_TAG_ptr_to_member_type))
5804 || (at->dw_attr == DW_AT_friend
5805 && tag == DW_TAG_friend))
5807 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5809 if (name_attr != NULL)
5811 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5813 if (decl == NULL)
5814 decl = target_die;
5815 CHECKSUM_ULEB128 ('N');
5816 CHECKSUM_ULEB128 (at->dw_attr);
5817 if (decl->die_parent != NULL)
5818 checksum_die_context (decl->die_parent, ctx);
5819 CHECKSUM_ULEB128 ('E');
5820 CHECKSUM_STRING (AT_string (name_attr));
5821 return;
5825 /* For all other references to another DIE, we check to see if the
5826 target DIE has already been visited. If it has, we emit a
5827 backward reference; if not, we descend recursively. */
5828 if (target_die->die_mark > 0)
5830 CHECKSUM_ULEB128 ('R');
5831 CHECKSUM_ULEB128 (at->dw_attr);
5832 CHECKSUM_ULEB128 (target_die->die_mark);
5834 else
5836 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5838 if (decl == NULL)
5839 decl = target_die;
5840 target_die->die_mark = ++(*mark);
5841 CHECKSUM_ULEB128 ('T');
5842 CHECKSUM_ULEB128 (at->dw_attr);
5843 if (decl->die_parent != NULL)
5844 checksum_die_context (decl->die_parent, ctx);
5845 die_checksum_ordered (target_die, ctx, mark);
5847 return;
5850 CHECKSUM_ULEB128 ('A');
5851 CHECKSUM_ULEB128 (at->dw_attr);
5853 switch (AT_class (at))
5855 case dw_val_class_const:
5856 CHECKSUM_ULEB128 (DW_FORM_sdata);
5857 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5858 break;
5860 case dw_val_class_unsigned_const:
5861 CHECKSUM_ULEB128 (DW_FORM_sdata);
5862 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5863 break;
5865 case dw_val_class_const_double:
5866 CHECKSUM_ULEB128 (DW_FORM_block);
5867 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5868 CHECKSUM (at->dw_attr_val.v.val_double);
5869 break;
5871 case dw_val_class_wide_int:
5872 CHECKSUM_ULEB128 (DW_FORM_block);
5873 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5874 CHECKSUM (*at->dw_attr_val.v.val_wide);
5875 break;
5877 case dw_val_class_vec:
5878 CHECKSUM_ULEB128 (DW_FORM_block);
5879 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5880 * at->dw_attr_val.v.val_vec.elt_size);
5881 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5882 (at->dw_attr_val.v.val_vec.length
5883 * at->dw_attr_val.v.val_vec.elt_size));
5884 break;
5886 case dw_val_class_flag:
5887 CHECKSUM_ULEB128 (DW_FORM_flag);
5888 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5889 break;
5891 case dw_val_class_str:
5892 CHECKSUM_ULEB128 (DW_FORM_string);
5893 CHECKSUM_STRING (AT_string (at));
5894 break;
5896 case dw_val_class_addr:
5897 r = AT_addr (at);
5898 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5899 CHECKSUM_ULEB128 (DW_FORM_string);
5900 CHECKSUM_STRING (XSTR (r, 0));
5901 break;
5903 case dw_val_class_offset:
5904 CHECKSUM_ULEB128 (DW_FORM_sdata);
5905 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5906 break;
5908 case dw_val_class_loc:
5909 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5910 loc_checksum_ordered (loc, ctx);
5911 break;
5913 case dw_val_class_fde_ref:
5914 case dw_val_class_lbl_id:
5915 case dw_val_class_lineptr:
5916 case dw_val_class_macptr:
5917 case dw_val_class_high_pc:
5918 break;
5920 case dw_val_class_file:
5921 CHECKSUM_ULEB128 (DW_FORM_string);
5922 CHECKSUM_STRING (AT_file (at)->filename);
5923 break;
5925 case dw_val_class_data8:
5926 CHECKSUM (at->dw_attr_val.v.val_data8);
5927 break;
5929 default:
5930 break;
5934 struct checksum_attributes
5936 dw_attr_ref at_name;
5937 dw_attr_ref at_type;
5938 dw_attr_ref at_friend;
5939 dw_attr_ref at_accessibility;
5940 dw_attr_ref at_address_class;
5941 dw_attr_ref at_allocated;
5942 dw_attr_ref at_artificial;
5943 dw_attr_ref at_associated;
5944 dw_attr_ref at_binary_scale;
5945 dw_attr_ref at_bit_offset;
5946 dw_attr_ref at_bit_size;
5947 dw_attr_ref at_bit_stride;
5948 dw_attr_ref at_byte_size;
5949 dw_attr_ref at_byte_stride;
5950 dw_attr_ref at_const_value;
5951 dw_attr_ref at_containing_type;
5952 dw_attr_ref at_count;
5953 dw_attr_ref at_data_location;
5954 dw_attr_ref at_data_member_location;
5955 dw_attr_ref at_decimal_scale;
5956 dw_attr_ref at_decimal_sign;
5957 dw_attr_ref at_default_value;
5958 dw_attr_ref at_digit_count;
5959 dw_attr_ref at_discr;
5960 dw_attr_ref at_discr_list;
5961 dw_attr_ref at_discr_value;
5962 dw_attr_ref at_encoding;
5963 dw_attr_ref at_endianity;
5964 dw_attr_ref at_explicit;
5965 dw_attr_ref at_is_optional;
5966 dw_attr_ref at_location;
5967 dw_attr_ref at_lower_bound;
5968 dw_attr_ref at_mutable;
5969 dw_attr_ref at_ordering;
5970 dw_attr_ref at_picture_string;
5971 dw_attr_ref at_prototyped;
5972 dw_attr_ref at_small;
5973 dw_attr_ref at_segment;
5974 dw_attr_ref at_string_length;
5975 dw_attr_ref at_threads_scaled;
5976 dw_attr_ref at_upper_bound;
5977 dw_attr_ref at_use_location;
5978 dw_attr_ref at_use_UTF8;
5979 dw_attr_ref at_variable_parameter;
5980 dw_attr_ref at_virtuality;
5981 dw_attr_ref at_visibility;
5982 dw_attr_ref at_vtable_elem_location;
5985 /* Collect the attributes that we will want to use for the checksum. */
5987 static void
5988 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5990 dw_attr_ref a;
5991 unsigned ix;
5993 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5995 switch (a->dw_attr)
5997 case DW_AT_name:
5998 attrs->at_name = a;
5999 break;
6000 case DW_AT_type:
6001 attrs->at_type = a;
6002 break;
6003 case DW_AT_friend:
6004 attrs->at_friend = a;
6005 break;
6006 case DW_AT_accessibility:
6007 attrs->at_accessibility = a;
6008 break;
6009 case DW_AT_address_class:
6010 attrs->at_address_class = a;
6011 break;
6012 case DW_AT_allocated:
6013 attrs->at_allocated = a;
6014 break;
6015 case DW_AT_artificial:
6016 attrs->at_artificial = a;
6017 break;
6018 case DW_AT_associated:
6019 attrs->at_associated = a;
6020 break;
6021 case DW_AT_binary_scale:
6022 attrs->at_binary_scale = a;
6023 break;
6024 case DW_AT_bit_offset:
6025 attrs->at_bit_offset = a;
6026 break;
6027 case DW_AT_bit_size:
6028 attrs->at_bit_size = a;
6029 break;
6030 case DW_AT_bit_stride:
6031 attrs->at_bit_stride = a;
6032 break;
6033 case DW_AT_byte_size:
6034 attrs->at_byte_size = a;
6035 break;
6036 case DW_AT_byte_stride:
6037 attrs->at_byte_stride = a;
6038 break;
6039 case DW_AT_const_value:
6040 attrs->at_const_value = a;
6041 break;
6042 case DW_AT_containing_type:
6043 attrs->at_containing_type = a;
6044 break;
6045 case DW_AT_count:
6046 attrs->at_count = a;
6047 break;
6048 case DW_AT_data_location:
6049 attrs->at_data_location = a;
6050 break;
6051 case DW_AT_data_member_location:
6052 attrs->at_data_member_location = a;
6053 break;
6054 case DW_AT_decimal_scale:
6055 attrs->at_decimal_scale = a;
6056 break;
6057 case DW_AT_decimal_sign:
6058 attrs->at_decimal_sign = a;
6059 break;
6060 case DW_AT_default_value:
6061 attrs->at_default_value = a;
6062 break;
6063 case DW_AT_digit_count:
6064 attrs->at_digit_count = a;
6065 break;
6066 case DW_AT_discr:
6067 attrs->at_discr = a;
6068 break;
6069 case DW_AT_discr_list:
6070 attrs->at_discr_list = a;
6071 break;
6072 case DW_AT_discr_value:
6073 attrs->at_discr_value = a;
6074 break;
6075 case DW_AT_encoding:
6076 attrs->at_encoding = a;
6077 break;
6078 case DW_AT_endianity:
6079 attrs->at_endianity = a;
6080 break;
6081 case DW_AT_explicit:
6082 attrs->at_explicit = a;
6083 break;
6084 case DW_AT_is_optional:
6085 attrs->at_is_optional = a;
6086 break;
6087 case DW_AT_location:
6088 attrs->at_location = a;
6089 break;
6090 case DW_AT_lower_bound:
6091 attrs->at_lower_bound = a;
6092 break;
6093 case DW_AT_mutable:
6094 attrs->at_mutable = a;
6095 break;
6096 case DW_AT_ordering:
6097 attrs->at_ordering = a;
6098 break;
6099 case DW_AT_picture_string:
6100 attrs->at_picture_string = a;
6101 break;
6102 case DW_AT_prototyped:
6103 attrs->at_prototyped = a;
6104 break;
6105 case DW_AT_small:
6106 attrs->at_small = a;
6107 break;
6108 case DW_AT_segment:
6109 attrs->at_segment = a;
6110 break;
6111 case DW_AT_string_length:
6112 attrs->at_string_length = a;
6113 break;
6114 case DW_AT_threads_scaled:
6115 attrs->at_threads_scaled = a;
6116 break;
6117 case DW_AT_upper_bound:
6118 attrs->at_upper_bound = a;
6119 break;
6120 case DW_AT_use_location:
6121 attrs->at_use_location = a;
6122 break;
6123 case DW_AT_use_UTF8:
6124 attrs->at_use_UTF8 = a;
6125 break;
6126 case DW_AT_variable_parameter:
6127 attrs->at_variable_parameter = a;
6128 break;
6129 case DW_AT_virtuality:
6130 attrs->at_virtuality = a;
6131 break;
6132 case DW_AT_visibility:
6133 attrs->at_visibility = a;
6134 break;
6135 case DW_AT_vtable_elem_location:
6136 attrs->at_vtable_elem_location = a;
6137 break;
6138 default:
6139 break;
6144 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6146 static void
6147 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6149 dw_die_ref c;
6150 dw_die_ref decl;
6151 struct checksum_attributes attrs;
6153 CHECKSUM_ULEB128 ('D');
6154 CHECKSUM_ULEB128 (die->die_tag);
6156 memset (&attrs, 0, sizeof (attrs));
6158 decl = get_AT_ref (die, DW_AT_specification);
6159 if (decl != NULL)
6160 collect_checksum_attributes (&attrs, decl);
6161 collect_checksum_attributes (&attrs, die);
6163 CHECKSUM_ATTR (attrs.at_name);
6164 CHECKSUM_ATTR (attrs.at_accessibility);
6165 CHECKSUM_ATTR (attrs.at_address_class);
6166 CHECKSUM_ATTR (attrs.at_allocated);
6167 CHECKSUM_ATTR (attrs.at_artificial);
6168 CHECKSUM_ATTR (attrs.at_associated);
6169 CHECKSUM_ATTR (attrs.at_binary_scale);
6170 CHECKSUM_ATTR (attrs.at_bit_offset);
6171 CHECKSUM_ATTR (attrs.at_bit_size);
6172 CHECKSUM_ATTR (attrs.at_bit_stride);
6173 CHECKSUM_ATTR (attrs.at_byte_size);
6174 CHECKSUM_ATTR (attrs.at_byte_stride);
6175 CHECKSUM_ATTR (attrs.at_const_value);
6176 CHECKSUM_ATTR (attrs.at_containing_type);
6177 CHECKSUM_ATTR (attrs.at_count);
6178 CHECKSUM_ATTR (attrs.at_data_location);
6179 CHECKSUM_ATTR (attrs.at_data_member_location);
6180 CHECKSUM_ATTR (attrs.at_decimal_scale);
6181 CHECKSUM_ATTR (attrs.at_decimal_sign);
6182 CHECKSUM_ATTR (attrs.at_default_value);
6183 CHECKSUM_ATTR (attrs.at_digit_count);
6184 CHECKSUM_ATTR (attrs.at_discr);
6185 CHECKSUM_ATTR (attrs.at_discr_list);
6186 CHECKSUM_ATTR (attrs.at_discr_value);
6187 CHECKSUM_ATTR (attrs.at_encoding);
6188 CHECKSUM_ATTR (attrs.at_endianity);
6189 CHECKSUM_ATTR (attrs.at_explicit);
6190 CHECKSUM_ATTR (attrs.at_is_optional);
6191 CHECKSUM_ATTR (attrs.at_location);
6192 CHECKSUM_ATTR (attrs.at_lower_bound);
6193 CHECKSUM_ATTR (attrs.at_mutable);
6194 CHECKSUM_ATTR (attrs.at_ordering);
6195 CHECKSUM_ATTR (attrs.at_picture_string);
6196 CHECKSUM_ATTR (attrs.at_prototyped);
6197 CHECKSUM_ATTR (attrs.at_small);
6198 CHECKSUM_ATTR (attrs.at_segment);
6199 CHECKSUM_ATTR (attrs.at_string_length);
6200 CHECKSUM_ATTR (attrs.at_threads_scaled);
6201 CHECKSUM_ATTR (attrs.at_upper_bound);
6202 CHECKSUM_ATTR (attrs.at_use_location);
6203 CHECKSUM_ATTR (attrs.at_use_UTF8);
6204 CHECKSUM_ATTR (attrs.at_variable_parameter);
6205 CHECKSUM_ATTR (attrs.at_virtuality);
6206 CHECKSUM_ATTR (attrs.at_visibility);
6207 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6208 CHECKSUM_ATTR (attrs.at_type);
6209 CHECKSUM_ATTR (attrs.at_friend);
6211 /* Checksum the child DIEs. */
6212 c = die->die_child;
6213 if (c) do {
6214 dw_attr_ref name_attr;
6216 c = c->die_sib;
6217 name_attr = get_AT (c, DW_AT_name);
6218 if (is_template_instantiation (c))
6220 /* Ignore instantiations of member type and function templates. */
6222 else if (name_attr != NULL
6223 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6225 /* Use a shallow checksum for named nested types and member
6226 functions. */
6227 CHECKSUM_ULEB128 ('S');
6228 CHECKSUM_ULEB128 (c->die_tag);
6229 CHECKSUM_STRING (AT_string (name_attr));
6231 else
6233 /* Use a deep checksum for other children. */
6234 /* Mark this DIE so it gets processed when unmarking. */
6235 if (c->die_mark == 0)
6236 c->die_mark = -1;
6237 die_checksum_ordered (c, ctx, mark);
6239 } while (c != die->die_child);
6241 CHECKSUM_ULEB128 (0);
6244 /* Add a type name and tag to a hash. */
6245 static void
6246 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6248 CHECKSUM_ULEB128 (tag);
6249 CHECKSUM_STRING (name);
6252 #undef CHECKSUM
6253 #undef CHECKSUM_STRING
6254 #undef CHECKSUM_ATTR
6255 #undef CHECKSUM_LEB128
6256 #undef CHECKSUM_ULEB128
6258 /* Generate the type signature for DIE. This is computed by generating an
6259 MD5 checksum over the DIE's tag, its relevant attributes, and its
6260 children. Attributes that are references to other DIEs are processed
6261 by recursion, using the MARK field to prevent infinite recursion.
6262 If the DIE is nested inside a namespace or another type, we also
6263 need to include that context in the signature. The lower 64 bits
6264 of the resulting MD5 checksum comprise the signature. */
6266 static void
6267 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6269 int mark;
6270 const char *name;
6271 unsigned char checksum[16];
6272 struct md5_ctx ctx;
6273 dw_die_ref decl;
6274 dw_die_ref parent;
6276 name = get_AT_string (die, DW_AT_name);
6277 decl = get_AT_ref (die, DW_AT_specification);
6278 parent = get_die_parent (die);
6280 /* First, compute a signature for just the type name (and its surrounding
6281 context, if any. This is stored in the type unit DIE for link-time
6282 ODR (one-definition rule) checking. */
6284 if (is_cxx () && name != NULL)
6286 md5_init_ctx (&ctx);
6288 /* Checksum the names of surrounding namespaces and structures. */
6289 if (parent != NULL)
6290 checksum_die_context (parent, &ctx);
6292 /* Checksum the current DIE. */
6293 die_odr_checksum (die->die_tag, name, &ctx);
6294 md5_finish_ctx (&ctx, checksum);
6296 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6299 /* Next, compute the complete type signature. */
6301 md5_init_ctx (&ctx);
6302 mark = 1;
6303 die->die_mark = mark;
6305 /* Checksum the names of surrounding namespaces and structures. */
6306 if (parent != NULL)
6307 checksum_die_context (parent, &ctx);
6309 /* Checksum the DIE and its children. */
6310 die_checksum_ordered (die, &ctx, &mark);
6311 unmark_all_dies (die);
6312 md5_finish_ctx (&ctx, checksum);
6314 /* Store the signature in the type node and link the type DIE and the
6315 type node together. */
6316 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6317 DWARF_TYPE_SIGNATURE_SIZE);
6318 die->comdat_type_p = true;
6319 die->die_id.die_type_node = type_node;
6320 type_node->type_die = die;
6322 /* If the DIE is a specification, link its declaration to the type node
6323 as well. */
6324 if (decl != NULL)
6326 decl->comdat_type_p = true;
6327 decl->die_id.die_type_node = type_node;
6331 /* Do the location expressions look same? */
6332 static inline int
6333 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6335 return loc1->dw_loc_opc == loc2->dw_loc_opc
6336 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6337 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6340 /* Do the values look the same? */
6341 static int
6342 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6344 dw_loc_descr_ref loc1, loc2;
6345 rtx r1, r2;
6347 if (v1->val_class != v2->val_class)
6348 return 0;
6350 switch (v1->val_class)
6352 case dw_val_class_const:
6353 return v1->v.val_int == v2->v.val_int;
6354 case dw_val_class_unsigned_const:
6355 return v1->v.val_unsigned == v2->v.val_unsigned;
6356 case dw_val_class_const_double:
6357 return v1->v.val_double.high == v2->v.val_double.high
6358 && v1->v.val_double.low == v2->v.val_double.low;
6359 case dw_val_class_wide_int:
6360 return *v1->v.val_wide == *v2->v.val_wide;
6361 case dw_val_class_vec:
6362 if (v1->v.val_vec.length != v2->v.val_vec.length
6363 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6364 return 0;
6365 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6366 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6367 return 0;
6368 return 1;
6369 case dw_val_class_flag:
6370 return v1->v.val_flag == v2->v.val_flag;
6371 case dw_val_class_str:
6372 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6374 case dw_val_class_addr:
6375 r1 = v1->v.val_addr;
6376 r2 = v2->v.val_addr;
6377 if (GET_CODE (r1) != GET_CODE (r2))
6378 return 0;
6379 return !rtx_equal_p (r1, r2);
6381 case dw_val_class_offset:
6382 return v1->v.val_offset == v2->v.val_offset;
6384 case dw_val_class_loc:
6385 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6386 loc1 && loc2;
6387 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6388 if (!same_loc_p (loc1, loc2, mark))
6389 return 0;
6390 return !loc1 && !loc2;
6392 case dw_val_class_die_ref:
6393 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6395 case dw_val_class_fde_ref:
6396 case dw_val_class_vms_delta:
6397 case dw_val_class_lbl_id:
6398 case dw_val_class_lineptr:
6399 case dw_val_class_macptr:
6400 case dw_val_class_high_pc:
6401 return 1;
6403 case dw_val_class_file:
6404 return v1->v.val_file == v2->v.val_file;
6406 case dw_val_class_data8:
6407 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6409 default:
6410 return 1;
6414 /* Do the attributes look the same? */
6416 static int
6417 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6419 if (at1->dw_attr != at2->dw_attr)
6420 return 0;
6422 /* We don't care that this was compiled with a different compiler
6423 snapshot; if the output is the same, that's what matters. */
6424 if (at1->dw_attr == DW_AT_producer)
6425 return 1;
6427 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6430 /* Do the dies look the same? */
6432 static int
6433 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6435 dw_die_ref c1, c2;
6436 dw_attr_ref a1;
6437 unsigned ix;
6439 /* To avoid infinite recursion. */
6440 if (die1->die_mark)
6441 return die1->die_mark == die2->die_mark;
6442 die1->die_mark = die2->die_mark = ++(*mark);
6444 if (die1->die_tag != die2->die_tag)
6445 return 0;
6447 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6448 return 0;
6450 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6451 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6452 return 0;
6454 c1 = die1->die_child;
6455 c2 = die2->die_child;
6456 if (! c1)
6458 if (c2)
6459 return 0;
6461 else
6462 for (;;)
6464 if (!same_die_p (c1, c2, mark))
6465 return 0;
6466 c1 = c1->die_sib;
6467 c2 = c2->die_sib;
6468 if (c1 == die1->die_child)
6470 if (c2 == die2->die_child)
6471 break;
6472 else
6473 return 0;
6477 return 1;
6480 /* Do the dies look the same? Wrapper around same_die_p. */
6482 static int
6483 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6485 int mark = 0;
6486 int ret = same_die_p (die1, die2, &mark);
6488 unmark_all_dies (die1);
6489 unmark_all_dies (die2);
6491 return ret;
6494 /* The prefix to attach to symbols on DIEs in the current comdat debug
6495 info section. */
6496 static const char *comdat_symbol_id;
6498 /* The index of the current symbol within the current comdat CU. */
6499 static unsigned int comdat_symbol_number;
6501 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6502 children, and set comdat_symbol_id accordingly. */
6504 static void
6505 compute_section_prefix (dw_die_ref unit_die)
6507 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6508 const char *base = die_name ? lbasename (die_name) : "anonymous";
6509 char *name = XALLOCAVEC (char, strlen (base) + 64);
6510 char *p;
6511 int i, mark;
6512 unsigned char checksum[16];
6513 struct md5_ctx ctx;
6515 /* Compute the checksum of the DIE, then append part of it as hex digits to
6516 the name filename of the unit. */
6518 md5_init_ctx (&ctx);
6519 mark = 0;
6520 die_checksum (unit_die, &ctx, &mark);
6521 unmark_all_dies (unit_die);
6522 md5_finish_ctx (&ctx, checksum);
6524 sprintf (name, "%s.", base);
6525 clean_symbol_name (name);
6527 p = name + strlen (name);
6528 for (i = 0; i < 4; i++)
6530 sprintf (p, "%.2x", checksum[i]);
6531 p += 2;
6534 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6535 comdat_symbol_number = 0;
6538 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6540 static int
6541 is_type_die (dw_die_ref die)
6543 switch (die->die_tag)
6545 case DW_TAG_array_type:
6546 case DW_TAG_class_type:
6547 case DW_TAG_interface_type:
6548 case DW_TAG_enumeration_type:
6549 case DW_TAG_pointer_type:
6550 case DW_TAG_reference_type:
6551 case DW_TAG_rvalue_reference_type:
6552 case DW_TAG_string_type:
6553 case DW_TAG_structure_type:
6554 case DW_TAG_subroutine_type:
6555 case DW_TAG_union_type:
6556 case DW_TAG_ptr_to_member_type:
6557 case DW_TAG_set_type:
6558 case DW_TAG_subrange_type:
6559 case DW_TAG_base_type:
6560 case DW_TAG_const_type:
6561 case DW_TAG_file_type:
6562 case DW_TAG_packed_type:
6563 case DW_TAG_volatile_type:
6564 case DW_TAG_typedef:
6565 return 1;
6566 default:
6567 return 0;
6571 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6572 Basically, we want to choose the bits that are likely to be shared between
6573 compilations (types) and leave out the bits that are specific to individual
6574 compilations (functions). */
6576 static int
6577 is_comdat_die (dw_die_ref c)
6579 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6580 we do for stabs. The advantage is a greater likelihood of sharing between
6581 objects that don't include headers in the same order (and therefore would
6582 put the base types in a different comdat). jason 8/28/00 */
6584 if (c->die_tag == DW_TAG_base_type)
6585 return 0;
6587 if (c->die_tag == DW_TAG_pointer_type
6588 || c->die_tag == DW_TAG_reference_type
6589 || c->die_tag == DW_TAG_rvalue_reference_type
6590 || c->die_tag == DW_TAG_const_type
6591 || c->die_tag == DW_TAG_volatile_type)
6593 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6595 return t ? is_comdat_die (t) : 0;
6598 return is_type_die (c);
6601 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6602 compilation unit. */
6604 static int
6605 is_symbol_die (dw_die_ref c)
6607 return (is_type_die (c)
6608 || is_declaration_die (c)
6609 || c->die_tag == DW_TAG_namespace
6610 || c->die_tag == DW_TAG_module);
6613 /* Returns true iff C is a compile-unit DIE. */
6615 static inline bool
6616 is_cu_die (dw_die_ref c)
6618 return c && c->die_tag == DW_TAG_compile_unit;
6621 /* Returns true iff C is a unit DIE of some sort. */
6623 static inline bool
6624 is_unit_die (dw_die_ref c)
6626 return c && (c->die_tag == DW_TAG_compile_unit
6627 || c->die_tag == DW_TAG_partial_unit
6628 || c->die_tag == DW_TAG_type_unit);
6631 /* Returns true iff C is a namespace DIE. */
6633 static inline bool
6634 is_namespace_die (dw_die_ref c)
6636 return c && c->die_tag == DW_TAG_namespace;
6639 /* Returns true iff C is a class or structure DIE. */
6641 static inline bool
6642 is_class_die (dw_die_ref c)
6644 return c && (c->die_tag == DW_TAG_class_type
6645 || c->die_tag == DW_TAG_structure_type);
6648 /* Return non-zero if this DIE is a template parameter. */
6650 static inline bool
6651 is_template_parameter (dw_die_ref die)
6653 switch (die->die_tag)
6655 case DW_TAG_template_type_param:
6656 case DW_TAG_template_value_param:
6657 case DW_TAG_GNU_template_template_param:
6658 case DW_TAG_GNU_template_parameter_pack:
6659 return true;
6660 default:
6661 return false;
6665 /* Return non-zero if this DIE represents a template instantiation. */
6667 static inline bool
6668 is_template_instantiation (dw_die_ref die)
6670 dw_die_ref c;
6672 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6673 return false;
6674 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6675 return false;
6678 static char *
6679 gen_internal_sym (const char *prefix)
6681 char buf[256];
6683 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6684 return xstrdup (buf);
6687 /* Assign symbols to all worthy DIEs under DIE. */
6689 static void
6690 assign_symbol_names (dw_die_ref die)
6692 dw_die_ref c;
6694 if (is_symbol_die (die) && !die->comdat_type_p)
6696 if (comdat_symbol_id)
6698 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6700 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6701 comdat_symbol_id, comdat_symbol_number++);
6702 die->die_id.die_symbol = xstrdup (p);
6704 else
6705 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6708 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6711 struct cu_hash_table_entry
6713 dw_die_ref cu;
6714 unsigned min_comdat_num, max_comdat_num;
6715 struct cu_hash_table_entry *next;
6718 /* Helpers to manipulate hash table of CUs. */
6720 struct cu_hash_table_entry_hasher
6722 typedef cu_hash_table_entry value_type;
6723 typedef die_struct compare_type;
6724 static inline hashval_t hash (const value_type *);
6725 static inline bool equal (const value_type *, const compare_type *);
6726 static inline void remove (value_type *);
6729 inline hashval_t
6730 cu_hash_table_entry_hasher::hash (const value_type *entry)
6732 return htab_hash_string (entry->cu->die_id.die_symbol);
6735 inline bool
6736 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6737 const compare_type *entry2)
6739 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6742 inline void
6743 cu_hash_table_entry_hasher::remove (value_type *entry)
6745 struct cu_hash_table_entry *next;
6747 while (entry)
6749 next = entry->next;
6750 free (entry);
6751 entry = next;
6755 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6757 /* Check whether we have already seen this CU and set up SYM_NUM
6758 accordingly. */
6759 static int
6760 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6762 struct cu_hash_table_entry dummy;
6763 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6765 dummy.max_comdat_num = 0;
6767 slot = htable->find_slot_with_hash (cu,
6768 htab_hash_string (cu->die_id.die_symbol),
6769 INSERT);
6770 entry = *slot;
6772 for (; entry; last = entry, entry = entry->next)
6774 if (same_die_p_wrap (cu, entry->cu))
6775 break;
6778 if (entry)
6780 *sym_num = entry->min_comdat_num;
6781 return 1;
6784 entry = XCNEW (struct cu_hash_table_entry);
6785 entry->cu = cu;
6786 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6787 entry->next = *slot;
6788 *slot = entry;
6790 return 0;
6793 /* Record SYM_NUM to record of CU in HTABLE. */
6794 static void
6795 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6796 unsigned int sym_num)
6798 struct cu_hash_table_entry **slot, *entry;
6800 slot = htable->find_slot_with_hash (cu,
6801 htab_hash_string (cu->die_id.die_symbol),
6802 NO_INSERT);
6803 entry = *slot;
6805 entry->max_comdat_num = sym_num;
6808 /* Traverse the DIE (which is always comp_unit_die), and set up
6809 additional compilation units for each of the include files we see
6810 bracketed by BINCL/EINCL. */
6812 static void
6813 break_out_includes (dw_die_ref die)
6815 dw_die_ref c;
6816 dw_die_ref unit = NULL;
6817 limbo_die_node *node, **pnode;
6819 c = die->die_child;
6820 if (c) do {
6821 dw_die_ref prev = c;
6822 c = c->die_sib;
6823 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6824 || (unit && is_comdat_die (c)))
6826 dw_die_ref next = c->die_sib;
6828 /* This DIE is for a secondary CU; remove it from the main one. */
6829 remove_child_with_prev (c, prev);
6831 if (c->die_tag == DW_TAG_GNU_BINCL)
6832 unit = push_new_compile_unit (unit, c);
6833 else if (c->die_tag == DW_TAG_GNU_EINCL)
6834 unit = pop_compile_unit (unit);
6835 else
6836 add_child_die (unit, c);
6837 c = next;
6838 if (c == die->die_child)
6839 break;
6841 } while (c != die->die_child);
6843 #if 0
6844 /* We can only use this in debugging, since the frontend doesn't check
6845 to make sure that we leave every include file we enter. */
6846 gcc_assert (!unit);
6847 #endif
6849 assign_symbol_names (die);
6850 cu_hash_type cu_hash_table (10);
6851 for (node = limbo_die_list, pnode = &limbo_die_list;
6852 node;
6853 node = node->next)
6855 int is_dupl;
6857 compute_section_prefix (node->die);
6858 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6859 &comdat_symbol_number);
6860 assign_symbol_names (node->die);
6861 if (is_dupl)
6862 *pnode = node->next;
6863 else
6865 pnode = &node->next;
6866 record_comdat_symbol_number (node->die, &cu_hash_table,
6867 comdat_symbol_number);
6872 /* Return non-zero if this DIE is a declaration. */
6874 static int
6875 is_declaration_die (dw_die_ref die)
6877 dw_attr_ref a;
6878 unsigned ix;
6880 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6881 if (a->dw_attr == DW_AT_declaration)
6882 return 1;
6884 return 0;
6887 /* Return non-zero if this DIE is nested inside a subprogram. */
6889 static int
6890 is_nested_in_subprogram (dw_die_ref die)
6892 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6894 if (decl == NULL)
6895 decl = die;
6896 return local_scope_p (decl);
6899 /* Return non-zero if this DIE contains a defining declaration of a
6900 subprogram. */
6902 static int
6903 contains_subprogram_definition (dw_die_ref die)
6905 dw_die_ref c;
6907 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6908 return 1;
6909 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6910 return 0;
6913 /* Return non-zero if this is a type DIE that should be moved to a
6914 COMDAT .debug_types section. */
6916 static int
6917 should_move_die_to_comdat (dw_die_ref die)
6919 switch (die->die_tag)
6921 case DW_TAG_class_type:
6922 case DW_TAG_structure_type:
6923 case DW_TAG_enumeration_type:
6924 case DW_TAG_union_type:
6925 /* Don't move declarations, inlined instances, types nested in a
6926 subprogram, or types that contain subprogram definitions. */
6927 if (is_declaration_die (die)
6928 || get_AT (die, DW_AT_abstract_origin)
6929 || is_nested_in_subprogram (die)
6930 || contains_subprogram_definition (die))
6931 return 0;
6932 return 1;
6933 case DW_TAG_array_type:
6934 case DW_TAG_interface_type:
6935 case DW_TAG_pointer_type:
6936 case DW_TAG_reference_type:
6937 case DW_TAG_rvalue_reference_type:
6938 case DW_TAG_string_type:
6939 case DW_TAG_subroutine_type:
6940 case DW_TAG_ptr_to_member_type:
6941 case DW_TAG_set_type:
6942 case DW_TAG_subrange_type:
6943 case DW_TAG_base_type:
6944 case DW_TAG_const_type:
6945 case DW_TAG_file_type:
6946 case DW_TAG_packed_type:
6947 case DW_TAG_volatile_type:
6948 case DW_TAG_typedef:
6949 default:
6950 return 0;
6954 /* Make a clone of DIE. */
6956 static dw_die_ref
6957 clone_die (dw_die_ref die)
6959 dw_die_ref clone;
6960 dw_attr_ref a;
6961 unsigned ix;
6963 clone = ggc_cleared_alloc<die_node> ();
6964 clone->die_tag = die->die_tag;
6966 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6967 add_dwarf_attr (clone, a);
6969 return clone;
6972 /* Make a clone of the tree rooted at DIE. */
6974 static dw_die_ref
6975 clone_tree (dw_die_ref die)
6977 dw_die_ref c;
6978 dw_die_ref clone = clone_die (die);
6980 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6982 return clone;
6985 /* Make a clone of DIE as a declaration. */
6987 static dw_die_ref
6988 clone_as_declaration (dw_die_ref die)
6990 dw_die_ref clone;
6991 dw_die_ref decl;
6992 dw_attr_ref a;
6993 unsigned ix;
6995 /* If the DIE is already a declaration, just clone it. */
6996 if (is_declaration_die (die))
6997 return clone_die (die);
6999 /* If the DIE is a specification, just clone its declaration DIE. */
7000 decl = get_AT_ref (die, DW_AT_specification);
7001 if (decl != NULL)
7003 clone = clone_die (decl);
7004 if (die->comdat_type_p)
7005 add_AT_die_ref (clone, DW_AT_signature, die);
7006 return clone;
7009 clone = ggc_cleared_alloc<die_node> ();
7010 clone->die_tag = die->die_tag;
7012 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7014 /* We don't want to copy over all attributes.
7015 For example we don't want DW_AT_byte_size because otherwise we will no
7016 longer have a declaration and GDB will treat it as a definition. */
7018 switch (a->dw_attr)
7020 case DW_AT_abstract_origin:
7021 case DW_AT_artificial:
7022 case DW_AT_containing_type:
7023 case DW_AT_external:
7024 case DW_AT_name:
7025 case DW_AT_type:
7026 case DW_AT_virtuality:
7027 case DW_AT_linkage_name:
7028 case DW_AT_MIPS_linkage_name:
7029 add_dwarf_attr (clone, a);
7030 break;
7031 case DW_AT_byte_size:
7032 default:
7033 break;
7037 if (die->comdat_type_p)
7038 add_AT_die_ref (clone, DW_AT_signature, die);
7040 add_AT_flag (clone, DW_AT_declaration, 1);
7041 return clone;
7045 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7047 struct decl_table_entry
7049 dw_die_ref orig;
7050 dw_die_ref copy;
7053 /* Helpers to manipulate hash table of copied declarations. */
7055 /* Hashtable helpers. */
7057 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7059 typedef decl_table_entry value_type;
7060 typedef die_struct compare_type;
7061 static inline hashval_t hash (const value_type *);
7062 static inline bool equal (const value_type *, const compare_type *);
7065 inline hashval_t
7066 decl_table_entry_hasher::hash (const value_type *entry)
7068 return htab_hash_pointer (entry->orig);
7071 inline bool
7072 decl_table_entry_hasher::equal (const value_type *entry1,
7073 const compare_type *entry2)
7075 return entry1->orig == entry2;
7078 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7080 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7081 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7082 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7083 to check if the ancestor has already been copied into UNIT. */
7085 static dw_die_ref
7086 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7087 decl_hash_type *decl_table)
7089 dw_die_ref parent = die->die_parent;
7090 dw_die_ref new_parent = unit;
7091 dw_die_ref copy;
7092 decl_table_entry **slot = NULL;
7093 struct decl_table_entry *entry = NULL;
7095 if (decl_table)
7097 /* Check if the entry has already been copied to UNIT. */
7098 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7099 INSERT);
7100 if (*slot != HTAB_EMPTY_ENTRY)
7102 entry = *slot;
7103 return entry->copy;
7106 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7107 entry = XCNEW (struct decl_table_entry);
7108 entry->orig = die;
7109 entry->copy = NULL;
7110 *slot = entry;
7113 if (parent != NULL)
7115 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7116 if (spec != NULL)
7117 parent = spec;
7118 if (!is_unit_die (parent))
7119 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7122 copy = clone_as_declaration (die);
7123 add_child_die (new_parent, copy);
7125 if (decl_table)
7127 /* Record the pointer to the copy. */
7128 entry->copy = copy;
7131 return copy;
7133 /* Copy the declaration context to the new type unit DIE. This includes
7134 any surrounding namespace or type declarations. If the DIE has an
7135 AT_specification attribute, it also includes attributes and children
7136 attached to the specification, and returns a pointer to the original
7137 parent of the declaration DIE. Returns NULL otherwise. */
7139 static dw_die_ref
7140 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7142 dw_die_ref decl;
7143 dw_die_ref new_decl;
7144 dw_die_ref orig_parent = NULL;
7146 decl = get_AT_ref (die, DW_AT_specification);
7147 if (decl == NULL)
7148 decl = die;
7149 else
7151 unsigned ix;
7152 dw_die_ref c;
7153 dw_attr_ref a;
7155 /* The original DIE will be changed to a declaration, and must
7156 be moved to be a child of the original declaration DIE. */
7157 orig_parent = decl->die_parent;
7159 /* Copy the type node pointer from the new DIE to the original
7160 declaration DIE so we can forward references later. */
7161 decl->comdat_type_p = true;
7162 decl->die_id.die_type_node = die->die_id.die_type_node;
7164 remove_AT (die, DW_AT_specification);
7166 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7168 if (a->dw_attr != DW_AT_name
7169 && a->dw_attr != DW_AT_declaration
7170 && a->dw_attr != DW_AT_external)
7171 add_dwarf_attr (die, a);
7174 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7177 if (decl->die_parent != NULL
7178 && !is_unit_die (decl->die_parent))
7180 new_decl = copy_ancestor_tree (unit, decl, NULL);
7181 if (new_decl != NULL)
7183 remove_AT (new_decl, DW_AT_signature);
7184 add_AT_specification (die, new_decl);
7188 return orig_parent;
7191 /* Generate the skeleton ancestor tree for the given NODE, then clone
7192 the DIE and add the clone into the tree. */
7194 static void
7195 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7197 if (node->new_die != NULL)
7198 return;
7200 node->new_die = clone_as_declaration (node->old_die);
7202 if (node->parent != NULL)
7204 generate_skeleton_ancestor_tree (node->parent);
7205 add_child_die (node->parent->new_die, node->new_die);
7209 /* Generate a skeleton tree of DIEs containing any declarations that are
7210 found in the original tree. We traverse the tree looking for declaration
7211 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7213 static void
7214 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7216 skeleton_chain_node node;
7217 dw_die_ref c;
7218 dw_die_ref first;
7219 dw_die_ref prev = NULL;
7220 dw_die_ref next = NULL;
7222 node.parent = parent;
7224 first = c = parent->old_die->die_child;
7225 if (c)
7226 next = c->die_sib;
7227 if (c) do {
7228 if (prev == NULL || prev->die_sib == c)
7229 prev = c;
7230 c = next;
7231 next = (c == first ? NULL : c->die_sib);
7232 node.old_die = c;
7233 node.new_die = NULL;
7234 if (is_declaration_die (c))
7236 if (is_template_instantiation (c))
7238 /* Instantiated templates do not need to be cloned into the
7239 type unit. Just move the DIE and its children back to
7240 the skeleton tree (in the main CU). */
7241 remove_child_with_prev (c, prev);
7242 add_child_die (parent->new_die, c);
7243 c = prev;
7245 else
7247 /* Clone the existing DIE, move the original to the skeleton
7248 tree (which is in the main CU), and put the clone, with
7249 all the original's children, where the original came from
7250 (which is about to be moved to the type unit). */
7251 dw_die_ref clone = clone_die (c);
7252 move_all_children (c, clone);
7254 /* If the original has a DW_AT_object_pointer attribute,
7255 it would now point to a child DIE just moved to the
7256 cloned tree, so we need to remove that attribute from
7257 the original. */
7258 remove_AT (c, DW_AT_object_pointer);
7260 replace_child (c, clone, prev);
7261 generate_skeleton_ancestor_tree (parent);
7262 add_child_die (parent->new_die, c);
7263 node.new_die = c;
7264 c = clone;
7267 generate_skeleton_bottom_up (&node);
7268 } while (next != NULL);
7271 /* Wrapper function for generate_skeleton_bottom_up. */
7273 static dw_die_ref
7274 generate_skeleton (dw_die_ref die)
7276 skeleton_chain_node node;
7278 node.old_die = die;
7279 node.new_die = NULL;
7280 node.parent = NULL;
7282 /* If this type definition is nested inside another type,
7283 and is not an instantiation of a template, always leave
7284 at least a declaration in its place. */
7285 if (die->die_parent != NULL
7286 && is_type_die (die->die_parent)
7287 && !is_template_instantiation (die))
7288 node.new_die = clone_as_declaration (die);
7290 generate_skeleton_bottom_up (&node);
7291 return node.new_die;
7294 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7295 declaration. The original DIE is moved to a new compile unit so that
7296 existing references to it follow it to the new location. If any of the
7297 original DIE's descendants is a declaration, we need to replace the
7298 original DIE with a skeleton tree and move the declarations back into the
7299 skeleton tree. */
7301 static dw_die_ref
7302 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7303 dw_die_ref prev)
7305 dw_die_ref skeleton, orig_parent;
7307 /* Copy the declaration context to the type unit DIE. If the returned
7308 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7309 that DIE. */
7310 orig_parent = copy_declaration_context (unit, child);
7312 skeleton = generate_skeleton (child);
7313 if (skeleton == NULL)
7314 remove_child_with_prev (child, prev);
7315 else
7317 skeleton->comdat_type_p = true;
7318 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7320 /* If the original DIE was a specification, we need to put
7321 the skeleton under the parent DIE of the declaration.
7322 This leaves the original declaration in the tree, but
7323 it will be pruned later since there are no longer any
7324 references to it. */
7325 if (orig_parent != NULL)
7327 remove_child_with_prev (child, prev);
7328 add_child_die (orig_parent, skeleton);
7330 else
7331 replace_child (child, skeleton, prev);
7334 return skeleton;
7337 /* Traverse the DIE and set up additional .debug_types sections for each
7338 type worthy of being placed in a COMDAT section. */
7340 static void
7341 break_out_comdat_types (dw_die_ref die)
7343 dw_die_ref c;
7344 dw_die_ref first;
7345 dw_die_ref prev = NULL;
7346 dw_die_ref next = NULL;
7347 dw_die_ref unit = NULL;
7349 first = c = die->die_child;
7350 if (c)
7351 next = c->die_sib;
7352 if (c) do {
7353 if (prev == NULL || prev->die_sib == c)
7354 prev = c;
7355 c = next;
7356 next = (c == first ? NULL : c->die_sib);
7357 if (should_move_die_to_comdat (c))
7359 dw_die_ref replacement;
7360 comdat_type_node_ref type_node;
7362 /* Break out nested types into their own type units. */
7363 break_out_comdat_types (c);
7365 /* Create a new type unit DIE as the root for the new tree, and
7366 add it to the list of comdat types. */
7367 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7368 add_AT_unsigned (unit, DW_AT_language,
7369 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7370 type_node = ggc_cleared_alloc<comdat_type_node> ();
7371 type_node->root_die = unit;
7372 type_node->next = comdat_type_list;
7373 comdat_type_list = type_node;
7375 /* Generate the type signature. */
7376 generate_type_signature (c, type_node);
7378 /* Copy the declaration context, attributes, and children of the
7379 declaration into the new type unit DIE, then remove this DIE
7380 from the main CU (or replace it with a skeleton if necessary). */
7381 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7382 type_node->skeleton_die = replacement;
7384 /* Add the DIE to the new compunit. */
7385 add_child_die (unit, c);
7387 if (replacement != NULL)
7388 c = replacement;
7390 else if (c->die_tag == DW_TAG_namespace
7391 || c->die_tag == DW_TAG_class_type
7392 || c->die_tag == DW_TAG_structure_type
7393 || c->die_tag == DW_TAG_union_type)
7395 /* Look for nested types that can be broken out. */
7396 break_out_comdat_types (c);
7398 } while (next != NULL);
7401 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7402 Enter all the cloned children into the hash table decl_table. */
7404 static dw_die_ref
7405 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7407 dw_die_ref c;
7408 dw_die_ref clone;
7409 struct decl_table_entry *entry;
7410 decl_table_entry **slot;
7412 if (die->die_tag == DW_TAG_subprogram)
7413 clone = clone_as_declaration (die);
7414 else
7415 clone = clone_die (die);
7417 slot = decl_table->find_slot_with_hash (die,
7418 htab_hash_pointer (die), INSERT);
7420 /* Assert that DIE isn't in the hash table yet. If it would be there
7421 before, the ancestors would be necessarily there as well, therefore
7422 clone_tree_partial wouldn't be called. */
7423 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7425 entry = XCNEW (struct decl_table_entry);
7426 entry->orig = die;
7427 entry->copy = clone;
7428 *slot = entry;
7430 if (die->die_tag != DW_TAG_subprogram)
7431 FOR_EACH_CHILD (die, c,
7432 add_child_die (clone, clone_tree_partial (c, decl_table)));
7434 return clone;
7437 /* Walk the DIE and its children, looking for references to incomplete
7438 or trivial types that are unmarked (i.e., that are not in the current
7439 type_unit). */
7441 static void
7442 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7444 dw_die_ref c;
7445 dw_attr_ref a;
7446 unsigned ix;
7448 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7450 if (AT_class (a) == dw_val_class_die_ref)
7452 dw_die_ref targ = AT_ref (a);
7453 decl_table_entry **slot;
7454 struct decl_table_entry *entry;
7456 if (targ->die_mark != 0 || targ->comdat_type_p)
7457 continue;
7459 slot = decl_table->find_slot_with_hash (targ,
7460 htab_hash_pointer (targ),
7461 INSERT);
7463 if (*slot != HTAB_EMPTY_ENTRY)
7465 /* TARG has already been copied, so we just need to
7466 modify the reference to point to the copy. */
7467 entry = *slot;
7468 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7470 else
7472 dw_die_ref parent = unit;
7473 dw_die_ref copy = clone_die (targ);
7475 /* Record in DECL_TABLE that TARG has been copied.
7476 Need to do this now, before the recursive call,
7477 because DECL_TABLE may be expanded and SLOT
7478 would no longer be a valid pointer. */
7479 entry = XCNEW (struct decl_table_entry);
7480 entry->orig = targ;
7481 entry->copy = copy;
7482 *slot = entry;
7484 /* If TARG is not a declaration DIE, we need to copy its
7485 children. */
7486 if (!is_declaration_die (targ))
7488 FOR_EACH_CHILD (
7489 targ, c,
7490 add_child_die (copy,
7491 clone_tree_partial (c, decl_table)));
7494 /* Make sure the cloned tree is marked as part of the
7495 type unit. */
7496 mark_dies (copy);
7498 /* If TARG has surrounding context, copy its ancestor tree
7499 into the new type unit. */
7500 if (targ->die_parent != NULL
7501 && !is_unit_die (targ->die_parent))
7502 parent = copy_ancestor_tree (unit, targ->die_parent,
7503 decl_table);
7505 add_child_die (parent, copy);
7506 a->dw_attr_val.v.val_die_ref.die = copy;
7508 /* Make sure the newly-copied DIE is walked. If it was
7509 installed in a previously-added context, it won't
7510 get visited otherwise. */
7511 if (parent != unit)
7513 /* Find the highest point of the newly-added tree,
7514 mark each node along the way, and walk from there. */
7515 parent->die_mark = 1;
7516 while (parent->die_parent
7517 && parent->die_parent->die_mark == 0)
7519 parent = parent->die_parent;
7520 parent->die_mark = 1;
7522 copy_decls_walk (unit, parent, decl_table);
7528 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7531 /* Copy declarations for "unworthy" types into the new comdat section.
7532 Incomplete types, modified types, and certain other types aren't broken
7533 out into comdat sections of their own, so they don't have a signature,
7534 and we need to copy the declaration into the same section so that we
7535 don't have an external reference. */
7537 static void
7538 copy_decls_for_unworthy_types (dw_die_ref unit)
7540 mark_dies (unit);
7541 decl_hash_type decl_table (10);
7542 copy_decls_walk (unit, unit, &decl_table);
7543 unmark_dies (unit);
7546 /* Traverse the DIE and add a sibling attribute if it may have the
7547 effect of speeding up access to siblings. To save some space,
7548 avoid generating sibling attributes for DIE's without children. */
7550 static void
7551 add_sibling_attributes (dw_die_ref die)
7553 dw_die_ref c;
7555 if (! die->die_child)
7556 return;
7558 if (die->die_parent && die != die->die_parent->die_child)
7559 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7561 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7564 /* Output all location lists for the DIE and its children. */
7566 static void
7567 output_location_lists (dw_die_ref die)
7569 dw_die_ref c;
7570 dw_attr_ref a;
7571 unsigned ix;
7573 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7574 if (AT_class (a) == dw_val_class_loc_list)
7575 output_loc_list (AT_loc_list (a));
7577 FOR_EACH_CHILD (die, c, output_location_lists (c));
7580 /* We want to limit the number of external references, because they are
7581 larger than local references: a relocation takes multiple words, and
7582 even a sig8 reference is always eight bytes, whereas a local reference
7583 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7584 So if we encounter multiple external references to the same type DIE, we
7585 make a local typedef stub for it and redirect all references there.
7587 This is the element of the hash table for keeping track of these
7588 references. */
7590 struct external_ref
7592 dw_die_ref type;
7593 dw_die_ref stub;
7594 unsigned n_refs;
7597 /* Hashtable helpers. */
7599 struct external_ref_hasher : typed_free_remove <external_ref>
7601 typedef external_ref value_type;
7602 typedef external_ref compare_type;
7603 static inline hashval_t hash (const value_type *);
7604 static inline bool equal (const value_type *, const compare_type *);
7607 inline hashval_t
7608 external_ref_hasher::hash (const value_type *r)
7610 dw_die_ref die = r->type;
7611 hashval_t h = 0;
7613 /* We can't use the address of the DIE for hashing, because
7614 that will make the order of the stub DIEs non-deterministic. */
7615 if (! die->comdat_type_p)
7616 /* We have a symbol; use it to compute a hash. */
7617 h = htab_hash_string (die->die_id.die_symbol);
7618 else
7620 /* We have a type signature; use a subset of the bits as the hash.
7621 The 8-byte signature is at least as large as hashval_t. */
7622 comdat_type_node_ref type_node = die->die_id.die_type_node;
7623 memcpy (&h, type_node->signature, sizeof (h));
7625 return h;
7628 inline bool
7629 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7631 return r1->type == r2->type;
7634 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7636 /* Return a pointer to the external_ref for references to DIE. */
7638 static struct external_ref *
7639 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7641 struct external_ref ref, *ref_p;
7642 external_ref **slot;
7644 ref.type = die;
7645 slot = map->find_slot (&ref, INSERT);
7646 if (*slot != HTAB_EMPTY_ENTRY)
7647 return *slot;
7649 ref_p = XCNEW (struct external_ref);
7650 ref_p->type = die;
7651 *slot = ref_p;
7652 return ref_p;
7655 /* Subroutine of optimize_external_refs, below.
7657 If we see a type skeleton, record it as our stub. If we see external
7658 references, remember how many we've seen. */
7660 static void
7661 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7663 dw_die_ref c;
7664 dw_attr_ref a;
7665 unsigned ix;
7666 struct external_ref *ref_p;
7668 if (is_type_die (die)
7669 && (c = get_AT_ref (die, DW_AT_signature)))
7671 /* This is a local skeleton; use it for local references. */
7672 ref_p = lookup_external_ref (map, c);
7673 ref_p->stub = die;
7676 /* Scan the DIE references, and remember any that refer to DIEs from
7677 other CUs (i.e. those which are not marked). */
7678 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7679 if (AT_class (a) == dw_val_class_die_ref
7680 && (c = AT_ref (a))->die_mark == 0
7681 && is_type_die (c))
7683 ref_p = lookup_external_ref (map, c);
7684 ref_p->n_refs++;
7687 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7690 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7691 points to an external_ref, DATA is the CU we're processing. If we don't
7692 already have a local stub, and we have multiple refs, build a stub. */
7695 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7697 struct external_ref *ref_p = *slot;
7699 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7701 /* We have multiple references to this type, so build a small stub.
7702 Both of these forms are a bit dodgy from the perspective of the
7703 DWARF standard, since technically they should have names. */
7704 dw_die_ref cu = data;
7705 dw_die_ref type = ref_p->type;
7706 dw_die_ref stub = NULL;
7708 if (type->comdat_type_p)
7710 /* If we refer to this type via sig8, use AT_signature. */
7711 stub = new_die (type->die_tag, cu, NULL_TREE);
7712 add_AT_die_ref (stub, DW_AT_signature, type);
7714 else
7716 /* Otherwise, use a typedef with no name. */
7717 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7718 add_AT_die_ref (stub, DW_AT_type, type);
7721 stub->die_mark++;
7722 ref_p->stub = stub;
7724 return 1;
7727 /* DIE is a unit; look through all the DIE references to see if there are
7728 any external references to types, and if so, create local stubs for
7729 them which will be applied in build_abbrev_table. This is useful because
7730 references to local DIEs are smaller. */
7732 static external_ref_hash_type *
7733 optimize_external_refs (dw_die_ref die)
7735 external_ref_hash_type *map = new external_ref_hash_type (10);
7736 optimize_external_refs_1 (die, map);
7737 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7738 return map;
7741 /* The format of each DIE (and its attribute value pairs) is encoded in an
7742 abbreviation table. This routine builds the abbreviation table and assigns
7743 a unique abbreviation id for each abbreviation entry. The children of each
7744 die are visited recursively. */
7746 static void
7747 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7749 unsigned long abbrev_id;
7750 unsigned int n_alloc;
7751 dw_die_ref c;
7752 dw_attr_ref a;
7753 unsigned ix;
7755 /* Scan the DIE references, and replace any that refer to
7756 DIEs from other CUs (i.e. those which are not marked) with
7757 the local stubs we built in optimize_external_refs. */
7758 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7759 if (AT_class (a) == dw_val_class_die_ref
7760 && (c = AT_ref (a))->die_mark == 0)
7762 struct external_ref *ref_p;
7763 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7765 ref_p = lookup_external_ref (extern_map, c);
7766 if (ref_p->stub && ref_p->stub != die)
7767 change_AT_die_ref (a, ref_p->stub);
7768 else
7769 /* We aren't changing this reference, so mark it external. */
7770 set_AT_ref_external (a, 1);
7773 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7775 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7776 dw_attr_ref die_a, abbrev_a;
7777 unsigned ix;
7778 bool ok = true;
7780 if (abbrev->die_tag != die->die_tag)
7781 continue;
7782 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7783 continue;
7785 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7786 continue;
7788 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7790 abbrev_a = &(*abbrev->die_attr)[ix];
7791 if ((abbrev_a->dw_attr != die_a->dw_attr)
7792 || (value_format (abbrev_a) != value_format (die_a)))
7794 ok = false;
7795 break;
7798 if (ok)
7799 break;
7802 if (abbrev_id >= abbrev_die_table_in_use)
7804 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7806 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7807 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7808 n_alloc);
7810 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7811 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7812 abbrev_die_table_allocated = n_alloc;
7815 ++abbrev_die_table_in_use;
7816 abbrev_die_table[abbrev_id] = die;
7819 die->die_abbrev = abbrev_id;
7820 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7823 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7825 static int
7826 constant_size (unsigned HOST_WIDE_INT value)
7828 int log;
7830 if (value == 0)
7831 log = 0;
7832 else
7833 log = floor_log2 (value);
7835 log = log / 8;
7836 log = 1 << (floor_log2 (log) + 1);
7838 return log;
7841 /* Return the size of a DIE as it is represented in the
7842 .debug_info section. */
7844 static unsigned long
7845 size_of_die (dw_die_ref die)
7847 unsigned long size = 0;
7848 dw_attr_ref a;
7849 unsigned ix;
7850 enum dwarf_form form;
7852 size += size_of_uleb128 (die->die_abbrev);
7853 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7855 switch (AT_class (a))
7857 case dw_val_class_addr:
7858 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7860 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7861 size += size_of_uleb128 (AT_index (a));
7863 else
7864 size += DWARF2_ADDR_SIZE;
7865 break;
7866 case dw_val_class_offset:
7867 size += DWARF_OFFSET_SIZE;
7868 break;
7869 case dw_val_class_loc:
7871 unsigned long lsize = size_of_locs (AT_loc (a));
7873 /* Block length. */
7874 if (dwarf_version >= 4)
7875 size += size_of_uleb128 (lsize);
7876 else
7877 size += constant_size (lsize);
7878 size += lsize;
7880 break;
7881 case dw_val_class_loc_list:
7882 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7884 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7885 size += size_of_uleb128 (AT_index (a));
7887 else
7888 size += DWARF_OFFSET_SIZE;
7889 break;
7890 case dw_val_class_range_list:
7891 size += DWARF_OFFSET_SIZE;
7892 break;
7893 case dw_val_class_const:
7894 size += size_of_sleb128 (AT_int (a));
7895 break;
7896 case dw_val_class_unsigned_const:
7898 int csize = constant_size (AT_unsigned (a));
7899 if (dwarf_version == 3
7900 && a->dw_attr == DW_AT_data_member_location
7901 && csize >= 4)
7902 size += size_of_uleb128 (AT_unsigned (a));
7903 else
7904 size += csize;
7906 break;
7907 case dw_val_class_const_double:
7908 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7909 if (HOST_BITS_PER_WIDE_INT >= 64)
7910 size++; /* block */
7911 break;
7912 case dw_val_class_wide_int:
7913 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7914 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7915 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7916 > 64)
7917 size++; /* block */
7918 break;
7919 case dw_val_class_vec:
7920 size += constant_size (a->dw_attr_val.v.val_vec.length
7921 * a->dw_attr_val.v.val_vec.elt_size)
7922 + a->dw_attr_val.v.val_vec.length
7923 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7924 break;
7925 case dw_val_class_flag:
7926 if (dwarf_version >= 4)
7927 /* Currently all add_AT_flag calls pass in 1 as last argument,
7928 so DW_FORM_flag_present can be used. If that ever changes,
7929 we'll need to use DW_FORM_flag and have some optimization
7930 in build_abbrev_table that will change those to
7931 DW_FORM_flag_present if it is set to 1 in all DIEs using
7932 the same abbrev entry. */
7933 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7934 else
7935 size += 1;
7936 break;
7937 case dw_val_class_die_ref:
7938 if (AT_ref_external (a))
7940 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7941 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7942 is sized by target address length, whereas in DWARF3
7943 it's always sized as an offset. */
7944 if (use_debug_types)
7945 size += DWARF_TYPE_SIGNATURE_SIZE;
7946 else if (dwarf_version == 2)
7947 size += DWARF2_ADDR_SIZE;
7948 else
7949 size += DWARF_OFFSET_SIZE;
7951 else
7952 size += DWARF_OFFSET_SIZE;
7953 break;
7954 case dw_val_class_fde_ref:
7955 size += DWARF_OFFSET_SIZE;
7956 break;
7957 case dw_val_class_lbl_id:
7958 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7960 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7961 size += size_of_uleb128 (AT_index (a));
7963 else
7964 size += DWARF2_ADDR_SIZE;
7965 break;
7966 case dw_val_class_lineptr:
7967 case dw_val_class_macptr:
7968 size += DWARF_OFFSET_SIZE;
7969 break;
7970 case dw_val_class_str:
7971 form = AT_string_form (a);
7972 if (form == DW_FORM_strp)
7973 size += DWARF_OFFSET_SIZE;
7974 else if (form == DW_FORM_GNU_str_index)
7975 size += size_of_uleb128 (AT_index (a));
7976 else
7977 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7978 break;
7979 case dw_val_class_file:
7980 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7981 break;
7982 case dw_val_class_data8:
7983 size += 8;
7984 break;
7985 case dw_val_class_vms_delta:
7986 size += DWARF_OFFSET_SIZE;
7987 break;
7988 case dw_val_class_high_pc:
7989 size += DWARF2_ADDR_SIZE;
7990 break;
7991 default:
7992 gcc_unreachable ();
7996 return size;
7999 /* Size the debugging information associated with a given DIE. Visits the
8000 DIE's children recursively. Updates the global variable next_die_offset, on
8001 each time through. Uses the current value of next_die_offset to update the
8002 die_offset field in each DIE. */
8004 static void
8005 calc_die_sizes (dw_die_ref die)
8007 dw_die_ref c;
8009 gcc_assert (die->die_offset == 0
8010 || (unsigned long int) die->die_offset == next_die_offset);
8011 die->die_offset = next_die_offset;
8012 next_die_offset += size_of_die (die);
8014 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8016 if (die->die_child != NULL)
8017 /* Count the null byte used to terminate sibling lists. */
8018 next_die_offset += 1;
8021 /* Size just the base type children at the start of the CU.
8022 This is needed because build_abbrev needs to size locs
8023 and sizing of type based stack ops needs to know die_offset
8024 values for the base types. */
8026 static void
8027 calc_base_type_die_sizes (void)
8029 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8030 unsigned int i;
8031 dw_die_ref base_type;
8032 #if ENABLE_ASSERT_CHECKING
8033 dw_die_ref prev = comp_unit_die ()->die_child;
8034 #endif
8036 die_offset += size_of_die (comp_unit_die ());
8037 for (i = 0; base_types.iterate (i, &base_type); i++)
8039 #if ENABLE_ASSERT_CHECKING
8040 gcc_assert (base_type->die_offset == 0
8041 && prev->die_sib == base_type
8042 && base_type->die_child == NULL
8043 && base_type->die_abbrev);
8044 prev = base_type;
8045 #endif
8046 base_type->die_offset = die_offset;
8047 die_offset += size_of_die (base_type);
8051 /* Set the marks for a die and its children. We do this so
8052 that we know whether or not a reference needs to use FORM_ref_addr; only
8053 DIEs in the same CU will be marked. We used to clear out the offset
8054 and use that as the flag, but ran into ordering problems. */
8056 static void
8057 mark_dies (dw_die_ref die)
8059 dw_die_ref c;
8061 gcc_assert (!die->die_mark);
8063 die->die_mark = 1;
8064 FOR_EACH_CHILD (die, c, mark_dies (c));
8067 /* Clear the marks for a die and its children. */
8069 static void
8070 unmark_dies (dw_die_ref die)
8072 dw_die_ref c;
8074 if (! use_debug_types)
8075 gcc_assert (die->die_mark);
8077 die->die_mark = 0;
8078 FOR_EACH_CHILD (die, c, unmark_dies (c));
8081 /* Clear the marks for a die, its children and referred dies. */
8083 static void
8084 unmark_all_dies (dw_die_ref die)
8086 dw_die_ref c;
8087 dw_attr_ref a;
8088 unsigned ix;
8090 if (!die->die_mark)
8091 return;
8092 die->die_mark = 0;
8094 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8096 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8097 if (AT_class (a) == dw_val_class_die_ref)
8098 unmark_all_dies (AT_ref (a));
8101 /* Calculate if the entry should appear in the final output file. It may be
8102 from a pruned a type. */
8104 static bool
8105 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8107 /* By limiting gnu pubnames to definitions only, gold can generate a
8108 gdb index without entries for declarations, which don't include
8109 enough information to be useful. */
8110 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8111 return false;
8113 if (table == pubname_table)
8115 /* Enumerator names are part of the pubname table, but the
8116 parent DW_TAG_enumeration_type die may have been pruned.
8117 Don't output them if that is the case. */
8118 if (p->die->die_tag == DW_TAG_enumerator &&
8119 (p->die->die_parent == NULL
8120 || !p->die->die_parent->die_perennial_p))
8121 return false;
8123 /* Everything else in the pubname table is included. */
8124 return true;
8127 /* The pubtypes table shouldn't include types that have been
8128 pruned. */
8129 return (p->die->die_offset != 0
8130 || !flag_eliminate_unused_debug_types);
8133 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8134 generated for the compilation unit. */
8136 static unsigned long
8137 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8139 unsigned long size;
8140 unsigned i;
8141 pubname_ref p;
8142 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8144 size = DWARF_PUBNAMES_HEADER_SIZE;
8145 FOR_EACH_VEC_ELT (*names, i, p)
8146 if (include_pubname_in_output (names, p))
8147 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8149 size += DWARF_OFFSET_SIZE;
8150 return size;
8153 /* Return the size of the information in the .debug_aranges section. */
8155 static unsigned long
8156 size_of_aranges (void)
8158 unsigned long size;
8160 size = DWARF_ARANGES_HEADER_SIZE;
8162 /* Count the address/length pair for this compilation unit. */
8163 if (text_section_used)
8164 size += 2 * DWARF2_ADDR_SIZE;
8165 if (cold_text_section_used)
8166 size += 2 * DWARF2_ADDR_SIZE;
8167 if (have_multiple_function_sections)
8169 unsigned fde_idx;
8170 dw_fde_ref fde;
8172 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8174 if (DECL_IGNORED_P (fde->decl))
8175 continue;
8176 if (!fde->in_std_section)
8177 size += 2 * DWARF2_ADDR_SIZE;
8178 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8179 size += 2 * DWARF2_ADDR_SIZE;
8183 /* Count the two zero words used to terminated the address range table. */
8184 size += 2 * DWARF2_ADDR_SIZE;
8185 return size;
8188 /* Select the encoding of an attribute value. */
8190 static enum dwarf_form
8191 value_format (dw_attr_ref a)
8193 switch (AT_class (a))
8195 case dw_val_class_addr:
8196 /* Only very few attributes allow DW_FORM_addr. */
8197 switch (a->dw_attr)
8199 case DW_AT_low_pc:
8200 case DW_AT_high_pc:
8201 case DW_AT_entry_pc:
8202 case DW_AT_trampoline:
8203 return (AT_index (a) == NOT_INDEXED
8204 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8205 default:
8206 break;
8208 switch (DWARF2_ADDR_SIZE)
8210 case 1:
8211 return DW_FORM_data1;
8212 case 2:
8213 return DW_FORM_data2;
8214 case 4:
8215 return DW_FORM_data4;
8216 case 8:
8217 return DW_FORM_data8;
8218 default:
8219 gcc_unreachable ();
8221 case dw_val_class_range_list:
8222 case dw_val_class_loc_list:
8223 if (dwarf_version >= 4)
8224 return DW_FORM_sec_offset;
8225 /* FALLTHRU */
8226 case dw_val_class_vms_delta:
8227 case dw_val_class_offset:
8228 switch (DWARF_OFFSET_SIZE)
8230 case 4:
8231 return DW_FORM_data4;
8232 case 8:
8233 return DW_FORM_data8;
8234 default:
8235 gcc_unreachable ();
8237 case dw_val_class_loc:
8238 if (dwarf_version >= 4)
8239 return DW_FORM_exprloc;
8240 switch (constant_size (size_of_locs (AT_loc (a))))
8242 case 1:
8243 return DW_FORM_block1;
8244 case 2:
8245 return DW_FORM_block2;
8246 case 4:
8247 return DW_FORM_block4;
8248 default:
8249 gcc_unreachable ();
8251 case dw_val_class_const:
8252 return DW_FORM_sdata;
8253 case dw_val_class_unsigned_const:
8254 switch (constant_size (AT_unsigned (a)))
8256 case 1:
8257 return DW_FORM_data1;
8258 case 2:
8259 return DW_FORM_data2;
8260 case 4:
8261 /* In DWARF3 DW_AT_data_member_location with
8262 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8263 constant, so we need to use DW_FORM_udata if we need
8264 a large constant. */
8265 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8266 return DW_FORM_udata;
8267 return DW_FORM_data4;
8268 case 8:
8269 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8270 return DW_FORM_udata;
8271 return DW_FORM_data8;
8272 default:
8273 gcc_unreachable ();
8275 case dw_val_class_const_double:
8276 switch (HOST_BITS_PER_WIDE_INT)
8278 case 8:
8279 return DW_FORM_data2;
8280 case 16:
8281 return DW_FORM_data4;
8282 case 32:
8283 return DW_FORM_data8;
8284 case 64:
8285 default:
8286 return DW_FORM_block1;
8288 case dw_val_class_wide_int:
8289 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8291 case 8:
8292 return DW_FORM_data1;
8293 case 16:
8294 return DW_FORM_data2;
8295 case 32:
8296 return DW_FORM_data4;
8297 case 64:
8298 return DW_FORM_data8;
8299 default:
8300 return DW_FORM_block1;
8302 case dw_val_class_vec:
8303 switch (constant_size (a->dw_attr_val.v.val_vec.length
8304 * a->dw_attr_val.v.val_vec.elt_size))
8306 case 1:
8307 return DW_FORM_block1;
8308 case 2:
8309 return DW_FORM_block2;
8310 case 4:
8311 return DW_FORM_block4;
8312 default:
8313 gcc_unreachable ();
8315 case dw_val_class_flag:
8316 if (dwarf_version >= 4)
8318 /* Currently all add_AT_flag calls pass in 1 as last argument,
8319 so DW_FORM_flag_present can be used. If that ever changes,
8320 we'll need to use DW_FORM_flag and have some optimization
8321 in build_abbrev_table that will change those to
8322 DW_FORM_flag_present if it is set to 1 in all DIEs using
8323 the same abbrev entry. */
8324 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8325 return DW_FORM_flag_present;
8327 return DW_FORM_flag;
8328 case dw_val_class_die_ref:
8329 if (AT_ref_external (a))
8330 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8331 else
8332 return DW_FORM_ref;
8333 case dw_val_class_fde_ref:
8334 return DW_FORM_data;
8335 case dw_val_class_lbl_id:
8336 return (AT_index (a) == NOT_INDEXED
8337 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8338 case dw_val_class_lineptr:
8339 case dw_val_class_macptr:
8340 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8341 case dw_val_class_str:
8342 return AT_string_form (a);
8343 case dw_val_class_file:
8344 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8346 case 1:
8347 return DW_FORM_data1;
8348 case 2:
8349 return DW_FORM_data2;
8350 case 4:
8351 return DW_FORM_data4;
8352 default:
8353 gcc_unreachable ();
8356 case dw_val_class_data8:
8357 return DW_FORM_data8;
8359 case dw_val_class_high_pc:
8360 switch (DWARF2_ADDR_SIZE)
8362 case 1:
8363 return DW_FORM_data1;
8364 case 2:
8365 return DW_FORM_data2;
8366 case 4:
8367 return DW_FORM_data4;
8368 case 8:
8369 return DW_FORM_data8;
8370 default:
8371 gcc_unreachable ();
8374 default:
8375 gcc_unreachable ();
8379 /* Output the encoding of an attribute value. */
8381 static void
8382 output_value_format (dw_attr_ref a)
8384 enum dwarf_form form = value_format (a);
8386 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8389 /* Given a die and id, produce the appropriate abbreviations. */
8391 static void
8392 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8394 unsigned ix;
8395 dw_attr_ref a_attr;
8397 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8398 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8399 dwarf_tag_name (abbrev->die_tag));
8401 if (abbrev->die_child != NULL)
8402 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8403 else
8404 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8406 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8408 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8409 dwarf_attr_name (a_attr->dw_attr));
8410 output_value_format (a_attr);
8413 dw2_asm_output_data (1, 0, NULL);
8414 dw2_asm_output_data (1, 0, NULL);
8418 /* Output the .debug_abbrev section which defines the DIE abbreviation
8419 table. */
8421 static void
8422 output_abbrev_section (void)
8424 unsigned long abbrev_id;
8426 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8427 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8429 /* Terminate the table. */
8430 dw2_asm_output_data (1, 0, NULL);
8433 /* Output a symbol we can use to refer to this DIE from another CU. */
8435 static inline void
8436 output_die_symbol (dw_die_ref die)
8438 const char *sym = die->die_id.die_symbol;
8440 gcc_assert (!die->comdat_type_p);
8442 if (sym == 0)
8443 return;
8445 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8446 /* We make these global, not weak; if the target doesn't support
8447 .linkonce, it doesn't support combining the sections, so debugging
8448 will break. */
8449 targetm.asm_out.globalize_label (asm_out_file, sym);
8451 ASM_OUTPUT_LABEL (asm_out_file, sym);
8454 /* Return a new location list, given the begin and end range, and the
8455 expression. */
8457 static inline dw_loc_list_ref
8458 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8459 const char *section)
8461 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8463 retlist->begin = begin;
8464 retlist->begin_entry = NULL;
8465 retlist->end = end;
8466 retlist->expr = expr;
8467 retlist->section = section;
8469 return retlist;
8472 /* Generate a new internal symbol for this location list node, if it
8473 hasn't got one yet. */
8475 static inline void
8476 gen_llsym (dw_loc_list_ref list)
8478 gcc_assert (!list->ll_symbol);
8479 list->ll_symbol = gen_internal_sym ("LLST");
8482 /* Output the location list given to us. */
8484 static void
8485 output_loc_list (dw_loc_list_ref list_head)
8487 dw_loc_list_ref curr = list_head;
8489 if (list_head->emitted)
8490 return;
8491 list_head->emitted = true;
8493 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8495 /* Walk the location list, and output each range + expression. */
8496 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8498 unsigned long size;
8499 /* Don't output an entry that starts and ends at the same address. */
8500 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8501 continue;
8502 size = size_of_locs (curr->expr);
8503 /* If the expression is too large, drop it on the floor. We could
8504 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8505 in the expression, but >= 64KB expressions for a single value
8506 in a single range are unlikely very useful. */
8507 if (size > 0xffff)
8508 continue;
8509 if (dwarf_split_debug_info)
8511 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8512 "Location list start/length entry (%s)",
8513 list_head->ll_symbol);
8514 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8515 "Location list range start index (%s)",
8516 curr->begin);
8517 /* The length field is 4 bytes. If we ever need to support
8518 an 8-byte length, we can add a new DW_LLE code or fall back
8519 to DW_LLE_GNU_start_end_entry. */
8520 dw2_asm_output_delta (4, curr->end, curr->begin,
8521 "Location list range length (%s)",
8522 list_head->ll_symbol);
8524 else if (!have_multiple_function_sections)
8526 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8527 "Location list begin address (%s)",
8528 list_head->ll_symbol);
8529 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8530 "Location list end address (%s)",
8531 list_head->ll_symbol);
8533 else
8535 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8536 "Location list begin address (%s)",
8537 list_head->ll_symbol);
8538 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8539 "Location list end address (%s)",
8540 list_head->ll_symbol);
8543 /* Output the block length for this list of location operations. */
8544 gcc_assert (size <= 0xffff);
8545 dw2_asm_output_data (2, size, "%s", "Location expression size");
8547 output_loc_sequence (curr->expr, -1);
8550 if (dwarf_split_debug_info)
8551 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8552 "Location list terminator (%s)",
8553 list_head->ll_symbol);
8554 else
8556 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8557 "Location list terminator begin (%s)",
8558 list_head->ll_symbol);
8559 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8560 "Location list terminator end (%s)",
8561 list_head->ll_symbol);
8565 /* Output a range_list offset into the debug_range section. Emit a
8566 relocated reference if val_entry is NULL, otherwise, emit an
8567 indirect reference. */
8569 static void
8570 output_range_list_offset (dw_attr_ref a)
8572 const char *name = dwarf_attr_name (a->dw_attr);
8574 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8576 char *p = strchr (ranges_section_label, '\0');
8577 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8578 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8579 debug_ranges_section, "%s", name);
8580 *p = '\0';
8582 else
8583 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8584 "%s (offset from %s)", name, ranges_section_label);
8587 /* Output the offset into the debug_loc section. */
8589 static void
8590 output_loc_list_offset (dw_attr_ref a)
8592 char *sym = AT_loc_list (a)->ll_symbol;
8594 gcc_assert (sym);
8595 if (dwarf_split_debug_info)
8596 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8597 "%s", dwarf_attr_name (a->dw_attr));
8598 else
8599 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8600 "%s", dwarf_attr_name (a->dw_attr));
8603 /* Output an attribute's index or value appropriately. */
8605 static void
8606 output_attr_index_or_value (dw_attr_ref a)
8608 const char *name = dwarf_attr_name (a->dw_attr);
8610 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8612 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8613 return;
8615 switch (AT_class (a))
8617 case dw_val_class_addr:
8618 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8619 break;
8620 case dw_val_class_high_pc:
8621 case dw_val_class_lbl_id:
8622 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8623 break;
8624 case dw_val_class_loc_list:
8625 output_loc_list_offset (a);
8626 break;
8627 default:
8628 gcc_unreachable ();
8632 /* Output a type signature. */
8634 static inline void
8635 output_signature (const char *sig, const char *name)
8637 int i;
8639 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8640 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8643 /* Output the DIE and its attributes. Called recursively to generate
8644 the definitions of each child DIE. */
8646 static void
8647 output_die (dw_die_ref die)
8649 dw_attr_ref a;
8650 dw_die_ref c;
8651 unsigned long size;
8652 unsigned ix;
8654 /* If someone in another CU might refer to us, set up a symbol for
8655 them to point to. */
8656 if (! die->comdat_type_p && die->die_id.die_symbol)
8657 output_die_symbol (die);
8659 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8660 (unsigned long)die->die_offset,
8661 dwarf_tag_name (die->die_tag));
8663 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8665 const char *name = dwarf_attr_name (a->dw_attr);
8667 switch (AT_class (a))
8669 case dw_val_class_addr:
8670 output_attr_index_or_value (a);
8671 break;
8673 case dw_val_class_offset:
8674 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8675 "%s", name);
8676 break;
8678 case dw_val_class_range_list:
8679 output_range_list_offset (a);
8680 break;
8682 case dw_val_class_loc:
8683 size = size_of_locs (AT_loc (a));
8685 /* Output the block length for this list of location operations. */
8686 if (dwarf_version >= 4)
8687 dw2_asm_output_data_uleb128 (size, "%s", name);
8688 else
8689 dw2_asm_output_data (constant_size (size), size, "%s", name);
8691 output_loc_sequence (AT_loc (a), -1);
8692 break;
8694 case dw_val_class_const:
8695 /* ??? It would be slightly more efficient to use a scheme like is
8696 used for unsigned constants below, but gdb 4.x does not sign
8697 extend. Gdb 5.x does sign extend. */
8698 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8699 break;
8701 case dw_val_class_unsigned_const:
8703 int csize = constant_size (AT_unsigned (a));
8704 if (dwarf_version == 3
8705 && a->dw_attr == DW_AT_data_member_location
8706 && csize >= 4)
8707 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8708 else
8709 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8711 break;
8713 case dw_val_class_const_double:
8715 unsigned HOST_WIDE_INT first, second;
8717 if (HOST_BITS_PER_WIDE_INT >= 64)
8718 dw2_asm_output_data (1,
8719 HOST_BITS_PER_DOUBLE_INT
8720 / HOST_BITS_PER_CHAR,
8721 NULL);
8723 if (WORDS_BIG_ENDIAN)
8725 first = a->dw_attr_val.v.val_double.high;
8726 second = a->dw_attr_val.v.val_double.low;
8728 else
8730 first = a->dw_attr_val.v.val_double.low;
8731 second = a->dw_attr_val.v.val_double.high;
8734 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8735 first, "%s", name);
8736 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8737 second, NULL);
8739 break;
8741 case dw_val_class_wide_int:
8743 int i;
8744 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8745 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8746 if (len * HOST_BITS_PER_WIDE_INT > 64)
8747 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8748 NULL);
8750 if (WORDS_BIG_ENDIAN)
8751 for (i = len - 1; i >= 0; --i)
8753 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8754 name);
8755 name = NULL;
8757 else
8758 for (i = 0; i < len; ++i)
8760 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8761 name);
8762 name = NULL;
8765 break;
8767 case dw_val_class_vec:
8769 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8770 unsigned int len = a->dw_attr_val.v.val_vec.length;
8771 unsigned int i;
8772 unsigned char *p;
8774 dw2_asm_output_data (constant_size (len * elt_size),
8775 len * elt_size, "%s", name);
8776 if (elt_size > sizeof (HOST_WIDE_INT))
8778 elt_size /= 2;
8779 len *= 2;
8781 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8782 i < len;
8783 i++, p += elt_size)
8784 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8785 "fp or vector constant word %u", i);
8786 break;
8789 case dw_val_class_flag:
8790 if (dwarf_version >= 4)
8792 /* Currently all add_AT_flag calls pass in 1 as last argument,
8793 so DW_FORM_flag_present can be used. If that ever changes,
8794 we'll need to use DW_FORM_flag and have some optimization
8795 in build_abbrev_table that will change those to
8796 DW_FORM_flag_present if it is set to 1 in all DIEs using
8797 the same abbrev entry. */
8798 gcc_assert (AT_flag (a) == 1);
8799 if (flag_debug_asm)
8800 fprintf (asm_out_file, "\t\t\t%s %s\n",
8801 ASM_COMMENT_START, name);
8802 break;
8804 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8805 break;
8807 case dw_val_class_loc_list:
8808 output_attr_index_or_value (a);
8809 break;
8811 case dw_val_class_die_ref:
8812 if (AT_ref_external (a))
8814 if (AT_ref (a)->comdat_type_p)
8816 comdat_type_node_ref type_node =
8817 AT_ref (a)->die_id.die_type_node;
8819 gcc_assert (type_node);
8820 output_signature (type_node->signature, name);
8822 else
8824 const char *sym = AT_ref (a)->die_id.die_symbol;
8825 int size;
8827 gcc_assert (sym);
8828 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8829 length, whereas in DWARF3 it's always sized as an
8830 offset. */
8831 if (dwarf_version == 2)
8832 size = DWARF2_ADDR_SIZE;
8833 else
8834 size = DWARF_OFFSET_SIZE;
8835 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8836 name);
8839 else
8841 gcc_assert (AT_ref (a)->die_offset);
8842 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8843 "%s", name);
8845 break;
8847 case dw_val_class_fde_ref:
8849 char l1[20];
8851 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8852 a->dw_attr_val.v.val_fde_index * 2);
8853 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8854 "%s", name);
8856 break;
8858 case dw_val_class_vms_delta:
8859 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8860 AT_vms_delta2 (a), AT_vms_delta1 (a),
8861 "%s", name);
8862 break;
8864 case dw_val_class_lbl_id:
8865 output_attr_index_or_value (a);
8866 break;
8868 case dw_val_class_lineptr:
8869 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8870 debug_line_section, "%s", name);
8871 break;
8873 case dw_val_class_macptr:
8874 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8875 debug_macinfo_section, "%s", name);
8876 break;
8878 case dw_val_class_str:
8879 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8880 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8881 a->dw_attr_val.v.val_str->label,
8882 debug_str_section,
8883 "%s: \"%s\"", name, AT_string (a));
8884 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8885 dw2_asm_output_data_uleb128 (AT_index (a),
8886 "%s: \"%s\"", name, AT_string (a));
8887 else
8888 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8889 break;
8891 case dw_val_class_file:
8893 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8895 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8896 a->dw_attr_val.v.val_file->filename);
8897 break;
8900 case dw_val_class_data8:
8902 int i;
8904 for (i = 0; i < 8; i++)
8905 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8906 i == 0 ? "%s" : NULL, name);
8907 break;
8910 case dw_val_class_high_pc:
8911 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8912 get_AT_low_pc (die), "DW_AT_high_pc");
8913 break;
8915 default:
8916 gcc_unreachable ();
8920 FOR_EACH_CHILD (die, c, output_die (c));
8922 /* Add null byte to terminate sibling list. */
8923 if (die->die_child != NULL)
8924 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8925 (unsigned long) die->die_offset);
8928 /* Output the compilation unit that appears at the beginning of the
8929 .debug_info section, and precedes the DIE descriptions. */
8931 static void
8932 output_compilation_unit_header (void)
8934 int ver = dwarf_version;
8936 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8937 dw2_asm_output_data (4, 0xffffffff,
8938 "Initial length escape value indicating 64-bit DWARF extension");
8939 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8940 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8941 "Length of Compilation Unit Info");
8942 dw2_asm_output_data (2, ver, "DWARF version number");
8943 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8944 debug_abbrev_section,
8945 "Offset Into Abbrev. Section");
8946 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8949 /* Output the compilation unit DIE and its children. */
8951 static void
8952 output_comp_unit (dw_die_ref die, int output_if_empty)
8954 const char *secname, *oldsym;
8955 char *tmp;
8957 /* Unless we are outputting main CU, we may throw away empty ones. */
8958 if (!output_if_empty && die->die_child == NULL)
8959 return;
8961 /* Even if there are no children of this DIE, we must output the information
8962 about the compilation unit. Otherwise, on an empty translation unit, we
8963 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8964 will then complain when examining the file. First mark all the DIEs in
8965 this CU so we know which get local refs. */
8966 mark_dies (die);
8968 external_ref_hash_type *extern_map = optimize_external_refs (die);
8970 build_abbrev_table (die, extern_map);
8972 delete extern_map;
8974 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8975 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8976 calc_die_sizes (die);
8978 oldsym = die->die_id.die_symbol;
8979 if (oldsym)
8981 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8983 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8984 secname = tmp;
8985 die->die_id.die_symbol = NULL;
8986 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8988 else
8990 switch_to_section (debug_info_section);
8991 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8992 info_section_emitted = true;
8995 /* Output debugging information. */
8996 output_compilation_unit_header ();
8997 output_die (die);
8999 /* Leave the marks on the main CU, so we can check them in
9000 output_pubnames. */
9001 if (oldsym)
9003 unmark_dies (die);
9004 die->die_id.die_symbol = oldsym;
9008 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9009 and .debug_pubtypes. This is configured per-target, but can be
9010 overridden by the -gpubnames or -gno-pubnames options. */
9012 static inline bool
9013 want_pubnames (void)
9015 if (debug_info_level <= DINFO_LEVEL_TERSE)
9016 return false;
9017 if (debug_generate_pub_sections != -1)
9018 return debug_generate_pub_sections;
9019 return targetm.want_debug_pub_sections;
9022 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9024 static void
9025 add_AT_pubnames (dw_die_ref die)
9027 if (want_pubnames ())
9028 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9031 /* Add a string attribute value to a skeleton DIE. */
9033 static inline void
9034 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9035 const char *str)
9037 dw_attr_node attr;
9038 struct indirect_string_node *node;
9040 if (! skeleton_debug_str_hash)
9041 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
9042 debug_str_eq, NULL);
9044 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9045 find_string_form (node);
9046 if (node->form == DW_FORM_GNU_str_index)
9047 node->form = DW_FORM_strp;
9049 attr.dw_attr = attr_kind;
9050 attr.dw_attr_val.val_class = dw_val_class_str;
9051 attr.dw_attr_val.val_entry = NULL;
9052 attr.dw_attr_val.v.val_str = node;
9053 add_dwarf_attr (die, &attr);
9056 /* Helper function to generate top-level dies for skeleton debug_info and
9057 debug_types. */
9059 static void
9060 add_top_level_skeleton_die_attrs (dw_die_ref die)
9062 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9063 const char *comp_dir = comp_dir_string ();
9065 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9066 if (comp_dir != NULL)
9067 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9068 add_AT_pubnames (die);
9069 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9072 /* Output skeleton debug sections that point to the dwo file. */
9074 static void
9075 output_skeleton_debug_sections (dw_die_ref comp_unit)
9077 /* These attributes will be found in the full debug_info section. */
9078 remove_AT (comp_unit, DW_AT_producer);
9079 remove_AT (comp_unit, DW_AT_language);
9081 switch_to_section (debug_skeleton_info_section);
9082 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9084 /* Produce the skeleton compilation-unit header. This one differs enough from
9085 a normal CU header that it's better not to call output_compilation_unit
9086 header. */
9087 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9088 dw2_asm_output_data (4, 0xffffffff,
9089 "Initial length escape value indicating 64-bit DWARF extension");
9091 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9092 DWARF_COMPILE_UNIT_HEADER_SIZE
9093 - DWARF_INITIAL_LENGTH_SIZE
9094 + size_of_die (comp_unit),
9095 "Length of Compilation Unit Info");
9096 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9097 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9098 debug_abbrev_section,
9099 "Offset Into Abbrev. Section");
9100 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9102 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9103 output_die (comp_unit);
9105 /* Build the skeleton debug_abbrev section. */
9106 switch_to_section (debug_skeleton_abbrev_section);
9107 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9109 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9111 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9114 /* Output a comdat type unit DIE and its children. */
9116 static void
9117 output_comdat_type_unit (comdat_type_node *node)
9119 const char *secname;
9120 char *tmp;
9121 int i;
9122 #if defined (OBJECT_FORMAT_ELF)
9123 tree comdat_key;
9124 #endif
9126 /* First mark all the DIEs in this CU so we know which get local refs. */
9127 mark_dies (node->root_die);
9129 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9131 build_abbrev_table (node->root_die, extern_map);
9133 delete extern_map;
9134 extern_map = NULL;
9136 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9137 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9138 calc_die_sizes (node->root_die);
9140 #if defined (OBJECT_FORMAT_ELF)
9141 if (!dwarf_split_debug_info)
9142 secname = ".debug_types";
9143 else
9144 secname = ".debug_types.dwo";
9146 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9147 sprintf (tmp, "wt.");
9148 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9149 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9150 comdat_key = get_identifier (tmp);
9151 targetm.asm_out.named_section (secname,
9152 SECTION_DEBUG | SECTION_LINKONCE,
9153 comdat_key);
9154 #else
9155 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9156 sprintf (tmp, ".gnu.linkonce.wt.");
9157 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9158 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9159 secname = tmp;
9160 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9161 #endif
9163 /* Output debugging information. */
9164 output_compilation_unit_header ();
9165 output_signature (node->signature, "Type Signature");
9166 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9167 "Offset to Type DIE");
9168 output_die (node->root_die);
9170 unmark_dies (node->root_die);
9173 /* Return the DWARF2/3 pubname associated with a decl. */
9175 static const char *
9176 dwarf2_name (tree decl, int scope)
9178 if (DECL_NAMELESS (decl))
9179 return NULL;
9180 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9183 /* Add a new entry to .debug_pubnames if appropriate. */
9185 static void
9186 add_pubname_string (const char *str, dw_die_ref die)
9188 pubname_entry e;
9190 e.die = die;
9191 e.name = xstrdup (str);
9192 vec_safe_push (pubname_table, e);
9195 static void
9196 add_pubname (tree decl, dw_die_ref die)
9198 if (!want_pubnames ())
9199 return;
9201 /* Don't add items to the table when we expect that the consumer will have
9202 just read the enclosing die. For example, if the consumer is looking at a
9203 class_member, it will either be inside the class already, or will have just
9204 looked up the class to find the member. Either way, searching the class is
9205 faster than searching the index. */
9206 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9207 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9209 const char *name = dwarf2_name (decl, 1);
9211 if (name)
9212 add_pubname_string (name, die);
9216 /* Add an enumerator to the pubnames section. */
9218 static void
9219 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9221 pubname_entry e;
9223 gcc_assert (scope_name);
9224 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9225 e.die = die;
9226 vec_safe_push (pubname_table, e);
9229 /* Add a new entry to .debug_pubtypes if appropriate. */
9231 static void
9232 add_pubtype (tree decl, dw_die_ref die)
9234 pubname_entry e;
9236 if (!want_pubnames ())
9237 return;
9239 if ((TREE_PUBLIC (decl)
9240 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9241 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9243 tree scope = NULL;
9244 const char *scope_name = "";
9245 const char *sep = is_cxx () ? "::" : ".";
9246 const char *name;
9248 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9249 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9251 scope_name = lang_hooks.dwarf_name (scope, 1);
9252 if (scope_name != NULL && scope_name[0] != '\0')
9253 scope_name = concat (scope_name, sep, NULL);
9254 else
9255 scope_name = "";
9258 if (TYPE_P (decl))
9259 name = type_tag (decl);
9260 else
9261 name = lang_hooks.dwarf_name (decl, 1);
9263 /* If we don't have a name for the type, there's no point in adding
9264 it to the table. */
9265 if (name != NULL && name[0] != '\0')
9267 e.die = die;
9268 e.name = concat (scope_name, name, NULL);
9269 vec_safe_push (pubtype_table, e);
9272 /* Although it might be more consistent to add the pubinfo for the
9273 enumerators as their dies are created, they should only be added if the
9274 enum type meets the criteria above. So rather than re-check the parent
9275 enum type whenever an enumerator die is created, just output them all
9276 here. This isn't protected by the name conditional because anonymous
9277 enums don't have names. */
9278 if (die->die_tag == DW_TAG_enumeration_type)
9280 dw_die_ref c;
9282 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9287 /* Output a single entry in the pubnames table. */
9289 static void
9290 output_pubname (dw_offset die_offset, pubname_entry *entry)
9292 dw_die_ref die = entry->die;
9293 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9295 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9297 if (debug_generate_pub_sections == 2)
9299 /* This logic follows gdb's method for determining the value of the flag
9300 byte. */
9301 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9302 switch (die->die_tag)
9304 case DW_TAG_typedef:
9305 case DW_TAG_base_type:
9306 case DW_TAG_subrange_type:
9307 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9308 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9309 break;
9310 case DW_TAG_enumerator:
9311 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9312 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9313 if (!is_cxx () && !is_java ())
9314 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9315 break;
9316 case DW_TAG_subprogram:
9317 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9318 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9319 if (!is_ada ())
9320 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9321 break;
9322 case DW_TAG_constant:
9323 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9324 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9325 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9326 break;
9327 case DW_TAG_variable:
9328 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9329 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9330 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9331 break;
9332 case DW_TAG_namespace:
9333 case DW_TAG_imported_declaration:
9334 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9335 break;
9336 case DW_TAG_class_type:
9337 case DW_TAG_interface_type:
9338 case DW_TAG_structure_type:
9339 case DW_TAG_union_type:
9340 case DW_TAG_enumeration_type:
9341 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9342 if (!is_cxx () && !is_java ())
9343 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9344 break;
9345 default:
9346 /* An unusual tag. Leave the flag-byte empty. */
9347 break;
9349 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9350 "GDB-index flags");
9353 dw2_asm_output_nstring (entry->name, -1, "external name");
9357 /* Output the public names table used to speed up access to externally
9358 visible names; or the public types table used to find type definitions. */
9360 static void
9361 output_pubnames (vec<pubname_entry, va_gc> *names)
9363 unsigned i;
9364 unsigned long pubnames_length = size_of_pubnames (names);
9365 pubname_ref pub;
9367 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9368 dw2_asm_output_data (4, 0xffffffff,
9369 "Initial length escape value indicating 64-bit DWARF extension");
9370 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9372 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9373 dw2_asm_output_data (2, 2, "DWARF Version");
9375 if (dwarf_split_debug_info)
9376 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9377 debug_skeleton_info_section,
9378 "Offset of Compilation Unit Info");
9379 else
9380 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9381 debug_info_section,
9382 "Offset of Compilation Unit Info");
9383 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9384 "Compilation Unit Length");
9386 FOR_EACH_VEC_ELT (*names, i, pub)
9388 if (include_pubname_in_output (names, pub))
9390 dw_offset die_offset = pub->die->die_offset;
9392 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9393 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9394 gcc_assert (pub->die->die_mark);
9396 /* If we're putting types in their own .debug_types sections,
9397 the .debug_pubtypes table will still point to the compile
9398 unit (not the type unit), so we want to use the offset of
9399 the skeleton DIE (if there is one). */
9400 if (pub->die->comdat_type_p && names == pubtype_table)
9402 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9404 if (type_node != NULL)
9405 die_offset = (type_node->skeleton_die != NULL
9406 ? type_node->skeleton_die->die_offset
9407 : comp_unit_die ()->die_offset);
9410 output_pubname (die_offset, pub);
9414 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9417 /* Output public names and types tables if necessary. */
9419 static void
9420 output_pubtables (void)
9422 if (!want_pubnames () || !info_section_emitted)
9423 return;
9425 switch_to_section (debug_pubnames_section);
9426 output_pubnames (pubname_table);
9427 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9428 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9429 simply won't look for the section. */
9430 switch_to_section (debug_pubtypes_section);
9431 output_pubnames (pubtype_table);
9435 /* Output the information that goes into the .debug_aranges table.
9436 Namely, define the beginning and ending address range of the
9437 text section generated for this compilation unit. */
9439 static void
9440 output_aranges (unsigned long aranges_length)
9442 unsigned i;
9444 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9445 dw2_asm_output_data (4, 0xffffffff,
9446 "Initial length escape value indicating 64-bit DWARF extension");
9447 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9448 "Length of Address Ranges Info");
9449 /* Version number for aranges is still 2, even in DWARF3. */
9450 dw2_asm_output_data (2, 2, "DWARF Version");
9451 if (dwarf_split_debug_info)
9452 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9453 debug_skeleton_info_section,
9454 "Offset of Compilation Unit Info");
9455 else
9456 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9457 debug_info_section,
9458 "Offset of Compilation Unit Info");
9459 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9460 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9462 /* We need to align to twice the pointer size here. */
9463 if (DWARF_ARANGES_PAD_SIZE)
9465 /* Pad using a 2 byte words so that padding is correct for any
9466 pointer size. */
9467 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9468 2 * DWARF2_ADDR_SIZE);
9469 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9470 dw2_asm_output_data (2, 0, NULL);
9473 /* It is necessary not to output these entries if the sections were
9474 not used; if the sections were not used, the length will be 0 and
9475 the address may end up as 0 if the section is discarded by ld
9476 --gc-sections, leaving an invalid (0, 0) entry that can be
9477 confused with the terminator. */
9478 if (text_section_used)
9480 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9481 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9482 text_section_label, "Length");
9484 if (cold_text_section_used)
9486 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9487 "Address");
9488 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9489 cold_text_section_label, "Length");
9492 if (have_multiple_function_sections)
9494 unsigned fde_idx;
9495 dw_fde_ref fde;
9497 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9499 if (DECL_IGNORED_P (fde->decl))
9500 continue;
9501 if (!fde->in_std_section)
9503 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9504 "Address");
9505 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9506 fde->dw_fde_begin, "Length");
9508 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9510 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9511 "Address");
9512 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9513 fde->dw_fde_second_begin, "Length");
9518 /* Output the terminator words. */
9519 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9520 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9523 /* Add a new entry to .debug_ranges. Return the offset at which it
9524 was placed. */
9526 static unsigned int
9527 add_ranges_num (int num)
9529 unsigned int in_use = ranges_table_in_use;
9531 if (in_use == ranges_table_allocated)
9533 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9534 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9535 ranges_table_allocated);
9536 memset (ranges_table + ranges_table_in_use, 0,
9537 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9540 ranges_table[in_use].num = num;
9541 ranges_table_in_use = in_use + 1;
9543 return in_use * 2 * DWARF2_ADDR_SIZE;
9546 /* Add a new entry to .debug_ranges corresponding to a block, or a
9547 range terminator if BLOCK is NULL. */
9549 static unsigned int
9550 add_ranges (const_tree block)
9552 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9555 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9556 When using dwarf_split_debug_info, address attributes in dies destined
9557 for the final executable should be direct references--setting the
9558 parameter force_direct ensures this behavior. */
9560 static void
9561 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9562 bool *added, bool force_direct)
9564 unsigned int in_use = ranges_by_label_in_use;
9565 unsigned int offset;
9567 if (in_use == ranges_by_label_allocated)
9569 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9570 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9571 ranges_by_label,
9572 ranges_by_label_allocated);
9573 memset (ranges_by_label + ranges_by_label_in_use, 0,
9574 RANGES_TABLE_INCREMENT
9575 * sizeof (struct dw_ranges_by_label_struct));
9578 ranges_by_label[in_use].begin = begin;
9579 ranges_by_label[in_use].end = end;
9580 ranges_by_label_in_use = in_use + 1;
9582 offset = add_ranges_num (-(int)in_use - 1);
9583 if (!*added)
9585 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9586 *added = true;
9590 static void
9591 output_ranges (void)
9593 unsigned i;
9594 static const char *const start_fmt = "Offset %#x";
9595 const char *fmt = start_fmt;
9597 for (i = 0; i < ranges_table_in_use; i++)
9599 int block_num = ranges_table[i].num;
9601 if (block_num > 0)
9603 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9604 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9606 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9607 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9609 /* If all code is in the text section, then the compilation
9610 unit base address defaults to DW_AT_low_pc, which is the
9611 base of the text section. */
9612 if (!have_multiple_function_sections)
9614 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9615 text_section_label,
9616 fmt, i * 2 * DWARF2_ADDR_SIZE);
9617 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9618 text_section_label, NULL);
9621 /* Otherwise, the compilation unit base address is zero,
9622 which allows us to use absolute addresses, and not worry
9623 about whether the target supports cross-section
9624 arithmetic. */
9625 else
9627 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9628 fmt, i * 2 * DWARF2_ADDR_SIZE);
9629 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9632 fmt = NULL;
9635 /* Negative block_num stands for an index into ranges_by_label. */
9636 else if (block_num < 0)
9638 int lab_idx = - block_num - 1;
9640 if (!have_multiple_function_sections)
9642 gcc_unreachable ();
9643 #if 0
9644 /* If we ever use add_ranges_by_labels () for a single
9645 function section, all we have to do is to take out
9646 the #if 0 above. */
9647 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9648 ranges_by_label[lab_idx].begin,
9649 text_section_label,
9650 fmt, i * 2 * DWARF2_ADDR_SIZE);
9651 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9652 ranges_by_label[lab_idx].end,
9653 text_section_label, NULL);
9654 #endif
9656 else
9658 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9659 ranges_by_label[lab_idx].begin,
9660 fmt, i * 2 * DWARF2_ADDR_SIZE);
9661 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9662 ranges_by_label[lab_idx].end,
9663 NULL);
9666 else
9668 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9669 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9670 fmt = start_fmt;
9675 /* Data structure containing information about input files. */
9676 struct file_info
9678 const char *path; /* Complete file name. */
9679 const char *fname; /* File name part. */
9680 int length; /* Length of entire string. */
9681 struct dwarf_file_data * file_idx; /* Index in input file table. */
9682 int dir_idx; /* Index in directory table. */
9685 /* Data structure containing information about directories with source
9686 files. */
9687 struct dir_info
9689 const char *path; /* Path including directory name. */
9690 int length; /* Path length. */
9691 int prefix; /* Index of directory entry which is a prefix. */
9692 int count; /* Number of files in this directory. */
9693 int dir_idx; /* Index of directory used as base. */
9696 /* Callback function for file_info comparison. We sort by looking at
9697 the directories in the path. */
9699 static int
9700 file_info_cmp (const void *p1, const void *p2)
9702 const struct file_info *const s1 = (const struct file_info *) p1;
9703 const struct file_info *const s2 = (const struct file_info *) p2;
9704 const unsigned char *cp1;
9705 const unsigned char *cp2;
9707 /* Take care of file names without directories. We need to make sure that
9708 we return consistent values to qsort since some will get confused if
9709 we return the same value when identical operands are passed in opposite
9710 orders. So if neither has a directory, return 0 and otherwise return
9711 1 or -1 depending on which one has the directory. */
9712 if ((s1->path == s1->fname || s2->path == s2->fname))
9713 return (s2->path == s2->fname) - (s1->path == s1->fname);
9715 cp1 = (const unsigned char *) s1->path;
9716 cp2 = (const unsigned char *) s2->path;
9718 while (1)
9720 ++cp1;
9721 ++cp2;
9722 /* Reached the end of the first path? If so, handle like above. */
9723 if ((cp1 == (const unsigned char *) s1->fname)
9724 || (cp2 == (const unsigned char *) s2->fname))
9725 return ((cp2 == (const unsigned char *) s2->fname)
9726 - (cp1 == (const unsigned char *) s1->fname));
9728 /* Character of current path component the same? */
9729 else if (*cp1 != *cp2)
9730 return *cp1 - *cp2;
9734 struct file_name_acquire_data
9736 struct file_info *files;
9737 int used_files;
9738 int max_files;
9741 /* Traversal function for the hash table. */
9743 static int
9744 file_name_acquire (void ** slot, void *data)
9746 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9747 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9748 struct file_info *fi;
9749 const char *f;
9751 gcc_assert (fnad->max_files >= d->emitted_number);
9753 if (! d->emitted_number)
9754 return 1;
9756 gcc_assert (fnad->max_files != fnad->used_files);
9758 fi = fnad->files + fnad->used_files++;
9760 /* Skip all leading "./". */
9761 f = d->filename;
9762 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9763 f += 2;
9765 /* Create a new array entry. */
9766 fi->path = f;
9767 fi->length = strlen (f);
9768 fi->file_idx = d;
9770 /* Search for the file name part. */
9771 f = strrchr (f, DIR_SEPARATOR);
9772 #if defined (DIR_SEPARATOR_2)
9774 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9776 if (g != NULL)
9778 if (f == NULL || f < g)
9779 f = g;
9782 #endif
9784 fi->fname = f == NULL ? fi->path : f + 1;
9785 return 1;
9788 /* Output the directory table and the file name table. We try to minimize
9789 the total amount of memory needed. A heuristic is used to avoid large
9790 slowdowns with many input files. */
9792 static void
9793 output_file_names (void)
9795 struct file_name_acquire_data fnad;
9796 int numfiles;
9797 struct file_info *files;
9798 struct dir_info *dirs;
9799 int *saved;
9800 int *savehere;
9801 int *backmap;
9802 int ndirs;
9803 int idx_offset;
9804 int i;
9806 if (!last_emitted_file)
9808 dw2_asm_output_data (1, 0, "End directory table");
9809 dw2_asm_output_data (1, 0, "End file name table");
9810 return;
9813 numfiles = last_emitted_file->emitted_number;
9815 /* Allocate the various arrays we need. */
9816 files = XALLOCAVEC (struct file_info, numfiles);
9817 dirs = XALLOCAVEC (struct dir_info, numfiles);
9819 fnad.files = files;
9820 fnad.used_files = 0;
9821 fnad.max_files = numfiles;
9822 htab_traverse (file_table, file_name_acquire, &fnad);
9823 gcc_assert (fnad.used_files == fnad.max_files);
9825 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9827 /* Find all the different directories used. */
9828 dirs[0].path = files[0].path;
9829 dirs[0].length = files[0].fname - files[0].path;
9830 dirs[0].prefix = -1;
9831 dirs[0].count = 1;
9832 dirs[0].dir_idx = 0;
9833 files[0].dir_idx = 0;
9834 ndirs = 1;
9836 for (i = 1; i < numfiles; i++)
9837 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9838 && memcmp (dirs[ndirs - 1].path, files[i].path,
9839 dirs[ndirs - 1].length) == 0)
9841 /* Same directory as last entry. */
9842 files[i].dir_idx = ndirs - 1;
9843 ++dirs[ndirs - 1].count;
9845 else
9847 int j;
9849 /* This is a new directory. */
9850 dirs[ndirs].path = files[i].path;
9851 dirs[ndirs].length = files[i].fname - files[i].path;
9852 dirs[ndirs].count = 1;
9853 dirs[ndirs].dir_idx = ndirs;
9854 files[i].dir_idx = ndirs;
9856 /* Search for a prefix. */
9857 dirs[ndirs].prefix = -1;
9858 for (j = 0; j < ndirs; j++)
9859 if (dirs[j].length < dirs[ndirs].length
9860 && dirs[j].length > 1
9861 && (dirs[ndirs].prefix == -1
9862 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9863 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9864 dirs[ndirs].prefix = j;
9866 ++ndirs;
9869 /* Now to the actual work. We have to find a subset of the directories which
9870 allow expressing the file name using references to the directory table
9871 with the least amount of characters. We do not do an exhaustive search
9872 where we would have to check out every combination of every single
9873 possible prefix. Instead we use a heuristic which provides nearly optimal
9874 results in most cases and never is much off. */
9875 saved = XALLOCAVEC (int, ndirs);
9876 savehere = XALLOCAVEC (int, ndirs);
9878 memset (saved, '\0', ndirs * sizeof (saved[0]));
9879 for (i = 0; i < ndirs; i++)
9881 int j;
9882 int total;
9884 /* We can always save some space for the current directory. But this
9885 does not mean it will be enough to justify adding the directory. */
9886 savehere[i] = dirs[i].length;
9887 total = (savehere[i] - saved[i]) * dirs[i].count;
9889 for (j = i + 1; j < ndirs; j++)
9891 savehere[j] = 0;
9892 if (saved[j] < dirs[i].length)
9894 /* Determine whether the dirs[i] path is a prefix of the
9895 dirs[j] path. */
9896 int k;
9898 k = dirs[j].prefix;
9899 while (k != -1 && k != (int) i)
9900 k = dirs[k].prefix;
9902 if (k == (int) i)
9904 /* Yes it is. We can possibly save some memory by
9905 writing the filenames in dirs[j] relative to
9906 dirs[i]. */
9907 savehere[j] = dirs[i].length;
9908 total += (savehere[j] - saved[j]) * dirs[j].count;
9913 /* Check whether we can save enough to justify adding the dirs[i]
9914 directory. */
9915 if (total > dirs[i].length + 1)
9917 /* It's worthwhile adding. */
9918 for (j = i; j < ndirs; j++)
9919 if (savehere[j] > 0)
9921 /* Remember how much we saved for this directory so far. */
9922 saved[j] = savehere[j];
9924 /* Remember the prefix directory. */
9925 dirs[j].dir_idx = i;
9930 /* Emit the directory name table. */
9931 idx_offset = dirs[0].length > 0 ? 1 : 0;
9932 for (i = 1 - idx_offset; i < ndirs; i++)
9933 dw2_asm_output_nstring (dirs[i].path,
9934 dirs[i].length
9935 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9936 "Directory Entry: %#x", i + idx_offset);
9938 dw2_asm_output_data (1, 0, "End directory table");
9940 /* We have to emit them in the order of emitted_number since that's
9941 used in the debug info generation. To do this efficiently we
9942 generate a back-mapping of the indices first. */
9943 backmap = XALLOCAVEC (int, numfiles);
9944 for (i = 0; i < numfiles; i++)
9945 backmap[files[i].file_idx->emitted_number - 1] = i;
9947 /* Now write all the file names. */
9948 for (i = 0; i < numfiles; i++)
9950 int file_idx = backmap[i];
9951 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9953 #ifdef VMS_DEBUGGING_INFO
9954 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9956 /* Setting these fields can lead to debugger miscomparisons,
9957 but VMS Debug requires them to be set correctly. */
9959 int ver;
9960 long long cdt;
9961 long siz;
9962 int maxfilelen = strlen (files[file_idx].path)
9963 + dirs[dir_idx].length
9964 + MAX_VMS_VERSION_LEN + 1;
9965 char *filebuf = XALLOCAVEC (char, maxfilelen);
9967 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9968 snprintf (filebuf, maxfilelen, "%s;%d",
9969 files[file_idx].path + dirs[dir_idx].length, ver);
9971 dw2_asm_output_nstring
9972 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9974 /* Include directory index. */
9975 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9977 /* Modification time. */
9978 dw2_asm_output_data_uleb128
9979 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9980 ? cdt : 0,
9981 NULL);
9983 /* File length in bytes. */
9984 dw2_asm_output_data_uleb128
9985 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9986 ? siz : 0,
9987 NULL);
9988 #else
9989 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9990 "File Entry: %#x", (unsigned) i + 1);
9992 /* Include directory index. */
9993 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9995 /* Modification time. */
9996 dw2_asm_output_data_uleb128 (0, NULL);
9998 /* File length in bytes. */
9999 dw2_asm_output_data_uleb128 (0, NULL);
10000 #endif /* VMS_DEBUGGING_INFO */
10003 dw2_asm_output_data (1, 0, "End file name table");
10007 /* Output one line number table into the .debug_line section. */
10009 static void
10010 output_one_line_info_table (dw_line_info_table *table)
10012 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10013 unsigned int current_line = 1;
10014 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10015 dw_line_info_entry *ent;
10016 size_t i;
10018 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10020 switch (ent->opcode)
10022 case LI_set_address:
10023 /* ??? Unfortunately, we have little choice here currently, and
10024 must always use the most general form. GCC does not know the
10025 address delta itself, so we can't use DW_LNS_advance_pc. Many
10026 ports do have length attributes which will give an upper bound
10027 on the address range. We could perhaps use length attributes
10028 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10029 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10031 /* This can handle any delta. This takes
10032 4+DWARF2_ADDR_SIZE bytes. */
10033 dw2_asm_output_data (1, 0, "set address %s", line_label);
10034 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10035 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10036 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10037 break;
10039 case LI_set_line:
10040 if (ent->val == current_line)
10042 /* We still need to start a new row, so output a copy insn. */
10043 dw2_asm_output_data (1, DW_LNS_copy,
10044 "copy line %u", current_line);
10046 else
10048 int line_offset = ent->val - current_line;
10049 int line_delta = line_offset - DWARF_LINE_BASE;
10051 current_line = ent->val;
10052 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10054 /* This can handle deltas from -10 to 234, using the current
10055 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10056 This takes 1 byte. */
10057 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10058 "line %u", current_line);
10060 else
10062 /* This can handle any delta. This takes at least 4 bytes,
10063 depending on the value being encoded. */
10064 dw2_asm_output_data (1, DW_LNS_advance_line,
10065 "advance to line %u", current_line);
10066 dw2_asm_output_data_sleb128 (line_offset, NULL);
10067 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10070 break;
10072 case LI_set_file:
10073 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10074 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10075 break;
10077 case LI_set_column:
10078 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10079 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10080 break;
10082 case LI_negate_stmt:
10083 current_is_stmt = !current_is_stmt;
10084 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10085 "is_stmt %d", current_is_stmt);
10086 break;
10088 case LI_set_prologue_end:
10089 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10090 "set prologue end");
10091 break;
10093 case LI_set_epilogue_begin:
10094 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10095 "set epilogue begin");
10096 break;
10098 case LI_set_discriminator:
10099 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10100 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10101 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10102 dw2_asm_output_data_uleb128 (ent->val, NULL);
10103 break;
10107 /* Emit debug info for the address of the end of the table. */
10108 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10109 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10110 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10111 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10113 dw2_asm_output_data (1, 0, "end sequence");
10114 dw2_asm_output_data_uleb128 (1, NULL);
10115 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10118 /* Output the source line number correspondence information. This
10119 information goes into the .debug_line section. */
10121 static void
10122 output_line_info (bool prologue_only)
10124 char l1[20], l2[20], p1[20], p2[20];
10125 int ver = dwarf_version;
10126 bool saw_one = false;
10127 int opc;
10129 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10130 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10131 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10132 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10134 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10135 dw2_asm_output_data (4, 0xffffffff,
10136 "Initial length escape value indicating 64-bit DWARF extension");
10137 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10138 "Length of Source Line Info");
10139 ASM_OUTPUT_LABEL (asm_out_file, l1);
10141 dw2_asm_output_data (2, ver, "DWARF Version");
10142 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10143 ASM_OUTPUT_LABEL (asm_out_file, p1);
10145 /* Define the architecture-dependent minimum instruction length (in bytes).
10146 In this implementation of DWARF, this field is used for information
10147 purposes only. Since GCC generates assembly language, we have no
10148 a priori knowledge of how many instruction bytes are generated for each
10149 source line, and therefore can use only the DW_LNE_set_address and
10150 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10151 this as '1', which is "correct enough" for all architectures,
10152 and don't let the target override. */
10153 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10155 if (ver >= 4)
10156 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10157 "Maximum Operations Per Instruction");
10158 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10159 "Default is_stmt_start flag");
10160 dw2_asm_output_data (1, DWARF_LINE_BASE,
10161 "Line Base Value (Special Opcodes)");
10162 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10163 "Line Range Value (Special Opcodes)");
10164 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10165 "Special Opcode Base");
10167 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10169 int n_op_args;
10170 switch (opc)
10172 case DW_LNS_advance_pc:
10173 case DW_LNS_advance_line:
10174 case DW_LNS_set_file:
10175 case DW_LNS_set_column:
10176 case DW_LNS_fixed_advance_pc:
10177 case DW_LNS_set_isa:
10178 n_op_args = 1;
10179 break;
10180 default:
10181 n_op_args = 0;
10182 break;
10185 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10186 opc, n_op_args);
10189 /* Write out the information about the files we use. */
10190 output_file_names ();
10191 ASM_OUTPUT_LABEL (asm_out_file, p2);
10192 if (prologue_only)
10194 /* Output the marker for the end of the line number info. */
10195 ASM_OUTPUT_LABEL (asm_out_file, l2);
10196 return;
10199 if (separate_line_info)
10201 dw_line_info_table *table;
10202 size_t i;
10204 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10205 if (table->in_use)
10207 output_one_line_info_table (table);
10208 saw_one = true;
10211 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10213 output_one_line_info_table (cold_text_section_line_info);
10214 saw_one = true;
10217 /* ??? Some Darwin linkers crash on a .debug_line section with no
10218 sequences. Further, merely a DW_LNE_end_sequence entry is not
10219 sufficient -- the address column must also be initialized.
10220 Make sure to output at least one set_address/end_sequence pair,
10221 choosing .text since that section is always present. */
10222 if (text_section_line_info->in_use || !saw_one)
10223 output_one_line_info_table (text_section_line_info);
10225 /* Output the marker for the end of the line number info. */
10226 ASM_OUTPUT_LABEL (asm_out_file, l2);
10229 /* Given a pointer to a tree node for some base type, return a pointer to
10230 a DIE that describes the given type.
10232 This routine must only be called for GCC type nodes that correspond to
10233 Dwarf base (fundamental) types. */
10235 static dw_die_ref
10236 base_type_die (tree type)
10238 dw_die_ref base_type_result;
10239 enum dwarf_type encoding;
10241 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10242 return 0;
10244 /* If this is a subtype that should not be emitted as a subrange type,
10245 use the base type. See subrange_type_for_debug_p. */
10246 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10247 type = TREE_TYPE (type);
10249 switch (TREE_CODE (type))
10251 case INTEGER_TYPE:
10252 if ((dwarf_version >= 4 || !dwarf_strict)
10253 && TYPE_NAME (type)
10254 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10255 && DECL_IS_BUILTIN (TYPE_NAME (type))
10256 && DECL_NAME (TYPE_NAME (type)))
10258 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10259 if (strcmp (name, "char16_t") == 0
10260 || strcmp (name, "char32_t") == 0)
10262 encoding = DW_ATE_UTF;
10263 break;
10266 if (TYPE_STRING_FLAG (type))
10268 if (TYPE_UNSIGNED (type))
10269 encoding = DW_ATE_unsigned_char;
10270 else
10271 encoding = DW_ATE_signed_char;
10273 else if (TYPE_UNSIGNED (type))
10274 encoding = DW_ATE_unsigned;
10275 else
10276 encoding = DW_ATE_signed;
10277 break;
10279 case REAL_TYPE:
10280 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10282 if (dwarf_version >= 3 || !dwarf_strict)
10283 encoding = DW_ATE_decimal_float;
10284 else
10285 encoding = DW_ATE_lo_user;
10287 else
10288 encoding = DW_ATE_float;
10289 break;
10291 case FIXED_POINT_TYPE:
10292 if (!(dwarf_version >= 3 || !dwarf_strict))
10293 encoding = DW_ATE_lo_user;
10294 else if (TYPE_UNSIGNED (type))
10295 encoding = DW_ATE_unsigned_fixed;
10296 else
10297 encoding = DW_ATE_signed_fixed;
10298 break;
10300 /* Dwarf2 doesn't know anything about complex ints, so use
10301 a user defined type for it. */
10302 case COMPLEX_TYPE:
10303 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10304 encoding = DW_ATE_complex_float;
10305 else
10306 encoding = DW_ATE_lo_user;
10307 break;
10309 case BOOLEAN_TYPE:
10310 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10311 encoding = DW_ATE_boolean;
10312 break;
10314 default:
10315 /* No other TREE_CODEs are Dwarf fundamental types. */
10316 gcc_unreachable ();
10319 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10321 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10322 int_size_in_bytes (type));
10323 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10324 add_pubtype (type, base_type_result);
10326 return base_type_result;
10329 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10330 named 'auto' in its type: return true for it, false otherwise. */
10332 static inline bool
10333 is_cxx_auto (tree type)
10335 if (is_cxx ())
10337 tree name = TYPE_IDENTIFIER (type);
10338 if (name == get_identifier ("auto")
10339 || name == get_identifier ("decltype(auto)"))
10340 return true;
10342 return false;
10345 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10346 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10348 static inline int
10349 is_base_type (tree type)
10351 switch (TREE_CODE (type))
10353 case ERROR_MARK:
10354 case VOID_TYPE:
10355 case INTEGER_TYPE:
10356 case REAL_TYPE:
10357 case FIXED_POINT_TYPE:
10358 case COMPLEX_TYPE:
10359 case BOOLEAN_TYPE:
10360 return 1;
10362 case ARRAY_TYPE:
10363 case RECORD_TYPE:
10364 case UNION_TYPE:
10365 case QUAL_UNION_TYPE:
10366 case ENUMERAL_TYPE:
10367 case FUNCTION_TYPE:
10368 case METHOD_TYPE:
10369 case POINTER_TYPE:
10370 case REFERENCE_TYPE:
10371 case NULLPTR_TYPE:
10372 case OFFSET_TYPE:
10373 case LANG_TYPE:
10374 case VECTOR_TYPE:
10375 return 0;
10377 default:
10378 if (is_cxx_auto (type))
10379 return 0;
10380 gcc_unreachable ();
10383 return 0;
10386 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10387 node, return the size in bits for the type if it is a constant, or else
10388 return the alignment for the type if the type's size is not constant, or
10389 else return BITS_PER_WORD if the type actually turns out to be an
10390 ERROR_MARK node. */
10392 static inline unsigned HOST_WIDE_INT
10393 simple_type_size_in_bits (const_tree type)
10395 if (TREE_CODE (type) == ERROR_MARK)
10396 return BITS_PER_WORD;
10397 else if (TYPE_SIZE (type) == NULL_TREE)
10398 return 0;
10399 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10400 return tree_to_uhwi (TYPE_SIZE (type));
10401 else
10402 return TYPE_ALIGN (type);
10405 /* Similarly, but return an offset_int instead of UHWI. */
10407 static inline offset_int
10408 offset_int_type_size_in_bits (const_tree type)
10410 if (TREE_CODE (type) == ERROR_MARK)
10411 return BITS_PER_WORD;
10412 else if (TYPE_SIZE (type) == NULL_TREE)
10413 return 0;
10414 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10415 return wi::to_offset (TYPE_SIZE (type));
10416 else
10417 return TYPE_ALIGN (type);
10420 /* Given a pointer to a tree node for a subrange type, return a pointer
10421 to a DIE that describes the given type. */
10423 static dw_die_ref
10424 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10426 dw_die_ref subrange_die;
10427 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10429 if (context_die == NULL)
10430 context_die = comp_unit_die ();
10432 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10434 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10436 /* The size of the subrange type and its base type do not match,
10437 so we need to generate a size attribute for the subrange type. */
10438 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10441 if (low)
10442 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10443 if (high)
10444 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10446 return subrange_die;
10449 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10450 entry that chains various modifiers in front of the given type. */
10452 static dw_die_ref
10453 modified_type_die (tree type, int type_quals,
10454 dw_die_ref context_die)
10456 const int is_const_type = ((type_quals & TYPE_QUAL_CONST) != 0);
10457 const int is_volatile_type = ((type_quals & TYPE_QUAL_VOLATILE) != 0);
10458 enum tree_code code = TREE_CODE (type);
10459 dw_die_ref mod_type_die;
10460 dw_die_ref sub_die = NULL;
10461 tree item_type = NULL;
10462 tree qualified_type;
10463 tree name, low, high;
10464 dw_die_ref mod_scope;
10466 if (code == ERROR_MARK)
10467 return NULL;
10469 /* See if we already have the appropriately qualified variant of
10470 this type. */
10471 qualified_type
10472 = get_qualified_type (type, type_quals);
10474 if (qualified_type == sizetype
10475 && TYPE_NAME (qualified_type)
10476 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10478 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10480 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10481 && TYPE_PRECISION (t)
10482 == TYPE_PRECISION (qualified_type)
10483 && TYPE_UNSIGNED (t)
10484 == TYPE_UNSIGNED (qualified_type));
10485 qualified_type = t;
10488 /* If we do, then we can just use its DIE, if it exists. */
10489 if (qualified_type)
10491 mod_type_die = lookup_type_die (qualified_type);
10492 if (mod_type_die)
10493 return mod_type_die;
10496 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10498 /* Handle C typedef types. */
10499 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10500 && !DECL_ARTIFICIAL (name))
10502 tree dtype = TREE_TYPE (name);
10504 if (qualified_type == dtype)
10506 /* For a named type, use the typedef. */
10507 gen_type_die (qualified_type, context_die);
10508 return lookup_type_die (qualified_type);
10510 else if (type_quals == TYPE_QUALS (dtype))
10511 /* cv-unqualified version of named type. Just use the unnamed
10512 type to which it refers. */
10513 return modified_type_die (DECL_ORIGINAL_TYPE (name), type_quals, context_die);
10514 /* Else cv-qualified version of named type; fall through. */
10517 mod_scope = scope_die_for (type, context_die);
10519 if ((type_quals & TYPE_QUAL_CONST)
10520 /* If both is_const_type and is_volatile_type, prefer the path
10521 which leads to a qualified type. */
10522 && (!is_volatile_type
10523 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10524 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10526 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10527 sub_die = modified_type_die (type, type_quals & ~TYPE_QUAL_CONST, context_die);
10529 else if (type_quals & TYPE_QUAL_VOLATILE)
10531 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10532 sub_die = modified_type_die (type, type_quals & ~TYPE_QUAL_VOLATILE, context_die);
10534 else if (use_upc_dwarf2_extensions
10535 && (type_quals & TYPE_QUAL_UPC_SHARED))
10537 HOST_WIDE_INT block_factor = 1;
10539 /* Inside the compiler,
10540 "shared int x;" TYPE_UPC_BLOCK_FACTOR is null.
10541 "shared [] int *p;" TYPE_UPC_BLOCK_FACTOR is zero.
10542 "shared [10] int x[50];" TYPE_UPC_BLOCK_FACTOR is 10 * bitsize(int)
10543 The DWARF2 encoding is as follows:
10544 "shared int x;" DW_AT_count: 1
10545 "shared [] int *p;" <no DW_AT_count attribute>
10546 "shared [10] int x[50];" DW_AT_count: 10
10547 The logic below handles thse various contingencies. */
10549 mod_type_die = new_die (DW_TAG_upc_shared_type,
10550 comp_unit_die (), type);
10552 if (TYPE_HAS_UPC_BLOCK_FACTOR (type))
10553 block_factor = TREE_INT_CST_LOW (TYPE_UPC_BLOCK_FACTOR (type));
10555 if (block_factor != 0)
10556 add_AT_unsigned (mod_type_die, DW_AT_count, block_factor);
10558 sub_die = modified_type_die (type,
10559 type_quals & ~TYPE_QUAL_UPC_SHARED,
10560 context_die);
10562 else if (use_upc_dwarf2_extensions && type_quals & TYPE_QUAL_UPC_STRICT)
10564 mod_type_die = new_die (DW_TAG_upc_strict_type,
10565 comp_unit_die (), type);
10566 sub_die = modified_type_die (type,
10567 type_quals & ~TYPE_QUAL_UPC_STRICT,
10568 context_die);
10570 else if (use_upc_dwarf2_extensions && type_quals & TYPE_QUAL_UPC_RELAXED)
10572 mod_type_die = new_die (DW_TAG_upc_relaxed_type,
10573 comp_unit_die (), type);
10574 sub_die = modified_type_die (type,
10575 type_quals & ~TYPE_QUAL_UPC_RELAXED,
10576 context_die);
10578 else if (code == POINTER_TYPE)
10580 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10581 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10582 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10583 item_type = TREE_TYPE (type);
10584 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10585 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10586 TYPE_ADDR_SPACE (item_type));
10588 else if (code == REFERENCE_TYPE)
10590 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10591 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10592 type);
10593 else
10594 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10595 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10596 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10597 item_type = TREE_TYPE (type);
10598 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10599 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10600 TYPE_ADDR_SPACE (item_type));
10602 else if (code == INTEGER_TYPE
10603 && TREE_TYPE (type) != NULL_TREE
10604 && subrange_type_for_debug_p (type, &low, &high))
10606 mod_type_die = subrange_type_die (type, low, high, context_die);
10607 item_type = TREE_TYPE (type);
10609 else if (is_base_type (type))
10610 mod_type_die = base_type_die (type);
10611 else
10613 gen_type_die (type, context_die);
10615 /* We have to get the type_main_variant here (and pass that to the
10616 `lookup_type_die' routine) because the ..._TYPE node we have
10617 might simply be a *copy* of some original type node (where the
10618 copy was created to help us keep track of typedef names) and
10619 that copy might have a different TYPE_UID from the original
10620 ..._TYPE node. */
10621 if (TREE_CODE (type) != VECTOR_TYPE)
10622 return lookup_type_die (type_main_variant (type));
10623 else
10624 /* Vectors have the debugging information in the type,
10625 not the main variant. */
10626 return lookup_type_die (type);
10629 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10630 don't output a DW_TAG_typedef, since there isn't one in the
10631 user's program; just attach a DW_AT_name to the type.
10632 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10633 if the base type already has the same name. */
10634 if (name
10635 && ((TREE_CODE (name) != TYPE_DECL
10636 && (qualified_type == TYPE_MAIN_VARIANT (type)
10637 || (!is_const_type && !is_volatile_type)))
10638 || (TREE_CODE (name) == TYPE_DECL
10639 && TREE_TYPE (name) == qualified_type
10640 && DECL_NAME (name))))
10642 if (TREE_CODE (name) == TYPE_DECL)
10643 /* Could just call add_name_and_src_coords_attributes here,
10644 but since this is a builtin type it doesn't have any
10645 useful source coordinates anyway. */
10646 name = DECL_NAME (name);
10647 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10649 /* This probably indicates a bug. */
10650 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10652 name = TYPE_IDENTIFIER (type);
10653 add_name_attribute (mod_type_die,
10654 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10657 if (qualified_type)
10658 equate_type_number_to_die (qualified_type, mod_type_die);
10660 if (item_type)
10661 /* We must do this after the equate_type_number_to_die call, in case
10662 this is a recursive type. This ensures that the modified_type_die
10663 recursion will terminate even if the type is recursive. Recursive
10664 types are possible in Ada. */
10665 sub_die = modified_type_die (item_type, TYPE_QUALS (item_type), context_die);
10667 if (sub_die != NULL)
10668 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10670 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10671 if (TYPE_ARTIFICIAL (type))
10672 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10674 return mod_type_die;
10677 /* Generate DIEs for the generic parameters of T.
10678 T must be either a generic type or a generic function.
10679 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10681 static void
10682 gen_generic_params_dies (tree t)
10684 tree parms, args;
10685 int parms_num, i;
10686 dw_die_ref die = NULL;
10687 int non_default;
10689 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10690 return;
10692 if (TYPE_P (t))
10693 die = lookup_type_die (t);
10694 else if (DECL_P (t))
10695 die = lookup_decl_die (t);
10697 gcc_assert (die);
10699 parms = lang_hooks.get_innermost_generic_parms (t);
10700 if (!parms)
10701 /* T has no generic parameter. It means T is neither a generic type
10702 or function. End of story. */
10703 return;
10705 parms_num = TREE_VEC_LENGTH (parms);
10706 args = lang_hooks.get_innermost_generic_args (t);
10707 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10708 non_default = int_cst_value (TREE_CHAIN (args));
10709 else
10710 non_default = TREE_VEC_LENGTH (args);
10711 for (i = 0; i < parms_num; i++)
10713 tree parm, arg, arg_pack_elems;
10714 dw_die_ref parm_die;
10716 parm = TREE_VEC_ELT (parms, i);
10717 arg = TREE_VEC_ELT (args, i);
10718 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10719 gcc_assert (parm && TREE_VALUE (parm) && arg);
10721 if (parm && TREE_VALUE (parm) && arg)
10723 /* If PARM represents a template parameter pack,
10724 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10725 by DW_TAG_template_*_parameter DIEs for the argument
10726 pack elements of ARG. Note that ARG would then be
10727 an argument pack. */
10728 if (arg_pack_elems)
10729 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10730 arg_pack_elems,
10731 die);
10732 else
10733 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10734 true /* emit name */, die);
10735 if (i >= non_default)
10736 add_AT_flag (parm_die, DW_AT_default_value, 1);
10741 /* Create and return a DIE for PARM which should be
10742 the representation of a generic type parameter.
10743 For instance, in the C++ front end, PARM would be a template parameter.
10744 ARG is the argument to PARM.
10745 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10746 name of the PARM.
10747 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10748 as a child node. */
10750 static dw_die_ref
10751 generic_parameter_die (tree parm, tree arg,
10752 bool emit_name_p,
10753 dw_die_ref parent_die)
10755 dw_die_ref tmpl_die = NULL;
10756 const char *name = NULL;
10758 if (!parm || !DECL_NAME (parm) || !arg)
10759 return NULL;
10761 /* We support non-type generic parameters and arguments,
10762 type generic parameters and arguments, as well as
10763 generic generic parameters (a.k.a. template template parameters in C++)
10764 and arguments. */
10765 if (TREE_CODE (parm) == PARM_DECL)
10766 /* PARM is a nontype generic parameter */
10767 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10768 else if (TREE_CODE (parm) == TYPE_DECL)
10769 /* PARM is a type generic parameter. */
10770 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10771 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10772 /* PARM is a generic generic parameter.
10773 Its DIE is a GNU extension. It shall have a
10774 DW_AT_name attribute to represent the name of the template template
10775 parameter, and a DW_AT_GNU_template_name attribute to represent the
10776 name of the template template argument. */
10777 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10778 parent_die, parm);
10779 else
10780 gcc_unreachable ();
10782 if (tmpl_die)
10784 tree tmpl_type;
10786 /* If PARM is a generic parameter pack, it means we are
10787 emitting debug info for a template argument pack element.
10788 In other terms, ARG is a template argument pack element.
10789 In that case, we don't emit any DW_AT_name attribute for
10790 the die. */
10791 if (emit_name_p)
10793 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10794 gcc_assert (name);
10795 add_AT_string (tmpl_die, DW_AT_name, name);
10798 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10800 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10801 TMPL_DIE should have a child DW_AT_type attribute that is set
10802 to the type of the argument to PARM, which is ARG.
10803 If PARM is a type generic parameter, TMPL_DIE should have a
10804 child DW_AT_type that is set to ARG. */
10805 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10806 add_type_attribute (tmpl_die, tmpl_type, 0,
10807 TREE_THIS_VOLATILE (tmpl_type),
10808 parent_die);
10810 else
10812 /* So TMPL_DIE is a DIE representing a
10813 a generic generic template parameter, a.k.a template template
10814 parameter in C++ and arg is a template. */
10816 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10817 to the name of the argument. */
10818 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10819 if (name)
10820 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10823 if (TREE_CODE (parm) == PARM_DECL)
10824 /* So PARM is a non-type generic parameter.
10825 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10826 attribute of TMPL_DIE which value represents the value
10827 of ARG.
10828 We must be careful here:
10829 The value of ARG might reference some function decls.
10830 We might currently be emitting debug info for a generic
10831 type and types are emitted before function decls, we don't
10832 know if the function decls referenced by ARG will actually be
10833 emitted after cgraph computations.
10834 So must defer the generation of the DW_AT_const_value to
10835 after cgraph is ready. */
10836 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10839 return tmpl_die;
10842 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10843 PARM_PACK must be a template parameter pack. The returned DIE
10844 will be child DIE of PARENT_DIE. */
10846 static dw_die_ref
10847 template_parameter_pack_die (tree parm_pack,
10848 tree parm_pack_args,
10849 dw_die_ref parent_die)
10851 dw_die_ref die;
10852 int j;
10854 gcc_assert (parent_die && parm_pack);
10856 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10857 add_name_and_src_coords_attributes (die, parm_pack);
10858 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10859 generic_parameter_die (parm_pack,
10860 TREE_VEC_ELT (parm_pack_args, j),
10861 false /* Don't emit DW_AT_name */,
10862 die);
10863 return die;
10866 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10867 an enumerated type. */
10869 static inline int
10870 type_is_enum (const_tree type)
10872 return TREE_CODE (type) == ENUMERAL_TYPE;
10875 /* Return the DBX register number described by a given RTL node. */
10877 static unsigned int
10878 dbx_reg_number (const_rtx rtl)
10880 unsigned regno = REGNO (rtl);
10882 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10884 #ifdef LEAF_REG_REMAP
10885 if (crtl->uses_only_leaf_regs)
10887 int leaf_reg = LEAF_REG_REMAP (regno);
10888 if (leaf_reg != -1)
10889 regno = (unsigned) leaf_reg;
10891 #endif
10893 regno = DBX_REGISTER_NUMBER (regno);
10894 gcc_assert (regno != INVALID_REGNUM);
10895 return regno;
10898 /* Optionally add a DW_OP_piece term to a location description expression.
10899 DW_OP_piece is only added if the location description expression already
10900 doesn't end with DW_OP_piece. */
10902 static void
10903 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10905 dw_loc_descr_ref loc;
10907 if (*list_head != NULL)
10909 /* Find the end of the chain. */
10910 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10913 if (loc->dw_loc_opc != DW_OP_piece)
10914 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10918 /* Return a location descriptor that designates a machine register or
10919 zero if there is none. */
10921 static dw_loc_descr_ref
10922 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10924 rtx regs;
10926 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10927 return 0;
10929 /* We only use "frame base" when we're sure we're talking about the
10930 post-prologue local stack frame. We do this by *not* running
10931 register elimination until this point, and recognizing the special
10932 argument pointer and soft frame pointer rtx's.
10933 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10934 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10935 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10937 dw_loc_descr_ref result = NULL;
10939 if (dwarf_version >= 4 || !dwarf_strict)
10941 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10942 initialized);
10943 if (result)
10944 add_loc_descr (&result,
10945 new_loc_descr (DW_OP_stack_value, 0, 0));
10947 return result;
10950 regs = targetm.dwarf_register_span (rtl);
10952 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10953 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10954 else
10956 unsigned int dbx_regnum = dbx_reg_number (rtl);
10957 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10958 return 0;
10959 return one_reg_loc_descriptor (dbx_regnum, initialized);
10963 /* Return a location descriptor that designates a machine register for
10964 a given hard register number. */
10966 static dw_loc_descr_ref
10967 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10969 dw_loc_descr_ref reg_loc_descr;
10971 if (regno <= 31)
10972 reg_loc_descr
10973 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10974 else
10975 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10977 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10978 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10980 return reg_loc_descr;
10983 /* Given an RTL of a register, return a location descriptor that
10984 designates a value that spans more than one register. */
10986 static dw_loc_descr_ref
10987 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10988 enum var_init_status initialized)
10990 int size, i;
10991 dw_loc_descr_ref loc_result = NULL;
10993 /* Simple, contiguous registers. */
10994 if (regs == NULL_RTX)
10996 unsigned reg = REGNO (rtl);
10997 int nregs;
10999 #ifdef LEAF_REG_REMAP
11000 if (crtl->uses_only_leaf_regs)
11002 int leaf_reg = LEAF_REG_REMAP (reg);
11003 if (leaf_reg != -1)
11004 reg = (unsigned) leaf_reg;
11006 #endif
11008 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11009 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11011 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11013 loc_result = NULL;
11014 while (nregs--)
11016 dw_loc_descr_ref t;
11018 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11019 VAR_INIT_STATUS_INITIALIZED);
11020 add_loc_descr (&loc_result, t);
11021 add_loc_descr_op_piece (&loc_result, size);
11022 ++reg;
11024 return loc_result;
11027 /* Now onto stupid register sets in non contiguous locations. */
11029 gcc_assert (GET_CODE (regs) == PARALLEL);
11031 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11032 loc_result = NULL;
11034 for (i = 0; i < XVECLEN (regs, 0); ++i)
11036 dw_loc_descr_ref t;
11038 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11039 VAR_INIT_STATUS_INITIALIZED);
11040 add_loc_descr (&loc_result, t);
11041 add_loc_descr_op_piece (&loc_result, size);
11044 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11045 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11046 return loc_result;
11049 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11051 /* Return a location descriptor that designates a constant i,
11052 as a compound operation from constant (i >> shift), constant shift
11053 and DW_OP_shl. */
11055 static dw_loc_descr_ref
11056 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11058 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11059 add_loc_descr (&ret, int_loc_descriptor (shift));
11060 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11061 return ret;
11064 /* Return a location descriptor that designates a constant. */
11066 static dw_loc_descr_ref
11067 int_loc_descriptor (HOST_WIDE_INT i)
11069 enum dwarf_location_atom op;
11071 /* Pick the smallest representation of a constant, rather than just
11072 defaulting to the LEB encoding. */
11073 if (i >= 0)
11075 int clz = clz_hwi (i);
11076 int ctz = ctz_hwi (i);
11077 if (i <= 31)
11078 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11079 else if (i <= 0xff)
11080 op = DW_OP_const1u;
11081 else if (i <= 0xffff)
11082 op = DW_OP_const2u;
11083 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11084 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11085 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11086 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11087 while DW_OP_const4u is 5 bytes. */
11088 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11089 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11090 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11091 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11092 while DW_OP_const4u is 5 bytes. */
11093 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11094 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11095 op = DW_OP_const4u;
11096 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11097 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11098 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11099 while DW_OP_constu of constant >= 0x100000000 takes at least
11100 6 bytes. */
11101 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11102 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11103 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11104 >= HOST_BITS_PER_WIDE_INT)
11105 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11106 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11107 while DW_OP_constu takes in this case at least 6 bytes. */
11108 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11109 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11110 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11111 && size_of_uleb128 (i) > 6)
11112 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11113 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11114 else
11115 op = DW_OP_constu;
11117 else
11119 if (i >= -0x80)
11120 op = DW_OP_const1s;
11121 else if (i >= -0x8000)
11122 op = DW_OP_const2s;
11123 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11125 if (size_of_int_loc_descriptor (i) < 5)
11127 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11128 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11129 return ret;
11131 op = DW_OP_const4s;
11133 else
11135 if (size_of_int_loc_descriptor (i)
11136 < (unsigned long) 1 + size_of_sleb128 (i))
11138 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11139 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11140 return ret;
11142 op = DW_OP_consts;
11146 return new_loc_descr (op, i, 0);
11149 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11150 without actually allocating it. */
11152 static unsigned long
11153 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11155 return size_of_int_loc_descriptor (i >> shift)
11156 + size_of_int_loc_descriptor (shift)
11157 + 1;
11160 /* Return size_of_locs (int_loc_descriptor (i)) without
11161 actually allocating it. */
11163 static unsigned long
11164 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11166 unsigned long s;
11168 if (i >= 0)
11170 int clz, ctz;
11171 if (i <= 31)
11172 return 1;
11173 else if (i <= 0xff)
11174 return 2;
11175 else if (i <= 0xffff)
11176 return 3;
11177 clz = clz_hwi (i);
11178 ctz = ctz_hwi (i);
11179 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11180 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11181 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11182 - clz - 5);
11183 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11184 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11185 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11186 - clz - 8);
11187 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11188 return 5;
11189 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11190 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11191 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11192 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11193 - clz - 8);
11194 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11195 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11196 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11197 - clz - 16);
11198 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11199 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11200 && s > 6)
11201 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11202 - clz - 32);
11203 else
11204 return 1 + s;
11206 else
11208 if (i >= -0x80)
11209 return 2;
11210 else if (i >= -0x8000)
11211 return 3;
11212 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11214 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11216 s = size_of_int_loc_descriptor (-i) + 1;
11217 if (s < 5)
11218 return s;
11220 return 5;
11222 else
11224 unsigned long r = 1 + size_of_sleb128 (i);
11225 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11227 s = size_of_int_loc_descriptor (-i) + 1;
11228 if (s < r)
11229 return s;
11231 return r;
11236 /* Return loc description representing "address" of integer value.
11237 This can appear only as toplevel expression. */
11239 static dw_loc_descr_ref
11240 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11242 int litsize;
11243 dw_loc_descr_ref loc_result = NULL;
11245 if (!(dwarf_version >= 4 || !dwarf_strict))
11246 return NULL;
11248 litsize = size_of_int_loc_descriptor (i);
11249 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11250 is more compact. For DW_OP_stack_value we need:
11251 litsize + 1 (DW_OP_stack_value)
11252 and for DW_OP_implicit_value:
11253 1 (DW_OP_implicit_value) + 1 (length) + size. */
11254 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11256 loc_result = int_loc_descriptor (i);
11257 add_loc_descr (&loc_result,
11258 new_loc_descr (DW_OP_stack_value, 0, 0));
11259 return loc_result;
11262 loc_result = new_loc_descr (DW_OP_implicit_value,
11263 size, 0);
11264 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11265 loc_result->dw_loc_oprnd2.v.val_int = i;
11266 return loc_result;
11269 /* Return a location descriptor that designates a base+offset location. */
11271 static dw_loc_descr_ref
11272 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11273 enum var_init_status initialized)
11275 unsigned int regno;
11276 dw_loc_descr_ref result;
11277 dw_fde_ref fde = cfun->fde;
11279 /* We only use "frame base" when we're sure we're talking about the
11280 post-prologue local stack frame. We do this by *not* running
11281 register elimination until this point, and recognizing the special
11282 argument pointer and soft frame pointer rtx's. */
11283 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11285 rtx elim = (ira_use_lra_p
11286 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11287 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11289 if (elim != reg)
11291 if (GET_CODE (elim) == PLUS)
11293 offset += INTVAL (XEXP (elim, 1));
11294 elim = XEXP (elim, 0);
11296 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11297 && (elim == hard_frame_pointer_rtx
11298 || elim == stack_pointer_rtx))
11299 || elim == (frame_pointer_needed
11300 ? hard_frame_pointer_rtx
11301 : stack_pointer_rtx));
11303 /* If drap register is used to align stack, use frame
11304 pointer + offset to access stack variables. If stack
11305 is aligned without drap, use stack pointer + offset to
11306 access stack variables. */
11307 if (crtl->stack_realign_tried
11308 && reg == frame_pointer_rtx)
11310 int base_reg
11311 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11312 ? HARD_FRAME_POINTER_REGNUM
11313 : REGNO (elim));
11314 return new_reg_loc_descr (base_reg, offset);
11317 gcc_assert (frame_pointer_fb_offset_valid);
11318 offset += frame_pointer_fb_offset;
11319 return new_loc_descr (DW_OP_fbreg, offset, 0);
11323 regno = REGNO (reg);
11324 #ifdef LEAF_REG_REMAP
11325 if (crtl->uses_only_leaf_regs)
11327 int leaf_reg = LEAF_REG_REMAP (regno);
11328 if (leaf_reg != -1)
11329 regno = (unsigned) leaf_reg;
11331 #endif
11332 regno = DWARF_FRAME_REGNUM (regno);
11334 if (!optimize && fde
11335 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11337 /* Use cfa+offset to represent the location of arguments passed
11338 on the stack when drap is used to align stack.
11339 Only do this when not optimizing, for optimized code var-tracking
11340 is supposed to track where the arguments live and the register
11341 used as vdrap or drap in some spot might be used for something
11342 else in other part of the routine. */
11343 return new_loc_descr (DW_OP_fbreg, offset, 0);
11346 if (regno <= 31)
11347 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11348 offset, 0);
11349 else
11350 result = new_loc_descr (DW_OP_bregx, regno, offset);
11352 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11353 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11355 return result;
11358 /* Return true if this RTL expression describes a base+offset calculation. */
11360 static inline int
11361 is_based_loc (const_rtx rtl)
11363 return (GET_CODE (rtl) == PLUS
11364 && ((REG_P (XEXP (rtl, 0))
11365 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11366 && CONST_INT_P (XEXP (rtl, 1)))));
11369 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11370 failed. */
11372 static dw_loc_descr_ref
11373 tls_mem_loc_descriptor (rtx mem)
11375 tree base;
11376 dw_loc_descr_ref loc_result;
11378 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11379 return NULL;
11381 base = get_base_address (MEM_EXPR (mem));
11382 if (base == NULL
11383 || TREE_CODE (base) != VAR_DECL
11384 || !DECL_THREAD_LOCAL_P (base))
11385 return NULL;
11387 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11388 if (loc_result == NULL)
11389 return NULL;
11391 if (MEM_OFFSET (mem))
11392 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11394 return loc_result;
11397 /* Output debug info about reason why we failed to expand expression as dwarf
11398 expression. */
11400 static void
11401 expansion_failed (tree expr, rtx rtl, char const *reason)
11403 if (dump_file && (dump_flags & TDF_DETAILS))
11405 fprintf (dump_file, "Failed to expand as dwarf: ");
11406 if (expr)
11407 print_generic_expr (dump_file, expr, dump_flags);
11408 if (rtl)
11410 fprintf (dump_file, "\n");
11411 print_rtl (dump_file, rtl);
11413 fprintf (dump_file, "\nReason: %s\n", reason);
11417 /* Helper function for const_ok_for_output, called either directly
11418 or via for_each_rtx. */
11420 static int
11421 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11423 rtx rtl = *rtlp;
11425 if (GET_CODE (rtl) == UNSPEC)
11427 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11428 we can't express it in the debug info. */
11429 #ifdef ENABLE_CHECKING
11430 /* Don't complain about TLS UNSPECs, those are just too hard to
11431 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11432 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11433 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11434 if (XVECLEN (rtl, 0) == 0
11435 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11436 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11437 inform (current_function_decl
11438 ? DECL_SOURCE_LOCATION (current_function_decl)
11439 : UNKNOWN_LOCATION,
11440 #if NUM_UNSPEC_VALUES > 0
11441 "non-delegitimized UNSPEC %s (%d) found in variable location",
11442 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11443 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11444 XINT (rtl, 1));
11445 #else
11446 "non-delegitimized UNSPEC %d found in variable location",
11447 XINT (rtl, 1));
11448 #endif
11449 #endif
11450 expansion_failed (NULL_TREE, rtl,
11451 "UNSPEC hasn't been delegitimized.\n");
11452 return 1;
11455 if (targetm.const_not_ok_for_debug_p (rtl))
11457 expansion_failed (NULL_TREE, rtl,
11458 "Expression rejected for debug by the backend.\n");
11459 return 1;
11462 /* FIXME: Refer to PR60655. It is possible for simplification
11463 of rtl expressions in var tracking to produce such expressions.
11464 We should really identify / validate expressions
11465 enclosed in CONST that can be handled by assemblers on various
11466 targets and only handle legitimate cases here. */
11467 if (GET_CODE (rtl) != SYMBOL_REF)
11469 if (GET_CODE (rtl) == NOT)
11470 return 1;
11472 return 0;
11475 if (CONSTANT_POOL_ADDRESS_P (rtl))
11477 bool marked;
11478 get_pool_constant_mark (rtl, &marked);
11479 /* If all references to this pool constant were optimized away,
11480 it was not output and thus we can't represent it. */
11481 if (!marked)
11483 expansion_failed (NULL_TREE, rtl,
11484 "Constant was removed from constant pool.\n");
11485 return 1;
11489 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11490 return 1;
11492 /* Avoid references to external symbols in debug info, on several targets
11493 the linker might even refuse to link when linking a shared library,
11494 and in many other cases the relocations for .debug_info/.debug_loc are
11495 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11496 to be defined within the same shared library or executable are fine. */
11497 if (SYMBOL_REF_EXTERNAL_P (rtl))
11499 tree decl = SYMBOL_REF_DECL (rtl);
11501 if (decl == NULL || !targetm.binds_local_p (decl))
11503 expansion_failed (NULL_TREE, rtl,
11504 "Symbol not defined in current TU.\n");
11505 return 1;
11509 return 0;
11512 /* Return true if constant RTL can be emitted in DW_OP_addr or
11513 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11514 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11516 static bool
11517 const_ok_for_output (rtx rtl)
11519 if (GET_CODE (rtl) == SYMBOL_REF)
11520 return const_ok_for_output_1 (&rtl, NULL) == 0;
11522 if (GET_CODE (rtl) == CONST)
11523 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11525 return true;
11528 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11529 if possible, NULL otherwise. */
11531 static dw_die_ref
11532 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11534 dw_die_ref type_die;
11535 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11537 if (type == NULL)
11538 return NULL;
11539 switch (TREE_CODE (type))
11541 case INTEGER_TYPE:
11542 case REAL_TYPE:
11543 break;
11544 default:
11545 return NULL;
11547 type_die = lookup_type_die (type);
11548 if (!type_die)
11549 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11550 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11551 return NULL;
11552 return type_die;
11555 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11556 type matching MODE, or, if MODE is narrower than or as wide as
11557 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11558 possible. */
11560 static dw_loc_descr_ref
11561 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11563 enum machine_mode outer_mode = mode;
11564 dw_die_ref type_die;
11565 dw_loc_descr_ref cvt;
11567 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11569 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11570 return op;
11572 type_die = base_type_for_mode (outer_mode, 1);
11573 if (type_die == NULL)
11574 return NULL;
11575 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11576 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11577 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11578 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11579 add_loc_descr (&op, cvt);
11580 return op;
11583 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11585 static dw_loc_descr_ref
11586 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11587 dw_loc_descr_ref op1)
11589 dw_loc_descr_ref ret = op0;
11590 add_loc_descr (&ret, op1);
11591 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11592 if (STORE_FLAG_VALUE != 1)
11594 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11595 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11597 return ret;
11600 /* Return location descriptor for signed comparison OP RTL. */
11602 static dw_loc_descr_ref
11603 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11604 enum machine_mode mem_mode)
11606 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11607 dw_loc_descr_ref op0, op1;
11608 int shift;
11610 if (op_mode == VOIDmode)
11611 op_mode = GET_MODE (XEXP (rtl, 1));
11612 if (op_mode == VOIDmode)
11613 return NULL;
11615 if (dwarf_strict
11616 && (GET_MODE_CLASS (op_mode) != MODE_INT
11617 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11618 return NULL;
11620 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11621 VAR_INIT_STATUS_INITIALIZED);
11622 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11623 VAR_INIT_STATUS_INITIALIZED);
11625 if (op0 == NULL || op1 == NULL)
11626 return NULL;
11628 if (GET_MODE_CLASS (op_mode) != MODE_INT
11629 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11630 return compare_loc_descriptor (op, op0, op1);
11632 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11634 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11635 dw_loc_descr_ref cvt;
11637 if (type_die == NULL)
11638 return NULL;
11639 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11640 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11641 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11642 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11643 add_loc_descr (&op0, cvt);
11644 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11645 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11646 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11647 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11648 add_loc_descr (&op1, cvt);
11649 return compare_loc_descriptor (op, op0, op1);
11652 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11653 /* For eq/ne, if the operands are known to be zero-extended,
11654 there is no need to do the fancy shifting up. */
11655 if (op == DW_OP_eq || op == DW_OP_ne)
11657 dw_loc_descr_ref last0, last1;
11658 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11660 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11662 /* deref_size zero extends, and for constants we can check
11663 whether they are zero extended or not. */
11664 if (((last0->dw_loc_opc == DW_OP_deref_size
11665 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11666 || (CONST_INT_P (XEXP (rtl, 0))
11667 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11668 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11669 && ((last1->dw_loc_opc == DW_OP_deref_size
11670 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11671 || (CONST_INT_P (XEXP (rtl, 1))
11672 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11673 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11674 return compare_loc_descriptor (op, op0, op1);
11676 /* EQ/NE comparison against constant in narrower type than
11677 DWARF2_ADDR_SIZE can be performed either as
11678 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11679 DW_OP_{eq,ne}
11681 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11682 DW_OP_{eq,ne}. Pick whatever is shorter. */
11683 if (CONST_INT_P (XEXP (rtl, 1))
11684 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11685 && (size_of_int_loc_descriptor (shift) + 1
11686 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11687 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11688 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11689 & GET_MODE_MASK (op_mode))))
11691 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11692 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11693 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11694 & GET_MODE_MASK (op_mode));
11695 return compare_loc_descriptor (op, op0, op1);
11698 add_loc_descr (&op0, int_loc_descriptor (shift));
11699 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11700 if (CONST_INT_P (XEXP (rtl, 1)))
11701 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11702 else
11704 add_loc_descr (&op1, int_loc_descriptor (shift));
11705 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11707 return compare_loc_descriptor (op, op0, op1);
11710 /* Return location descriptor for unsigned comparison OP RTL. */
11712 static dw_loc_descr_ref
11713 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11714 enum machine_mode mem_mode)
11716 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11717 dw_loc_descr_ref op0, op1;
11719 if (op_mode == VOIDmode)
11720 op_mode = GET_MODE (XEXP (rtl, 1));
11721 if (op_mode == VOIDmode)
11722 return NULL;
11723 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11724 return NULL;
11726 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11727 return NULL;
11729 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11730 VAR_INIT_STATUS_INITIALIZED);
11731 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11732 VAR_INIT_STATUS_INITIALIZED);
11734 if (op0 == NULL || op1 == NULL)
11735 return NULL;
11737 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11739 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11740 dw_loc_descr_ref last0, last1;
11741 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11743 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11745 if (CONST_INT_P (XEXP (rtl, 0)))
11746 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11747 /* deref_size zero extends, so no need to mask it again. */
11748 else if (last0->dw_loc_opc != DW_OP_deref_size
11749 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11751 add_loc_descr (&op0, int_loc_descriptor (mask));
11752 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11754 if (CONST_INT_P (XEXP (rtl, 1)))
11755 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11756 /* deref_size zero extends, so no need to mask it again. */
11757 else if (last1->dw_loc_opc != DW_OP_deref_size
11758 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11760 add_loc_descr (&op1, int_loc_descriptor (mask));
11761 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11764 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11766 HOST_WIDE_INT bias = 1;
11767 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11768 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11769 if (CONST_INT_P (XEXP (rtl, 1)))
11770 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11771 + INTVAL (XEXP (rtl, 1)));
11772 else
11773 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11774 bias, 0));
11776 return compare_loc_descriptor (op, op0, op1);
11779 /* Return location descriptor for {U,S}{MIN,MAX}. */
11781 static dw_loc_descr_ref
11782 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11783 enum machine_mode mem_mode)
11785 enum dwarf_location_atom op;
11786 dw_loc_descr_ref op0, op1, ret;
11787 dw_loc_descr_ref bra_node, drop_node;
11789 if (dwarf_strict
11790 && (GET_MODE_CLASS (mode) != MODE_INT
11791 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11792 return NULL;
11794 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11795 VAR_INIT_STATUS_INITIALIZED);
11796 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11797 VAR_INIT_STATUS_INITIALIZED);
11799 if (op0 == NULL || op1 == NULL)
11800 return NULL;
11802 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11803 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11804 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11805 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11807 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11809 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11810 add_loc_descr (&op0, int_loc_descriptor (mask));
11811 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11812 add_loc_descr (&op1, int_loc_descriptor (mask));
11813 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11815 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11817 HOST_WIDE_INT bias = 1;
11818 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11819 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11820 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11823 else if (GET_MODE_CLASS (mode) == MODE_INT
11824 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11826 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11827 add_loc_descr (&op0, int_loc_descriptor (shift));
11828 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11829 add_loc_descr (&op1, int_loc_descriptor (shift));
11830 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11832 else if (GET_MODE_CLASS (mode) == MODE_INT
11833 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11835 dw_die_ref type_die = base_type_for_mode (mode, 0);
11836 dw_loc_descr_ref cvt;
11837 if (type_die == NULL)
11838 return NULL;
11839 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11840 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11841 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11842 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11843 add_loc_descr (&op0, cvt);
11844 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11845 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11846 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11847 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11848 add_loc_descr (&op1, cvt);
11851 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11852 op = DW_OP_lt;
11853 else
11854 op = DW_OP_gt;
11855 ret = op0;
11856 add_loc_descr (&ret, op1);
11857 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11858 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11859 add_loc_descr (&ret, bra_node);
11860 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11861 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11862 add_loc_descr (&ret, drop_node);
11863 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11864 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11865 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11866 && GET_MODE_CLASS (mode) == MODE_INT
11867 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11868 ret = convert_descriptor_to_mode (mode, ret);
11869 return ret;
11872 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11873 but after converting arguments to type_die, afterwards
11874 convert back to unsigned. */
11876 static dw_loc_descr_ref
11877 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11878 enum machine_mode mode, enum machine_mode mem_mode)
11880 dw_loc_descr_ref cvt, op0, op1;
11882 if (type_die == NULL)
11883 return NULL;
11884 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11885 VAR_INIT_STATUS_INITIALIZED);
11886 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11887 VAR_INIT_STATUS_INITIALIZED);
11888 if (op0 == NULL || op1 == NULL)
11889 return NULL;
11890 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11891 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11892 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11893 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11894 add_loc_descr (&op0, cvt);
11895 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11896 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11897 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11898 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11899 add_loc_descr (&op1, cvt);
11900 add_loc_descr (&op0, op1);
11901 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11902 return convert_descriptor_to_mode (mode, op0);
11905 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11906 const0 is DW_OP_lit0 or corresponding typed constant,
11907 const1 is DW_OP_lit1 or corresponding typed constant
11908 and constMSB is constant with just the MSB bit set
11909 for the mode):
11910 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11911 L1: const0 DW_OP_swap
11912 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11913 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11914 L3: DW_OP_drop
11915 L4: DW_OP_nop
11917 CTZ is similar:
11918 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11919 L1: const0 DW_OP_swap
11920 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11921 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11922 L3: DW_OP_drop
11923 L4: DW_OP_nop
11925 FFS is similar:
11926 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11927 L1: const1 DW_OP_swap
11928 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11929 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11930 L3: DW_OP_drop
11931 L4: DW_OP_nop */
11933 static dw_loc_descr_ref
11934 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11935 enum machine_mode mem_mode)
11937 dw_loc_descr_ref op0, ret, tmp;
11938 HOST_WIDE_INT valv;
11939 dw_loc_descr_ref l1jump, l1label;
11940 dw_loc_descr_ref l2jump, l2label;
11941 dw_loc_descr_ref l3jump, l3label;
11942 dw_loc_descr_ref l4jump, l4label;
11943 rtx msb;
11945 if (GET_MODE_CLASS (mode) != MODE_INT
11946 || GET_MODE (XEXP (rtl, 0)) != mode)
11947 return NULL;
11949 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11950 VAR_INIT_STATUS_INITIALIZED);
11951 if (op0 == NULL)
11952 return NULL;
11953 ret = op0;
11954 if (GET_CODE (rtl) == CLZ)
11956 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11957 valv = GET_MODE_BITSIZE (mode);
11959 else if (GET_CODE (rtl) == FFS)
11960 valv = 0;
11961 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11962 valv = GET_MODE_BITSIZE (mode);
11963 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11964 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11965 add_loc_descr (&ret, l1jump);
11966 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11967 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11968 VAR_INIT_STATUS_INITIALIZED);
11969 if (tmp == NULL)
11970 return NULL;
11971 add_loc_descr (&ret, tmp);
11972 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11973 add_loc_descr (&ret, l4jump);
11974 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11975 ? const1_rtx : const0_rtx,
11976 mode, mem_mode,
11977 VAR_INIT_STATUS_INITIALIZED);
11978 if (l1label == NULL)
11979 return NULL;
11980 add_loc_descr (&ret, l1label);
11981 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11982 l2label = new_loc_descr (DW_OP_dup, 0, 0);
11983 add_loc_descr (&ret, l2label);
11984 if (GET_CODE (rtl) != CLZ)
11985 msb = const1_rtx;
11986 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11987 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11988 << (GET_MODE_BITSIZE (mode) - 1));
11989 else
11990 msb = immed_wide_int_const
11991 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
11992 GET_MODE_PRECISION (mode)), mode);
11993 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11994 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11995 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11996 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11997 else
11998 tmp = mem_loc_descriptor (msb, mode, mem_mode,
11999 VAR_INIT_STATUS_INITIALIZED);
12000 if (tmp == NULL)
12001 return NULL;
12002 add_loc_descr (&ret, tmp);
12003 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12004 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12005 add_loc_descr (&ret, l3jump);
12006 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12007 VAR_INIT_STATUS_INITIALIZED);
12008 if (tmp == NULL)
12009 return NULL;
12010 add_loc_descr (&ret, tmp);
12011 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12012 ? DW_OP_shl : DW_OP_shr, 0, 0));
12013 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12014 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12015 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12016 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12017 add_loc_descr (&ret, l2jump);
12018 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12019 add_loc_descr (&ret, l3label);
12020 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12021 add_loc_descr (&ret, l4label);
12022 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12023 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12024 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12025 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12026 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12027 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12028 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12029 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12030 return ret;
12033 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12034 const1 is DW_OP_lit1 or corresponding typed constant):
12035 const0 DW_OP_swap
12036 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12037 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12038 L2: DW_OP_drop
12040 PARITY is similar:
12041 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12042 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12043 L2: DW_OP_drop */
12045 static dw_loc_descr_ref
12046 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
12047 enum machine_mode mem_mode)
12049 dw_loc_descr_ref op0, ret, tmp;
12050 dw_loc_descr_ref l1jump, l1label;
12051 dw_loc_descr_ref l2jump, l2label;
12053 if (GET_MODE_CLASS (mode) != MODE_INT
12054 || GET_MODE (XEXP (rtl, 0)) != mode)
12055 return NULL;
12057 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12058 VAR_INIT_STATUS_INITIALIZED);
12059 if (op0 == NULL)
12060 return NULL;
12061 ret = op0;
12062 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12063 VAR_INIT_STATUS_INITIALIZED);
12064 if (tmp == NULL)
12065 return NULL;
12066 add_loc_descr (&ret, tmp);
12067 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12068 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12069 add_loc_descr (&ret, l1label);
12070 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12071 add_loc_descr (&ret, l2jump);
12072 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12073 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12074 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12075 VAR_INIT_STATUS_INITIALIZED);
12076 if (tmp == NULL)
12077 return NULL;
12078 add_loc_descr (&ret, tmp);
12079 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12080 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12081 ? DW_OP_plus : DW_OP_xor, 0, 0));
12082 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12083 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12084 VAR_INIT_STATUS_INITIALIZED);
12085 add_loc_descr (&ret, tmp);
12086 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12087 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12088 add_loc_descr (&ret, l1jump);
12089 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12090 add_loc_descr (&ret, l2label);
12091 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12092 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12093 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12094 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12095 return ret;
12098 /* BSWAP (constS is initial shift count, either 56 or 24):
12099 constS const0
12100 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12101 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12102 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12103 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12104 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12106 static dw_loc_descr_ref
12107 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
12108 enum machine_mode mem_mode)
12110 dw_loc_descr_ref op0, ret, tmp;
12111 dw_loc_descr_ref l1jump, l1label;
12112 dw_loc_descr_ref l2jump, l2label;
12114 if (GET_MODE_CLASS (mode) != MODE_INT
12115 || BITS_PER_UNIT != 8
12116 || (GET_MODE_BITSIZE (mode) != 32
12117 && GET_MODE_BITSIZE (mode) != 64))
12118 return NULL;
12120 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12121 VAR_INIT_STATUS_INITIALIZED);
12122 if (op0 == NULL)
12123 return NULL;
12125 ret = op0;
12126 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12127 mode, mem_mode,
12128 VAR_INIT_STATUS_INITIALIZED);
12129 if (tmp == NULL)
12130 return NULL;
12131 add_loc_descr (&ret, tmp);
12132 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12133 VAR_INIT_STATUS_INITIALIZED);
12134 if (tmp == NULL)
12135 return NULL;
12136 add_loc_descr (&ret, tmp);
12137 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12138 add_loc_descr (&ret, l1label);
12139 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12140 mode, mem_mode,
12141 VAR_INIT_STATUS_INITIALIZED);
12142 add_loc_descr (&ret, tmp);
12143 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12144 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12145 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12146 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12147 VAR_INIT_STATUS_INITIALIZED);
12148 if (tmp == NULL)
12149 return NULL;
12150 add_loc_descr (&ret, tmp);
12151 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12152 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12153 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12154 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12155 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12156 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12157 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12158 VAR_INIT_STATUS_INITIALIZED);
12159 add_loc_descr (&ret, tmp);
12160 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12161 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12162 add_loc_descr (&ret, l2jump);
12163 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12164 VAR_INIT_STATUS_INITIALIZED);
12165 add_loc_descr (&ret, tmp);
12166 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12167 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12168 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12169 add_loc_descr (&ret, l1jump);
12170 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12171 add_loc_descr (&ret, l2label);
12172 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12173 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12174 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12175 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12176 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12177 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12178 return ret;
12181 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12182 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12183 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12184 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12186 ROTATERT is similar:
12187 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12188 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12189 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12191 static dw_loc_descr_ref
12192 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12193 enum machine_mode mem_mode)
12195 rtx rtlop1 = XEXP (rtl, 1);
12196 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12197 int i;
12199 if (GET_MODE_CLASS (mode) != MODE_INT)
12200 return NULL;
12202 if (GET_MODE (rtlop1) != VOIDmode
12203 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12204 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12205 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12206 VAR_INIT_STATUS_INITIALIZED);
12207 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12208 VAR_INIT_STATUS_INITIALIZED);
12209 if (op0 == NULL || op1 == NULL)
12210 return NULL;
12211 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12212 for (i = 0; i < 2; i++)
12214 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12215 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12216 mode, mem_mode,
12217 VAR_INIT_STATUS_INITIALIZED);
12218 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12219 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12220 ? DW_OP_const4u
12221 : HOST_BITS_PER_WIDE_INT == 64
12222 ? DW_OP_const8u : DW_OP_constu,
12223 GET_MODE_MASK (mode), 0);
12224 else
12225 mask[i] = NULL;
12226 if (mask[i] == NULL)
12227 return NULL;
12228 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12230 ret = op0;
12231 add_loc_descr (&ret, op1);
12232 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12233 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12234 if (GET_CODE (rtl) == ROTATERT)
12236 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12237 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12238 GET_MODE_BITSIZE (mode), 0));
12240 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12241 if (mask[0] != NULL)
12242 add_loc_descr (&ret, mask[0]);
12243 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12244 if (mask[1] != NULL)
12246 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12247 add_loc_descr (&ret, mask[1]);
12248 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12250 if (GET_CODE (rtl) == ROTATE)
12252 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12253 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12254 GET_MODE_BITSIZE (mode), 0));
12256 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12257 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12258 return ret;
12261 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12262 for DEBUG_PARAMETER_REF RTL. */
12264 static dw_loc_descr_ref
12265 parameter_ref_descriptor (rtx rtl)
12267 dw_loc_descr_ref ret;
12268 dw_die_ref ref;
12270 if (dwarf_strict)
12271 return NULL;
12272 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12273 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12274 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12275 if (ref)
12277 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12278 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12279 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12281 else
12283 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12284 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12286 return ret;
12289 /* The following routine converts the RTL for a variable or parameter
12290 (resident in memory) into an equivalent Dwarf representation of a
12291 mechanism for getting the address of that same variable onto the top of a
12292 hypothetical "address evaluation" stack.
12294 When creating memory location descriptors, we are effectively transforming
12295 the RTL for a memory-resident object into its Dwarf postfix expression
12296 equivalent. This routine recursively descends an RTL tree, turning
12297 it into Dwarf postfix code as it goes.
12299 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12301 MEM_MODE is the mode of the memory reference, needed to handle some
12302 autoincrement addressing modes.
12304 Return 0 if we can't represent the location. */
12306 dw_loc_descr_ref
12307 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12308 enum machine_mode mem_mode,
12309 enum var_init_status initialized)
12311 dw_loc_descr_ref mem_loc_result = NULL;
12312 enum dwarf_location_atom op;
12313 dw_loc_descr_ref op0, op1;
12314 rtx inner = NULL_RTX;
12316 if (mode == VOIDmode)
12317 mode = GET_MODE (rtl);
12319 /* Note that for a dynamically sized array, the location we will generate a
12320 description of here will be the lowest numbered location which is
12321 actually within the array. That's *not* necessarily the same as the
12322 zeroth element of the array. */
12324 rtl = targetm.delegitimize_address (rtl);
12326 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12327 return NULL;
12329 switch (GET_CODE (rtl))
12331 case POST_INC:
12332 case POST_DEC:
12333 case POST_MODIFY:
12334 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12336 case SUBREG:
12337 /* The case of a subreg may arise when we have a local (register)
12338 variable or a formal (register) parameter which doesn't quite fill
12339 up an entire register. For now, just assume that it is
12340 legitimate to make the Dwarf info refer to the whole register which
12341 contains the given subreg. */
12342 if (!subreg_lowpart_p (rtl))
12343 break;
12344 inner = SUBREG_REG (rtl);
12345 case TRUNCATE:
12346 if (inner == NULL_RTX)
12347 inner = XEXP (rtl, 0);
12348 if (GET_MODE_CLASS (mode) == MODE_INT
12349 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12350 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12351 #ifdef POINTERS_EXTEND_UNSIGNED
12352 || (mode == Pmode && mem_mode != VOIDmode)
12353 #endif
12355 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12357 mem_loc_result = mem_loc_descriptor (inner,
12358 GET_MODE (inner),
12359 mem_mode, initialized);
12360 break;
12362 if (dwarf_strict)
12363 break;
12364 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12365 break;
12366 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12367 && (GET_MODE_CLASS (mode) != MODE_INT
12368 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12369 break;
12370 else
12372 dw_die_ref type_die;
12373 dw_loc_descr_ref cvt;
12375 mem_loc_result = mem_loc_descriptor (inner,
12376 GET_MODE (inner),
12377 mem_mode, initialized);
12378 if (mem_loc_result == NULL)
12379 break;
12380 type_die = base_type_for_mode (mode,
12381 GET_MODE_CLASS (mode) == MODE_INT);
12382 if (type_die == NULL)
12384 mem_loc_result = NULL;
12385 break;
12387 if (GET_MODE_SIZE (mode)
12388 != GET_MODE_SIZE (GET_MODE (inner)))
12389 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12390 else
12391 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12392 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12393 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12394 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12395 add_loc_descr (&mem_loc_result, cvt);
12397 break;
12399 case REG:
12400 if (GET_MODE_CLASS (mode) != MODE_INT
12401 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12402 && rtl != arg_pointer_rtx
12403 && rtl != frame_pointer_rtx
12404 #ifdef POINTERS_EXTEND_UNSIGNED
12405 && (mode != Pmode || mem_mode == VOIDmode)
12406 #endif
12409 dw_die_ref type_die;
12410 unsigned int dbx_regnum;
12412 if (dwarf_strict)
12413 break;
12414 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12415 break;
12416 type_die = base_type_for_mode (mode,
12417 GET_MODE_CLASS (mode) == MODE_INT);
12418 if (type_die == NULL)
12419 break;
12421 dbx_regnum = dbx_reg_number (rtl);
12422 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12423 break;
12424 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12425 dbx_regnum, 0);
12426 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12427 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12428 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12429 break;
12431 /* Whenever a register number forms a part of the description of the
12432 method for calculating the (dynamic) address of a memory resident
12433 object, DWARF rules require the register number be referred to as
12434 a "base register". This distinction is not based in any way upon
12435 what category of register the hardware believes the given register
12436 belongs to. This is strictly DWARF terminology we're dealing with
12437 here. Note that in cases where the location of a memory-resident
12438 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12439 OP_CONST (0)) the actual DWARF location descriptor that we generate
12440 may just be OP_BASEREG (basereg). This may look deceptively like
12441 the object in question was allocated to a register (rather than in
12442 memory) so DWARF consumers need to be aware of the subtle
12443 distinction between OP_REG and OP_BASEREG. */
12444 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12445 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12446 else if (stack_realign_drap
12447 && crtl->drap_reg
12448 && crtl->args.internal_arg_pointer == rtl
12449 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12451 /* If RTL is internal_arg_pointer, which has been optimized
12452 out, use DRAP instead. */
12453 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12454 VAR_INIT_STATUS_INITIALIZED);
12456 break;
12458 case SIGN_EXTEND:
12459 case ZERO_EXTEND:
12460 if (GET_MODE_CLASS (mode) != MODE_INT)
12461 break;
12462 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12463 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12464 if (op0 == 0)
12465 break;
12466 else if (GET_CODE (rtl) == ZERO_EXTEND
12467 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12468 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12469 < HOST_BITS_PER_WIDE_INT
12470 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12471 to expand zero extend as two shifts instead of
12472 masking. */
12473 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12475 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12476 mem_loc_result = op0;
12477 add_loc_descr (&mem_loc_result,
12478 int_loc_descriptor (GET_MODE_MASK (imode)));
12479 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12481 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12483 int shift = DWARF2_ADDR_SIZE
12484 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12485 shift *= BITS_PER_UNIT;
12486 if (GET_CODE (rtl) == SIGN_EXTEND)
12487 op = DW_OP_shra;
12488 else
12489 op = DW_OP_shr;
12490 mem_loc_result = op0;
12491 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12492 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12493 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12494 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12496 else if (!dwarf_strict)
12498 dw_die_ref type_die1, type_die2;
12499 dw_loc_descr_ref cvt;
12501 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12502 GET_CODE (rtl) == ZERO_EXTEND);
12503 if (type_die1 == NULL)
12504 break;
12505 type_die2 = base_type_for_mode (mode, 1);
12506 if (type_die2 == NULL)
12507 break;
12508 mem_loc_result = op0;
12509 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12510 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12511 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12512 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12513 add_loc_descr (&mem_loc_result, cvt);
12514 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12515 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12516 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12517 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12518 add_loc_descr (&mem_loc_result, cvt);
12520 break;
12522 case MEM:
12524 rtx new_rtl = avoid_constant_pool_reference (rtl);
12525 if (new_rtl != rtl)
12527 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12528 initialized);
12529 if (mem_loc_result != NULL)
12530 return mem_loc_result;
12533 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12534 get_address_mode (rtl), mode,
12535 VAR_INIT_STATUS_INITIALIZED);
12536 if (mem_loc_result == NULL)
12537 mem_loc_result = tls_mem_loc_descriptor (rtl);
12538 if (mem_loc_result != NULL)
12540 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12541 || GET_MODE_CLASS (mode) != MODE_INT)
12543 dw_die_ref type_die;
12544 dw_loc_descr_ref deref;
12546 if (dwarf_strict)
12547 return NULL;
12548 type_die
12549 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12550 if (type_die == NULL)
12551 return NULL;
12552 deref = new_loc_descr (DW_OP_GNU_deref_type,
12553 GET_MODE_SIZE (mode), 0);
12554 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12555 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12556 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12557 add_loc_descr (&mem_loc_result, deref);
12559 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12560 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12561 else
12562 add_loc_descr (&mem_loc_result,
12563 new_loc_descr (DW_OP_deref_size,
12564 GET_MODE_SIZE (mode), 0));
12566 break;
12568 case LO_SUM:
12569 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12571 case LABEL_REF:
12572 /* Some ports can transform a symbol ref into a label ref, because
12573 the symbol ref is too far away and has to be dumped into a constant
12574 pool. */
12575 case CONST:
12576 case SYMBOL_REF:
12577 if (GET_MODE_CLASS (mode) != MODE_INT
12578 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12579 #ifdef POINTERS_EXTEND_UNSIGNED
12580 && (mode != Pmode || mem_mode == VOIDmode)
12581 #endif
12583 break;
12584 if (GET_CODE (rtl) == SYMBOL_REF
12585 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12587 dw_loc_descr_ref temp;
12589 /* If this is not defined, we have no way to emit the data. */
12590 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12591 break;
12593 temp = new_addr_loc_descr (rtl, dtprel_true);
12595 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12596 add_loc_descr (&mem_loc_result, temp);
12598 break;
12601 if (!const_ok_for_output (rtl))
12602 break;
12604 symref:
12605 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12606 vec_safe_push (used_rtx_array, rtl);
12607 break;
12609 case CONCAT:
12610 case CONCATN:
12611 case VAR_LOCATION:
12612 case DEBUG_IMPLICIT_PTR:
12613 expansion_failed (NULL_TREE, rtl,
12614 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12615 return 0;
12617 case ENTRY_VALUE:
12618 if (dwarf_strict)
12619 return NULL;
12620 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12622 if (GET_MODE_CLASS (mode) != MODE_INT
12623 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12624 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12625 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12626 else
12628 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12629 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12630 return NULL;
12631 op0 = one_reg_loc_descriptor (dbx_regnum,
12632 VAR_INIT_STATUS_INITIALIZED);
12635 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12636 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12638 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12639 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12640 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12641 return NULL;
12643 else
12644 gcc_unreachable ();
12645 if (op0 == NULL)
12646 return NULL;
12647 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12648 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12649 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12650 break;
12652 case DEBUG_PARAMETER_REF:
12653 mem_loc_result = parameter_ref_descriptor (rtl);
12654 break;
12656 case PRE_MODIFY:
12657 /* Extract the PLUS expression nested inside and fall into
12658 PLUS code below. */
12659 rtl = XEXP (rtl, 1);
12660 goto plus;
12662 case PRE_INC:
12663 case PRE_DEC:
12664 /* Turn these into a PLUS expression and fall into the PLUS code
12665 below. */
12666 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12667 gen_int_mode (GET_CODE (rtl) == PRE_INC
12668 ? GET_MODE_UNIT_SIZE (mem_mode)
12669 : -GET_MODE_UNIT_SIZE (mem_mode),
12670 mode));
12672 /* ... fall through ... */
12674 case PLUS:
12675 plus:
12676 if (is_based_loc (rtl)
12677 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12678 || XEXP (rtl, 0) == arg_pointer_rtx
12679 || XEXP (rtl, 0) == frame_pointer_rtx)
12680 && GET_MODE_CLASS (mode) == MODE_INT)
12681 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12682 INTVAL (XEXP (rtl, 1)),
12683 VAR_INIT_STATUS_INITIALIZED);
12684 else
12686 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12687 VAR_INIT_STATUS_INITIALIZED);
12688 if (mem_loc_result == 0)
12689 break;
12691 if (CONST_INT_P (XEXP (rtl, 1))
12692 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12693 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12694 else
12696 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12697 VAR_INIT_STATUS_INITIALIZED);
12698 if (op1 == 0)
12699 break;
12700 add_loc_descr (&mem_loc_result, op1);
12701 add_loc_descr (&mem_loc_result,
12702 new_loc_descr (DW_OP_plus, 0, 0));
12705 break;
12707 /* If a pseudo-reg is optimized away, it is possible for it to
12708 be replaced with a MEM containing a multiply or shift. */
12709 case MINUS:
12710 op = DW_OP_minus;
12711 goto do_binop;
12713 case MULT:
12714 op = DW_OP_mul;
12715 goto do_binop;
12717 case DIV:
12718 if (!dwarf_strict
12719 && GET_MODE_CLASS (mode) == MODE_INT
12720 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12722 mem_loc_result = typed_binop (DW_OP_div, rtl,
12723 base_type_for_mode (mode, 0),
12724 mode, mem_mode);
12725 break;
12727 op = DW_OP_div;
12728 goto do_binop;
12730 case UMOD:
12731 op = DW_OP_mod;
12732 goto do_binop;
12734 case ASHIFT:
12735 op = DW_OP_shl;
12736 goto do_shift;
12738 case ASHIFTRT:
12739 op = DW_OP_shra;
12740 goto do_shift;
12742 case LSHIFTRT:
12743 op = DW_OP_shr;
12744 goto do_shift;
12746 do_shift:
12747 if (GET_MODE_CLASS (mode) != MODE_INT)
12748 break;
12749 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12750 VAR_INIT_STATUS_INITIALIZED);
12752 rtx rtlop1 = XEXP (rtl, 1);
12753 if (GET_MODE (rtlop1) != VOIDmode
12754 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12755 < GET_MODE_BITSIZE (mode))
12756 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12757 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12758 VAR_INIT_STATUS_INITIALIZED);
12761 if (op0 == 0 || op1 == 0)
12762 break;
12764 mem_loc_result = op0;
12765 add_loc_descr (&mem_loc_result, op1);
12766 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12767 break;
12769 case AND:
12770 op = DW_OP_and;
12771 goto do_binop;
12773 case IOR:
12774 op = DW_OP_or;
12775 goto do_binop;
12777 case XOR:
12778 op = DW_OP_xor;
12779 goto do_binop;
12781 do_binop:
12782 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12783 VAR_INIT_STATUS_INITIALIZED);
12784 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12785 VAR_INIT_STATUS_INITIALIZED);
12787 if (op0 == 0 || op1 == 0)
12788 break;
12790 mem_loc_result = op0;
12791 add_loc_descr (&mem_loc_result, op1);
12792 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12793 break;
12795 case MOD:
12796 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12798 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12799 base_type_for_mode (mode, 0),
12800 mode, mem_mode);
12801 break;
12804 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12805 VAR_INIT_STATUS_INITIALIZED);
12806 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12807 VAR_INIT_STATUS_INITIALIZED);
12809 if (op0 == 0 || op1 == 0)
12810 break;
12812 mem_loc_result = op0;
12813 add_loc_descr (&mem_loc_result, op1);
12814 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12815 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12816 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12817 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12818 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12819 break;
12821 case UDIV:
12822 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12824 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12826 op = DW_OP_div;
12827 goto do_binop;
12829 mem_loc_result = typed_binop (DW_OP_div, rtl,
12830 base_type_for_mode (mode, 1),
12831 mode, mem_mode);
12833 break;
12835 case NOT:
12836 op = DW_OP_not;
12837 goto do_unop;
12839 case ABS:
12840 op = DW_OP_abs;
12841 goto do_unop;
12843 case NEG:
12844 op = DW_OP_neg;
12845 goto do_unop;
12847 do_unop:
12848 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12849 VAR_INIT_STATUS_INITIALIZED);
12851 if (op0 == 0)
12852 break;
12854 mem_loc_result = op0;
12855 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12856 break;
12858 case CONST_INT:
12859 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12860 #ifdef POINTERS_EXTEND_UNSIGNED
12861 || (mode == Pmode
12862 && mem_mode != VOIDmode
12863 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12864 #endif
12867 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12868 break;
12870 if (!dwarf_strict
12871 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12872 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12874 dw_die_ref type_die = base_type_for_mode (mode, 1);
12875 enum machine_mode amode;
12876 if (type_die == NULL)
12877 return NULL;
12878 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12879 MODE_INT, 0);
12880 if (INTVAL (rtl) >= 0
12881 && amode != BLKmode
12882 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12883 /* const DW_OP_GNU_convert <XXX> vs.
12884 DW_OP_GNU_const_type <XXX, 1, const>. */
12885 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12886 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12888 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12889 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12890 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12891 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12892 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12893 add_loc_descr (&mem_loc_result, op0);
12894 return mem_loc_result;
12896 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12897 INTVAL (rtl));
12898 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12899 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12900 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12901 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12902 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12903 else
12905 mem_loc_result->dw_loc_oprnd2.val_class
12906 = dw_val_class_const_double;
12907 mem_loc_result->dw_loc_oprnd2.v.val_double
12908 = double_int::from_shwi (INTVAL (rtl));
12911 break;
12913 case CONST_DOUBLE:
12914 if (!dwarf_strict)
12916 dw_die_ref type_die;
12918 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12919 CONST_DOUBLE rtx could represent either a large integer
12920 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
12921 the value is always a floating point constant.
12923 When it is an integer, a CONST_DOUBLE is used whenever
12924 the constant requires 2 HWIs to be adequately represented.
12925 We output CONST_DOUBLEs as blocks. */
12926 if (mode == VOIDmode
12927 || (GET_MODE (rtl) == VOIDmode
12928 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12929 break;
12930 type_die = base_type_for_mode (mode,
12931 GET_MODE_CLASS (mode) == MODE_INT);
12932 if (type_die == NULL)
12933 return NULL;
12934 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12935 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12936 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12937 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12938 #if TARGET_SUPPORTS_WIDE_INT == 0
12939 if (!SCALAR_FLOAT_MODE_P (mode))
12941 mem_loc_result->dw_loc_oprnd2.val_class
12942 = dw_val_class_const_double;
12943 mem_loc_result->dw_loc_oprnd2.v.val_double
12944 = rtx_to_double_int (rtl);
12946 else
12947 #endif
12949 unsigned int length = GET_MODE_SIZE (mode);
12950 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
12952 insert_float (rtl, array);
12953 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12954 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12955 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12956 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12959 break;
12961 case CONST_WIDE_INT:
12962 if (!dwarf_strict)
12964 dw_die_ref type_die;
12966 type_die = base_type_for_mode (mode,
12967 GET_MODE_CLASS (mode) == MODE_INT);
12968 if (type_die == NULL)
12969 return NULL;
12970 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12971 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12972 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12973 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12974 mem_loc_result->dw_loc_oprnd2.val_class
12975 = dw_val_class_wide_int;
12976 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
12977 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
12979 break;
12981 case EQ:
12982 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12983 break;
12985 case GE:
12986 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12987 break;
12989 case GT:
12990 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12991 break;
12993 case LE:
12994 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12995 break;
12997 case LT:
12998 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12999 break;
13001 case NE:
13002 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13003 break;
13005 case GEU:
13006 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13007 break;
13009 case GTU:
13010 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13011 break;
13013 case LEU:
13014 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13015 break;
13017 case LTU:
13018 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13019 break;
13021 case UMIN:
13022 case UMAX:
13023 if (GET_MODE_CLASS (mode) != MODE_INT)
13024 break;
13025 /* FALLTHRU */
13026 case SMIN:
13027 case SMAX:
13028 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13029 break;
13031 case ZERO_EXTRACT:
13032 case SIGN_EXTRACT:
13033 if (CONST_INT_P (XEXP (rtl, 1))
13034 && CONST_INT_P (XEXP (rtl, 2))
13035 && ((unsigned) INTVAL (XEXP (rtl, 1))
13036 + (unsigned) INTVAL (XEXP (rtl, 2))
13037 <= GET_MODE_BITSIZE (mode))
13038 && GET_MODE_CLASS (mode) == MODE_INT
13039 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13040 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13042 int shift, size;
13043 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13044 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13045 if (op0 == 0)
13046 break;
13047 if (GET_CODE (rtl) == SIGN_EXTRACT)
13048 op = DW_OP_shra;
13049 else
13050 op = DW_OP_shr;
13051 mem_loc_result = op0;
13052 size = INTVAL (XEXP (rtl, 1));
13053 shift = INTVAL (XEXP (rtl, 2));
13054 if (BITS_BIG_ENDIAN)
13055 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13056 - shift - size;
13057 if (shift + size != (int) DWARF2_ADDR_SIZE)
13059 add_loc_descr (&mem_loc_result,
13060 int_loc_descriptor (DWARF2_ADDR_SIZE
13061 - shift - size));
13062 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13064 if (size != (int) DWARF2_ADDR_SIZE)
13066 add_loc_descr (&mem_loc_result,
13067 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13068 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13071 break;
13073 case IF_THEN_ELSE:
13075 dw_loc_descr_ref op2, bra_node, drop_node;
13076 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13077 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13078 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13079 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13080 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13081 VAR_INIT_STATUS_INITIALIZED);
13082 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13083 VAR_INIT_STATUS_INITIALIZED);
13084 if (op0 == NULL || op1 == NULL || op2 == NULL)
13085 break;
13087 mem_loc_result = op1;
13088 add_loc_descr (&mem_loc_result, op2);
13089 add_loc_descr (&mem_loc_result, op0);
13090 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13091 add_loc_descr (&mem_loc_result, bra_node);
13092 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13093 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13094 add_loc_descr (&mem_loc_result, drop_node);
13095 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13096 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13098 break;
13100 case FLOAT_EXTEND:
13101 case FLOAT_TRUNCATE:
13102 case FLOAT:
13103 case UNSIGNED_FLOAT:
13104 case FIX:
13105 case UNSIGNED_FIX:
13106 if (!dwarf_strict)
13108 dw_die_ref type_die;
13109 dw_loc_descr_ref cvt;
13111 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13112 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13113 if (op0 == NULL)
13114 break;
13115 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13116 && (GET_CODE (rtl) == FLOAT
13117 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13118 <= DWARF2_ADDR_SIZE))
13120 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13121 GET_CODE (rtl) == UNSIGNED_FLOAT);
13122 if (type_die == NULL)
13123 break;
13124 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13125 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13126 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13127 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13128 add_loc_descr (&op0, cvt);
13130 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13131 if (type_die == NULL)
13132 break;
13133 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13134 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13135 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13136 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13137 add_loc_descr (&op0, cvt);
13138 if (GET_MODE_CLASS (mode) == MODE_INT
13139 && (GET_CODE (rtl) == FIX
13140 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13142 op0 = convert_descriptor_to_mode (mode, op0);
13143 if (op0 == NULL)
13144 break;
13146 mem_loc_result = op0;
13148 break;
13150 case CLZ:
13151 case CTZ:
13152 case FFS:
13153 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13154 break;
13156 case POPCOUNT:
13157 case PARITY:
13158 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13159 break;
13161 case BSWAP:
13162 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13163 break;
13165 case ROTATE:
13166 case ROTATERT:
13167 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13168 break;
13170 case COMPARE:
13171 /* In theory, we could implement the above. */
13172 /* DWARF cannot represent the unsigned compare operations
13173 natively. */
13174 case SS_MULT:
13175 case US_MULT:
13176 case SS_DIV:
13177 case US_DIV:
13178 case SS_PLUS:
13179 case US_PLUS:
13180 case SS_MINUS:
13181 case US_MINUS:
13182 case SS_NEG:
13183 case US_NEG:
13184 case SS_ABS:
13185 case SS_ASHIFT:
13186 case US_ASHIFT:
13187 case SS_TRUNCATE:
13188 case US_TRUNCATE:
13189 case UNORDERED:
13190 case ORDERED:
13191 case UNEQ:
13192 case UNGE:
13193 case UNGT:
13194 case UNLE:
13195 case UNLT:
13196 case LTGT:
13197 case FRACT_CONVERT:
13198 case UNSIGNED_FRACT_CONVERT:
13199 case SAT_FRACT:
13200 case UNSIGNED_SAT_FRACT:
13201 case SQRT:
13202 case ASM_OPERANDS:
13203 case VEC_MERGE:
13204 case VEC_SELECT:
13205 case VEC_CONCAT:
13206 case VEC_DUPLICATE:
13207 case UNSPEC:
13208 case HIGH:
13209 case FMA:
13210 case STRICT_LOW_PART:
13211 case CONST_VECTOR:
13212 case CONST_FIXED:
13213 case CLRSB:
13214 case CLOBBER:
13215 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13216 can't express it in the debug info. This can happen e.g. with some
13217 TLS UNSPECs. */
13218 break;
13220 case CONST_STRING:
13221 resolve_one_addr (&rtl, NULL);
13222 goto symref;
13224 default:
13225 #ifdef ENABLE_CHECKING
13226 print_rtl (stderr, rtl);
13227 gcc_unreachable ();
13228 #else
13229 break;
13230 #endif
13233 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13234 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13236 return mem_loc_result;
13239 /* Return a descriptor that describes the concatenation of two locations.
13240 This is typically a complex variable. */
13242 static dw_loc_descr_ref
13243 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13245 dw_loc_descr_ref cc_loc_result = NULL;
13246 dw_loc_descr_ref x0_ref
13247 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13248 dw_loc_descr_ref x1_ref
13249 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13251 if (x0_ref == 0 || x1_ref == 0)
13252 return 0;
13254 cc_loc_result = x0_ref;
13255 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13257 add_loc_descr (&cc_loc_result, x1_ref);
13258 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13260 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13261 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13263 return cc_loc_result;
13266 /* Return a descriptor that describes the concatenation of N
13267 locations. */
13269 static dw_loc_descr_ref
13270 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13272 unsigned int i;
13273 dw_loc_descr_ref cc_loc_result = NULL;
13274 unsigned int n = XVECLEN (concatn, 0);
13276 for (i = 0; i < n; ++i)
13278 dw_loc_descr_ref ref;
13279 rtx x = XVECEXP (concatn, 0, i);
13281 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13282 if (ref == NULL)
13283 return NULL;
13285 add_loc_descr (&cc_loc_result, ref);
13286 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13289 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13290 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13292 return cc_loc_result;
13295 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13296 for DEBUG_IMPLICIT_PTR RTL. */
13298 static dw_loc_descr_ref
13299 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13301 dw_loc_descr_ref ret;
13302 dw_die_ref ref;
13304 if (dwarf_strict)
13305 return NULL;
13306 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13307 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13308 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13309 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13310 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13311 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13312 if (ref)
13314 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13315 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13316 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13318 else
13320 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13321 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13323 return ret;
13326 /* Output a proper Dwarf location descriptor for a variable or parameter
13327 which is either allocated in a register or in a memory location. For a
13328 register, we just generate an OP_REG and the register number. For a
13329 memory location we provide a Dwarf postfix expression describing how to
13330 generate the (dynamic) address of the object onto the address stack.
13332 MODE is mode of the decl if this loc_descriptor is going to be used in
13333 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13334 allowed, VOIDmode otherwise.
13336 If we don't know how to describe it, return 0. */
13338 static dw_loc_descr_ref
13339 loc_descriptor (rtx rtl, enum machine_mode mode,
13340 enum var_init_status initialized)
13342 dw_loc_descr_ref loc_result = NULL;
13344 switch (GET_CODE (rtl))
13346 case SUBREG:
13347 /* The case of a subreg may arise when we have a local (register)
13348 variable or a formal (register) parameter which doesn't quite fill
13349 up an entire register. For now, just assume that it is
13350 legitimate to make the Dwarf info refer to the whole register which
13351 contains the given subreg. */
13352 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13353 loc_result = loc_descriptor (SUBREG_REG (rtl),
13354 GET_MODE (SUBREG_REG (rtl)), initialized);
13355 else
13356 goto do_default;
13357 break;
13359 case REG:
13360 loc_result = reg_loc_descriptor (rtl, initialized);
13361 break;
13363 case MEM:
13364 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13365 GET_MODE (rtl), initialized);
13366 if (loc_result == NULL)
13367 loc_result = tls_mem_loc_descriptor (rtl);
13368 if (loc_result == NULL)
13370 rtx new_rtl = avoid_constant_pool_reference (rtl);
13371 if (new_rtl != rtl)
13372 loc_result = loc_descriptor (new_rtl, mode, initialized);
13374 break;
13376 case CONCAT:
13377 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13378 initialized);
13379 break;
13381 case CONCATN:
13382 loc_result = concatn_loc_descriptor (rtl, initialized);
13383 break;
13385 case VAR_LOCATION:
13386 /* Single part. */
13387 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13389 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13390 if (GET_CODE (loc) == EXPR_LIST)
13391 loc = XEXP (loc, 0);
13392 loc_result = loc_descriptor (loc, mode, initialized);
13393 break;
13396 rtl = XEXP (rtl, 1);
13397 /* FALLTHRU */
13399 case PARALLEL:
13401 rtvec par_elems = XVEC (rtl, 0);
13402 int num_elem = GET_NUM_ELEM (par_elems);
13403 enum machine_mode mode;
13404 int i;
13406 /* Create the first one, so we have something to add to. */
13407 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13408 VOIDmode, initialized);
13409 if (loc_result == NULL)
13410 return NULL;
13411 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13412 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13413 for (i = 1; i < num_elem; i++)
13415 dw_loc_descr_ref temp;
13417 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13418 VOIDmode, initialized);
13419 if (temp == NULL)
13420 return NULL;
13421 add_loc_descr (&loc_result, temp);
13422 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13423 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13426 break;
13428 case CONST_INT:
13429 if (mode != VOIDmode && mode != BLKmode)
13430 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13431 INTVAL (rtl));
13432 break;
13434 case CONST_DOUBLE:
13435 if (mode == VOIDmode)
13436 mode = GET_MODE (rtl);
13438 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13440 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13442 /* Note that a CONST_DOUBLE rtx could represent either an integer
13443 or a floating-point constant. A CONST_DOUBLE is used whenever
13444 the constant requires more than one word in order to be
13445 adequately represented. We output CONST_DOUBLEs as blocks. */
13446 loc_result = new_loc_descr (DW_OP_implicit_value,
13447 GET_MODE_SIZE (mode), 0);
13448 #if TARGET_SUPPORTS_WIDE_INT == 0
13449 if (!SCALAR_FLOAT_MODE_P (mode))
13451 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13452 loc_result->dw_loc_oprnd2.v.val_double
13453 = rtx_to_double_int (rtl);
13455 else
13456 #endif
13458 unsigned int length = GET_MODE_SIZE (mode);
13459 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13461 insert_float (rtl, array);
13462 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13463 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13464 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13465 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13468 break;
13470 case CONST_WIDE_INT:
13471 if (mode == VOIDmode)
13472 mode = GET_MODE (rtl);
13474 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13476 loc_result = new_loc_descr (DW_OP_implicit_value,
13477 GET_MODE_SIZE (mode), 0);
13478 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13479 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13480 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13482 break;
13484 case CONST_VECTOR:
13485 if (mode == VOIDmode)
13486 mode = GET_MODE (rtl);
13488 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13490 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13491 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13492 unsigned char *array
13493 = ggc_vec_alloc<unsigned char> (length * elt_size);
13494 unsigned int i;
13495 unsigned char *p;
13496 enum machine_mode imode = GET_MODE_INNER (mode);
13498 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13499 switch (GET_MODE_CLASS (mode))
13501 case MODE_VECTOR_INT:
13502 for (i = 0, p = array; i < length; i++, p += elt_size)
13504 rtx elt = CONST_VECTOR_ELT (rtl, i);
13505 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13507 break;
13509 case MODE_VECTOR_FLOAT:
13510 for (i = 0, p = array; i < length; i++, p += elt_size)
13512 rtx elt = CONST_VECTOR_ELT (rtl, i);
13513 insert_float (elt, p);
13515 break;
13517 default:
13518 gcc_unreachable ();
13521 loc_result = new_loc_descr (DW_OP_implicit_value,
13522 length * elt_size, 0);
13523 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13524 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13525 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13526 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13528 break;
13530 case CONST:
13531 if (mode == VOIDmode
13532 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13533 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13534 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13536 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13537 break;
13539 /* FALLTHROUGH */
13540 case SYMBOL_REF:
13541 if (!const_ok_for_output (rtl))
13542 break;
13543 case LABEL_REF:
13544 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13545 && (dwarf_version >= 4 || !dwarf_strict))
13547 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13548 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13549 vec_safe_push (used_rtx_array, rtl);
13551 break;
13553 case DEBUG_IMPLICIT_PTR:
13554 loc_result = implicit_ptr_descriptor (rtl, 0);
13555 break;
13557 case PLUS:
13558 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13559 && CONST_INT_P (XEXP (rtl, 1)))
13561 loc_result
13562 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13563 break;
13565 /* FALLTHRU */
13566 do_default:
13567 default:
13568 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13569 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13570 && dwarf_version >= 4)
13571 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13573 /* Value expression. */
13574 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13575 if (loc_result)
13576 add_loc_descr (&loc_result,
13577 new_loc_descr (DW_OP_stack_value, 0, 0));
13579 break;
13582 return loc_result;
13585 /* We need to figure out what section we should use as the base for the
13586 address ranges where a given location is valid.
13587 1. If this particular DECL has a section associated with it, use that.
13588 2. If this function has a section associated with it, use that.
13589 3. Otherwise, use the text section.
13590 XXX: If you split a variable across multiple sections, we won't notice. */
13592 static const char *
13593 secname_for_decl (const_tree decl)
13595 const char *secname;
13597 if (VAR_OR_FUNCTION_DECL_P (decl)
13598 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13599 && DECL_SECTION_NAME (decl))
13600 secname = DECL_SECTION_NAME (decl);
13601 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13602 secname = DECL_SECTION_NAME (current_function_decl);
13603 else if (cfun && in_cold_section_p)
13604 secname = crtl->subsections.cold_section_label;
13605 else
13606 secname = text_section_label;
13608 return secname;
13611 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13613 static bool
13614 decl_by_reference_p (tree decl)
13616 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13617 || TREE_CODE (decl) == VAR_DECL)
13618 && DECL_BY_REFERENCE (decl));
13621 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13622 for VARLOC. */
13624 static dw_loc_descr_ref
13625 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13626 enum var_init_status initialized)
13628 int have_address = 0;
13629 dw_loc_descr_ref descr;
13630 enum machine_mode mode;
13632 if (want_address != 2)
13634 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13635 /* Single part. */
13636 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13638 varloc = PAT_VAR_LOCATION_LOC (varloc);
13639 if (GET_CODE (varloc) == EXPR_LIST)
13640 varloc = XEXP (varloc, 0);
13641 mode = GET_MODE (varloc);
13642 if (MEM_P (varloc))
13644 rtx addr = XEXP (varloc, 0);
13645 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13646 mode, initialized);
13647 if (descr)
13648 have_address = 1;
13649 else
13651 rtx x = avoid_constant_pool_reference (varloc);
13652 if (x != varloc)
13653 descr = mem_loc_descriptor (x, mode, VOIDmode,
13654 initialized);
13657 else
13658 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13660 else
13661 return 0;
13663 else
13665 if (GET_CODE (varloc) == VAR_LOCATION)
13666 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13667 else
13668 mode = DECL_MODE (loc);
13669 descr = loc_descriptor (varloc, mode, initialized);
13670 have_address = 1;
13673 if (!descr)
13674 return 0;
13676 if (want_address == 2 && !have_address
13677 && (dwarf_version >= 4 || !dwarf_strict))
13679 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13681 expansion_failed (loc, NULL_RTX,
13682 "DWARF address size mismatch");
13683 return 0;
13685 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13686 have_address = 1;
13688 /* Show if we can't fill the request for an address. */
13689 if (want_address && !have_address)
13691 expansion_failed (loc, NULL_RTX,
13692 "Want address and only have value");
13693 return 0;
13696 /* If we've got an address and don't want one, dereference. */
13697 if (!want_address && have_address)
13699 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13700 enum dwarf_location_atom op;
13702 if (size > DWARF2_ADDR_SIZE || size == -1)
13704 expansion_failed (loc, NULL_RTX,
13705 "DWARF address size mismatch");
13706 return 0;
13708 else if (size == DWARF2_ADDR_SIZE)
13709 op = DW_OP_deref;
13710 else
13711 op = DW_OP_deref_size;
13713 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13716 return descr;
13719 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13720 if it is not possible. */
13722 static dw_loc_descr_ref
13723 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13725 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13726 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13727 else if (dwarf_version >= 3 || !dwarf_strict)
13728 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13729 else
13730 return NULL;
13733 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13734 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13736 static dw_loc_descr_ref
13737 dw_sra_loc_expr (tree decl, rtx loc)
13739 rtx p;
13740 unsigned int padsize = 0;
13741 dw_loc_descr_ref descr, *descr_tail;
13742 unsigned HOST_WIDE_INT decl_size;
13743 rtx varloc;
13744 enum var_init_status initialized;
13746 if (DECL_SIZE (decl) == NULL
13747 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13748 return NULL;
13750 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13751 descr = NULL;
13752 descr_tail = &descr;
13754 for (p = loc; p; p = XEXP (p, 1))
13756 unsigned int bitsize = decl_piece_bitsize (p);
13757 rtx loc_note = *decl_piece_varloc_ptr (p);
13758 dw_loc_descr_ref cur_descr;
13759 dw_loc_descr_ref *tail, last = NULL;
13760 unsigned int opsize = 0;
13762 if (loc_note == NULL_RTX
13763 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13765 padsize += bitsize;
13766 continue;
13768 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13769 varloc = NOTE_VAR_LOCATION (loc_note);
13770 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13771 if (cur_descr == NULL)
13773 padsize += bitsize;
13774 continue;
13777 /* Check that cur_descr either doesn't use
13778 DW_OP_*piece operations, or their sum is equal
13779 to bitsize. Otherwise we can't embed it. */
13780 for (tail = &cur_descr; *tail != NULL;
13781 tail = &(*tail)->dw_loc_next)
13782 if ((*tail)->dw_loc_opc == DW_OP_piece)
13784 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13785 * BITS_PER_UNIT;
13786 last = *tail;
13788 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13790 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13791 last = *tail;
13794 if (last != NULL && opsize != bitsize)
13796 padsize += bitsize;
13797 /* Discard the current piece of the descriptor and release any
13798 addr_table entries it uses. */
13799 remove_loc_list_addr_table_entries (cur_descr);
13800 continue;
13803 /* If there is a hole, add DW_OP_*piece after empty DWARF
13804 expression, which means that those bits are optimized out. */
13805 if (padsize)
13807 if (padsize > decl_size)
13809 remove_loc_list_addr_table_entries (cur_descr);
13810 goto discard_descr;
13812 decl_size -= padsize;
13813 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13814 if (*descr_tail == NULL)
13816 remove_loc_list_addr_table_entries (cur_descr);
13817 goto discard_descr;
13819 descr_tail = &(*descr_tail)->dw_loc_next;
13820 padsize = 0;
13822 *descr_tail = cur_descr;
13823 descr_tail = tail;
13824 if (bitsize > decl_size)
13825 goto discard_descr;
13826 decl_size -= bitsize;
13827 if (last == NULL)
13829 HOST_WIDE_INT offset = 0;
13830 if (GET_CODE (varloc) == VAR_LOCATION
13831 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13833 varloc = PAT_VAR_LOCATION_LOC (varloc);
13834 if (GET_CODE (varloc) == EXPR_LIST)
13835 varloc = XEXP (varloc, 0);
13839 if (GET_CODE (varloc) == CONST
13840 || GET_CODE (varloc) == SIGN_EXTEND
13841 || GET_CODE (varloc) == ZERO_EXTEND)
13842 varloc = XEXP (varloc, 0);
13843 else if (GET_CODE (varloc) == SUBREG)
13844 varloc = SUBREG_REG (varloc);
13845 else
13846 break;
13848 while (1);
13849 /* DW_OP_bit_size offset should be zero for register
13850 or implicit location descriptions and empty location
13851 descriptions, but for memory addresses needs big endian
13852 adjustment. */
13853 if (MEM_P (varloc))
13855 unsigned HOST_WIDE_INT memsize
13856 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13857 if (memsize != bitsize)
13859 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13860 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13861 goto discard_descr;
13862 if (memsize < bitsize)
13863 goto discard_descr;
13864 if (BITS_BIG_ENDIAN)
13865 offset = memsize - bitsize;
13869 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13870 if (*descr_tail == NULL)
13871 goto discard_descr;
13872 descr_tail = &(*descr_tail)->dw_loc_next;
13876 /* If there were any non-empty expressions, add padding till the end of
13877 the decl. */
13878 if (descr != NULL && decl_size != 0)
13880 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13881 if (*descr_tail == NULL)
13882 goto discard_descr;
13884 return descr;
13886 discard_descr:
13887 /* Discard the descriptor and release any addr_table entries it uses. */
13888 remove_loc_list_addr_table_entries (descr);
13889 return NULL;
13892 /* Return the dwarf representation of the location list LOC_LIST of
13893 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13894 function. */
13896 static dw_loc_list_ref
13897 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13899 const char *endname, *secname;
13900 rtx varloc;
13901 enum var_init_status initialized;
13902 struct var_loc_node *node;
13903 dw_loc_descr_ref descr;
13904 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13905 dw_loc_list_ref list = NULL;
13906 dw_loc_list_ref *listp = &list;
13908 /* Now that we know what section we are using for a base,
13909 actually construct the list of locations.
13910 The first location information is what is passed to the
13911 function that creates the location list, and the remaining
13912 locations just get added on to that list.
13913 Note that we only know the start address for a location
13914 (IE location changes), so to build the range, we use
13915 the range [current location start, next location start].
13916 This means we have to special case the last node, and generate
13917 a range of [last location start, end of function label]. */
13919 secname = secname_for_decl (decl);
13921 for (node = loc_list->first; node; node = node->next)
13922 if (GET_CODE (node->loc) == EXPR_LIST
13923 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13925 if (GET_CODE (node->loc) == EXPR_LIST)
13927 /* This requires DW_OP_{,bit_}piece, which is not usable
13928 inside DWARF expressions. */
13929 if (want_address != 2)
13930 continue;
13931 descr = dw_sra_loc_expr (decl, node->loc);
13932 if (descr == NULL)
13933 continue;
13935 else
13937 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13938 varloc = NOTE_VAR_LOCATION (node->loc);
13939 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13941 if (descr)
13943 bool range_across_switch = false;
13944 /* If section switch happens in between node->label
13945 and node->next->label (or end of function) and
13946 we can't emit it as a single entry list,
13947 emit two ranges, first one ending at the end
13948 of first partition and second one starting at the
13949 beginning of second partition. */
13950 if (node == loc_list->last_before_switch
13951 && (node != loc_list->first || loc_list->first->next)
13952 && current_function_decl)
13954 endname = cfun->fde->dw_fde_end;
13955 range_across_switch = true;
13957 /* The variable has a location between NODE->LABEL and
13958 NODE->NEXT->LABEL. */
13959 else if (node->next)
13960 endname = node->next->label;
13961 /* If the variable has a location at the last label
13962 it keeps its location until the end of function. */
13963 else if (!current_function_decl)
13964 endname = text_end_label;
13965 else
13967 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13968 current_function_funcdef_no);
13969 endname = ggc_strdup (label_id);
13972 *listp = new_loc_list (descr, node->label, endname, secname);
13973 if (TREE_CODE (decl) == PARM_DECL
13974 && node == loc_list->first
13975 && NOTE_P (node->loc)
13976 && strcmp (node->label, endname) == 0)
13977 (*listp)->force = true;
13978 listp = &(*listp)->dw_loc_next;
13980 if (range_across_switch)
13982 if (GET_CODE (node->loc) == EXPR_LIST)
13983 descr = dw_sra_loc_expr (decl, node->loc);
13984 else
13986 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13987 varloc = NOTE_VAR_LOCATION (node->loc);
13988 descr = dw_loc_list_1 (decl, varloc, want_address,
13989 initialized);
13991 gcc_assert (descr);
13992 /* The variable has a location between NODE->LABEL and
13993 NODE->NEXT->LABEL. */
13994 if (node->next)
13995 endname = node->next->label;
13996 else
13997 endname = cfun->fde->dw_fde_second_end;
13998 *listp = new_loc_list (descr,
13999 cfun->fde->dw_fde_second_begin,
14000 endname, secname);
14001 listp = &(*listp)->dw_loc_next;
14006 /* Try to avoid the overhead of a location list emitting a location
14007 expression instead, but only if we didn't have more than one
14008 location entry in the first place. If some entries were not
14009 representable, we don't want to pretend a single entry that was
14010 applies to the entire scope in which the variable is
14011 available. */
14012 if (list && loc_list->first->next)
14013 gen_llsym (list);
14015 return list;
14018 /* Return if the loc_list has only single element and thus can be represented
14019 as location description. */
14021 static bool
14022 single_element_loc_list_p (dw_loc_list_ref list)
14024 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14025 return !list->ll_symbol;
14028 /* To each location in list LIST add loc descr REF. */
14030 static void
14031 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14033 dw_loc_descr_ref copy;
14034 add_loc_descr (&list->expr, ref);
14035 list = list->dw_loc_next;
14036 while (list)
14038 copy = ggc_alloc<dw_loc_descr_node> ();
14039 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14040 add_loc_descr (&list->expr, copy);
14041 while (copy->dw_loc_next)
14043 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14044 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14045 copy->dw_loc_next = new_copy;
14046 copy = new_copy;
14048 list = list->dw_loc_next;
14052 /* Given two lists RET and LIST
14053 produce location list that is result of adding expression in LIST
14054 to expression in RET on each position in program.
14055 Might be destructive on both RET and LIST.
14057 TODO: We handle only simple cases of RET or LIST having at most one
14058 element. General case would inolve sorting the lists in program order
14059 and merging them that will need some additional work.
14060 Adding that will improve quality of debug info especially for SRA-ed
14061 structures. */
14063 static void
14064 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14066 if (!list)
14067 return;
14068 if (!*ret)
14070 *ret = list;
14071 return;
14073 if (!list->dw_loc_next)
14075 add_loc_descr_to_each (*ret, list->expr);
14076 return;
14078 if (!(*ret)->dw_loc_next)
14080 add_loc_descr_to_each (list, (*ret)->expr);
14081 *ret = list;
14082 return;
14084 expansion_failed (NULL_TREE, NULL_RTX,
14085 "Don't know how to merge two non-trivial"
14086 " location lists.\n");
14087 *ret = NULL;
14088 return;
14091 /* LOC is constant expression. Try a luck, look it up in constant
14092 pool and return its loc_descr of its address. */
14094 static dw_loc_descr_ref
14095 cst_pool_loc_descr (tree loc)
14097 /* Get an RTL for this, if something has been emitted. */
14098 rtx rtl = lookup_constant_def (loc);
14100 if (!rtl || !MEM_P (rtl))
14102 gcc_assert (!rtl);
14103 return 0;
14105 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14107 /* TODO: We might get more coverage if we was actually delaying expansion
14108 of all expressions till end of compilation when constant pools are fully
14109 populated. */
14110 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14112 expansion_failed (loc, NULL_RTX,
14113 "CST value in contant pool but not marked.");
14114 return 0;
14116 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14117 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14120 /* Return dw_loc_list representing address of addr_expr LOC
14121 by looking for inner INDIRECT_REF expression and turning
14122 it into simple arithmetics. */
14124 static dw_loc_list_ref
14125 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14127 tree obj, offset;
14128 HOST_WIDE_INT bitsize, bitpos, bytepos;
14129 enum machine_mode mode;
14130 int unsignedp, volatilep = 0;
14131 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14133 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14134 &bitsize, &bitpos, &offset, &mode,
14135 &unsignedp, &volatilep, false);
14136 STRIP_NOPS (obj);
14137 if (bitpos % BITS_PER_UNIT)
14139 expansion_failed (loc, NULL_RTX, "bitfield access");
14140 return 0;
14142 if (!INDIRECT_REF_P (obj))
14144 expansion_failed (obj,
14145 NULL_RTX, "no indirect ref in inner refrence");
14146 return 0;
14148 if (!offset && !bitpos)
14149 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14150 else if (toplev
14151 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14152 && (dwarf_version >= 4 || !dwarf_strict))
14154 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14155 if (!list_ret)
14156 return 0;
14157 if (offset)
14159 /* Variable offset. */
14160 list_ret1 = loc_list_from_tree (offset, 0);
14161 if (list_ret1 == 0)
14162 return 0;
14163 add_loc_list (&list_ret, list_ret1);
14164 if (!list_ret)
14165 return 0;
14166 add_loc_descr_to_each (list_ret,
14167 new_loc_descr (DW_OP_plus, 0, 0));
14169 bytepos = bitpos / BITS_PER_UNIT;
14170 if (bytepos > 0)
14171 add_loc_descr_to_each (list_ret,
14172 new_loc_descr (DW_OP_plus_uconst,
14173 bytepos, 0));
14174 else if (bytepos < 0)
14175 loc_list_plus_const (list_ret, bytepos);
14176 add_loc_descr_to_each (list_ret,
14177 new_loc_descr (DW_OP_stack_value, 0, 0));
14179 return list_ret;
14183 /* Generate Dwarf location list representing LOC.
14184 If WANT_ADDRESS is false, expression computing LOC will be computed
14185 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14186 if WANT_ADDRESS is 2, expression computing address useable in location
14187 will be returned (i.e. DW_OP_reg can be used
14188 to refer to register values). */
14190 static dw_loc_list_ref
14191 loc_list_from_tree (tree loc, int want_address)
14193 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14194 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14195 int have_address = 0;
14196 enum dwarf_location_atom op;
14198 /* ??? Most of the time we do not take proper care for sign/zero
14199 extending the values properly. Hopefully this won't be a real
14200 problem... */
14202 switch (TREE_CODE (loc))
14204 case ERROR_MARK:
14205 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14206 return 0;
14208 case PLACEHOLDER_EXPR:
14209 /* This case involves extracting fields from an object to determine the
14210 position of other fields. We don't try to encode this here. The
14211 only user of this is Ada, which encodes the needed information using
14212 the names of types. */
14213 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14214 return 0;
14216 case CALL_EXPR:
14217 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14218 /* There are no opcodes for these operations. */
14219 return 0;
14221 case PREINCREMENT_EXPR:
14222 case PREDECREMENT_EXPR:
14223 case POSTINCREMENT_EXPR:
14224 case POSTDECREMENT_EXPR:
14225 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14226 /* There are no opcodes for these operations. */
14227 return 0;
14229 case ADDR_EXPR:
14230 /* If we already want an address, see if there is INDIRECT_REF inside
14231 e.g. for &this->field. */
14232 if (want_address)
14234 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14235 (loc, want_address == 2);
14236 if (list_ret)
14237 have_address = 1;
14238 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14239 && (ret = cst_pool_loc_descr (loc)))
14240 have_address = 1;
14242 /* Otherwise, process the argument and look for the address. */
14243 if (!list_ret && !ret)
14244 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14245 else
14247 if (want_address)
14248 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14249 return NULL;
14251 break;
14253 case VAR_DECL:
14254 if (DECL_THREAD_LOCAL_P (loc))
14256 rtx rtl;
14257 enum dwarf_location_atom tls_op;
14258 enum dtprel_bool dtprel = dtprel_false;
14260 if (targetm.have_tls)
14262 /* If this is not defined, we have no way to emit the
14263 data. */
14264 if (!targetm.asm_out.output_dwarf_dtprel)
14265 return 0;
14267 /* The way DW_OP_GNU_push_tls_address is specified, we
14268 can only look up addresses of objects in the current
14269 module. We used DW_OP_addr as first op, but that's
14270 wrong, because DW_OP_addr is relocated by the debug
14271 info consumer, while DW_OP_GNU_push_tls_address
14272 operand shouldn't be. */
14273 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14274 return 0;
14275 dtprel = dtprel_true;
14276 tls_op = DW_OP_GNU_push_tls_address;
14278 else
14280 if (!targetm.emutls.debug_form_tls_address
14281 || !(dwarf_version >= 3 || !dwarf_strict))
14282 return 0;
14283 /* We stuffed the control variable into the DECL_VALUE_EXPR
14284 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14285 no longer appear in gimple code. We used the control
14286 variable in specific so that we could pick it up here. */
14287 loc = DECL_VALUE_EXPR (loc);
14288 tls_op = DW_OP_form_tls_address;
14291 rtl = rtl_for_decl_location (loc);
14292 if (rtl == NULL_RTX)
14293 return 0;
14295 if (!MEM_P (rtl))
14296 return 0;
14297 rtl = XEXP (rtl, 0);
14298 if (! CONSTANT_P (rtl))
14299 return 0;
14301 ret = new_addr_loc_descr (rtl, dtprel);
14302 ret1 = new_loc_descr (tls_op, 0, 0);
14303 add_loc_descr (&ret, ret1);
14305 have_address = 1;
14306 break;
14308 /* FALLTHRU */
14310 case PARM_DECL:
14311 case RESULT_DECL:
14312 if (DECL_HAS_VALUE_EXPR_P (loc))
14313 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14314 want_address);
14315 /* FALLTHRU */
14317 case FUNCTION_DECL:
14319 rtx rtl;
14320 var_loc_list *loc_list = lookup_decl_loc (loc);
14322 if (loc_list && loc_list->first)
14324 list_ret = dw_loc_list (loc_list, loc, want_address);
14325 have_address = want_address != 0;
14326 break;
14328 rtl = rtl_for_decl_location (loc);
14329 if (rtl == NULL_RTX)
14331 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14332 return 0;
14334 else if (CONST_INT_P (rtl))
14336 HOST_WIDE_INT val = INTVAL (rtl);
14337 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14338 val &= GET_MODE_MASK (DECL_MODE (loc));
14339 ret = int_loc_descriptor (val);
14341 else if (GET_CODE (rtl) == CONST_STRING)
14343 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14344 return 0;
14346 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14347 ret = new_addr_loc_descr (rtl, dtprel_false);
14348 else
14350 enum machine_mode mode, mem_mode;
14352 /* Certain constructs can only be represented at top-level. */
14353 if (want_address == 2)
14355 ret = loc_descriptor (rtl, VOIDmode,
14356 VAR_INIT_STATUS_INITIALIZED);
14357 have_address = 1;
14359 else
14361 mode = GET_MODE (rtl);
14362 mem_mode = VOIDmode;
14363 if (MEM_P (rtl))
14365 mem_mode = mode;
14366 mode = get_address_mode (rtl);
14367 rtl = XEXP (rtl, 0);
14368 have_address = 1;
14370 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14371 VAR_INIT_STATUS_INITIALIZED);
14373 if (!ret)
14374 expansion_failed (loc, rtl,
14375 "failed to produce loc descriptor for rtl");
14378 break;
14380 case MEM_REF:
14381 /* ??? FIXME. */
14382 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14383 return 0;
14384 /* Fallthru. */
14385 case INDIRECT_REF:
14386 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14387 have_address = 1;
14388 break;
14390 case COMPOUND_EXPR:
14391 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14393 CASE_CONVERT:
14394 case VIEW_CONVERT_EXPR:
14395 case SAVE_EXPR:
14396 case MODIFY_EXPR:
14397 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14399 case COMPONENT_REF:
14400 case BIT_FIELD_REF:
14401 case ARRAY_REF:
14402 case ARRAY_RANGE_REF:
14403 case REALPART_EXPR:
14404 case IMAGPART_EXPR:
14406 tree obj, offset;
14407 HOST_WIDE_INT bitsize, bitpos, bytepos;
14408 enum machine_mode mode;
14409 int unsignedp, volatilep = 0;
14411 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14412 &unsignedp, &volatilep, false);
14414 gcc_assert (obj != loc);
14416 list_ret = loc_list_from_tree (obj,
14417 want_address == 2
14418 && !bitpos && !offset ? 2 : 1);
14419 /* TODO: We can extract value of the small expression via shifting even
14420 for nonzero bitpos. */
14421 if (list_ret == 0)
14422 return 0;
14423 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14425 expansion_failed (loc, NULL_RTX,
14426 "bitfield access");
14427 return 0;
14430 if (offset != NULL_TREE)
14432 /* Variable offset. */
14433 list_ret1 = loc_list_from_tree (offset, 0);
14434 if (list_ret1 == 0)
14435 return 0;
14436 add_loc_list (&list_ret, list_ret1);
14437 if (!list_ret)
14438 return 0;
14439 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14442 bytepos = bitpos / BITS_PER_UNIT;
14443 if (bytepos > 0)
14444 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14445 else if (bytepos < 0)
14446 loc_list_plus_const (list_ret, bytepos);
14448 have_address = 1;
14449 break;
14452 case INTEGER_CST:
14453 if ((want_address || !tree_fits_shwi_p (loc))
14454 && (ret = cst_pool_loc_descr (loc)))
14455 have_address = 1;
14456 else if (want_address == 2
14457 && tree_fits_shwi_p (loc)
14458 && (ret = address_of_int_loc_descriptor
14459 (int_size_in_bytes (TREE_TYPE (loc)),
14460 tree_to_shwi (loc))))
14461 have_address = 1;
14462 else if (tree_fits_shwi_p (loc))
14463 ret = int_loc_descriptor (tree_to_shwi (loc));
14464 else
14466 expansion_failed (loc, NULL_RTX,
14467 "Integer operand is not host integer");
14468 return 0;
14470 break;
14472 case CONSTRUCTOR:
14473 case REAL_CST:
14474 case STRING_CST:
14475 case COMPLEX_CST:
14476 if ((ret = cst_pool_loc_descr (loc)))
14477 have_address = 1;
14478 else
14479 /* We can construct small constants here using int_loc_descriptor. */
14480 expansion_failed (loc, NULL_RTX,
14481 "constructor or constant not in constant pool");
14482 break;
14484 case TRUTH_AND_EXPR:
14485 case TRUTH_ANDIF_EXPR:
14486 case BIT_AND_EXPR:
14487 op = DW_OP_and;
14488 goto do_binop;
14490 case TRUTH_XOR_EXPR:
14491 case BIT_XOR_EXPR:
14492 op = DW_OP_xor;
14493 goto do_binop;
14495 case TRUTH_OR_EXPR:
14496 case TRUTH_ORIF_EXPR:
14497 case BIT_IOR_EXPR:
14498 op = DW_OP_or;
14499 goto do_binop;
14501 case FLOOR_DIV_EXPR:
14502 case CEIL_DIV_EXPR:
14503 case ROUND_DIV_EXPR:
14504 case TRUNC_DIV_EXPR:
14505 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14506 return 0;
14507 op = DW_OP_div;
14508 goto do_binop;
14510 case MINUS_EXPR:
14511 op = DW_OP_minus;
14512 goto do_binop;
14514 case FLOOR_MOD_EXPR:
14515 case CEIL_MOD_EXPR:
14516 case ROUND_MOD_EXPR:
14517 case TRUNC_MOD_EXPR:
14518 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14520 op = DW_OP_mod;
14521 goto do_binop;
14523 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14524 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14525 if (list_ret == 0 || list_ret1 == 0)
14526 return 0;
14528 add_loc_list (&list_ret, list_ret1);
14529 if (list_ret == 0)
14530 return 0;
14531 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14532 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14533 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14534 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14535 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14536 break;
14538 case MULT_EXPR:
14539 op = DW_OP_mul;
14540 goto do_binop;
14542 case LSHIFT_EXPR:
14543 op = DW_OP_shl;
14544 goto do_binop;
14546 case RSHIFT_EXPR:
14547 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14548 goto do_binop;
14550 case POINTER_PLUS_EXPR:
14551 case PLUS_EXPR:
14552 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14554 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14555 if (list_ret == 0)
14556 return 0;
14558 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14559 break;
14562 op = DW_OP_plus;
14563 goto do_binop;
14565 case LE_EXPR:
14566 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14567 return 0;
14569 op = DW_OP_le;
14570 goto do_binop;
14572 case GE_EXPR:
14573 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14574 return 0;
14576 op = DW_OP_ge;
14577 goto do_binop;
14579 case LT_EXPR:
14580 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14581 return 0;
14583 op = DW_OP_lt;
14584 goto do_binop;
14586 case GT_EXPR:
14587 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14588 return 0;
14590 op = DW_OP_gt;
14591 goto do_binop;
14593 case EQ_EXPR:
14594 op = DW_OP_eq;
14595 goto do_binop;
14597 case NE_EXPR:
14598 op = DW_OP_ne;
14599 goto do_binop;
14601 do_binop:
14602 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14603 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14604 if (list_ret == 0 || list_ret1 == 0)
14605 return 0;
14607 add_loc_list (&list_ret, list_ret1);
14608 if (list_ret == 0)
14609 return 0;
14610 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14611 break;
14613 case TRUTH_NOT_EXPR:
14614 case BIT_NOT_EXPR:
14615 op = DW_OP_not;
14616 goto do_unop;
14618 case ABS_EXPR:
14619 op = DW_OP_abs;
14620 goto do_unop;
14622 case NEGATE_EXPR:
14623 op = DW_OP_neg;
14624 goto do_unop;
14626 do_unop:
14627 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14628 if (list_ret == 0)
14629 return 0;
14631 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14632 break;
14634 case MIN_EXPR:
14635 case MAX_EXPR:
14637 const enum tree_code code =
14638 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14640 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14641 build2 (code, integer_type_node,
14642 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14643 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14646 /* ... fall through ... */
14648 case COND_EXPR:
14650 dw_loc_descr_ref lhs
14651 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14652 dw_loc_list_ref rhs
14653 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14654 dw_loc_descr_ref bra_node, jump_node, tmp;
14656 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14657 if (list_ret == 0 || lhs == 0 || rhs == 0)
14658 return 0;
14660 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14661 add_loc_descr_to_each (list_ret, bra_node);
14663 add_loc_list (&list_ret, rhs);
14664 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14665 add_loc_descr_to_each (list_ret, jump_node);
14667 add_loc_descr_to_each (list_ret, lhs);
14668 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14669 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14671 /* ??? Need a node to point the skip at. Use a nop. */
14672 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14673 add_loc_descr_to_each (list_ret, tmp);
14674 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14675 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14677 break;
14679 case FIX_TRUNC_EXPR:
14680 return 0;
14682 default:
14683 /* Leave front-end specific codes as simply unknown. This comes
14684 up, for instance, with the C STMT_EXPR. */
14685 if ((unsigned int) TREE_CODE (loc)
14686 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14688 expansion_failed (loc, NULL_RTX,
14689 "language specific tree node");
14690 return 0;
14693 #ifdef ENABLE_CHECKING
14694 /* Otherwise this is a generic code; we should just lists all of
14695 these explicitly. We forgot one. */
14696 gcc_unreachable ();
14697 #else
14698 /* In a release build, we want to degrade gracefully: better to
14699 generate incomplete debugging information than to crash. */
14700 return NULL;
14701 #endif
14704 if (!ret && !list_ret)
14705 return 0;
14707 if (want_address == 2 && !have_address
14708 && (dwarf_version >= 4 || !dwarf_strict))
14710 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14712 expansion_failed (loc, NULL_RTX,
14713 "DWARF address size mismatch");
14714 return 0;
14716 if (ret)
14717 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14718 else
14719 add_loc_descr_to_each (list_ret,
14720 new_loc_descr (DW_OP_stack_value, 0, 0));
14721 have_address = 1;
14723 /* Show if we can't fill the request for an address. */
14724 if (want_address && !have_address)
14726 expansion_failed (loc, NULL_RTX,
14727 "Want address and only have value");
14728 return 0;
14731 gcc_assert (!ret || !list_ret);
14733 /* If we've got an address and don't want one, dereference. */
14734 if (!want_address && have_address)
14736 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14738 if (size > DWARF2_ADDR_SIZE || size == -1)
14740 expansion_failed (loc, NULL_RTX,
14741 "DWARF address size mismatch");
14742 return 0;
14744 else if (size == DWARF2_ADDR_SIZE)
14745 op = DW_OP_deref;
14746 else
14747 op = DW_OP_deref_size;
14749 if (ret)
14750 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14751 else
14752 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14754 if (ret)
14755 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14757 return list_ret;
14760 /* Same as above but return only single location expression. */
14761 static dw_loc_descr_ref
14762 loc_descriptor_from_tree (tree loc, int want_address)
14764 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14765 if (!ret)
14766 return NULL;
14767 if (ret->dw_loc_next)
14769 expansion_failed (loc, NULL_RTX,
14770 "Location list where only loc descriptor needed");
14771 return NULL;
14773 return ret->expr;
14776 /* Given a value, round it up to the lowest multiple of `boundary'
14777 which is not less than the value itself. */
14779 static inline HOST_WIDE_INT
14780 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14782 return (((value + boundary - 1) / boundary) * boundary);
14785 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14786 pointer to the declared type for the relevant field variable, or return
14787 `integer_type_node' if the given node turns out to be an
14788 ERROR_MARK node. */
14790 static inline tree
14791 field_type (const_tree decl)
14793 tree type;
14795 if (TREE_CODE (decl) == ERROR_MARK)
14796 return integer_type_node;
14798 type = DECL_BIT_FIELD_TYPE (decl);
14799 if (type == NULL_TREE)
14800 type = TREE_TYPE (decl);
14802 return type;
14805 /* Given a pointer to a tree node, return the alignment in bits for
14806 it, or else return BITS_PER_WORD if the node actually turns out to
14807 be an ERROR_MARK node. */
14809 static inline unsigned
14810 simple_type_align_in_bits (const_tree type)
14812 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14815 static inline unsigned
14816 simple_decl_align_in_bits (const_tree decl)
14818 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14821 /* Return the result of rounding T up to ALIGN. */
14823 static inline offset_int
14824 round_up_to_align (const offset_int &t, unsigned int align)
14826 return wi::udiv_trunc (t + align - 1, align) * align;
14829 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14830 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14831 or return 0 if we are unable to determine what that offset is, either
14832 because the argument turns out to be a pointer to an ERROR_MARK node, or
14833 because the offset is actually variable. (We can't handle the latter case
14834 just yet). */
14836 static HOST_WIDE_INT
14837 field_byte_offset (const_tree decl)
14839 offset_int object_offset_in_bits;
14840 offset_int object_offset_in_bytes;
14841 offset_int bitpos_int;
14843 if (TREE_CODE (decl) == ERROR_MARK)
14844 return 0;
14846 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14848 /* We cannot yet cope with fields whose positions are variable, so
14849 for now, when we see such things, we simply return 0. Someday, we may
14850 be able to handle such cases, but it will be damn difficult. */
14851 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14852 return 0;
14854 bitpos_int = wi::to_offset (bit_position (decl));
14856 #ifdef PCC_BITFIELD_TYPE_MATTERS
14857 if (PCC_BITFIELD_TYPE_MATTERS)
14859 tree type;
14860 tree field_size_tree;
14861 offset_int deepest_bitpos;
14862 offset_int field_size_in_bits;
14863 unsigned int type_align_in_bits;
14864 unsigned int decl_align_in_bits;
14865 offset_int type_size_in_bits;
14867 type = field_type (decl);
14868 type_size_in_bits = offset_int_type_size_in_bits (type);
14869 type_align_in_bits = simple_type_align_in_bits (type);
14871 field_size_tree = DECL_SIZE (decl);
14873 /* The size could be unspecified if there was an error, or for
14874 a flexible array member. */
14875 if (!field_size_tree)
14876 field_size_tree = bitsize_zero_node;
14878 /* If the size of the field is not constant, use the type size. */
14879 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14880 field_size_in_bits = wi::to_offset (field_size_tree);
14881 else
14882 field_size_in_bits = type_size_in_bits;
14884 decl_align_in_bits = simple_decl_align_in_bits (decl);
14886 /* The GCC front-end doesn't make any attempt to keep track of the
14887 starting bit offset (relative to the start of the containing
14888 structure type) of the hypothetical "containing object" for a
14889 bit-field. Thus, when computing the byte offset value for the
14890 start of the "containing object" of a bit-field, we must deduce
14891 this information on our own. This can be rather tricky to do in
14892 some cases. For example, handling the following structure type
14893 definition when compiling for an i386/i486 target (which only
14894 aligns long long's to 32-bit boundaries) can be very tricky:
14896 struct S { int field1; long long field2:31; };
14898 Fortunately, there is a simple rule-of-thumb which can be used
14899 in such cases. When compiling for an i386/i486, GCC will
14900 allocate 8 bytes for the structure shown above. It decides to
14901 do this based upon one simple rule for bit-field allocation.
14902 GCC allocates each "containing object" for each bit-field at
14903 the first (i.e. lowest addressed) legitimate alignment boundary
14904 (based upon the required minimum alignment for the declared
14905 type of the field) which it can possibly use, subject to the
14906 condition that there is still enough available space remaining
14907 in the containing object (when allocated at the selected point)
14908 to fully accommodate all of the bits of the bit-field itself.
14910 This simple rule makes it obvious why GCC allocates 8 bytes for
14911 each object of the structure type shown above. When looking
14912 for a place to allocate the "containing object" for `field2',
14913 the compiler simply tries to allocate a 64-bit "containing
14914 object" at each successive 32-bit boundary (starting at zero)
14915 until it finds a place to allocate that 64- bit field such that
14916 at least 31 contiguous (and previously unallocated) bits remain
14917 within that selected 64 bit field. (As it turns out, for the
14918 example above, the compiler finds it is OK to allocate the
14919 "containing object" 64-bit field at bit-offset zero within the
14920 structure type.)
14922 Here we attempt to work backwards from the limited set of facts
14923 we're given, and we try to deduce from those facts, where GCC
14924 must have believed that the containing object started (within
14925 the structure type). The value we deduce is then used (by the
14926 callers of this routine) to generate DW_AT_location and
14927 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14928 the case of DW_AT_location, regular fields as well). */
14930 /* Figure out the bit-distance from the start of the structure to
14931 the "deepest" bit of the bit-field. */
14932 deepest_bitpos = bitpos_int + field_size_in_bits;
14934 /* This is the tricky part. Use some fancy footwork to deduce
14935 where the lowest addressed bit of the containing object must
14936 be. */
14937 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14939 /* Round up to type_align by default. This works best for
14940 bitfields. */
14941 object_offset_in_bits
14942 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14944 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
14946 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14948 /* Round up to decl_align instead. */
14949 object_offset_in_bits
14950 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14953 else
14954 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14955 object_offset_in_bits = bitpos_int;
14957 object_offset_in_bytes
14958 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
14959 return object_offset_in_bytes.to_shwi ();
14962 /* The following routines define various Dwarf attributes and any data
14963 associated with them. */
14965 /* Add a location description attribute value to a DIE.
14967 This emits location attributes suitable for whole variables and
14968 whole parameters. Note that the location attributes for struct fields are
14969 generated by the routine `data_member_location_attribute' below. */
14971 static inline void
14972 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14973 dw_loc_list_ref descr)
14975 if (descr == 0)
14976 return;
14977 if (single_element_loc_list_p (descr))
14978 add_AT_loc (die, attr_kind, descr->expr);
14979 else
14980 add_AT_loc_list (die, attr_kind, descr);
14983 /* Add DW_AT_accessibility attribute to DIE if needed. */
14985 static void
14986 add_accessibility_attribute (dw_die_ref die, tree decl)
14988 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14989 children, otherwise the default is DW_ACCESS_public. In DWARF2
14990 the default has always been DW_ACCESS_public. */
14991 if (TREE_PROTECTED (decl))
14992 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14993 else if (TREE_PRIVATE (decl))
14995 if (dwarf_version == 2
14996 || die->die_parent == NULL
14997 || die->die_parent->die_tag != DW_TAG_class_type)
14998 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15000 else if (dwarf_version > 2
15001 && die->die_parent
15002 && die->die_parent->die_tag == DW_TAG_class_type)
15003 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15006 /* Attach the specialized form of location attribute used for data members of
15007 struct and union types. In the special case of a FIELD_DECL node which
15008 represents a bit-field, the "offset" part of this special location
15009 descriptor must indicate the distance in bytes from the lowest-addressed
15010 byte of the containing struct or union type to the lowest-addressed byte of
15011 the "containing object" for the bit-field. (See the `field_byte_offset'
15012 function above).
15014 For any given bit-field, the "containing object" is a hypothetical object
15015 (of some integral or enum type) within which the given bit-field lives. The
15016 type of this hypothetical "containing object" is always the same as the
15017 declared type of the individual bit-field itself (for GCC anyway... the
15018 DWARF spec doesn't actually mandate this). Note that it is the size (in
15019 bytes) of the hypothetical "containing object" which will be given in the
15020 DW_AT_byte_size attribute for this bit-field. (See the
15021 `byte_size_attribute' function below.) It is also used when calculating the
15022 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15023 function below.) */
15025 static void
15026 add_data_member_location_attribute (dw_die_ref die, tree decl)
15028 HOST_WIDE_INT offset;
15029 dw_loc_descr_ref loc_descr = 0;
15031 if (TREE_CODE (decl) == TREE_BINFO)
15033 /* We're working on the TAG_inheritance for a base class. */
15034 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15036 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15037 aren't at a fixed offset from all (sub)objects of the same
15038 type. We need to extract the appropriate offset from our
15039 vtable. The following dwarf expression means
15041 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15043 This is specific to the V3 ABI, of course. */
15045 dw_loc_descr_ref tmp;
15047 /* Make a copy of the object address. */
15048 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15049 add_loc_descr (&loc_descr, tmp);
15051 /* Extract the vtable address. */
15052 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15053 add_loc_descr (&loc_descr, tmp);
15055 /* Calculate the address of the offset. */
15056 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15057 gcc_assert (offset < 0);
15059 tmp = int_loc_descriptor (-offset);
15060 add_loc_descr (&loc_descr, tmp);
15061 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15062 add_loc_descr (&loc_descr, tmp);
15064 /* Extract the offset. */
15065 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15066 add_loc_descr (&loc_descr, tmp);
15068 /* Add it to the object address. */
15069 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15070 add_loc_descr (&loc_descr, tmp);
15072 else
15073 offset = tree_to_shwi (BINFO_OFFSET (decl));
15075 else
15076 offset = field_byte_offset (decl);
15078 if (! loc_descr)
15080 if (dwarf_version > 2)
15082 /* Don't need to output a location expression, just the constant. */
15083 if (offset < 0)
15084 add_AT_int (die, DW_AT_data_member_location, offset);
15085 else
15086 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15087 return;
15089 else
15091 enum dwarf_location_atom op;
15093 /* The DWARF2 standard says that we should assume that the structure
15094 address is already on the stack, so we can specify a structure
15095 field address by using DW_OP_plus_uconst. */
15096 op = DW_OP_plus_uconst;
15097 loc_descr = new_loc_descr (op, offset, 0);
15101 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15104 /* Writes integer values to dw_vec_const array. */
15106 static void
15107 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15109 while (size != 0)
15111 *dest++ = val & 0xff;
15112 val >>= 8;
15113 --size;
15117 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15119 static HOST_WIDE_INT
15120 extract_int (const unsigned char *src, unsigned int size)
15122 HOST_WIDE_INT val = 0;
15124 src += size;
15125 while (size != 0)
15127 val <<= 8;
15128 val |= *--src & 0xff;
15129 --size;
15131 return val;
15134 /* Writes wide_int values to dw_vec_const array. */
15136 static void
15137 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15139 int i;
15141 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15143 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15144 return;
15147 /* We'd have to extend this code to support odd sizes. */
15148 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15150 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15152 if (WORDS_BIG_ENDIAN)
15153 for (i = n - 1; i >= 0; i--)
15155 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15156 dest += sizeof (HOST_WIDE_INT);
15158 else
15159 for (i = 0; i < n; i++)
15161 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15162 dest += sizeof (HOST_WIDE_INT);
15166 /* Writes floating point values to dw_vec_const array. */
15168 static void
15169 insert_float (const_rtx rtl, unsigned char *array)
15171 REAL_VALUE_TYPE rv;
15172 long val[4];
15173 int i;
15175 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15176 real_to_target (val, &rv, GET_MODE (rtl));
15178 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15179 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15181 insert_int (val[i], 4, array);
15182 array += 4;
15186 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15187 does not have a "location" either in memory or in a register. These
15188 things can arise in GNU C when a constant is passed as an actual parameter
15189 to an inlined function. They can also arise in C++ where declared
15190 constants do not necessarily get memory "homes". */
15192 static bool
15193 add_const_value_attribute (dw_die_ref die, rtx rtl)
15195 switch (GET_CODE (rtl))
15197 case CONST_INT:
15199 HOST_WIDE_INT val = INTVAL (rtl);
15201 if (val < 0)
15202 add_AT_int (die, DW_AT_const_value, val);
15203 else
15204 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15206 return true;
15208 case CONST_WIDE_INT:
15209 add_AT_wide (die, DW_AT_const_value,
15210 std::make_pair (rtl, GET_MODE (rtl)));
15211 return true;
15213 case CONST_DOUBLE:
15214 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15215 floating-point constant. A CONST_DOUBLE is used whenever the
15216 constant requires more than one word in order to be adequately
15217 represented. */
15219 enum machine_mode mode = GET_MODE (rtl);
15221 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15222 add_AT_double (die, DW_AT_const_value,
15223 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15224 else
15226 unsigned int length = GET_MODE_SIZE (mode);
15227 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15229 insert_float (rtl, array);
15230 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15233 return true;
15235 case CONST_VECTOR:
15237 enum machine_mode mode = GET_MODE (rtl);
15238 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15239 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15240 unsigned char *array
15241 = ggc_vec_alloc<unsigned char> (length * elt_size);
15242 unsigned int i;
15243 unsigned char *p;
15244 enum machine_mode imode = GET_MODE_INNER (mode);
15246 switch (GET_MODE_CLASS (mode))
15248 case MODE_VECTOR_INT:
15249 for (i = 0, p = array; i < length; i++, p += elt_size)
15251 rtx elt = CONST_VECTOR_ELT (rtl, i);
15252 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15254 break;
15256 case MODE_VECTOR_FLOAT:
15257 for (i = 0, p = array; i < length; i++, p += elt_size)
15259 rtx elt = CONST_VECTOR_ELT (rtl, i);
15260 insert_float (elt, p);
15262 break;
15264 default:
15265 gcc_unreachable ();
15268 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15270 return true;
15272 case CONST_STRING:
15273 if (dwarf_version >= 4 || !dwarf_strict)
15275 dw_loc_descr_ref loc_result;
15276 resolve_one_addr (&rtl, NULL);
15277 rtl_addr:
15278 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15279 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15280 add_AT_loc (die, DW_AT_location, loc_result);
15281 vec_safe_push (used_rtx_array, rtl);
15282 return true;
15284 return false;
15286 case CONST:
15287 if (CONSTANT_P (XEXP (rtl, 0)))
15288 return add_const_value_attribute (die, XEXP (rtl, 0));
15289 /* FALLTHROUGH */
15290 case SYMBOL_REF:
15291 if (!const_ok_for_output (rtl))
15292 return false;
15293 case LABEL_REF:
15294 if (dwarf_version >= 4 || !dwarf_strict)
15295 goto rtl_addr;
15296 return false;
15298 case PLUS:
15299 /* In cases where an inlined instance of an inline function is passed
15300 the address of an `auto' variable (which is local to the caller) we
15301 can get a situation where the DECL_RTL of the artificial local
15302 variable (for the inlining) which acts as a stand-in for the
15303 corresponding formal parameter (of the inline function) will look
15304 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15305 exactly a compile-time constant expression, but it isn't the address
15306 of the (artificial) local variable either. Rather, it represents the
15307 *value* which the artificial local variable always has during its
15308 lifetime. We currently have no way to represent such quasi-constant
15309 values in Dwarf, so for now we just punt and generate nothing. */
15310 return false;
15312 case HIGH:
15313 case CONST_FIXED:
15314 return false;
15316 case MEM:
15317 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15318 && MEM_READONLY_P (rtl)
15319 && GET_MODE (rtl) == BLKmode)
15321 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15322 return true;
15324 return false;
15326 default:
15327 /* No other kinds of rtx should be possible here. */
15328 gcc_unreachable ();
15330 return false;
15333 /* Determine whether the evaluation of EXPR references any variables
15334 or functions which aren't otherwise used (and therefore may not be
15335 output). */
15336 static tree
15337 reference_to_unused (tree * tp, int * walk_subtrees,
15338 void * data ATTRIBUTE_UNUSED)
15340 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15341 *walk_subtrees = 0;
15343 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15344 && ! TREE_ASM_WRITTEN (*tp))
15345 return *tp;
15346 /* ??? The C++ FE emits debug information for using decls, so
15347 putting gcc_unreachable here falls over. See PR31899. For now
15348 be conservative. */
15349 else if (!cgraph_global_info_ready
15350 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15351 return *tp;
15352 else if (TREE_CODE (*tp) == VAR_DECL)
15354 varpool_node *node = varpool_node::get (*tp);
15355 if (!node || !node->definition)
15356 return *tp;
15358 else if (TREE_CODE (*tp) == FUNCTION_DECL
15359 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15361 /* The call graph machinery must have finished analyzing,
15362 optimizing and gimplifying the CU by now.
15363 So if *TP has no call graph node associated
15364 to it, it means *TP will not be emitted. */
15365 if (!cgraph_node::get (*tp))
15366 return *tp;
15368 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15369 return *tp;
15371 return NULL_TREE;
15374 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15375 for use in a later add_const_value_attribute call. */
15377 static rtx
15378 rtl_for_decl_init (tree init, tree type)
15380 rtx rtl = NULL_RTX;
15382 STRIP_NOPS (init);
15384 /* If a variable is initialized with a string constant without embedded
15385 zeros, build CONST_STRING. */
15386 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15388 tree enttype = TREE_TYPE (type);
15389 tree domain = TYPE_DOMAIN (type);
15390 enum machine_mode mode = TYPE_MODE (enttype);
15392 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15393 && domain
15394 && integer_zerop (TYPE_MIN_VALUE (domain))
15395 && compare_tree_int (TYPE_MAX_VALUE (domain),
15396 TREE_STRING_LENGTH (init) - 1) == 0
15397 && ((size_t) TREE_STRING_LENGTH (init)
15398 == strlen (TREE_STRING_POINTER (init)) + 1))
15400 rtl = gen_rtx_CONST_STRING (VOIDmode,
15401 ggc_strdup (TREE_STRING_POINTER (init)));
15402 rtl = gen_rtx_MEM (BLKmode, rtl);
15403 MEM_READONLY_P (rtl) = 1;
15406 /* Other aggregates, and complex values, could be represented using
15407 CONCAT: FIXME! */
15408 else if (AGGREGATE_TYPE_P (type)
15409 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15410 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15411 || TREE_CODE (type) == COMPLEX_TYPE)
15413 /* Vectors only work if their mode is supported by the target.
15414 FIXME: generic vectors ought to work too. */
15415 else if (TREE_CODE (type) == VECTOR_TYPE
15416 && !VECTOR_MODE_P (TYPE_MODE (type)))
15418 /* If the initializer is something that we know will expand into an
15419 immediate RTL constant, expand it now. We must be careful not to
15420 reference variables which won't be output. */
15421 else if (initializer_constant_valid_p (init, type)
15422 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15424 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15425 possible. */
15426 if (TREE_CODE (type) == VECTOR_TYPE)
15427 switch (TREE_CODE (init))
15429 case VECTOR_CST:
15430 break;
15431 case CONSTRUCTOR:
15432 if (TREE_CONSTANT (init))
15434 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15435 bool constant_p = true;
15436 tree value;
15437 unsigned HOST_WIDE_INT ix;
15439 /* Even when ctor is constant, it might contain non-*_CST
15440 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15441 belong into VECTOR_CST nodes. */
15442 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15443 if (!CONSTANT_CLASS_P (value))
15445 constant_p = false;
15446 break;
15449 if (constant_p)
15451 init = build_vector_from_ctor (type, elts);
15452 break;
15455 /* FALLTHRU */
15457 default:
15458 return NULL;
15461 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15463 /* If expand_expr returns a MEM, it wasn't immediate. */
15464 gcc_assert (!rtl || !MEM_P (rtl));
15467 return rtl;
15470 /* Generate RTL for the variable DECL to represent its location. */
15472 static rtx
15473 rtl_for_decl_location (tree decl)
15475 rtx rtl;
15477 /* Here we have to decide where we are going to say the parameter "lives"
15478 (as far as the debugger is concerned). We only have a couple of
15479 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15481 DECL_RTL normally indicates where the parameter lives during most of the
15482 activation of the function. If optimization is enabled however, this
15483 could be either NULL or else a pseudo-reg. Both of those cases indicate
15484 that the parameter doesn't really live anywhere (as far as the code
15485 generation parts of GCC are concerned) during most of the function's
15486 activation. That will happen (for example) if the parameter is never
15487 referenced within the function.
15489 We could just generate a location descriptor here for all non-NULL
15490 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15491 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15492 where DECL_RTL is NULL or is a pseudo-reg.
15494 Note however that we can only get away with using DECL_INCOMING_RTL as
15495 a backup substitute for DECL_RTL in certain limited cases. In cases
15496 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15497 we can be sure that the parameter was passed using the same type as it is
15498 declared to have within the function, and that its DECL_INCOMING_RTL
15499 points us to a place where a value of that type is passed.
15501 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15502 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15503 because in these cases DECL_INCOMING_RTL points us to a value of some
15504 type which is *different* from the type of the parameter itself. Thus,
15505 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15506 such cases, the debugger would end up (for example) trying to fetch a
15507 `float' from a place which actually contains the first part of a
15508 `double'. That would lead to really incorrect and confusing
15509 output at debug-time.
15511 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15512 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15513 are a couple of exceptions however. On little-endian machines we can
15514 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15515 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15516 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15517 when (on a little-endian machine) a non-prototyped function has a
15518 parameter declared to be of type `short' or `char'. In such cases,
15519 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15520 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15521 passed `int' value. If the debugger then uses that address to fetch
15522 a `short' or a `char' (on a little-endian machine) the result will be
15523 the correct data, so we allow for such exceptional cases below.
15525 Note that our goal here is to describe the place where the given formal
15526 parameter lives during most of the function's activation (i.e. between the
15527 end of the prologue and the start of the epilogue). We'll do that as best
15528 as we can. Note however that if the given formal parameter is modified
15529 sometime during the execution of the function, then a stack backtrace (at
15530 debug-time) will show the function as having been called with the *new*
15531 value rather than the value which was originally passed in. This happens
15532 rarely enough that it is not a major problem, but it *is* a problem, and
15533 I'd like to fix it.
15535 A future version of dwarf2out.c may generate two additional attributes for
15536 any given DW_TAG_formal_parameter DIE which will describe the "passed
15537 type" and the "passed location" for the given formal parameter in addition
15538 to the attributes we now generate to indicate the "declared type" and the
15539 "active location" for each parameter. This additional set of attributes
15540 could be used by debuggers for stack backtraces. Separately, note that
15541 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15542 This happens (for example) for inlined-instances of inline function formal
15543 parameters which are never referenced. This really shouldn't be
15544 happening. All PARM_DECL nodes should get valid non-NULL
15545 DECL_INCOMING_RTL values. FIXME. */
15547 /* Use DECL_RTL as the "location" unless we find something better. */
15548 rtl = DECL_RTL_IF_SET (decl);
15550 /* When generating abstract instances, ignore everything except
15551 constants, symbols living in memory, and symbols living in
15552 fixed registers. */
15553 if (! reload_completed)
15555 if (rtl
15556 && (CONSTANT_P (rtl)
15557 || (MEM_P (rtl)
15558 && CONSTANT_P (XEXP (rtl, 0)))
15559 || (REG_P (rtl)
15560 && TREE_CODE (decl) == VAR_DECL
15561 && TREE_STATIC (decl))))
15563 rtl = targetm.delegitimize_address (rtl);
15564 return rtl;
15566 rtl = NULL_RTX;
15568 else if (TREE_CODE (decl) == PARM_DECL)
15570 if (rtl == NULL_RTX
15571 || is_pseudo_reg (rtl)
15572 || (MEM_P (rtl)
15573 && is_pseudo_reg (XEXP (rtl, 0))
15574 && DECL_INCOMING_RTL (decl)
15575 && MEM_P (DECL_INCOMING_RTL (decl))
15576 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15578 tree declared_type = TREE_TYPE (decl);
15579 tree passed_type = DECL_ARG_TYPE (decl);
15580 enum machine_mode dmode = TYPE_MODE (declared_type);
15581 enum machine_mode pmode = TYPE_MODE (passed_type);
15583 /* This decl represents a formal parameter which was optimized out.
15584 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15585 all cases where (rtl == NULL_RTX) just below. */
15586 if (dmode == pmode)
15587 rtl = DECL_INCOMING_RTL (decl);
15588 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15589 && SCALAR_INT_MODE_P (dmode)
15590 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15591 && DECL_INCOMING_RTL (decl))
15593 rtx inc = DECL_INCOMING_RTL (decl);
15594 if (REG_P (inc))
15595 rtl = inc;
15596 else if (MEM_P (inc))
15598 if (BYTES_BIG_ENDIAN)
15599 rtl = adjust_address_nv (inc, dmode,
15600 GET_MODE_SIZE (pmode)
15601 - GET_MODE_SIZE (dmode));
15602 else
15603 rtl = inc;
15608 /* If the parm was passed in registers, but lives on the stack, then
15609 make a big endian correction if the mode of the type of the
15610 parameter is not the same as the mode of the rtl. */
15611 /* ??? This is the same series of checks that are made in dbxout.c before
15612 we reach the big endian correction code there. It isn't clear if all
15613 of these checks are necessary here, but keeping them all is the safe
15614 thing to do. */
15615 else if (MEM_P (rtl)
15616 && XEXP (rtl, 0) != const0_rtx
15617 && ! CONSTANT_P (XEXP (rtl, 0))
15618 /* Not passed in memory. */
15619 && !MEM_P (DECL_INCOMING_RTL (decl))
15620 /* Not passed by invisible reference. */
15621 && (!REG_P (XEXP (rtl, 0))
15622 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15623 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15624 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15625 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15626 #endif
15628 /* Big endian correction check. */
15629 && BYTES_BIG_ENDIAN
15630 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15631 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15632 < UNITS_PER_WORD))
15634 enum machine_mode addr_mode = get_address_mode (rtl);
15635 int offset = (UNITS_PER_WORD
15636 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15638 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15639 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15642 else if (TREE_CODE (decl) == VAR_DECL
15643 && rtl
15644 && MEM_P (rtl)
15645 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15646 && BYTES_BIG_ENDIAN)
15648 enum machine_mode addr_mode = get_address_mode (rtl);
15649 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15650 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15652 /* If a variable is declared "register" yet is smaller than
15653 a register, then if we store the variable to memory, it
15654 looks like we're storing a register-sized value, when in
15655 fact we are not. We need to adjust the offset of the
15656 storage location to reflect the actual value's bytes,
15657 else gdb will not be able to display it. */
15658 if (rsize > dsize)
15659 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15660 plus_constant (addr_mode, XEXP (rtl, 0),
15661 rsize - dsize));
15664 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15665 and will have been substituted directly into all expressions that use it.
15666 C does not have such a concept, but C++ and other languages do. */
15667 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15668 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15670 if (rtl)
15671 rtl = targetm.delegitimize_address (rtl);
15673 /* If we don't look past the constant pool, we risk emitting a
15674 reference to a constant pool entry that isn't referenced from
15675 code, and thus is not emitted. */
15676 if (rtl)
15677 rtl = avoid_constant_pool_reference (rtl);
15679 /* Try harder to get a rtl. If this symbol ends up not being emitted
15680 in the current CU, resolve_addr will remove the expression referencing
15681 it. */
15682 if (rtl == NULL_RTX
15683 && TREE_CODE (decl) == VAR_DECL
15684 && !DECL_EXTERNAL (decl)
15685 && TREE_STATIC (decl)
15686 && DECL_NAME (decl)
15687 && !DECL_HARD_REGISTER (decl)
15688 && DECL_MODE (decl) != VOIDmode)
15690 rtl = make_decl_rtl_for_debug (decl);
15691 if (!MEM_P (rtl)
15692 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15693 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15694 rtl = NULL_RTX;
15697 return rtl;
15700 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15701 returned. If so, the decl for the COMMON block is returned, and the
15702 value is the offset into the common block for the symbol. */
15704 static tree
15705 fortran_common (tree decl, HOST_WIDE_INT *value)
15707 tree val_expr, cvar;
15708 enum machine_mode mode;
15709 HOST_WIDE_INT bitsize, bitpos;
15710 tree offset;
15711 int unsignedp, volatilep = 0;
15713 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15714 it does not have a value (the offset into the common area), or if it
15715 is thread local (as opposed to global) then it isn't common, and shouldn't
15716 be handled as such. */
15717 if (TREE_CODE (decl) != VAR_DECL
15718 || !TREE_STATIC (decl)
15719 || !DECL_HAS_VALUE_EXPR_P (decl)
15720 || !is_fortran ())
15721 return NULL_TREE;
15723 val_expr = DECL_VALUE_EXPR (decl);
15724 if (TREE_CODE (val_expr) != COMPONENT_REF)
15725 return NULL_TREE;
15727 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15728 &mode, &unsignedp, &volatilep, true);
15730 if (cvar == NULL_TREE
15731 || TREE_CODE (cvar) != VAR_DECL
15732 || DECL_ARTIFICIAL (cvar)
15733 || !TREE_PUBLIC (cvar))
15734 return NULL_TREE;
15736 *value = 0;
15737 if (offset != NULL)
15739 if (!tree_fits_shwi_p (offset))
15740 return NULL_TREE;
15741 *value = tree_to_shwi (offset);
15743 if (bitpos != 0)
15744 *value += bitpos / BITS_PER_UNIT;
15746 return cvar;
15749 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15750 data attribute for a variable or a parameter. We generate the
15751 DW_AT_const_value attribute only in those cases where the given variable
15752 or parameter does not have a true "location" either in memory or in a
15753 register. This can happen (for example) when a constant is passed as an
15754 actual argument in a call to an inline function. (It's possible that
15755 these things can crop up in other ways also.) Note that one type of
15756 constant value which can be passed into an inlined function is a constant
15757 pointer. This can happen for example if an actual argument in an inlined
15758 function call evaluates to a compile-time constant address.
15760 CACHE_P is true if it is worth caching the location list for DECL,
15761 so that future calls can reuse it rather than regenerate it from scratch.
15762 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15763 since we will need to refer to them each time the function is inlined. */
15765 static bool
15766 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15767 enum dwarf_attribute attr)
15769 rtx rtl;
15770 dw_loc_list_ref list;
15771 var_loc_list *loc_list;
15772 cached_dw_loc_list *cache;
15773 void **slot;
15775 if (TREE_CODE (decl) == ERROR_MARK)
15776 return false;
15778 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15779 || TREE_CODE (decl) == RESULT_DECL);
15781 /* Try to get some constant RTL for this decl, and use that as the value of
15782 the location. */
15784 rtl = rtl_for_decl_location (decl);
15785 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15786 && add_const_value_attribute (die, rtl))
15787 return true;
15789 /* See if we have single element location list that is equivalent to
15790 a constant value. That way we are better to use add_const_value_attribute
15791 rather than expanding constant value equivalent. */
15792 loc_list = lookup_decl_loc (decl);
15793 if (loc_list
15794 && loc_list->first
15795 && loc_list->first->next == NULL
15796 && NOTE_P (loc_list->first->loc)
15797 && NOTE_VAR_LOCATION (loc_list->first->loc)
15798 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15800 struct var_loc_node *node;
15802 node = loc_list->first;
15803 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15804 if (GET_CODE (rtl) == EXPR_LIST)
15805 rtl = XEXP (rtl, 0);
15806 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15807 && add_const_value_attribute (die, rtl))
15808 return true;
15810 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15811 list several times. See if we've already cached the contents. */
15812 list = NULL;
15813 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15814 cache_p = false;
15815 if (cache_p)
15817 cache = (cached_dw_loc_list *)
15818 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15819 if (cache)
15820 list = cache->loc_list;
15822 if (list == NULL)
15824 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15825 /* It is usually worth caching this result if the decl is from
15826 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15827 if (cache_p && list && list->dw_loc_next)
15829 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15830 DECL_UID (decl), INSERT);
15831 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15832 cache->decl_id = DECL_UID (decl);
15833 cache->loc_list = list;
15834 *slot = cache;
15837 if (list)
15839 add_AT_location_description (die, attr, list);
15840 return true;
15842 /* None of that worked, so it must not really have a location;
15843 try adding a constant value attribute from the DECL_INITIAL. */
15844 return tree_add_const_value_attribute_for_decl (die, decl);
15847 /* Add VARIABLE and DIE into deferred locations list. */
15849 static void
15850 defer_location (tree variable, dw_die_ref die)
15852 deferred_locations entry;
15853 entry.variable = variable;
15854 entry.die = die;
15855 vec_safe_push (deferred_locations_list, entry);
15858 /* Helper function for tree_add_const_value_attribute. Natively encode
15859 initializer INIT into an array. Return true if successful. */
15861 static bool
15862 native_encode_initializer (tree init, unsigned char *array, int size)
15864 tree type;
15866 if (init == NULL_TREE)
15867 return false;
15869 STRIP_NOPS (init);
15870 switch (TREE_CODE (init))
15872 case STRING_CST:
15873 type = TREE_TYPE (init);
15874 if (TREE_CODE (type) == ARRAY_TYPE)
15876 tree enttype = TREE_TYPE (type);
15877 enum machine_mode mode = TYPE_MODE (enttype);
15879 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15880 return false;
15881 if (int_size_in_bytes (type) != size)
15882 return false;
15883 if (size > TREE_STRING_LENGTH (init))
15885 memcpy (array, TREE_STRING_POINTER (init),
15886 TREE_STRING_LENGTH (init));
15887 memset (array + TREE_STRING_LENGTH (init),
15888 '\0', size - TREE_STRING_LENGTH (init));
15890 else
15891 memcpy (array, TREE_STRING_POINTER (init), size);
15892 return true;
15894 return false;
15895 case CONSTRUCTOR:
15896 type = TREE_TYPE (init);
15897 if (int_size_in_bytes (type) != size)
15898 return false;
15899 if (TREE_CODE (type) == ARRAY_TYPE)
15901 HOST_WIDE_INT min_index;
15902 unsigned HOST_WIDE_INT cnt;
15903 int curpos = 0, fieldsize;
15904 constructor_elt *ce;
15906 if (TYPE_DOMAIN (type) == NULL_TREE
15907 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15908 return false;
15910 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15911 if (fieldsize <= 0)
15912 return false;
15914 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15915 memset (array, '\0', size);
15916 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15918 tree val = ce->value;
15919 tree index = ce->index;
15920 int pos = curpos;
15921 if (index && TREE_CODE (index) == RANGE_EXPR)
15922 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15923 * fieldsize;
15924 else if (index)
15925 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15927 if (val)
15929 STRIP_NOPS (val);
15930 if (!native_encode_initializer (val, array + pos, fieldsize))
15931 return false;
15933 curpos = pos + fieldsize;
15934 if (index && TREE_CODE (index) == RANGE_EXPR)
15936 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15937 - tree_to_shwi (TREE_OPERAND (index, 0));
15938 while (count-- > 0)
15940 if (val)
15941 memcpy (array + curpos, array + pos, fieldsize);
15942 curpos += fieldsize;
15945 gcc_assert (curpos <= size);
15947 return true;
15949 else if (TREE_CODE (type) == RECORD_TYPE
15950 || TREE_CODE (type) == UNION_TYPE)
15952 tree field = NULL_TREE;
15953 unsigned HOST_WIDE_INT cnt;
15954 constructor_elt *ce;
15956 if (int_size_in_bytes (type) != size)
15957 return false;
15959 if (TREE_CODE (type) == RECORD_TYPE)
15960 field = TYPE_FIELDS (type);
15962 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15964 tree val = ce->value;
15965 int pos, fieldsize;
15967 if (ce->index != 0)
15968 field = ce->index;
15970 if (val)
15971 STRIP_NOPS (val);
15973 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15974 return false;
15976 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15977 && TYPE_DOMAIN (TREE_TYPE (field))
15978 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15979 return false;
15980 else if (DECL_SIZE_UNIT (field) == NULL_TREE
15981 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
15982 return false;
15983 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
15984 pos = int_byte_position (field);
15985 gcc_assert (pos + fieldsize <= size);
15986 if (val
15987 && !native_encode_initializer (val, array + pos, fieldsize))
15988 return false;
15990 return true;
15992 return false;
15993 case VIEW_CONVERT_EXPR:
15994 case NON_LVALUE_EXPR:
15995 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15996 default:
15997 return native_encode_expr (init, array, size) == size;
16001 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16002 attribute is the const value T. */
16004 static bool
16005 tree_add_const_value_attribute (dw_die_ref die, tree t)
16007 tree init;
16008 tree type = TREE_TYPE (t);
16009 rtx rtl;
16011 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16012 return false;
16014 init = t;
16015 gcc_assert (!DECL_P (init));
16017 rtl = rtl_for_decl_init (init, type);
16018 if (rtl)
16019 return add_const_value_attribute (die, rtl);
16020 /* If the host and target are sane, try harder. */
16021 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16022 && initializer_constant_valid_p (init, type))
16024 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16025 if (size > 0 && (int) size == size)
16027 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16029 if (native_encode_initializer (init, array, size))
16031 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16032 return true;
16034 ggc_free (array);
16037 return false;
16040 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16041 attribute is the const value of T, where T is an integral constant
16042 variable with static storage duration
16043 (so it can't be a PARM_DECL or a RESULT_DECL). */
16045 static bool
16046 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16049 if (!decl
16050 || (TREE_CODE (decl) != VAR_DECL
16051 && TREE_CODE (decl) != CONST_DECL)
16052 || (TREE_CODE (decl) == VAR_DECL
16053 && !TREE_STATIC (decl)))
16054 return false;
16056 if (TREE_READONLY (decl)
16057 && ! TREE_THIS_VOLATILE (decl)
16058 && DECL_INITIAL (decl))
16059 /* OK */;
16060 else
16061 return false;
16063 /* Don't add DW_AT_const_value if abstract origin already has one. */
16064 if (get_AT (var_die, DW_AT_const_value))
16065 return false;
16067 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16070 /* Convert the CFI instructions for the current function into a
16071 location list. This is used for DW_AT_frame_base when we targeting
16072 a dwarf2 consumer that does not support the dwarf3
16073 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16074 expressions. */
16076 static dw_loc_list_ref
16077 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16079 int ix;
16080 dw_fde_ref fde;
16081 dw_loc_list_ref list, *list_tail;
16082 dw_cfi_ref cfi;
16083 dw_cfa_location last_cfa, next_cfa;
16084 const char *start_label, *last_label, *section;
16085 dw_cfa_location remember;
16087 fde = cfun->fde;
16088 gcc_assert (fde != NULL);
16090 section = secname_for_decl (current_function_decl);
16091 list_tail = &list;
16092 list = NULL;
16094 memset (&next_cfa, 0, sizeof (next_cfa));
16095 next_cfa.reg = INVALID_REGNUM;
16096 remember = next_cfa;
16098 start_label = fde->dw_fde_begin;
16100 /* ??? Bald assumption that the CIE opcode list does not contain
16101 advance opcodes. */
16102 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16103 lookup_cfa_1 (cfi, &next_cfa, &remember);
16105 last_cfa = next_cfa;
16106 last_label = start_label;
16108 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16110 /* If the first partition contained no CFI adjustments, the
16111 CIE opcodes apply to the whole first partition. */
16112 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16113 fde->dw_fde_begin, fde->dw_fde_end, section);
16114 list_tail =&(*list_tail)->dw_loc_next;
16115 start_label = last_label = fde->dw_fde_second_begin;
16118 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16120 switch (cfi->dw_cfi_opc)
16122 case DW_CFA_set_loc:
16123 case DW_CFA_advance_loc1:
16124 case DW_CFA_advance_loc2:
16125 case DW_CFA_advance_loc4:
16126 if (!cfa_equal_p (&last_cfa, &next_cfa))
16128 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16129 start_label, last_label, section);
16131 list_tail = &(*list_tail)->dw_loc_next;
16132 last_cfa = next_cfa;
16133 start_label = last_label;
16135 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16136 break;
16138 case DW_CFA_advance_loc:
16139 /* The encoding is complex enough that we should never emit this. */
16140 gcc_unreachable ();
16142 default:
16143 lookup_cfa_1 (cfi, &next_cfa, &remember);
16144 break;
16146 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16148 if (!cfa_equal_p (&last_cfa, &next_cfa))
16150 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16151 start_label, last_label, section);
16153 list_tail = &(*list_tail)->dw_loc_next;
16154 last_cfa = next_cfa;
16155 start_label = last_label;
16157 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16158 start_label, fde->dw_fde_end, section);
16159 list_tail = &(*list_tail)->dw_loc_next;
16160 start_label = last_label = fde->dw_fde_second_begin;
16164 if (!cfa_equal_p (&last_cfa, &next_cfa))
16166 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16167 start_label, last_label, section);
16168 list_tail = &(*list_tail)->dw_loc_next;
16169 start_label = last_label;
16172 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16173 start_label,
16174 fde->dw_fde_second_begin
16175 ? fde->dw_fde_second_end : fde->dw_fde_end,
16176 section);
16178 if (list && list->dw_loc_next)
16179 gen_llsym (list);
16181 return list;
16184 /* Compute a displacement from the "steady-state frame pointer" to the
16185 frame base (often the same as the CFA), and store it in
16186 frame_pointer_fb_offset. OFFSET is added to the displacement
16187 before the latter is negated. */
16189 static void
16190 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16192 rtx reg, elim;
16194 #ifdef FRAME_POINTER_CFA_OFFSET
16195 reg = frame_pointer_rtx;
16196 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16197 #else
16198 reg = arg_pointer_rtx;
16199 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16200 #endif
16202 elim = (ira_use_lra_p
16203 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16204 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16205 if (GET_CODE (elim) == PLUS)
16207 offset += INTVAL (XEXP (elim, 1));
16208 elim = XEXP (elim, 0);
16211 frame_pointer_fb_offset = -offset;
16213 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16214 in which to eliminate. This is because it's stack pointer isn't
16215 directly accessible as a register within the ISA. To work around
16216 this, assume that while we cannot provide a proper value for
16217 frame_pointer_fb_offset, we won't need one either. */
16218 frame_pointer_fb_offset_valid
16219 = ((SUPPORTS_STACK_ALIGNMENT
16220 && (elim == hard_frame_pointer_rtx
16221 || elim == stack_pointer_rtx))
16222 || elim == (frame_pointer_needed
16223 ? hard_frame_pointer_rtx
16224 : stack_pointer_rtx));
16227 /* Generate a DW_AT_name attribute given some string value to be included as
16228 the value of the attribute. */
16230 static void
16231 add_name_attribute (dw_die_ref die, const char *name_string)
16233 if (name_string != NULL && *name_string != 0)
16235 if (demangle_name_func)
16236 name_string = (*demangle_name_func) (name_string);
16238 add_AT_string (die, DW_AT_name, name_string);
16242 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16243 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16244 of TYPE accordingly.
16246 ??? This is a temporary measure until after we're able to generate
16247 regular DWARF for the complex Ada type system. */
16249 static void
16250 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16251 dw_die_ref context_die)
16253 tree dtype;
16254 dw_die_ref dtype_die;
16256 if (!lang_hooks.types.descriptive_type)
16257 return;
16259 dtype = lang_hooks.types.descriptive_type (type);
16260 if (!dtype)
16261 return;
16263 dtype_die = lookup_type_die (dtype);
16264 if (!dtype_die)
16266 gen_type_die (dtype, context_die);
16267 dtype_die = lookup_type_die (dtype);
16268 gcc_assert (dtype_die);
16271 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16274 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16276 static const char *
16277 comp_dir_string (void)
16279 const char *wd;
16280 char *wd1;
16281 static const char *cached_wd = NULL;
16283 if (cached_wd != NULL)
16284 return cached_wd;
16286 wd = get_src_pwd ();
16287 if (wd == NULL)
16288 return NULL;
16290 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16292 int wdlen;
16294 wdlen = strlen (wd);
16295 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16296 strcpy (wd1, wd);
16297 wd1 [wdlen] = DIR_SEPARATOR;
16298 wd1 [wdlen + 1] = 0;
16299 wd = wd1;
16302 cached_wd = remap_debug_filename (wd);
16303 return cached_wd;
16306 /* Generate a DW_AT_comp_dir attribute for DIE. */
16308 static void
16309 add_comp_dir_attribute (dw_die_ref die)
16311 const char * wd = comp_dir_string ();
16312 if (wd != NULL)
16313 add_AT_string (die, DW_AT_comp_dir, wd);
16316 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16317 default. */
16319 static int
16320 lower_bound_default (void)
16322 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16324 case DW_LANG_C:
16325 case DW_LANG_C89:
16326 case DW_LANG_C99:
16327 case DW_LANG_C_plus_plus:
16328 case DW_LANG_ObjC:
16329 case DW_LANG_ObjC_plus_plus:
16330 case DW_LANG_Java:
16331 return 0;
16332 case DW_LANG_Fortran77:
16333 case DW_LANG_Fortran90:
16334 case DW_LANG_Fortran95:
16335 return 1;
16336 case DW_LANG_UPC:
16337 case DW_LANG_D:
16338 case DW_LANG_Python:
16339 return dwarf_version >= 4 ? 0 : -1;
16340 case DW_LANG_Ada95:
16341 case DW_LANG_Ada83:
16342 case DW_LANG_Cobol74:
16343 case DW_LANG_Cobol85:
16344 case DW_LANG_Pascal83:
16345 case DW_LANG_Modula2:
16346 case DW_LANG_PLI:
16347 return dwarf_version >= 4 ? 1 : -1;
16348 default:
16349 return -1;
16353 /* Given a tree node describing an array bound (either lower or upper) output
16354 a representation for that bound. */
16356 static void
16357 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16359 switch (TREE_CODE (bound))
16361 case ERROR_MARK:
16362 return;
16364 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16365 case INTEGER_CST:
16367 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16368 int dflt;
16370 /* Use the default if possible. */
16371 if (bound_attr == DW_AT_lower_bound
16372 && tree_fits_shwi_p (bound)
16373 && (dflt = lower_bound_default ()) != -1
16374 && tree_to_shwi (bound) == dflt)
16377 /* If HOST_WIDE_INT is big enough then represent the bound as
16378 a constant value. We need to choose a form based on
16379 whether the type is signed or unsigned. We cannot just
16380 call add_AT_unsigned if the value itself is positive
16381 (add_AT_unsigned might add the unsigned value encoded as
16382 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16383 bounds type and then sign extend any unsigned values found
16384 for signed types. This is needed only for
16385 DW_AT_{lower,upper}_bound, since for most other attributes,
16386 consumers will treat DW_FORM_data[1248] as unsigned values,
16387 regardless of the underlying type. */
16388 else if (prec <= HOST_BITS_PER_WIDE_INT
16389 || tree_fits_uhwi_p (bound))
16391 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16392 add_AT_unsigned (subrange_die, bound_attr,
16393 TREE_INT_CST_LOW (bound));
16394 else
16395 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16397 else
16398 /* Otherwise represent the bound as an unsigned value with
16399 the precision of its type. The precision and signedness
16400 of the type will be necessary to re-interpret it
16401 unambiguously. */
16402 add_AT_wide (subrange_die, bound_attr, bound);
16404 break;
16406 CASE_CONVERT:
16407 case VIEW_CONVERT_EXPR:
16408 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16409 break;
16411 case SAVE_EXPR:
16412 break;
16414 case VAR_DECL:
16415 case PARM_DECL:
16416 case RESULT_DECL:
16418 dw_die_ref decl_die = lookup_decl_die (bound);
16420 /* ??? Can this happen, or should the variable have been bound
16421 first? Probably it can, since I imagine that we try to create
16422 the types of parameters in the order in which they exist in
16423 the list, and won't have created a forward reference to a
16424 later parameter. */
16425 if (decl_die != NULL)
16427 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16428 break;
16431 /* FALLTHRU */
16433 default:
16435 /* Otherwise try to create a stack operation procedure to
16436 evaluate the value of the array bound. */
16438 dw_die_ref ctx, decl_die;
16439 dw_loc_list_ref list;
16441 list = loc_list_from_tree (bound, 2);
16442 if (list == NULL || single_element_loc_list_p (list))
16444 /* If DW_AT_*bound is not a reference nor constant, it is
16445 a DWARF expression rather than location description.
16446 For that loc_list_from_tree (bound, 0) is needed.
16447 If that fails to give a single element list,
16448 fall back to outputting this as a reference anyway. */
16449 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16450 if (list2 && single_element_loc_list_p (list2))
16452 add_AT_loc (subrange_die, bound_attr, list2->expr);
16453 break;
16456 if (list == NULL)
16457 break;
16459 if (current_function_decl == 0)
16460 ctx = comp_unit_die ();
16461 else
16462 ctx = lookup_decl_die (current_function_decl);
16464 decl_die = new_die (DW_TAG_variable, ctx, bound);
16465 add_AT_flag (decl_die, DW_AT_artificial, 1);
16466 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16467 add_AT_location_description (decl_die, DW_AT_location, list);
16468 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16469 break;
16474 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16475 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16476 Note that the block of subscript information for an array type also
16477 includes information about the element type of the given array type. */
16479 static void
16480 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16482 unsigned dimension_number;
16483 tree lower, upper;
16484 dw_die_ref subrange_die;
16486 for (dimension_number = 0;
16487 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16488 type = TREE_TYPE (type), dimension_number++)
16490 tree domain = TYPE_DOMAIN (type);
16492 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16493 break;
16495 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16496 and (in GNU C only) variable bounds. Handle all three forms
16497 here. */
16498 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16500 if (use_upc_dwarf2_extensions && TYPE_HAS_THREADS_FACTOR (type))
16502 add_AT_flag (subrange_die, DW_AT_upc_threads_scaled, 1);
16505 if (domain)
16507 /* We have an array type with specified bounds. */
16508 lower = TYPE_MIN_VALUE (domain);
16509 upper = TYPE_MAX_VALUE (domain);
16511 /* Define the index type. */
16512 if (TREE_TYPE (domain))
16514 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16515 TREE_TYPE field. We can't emit debug info for this
16516 because it is an unnamed integral type. */
16517 if (TREE_CODE (domain) == INTEGER_TYPE
16518 && TYPE_NAME (domain) == NULL_TREE
16519 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16520 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16522 else
16523 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16524 type_die);
16527 /* ??? If upper is NULL, the array has unspecified length,
16528 but it does have a lower bound. This happens with Fortran
16529 dimension arr(N:*)
16530 Since the debugger is definitely going to need to know N
16531 to produce useful results, go ahead and output the lower
16532 bound solo, and hope the debugger can cope. */
16534 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16535 if (upper)
16536 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16539 /* Otherwise we have an array type with an unspecified length. The
16540 DWARF-2 spec does not say how to handle this; let's just leave out the
16541 bounds. */
16545 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16547 static void
16548 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16550 dw_die_ref decl_die;
16551 HOST_WIDE_INT size;
16553 switch (TREE_CODE (tree_node))
16555 case ERROR_MARK:
16556 size = 0;
16557 break;
16558 case ENUMERAL_TYPE:
16559 case RECORD_TYPE:
16560 case UNION_TYPE:
16561 case QUAL_UNION_TYPE:
16562 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16563 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16565 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16566 return;
16568 size = int_size_in_bytes (tree_node);
16569 break;
16570 case FIELD_DECL:
16571 /* For a data member of a struct or union, the DW_AT_byte_size is
16572 generally given as the number of bytes normally allocated for an
16573 object of the *declared* type of the member itself. This is true
16574 even for bit-fields. */
16575 size = int_size_in_bytes (field_type (tree_node));
16576 break;
16577 default:
16578 gcc_unreachable ();
16581 /* Note that `size' might be -1 when we get to this point. If it is, that
16582 indicates that the byte size of the entity in question is variable. We
16583 have no good way of expressing this fact in Dwarf at the present time,
16584 when location description was not used by the caller code instead. */
16585 if (size >= 0)
16586 add_AT_unsigned (die, DW_AT_byte_size, size);
16589 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16590 which specifies the distance in bits from the highest order bit of the
16591 "containing object" for the bit-field to the highest order bit of the
16592 bit-field itself.
16594 For any given bit-field, the "containing object" is a hypothetical object
16595 (of some integral or enum type) within which the given bit-field lives. The
16596 type of this hypothetical "containing object" is always the same as the
16597 declared type of the individual bit-field itself. The determination of the
16598 exact location of the "containing object" for a bit-field is rather
16599 complicated. It's handled by the `field_byte_offset' function (above).
16601 Note that it is the size (in bytes) of the hypothetical "containing object"
16602 which will be given in the DW_AT_byte_size attribute for this bit-field.
16603 (See `byte_size_attribute' above). */
16605 static inline void
16606 add_bit_offset_attribute (dw_die_ref die, tree decl)
16608 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16609 tree type = DECL_BIT_FIELD_TYPE (decl);
16610 HOST_WIDE_INT bitpos_int;
16611 HOST_WIDE_INT highest_order_object_bit_offset;
16612 HOST_WIDE_INT highest_order_field_bit_offset;
16613 HOST_WIDE_INT bit_offset;
16615 /* Must be a field and a bit field. */
16616 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16618 /* We can't yet handle bit-fields whose offsets are variable, so if we
16619 encounter such things, just return without generating any attribute
16620 whatsoever. Likewise for variable or too large size. */
16621 if (! tree_fits_shwi_p (bit_position (decl))
16622 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16623 return;
16625 bitpos_int = int_bit_position (decl);
16627 /* Note that the bit offset is always the distance (in bits) from the
16628 highest-order bit of the "containing object" to the highest-order bit of
16629 the bit-field itself. Since the "high-order end" of any object or field
16630 is different on big-endian and little-endian machines, the computation
16631 below must take account of these differences. */
16632 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16633 highest_order_field_bit_offset = bitpos_int;
16635 if (! BYTES_BIG_ENDIAN)
16637 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16638 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16641 bit_offset
16642 = (! BYTES_BIG_ENDIAN
16643 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16644 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16646 if (bit_offset < 0)
16647 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16648 else
16649 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16652 /* For a FIELD_DECL node which represents a bit field, output an attribute
16653 which specifies the length in bits of the given field. */
16655 static inline void
16656 add_bit_size_attribute (dw_die_ref die, tree decl)
16658 /* Must be a field and a bit field. */
16659 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16660 && DECL_BIT_FIELD_TYPE (decl));
16662 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16663 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16666 /* If the compiled language is ANSI C, then add a 'prototyped'
16667 attribute, if arg types are given for the parameters of a function. */
16669 static inline void
16670 add_prototyped_attribute (dw_die_ref die, tree func_type)
16672 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16673 && prototype_p (func_type))
16674 add_AT_flag (die, DW_AT_prototyped, 1);
16677 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16678 by looking in either the type declaration or object declaration
16679 equate table. */
16681 static inline dw_die_ref
16682 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16684 dw_die_ref origin_die = NULL;
16686 if (TREE_CODE (origin) != FUNCTION_DECL)
16688 /* We may have gotten separated from the block for the inlined
16689 function, if we're in an exception handler or some such; make
16690 sure that the abstract function has been written out.
16692 Doing this for nested functions is wrong, however; functions are
16693 distinct units, and our context might not even be inline. */
16694 tree fn = origin;
16696 if (TYPE_P (fn))
16697 fn = TYPE_STUB_DECL (fn);
16699 fn = decl_function_context (fn);
16700 if (fn)
16701 dwarf2out_abstract_function (fn);
16704 if (DECL_P (origin))
16705 origin_die = lookup_decl_die (origin);
16706 else if (TYPE_P (origin))
16707 origin_die = lookup_type_die (origin);
16709 /* XXX: Functions that are never lowered don't always have correct block
16710 trees (in the case of java, they simply have no block tree, in some other
16711 languages). For these functions, there is nothing we can really do to
16712 output correct debug info for inlined functions in all cases. Rather
16713 than die, we'll just produce deficient debug info now, in that we will
16714 have variables without a proper abstract origin. In the future, when all
16715 functions are lowered, we should re-add a gcc_assert (origin_die)
16716 here. */
16718 if (origin_die)
16719 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16720 return origin_die;
16723 /* We do not currently support the pure_virtual attribute. */
16725 static inline void
16726 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16728 if (DECL_VINDEX (func_decl))
16730 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16732 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16733 add_AT_loc (die, DW_AT_vtable_elem_location,
16734 new_loc_descr (DW_OP_constu,
16735 tree_to_shwi (DECL_VINDEX (func_decl)),
16736 0));
16738 /* GNU extension: Record what type this method came from originally. */
16739 if (debug_info_level > DINFO_LEVEL_TERSE
16740 && DECL_CONTEXT (func_decl))
16741 add_AT_die_ref (die, DW_AT_containing_type,
16742 lookup_type_die (DECL_CONTEXT (func_decl)));
16746 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16747 given decl. This used to be a vendor extension until after DWARF 4
16748 standardized it. */
16750 static void
16751 add_linkage_attr (dw_die_ref die, tree decl)
16753 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16755 /* Mimic what assemble_name_raw does with a leading '*'. */
16756 if (name[0] == '*')
16757 name = &name[1];
16759 if (dwarf_version >= 4)
16760 add_AT_string (die, DW_AT_linkage_name, name);
16761 else
16762 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16765 /* Add source coordinate attributes for the given decl. */
16767 static void
16768 add_src_coords_attributes (dw_die_ref die, tree decl)
16770 expanded_location s;
16772 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16773 return;
16774 s = expand_location (DECL_SOURCE_LOCATION (decl));
16775 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16776 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16779 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16781 static void
16782 add_linkage_name (dw_die_ref die, tree decl)
16784 if (debug_info_level > DINFO_LEVEL_NONE
16785 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16786 && TREE_PUBLIC (decl)
16787 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16788 && die->die_tag != DW_TAG_member)
16790 /* Defer until we have an assembler name set. */
16791 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16793 limbo_die_node *asm_name;
16795 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16796 asm_name->die = die;
16797 asm_name->created_for = decl;
16798 asm_name->next = deferred_asm_name;
16799 deferred_asm_name = asm_name;
16801 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16802 add_linkage_attr (die, decl);
16806 /* Add a DW_AT_name attribute and source coordinate attribute for the
16807 given decl, but only if it actually has a name. */
16809 static void
16810 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16812 tree decl_name;
16814 decl_name = DECL_NAME (decl);
16815 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16817 const char *name = dwarf2_name (decl, 0);
16818 if (name)
16819 add_name_attribute (die, name);
16820 if (! DECL_ARTIFICIAL (decl))
16821 add_src_coords_attributes (die, decl);
16823 add_linkage_name (die, decl);
16826 #ifdef VMS_DEBUGGING_INFO
16827 /* Get the function's name, as described by its RTL. This may be different
16828 from the DECL_NAME name used in the source file. */
16829 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16831 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16832 XEXP (DECL_RTL (decl), 0), false);
16833 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16835 #endif /* VMS_DEBUGGING_INFO */
16838 #ifdef VMS_DEBUGGING_INFO
16839 /* Output the debug main pointer die for VMS */
16841 void
16842 dwarf2out_vms_debug_main_pointer (void)
16844 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16845 dw_die_ref die;
16847 /* Allocate the VMS debug main subprogram die. */
16848 die = ggc_cleared_alloc<die_node> ();
16849 die->die_tag = DW_TAG_subprogram;
16850 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16851 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16852 current_function_funcdef_no);
16853 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16855 /* Make it the first child of comp_unit_die (). */
16856 die->die_parent = comp_unit_die ();
16857 if (comp_unit_die ()->die_child)
16859 die->die_sib = comp_unit_die ()->die_child->die_sib;
16860 comp_unit_die ()->die_child->die_sib = die;
16862 else
16864 die->die_sib = die;
16865 comp_unit_die ()->die_child = die;
16868 #endif /* VMS_DEBUGGING_INFO */
16870 /* Push a new declaration scope. */
16872 static void
16873 push_decl_scope (tree scope)
16875 vec_safe_push (decl_scope_table, scope);
16878 /* Pop a declaration scope. */
16880 static inline void
16881 pop_decl_scope (void)
16883 decl_scope_table->pop ();
16886 /* walk_tree helper function for uses_local_type, below. */
16888 static tree
16889 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16891 if (!TYPE_P (*tp))
16892 *walk_subtrees = 0;
16893 else
16895 tree name = TYPE_NAME (*tp);
16896 if (name && DECL_P (name) && decl_function_context (name))
16897 return *tp;
16899 return NULL_TREE;
16902 /* If TYPE involves a function-local type (including a local typedef to a
16903 non-local type), returns that type; otherwise returns NULL_TREE. */
16905 static tree
16906 uses_local_type (tree type)
16908 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16909 return used;
16912 /* Return the DIE for the scope that immediately contains this type.
16913 Non-named types that do not involve a function-local type get global
16914 scope. Named types nested in namespaces or other types get their
16915 containing scope. All other types (i.e. function-local named types) get
16916 the current active scope. */
16918 static dw_die_ref
16919 scope_die_for (tree t, dw_die_ref context_die)
16921 dw_die_ref scope_die = NULL;
16922 tree containing_scope;
16924 /* Non-types always go in the current scope. */
16925 gcc_assert (TYPE_P (t));
16927 /* Use the scope of the typedef, rather than the scope of the type
16928 it refers to. */
16929 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16930 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16931 else
16932 containing_scope = TYPE_CONTEXT (t);
16934 /* Use the containing namespace if there is one. */
16935 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16937 if (context_die == lookup_decl_die (containing_scope))
16938 /* OK */;
16939 else if (debug_info_level > DINFO_LEVEL_TERSE)
16940 context_die = get_context_die (containing_scope);
16941 else
16942 containing_scope = NULL_TREE;
16945 /* Ignore function type "scopes" from the C frontend. They mean that
16946 a tagged type is local to a parmlist of a function declarator, but
16947 that isn't useful to DWARF. */
16948 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16949 containing_scope = NULL_TREE;
16951 if (SCOPE_FILE_SCOPE_P (containing_scope))
16953 /* If T uses a local type keep it local as well, to avoid references
16954 to function-local DIEs from outside the function. */
16955 if (current_function_decl && uses_local_type (t))
16956 scope_die = context_die;
16957 else
16958 scope_die = comp_unit_die ();
16960 else if (TYPE_P (containing_scope))
16962 /* For types, we can just look up the appropriate DIE. */
16963 if (debug_info_level > DINFO_LEVEL_TERSE)
16964 scope_die = get_context_die (containing_scope);
16965 else
16967 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16968 if (scope_die == NULL)
16969 scope_die = comp_unit_die ();
16972 else
16973 scope_die = context_die;
16975 return scope_die;
16978 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16980 static inline int
16981 local_scope_p (dw_die_ref context_die)
16983 for (; context_die; context_die = context_die->die_parent)
16984 if (context_die->die_tag == DW_TAG_inlined_subroutine
16985 || context_die->die_tag == DW_TAG_subprogram)
16986 return 1;
16988 return 0;
16991 /* Returns nonzero if CONTEXT_DIE is a class. */
16993 static inline int
16994 class_scope_p (dw_die_ref context_die)
16996 return (context_die
16997 && (context_die->die_tag == DW_TAG_structure_type
16998 || context_die->die_tag == DW_TAG_class_type
16999 || context_die->die_tag == DW_TAG_interface_type
17000 || context_die->die_tag == DW_TAG_union_type));
17003 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17004 whether or not to treat a DIE in this context as a declaration. */
17006 static inline int
17007 class_or_namespace_scope_p (dw_die_ref context_die)
17009 return (class_scope_p (context_die)
17010 || (context_die && context_die->die_tag == DW_TAG_namespace));
17013 /* Many forms of DIEs require a "type description" attribute. This
17014 routine locates the proper "type descriptor" die for the type given
17015 by 'type', and adds a DW_AT_type attribute below the given die. */
17017 static void
17018 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17019 int decl_volatile, dw_die_ref context_die)
17021 enum tree_code code = TREE_CODE (type);
17022 dw_die_ref type_die = NULL;
17023 int type_quals;
17025 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17026 or fixed-point type, use the inner type. This is because we have no
17027 support for unnamed types in base_type_die. This can happen if this is
17028 an Ada subrange type. Correct solution is emit a subrange type die. */
17029 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17030 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17031 type = TREE_TYPE (type), code = TREE_CODE (type);
17033 if (code == ERROR_MARK
17034 /* Handle a special case. For functions whose return type is void, we
17035 generate *no* type attribute. (Note that no object may have type
17036 `void', so this only applies to function return types). */
17037 || code == VOID_TYPE)
17038 return;
17040 type_quals = TYPE_QUALS (type)
17041 | (decl_const * TYPE_QUAL_CONST)
17042 | (decl_volatile * TYPE_QUAL_VOLATILE);
17043 type_die = modified_type_die (type, type_quals, context_die);
17045 if (type_die != NULL)
17046 add_AT_die_ref (object_die, DW_AT_type, type_die);
17049 /* Given an object die, add the calling convention attribute for the
17050 function call type. */
17051 static void
17052 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17054 enum dwarf_calling_convention value = DW_CC_normal;
17056 value = ((enum dwarf_calling_convention)
17057 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17059 if (is_fortran ()
17060 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17062 /* DWARF 2 doesn't provide a way to identify a program's source-level
17063 entry point. DW_AT_calling_convention attributes are only meant
17064 to describe functions' calling conventions. However, lacking a
17065 better way to signal the Fortran main program, we used this for
17066 a long time, following existing custom. Now, DWARF 4 has
17067 DW_AT_main_subprogram, which we add below, but some tools still
17068 rely on the old way, which we thus keep. */
17069 value = DW_CC_program;
17071 if (dwarf_version >= 4 || !dwarf_strict)
17072 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17075 /* Only add the attribute if the backend requests it, and
17076 is not DW_CC_normal. */
17077 if (value && (value != DW_CC_normal))
17078 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17081 /* Given a tree pointer to a struct, class, union, or enum type node, return
17082 a pointer to the (string) tag name for the given type, or zero if the type
17083 was declared without a tag. */
17085 static const char *
17086 type_tag (const_tree type)
17088 const char *name = 0;
17090 if (TYPE_NAME (type) != 0)
17092 tree t = 0;
17094 /* Find the IDENTIFIER_NODE for the type name. */
17095 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17096 && !TYPE_NAMELESS (type))
17097 t = TYPE_NAME (type);
17099 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17100 a TYPE_DECL node, regardless of whether or not a `typedef' was
17101 involved. */
17102 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17103 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17105 /* We want to be extra verbose. Don't call dwarf_name if
17106 DECL_NAME isn't set. The default hook for decl_printable_name
17107 doesn't like that, and in this context it's correct to return
17108 0, instead of "<anonymous>" or the like. */
17109 if (DECL_NAME (TYPE_NAME (type))
17110 && !DECL_NAMELESS (TYPE_NAME (type)))
17111 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17114 /* Now get the name as a string, or invent one. */
17115 if (!name && t != 0)
17116 name = IDENTIFIER_POINTER (t);
17119 return (name == 0 || *name == '\0') ? 0 : name;
17122 /* Return the type associated with a data member, make a special check
17123 for bit field types. */
17125 static inline tree
17126 member_declared_type (const_tree member)
17128 return (DECL_BIT_FIELD_TYPE (member)
17129 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17132 /* Get the decl's label, as described by its RTL. This may be different
17133 from the DECL_NAME name used in the source file. */
17135 #if 0
17136 static const char *
17137 decl_start_label (tree decl)
17139 rtx x;
17140 const char *fnname;
17142 x = DECL_RTL (decl);
17143 gcc_assert (MEM_P (x));
17145 x = XEXP (x, 0);
17146 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17148 fnname = XSTR (x, 0);
17149 return fnname;
17151 #endif
17153 /* These routines generate the internal representation of the DIE's for
17154 the compilation unit. Debugging information is collected by walking
17155 the declaration trees passed in from dwarf2out_decl(). */
17157 static void
17158 gen_array_type_die (tree type, dw_die_ref context_die)
17160 dw_die_ref scope_die = scope_die_for (type, context_die);
17161 dw_die_ref array_die;
17163 /* GNU compilers represent multidimensional array types as sequences of one
17164 dimensional array types whose element types are themselves array types.
17165 We sometimes squish that down to a single array_type DIE with multiple
17166 subscripts in the Dwarf debugging info. The draft Dwarf specification
17167 say that we are allowed to do this kind of compression in C, because
17168 there is no difference between an array of arrays and a multidimensional
17169 array. We don't do this for Ada to remain as close as possible to the
17170 actual representation, which is especially important against the language
17171 flexibilty wrt arrays of variable size. */
17173 bool collapse_nested_arrays = !is_ada ();
17174 tree element_type;
17176 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17177 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17178 if (TYPE_STRING_FLAG (type)
17179 && TREE_CODE (type) == ARRAY_TYPE
17180 && is_fortran ()
17181 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17183 HOST_WIDE_INT size;
17185 array_die = new_die (DW_TAG_string_type, scope_die, type);
17186 add_name_attribute (array_die, type_tag (type));
17187 equate_type_number_to_die (type, array_die);
17188 size = int_size_in_bytes (type);
17189 if (size >= 0)
17190 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17191 else if (TYPE_DOMAIN (type) != NULL_TREE
17192 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17193 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17195 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17196 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17198 size = int_size_in_bytes (TREE_TYPE (szdecl));
17199 if (loc && size > 0)
17201 add_AT_location_description (array_die, DW_AT_string_length, loc);
17202 if (size != DWARF2_ADDR_SIZE)
17203 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17206 return;
17209 array_die = new_die (DW_TAG_array_type, scope_die, type);
17210 add_name_attribute (array_die, type_tag (type));
17211 equate_type_number_to_die (type, array_die);
17213 if (TREE_CODE (type) == VECTOR_TYPE)
17214 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17216 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17217 if (is_fortran ()
17218 && TREE_CODE (type) == ARRAY_TYPE
17219 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17220 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17221 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17223 #if 0
17224 /* We default the array ordering. SDB will probably do
17225 the right things even if DW_AT_ordering is not present. It's not even
17226 an issue until we start to get into multidimensional arrays anyway. If
17227 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17228 then we'll have to put the DW_AT_ordering attribute back in. (But if
17229 and when we find out that we need to put these in, we will only do so
17230 for multidimensional arrays. */
17231 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17232 #endif
17234 if (TREE_CODE (type) == VECTOR_TYPE)
17236 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17237 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17238 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17239 add_bound_info (subrange_die, DW_AT_upper_bound,
17240 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17242 else
17243 add_subscript_info (array_die, type, collapse_nested_arrays);
17245 /* Add representation of the type of the elements of this array type and
17246 emit the corresponding DIE if we haven't done it already. */
17247 element_type = TREE_TYPE (type);
17248 if (collapse_nested_arrays)
17249 while (TREE_CODE (element_type) == ARRAY_TYPE)
17251 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17252 break;
17253 element_type = TREE_TYPE (element_type);
17256 add_type_attribute (array_die, element_type, 0, 0, context_die);
17258 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17259 if (TYPE_ARTIFICIAL (type))
17260 add_AT_flag (array_die, DW_AT_artificial, 1);
17262 if (get_AT (array_die, DW_AT_name))
17263 add_pubtype (type, array_die);
17266 static dw_loc_descr_ref
17267 descr_info_loc (tree val, tree base_decl)
17269 HOST_WIDE_INT size;
17270 dw_loc_descr_ref loc, loc2;
17271 enum dwarf_location_atom op;
17273 if (val == base_decl)
17274 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17276 switch (TREE_CODE (val))
17278 CASE_CONVERT:
17279 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17280 case VAR_DECL:
17281 return loc_descriptor_from_tree (val, 0);
17282 case INTEGER_CST:
17283 if (tree_fits_shwi_p (val))
17284 return int_loc_descriptor (tree_to_shwi (val));
17285 break;
17286 case INDIRECT_REF:
17287 size = int_size_in_bytes (TREE_TYPE (val));
17288 if (size < 0)
17289 break;
17290 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17291 if (!loc)
17292 break;
17293 if (size == DWARF2_ADDR_SIZE)
17294 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17295 else
17296 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17297 return loc;
17298 case POINTER_PLUS_EXPR:
17299 case PLUS_EXPR:
17300 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17301 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17303 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17304 if (!loc)
17305 break;
17306 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17308 else
17310 op = DW_OP_plus;
17311 do_binop:
17312 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17313 if (!loc)
17314 break;
17315 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17316 if (!loc2)
17317 break;
17318 add_loc_descr (&loc, loc2);
17319 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17321 return loc;
17322 case MINUS_EXPR:
17323 op = DW_OP_minus;
17324 goto do_binop;
17325 case MULT_EXPR:
17326 op = DW_OP_mul;
17327 goto do_binop;
17328 case EQ_EXPR:
17329 op = DW_OP_eq;
17330 goto do_binop;
17331 case NE_EXPR:
17332 op = DW_OP_ne;
17333 goto do_binop;
17334 default:
17335 break;
17337 return NULL;
17340 static void
17341 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17342 tree val, tree base_decl)
17344 dw_loc_descr_ref loc;
17346 if (tree_fits_shwi_p (val))
17348 add_AT_unsigned (die, attr, tree_to_shwi (val));
17349 return;
17352 loc = descr_info_loc (val, base_decl);
17353 if (!loc)
17354 return;
17356 add_AT_loc (die, attr, loc);
17359 /* This routine generates DIE for array with hidden descriptor, details
17360 are filled into *info by a langhook. */
17362 static void
17363 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17364 dw_die_ref context_die)
17366 dw_die_ref scope_die = scope_die_for (type, context_die);
17367 dw_die_ref array_die;
17368 int dim;
17370 array_die = new_die (DW_TAG_array_type, scope_die, type);
17371 add_name_attribute (array_die, type_tag (type));
17372 equate_type_number_to_die (type, array_die);
17374 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17375 if (is_fortran ()
17376 && info->ndimensions >= 2)
17377 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17379 if (info->data_location)
17380 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17381 info->base_decl);
17382 if (info->associated)
17383 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17384 info->base_decl);
17385 if (info->allocated)
17386 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17387 info->base_decl);
17389 for (dim = 0; dim < info->ndimensions; dim++)
17391 dw_die_ref subrange_die
17392 = new_die (DW_TAG_subrange_type, array_die, NULL);
17394 if (info->dimen[dim].lower_bound)
17396 /* If it is the default value, omit it. */
17397 int dflt;
17399 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17400 && (dflt = lower_bound_default ()) != -1
17401 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17403 else
17404 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17405 info->dimen[dim].lower_bound,
17406 info->base_decl);
17408 if (info->dimen[dim].upper_bound)
17409 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17410 info->dimen[dim].upper_bound,
17411 info->base_decl);
17412 if (info->dimen[dim].stride)
17413 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17414 info->dimen[dim].stride,
17415 info->base_decl);
17418 gen_type_die (info->element_type, context_die);
17419 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17421 if (get_AT (array_die, DW_AT_name))
17422 add_pubtype (type, array_die);
17425 #if 0
17426 static void
17427 gen_entry_point_die (tree decl, dw_die_ref context_die)
17429 tree origin = decl_ultimate_origin (decl);
17430 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17432 if (origin != NULL)
17433 add_abstract_origin_attribute (decl_die, origin);
17434 else
17436 add_name_and_src_coords_attributes (decl_die, decl);
17437 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17438 0, 0, context_die);
17441 if (DECL_ABSTRACT (decl))
17442 equate_decl_number_to_die (decl, decl_die);
17443 else
17444 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17446 #endif
17448 /* Walk through the list of incomplete types again, trying once more to
17449 emit full debugging info for them. */
17451 static void
17452 retry_incomplete_types (void)
17454 int i;
17456 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17457 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17458 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17461 /* Determine what tag to use for a record type. */
17463 static enum dwarf_tag
17464 record_type_tag (tree type)
17466 if (! lang_hooks.types.classify_record)
17467 return DW_TAG_structure_type;
17469 switch (lang_hooks.types.classify_record (type))
17471 case RECORD_IS_STRUCT:
17472 return DW_TAG_structure_type;
17474 case RECORD_IS_CLASS:
17475 return DW_TAG_class_type;
17477 case RECORD_IS_INTERFACE:
17478 if (dwarf_version >= 3 || !dwarf_strict)
17479 return DW_TAG_interface_type;
17480 return DW_TAG_structure_type;
17482 default:
17483 gcc_unreachable ();
17487 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17488 include all of the information about the enumeration values also. Each
17489 enumerated type name/value is listed as a child of the enumerated type
17490 DIE. */
17492 static dw_die_ref
17493 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17495 dw_die_ref type_die = lookup_type_die (type);
17497 if (type_die == NULL)
17499 type_die = new_die (DW_TAG_enumeration_type,
17500 scope_die_for (type, context_die), type);
17501 equate_type_number_to_die (type, type_die);
17502 add_name_attribute (type_die, type_tag (type));
17503 if (dwarf_version >= 4 || !dwarf_strict)
17505 if (ENUM_IS_SCOPED (type))
17506 add_AT_flag (type_die, DW_AT_enum_class, 1);
17507 if (ENUM_IS_OPAQUE (type))
17508 add_AT_flag (type_die, DW_AT_declaration, 1);
17511 else if (! TYPE_SIZE (type))
17512 return type_die;
17513 else
17514 remove_AT (type_die, DW_AT_declaration);
17516 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17517 given enum type is incomplete, do not generate the DW_AT_byte_size
17518 attribute or the DW_AT_element_list attribute. */
17519 if (TYPE_SIZE (type))
17521 tree link;
17523 TREE_ASM_WRITTEN (type) = 1;
17524 add_byte_size_attribute (type_die, type);
17525 if (dwarf_version >= 3 || !dwarf_strict)
17527 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17528 add_type_attribute (type_die, underlying, 0, 0, context_die);
17530 if (TYPE_STUB_DECL (type) != NULL_TREE)
17532 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17533 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17536 /* If the first reference to this type was as the return type of an
17537 inline function, then it may not have a parent. Fix this now. */
17538 if (type_die->die_parent == NULL)
17539 add_child_die (scope_die_for (type, context_die), type_die);
17541 for (link = TYPE_VALUES (type);
17542 link != NULL; link = TREE_CHAIN (link))
17544 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17545 tree value = TREE_VALUE (link);
17547 add_name_attribute (enum_die,
17548 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17550 if (TREE_CODE (value) == CONST_DECL)
17551 value = DECL_INITIAL (value);
17553 if (simple_type_size_in_bits (TREE_TYPE (value))
17554 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17556 /* For constant forms created by add_AT_unsigned DWARF
17557 consumers (GDB, elfutils, etc.) always zero extend
17558 the value. Only when the actual value is negative
17559 do we need to use add_AT_int to generate a constant
17560 form that can represent negative values. */
17561 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17562 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17563 add_AT_unsigned (enum_die, DW_AT_const_value,
17564 (unsigned HOST_WIDE_INT) val);
17565 else
17566 add_AT_int (enum_die, DW_AT_const_value, val);
17568 else
17569 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17570 that here. TODO: This should be re-worked to use correct
17571 signed/unsigned double tags for all cases. */
17572 add_AT_wide (enum_die, DW_AT_const_value, value);
17575 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17576 if (TYPE_ARTIFICIAL (type))
17577 add_AT_flag (type_die, DW_AT_artificial, 1);
17579 else
17580 add_AT_flag (type_die, DW_AT_declaration, 1);
17582 add_pubtype (type, type_die);
17584 return type_die;
17587 /* Generate a DIE to represent either a real live formal parameter decl or to
17588 represent just the type of some formal parameter position in some function
17589 type.
17591 Note that this routine is a bit unusual because its argument may be a
17592 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17593 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17594 node. If it's the former then this function is being called to output a
17595 DIE to represent a formal parameter object (or some inlining thereof). If
17596 it's the latter, then this function is only being called to output a
17597 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17598 argument type of some subprogram type.
17599 If EMIT_NAME_P is true, name and source coordinate attributes
17600 are emitted. */
17602 static dw_die_ref
17603 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17604 dw_die_ref context_die)
17606 tree node_or_origin = node ? node : origin;
17607 tree ultimate_origin;
17608 dw_die_ref parm_die
17609 = new_die (DW_TAG_formal_parameter, context_die, node);
17611 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17613 case tcc_declaration:
17614 ultimate_origin = decl_ultimate_origin (node_or_origin);
17615 if (node || ultimate_origin)
17616 origin = ultimate_origin;
17617 if (origin != NULL)
17618 add_abstract_origin_attribute (parm_die, origin);
17619 else if (emit_name_p)
17620 add_name_and_src_coords_attributes (parm_die, node);
17621 if (origin == NULL
17622 || (! DECL_ABSTRACT (node_or_origin)
17623 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17624 decl_function_context
17625 (node_or_origin))))
17627 tree type = TREE_TYPE (node_or_origin);
17628 if (decl_by_reference_p (node_or_origin))
17629 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17630 context_die);
17631 else
17632 add_type_attribute (parm_die, type,
17633 TREE_READONLY (node_or_origin),
17634 TREE_THIS_VOLATILE (node_or_origin),
17635 context_die);
17637 if (origin == NULL && DECL_ARTIFICIAL (node))
17638 add_AT_flag (parm_die, DW_AT_artificial, 1);
17640 if (node && node != origin)
17641 equate_decl_number_to_die (node, parm_die);
17642 if (! DECL_ABSTRACT (node_or_origin))
17643 add_location_or_const_value_attribute (parm_die, node_or_origin,
17644 node == NULL, DW_AT_location);
17646 break;
17648 case tcc_type:
17649 /* We were called with some kind of a ..._TYPE node. */
17650 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17651 break;
17653 default:
17654 gcc_unreachable ();
17657 return parm_die;
17660 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17661 children DW_TAG_formal_parameter DIEs representing the arguments of the
17662 parameter pack.
17664 PARM_PACK must be a function parameter pack.
17665 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17666 must point to the subsequent arguments of the function PACK_ARG belongs to.
17667 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17668 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17669 following the last one for which a DIE was generated. */
17671 static dw_die_ref
17672 gen_formal_parameter_pack_die (tree parm_pack,
17673 tree pack_arg,
17674 dw_die_ref subr_die,
17675 tree *next_arg)
17677 tree arg;
17678 dw_die_ref parm_pack_die;
17680 gcc_assert (parm_pack
17681 && lang_hooks.function_parameter_pack_p (parm_pack)
17682 && subr_die);
17684 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17685 add_src_coords_attributes (parm_pack_die, parm_pack);
17687 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17689 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17690 parm_pack))
17691 break;
17692 gen_formal_parameter_die (arg, NULL,
17693 false /* Don't emit name attribute. */,
17694 parm_pack_die);
17696 if (next_arg)
17697 *next_arg = arg;
17698 return parm_pack_die;
17701 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17702 at the end of an (ANSI prototyped) formal parameters list. */
17704 static void
17705 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17707 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17710 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17711 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17712 parameters as specified in some function type specification (except for
17713 those which appear as part of a function *definition*). */
17715 static void
17716 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17718 tree link;
17719 tree formal_type = NULL;
17720 tree first_parm_type;
17721 tree arg;
17723 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17725 arg = DECL_ARGUMENTS (function_or_method_type);
17726 function_or_method_type = TREE_TYPE (function_or_method_type);
17728 else
17729 arg = NULL_TREE;
17731 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17733 /* Make our first pass over the list of formal parameter types and output a
17734 DW_TAG_formal_parameter DIE for each one. */
17735 for (link = first_parm_type; link; )
17737 dw_die_ref parm_die;
17739 formal_type = TREE_VALUE (link);
17740 if (formal_type == void_type_node)
17741 break;
17743 /* Output a (nameless) DIE to represent the formal parameter itself. */
17744 parm_die = gen_formal_parameter_die (formal_type, NULL,
17745 true /* Emit name attribute. */,
17746 context_die);
17747 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17748 && link == first_parm_type)
17750 add_AT_flag (parm_die, DW_AT_artificial, 1);
17751 if (dwarf_version >= 3 || !dwarf_strict)
17752 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17754 else if (arg && DECL_ARTIFICIAL (arg))
17755 add_AT_flag (parm_die, DW_AT_artificial, 1);
17757 link = TREE_CHAIN (link);
17758 if (arg)
17759 arg = DECL_CHAIN (arg);
17762 /* If this function type has an ellipsis, add a
17763 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17764 if (formal_type != void_type_node)
17765 gen_unspecified_parameters_die (function_or_method_type, context_die);
17767 /* Make our second (and final) pass over the list of formal parameter types
17768 and output DIEs to represent those types (as necessary). */
17769 for (link = TYPE_ARG_TYPES (function_or_method_type);
17770 link && TREE_VALUE (link);
17771 link = TREE_CHAIN (link))
17772 gen_type_die (TREE_VALUE (link), context_die);
17775 /* We want to generate the DIE for TYPE so that we can generate the
17776 die for MEMBER, which has been defined; we will need to refer back
17777 to the member declaration nested within TYPE. If we're trying to
17778 generate minimal debug info for TYPE, processing TYPE won't do the
17779 trick; we need to attach the member declaration by hand. */
17781 static void
17782 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17784 gen_type_die (type, context_die);
17786 /* If we're trying to avoid duplicate debug info, we may not have
17787 emitted the member decl for this function. Emit it now. */
17788 if (TYPE_STUB_DECL (type)
17789 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17790 && ! lookup_decl_die (member))
17792 dw_die_ref type_die;
17793 gcc_assert (!decl_ultimate_origin (member));
17795 push_decl_scope (type);
17796 type_die = lookup_type_die_strip_naming_typedef (type);
17797 if (TREE_CODE (member) == FUNCTION_DECL)
17798 gen_subprogram_die (member, type_die);
17799 else if (TREE_CODE (member) == FIELD_DECL)
17801 /* Ignore the nameless fields that are used to skip bits but handle
17802 C++ anonymous unions and structs. */
17803 if (DECL_NAME (member) != NULL_TREE
17804 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17805 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17807 gen_type_die (member_declared_type (member), type_die);
17808 gen_field_die (member, type_die);
17811 else
17812 gen_variable_die (member, NULL_TREE, type_die);
17814 pop_decl_scope ();
17818 /* Forward declare these functions, because they are mutually recursive
17819 with their set_block_* pairing functions. */
17820 static void set_decl_origin_self (tree);
17821 static void set_decl_abstract_flags (tree, int);
17823 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17824 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17825 that it points to the node itself, thus indicating that the node is its
17826 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17827 the given node is NULL, recursively descend the decl/block tree which
17828 it is the root of, and for each other ..._DECL or BLOCK node contained
17829 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17830 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17831 values to point to themselves. */
17833 static void
17834 set_block_origin_self (tree stmt)
17836 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17838 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17841 tree local_decl;
17843 for (local_decl = BLOCK_VARS (stmt);
17844 local_decl != NULL_TREE;
17845 local_decl = DECL_CHAIN (local_decl))
17846 if (! DECL_EXTERNAL (local_decl))
17847 set_decl_origin_self (local_decl); /* Potential recursion. */
17851 tree subblock;
17853 for (subblock = BLOCK_SUBBLOCKS (stmt);
17854 subblock != NULL_TREE;
17855 subblock = BLOCK_CHAIN (subblock))
17856 set_block_origin_self (subblock); /* Recurse. */
17861 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17862 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17863 node to so that it points to the node itself, thus indicating that the
17864 node represents its own (abstract) origin. Additionally, if the
17865 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17866 the decl/block tree of which the given node is the root of, and for
17867 each other ..._DECL or BLOCK node contained therein whose
17868 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17869 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17870 point to themselves. */
17872 static void
17873 set_decl_origin_self (tree decl)
17875 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17877 DECL_ABSTRACT_ORIGIN (decl) = decl;
17878 if (TREE_CODE (decl) == FUNCTION_DECL)
17880 tree arg;
17882 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17883 DECL_ABSTRACT_ORIGIN (arg) = arg;
17884 if (DECL_INITIAL (decl) != NULL_TREE
17885 && DECL_INITIAL (decl) != error_mark_node)
17886 set_block_origin_self (DECL_INITIAL (decl));
17891 /* Given a pointer to some BLOCK node, and a boolean value to set the
17892 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17893 the given block, and for all local decls and all local sub-blocks
17894 (recursively) which are contained therein. */
17896 static void
17897 set_block_abstract_flags (tree stmt, int setting)
17899 tree local_decl;
17900 tree subblock;
17901 unsigned int i;
17903 BLOCK_ABSTRACT (stmt) = setting;
17905 for (local_decl = BLOCK_VARS (stmt);
17906 local_decl != NULL_TREE;
17907 local_decl = DECL_CHAIN (local_decl))
17908 if (! DECL_EXTERNAL (local_decl))
17909 set_decl_abstract_flags (local_decl, setting);
17911 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17913 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17914 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17915 || TREE_CODE (local_decl) == PARM_DECL)
17916 set_decl_abstract_flags (local_decl, setting);
17919 for (subblock = BLOCK_SUBBLOCKS (stmt);
17920 subblock != NULL_TREE;
17921 subblock = BLOCK_CHAIN (subblock))
17922 set_block_abstract_flags (subblock, setting);
17925 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17926 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17927 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17928 set the abstract flags for all of the parameters, local vars, local
17929 blocks and sub-blocks (recursively) to the same setting. */
17931 static void
17932 set_decl_abstract_flags (tree decl, int setting)
17934 DECL_ABSTRACT (decl) = setting;
17935 if (TREE_CODE (decl) == FUNCTION_DECL)
17937 tree arg;
17939 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17940 DECL_ABSTRACT (arg) = setting;
17941 if (DECL_INITIAL (decl) != NULL_TREE
17942 && DECL_INITIAL (decl) != error_mark_node)
17943 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17947 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17948 may later generate inlined and/or out-of-line instances of. */
17950 static void
17951 dwarf2out_abstract_function (tree decl)
17953 dw_die_ref old_die;
17954 tree save_fn;
17955 tree context;
17956 int was_abstract;
17957 htab_t old_decl_loc_table;
17958 htab_t old_cached_dw_loc_list_table;
17959 int old_call_site_count, old_tail_call_site_count;
17960 struct call_arg_loc_node *old_call_arg_locations;
17962 /* Make sure we have the actual abstract inline, not a clone. */
17963 decl = DECL_ORIGIN (decl);
17965 old_die = lookup_decl_die (decl);
17966 if (old_die && get_AT (old_die, DW_AT_inline))
17967 /* We've already generated the abstract instance. */
17968 return;
17970 /* We can be called while recursively when seeing block defining inlined subroutine
17971 DIE. Be sure to not clobber the outer location table nor use it or we would
17972 get locations in abstract instantces. */
17973 old_decl_loc_table = decl_loc_table;
17974 decl_loc_table = NULL;
17975 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17976 cached_dw_loc_list_table = NULL;
17977 old_call_arg_locations = call_arg_locations;
17978 call_arg_locations = NULL;
17979 old_call_site_count = call_site_count;
17980 call_site_count = -1;
17981 old_tail_call_site_count = tail_call_site_count;
17982 tail_call_site_count = -1;
17984 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17985 we don't get confused by DECL_ABSTRACT. */
17986 if (debug_info_level > DINFO_LEVEL_TERSE)
17988 context = decl_class_context (decl);
17989 if (context)
17990 gen_type_die_for_member
17991 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17994 /* Pretend we've just finished compiling this function. */
17995 save_fn = current_function_decl;
17996 current_function_decl = decl;
17998 was_abstract = DECL_ABSTRACT (decl);
17999 set_decl_abstract_flags (decl, 1);
18000 dwarf2out_decl (decl);
18001 if (! was_abstract)
18002 set_decl_abstract_flags (decl, 0);
18004 current_function_decl = save_fn;
18005 decl_loc_table = old_decl_loc_table;
18006 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18007 call_arg_locations = old_call_arg_locations;
18008 call_site_count = old_call_site_count;
18009 tail_call_site_count = old_tail_call_site_count;
18012 /* Helper function of premark_used_types() which gets called through
18013 htab_traverse.
18015 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18016 marked as unused by prune_unused_types. */
18018 static int
18019 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18021 tree type;
18022 dw_die_ref die;
18024 type = (tree) *slot;
18025 die = lookup_type_die (type);
18026 if (die != NULL)
18027 die->die_perennial_p = 1;
18028 return 1;
18031 /* Helper function of premark_types_used_by_global_vars which gets called
18032 through htab_traverse.
18034 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18035 marked as unused by prune_unused_types. The DIE of the type is marked
18036 only if the global variable using the type will actually be emitted. */
18038 static int
18039 premark_types_used_by_global_vars_helper (void **slot,
18040 void *data ATTRIBUTE_UNUSED)
18042 struct types_used_by_vars_entry *entry;
18043 dw_die_ref die;
18045 entry = (struct types_used_by_vars_entry *) *slot;
18046 gcc_assert (entry->type != NULL
18047 && entry->var_decl != NULL);
18048 die = lookup_type_die (entry->type);
18049 if (die)
18051 /* Ask cgraph if the global variable really is to be emitted.
18052 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18053 varpool_node *node = varpool_node::get (entry->var_decl);
18054 if (node && node->definition)
18056 die->die_perennial_p = 1;
18057 /* Keep the parent DIEs as well. */
18058 while ((die = die->die_parent) && die->die_perennial_p == 0)
18059 die->die_perennial_p = 1;
18062 return 1;
18065 /* Mark all members of used_types_hash as perennial. */
18067 static void
18068 premark_used_types (struct function *fun)
18070 if (fun && fun->used_types_hash)
18071 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
18074 /* Mark all members of types_used_by_vars_entry as perennial. */
18076 static void
18077 premark_types_used_by_global_vars (void)
18079 if (types_used_by_vars_hash)
18080 htab_traverse (types_used_by_vars_hash,
18081 premark_types_used_by_global_vars_helper, NULL);
18084 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18085 for CA_LOC call arg loc node. */
18087 static dw_die_ref
18088 gen_call_site_die (tree decl, dw_die_ref subr_die,
18089 struct call_arg_loc_node *ca_loc)
18091 dw_die_ref stmt_die = NULL, die;
18092 tree block = ca_loc->block;
18094 while (block
18095 && block != DECL_INITIAL (decl)
18096 && TREE_CODE (block) == BLOCK)
18098 if (block_map.length () > BLOCK_NUMBER (block))
18099 stmt_die = block_map[BLOCK_NUMBER (block)];
18100 if (stmt_die)
18101 break;
18102 block = BLOCK_SUPERCONTEXT (block);
18104 if (stmt_die == NULL)
18105 stmt_die = subr_die;
18106 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18107 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18108 if (ca_loc->tail_call_p)
18109 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18110 if (ca_loc->symbol_ref)
18112 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18113 if (tdie)
18114 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18115 else
18116 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18118 return die;
18121 /* Generate a DIE to represent a declared function (either file-scope or
18122 block-local). */
18124 static void
18125 gen_subprogram_die (tree decl, dw_die_ref context_die)
18127 tree origin = decl_ultimate_origin (decl);
18128 dw_die_ref subr_die;
18129 tree outer_scope;
18130 dw_die_ref old_die = lookup_decl_die (decl);
18131 int declaration = (current_function_decl != decl
18132 || class_or_namespace_scope_p (context_die));
18134 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18136 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18137 started to generate the abstract instance of an inline, decided to output
18138 its containing class, and proceeded to emit the declaration of the inline
18139 from the member list for the class. If so, DECLARATION takes priority;
18140 we'll get back to the abstract instance when done with the class. */
18142 /* The class-scope declaration DIE must be the primary DIE. */
18143 if (origin && declaration && class_or_namespace_scope_p (context_die))
18145 origin = NULL;
18146 gcc_assert (!old_die);
18149 /* Now that the C++ front end lazily declares artificial member fns, we
18150 might need to retrofit the declaration into its class. */
18151 if (!declaration && !origin && !old_die
18152 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18153 && !class_or_namespace_scope_p (context_die)
18154 && debug_info_level > DINFO_LEVEL_TERSE)
18155 old_die = force_decl_die (decl);
18157 if (origin != NULL)
18159 gcc_assert (!declaration || local_scope_p (context_die));
18161 /* Fixup die_parent for the abstract instance of a nested
18162 inline function. */
18163 if (old_die && old_die->die_parent == NULL)
18164 add_child_die (context_die, old_die);
18166 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18167 add_abstract_origin_attribute (subr_die, origin);
18168 /* This is where the actual code for a cloned function is.
18169 Let's emit linkage name attribute for it. This helps
18170 debuggers to e.g, set breakpoints into
18171 constructors/destructors when the user asks "break
18172 K::K". */
18173 add_linkage_name (subr_die, decl);
18175 else if (old_die)
18177 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18178 struct dwarf_file_data * file_index = lookup_filename (s.file);
18180 if (!get_AT_flag (old_die, DW_AT_declaration)
18181 /* We can have a normal definition following an inline one in the
18182 case of redefinition of GNU C extern inlines.
18183 It seems reasonable to use AT_specification in this case. */
18184 && !get_AT (old_die, DW_AT_inline))
18186 /* Detect and ignore this case, where we are trying to output
18187 something we have already output. */
18188 return;
18191 /* If the definition comes from the same place as the declaration,
18192 maybe use the old DIE. We always want the DIE for this function
18193 that has the *_pc attributes to be under comp_unit_die so the
18194 debugger can find it. We also need to do this for abstract
18195 instances of inlines, since the spec requires the out-of-line copy
18196 to have the same parent. For local class methods, this doesn't
18197 apply; we just use the old DIE. */
18198 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18199 && (DECL_ARTIFICIAL (decl)
18200 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18201 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18202 == (unsigned) s.line))))
18204 subr_die = old_die;
18206 /* Clear out the declaration attribute and the formal parameters.
18207 Do not remove all children, because it is possible that this
18208 declaration die was forced using force_decl_die(). In such
18209 cases die that forced declaration die (e.g. TAG_imported_module)
18210 is one of the children that we do not want to remove. */
18211 remove_AT (subr_die, DW_AT_declaration);
18212 remove_AT (subr_die, DW_AT_object_pointer);
18213 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18215 else
18217 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18218 add_AT_specification (subr_die, old_die);
18219 add_pubname (decl, subr_die);
18220 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18221 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18222 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18223 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18225 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18226 emit the real type on the definition die. */
18227 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18229 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18230 if (die == auto_die || die == decltype_auto_die)
18231 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18232 0, 0, context_die);
18236 else
18238 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18240 if (TREE_PUBLIC (decl))
18241 add_AT_flag (subr_die, DW_AT_external, 1);
18243 add_name_and_src_coords_attributes (subr_die, decl);
18244 add_pubname (decl, subr_die);
18245 if (debug_info_level > DINFO_LEVEL_TERSE)
18247 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18248 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18249 0, 0, context_die);
18252 add_pure_or_virtual_attribute (subr_die, decl);
18253 if (DECL_ARTIFICIAL (decl))
18254 add_AT_flag (subr_die, DW_AT_artificial, 1);
18256 add_accessibility_attribute (subr_die, decl);
18259 if (declaration)
18261 if (!old_die || !get_AT (old_die, DW_AT_inline))
18263 add_AT_flag (subr_die, DW_AT_declaration, 1);
18265 /* If this is an explicit function declaration then generate
18266 a DW_AT_explicit attribute. */
18267 if (lang_hooks.decls.function_decl_explicit_p (decl)
18268 && (dwarf_version >= 3 || !dwarf_strict))
18269 add_AT_flag (subr_die, DW_AT_explicit, 1);
18271 /* The first time we see a member function, it is in the context of
18272 the class to which it belongs. We make sure of this by emitting
18273 the class first. The next time is the definition, which is
18274 handled above. The two may come from the same source text.
18276 Note that force_decl_die() forces function declaration die. It is
18277 later reused to represent definition. */
18278 equate_decl_number_to_die (decl, subr_die);
18281 else if (DECL_ABSTRACT (decl))
18283 if (DECL_DECLARED_INLINE_P (decl))
18285 if (cgraph_function_possibly_inlined_p (decl))
18286 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18287 else
18288 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18290 else
18292 if (cgraph_function_possibly_inlined_p (decl))
18293 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18294 else
18295 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18298 if (DECL_DECLARED_INLINE_P (decl)
18299 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18300 add_AT_flag (subr_die, DW_AT_artificial, 1);
18302 equate_decl_number_to_die (decl, subr_die);
18304 else if (!DECL_EXTERNAL (decl))
18306 HOST_WIDE_INT cfa_fb_offset;
18307 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18309 if (!old_die || !get_AT (old_die, DW_AT_inline))
18310 equate_decl_number_to_die (decl, subr_die);
18312 gcc_checking_assert (fun);
18313 if (!flag_reorder_blocks_and_partition)
18315 dw_fde_ref fde = fun->fde;
18316 if (fde->dw_fde_begin)
18318 /* We have already generated the labels. */
18319 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18320 fde->dw_fde_end, false);
18322 else
18324 /* Create start/end labels and add the range. */
18325 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18326 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18327 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18328 current_function_funcdef_no);
18329 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18330 current_function_funcdef_no);
18331 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18332 false);
18335 #if VMS_DEBUGGING_INFO
18336 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18337 Section 2.3 Prologue and Epilogue Attributes:
18338 When a breakpoint is set on entry to a function, it is generally
18339 desirable for execution to be suspended, not on the very first
18340 instruction of the function, but rather at a point after the
18341 function's frame has been set up, after any language defined local
18342 declaration processing has been completed, and before execution of
18343 the first statement of the function begins. Debuggers generally
18344 cannot properly determine where this point is. Similarly for a
18345 breakpoint set on exit from a function. The prologue and epilogue
18346 attributes allow a compiler to communicate the location(s) to use. */
18349 if (fde->dw_fde_vms_end_prologue)
18350 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18351 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18353 if (fde->dw_fde_vms_begin_epilogue)
18354 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18355 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18357 #endif
18360 else
18362 /* Generate pubnames entries for the split function code ranges. */
18363 dw_fde_ref fde = fun->fde;
18365 if (fde->dw_fde_second_begin)
18367 if (dwarf_version >= 3 || !dwarf_strict)
18369 /* We should use ranges for non-contiguous code section
18370 addresses. Use the actual code range for the initial
18371 section, since the HOT/COLD labels might precede an
18372 alignment offset. */
18373 bool range_list_added = false;
18374 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18375 fde->dw_fde_end, &range_list_added,
18376 false);
18377 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18378 fde->dw_fde_second_end,
18379 &range_list_added, false);
18380 if (range_list_added)
18381 add_ranges (NULL);
18383 else
18385 /* There is no real support in DW2 for this .. so we make
18386 a work-around. First, emit the pub name for the segment
18387 containing the function label. Then make and emit a
18388 simplified subprogram DIE for the second segment with the
18389 name pre-fixed by __hot/cold_sect_of_. We use the same
18390 linkage name for the second die so that gdb will find both
18391 sections when given "b foo". */
18392 const char *name = NULL;
18393 tree decl_name = DECL_NAME (decl);
18394 dw_die_ref seg_die;
18396 /* Do the 'primary' section. */
18397 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18398 fde->dw_fde_end, false);
18400 /* Build a minimal DIE for the secondary section. */
18401 seg_die = new_die (DW_TAG_subprogram,
18402 subr_die->die_parent, decl);
18404 if (TREE_PUBLIC (decl))
18405 add_AT_flag (seg_die, DW_AT_external, 1);
18407 if (decl_name != NULL
18408 && IDENTIFIER_POINTER (decl_name) != NULL)
18410 name = dwarf2_name (decl, 1);
18411 if (! DECL_ARTIFICIAL (decl))
18412 add_src_coords_attributes (seg_die, decl);
18414 add_linkage_name (seg_die, decl);
18416 gcc_assert (name != NULL);
18417 add_pure_or_virtual_attribute (seg_die, decl);
18418 if (DECL_ARTIFICIAL (decl))
18419 add_AT_flag (seg_die, DW_AT_artificial, 1);
18421 name = concat ("__second_sect_of_", name, NULL);
18422 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18423 fde->dw_fde_second_end, false);
18424 add_name_attribute (seg_die, name);
18425 if (want_pubnames ())
18426 add_pubname_string (name, seg_die);
18429 else
18430 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18431 false);
18434 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18436 /* We define the "frame base" as the function's CFA. This is more
18437 convenient for several reasons: (1) It's stable across the prologue
18438 and epilogue, which makes it better than just a frame pointer,
18439 (2) With dwarf3, there exists a one-byte encoding that allows us
18440 to reference the .debug_frame data by proxy, but failing that,
18441 (3) We can at least reuse the code inspection and interpretation
18442 code that determines the CFA position at various points in the
18443 function. */
18444 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18446 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18447 add_AT_loc (subr_die, DW_AT_frame_base, op);
18449 else
18451 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18452 if (list->dw_loc_next)
18453 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18454 else
18455 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18458 /* Compute a displacement from the "steady-state frame pointer" to
18459 the CFA. The former is what all stack slots and argument slots
18460 will reference in the rtl; the latter is what we've told the
18461 debugger about. We'll need to adjust all frame_base references
18462 by this displacement. */
18463 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18465 if (fun->static_chain_decl)
18466 add_AT_location_description (subr_die, DW_AT_static_link,
18467 loc_list_from_tree (fun->static_chain_decl, 2));
18470 /* Generate child dies for template paramaters. */
18471 if (debug_info_level > DINFO_LEVEL_TERSE)
18472 gen_generic_params_dies (decl);
18474 /* Now output descriptions of the arguments for this function. This gets
18475 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18476 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18477 `...' at the end of the formal parameter list. In order to find out if
18478 there was a trailing ellipsis or not, we must instead look at the type
18479 associated with the FUNCTION_DECL. This will be a node of type
18480 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18481 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18482 an ellipsis at the end. */
18484 /* In the case where we are describing a mere function declaration, all we
18485 need to do here (and all we *can* do here) is to describe the *types* of
18486 its formal parameters. */
18487 if (debug_info_level <= DINFO_LEVEL_TERSE)
18489 else if (declaration)
18490 gen_formal_types_die (decl, subr_die);
18491 else
18493 /* Generate DIEs to represent all known formal parameters. */
18494 tree parm = DECL_ARGUMENTS (decl);
18495 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18496 tree generic_decl_parm = generic_decl
18497 ? DECL_ARGUMENTS (generic_decl)
18498 : NULL;
18500 /* Now we want to walk the list of parameters of the function and
18501 emit their relevant DIEs.
18503 We consider the case of DECL being an instance of a generic function
18504 as well as it being a normal function.
18506 If DECL is an instance of a generic function we walk the
18507 parameters of the generic function declaration _and_ the parameters of
18508 DECL itself. This is useful because we want to emit specific DIEs for
18509 function parameter packs and those are declared as part of the
18510 generic function declaration. In that particular case,
18511 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18512 That DIE has children DIEs representing the set of arguments
18513 of the pack. Note that the set of pack arguments can be empty.
18514 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18515 children DIE.
18517 Otherwise, we just consider the parameters of DECL. */
18518 while (generic_decl_parm || parm)
18520 if (generic_decl_parm
18521 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18522 gen_formal_parameter_pack_die (generic_decl_parm,
18523 parm, subr_die,
18524 &parm);
18525 else if (parm)
18527 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18529 if (parm == DECL_ARGUMENTS (decl)
18530 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18531 && parm_die
18532 && (dwarf_version >= 3 || !dwarf_strict))
18533 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18535 parm = DECL_CHAIN (parm);
18538 if (generic_decl_parm)
18539 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18542 /* Decide whether we need an unspecified_parameters DIE at the end.
18543 There are 2 more cases to do this for: 1) the ansi ... declaration -
18544 this is detectable when the end of the arg list is not a
18545 void_type_node 2) an unprototyped function declaration (not a
18546 definition). This just means that we have no info about the
18547 parameters at all. */
18548 if (prototype_p (TREE_TYPE (decl)))
18550 /* This is the prototyped case, check for.... */
18551 if (stdarg_p (TREE_TYPE (decl)))
18552 gen_unspecified_parameters_die (decl, subr_die);
18554 else if (DECL_INITIAL (decl) == NULL_TREE)
18555 gen_unspecified_parameters_die (decl, subr_die);
18558 /* Output Dwarf info for all of the stuff within the body of the function
18559 (if it has one - it may be just a declaration). */
18560 outer_scope = DECL_INITIAL (decl);
18562 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18563 a function. This BLOCK actually represents the outermost binding contour
18564 for the function, i.e. the contour in which the function's formal
18565 parameters and labels get declared. Curiously, it appears that the front
18566 end doesn't actually put the PARM_DECL nodes for the current function onto
18567 the BLOCK_VARS list for this outer scope, but are strung off of the
18568 DECL_ARGUMENTS list for the function instead.
18570 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18571 the LABEL_DECL nodes for the function however, and we output DWARF info
18572 for those in decls_for_scope. Just within the `outer_scope' there will be
18573 a BLOCK node representing the function's outermost pair of curly braces,
18574 and any blocks used for the base and member initializers of a C++
18575 constructor function. */
18576 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18578 int call_site_note_count = 0;
18579 int tail_call_site_note_count = 0;
18581 /* Emit a DW_TAG_variable DIE for a named return value. */
18582 if (DECL_NAME (DECL_RESULT (decl)))
18583 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18585 current_function_has_inlines = 0;
18586 decls_for_scope (outer_scope, subr_die, 0);
18588 if (call_arg_locations && !dwarf_strict)
18590 struct call_arg_loc_node *ca_loc;
18591 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18593 dw_die_ref die = NULL;
18594 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18595 rtx arg, next_arg;
18597 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18598 arg; arg = next_arg)
18600 dw_loc_descr_ref reg, val;
18601 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18602 dw_die_ref cdie, tdie = NULL;
18604 next_arg = XEXP (arg, 1);
18605 if (REG_P (XEXP (XEXP (arg, 0), 0))
18606 && next_arg
18607 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18608 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18609 && REGNO (XEXP (XEXP (arg, 0), 0))
18610 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18611 next_arg = XEXP (next_arg, 1);
18612 if (mode == VOIDmode)
18614 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18615 if (mode == VOIDmode)
18616 mode = GET_MODE (XEXP (arg, 0));
18618 if (mode == VOIDmode || mode == BLKmode)
18619 continue;
18620 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18622 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18623 tloc = XEXP (XEXP (arg, 0), 1);
18624 continue;
18626 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18627 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18629 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18630 tlocc = XEXP (XEXP (arg, 0), 1);
18631 continue;
18633 reg = NULL;
18634 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18635 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18636 VAR_INIT_STATUS_INITIALIZED);
18637 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18639 rtx mem = XEXP (XEXP (arg, 0), 0);
18640 reg = mem_loc_descriptor (XEXP (mem, 0),
18641 get_address_mode (mem),
18642 GET_MODE (mem),
18643 VAR_INIT_STATUS_INITIALIZED);
18645 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18646 == DEBUG_PARAMETER_REF)
18648 tree tdecl
18649 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18650 tdie = lookup_decl_die (tdecl);
18651 if (tdie == NULL)
18652 continue;
18654 else
18655 continue;
18656 if (reg == NULL
18657 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18658 != DEBUG_PARAMETER_REF)
18659 continue;
18660 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18661 VOIDmode,
18662 VAR_INIT_STATUS_INITIALIZED);
18663 if (val == NULL)
18664 continue;
18665 if (die == NULL)
18666 die = gen_call_site_die (decl, subr_die, ca_loc);
18667 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18668 NULL_TREE);
18669 if (reg != NULL)
18670 add_AT_loc (cdie, DW_AT_location, reg);
18671 else if (tdie != NULL)
18672 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18673 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18674 if (next_arg != XEXP (arg, 1))
18676 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18677 if (mode == VOIDmode)
18678 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18679 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18680 0), 1),
18681 mode, VOIDmode,
18682 VAR_INIT_STATUS_INITIALIZED);
18683 if (val != NULL)
18684 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18687 if (die == NULL
18688 && (ca_loc->symbol_ref || tloc))
18689 die = gen_call_site_die (decl, subr_die, ca_loc);
18690 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18692 dw_loc_descr_ref tval = NULL;
18694 if (tloc != NULL_RTX)
18695 tval = mem_loc_descriptor (tloc,
18696 GET_MODE (tloc) == VOIDmode
18697 ? Pmode : GET_MODE (tloc),
18698 VOIDmode,
18699 VAR_INIT_STATUS_INITIALIZED);
18700 if (tval)
18701 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18702 else if (tlocc != NULL_RTX)
18704 tval = mem_loc_descriptor (tlocc,
18705 GET_MODE (tlocc) == VOIDmode
18706 ? Pmode : GET_MODE (tlocc),
18707 VOIDmode,
18708 VAR_INIT_STATUS_INITIALIZED);
18709 if (tval)
18710 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18711 tval);
18714 if (die != NULL)
18716 call_site_note_count++;
18717 if (ca_loc->tail_call_p)
18718 tail_call_site_note_count++;
18722 call_arg_locations = NULL;
18723 call_arg_loc_last = NULL;
18724 if (tail_call_site_count >= 0
18725 && tail_call_site_count == tail_call_site_note_count
18726 && !dwarf_strict)
18728 if (call_site_count >= 0
18729 && call_site_count == call_site_note_count)
18730 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18731 else
18732 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18734 call_site_count = -1;
18735 tail_call_site_count = -1;
18738 if (subr_die != old_die)
18739 /* Add the calling convention attribute if requested. */
18740 add_calling_convention_attribute (subr_die, decl);
18743 /* Returns a hash value for X (which really is a die_struct). */
18745 static hashval_t
18746 common_block_die_table_hash (const void *x)
18748 const_dw_die_ref d = (const_dw_die_ref) x;
18749 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18752 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18753 as decl_id and die_parent of die_struct Y. */
18755 static int
18756 common_block_die_table_eq (const void *x, const void *y)
18758 const_dw_die_ref d = (const_dw_die_ref) x;
18759 const_dw_die_ref e = (const_dw_die_ref) y;
18760 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18763 /* Generate a DIE to represent a declared data object.
18764 Either DECL or ORIGIN must be non-null. */
18766 static void
18767 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18769 HOST_WIDE_INT off = 0;
18770 tree com_decl;
18771 tree decl_or_origin = decl ? decl : origin;
18772 tree ultimate_origin;
18773 dw_die_ref var_die;
18774 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18775 dw_die_ref origin_die;
18776 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18777 || class_or_namespace_scope_p (context_die));
18778 bool specialization_p = false;
18780 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18781 if (decl || ultimate_origin)
18782 origin = ultimate_origin;
18783 com_decl = fortran_common (decl_or_origin, &off);
18785 /* Symbol in common gets emitted as a child of the common block, in the form
18786 of a data member. */
18787 if (com_decl)
18789 dw_die_ref com_die;
18790 dw_loc_list_ref loc;
18791 die_node com_die_arg;
18793 var_die = lookup_decl_die (decl_or_origin);
18794 if (var_die)
18796 if (get_AT (var_die, DW_AT_location) == NULL)
18798 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18799 if (loc)
18801 if (off)
18803 /* Optimize the common case. */
18804 if (single_element_loc_list_p (loc)
18805 && loc->expr->dw_loc_opc == DW_OP_addr
18806 && loc->expr->dw_loc_next == NULL
18807 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18808 == SYMBOL_REF)
18810 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18811 loc->expr->dw_loc_oprnd1.v.val_addr
18812 = plus_constant (GET_MODE (x), x , off);
18814 else
18815 loc_list_plus_const (loc, off);
18817 add_AT_location_description (var_die, DW_AT_location, loc);
18818 remove_AT (var_die, DW_AT_declaration);
18821 return;
18824 if (common_block_die_table == NULL)
18825 common_block_die_table
18826 = htab_create_ggc (10, common_block_die_table_hash,
18827 common_block_die_table_eq, NULL);
18829 com_die_arg.decl_id = DECL_UID (com_decl);
18830 com_die_arg.die_parent = context_die;
18831 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18832 loc = loc_list_from_tree (com_decl, 2);
18833 if (com_die == NULL)
18835 const char *cnam
18836 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18837 void **slot;
18839 com_die = new_die (DW_TAG_common_block, context_die, decl);
18840 add_name_and_src_coords_attributes (com_die, com_decl);
18841 if (loc)
18843 add_AT_location_description (com_die, DW_AT_location, loc);
18844 /* Avoid sharing the same loc descriptor between
18845 DW_TAG_common_block and DW_TAG_variable. */
18846 loc = loc_list_from_tree (com_decl, 2);
18848 else if (DECL_EXTERNAL (decl))
18849 add_AT_flag (com_die, DW_AT_declaration, 1);
18850 if (want_pubnames ())
18851 add_pubname_string (cnam, com_die); /* ??? needed? */
18852 com_die->decl_id = DECL_UID (com_decl);
18853 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18854 *slot = (void *) com_die;
18856 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18858 add_AT_location_description (com_die, DW_AT_location, loc);
18859 loc = loc_list_from_tree (com_decl, 2);
18860 remove_AT (com_die, DW_AT_declaration);
18862 var_die = new_die (DW_TAG_variable, com_die, decl);
18863 add_name_and_src_coords_attributes (var_die, decl);
18864 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18865 TREE_THIS_VOLATILE (decl), context_die);
18866 add_AT_flag (var_die, DW_AT_external, 1);
18867 if (loc)
18869 if (off)
18871 /* Optimize the common case. */
18872 if (single_element_loc_list_p (loc)
18873 && loc->expr->dw_loc_opc == DW_OP_addr
18874 && loc->expr->dw_loc_next == NULL
18875 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18877 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18878 loc->expr->dw_loc_oprnd1.v.val_addr
18879 = plus_constant (GET_MODE (x), x, off);
18881 else
18882 loc_list_plus_const (loc, off);
18884 add_AT_location_description (var_die, DW_AT_location, loc);
18886 else if (DECL_EXTERNAL (decl))
18887 add_AT_flag (var_die, DW_AT_declaration, 1);
18888 equate_decl_number_to_die (decl, var_die);
18889 return;
18892 /* If the compiler emitted a definition for the DECL declaration
18893 and if we already emitted a DIE for it, don't emit a second
18894 DIE for it again. Allow re-declarations of DECLs that are
18895 inside functions, though. */
18896 if (old_die && declaration && !local_scope_p (context_die))
18897 return;
18899 /* For static data members, the declaration in the class is supposed
18900 to have DW_TAG_member tag; the specification should still be
18901 DW_TAG_variable referencing the DW_TAG_member DIE. */
18902 if (declaration && class_scope_p (context_die))
18903 var_die = new_die (DW_TAG_member, context_die, decl);
18904 else
18905 var_die = new_die (DW_TAG_variable, context_die, decl);
18907 origin_die = NULL;
18908 if (origin != NULL)
18909 origin_die = add_abstract_origin_attribute (var_die, origin);
18911 /* Loop unrolling can create multiple blocks that refer to the same
18912 static variable, so we must test for the DW_AT_declaration flag.
18914 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18915 copy decls and set the DECL_ABSTRACT flag on them instead of
18916 sharing them.
18918 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18920 ??? The declare_in_namespace support causes us to get two DIEs for one
18921 variable, both of which are declarations. We want to avoid considering
18922 one to be a specification, so we must test that this DIE is not a
18923 declaration. */
18924 else if (old_die && TREE_STATIC (decl) && ! declaration
18925 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18927 /* This is a definition of a C++ class level static. */
18928 add_AT_specification (var_die, old_die);
18929 specialization_p = true;
18930 if (DECL_NAME (decl))
18932 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18933 struct dwarf_file_data * file_index = lookup_filename (s.file);
18935 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18936 add_AT_file (var_die, DW_AT_decl_file, file_index);
18938 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18939 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18941 if (old_die->die_tag == DW_TAG_member)
18942 add_linkage_name (var_die, decl);
18945 else
18946 add_name_and_src_coords_attributes (var_die, decl);
18948 if ((origin == NULL && !specialization_p)
18949 || (origin != NULL
18950 && !DECL_ABSTRACT (decl_or_origin)
18951 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18952 decl_function_context
18953 (decl_or_origin))))
18955 tree type = TREE_TYPE (decl_or_origin);
18957 if (decl_by_reference_p (decl_or_origin))
18958 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18959 else
18960 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18961 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18964 if (origin == NULL && !specialization_p)
18966 if (TREE_PUBLIC (decl))
18967 add_AT_flag (var_die, DW_AT_external, 1);
18969 if (DECL_ARTIFICIAL (decl))
18970 add_AT_flag (var_die, DW_AT_artificial, 1);
18972 add_accessibility_attribute (var_die, decl);
18975 if (declaration)
18976 add_AT_flag (var_die, DW_AT_declaration, 1);
18978 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18979 equate_decl_number_to_die (decl, var_die);
18981 if (! declaration
18982 && (! DECL_ABSTRACT (decl_or_origin)
18983 /* Local static vars are shared between all clones/inlines,
18984 so emit DW_AT_location on the abstract DIE if DECL_RTL is
18985 already set. */
18986 || (TREE_CODE (decl_or_origin) == VAR_DECL
18987 && TREE_STATIC (decl_or_origin)
18988 && DECL_RTL_SET_P (decl_or_origin)))
18989 /* When abstract origin already has DW_AT_location attribute, no need
18990 to add it again. */
18991 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18993 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18994 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18995 defer_location (decl_or_origin, var_die);
18996 else
18997 add_location_or_const_value_attribute (var_die, decl_or_origin,
18998 decl == NULL, DW_AT_location);
18999 add_pubname (decl_or_origin, var_die);
19001 else
19002 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19005 /* Generate a DIE to represent a named constant. */
19007 static void
19008 gen_const_die (tree decl, dw_die_ref context_die)
19010 dw_die_ref const_die;
19011 tree type = TREE_TYPE (decl);
19013 const_die = new_die (DW_TAG_constant, context_die, decl);
19014 add_name_and_src_coords_attributes (const_die, decl);
19015 add_type_attribute (const_die, type, 1, 0, context_die);
19016 if (TREE_PUBLIC (decl))
19017 add_AT_flag (const_die, DW_AT_external, 1);
19018 if (DECL_ARTIFICIAL (decl))
19019 add_AT_flag (const_die, DW_AT_artificial, 1);
19020 tree_add_const_value_attribute_for_decl (const_die, decl);
19023 /* Generate a DIE to represent a label identifier. */
19025 static void
19026 gen_label_die (tree decl, dw_die_ref context_die)
19028 tree origin = decl_ultimate_origin (decl);
19029 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19030 rtx insn;
19031 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19033 if (origin != NULL)
19034 add_abstract_origin_attribute (lbl_die, origin);
19035 else
19036 add_name_and_src_coords_attributes (lbl_die, decl);
19038 if (DECL_ABSTRACT (decl))
19039 equate_decl_number_to_die (decl, lbl_die);
19040 else
19042 insn = DECL_RTL_IF_SET (decl);
19044 /* Deleted labels are programmer specified labels which have been
19045 eliminated because of various optimizations. We still emit them
19046 here so that it is possible to put breakpoints on them. */
19047 if (insn
19048 && (LABEL_P (insn)
19049 || ((NOTE_P (insn)
19050 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19052 /* When optimization is enabled (via -O) some parts of the compiler
19053 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19054 represent source-level labels which were explicitly declared by
19055 the user. This really shouldn't be happening though, so catch
19056 it if it ever does happen. */
19057 gcc_assert (!INSN_DELETED_P (insn));
19059 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19060 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19062 else if (insn
19063 && NOTE_P (insn)
19064 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19065 && CODE_LABEL_NUMBER (insn) != -1)
19067 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19068 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19073 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19074 attributes to the DIE for a block STMT, to describe where the inlined
19075 function was called from. This is similar to add_src_coords_attributes. */
19077 static inline void
19078 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19080 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19082 if (dwarf_version >= 3 || !dwarf_strict)
19084 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19085 add_AT_unsigned (die, DW_AT_call_line, s.line);
19090 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19091 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19093 static inline void
19094 add_high_low_attributes (tree stmt, dw_die_ref die)
19096 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19098 if (BLOCK_FRAGMENT_CHAIN (stmt)
19099 && (dwarf_version >= 3 || !dwarf_strict))
19101 tree chain, superblock = NULL_TREE;
19102 dw_die_ref pdie;
19103 dw_attr_ref attr = NULL;
19105 if (inlined_function_outer_scope_p (stmt))
19107 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19108 BLOCK_NUMBER (stmt));
19109 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19112 /* Optimize duplicate .debug_ranges lists or even tails of
19113 lists. If this BLOCK has same ranges as its supercontext,
19114 lookup DW_AT_ranges attribute in the supercontext (and
19115 recursively so), verify that the ranges_table contains the
19116 right values and use it instead of adding a new .debug_range. */
19117 for (chain = stmt, pdie = die;
19118 BLOCK_SAME_RANGE (chain);
19119 chain = BLOCK_SUPERCONTEXT (chain))
19121 dw_attr_ref new_attr;
19123 pdie = pdie->die_parent;
19124 if (pdie == NULL)
19125 break;
19126 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19127 break;
19128 new_attr = get_AT (pdie, DW_AT_ranges);
19129 if (new_attr == NULL
19130 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19131 break;
19132 attr = new_attr;
19133 superblock = BLOCK_SUPERCONTEXT (chain);
19135 if (attr != NULL
19136 && (ranges_table[attr->dw_attr_val.v.val_offset
19137 / 2 / DWARF2_ADDR_SIZE].num
19138 == BLOCK_NUMBER (superblock))
19139 && BLOCK_FRAGMENT_CHAIN (superblock))
19141 unsigned long off = attr->dw_attr_val.v.val_offset
19142 / 2 / DWARF2_ADDR_SIZE;
19143 unsigned long supercnt = 0, thiscnt = 0;
19144 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19145 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19147 ++supercnt;
19148 gcc_checking_assert (ranges_table[off + supercnt].num
19149 == BLOCK_NUMBER (chain));
19151 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19152 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19153 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19154 ++thiscnt;
19155 gcc_assert (supercnt >= thiscnt);
19156 add_AT_range_list (die, DW_AT_ranges,
19157 ((off + supercnt - thiscnt)
19158 * 2 * DWARF2_ADDR_SIZE),
19159 false);
19160 return;
19163 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19165 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19168 add_ranges (chain);
19169 chain = BLOCK_FRAGMENT_CHAIN (chain);
19171 while (chain);
19172 add_ranges (NULL);
19174 else
19176 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19177 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19178 BLOCK_NUMBER (stmt));
19179 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19180 BLOCK_NUMBER (stmt));
19181 add_AT_low_high_pc (die, label, label_high, false);
19185 /* Generate a DIE for a lexical block. */
19187 static void
19188 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19190 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19192 if (call_arg_locations)
19194 if (block_map.length () <= BLOCK_NUMBER (stmt))
19195 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19196 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19199 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19200 add_high_low_attributes (stmt, stmt_die);
19202 decls_for_scope (stmt, stmt_die, depth);
19205 /* Generate a DIE for an inlined subprogram. */
19207 static void
19208 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19210 tree decl;
19212 /* The instance of function that is effectively being inlined shall not
19213 be abstract. */
19214 gcc_assert (! BLOCK_ABSTRACT (stmt));
19216 decl = block_ultimate_origin (stmt);
19218 /* Emit info for the abstract instance first, if we haven't yet. We
19219 must emit this even if the block is abstract, otherwise when we
19220 emit the block below (or elsewhere), we may end up trying to emit
19221 a die whose origin die hasn't been emitted, and crashing. */
19222 dwarf2out_abstract_function (decl);
19224 if (! BLOCK_ABSTRACT (stmt))
19226 dw_die_ref subr_die
19227 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19229 if (call_arg_locations)
19231 if (block_map.length () <= BLOCK_NUMBER (stmt))
19232 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19233 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19235 add_abstract_origin_attribute (subr_die, decl);
19236 if (TREE_ASM_WRITTEN (stmt))
19237 add_high_low_attributes (stmt, subr_die);
19238 add_call_src_coords_attributes (stmt, subr_die);
19240 decls_for_scope (stmt, subr_die, depth);
19241 current_function_has_inlines = 1;
19245 /* Generate a DIE for a field in a record, or structure. */
19247 static void
19248 gen_field_die (tree decl, dw_die_ref context_die)
19250 dw_die_ref decl_die;
19252 if (TREE_TYPE (decl) == error_mark_node)
19253 return;
19255 decl_die = new_die (DW_TAG_member, context_die, decl);
19256 add_name_and_src_coords_attributes (decl_die, decl);
19257 add_type_attribute (decl_die, member_declared_type (decl), 0, 0, context_die);
19259 if (DECL_BIT_FIELD_TYPE (decl))
19261 add_byte_size_attribute (decl_die, decl);
19262 add_bit_size_attribute (decl_die, decl);
19263 add_bit_offset_attribute (decl_die, decl);
19266 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19267 add_data_member_location_attribute (decl_die, decl);
19269 if (DECL_ARTIFICIAL (decl))
19270 add_AT_flag (decl_die, DW_AT_artificial, 1);
19272 add_accessibility_attribute (decl_die, decl);
19274 /* Equate decl number to die, so that we can look up this decl later on. */
19275 equate_decl_number_to_die (decl, decl_die);
19278 #if 0
19279 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19280 Use modified_type_die instead.
19281 We keep this code here just in case these types of DIEs may be needed to
19282 represent certain things in other languages (e.g. Pascal) someday. */
19284 static void
19285 gen_pointer_type_die (tree type, dw_die_ref context_die)
19287 dw_die_ref ptr_die
19288 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19290 equate_type_number_to_die (type, ptr_die);
19291 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19292 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19295 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19296 Use modified_type_die instead.
19297 We keep this code here just in case these types of DIEs may be needed to
19298 represent certain things in other languages (e.g. Pascal) someday. */
19300 static void
19301 gen_reference_type_die (tree type, dw_die_ref context_die)
19303 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19305 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19306 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19307 else
19308 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19310 equate_type_number_to_die (type, ref_die);
19311 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19312 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19314 #endif
19316 /* Generate a DIE for a pointer to a member type. */
19318 static void
19319 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19321 dw_die_ref ptr_die
19322 = new_die (DW_TAG_ptr_to_member_type,
19323 scope_die_for (type, context_die), type);
19325 equate_type_number_to_die (type, ptr_die);
19326 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19327 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19328 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19331 typedef const char *dchar_p; /* For DEF_VEC_P. */
19333 static char *producer_string;
19335 /* Return a heap allocated producer string including command line options
19336 if -grecord-gcc-switches. */
19338 static char *
19339 gen_producer_string (void)
19341 size_t j;
19342 auto_vec<dchar_p> switches;
19343 const char *language_string = lang_hooks.name;
19344 char *producer, *tail;
19345 const char *p;
19346 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19347 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19349 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19350 switch (save_decoded_options[j].opt_index)
19352 case OPT_o:
19353 case OPT_d:
19354 case OPT_dumpbase:
19355 case OPT_dumpdir:
19356 case OPT_auxbase:
19357 case OPT_auxbase_strip:
19358 case OPT_quiet:
19359 case OPT_version:
19360 case OPT_v:
19361 case OPT_w:
19362 case OPT_L:
19363 case OPT_D:
19364 case OPT_I:
19365 case OPT_U:
19366 case OPT_SPECIAL_unknown:
19367 case OPT_SPECIAL_ignore:
19368 case OPT_SPECIAL_program_name:
19369 case OPT_SPECIAL_input_file:
19370 case OPT_grecord_gcc_switches:
19371 case OPT_gno_record_gcc_switches:
19372 case OPT__output_pch_:
19373 case OPT_fdiagnostics_show_location_:
19374 case OPT_fdiagnostics_show_option:
19375 case OPT_fdiagnostics_show_caret:
19376 case OPT_fdiagnostics_color_:
19377 case OPT_fverbose_asm:
19378 case OPT____:
19379 case OPT__sysroot_:
19380 case OPT_nostdinc:
19381 case OPT_nostdinc__:
19382 /* Ignore these. */
19383 continue;
19384 default:
19385 if (cl_options[save_decoded_options[j].opt_index].flags
19386 & CL_NO_DWARF_RECORD)
19387 continue;
19388 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19389 == '-');
19390 switch (save_decoded_options[j].canonical_option[0][1])
19392 case 'M':
19393 case 'i':
19394 case 'W':
19395 continue;
19396 case 'f':
19397 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19398 "dump", 4) == 0)
19399 continue;
19400 break;
19401 default:
19402 break;
19404 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19405 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19406 break;
19409 producer = XNEWVEC (char, plen + 1 + len + 1);
19410 tail = producer;
19411 sprintf (tail, "%s %s", language_string, version_string);
19412 tail += plen;
19414 FOR_EACH_VEC_ELT (switches, j, p)
19416 len = strlen (p);
19417 *tail = ' ';
19418 memcpy (tail + 1, p, len);
19419 tail += len + 1;
19422 *tail = '\0';
19423 return producer;
19426 /* Generate the DIE for the compilation unit. */
19428 static dw_die_ref
19429 gen_compile_unit_die (const char *filename)
19431 dw_die_ref die;
19432 const char *language_string = lang_hooks.name;
19433 int language;
19435 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19437 if (filename)
19439 add_name_attribute (die, filename);
19440 /* Don't add cwd for <built-in>. */
19441 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19442 add_comp_dir_attribute (die);
19445 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19447 /* If our producer is LTO try to figure out a common language to use
19448 from the global list of translation units. */
19449 if (strcmp (language_string, "GNU GIMPLE") == 0)
19451 unsigned i;
19452 tree t;
19453 const char *common_lang = NULL;
19455 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19457 if (!TRANSLATION_UNIT_LANGUAGE (t))
19458 continue;
19459 if (!common_lang)
19460 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19461 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19463 else if (strncmp (common_lang, "GNU C", 5) == 0
19464 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19465 /* Mixing C and C++ is ok, use C++ in that case. */
19466 common_lang = "GNU C++";
19467 else
19469 /* Fall back to C. */
19470 common_lang = NULL;
19471 break;
19475 if (common_lang)
19476 language_string = common_lang;
19479 language = DW_LANG_C89;
19480 if (strcmp (language_string, "GNU C++") == 0)
19481 language = DW_LANG_C_plus_plus;
19482 else if (strcmp (language_string, "GNU F77") == 0)
19483 language = DW_LANG_Fortran77;
19484 else if (strcmp (language_string, "GNU Pascal") == 0)
19485 language = DW_LANG_Pascal83;
19486 else if (use_upc_dwarf2_extensions
19487 && (strcmp (language_string, "GNU UPC") == 0))
19488 language = DW_LANG_Upc;
19489 else if (dwarf_version >= 3 || !dwarf_strict)
19491 if (strcmp (language_string, "GNU Ada") == 0)
19492 language = DW_LANG_Ada95;
19493 else if (strcmp (language_string, "GNU Fortran") == 0)
19494 language = DW_LANG_Fortran95;
19495 else if (strcmp (language_string, "GNU Java") == 0)
19496 language = DW_LANG_Java;
19497 else if (strcmp (language_string, "GNU Objective-C") == 0)
19498 language = DW_LANG_ObjC;
19499 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19500 language = DW_LANG_ObjC_plus_plus;
19501 else if (dwarf_version >= 5 || !dwarf_strict)
19503 if (strcmp (language_string, "GNU Go") == 0)
19504 language = DW_LANG_Go;
19507 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19508 else if (strcmp (language_string, "GNU Fortran") == 0)
19509 language = DW_LANG_Fortran90;
19511 add_AT_unsigned (die, DW_AT_language, language);
19513 switch (language)
19515 case DW_LANG_Fortran77:
19516 case DW_LANG_Fortran90:
19517 case DW_LANG_Fortran95:
19518 /* Fortran has case insensitive identifiers and the front-end
19519 lowercases everything. */
19520 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19521 break;
19522 default:
19523 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19524 break;
19526 return die;
19529 /* Generate the DIE for a base class. */
19531 static void
19532 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19534 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19536 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19537 add_data_member_location_attribute (die, binfo);
19539 if (BINFO_VIRTUAL_P (binfo))
19540 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19542 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19543 children, otherwise the default is DW_ACCESS_public. In DWARF2
19544 the default has always been DW_ACCESS_private. */
19545 if (access == access_public_node)
19547 if (dwarf_version == 2
19548 || context_die->die_tag == DW_TAG_class_type)
19549 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19551 else if (access == access_protected_node)
19552 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19553 else if (dwarf_version > 2
19554 && context_die->die_tag != DW_TAG_class_type)
19555 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19558 /* Generate a DIE for a class member. */
19560 static void
19561 gen_member_die (tree type, dw_die_ref context_die)
19563 tree member;
19564 tree binfo = TYPE_BINFO (type);
19565 dw_die_ref child;
19567 /* If this is not an incomplete type, output descriptions of each of its
19568 members. Note that as we output the DIEs necessary to represent the
19569 members of this record or union type, we will also be trying to output
19570 DIEs to represent the *types* of those members. However the `type'
19571 function (above) will specifically avoid generating type DIEs for member
19572 types *within* the list of member DIEs for this (containing) type except
19573 for those types (of members) which are explicitly marked as also being
19574 members of this (containing) type themselves. The g++ front- end can
19575 force any given type to be treated as a member of some other (containing)
19576 type by setting the TYPE_CONTEXT of the given (member) type to point to
19577 the TREE node representing the appropriate (containing) type. */
19579 /* First output info about the base classes. */
19580 if (binfo)
19582 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19583 int i;
19584 tree base;
19586 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19587 gen_inheritance_die (base,
19588 (accesses ? (*accesses)[i] : access_public_node),
19589 context_die);
19592 /* Now output info about the data members and type members. */
19593 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19595 /* If we thought we were generating minimal debug info for TYPE
19596 and then changed our minds, some of the member declarations
19597 may have already been defined. Don't define them again, but
19598 do put them in the right order. */
19600 child = lookup_decl_die (member);
19601 if (child)
19602 splice_child_die (context_die, child);
19603 else
19604 gen_decl_die (member, NULL, context_die);
19607 /* Now output info about the function members (if any). */
19608 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19610 /* Don't include clones in the member list. */
19611 if (DECL_ABSTRACT_ORIGIN (member))
19612 continue;
19614 child = lookup_decl_die (member);
19615 if (child)
19616 splice_child_die (context_die, child);
19617 else
19618 gen_decl_die (member, NULL, context_die);
19622 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19623 is set, we pretend that the type was never defined, so we only get the
19624 member DIEs needed by later specification DIEs. */
19626 static void
19627 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19628 enum debug_info_usage usage)
19630 dw_die_ref type_die = lookup_type_die (type);
19631 dw_die_ref scope_die = 0;
19632 int nested = 0;
19633 int complete = (TYPE_SIZE (type)
19634 && (! TYPE_STUB_DECL (type)
19635 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19636 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19637 complete = complete && should_emit_struct_debug (type, usage);
19639 if (type_die && ! complete)
19640 return;
19642 if (TYPE_CONTEXT (type) != NULL_TREE
19643 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19644 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19645 nested = 1;
19647 scope_die = scope_die_for (type, context_die);
19649 /* Generate child dies for template paramaters. */
19650 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19651 schedule_generic_params_dies_gen (type);
19653 if (! type_die || (nested && is_cu_die (scope_die)))
19654 /* First occurrence of type or toplevel definition of nested class. */
19656 dw_die_ref old_die = type_die;
19658 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19659 ? record_type_tag (type) : DW_TAG_union_type,
19660 scope_die, type);
19661 equate_type_number_to_die (type, type_die);
19662 if (old_die)
19663 add_AT_specification (type_die, old_die);
19664 else
19665 add_name_attribute (type_die, type_tag (type));
19667 else
19668 remove_AT (type_die, DW_AT_declaration);
19670 /* If this type has been completed, then give it a byte_size attribute and
19671 then give a list of members. */
19672 if (complete && !ns_decl)
19674 /* Prevent infinite recursion in cases where the type of some member of
19675 this type is expressed in terms of this type itself. */
19676 TREE_ASM_WRITTEN (type) = 1;
19677 add_byte_size_attribute (type_die, type);
19678 if (TYPE_STUB_DECL (type) != NULL_TREE)
19680 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19681 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19684 /* If the first reference to this type was as the return type of an
19685 inline function, then it may not have a parent. Fix this now. */
19686 if (type_die->die_parent == NULL)
19687 add_child_die (scope_die, type_die);
19689 push_decl_scope (type);
19690 gen_member_die (type, type_die);
19691 pop_decl_scope ();
19693 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19694 if (TYPE_ARTIFICIAL (type))
19695 add_AT_flag (type_die, DW_AT_artificial, 1);
19697 /* GNU extension: Record what type our vtable lives in. */
19698 if (TYPE_VFIELD (type))
19700 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19702 gen_type_die (vtype, context_die);
19703 add_AT_die_ref (type_die, DW_AT_containing_type,
19704 lookup_type_die (vtype));
19707 else
19709 add_AT_flag (type_die, DW_AT_declaration, 1);
19711 /* We don't need to do this for function-local types. */
19712 if (TYPE_STUB_DECL (type)
19713 && ! decl_function_context (TYPE_STUB_DECL (type)))
19714 vec_safe_push (incomplete_types, type);
19717 if (get_AT (type_die, DW_AT_name))
19718 add_pubtype (type, type_die);
19721 /* Generate a DIE for a subroutine _type_. */
19723 static void
19724 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19726 tree return_type = TREE_TYPE (type);
19727 dw_die_ref subr_die
19728 = new_die (DW_TAG_subroutine_type,
19729 scope_die_for (type, context_die), type);
19731 equate_type_number_to_die (type, subr_die);
19732 add_prototyped_attribute (subr_die, type);
19733 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19734 gen_formal_types_die (type, subr_die);
19736 if (get_AT (subr_die, DW_AT_name))
19737 add_pubtype (type, subr_die);
19740 /* Generate a DIE for a type definition. */
19742 static void
19743 gen_typedef_die (tree decl, dw_die_ref context_die)
19745 dw_die_ref type_die;
19746 tree origin;
19748 if (TREE_ASM_WRITTEN (decl))
19749 return;
19751 TREE_ASM_WRITTEN (decl) = 1;
19752 type_die = new_die (DW_TAG_typedef, context_die, decl);
19753 origin = decl_ultimate_origin (decl);
19754 if (origin != NULL)
19755 add_abstract_origin_attribute (type_die, origin);
19756 else
19758 tree type;
19760 add_name_and_src_coords_attributes (type_die, decl);
19761 if (DECL_ORIGINAL_TYPE (decl))
19763 type = DECL_ORIGINAL_TYPE (decl);
19765 gcc_assert (type != TREE_TYPE (decl));
19766 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19768 else
19770 type = TREE_TYPE (decl);
19772 if (is_naming_typedef_decl (TYPE_NAME (type)))
19774 /* Here, we are in the case of decl being a typedef naming
19775 an anonymous type, e.g:
19776 typedef struct {...} foo;
19777 In that case TREE_TYPE (decl) is not a typedef variant
19778 type and TYPE_NAME of the anonymous type is set to the
19779 TYPE_DECL of the typedef. This construct is emitted by
19780 the C++ FE.
19782 TYPE is the anonymous struct named by the typedef
19783 DECL. As we need the DW_AT_type attribute of the
19784 DW_TAG_typedef to point to the DIE of TYPE, let's
19785 generate that DIE right away. add_type_attribute
19786 called below will then pick (via lookup_type_die) that
19787 anonymous struct DIE. */
19788 if (!TREE_ASM_WRITTEN (type))
19789 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19791 /* This is a GNU Extension. We are adding a
19792 DW_AT_linkage_name attribute to the DIE of the
19793 anonymous struct TYPE. The value of that attribute
19794 is the name of the typedef decl naming the anonymous
19795 struct. This greatly eases the work of consumers of
19796 this debug info. */
19797 add_linkage_attr (lookup_type_die (type), decl);
19801 add_type_attribute (type_die, type, TREE_READONLY (decl),
19802 TREE_THIS_VOLATILE (decl), context_die);
19804 if (is_naming_typedef_decl (decl))
19805 /* We want that all subsequent calls to lookup_type_die with
19806 TYPE in argument yield the DW_TAG_typedef we have just
19807 created. */
19808 equate_type_number_to_die (type, type_die);
19810 add_accessibility_attribute (type_die, decl);
19813 if (DECL_ABSTRACT (decl))
19814 equate_decl_number_to_die (decl, type_die);
19816 if (get_AT (type_die, DW_AT_name))
19817 add_pubtype (decl, type_die);
19820 /* Generate a DIE for a struct, class, enum or union type. */
19822 static void
19823 gen_tagged_type_die (tree type,
19824 dw_die_ref context_die,
19825 enum debug_info_usage usage)
19827 int need_pop;
19829 if (type == NULL_TREE
19830 || !is_tagged_type (type))
19831 return;
19833 /* If this is a nested type whose containing class hasn't been written
19834 out yet, writing it out will cover this one, too. This does not apply
19835 to instantiations of member class templates; they need to be added to
19836 the containing class as they are generated. FIXME: This hurts the
19837 idea of combining type decls from multiple TUs, since we can't predict
19838 what set of template instantiations we'll get. */
19839 if (TYPE_CONTEXT (type)
19840 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19841 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19843 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19845 if (TREE_ASM_WRITTEN (type))
19846 return;
19848 /* If that failed, attach ourselves to the stub. */
19849 push_decl_scope (TYPE_CONTEXT (type));
19850 context_die = lookup_type_die (TYPE_CONTEXT (type));
19851 need_pop = 1;
19853 else if (TYPE_CONTEXT (type) != NULL_TREE
19854 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19856 /* If this type is local to a function that hasn't been written
19857 out yet, use a NULL context for now; it will be fixed up in
19858 decls_for_scope. */
19859 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19860 /* A declaration DIE doesn't count; nested types need to go in the
19861 specification. */
19862 if (context_die && is_declaration_die (context_die))
19863 context_die = NULL;
19864 need_pop = 0;
19866 else
19868 context_die = declare_in_namespace (type, context_die);
19869 need_pop = 0;
19872 if (TREE_CODE (type) == ENUMERAL_TYPE)
19874 /* This might have been written out by the call to
19875 declare_in_namespace. */
19876 if (!TREE_ASM_WRITTEN (type))
19877 gen_enumeration_type_die (type, context_die);
19879 else
19880 gen_struct_or_union_type_die (type, context_die, usage);
19882 if (need_pop)
19883 pop_decl_scope ();
19885 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19886 it up if it is ever completed. gen_*_type_die will set it for us
19887 when appropriate. */
19890 /* Generate a type description DIE. */
19892 static void
19893 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19894 enum debug_info_usage usage)
19896 struct array_descr_info info;
19898 if (type == NULL_TREE || type == error_mark_node)
19899 return;
19901 if (TYPE_NAME (type) != NULL_TREE
19902 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19903 && is_redundant_typedef (TYPE_NAME (type))
19904 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19905 /* The DECL of this type is a typedef we don't want to emit debug
19906 info for but we want debug info for its underlying typedef.
19907 This can happen for e.g, the injected-class-name of a C++
19908 type. */
19909 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19911 /* If TYPE is a typedef type variant, let's generate debug info
19912 for the parent typedef which TYPE is a type of. */
19913 if (typedef_variant_p (type))
19915 if (TREE_ASM_WRITTEN (type))
19916 return;
19918 /* Prevent broken recursion; we can't hand off to the same type. */
19919 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19921 /* Give typedefs the right scope. */
19922 context_die = scope_die_for (type, context_die);
19924 TREE_ASM_WRITTEN (type) = 1;
19926 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19927 return;
19930 /* If type is an anonymous tagged type named by a typedef, let's
19931 generate debug info for the typedef. */
19932 if (is_naming_typedef_decl (TYPE_NAME (type)))
19934 /* Use the DIE of the containing namespace as the parent DIE of
19935 the type description DIE we want to generate. */
19936 if (DECL_CONTEXT (TYPE_NAME (type))
19937 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19938 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19940 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19941 return;
19944 /* If this is an array type with hidden descriptor, handle it first. */
19945 if (!TREE_ASM_WRITTEN (type)
19946 && lang_hooks.types.get_array_descr_info
19947 && lang_hooks.types.get_array_descr_info (type, &info)
19948 && (dwarf_version >= 3 || !dwarf_strict))
19950 gen_descr_array_type_die (type, &info, context_die);
19951 TREE_ASM_WRITTEN (type) = 1;
19952 return;
19955 /* We are going to output a DIE to represent the unqualified version
19956 of this type (i.e. without any const or volatile qualifiers) so
19957 get the main variant (i.e. the unqualified version) of this type
19958 now. (Vectors are special because the debugging info is in the
19959 cloned type itself). */
19960 if (TREE_CODE (type) != VECTOR_TYPE)
19961 type = type_main_variant (type);
19963 if (TREE_ASM_WRITTEN (type))
19964 return;
19966 switch (TREE_CODE (type))
19968 case ERROR_MARK:
19969 break;
19971 case POINTER_TYPE:
19972 case REFERENCE_TYPE:
19973 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19974 ensures that the gen_type_die recursion will terminate even if the
19975 type is recursive. Recursive types are possible in Ada. */
19976 /* ??? We could perhaps do this for all types before the switch
19977 statement. */
19978 TREE_ASM_WRITTEN (type) = 1;
19980 /* For these types, all that is required is that we output a DIE (or a
19981 set of DIEs) to represent the "basis" type. */
19982 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19983 DINFO_USAGE_IND_USE);
19984 break;
19986 case OFFSET_TYPE:
19987 /* This code is used for C++ pointer-to-data-member types.
19988 Output a description of the relevant class type. */
19989 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19990 DINFO_USAGE_IND_USE);
19992 /* Output a description of the type of the object pointed to. */
19993 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19994 DINFO_USAGE_IND_USE);
19996 /* Now output a DIE to represent this pointer-to-data-member type
19997 itself. */
19998 gen_ptr_to_mbr_type_die (type, context_die);
19999 break;
20001 case FUNCTION_TYPE:
20002 /* Force out return type (in case it wasn't forced out already). */
20003 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20004 DINFO_USAGE_DIR_USE);
20005 gen_subroutine_type_die (type, context_die);
20006 break;
20008 case METHOD_TYPE:
20009 /* Force out return type (in case it wasn't forced out already). */
20010 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20011 DINFO_USAGE_DIR_USE);
20012 gen_subroutine_type_die (type, context_die);
20013 break;
20015 case ARRAY_TYPE:
20016 gen_array_type_die (type, context_die);
20017 break;
20019 case VECTOR_TYPE:
20020 gen_array_type_die (type, context_die);
20021 break;
20023 case ENUMERAL_TYPE:
20024 case RECORD_TYPE:
20025 case UNION_TYPE:
20026 case QUAL_UNION_TYPE:
20027 gen_tagged_type_die (type, context_die, usage);
20028 return;
20030 case VOID_TYPE:
20031 case INTEGER_TYPE:
20032 case REAL_TYPE:
20033 case FIXED_POINT_TYPE:
20034 case COMPLEX_TYPE:
20035 case BOOLEAN_TYPE:
20036 /* No DIEs needed for fundamental types. */
20037 break;
20039 case NULLPTR_TYPE:
20040 case LANG_TYPE:
20041 /* Just use DW_TAG_unspecified_type. */
20043 dw_die_ref type_die = lookup_type_die (type);
20044 if (type_die == NULL)
20046 tree name = TYPE_IDENTIFIER (type);
20047 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20048 type);
20049 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20050 equate_type_number_to_die (type, type_die);
20053 break;
20055 default:
20056 if (is_cxx_auto (type))
20058 tree name = TYPE_IDENTIFIER (type);
20059 dw_die_ref *die = (name == get_identifier ("auto")
20060 ? &auto_die : &decltype_auto_die);
20061 if (!*die)
20063 *die = new_die (DW_TAG_unspecified_type,
20064 comp_unit_die (), NULL_TREE);
20065 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20067 equate_type_number_to_die (type, *die);
20068 break;
20070 gcc_unreachable ();
20073 TREE_ASM_WRITTEN (type) = 1;
20076 static void
20077 gen_type_die (tree type, dw_die_ref context_die)
20079 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20082 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20083 things which are local to the given block. */
20085 static void
20086 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20088 int must_output_die = 0;
20089 bool inlined_func;
20091 /* Ignore blocks that are NULL. */
20092 if (stmt == NULL_TREE)
20093 return;
20095 inlined_func = inlined_function_outer_scope_p (stmt);
20097 /* If the block is one fragment of a non-contiguous block, do not
20098 process the variables, since they will have been done by the
20099 origin block. Do process subblocks. */
20100 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20102 tree sub;
20104 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20105 gen_block_die (sub, context_die, depth + 1);
20107 return;
20110 /* Determine if we need to output any Dwarf DIEs at all to represent this
20111 block. */
20112 if (inlined_func)
20113 /* The outer scopes for inlinings *must* always be represented. We
20114 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20115 must_output_die = 1;
20116 else
20118 /* Determine if this block directly contains any "significant"
20119 local declarations which we will need to output DIEs for. */
20120 if (debug_info_level > DINFO_LEVEL_TERSE)
20121 /* We are not in terse mode so *any* local declaration counts
20122 as being a "significant" one. */
20123 must_output_die = ((BLOCK_VARS (stmt) != NULL
20124 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20125 && (TREE_USED (stmt)
20126 || TREE_ASM_WRITTEN (stmt)
20127 || BLOCK_ABSTRACT (stmt)));
20128 else if ((TREE_USED (stmt)
20129 || TREE_ASM_WRITTEN (stmt)
20130 || BLOCK_ABSTRACT (stmt))
20131 && !dwarf2out_ignore_block (stmt))
20132 must_output_die = 1;
20135 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20136 DIE for any block which contains no significant local declarations at
20137 all. Rather, in such cases we just call `decls_for_scope' so that any
20138 needed Dwarf info for any sub-blocks will get properly generated. Note
20139 that in terse mode, our definition of what constitutes a "significant"
20140 local declaration gets restricted to include only inlined function
20141 instances and local (nested) function definitions. */
20142 if (must_output_die)
20144 if (inlined_func)
20146 /* If STMT block is abstract, that means we have been called
20147 indirectly from dwarf2out_abstract_function.
20148 That function rightfully marks the descendent blocks (of
20149 the abstract function it is dealing with) as being abstract,
20150 precisely to prevent us from emitting any
20151 DW_TAG_inlined_subroutine DIE as a descendent
20152 of an abstract function instance. So in that case, we should
20153 not call gen_inlined_subroutine_die.
20155 Later though, when cgraph asks dwarf2out to emit info
20156 for the concrete instance of the function decl into which
20157 the concrete instance of STMT got inlined, the later will lead
20158 to the generation of a DW_TAG_inlined_subroutine DIE. */
20159 if (! BLOCK_ABSTRACT (stmt))
20160 gen_inlined_subroutine_die (stmt, context_die, depth);
20162 else
20163 gen_lexical_block_die (stmt, context_die, depth);
20165 else
20166 decls_for_scope (stmt, context_die, depth);
20169 /* Process variable DECL (or variable with origin ORIGIN) within
20170 block STMT and add it to CONTEXT_DIE. */
20171 static void
20172 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20174 dw_die_ref die;
20175 tree decl_or_origin = decl ? decl : origin;
20177 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20178 die = lookup_decl_die (decl_or_origin);
20179 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20180 && TYPE_DECL_IS_STUB (decl_or_origin))
20181 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20182 else
20183 die = NULL;
20185 if (die != NULL && die->die_parent == NULL)
20186 add_child_die (context_die, die);
20187 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20188 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20189 stmt, context_die);
20190 else
20191 gen_decl_die (decl, origin, context_die);
20194 /* Generate all of the decls declared within a given scope and (recursively)
20195 all of its sub-blocks. */
20197 static void
20198 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20200 tree decl;
20201 unsigned int i;
20202 tree subblocks;
20204 /* Ignore NULL blocks. */
20205 if (stmt == NULL_TREE)
20206 return;
20208 /* Output the DIEs to represent all of the data objects and typedefs
20209 declared directly within this block but not within any nested
20210 sub-blocks. Also, nested function and tag DIEs have been
20211 generated with a parent of NULL; fix that up now. We don't
20212 have to do this if we're at -g1. */
20213 if (debug_info_level > DINFO_LEVEL_TERSE)
20215 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20216 process_scope_var (stmt, decl, NULL_TREE, context_die);
20217 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20218 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20219 context_die);
20222 /* Even if we're at -g1, we need to process the subblocks in order to get
20223 inlined call information. */
20225 /* Output the DIEs to represent all sub-blocks (and the items declared
20226 therein) of this block. */
20227 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20228 subblocks != NULL;
20229 subblocks = BLOCK_CHAIN (subblocks))
20230 gen_block_die (subblocks, context_die, depth + 1);
20233 /* Is this a typedef we can avoid emitting? */
20235 static inline int
20236 is_redundant_typedef (const_tree decl)
20238 if (TYPE_DECL_IS_STUB (decl))
20239 return 1;
20241 if (DECL_ARTIFICIAL (decl)
20242 && DECL_CONTEXT (decl)
20243 && is_tagged_type (DECL_CONTEXT (decl))
20244 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20245 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20246 /* Also ignore the artificial member typedef for the class name. */
20247 return 1;
20249 return 0;
20252 /* Return TRUE if TYPE is a typedef that names a type for linkage
20253 purposes. This kind of typedefs is produced by the C++ FE for
20254 constructs like:
20256 typedef struct {...} foo;
20258 In that case, there is no typedef variant type produced for foo.
20259 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20260 struct type. */
20262 static bool
20263 is_naming_typedef_decl (const_tree decl)
20265 if (decl == NULL_TREE
20266 || TREE_CODE (decl) != TYPE_DECL
20267 || !is_tagged_type (TREE_TYPE (decl))
20268 || DECL_IS_BUILTIN (decl)
20269 || is_redundant_typedef (decl)
20270 /* It looks like Ada produces TYPE_DECLs that are very similar
20271 to C++ naming typedefs but that have different
20272 semantics. Let's be specific to c++ for now. */
20273 || !is_cxx ())
20274 return FALSE;
20276 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20277 && TYPE_NAME (TREE_TYPE (decl)) == decl
20278 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20279 != TYPE_NAME (TREE_TYPE (decl))));
20282 /* Returns the DIE for a context. */
20284 static inline dw_die_ref
20285 get_context_die (tree context)
20287 if (context)
20289 /* Find die that represents this context. */
20290 if (TYPE_P (context))
20292 context = TYPE_MAIN_VARIANT (context);
20293 return strip_naming_typedef (context, force_type_die (context));
20295 else
20296 return force_decl_die (context);
20298 return comp_unit_die ();
20301 /* Returns the DIE for decl. A DIE will always be returned. */
20303 static dw_die_ref
20304 force_decl_die (tree decl)
20306 dw_die_ref decl_die;
20307 unsigned saved_external_flag;
20308 tree save_fn = NULL_TREE;
20309 decl_die = lookup_decl_die (decl);
20310 if (!decl_die)
20312 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20314 decl_die = lookup_decl_die (decl);
20315 if (decl_die)
20316 return decl_die;
20318 switch (TREE_CODE (decl))
20320 case FUNCTION_DECL:
20321 /* Clear current_function_decl, so that gen_subprogram_die thinks
20322 that this is a declaration. At this point, we just want to force
20323 declaration die. */
20324 save_fn = current_function_decl;
20325 current_function_decl = NULL_TREE;
20326 gen_subprogram_die (decl, context_die);
20327 current_function_decl = save_fn;
20328 break;
20330 case VAR_DECL:
20331 /* Set external flag to force declaration die. Restore it after
20332 gen_decl_die() call. */
20333 saved_external_flag = DECL_EXTERNAL (decl);
20334 DECL_EXTERNAL (decl) = 1;
20335 gen_decl_die (decl, NULL, context_die);
20336 DECL_EXTERNAL (decl) = saved_external_flag;
20337 break;
20339 case NAMESPACE_DECL:
20340 if (dwarf_version >= 3 || !dwarf_strict)
20341 dwarf2out_decl (decl);
20342 else
20343 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20344 decl_die = comp_unit_die ();
20345 break;
20347 case TRANSLATION_UNIT_DECL:
20348 decl_die = comp_unit_die ();
20349 break;
20351 default:
20352 gcc_unreachable ();
20355 /* We should be able to find the DIE now. */
20356 if (!decl_die)
20357 decl_die = lookup_decl_die (decl);
20358 gcc_assert (decl_die);
20361 return decl_die;
20364 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20365 always returned. */
20367 static dw_die_ref
20368 force_type_die (tree type)
20370 dw_die_ref type_die;
20372 type_die = lookup_type_die (type);
20373 if (!type_die)
20375 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20377 type_die = modified_type_die (type, TYPE_QUALS (type), context_die);
20378 gcc_assert (type_die);
20380 return type_die;
20383 /* Force out any required namespaces to be able to output DECL,
20384 and return the new context_die for it, if it's changed. */
20386 static dw_die_ref
20387 setup_namespace_context (tree thing, dw_die_ref context_die)
20389 tree context = (DECL_P (thing)
20390 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20391 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20392 /* Force out the namespace. */
20393 context_die = force_decl_die (context);
20395 return context_die;
20398 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20399 type) within its namespace, if appropriate.
20401 For compatibility with older debuggers, namespace DIEs only contain
20402 declarations; all definitions are emitted at CU scope. */
20404 static dw_die_ref
20405 declare_in_namespace (tree thing, dw_die_ref context_die)
20407 dw_die_ref ns_context;
20409 if (debug_info_level <= DINFO_LEVEL_TERSE)
20410 return context_die;
20412 /* If this decl is from an inlined function, then don't try to emit it in its
20413 namespace, as we will get confused. It would have already been emitted
20414 when the abstract instance of the inline function was emitted anyways. */
20415 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20416 return context_die;
20418 ns_context = setup_namespace_context (thing, context_die);
20420 if (ns_context != context_die)
20422 if (is_fortran ())
20423 return ns_context;
20424 if (DECL_P (thing))
20425 gen_decl_die (thing, NULL, ns_context);
20426 else
20427 gen_type_die (thing, ns_context);
20429 return context_die;
20432 /* Generate a DIE for a namespace or namespace alias. */
20434 static void
20435 gen_namespace_die (tree decl, dw_die_ref context_die)
20437 dw_die_ref namespace_die;
20439 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20440 they are an alias of. */
20441 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20443 /* Output a real namespace or module. */
20444 context_die = setup_namespace_context (decl, comp_unit_die ());
20445 namespace_die = new_die (is_fortran ()
20446 ? DW_TAG_module : DW_TAG_namespace,
20447 context_die, decl);
20448 /* For Fortran modules defined in different CU don't add src coords. */
20449 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20451 const char *name = dwarf2_name (decl, 0);
20452 if (name)
20453 add_name_attribute (namespace_die, name);
20455 else
20456 add_name_and_src_coords_attributes (namespace_die, decl);
20457 if (DECL_EXTERNAL (decl))
20458 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20459 equate_decl_number_to_die (decl, namespace_die);
20461 else
20463 /* Output a namespace alias. */
20465 /* Force out the namespace we are an alias of, if necessary. */
20466 dw_die_ref origin_die
20467 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20469 if (DECL_FILE_SCOPE_P (decl)
20470 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20471 context_die = setup_namespace_context (decl, comp_unit_die ());
20472 /* Now create the namespace alias DIE. */
20473 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20474 add_name_and_src_coords_attributes (namespace_die, decl);
20475 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20476 equate_decl_number_to_die (decl, namespace_die);
20478 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20479 if (want_pubnames ())
20480 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20483 /* Generate Dwarf debug information for a decl described by DECL.
20484 The return value is currently only meaningful for PARM_DECLs,
20485 for all other decls it returns NULL. */
20487 static dw_die_ref
20488 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20490 tree decl_or_origin = decl ? decl : origin;
20491 tree class_origin = NULL, ultimate_origin;
20493 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20494 return NULL;
20496 switch (TREE_CODE (decl_or_origin))
20498 case ERROR_MARK:
20499 break;
20501 case CONST_DECL:
20502 if (!is_fortran () && !is_ada ())
20504 /* The individual enumerators of an enum type get output when we output
20505 the Dwarf representation of the relevant enum type itself. */
20506 break;
20509 /* Emit its type. */
20510 gen_type_die (TREE_TYPE (decl), context_die);
20512 /* And its containing namespace. */
20513 context_die = declare_in_namespace (decl, context_die);
20515 gen_const_die (decl, context_die);
20516 break;
20518 case FUNCTION_DECL:
20519 /* Don't output any DIEs to represent mere function declarations,
20520 unless they are class members or explicit block externs. */
20521 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20522 && DECL_FILE_SCOPE_P (decl_or_origin)
20523 && (current_function_decl == NULL_TREE
20524 || DECL_ARTIFICIAL (decl_or_origin)))
20525 break;
20527 #if 0
20528 /* FIXME */
20529 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20530 on local redeclarations of global functions. That seems broken. */
20531 if (current_function_decl != decl)
20532 /* This is only a declaration. */;
20533 #endif
20535 /* If we're emitting a clone, emit info for the abstract instance. */
20536 if (origin || DECL_ORIGIN (decl) != decl)
20537 dwarf2out_abstract_function (origin
20538 ? DECL_ORIGIN (origin)
20539 : DECL_ABSTRACT_ORIGIN (decl));
20541 /* If we're emitting an out-of-line copy of an inline function,
20542 emit info for the abstract instance and set up to refer to it. */
20543 else if (cgraph_function_possibly_inlined_p (decl)
20544 && ! DECL_ABSTRACT (decl)
20545 && ! class_or_namespace_scope_p (context_die)
20546 /* dwarf2out_abstract_function won't emit a die if this is just
20547 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20548 that case, because that works only if we have a die. */
20549 && DECL_INITIAL (decl) != NULL_TREE)
20551 dwarf2out_abstract_function (decl);
20552 set_decl_origin_self (decl);
20555 /* Otherwise we're emitting the primary DIE for this decl. */
20556 else if (debug_info_level > DINFO_LEVEL_TERSE)
20558 /* Before we describe the FUNCTION_DECL itself, make sure that we
20559 have its containing type. */
20560 if (!origin)
20561 origin = decl_class_context (decl);
20562 if (origin != NULL_TREE)
20563 gen_type_die (origin, context_die);
20565 /* And its return type. */
20566 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20568 /* And its virtual context. */
20569 if (DECL_VINDEX (decl) != NULL_TREE)
20570 gen_type_die (DECL_CONTEXT (decl), context_die);
20572 /* Make sure we have a member DIE for decl. */
20573 if (origin != NULL_TREE)
20574 gen_type_die_for_member (origin, decl, context_die);
20576 /* And its containing namespace. */
20577 context_die = declare_in_namespace (decl, context_die);
20580 /* Now output a DIE to represent the function itself. */
20581 if (decl)
20582 gen_subprogram_die (decl, context_die);
20583 break;
20585 case TYPE_DECL:
20586 /* If we are in terse mode, don't generate any DIEs to represent any
20587 actual typedefs. */
20588 if (debug_info_level <= DINFO_LEVEL_TERSE)
20589 break;
20591 /* In the special case of a TYPE_DECL node representing the declaration
20592 of some type tag, if the given TYPE_DECL is marked as having been
20593 instantiated from some other (original) TYPE_DECL node (e.g. one which
20594 was generated within the original definition of an inline function) we
20595 used to generate a special (abbreviated) DW_TAG_structure_type,
20596 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20597 should be actually referencing those DIEs, as variable DIEs with that
20598 type would be emitted already in the abstract origin, so it was always
20599 removed during unused type prunning. Don't add anything in this
20600 case. */
20601 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20602 break;
20604 if (is_redundant_typedef (decl))
20605 gen_type_die (TREE_TYPE (decl), context_die);
20606 else
20607 /* Output a DIE to represent the typedef itself. */
20608 gen_typedef_die (decl, context_die);
20609 break;
20611 case LABEL_DECL:
20612 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20613 gen_label_die (decl, context_die);
20614 break;
20616 case VAR_DECL:
20617 case RESULT_DECL:
20618 /* If we are in terse mode, don't generate any DIEs to represent any
20619 variable declarations or definitions. */
20620 if (debug_info_level <= DINFO_LEVEL_TERSE)
20621 break;
20623 /* Output any DIEs that are needed to specify the type of this data
20624 object. */
20625 if (decl_by_reference_p (decl_or_origin))
20626 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20627 else
20628 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20630 /* And its containing type. */
20631 class_origin = decl_class_context (decl_or_origin);
20632 if (class_origin != NULL_TREE)
20633 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20635 /* And its containing namespace. */
20636 context_die = declare_in_namespace (decl_or_origin, context_die);
20638 /* Now output the DIE to represent the data object itself. This gets
20639 complicated because of the possibility that the VAR_DECL really
20640 represents an inlined instance of a formal parameter for an inline
20641 function. */
20642 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20643 if (ultimate_origin != NULL_TREE
20644 && TREE_CODE (ultimate_origin) == PARM_DECL)
20645 gen_formal_parameter_die (decl, origin,
20646 true /* Emit name attribute. */,
20647 context_die);
20648 else
20649 gen_variable_die (decl, origin, context_die);
20650 break;
20652 case FIELD_DECL:
20653 /* Ignore the nameless fields that are used to skip bits but handle C++
20654 anonymous unions and structs. */
20655 if (DECL_NAME (decl) != NULL_TREE
20656 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20657 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20659 gen_type_die (member_declared_type (decl), context_die);
20660 gen_field_die (decl, context_die);
20662 break;
20664 case PARM_DECL:
20665 if (DECL_BY_REFERENCE (decl_or_origin))
20666 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20667 else
20668 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20669 return gen_formal_parameter_die (decl, origin,
20670 true /* Emit name attribute. */,
20671 context_die);
20673 case NAMESPACE_DECL:
20674 case IMPORTED_DECL:
20675 if (dwarf_version >= 3 || !dwarf_strict)
20676 gen_namespace_die (decl, context_die);
20677 break;
20679 case NAMELIST_DECL:
20680 gen_namelist_decl (DECL_NAME (decl), context_die,
20681 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20682 break;
20684 default:
20685 /* Probably some frontend-internal decl. Assume we don't care. */
20686 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20687 break;
20690 return NULL;
20693 /* Output debug information for global decl DECL. Called from toplev.c after
20694 compilation proper has finished. */
20696 static void
20697 dwarf2out_global_decl (tree decl)
20699 /* Output DWARF2 information for file-scope tentative data object
20700 declarations, file-scope (extern) function declarations (which
20701 had no corresponding body) and file-scope tagged type declarations
20702 and definitions which have not yet been forced out. */
20703 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20704 dwarf2out_decl (decl);
20707 /* Output debug information for type decl DECL. Called from toplev.c
20708 and from language front ends (to record built-in types). */
20709 static void
20710 dwarf2out_type_decl (tree decl, int local)
20712 if (!local)
20713 dwarf2out_decl (decl);
20716 /* Output debug information for imported module or decl DECL.
20717 NAME is non-NULL name in the lexical block if the decl has been renamed.
20718 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20719 that DECL belongs to.
20720 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20721 static void
20722 dwarf2out_imported_module_or_decl_1 (tree decl,
20723 tree name,
20724 tree lexical_block,
20725 dw_die_ref lexical_block_die)
20727 expanded_location xloc;
20728 dw_die_ref imported_die = NULL;
20729 dw_die_ref at_import_die;
20731 if (TREE_CODE (decl) == IMPORTED_DECL)
20733 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20734 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20735 gcc_assert (decl);
20737 else
20738 xloc = expand_location (input_location);
20740 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20742 at_import_die = force_type_die (TREE_TYPE (decl));
20743 /* For namespace N { typedef void T; } using N::T; base_type_die
20744 returns NULL, but DW_TAG_imported_declaration requires
20745 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20746 if (!at_import_die)
20748 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20749 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20750 at_import_die = lookup_type_die (TREE_TYPE (decl));
20751 gcc_assert (at_import_die);
20754 else
20756 at_import_die = lookup_decl_die (decl);
20757 if (!at_import_die)
20759 /* If we're trying to avoid duplicate debug info, we may not have
20760 emitted the member decl for this field. Emit it now. */
20761 if (TREE_CODE (decl) == FIELD_DECL)
20763 tree type = DECL_CONTEXT (decl);
20765 if (TYPE_CONTEXT (type)
20766 && TYPE_P (TYPE_CONTEXT (type))
20767 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20768 DINFO_USAGE_DIR_USE))
20769 return;
20770 gen_type_die_for_member (type, decl,
20771 get_context_die (TYPE_CONTEXT (type)));
20773 if (TREE_CODE (decl) == NAMELIST_DECL)
20774 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20775 get_context_die (DECL_CONTEXT (decl)),
20776 NULL_TREE);
20777 else
20778 at_import_die = force_decl_die (decl);
20782 if (TREE_CODE (decl) == NAMESPACE_DECL)
20784 if (dwarf_version >= 3 || !dwarf_strict)
20785 imported_die = new_die (DW_TAG_imported_module,
20786 lexical_block_die,
20787 lexical_block);
20788 else
20789 return;
20791 else
20792 imported_die = new_die (DW_TAG_imported_declaration,
20793 lexical_block_die,
20794 lexical_block);
20796 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20797 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20798 if (name)
20799 add_AT_string (imported_die, DW_AT_name,
20800 IDENTIFIER_POINTER (name));
20801 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20804 /* Output debug information for imported module or decl DECL.
20805 NAME is non-NULL name in context if the decl has been renamed.
20806 CHILD is true if decl is one of the renamed decls as part of
20807 importing whole module. */
20809 static void
20810 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20811 bool child)
20813 /* dw_die_ref at_import_die; */
20814 dw_die_ref scope_die;
20816 if (debug_info_level <= DINFO_LEVEL_TERSE)
20817 return;
20819 gcc_assert (decl);
20821 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20822 We need decl DIE for reference and scope die. First, get DIE for the decl
20823 itself. */
20825 /* Get the scope die for decl context. Use comp_unit_die for global module
20826 or decl. If die is not found for non globals, force new die. */
20827 if (context
20828 && TYPE_P (context)
20829 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20830 return;
20832 if (!(dwarf_version >= 3 || !dwarf_strict))
20833 return;
20835 scope_die = get_context_die (context);
20837 if (child)
20839 gcc_assert (scope_die->die_child);
20840 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20841 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20842 scope_die = scope_die->die_child;
20845 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20846 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20850 /* Output debug information for namelists. */
20852 static dw_die_ref
20853 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20855 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20856 tree value;
20857 unsigned i;
20859 if (debug_info_level <= DINFO_LEVEL_TERSE)
20860 return NULL;
20862 gcc_assert (scope_die != NULL);
20863 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20864 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20866 /* If there are no item_decls, we have a nondefining namelist, e.g.
20867 with USE association; hence, set DW_AT_declaration. */
20868 if (item_decls == NULL_TREE)
20870 add_AT_flag (nml_die, DW_AT_declaration, 1);
20871 return nml_die;
20874 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20876 nml_item_ref_die = lookup_decl_die (value);
20877 if (!nml_item_ref_die)
20878 nml_item_ref_die = force_decl_die (value);
20880 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20881 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20883 return nml_die;
20887 /* Write the debugging output for DECL. */
20889 void
20890 dwarf2out_decl (tree decl)
20892 dw_die_ref context_die = comp_unit_die ();
20894 switch (TREE_CODE (decl))
20896 case ERROR_MARK:
20897 return;
20899 case FUNCTION_DECL:
20900 /* What we would really like to do here is to filter out all mere
20901 file-scope declarations of file-scope functions which are never
20902 referenced later within this translation unit (and keep all of ones
20903 that *are* referenced later on) but we aren't clairvoyant, so we have
20904 no idea which functions will be referenced in the future (i.e. later
20905 on within the current translation unit). So here we just ignore all
20906 file-scope function declarations which are not also definitions. If
20907 and when the debugger needs to know something about these functions,
20908 it will have to hunt around and find the DWARF information associated
20909 with the definition of the function.
20911 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20912 nodes represent definitions and which ones represent mere
20913 declarations. We have to check DECL_INITIAL instead. That's because
20914 the C front-end supports some weird semantics for "extern inline"
20915 function definitions. These can get inlined within the current
20916 translation unit (and thus, we need to generate Dwarf info for their
20917 abstract instances so that the Dwarf info for the concrete inlined
20918 instances can have something to refer to) but the compiler never
20919 generates any out-of-lines instances of such things (despite the fact
20920 that they *are* definitions).
20922 The important point is that the C front-end marks these "extern
20923 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20924 them anyway. Note that the C++ front-end also plays some similar games
20925 for inline function definitions appearing within include files which
20926 also contain `#pragma interface' pragmas.
20928 If we are called from dwarf2out_abstract_function output a DIE
20929 anyway. We can end up here this way with early inlining and LTO
20930 where the inlined function is output in a different LTRANS unit
20931 or not at all. */
20932 if (DECL_INITIAL (decl) == NULL_TREE
20933 && ! DECL_ABSTRACT (decl))
20934 return;
20936 /* If we're a nested function, initially use a parent of NULL; if we're
20937 a plain function, this will be fixed up in decls_for_scope. If
20938 we're a method, it will be ignored, since we already have a DIE. */
20939 if (decl_function_context (decl)
20940 /* But if we're in terse mode, we don't care about scope. */
20941 && debug_info_level > DINFO_LEVEL_TERSE)
20942 context_die = NULL;
20943 break;
20945 case VAR_DECL:
20946 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20947 declaration and if the declaration was never even referenced from
20948 within this entire compilation unit. We suppress these DIEs in
20949 order to save space in the .debug section (by eliminating entries
20950 which are probably useless). Note that we must not suppress
20951 block-local extern declarations (whether used or not) because that
20952 would screw-up the debugger's name lookup mechanism and cause it to
20953 miss things which really ought to be in scope at a given point. */
20954 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20955 return;
20957 /* For local statics lookup proper context die. */
20958 if (TREE_STATIC (decl)
20959 && DECL_CONTEXT (decl)
20960 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20961 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20963 /* If we are in terse mode, don't generate any DIEs to represent any
20964 variable declarations or definitions. */
20965 if (debug_info_level <= DINFO_LEVEL_TERSE)
20966 return;
20967 break;
20969 case CONST_DECL:
20970 if (debug_info_level <= DINFO_LEVEL_TERSE)
20971 return;
20972 if (!is_fortran () && !is_ada ())
20973 return;
20974 if (TREE_STATIC (decl) && decl_function_context (decl))
20975 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20976 break;
20978 case NAMESPACE_DECL:
20979 case IMPORTED_DECL:
20980 if (debug_info_level <= DINFO_LEVEL_TERSE)
20981 return;
20982 if (lookup_decl_die (decl) != NULL)
20983 return;
20984 break;
20986 case TYPE_DECL:
20987 /* Don't emit stubs for types unless they are needed by other DIEs. */
20988 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20989 return;
20991 /* Don't bother trying to generate any DIEs to represent any of the
20992 normal built-in types for the language we are compiling. */
20993 if (DECL_IS_BUILTIN (decl))
20994 return;
20996 /* If we are in terse mode, don't generate any DIEs for types. */
20997 if (debug_info_level <= DINFO_LEVEL_TERSE)
20998 return;
21000 /* If we're a function-scope tag, initially use a parent of NULL;
21001 this will be fixed up in decls_for_scope. */
21002 if (decl_function_context (decl))
21003 context_die = NULL;
21005 break;
21007 case NAMELIST_DECL:
21008 break;
21010 default:
21011 return;
21014 gen_decl_die (decl, NULL, context_die);
21017 /* Write the debugging output for DECL. */
21019 static void
21020 dwarf2out_function_decl (tree decl)
21022 dwarf2out_decl (decl);
21023 call_arg_locations = NULL;
21024 call_arg_loc_last = NULL;
21025 call_site_count = -1;
21026 tail_call_site_count = -1;
21027 block_map.release ();
21028 htab_empty (decl_loc_table);
21029 htab_empty (cached_dw_loc_list_table);
21032 /* Output a marker (i.e. a label) for the beginning of the generated code for
21033 a lexical block. */
21035 static void
21036 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21037 unsigned int blocknum)
21039 switch_to_section (current_function_section ());
21040 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21043 /* Output a marker (i.e. a label) for the end of the generated code for a
21044 lexical block. */
21046 static void
21047 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21049 switch_to_section (current_function_section ());
21050 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21053 /* Returns nonzero if it is appropriate not to emit any debugging
21054 information for BLOCK, because it doesn't contain any instructions.
21056 Don't allow this for blocks with nested functions or local classes
21057 as we would end up with orphans, and in the presence of scheduling
21058 we may end up calling them anyway. */
21060 static bool
21061 dwarf2out_ignore_block (const_tree block)
21063 tree decl;
21064 unsigned int i;
21066 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21067 if (TREE_CODE (decl) == FUNCTION_DECL
21068 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21069 return 0;
21070 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21072 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21073 if (TREE_CODE (decl) == FUNCTION_DECL
21074 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21075 return 0;
21078 return 1;
21081 /* Hash table routines for file_hash. */
21083 static int
21084 file_table_eq (const void *p1_p, const void *p2_p)
21086 const struct dwarf_file_data *const p1 =
21087 (const struct dwarf_file_data *) p1_p;
21088 const char *const p2 = (const char *) p2_p;
21089 return filename_cmp (p1->filename, p2) == 0;
21092 static hashval_t
21093 file_table_hash (const void *p_p)
21095 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21096 return htab_hash_string (p->filename);
21099 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21100 dwarf2out.c) and return its "index". The index of each (known) filename is
21101 just a unique number which is associated with only that one filename. We
21102 need such numbers for the sake of generating labels (in the .debug_sfnames
21103 section) and references to those files numbers (in the .debug_srcinfo
21104 and.debug_macinfo sections). If the filename given as an argument is not
21105 found in our current list, add it to the list and assign it the next
21106 available unique index number. In order to speed up searches, we remember
21107 the index of the filename was looked up last. This handles the majority of
21108 all searches. */
21110 static struct dwarf_file_data *
21111 lookup_filename (const char *file_name)
21113 void ** slot;
21114 struct dwarf_file_data * created;
21116 /* Check to see if the file name that was searched on the previous
21117 call matches this file name. If so, return the index. */
21118 if (file_table_last_lookup
21119 && (file_name == file_table_last_lookup->filename
21120 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21121 return file_table_last_lookup;
21123 /* Didn't match the previous lookup, search the table. */
21124 slot = htab_find_slot_with_hash (file_table, file_name,
21125 htab_hash_string (file_name), INSERT);
21126 if (*slot)
21127 return (struct dwarf_file_data *) *slot;
21129 created = ggc_alloc<dwarf_file_data> ();
21130 created->filename = file_name;
21131 created->emitted_number = 0;
21132 *slot = created;
21133 return created;
21136 /* If the assembler will construct the file table, then translate the compiler
21137 internal file table number into the assembler file table number, and emit
21138 a .file directive if we haven't already emitted one yet. The file table
21139 numbers are different because we prune debug info for unused variables and
21140 types, which may include filenames. */
21142 static int
21143 maybe_emit_file (struct dwarf_file_data * fd)
21145 if (! fd->emitted_number)
21147 if (last_emitted_file)
21148 fd->emitted_number = last_emitted_file->emitted_number + 1;
21149 else
21150 fd->emitted_number = 1;
21151 last_emitted_file = fd;
21153 if (DWARF2_ASM_LINE_DEBUG_INFO)
21155 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21156 output_quoted_string (asm_out_file,
21157 remap_debug_filename (fd->filename));
21158 fputc ('\n', asm_out_file);
21162 return fd->emitted_number;
21165 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21166 That generation should happen after function debug info has been
21167 generated. The value of the attribute is the constant value of ARG. */
21169 static void
21170 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21172 die_arg_entry entry;
21174 if (!die || !arg)
21175 return;
21177 if (!tmpl_value_parm_die_table)
21178 vec_alloc (tmpl_value_parm_die_table, 32);
21180 entry.die = die;
21181 entry.arg = arg;
21182 vec_safe_push (tmpl_value_parm_die_table, entry);
21185 /* Return TRUE if T is an instance of generic type, FALSE
21186 otherwise. */
21188 static bool
21189 generic_type_p (tree t)
21191 if (t == NULL_TREE || !TYPE_P (t))
21192 return false;
21193 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21196 /* Schedule the generation of the generic parameter dies for the
21197 instance of generic type T. The proper generation itself is later
21198 done by gen_scheduled_generic_parms_dies. */
21200 static void
21201 schedule_generic_params_dies_gen (tree t)
21203 if (!generic_type_p (t))
21204 return;
21206 if (!generic_type_instances)
21207 vec_alloc (generic_type_instances, 256);
21209 vec_safe_push (generic_type_instances, t);
21212 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21213 by append_entry_to_tmpl_value_parm_die_table. This function must
21214 be called after function DIEs have been generated. */
21216 static void
21217 gen_remaining_tmpl_value_param_die_attribute (void)
21219 if (tmpl_value_parm_die_table)
21221 unsigned i;
21222 die_arg_entry *e;
21224 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21225 tree_add_const_value_attribute (e->die, e->arg);
21229 /* Generate generic parameters DIEs for instances of generic types
21230 that have been previously scheduled by
21231 schedule_generic_params_dies_gen. This function must be called
21232 after all the types of the CU have been laid out. */
21234 static void
21235 gen_scheduled_generic_parms_dies (void)
21237 unsigned i;
21238 tree t;
21240 if (!generic_type_instances)
21241 return;
21243 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21244 if (COMPLETE_TYPE_P (t))
21245 gen_generic_params_dies (t);
21249 /* Replace DW_AT_name for the decl with name. */
21251 static void
21252 dwarf2out_set_name (tree decl, tree name)
21254 dw_die_ref die;
21255 dw_attr_ref attr;
21256 const char *dname;
21258 die = TYPE_SYMTAB_DIE (decl);
21259 if (!die)
21260 return;
21262 dname = dwarf2_name (name, 0);
21263 if (!dname)
21264 return;
21266 attr = get_AT (die, DW_AT_name);
21267 if (attr)
21269 struct indirect_string_node *node;
21271 node = find_AT_string (dname);
21272 /* replace the string. */
21273 attr->dw_attr_val.v.val_str = node;
21276 else
21277 add_name_attribute (die, dname);
21280 /* True if before or during processing of the first function being emitted. */
21281 static bool in_first_function_p = true;
21282 /* True if loc_note during dwarf2out_var_location call might still be
21283 before first real instruction at address equal to .Ltext0. */
21284 static bool maybe_at_text_label_p = true;
21285 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21286 static unsigned int first_loclabel_num_not_at_text_label;
21288 /* Called by the final INSN scan whenever we see a var location. We
21289 use it to drop labels in the right places, and throw the location in
21290 our lookup table. */
21292 static void
21293 dwarf2out_var_location (rtx loc_note)
21295 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21296 struct var_loc_node *newloc;
21297 rtx next_real, next_note;
21298 static const char *last_label;
21299 static const char *last_postcall_label;
21300 static bool last_in_cold_section_p;
21301 static rtx expected_next_loc_note;
21302 tree decl;
21303 bool var_loc_p;
21305 if (!NOTE_P (loc_note))
21307 if (CALL_P (loc_note))
21309 call_site_count++;
21310 if (SIBLING_CALL_P (loc_note))
21311 tail_call_site_count++;
21313 return;
21316 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21317 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21318 return;
21320 /* Optimize processing a large consecutive sequence of location
21321 notes so we don't spend too much time in next_real_insn. If the
21322 next insn is another location note, remember the next_real_insn
21323 calculation for next time. */
21324 next_real = cached_next_real_insn;
21325 if (next_real)
21327 if (expected_next_loc_note != loc_note)
21328 next_real = NULL_RTX;
21331 next_note = NEXT_INSN (loc_note);
21332 if (! next_note
21333 || INSN_DELETED_P (next_note)
21334 || ! NOTE_P (next_note)
21335 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21336 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21337 next_note = NULL_RTX;
21339 if (! next_real)
21340 next_real = next_real_insn (loc_note);
21342 if (next_note)
21344 expected_next_loc_note = next_note;
21345 cached_next_real_insn = next_real;
21347 else
21348 cached_next_real_insn = NULL_RTX;
21350 /* If there are no instructions which would be affected by this note,
21351 don't do anything. */
21352 if (var_loc_p
21353 && next_real == NULL_RTX
21354 && !NOTE_DURING_CALL_P (loc_note))
21355 return;
21357 if (next_real == NULL_RTX)
21358 next_real = get_last_insn ();
21360 /* If there were any real insns between note we processed last time
21361 and this note (or if it is the first note), clear
21362 last_{,postcall_}label so that they are not reused this time. */
21363 if (last_var_location_insn == NULL_RTX
21364 || last_var_location_insn != next_real
21365 || last_in_cold_section_p != in_cold_section_p)
21367 last_label = NULL;
21368 last_postcall_label = NULL;
21371 if (var_loc_p)
21373 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21374 newloc = add_var_loc_to_decl (decl, loc_note,
21375 NOTE_DURING_CALL_P (loc_note)
21376 ? last_postcall_label : last_label);
21377 if (newloc == NULL)
21378 return;
21380 else
21382 decl = NULL_TREE;
21383 newloc = NULL;
21386 /* If there were no real insns between note we processed last time
21387 and this note, use the label we emitted last time. Otherwise
21388 create a new label and emit it. */
21389 if (last_label == NULL)
21391 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21392 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21393 loclabel_num++;
21394 last_label = ggc_strdup (loclabel);
21395 /* See if loclabel might be equal to .Ltext0. If yes,
21396 bump first_loclabel_num_not_at_text_label. */
21397 if (!have_multiple_function_sections
21398 && in_first_function_p
21399 && maybe_at_text_label_p)
21401 static rtx last_start;
21402 rtx insn;
21403 for (insn = loc_note; insn; insn = previous_insn (insn))
21404 if (insn == last_start)
21405 break;
21406 else if (!NONDEBUG_INSN_P (insn))
21407 continue;
21408 else
21410 rtx body = PATTERN (insn);
21411 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21412 continue;
21413 /* Inline asm could occupy zero bytes. */
21414 else if (GET_CODE (body) == ASM_INPUT
21415 || asm_noperands (body) >= 0)
21416 continue;
21417 #ifdef HAVE_attr_length
21418 else if (get_attr_min_length (insn) == 0)
21419 continue;
21420 #endif
21421 else
21423 /* Assume insn has non-zero length. */
21424 maybe_at_text_label_p = false;
21425 break;
21428 if (maybe_at_text_label_p)
21430 last_start = loc_note;
21431 first_loclabel_num_not_at_text_label = loclabel_num;
21436 if (!var_loc_p)
21438 struct call_arg_loc_node *ca_loc
21439 = ggc_cleared_alloc<call_arg_loc_node> ();
21440 rtx prev = prev_real_insn (loc_note), x;
21441 ca_loc->call_arg_loc_note = loc_note;
21442 ca_loc->next = NULL;
21443 ca_loc->label = last_label;
21444 gcc_assert (prev
21445 && (CALL_P (prev)
21446 || (NONJUMP_INSN_P (prev)
21447 && GET_CODE (PATTERN (prev)) == SEQUENCE
21448 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21449 if (!CALL_P (prev))
21450 prev = XVECEXP (PATTERN (prev), 0, 0);
21451 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21452 x = get_call_rtx_from (PATTERN (prev));
21453 if (x)
21455 x = XEXP (XEXP (x, 0), 0);
21456 if (GET_CODE (x) == SYMBOL_REF
21457 && SYMBOL_REF_DECL (x)
21458 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21459 ca_loc->symbol_ref = x;
21461 ca_loc->block = insn_scope (prev);
21462 if (call_arg_locations)
21463 call_arg_loc_last->next = ca_loc;
21464 else
21465 call_arg_locations = ca_loc;
21466 call_arg_loc_last = ca_loc;
21468 else if (!NOTE_DURING_CALL_P (loc_note))
21469 newloc->label = last_label;
21470 else
21472 if (!last_postcall_label)
21474 sprintf (loclabel, "%s-1", last_label);
21475 last_postcall_label = ggc_strdup (loclabel);
21477 newloc->label = last_postcall_label;
21480 last_var_location_insn = next_real;
21481 last_in_cold_section_p = in_cold_section_p;
21484 /* Note in one location list that text section has changed. */
21486 static int
21487 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21489 var_loc_list *list = (var_loc_list *) *slot;
21490 if (list->first)
21491 list->last_before_switch
21492 = list->last->next ? list->last->next : list->last;
21493 return 1;
21496 /* Note in all location lists that text section has changed. */
21498 static void
21499 var_location_switch_text_section (void)
21501 if (decl_loc_table == NULL)
21502 return;
21504 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21507 /* Create a new line number table. */
21509 static dw_line_info_table *
21510 new_line_info_table (void)
21512 dw_line_info_table *table;
21514 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21515 table->file_num = 1;
21516 table->line_num = 1;
21517 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21519 return table;
21522 /* Lookup the "current" table into which we emit line info, so
21523 that we don't have to do it for every source line. */
21525 static void
21526 set_cur_line_info_table (section *sec)
21528 dw_line_info_table *table;
21530 if (sec == text_section)
21531 table = text_section_line_info;
21532 else if (sec == cold_text_section)
21534 table = cold_text_section_line_info;
21535 if (!table)
21537 cold_text_section_line_info = table = new_line_info_table ();
21538 table->end_label = cold_end_label;
21541 else
21543 const char *end_label;
21545 if (flag_reorder_blocks_and_partition)
21547 if (in_cold_section_p)
21548 end_label = crtl->subsections.cold_section_end_label;
21549 else
21550 end_label = crtl->subsections.hot_section_end_label;
21552 else
21554 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21555 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21556 current_function_funcdef_no);
21557 end_label = ggc_strdup (label);
21560 table = new_line_info_table ();
21561 table->end_label = end_label;
21563 vec_safe_push (separate_line_info, table);
21566 if (DWARF2_ASM_LINE_DEBUG_INFO)
21567 table->is_stmt = (cur_line_info_table
21568 ? cur_line_info_table->is_stmt
21569 : DWARF_LINE_DEFAULT_IS_STMT_START);
21570 cur_line_info_table = table;
21574 /* We need to reset the locations at the beginning of each
21575 function. We can't do this in the end_function hook, because the
21576 declarations that use the locations won't have been output when
21577 that hook is called. Also compute have_multiple_function_sections here. */
21579 static void
21580 dwarf2out_begin_function (tree fun)
21582 section *sec = function_section (fun);
21584 if (sec != text_section)
21585 have_multiple_function_sections = true;
21587 if (flag_reorder_blocks_and_partition && !cold_text_section)
21589 gcc_assert (current_function_decl == fun);
21590 cold_text_section = unlikely_text_section ();
21591 switch_to_section (cold_text_section);
21592 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21593 switch_to_section (sec);
21596 dwarf2out_note_section_used ();
21597 call_site_count = 0;
21598 tail_call_site_count = 0;
21600 set_cur_line_info_table (sec);
21603 /* Helper function of dwarf2out_end_function, called only after emitting
21604 the very first function into assembly. Check if some .debug_loc range
21605 might end with a .LVL* label that could be equal to .Ltext0.
21606 In that case we must force using absolute addresses in .debug_loc ranges,
21607 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21608 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21609 list terminator.
21610 Set have_multiple_function_sections to true in that case and
21611 terminate htab traversal. */
21613 static int
21614 find_empty_loc_ranges_at_text_label (void **slot, void *)
21616 var_loc_list *entry;
21617 struct var_loc_node *node;
21619 entry = (var_loc_list *) *slot;
21620 node = entry->first;
21621 if (node && node->next && node->next->label)
21623 unsigned int i;
21624 const char *label = node->next->label;
21625 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21627 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21629 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21630 if (strcmp (label, loclabel) == 0)
21632 have_multiple_function_sections = true;
21633 return 0;
21637 return 1;
21640 /* Hook called after emitting a function into assembly.
21641 This does something only for the very first function emitted. */
21643 static void
21644 dwarf2out_end_function (unsigned int)
21646 if (in_first_function_p
21647 && !have_multiple_function_sections
21648 && first_loclabel_num_not_at_text_label
21649 && decl_loc_table)
21650 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21651 NULL);
21652 in_first_function_p = false;
21653 maybe_at_text_label_p = false;
21656 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21658 static void
21659 push_dw_line_info_entry (dw_line_info_table *table,
21660 enum dw_line_info_opcode opcode, unsigned int val)
21662 dw_line_info_entry e;
21663 e.opcode = opcode;
21664 e.val = val;
21665 vec_safe_push (table->entries, e);
21668 /* Output a label to mark the beginning of a source code line entry
21669 and record information relating to this source line, in
21670 'line_info_table' for later output of the .debug_line section. */
21671 /* ??? The discriminator parameter ought to be unsigned. */
21673 static void
21674 dwarf2out_source_line (unsigned int line, const char *filename,
21675 int discriminator, bool is_stmt)
21677 unsigned int file_num;
21678 dw_line_info_table *table;
21680 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21681 return;
21683 /* The discriminator column was added in dwarf4. Simplify the below
21684 by simply removing it if we're not supposed to output it. */
21685 if (dwarf_version < 4 && dwarf_strict)
21686 discriminator = 0;
21688 table = cur_line_info_table;
21689 file_num = maybe_emit_file (lookup_filename (filename));
21691 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21692 the debugger has used the second (possibly duplicate) line number
21693 at the beginning of the function to mark the end of the prologue.
21694 We could eliminate any other duplicates within the function. For
21695 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21696 that second line number entry. */
21697 /* Recall that this end-of-prologue indication is *not* the same thing
21698 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21699 to which the hook corresponds, follows the last insn that was
21700 emitted by gen_prologue. What we need is to precede the first insn
21701 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21702 insn that corresponds to something the user wrote. These may be
21703 very different locations once scheduling is enabled. */
21705 if (0 && file_num == table->file_num
21706 && line == table->line_num
21707 && discriminator == table->discrim_num
21708 && is_stmt == table->is_stmt)
21709 return;
21711 switch_to_section (current_function_section ());
21713 /* If requested, emit something human-readable. */
21714 if (flag_debug_asm)
21715 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21717 if (DWARF2_ASM_LINE_DEBUG_INFO)
21719 /* Emit the .loc directive understood by GNU as. */
21720 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21721 file_num, line, is_stmt, discriminator */
21722 fputs ("\t.loc ", asm_out_file);
21723 fprint_ul (asm_out_file, file_num);
21724 putc (' ', asm_out_file);
21725 fprint_ul (asm_out_file, line);
21726 putc (' ', asm_out_file);
21727 putc ('0', asm_out_file);
21729 if (is_stmt != table->is_stmt)
21731 fputs (" is_stmt ", asm_out_file);
21732 putc (is_stmt ? '1' : '0', asm_out_file);
21734 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21736 gcc_assert (discriminator > 0);
21737 fputs (" discriminator ", asm_out_file);
21738 fprint_ul (asm_out_file, (unsigned long) discriminator);
21740 putc ('\n', asm_out_file);
21742 else
21744 unsigned int label_num = ++line_info_label_num;
21746 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21748 push_dw_line_info_entry (table, LI_set_address, label_num);
21749 if (file_num != table->file_num)
21750 push_dw_line_info_entry (table, LI_set_file, file_num);
21751 if (discriminator != table->discrim_num)
21752 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21753 if (is_stmt != table->is_stmt)
21754 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21755 push_dw_line_info_entry (table, LI_set_line, line);
21758 table->file_num = file_num;
21759 table->line_num = line;
21760 table->discrim_num = discriminator;
21761 table->is_stmt = is_stmt;
21762 table->in_use = true;
21765 /* Record the beginning of a new source file. */
21767 static void
21768 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21770 if (flag_eliminate_dwarf2_dups)
21772 /* Record the beginning of the file for break_out_includes. */
21773 dw_die_ref bincl_die;
21775 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21776 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21779 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21781 macinfo_entry e;
21782 e.code = DW_MACINFO_start_file;
21783 e.lineno = lineno;
21784 e.info = ggc_strdup (filename);
21785 vec_safe_push (macinfo_table, e);
21789 /* Record the end of a source file. */
21791 static void
21792 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21794 if (flag_eliminate_dwarf2_dups)
21795 /* Record the end of the file for break_out_includes. */
21796 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21798 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21800 macinfo_entry e;
21801 e.code = DW_MACINFO_end_file;
21802 e.lineno = lineno;
21803 e.info = NULL;
21804 vec_safe_push (macinfo_table, e);
21808 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21809 the tail part of the directive line, i.e. the part which is past the
21810 initial whitespace, #, whitespace, directive-name, whitespace part. */
21812 static void
21813 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21814 const char *buffer ATTRIBUTE_UNUSED)
21816 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21818 macinfo_entry e;
21819 /* Insert a dummy first entry to be able to optimize the whole
21820 predefined macro block using DW_MACRO_GNU_transparent_include. */
21821 if (macinfo_table->is_empty () && lineno <= 1)
21823 e.code = 0;
21824 e.lineno = 0;
21825 e.info = NULL;
21826 vec_safe_push (macinfo_table, e);
21828 e.code = DW_MACINFO_define;
21829 e.lineno = lineno;
21830 e.info = ggc_strdup (buffer);
21831 vec_safe_push (macinfo_table, e);
21835 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21836 the tail part of the directive line, i.e. the part which is past the
21837 initial whitespace, #, whitespace, directive-name, whitespace part. */
21839 static void
21840 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21841 const char *buffer ATTRIBUTE_UNUSED)
21843 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21845 macinfo_entry e;
21846 /* Insert a dummy first entry to be able to optimize the whole
21847 predefined macro block using DW_MACRO_GNU_transparent_include. */
21848 if (macinfo_table->is_empty () && lineno <= 1)
21850 e.code = 0;
21851 e.lineno = 0;
21852 e.info = NULL;
21853 vec_safe_push (macinfo_table, e);
21855 e.code = DW_MACINFO_undef;
21856 e.lineno = lineno;
21857 e.info = ggc_strdup (buffer);
21858 vec_safe_push (macinfo_table, e);
21862 /* Helpers to manipulate hash table of CUs. */
21864 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21866 typedef macinfo_entry value_type;
21867 typedef macinfo_entry compare_type;
21868 static inline hashval_t hash (const value_type *);
21869 static inline bool equal (const value_type *, const compare_type *);
21872 inline hashval_t
21873 macinfo_entry_hasher::hash (const value_type *entry)
21875 return htab_hash_string (entry->info);
21878 inline bool
21879 macinfo_entry_hasher::equal (const value_type *entry1,
21880 const compare_type *entry2)
21882 return !strcmp (entry1->info, entry2->info);
21885 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
21887 /* Output a single .debug_macinfo entry. */
21889 static void
21890 output_macinfo_op (macinfo_entry *ref)
21892 int file_num;
21893 size_t len;
21894 struct indirect_string_node *node;
21895 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21896 struct dwarf_file_data *fd;
21898 switch (ref->code)
21900 case DW_MACINFO_start_file:
21901 fd = lookup_filename (ref->info);
21902 file_num = maybe_emit_file (fd);
21903 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21904 dw2_asm_output_data_uleb128 (ref->lineno,
21905 "Included from line number %lu",
21906 (unsigned long) ref->lineno);
21907 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21908 break;
21909 case DW_MACINFO_end_file:
21910 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21911 break;
21912 case DW_MACINFO_define:
21913 case DW_MACINFO_undef:
21914 len = strlen (ref->info) + 1;
21915 if (!dwarf_strict
21916 && len > DWARF_OFFSET_SIZE
21917 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21918 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21920 ref->code = ref->code == DW_MACINFO_define
21921 ? DW_MACRO_GNU_define_indirect
21922 : DW_MACRO_GNU_undef_indirect;
21923 output_macinfo_op (ref);
21924 return;
21926 dw2_asm_output_data (1, ref->code,
21927 ref->code == DW_MACINFO_define
21928 ? "Define macro" : "Undefine macro");
21929 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21930 (unsigned long) ref->lineno);
21931 dw2_asm_output_nstring (ref->info, -1, "The macro");
21932 break;
21933 case DW_MACRO_GNU_define_indirect:
21934 case DW_MACRO_GNU_undef_indirect:
21935 node = find_AT_string (ref->info);
21936 gcc_assert (node
21937 && ((node->form == DW_FORM_strp)
21938 || (node->form == DW_FORM_GNU_str_index)));
21939 dw2_asm_output_data (1, ref->code,
21940 ref->code == DW_MACRO_GNU_define_indirect
21941 ? "Define macro indirect"
21942 : "Undefine macro indirect");
21943 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21944 (unsigned long) ref->lineno);
21945 if (node->form == DW_FORM_strp)
21946 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21947 debug_str_section, "The macro: \"%s\"",
21948 ref->info);
21949 else
21950 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21951 ref->info);
21952 break;
21953 case DW_MACRO_GNU_transparent_include:
21954 dw2_asm_output_data (1, ref->code, "Transparent include");
21955 ASM_GENERATE_INTERNAL_LABEL (label,
21956 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21957 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21958 break;
21959 default:
21960 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21961 ASM_COMMENT_START, (unsigned long) ref->code);
21962 break;
21966 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21967 other compilation unit .debug_macinfo sections. IDX is the first
21968 index of a define/undef, return the number of ops that should be
21969 emitted in a comdat .debug_macinfo section and emit
21970 a DW_MACRO_GNU_transparent_include entry referencing it.
21971 If the define/undef entry should be emitted normally, return 0. */
21973 static unsigned
21974 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21975 macinfo_hash_type **macinfo_htab)
21977 macinfo_entry *first, *second, *cur, *inc;
21978 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21979 unsigned char checksum[16];
21980 struct md5_ctx ctx;
21981 char *grp_name, *tail;
21982 const char *base;
21983 unsigned int i, count, encoded_filename_len, linebuf_len;
21984 macinfo_entry **slot;
21986 first = &(*macinfo_table)[idx];
21987 second = &(*macinfo_table)[idx + 1];
21989 /* Optimize only if there are at least two consecutive define/undef ops,
21990 and either all of them are before first DW_MACINFO_start_file
21991 with lineno {0,1} (i.e. predefined macro block), or all of them are
21992 in some included header file. */
21993 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21994 return 0;
21995 if (vec_safe_is_empty (files))
21997 if (first->lineno > 1 || second->lineno > 1)
21998 return 0;
22000 else if (first->lineno == 0)
22001 return 0;
22003 /* Find the last define/undef entry that can be grouped together
22004 with first and at the same time compute md5 checksum of their
22005 codes, linenumbers and strings. */
22006 md5_init_ctx (&ctx);
22007 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22008 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22009 break;
22010 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22011 break;
22012 else
22014 unsigned char code = cur->code;
22015 md5_process_bytes (&code, 1, &ctx);
22016 checksum_uleb128 (cur->lineno, &ctx);
22017 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22019 md5_finish_ctx (&ctx, checksum);
22020 count = i - idx;
22022 /* From the containing include filename (if any) pick up just
22023 usable characters from its basename. */
22024 if (vec_safe_is_empty (files))
22025 base = "";
22026 else
22027 base = lbasename (files->last ().info);
22028 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22029 if (ISIDNUM (base[i]) || base[i] == '.')
22030 encoded_filename_len++;
22031 /* Count . at the end. */
22032 if (encoded_filename_len)
22033 encoded_filename_len++;
22035 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22036 linebuf_len = strlen (linebuf);
22038 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22039 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22040 + 16 * 2 + 1);
22041 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22042 tail = grp_name + 4;
22043 if (encoded_filename_len)
22045 for (i = 0; base[i]; i++)
22046 if (ISIDNUM (base[i]) || base[i] == '.')
22047 *tail++ = base[i];
22048 *tail++ = '.';
22050 memcpy (tail, linebuf, linebuf_len);
22051 tail += linebuf_len;
22052 *tail++ = '.';
22053 for (i = 0; i < 16; i++)
22054 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22056 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22057 in the empty vector entry before the first define/undef. */
22058 inc = &(*macinfo_table)[idx - 1];
22059 inc->code = DW_MACRO_GNU_transparent_include;
22060 inc->lineno = 0;
22061 inc->info = ggc_strdup (grp_name);
22062 if (!*macinfo_htab)
22063 *macinfo_htab = new macinfo_hash_type (10);
22064 /* Avoid emitting duplicates. */
22065 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22066 if (*slot != NULL)
22068 inc->code = 0;
22069 inc->info = NULL;
22070 /* If such an entry has been used before, just emit
22071 a DW_MACRO_GNU_transparent_include op. */
22072 inc = *slot;
22073 output_macinfo_op (inc);
22074 /* And clear all macinfo_entry in the range to avoid emitting them
22075 in the second pass. */
22076 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22078 cur->code = 0;
22079 cur->info = NULL;
22082 else
22084 *slot = inc;
22085 inc->lineno = (*macinfo_htab)->elements ();
22086 output_macinfo_op (inc);
22088 return count;
22091 /* Save any strings needed by the macinfo table in the debug str
22092 table. All strings must be collected into the table by the time
22093 index_string is called. */
22095 static void
22096 save_macinfo_strings (void)
22098 unsigned len;
22099 unsigned i;
22100 macinfo_entry *ref;
22102 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22104 switch (ref->code)
22106 /* Match the logic in output_macinfo_op to decide on
22107 indirect strings. */
22108 case DW_MACINFO_define:
22109 case DW_MACINFO_undef:
22110 len = strlen (ref->info) + 1;
22111 if (!dwarf_strict
22112 && len > DWARF_OFFSET_SIZE
22113 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22114 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22115 set_indirect_string (find_AT_string (ref->info));
22116 break;
22117 case DW_MACRO_GNU_define_indirect:
22118 case DW_MACRO_GNU_undef_indirect:
22119 set_indirect_string (find_AT_string (ref->info));
22120 break;
22121 default:
22122 break;
22127 /* Output macinfo section(s). */
22129 static void
22130 output_macinfo (void)
22132 unsigned i;
22133 unsigned long length = vec_safe_length (macinfo_table);
22134 macinfo_entry *ref;
22135 vec<macinfo_entry, va_gc> *files = NULL;
22136 macinfo_hash_type *macinfo_htab = NULL;
22138 if (! length)
22139 return;
22141 /* output_macinfo* uses these interchangeably. */
22142 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22143 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22144 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22145 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22147 /* For .debug_macro emit the section header. */
22148 if (!dwarf_strict)
22150 dw2_asm_output_data (2, 4, "DWARF macro version number");
22151 if (DWARF_OFFSET_SIZE == 8)
22152 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22153 else
22154 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22155 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22156 (!dwarf_split_debug_info ? debug_line_section_label
22157 : debug_skeleton_line_section_label),
22158 debug_line_section, NULL);
22161 /* In the first loop, it emits the primary .debug_macinfo section
22162 and after each emitted op the macinfo_entry is cleared.
22163 If a longer range of define/undef ops can be optimized using
22164 DW_MACRO_GNU_transparent_include, the
22165 DW_MACRO_GNU_transparent_include op is emitted and kept in
22166 the vector before the first define/undef in the range and the
22167 whole range of define/undef ops is not emitted and kept. */
22168 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22170 switch (ref->code)
22172 case DW_MACINFO_start_file:
22173 vec_safe_push (files, *ref);
22174 break;
22175 case DW_MACINFO_end_file:
22176 if (!vec_safe_is_empty (files))
22177 files->pop ();
22178 break;
22179 case DW_MACINFO_define:
22180 case DW_MACINFO_undef:
22181 if (!dwarf_strict
22182 && HAVE_COMDAT_GROUP
22183 && vec_safe_length (files) != 1
22184 && i > 0
22185 && i + 1 < length
22186 && (*macinfo_table)[i - 1].code == 0)
22188 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22189 if (count)
22191 i += count - 1;
22192 continue;
22195 break;
22196 case 0:
22197 /* A dummy entry may be inserted at the beginning to be able
22198 to optimize the whole block of predefined macros. */
22199 if (i == 0)
22200 continue;
22201 default:
22202 break;
22204 output_macinfo_op (ref);
22205 ref->info = NULL;
22206 ref->code = 0;
22209 if (!macinfo_htab)
22210 return;
22212 delete macinfo_htab;
22213 macinfo_htab = NULL;
22215 /* If any DW_MACRO_GNU_transparent_include were used, on those
22216 DW_MACRO_GNU_transparent_include entries terminate the
22217 current chain and switch to a new comdat .debug_macinfo
22218 section and emit the define/undef entries within it. */
22219 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22220 switch (ref->code)
22222 case 0:
22223 continue;
22224 case DW_MACRO_GNU_transparent_include:
22226 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22227 tree comdat_key = get_identifier (ref->info);
22228 /* Terminate the previous .debug_macinfo section. */
22229 dw2_asm_output_data (1, 0, "End compilation unit");
22230 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22231 SECTION_DEBUG
22232 | SECTION_LINKONCE,
22233 comdat_key);
22234 ASM_GENERATE_INTERNAL_LABEL (label,
22235 DEBUG_MACRO_SECTION_LABEL,
22236 ref->lineno);
22237 ASM_OUTPUT_LABEL (asm_out_file, label);
22238 ref->code = 0;
22239 ref->info = NULL;
22240 dw2_asm_output_data (2, 4, "DWARF macro version number");
22241 if (DWARF_OFFSET_SIZE == 8)
22242 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22243 else
22244 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22246 break;
22247 case DW_MACINFO_define:
22248 case DW_MACINFO_undef:
22249 output_macinfo_op (ref);
22250 ref->code = 0;
22251 ref->info = NULL;
22252 break;
22253 default:
22254 gcc_unreachable ();
22258 /* Set up for Dwarf output at the start of compilation. */
22260 static void
22261 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22263 /* Allocate the file_table. */
22264 file_table = htab_create_ggc (50, file_table_hash,
22265 file_table_eq, NULL);
22267 /* Allocate the decl_die_table. */
22268 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22269 decl_die_table_eq, NULL);
22271 /* Allocate the decl_loc_table. */
22272 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22273 decl_loc_table_eq, NULL);
22275 /* Allocate the cached_dw_loc_list_table. */
22276 cached_dw_loc_list_table
22277 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22278 cached_dw_loc_list_table_eq, NULL);
22280 /* Allocate the initial hunk of the decl_scope_table. */
22281 vec_alloc (decl_scope_table, 256);
22283 /* Allocate the initial hunk of the abbrev_die_table. */
22284 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22285 (ABBREV_DIE_TABLE_INCREMENT);
22286 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22287 /* Zero-th entry is allocated, but unused. */
22288 abbrev_die_table_in_use = 1;
22290 /* Allocate the pubtypes and pubnames vectors. */
22291 vec_alloc (pubname_table, 32);
22292 vec_alloc (pubtype_table, 32);
22294 vec_alloc (incomplete_types, 64);
22296 vec_alloc (used_rtx_array, 32);
22298 if (!dwarf_split_debug_info)
22300 debug_info_section = get_section (DEBUG_INFO_SECTION,
22301 SECTION_DEBUG, NULL);
22302 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22303 SECTION_DEBUG, NULL);
22304 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22305 SECTION_DEBUG, NULL);
22307 else
22309 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22310 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22311 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22312 SECTION_DEBUG | SECTION_EXCLUDE,
22313 NULL);
22314 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22315 SECTION_DEBUG, NULL);
22316 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22317 SECTION_DEBUG, NULL);
22318 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22319 SECTION_DEBUG, NULL);
22320 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22321 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22323 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22324 the main .o, but the skeleton_line goes into the split off dwo. */
22325 debug_skeleton_line_section
22326 = get_section (DEBUG_DWO_LINE_SECTION,
22327 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22328 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22329 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22330 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22331 SECTION_DEBUG | SECTION_EXCLUDE,
22332 NULL);
22333 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22334 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22335 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22336 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22337 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22338 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22340 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22341 SECTION_DEBUG, NULL);
22342 debug_macinfo_section = get_section (dwarf_strict
22343 ? DEBUG_MACINFO_SECTION
22344 : DEBUG_MACRO_SECTION,
22345 DEBUG_MACRO_SECTION_FLAGS, NULL);
22346 debug_line_section = get_section (DEBUG_LINE_SECTION,
22347 SECTION_DEBUG, NULL);
22348 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22349 SECTION_DEBUG, NULL);
22350 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22351 SECTION_DEBUG, NULL);
22352 debug_str_section = get_section (DEBUG_STR_SECTION,
22353 DEBUG_STR_SECTION_FLAGS, NULL);
22354 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22355 SECTION_DEBUG, NULL);
22356 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22357 SECTION_DEBUG, NULL);
22359 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22360 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22361 DEBUG_ABBREV_SECTION_LABEL, 0);
22362 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22363 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22364 COLD_TEXT_SECTION_LABEL, 0);
22365 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22367 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22368 DEBUG_INFO_SECTION_LABEL, 0);
22369 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22370 DEBUG_LINE_SECTION_LABEL, 0);
22371 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22372 DEBUG_RANGES_SECTION_LABEL, 0);
22373 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22374 DEBUG_ADDR_SECTION_LABEL, 0);
22375 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22376 dwarf_strict
22377 ? DEBUG_MACINFO_SECTION_LABEL
22378 : DEBUG_MACRO_SECTION_LABEL, 0);
22379 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22381 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22382 vec_alloc (macinfo_table, 64);
22384 switch_to_section (text_section);
22385 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22387 /* Make sure the line number table for .text always exists. */
22388 text_section_line_info = new_line_info_table ();
22389 text_section_line_info->end_label = text_end_label;
22392 /* Called before compile () starts outputtting functions, variables
22393 and toplevel asms into assembly. */
22395 static void
22396 dwarf2out_assembly_start (void)
22398 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22399 && dwarf2out_do_cfi_asm ()
22400 && (!(flag_unwind_tables || flag_exceptions)
22401 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22402 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22405 /* A helper function for dwarf2out_finish called through
22406 htab_traverse. Assign a string its index. All strings must be
22407 collected into the table by the time index_string is called,
22408 because the indexing code relies on htab_traverse to traverse nodes
22409 in the same order for each run. */
22411 static int
22412 index_string (void **h, void *v)
22414 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22415 unsigned int *index = (unsigned int *) v;
22417 find_string_form (node);
22418 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22420 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22421 node->index = *index;
22422 *index += 1;
22424 return 1;
22427 /* A helper function for output_indirect_strings called through
22428 htab_traverse. Output the offset to a string and update the
22429 current offset. */
22431 static int
22432 output_index_string_offset (void **h, void *v)
22434 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22435 unsigned int *offset = (unsigned int *) v;
22437 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22439 /* Assert that this node has been assigned an index. */
22440 gcc_assert (node->index != NO_INDEX_ASSIGNED
22441 && node->index != NOT_INDEXED);
22442 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22443 "indexed string 0x%x: %s", node->index, node->str);
22444 *offset += strlen (node->str) + 1;
22446 return 1;
22449 /* A helper function for dwarf2out_finish called through
22450 htab_traverse. Output the indexed string. */
22452 static int
22453 output_index_string (void **h, void *v)
22455 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22456 unsigned int *cur_idx = (unsigned int *) v;
22458 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22460 /* Assert that the strings are output in the same order as their
22461 indexes were assigned. */
22462 gcc_assert (*cur_idx == node->index);
22463 assemble_string (node->str, strlen (node->str) + 1);
22464 *cur_idx += 1;
22466 return 1;
22469 /* A helper function for dwarf2out_finish called through
22470 htab_traverse. Emit one queued .debug_str string. */
22472 static int
22473 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22475 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22477 node->form = find_string_form (node);
22478 if (node->form == DW_FORM_strp && node->refcount > 0)
22480 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22481 assemble_string (node->str, strlen (node->str) + 1);
22484 return 1;
22487 /* Output the indexed string table. */
22489 static void
22490 output_indirect_strings (void)
22492 switch_to_section (debug_str_section);
22493 if (!dwarf_split_debug_info)
22494 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22495 else
22497 unsigned int offset = 0;
22498 unsigned int cur_idx = 0;
22500 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22502 switch_to_section (debug_str_offsets_section);
22503 htab_traverse_noresize (debug_str_hash,
22504 output_index_string_offset,
22505 &offset);
22506 switch_to_section (debug_str_dwo_section);
22507 htab_traverse_noresize (debug_str_hash,
22508 output_index_string,
22509 &cur_idx);
22513 /* Callback for htab_traverse to assign an index to an entry in the
22514 table, and to write that entry to the .debug_addr section. */
22516 static int
22517 output_addr_table_entry (void **slot, void *data)
22519 addr_table_entry *entry = (addr_table_entry *) *slot;
22520 unsigned int *cur_index = (unsigned int *)data;
22522 if (entry->refcount == 0)
22524 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22525 || entry->index == NOT_INDEXED);
22526 return 1;
22529 gcc_assert (entry->index == *cur_index);
22530 (*cur_index)++;
22532 switch (entry->kind)
22534 case ate_kind_rtx:
22535 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22536 "0x%x", entry->index);
22537 break;
22538 case ate_kind_rtx_dtprel:
22539 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22540 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22541 DWARF2_ADDR_SIZE,
22542 entry->addr.rtl);
22543 fputc ('\n', asm_out_file);
22544 break;
22545 case ate_kind_label:
22546 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22547 "0x%x", entry->index);
22548 break;
22549 default:
22550 gcc_unreachable ();
22552 return 1;
22555 /* Produce the .debug_addr section. */
22557 static void
22558 output_addr_table (void)
22560 unsigned int index = 0;
22561 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22562 return;
22564 switch_to_section (debug_addr_section);
22565 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22568 #if ENABLE_ASSERT_CHECKING
22569 /* Verify that all marks are clear. */
22571 static void
22572 verify_marks_clear (dw_die_ref die)
22574 dw_die_ref c;
22576 gcc_assert (! die->die_mark);
22577 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22579 #endif /* ENABLE_ASSERT_CHECKING */
22581 /* Clear the marks for a die and its children.
22582 Be cool if the mark isn't set. */
22584 static void
22585 prune_unmark_dies (dw_die_ref die)
22587 dw_die_ref c;
22589 if (die->die_mark)
22590 die->die_mark = 0;
22591 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22594 /* Given DIE that we're marking as used, find any other dies
22595 it references as attributes and mark them as used. */
22597 static void
22598 prune_unused_types_walk_attribs (dw_die_ref die)
22600 dw_attr_ref a;
22601 unsigned ix;
22603 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22605 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22607 /* A reference to another DIE.
22608 Make sure that it will get emitted.
22609 If it was broken out into a comdat group, don't follow it. */
22610 if (! AT_ref (a)->comdat_type_p
22611 || a->dw_attr == DW_AT_specification)
22612 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22614 /* Set the string's refcount to 0 so that prune_unused_types_mark
22615 accounts properly for it. */
22616 if (AT_class (a) == dw_val_class_str)
22617 a->dw_attr_val.v.val_str->refcount = 0;
22621 /* Mark the generic parameters and arguments children DIEs of DIE. */
22623 static void
22624 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22626 dw_die_ref c;
22628 if (die == NULL || die->die_child == NULL)
22629 return;
22630 c = die->die_child;
22633 if (is_template_parameter (c))
22634 prune_unused_types_mark (c, 1);
22635 c = c->die_sib;
22636 } while (c && c != die->die_child);
22639 /* Mark DIE as being used. If DOKIDS is true, then walk down
22640 to DIE's children. */
22642 static void
22643 prune_unused_types_mark (dw_die_ref die, int dokids)
22645 dw_die_ref c;
22647 if (die->die_mark == 0)
22649 /* We haven't done this node yet. Mark it as used. */
22650 die->die_mark = 1;
22651 /* If this is the DIE of a generic type instantiation,
22652 mark the children DIEs that describe its generic parms and
22653 args. */
22654 prune_unused_types_mark_generic_parms_dies (die);
22656 /* We also have to mark its parents as used.
22657 (But we don't want to mark our parent's kids due to this,
22658 unless it is a class.) */
22659 if (die->die_parent)
22660 prune_unused_types_mark (die->die_parent,
22661 class_scope_p (die->die_parent));
22663 /* Mark any referenced nodes. */
22664 prune_unused_types_walk_attribs (die);
22666 /* If this node is a specification,
22667 also mark the definition, if it exists. */
22668 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22669 prune_unused_types_mark (die->die_definition, 1);
22672 if (dokids && die->die_mark != 2)
22674 /* We need to walk the children, but haven't done so yet.
22675 Remember that we've walked the kids. */
22676 die->die_mark = 2;
22678 /* If this is an array type, we need to make sure our
22679 kids get marked, even if they're types. If we're
22680 breaking out types into comdat sections, do this
22681 for all type definitions. */
22682 if (die->die_tag == DW_TAG_array_type
22683 || (use_debug_types
22684 && is_type_die (die) && ! is_declaration_die (die)))
22685 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22686 else
22687 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22691 /* For local classes, look if any static member functions were emitted
22692 and if so, mark them. */
22694 static void
22695 prune_unused_types_walk_local_classes (dw_die_ref die)
22697 dw_die_ref c;
22699 if (die->die_mark == 2)
22700 return;
22702 switch (die->die_tag)
22704 case DW_TAG_structure_type:
22705 case DW_TAG_union_type:
22706 case DW_TAG_class_type:
22707 break;
22709 case DW_TAG_subprogram:
22710 if (!get_AT_flag (die, DW_AT_declaration)
22711 || die->die_definition != NULL)
22712 prune_unused_types_mark (die, 1);
22713 return;
22715 default:
22716 return;
22719 /* Mark children. */
22720 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22723 /* Walk the tree DIE and mark types that we actually use. */
22725 static void
22726 prune_unused_types_walk (dw_die_ref die)
22728 dw_die_ref c;
22730 /* Don't do anything if this node is already marked and
22731 children have been marked as well. */
22732 if (die->die_mark == 2)
22733 return;
22735 switch (die->die_tag)
22737 case DW_TAG_structure_type:
22738 case DW_TAG_union_type:
22739 case DW_TAG_class_type:
22740 if (die->die_perennial_p)
22741 break;
22743 for (c = die->die_parent; c; c = c->die_parent)
22744 if (c->die_tag == DW_TAG_subprogram)
22745 break;
22747 /* Finding used static member functions inside of classes
22748 is needed just for local classes, because for other classes
22749 static member function DIEs with DW_AT_specification
22750 are emitted outside of the DW_TAG_*_type. If we ever change
22751 it, we'd need to call this even for non-local classes. */
22752 if (c)
22753 prune_unused_types_walk_local_classes (die);
22755 /* It's a type node --- don't mark it. */
22756 return;
22758 case DW_TAG_const_type:
22759 case DW_TAG_packed_type:
22760 case DW_TAG_pointer_type:
22761 case DW_TAG_reference_type:
22762 case DW_TAG_rvalue_reference_type:
22763 case DW_TAG_volatile_type:
22764 case DW_TAG_typedef:
22765 case DW_TAG_array_type:
22766 case DW_TAG_interface_type:
22767 case DW_TAG_friend:
22768 case DW_TAG_variant_part:
22769 case DW_TAG_enumeration_type:
22770 case DW_TAG_subroutine_type:
22771 case DW_TAG_string_type:
22772 case DW_TAG_set_type:
22773 case DW_TAG_subrange_type:
22774 case DW_TAG_ptr_to_member_type:
22775 case DW_TAG_file_type:
22776 if (die->die_perennial_p)
22777 break;
22779 /* It's a type node --- don't mark it. */
22780 return;
22782 default:
22783 /* Mark everything else. */
22784 break;
22787 if (die->die_mark == 0)
22789 die->die_mark = 1;
22791 /* Now, mark any dies referenced from here. */
22792 prune_unused_types_walk_attribs (die);
22795 die->die_mark = 2;
22797 /* Mark children. */
22798 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22801 /* Increment the string counts on strings referred to from DIE's
22802 attributes. */
22804 static void
22805 prune_unused_types_update_strings (dw_die_ref die)
22807 dw_attr_ref a;
22808 unsigned ix;
22810 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22811 if (AT_class (a) == dw_val_class_str)
22813 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22814 s->refcount++;
22815 /* Avoid unnecessarily putting strings that are used less than
22816 twice in the hash table. */
22817 if (s->refcount
22818 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22820 void ** slot;
22821 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22822 htab_hash_string (s->str),
22823 INSERT);
22824 gcc_assert (*slot == NULL);
22825 *slot = s;
22830 /* Remove from the tree DIE any dies that aren't marked. */
22832 static void
22833 prune_unused_types_prune (dw_die_ref die)
22835 dw_die_ref c;
22837 gcc_assert (die->die_mark);
22838 prune_unused_types_update_strings (die);
22840 if (! die->die_child)
22841 return;
22843 c = die->die_child;
22844 do {
22845 dw_die_ref prev = c;
22846 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22847 if (c == die->die_child)
22849 /* No marked children between 'prev' and the end of the list. */
22850 if (prev == c)
22851 /* No marked children at all. */
22852 die->die_child = NULL;
22853 else
22855 prev->die_sib = c->die_sib;
22856 die->die_child = prev;
22858 return;
22861 if (c != prev->die_sib)
22862 prev->die_sib = c;
22863 prune_unused_types_prune (c);
22864 } while (c != die->die_child);
22867 /* Remove dies representing declarations that we never use. */
22869 static void
22870 prune_unused_types (void)
22872 unsigned int i;
22873 limbo_die_node *node;
22874 comdat_type_node *ctnode;
22875 pubname_ref pub;
22876 dw_die_ref base_type;
22878 #if ENABLE_ASSERT_CHECKING
22879 /* All the marks should already be clear. */
22880 verify_marks_clear (comp_unit_die ());
22881 for (node = limbo_die_list; node; node = node->next)
22882 verify_marks_clear (node->die);
22883 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22884 verify_marks_clear (ctnode->root_die);
22885 #endif /* ENABLE_ASSERT_CHECKING */
22887 /* Mark types that are used in global variables. */
22888 premark_types_used_by_global_vars ();
22890 /* Set the mark on nodes that are actually used. */
22891 prune_unused_types_walk (comp_unit_die ());
22892 for (node = limbo_die_list; node; node = node->next)
22893 prune_unused_types_walk (node->die);
22894 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22896 prune_unused_types_walk (ctnode->root_die);
22897 prune_unused_types_mark (ctnode->type_die, 1);
22900 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22901 are unusual in that they are pubnames that are the children of pubtypes.
22902 They should only be marked via their parent DW_TAG_enumeration_type die,
22903 not as roots in themselves. */
22904 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22905 if (pub->die->die_tag != DW_TAG_enumerator)
22906 prune_unused_types_mark (pub->die, 1);
22907 for (i = 0; base_types.iterate (i, &base_type); i++)
22908 prune_unused_types_mark (base_type, 1);
22910 if (debug_str_hash)
22911 htab_empty (debug_str_hash);
22912 if (skeleton_debug_str_hash)
22913 htab_empty (skeleton_debug_str_hash);
22914 prune_unused_types_prune (comp_unit_die ());
22915 for (node = limbo_die_list; node; node = node->next)
22916 prune_unused_types_prune (node->die);
22917 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22918 prune_unused_types_prune (ctnode->root_die);
22920 /* Leave the marks clear. */
22921 prune_unmark_dies (comp_unit_die ());
22922 for (node = limbo_die_list; node; node = node->next)
22923 prune_unmark_dies (node->die);
22924 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22925 prune_unmark_dies (ctnode->root_die);
22928 /* Set the parameter to true if there are any relative pathnames in
22929 the file table. */
22930 static int
22931 file_table_relative_p (void ** slot, void *param)
22933 bool *p = (bool *) param;
22934 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22935 if (!IS_ABSOLUTE_PATH (d->filename))
22937 *p = true;
22938 return 0;
22940 return 1;
22943 /* Helpers to manipulate hash table of comdat type units. */
22945 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22947 typedef comdat_type_node value_type;
22948 typedef comdat_type_node compare_type;
22949 static inline hashval_t hash (const value_type *);
22950 static inline bool equal (const value_type *, const compare_type *);
22953 inline hashval_t
22954 comdat_type_hasher::hash (const value_type *type_node)
22956 hashval_t h;
22957 memcpy (&h, type_node->signature, sizeof (h));
22958 return h;
22961 inline bool
22962 comdat_type_hasher::equal (const value_type *type_node_1,
22963 const compare_type *type_node_2)
22965 return (! memcmp (type_node_1->signature, type_node_2->signature,
22966 DWARF_TYPE_SIGNATURE_SIZE));
22969 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22970 to the location it would have been added, should we know its
22971 DECL_ASSEMBLER_NAME when we added other attributes. This will
22972 probably improve compactness of debug info, removing equivalent
22973 abbrevs, and hide any differences caused by deferring the
22974 computation of the assembler name, triggered by e.g. PCH. */
22976 static inline void
22977 move_linkage_attr (dw_die_ref die)
22979 unsigned ix = vec_safe_length (die->die_attr);
22980 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22982 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22983 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22985 while (--ix > 0)
22987 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22989 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22990 break;
22993 if (ix != vec_safe_length (die->die_attr) - 1)
22995 die->die_attr->pop ();
22996 die->die_attr->quick_insert (ix, linkage);
23000 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23001 referenced from typed stack ops and count how often they are used. */
23003 static void
23004 mark_base_types (dw_loc_descr_ref loc)
23006 dw_die_ref base_type = NULL;
23008 for (; loc; loc = loc->dw_loc_next)
23010 switch (loc->dw_loc_opc)
23012 case DW_OP_GNU_regval_type:
23013 case DW_OP_GNU_deref_type:
23014 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23015 break;
23016 case DW_OP_GNU_convert:
23017 case DW_OP_GNU_reinterpret:
23018 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23019 continue;
23020 /* FALLTHRU */
23021 case DW_OP_GNU_const_type:
23022 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23023 break;
23024 case DW_OP_GNU_entry_value:
23025 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23026 continue;
23027 default:
23028 continue;
23030 gcc_assert (base_type->die_parent == comp_unit_die ());
23031 if (base_type->die_mark)
23032 base_type->die_mark++;
23033 else
23035 base_types.safe_push (base_type);
23036 base_type->die_mark = 1;
23041 /* Comparison function for sorting marked base types. */
23043 static int
23044 base_type_cmp (const void *x, const void *y)
23046 dw_die_ref dx = *(const dw_die_ref *) x;
23047 dw_die_ref dy = *(const dw_die_ref *) y;
23048 unsigned int byte_size1, byte_size2;
23049 unsigned int encoding1, encoding2;
23050 if (dx->die_mark > dy->die_mark)
23051 return -1;
23052 if (dx->die_mark < dy->die_mark)
23053 return 1;
23054 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23055 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23056 if (byte_size1 < byte_size2)
23057 return 1;
23058 if (byte_size1 > byte_size2)
23059 return -1;
23060 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23061 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23062 if (encoding1 < encoding2)
23063 return 1;
23064 if (encoding1 > encoding2)
23065 return -1;
23066 return 0;
23069 /* Move base types marked by mark_base_types as early as possible
23070 in the CU, sorted by decreasing usage count both to make the
23071 uleb128 references as small as possible and to make sure they
23072 will have die_offset already computed by calc_die_sizes when
23073 sizes of typed stack loc ops is computed. */
23075 static void
23076 move_marked_base_types (void)
23078 unsigned int i;
23079 dw_die_ref base_type, die, c;
23081 if (base_types.is_empty ())
23082 return;
23084 /* Sort by decreasing usage count, they will be added again in that
23085 order later on. */
23086 base_types.qsort (base_type_cmp);
23087 die = comp_unit_die ();
23088 c = die->die_child;
23091 dw_die_ref prev = c;
23092 c = c->die_sib;
23093 while (c->die_mark)
23095 remove_child_with_prev (c, prev);
23096 /* As base types got marked, there must be at least
23097 one node other than DW_TAG_base_type. */
23098 gcc_assert (c != c->die_sib);
23099 c = c->die_sib;
23102 while (c != die->die_child);
23103 gcc_assert (die->die_child);
23104 c = die->die_child;
23105 for (i = 0; base_types.iterate (i, &base_type); i++)
23107 base_type->die_mark = 0;
23108 base_type->die_sib = c->die_sib;
23109 c->die_sib = base_type;
23110 c = base_type;
23114 /* Helper function for resolve_addr, attempt to resolve
23115 one CONST_STRING, return non-zero if not successful. Similarly verify that
23116 SYMBOL_REFs refer to variables emitted in the current CU. */
23118 static int
23119 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
23121 rtx rtl = *addr;
23123 if (GET_CODE (rtl) == CONST_STRING)
23125 size_t len = strlen (XSTR (rtl, 0)) + 1;
23126 tree t = build_string (len, XSTR (rtl, 0));
23127 tree tlen = size_int (len - 1);
23128 TREE_TYPE (t)
23129 = build_array_type (char_type_node, build_index_type (tlen));
23130 rtl = lookup_constant_def (t);
23131 if (!rtl || !MEM_P (rtl))
23132 return 1;
23133 rtl = XEXP (rtl, 0);
23134 if (GET_CODE (rtl) == SYMBOL_REF
23135 && SYMBOL_REF_DECL (rtl)
23136 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23137 return 1;
23138 vec_safe_push (used_rtx_array, rtl);
23139 *addr = rtl;
23140 return 0;
23143 if (GET_CODE (rtl) == SYMBOL_REF
23144 && SYMBOL_REF_DECL (rtl))
23146 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23148 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23149 return 1;
23151 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23152 return 1;
23155 if (GET_CODE (rtl) == CONST
23156 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
23157 return 1;
23159 return 0;
23162 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23163 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23164 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23166 static rtx
23167 string_cst_pool_decl (tree t)
23169 rtx rtl = output_constant_def (t, 1);
23170 unsigned char *array;
23171 dw_loc_descr_ref l;
23172 tree decl;
23173 size_t len;
23174 dw_die_ref ref;
23176 if (!rtl || !MEM_P (rtl))
23177 return NULL_RTX;
23178 rtl = XEXP (rtl, 0);
23179 if (GET_CODE (rtl) != SYMBOL_REF
23180 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23181 return NULL_RTX;
23183 decl = SYMBOL_REF_DECL (rtl);
23184 if (!lookup_decl_die (decl))
23186 len = TREE_STRING_LENGTH (t);
23187 vec_safe_push (used_rtx_array, rtl);
23188 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23189 array = ggc_vec_alloc<unsigned char> (len);
23190 memcpy (array, TREE_STRING_POINTER (t), len);
23191 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23192 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23193 l->dw_loc_oprnd2.v.val_vec.length = len;
23194 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23195 l->dw_loc_oprnd2.v.val_vec.array = array;
23196 add_AT_loc (ref, DW_AT_location, l);
23197 equate_decl_number_to_die (decl, ref);
23199 return rtl;
23202 /* Helper function of resolve_addr_in_expr. LOC is
23203 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23204 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23205 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23206 with DW_OP_GNU_implicit_pointer if possible
23207 and return true, if unsuccessful, return false. */
23209 static bool
23210 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23212 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23213 HOST_WIDE_INT offset = 0;
23214 dw_die_ref ref = NULL;
23215 tree decl;
23217 if (GET_CODE (rtl) == CONST
23218 && GET_CODE (XEXP (rtl, 0)) == PLUS
23219 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23221 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23222 rtl = XEXP (XEXP (rtl, 0), 0);
23224 if (GET_CODE (rtl) == CONST_STRING)
23226 size_t len = strlen (XSTR (rtl, 0)) + 1;
23227 tree t = build_string (len, XSTR (rtl, 0));
23228 tree tlen = size_int (len - 1);
23230 TREE_TYPE (t)
23231 = build_array_type (char_type_node, build_index_type (tlen));
23232 rtl = string_cst_pool_decl (t);
23233 if (!rtl)
23234 return false;
23236 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23238 decl = SYMBOL_REF_DECL (rtl);
23239 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23241 ref = lookup_decl_die (decl);
23242 if (ref && (get_AT (ref, DW_AT_location)
23243 || get_AT (ref, DW_AT_const_value)))
23245 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23246 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23247 loc->dw_loc_oprnd1.val_entry = NULL;
23248 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23249 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23250 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23251 loc->dw_loc_oprnd2.v.val_int = offset;
23252 return true;
23256 return false;
23259 /* Helper function for resolve_addr, handle one location
23260 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23261 the location list couldn't be resolved. */
23263 static bool
23264 resolve_addr_in_expr (dw_loc_descr_ref loc)
23266 dw_loc_descr_ref keep = NULL;
23267 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23268 switch (loc->dw_loc_opc)
23270 case DW_OP_addr:
23271 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23273 if ((prev == NULL
23274 || prev->dw_loc_opc == DW_OP_piece
23275 || prev->dw_loc_opc == DW_OP_bit_piece)
23276 && loc->dw_loc_next
23277 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23278 && !dwarf_strict
23279 && optimize_one_addr_into_implicit_ptr (loc))
23280 break;
23281 return false;
23283 break;
23284 case DW_OP_GNU_addr_index:
23285 case DW_OP_GNU_const_index:
23286 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23287 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23289 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23290 if (resolve_one_addr (&rtl, NULL))
23291 return false;
23292 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23293 loc->dw_loc_oprnd1.val_entry =
23294 add_addr_table_entry (rtl, ate_kind_rtx);
23296 break;
23297 case DW_OP_const4u:
23298 case DW_OP_const8u:
23299 if (loc->dtprel
23300 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23301 return false;
23302 break;
23303 case DW_OP_plus_uconst:
23304 if (size_of_loc_descr (loc)
23305 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23307 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23309 dw_loc_descr_ref repl
23310 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23311 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23312 add_loc_descr (&repl, loc->dw_loc_next);
23313 *loc = *repl;
23315 break;
23316 case DW_OP_implicit_value:
23317 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23318 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23319 return false;
23320 break;
23321 case DW_OP_GNU_implicit_pointer:
23322 case DW_OP_GNU_parameter_ref:
23323 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23325 dw_die_ref ref
23326 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23327 if (ref == NULL)
23328 return false;
23329 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23330 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23331 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23333 break;
23334 case DW_OP_GNU_const_type:
23335 case DW_OP_GNU_regval_type:
23336 case DW_OP_GNU_deref_type:
23337 case DW_OP_GNU_convert:
23338 case DW_OP_GNU_reinterpret:
23339 while (loc->dw_loc_next
23340 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23342 dw_die_ref base1, base2;
23343 unsigned enc1, enc2, size1, size2;
23344 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23345 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23346 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23347 else if (loc->dw_loc_oprnd1.val_class
23348 == dw_val_class_unsigned_const)
23349 break;
23350 else
23351 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23352 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23353 == dw_val_class_unsigned_const)
23354 break;
23355 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23356 gcc_assert (base1->die_tag == DW_TAG_base_type
23357 && base2->die_tag == DW_TAG_base_type);
23358 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23359 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23360 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23361 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23362 if (size1 == size2
23363 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23364 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23365 && loc != keep)
23366 || enc1 == enc2))
23368 /* Optimize away next DW_OP_GNU_convert after
23369 adjusting LOC's base type die reference. */
23370 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23371 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23372 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23373 else
23374 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23375 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23376 continue;
23378 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23379 point typed stack entry. */
23380 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23381 keep = loc->dw_loc_next;
23382 break;
23384 break;
23385 default:
23386 break;
23388 return true;
23391 /* Helper function of resolve_addr. DIE had DW_AT_location of
23392 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23393 and DW_OP_addr couldn't be resolved. resolve_addr has already
23394 removed the DW_AT_location attribute. This function attempts to
23395 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23396 to it or DW_AT_const_value attribute, if possible. */
23398 static void
23399 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23401 if (TREE_CODE (decl) != VAR_DECL
23402 || lookup_decl_die (decl) != die
23403 || DECL_EXTERNAL (decl)
23404 || !TREE_STATIC (decl)
23405 || DECL_INITIAL (decl) == NULL_TREE
23406 || DECL_P (DECL_INITIAL (decl))
23407 || get_AT (die, DW_AT_const_value))
23408 return;
23410 tree init = DECL_INITIAL (decl);
23411 HOST_WIDE_INT offset = 0;
23412 /* For variables that have been optimized away and thus
23413 don't have a memory location, see if we can emit
23414 DW_AT_const_value instead. */
23415 if (tree_add_const_value_attribute (die, init))
23416 return;
23417 if (dwarf_strict)
23418 return;
23419 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23420 and ADDR_EXPR refers to a decl that has DW_AT_location or
23421 DW_AT_const_value (but isn't addressable, otherwise
23422 resolving the original DW_OP_addr wouldn't fail), see if
23423 we can add DW_OP_GNU_implicit_pointer. */
23424 STRIP_NOPS (init);
23425 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23426 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23428 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23429 init = TREE_OPERAND (init, 0);
23430 STRIP_NOPS (init);
23432 if (TREE_CODE (init) != ADDR_EXPR)
23433 return;
23434 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23435 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23436 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23437 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23438 && TREE_OPERAND (init, 0) != decl))
23440 dw_die_ref ref;
23441 dw_loc_descr_ref l;
23443 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23445 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23446 if (!rtl)
23447 return;
23448 decl = SYMBOL_REF_DECL (rtl);
23450 else
23451 decl = TREE_OPERAND (init, 0);
23452 ref = lookup_decl_die (decl);
23453 if (ref == NULL
23454 || (!get_AT (ref, DW_AT_location)
23455 && !get_AT (ref, DW_AT_const_value)))
23456 return;
23457 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23458 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23459 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23460 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23461 add_AT_loc (die, DW_AT_location, l);
23465 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23466 an address in .rodata section if the string literal is emitted there,
23467 or remove the containing location list or replace DW_AT_const_value
23468 with DW_AT_location and empty location expression, if it isn't found
23469 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23470 to something that has been emitted in the current CU. */
23472 static void
23473 resolve_addr (dw_die_ref die)
23475 dw_die_ref c;
23476 dw_attr_ref a;
23477 dw_loc_list_ref *curr, *start, loc;
23478 unsigned ix;
23480 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23481 switch (AT_class (a))
23483 case dw_val_class_loc_list:
23484 start = curr = AT_loc_list_ptr (a);
23485 loc = *curr;
23486 gcc_assert (loc);
23487 /* The same list can be referenced more than once. See if we have
23488 already recorded the result from a previous pass. */
23489 if (loc->replaced)
23490 *curr = loc->dw_loc_next;
23491 else if (!loc->resolved_addr)
23493 /* As things stand, we do not expect or allow one die to
23494 reference a suffix of another die's location list chain.
23495 References must be identical or completely separate.
23496 There is therefore no need to cache the result of this
23497 pass on any list other than the first; doing so
23498 would lead to unnecessary writes. */
23499 while (*curr)
23501 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23502 if (!resolve_addr_in_expr ((*curr)->expr))
23504 dw_loc_list_ref next = (*curr)->dw_loc_next;
23505 dw_loc_descr_ref l = (*curr)->expr;
23507 if (next && (*curr)->ll_symbol)
23509 gcc_assert (!next->ll_symbol);
23510 next->ll_symbol = (*curr)->ll_symbol;
23512 if (dwarf_split_debug_info)
23513 remove_loc_list_addr_table_entries (l);
23514 *curr = next;
23516 else
23518 mark_base_types ((*curr)->expr);
23519 curr = &(*curr)->dw_loc_next;
23522 if (loc == *start)
23523 loc->resolved_addr = 1;
23524 else
23526 loc->replaced = 1;
23527 loc->dw_loc_next = *start;
23530 if (!*start)
23532 remove_AT (die, a->dw_attr);
23533 ix--;
23535 break;
23536 case dw_val_class_loc:
23538 dw_loc_descr_ref l = AT_loc (a);
23539 /* For -gdwarf-2 don't attempt to optimize
23540 DW_AT_data_member_location containing
23541 DW_OP_plus_uconst - older consumers might
23542 rely on it being that op instead of a more complex,
23543 but shorter, location description. */
23544 if ((dwarf_version > 2
23545 || a->dw_attr != DW_AT_data_member_location
23546 || l == NULL
23547 || l->dw_loc_opc != DW_OP_plus_uconst
23548 || l->dw_loc_next != NULL)
23549 && !resolve_addr_in_expr (l))
23551 if (dwarf_split_debug_info)
23552 remove_loc_list_addr_table_entries (l);
23553 if (l != NULL
23554 && l->dw_loc_next == NULL
23555 && l->dw_loc_opc == DW_OP_addr
23556 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23557 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23558 && a->dw_attr == DW_AT_location)
23560 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23561 remove_AT (die, a->dw_attr);
23562 ix--;
23563 optimize_location_into_implicit_ptr (die, decl);
23564 break;
23566 remove_AT (die, a->dw_attr);
23567 ix--;
23569 else
23570 mark_base_types (l);
23572 break;
23573 case dw_val_class_addr:
23574 if (a->dw_attr == DW_AT_const_value
23575 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23577 if (AT_index (a) != NOT_INDEXED)
23578 remove_addr_table_entry (a->dw_attr_val.val_entry);
23579 remove_AT (die, a->dw_attr);
23580 ix--;
23582 if (die->die_tag == DW_TAG_GNU_call_site
23583 && a->dw_attr == DW_AT_abstract_origin)
23585 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23586 dw_die_ref tdie = lookup_decl_die (tdecl);
23587 if (tdie == NULL
23588 && DECL_EXTERNAL (tdecl)
23589 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23591 force_decl_die (tdecl);
23592 tdie = lookup_decl_die (tdecl);
23594 if (tdie)
23596 a->dw_attr_val.val_class = dw_val_class_die_ref;
23597 a->dw_attr_val.v.val_die_ref.die = tdie;
23598 a->dw_attr_val.v.val_die_ref.external = 0;
23600 else
23602 if (AT_index (a) != NOT_INDEXED)
23603 remove_addr_table_entry (a->dw_attr_val.val_entry);
23604 remove_AT (die, a->dw_attr);
23605 ix--;
23608 break;
23609 default:
23610 break;
23613 FOR_EACH_CHILD (die, c, resolve_addr (c));
23616 /* Helper routines for optimize_location_lists.
23617 This pass tries to share identical local lists in .debug_loc
23618 section. */
23620 /* Iteratively hash operands of LOC opcode into HSTATE. */
23622 static void
23623 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23625 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23626 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23628 switch (loc->dw_loc_opc)
23630 case DW_OP_const4u:
23631 case DW_OP_const8u:
23632 if (loc->dtprel)
23633 goto hash_addr;
23634 /* FALLTHRU */
23635 case DW_OP_const1u:
23636 case DW_OP_const1s:
23637 case DW_OP_const2u:
23638 case DW_OP_const2s:
23639 case DW_OP_const4s:
23640 case DW_OP_const8s:
23641 case DW_OP_constu:
23642 case DW_OP_consts:
23643 case DW_OP_pick:
23644 case DW_OP_plus_uconst:
23645 case DW_OP_breg0:
23646 case DW_OP_breg1:
23647 case DW_OP_breg2:
23648 case DW_OP_breg3:
23649 case DW_OP_breg4:
23650 case DW_OP_breg5:
23651 case DW_OP_breg6:
23652 case DW_OP_breg7:
23653 case DW_OP_breg8:
23654 case DW_OP_breg9:
23655 case DW_OP_breg10:
23656 case DW_OP_breg11:
23657 case DW_OP_breg12:
23658 case DW_OP_breg13:
23659 case DW_OP_breg14:
23660 case DW_OP_breg15:
23661 case DW_OP_breg16:
23662 case DW_OP_breg17:
23663 case DW_OP_breg18:
23664 case DW_OP_breg19:
23665 case DW_OP_breg20:
23666 case DW_OP_breg21:
23667 case DW_OP_breg22:
23668 case DW_OP_breg23:
23669 case DW_OP_breg24:
23670 case DW_OP_breg25:
23671 case DW_OP_breg26:
23672 case DW_OP_breg27:
23673 case DW_OP_breg28:
23674 case DW_OP_breg29:
23675 case DW_OP_breg30:
23676 case DW_OP_breg31:
23677 case DW_OP_regx:
23678 case DW_OP_fbreg:
23679 case DW_OP_piece:
23680 case DW_OP_deref_size:
23681 case DW_OP_xderef_size:
23682 hstate.add_object (val1->v.val_int);
23683 break;
23684 case DW_OP_skip:
23685 case DW_OP_bra:
23687 int offset;
23689 gcc_assert (val1->val_class == dw_val_class_loc);
23690 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23691 hstate.add_object (offset);
23693 break;
23694 case DW_OP_implicit_value:
23695 hstate.add_object (val1->v.val_unsigned);
23696 switch (val2->val_class)
23698 case dw_val_class_const:
23699 hstate.add_object (val2->v.val_int);
23700 break;
23701 case dw_val_class_vec:
23703 unsigned int elt_size = val2->v.val_vec.elt_size;
23704 unsigned int len = val2->v.val_vec.length;
23706 hstate.add_int (elt_size);
23707 hstate.add_int (len);
23708 hstate.add (val2->v.val_vec.array, len * elt_size);
23710 break;
23711 case dw_val_class_const_double:
23712 hstate.add_object (val2->v.val_double.low);
23713 hstate.add_object (val2->v.val_double.high);
23714 break;
23715 case dw_val_class_wide_int:
23716 hstate.add_object (*val2->v.val_wide);
23717 break;
23718 case dw_val_class_addr:
23719 inchash::add_rtx (val2->v.val_addr, hstate);
23720 break;
23721 default:
23722 gcc_unreachable ();
23724 break;
23725 case DW_OP_bregx:
23726 case DW_OP_bit_piece:
23727 hstate.add_object (val1->v.val_int);
23728 hstate.add_object (val2->v.val_int);
23729 break;
23730 case DW_OP_addr:
23731 hash_addr:
23732 if (loc->dtprel)
23734 unsigned char dtprel = 0xd1;
23735 hstate.add_object (dtprel);
23737 inchash::add_rtx (val1->v.val_addr, hstate);
23738 break;
23739 case DW_OP_GNU_addr_index:
23740 case DW_OP_GNU_const_index:
23742 if (loc->dtprel)
23744 unsigned char dtprel = 0xd1;
23745 hstate.add_object (dtprel);
23747 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
23749 break;
23750 case DW_OP_GNU_implicit_pointer:
23751 hstate.add_int (val2->v.val_int);
23752 break;
23753 case DW_OP_GNU_entry_value:
23754 hstate.add_object (val1->v.val_loc);
23755 break;
23756 case DW_OP_GNU_regval_type:
23757 case DW_OP_GNU_deref_type:
23759 unsigned int byte_size
23760 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23761 unsigned int encoding
23762 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23763 hstate.add_object (val1->v.val_int);
23764 hstate.add_object (byte_size);
23765 hstate.add_object (encoding);
23767 break;
23768 case DW_OP_GNU_convert:
23769 case DW_OP_GNU_reinterpret:
23770 if (val1->val_class == dw_val_class_unsigned_const)
23772 hstate.add_object (val1->v.val_unsigned);
23773 break;
23775 /* FALLTHRU */
23776 case DW_OP_GNU_const_type:
23778 unsigned int byte_size
23779 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23780 unsigned int encoding
23781 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23782 hstate.add_object (byte_size);
23783 hstate.add_object (encoding);
23784 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23785 break;
23786 hstate.add_object (val2->val_class);
23787 switch (val2->val_class)
23789 case dw_val_class_const:
23790 hstate.add_object (val2->v.val_int);
23791 break;
23792 case dw_val_class_vec:
23794 unsigned int elt_size = val2->v.val_vec.elt_size;
23795 unsigned int len = val2->v.val_vec.length;
23797 hstate.add_object (elt_size);
23798 hstate.add_object (len);
23799 hstate.add (val2->v.val_vec.array, len * elt_size);
23801 break;
23802 case dw_val_class_const_double:
23803 hstate.add_object (val2->v.val_double.low);
23804 hstate.add_object (val2->v.val_double.high);
23805 break;
23806 case dw_val_class_wide_int:
23807 hstate.add_object (*val2->v.val_wide);
23808 break;
23809 default:
23810 gcc_unreachable ();
23813 break;
23815 default:
23816 /* Other codes have no operands. */
23817 break;
23821 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
23823 static inline void
23824 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
23826 dw_loc_descr_ref l;
23827 bool sizes_computed = false;
23828 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23829 size_of_locs (loc);
23831 for (l = loc; l != NULL; l = l->dw_loc_next)
23833 enum dwarf_location_atom opc = l->dw_loc_opc;
23834 hstate.add_object (opc);
23835 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23837 size_of_locs (loc);
23838 sizes_computed = true;
23840 hash_loc_operands (l, hstate);
23844 /* Compute hash of the whole location list LIST_HEAD. */
23846 static inline void
23847 hash_loc_list (dw_loc_list_ref list_head)
23849 dw_loc_list_ref curr = list_head;
23850 inchash::hash hstate;
23852 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23854 hstate.add (curr->begin, strlen (curr->begin) + 1);
23855 hstate.add (curr->end, strlen (curr->end) + 1);
23856 if (curr->section)
23857 hstate.add (curr->section, strlen (curr->section) + 1);
23858 hash_locs (curr->expr, hstate);
23860 list_head->hash = hstate.end ();
23863 /* Return true if X and Y opcodes have the same operands. */
23865 static inline bool
23866 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23868 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23869 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23870 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23871 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23873 switch (x->dw_loc_opc)
23875 case DW_OP_const4u:
23876 case DW_OP_const8u:
23877 if (x->dtprel)
23878 goto hash_addr;
23879 /* FALLTHRU */
23880 case DW_OP_const1u:
23881 case DW_OP_const1s:
23882 case DW_OP_const2u:
23883 case DW_OP_const2s:
23884 case DW_OP_const4s:
23885 case DW_OP_const8s:
23886 case DW_OP_constu:
23887 case DW_OP_consts:
23888 case DW_OP_pick:
23889 case DW_OP_plus_uconst:
23890 case DW_OP_breg0:
23891 case DW_OP_breg1:
23892 case DW_OP_breg2:
23893 case DW_OP_breg3:
23894 case DW_OP_breg4:
23895 case DW_OP_breg5:
23896 case DW_OP_breg6:
23897 case DW_OP_breg7:
23898 case DW_OP_breg8:
23899 case DW_OP_breg9:
23900 case DW_OP_breg10:
23901 case DW_OP_breg11:
23902 case DW_OP_breg12:
23903 case DW_OP_breg13:
23904 case DW_OP_breg14:
23905 case DW_OP_breg15:
23906 case DW_OP_breg16:
23907 case DW_OP_breg17:
23908 case DW_OP_breg18:
23909 case DW_OP_breg19:
23910 case DW_OP_breg20:
23911 case DW_OP_breg21:
23912 case DW_OP_breg22:
23913 case DW_OP_breg23:
23914 case DW_OP_breg24:
23915 case DW_OP_breg25:
23916 case DW_OP_breg26:
23917 case DW_OP_breg27:
23918 case DW_OP_breg28:
23919 case DW_OP_breg29:
23920 case DW_OP_breg30:
23921 case DW_OP_breg31:
23922 case DW_OP_regx:
23923 case DW_OP_fbreg:
23924 case DW_OP_piece:
23925 case DW_OP_deref_size:
23926 case DW_OP_xderef_size:
23927 return valx1->v.val_int == valy1->v.val_int;
23928 case DW_OP_skip:
23929 case DW_OP_bra:
23930 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23931 can cause irrelevant differences in dw_loc_addr. */
23932 gcc_assert (valx1->val_class == dw_val_class_loc
23933 && valy1->val_class == dw_val_class_loc
23934 && (dwarf_split_debug_info
23935 || x->dw_loc_addr == y->dw_loc_addr));
23936 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23937 case DW_OP_implicit_value:
23938 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23939 || valx2->val_class != valy2->val_class)
23940 return false;
23941 switch (valx2->val_class)
23943 case dw_val_class_const:
23944 return valx2->v.val_int == valy2->v.val_int;
23945 case dw_val_class_vec:
23946 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23947 && valx2->v.val_vec.length == valy2->v.val_vec.length
23948 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23949 valx2->v.val_vec.elt_size
23950 * valx2->v.val_vec.length) == 0;
23951 case dw_val_class_const_double:
23952 return valx2->v.val_double.low == valy2->v.val_double.low
23953 && valx2->v.val_double.high == valy2->v.val_double.high;
23954 case dw_val_class_wide_int:
23955 return *valx2->v.val_wide == *valy2->v.val_wide;
23956 case dw_val_class_addr:
23957 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23958 default:
23959 gcc_unreachable ();
23961 case DW_OP_bregx:
23962 case DW_OP_bit_piece:
23963 return valx1->v.val_int == valy1->v.val_int
23964 && valx2->v.val_int == valy2->v.val_int;
23965 case DW_OP_addr:
23966 hash_addr:
23967 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23968 case DW_OP_GNU_addr_index:
23969 case DW_OP_GNU_const_index:
23971 rtx ax1 = valx1->val_entry->addr.rtl;
23972 rtx ay1 = valy1->val_entry->addr.rtl;
23973 return rtx_equal_p (ax1, ay1);
23975 case DW_OP_GNU_implicit_pointer:
23976 return valx1->val_class == dw_val_class_die_ref
23977 && valx1->val_class == valy1->val_class
23978 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23979 && valx2->v.val_int == valy2->v.val_int;
23980 case DW_OP_GNU_entry_value:
23981 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23982 case DW_OP_GNU_const_type:
23983 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23984 || valx2->val_class != valy2->val_class)
23985 return false;
23986 switch (valx2->val_class)
23988 case dw_val_class_const:
23989 return valx2->v.val_int == valy2->v.val_int;
23990 case dw_val_class_vec:
23991 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23992 && valx2->v.val_vec.length == valy2->v.val_vec.length
23993 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23994 valx2->v.val_vec.elt_size
23995 * valx2->v.val_vec.length) == 0;
23996 case dw_val_class_const_double:
23997 return valx2->v.val_double.low == valy2->v.val_double.low
23998 && valx2->v.val_double.high == valy2->v.val_double.high;
23999 case dw_val_class_wide_int:
24000 return *valx2->v.val_wide == *valy2->v.val_wide;
24001 default:
24002 gcc_unreachable ();
24004 case DW_OP_GNU_regval_type:
24005 case DW_OP_GNU_deref_type:
24006 return valx1->v.val_int == valy1->v.val_int
24007 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24008 case DW_OP_GNU_convert:
24009 case DW_OP_GNU_reinterpret:
24010 if (valx1->val_class != valy1->val_class)
24011 return false;
24012 if (valx1->val_class == dw_val_class_unsigned_const)
24013 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24014 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24015 case DW_OP_GNU_parameter_ref:
24016 return valx1->val_class == dw_val_class_die_ref
24017 && valx1->val_class == valy1->val_class
24018 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24019 default:
24020 /* Other codes have no operands. */
24021 return true;
24025 /* Return true if DWARF location expressions X and Y are the same. */
24027 static inline bool
24028 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24030 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24031 if (x->dw_loc_opc != y->dw_loc_opc
24032 || x->dtprel != y->dtprel
24033 || !compare_loc_operands (x, y))
24034 break;
24035 return x == NULL && y == NULL;
24038 /* Hashtable helpers. */
24040 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24042 typedef dw_loc_list_struct value_type;
24043 typedef dw_loc_list_struct compare_type;
24044 static inline hashval_t hash (const value_type *);
24045 static inline bool equal (const value_type *, const compare_type *);
24048 /* Return precomputed hash of location list X. */
24050 inline hashval_t
24051 loc_list_hasher::hash (const value_type *x)
24053 return x->hash;
24056 /* Return true if location lists A and B are the same. */
24058 inline bool
24059 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24061 if (a == b)
24062 return 1;
24063 if (a->hash != b->hash)
24064 return 0;
24065 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24066 if (strcmp (a->begin, b->begin) != 0
24067 || strcmp (a->end, b->end) != 0
24068 || (a->section == NULL) != (b->section == NULL)
24069 || (a->section && strcmp (a->section, b->section) != 0)
24070 || !compare_locs (a->expr, b->expr))
24071 break;
24072 return a == NULL && b == NULL;
24075 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24078 /* Recursively optimize location lists referenced from DIE
24079 children and share them whenever possible. */
24081 static void
24082 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24084 dw_die_ref c;
24085 dw_attr_ref a;
24086 unsigned ix;
24087 dw_loc_list_struct **slot;
24089 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24090 if (AT_class (a) == dw_val_class_loc_list)
24092 dw_loc_list_ref list = AT_loc_list (a);
24093 /* TODO: perform some optimizations here, before hashing
24094 it and storing into the hash table. */
24095 hash_loc_list (list);
24096 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24097 if (*slot == NULL)
24098 *slot = list;
24099 else
24100 a->dw_attr_val.v.val_loc_list = *slot;
24103 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24107 /* Recursively assign each location list a unique index into the debug_addr
24108 section. */
24110 static void
24111 index_location_lists (dw_die_ref die)
24113 dw_die_ref c;
24114 dw_attr_ref a;
24115 unsigned ix;
24117 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24118 if (AT_class (a) == dw_val_class_loc_list)
24120 dw_loc_list_ref list = AT_loc_list (a);
24121 dw_loc_list_ref curr;
24122 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24124 /* Don't index an entry that has already been indexed
24125 or won't be output. */
24126 if (curr->begin_entry != NULL
24127 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24128 continue;
24130 curr->begin_entry
24131 = add_addr_table_entry (xstrdup (curr->begin),
24132 ate_kind_label);
24136 FOR_EACH_CHILD (die, c, index_location_lists (c));
24139 /* Optimize location lists referenced from DIE
24140 children and share them whenever possible. */
24142 static void
24143 optimize_location_lists (dw_die_ref die)
24145 loc_list_hash_type htab (500);
24146 optimize_location_lists_1 (die, &htab);
24149 /* Output stuff that dwarf requires at the end of every file,
24150 and generate the DWARF-2 debugging info. */
24152 static void
24153 dwarf2out_finish (const char *filename)
24155 limbo_die_node *node, *next_node;
24156 comdat_type_node *ctnode;
24157 unsigned int i;
24158 dw_die_ref main_comp_unit_die;
24160 /* PCH might result in DW_AT_producer string being restored from the
24161 header compilation, so always fill it with empty string initially
24162 and overwrite only here. */
24163 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24164 producer_string = gen_producer_string ();
24165 producer->dw_attr_val.v.val_str->refcount--;
24166 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24168 gen_scheduled_generic_parms_dies ();
24169 gen_remaining_tmpl_value_param_die_attribute ();
24171 /* Add the name for the main input file now. We delayed this from
24172 dwarf2out_init to avoid complications with PCH. */
24173 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24174 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24175 add_comp_dir_attribute (comp_unit_die ());
24176 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24178 bool p = false;
24179 htab_traverse (file_table, file_table_relative_p, &p);
24180 if (p)
24181 add_comp_dir_attribute (comp_unit_die ());
24184 if (deferred_locations_list)
24185 for (i = 0; i < deferred_locations_list->length (); i++)
24187 add_location_or_const_value_attribute (
24188 (*deferred_locations_list)[i].die,
24189 (*deferred_locations_list)[i].variable,
24190 false,
24191 DW_AT_location);
24194 /* Traverse the limbo die list, and add parent/child links. The only
24195 dies without parents that should be here are concrete instances of
24196 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24197 For concrete instances, we can get the parent die from the abstract
24198 instance. */
24199 for (node = limbo_die_list; node; node = next_node)
24201 dw_die_ref die = node->die;
24202 next_node = node->next;
24204 if (die->die_parent == NULL)
24206 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24208 if (origin && origin->die_parent)
24209 add_child_die (origin->die_parent, die);
24210 else if (is_cu_die (die))
24212 else if (seen_error ())
24213 /* It's OK to be confused by errors in the input. */
24214 add_child_die (comp_unit_die (), die);
24215 else
24217 /* In certain situations, the lexical block containing a
24218 nested function can be optimized away, which results
24219 in the nested function die being orphaned. Likewise
24220 with the return type of that nested function. Force
24221 this to be a child of the containing function.
24223 It may happen that even the containing function got fully
24224 inlined and optimized out. In that case we are lost and
24225 assign the empty child. This should not be big issue as
24226 the function is likely unreachable too. */
24227 gcc_assert (node->created_for);
24229 if (DECL_P (node->created_for))
24230 origin = get_context_die (DECL_CONTEXT (node->created_for));
24231 else if (TYPE_P (node->created_for))
24232 origin = scope_die_for (node->created_for, comp_unit_die ());
24233 else
24234 origin = comp_unit_die ();
24236 add_child_die (origin, die);
24241 limbo_die_list = NULL;
24243 #if ENABLE_ASSERT_CHECKING
24245 dw_die_ref die = comp_unit_die (), c;
24246 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24248 #endif
24249 resolve_addr (comp_unit_die ());
24250 move_marked_base_types ();
24252 for (node = deferred_asm_name; node; node = node->next)
24254 tree decl = node->created_for;
24255 /* When generating LTO bytecode we can not generate new assembler
24256 names at this point and all important decls got theirs via
24257 free-lang-data. */
24258 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24259 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24261 add_linkage_attr (node->die, decl);
24262 move_linkage_attr (node->die);
24266 deferred_asm_name = NULL;
24268 /* Walk through the list of incomplete types again, trying once more to
24269 emit full debugging info for them. */
24270 retry_incomplete_types ();
24272 if (flag_eliminate_unused_debug_types)
24273 prune_unused_types ();
24275 /* Generate separate COMDAT sections for type DIEs. */
24276 if (use_debug_types)
24278 break_out_comdat_types (comp_unit_die ());
24280 /* Each new type_unit DIE was added to the limbo die list when created.
24281 Since these have all been added to comdat_type_list, clear the
24282 limbo die list. */
24283 limbo_die_list = NULL;
24285 /* For each new comdat type unit, copy declarations for incomplete
24286 types to make the new unit self-contained (i.e., no direct
24287 references to the main compile unit). */
24288 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24289 copy_decls_for_unworthy_types (ctnode->root_die);
24290 copy_decls_for_unworthy_types (comp_unit_die ());
24292 /* In the process of copying declarations from one unit to another,
24293 we may have left some declarations behind that are no longer
24294 referenced. Prune them. */
24295 prune_unused_types ();
24298 /* Generate separate CUs for each of the include files we've seen.
24299 They will go into limbo_die_list. */
24300 if (flag_eliminate_dwarf2_dups)
24301 break_out_includes (comp_unit_die ());
24303 /* Traverse the DIE's and add add sibling attributes to those DIE's
24304 that have children. */
24305 add_sibling_attributes (comp_unit_die ());
24306 for (node = limbo_die_list; node; node = node->next)
24307 add_sibling_attributes (node->die);
24308 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24309 add_sibling_attributes (ctnode->root_die);
24311 /* When splitting DWARF info, we put some attributes in the
24312 skeleton compile_unit DIE that remains in the .o, while
24313 most attributes go in the DWO compile_unit_die. */
24314 if (dwarf_split_debug_info)
24315 main_comp_unit_die = gen_compile_unit_die (NULL);
24316 else
24317 main_comp_unit_die = comp_unit_die ();
24319 /* Output a terminator label for the .text section. */
24320 switch_to_section (text_section);
24321 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24322 if (cold_text_section)
24324 switch_to_section (cold_text_section);
24325 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24328 /* We can only use the low/high_pc attributes if all of the code was
24329 in .text. */
24330 if (!have_multiple_function_sections
24331 || (dwarf_version < 3 && dwarf_strict))
24333 /* Don't add if the CU has no associated code. */
24334 if (text_section_used)
24335 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24336 text_end_label, true);
24338 else
24340 unsigned fde_idx;
24341 dw_fde_ref fde;
24342 bool range_list_added = false;
24344 if (text_section_used)
24345 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24346 text_end_label, &range_list_added, true);
24347 if (cold_text_section_used)
24348 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24349 cold_end_label, &range_list_added, true);
24351 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24353 if (DECL_IGNORED_P (fde->decl))
24354 continue;
24355 if (!fde->in_std_section)
24356 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24357 fde->dw_fde_end, &range_list_added,
24358 true);
24359 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24360 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24361 fde->dw_fde_second_end, &range_list_added,
24362 true);
24365 if (range_list_added)
24367 /* We need to give .debug_loc and .debug_ranges an appropriate
24368 "base address". Use zero so that these addresses become
24369 absolute. Historically, we've emitted the unexpected
24370 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24371 Emit both to give time for other tools to adapt. */
24372 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24373 if (! dwarf_strict && dwarf_version < 4)
24374 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24376 add_ranges (NULL);
24380 if (debug_info_level >= DINFO_LEVEL_TERSE)
24381 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24382 debug_line_section_label);
24384 if (have_macinfo)
24385 add_AT_macptr (comp_unit_die (),
24386 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24387 macinfo_section_label);
24389 if (dwarf_split_debug_info)
24391 /* optimize_location_lists calculates the size of the lists,
24392 so index them first, and assign indices to the entries.
24393 Although optimize_location_lists will remove entries from
24394 the table, it only does so for duplicates, and therefore
24395 only reduces ref_counts to 1. */
24396 index_location_lists (comp_unit_die ());
24398 if (addr_index_table != NULL)
24400 unsigned int index = 0;
24401 htab_traverse_noresize (addr_index_table,
24402 index_addr_table_entry, &index);
24406 if (have_location_lists)
24407 optimize_location_lists (comp_unit_die ());
24409 save_macinfo_strings ();
24411 if (dwarf_split_debug_info)
24413 unsigned int index = 0;
24415 /* Add attributes common to skeleton compile_units and
24416 type_units. Because these attributes include strings, it
24417 must be done before freezing the string table. Top-level
24418 skeleton die attrs are added when the skeleton type unit is
24419 created, so ensure it is created by this point. */
24420 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24421 htab_traverse_noresize (debug_str_hash, index_string, &index);
24424 /* Output all of the compilation units. We put the main one last so that
24425 the offsets are available to output_pubnames. */
24426 for (node = limbo_die_list; node; node = node->next)
24427 output_comp_unit (node->die, 0);
24429 hash_table<comdat_type_hasher> comdat_type_table (100);
24430 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24432 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24434 /* Don't output duplicate types. */
24435 if (*slot != HTAB_EMPTY_ENTRY)
24436 continue;
24438 /* Add a pointer to the line table for the main compilation unit
24439 so that the debugger can make sense of DW_AT_decl_file
24440 attributes. */
24441 if (debug_info_level >= DINFO_LEVEL_TERSE)
24442 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24443 (!dwarf_split_debug_info
24444 ? debug_line_section_label
24445 : debug_skeleton_line_section_label));
24447 output_comdat_type_unit (ctnode);
24448 *slot = ctnode;
24451 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24452 both the main_cu and all skeleton TUs. Making this call unconditional
24453 would end up either adding a second copy of the AT_pubnames attribute, or
24454 requiring a special case in add_top_level_skeleton_die_attrs. */
24455 if (!dwarf_split_debug_info)
24456 add_AT_pubnames (comp_unit_die ());
24458 if (dwarf_split_debug_info)
24460 int mark;
24461 unsigned char checksum[16];
24462 struct md5_ctx ctx;
24464 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24465 md5_init_ctx (&ctx);
24466 mark = 0;
24467 die_checksum (comp_unit_die (), &ctx, &mark);
24468 unmark_all_dies (comp_unit_die ());
24469 md5_finish_ctx (&ctx, checksum);
24471 /* Use the first 8 bytes of the checksum as the dwo_id,
24472 and add it to both comp-unit DIEs. */
24473 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24474 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24476 /* Add the base offset of the ranges table to the skeleton
24477 comp-unit DIE. */
24478 if (ranges_table_in_use)
24479 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24480 ranges_section_label);
24482 switch_to_section (debug_addr_section);
24483 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24484 output_addr_table ();
24487 /* Output the main compilation unit if non-empty or if .debug_macinfo
24488 or .debug_macro will be emitted. */
24489 output_comp_unit (comp_unit_die (), have_macinfo);
24491 if (dwarf_split_debug_info && info_section_emitted)
24492 output_skeleton_debug_sections (main_comp_unit_die);
24494 /* Output the abbreviation table. */
24495 if (abbrev_die_table_in_use != 1)
24497 switch_to_section (debug_abbrev_section);
24498 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24499 output_abbrev_section ();
24502 /* Output location list section if necessary. */
24503 if (have_location_lists)
24505 /* Output the location lists info. */
24506 switch_to_section (debug_loc_section);
24507 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24508 output_location_lists (comp_unit_die ());
24511 output_pubtables ();
24513 /* Output the address range information if a CU (.debug_info section)
24514 was emitted. We output an empty table even if we had no functions
24515 to put in it. This because the consumer has no way to tell the
24516 difference between an empty table that we omitted and failure to
24517 generate a table that would have contained data. */
24518 if (info_section_emitted)
24520 unsigned long aranges_length = size_of_aranges ();
24522 switch_to_section (debug_aranges_section);
24523 output_aranges (aranges_length);
24526 /* Output ranges section if necessary. */
24527 if (ranges_table_in_use)
24529 switch_to_section (debug_ranges_section);
24530 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24531 output_ranges ();
24534 /* Have to end the macro section. */
24535 if (have_macinfo)
24537 switch_to_section (debug_macinfo_section);
24538 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24539 output_macinfo ();
24540 dw2_asm_output_data (1, 0, "End compilation unit");
24543 /* Output the source line correspondence table. We must do this
24544 even if there is no line information. Otherwise, on an empty
24545 translation unit, we will generate a present, but empty,
24546 .debug_info section. IRIX 6.5 `nm' will then complain when
24547 examining the file. This is done late so that any filenames
24548 used by the debug_info section are marked as 'used'. */
24549 switch_to_section (debug_line_section);
24550 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24551 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24552 output_line_info (false);
24554 if (dwarf_split_debug_info && info_section_emitted)
24556 switch_to_section (debug_skeleton_line_section);
24557 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24558 output_line_info (true);
24561 /* If we emitted any indirect strings, output the string table too. */
24562 if (debug_str_hash || skeleton_debug_str_hash)
24563 output_indirect_strings ();
24566 #include "gt-dwarf2out.h"